]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/texture_backend.cpp
88e1c2b33877cc1349b93375b8201f4de318047c
[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 int OpenGLTexture::swizzle_orders[] =
16 {
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
21 };
22
23 OpenGLTexture *OpenGLTexture::scratch_binding = 0;
24
25 OpenGLTexture::OpenGLTexture(unsigned t):
26         target(t)
27 {
28         static bool alignment_init = false;
29         if(!alignment_init)
30         {
31                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
32                 alignment_init = true;
33         }
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         Texture::FormatSwizzle swizzle = static_cast<const Texture *>(this)->swizzle;
67         if(swizzle==Texture::NO_SWIZZLE)
68                 return;
69
70         if(get_backend_api()==OPENGL_ES)
71         {
72                 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
73                 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
74                 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
75                 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
76         }
77         else
78         {
79                 if(ARB_direct_state_access)
80                         glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
81                 else
82                         glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
83         }
84 }
85
86 void OpenGLTexture::set_parameter_i(unsigned param, int value) const
87 {
88         if(ARB_direct_state_access)
89                 glTextureParameteri(id, param, value);
90         else
91                 glTexParameteri(target, param, value);
92 }
93
94 void OpenGLTexture::generate_mipmap()
95 {
96         // glGenerateMipmap is defined here
97         static Require _req(EXT_framebuffer_object);
98
99         if(ARB_direct_state_access)
100                 glGenerateTextureMipmap(id);
101         else
102         {
103                 bind_scratch();
104                 glGenerateMipmap(target);
105         }
106 }
107
108 void OpenGLTexture::set_debug_name(const string &name)
109 {
110 #ifdef DEBUG
111         debug_name = name;
112         if(id && KHR_debug)
113                 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
114 #else
115         (void)name;
116 #endif
117 }
118
119 void OpenGLTexture::bind_scratch()
120 {
121         if(!scratch_binding)
122                 glActiveTexture(GL_TEXTURE0);
123         if(scratch_binding!=this)
124         {
125                 if(scratch_binding && scratch_binding->target!=target)
126                         glBindTexture(scratch_binding->target, 0);
127                 glBindTexture(target, id);
128                 scratch_binding = this;
129         }
130 }
131
132 void OpenGLTexture::unbind_scratch()
133 {
134         if(scratch_binding)
135         {
136                 glBindTexture(scratch_binding->target, 0);
137                 scratch_binding = 0;
138         }
139 }
140
141 } // namespace GL
142 } // namespace Msp