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)
25 throw invalid_operation("Texture1D::storage");
27 throw invalid_argument("Texture1D::storage");
29 set_internal_format(fmt);
33 void Texture1D::allocate(unsigned level)
35 if(allocated&(1<<level))
38 if(ARB_texture_storage)
40 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
41 if(ARB_direct_state_access)
42 glTextureStorage1D(id, n_levels, ifmt, width);
45 BindRestore _bind(this);
46 glTexStorage1D(target, n_levels, ifmt, width);
48 allocated |= (1<<n_levels)-1;
52 PixelFormat base_fmt = get_base_pixelformat(ifmt);
53 image(level, base_fmt, get_alloc_type(base_fmt), 0);
57 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
60 throw invalid_operation("Texture1D::image");
62 unsigned w = get_level_size(level);
64 if(ARB_texture_storage)
65 return sub_image(level, 0, w, fmt, type, data);
67 BindRestore _bind(this);
68 glTexImage1D(target, level, ifmt, w, 0, get_upload_format(fmt), type, data);
70 allocated |= 1<<level;
71 if(gen_mipmap && level==0)
73 auto_generate_mipmap();
74 allocated |= (1<<get_n_levels())-1;
78 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
81 throw invalid_operation("Texture3D::image");
83 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
86 fmt = get_upload_format(fmt);
87 if(ARB_direct_state_access)
88 glTextureSubImage1D(id, level, x, wd, fmt, type, data);
90 glTexSubImage1D(target, level, x, wd, fmt, type, data);
92 if(gen_mipmap && level==0)
93 auto_generate_mipmap();
96 void Texture1D::image(const Graphics::Image &img, bool srgb)
98 if(img.get_height()!=1)
99 throw incompatible_data("Texture1D::image");
101 unsigned w = img.get_width();
102 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
104 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
106 throw incompatible_data("Texture1D::image");
108 image(0, fmt, UNSIGNED_BYTE, img.get_data());
111 unsigned Texture1D::get_n_levels() const
114 for(unsigned s=width; s; s>>=1, ++n) ;
118 unsigned Texture1D::get_level_size(unsigned level) const
123 UInt64 Texture1D::get_data_size() const
125 return id ? width*get_pixel_size(ifmt) : 0;
129 Texture1D::Loader::Loader(Texture1D &t):
130 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
135 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
136 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
141 void Texture1D::Loader::init()
143 add("raw_data", &Loader::raw_data);
144 add("storage", &Loader::storage);
147 void Texture1D::Loader::raw_data(const string &data)
149 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
152 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
155 fmt = get_srgb_pixelformat(fmt);