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),
20 static Require _req(MSP_texture1D);
23 void Texture1D::storage(PixelFormat fmt, unsigned wd)
26 throw invalid_operation("Texture1D::storage");
28 throw invalid_argument("Texture1D::storage");
30 if(MSP_sized_internal_formats)
31 fmt = get_sized_pixelformat(fmt);
32 require_pixelformat(fmt);
38 void Texture1D::allocate(unsigned level)
40 if(allocated&(1<<level))
43 if(ARB_texture_storage)
45 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
46 if(ARB_direct_state_access)
47 glTextureStorage1D(id, n_levels, ifmt, width);
50 BindRestore _bind(this);
51 glTexStorage1D(target, n_levels, ifmt, width);
53 allocated |= (1<<n_levels)-1;
57 PixelFormat base_fmt = get_base_pixelformat(ifmt);
58 image(level, base_fmt, get_alloc_type(base_fmt), 0);
62 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
65 throw invalid_operation("Texture1D::image");
67 unsigned w = get_level_size(level);
69 if(ARB_texture_storage)
70 return sub_image(level, 0, w, fmt, type, data);
72 BindRestore _bind(this);
73 glTexImage1D(target, level, ifmt, w, 0, fmt, type, data);
75 allocated |= 1<<level;
76 if(gen_mipmap && level==0)
78 auto_generate_mipmap();
79 allocated |= (1<<get_n_levels())-1;
83 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
86 throw invalid_operation("Texture3D::image");
88 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
91 if(ARB_direct_state_access)
92 glTextureSubImage1D(id, level, x, wd, fmt, type, data);
94 glTexSubImage1D(target, level, x, wd, fmt, type, data);
96 if(gen_mipmap && level==0)
97 auto_generate_mipmap();
100 void Texture1D::image(const Graphics::Image &img, bool srgb)
102 if(img.get_height()!=1)
103 throw incompatible_data("Texture1D::image");
105 unsigned w = img.get_width();
106 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
108 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
110 throw incompatible_data("Texture1D::image");
112 image(0, fmt, UNSIGNED_BYTE, img.get_data());
115 unsigned Texture1D::get_n_levels() const
118 for(unsigned s=width; s; s>>=1, ++n) ;
122 unsigned Texture1D::get_level_size(unsigned level) const
127 UInt64 Texture1D::get_data_size() const
129 return id ? width*get_pixel_size(ifmt) : 0;
133 Texture1D::Loader::Loader(Texture1D &t):
134 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
139 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
140 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
145 void Texture1D::Loader::init()
147 add("raw_data", &Loader::raw_data);
148 add("storage", &Loader::storage);
151 void Texture1D::Loader::raw_data(const string &data)
153 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
156 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
159 fmt = get_srgb_pixelformat(fmt);