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(OpenGLTexture &&other):
31 debug_name(move(other.debug_name))
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 ComponentSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
67 if(swizzle==NO_SWIZZLE)
70 const int *swizzle_order = get_gl_swizzle(swizzle);
71 if(get_backend_api()==OPENGL_ES)
73 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_order[0]);
74 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_order[1]);
75 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_order[2]);
76 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_order[3]);
80 if(ARB_direct_state_access)
81 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
83 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
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);