]> git.tdb.fi Git - libs/gl.git/blob - source/core/pixelformat.cpp
Remove remaining deprecated things from the core classes
[libs/gl.git] / source / core / pixelformat.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 <msp/io/print.h>
9 #include <msp/strings/format.h>
10 #include "pixelformat.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
18 {
19         if(conv.get()=="STENCIL_INDEX")
20                 comp = STENCIL_INDEX;
21         else if(conv.get()=="DEPTH_COMPONENT")
22                 comp = DEPTH_COMPONENT;
23         else if(conv.get()=="RED")
24                 comp = RED;
25         else if(conv.get()=="RG")
26                 comp = RG;
27         else if(conv.get()=="RGB")
28                 comp = RGB;
29         else if(conv.get()=="RGBA")
30                 comp = RGBA;
31         else if(conv.get()=="BGR")
32                 comp = BGR;
33         else if(conv.get()=="BGRA")
34                 comp = BGRA;
35         else if(conv.get()=="LUMINANCE")
36                 comp = LUMINANCE;
37         else if(conv.get()=="LUMINANCE_ALPHA")
38                 comp = LUMINANCE_ALPHA;
39         else
40                 throw lexical_error(format("conversion of '%s' to PixelComponents", conv.get()));
41 }
42
43 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
44 {
45         if(conv.get()=="R8")
46                 fmt = R8;
47         else if(conv.get()=="R16F")
48                 fmt = R16F;
49         else if(conv.get()=="R32F")
50                 fmt = R32F;
51         else if(conv.get()=="RG8")
52                 fmt = RG8;
53         else if(conv.get()=="RG16F")
54                 fmt = RG16F;
55         else if(conv.get()=="RG32F")
56                 fmt = RG32F;
57         else if(conv.get()=="RGB8")
58                 fmt = RGB8;
59         else if(conv.get()=="RGB16F")
60                 fmt = RGB16F;
61         else if(conv.get()=="RGB32F")
62                 fmt = RGB32F;
63         else if(conv.get()=="RGBA8")
64                 fmt = RGBA8;
65         else if(conv.get()=="RGBA16F")
66                 fmt = RGBA16F;
67         else if(conv.get()=="RGBA32F")
68                 fmt = RGBA32F;
69         else if(conv.get()=="SRGB8")
70                 fmt = SRGB8;
71         else if(conv.get()=="SRGB8_ALPHA8")
72                 fmt = SRGB8_ALPHA8;
73         else if(conv.get()=="BGR8")
74                 fmt = BGR8;
75         else if(conv.get()=="BGRA8")
76                 fmt = BGRA8;
77         else if(conv.get()=="LUMINANCE8")
78                 fmt = LUMINANCE8;
79         else if(conv.get()=="LUMINANCE8_ALPHA8")
80                 fmt = LUMINANCE8_ALPHA8;
81         else if(conv.get()=="DEPTH_COMPONENT16")
82                 fmt = DEPTH_COMPONENT16;
83         else if(conv.get()=="DEPTH_COMPONENT24")
84                 fmt = DEPTH_COMPONENT24;
85         else if(conv.get()=="DEPTH_COMPONENT32")
86                 fmt = DEPTH_COMPONENT32;
87         else if(conv.get()=="DEPTH_COMPONENT32F")
88                 fmt = DEPTH_COMPONENT32F;
89         else
90                 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
91 }
92
93 PixelComponents components_from_graphics(Graphics::PixelFormat pf)
94 {
95         switch(pf)
96         {
97         case Graphics::LUMINANCE: return LUMINANCE;
98         case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
99         case Graphics::RGB: return RGB;
100         case Graphics::RGBX:
101         case Graphics::RGBA: return RGBA;
102         case Graphics::BGR: return BGR;
103         case Graphics::BGRX:
104         case Graphics::BGRA: return BGRA;
105         default: throw invalid_argument("pixelformat_from_graphics");
106         }
107 }
108
109 PixelFormat pixelformat_from_image(const Graphics::Image &image)
110 {
111         PixelComponents comp = components_from_graphics(image.get_format());
112         return make_pixelformat(comp, UNSIGNED_BYTE);
113 }
114
115 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
116 {
117         bool normalized = !is_float(type);
118         return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
119 }
120
121 void require_pixelformat(PixelFormat pf)
122 {
123         /* TODO These checks are only accurate for textures.  On OpenGL ES some
124         formats are allowed for render buffers earlier than textures.  In particular
125         it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
126         depth textures are only available with 3.0 or the OES_depth_texture
127         extension.*/
128         switch(pf)
129         {
130         case RGB8:
131         case RGBA8:
132                 { static Require _req(OES_required_internalformat); }
133                 break;
134         case R8:
135         case RG8:
136                 { static Require _req(ARB_texture_rg); }
137                 break;
138         case R16F:
139         case R32F:
140         case RG16F:
141         case RG32F:
142                 { static Require _req(ARB_texture_rg); }
143                 { static Require _req(ARB_texture_float); }
144                 break;
145         case RGB16F:
146         case RGB32F:
147         case RGBA16F:
148         case RGBA32F:
149                 { static Require _req(ARB_texture_float); }
150                 break;
151         case SRGB8:
152         case SRGB8_ALPHA8:
153                 { static Require _req(EXT_texture_sRGB); }
154                 break;
155         case DEPTH_COMPONENT16:
156         case DEPTH_COMPONENT24:
157         case DEPTH_COMPONENT32:
158                 { static Require _req(ARB_depth_texture); }
159                 { static Require _req(OES_required_internalformat); }
160                 break;
161         case DEPTH_COMPONENT32F:
162                 { static Require _req(ARB_depth_buffer_float); }
163                 break;
164         case STENCIL_INDEX8:
165                 { static Require _req(OES_texture_stencil8); }
166                 break;
167         default:
168                 throw invalid_argument("require_pixelformat");
169         }
170 }
171
172 GLenum get_gl_components(PixelComponents comp)
173 {
174         switch(comp)
175         {
176         case RED: return GL_RED;
177         case RG: return GL_RG;
178         case RGB: return GL_RGB;
179         case RGBA: return GL_RGBA;
180         case DEPTH_COMPONENT: return GL_DEPTH_COMPONENT;
181         case STENCIL_INDEX: return GL_STENCIL_INDEX;
182         default: throw invalid_argument("get_gl_components");
183         }
184 }
185
186 GLenum get_gl_pixelformat(PixelFormat pf)
187 {
188         switch(pf)
189         {
190         case R8: return GL_R8;
191         case R16F: return GL_R16F;
192         case R32F: return GL_R32F;
193         case RG8: return GL_RG8;
194         case RG16F: return GL_RG16F;
195         case RG32F: return GL_RG32F;
196         case RGB8: return GL_RGB8;
197         case RGB16F: return GL_RGB16F;
198         case RGB32F: return GL_RGB32F;
199         case RGBA8: return GL_RGBA8;
200         case RGBA16F: return GL_RGBA16F;
201         case RGBA32F: return GL_RGBA32F;
202         case SRGB8: return GL_SRGB8;
203         case SRGB8_ALPHA8: return GL_SRGB8_ALPHA8;
204         case DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT16;
205         case DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT24;
206         case DEPTH_COMPONENT32: return GL_DEPTH_COMPONENT32;
207         case DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT32F;
208         case STENCIL_INDEX8: return GL_STENCIL_INDEX8;
209         default: throw invalid_argument("get_gl_pixelformat");
210         }
211 }
212
213 } // namespace GL
214 } // namespace Msp