1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_swizzle.h>
3 #include <msp/gl/extensions/ext_framebuffer_object.h>
4 #include <msp/gl/extensions/khr_debug.h>
8 #include "texture_backend.h"
15 int OpenGLTexture::swizzle_orders[] =
17 GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
18 GL_RED, GL_RED, GL_RED, GL_ONE,
19 GL_RED, GL_RED, GL_RED, GL_GREEN,
20 GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
23 OpenGLTexture *OpenGLTexture::scratch_binding = 0;
25 OpenGLTexture::OpenGLTexture(unsigned t):
28 static bool alignment_init = false;
31 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
32 alignment_init = true;
36 OpenGLTexture::~OpenGLTexture()
38 if(this==scratch_binding)
41 glDeleteTextures(1, &id);
44 void OpenGLTexture::create()
47 throw invalid_operation("OpenGLTexture::create");
48 if(ARB_direct_state_access)
49 glCreateTextures(target, 1, &id);
51 glGenTextures(1, &id);
54 if(!debug_name.empty() && KHR_debug)
55 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
59 void OpenGLTexture::require_swizzle()
61 static Require _req(ARB_texture_swizzle);
64 void OpenGLTexture::apply_swizzle()
66 Texture::FormatSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
67 if(swizzle==Texture::NO_SWIZZLE)
70 if(get_backend_api()==OPENGL_ES)
72 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
73 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
74 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
75 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
79 if(ARB_direct_state_access)
80 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
82 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
86 void OpenGLTexture::set_parameter_i(unsigned param, int value) const
88 if(ARB_direct_state_access)
89 glTextureParameteri(id, param, value);
91 glTexParameteri(target, param, value);
94 void OpenGLTexture::generate_mipmap()
96 // glGenerateMipmap is defined here
97 static Require _req(EXT_framebuffer_object);
99 if(ARB_direct_state_access)
100 glGenerateTextureMipmap(id);
104 glGenerateMipmap(target);
108 void OpenGLTexture::set_debug_name(const string &name)
113 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
119 void OpenGLTexture::bind_scratch()
122 glActiveTexture(GL_TEXTURE0);
123 if(scratch_binding!=this)
125 if(scratch_binding && scratch_binding->target!=target)
126 glBindTexture(scratch_binding->target, 0);
127 glBindTexture(target, id);
128 scratch_binding = this;
132 void OpenGLTexture::unbind_scratch()
136 glBindTexture(scratch_binding->target, 0);