Make the templates of native struct flexible
Rename wgpu_structs.cpp/h to api_structs.cpp/h and replace hardcode contents
with metadata.
BUG=dawn:1201
Change-Id: I4f2978c6abec7b492da142499890733567e2cec3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/73300
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index 1d810ca..60dfbe7 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -860,6 +860,7 @@
impl_dir = metadata.impl_dir + '/' if metadata.impl_dir else ''
native_dir = impl_dir + Name(metadata.native_namespace).snake_case()
+ namespace = metadata.namespace
renders.append(
FileRender('dawn_native/ValidationUtils.h',
'src/' + native_dir + '/ValidationUtils_autogen.h',
@@ -873,12 +874,12 @@
'src/' + native_dir + '/' + prefix + '_platform_autogen.h',
frontend_params))
renders.append(
- FileRender('dawn_native/wgpu_structs.h',
- 'src/dawn_native/wgpu_structs_autogen.h',
+ FileRender('dawn_native/api_structs.h',
+ 'src/' + native_dir + '/' + namespace + '_structs_autogen.h',
frontend_params))
renders.append(
- FileRender('dawn_native/wgpu_structs.cpp',
- 'src/dawn_native/wgpu_structs_autogen.cpp',
+ FileRender('dawn_native/api_structs.cpp',
+ 'src/' + native_dir + '/' + namespace + '_structs_autogen.cpp',
frontend_params))
renders.append(
FileRender('dawn_native/ProcTable.cpp',
diff --git a/generator/templates/dawn_native/wgpu_structs.cpp b/generator/templates/dawn_native/api_structs.cpp
similarity index 76%
rename from generator/templates/dawn_native/wgpu_structs.cpp
rename to generator/templates/dawn_native/api_structs.cpp
index e1ed8b2..40aca8f 100644
--- a/generator/templates/dawn_native/wgpu_structs.cpp
+++ b/generator/templates/dawn_native/api_structs.cpp
@@ -12,24 +12,29 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
-#include "dawn_native/wgpu_structs_autogen.h"
+{% set native_namespace = Name(metadata.native_namespace).snake_case() %}
+{% set impl_dir = metadata.impl_dir + "/" if metadata.impl_dir else "" %}
+{% set native_dir = impl_dir + native_namespace %}
+{% set namespace = metadata.namespace %}
+#include "{{native_dir}}/{{namespace}}_structs_autogen.h"
#include <tuple>
#ifdef __GNUC__
-// error: 'offsetof' within non-standard-layout type 'wgpu::XXX' is conditionally-supported
+// error: 'offsetof' within non-standard-layout type '{{namespace}}::XXX' is conditionally-supported
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#endif
-namespace dawn_native {
+namespace {{native_namespace}} {
- static_assert(sizeof(ChainedStruct) == sizeof(WGPUChainedStruct),
+ {% set c_prefix = metadata.c_prefix %}
+ static_assert(sizeof(ChainedStruct) == sizeof({{c_prefix}}ChainedStruct),
"sizeof mismatch for ChainedStruct");
- static_assert(alignof(ChainedStruct) == alignof(WGPUChainedStruct),
+ static_assert(alignof(ChainedStruct) == alignof({{c_prefix}}ChainedStruct),
"alignof mismatch for ChainedStruct");
- static_assert(offsetof(ChainedStruct, nextInChain) == offsetof(WGPUChainedStruct, next),
+ static_assert(offsetof(ChainedStruct, nextInChain) == offsetof({{c_prefix}}ChainedStruct, next),
"offsetof mismatch for ChainedStruct::nextInChain");
- static_assert(offsetof(ChainedStruct, sType) == offsetof(WGPUChainedStruct, sType),
+ static_assert(offsetof(ChainedStruct, sType) == offsetof({{c_prefix}}ChainedStruct, sType),
"offsetof mismatch for ChainedStruct::sType");
{% for type in by_category["structure"] %}
@@ -66,4 +71,4 @@
}
{% endfor %}
-}
+} // namespace {{native_namespace}}
diff --git a/generator/templates/dawn_native/wgpu_structs.h b/generator/templates/dawn_native/api_structs.h
similarity index 74%
rename from generator/templates/dawn_native/wgpu_structs.h
rename to generator/templates/dawn_native/api_structs.h
index 2249540..2bf4296 100644
--- a/generator/templates/dawn_native/wgpu_structs.h
+++ b/generator/templates/dawn_native/api_structs.h
@@ -12,13 +12,20 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
-#ifndef DAWNNATIVE_WGPU_STRUCTS_H_
-#define DAWNNATIVE_WGPU_STRUCTS_H_
+{% set namespace_name = Name(metadata.native_namespace) %}
+{% set DIR = namespace_name.concatcase().upper() %}
+{% set namespace = metadata.namespace %}
+#ifndef {{DIR}}_{{namespace.upper()}}_STRUCTS_H_
+#define {{DIR}}_{{namespace.upper()}}_STRUCTS_H_
-#include "dawn/webgpu_cpp.h"
-#include "dawn_native/Forward.h"
+{% set api = metadata.api.lower() %}
+#include "dawn/{{api}}_cpp.h"
+{% set impl_dir = metadata.impl_dir + "/" if metadata.impl_dir else "" %}
+{% set native_namespace = namespace_name.snake_case() %}
+{% set native_dir = impl_dir + native_namespace %}
+#include "{{native_dir}}/Forward.h"
-namespace dawn_native {
+namespace {{native_namespace}} {
{% macro render_cpp_default_value(member) -%}
{%- if member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
@@ -26,7 +33,7 @@
{%- elif member.type.category == "object" and member.optional -%}
{{" "}}= nullptr
{%- elif member.type.category in ["enum", "bitmask"] and member.default_value != None -%}
- {{" "}}= wgpu::{{as_cppType(member.type.name)}}::{{as_cppEnum(Name(member.default_value))}}
+ {{" "}}= {{namespace}}::{{as_cppType(member.type.name)}}::{{as_cppEnum(Name(member.default_value))}}
{%- elif member.type.category == "native" and member.default_value != None -%}
{{" "}}= {{member.default_value}}
{%- else -%}
@@ -36,14 +43,14 @@
struct ChainedStruct {
ChainedStruct const * nextInChain = nullptr;
- wgpu::SType sType = wgpu::SType::Invalid;
+ {{namespace}}::SType sType = {{namespace}}::SType::Invalid;
};
{% for type in by_category["structure"] %}
{% if type.chained %}
struct {{as_cppType(type.name)}} : ChainedStruct {
{{as_cppType(type.name)}}() {
- sType = wgpu::SType::{{type.name.CamelCase()}};
+ sType = {{namespace}}::SType::{{type.name.CamelCase()}};
}
{% else %}
struct {{as_cppType(type.name)}} {
@@ -72,6 +79,6 @@
using {{as_cppType(typeDef.name)}} = {{as_cppType(typeDef.type.name)}};
{% endfor %}
-} // namespace dawn_native
+} // namespace {{native_namespace}}
-#endif // DAWNNATIVE_WGPU_STRUCTS_H_
+#endif // {{DIR}}_{{namespace.upper()}}_STRUCTS_H_