]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/pixelformat_backend.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / backends / opengl / pixelformat_backend.cpp
1 #include <msp/gl/extensions/arb_depth_buffer_float.h>
2 #include <msp/gl/extensions/arb_depth_texture.h>
3 #include <msp/gl/extensions/arb_texture_float.h>
4 #include <msp/gl/extensions/arb_texture_rg.h>
5 #include <msp/gl/extensions/ext_texture_srgb.h>
6 #include <msp/gl/extensions/oes_required_internalformat.h>
7 #include <msp/gl/extensions/oes_texture_stencil8.h>
8 #include "gl.h"
9 #include "pixelformat.h"
10 #include "pixelformat_backend.h"
11
12 using namespace std;
13
14 namespace {
15
16 int swizzle_orders[] =
17 {
18         GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
19         GL_RED, GL_RED, GL_RED, GL_ONE,
20         GL_RED, GL_RED, GL_RED, GL_GREEN,
21         GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA,
22         GL_RED, GL_GREEN, GL_BLUE, GL_ONE
23 };
24
25 }
26
27 namespace Msp {
28 namespace GL {
29
30 ComponentSwizzle get_required_swizzle(PixelComponents comp)
31 {
32         switch(comp)
33         {
34         case BGR: return RGB_TO_BGR;
35         case BGRA: return RGB_TO_BGR;
36         case LUMINANCE: return R_TO_LUMINANCE;
37         case LUMINANCE_ALPHA: return RG_TO_LUMINANCE_ALPHA;
38         default: return NO_SWIZZLE;
39         }
40 }
41
42 void require_pixelformat(PixelFormat pf)
43 {
44         /* TODO These checks are only accurate for textures.  On OpenGL ES some
45         formats are allowed for render buffers earlier than textures.  In particular
46         it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
47         depth textures are only available with 3.0 or the OES_depth_texture
48         extension.*/
49         switch(pf)
50         {
51         case RGB8:
52         case RGBA8:
53                 { static Require _req(OES_required_internalformat); }
54                 break;
55         case R8:
56         case RG8:
57                 { static Require _req(ARB_texture_rg); }
58                 break;
59         case R16F:
60         case R32F:
61         case RG16F:
62         case RG32F:
63                 { static Require _req(ARB_texture_rg); }
64                 { static Require _req(ARB_texture_float); }
65                 break;
66         case RGB16F:
67         case RGB32F:
68         case RGBA16F:
69         case RGBA32F:
70                 { static Require _req(ARB_texture_float); }
71                 break;
72         case SRGB8:
73         case SRGB8_ALPHA8:
74                 { static Require _req(EXT_texture_sRGB); }
75                 break;
76         case DEPTH_COMPONENT16:
77         case DEPTH_COMPONENT24:
78         case DEPTH_COMPONENT32:
79                 { static Require _req(ARB_depth_texture); }
80                 { static Require _req(OES_required_internalformat); }
81                 break;
82         case DEPTH_COMPONENT32F:
83                 { static Require _req(ARB_depth_buffer_float); }
84                 break;
85         case STENCIL_INDEX8:
86                 { static Require _req(OES_texture_stencil8); }
87                 break;
88         default:
89                 throw invalid_argument("require_pixelformat");
90         }
91 }
92
93 unsigned get_gl_components(PixelComponents comp)
94 {
95         switch(comp)
96         {
97         case RED: return GL_RED;
98         case RG: return GL_RG;
99         case RGB: return GL_RGB;
100         case RGBA: return GL_RGBA;
101         case DEPTH_COMPONENT: return GL_DEPTH_COMPONENT;
102         case STENCIL_INDEX: return GL_STENCIL_INDEX;
103         default: throw invalid_argument("get_gl_components");
104         }
105 }
106
107 unsigned get_gl_pixelformat(PixelFormat pf)
108 {
109         switch(pf)
110         {
111         case R8: return GL_R8;
112         case R16F: return GL_R16F;
113         case R32F: return GL_R32F;
114         case RG8: return GL_RG8;
115         case RG16F: return GL_RG16F;
116         case RG32F: return GL_RG32F;
117         case RGB8: return GL_RGB8;
118         case RGB16F: return GL_RGB16F;
119         case RGB32F: return GL_RGB32F;
120         case RGBA8: return GL_RGBA8;
121         case RGBA16F: return GL_RGBA16F;
122         case RGBA32F: return GL_RGBA32F;
123         case SRGB8: return GL_SRGB8;
124         case SRGB8_ALPHA8: return GL_SRGB8_ALPHA8;
125         case DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT16;
126         case DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT24;
127         case DEPTH_COMPONENT32: return GL_DEPTH_COMPONENT32;
128         case DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT32F;
129         case STENCIL_INDEX8: return GL_STENCIL_INDEX8;
130         default: throw invalid_argument("get_gl_pixelformat");
131         }
132 }
133
134 const int *get_gl_swizzle(ComponentSwizzle swiz)
135 {
136         return swizzle_orders+4*swiz;
137 }
138
139 } // namespace GL
140 } // namespace Msp