Add ability to generate constants from dawn.json
The "constant" category has two keys:
- `"type"`: a string, the name of the base data type
- `"value"`: a string, the value is defined with preprocessor macro
Remove deprecated constant InputStepMode.
BUG=dawn:1201, dawn:1023
Change-Id: If4308bd5b25dddd9453514ce362bebe4fd771a57
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/70704
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
diff --git a/dawn.json b/dawn.json
index e95b07e..3fc6764 100644
--- a/dawn.json
+++ b/dawn.json
@@ -2581,6 +2581,48 @@
{"value": 30, "name": "sint32x4"}
]
},
+ "whole size" : {
+ "category": "constant",
+ "type": "uint64_t",
+ "value": "(0xffffffffffffffffULL)"
+ },
+ "whole map size" : {
+ "category": "constant",
+ "type": "size_t",
+ "value": "SIZE_MAX"
+ },
+ "stride undefined" : {
+ "category": "constant",
+ "tags": ["deprecated"],
+ "_TODO": "crbug.com/dawn/520: Remove WGPU_STRIDE_UNDEFINED in favor of WGPU_COPY_STRIDE_UNDEFINED.",
+ "type": "uint32_t",
+ "value": "(0xffffffffUL)"
+ },
+ "copy stride undefined" : {
+ "category": "constant",
+ "type": "uint32_t",
+ "value": "(0xffffffffUL)"
+ },
+ "limit u32 undefined" : {
+ "category": "constant",
+ "type": "uint32_t",
+ "value": "(0xffffffffUL)"
+ },
+ "limit u64 undefined" : {
+ "category": "constant",
+ "type": "uint64_t",
+ "value": "(0xffffffffffffffffULL)"
+ },
+ "array layer count undefined" : {
+ "category": "constant",
+ "type": "uint32_t",
+ "value": "(0xffffffffUL)"
+ },
+ "mip level count undefined" : {
+ "category": "constant",
+ "type": "uint32_t",
+ "value": "(0xffffffffUL)"
+ },
"ObjectType": {
"_comment": "Only used for the wire",
"category": "native"
diff --git a/docs/codegen.md b/docs/codegen.md
index e6cb773..4683602 100644
--- a/docs/codegen.md
+++ b/docs/codegen.md
@@ -71,6 +71,10 @@
- `"return_type"` (default to no return type) a string that's the name of the return type.
- `"arguments"` a **record**, so an array of **record members**
+**`"constant"`**
+ - `"type"`: a string, the name of the base data type
+ - `"value"`: a string, the value is defined with preprocessor macro
+
## Dawn "wire" generators
The generator for the pieces of dawn_wire need additional data which is found in [`dawn_wire_json`](../dawn_wire.json). Examples of pieces that are generated are:
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index be2456a..034ad39 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -239,6 +239,14 @@
return self.chained == "out" or self.extensible == "out"
+class ConstantDefinition():
+ def __init__(self, is_enabled, name, json_data):
+ self.type = None
+ self.value = json_data['value']
+ self.json_data = json_data
+ self.name = Name(name)
+
+
class Command(Record):
def __init__(self, name, members=None):
Record.__init__(self, name)
@@ -310,6 +318,11 @@
typedef.type = types[typedef.json_data['type']]
+def link_constant(constant, types):
+ constant.type = types[constant.json_data['type']]
+ assert constant.type.name.native
+
+
# Sort structures so that if struct A has struct B as a member, then B is
# listed before A.
#
@@ -362,6 +375,7 @@
'object': ObjectType,
'structure': StructureType,
'typedef': TypedefType,
+ 'constant': ConstantDefinition,
}
types = {}
@@ -390,6 +404,9 @@
for typedef in by_category['typedef']:
link_typedef(typedef, types)
+ for constant in by_category['constant']:
+ link_constant(constant, 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/api.h b/generator/templates/api.h
index f03dbae..48e629c 100644
--- a/generator/templates/api.h
+++ b/generator/templates/api.h
@@ -75,17 +75,9 @@
#include <stddef.h>
#include <stdbool.h>
-#define WGPU_WHOLE_SIZE (0xffffffffffffffffULL)
-#define WGPU_WHOLE_MAP_SIZE SIZE_MAX
-{% if 'deprecated' in enabled_tags %}
- // TODO(crbug.com/dawn/520): Remove WGPU_STRIDE_UNDEFINED in favor of WGPU_COPY_STRIDE_UNDEFINED.
- #define WGPU_STRIDE_UNDEFINED (0xffffffffUL)
-{% endif %}
-#define WGPU_COPY_STRIDE_UNDEFINED (0xffffffffUL)
-#define WGPU_LIMIT_U32_UNDEFINED (0xffffffffUL)
-#define WGPU_LIMIT_U64_UNDEFINED (0xffffffffffffffffULL)
-#define WGPU_ARRAY_LAYER_COUNT_UNDEFINED (0xffffffffUL)
-#define WGPU_MIP_LEVEL_COUNT_UNDEFINED (0xffffffffUL)
+{% for constant in by_category["constant"] %}
+ #define {{c_prefix}}_{{constant.name.SNAKE_CASE()}} {{constant.value}}
+{% endfor %}
typedef uint32_t {{c_prefix}}Flags;
@@ -138,13 +130,6 @@
typedef {{as_cType(typeDef.type.name)}} {{as_cType(typeDef.name)}};
{% endfor %}
-{% if 'deprecated' in enabled_tags %}
- // TODO(crbug.com/dawn/1023): Remove after the deprecation period.
- #define WGPUInputStepMode_Vertex WGPUVertexStepMode_Vertex
- #define WGPUInputStepMode_Instance WGPUVertexStepMode_Instance
- #define WGPUInputStepMode_Force32 WGPUVertexStepMode_Force32
-
-{% endif %}
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/generator/templates/webgpu_cpp.h b/generator/templates/webgpu_cpp.h
index dfbe6c4..398cb72 100644
--- a/generator/templates/webgpu_cpp.h
+++ b/generator/templates/webgpu_cpp.h
@@ -19,17 +19,12 @@
namespace wgpu {
- static constexpr uint64_t kWholeSize = WGPU_WHOLE_SIZE;
- static constexpr size_t kWholeMapSize = WGPU_WHOLE_MAP_SIZE;
- {% if 'deprecated' in enabled_tags %}
- // TODO(crbug.com/520): Remove kStrideUndefined in favor of kCopyStrideUndefined.
- static constexpr uint32_t kStrideUndefined = WGPU_STRIDE_UNDEFINED;
- {% endif %}
- static constexpr uint32_t kCopyStrideUndefined = WGPU_COPY_STRIDE_UNDEFINED;
- static constexpr uint32_t kLimitU32Undefined = WGPU_LIMIT_U32_UNDEFINED;
- static constexpr uint64_t kLimitU64Undefined = WGPU_LIMIT_U64_UNDEFINED;
- static constexpr uint32_t kArrayLayerCountUndefined = WGPU_ARRAY_LAYER_COUNT_UNDEFINED;
- static constexpr uint32_t kMipLevelCountUndefined = WGPU_MIP_LEVEL_COUNT_UNDEFINED;
+ {% set c_prefix = metadata.c_prefix %}
+ {% for constant in by_category["constant"] %}
+ {% set type = as_cppType(constant.type.name) %}
+ {% set value = c_prefix + "_" + constant.name.SNAKE_CASE() %}
+ static constexpr {{type}} k{{as_cppType(constant.name)}} = {{ value }};
+ {% endfor %}
{% for type in by_category["enum"] %}
enum class {{as_cppType(type.name)}} : uint32_t {