]> git.cworth.org Git - apitrace/blob - retrace/glstate.cpp
0d5a5f3bd629ec2677451098f44060ad5d4525c6
[apitrace] / retrace / glstate.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <string.h>
28
29 #include <algorithm>
30 #include <iostream>
31
32 #include "image.hpp"
33 #include "json.hpp"
34 #include "glproc.hpp"
35 #include "glsize.hpp"
36 #include "glstate.hpp"
37 #include "glstate_internal.hpp"
38
39
40 namespace glstate {
41
42
43 Context::Context(void) {
44     memset(this, 0, sizeof *this);
45
46     const char *version = (const char *)glGetString(GL_VERSION);
47     if (version) {
48         if (version[0] == 'O' &&
49             version[1] == 'p' &&
50             version[2] == 'e' &&
51             version[3] == 'n' &&
52             version[4] == 'G' &&
53             version[5] == 'L' &&
54             version[6] == ' ' &&
55             version[7] == 'E' &&
56             version[8] == 'S' &&
57             (version[9] == ' ' || version[9] == '-')) {
58             ES = true;
59         }
60     }
61
62     ARB_draw_buffers = !ES;
63
64     // TODO: Check extensions we use below
65 }
66
67 void
68 Context::resetPixelPackState(void) {
69     if (!ES) {
70         glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
71         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
72         glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
73         glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
74         glPixelStorei(GL_PACK_ROW_LENGTH, 0);
75         glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
76         glPixelStorei(GL_PACK_SKIP_ROWS, 0);
77         glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
78         glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
79     } else {
80         packAlignment = 4;
81         glGetIntegerv(GL_PACK_ALIGNMENT, &packAlignment);
82     }
83     glPixelStorei(GL_PACK_ALIGNMENT, 1);
84 }
85
86 void
87 Context::restorePixelPackState(void) {
88     if (!ES) {
89         glPopClientAttrib();
90     } else {
91         glPixelStorei(GL_PACK_ALIGNMENT, packAlignment);
92     }
93 }
94
95
96 static const GLenum bindings[] = {
97     GL_DRAW_BUFFER,
98     GL_READ_BUFFER,
99     GL_PIXEL_PACK_BUFFER_BINDING,
100     GL_PIXEL_UNPACK_BUFFER_BINDING,
101     GL_TEXTURE_BINDING_1D,
102     GL_TEXTURE_BINDING_2D,
103     GL_TEXTURE_BINDING_3D,
104     GL_TEXTURE_BINDING_RECTANGLE,
105     GL_TEXTURE_BINDING_CUBE_MAP,
106     GL_DRAW_FRAMEBUFFER_BINDING,
107     GL_READ_FRAMEBUFFER_BINDING,
108     GL_RENDERBUFFER_BINDING,
109     GL_DRAW_BUFFER0,
110     GL_DRAW_BUFFER1,
111     GL_DRAW_BUFFER2,
112     GL_DRAW_BUFFER3,
113     GL_DRAW_BUFFER4,
114     GL_DRAW_BUFFER5,
115     GL_DRAW_BUFFER6,
116     GL_DRAW_BUFFER7,
117 };
118
119
120 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
121
122
123 void dumpCurrentContext(std::ostream &os)
124 {
125     JSONWriter json(os);
126
127 #ifndef NDEBUG
128     GLint old_bindings[NUM_BINDINGS];
129     for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
130         old_bindings[i] = 0;
131         glGetIntegerv(bindings[i], &old_bindings[i]);
132     }
133 #endif
134
135     Context context;
136
137     dumpParameters(json, context);
138     dumpShadersUniforms(json, context);
139     dumpTextures(json, context);
140     dumpFramebuffer(json, context);
141
142 #ifndef NDEBUG
143     for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
144         GLint new_binding = 0;
145         glGetIntegerv(bindings[i], &new_binding);
146         if (new_binding != old_bindings[i]) {
147             std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
148         }
149     }
150 #endif
151
152 }
153
154
155 } /* namespace glstate */