1 | # Copyright (c) 2001, Stanford University
|
---|
2 | # All rights reserved.
|
---|
3 | #
|
---|
4 | # See the file LICENSE.txt for information on redistributing this software.
|
---|
5 |
|
---|
6 | # This script generates the packer.c file from the gl_header.parsed file.
|
---|
7 |
|
---|
8 | from __future__ import print_function
|
---|
9 | import sys, string, re
|
---|
10 |
|
---|
11 | import apiutil
|
---|
12 |
|
---|
13 |
|
---|
14 |
|
---|
15 | def WriteData( offset, arg_type, arg_name, is_swapped ):
|
---|
16 | """Return a string to write a variable to the packing buffer."""
|
---|
17 | retval = 9
|
---|
18 | if apiutil.IsPointer(arg_type):
|
---|
19 | retval = "\tWRITE_NETWORK_POINTER( %d, (void *) %s );" % (offset, arg_name )
|
---|
20 | else:
|
---|
21 | if is_swapped:
|
---|
22 | if arg_type == "GLfloat" or arg_type == "GLclampf":
|
---|
23 | retval = "\tWRITE_DATA( %d, GLuint, SWAPFLOAT(%s) );" % (offset, arg_name)
|
---|
24 | elif arg_type == "GLdouble" or arg_type == "GLclampd":
|
---|
25 | retval = "\tWRITE_SWAPPED_DOUBLE( %d, %s );" % (offset, arg_name)
|
---|
26 | elif apiutil.sizeof(arg_type) == 1:
|
---|
27 | retval = "\tWRITE_DATA( %d, %s, %s );" % (offset, arg_type, arg_name)
|
---|
28 | elif apiutil.sizeof(arg_type) == 2:
|
---|
29 | retval = "\tWRITE_DATA( %d, %s, SWAP16(%s) );" % (offset, arg_type, arg_name)
|
---|
30 | elif apiutil.sizeof(arg_type) == 4:
|
---|
31 | retval = "\tWRITE_DATA( %d, %s, SWAP32(%s) );" % (offset, arg_type, arg_name)
|
---|
32 | else:
|
---|
33 | if arg_type == "GLdouble" or arg_type == "GLclampd":
|
---|
34 | retval = "\tWRITE_DOUBLE( %d, %s );" % (offset, arg_name)
|
---|
35 | else:
|
---|
36 | retval = "\tWRITE_DATA( %d, %s, %s );" % (offset, arg_type, arg_name)
|
---|
37 | if retval == 9:
|
---|
38 | print >>sys.stderr, "no retval for %s %s" % (arg_name, arg_type)
|
---|
39 | assert 0
|
---|
40 | return retval
|
---|
41 |
|
---|
42 |
|
---|
43 | def UpdateCurrentPointer( func_name ):
|
---|
44 | m = re.search( r"^(Color|Normal)([1234])(ub|b|us|s|ui|i|f|d)$", func_name )
|
---|
45 | if m :
|
---|
46 | k = m.group(1)
|
---|
47 | name = '%s%s' % (k[:1].lower(),k[1:])
|
---|
48 | type = m.group(3) + m.group(2)
|
---|
49 | print("\tpc->current.c.%s.%s = data_ptr;" % (name,type))
|
---|
50 | return
|
---|
51 |
|
---|
52 | m = re.search( r"^(SecondaryColor)(3)(ub|b|us|s|ui|i|f|d)EXT$", func_name )
|
---|
53 | if m :
|
---|
54 | k = m.group(1)
|
---|
55 | name = 'secondaryColor'
|
---|
56 | type = m.group(3) + m.group(2)
|
---|
57 | print("\tpc->current.c.%s.%s = data_ptr;" % (name,type))
|
---|
58 | return
|
---|
59 |
|
---|
60 | m = re.search( r"^(TexCoord)([1234])(ub|b|us|s|ui|i|f|d)$", func_name )
|
---|
61 | if m :
|
---|
62 | k = m.group(1)
|
---|
63 | name = 'texCoord'
|
---|
64 | type = m.group(3) + m.group(2)
|
---|
65 | print("\tpc->current.c.%s.%s[0] = data_ptr;" % (name,type))
|
---|
66 | return
|
---|
67 |
|
---|
68 | m = re.search( r"^(MultiTexCoord)([1234])(ub|b|us|s|ui|i|f|d)ARB$", func_name )
|
---|
69 | if m :
|
---|
70 | k = m.group(1)
|
---|
71 | name = 'texCoord'
|
---|
72 | type = m.group(3) + m.group(2)
|
---|
73 | print("\tpc->current.c.%s.%s[texture-GL_TEXTURE0_ARB] = data_ptr + 4;" % (name,type))
|
---|
74 | return
|
---|
75 |
|
---|
76 | m = re.match( r"^(Index)(ub|b|us|s|ui|i|f|d)$", func_name )
|
---|
77 | if m :
|
---|
78 | k = m.group(1)
|
---|
79 | name = 'index'
|
---|
80 | type = m.group(2) + "1"
|
---|
81 | print("\tpc->current.c.%s.%s = data_ptr;" % (name,type))
|
---|
82 | return
|
---|
83 |
|
---|
84 | m = re.match( r"^(EdgeFlag)$", func_name )
|
---|
85 | if m :
|
---|
86 | k = m.group(1)
|
---|
87 | name = 'edgeFlag'
|
---|
88 | type = "l1"
|
---|
89 | print("\tpc->current.c.%s.%s = data_ptr;" % (name,type))
|
---|
90 | return
|
---|
91 |
|
---|
92 | m = re.match( r"^(FogCoord)(f|d)EXT$", func_name )
|
---|
93 | if m :
|
---|
94 | k = m.group(1)
|
---|
95 | name = 'fogCoord'
|
---|
96 | type = m.group(2) + "1"
|
---|
97 | print("\tpc->current.c.%s.%s = data_ptr;" % (name,type))
|
---|
98 | return
|
---|
99 |
|
---|
100 |
|
---|
101 | m = re.search( r"^(VertexAttrib)([1234])N?(ub|b|s|f|d)(NV|ARB)$", func_name )
|
---|
102 | if m :
|
---|
103 | k = m.group(1)
|
---|
104 | name = 'vertexAttrib'
|
---|
105 | type = m.group(3) + m.group(2)
|
---|
106 | # Add 12 to skip the packet length, opcode and index fields
|
---|
107 | print("\tpc->current.c.%s.%s[index] = data_ptr + 4;" % (name,type))
|
---|
108 | if m.group(4) == "ARB" or m.group(4) == "NV":
|
---|
109 | print("\tpc->current.attribsUsedMask |= (1 << index);")
|
---|
110 | print("\tpc->current.changedVertexAttrib |= (1 << index);")
|
---|
111 | return
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 | def PrintFunc( func_name, params, is_swapped, can_have_pointers ):
|
---|
116 | """Emit a packer function."""
|
---|
117 | if is_swapped:
|
---|
118 | print('void PACK_APIENTRY crPack%sSWAP( %s )' % (func_name, apiutil.MakeDeclarationStringWithContext('CR_PACKER_CONTEXT', params)))
|
---|
119 | else:
|
---|
120 | print('void PACK_APIENTRY crPack%s( %s )' % (func_name, apiutil.MakeDeclarationStringWithContext('CR_PACKER_CONTEXT', params)))
|
---|
121 | print('{')
|
---|
122 | print('\tCR_GET_PACKER_CONTEXT(pc);')
|
---|
123 |
|
---|
124 | # Save original function name
|
---|
125 | orig_func_name = func_name
|
---|
126 |
|
---|
127 | # Convert to a non-vector version of the function if possible
|
---|
128 | func_name = apiutil.NonVectorFunction( func_name )
|
---|
129 | if not func_name:
|
---|
130 | func_name = orig_func_name
|
---|
131 |
|
---|
132 | # Check if there are any pointer parameters.
|
---|
133 | # That's usually a problem so we'll emit an error function.
|
---|
134 | nonVecParams = apiutil.Parameters(func_name)
|
---|
135 | bail_out = 0
|
---|
136 | for (name, type, vecSize) in nonVecParams:
|
---|
137 | if apiutil.IsPointer(type) and vecSize == 0 and not can_have_pointers:
|
---|
138 | bail_out = 1
|
---|
139 | if bail_out:
|
---|
140 | for (name, type, vecSize) in nonVecParams:
|
---|
141 | print('\t(void)%s;' % (name))
|
---|
142 | print('\tcrError ( "%s needs to be special cased %d %d!");' % (func_name, vecSize, can_have_pointers))
|
---|
143 | print('\t(void) pc;')
|
---|
144 | print('}')
|
---|
145 | # XXX we should really abort here
|
---|
146 | return
|
---|
147 |
|
---|
148 | if "extpack" in apiutil.ChromiumProps(func_name):
|
---|
149 | is_extended = 1
|
---|
150 | else:
|
---|
151 | is_extended = 0
|
---|
152 |
|
---|
153 |
|
---|
154 | print("\tunsigned char *data_ptr;")
|
---|
155 | print('\t(void) pc;')
|
---|
156 | #if func_name == "Enable" or func_name == "Disable":
|
---|
157 | # print "\tCRASSERT(!pc->buffer.geometry_only); /* sanity check */"
|
---|
158 |
|
---|
159 | for index in range(0,len(params)):
|
---|
160 | (name, type, vecSize) = params[index]
|
---|
161 | if vecSize>0 and func_name!=orig_func_name:
|
---|
162 | print(" if (!%s) {" % name)
|
---|
163 | # Know the reason for this one, so avoid the spam.
|
---|
164 | if orig_func_name != "SecondaryColor3fvEXT":
|
---|
165 | print(" crDebug(\"App passed NULL as %s for %s\");" % (name, orig_func_name))
|
---|
166 | print(" return;")
|
---|
167 | print(" }")
|
---|
168 |
|
---|
169 | packet_length = apiutil.PacketLength(nonVecParams)
|
---|
170 |
|
---|
171 | if packet_length == 0 and not is_extended:
|
---|
172 | print("\tCR_GET_BUFFERED_POINTER_NO_ARGS( pc );")
|
---|
173 | elif func_name[:9] == "Translate" or func_name[:5] == "Color":
|
---|
174 | # XXX WTF is the purpose of this?
|
---|
175 | if is_extended:
|
---|
176 | packet_length += 8
|
---|
177 | print("\tCR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, %d, GL_TRUE );" % packet_length)
|
---|
178 | else:
|
---|
179 | if is_extended:
|
---|
180 | packet_length += 8
|
---|
181 | print("\tCR_GET_BUFFERED_POINTER( pc, %d );" % packet_length)
|
---|
182 | UpdateCurrentPointer( func_name )
|
---|
183 |
|
---|
184 | if is_extended:
|
---|
185 | counter = 8
|
---|
186 | print(WriteData( 0, 'GLint', packet_length, is_swapped ))
|
---|
187 | print(WriteData( 4, 'GLenum', apiutil.ExtendedOpcodeName( func_name ), is_swapped ))
|
---|
188 | else:
|
---|
189 | counter = 0
|
---|
190 |
|
---|
191 | # Now emit the WRITE_() macros for all parameters
|
---|
192 | for index in range(0,len(params)):
|
---|
193 | (name, type, vecSize) = params[index]
|
---|
194 | # if we're converting a vector-valued function to a non-vector func:
|
---|
195 | if vecSize > 0 and func_name != orig_func_name:
|
---|
196 | ptrType = apiutil.PointerType(type)
|
---|
197 | for i in range(0, vecSize):
|
---|
198 | print(WriteData( counter + i * apiutil.sizeof(ptrType),
|
---|
199 | ptrType, "%s[%d]" % (name, i), is_swapped ))
|
---|
200 | # XXX increment counter here?
|
---|
201 | else:
|
---|
202 | print(WriteData( counter, type, name, is_swapped ))
|
---|
203 | if apiutil.IsPointer(type):
|
---|
204 | counter += apiutil.PointerSize()
|
---|
205 | else:
|
---|
206 | counter += apiutil.sizeof(type)
|
---|
207 |
|
---|
208 | # finish up
|
---|
209 | if is_extended:
|
---|
210 | print("\tWRITE_OPCODE( pc, CR_EXTEND_OPCODE );")
|
---|
211 | else:
|
---|
212 | print("\tWRITE_OPCODE( pc, %s );" % apiutil.OpcodeName( func_name ))
|
---|
213 |
|
---|
214 | if "get" in apiutil.Properties(func_name):
|
---|
215 | print('\tCR_CMDBLOCK_CHECK_FLUSH(pc);')
|
---|
216 |
|
---|
217 | print('\tCR_UNLOCK_PACKER_CONTEXT(pc);')
|
---|
218 | print('}\n')
|
---|
219 |
|
---|
220 |
|
---|
221 | r0_funcs = [ 'ChromiumParameteriCR', 'WindowSize', 'WindowShow', 'WindowPosition' ]
|
---|
222 |
|
---|
223 |
|
---|
224 | apiutil.CopyrightC()
|
---|
225 |
|
---|
226 | print("""
|
---|
227 | /* DO NOT EDIT - THIS FILE GENERATED BY THE packer.py SCRIPT */
|
---|
228 |
|
---|
229 | /* For each of the OpenGL functions we have a packer function which
|
---|
230 | * packs the function's opcode and arguments into a buffer.
|
---|
231 | */
|
---|
232 |
|
---|
233 | #include "packer.h"
|
---|
234 | #include "cr_opcodes.h"
|
---|
235 |
|
---|
236 | """)
|
---|
237 |
|
---|
238 |
|
---|
239 | keys = apiutil.GetDispatchedFunctions(sys.argv[1]+"/APIspec.txt")
|
---|
240 |
|
---|
241 | for func_name in keys:
|
---|
242 | if apiutil.FindSpecial( "packer", func_name ):
|
---|
243 | continue
|
---|
244 |
|
---|
245 | if not apiutil.HasPackOpcode(func_name):
|
---|
246 | continue
|
---|
247 |
|
---|
248 | pointers_ok = 0
|
---|
249 |
|
---|
250 | return_type = apiutil.ReturnType(func_name)
|
---|
251 | params = apiutil.Parameters(func_name)
|
---|
252 |
|
---|
253 | if return_type != 'void':
|
---|
254 | # Yet another gross hack for glGetString
|
---|
255 | if return_type.find('*') == -1:
|
---|
256 | return_type = return_type + " *"
|
---|
257 | params.append(("return_value", return_type, 0))
|
---|
258 |
|
---|
259 | if "get" in apiutil.Properties(func_name):
|
---|
260 | pointers_ok = 1
|
---|
261 | params.append(("writeback", "int *", 0))
|
---|
262 |
|
---|
263 | if func_name == 'Writeback':
|
---|
264 | pointers_ok = 1
|
---|
265 |
|
---|
266 | if not func_name in r0_funcs:
|
---|
267 | print('#ifndef IN_RING0')
|
---|
268 |
|
---|
269 | PrintFunc( func_name, params, 0, pointers_ok )
|
---|
270 | PrintFunc( func_name, params, 1, pointers_ok )
|
---|
271 |
|
---|
272 | if not func_name in r0_funcs:
|
---|
273 | print('#endif')
|
---|
274 |
|
---|