1 ##########################################################################
3 # Copyright 2010 VMware, Inc.
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 ##########################################################################/
27 """GL retracer generator."""
32 from retrace import Retracer
35 class GlRetracer(Retracer):
37 def retrace_function(self, function):
38 Retracer.retrace_function(self, function)
40 array_pointer_function_names = set((
48 "glSecondaryColorPointer",
50 "glInterleavedArrays",
56 "glTexCoordPointerEXT",
57 "glEdgeFlagPointerEXT",
58 "glFogCoordPointerEXT",
59 "glSecondaryColorPointerEXT",
61 "glVertexAttribPointer",
62 "glVertexAttribPointerARB",
63 "glVertexAttribPointerNV",
64 "glVertexAttribIPointer",
65 "glVertexAttribIPointerEXT",
66 "glVertexAttribLPointer",
67 "glVertexAttribLPointerEXT",
69 #"glMatrixIndexPointerARB",
72 draw_array_function_names = set([
75 "glDrawArraysIndirect",
76 "glDrawArraysInstanced",
77 "glDrawArraysInstancedARB",
78 "glDrawArraysInstancedEXT",
79 "glDrawMeshArraysSUN",
81 "glMultiDrawArraysEXT",
82 "glMultiModeDrawArraysIBM",
85 draw_elements_function_names = set([
87 "glDrawElementsBaseVertex",
88 "glDrawElementsIndirect",
89 "glDrawElementsInstanced",
90 "glDrawElementsInstancedARB",
91 "glDrawElementsInstancedBaseVertex",
92 "glDrawElementsInstancedEXT",
93 "glDrawRangeElements",
94 "glDrawRangeElementsBaseVertex",
95 "glDrawRangeElementsEXT",
96 #"glMultiDrawElements",
97 #"glMultiDrawElementsBaseVertex",
98 #"glMultiDrawElementsEXT",
99 #"glMultiModeDrawElementsIBM",
102 def retrace_function_body(self, function):
103 is_array_pointer = function.name in self.array_pointer_function_names
104 is_draw_array = function.name in self.draw_array_function_names
105 is_draw_elements = function.name in self.draw_elements_function_names
107 if is_array_pointer or is_draw_array or is_draw_elements:
108 print ' if (glretrace::parser.version < 1) {'
110 if is_array_pointer or is_draw_array:
111 print ' GLint __array_buffer = 0;'
112 print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
113 print ' if (!__array_buffer) {'
114 self.fail_function(function)
118 print ' GLint __element_array_buffer = 0;'
119 print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
120 print ' if (!__element_array_buffer) {'
121 self.fail_function(function)
126 Retracer.retrace_function_body(self, function)
128 def call_function(self, function):
129 if function.name == "glViewport":
130 print ' if (glretrace::drawable) {'
131 print ' int drawable_width = x + width;'
132 print ' int drawable_height = y + height;'
133 print ' if (drawable_width > (int)glretrace::drawable->width ||'
134 print ' drawable_height > (int)glretrace::drawable->height) {'
135 print ' glretrace::drawable->resize(drawable_width, drawable_height);'
139 if function.name == "glEnd":
140 print ' glretrace::insideGlBeginEnd = false;'
142 Retracer.call_function(self, function)
144 if function.name == "glBegin":
145 print ' glretrace::insideGlBeginEnd = true;'
146 elif function.name.startswith('gl'):
147 # glGetError is not allowed inside glBegin/glEnd
148 print ' glretrace::checkGlError(call);'
150 if function.name == 'glFlush':
151 print ' if (!glretrace::double_buffer) {'
152 print ' glretrace::frame_complete(call.no);'
155 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
156 if function.name in self.array_pointer_function_names and arg.name == 'pointer':
157 print ' %s = static_cast<%s>(%s.toPointer());' % (lvalue, arg_type, rvalue)
160 if function.name in self.draw_elements_function_names and arg.name == 'indices':
161 print ' %s = %s.toPointer();' % (lvalue, rvalue)
164 if function.name.startswith('glUniform') and function.args[0].name == arg.name == 'location':
165 print ' GLint program = -1;'
166 print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);'
168 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
171 if __name__ == '__main__':
175 #include "glproc.hpp"
176 #include "glretrace.hpp"
181 api.add_function(glapi.memcpy)
182 retracer = GlRetracer()
183 retracer.retrace_api(api)