Template hardcode functions in webgpu_cpp.cpp
Rename webgpu_cpp.cpp to api_cpp.cpp and make it flexible.
BUG=dawn:1201
Change-Id: Ib964be9399e9733c4fe9de5d1cd0361182bb0836
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/72460
Reviewed-by: Corentin Wallez <cwallez@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 fd95ccc..18c0e95 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -580,6 +580,8 @@
converted_members = ',\n'.join(converted_members)
return as_cppType(typ.name) + ' {\n' + converted_members + '\n}'
+ elif typ.category == 'function pointer':
+ return 'reinterpret_cast<{}>({})'.format(as_cppType(typ.name), arg)
else:
return 'static_cast<{}>({})'.format(as_cppType(typ.name), arg)
else:
@@ -794,7 +796,7 @@
if 'dawncpp' in targets:
renders.append(
- FileRender('webgpu_cpp.cpp', 'src/dawn/webgpu_cpp.cpp',
+ FileRender('api_cpp.cpp', 'src/dawn/' + api + '_cpp.cpp',
[RENDER_PARAMS_BASE, params_dawn]))
if 'webgpu_headers' in targets:
@@ -814,7 +816,7 @@
FileRender('api_cpp.h', 'emscripten-bits/' + api + '_cpp.h',
[RENDER_PARAMS_BASE, params_emscripten]))
renders.append(
- FileRender('webgpu_cpp.cpp', 'emscripten-bits/webgpu_cpp.cpp',
+ FileRender('api_cpp.cpp', 'emscripten-bits/' + api + '_cpp.cpp',
[RENDER_PARAMS_BASE, params_emscripten]))
renders.append(
FileRender('webgpu_struct_info.json',
diff --git a/generator/templates/webgpu_cpp.cpp b/generator/templates/api_cpp.cpp
similarity index 69%
rename from generator/templates/webgpu_cpp.cpp
rename to generator/templates/api_cpp.cpp
index 86a70ee..d540e0b 100644
--- a/generator/templates/webgpu_cpp.cpp
+++ b/generator/templates/api_cpp.cpp
@@ -11,18 +11,19 @@
//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//* See the License for the specific language governing permissions and
//* limitations under the License.
+{% set api = metadata.api.lower() %}
{% if 'dawn' in enabled_tags %}
- #include "dawn/webgpu_cpp.h"
+ #include "dawn/{{api}}_cpp.h"
{% else %}
- #include "webgpu/webgpu_cpp.h"
+ #include "{{api}}/{{api}}_cpp.h"
{% endif %}
#ifdef __GNUC__
-// error: 'offsetof' within non-standard-layout type 'wgpu::XXX' is conditionally-supported
+// error: 'offsetof' within non-standard-layout type '{{metadata.namespace}}::XXX' is conditionally-supported
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#endif
-namespace wgpu {
+namespace {{metadata.namespace}} {
{% for type in by_category["enum"] %}
{% set CppType = as_cppType(type.name) %}
{% set CType = as_cType(type.name) %}
@@ -53,13 +54,14 @@
// ChainedStruct
- 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"] %}
{% set CppType = as_cppType(type.name) %}
@@ -81,6 +83,22 @@
{% endfor %}
{% endfor -%}
+ {%- macro render_c_actual_arg(arg) -%}
+ {%- if arg.annotation == "value" -%}
+ {%- if arg.type.category == "object" -%}
+ {{as_varName(arg.name)}}.Get()
+ {%- elif arg.type.category == "enum" or arg.type.category == "bitmask" -%}
+ static_cast<{{as_cType(arg.type.name)}}>({{as_varName(arg.name)}})
+ {%- elif arg.type.category in ["function pointer", "native"] -%}
+ {{as_varName(arg.name)}}
+ {%- else -%}
+ UNHANDLED
+ {%- endif -%}
+ {%- else -%}
+ reinterpret_cast<{{decorate("", as_cType(arg.type.name), arg)}}>({{as_varName(arg.name)}})
+ {%- endif -%}
+ {%- endmacro -%}
+
{% for type in by_category["object"] %}
{% set CppType = as_cppType(type.name) %}
{% set CType = as_cType(type.name) %}
@@ -106,20 +124,7 @@
{%- macro render_cpp_to_c_method_call(type, method) -%}
{{as_cMethod(type.name, method.name)}}(Get()
- {%- for arg in method.arguments -%},{{" "}}
- {%- if arg.annotation == "value" -%}
- {%- if arg.type.category == "object" -%}
- {{as_varName(arg.name)}}.Get()
- {%- elif arg.type.category == "enum" or arg.type.category == "bitmask" -%}
- static_cast<{{as_cType(arg.type.name)}}>({{as_varName(arg.name)}})
- {%- elif arg.type.category in ["function pointer", "native"] -%}
- {{as_varName(arg.name)}}
- {%- else -%}
- UNHANDLED
- {%- endif -%}
- {%- else -%}
- reinterpret_cast<{{decorate("", as_cType(arg.type.name), arg)}}>({{as_varName(arg.name)}})
- {%- endif -%}
+ {%- for arg in method.arguments -%},{{" "}}{{render_c_actual_arg(arg)}}
{%- endfor -%}
)
{%- endmacro -%}
@@ -134,28 +139,37 @@
{% endif %}
}
{% endfor %}
- void {{CppType}}::WGPUReference({{CType}} handle) {
+ void {{CppType}}::{{c_prefix}}Reference({{CType}} handle) {
if (handle != nullptr) {
{{as_cMethod(type.name, Name("reference"))}}(handle);
}
}
- void {{CppType}}::WGPURelease({{CType}} handle) {
+ void {{CppType}}::{{c_prefix}}Release({{CType}} handle) {
if (handle != nullptr) {
{{as_cMethod(type.name, Name("release"))}}(handle);
}
}
{% endfor %}
- // Instance
+ // Function
- Instance CreateInstance(const InstanceDescriptor* descriptor) {
- const WGPUInstanceDescriptor* cDescriptor =
- reinterpret_cast<const WGPUInstanceDescriptor*>(descriptor);
- return Instance::Acquire(wgpuCreateInstance(cDescriptor));
- }
+ {% for function in by_category["function"] %}
+ {%- macro render_function_call(function) -%}
+ {{as_cMethod(None, function.name)}}(
+ {%- for arg in function.arguments -%}
+ {% if not loop.first %}, {% endif %}{{render_c_actual_arg(arg)}}
+ {%- endfor -%}
+ )
+ {%- endmacro -%}
- Proc GetProcAddress(Device const& device, const char* procName) {
- return reinterpret_cast<Proc>(wgpuGetProcAddress(device.Get(), procName));
- }
+ {{as_cppType(function.return_type.name) | indent(4, true) }} {{as_cppType(function.name) }}(
+ {%- for arg in function.arguments -%}
+ {% if not loop.first %}, {% endif %}{{as_annotated_cppType(arg)}}
+ {%- endfor -%}
+ ) {
+ auto result = {{render_function_call(function)}};
+ return {{convert_cType_to_cppType(function.return_type, 'value', 'result')}};
+ }
+ {% endfor %}
}
diff --git a/generator/templates/api_cpp.h b/generator/templates/api_cpp.h
index cc61a8c..f9b6bf6 100644
--- a/generator/templates/api_cpp.h
+++ b/generator/templates/api_cpp.h
@@ -189,11 +189,10 @@
{% endfor %}
{% for function in by_category["function"] %}
- {{as_cppType(function.return_type.name)}} {{as_cppType(function.name)}}(
+ {{as_cppType(function.return_type.name)}} {{as_cppType(function.name)}}(
{%- for arg in function.arguments -%}
- {% if not loop.first %}, {% endif %}
- {{as_annotated_cppType(arg)}}
- {{render_cpp_default_value(arg, False)}}
+ {%- if not loop.first %}, {% endif -%}
+ {{as_annotated_cppType(arg)}}{{render_cpp_default_value(arg, False)}}
{%- endfor -%}
);
{% endfor %}