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/strings/format.h>
9 #include "pixelformat.h"
16 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
18 if(conv.get()=="STENCIL_INDEX")
20 else if(conv.get()=="DEPTH_COMPONENT")
21 comp = DEPTH_COMPONENT;
22 else if(conv.get()=="RED")
24 else if(conv.get()=="RG")
26 else if(conv.get()=="RGB")
28 else if(conv.get()=="RGBA")
30 else if(conv.get()=="BGR")
32 else if(conv.get()=="BGRA")
34 else if(conv.get()=="LUMINANCE")
36 else if(conv.get()=="LUMINANCE_ALPHA")
37 comp = LUMINANCE_ALPHA;
39 throw lexical_error(format("conversion of '%s' to PixelComponents", conv.get()));
42 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
46 else if(conv.get()=="R16F")
48 else if(conv.get()=="R32F")
50 else if(conv.get()=="RG8")
52 else if(conv.get()=="RG16F")
54 else if(conv.get()=="RG32F")
56 else if(conv.get()=="RGB8")
58 else if(conv.get()=="RGB16F")
60 else if(conv.get()=="RGB32F")
62 else if(conv.get()=="RGBA8")
64 else if(conv.get()=="RGBA16F")
66 else if(conv.get()=="RGBA32F")
68 else if(conv.get()=="SRGB8")
70 else if(conv.get()=="SRGB8_ALPHA8")
72 else if(conv.get()=="BGR8")
74 else if(conv.get()=="BGRA8")
76 else if(conv.get()=="LUMINANCE8")
78 else if(conv.get()=="LUMINANCE8_ALPHA8")
79 fmt = LUMINANCE8_ALPHA8;
80 else if(conv.get()=="DEPTH_COMPONENT16")
81 fmt = DEPTH_COMPONENT16;
82 else if(conv.get()=="DEPTH_COMPONENT24")
83 fmt = DEPTH_COMPONENT24;
84 else if(conv.get()=="DEPTH_COMPONENT32")
85 fmt = DEPTH_COMPONENT32;
86 else if(conv.get()=="DEPTH_COMPONENT32F")
87 fmt = DEPTH_COMPONENT32F;
89 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
92 PixelComponents components_from_graphics(Graphics::PixelFormat pf)
96 case Graphics::LUMINANCE: return LUMINANCE;
97 case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
98 case Graphics::RGB: return RGB;
100 case Graphics::RGBA: return RGBA;
101 case Graphics::BGR: return BGR;
103 case Graphics::BGRA: return BGRA;
104 default: throw invalid_argument("pixelformat_from_graphics");
108 PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
110 PixelComponents comp = components_from_graphics(image.get_format());
111 return make_pixelformat(comp, UNSIGNED_BYTE, srgb);
114 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
116 bool normalized = !is_float(type);
117 return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
120 void require_pixelformat(PixelFormat pf)
122 /* TODO These checks are only accurate for textures. On OpenGL ES some
123 formats are allowed for render buffers earlier than textures. In particular
124 it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
125 depth textures are only available with 3.0 or the OES_depth_texture
131 { static Require _req(OES_required_internalformat); }
135 { static Require _req(ARB_texture_rg); }
141 { static Require _req(ARB_texture_rg); }
142 { static Require _req(ARB_texture_float); }
148 { static Require _req(ARB_texture_float); }
152 { static Require _req(EXT_texture_sRGB); }
154 case DEPTH_COMPONENT16:
155 case DEPTH_COMPONENT24:
156 case DEPTH_COMPONENT32:
157 { static Require _req(ARB_depth_texture); }
158 { static Require _req(OES_required_internalformat); }
160 case DEPTH_COMPONENT32F:
161 { static Require _req(ARB_depth_buffer_float); }
164 { static Require _req(OES_texture_stencil8); }
167 throw invalid_argument("require_pixelformat");
171 unsigned get_gl_components(PixelComponents comp)
175 case RED: return GL_RED;
176 case RG: return GL_RG;
177 case RGB: return GL_RGB;
178 case RGBA: return GL_RGBA;
179 case DEPTH_COMPONENT: return GL_DEPTH_COMPONENT;
180 case STENCIL_INDEX: return GL_STENCIL_INDEX;
181 default: throw invalid_argument("get_gl_components");
185 unsigned get_gl_pixelformat(PixelFormat pf)
189 case R8: return GL_R8;
190 case R16F: return GL_R16F;
191 case R32F: return GL_R32F;
192 case RG8: return GL_RG8;
193 case RG16F: return GL_RG16F;
194 case RG32F: return GL_RG32F;
195 case RGB8: return GL_RGB8;
196 case RGB16F: return GL_RGB16F;
197 case RGB32F: return GL_RGB32F;
198 case RGBA8: return GL_RGBA8;
199 case RGBA16F: return GL_RGBA16F;
200 case RGBA32F: return GL_RGBA32F;
201 case SRGB8: return GL_SRGB8;
202 case SRGB8_ALPHA8: return GL_SRGB8_ALPHA8;
203 case DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT16;
204 case DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT24;
205 case DEPTH_COMPONENT32: return GL_DEPTH_COMPONENT32;
206 case DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT32F;
207 case STENCIL_INDEX8: return GL_STENCIL_INDEX8;
208 default: throw invalid_argument("get_gl_pixelformat");