1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/gl/extensions/msp_texture1d.h>
14 Texture1D::Texture1D():
15 Texture(GL_TEXTURE_1D),
19 static Require _req(MSP_texture1D);
22 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
26 if(fmt!=format || wd!=width || (lv && lv!=levels))
27 throw incompatible_data("Texture1D::storage");
31 throw invalid_argument("Texture1D::storage");
35 levels = get_n_levels();
37 levels = min(levels, lv);
40 void Texture1D::allocate(unsigned level)
43 throw invalid_operation("Texture1D::allocate");
45 throw invalid_argument("Texture1D::allocate");
46 if(allocated&(1<<level))
49 if(ARB_texture_storage)
51 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
52 if(ARB_direct_state_access)
53 glTextureStorage1D(id, levels, storage_fmt, width);
55 glTexStorage1D(target, levels, storage_fmt, width);
57 allocated |= (1<<levels)-1;
63 void Texture1D::image(unsigned level, const void *data)
66 throw invalid_operation("Texture1D::image");
68 unsigned w = get_level_size(level);
70 if(ARB_texture_storage)
71 return sub_image(level, 0, w, data);
73 BindRestore _bind(this);
77 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
81 PixelComponents comp = get_components(storage_fmt);
82 GLenum type = get_gl_type(get_component_type(storage_fmt));
83 glTexImage1D(target, level, storage_fmt, w, 0, comp, type, data);
85 allocated |= 1<<level;
86 if(auto_gen_mipmap && level==0)
89 allocated |= (1<<levels)-1;
93 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
95 if(comp!=get_components(format) || type!=get_component_type(format))
96 throw incompatible_data("Texture1D::image");
100 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
103 throw invalid_operation("Texture3D::image");
105 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
108 PixelComponents comp = get_components(storage_fmt);
109 GLenum type = get_gl_type(get_component_type(storage_fmt));
110 if(ARB_direct_state_access)
111 glTextureSubImage1D(id, level, x, wd, comp, type, data);
113 glTexSubImage1D(target, level, x, wd, comp, type, data);
115 if(auto_gen_mipmap && level==0)
119 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
121 if(comp!=get_components(format) || type!=get_component_type(format))
122 throw incompatible_data("Texture1D::sub_image");
123 sub_image(level, x, wd, data);
126 void Texture1D::image(const Graphics::Image &img, unsigned lv)
128 if(img.get_height()!=1)
129 throw incompatible_data("Texture1D::image");
131 unsigned w = img.get_width();
132 PixelFormat fmt = pixelformat_from_image(img);
133 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
135 image(0, img.get_pixels());
138 unsigned Texture1D::get_n_levels() const
141 for(unsigned s=width; s; s>>=1, ++n) ;
145 unsigned Texture1D::get_level_size(unsigned level) const
150 UInt64 Texture1D::get_data_size() const
152 return id ? width*get_pixel_size(storage_fmt) : 0;
156 Texture1D::Loader::Loader(Texture1D &t):
157 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
162 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
163 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
168 void Texture1D::Loader::init()
170 add("raw_data", &Loader::raw_data);
171 add("storage", &Loader::storage);
172 add("storage", &Loader::storage_levels);
175 void Texture1D::Loader::raw_data(const string &data)
177 obj.image(0, data.data());
180 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
185 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
187 obj.storage(fmt, w, l);