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, bool create):
32 static bool alignment_init = false;
35 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
36 alignment_init = true;
40 OpenGLTexture::~OpenGLTexture()
42 if(this==scratch_binding)
45 glDeleteTextures(1, &id);
48 void OpenGLTexture::generate_id()
51 throw invalid_operation("OpenGLTexture::generate_id");
52 if(ARB_direct_state_access)
53 glCreateTextures(target, 1, &id);
55 glGenTextures(1, &id);
58 if(!debug_name.empty() && KHR_debug)
59 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
63 void OpenGLTexture::require_swizzle()
65 static Require _req(ARB_texture_swizzle);
68 void OpenGLTexture::apply_swizzle()
70 Texture::FormatSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
71 if(swizzle==Texture::NO_SWIZZLE)
74 if(get_backend_api()==OPENGL_ES)
76 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
77 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
78 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
79 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
83 if(ARB_direct_state_access)
84 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
86 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
90 void OpenGLTexture::set_parameter_i(unsigned param, int value) const
92 if(ARB_direct_state_access)
93 glTextureParameteri(id, param, value);
95 glTexParameteri(target, param, value);
98 void OpenGLTexture::generate_mipmap()
100 // glGenerateMipmap is defined here
101 static Require _req(EXT_framebuffer_object);
103 if(ARB_direct_state_access)
104 glGenerateTextureMipmap(id);
108 glGenerateMipmap(target);
112 void OpenGLTexture::set_debug_name(const string &name)
117 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
123 void OpenGLTexture::bind_scratch()
126 glActiveTexture(GL_TEXTURE0);
127 if(scratch_binding!=this)
129 if(scratch_binding && scratch_binding->target!=target)
130 glBindTexture(scratch_binding->target, 0);
131 glBindTexture(target, id);
132 scratch_binding = this;
136 void OpenGLTexture::unbind_scratch()
140 glBindTexture(scratch_binding->target, 0);