1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
##########################################################################
#
# Copyright 2008-2015 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/
"""Common code generation."""
# Adjust path
import os.path
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from specs import stdapi
from specs import winapi
from specs import dxgi
from specs import d3d10
from specs import d3d11
from specs import d3d11
class ValueDumper(stdapi.Visitor, stdapi.ExpanderMixin):
'''Visitor which generates code to dump any type.
'''
# TODO: Don't dump all types inline -- do like trace.py's ComplexValueDumper
def visitLiteral(self, literal, instance):
if literal.kind in ('SInt', 'UInt'):
print ' writer.writeInt(%s);' % (instance)
elif literal.kind in ('Float',):
print ' writer.writeFloat(%s);' % (instance)
else:
raise NotImplementedError
def visitString(self, string, instance):
if not string.wide:
cast = 'const char *'
suffix = 'String'
else:
assert False
cast = 'const wchar_t *'
suffix = 'WString'
if cast != string.expr:
# reinterpret_cast is necessary for GLubyte * <=> char *
instance = 'reinterpret_cast<%s>(%s)' % (cast, instance)
assert string.length is None
print ' writer.write%s(%s);' % (suffix, instance)
def visitConst(self, const, instance):
self.visit(const.type, instance)
def visitStruct(self, struct, instance):
print ' writer.beginObject();'
for member in struct.members:
memberType, memberName = member
if memberName is not None:
print ' writer.beginMember("%s");' % memberName
self.visitMember(member, instance)
if memberName is not None:
print ' writer.endMember(); // %s' % memberName
print ' writer.endObject();'
def visitArray(self, array, instance):
length = '_c' + array.type.tag
index = '_i' + array.type.tag
array_length = self.expand(array.length)
print ' if (%s) {' % instance
print ' size_t %s = %s > 0 ? %s : 0;' % (length, array_length, array_length)
print ' writer.beginArray();'
print ' for (size_t %s = 0; %s < %s; ++%s) {' % (index, index, length, index)
self.visitElement(index, array.type, '(%s)[%s]' % (instance, index))
print ' }'
print ' writer.endArray();'
print ' } else {'
print ' writer.writeNull();'
print ' }'
def visitAttribArray(self, array, instance):
raise NotImplementedError
def visitBlob(self, blob, instance):
raise NotImplementedError
def visitEnum(self, enum, instance):
print ' switch (%s) {' % instance
for value in enum.values:
print ' case %s:' % value
print ' writer.writeString("%s");' % value
print ' break;'
print ' default:'
print ' writer.writeInt(%s);' % instance
print ' break;'
print ' }'
def visitBitmask(self, bitmask, instance):
# TODO
self.visit(bitmask.type, instance)
def visitPointer(self, pointer, instance):
print ' if (%s) {' % instance
print ' writer.beginArray();'
self.visit(pointer.type, "*" + instance)
print ' writer.endArray();'
print ' } else {'
print ' writer.writeNull();'
print ' }'
def visitIntPointer(self, pointer, instance):
print ' writer.writeInt((uintptr_t)%s);' % instance
def visitObjPointer(self, pointer, instance):
print ' writer.writeInt((uintptr_t)%s);' % instance
def visitLinearPointer(self, pointer, instance):
print ' writer.writeInt((uintptr_t)%s);' % instance
def visitReference(self, reference, instance):
self.visit(reference.type, instance)
def visitHandle(self, handle, instance):
self.visit(handle.type, instance)
def visitAlias(self, alias, instance):
self.visit(alias.type, instance)
def visitOpaque(self, opaque, instance):
print ' writer.writeInt((uintptr_t)%s);' % instance
def visitInterface(self, interface, instance):
assert False
def visitPolymorphic(self, polymorphic, instance):
switchExpr = self.expand(polymorphic.switchExpr)
print ' switch (static_cast<int>(%s)) {' % switchExpr
for cases, type in polymorphic.iterSwitch():
for case in cases:
print ' %s:' % case
caseInstance = instance
if type.expr is not None:
caseInstance = 'static_cast<%s>(%s)' % (type, caseInstance)
self.visit(type, caseInstance)
print ' break;'
print ' }'
class Dumper:
'''Base class to orchestrate the code generation of state object dumpers.'''
def __init__(self, api, types):
self.api = api
self.types = types
def dumperFactory(self):
'''Create a dumper.
Can be overriden by derived classes to inject their own dumper visitor.
'''
return ValueDumper()
def dump(self):
self.header()
# Includes
for module in api.modules:
for header in module.headers:
print header
print
# Generate the dumper functions
types = api.getAllTypes()
for type in self.types:
self.dumpType(type)
def dumpType(self, type):
print r'void'
print r'dumpStateObject(StateWriter &writer, const %s & so)' % type
print r'{'
visitor = self.dumperFactory()
visitor.visit(type, 'so')
print r'}'
print
def header(self):
pass
def footer(self):
pass
if __name__ == '__main__':
print r'#include "dxgistate_so.hpp"'
print
print r'#include "state_writer.hpp"'
print
api = stdapi.API()
api.addModule(dxgi.dxgi)
api.addModule(d3d10.d3d10)
api.addModule(d3d10.d3d10_1)
api.addModule(d3d11.d3d11)
types = [
winapi.RECT,
dxgi.DXGI_FORMAT,
d3d10.D3D10_BLEND_DESC,
d3d10.D3D10_BUFFER_DESC,
d3d10.D3D10_COUNTER_DESC,
d3d10.D3D10_DEPTH_STENCIL_DESC,
d3d10.D3D10_DEPTH_STENCILOP_DESC,
d3d10.D3D10_DEPTH_STENCIL_VIEW_DESC,
d3d10.D3D10_INPUT_ELEMENT_DESC,
# FIXME: aliases to D3D_PRIMITIVE_TOPOLOGY clashing with D3D11_PRIMITIVE_TOPOLOGY
#d3d10.D3D10_PRIMITIVE_TOPOLOGY,
d3d10.D3D10_QUERY_DESC,
d3d10.D3D10_RASTERIZER_DESC,
d3d10.D3D10_RENDER_TARGET_VIEW_DESC,
d3d10.D3D10_SAMPLER_DESC,
d3d10.D3D10_SHADER_RESOURCE_VIEW_DESC,
d3d10.D3D10_TEXTURE1D_DESC,
d3d10.D3D10_TEXTURE2D_DESC,
d3d10.D3D10_TEXTURE3D_DESC,
d3d10.D3D10_VIEWPORT,
d3d10.D3D10_BLEND_DESC1,
d3d10.D3D10_SHADER_RESOURCE_VIEW_DESC1,
d3d11.D3D11_BLEND_DESC,
d3d11.D3D11_BLEND_DESC1,
d3d11.D3D11_BUFFER_DESC,
d3d11.D3D11_CLASS_INSTANCE_DESC,
d3d11.D3D11_COUNTER_DESC,
d3d11.D3D11_DEPTH_STENCIL_DESC,
d3d11.D3D11_DEPTH_STENCILOP_DESC,
d3d11.D3D11_DEPTH_STENCIL_VIEW_DESC,
d3d11.D3D11_INPUT_ELEMENT_DESC,
d3d11.D3D11_PACKED_MIP_DESC,
d3d11.D3D11_PRIMITIVE_TOPOLOGY,
d3d11.D3D11_QUERY_DESC,
d3d11.D3D11_RASTERIZER_DESC,
d3d11.D3D11_RASTERIZER_DESC1,
d3d11.D3D11_RENDER_TARGET_BLEND_DESC,
d3d11.D3D11_RENDER_TARGET_VIEW_DESC,
d3d11.D3D11_SAMPLER_DESC,
d3d11.D3D11_SHADER_RESOURCE_VIEW_DESC,
d3d11.D3D11_TEXTURE1D_DESC,
d3d11.D3D11_TEXTURE2D_DESC,
d3d11.D3D11_TEXTURE3D_DESC,
d3d11.D3D11_UNORDERED_ACCESS_VIEW_DESC,
d3d11.D3D11_VIEWPORT,
]
dumper = Dumper(api, types)
dumper.dump()
|