File: glStateDebug.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (215 lines) | stat: -rw-r--r-- 7,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#if defined(DEBUG_GLSTATE)

#define NO_GL_WRAP

#include <map>
#include <string>
#include <set>

#include "glStateDebug.h"

#include "System/Log/ILog.h"

#include "System/Platform/Threading.h"

template<typename T, typename F>
static void VERIFYGL(F func, GLenum pname, T defaultValue, std::string pstr, std::string area) {
	if (CGLStateChecker::errorsSet.find(area + pstr) == CGLStateChecker::errorsSet.end()) {
		T _temp;
		func(pname, &_temp);
		if (_temp != defaultValue) {
			LOG_L(L_ERROR, "%s was not set correctly when %s: %f", pstr.c_str(), area.c_str(), (float) _temp);
			LOG_L(L_ERROR, "last time set here: %s", CGLStateChecker::lastSet[pstr].c_str());
			CGLStateChecker::errorsSet.insert(area + pstr);
		}
	}
}

template<typename T, typename F>
static void VERIFYGL2(F func, GLenum pname, T default0, T default1, std::string pstr, std::string area) {
	if (CGLStateChecker::errorsSet.find(area + pstr) == CGLStateChecker::errorsSet.end()) {
		T _temp[4];
		func(pname, _temp);
		if (_temp[0] != default0 || _temp[1] != default1) {
			LOG_L(L_ERROR, "%s was not set correctly when %s: %f %f", pstr.c_str(), area.c_str(), (float) _temp[0], (float) _temp[1]);
			LOG_L(L_ERROR, "last time set here: %s", CGLStateChecker::lastSet[pstr].c_str());
			CGLStateChecker::errorsSet.insert(area + pstr);
		}
	}
}

template<typename T, typename F>
static void VERIFYGL4(F func, GLenum pname, T default0, T default1, T default2, T default3, std::string pstr, std::string area) {
	if (CGLStateChecker::errorsSet.find(area + pstr) == CGLStateChecker::errorsSet.end()) {
		T _temp[4];
		func(pname, _temp);
		if (_temp[0] != default0 || _temp[1] != default1 || _temp[2] != default2 || _temp[3] != default3) {
			LOG_L(L_ERROR, "%s was not set correctly when %s: %f %f %f %f", pstr.c_str(), area.c_str(), (float) _temp[0], (float) _temp[1], (float) _temp[2], (float) _temp[3]);
			LOG_L(L_ERROR, "last time set here: %s", CGLStateChecker::lastSet[pstr].c_str());
			CGLStateChecker::errorsSet.insert(area + pstr);
		}
	}
}
	
#define VERIFYGLBOOL(pname, defaultValue, area) VERIFYGL(glGetBooleanv, pname, (GLboolean) defaultValue, #pname, area)
#define VERIFYGLBOOL4(pname, default0, default1, default2, default3, area) VERIFYGL4(glGetBooleanv, pname, (GLboolean) default0, (GLboolean) default1, (GLboolean) default2, (GLboolean) default3, #pname, area)

#define VERIFYGLFLOAT(pname, defaultValue, area) VERIFYGL(glGetFloatv, pname, (GLfloat) defaultValue, #pname, area)
#define VERIFYGLFLOAT4(pname, default0, default1, default2, default3, area) VERIFYGL4(glGetFloatv, pname, (GLfloat) default0, (GLfloat) default1, (GLfloat) default2, (GLfloat) default3, #pname, area)

#define VERIFYGLINT(pname, defaultValue, area) VERIFYGL(glGetIntegerv, pname, (GLint) defaultValue, #pname, area)
#define VERIFYGLINT2(pname, default0, default1, area) VERIFYGL2(glGetIntegerv, pname, (GLint) default0, (GLint) default1, #pname, area)

std::map<std::string, std::string> CGLStateChecker::lastSet;
std::set<std::string> CGLStateChecker::errorsSet;


void _wrap_glEnable(GLenum pname, std::string pstr, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet[pstr] = location;
	}
	glEnable(pname);
}

void _wrap_glDisable(GLenum pname, std::string pstr, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet[pstr] = location;
	}
	glDisable(pname);
}

void _wrap_glBlendFunc(GLenum sfactor, GLenum dfactor, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_BLEND_SRC_RGB"] = location;
		CGLStateChecker::lastSet["GL_BLEND_SRC_ALPHA"] = location;
		CGLStateChecker::lastSet["GL_BLEND_DST_RGB"] = location;
		CGLStateChecker::lastSet["GL_BLEND_DST_ALPHA"] = location;
	}
	glBlendFunc(sfactor, dfactor);
}

void _wrap_glBlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_BLEND_SRC_RGB"] = location;
		CGLStateChecker::lastSet["GL_BLEND_SRC_ALPHA"] = location;
		CGLStateChecker::lastSet["GL_BLEND_DST_RGB"] = location;
		CGLStateChecker::lastSet["GL_BLEND_DST_ALPHA"] = location;
	}
	glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
}

void _wrap_glColor3f(GLfloat red, GLfloat green, GLfloat blue, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_CURRENT_COLOR"] = location;
	}
	glColor3f(red, green, blue);
}

void _wrap_glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_CURRENT_COLOR"] = location;
	}
	glColor4f(red, green, blue, alpha);
}

void _wrap_glColor4fv(const GLfloat *v, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_CURRENT_COLOR"] = location;
	}
	glColor4fv(v);
}

void _wrap_glDepthMask(GLboolean flag, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_DEPTH_WRITEMASK"] = location;
	}
	glDepthMask(flag);
}


void _wrap_glDepthFunc(GLenum func, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_DEPTH_FUNC"] = location;
	}
	glDepthFunc(func);
}

void _wrap_glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha, std::string location)
{
	if(Threading::IsMainThread())
	{
		CGLStateChecker::lastSet["GL_COLOR_WRITEMASK"] = location;
	}
	glColorMask(red, green, blue, alpha);
}

void CGLStateChecker::VerifyState(std::string area) {
	if (Threading::IsMainThread())
	{
		std::string _area = area + " " + id;
		//VERIFYGLBOOL(GL_CULL_FACE, GL_FALSE)
		VERIFYGLBOOL(GL_ALPHA_TEST, GL_FALSE, _area);
		VERIFYGLBOOL(GL_LIGHTING, GL_FALSE, _area);
		VERIFYGLBOOL(GL_SCISSOR_TEST, GL_FALSE, _area);
		VERIFYGLBOOL(GL_STENCIL_TEST, GL_FALSE, _area);
		// VERIFYGLBOOL(GL_TEXTURE_2D, GL_FALSE);
		// VERIFYGLBOOL(GL_TEXTURE_GEN_S, GL_FALSE);
		// VERIFYGLBOOL(GL_TEXTURE_GEN_T, GL_FALSE);
		// VERIFYGLBOOL(GL_TEXTURE_GEN_R, GL_FALSE);
		// VERIFYGLBOOL(GL_TEXTURE_GEN_Q, GL_FALSE);
		VERIFYGLINT(GL_STENCIL_WRITEMASK, ~0, _area);
		VERIFYGLINT2(GL_POLYGON_MODE, GL_FILL, GL_FILL, _area);
		VERIFYGLBOOL(GL_POLYGON_OFFSET_FILL, GL_FALSE, _area);
		VERIFYGLBOOL(GL_POLYGON_OFFSET_LINE, GL_FALSE, _area);
		VERIFYGLBOOL(GL_POLYGON_OFFSET_POINT, GL_FALSE, _area);
		VERIFYGLBOOL(GL_LINE_STIPPLE, GL_FALSE, _area);
		VERIFYGLBOOL(GL_CLIP_PLANE0, GL_FALSE, _area);
		VERIFYGLBOOL(GL_CLIP_PLANE1, GL_FALSE, _area);
		VERIFYGLBOOL(GL_CLIP_PLANE2, GL_FALSE, _area);
		VERIFYGLBOOL(GL_CLIP_PLANE3, GL_FALSE, _area);
		VERIFYGLBOOL(GL_CLIP_PLANE4, GL_FALSE, _area);
		VERIFYGLBOOL(GL_CLIP_PLANE5, GL_FALSE, _area);
		VERIFYGLFLOAT(GL_LINE_WIDTH, 1.0f, _area);
		VERIFYGLFLOAT(GL_POINT_SIZE, 1.0f, _area);
		VERIFYGLFLOAT4(GL_CURRENT_COLOR, 1.0f, 1.0f, 1.0f, 1.0f, _area);
		VERIFYGLBOOL4(GL_COLOR_WRITEMASK, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE, _area);
		VERIFYGLBOOL(GL_DEPTH_WRITEMASK, GL_TRUE, _area);
		VERIFYGLINT(GL_DEPTH_FUNC, GL_LEQUAL, _area);
		VERIFYGLINT(GL_BLEND_SRC_RGB, GL_SRC_ALPHA, _area);
		VERIFYGLINT(GL_BLEND_SRC_ALPHA, GL_SRC_ALPHA, _area);
		VERIFYGLINT(GL_BLEND_DST_RGB, GL_ONE_MINUS_SRC_ALPHA, _area);
		VERIFYGLINT(GL_BLEND_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA, _area);
	}
}

CGLStateChecker::CGLStateChecker(std::string id) : id(id)
{
	VerifyState("entering");
}

CGLStateChecker::~CGLStateChecker()
{
	VerifyState("exiting");
}

#endif //DEBUG_GLSTATE