From 58a471ae2546b333ebeaa86add3a272032e40809 Mon Sep 17 00:00:00 2001 From: Brandon Jones Date: Mon, 8 Feb 2021 19:48:06 +0000 Subject: [PATCH] Add ability to generate typedefs from dawn.json Can be used to help with deprecation during simple struct renames. Includes typedefs for VertexAttributeDescriptor -> VertexAttribute and VertexBufferLayoutDescriptor -> VertexBufferLayout as specified by the latest RenderPipelineDescriptor changes. Bug: dawn:642 Change-Id: Iab3d74d179884499540e813b0e66859713031ccb Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/40581 Commit-Queue: Brandon Jones Reviewed-by: Corentin Wallez --- dawn.json | 16 ++++++++++++---- generator/dawn_json_generator.py | 14 ++++++++++++++ generator/templates/dawn_native/wgpu_structs.h | 4 ++++ generator/templates/webgpu.h | 7 +++++++ generator/templates/webgpu_cpp.h | 7 +++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/dawn.json b/dawn.json index 9eacbf96e2..33c91570ec 100644 --- a/dawn.json +++ b/dawn.json @@ -954,7 +954,7 @@ "extensible": true, "members": [] }, - "vertex attribute descriptor": { + "vertex attribute": { "category": "structure", "extensible": false, "members": [ @@ -963,23 +963,31 @@ {"name": "shader location", "type": "uint32_t"} ] }, - "vertex buffer layout descriptor": { + "vertex attribute descriptor": { + "category": "typedef", + "type": "vertex attribute" + }, + "vertex buffer layout": { "category": "structure", "extensible": false, "members": [ {"name": "array stride", "type": "uint64_t"}, {"name": "step mode", "type": "input step mode", "default": "vertex"}, {"name": "attribute count", "type": "uint32_t"}, - {"name": "attributes", "type": "vertex attribute descriptor", "annotation": "const*", "length": "attribute count"} + {"name": "attributes", "type": "vertex attribute", "annotation": "const*", "length": "attribute count"} ] }, + "vertex buffer layout descriptor": { + "category": "typedef", + "type": "vertex buffer layout" + }, "vertex state descriptor": { "category": "structure", "extensible": true, "members": [ {"name": "index format", "type": "index format", "default": "undefined"}, {"name": "vertex buffer count", "type": "uint32_t", "default": 0}, - {"name": "vertex buffers", "type": "vertex buffer layout descriptor", "annotation": "const*", "length": "vertex buffer count"} + {"name": "vertex buffers", "type": "vertex buffer layout", "annotation": "const*", "length": "vertex buffer count"} ] }, "input step mode": { diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py index 01d7040b30..8af22721f6 100644 --- a/generator/dawn_json_generator.py +++ b/generator/dawn_json_generator.py @@ -128,6 +128,12 @@ class CallbackType(Type): self.arguments = [] +class TypedefType(Type): + def __init__(self, name, json_data): + Type.__init__(self, name, json_data) + self.type = None + + class NativeType(Type): def __init__(self, name, json_data): Type.__init__(self, name, json_data, native=True) @@ -271,6 +277,10 @@ def link_callback(callback, types): types) +def link_typedef(typedef, types): + typedef.type = types[typedef.json_data['type']] + + # Sort structures so that if struct A has struct B as a member, then B is # listed before A. # @@ -321,6 +331,7 @@ def parse_json(json): 'callback': CallbackType, 'object': ObjectType, 'structure': StructureType, + 'typedef': TypedefType, } types = {} @@ -346,6 +357,9 @@ def parse_json(json): for callback in by_category['callback']: link_callback(callback, types) + for typedef in by_category['typedef']: + link_typedef(typedef, types) + for category in by_category.keys(): by_category[category] = sorted( by_category[category], key=lambda typ: typ.name.canonical_case()) diff --git a/generator/templates/dawn_native/wgpu_structs.h b/generator/templates/dawn_native/wgpu_structs.h index 3e267cb7fa..0bffebb73f 100644 --- a/generator/templates/dawn_native/wgpu_structs.h +++ b/generator/templates/dawn_native/wgpu_structs.h @@ -64,6 +64,10 @@ namespace dawn_native { {% endfor %} + {% for typeDef in by_category["typedef"] %} + using {{as_cppType(typeDef.name)}} = {{as_cppType(typeDef.type.name)}}; + {% endfor %} + } // namespace dawn_native #endif // DAWNNATIVE_WGPU_STRUCTS_H_ diff --git a/generator/templates/webgpu.h b/generator/templates/webgpu.h index 7704707e62..c44fa391be 100644 --- a/generator/templates/webgpu.h +++ b/generator/templates/webgpu.h @@ -117,6 +117,13 @@ typedef struct WGPUChainedStruct { {% endfor %} +{% for typeDef in by_category["typedef"] %} + // {{as_cType(typeDef.name)}} is deprecated. + // Use {{as_cType(typeDef.type.name)}} instead. + typedef {{as_cType(typeDef.type.name)}} {{as_cType(typeDef.name)}}; + +{% endfor %} + #ifdef __cplusplus extern "C" { #endif diff --git a/generator/templates/webgpu_cpp.h b/generator/templates/webgpu_cpp.h index 3e6b80c66d..40b804f0d7 100644 --- a/generator/templates/webgpu_cpp.h +++ b/generator/templates/webgpu_cpp.h @@ -63,6 +63,13 @@ namespace wgpu { struct {{as_cppType(type.name)}}; {% endfor %} + {% for typeDef in by_category["typedef"] %} + // {{as_cppType(typeDef.name)}} is deprecated. + // Use {{as_cppType(typeDef.type.name)}} instead. + using {{as_cppType(typeDef.name)}} = {{as_cppType(typeDef.type.name)}}; + + {% endfor %} + template class ObjectBase { public: