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):
29 static bool alignment_init = false;
32 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
33 alignment_init = true;
37 OpenGLTexture::~OpenGLTexture()
39 if(this==scratch_binding)
42 glDeleteTextures(1, &id);
45 void OpenGLTexture::create()
48 throw invalid_operation("OpenGLTexture::create");
49 if(ARB_direct_state_access)
50 glCreateTextures(target, 1, &id);
52 glGenTextures(1, &id);
55 if(!debug_name.empty() && KHR_debug)
56 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
60 void OpenGLTexture::require_swizzle()
62 static Require _req(ARB_texture_swizzle);
65 void OpenGLTexture::apply_swizzle()
67 Texture::FormatSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
68 if(swizzle==Texture::NO_SWIZZLE)
71 if(get_backend_api()==OPENGL_ES)
73 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
74 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
75 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
76 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
80 if(ARB_direct_state_access)
81 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
83 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
87 void OpenGLTexture::set_parameter_i(unsigned param, int value) const
89 if(ARB_direct_state_access)
90 glTextureParameteri(id, param, value);
92 glTexParameteri(target, param, value);
95 void OpenGLTexture::generate_mipmap()
97 // glGenerateMipmap is defined here
98 static Require _req(EXT_framebuffer_object);
100 if(ARB_direct_state_access)
101 glGenerateTextureMipmap(id);
105 glGenerateMipmap(target);
109 void OpenGLTexture::set_debug_name(const string &name)
114 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
120 void OpenGLTexture::bind_scratch()
123 glActiveTexture(GL_TEXTURE0);
124 if(scratch_binding!=this)
126 if(scratch_binding && scratch_binding->target!=target)
127 glBindTexture(scratch_binding->target, 0);
128 glBindTexture(target, id);
129 scratch_binding = this;
133 void OpenGLTexture::unbind_scratch()
137 glBindTexture(scratch_binding->target, 0);