blob: fd95ccc58b476d42cc9c419d9f139c1ec473c8b6 [file] [log] [blame]
Corentin Wallez59382b72020-04-17 20:43:07 +00001#!/usr/bin/env python3
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02002# Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Corentin Wallez0c38e922019-06-07 08:59:17 +000016import json, os, sys
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040017from collections import namedtuple
Corentin Wallez0c38e922019-06-07 08:59:17 +000018
Corentin Wallez0c38e922019-06-07 08:59:17 +000019from generator_lib import Generator, run_generator, FileRender
Corentin Wallez031fbbb2019-06-11 18:03:05 +000020
21############################################################
22# OBJECT MODEL
23############################################################
24
Kai Ninomiya01aeca22020-07-15 19:51:17 +000025
fujunwei76bda372021-11-23 08:47:35 +000026class Metadata:
27 def __init__(self, metadata):
28 self.api = metadata['api']
29 self.namespace = metadata['namespace']
30 self.c_prefix = metadata.get('c_prefix', self.namespace.upper())
fujunwei3a464762021-12-05 05:29:44 +000031 self.proc_table_prefix = metadata['proc_table_prefix']
fujunwei76bda372021-11-23 08:47:35 +000032 self.copyright_year = metadata.get('copyright_year', None)
33
34
Corentin Wallez031fbbb2019-06-11 18:03:05 +000035class Name:
36 def __init__(self, name, native=False):
37 self.native = native
Austin Eng76a8d0b2020-04-03 17:37:48 +000038 self.name = name
Corentin Wallez031fbbb2019-06-11 18:03:05 +000039 if native:
40 self.chunks = [name]
41 else:
42 self.chunks = name.split(' ')
43
Austin Eng76a8d0b2020-04-03 17:37:48 +000044 def get(self):
45 return self.name
46
Corentin Wallez031fbbb2019-06-11 18:03:05 +000047 def CamelChunk(self, chunk):
48 return chunk[0].upper() + chunk[1:]
49
50 def canonical_case(self):
51 return (' '.join(self.chunks)).lower()
52
53 def concatcase(self):
54 return ''.join(self.chunks)
55
56 def camelCase(self):
Kai Ninomiya01aeca22020-07-15 19:51:17 +000057 return self.chunks[0] + ''.join(
58 [self.CamelChunk(chunk) for chunk in self.chunks[1:]])
Corentin Wallez031fbbb2019-06-11 18:03:05 +000059
60 def CamelCase(self):
61 return ''.join([self.CamelChunk(chunk) for chunk in self.chunks])
62
63 def SNAKE_CASE(self):
64 return '_'.join([chunk.upper() for chunk in self.chunks])
65
66 def snake_case(self):
67 return '_'.join(self.chunks)
68
Kai Ninomiya7b6246a2020-01-28 23:54:38 +000069 def js_enum_case(self):
70 result = self.chunks[0].lower()
71 for chunk in self.chunks[1:]:
72 if not result[-1].isdigit():
73 result += '-'
74 result += chunk.lower()
75 return result
76
Kai Ninomiya01aeca22020-07-15 19:51:17 +000077
Corentin Wallez031fbbb2019-06-11 18:03:05 +000078def concat_names(*names):
79 return ' '.join([name.canonical_case() for name in names])
80
Kai Ninomiya01aeca22020-07-15 19:51:17 +000081
Corentin Wallez031fbbb2019-06-11 18:03:05 +000082class Type:
83 def __init__(self, name, json_data, native=False):
84 self.json_data = json_data
85 self.dict_name = name
86 self.name = Name(name, native=native)
87 self.category = json_data['category']
Jiawei Shao6d6b63c2021-12-02 07:29:41 +000088 self.is_wire_transparent = False
Corentin Wallez031fbbb2019-06-11 18:03:05 +000089
Kai Ninomiya01aeca22020-07-15 19:51:17 +000090
Kai Ninomiya930e9182021-09-17 19:44:43 +000091EnumValue = namedtuple('EnumValue', ['name', 'value', 'valid', 'json_data'])
Kai Ninomiya01aeca22020-07-15 19:51:17 +000092
93
Corentin Wallez031fbbb2019-06-11 18:03:05 +000094class EnumType(Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +000095 def __init__(self, is_enabled, name, json_data):
Corentin Wallez031fbbb2019-06-11 18:03:05 +000096 Type.__init__(self, name, json_data)
Kai Ninomiya7b6246a2020-01-28 23:54:38 +000097
98 self.values = []
99 self.contiguousFromZero = True
100 lastValue = -1
101 for m in self.json_data['values']:
Kai Ninomiya930e9182021-09-17 19:44:43 +0000102 if not is_enabled(m):
103 continue
Kai Ninomiya7b6246a2020-01-28 23:54:38 +0000104 value = m['value']
105 if value != lastValue + 1:
106 self.contiguousFromZero = False
107 lastValue = value
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000108 self.values.append(
Kai Ninomiya930e9182021-09-17 19:44:43 +0000109 EnumValue(Name(m['name']), value, m.get('valid', True), m))
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000110
Corentin Wallez7ac08152019-06-26 19:54:43 +0000111 # Assert that all values are unique in enums
112 all_values = set()
113 for value in self.values:
114 if value.value in all_values:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000115 raise Exception("Duplicate value {} in enum {}".format(
116 value.value, name))
Corentin Wallez7ac08152019-06-26 19:54:43 +0000117 all_values.add(value.value)
Jiawei Shao6d6b63c2021-12-02 07:29:41 +0000118 self.is_wire_transparent = True
Corentin Wallez7ac08152019-06-26 19:54:43 +0000119
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000120
Kai Ninomiya930e9182021-09-17 19:44:43 +0000121BitmaskValue = namedtuple('BitmaskValue', ['name', 'value', 'json_data'])
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000122
123
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000124class BitmaskType(Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000125 def __init__(self, is_enabled, name, json_data):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000126 Type.__init__(self, name, json_data)
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000127 self.values = [
Kai Ninomiya930e9182021-09-17 19:44:43 +0000128 BitmaskValue(Name(m['name']), m['value'], m)
129 for m in self.json_data['values'] if is_enabled(m)
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000130 ]
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000131 self.full_mask = 0
132 for value in self.values:
133 self.full_mask = self.full_mask | value.value
Jiawei Shao6d6b63c2021-12-02 07:29:41 +0000134 self.is_wire_transparent = True
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000135
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000136
fujunwei23f71622021-12-02 07:41:21 +0000137class FunctionPointerType(Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000138 def __init__(self, is_enabled, name, json_data):
Corentin Wallez540abab2019-11-22 13:18:22 +0000139 Type.__init__(self, name, json_data)
fujunwei23f71622021-12-02 07:41:21 +0000140 self.return_type = None
Corentin Wallez540abab2019-11-22 13:18:22 +0000141 self.arguments = []
142
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000143
Brandon Jones58a471a2021-02-08 19:48:06 +0000144class TypedefType(Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000145 def __init__(self, is_enabled, name, json_data):
Brandon Jones58a471a2021-02-08 19:48:06 +0000146 Type.__init__(self, name, json_data)
147 self.type = None
148
149
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000150class NativeType(Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000151 def __init__(self, is_enabled, name, json_data):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000152 Type.__init__(self, name, json_data, native=True)
Jiawei Shao6d6b63c2021-12-02 07:29:41 +0000153 self.is_wire_transparent = True
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000154
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000155
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000156# Methods and structures are both "records", so record members correspond to
157# method arguments or structure members.
158class RecordMember:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000159 def __init__(self,
160 name,
161 typ,
162 annotation,
Kai Ninomiya930e9182021-09-17 19:44:43 +0000163 json_data,
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000164 optional=False,
165 is_return_value=False,
166 default_value=None,
Austin Eng6a5418a2019-07-19 16:01:48 +0000167 skip_serialize=False):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000168 self.name = name
169 self.type = typ
170 self.annotation = annotation
Kai Ninomiya930e9182021-09-17 19:44:43 +0000171 self.json_data = json_data
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000172 self.length = None
173 self.optional = optional
174 self.is_return_value = is_return_value
175 self.handle_type = None
Corentin Wallez8f938712019-07-08 19:20:22 +0000176 self.default_value = default_value
Austin Eng6a5418a2019-07-19 16:01:48 +0000177 self.skip_serialize = skip_serialize
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000178
179 def set_handle_type(self, handle_type):
180 assert self.type.dict_name == "ObjectHandle"
181 self.handle_type = handle_type
182
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000183
Kai Ninomiya930e9182021-09-17 19:44:43 +0000184Method = namedtuple('Method',
185 ['name', 'return_type', 'arguments', 'json_data'])
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000186
187
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000188class ObjectType(Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000189 def __init__(self, is_enabled, name, json_data):
190 json_data_override = {'methods': []}
191 if 'methods' in json_data:
192 json_data_override['methods'] = [
193 m for m in json_data['methods'] if is_enabled(m)
194 ]
195 Type.__init__(self, name, dict(json_data, **json_data_override))
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000196
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000197
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000198class Record:
199 def __init__(self, name):
200 self.name = Name(name)
201 self.members = []
Austin Eng76a8d0b2020-04-03 17:37:48 +0000202 self.may_have_dawn_object = False
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000203
204 def update_metadata(self):
Austin Eng76a8d0b2020-04-03 17:37:48 +0000205 def may_have_dawn_object(member):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000206 if isinstance(member.type, ObjectType):
207 return True
208 elif isinstance(member.type, StructureType):
Austin Eng76a8d0b2020-04-03 17:37:48 +0000209 return member.type.may_have_dawn_object
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000210 else:
211 return False
212
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000213 self.may_have_dawn_object = any(
214 may_have_dawn_object(member) for member in self.members)
Austin Eng76a8d0b2020-04-03 17:37:48 +0000215
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000216 # Set may_have_dawn_object to true if the type is chained or
217 # extensible. Chained structs may contain a Dawn object.
Austin Eng76a8d0b2020-04-03 17:37:48 +0000218 if isinstance(self, StructureType):
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000219 self.may_have_dawn_object = (self.may_have_dawn_object
220 or self.chained or self.extensible)
221
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000222
223class StructureType(Record, Type):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000224 def __init__(self, is_enabled, name, json_data):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000225 Record.__init__(self, name)
Kai Ninomiya930e9182021-09-17 19:44:43 +0000226 json_data_override = {}
227 if 'members' in json_data:
228 json_data_override['members'] = [
229 m for m in json_data['members'] if is_enabled(m)
230 ]
231 Type.__init__(self, name, dict(json_data, **json_data_override))
Austin Eng0c824052021-09-20 16:07:25 +0000232 self.chained = json_data.get("chained", None)
233 self.extensible = json_data.get("extensible", None)
234 if self.chained:
235 assert (self.chained == "in" or self.chained == "out")
236 if self.extensible:
237 assert (self.extensible == "in" or self.extensible == "out")
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000238 # Chained structs inherit from wgpu::ChainedStruct, which has
239 # nextInChain, so setting both extensible and chained would result in
240 # two nextInChain members.
241 assert not (self.extensible and self.chained)
242
Austin Eng0c824052021-09-20 16:07:25 +0000243 @property
244 def output(self):
245 return self.chained == "out" or self.extensible == "out"
246
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000247
fujunwei4e876902021-11-25 08:44:01 +0000248class ConstantDefinition():
249 def __init__(self, is_enabled, name, json_data):
250 self.type = None
251 self.value = json_data['value']
252 self.json_data = json_data
253 self.name = Name(name)
254
255
fujunwei23f71622021-12-02 07:41:21 +0000256class FunctionDeclaration():
257 def __init__(self, is_enabled, name, json_data):
258 self.return_type = None
259 self.arguments = []
260 self.json_data = json_data
261 self.name = Name(name)
262
263
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000264class Command(Record):
265 def __init__(self, name, members=None):
266 Record.__init__(self, name)
267 self.members = members or []
268 self.derived_object = None
269 self.derived_method = None
270
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000271
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000272def linked_record_members(json_data, types):
273 members = []
274 members_by_name = {}
275 for m in json_data:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000276 member = RecordMember(Name(m['name']),
277 types[m['type']],
Corentin Wallez8f938712019-07-08 19:20:22 +0000278 m.get('annotation', 'value'),
Kai Ninomiya930e9182021-09-17 19:44:43 +0000279 m,
Corentin Wallez8f938712019-07-08 19:20:22 +0000280 optional=m.get('optional', False),
281 is_return_value=m.get('is_return_value', False),
Austin Eng6a5418a2019-07-19 16:01:48 +0000282 default_value=m.get('default', None),
283 skip_serialize=m.get('skip_serialize', False))
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000284 handle_type = m.get('handle_type')
285 if handle_type:
286 member.set_handle_type(types[handle_type])
287 members.append(member)
288 members_by_name[member.name.canonical_case()] = member
289
290 for (member, m) in zip(members, json_data):
291 if member.annotation != 'value':
292 if not 'length' in m:
293 if member.type.category != 'object':
294 member.length = "constant"
295 member.constant_length = 1
296 else:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000297 assert False
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000298 elif m['length'] == 'strlen':
299 member.length = 'strlen'
300 else:
301 member.length = members_by_name[m['length']]
302
303 return members
Corentin Walleza6416542018-05-17 16:55:53 -0400304
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000305
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400306############################################################
307# PARSE
308############################################################
Corentin Wallez4b410a32017-04-20 14:42:36 -0400309
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000310
Corentin Walleze2f94842018-12-05 17:49:04 +0000311def link_object(obj, types):
312 def make_method(json_data):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000313 arguments = linked_record_members(json_data.get('args', []), types)
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000314 return Method(Name(json_data['name']),
Kai Ninomiya930e9182021-09-17 19:44:43 +0000315 types[json_data.get('returns',
316 'void')], arguments, json_data)
Corentin Walleze2f94842018-12-05 17:49:04 +0000317
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000318 obj.methods = [make_method(m) for m in obj.json_data.get('methods', [])]
Corentin Wallez97231682019-10-08 07:38:01 +0000319 obj.methods.sort(key=lambda method: method.name.canonical_case())
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400320
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000321
Corentin Walleza6416542018-05-17 16:55:53 -0400322def link_structure(struct, types):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000323 struct.members = linked_record_members(struct.json_data['members'], types)
Corentin Walleza6416542018-05-17 16:55:53 -0400324
Corentin Wallez540abab2019-11-22 13:18:22 +0000325
fujunwei23f71622021-12-02 07:41:21 +0000326def link_function_pointer(function_pointer, types):
327 link_function(function_pointer, types)
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000328
329
Brandon Jones58a471a2021-02-08 19:48:06 +0000330def link_typedef(typedef, types):
331 typedef.type = types[typedef.json_data['type']]
332
333
fujunwei4e876902021-11-25 08:44:01 +0000334def link_constant(constant, types):
335 constant.type = types[constant.json_data['type']]
336 assert constant.type.name.native
337
338
fujunwei23f71622021-12-02 07:41:21 +0000339def link_function(function, types):
340 function.return_type = types[function.json_data.get('returns', 'void')]
341 function.arguments = linked_record_members(function.json_data['args'],
342 types)
343
344
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000345# Sort structures so that if struct A has struct B as a member, then B is
346# listed before A.
347#
348# This is a form of topological sort where we try to keep the order reasonably
349# similar to the original order (though the sort isn't technically stable).
350#
351# It works by computing for each struct type what is the depth of its DAG of
Kai Ninomiya7d174a12021-09-21 17:36:27 +0000352# dependents, then re-sorting based on that depth using Python's stable sort.
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000353# This makes a toposort because if A depends on B then its depth will be bigger
354# than B's. It is also nice because all nodes with the same depth are kept in
355# the input order.
Corentin Wallez29353d62018-09-18 12:49:22 +0000356def topo_sort_structure(structs):
357 for struct in structs:
358 struct.visited = False
359 struct.subdag_depth = 0
360
361 def compute_depth(struct):
362 if struct.visited:
363 return struct.subdag_depth
364
365 max_dependent_depth = 0
366 for member in struct.members:
Corentin Walleze2f94842018-12-05 17:49:04 +0000367 if member.type.category == 'structure':
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000368 max_dependent_depth = max(max_dependent_depth,
369 compute_depth(member.type) + 1)
Corentin Wallez29353d62018-09-18 12:49:22 +0000370
371 struct.subdag_depth = max_dependent_depth
372 struct.visited = True
373 return struct.subdag_depth
374
375 for struct in structs:
376 compute_depth(struct)
377
378 result = sorted(structs, key=lambda struct: struct.subdag_depth)
379
380 for struct in structs:
381 del struct.visited
382 del struct.subdag_depth
383
384 return result
385
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000386
Kai Ninomiya930e9182021-09-17 19:44:43 +0000387def parse_json(json, enabled_tags):
388 is_enabled = lambda json_data: item_is_enabled(enabled_tags, json_data)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400389 category_to_parser = {
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000390 'bitmask': BitmaskType,
391 'enum': EnumType,
392 'native': NativeType,
fujunwei23f71622021-12-02 07:41:21 +0000393 'function pointer': FunctionPointerType,
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000394 'object': ObjectType,
395 'structure': StructureType,
Brandon Jones58a471a2021-02-08 19:48:06 +0000396 'typedef': TypedefType,
fujunwei4e876902021-11-25 08:44:01 +0000397 'constant': ConstantDefinition,
fujunwei23f71622021-12-02 07:41:21 +0000398 'function': FunctionDeclaration
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400399 }
400
401 types = {}
402
403 by_category = {}
404 for name in category_to_parser.keys():
405 by_category[name] = []
406
Corentin Walleze2f94842018-12-05 17:49:04 +0000407 for (name, json_data) in json.items():
Kai Ninomiya930e9182021-09-17 19:44:43 +0000408 if name[0] == '_' or not item_is_enabled(enabled_tags, json_data):
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400409 continue
Corentin Walleze2f94842018-12-05 17:49:04 +0000410 category = json_data['category']
Kai Ninomiya930e9182021-09-17 19:44:43 +0000411 parsed = category_to_parser[category](is_enabled, name, json_data)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400412 by_category[category].append(parsed)
413 types[name] = parsed
414
415 for obj in by_category['object']:
416 link_object(obj, types)
417
Corentin Walleza6416542018-05-17 16:55:53 -0400418 for struct in by_category['structure']:
419 link_structure(struct, types)
420
fujunwei23f71622021-12-02 07:41:21 +0000421 for function_pointer in by_category['function pointer']:
422 link_function_pointer(function_pointer, types)
Corentin Wallez540abab2019-11-22 13:18:22 +0000423
Brandon Jones58a471a2021-02-08 19:48:06 +0000424 for typedef in by_category['typedef']:
425 link_typedef(typedef, types)
426
fujunwei4e876902021-11-25 08:44:01 +0000427 for constant in by_category['constant']:
428 link_constant(constant, types)
429
fujunwei23f71622021-12-02 07:41:21 +0000430 for function in by_category['function']:
431 link_function(function, types)
432
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400433 for category in by_category.keys():
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000434 by_category[category] = sorted(
435 by_category[category], key=lambda typ: typ.name.canonical_case())
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400436
Corentin Wallez29353d62018-09-18 12:49:22 +0000437 by_category['structure'] = topo_sort_structure(by_category['structure'])
438
Austin Engc7f416c2019-01-15 20:49:53 +0000439 for struct in by_category['structure']:
440 struct.update_metadata()
441
Kai Ninomiya930e9182021-09-17 19:44:43 +0000442 api_params = {
443 'types': types,
444 'by_category': by_category,
445 'enabled_tags': enabled_tags,
446 }
447 return {
fujunwei76bda372021-11-23 08:47:35 +0000448 'metadata': Metadata(json['_metadata']),
Kai Ninomiya930e9182021-09-17 19:44:43 +0000449 'types': types,
450 'by_category': by_category,
451 'enabled_tags': enabled_tags,
452 'c_methods': lambda typ: c_methods(api_params, typ),
453 'c_methods_sorted_by_name': get_c_methods_sorted_by_name(api_params),
454 }
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000455
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400456
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000457############################################################
458# WIRE STUFF
459############################################################
460
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000461
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000462# Create wire commands from api methods
463def compute_wire_params(api_params, wire_json):
464 wire_params = api_params.copy()
465 types = wire_params['types']
466
467 commands = []
468 return_commands = []
469
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000470 wire_json['special items']['client_handwritten_commands'] += wire_json[
471 'special items']['client_side_commands']
Corentin Wallez540abab2019-11-22 13:18:22 +0000472
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000473 # Generate commands from object methods
474 for api_object in wire_params['by_category']['object']:
475 for method in api_object.methods:
476 command_name = concat_names(api_object.name, method.name)
477 command_suffix = Name(command_name).CamelCase()
478
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000479 # Only object return values or void are supported.
480 # Other methods must be handwritten.
481 is_object = method.return_type.category == 'object'
482 is_void = method.return_type.name.canonical_case() == 'void'
483 if not (is_object or is_void):
484 assert command_suffix in (
485 wire_json['special items']['client_handwritten_commands'])
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000486 continue
487
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000488 if command_suffix in (
489 wire_json['special items']['client_side_commands']):
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000490 continue
491
492 # Create object method commands by prepending "self"
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000493 members = [
494 RecordMember(Name('self'), types[api_object.dict_name],
Kai Ninomiya930e9182021-09-17 19:44:43 +0000495 'value', {})
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000496 ]
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000497 members += method.arguments
498
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000499 # Client->Server commands that return an object return the
500 # result object handle
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000501 if method.return_type.category == 'object':
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000502 result = RecordMember(Name('result'),
503 types['ObjectHandle'],
Kai Ninomiya930e9182021-09-17 19:44:43 +0000504 'value', {},
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000505 is_return_value=True)
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000506 result.set_handle_type(method.return_type)
507 members.append(result)
508
509 command = Command(command_name, members)
510 command.derived_object = api_object
511 command.derived_method = method
512 commands.append(command)
513
514 for (name, json_data) in wire_json['commands'].items():
515 commands.append(Command(name, linked_record_members(json_data, types)))
516
517 for (name, json_data) in wire_json['return commands'].items():
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000518 return_commands.append(
519 Command(name, linked_record_members(json_data, types)))
Corentin Wallez031fbbb2019-06-11 18:03:05 +0000520
521 wire_params['cmd_records'] = {
522 'command': commands,
523 'return command': return_commands
524 }
525
526 for commands in wire_params['cmd_records'].values():
527 for command in commands:
528 command.update_metadata()
529 commands.sort(key=lambda c: c.name.canonical_case())
530
531 wire_params.update(wire_json.get('special items', {}))
532
533 return wire_params
534
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000535
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400536#############################################################
Corentin Wallez0c38e922019-06-07 08:59:17 +0000537# Generator
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400538#############################################################
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400539
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000540
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400541def as_varName(*names):
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000542 return names[0].camelCase() + ''.join(
543 [name.CamelCase() for name in names[1:]])
544
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400545
fujunwei76bda372021-11-23 08:47:35 +0000546def as_cType(c_prefix, name):
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400547 if name.native:
548 return name.concatcase()
549 else:
fujunwei76bda372021-11-23 08:47:35 +0000550 return c_prefix + name.CamelCase()
Corentin Wallez919812e2019-10-17 08:46:07 +0000551
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000552
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400553def as_cppType(name):
554 if name.native:
555 return name.concatcase()
556 else:
557 return name.CamelCase()
558
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000559
Kai Ninomiya7b6246a2020-01-28 23:54:38 +0000560def as_jsEnumValue(value):
Kai Ninomiya930e9182021-09-17 19:44:43 +0000561 if 'jsrepr' in value.json_data: return value.json_data['jsrepr']
Kai Ninomiya7b6246a2020-01-28 23:54:38 +0000562 return "'" + value.name.js_enum_case() + "'"
563
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000564
Austin Eng740995c2019-05-15 18:55:22 +0000565def convert_cType_to_cppType(typ, annotation, arg, indent=0):
566 if typ.category == 'native':
567 return arg
568 if annotation == 'value':
569 if typ.category == 'object':
570 return '{}::Acquire({})'.format(as_cppType(typ.name), arg)
571 elif typ.category == 'structure':
572 converted_members = [
573 convert_cType_to_cppType(
574 member.type, member.annotation,
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000575 '{}.{}'.format(arg, as_varName(member.name)), indent + 1)
576 for member in typ.members
577 ]
Austin Eng740995c2019-05-15 18:55:22 +0000578
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000579 converted_members = [(' ' * 4) + m for m in converted_members]
Austin Eng740995c2019-05-15 18:55:22 +0000580 converted_members = ',\n'.join(converted_members)
581
582 return as_cppType(typ.name) + ' {\n' + converted_members + '\n}'
583 else:
584 return 'static_cast<{}>({})'.format(as_cppType(typ.name), arg)
585 else:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000586 return 'reinterpret_cast<{} {}>({})'.format(as_cppType(typ.name),
587 annotation, arg)
588
Austin Eng740995c2019-05-15 18:55:22 +0000589
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400590def decorate(name, typ, arg):
591 if arg.annotation == 'value':
592 return typ + ' ' + name
Austin Eng740995c2019-05-15 18:55:22 +0000593 elif arg.annotation == '*':
594 return typ + ' * ' + name
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400595 elif arg.annotation == 'const*':
596 return typ + ' const * ' + name
597 else:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000598 assert False
599
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400600
601def annotated(typ, arg):
602 name = as_varName(arg.name)
603 return decorate(name, typ, arg)
604
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000605
Kai Ninomiya930e9182021-09-17 19:44:43 +0000606def item_is_enabled(enabled_tags, json_data):
607 tags = json_data.get('tags')
608 if tags is None: return True
609 return any(tag in enabled_tags for tag in tags)
610
611
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400612def as_cppEnum(value_name):
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000613 assert not value_name.native
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400614 if value_name.concatcase()[0].isdigit():
615 return "e" + value_name.CamelCase()
616 return value_name.CamelCase()
617
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000618
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400619def as_MethodSuffix(type_name, method_name):
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000620 assert not type_name.native and not method_name.native
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400621 return type_name.CamelCase() + method_name.CamelCase()
622
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000623
fujunwei76bda372021-11-23 08:47:35 +0000624def as_frontendType(metadata, typ):
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400625 if typ.category == 'object':
Corentin Wallez4f5fc2d2019-04-01 21:48:38 +0000626 return typ.name.CamelCase() + 'Base*'
Corentin Wallezfe253f12018-08-01 15:12:10 +0200627 elif typ.category in ['bitmask', 'enum']:
fujunwei76bda372021-11-23 08:47:35 +0000628 return metadata.namespace + '::' + typ.name.CamelCase()
Corentin Wallezfe253f12018-08-01 15:12:10 +0200629 elif typ.category == 'structure':
630 return as_cppType(typ.name)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400631 else:
fujunwei76bda372021-11-23 08:47:35 +0000632 return as_cType(metadata.c_prefix, typ.name)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400633
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000634
fujunwei76bda372021-11-23 08:47:35 +0000635def as_wireType(metadata, typ):
Austin Engcb0cb652019-08-27 21:41:56 +0000636 if typ.category == 'object':
637 return typ.name.CamelCase() + '*'
Brandon Jones6f2bbe92021-04-05 23:34:17 +0000638 elif typ.category in ['bitmask', 'enum', 'structure']:
fujunwei76bda372021-11-23 08:47:35 +0000639 return metadata.c_prefix + typ.name.CamelCase()
Austin Engcb0cb652019-08-27 21:41:56 +0000640 else:
641 return as_cppType(typ.name)
642
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000643
Kai Ninomiya930e9182021-09-17 19:44:43 +0000644def c_methods(params, typ):
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000645 return typ.methods + [
Kai Ninomiya930e9182021-09-17 19:44:43 +0000646 x for x in [
647 Method(Name('reference'), params['types']['void'], [],
648 {'tags': ['dawn', 'emscripten']}),
649 Method(Name('release'), params['types']['void'], [],
650 {'tags': ['dawn', 'emscripten']}),
651 ] if item_is_enabled(params['enabled_tags'], x.json_data)
Corentin Wallez4b410a32017-04-20 14:42:36 -0400652 ]
653
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000654
Corentin Wallezaca8c4a2019-11-22 14:02:52 +0000655def get_c_methods_sorted_by_name(api_params):
Corentin Wallezc57b1802019-10-15 12:08:48 +0000656 unsorted = [(as_MethodSuffix(typ.name, method.name), typ, method) \
657 for typ in api_params['by_category']['object'] \
Kai Ninomiya930e9182021-09-17 19:44:43 +0000658 for method in c_methods(api_params, typ) ]
Corentin Wallezc57b1802019-10-15 12:08:48 +0000659 return [(typ, method) for (_, typ, method) in sorted(unsorted)]
660
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000661
Corentin Wallez540abab2019-11-22 13:18:22 +0000662def has_callback_arguments(method):
fujunwei23f71622021-12-02 07:41:21 +0000663 return any(arg.type.category == 'function pointer' for arg in method.arguments)
Corentin Wallez540abab2019-11-22 13:18:22 +0000664
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000665
fujunwei76bda372021-11-23 08:47:35 +0000666def make_base_render_params(metadata):
667 c_prefix = metadata.c_prefix
668
669 def as_cTypeEnumSpecialCase(typ):
670 if typ.category == 'bitmask':
671 return as_cType(c_prefix, typ.name) + 'Flags'
672 return as_cType(c_prefix, typ.name)
673
674 def as_cEnum(type_name, value_name):
675 assert not type_name.native and not value_name.native
676 return c_prefix + type_name.CamelCase() + '_' + value_name.CamelCase()
677
678 def as_cMethod(type_name, method_name):
fujunwei23f71622021-12-02 07:41:21 +0000679 c_method = c_prefix.lower()
680 if type_name != None:
681 assert not type_name.native
682 c_method += type_name.CamelCase()
683 assert not method_name.native
684 c_method += method_name.CamelCase()
685 return c_method
fujunwei76bda372021-11-23 08:47:35 +0000686
687 def as_cProc(type_name, method_name):
fujunwei23f71622021-12-02 07:41:21 +0000688 c_proc = c_prefix + 'Proc'
689 if type_name != None:
690 assert not type_name.native
691 c_proc += type_name.CamelCase()
692 assert not method_name.native
693 c_proc += method_name.CamelCase()
694 return c_proc
fujunwei76bda372021-11-23 08:47:35 +0000695
696 return {
697 'Name': lambda name: Name(name),
698 'as_annotated_cType': \
699 lambda arg: annotated(as_cTypeEnumSpecialCase(arg.type), arg),
700 'as_annotated_cppType': \
701 lambda arg: annotated(as_cppType(arg.type.name), arg),
702 'as_cEnum': as_cEnum,
703 'as_cppEnum': as_cppEnum,
704 'as_cMethod': as_cMethod,
705 'as_MethodSuffix': as_MethodSuffix,
706 'as_cProc': as_cProc,
707 'as_cType': lambda name: as_cType(c_prefix, name),
708 'as_cppType': as_cppType,
709 'as_jsEnumValue': as_jsEnumValue,
710 'convert_cType_to_cppType': convert_cType_to_cppType,
711 'as_varName': as_varName,
712 'decorate': decorate
713 }
714
715
Corentin Wallez0c38e922019-06-07 08:59:17 +0000716class MultiGeneratorFromDawnJSON(Generator):
717 def get_description(self):
718 return 'Generates code for various target from Dawn.json.'
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400719
Corentin Wallez0c38e922019-06-07 08:59:17 +0000720 def add_commandline_arguments(self, parser):
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000721 allowed_targets = [
722 'dawn_headers', 'dawncpp_headers', 'dawncpp', 'dawn_proc',
723 'mock_webgpu', 'dawn_wire', "dawn_native_utils"
724 ]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400725
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000726 parser.add_argument('--dawn-json',
727 required=True,
728 type=str,
729 help='The DAWN JSON definition to use.')
730 parser.add_argument('--wire-json',
731 default=None,
732 type=str,
733 help='The DAWN WIRE JSON definition to use.')
734 parser.add_argument(
735 '--targets',
736 required=True,
737 type=str,
738 help=
739 'Comma-separated subset of targets to output. Available targets: '
740 + ', '.join(allowed_targets))
Corentin Wallez0c38e922019-06-07 08:59:17 +0000741 def get_file_renders(self, args):
742 with open(args.dawn_json) as f:
743 loaded_json = json.loads(f.read())
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400744
Corentin Wallez0c38e922019-06-07 08:59:17 +0000745 targets = args.targets.split(',')
Corentin Wallez4b410a32017-04-20 14:42:36 -0400746
Corentin Wallez0c38e922019-06-07 08:59:17 +0000747 wire_json = None
748 if args.wire_json:
749 with open(args.wire_json) as f:
750 wire_json = json.loads(f.read())
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400751
Kai Ninomiya930e9182021-09-17 19:44:43 +0000752 renders = []
753
Kai Ninomiya930e9182021-09-17 19:44:43 +0000754 params_dawn = parse_json(loaded_json,
755 enabled_tags=['dawn', 'native', 'deprecated'])
fujunwei76bda372021-11-23 08:47:35 +0000756 metadata = params_dawn['metadata']
757 RENDER_PARAMS_BASE = make_base_render_params(metadata)
Corentin Wallez79a62bf2017-05-24 16:04:55 +0200758
fujunweic7d4f2c2021-12-07 00:46:35 +0000759 api = metadata.api.lower()
fujunweia8405742021-12-10 01:35:19 +0000760 prefix = metadata.proc_table_prefix.lower()
Corentin Wallez0c38e922019-06-07 08:59:17 +0000761 if 'dawn_headers' in targets:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000762 renders.append(
fujunweic7d4f2c2021-12-07 00:46:35 +0000763 FileRender('api.h', 'src/include/dawn/' + api + '.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000764 [RENDER_PARAMS_BASE, params_dawn]))
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000765 renders.append(
766 FileRender('dawn_proc_table.h',
fujunwei3a464762021-12-05 05:29:44 +0000767 'src/include/dawn/' + prefix + '_proc_table.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000768 [RENDER_PARAMS_BASE, params_dawn]))
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400769
Austin Engcc071e42019-10-16 10:26:01 +0000770 if 'dawncpp_headers' in targets:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000771 renders.append(
fujunweic7d4f2c2021-12-07 00:46:35 +0000772 FileRender('api_cpp.h', 'src/include/dawn/' + api + '_cpp.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000773 [RENDER_PARAMS_BASE, params_dawn]))
Austin Engcc071e42019-10-16 10:26:01 +0000774
Austin Enga9e39e12021-06-01 18:49:12 +0000775 renders.append(
fujunweic7d4f2c2021-12-07 00:46:35 +0000776 FileRender('api_cpp_print.h',
777 'src/include/dawn/' + api + '_cpp_print.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000778 [RENDER_PARAMS_BASE, params_dawn]))
Austin Enga9e39e12021-06-01 18:49:12 +0000779
Corentin Wallez96496822019-10-15 11:44:38 +0000780 if 'dawn_proc' in targets:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000781 renders.append(
fujunweia8405742021-12-10 01:35:19 +0000782 FileRender('dawn_proc.c', 'src/dawn/' + prefix + '_proc.c',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000783 [RENDER_PARAMS_BASE, params_dawn]))
Austin Eng16e01af2020-10-06 16:13:42 +0000784 renders.append(
785 FileRender('dawn_thread_dispatch_proc.cpp',
fujunweia8405742021-12-10 01:35:19 +0000786 'src/dawn/' + prefix + '_thread_dispatch_proc.cpp',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000787 [RENDER_PARAMS_BASE, params_dawn]))
Corentin Wallez96496822019-10-15 11:44:38 +0000788
Austin Eng63f65462021-12-09 20:03:48 +0000789 if 'webgpu_dawn_native_proc' in targets:
790 renders.append(
791 FileRender('dawn_native/api_dawn_native_proc.cpp',
792 'src/dawn_native/webgpu_dawn_native_proc.cpp',
793 [RENDER_PARAMS_BASE, params_dawn]))
794
Corentin Wallez96496822019-10-15 11:44:38 +0000795 if 'dawncpp' in targets:
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000796 renders.append(
797 FileRender('webgpu_cpp.cpp', 'src/dawn/webgpu_cpp.cpp',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000798 [RENDER_PARAMS_BASE, params_dawn]))
799
800 if 'webgpu_headers' in targets:
801 params_upstream = parse_json(loaded_json,
802 enabled_tags=['upstream', 'native'])
803 renders.append(
fujunweic7d4f2c2021-12-07 00:46:35 +0000804 FileRender('api.h', 'webgpu-headers/' + api + '.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000805 [RENDER_PARAMS_BASE, params_upstream]))
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200806
Kai Ninomiya7b6246a2020-01-28 23:54:38 +0000807 if 'emscripten_bits' in targets:
Kai Ninomiya930e9182021-09-17 19:44:43 +0000808 params_emscripten = parse_json(
809 loaded_json, enabled_tags=['upstream', 'emscripten'])
810 renders.append(
fujunweic7d4f2c2021-12-07 00:46:35 +0000811 FileRender('api.h', 'emscripten-bits/' + api + '.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000812 [RENDER_PARAMS_BASE, params_emscripten]))
813 renders.append(
fujunweic7d4f2c2021-12-07 00:46:35 +0000814 FileRender('api_cpp.h', 'emscripten-bits/' + api + '_cpp.h',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000815 [RENDER_PARAMS_BASE, params_emscripten]))
816 renders.append(
817 FileRender('webgpu_cpp.cpp', 'emscripten-bits/webgpu_cpp.cpp',
818 [RENDER_PARAMS_BASE, params_emscripten]))
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000819 renders.append(
820 FileRender('webgpu_struct_info.json',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000821 'emscripten-bits/webgpu_struct_info.json',
822 [RENDER_PARAMS_BASE, params_emscripten]))
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000823 renders.append(
824 FileRender('library_webgpu_enum_tables.js',
Kai Ninomiya930e9182021-09-17 19:44:43 +0000825 'emscripten-bits/library_webgpu_enum_tables.js',
826 [RENDER_PARAMS_BASE, params_emscripten]))
Kai Ninomiya7b6246a2020-01-28 23:54:38 +0000827
Corentin Wallez45b51f52019-10-28 22:15:47 +0000828 if 'mock_webgpu' in targets:
Corentin Wallez540abab2019-11-22 13:18:22 +0000829 mock_params = [
Kai Ninomiya930e9182021-09-17 19:44:43 +0000830 RENDER_PARAMS_BASE, params_dawn, {
Corentin Wallez540abab2019-11-22 13:18:22 +0000831 'has_callback_arguments': has_callback_arguments
832 }
833 ]
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000834 renders.append(
835 FileRender('mock_webgpu.h', 'src/dawn/mock_webgpu.h',
836 mock_params))
837 renders.append(
838 FileRender('mock_webgpu.cpp', 'src/dawn/mock_webgpu.cpp',
839 mock_params))
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200840
Corentin Wallez0c38e922019-06-07 08:59:17 +0000841 if 'dawn_native_utils' in targets:
842 frontend_params = [
Kai Ninomiya930e9182021-09-17 19:44:43 +0000843 RENDER_PARAMS_BASE,
844 params_dawn,
Corentin Wallez0c38e922019-06-07 08:59:17 +0000845 {
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000846 # TODO: as_frontendType and co. take a Type, not a Name :(
fujunwei76bda372021-11-23 08:47:35 +0000847 'as_frontendType': lambda typ: as_frontendType(metadata, typ),
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000848 'as_annotated_frontendType': \
fujunwei76bda372021-11-23 08:47:35 +0000849 lambda arg: annotated(as_frontendType(metadata, arg.type), arg),
Corentin Wallez0c38e922019-06-07 08:59:17 +0000850 }
851 ]
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200852
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000853 renders.append(
854 FileRender('dawn_native/ValidationUtils.h',
855 'src/dawn_native/ValidationUtils_autogen.h',
856 frontend_params))
857 renders.append(
858 FileRender('dawn_native/ValidationUtils.cpp',
859 'src/dawn_native/ValidationUtils_autogen.cpp',
860 frontend_params))
861 renders.append(
Austin Eng3faa4782021-10-27 19:07:37 +0000862 FileRender('dawn_native/dawn_platform.h',
863 'src/dawn_native/dawn_platform_autogen.h',
864 frontend_params))
865 renders.append(
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000866 FileRender('dawn_native/wgpu_structs.h',
867 'src/dawn_native/wgpu_structs_autogen.h',
868 frontend_params))
869 renders.append(
870 FileRender('dawn_native/wgpu_structs.cpp',
871 'src/dawn_native/wgpu_structs_autogen.cpp',
872 frontend_params))
873 renders.append(
874 FileRender('dawn_native/ProcTable.cpp',
875 'src/dawn_native/ProcTable.cpp', frontend_params))
Brian Ho5346e772021-04-22 17:49:42 +0000876 renders.append(
877 FileRender('dawn_native/ChainUtils.h',
878 'src/dawn_native/ChainUtils_autogen.h',
879 frontend_params))
880 renders.append(
881 FileRender('dawn_native/ChainUtils.cpp',
882 'src/dawn_native/ChainUtils_autogen.cpp',
883 frontend_params))
Brandon Jonesba662952021-09-23 21:26:33 +0000884 renders.append(
885 FileRender('dawn_native/webgpu_absl_format.h',
886 'src/dawn_native/webgpu_absl_format_autogen.h',
887 frontend_params))
888 renders.append(
889 FileRender('dawn_native/webgpu_absl_format.cpp',
890 'src/dawn_native/webgpu_absl_format_autogen.cpp',
891 frontend_params))
Loko Kung8d195d52021-09-28 15:40:01 +0000892 renders.append(
893 FileRender('dawn_native/ObjectType.h',
894 'src/dawn_native/ObjectType_autogen.h',
895 frontend_params))
896 renders.append(
897 FileRender('dawn_native/ObjectType.cpp',
898 'src/dawn_native/ObjectType_autogen.cpp',
899 frontend_params))
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200900
Corentin Wallez0c38e922019-06-07 08:59:17 +0000901 if 'dawn_wire' in targets:
Kai Ninomiya930e9182021-09-17 19:44:43 +0000902 additional_params = compute_wire_params(params_dawn, wire_json)
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200903
Corentin Wallez0c38e922019-06-07 08:59:17 +0000904 wire_params = [
Kai Ninomiya930e9182021-09-17 19:44:43 +0000905 RENDER_PARAMS_BASE, params_dawn, {
fujunwei76bda372021-11-23 08:47:35 +0000906 'as_wireType': lambda type : as_wireType(metadata, type),
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000907 'as_annotated_wireType': \
fujunwei76bda372021-11-23 08:47:35 +0000908 lambda arg: annotated(as_wireType(metadata, arg.type), arg),
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000909 }, additional_params
Corentin Wallez0c38e922019-06-07 08:59:17 +0000910 ]
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000911 renders.append(
Austin Eng3120d5e2020-11-11 19:46:18 +0000912 FileRender('dawn_wire/ObjectType.h',
913 'src/dawn_wire/ObjectType_autogen.h', wire_params))
914 renders.append(
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000915 FileRender('dawn_wire/WireCmd.h',
916 'src/dawn_wire/WireCmd_autogen.h', wire_params))
917 renders.append(
918 FileRender('dawn_wire/WireCmd.cpp',
919 'src/dawn_wire/WireCmd_autogen.cpp', wire_params))
920 renders.append(
921 FileRender('dawn_wire/client/ApiObjects.h',
922 'src/dawn_wire/client/ApiObjects_autogen.h',
923 wire_params))
924 renders.append(
925 FileRender('dawn_wire/client/ApiProcs.cpp',
926 'src/dawn_wire/client/ApiProcs_autogen.cpp',
927 wire_params))
928 renders.append(
929 FileRender('dawn_wire/client/ClientBase.h',
930 'src/dawn_wire/client/ClientBase_autogen.h',
931 wire_params))
932 renders.append(
933 FileRender('dawn_wire/client/ClientHandlers.cpp',
934 'src/dawn_wire/client/ClientHandlers_autogen.cpp',
935 wire_params))
936 renders.append(
937 FileRender(
938 'dawn_wire/client/ClientPrototypes.inc',
939 'src/dawn_wire/client/ClientPrototypes_autogen.inc',
940 wire_params))
941 renders.append(
942 FileRender('dawn_wire/server/ServerBase.h',
943 'src/dawn_wire/server/ServerBase_autogen.h',
944 wire_params))
945 renders.append(
946 FileRender('dawn_wire/server/ServerDoers.cpp',
947 'src/dawn_wire/server/ServerDoers_autogen.cpp',
948 wire_params))
949 renders.append(
950 FileRender('dawn_wire/server/ServerHandlers.cpp',
951 'src/dawn_wire/server/ServerHandlers_autogen.cpp',
952 wire_params))
953 renders.append(
954 FileRender(
955 'dawn_wire/server/ServerPrototypes.inc',
956 'src/dawn_wire/server/ServerPrototypes_autogen.inc',
957 wire_params))
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200958
Corentin Wallez0c38e922019-06-07 08:59:17 +0000959 return renders
Corentin Wallez59e7fad2018-08-16 15:32:35 +0200960
Corentin Wallez0c38e922019-06-07 08:59:17 +0000961 def get_dependencies(self, args):
962 deps = [os.path.abspath(args.dawn_json)]
963 if args.wire_json != None:
964 deps += [os.path.abspath(args.wire_json)]
965 return deps
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400966
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000967
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400968if __name__ == '__main__':
Corentin Wallez0c38e922019-06-07 08:59:17 +0000969 sys.exit(run_generator(MultiGeneratorFromDawnJSON()))