1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/msp_texture1d.h>
12 Texture1D::Texture1D():
13 Texture(GL_TEXTURE_1D),
17 static Require _req(MSP_texture1D);
20 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
24 if(fmt!=format || wd!=width || (lv && lv!=levels))
25 throw incompatible_data("Texture1D::storage");
29 throw invalid_argument("Texture1D::storage");
33 levels = get_n_levels();
35 levels = min(levels, lv);
38 void Texture1D::allocate(unsigned level)
41 throw invalid_operation("Texture1D::allocate");
43 throw invalid_argument("Texture1D::allocate");
44 if(allocated&(1<<level))
47 if(ARB_texture_storage)
49 GLenum fmt = get_gl_pixelformat(storage_fmt);
50 if(ARB_direct_state_access)
51 glTextureStorage1D(id, levels, fmt, width);
55 glTexStorage1D(target, levels, fmt, width);
58 allocated |= (1<<levels)-1;
64 void Texture1D::image(unsigned level, const void *data)
67 throw invalid_operation("Texture1D::image");
69 throw out_of_range("Texture1D::image");
71 if(ARB_texture_storage)
72 return sub_image(level, 0, get_level_size(level), data);
78 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
82 GLenum fmt = get_gl_pixelformat(storage_fmt);
83 GLenum comp = get_gl_components(get_components(storage_fmt));
84 GLenum type = get_gl_type(get_component_type(storage_fmt));
85 glTexImage1D(target, level, fmt, get_level_size(level), 0, comp, type, data);
87 allocated |= 1<<level;
90 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
93 throw invalid_operation("Texture1D::sub_image");
95 throw out_of_range("Texture1D::sub_image");
99 GLenum comp = get_gl_components(get_components(storage_fmt));
100 GLenum type = get_gl_type(get_component_type(storage_fmt));
101 if(ARB_direct_state_access)
102 glTextureSubImage1D(id, level, x, wd, comp, type, data);
106 glTexSubImage1D(target, level, x, wd, comp, type, data);
110 void Texture1D::image(const Graphics::Image &img, unsigned lv)
112 if(img.get_height()!=1)
113 throw incompatible_data("Texture1D::image");
115 unsigned w = img.get_width();
116 PixelFormat fmt = pixelformat_from_image(img);
117 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
119 image(0, img.get_pixels());
122 unsigned Texture1D::get_n_levels() const
125 for(unsigned s=width; s; s>>=1, ++n) ;
129 unsigned Texture1D::get_level_size(unsigned level) const
134 uint64_t Texture1D::get_data_size() const
136 return id ? width*get_pixel_size(storage_fmt) : 0;
140 Texture1D::Loader::Loader(Texture1D &t):
141 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
146 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
147 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
152 void Texture1D::Loader::init()
154 add("raw_data", &Loader::raw_data);
155 add("storage", &Loader::storage);
156 add("storage", &Loader::storage_levels);
159 void Texture1D::Loader::raw_data(const string &data)
161 obj.image(0, data.data());
164 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
169 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
171 obj.storage(fmt, w, l);