namespace Msp {
namespace GL {
+Buffer *Buffer::scratch_binding = 0;
+
Buffer::Buffer():
size(0),
allocated(false)
Buffer::~Buffer()
{
+ if(this==scratch_binding)
+ unbind_scratch();
glDeleteBuffers(1, &id);
}
glNamedBufferStorage(id, size, 0, flags);
else
{
- glBindBuffer(GL_ARRAY_BUFFER, id);
+ bind_scratch();
glBufferStorage(GL_ARRAY_BUFFER, size, 0, flags);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
}
allocated = true;
glNamedBufferData(id, size, d, STATIC_DRAW);
else
{
- glBindBuffer(GL_ARRAY_BUFFER, id);
+ bind_scratch();
glBufferData(GL_ARRAY_BUFFER, size, d, STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
}
allocated = true;
glNamedBufferSubData(id, off, sz, d);
else
{
- glBindBuffer(GL_ARRAY_BUFFER, id);
+ bind_scratch();
glBufferSubData(GL_ARRAY_BUFFER, off, sz, d);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
return glMapNamedBufferRange(id, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
else
{
- glBindBuffer(GL_ARRAY_BUFFER, id);
+ bind_scratch();
void *result = glMapBufferRange(GL_ARRAY_BUFFER, 0, size, GL_MAP_READ_BIT|GL_MAP_WRITE_BIT);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
return result;
}
}
return glUnmapNamedBuffer(id);
else if(OES_mapbuffer)
{
- glBindBuffer(GL_ARRAY_BUFFER, id);
+ bind_scratch();
bool result = glUnmapBuffer(GL_ARRAY_BUFFER);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
return result;
}
else
#endif
}
+void Buffer::bind_scratch()
+{
+ if(scratch_binding!=this)
+ {
+ glBindBuffer(GL_ARRAY_BUFFER, id);
+ scratch_binding = this;
+ }
+}
+
+void Buffer::unbind_scratch()
+{
+ if(scratch_binding)
+ {
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ scratch_binding = 0;
+ }
+}
+
} // namespace GL
} // namespace Msp
unsigned size;
bool allocated;
+ static Buffer *scratch_binding;
+
public:
Buffer();
~Buffer();
bool unmap();
void set_debug_name(const std::string &);
+
+private:
+ void bind_scratch();
+public:
+ static void unbind_scratch();
};
} // namespace GL
void PipelineState::apply() const
{
+ if(!last_applied)
+ Texture::unbind_scratch();
+
apply(this==last_applied ? changes : ~0U);
}
GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
};
+Texture *Texture::scratch_binding = 0;
+
Texture::Texture(GLenum t, ResourceManager *m):
id(0),
target(t),
Texture::~Texture()
{
+ if(this==scratch_binding)
+ unbind_scratch();
if(id)
glDeleteTextures(1, &id);
}
// glGenerateMipmap is defined here
static Require _req(EXT_framebuffer_object);
- if(!ARB_direct_state_access)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
- generate_mipmap_();
- if(!ARB_direct_state_access)
- glBindTexture(target, 0);
-}
-
-void Texture::generate_mipmap_()
-{
if(ARB_direct_state_access)
glGenerateTextureMipmap(id);
else
+ {
+ bind_scratch();
glGenerateMipmap(target);
+ }
}
void Texture::set_auto_generate_mipmap(bool gm)
#endif
}
+void Texture::bind_scratch()
+{
+ if(!scratch_binding)
+ glActiveTexture(GL_TEXTURE0);
+ if(scratch_binding!=this)
+ {
+ if(scratch_binding && scratch_binding->target!=target)
+ glBindTexture(scratch_binding->target, 0);
+ glBindTexture(target, id);
+ scratch_binding = this;
+ }
+}
+
+void Texture::unbind_scratch()
+{
+ if(scratch_binding)
+ {
+ glBindTexture(scratch_binding->target, 0);
+ scratch_binding = 0;
+ }
+}
+
Texture::Loader::Loader(Texture &t):
DataFile::CollectionObjectLoader<Texture>(t, 0)
std::string debug_name;
static int swizzle_orders[];
+ static Texture *scratch_binding;
Texture(GLenum, ResourceManager * = 0);
Texture(const Texture &);
static bool can_generate_mipmap();
void generate_mipmap();
-protected:
- void generate_mipmap_();
-public:
/** Sets automatic mipmap generation. If enabled, mipmaps are generated
when a texture image is uploaded. */
void set_auto_generate_mipmap(bool);
virtual UInt64 get_data_size() const { return 0; }
void set_debug_name(const std::string &);
+
+protected:
+ void bind_scratch();
+public:
+ static void unbind_scratch();
};
} // namespace GL
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;
if(ARB_direct_state_access)
glTextureStorage1D(id, levels, fmt, width);
else
+ {
+ bind_scratch();
glTexStorage1D(target, levels, fmt, width);
+ }
apply_swizzle();
allocated |= (1<<levels)-1;
}
else
- image_(level, 0);
+ image(level, 0);
}
void Texture1D::image(unsigned level, const void *data)
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;
- }
+ bind_scratch();
- glBindTexture(target, 0);
-}
-
-void Texture1D::image_(unsigned level, const void *data)
-{
if(!allocated)
{
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
glTexImage1D(target, level, fmt, get_level_size(level), 0, comp, type, data);
allocated |= 1<<level;
+ if(auto_gen_mipmap && level==0)
+ {
+ generate_mipmap();
+ allocated |= (1<<levels)-1;
+ }
}
void Texture1D::image(unsigned level, PixelComponents comp, DataType type, 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);
+ 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);
+ generate_mipmap();
}
void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
{ storage(make_pixelformat(c, UNSIGNED_BYTE), w, l); }
void allocate(unsigned);
-private:
- void allocate_(unsigned);
-public:
void image(unsigned, const void *);
-private:
- void image_(unsigned, const void *);
-public:
DEPRECATED void image(unsigned, PixelComponents, DataType, const void *);
void sub_image(unsigned, int, unsigned, const void *);
DEPRECATED void sub_image(unsigned, int, unsigned, PixelComponents, DataType, const void *);
throw invalid_operation("Texture2D::allocate");
if(level>=levels)
throw invalid_argument("Texture2D::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 Texture2D::allocate_(unsigned level)
-{
if(allocated&(1<<level))
return;
if(ARB_direct_state_access)
glTextureStorage2D(id, levels, fmt, width, height);
else
+ {
+ bind_scratch();
glTexStorage2D(target, levels, fmt, width, height);
+ }
apply_swizzle();
allocated |= (1<<levels)-1;
}
else
- image_(level, 0);
+ image(level, 0);
}
void Texture2D::image(unsigned level, const void *data)
if(level>=levels)
throw out_of_range("Texture2D::image");
+ LinAl::Vector<unsigned, 2> size = get_level_size(level);
+
if(ARB_texture_storage)
- {
- LinAl::Vector<unsigned, 2> size = get_level_size(level);
return sub_image(level, 0, 0, size.x, size.y, 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);
-}
+ bind_scratch();
-void Texture2D::image_(unsigned level, const void *data)
-{
if(!allocated)
{
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
apply_swizzle();
}
- LinAl::Vector<unsigned, 2> size = get_level_size(level);
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));
glTexImage2D(target, level, fmt, size.x, size.y, 0, comp, type, data);
allocated |= 1<<level;
+ if(auto_gen_mipmap && level==0)
+ {
+ generate_mipmap();
+ allocated |= (1<<levels)-1;
+ }
}
void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
if(level>=levels)
throw out_of_range("Texture2D::sub_image");
- bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
- if(!direct)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
-
- allocate_(level);
+ 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)
glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
else
+ {
+ bind_scratch();
glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
+ }
if(auto_gen_mipmap && level==0)
- generate_mipmap_();
-
- if(!direct)
- glBindTexture(target, 0);
+ generate_mipmap();
}
void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
undefined. If storage has already been allocated, does nothing. */
void allocate(unsigned level);
-private:
- void allocate_(unsigned level);
-
-public:
/** Updates the contents of the entire texture. Storage must be defined
beforehand. The image data must have dimensions and format matching the
defined storage. */
virtual void image(unsigned level, const void *data);
-private:
- void image_(unsigned level, const void *data);
-
-public:
DEPRECATED void image(unsigned level, PixelComponents fmt, DataType type, const void *data);
/** Updates a rectangular region of the texture. Storage must be defined
height = ht;
samples = sm;
- bool direct = ARB_texture_storage_multisample && ARB_direct_state_access;
- if(!direct)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
-
GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
if(ARB_texture_storage_multisample)
{
if(ARB_direct_state_access)
glTextureStorage2DMultisample(id, samples, gl_fmt, width, height, false);
else
+ {
+ bind_scratch();
glTexStorage2DMultisample(target, samples, gl_fmt, width, height, false);
+ }
}
else
+ {
+ bind_scratch();
glTexImage2DMultisample(target, samples, gl_fmt, width, height, false);
+ }
apply_swizzle();
-
- if(!direct)
- glBindTexture(target, 0);
}
void Texture2DMultisample::image(const Graphics::Image &, unsigned)
throw invalid_operation("Texture3D::allocate");
if(level>=levels)
throw invalid_argument("Texture3D::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 Texture3D::allocate_(unsigned level)
-{
if(allocated&(1<<level))
return;
if(ARB_direct_state_access)
glTextureStorage3D(id, levels, fmt, width, height, depth);
else
+ {
+ bind_scratch();
glTexStorage3D(target, levels, fmt, width, height, depth);
+ }
apply_swizzle();
allocated |= (1<<levels)-1;
}
else
- image_(level, 0);
+ image(level, 0);
}
void Texture3D::image(unsigned level, const void *data)
if(level>=levels)
throw out_of_range("Texture3D::image");
+ LinAl::Vector<unsigned, 3> size = get_level_size(level);
+
if(ARB_texture_storage)
- {
- LinAl::Vector<unsigned, 3> size = get_level_size(level);
return sub_image(level, 0, 0, 0, size.x, size.y, size.z, 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);
-}
+ bind_scratch();
-void Texture3D::image_(unsigned level, const void *data)
-{
if(!allocated)
{
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
apply_swizzle();
}
- LinAl::Vector<unsigned, 3> size = get_level_size(level);
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));
glTexImage3D(target, level, fmt, size.x, size.y, size.z, 0, comp, type, data);
allocated |= 1<<level;
+ if(auto_gen_mipmap && level==0)
+ {
+ generate_mipmap();
+ allocated |= (1<<levels)-1;
+ }
}
void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
if(level>=levels)
throw out_of_range("Texture3D::sub_image");
- bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
- if(!direct)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
-
- allocate_(level);
+ 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)
glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
else
+ {
+ bind_scratch();
glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
+ }
if(auto_gen_mipmap && level==0)
- generate_mipmap_();
-
- if(!direct)
- glBindTexture(target, 0);
+ generate_mipmap();
}
void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelComponents comp, DataType type, const void *data)
undefined. If storage has already been allocated, does nothing. */
void allocate(unsigned level);
-private:
- void allocate_(unsigned);
-
-public:
/** Updates the contents of the entire texture. Storage must be defined
beforehand. The image data must have dimensions and format matching the
defined storage. */
void image(unsigned level, const void *data);
-private:
- void image_(unsigned, const void *);
-
-public:
DEPRECATED void image(unsigned level, PixelComponents comp, DataType type, const void *data);
/** Updates a cuboid-shaped region of the texture. Storage must be defined
throw invalid_operation("TextureCube::allocate");
if(level>=levels)
throw invalid_argument("TextureCube::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 TextureCube::allocate_(unsigned level)
-{
if(allocated&(64<<level))
return;
if(ARB_direct_state_access)
glTextureStorage2D(id, levels, fmt, size, size);
else
+ {
+ bind_scratch();
glTexStorage2D(target, levels, fmt, size, size);
+ }
apply_swizzle();
allocated |= (64<<levels)-1;
}
else
{
for(unsigned i=0; i<6; ++i)
- image_(enumerate_faces(i), level, 0);
+ image(enumerate_faces(i), level, 0);
}
}
if(level>=levels)
throw out_of_range("TextureCube::image");
+ unsigned lsz = get_level_size(level);
+
if(ARB_texture_storage)
- {
- unsigned lsz = get_level_size(level);
return sub_image(face, level, 0, 0, lsz, lsz, data);
- }
-
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
-
- image_(face, level, data);
- if(auto_gen_mipmap && level==0 && (allocated&63)==63)
- {
- generate_mipmap_();
- allocated |= (64<<levels)-1;
- }
-
- glBindTexture(target, 0);
-}
-
-void TextureCube::image_(TextureCubeFace face, unsigned level, const void *data)
-{
if(!allocated)
{
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
apply_swizzle();
}
- unsigned lsz = get_level_size(level);
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));
{
allocated |= 1<<get_face_index(face);
if((allocated&63)==63)
+ {
allocated |= 64;
+ if(auto_gen_mipmap && level==0 && (allocated&63)==63)
+ {
+ generate_mipmap();
+ allocated |= (64<<levels)-1;
+ }
+ }
}
else if(!(allocated&(64<<level)))
{
if(level>=levels)
throw out_of_range("TextureCube::sub_image");
- bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
- if(!direct)
- {
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(target, id);
- }
-
- allocate_(level);
+ 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)
glTextureSubImage3D(id, level, x, y, get_face_index(face), wd, ht, 1, comp, type, data);
else
+ {
+ bind_scratch();
glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
+ }
if(auto_gen_mipmap && level==0)
- generate_mipmap_();
-
- if(!direct)
- glBindTexture(target, 0);
+ generate_mipmap();
}
void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
undefined. If storage has already been allocated, does nothing. */
void allocate(unsigned level);
-private:
- void allocate_(unsigned);
-
-public:
/** Updates the contents of a face. Storage must be defined beforehand.
The image data must have dimensions and format matching the defined
storage. */
void image(TextureCubeFace face, unsigned level, const void *data);
-private:
- void image_(TextureCubeFace, unsigned, const void *);
-
-public:
DEPRECATED void image(TextureCubeFace face, unsigned level,
PixelComponents comp, DataType type, const void *data);
void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
{
if(!direct)
+ {
+ Buffer::unbind_scratch();
glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
+ }
const VertexFormat &fmt = array.get_format();
unsigned stride = fmt.stride();