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>
This commit is contained in:
parent
d7304d1971
commit
d3cac111b6
|
@ -580,6 +580,8 @@ def convert_cType_to_cppType(typ, annotation, arg, indent=0):
|
||||||
converted_members = ',\n'.join(converted_members)
|
converted_members = ',\n'.join(converted_members)
|
||||||
|
|
||||||
return as_cppType(typ.name) + ' {\n' + converted_members + '\n}'
|
return as_cppType(typ.name) + ' {\n' + converted_members + '\n}'
|
||||||
|
elif typ.category == 'function pointer':
|
||||||
|
return 'reinterpret_cast<{}>({})'.format(as_cppType(typ.name), arg)
|
||||||
else:
|
else:
|
||||||
return 'static_cast<{}>({})'.format(as_cppType(typ.name), arg)
|
return 'static_cast<{}>({})'.format(as_cppType(typ.name), arg)
|
||||||
else:
|
else:
|
||||||
|
@ -794,7 +796,7 @@ class MultiGeneratorFromDawnJSON(Generator):
|
||||||
|
|
||||||
if 'dawncpp' in targets:
|
if 'dawncpp' in targets:
|
||||||
renders.append(
|
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]))
|
[RENDER_PARAMS_BASE, params_dawn]))
|
||||||
|
|
||||||
if 'webgpu_headers' in targets:
|
if 'webgpu_headers' in targets:
|
||||||
|
@ -814,7 +816,7 @@ class MultiGeneratorFromDawnJSON(Generator):
|
||||||
FileRender('api_cpp.h', 'emscripten-bits/' + api + '_cpp.h',
|
FileRender('api_cpp.h', 'emscripten-bits/' + api + '_cpp.h',
|
||||||
[RENDER_PARAMS_BASE, params_emscripten]))
|
[RENDER_PARAMS_BASE, params_emscripten]))
|
||||||
renders.append(
|
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]))
|
[RENDER_PARAMS_BASE, params_emscripten]))
|
||||||
renders.append(
|
renders.append(
|
||||||
FileRender('webgpu_struct_info.json',
|
FileRender('webgpu_struct_info.json',
|
||||||
|
|
|
@ -11,18 +11,19 @@
|
||||||
//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
//* See the License for the specific language governing permissions and
|
//* See the License for the specific language governing permissions and
|
||||||
//* limitations under the License.
|
//* limitations under the License.
|
||||||
|
{% set api = metadata.api.lower() %}
|
||||||
{% if 'dawn' in enabled_tags %}
|
{% if 'dawn' in enabled_tags %}
|
||||||
#include "dawn/webgpu_cpp.h"
|
#include "dawn/{{api}}_cpp.h"
|
||||||
{% else %}
|
{% else %}
|
||||||
#include "webgpu/webgpu_cpp.h"
|
#include "{{api}}/{{api}}_cpp.h"
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#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"
|
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace wgpu {
|
namespace {{metadata.namespace}} {
|
||||||
{% for type in by_category["enum"] %}
|
{% for type in by_category["enum"] %}
|
||||||
{% set CppType = as_cppType(type.name) %}
|
{% set CppType = as_cppType(type.name) %}
|
||||||
{% set CType = as_cType(type.name) %}
|
{% set CType = as_cType(type.name) %}
|
||||||
|
@ -53,13 +54,14 @@ namespace wgpu {
|
||||||
|
|
||||||
// ChainedStruct
|
// 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");
|
"sizeof mismatch for ChainedStruct");
|
||||||
static_assert(alignof(ChainedStruct) == alignof(WGPUChainedStruct),
|
static_assert(alignof(ChainedStruct) == alignof({{c_prefix}}ChainedStruct),
|
||||||
"alignof mismatch for 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");
|
"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");
|
"offsetof mismatch for ChainedStruct::sType");
|
||||||
{% for type in by_category["structure"] %}
|
{% for type in by_category["structure"] %}
|
||||||
{% set CppType = as_cppType(type.name) %}
|
{% set CppType = as_cppType(type.name) %}
|
||||||
|
@ -81,6 +83,22 @@ namespace wgpu {
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% 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"] %}
|
{% for type in by_category["object"] %}
|
||||||
{% set CppType = as_cppType(type.name) %}
|
{% set CppType = as_cppType(type.name) %}
|
||||||
{% set CType = as_cType(type.name) %}
|
{% set CType = as_cType(type.name) %}
|
||||||
|
@ -106,20 +124,7 @@ namespace wgpu {
|
||||||
|
|
||||||
{%- macro render_cpp_to_c_method_call(type, method) -%}
|
{%- macro render_cpp_to_c_method_call(type, method) -%}
|
||||||
{{as_cMethod(type.name, method.name)}}(Get()
|
{{as_cMethod(type.name, method.name)}}(Get()
|
||||||
{%- for arg in method.arguments -%},{{" "}}
|
{%- for arg in method.arguments -%},{{" "}}{{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 -%}
|
|
||||||
{%- endfor -%}
|
{%- endfor -%}
|
||||||
)
|
)
|
||||||
{%- endmacro -%}
|
{%- endmacro -%}
|
||||||
|
@ -134,28 +139,37 @@ namespace wgpu {
|
||||||
{% endif %}
|
{% endif %}
|
||||||
}
|
}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
void {{CppType}}::WGPUReference({{CType}} handle) {
|
void {{CppType}}::{{c_prefix}}Reference({{CType}} handle) {
|
||||||
if (handle != nullptr) {
|
if (handle != nullptr) {
|
||||||
{{as_cMethod(type.name, Name("reference"))}}(handle);
|
{{as_cMethod(type.name, Name("reference"))}}(handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void {{CppType}}::WGPURelease({{CType}} handle) {
|
void {{CppType}}::{{c_prefix}}Release({{CType}} handle) {
|
||||||
if (handle != nullptr) {
|
if (handle != nullptr) {
|
||||||
{{as_cMethod(type.name, Name("release"))}}(handle);
|
{{as_cMethod(type.name, Name("release"))}}(handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
// Instance
|
// Function
|
||||||
|
|
||||||
Instance CreateInstance(const InstanceDescriptor* descriptor) {
|
{% for function in by_category["function"] %}
|
||||||
const WGPUInstanceDescriptor* cDescriptor =
|
{%- macro render_function_call(function) -%}
|
||||||
reinterpret_cast<const WGPUInstanceDescriptor*>(descriptor);
|
{{as_cMethod(None, function.name)}}(
|
||||||
return Instance::Acquire(wgpuCreateInstance(cDescriptor));
|
{%- 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) {
|
{{as_cppType(function.return_type.name) | indent(4, true) }} {{as_cppType(function.name) }}(
|
||||||
return reinterpret_cast<Proc>(wgpuGetProcAddress(device.Get(), procName));
|
{%- 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 %}
|
||||||
|
|
||||||
}
|
}
|
|
@ -189,11 +189,10 @@ namespace {{metadata.namespace}} {
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% for function in by_category["function"] %}
|
{% 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 -%}
|
{%- for arg in function.arguments -%}
|
||||||
{% if not loop.first %}, {% endif %}
|
{%- if not loop.first %}, {% endif -%}
|
||||||
{{as_annotated_cppType(arg)}}
|
{{as_annotated_cppType(arg)}}{{render_cpp_default_value(arg, False)}}
|
||||||
{{render_cpp_default_value(arg, False)}}
|
|
||||||
{%- endfor -%}
|
{%- endfor -%}
|
||||||
);
|
);
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
Loading…
Reference in New Issue