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)
25 throw invalid_operation("Texture1D::storage");
27 throw invalid_argument("Texture1D::storage");
29 set_internal_format(fmt);
31 levels = get_n_levels();
33 levels = min(levels, lv);
36 void Texture1D::allocate(unsigned level)
39 throw invalid_operation("Texture1D::allocate");
41 throw invalid_argument("Texture1D::allocate");
42 if(allocated&(1<<level))
45 if(ARB_texture_storage)
47 if(ARB_direct_state_access)
48 glTextureStorage1D(id, levels, ifmt, width);
51 BindRestore _bind(this);
52 glTexStorage1D(target, levels, ifmt, width);
54 allocated |= (1<<levels)-1;
58 PixelFormat base_fmt = get_base_pixelformat(ifmt);
59 image(level, base_fmt, get_alloc_type(base_fmt), 0);
63 void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, 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, fmt, type, data);
73 BindRestore _bind(this);
76 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
77 glTexImage1D(target, level, ifmt, w, 0, get_upload_format(fmt), type, data);
79 allocated |= 1<<level;
80 if(auto_gen_mipmap && level==0)
83 allocated |= (1<<levels)-1;
87 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelFormat fmt, DataType type, const void *data)
90 throw invalid_operation("Texture3D::image");
92 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
95 fmt = get_upload_format(fmt);
96 if(ARB_direct_state_access)
97 glTextureSubImage1D(id, level, x, wd, fmt, type, data);
99 glTexSubImage1D(target, level, x, wd, fmt, type, data);
101 if(auto_gen_mipmap && level==0)
105 void Texture1D::image(const Graphics::Image &img, unsigned lv, bool srgb)
107 if(img.get_height()!=1)
108 throw incompatible_data("Texture1D::image");
110 unsigned w = img.get_width();
111 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
113 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, lv);
115 throw incompatible_data("Texture1D::image");
117 image(0, fmt, UNSIGNED_BYTE, img.get_data());
120 unsigned Texture1D::get_n_levels() const
123 for(unsigned s=width; s; s>>=1, ++n) ;
127 unsigned Texture1D::get_level_size(unsigned level) const
132 UInt64 Texture1D::get_data_size() const
134 return id ? width*get_pixel_size(ifmt) : 0;
138 Texture1D::Loader::Loader(Texture1D &t):
139 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
144 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
145 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
150 void Texture1D::Loader::init()
152 add("raw_data", &Loader::raw_data);
153 add("storage", &Loader::storage);
154 add("storage", &Loader::storage_levels);
157 void Texture1D::Loader::raw_data(const string &data)
159 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
162 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
167 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
169 obj.storage(fmt, w, l);