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