Configure the namespace of native implementation in api.json

Make ValidationUtils flexible for other generation.

BUG=dawn:1201
Change-Id: I42ccbd3d9c2fe37abec4b8f7eb395583dbe1dc8d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/72980
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
This commit is contained in:
fujunwei 2021-12-16 04:54:38 +00:00 committed by Dawn LUCI CQ
parent 8921987f08
commit a2241d402e
5 changed files with 32 additions and 16 deletions

View File

@ -22,6 +22,7 @@
"c_prefix": "WGPU",
"namespace": "wgpu",
"proc_table_prefix": "Dawn",
"native_namespace": "dawn native",
"copyright_year": "2019"
},

View File

@ -28,8 +28,10 @@ The `"_metadata"` key in the JSON file is used by flexible templates for generat
- `"api"` a string, the name of the Web API
- `"namespace"` a string, the namespace of C++ wrapper
- `"c_prefix"` (optional) a string, the prefix of C function and data type, it will default to upper-case of `"namespace"` if it's not provided.
- `"proc_table_prefix"` a string, the prefix of proc table.
- `"copyright_year"` (optional) a string, templates will use the the year of copyright.
- `"proc_table_prefix"` a string, the prefix of proc table.
- `"impl_dir"` a string, the directory of API implementation
- `"native_namespace"` a string, the namespace of native implementation
- `"copyright_year"` (optional) a string, templates will use the year of copyright.
The basic schema is that every entry is a thing with a `"category"` key what determines the sub-schema to apply to that thing. Categories and their sub-shema are defined below. Several parts of the schema use the concept of "record" which is a list of "record members" which are a combination of a type, a name and other metadata. For example the list of arguments of a function is a record. The list of structure members is a record. This combined concept is useful for the dawn_wire generator to generate code for structure and function calls in a very similar way.

View File

@ -29,6 +29,8 @@ class Metadata:
self.namespace = metadata['namespace']
self.c_prefix = metadata.get('c_prefix', self.namespace.upper())
self.proc_table_prefix = metadata['proc_table_prefix']
self.impl_dir = metadata.get('impl_dir', '')
self.native_namespace = metadata['native_namespace']
self.copyright_year = metadata.get('copyright_year', None)
@ -856,13 +858,15 @@ class MultiGeneratorFromDawnJSON(Generator):
}
]
impl_dir = metadata.impl_dir + '/' if metadata.impl_dir else ''
native_dir = impl_dir + Name(metadata.native_namespace).snake_case()
renders.append(
FileRender('dawn_native/ValidationUtils.h',
'src/dawn_native/ValidationUtils_autogen.h',
'src/' + native_dir + '/ValidationUtils_autogen.h',
frontend_params))
renders.append(
FileRender('dawn_native/ValidationUtils.cpp',
'src/dawn_native/ValidationUtils_autogen.cpp',
'src/' + native_dir + '/ValidationUtils_autogen.cpp',
frontend_params))
renders.append(
FileRender('dawn_native/dawn_platform.h',

View File

@ -12,15 +12,19 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
#include "dawn_native/ValidationUtils_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}}/ValidationUtils_autogen.h"
namespace dawn_native {
namespace {{native_namespace}} {
{% set namespace = metadata.namespace %}
{% for type in by_category["enum"] %}
MaybeError Validate{{type.name.CamelCase()}}(wgpu::{{as_cppType(type.name)}} value) {
MaybeError Validate{{type.name.CamelCase()}}({{namespace}}::{{as_cppType(type.name)}} value) {
switch (value) {
{% for value in type.values if value.valid %}
case wgpu::{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
case {{namespace}}::{{as_cppType(type.name)}}::{{as_cppEnum(value.name)}}:
return {};
{% endfor %}
default:
@ -31,8 +35,8 @@ namespace dawn_native {
{% endfor %}
{% for type in by_category["bitmask"] %}
MaybeError Validate{{type.name.CamelCase()}}(wgpu::{{as_cppType(type.name)}} value) {
if ((value & static_cast<wgpu::{{as_cppType(type.name)}}>(~{{type.full_mask}})) == 0) {
MaybeError Validate{{type.name.CamelCase()}}({{namespace}}::{{as_cppType(type.name)}} value) {
if ((value & static_cast<{{namespace}}::{{as_cppType(type.name)}}>(~{{type.full_mask}})) == 0) {
return {};
}
return DAWN_VALIDATION_ERROR("Invalid value for {{as_cType(type.name)}}");
@ -40,4 +44,4 @@ namespace dawn_native {
{% endfor %}
} // namespace dawn_native
} // namespace {{native_namespace}}

View File

@ -15,17 +15,22 @@
#ifndef BACKEND_VALIDATIONUTILS_H_
#define BACKEND_VALIDATIONUTILS_H_
#include "dawn/webgpu_cpp.h"
{% set api = metadata.api.lower() %}
#include "dawn/{{api}}_cpp.h"
#include "dawn_native/Error.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}}/Error.h"
namespace dawn_native {
namespace {{native_namespace}} {
// Helper functions to check the value of enums and bitmasks
{% for type in by_category["enum"] + by_category["bitmask"] %}
MaybeError Validate{{type.name.CamelCase()}}(wgpu::{{as_cppType(type.name)}} value);
{% set namespace = metadata.namespace %}
MaybeError Validate{{type.name.CamelCase()}}({{namespace}}::{{as_cppType(type.name)}} value);
{% endfor %}
} // namespace dawn_native
} // namespace {{native_namespace}}
#endif // BACKEND_VALIDATIONUTILS_H_