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 OpenGLTexture *OpenGLTexture::scratch_binding = 0;
17 OpenGLTexture::OpenGLTexture(unsigned t):
20 static bool alignment_init = false;
23 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
24 alignment_init = true;
28 OpenGLTexture::~OpenGLTexture()
30 if(this==scratch_binding)
33 glDeleteTextures(1, &id);
36 void OpenGLTexture::create()
39 throw invalid_operation("OpenGLTexture::create");
40 if(ARB_direct_state_access)
41 glCreateTextures(target, 1, &id);
43 glGenTextures(1, &id);
46 if(!debug_name.empty() && KHR_debug)
47 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
51 void OpenGLTexture::require_swizzle()
53 static Require _req(ARB_texture_swizzle);
56 void OpenGLTexture::apply_swizzle()
58 ComponentSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
59 if(swizzle==NO_SWIZZLE)
62 const int *swizzle_order = get_gl_swizzle(swizzle);
63 if(get_backend_api()==OPENGL_ES)
65 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_order[0]);
66 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_order[1]);
67 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_order[2]);
68 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_order[3]);
72 if(ARB_direct_state_access)
73 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
75 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
79 void OpenGLTexture::set_parameter_i(unsigned param, int value) const
81 if(ARB_direct_state_access)
82 glTextureParameteri(id, param, value);
84 glTexParameteri(target, param, value);
87 void OpenGLTexture::generate_mipmap()
89 // glGenerateMipmap is defined here
90 static Require _req(EXT_framebuffer_object);
92 if(ARB_direct_state_access)
93 glGenerateTextureMipmap(id);
97 glGenerateMipmap(target);
101 void OpenGLTexture::set_debug_name(const string &name)
106 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
112 void OpenGLTexture::bind_scratch()
115 glActiveTexture(GL_TEXTURE0);
116 if(scratch_binding!=this)
118 if(scratch_binding && scratch_binding->target!=target)
119 glBindTexture(scratch_binding->target, 0);
120 glBindTexture(target, id);
121 scratch_binding = this;
125 void OpenGLTexture::unbind_scratch()
129 glBindTexture(scratch_binding->target, 0);