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"
17 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
19 if(conv.get()=="STENCIL_INDEX")
21 else if(conv.get()=="DEPTH_COMPONENT")
22 comp = DEPTH_COMPONENT;
23 else if(conv.get()=="RED")
25 else if(conv.get()=="RG")
27 else if(conv.get()=="RGB")
29 else if(conv.get()=="RGBA")
31 else if(conv.get()=="BGR")
33 else if(conv.get()=="BGRA")
35 else if(conv.get()=="LUMINANCE")
37 else if(conv.get()=="LUMINANCE_ALPHA")
38 comp = LUMINANCE_ALPHA;
40 throw lexical_error(format("conversion of '%s' to PixelComponents", conv.get()));
43 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
47 else if(conv.get()=="R16F")
49 else if(conv.get()=="R32F")
51 else if(conv.get()=="RG8")
53 else if(conv.get()=="RG16F")
55 else if(conv.get()=="RG32F")
57 else if(conv.get()=="RGB8")
59 else if(conv.get()=="RGB16F")
61 else if(conv.get()=="RGB32F")
63 else if(conv.get()=="RGBA8")
65 else if(conv.get()=="RGBA16F")
67 else if(conv.get()=="RGBA32F")
69 else if(conv.get()=="SRGB8")
71 else if(conv.get()=="SRGB8_ALPHA8")
73 else if(conv.get()=="BGR8")
75 else if(conv.get()=="BGRA8")
77 else if(conv.get()=="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;
90 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
93 PixelComponents components_from_graphics(Graphics::PixelFormat pf)
97 case Graphics::LUMINANCE: return LUMINANCE;
98 case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
99 case Graphics::RGB: return RGB;
101 case Graphics::RGBA: return RGBA;
102 case Graphics::BGR: return BGR;
104 case Graphics::BGRA: return BGRA;
105 default: throw invalid_argument("pixelformat_from_graphics");
109 PixelFormat pixelformat_from_image(const Graphics::Image &image)
111 PixelComponents comp = components_from_graphics(image.get_format());
112 return make_pixelformat(comp, UNSIGNED_BYTE);
115 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
117 bool normalized = !is_float(type);
118 return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
121 void require_pixelformat(PixelFormat pf)
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
132 { static Require _req(OES_required_internalformat); }
136 { static Require _req(ARB_texture_rg); }
142 { static Require _req(ARB_texture_rg); }
143 { static Require _req(ARB_texture_float); }
149 { static Require _req(ARB_texture_float); }
153 { static Require _req(EXT_texture_sRGB); }
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); }
161 case DEPTH_COMPONENT32F:
162 { static Require _req(ARB_depth_buffer_float); }
165 { static Require _req(OES_texture_stencil8); }
168 throw invalid_argument("require_pixelformat");
172 GLenum get_gl_components(PixelComponents comp)
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");
186 GLenum get_gl_pixelformat(PixelFormat pf)
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");