Make the templates of ChainUtils and ObjectType flexible
Replace hardcode contents with metadata.
BUG=dawn:1201
Change-Id: I5e000edfeae3cc597127e487da29455c99fa8de2
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/73920
Reviewed-by: ningxin hu <ningxin.hu@intel.com>
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 6a89240..35638cc 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -901,11 +901,11 @@
'src/dawn_native/ProcTable.cpp', frontend_params))
renders.append(
FileRender('dawn_native/ChainUtils.h',
- 'src/dawn_native/ChainUtils_autogen.h',
+ 'src/' + native_dir + '/ChainUtils_autogen.h',
frontend_params))
renders.append(
FileRender('dawn_native/ChainUtils.cpp',
- 'src/dawn_native/ChainUtils_autogen.cpp',
+ 'src/' + native_dir + '/ChainUtils_autogen.cpp',
frontend_params))
renders.append(
FileRender('dawn_native/webgpu_absl_format.h',
@@ -917,11 +917,11 @@
frontend_params))
renders.append(
FileRender('dawn_native/ObjectType.h',
- 'src/dawn_native/ObjectType_autogen.h',
+ 'src/' + native_dir + '/ObjectType_autogen.h',
frontend_params))
renders.append(
FileRender('dawn_native/ObjectType.cpp',
- 'src/dawn_native/ObjectType_autogen.cpp',
+ 'src/' + native_dir + '/ObjectType_autogen.cpp',
frontend_params))
if 'dawn_wire' in targets:
diff --git a/generator/templates/dawn_native/ChainUtils.cpp b/generator/templates/dawn_native/ChainUtils.cpp
index 2a42db2..11d150b 100644
--- a/generator/templates/dawn_native/ChainUtils.cpp
+++ b/generator/templates/dawn_native/ChainUtils.cpp
@@ -12,17 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "dawn_native/ChainUtils_autogen.h"
+{% set impl_dir = metadata.impl_dir + "/" if metadata.impl_dir else "" %}
+{% set native_namespace = Name(metadata.native_namespace).snake_case() %}
+{% set native_dir = impl_dir + native_namespace %}
+#include "{{native_dir}}/ChainUtils_autogen.h"
#include <unordered_set>
-namespace dawn_native {
+namespace {{native_namespace}} {
+{% set namespace = metadata.namespace %}
{% for value in types["s type"].values %}
{% if value.valid %}
void FindInChain(const ChainedStruct* chain, const {{as_cppEnum(value.name)}}** out) {
for (; chain; chain = chain->nextInChain) {
- if (chain->sType == wgpu::SType::{{as_cppEnum(value.name)}}) {
+ if (chain->sType == {{namespace}}::SType::{{as_cppEnum(value.name)}}) {
*out = static_cast<const {{as_cppEnum(value.name)}}*>(chain);
break;
}
@@ -32,8 +36,8 @@
{% endfor %}
MaybeError ValidateSTypes(const ChainedStruct* chain,
- std::vector<std::vector<wgpu::SType>> oneOfConstraints) {
- std::unordered_set<wgpu::SType> allSTypes;
+ std::vector<std::vector<{{namespace}}::SType>> oneOfConstraints) {
+ std::unordered_set<{{namespace}}::SType> allSTypes;
for (; chain; chain = chain->nextInChain) {
if (allSTypes.find(chain->sType) != allSTypes.end()) {
return DAWN_VALIDATION_ERROR("Chain cannot have duplicate sTypes");
@@ -42,7 +46,7 @@
}
for (const auto& oneOfConstraint : oneOfConstraints) {
bool satisfied = false;
- for (wgpu::SType oneOfSType : oneOfConstraint) {
+ for ({{namespace}}::SType oneOfSType : oneOfConstraint) {
if (allSTypes.find(oneOfSType) != allSTypes.end()) {
if (satisfied) {
return DAWN_VALIDATION_ERROR("Unsupported sType combination");
@@ -58,4 +62,4 @@
return {};
}
-} // namespace dawn_native
+} // namespace {{native_namespace}}
diff --git a/generator/templates/dawn_native/ChainUtils.h b/generator/templates/dawn_native/ChainUtils.h
index 7bc1dae..37cf5ef 100644
--- a/generator/templates/dawn_native/ChainUtils.h
+++ b/generator/templates/dawn_native/ChainUtils.h
@@ -12,13 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#ifndef DAWNNATIVE_CHAIN_UTILS_H_
-#define DAWNNATIVE_CHAIN_UTILS_H_
+{% set namespace_name = Name(metadata.native_namespace) %}
+{% set DIR = namespace_name.concatcase().upper() %}
+#ifndef {{DIR}}_CHAIN_UTILS_H_
+#define {{DIR}}_CHAIN_UTILS_H_
-#include "dawn_native/dawn_platform.h"
-#include "dawn_native/Error.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 %}
+{% set prefix = metadata.proc_table_prefix.lower() %}
+#include "{{native_dir}}/{{prefix}}_platform.h"
+#include "{{native_dir}}/Error.h"
-namespace dawn_native {
+namespace {{native_namespace}} {
{% for value in types["s type"].values %}
{% if value.valid %}
void FindInChain(const ChainedStruct* chain, const {{as_cppEnum(value.name)}}** out);
@@ -31,8 +37,9 @@
// For example:
// ValidateSTypes(chain, { { ShaderModuleSPIRVDescriptor, ShaderModuleWGSLDescriptor } }))
// ValidateSTypes(chain, { { Extension1 }, { Extension2 } })
+ {% set namespace = metadata.namespace %}
MaybeError ValidateSTypes(const ChainedStruct* chain,
- std::vector<std::vector<wgpu::SType>> oneOfConstraints);
+ std::vector<std::vector<{{namespace}}::SType>> oneOfConstraints);
template <typename T>
MaybeError ValidateSingleSTypeInner(const ChainedStruct* chain, T sType) {
@@ -73,6 +80,6 @@
return ValidateSingleSTypeInner(chain, sType, sTypes...);
}
-} // namespace dawn_native
+} // namespace {{native_namespace}}
-#endif // DAWNNATIVE_CHAIN_UTILS_H_
+#endif // {{DIR}}_CHAIN_UTILS_H_
diff --git a/generator/templates/dawn_native/ObjectType.cpp b/generator/templates/dawn_native/ObjectType.cpp
index df3e0fc..0f79327 100644
--- a/generator/templates/dawn_native/ObjectType.cpp
+++ b/generator/templates/dawn_native/ObjectType.cpp
@@ -12,9 +12,12 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
-#include "dawn_native/ObjectType_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 %}
+#include "{{native_dir}}/ObjectType_autogen.h"
-namespace dawn_native {
+namespace {{native_namespace}} {
const char* ObjectTypeAsString(ObjectType type) {
switch (type) {
@@ -27,4 +30,4 @@
}
}
-} // namespace dawn_native
+} // namespace {{native_namespace}}
diff --git a/generator/templates/dawn_native/ObjectType.h b/generator/templates/dawn_native/ObjectType.h
index cde4b07..6430e6f 100644
--- a/generator/templates/dawn_native/ObjectType.h
+++ b/generator/templates/dawn_native/ObjectType.h
@@ -12,14 +12,17 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
-#ifndef DAWNNATIVE_OBJECTTPYE_AUTOGEN_H_
-#define DAWNNATIVE_OBJECTTPYE_AUTOGEN_H_
+{% set namespace_name = Name(metadata.native_namespace) %}
+{% set DIR = namespace_name.concatcase().upper() %}
+#ifndef {{DIR}}_OBJECTTPYE_AUTOGEN_H_
+#define {{DIR}}_OBJECTTPYE_AUTOGEN_H_
#include "common/ityp_array.h"
#include <cstdint>
-namespace dawn_native {
+{% set native_namespace = namespace_name.snake_case() %}
+namespace {{native_namespace}} {
enum class ObjectType : uint32_t {
{% for type in by_category["object"] %}
@@ -32,7 +35,7 @@
const char* ObjectTypeAsString(ObjectType type);
-} // namespace dawn_native
+} // namespace {{native_namespace}}
-#endif // DAWNNATIVE_OBJECTTPYE_AUTOGEN_H_
+#endif // {{DIR}}_OBJECTTPYE_AUTOGEN_H_