-#include <msp/core/raii.h>
#include <msp/gl/extensions/arb_direct_state_access.h>
#include <msp/gl/extensions/arb_texture_storage.h>
#include <msp/gl/extensions/msp_texture1d.h>
Texture1D::Texture1D():
Texture(GL_TEXTURE_1D),
- width(0),
- allocated(0)
+ width(0)
{
static Require _req(MSP_texture1D);
}
levels = get_n_levels();
if(lv)
levels = min(levels, lv);
-}
-
-void Texture1D::allocate(unsigned level)
-{
- if(width==0)
- throw invalid_operation("Texture1D::allocate");
- if(level>=levels)
- throw invalid_argument("Texture1D::allocate");
-
- bool direct = ARB_texture_storage && ARB_direct_state_access;
- if(!direct)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
-
- allocate_(level);
-
- if(!direct)
- glBindTexture(target, 0);
-}
-
-void Texture1D::allocate_(unsigned level)
-{
- if(allocated&(1<<level))
- return;
+ GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
if(ARB_texture_storage)
{
- GLenum fmt = get_gl_pixelformat(storage_fmt);
if(ARB_direct_state_access)
- glTextureStorage1D(id, levels, fmt, width);
+ glTextureStorage1D(id, levels, gl_fmt, width);
else
- glTexStorage1D(target, levels, fmt, width);
- apply_swizzle();
- allocated |= (1<<levels)-1;
+ {
+ bind_scratch();
+ glTexStorage1D(target, levels, gl_fmt, width);
+ }
}
else
- image_(level, 0);
-}
-
-void Texture1D::image(unsigned level, const void *data)
-{
- if(width==0)
- throw invalid_operation("Texture1D::image");
- if(level>=levels)
- throw out_of_range("Texture1D::image");
-
- if(ARB_texture_storage)
- return sub_image(level, 0, get_level_size(level), data);
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
-
- image_(level, data);
-
- if(auto_gen_mipmap && level==0)
- {
- generate_mipmap_();
- allocated |= (1<<levels)-1;
- }
-
- glBindTexture(target, 0);
-}
-
-void Texture1D::image_(unsigned level, const void *data)
-{
- if(!allocated)
{
+ bind_scratch();
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
- apply_swizzle();
+ GLenum comp = get_gl_components(get_components(storage_fmt));
+ GLenum type = get_gl_type(get_component_type(storage_fmt));
+ for(unsigned i=0; i<levels; ++i)
+ glTexImage1D(target, i, gl_fmt, get_level_size(i), 0, comp, type, 0);
}
- GLenum fmt = get_gl_pixelformat(storage_fmt);
- GLenum comp = get_gl_components(get_components(storage_fmt));
- GLenum type = get_gl_type(get_component_type(storage_fmt));
- glTexImage1D(target, level, fmt, get_level_size(level), 0, comp, type, data);
-
- allocated |= 1<<level;
+ apply_swizzle();
}
-void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
+void Texture1D::image(unsigned level, const void *data)
{
- if(comp!=get_components(format) || type!=get_component_type(format))
- throw incompatible_data("Texture1D::image");
- image(level, data);
+ return sub_image(level, 0, get_level_size(level), data);
}
void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
if(level>=levels)
throw out_of_range("Texture1D::sub_image");
- bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
- if(!direct)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
-
- allocate_(level);
-
GLenum comp = get_gl_components(get_components(storage_fmt));
GLenum type = get_gl_type(get_component_type(storage_fmt));
if(ARB_direct_state_access)
glTextureSubImage1D(id, level, x, wd, comp, type, data);
else
+ {
+ bind_scratch();
glTexSubImage1D(target, level, x, wd, comp, type, data);
-
- if(auto_gen_mipmap && level==0)
- generate_mipmap_();
-
- if(!direct)
- glBindTexture(target, 0);
-}
-
-void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
-{
- if(comp!=get_components(format) || type!=get_component_type(format))
- throw incompatible_data("Texture1D::sub_image");
- sub_image(level, x, wd, data);
+ }
}
void Texture1D::image(const Graphics::Image &img, unsigned lv)
return width>>level;
}
-UInt64 Texture1D::get_data_size() const
+uint64_t Texture1D::get_data_size() const
{
return id ? width*get_pixel_size(storage_fmt) : 0;
}