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