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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
##########################################################################
#
# Copyright 2010 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/
"""GL retracer generator."""
import stdapi
import glapi
from retrace import Retracer
class GlRetracer(Retracer):
def retrace_function(self, function):
Retracer.retrace_function(self, function)
array_pointer_function_names = set((
"glVertexPointer",
"glNormalPointer",
"glColorPointer",
"glIndexPointer",
"glTexCoordPointer",
"glEdgeFlagPointer",
"glFogCoordPointer",
"glSecondaryColorPointer",
"glInterleavedArrays",
"glVertexPointerEXT",
"glNormalPointerEXT",
"glColorPointerEXT",
"glIndexPointerEXT",
"glTexCoordPointerEXT",
"glEdgeFlagPointerEXT",
"glFogCoordPointerEXT",
"glSecondaryColorPointerEXT",
"glVertexAttribPointer",
"glVertexAttribPointerARB",
"glVertexAttribPointerNV",
"glVertexAttribIPointer",
"glVertexAttribIPointerEXT",
"glVertexAttribLPointer",
"glVertexAttribLPointerEXT",
#"glMatrixIndexPointerARB",
))
draw_array_function_names = set([
"glDrawArrays",
"glDrawArraysEXT",
"glDrawArraysIndirect",
"glDrawArraysInstanced",
"glDrawArraysInstancedARB",
"glDrawArraysInstancedEXT",
"glDrawMeshArraysSUN",
"glMultiDrawArrays",
"glMultiDrawArraysEXT",
"glMultiModeDrawArraysIBM",
])
draw_elements_function_names = set([
"glDrawElements",
"glDrawElementsBaseVertex",
"glDrawElementsIndirect",
"glDrawElementsInstanced",
"glDrawElementsInstancedARB",
"glDrawElementsInstancedBaseVertex",
"glDrawElementsInstancedEXT",
"glDrawRangeElements",
"glDrawRangeElementsBaseVertex",
"glDrawRangeElementsEXT",
#"glMultiDrawElements",
#"glMultiDrawElementsBaseVertex",
#"glMultiDrawElementsEXT",
#"glMultiModeDrawElementsIBM",
])
def retrace_function_body(self, function):
is_array_pointer = function.name in self.array_pointer_function_names
is_draw_array = function.name in self.draw_array_function_names
is_draw_elements = function.name in self.draw_elements_function_names
if is_array_pointer or is_draw_array or is_draw_elements:
print ' if (glretrace::parser.version < 1) {'
if is_array_pointer or is_draw_array:
print ' GLint __array_buffer = 0;'
print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
print ' if (!__array_buffer) {'
self.fail_function(function)
print ' }'
if is_draw_elements:
print ' GLint __element_array_buffer = 0;'
print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
print ' if (!__element_array_buffer) {'
self.fail_function(function)
print ' }'
print ' }'
Retracer.retrace_function_body(self, function)
if function.name in ('glFlush', 'glFinish'):
print ' if (!glretrace::double_buffer) {'
print ' glretrace::frame_complete(call.no);'
print ' }'
if function.name == 'glReadPixels':
print ' glFinish();'
print ' glretrace::snapshot(call.no);'
def call_function(self, function):
if function.name == "glViewport":
print ' if (glretrace::drawable) {'
print ' int drawable_width = x + width;'
print ' int drawable_height = y + height;'
print ' if (drawable_width > (int)glretrace::drawable->width ||'
print ' drawable_height > (int)glretrace::drawable->height) {'
print ' glretrace::drawable->resize(drawable_width, drawable_height);'
print ' if (!glretrace::drawable->visible) {'
print ' glretrace::drawable->show();'
print ' }'
print ' glScissor(0, 0, drawable_width, drawable_height);'
print ' }'
print ' }'
if function.name == "glEnd":
print ' glretrace::insideGlBeginEnd = false;'
if function.name == 'memcpy':
print ' if (!dest || !src || !n) return;'
Retracer.call_function(self, function)
# Error checking
if function.name == "glBegin":
print ' glretrace::insideGlBeginEnd = true;'
elif function.name.startswith('gl'):
# glGetError is not allowed inside glBegin/glEnd
print ' if (!glretrace::benchmark && !glretrace::insideGlBeginEnd) {'
print ' glretrace::checkGlError(call);'
if function.name in ('glProgramStringARB', 'glProgramStringNV'):
print r' GLint error_position = -1;'
print r' glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_position);'
print r' if (error_position != -1) {'
print r' const char *error_string = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);'
print r' std::cerr << call.no << ": warning: " << error_string << "\n";'
print r' }'
if function.name == 'glCompileShader':
print r' GLint compile_status = 0;'
print r' glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);'
print r' if (!compile_status) {'
print r' GLint info_log_length = 0;'
print r' glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);'
print r' GLchar *infoLog = new GLchar[info_log_length];'
print r' glGetShaderInfoLog(shader, info_log_length, NULL, infoLog);'
print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
print r' delete [] infoLog;'
print r' }'
if function.name == 'glLinkProgram':
print r' GLint link_status = 0;'
print r' glGetProgramiv(program, GL_LINK_STATUS, &link_status);'
print r' if (!link_status) {'
print r' GLint info_log_length = 0;'
print r' glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);'
print r' GLchar *infoLog = new GLchar[info_log_length];'
print r' glGetProgramInfoLog(program, info_log_length, NULL, infoLog);'
print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
print r' delete [] infoLog;'
print r' }'
if function.name == 'glCompileShaderARB':
print r' GLint compile_status = 0;'
print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_COMPILE_STATUS_ARB, &compile_status);'
print r' if (!compile_status) {'
print r' GLint info_log_length = 0;'
print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);'
print r' GLchar *infoLog = new GLchar[info_log_length];'
print r' glGetInfoLogARB(shaderObj, info_log_length, NULL, infoLog);'
print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
print r' delete [] infoLog;'
print r' }'
if function.name == 'glLinkProgramARB':
print r' GLint link_status = 0;'
print r' glGetObjectParameterivARB(programObj, GL_OBJECT_LINK_STATUS_ARB, &link_status);'
print r' if (!link_status) {'
print r' GLint info_log_length = 0;'
print r' glGetObjectParameterivARB(programObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);'
print r' GLchar *infoLog = new GLchar[info_log_length];'
print r' glGetInfoLogARB(programObj, info_log_length, NULL, infoLog);'
print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
print r' delete [] infoLog;'
print r' }'
if function.name in ('glMapBuffer', 'glMapBufferARB', 'glMapBufferRange', 'glMapNamedBufferEXT', 'glMapNamedBufferRangeEXT'):
print r' if (!__result) {'
print r' std::cerr << call.no << ": warning: failed to map buffer\n";'
print r' }'
if function.name in ('glGetAttribLocation', 'glGetAttribLocationARB'):
print r' GLint __orig_result = call.ret->toSInt();'
print r' if (__result != __orig_result) {'
print r' std::cerr << call.no << ": warning vertex attrib location mismatch " << __orig_result << " -> " << __result << "\n";'
print r' }'
print ' }'
def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
if function.name in self.array_pointer_function_names and arg.name == 'pointer':
print ' %s = static_cast<%s>(%s.toPointer());' % (lvalue, arg_type, rvalue)
return
if function.name in self.draw_elements_function_names and arg.name == 'indices':
print ' %s = %s.toPointer();' % (lvalue, rvalue)
return
if arg.type is glapi.GLlocation \
and 'program' not in [arg.name for arg in function.args]:
print ' GLint program = -1;'
print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);'
if arg.type is glapi.GLlocationARB \
and 'programObj' not in [arg.name for arg in function.args]:
print ' GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);'
Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
# Don't try to use more samples than the implementation supports
if arg.name == 'samples':
assert arg.type is glapi.GLsizei
print ' GLint max_samples = 0;'
print ' glGetIntegerv(GL_MAX_SAMPLES, &max_samples);'
print ' if (samples > max_samples) {'
print ' samples = max_samples;'
print ' }'
if __name__ == '__main__':
print r'''
#include <string.h>
#include "glproc.hpp"
#include "glretrace.hpp"
'''
api = glapi.glapi
api.add_function(glapi.memcpy)
retracer = GlRetracer()
retracer.retrace_api(api)
|