1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
37 #include "glstate.hpp"
38 #include "glstate_internal.hpp"
44 Context::Context(void) {
45 memset(this, 0, sizeof *this);
47 const char *version = (const char *)glGetString(GL_VERSION);
48 unsigned version_major = 0;
49 unsigned version_minor = 0;
50 unsigned version_release = 0;
52 if (version[0] == 'O' &&
61 (version[9] == ' ' || version[9] == '-')) {
64 if (version[0] >= '0' && version[0] <= '9') {
65 sscanf(version, "%u.%u.%u", &version_major, &version_minor, &version_release);
69 ARB_draw_buffers = !ES;
71 // Check extensions we use.
74 if (version_major > 3 ||
75 (version_major == 3 && version_minor >= 2)) {
76 GLint num_extensions = 0;
77 glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
78 for (GLint i = 0; i < num_extensions; ++i) {
79 const char *extension = (const char *)glGetStringi(GL_EXTENSIONS, i);
81 if (strcmp(extension, "GL_ARB_sampler_objects") == 0) {
82 ARB_sampler_objects = true;
83 } else if (strcmp(extension, "GL_KHR_debug") == 0) {
89 const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
90 ARB_sampler_objects = glws::checkExtension("GL_ARB_sampler_objects", extensions);
91 KHR_debug = glws::checkExtension("GL_KHR_debug", extensions);
94 const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
95 KHR_debug = glws::checkExtension("GL_KHR_debug", extensions);
100 Context::resetPixelPackState(void) {
101 // Start with default state
103 pack_image_height = 0;
104 pack_lsb_first = GL_FALSE;
106 pack_skip_images = 0;
107 pack_skip_pixels = 0;
109 pack_swap_bytes = GL_FALSE;
110 pixel_pack_buffer_binding = 0;
113 glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment);
115 glGetIntegerv(GL_PACK_IMAGE_HEIGHT, &pack_image_height);
116 glGetIntegerv(GL_PACK_LSB_FIRST, &pack_lsb_first);
117 glGetIntegerv(GL_PACK_ROW_LENGTH, &pack_row_length);
118 glGetIntegerv(GL_PACK_SKIP_IMAGES, &pack_skip_images);
119 glGetIntegerv(GL_PACK_SKIP_PIXELS, &pack_skip_pixels);
120 glGetIntegerv(GL_PACK_SKIP_ROWS, &pack_skip_rows);
121 glGetIntegerv(GL_PACK_SWAP_BYTES, &pack_swap_bytes);
122 glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &pixel_pack_buffer_binding);
125 // Reset state for compact images
126 glPixelStorei(GL_PACK_ALIGNMENT, 1);
128 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
129 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
130 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
131 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
132 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
133 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
134 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
135 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
140 Context::restorePixelPackState(void) {
141 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment);
143 glPixelStorei(GL_PACK_IMAGE_HEIGHT, pack_image_height);
144 glPixelStorei(GL_PACK_LSB_FIRST, pack_lsb_first);
145 glPixelStorei(GL_PACK_ROW_LENGTH, pack_row_length);
146 glPixelStorei(GL_PACK_SKIP_IMAGES, pack_skip_images);
147 glPixelStorei(GL_PACK_SKIP_PIXELS, pack_skip_pixels);
148 glPixelStorei(GL_PACK_SKIP_ROWS, pack_skip_rows);
149 glPixelStorei(GL_PACK_SWAP_BYTES, pack_swap_bytes);
150 glBindBuffer(GL_PIXEL_PACK_BUFFER, pixel_pack_buffer_binding);
156 * Dump a GL_KHR_debug object label.
159 dumpObjectLabel(JSONWriter &json, Context &context, GLenum identifier, GLuint name, const char *member)
165 if (!context.KHR_debug) {
170 glGetObjectLabel(identifier, name, 0, &length, NULL);
175 char *label = (char *)malloc(length + 1);
180 glGetObjectLabel(identifier, name, length + 1, NULL, label);
182 json.writeStringMember(member, label);
188 static const GLenum bindings[] = {
191 GL_PIXEL_PACK_BUFFER_BINDING,
192 GL_PIXEL_UNPACK_BUFFER_BINDING,
193 GL_TEXTURE_BINDING_1D,
194 GL_TEXTURE_BINDING_1D_ARRAY,
195 GL_TEXTURE_BINDING_2D,
196 GL_TEXTURE_BINDING_2D_ARRAY,
197 GL_TEXTURE_BINDING_2D_MULTISAMPLE,
198 GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY,
199 GL_TEXTURE_BINDING_3D,
200 GL_TEXTURE_BINDING_RECTANGLE,
201 GL_TEXTURE_BINDING_CUBE_MAP,
202 GL_TEXTURE_BINDING_CUBE_MAP_ARRAY,
203 GL_DRAW_FRAMEBUFFER_BINDING,
204 GL_READ_FRAMEBUFFER_BINDING,
205 GL_RENDERBUFFER_BINDING,
217 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
220 void dumpCurrentContext(std::ostream &os)
225 GLint old_bindings[NUM_BINDINGS];
226 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
228 glGetIntegerv(bindings[i], &old_bindings[i]);
234 dumpParameters(json, context);
235 dumpShadersUniforms(json, context);
236 dumpTextures(json, context);
237 dumpFramebuffer(json, context);
240 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
241 GLint new_binding = 0;
242 glGetIntegerv(bindings[i], &new_binding);
243 if (new_binding != old_bindings[i]) {
244 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
252 } /* namespace glstate */