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