José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 1 | ########################################################################## |
| 2 | # |
| 3 | # Copyright 2010 VMware, Inc. |
| 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 | |
José Fonseca | 4a826ed | 2010-11-30 16:58:22 +0000 | [diff] [blame] | 27 | """GL retracer generator.""" |
| 28 | |
| 29 | |
José Fonseca | 9796b84 | 2010-11-25 11:44:50 +0000 | [diff] [blame] | 30 | import stdapi |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 31 | import glapi |
José Fonseca | dacd8dd | 2010-11-25 17:50:26 +0000 | [diff] [blame] | 32 | from retrace import Retracer |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 33 | |
| 34 | |
José Fonseca | dacd8dd | 2010-11-25 17:50:26 +0000 | [diff] [blame] | 35 | class GlRetracer(Retracer): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 36 | |
José Fonseca | 3d245f4 | 2010-11-28 00:08:23 +0000 | [diff] [blame] | 37 | def retrace_function(self, function): |
| 38 | Retracer.retrace_function(self, function) |
| 39 | |
José Fonseca | 6221297 | 2011-03-23 13:22:55 +0000 | [diff] [blame] | 40 | array_pointer_function_names = set(( |
| 41 | "glVertexPointer", |
| 42 | "glNormalPointer", |
| 43 | "glColorPointer", |
| 44 | "glIndexPointer", |
| 45 | "glTexCoordPointer", |
| 46 | "glEdgeFlagPointer", |
| 47 | "glFogCoordPointer", |
| 48 | "glSecondaryColorPointer", |
| 49 | |
| 50 | "glInterleavedArrays", |
| 51 | |
José Fonseca | ac5285b | 2011-05-04 11:09:08 +0100 | [diff] [blame^] | 52 | "glVertexPointerEXT", |
| 53 | "glNormalPointerEXT", |
| 54 | "glColorPointerEXT", |
| 55 | "glIndexPointerEXT", |
| 56 | "glTexCoordPointerEXT", |
| 57 | "glEdgeFlagPointerEXT", |
| 58 | "glFogCoordPointerEXT", |
| 59 | "glSecondaryColorPointerEXT", |
José Fonseca | 6221297 | 2011-03-23 13:22:55 +0000 | [diff] [blame] | 60 | |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 61 | "glVertexAttribPointer", |
| 62 | "glVertexAttribPointerARB", |
| 63 | "glVertexAttribPointerNV", |
José Fonseca | ac5285b | 2011-05-04 11:09:08 +0100 | [diff] [blame^] | 64 | "glVertexAttribIPointer", |
| 65 | "glVertexAttribIPointerEXT", |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 66 | "glVertexAttribLPointer", |
| 67 | "glVertexAttribLPointerEXT", |
José Fonseca | 6221297 | 2011-03-23 13:22:55 +0000 | [diff] [blame] | 68 | |
| 69 | #"glMatrixIndexPointerARB", |
| 70 | )) |
| 71 | |
José Fonseca | 8caf2c8 | 2010-11-30 12:09:12 +0000 | [diff] [blame] | 72 | draw_array_function_names = set([ |
| 73 | "glDrawArrays", |
| 74 | "glDrawArraysEXT", |
| 75 | "glDrawArraysIndirect", |
| 76 | "glDrawArraysInstanced", |
| 77 | "glDrawArraysInstancedARB", |
| 78 | "glDrawArraysInstancedEXT", |
| 79 | "glDrawMeshArraysSUN", |
| 80 | "glMultiDrawArrays", |
| 81 | "glMultiDrawArraysEXT", |
| 82 | "glMultiModeDrawArraysIBM", |
| 83 | ]) |
| 84 | |
| 85 | draw_elements_function_names = set([ |
| 86 | "glDrawElements", |
| 87 | "glDrawElementsBaseVertex", |
| 88 | "glDrawElementsIndirect", |
| 89 | "glDrawElementsInstanced", |
| 90 | "glDrawElementsInstancedARB", |
| 91 | "glDrawElementsInstancedBaseVertex", |
| 92 | "glDrawElementsInstancedEXT", |
| 93 | "glDrawRangeElements", |
| 94 | "glDrawRangeElementsBaseVertex", |
| 95 | "glDrawRangeElementsEXT", |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 96 | #"glMultiDrawElements", |
| 97 | #"glMultiDrawElementsBaseVertex", |
| 98 | #"glMultiDrawElementsEXT", |
| 99 | #"glMultiModeDrawElementsIBM", |
José Fonseca | 8caf2c8 | 2010-11-30 12:09:12 +0000 | [diff] [blame] | 100 | ]) |
| 101 | |
José Fonseca | 6221297 | 2011-03-23 13:22:55 +0000 | [diff] [blame] | 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 |
| 106 | |
| 107 | if is_array_pointer or is_draw_array or is_draw_elements: |
José Fonseca | 6117e31 | 2011-04-15 23:52:58 +0100 | [diff] [blame] | 108 | print ' if (glretrace::parser.version < 1) {' |
José Fonseca | 6221297 | 2011-03-23 13:22:55 +0000 | [diff] [blame] | 109 | |
| 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) |
| 115 | print ' }' |
| 116 | |
| 117 | if is_draw_elements: |
| 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) |
| 122 | print ' }' |
| 123 | |
| 124 | print ' }' |
| 125 | |
| 126 | Retracer.retrace_function_body(self, function) |
| 127 | |
José Fonseca | fa15d33 | 2010-11-25 20:22:39 +0000 | [diff] [blame] | 128 | def call_function(self, function): |
José Fonseca | cdb574a | 2010-11-29 12:23:35 +0000 | [diff] [blame] | 129 | if function.name == "glViewport": |
José Fonseca | d0c9175 | 2011-04-12 14:14:27 +0100 | [diff] [blame] | 130 | print ' bool reshape_window = false;' |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 131 | print ' if (x + width > glretrace::window_width) {' |
| 132 | print ' glretrace::window_width = x + width;' |
José Fonseca | d0c9175 | 2011-04-12 14:14:27 +0100 | [diff] [blame] | 133 | print ' reshape_window = true;' |
José Fonseca | cdb574a | 2010-11-29 12:23:35 +0000 | [diff] [blame] | 134 | print ' }' |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 135 | print ' if (y + height > glretrace::window_height) {' |
| 136 | print ' glretrace::window_height = y + height;' |
José Fonseca | d0c9175 | 2011-04-12 14:14:27 +0100 | [diff] [blame] | 137 | print ' reshape_window = true;' |
| 138 | print ' }' |
| 139 | print ' if (reshape_window) {' |
| 140 | print ' // XXX: does not always work' |
| 141 | print ' glretrace::drawable->resize(glretrace::window_width, glretrace::window_height);' |
| 142 | print ' reshape_window = false;' |
José Fonseca | cdb574a | 2010-11-29 12:23:35 +0000 | [diff] [blame] | 143 | print ' }' |
| 144 | |
José Fonseca | 3d245f4 | 2010-11-28 00:08:23 +0000 | [diff] [blame] | 145 | if function.name == "glEnd": |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 146 | print ' glretrace::insideGlBeginEnd = false;' |
José Fonseca | cdb574a | 2010-11-29 12:23:35 +0000 | [diff] [blame] | 147 | |
José Fonseca | fa15d33 | 2010-11-25 20:22:39 +0000 | [diff] [blame] | 148 | Retracer.call_function(self, function) |
José Fonseca | cdb574a | 2010-11-29 12:23:35 +0000 | [diff] [blame] | 149 | |
José Fonseca | 3d245f4 | 2010-11-28 00:08:23 +0000 | [diff] [blame] | 150 | if function.name == "glBegin": |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 151 | print ' glretrace::insideGlBeginEnd = true;' |
José Fonseca | 3d245f4 | 2010-11-28 00:08:23 +0000 | [diff] [blame] | 152 | else: |
| 153 | # glGetError is not allowed inside glBegin/glEnd |
Zack Rusin | 20bc32d | 2011-04-19 16:44:46 -0400 | [diff] [blame] | 154 | print ' glretrace::checkGlError(call.no);' |
José Fonseca | 3d245f4 | 2010-11-28 00:08:23 +0000 | [diff] [blame] | 155 | |
José Fonseca | dacd8dd | 2010-11-25 17:50:26 +0000 | [diff] [blame] | 156 | def extract_arg(self, function, arg, arg_type, lvalue, rvalue): |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 157 | if function.name in self.array_pointer_function_names and arg.name == 'pointer': |
José Fonseca | ac5285b | 2011-05-04 11:09:08 +0100 | [diff] [blame^] | 158 | print ' %s = static_cast<%s>(%s.blob());' % (lvalue, arg_type, rvalue) |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 159 | return |
| 160 | |
| 161 | if function.name in self.draw_elements_function_names and arg.name == 'indices': |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 162 | print ' %s = %s.blob();' % (lvalue, rvalue) |
José Fonseca | 8a844ae | 2010-12-06 18:50:52 +0000 | [diff] [blame] | 163 | return |
José Fonseca | dacd8dd | 2010-11-25 17:50:26 +0000 | [diff] [blame] | 164 | |
José Fonseca | 8a844ae | 2010-12-06 18:50:52 +0000 | [diff] [blame] | 165 | if function.name.startswith('glUniform') and function.args[0].name == arg.name == 'location': |
| 166 | print ' GLint program = -1;' |
| 167 | print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);' |
| 168 | |
| 169 | Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue) |
José Fonseca | e6a50bd | 2010-11-24 10:12:22 +0000 | [diff] [blame] | 170 | |
| 171 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 172 | if __name__ == '__main__': |
José Fonseca | 796a304 | 2010-11-29 14:21:06 +0000 | [diff] [blame] | 173 | print r''' |
José Fonseca | 867b1b7 | 2011-04-24 11:58:04 +0100 | [diff] [blame] | 174 | #include <string.h> |
| 175 | |
José Fonseca | df66a90 | 2010-11-29 13:24:20 +0000 | [diff] [blame] | 176 | #include "glproc.hpp" |
José Fonseca | 32871ed | 2011-04-10 13:40:52 +0100 | [diff] [blame] | 177 | #include "glretrace.hpp" |
José Fonseca | 589082d | 2011-03-30 09:10:40 +0100 | [diff] [blame] | 178 | |
José Fonseca | df66a90 | 2010-11-29 13:24:20 +0000 | [diff] [blame] | 179 | |
José Fonseca | 3d245f4 | 2010-11-28 00:08:23 +0000 | [diff] [blame] | 180 | ''' |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 181 | api = glapi.glapi |
José Fonseca | 867b1b7 | 2011-04-24 11:58:04 +0100 | [diff] [blame] | 182 | api.add_function(glapi.memcpy) |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 183 | retracer = GlRetracer() |
| 184 | retracer.retrace_api(glapi.glapi) |