1 #include <msp/gl/extensions/arb_texture_storage.h>
2 #include <msp/gl/extensions/msp_texture1d.h>
12 Texture1D::Texture1D():
13 Texture(GL_TEXTURE_1D),
18 static Require _req(MSP_texture1D);
21 void Texture1D::storage(PixelFormat fmt, unsigned wd)
24 throw invalid_operation("Texture1D::storage");
26 throw invalid_argument("Texture1D::storage");
28 if(MSP_sized_internal_formats)
29 fmt = get_sized_pixelformat(fmt);
30 require_pixelformat(fmt);
36 void Texture1D::allocate(unsigned level)
38 if(allocated&(1<<level))
41 if(ARB_texture_storage)
43 BindRestore _bind(this);
44 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
45 glTexStorage1D(target, n_levels, ifmt, width);
46 allocated |= (1<<n_levels)-1;
50 PixelFormat base_fmt = get_base_pixelformat(ifmt);
51 image(level, base_fmt, get_alloc_type(base_fmt), 0);
55 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
58 throw invalid_operation("Texture1D::image");
60 unsigned w = get_level_size(level);
62 BindRestore _bind(this);
63 if(ARB_texture_storage)
64 return sub_image(level, 0, w, fmt, type, data);
66 glTexImage1D(target, level, ifmt, w, 0, fmt, type, data);
68 allocated |= 1<<level;
69 if(gen_mipmap && level==0)
71 auto_generate_mipmap();
72 allocated |= (1<<get_n_levels())-1;
76 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
79 throw invalid_operation("Texture3D::image");
83 BindRestore _bind(this);
84 glTexSubImage1D(target, level, x, wd, fmt, type, data);
86 if(gen_mipmap && level==0)
87 auto_generate_mipmap();
90 void Texture1D::image(const Graphics::Image &img, bool srgb)
92 if(img.get_height()!=1)
93 throw incompatible_data("Texture1D::image");
95 unsigned w = img.get_width();
96 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
98 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
100 throw incompatible_data("Texture1D::image");
102 image(0, fmt, UNSIGNED_BYTE, img.get_data());
105 unsigned Texture1D::get_n_levels() const
108 for(unsigned s=width; s; s>>=1, ++n) ;
112 unsigned Texture1D::get_level_size(unsigned level) const
117 UInt64 Texture1D::get_data_size() const
119 return id ? width*get_pixel_size(ifmt) : 0;
123 Texture1D::Loader::Loader(Texture1D &t):
124 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
129 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
130 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
135 void Texture1D::Loader::init()
137 add("raw_data", &Loader::raw_data);
138 add("storage", &Loader::storage);
141 void Texture1D::Loader::raw_data(const string &data)
143 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
146 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
149 fmt = get_srgb_pixelformat(fmt);