]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/texture_backend.cpp
Add support for padding in vertex formats
[libs/gl.git] / source / backends / opengl / texture_backend.cpp
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>
5 #include "gl.h"
6 #include "error.h"
7 #include "texture.h"
8 #include "texture_backend.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 OpenGLTexture *OpenGLTexture::scratch_binding = 0;
16
17 OpenGLTexture::OpenGLTexture(unsigned t):
18         target(t)
19 {
20         static bool alignment_init = false;
21         if(!alignment_init)
22         {
23                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
24                 alignment_init = true;
25         }
26 }
27
28 OpenGLTexture::OpenGLTexture(OpenGLTexture &&other):
29         id(other.id),
30         target(other.target),
31         debug_name(move(other.debug_name))
32 {
33         other.id = 0;
34 }
35
36 OpenGLTexture::~OpenGLTexture()
37 {
38         if(this==scratch_binding)
39                 unbind_scratch();
40         if(id)
41                 glDeleteTextures(1, &id);
42 }
43
44 void OpenGLTexture::create()
45 {
46         if(id)
47                 throw invalid_operation("OpenGLTexture::create");
48         if(ARB_direct_state_access)
49                 glCreateTextures(target, 1, &id);
50         else
51                 glGenTextures(1, &id);
52
53 #ifdef DEBUG
54         if(!debug_name.empty() && KHR_debug)
55                 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
56 #endif
57 }
58
59 void OpenGLTexture::require_swizzle()
60 {
61         static Require _req(ARB_texture_swizzle);
62 }
63
64 void OpenGLTexture::apply_swizzle()
65 {
66         ComponentSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
67         if(swizzle==NO_SWIZZLE)
68                 return;
69
70         const int *swizzle_order = get_gl_swizzle(swizzle);
71         if(get_backend_api()==OPENGL_ES)
72         {
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]);
77         }
78         else
79         {
80                 if(ARB_direct_state_access)
81                         glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
82                 else
83                         glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order);
84         }
85 }
86
87 void OpenGLTexture::set_parameter_i(unsigned param, int value) const
88 {
89         if(ARB_direct_state_access)
90                 glTextureParameteri(id, param, value);
91         else
92                 glTexParameteri(target, param, value);
93 }
94
95 void OpenGLTexture::generate_mipmap()
96 {
97         // glGenerateMipmap is defined here
98         static Require _req(EXT_framebuffer_object);
99
100         if(ARB_direct_state_access)
101                 glGenerateTextureMipmap(id);
102         else
103         {
104                 bind_scratch();
105                 glGenerateMipmap(target);
106         }
107 }
108
109 void OpenGLTexture::set_debug_name(const string &name)
110 {
111 #ifdef DEBUG
112         debug_name = name;
113         if(id && KHR_debug)
114                 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
115 #else
116         (void)name;
117 #endif
118 }
119
120 void OpenGLTexture::bind_scratch()
121 {
122         if(!scratch_binding)
123                 glActiveTexture(GL_TEXTURE0);
124         if(scratch_binding!=this)
125         {
126                 if(scratch_binding && scratch_binding->target!=target)
127                         glBindTexture(scratch_binding->target, 0);
128                 glBindTexture(target, id);
129                 scratch_binding = this;
130         }
131 }
132
133 void OpenGLTexture::unbind_scratch()
134 {
135         if(scratch_binding)
136         {
137                 glBindTexture(scratch_binding->target, 0);
138                 scratch_binding = 0;
139         }
140 }
141
142 } // namespace GL
143 } // namespace Msp