2018-07-18 09:40:26 +00:00
|
|
|
//* Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//*
|
|
|
|
//* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
//* you may not use this file except in compliance with the License.
|
|
|
|
//* You may obtain a copy of the License at
|
|
|
|
//*
|
|
|
|
//* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//*
|
|
|
|
//* Unless required by applicable law or agreed to in writing, software
|
|
|
|
//* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
//* 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.
|
2021-12-07 00:46:35 +00:00
|
|
|
{% set API = metadata.api.upper() %}
|
|
|
|
{% set api = API.lower() %}
|
2023-05-16 04:50:32 +00:00
|
|
|
{% if 'dawn' in enabled_tags %}
|
2022-02-09 19:42:51 +00:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#error "Do not include this header. Emscripten already provides headers needed for {{metadata.api}}."
|
|
|
|
#endif
|
|
|
|
{% endif %}
|
2021-12-07 00:46:35 +00:00
|
|
|
#ifndef {{API}}_CPP_H_
|
|
|
|
#define {{API}}_CPP_H_
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-12-07 00:46:35 +00:00
|
|
|
#include "dawn/{{api}}.h"
|
2023-05-16 04:50:32 +00:00
|
|
|
#include "dawn/{{api}}_cpp_chained_struct.h"
|
2018-07-18 12:32:45 +00:00
|
|
|
#include "dawn/EnumClassBitmasks.h"
|
2022-02-19 00:44:12 +00:00
|
|
|
#include <cmath>
|
2023-05-25 23:19:48 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-12-07 00:46:35 +00:00
|
|
|
namespace {{metadata.namespace}} {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-11-25 08:44:01 +00:00
|
|
|
{% 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 %}
|
2019-07-17 08:54:19 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
{% for type in by_category["enum"] %}
|
|
|
|
enum class {{as_cppType(type.name)}} : uint32_t {
|
|
|
|
{% for value in type.values %}
|
|
|
|
{{as_cppEnum(value.name)}} = 0x{{format(value.value, "08X")}},
|
|
|
|
{% endfor %}
|
|
|
|
};
|
|
|
|
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
{% for type in by_category["bitmask"] %}
|
|
|
|
enum class {{as_cppType(type.name)}} : uint32_t {
|
|
|
|
{% for value in type.values %}
|
|
|
|
{{as_cppEnum(value.name)}} = 0x{{format(value.value, "08X")}},
|
|
|
|
{% endfor %}
|
|
|
|
};
|
|
|
|
|
|
|
|
{% endfor %}
|
|
|
|
|
2021-12-02 07:41:21 +00:00
|
|
|
{% for type in by_category["function pointer"] %}
|
2017-04-20 18:42:36 +00:00
|
|
|
using {{as_cppType(type.name)}} = {{as_cType(type.name)}};
|
|
|
|
{% endfor %}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
class {{as_cppType(type.name)}};
|
|
|
|
{% endfor %}
|
|
|
|
|
2018-05-17 20:55:53 +00:00
|
|
|
{% for type in by_category["structure"] %}
|
2018-08-27 21:12:56 +00:00
|
|
|
struct {{as_cppType(type.name)}};
|
2018-05-17 20:55:53 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2021-02-08 19:48:06 +00:00
|
|
|
{% for typeDef in by_category["typedef"] %}
|
|
|
|
// {{as_cppType(typeDef.name)}} is deprecated.
|
|
|
|
// Use {{as_cppType(typeDef.type.name)}} instead.
|
|
|
|
using {{as_cppType(typeDef.name)}} = {{as_cppType(typeDef.type.name)}};
|
|
|
|
|
|
|
|
{% endfor %}
|
2017-04-20 18:38:20 +00:00
|
|
|
template<typename Derived, typename CType>
|
|
|
|
class ObjectBase {
|
2019-07-08 19:20:22 +00:00
|
|
|
public:
|
|
|
|
ObjectBase() = default;
|
|
|
|
ObjectBase(CType handle): mHandle(handle) {
|
2021-12-07 00:46:35 +00:00
|
|
|
if (mHandle) Derived::{{c_prefix}}Reference(mHandle);
|
2019-07-08 19:20:22 +00:00
|
|
|
}
|
|
|
|
~ObjectBase() {
|
2021-12-07 00:46:35 +00:00
|
|
|
if (mHandle) Derived::{{c_prefix}}Release(mHandle);
|
2019-07-08 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectBase(ObjectBase const& other)
|
|
|
|
: ObjectBase(other.Get()) {
|
|
|
|
}
|
|
|
|
Derived& operator=(ObjectBase const& other) {
|
|
|
|
if (&other != this) {
|
2021-12-07 00:46:35 +00:00
|
|
|
if (mHandle) Derived::{{c_prefix}}Release(mHandle);
|
2019-07-08 19:20:22 +00:00
|
|
|
mHandle = other.mHandle;
|
2021-12-07 00:46:35 +00:00
|
|
|
if (mHandle) Derived::{{c_prefix}}Reference(mHandle);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:20:22 +00:00
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-07-08 19:20:22 +00:00
|
|
|
ObjectBase(ObjectBase&& other) {
|
|
|
|
mHandle = other.mHandle;
|
|
|
|
other.mHandle = 0;
|
|
|
|
}
|
|
|
|
Derived& operator=(ObjectBase&& other) {
|
|
|
|
if (&other != this) {
|
2021-12-07 00:46:35 +00:00
|
|
|
if (mHandle) Derived::{{c_prefix}}Release(mHandle);
|
2017-11-23 21:04:26 +00:00
|
|
|
mHandle = other.mHandle;
|
|
|
|
other.mHandle = 0;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:20:22 +00:00
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
2018-12-04 12:13:03 +00:00
|
|
|
|
2019-07-08 19:20:22 +00:00
|
|
|
ObjectBase(std::nullptr_t) {}
|
|
|
|
Derived& operator=(std::nullptr_t) {
|
|
|
|
if (mHandle != nullptr) {
|
2021-12-07 00:46:35 +00:00
|
|
|
Derived::{{c_prefix}}Release(mHandle);
|
2019-07-08 19:20:22 +00:00
|
|
|
mHandle = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2019-07-08 19:20:22 +00:00
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:02:22 +00:00
|
|
|
bool operator==(std::nullptr_t) const {
|
|
|
|
return mHandle == nullptr;
|
|
|
|
}
|
|
|
|
bool operator!=(std::nullptr_t) const {
|
|
|
|
return mHandle != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:20:22 +00:00
|
|
|
explicit operator bool() const {
|
|
|
|
return mHandle != nullptr;
|
|
|
|
}
|
|
|
|
CType Get() const {
|
|
|
|
return mHandle;
|
|
|
|
}
|
2023-05-10 23:48:22 +00:00
|
|
|
// TODO(dawn:1639) Deprecate Release after uses have been removed.
|
2019-07-08 19:20:22 +00:00
|
|
|
CType Release() {
|
|
|
|
CType result = mHandle;
|
|
|
|
mHandle = 0;
|
|
|
|
return result;
|
2023-05-10 23:48:22 +00:00
|
|
|
}
|
|
|
|
CType MoveToCHandle() {
|
|
|
|
CType result = mHandle;
|
|
|
|
mHandle = 0;
|
|
|
|
return result;
|
2019-07-08 19:20:22 +00:00
|
|
|
}
|
|
|
|
static Derived Acquire(CType handle) {
|
|
|
|
Derived result;
|
|
|
|
result.mHandle = handle;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
CType mHandle = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2020-11-04 10:04:17 +00:00
|
|
|
{% macro render_cpp_default_value(member, is_struct=True) -%}
|
2022-07-26 17:36:44 +00:00
|
|
|
{%- if member.json_data.get("no_default", false) -%}
|
|
|
|
{%- elif member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
|
2020-10-29 13:09:12 +00:00
|
|
|
{{" "}}= nullptr
|
2020-11-04 10:04:17 +00:00
|
|
|
{%- elif member.type.category == "object" and member.optional and is_struct -%}
|
2019-07-08 19:20:22 +00:00
|
|
|
{{" "}}= nullptr
|
|
|
|
{%- elif member.type.category in ["enum", "bitmask"] and member.default_value != None -%}
|
|
|
|
{{" "}}= {{as_cppType(member.type.name)}}::{{as_cppEnum(Name(member.default_value))}}
|
|
|
|
{%- elif member.type.category == "native" and member.default_value != None -%}
|
|
|
|
{{" "}}= {{member.default_value}}
|
2022-02-19 00:44:12 +00:00
|
|
|
{%- elif member.default_value != None -%}
|
|
|
|
{{" "}}= {{member.default_value}}
|
2019-07-08 19:20:22 +00:00
|
|
|
{%- else -%}
|
|
|
|
{{assert(member.default_value == None)}}
|
|
|
|
{%- endif -%}
|
|
|
|
{%- endmacro %}
|
|
|
|
|
|
|
|
{% macro render_cpp_method_declaration(type, method) %}
|
|
|
|
{% set CppType = as_cppType(type.name) %}
|
2019-10-15 11:44:38 +00:00
|
|
|
{{as_cppType(method.return_type.name)}} {{method.name.CamelCase()}}(
|
2019-07-08 19:20:22 +00:00
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
{%- if not loop.first %}, {% endif -%}
|
|
|
|
{%- if arg.type.category == "object" and arg.annotation == "value" -%}
|
|
|
|
{{as_cppType(arg.type.name)}} const& {{as_varName(arg.name)}}
|
|
|
|
{%- else -%}
|
|
|
|
{{as_annotated_cppType(arg)}}
|
|
|
|
{%- endif -%}
|
2020-11-04 10:04:17 +00:00
|
|
|
{{render_cpp_default_value(arg, False)}}
|
2019-07-08 19:20:22 +00:00
|
|
|
{%- endfor -%}
|
|
|
|
) const
|
|
|
|
{%- endmacro %}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
{% set CppType = as_cppType(type.name) %}
|
|
|
|
{% set CType = as_cType(type.name) %}
|
2019-01-09 09:05:18 +00:00
|
|
|
class {{CppType}} : public ObjectBase<{{CppType}}, {{CType}}> {
|
2019-07-08 19:20:22 +00:00
|
|
|
public:
|
|
|
|
using ObjectBase::ObjectBase;
|
|
|
|
using ObjectBase::operator=;
|
|
|
|
|
2019-11-22 14:02:52 +00:00
|
|
|
{% for method in type.methods %}
|
2019-07-08 19:20:22 +00:00
|
|
|
{{render_cpp_method_declaration(type, method)}};
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend ObjectBase<{{CppType}}, {{CType}}>;
|
2021-12-07 00:46:35 +00:00
|
|
|
static void {{c_prefix}}Reference({{CType}} handle);
|
|
|
|
static void {{c_prefix}}Release({{CType}} handle);
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
{% endfor %}
|
|
|
|
|
2021-12-07 00:46:35 +00:00
|
|
|
{% for function in by_category["function"] %}
|
2021-12-14 02:20:15 +00:00
|
|
|
{{as_cppType(function.return_type.name)}} {{as_cppType(function.name)}}(
|
2021-12-07 00:46:35 +00:00
|
|
|
{%- for arg in function.arguments -%}
|
2021-12-14 02:20:15 +00:00
|
|
|
{%- if not loop.first %}, {% endif -%}
|
|
|
|
{{as_annotated_cppType(arg)}}{{render_cpp_default_value(arg, False)}}
|
2021-12-07 00:46:35 +00:00
|
|
|
{%- endfor -%}
|
|
|
|
);
|
|
|
|
{% endfor %}
|
2019-10-15 12:08:48 +00:00
|
|
|
|
2018-08-27 21:12:56 +00:00
|
|
|
{% for type in by_category["structure"] %}
|
2021-09-17 15:36:00 +00:00
|
|
|
{% set Out = "Out" if type.output else "" %}
|
|
|
|
{% set const = "const" if not type.output else "" %}
|
2020-01-15 09:54:42 +00:00
|
|
|
{% if type.chained %}
|
2022-07-14 12:58:25 +00:00
|
|
|
{% for root in type.chain_roots %}
|
|
|
|
// Can be chained in {{as_cppType(root.name)}}
|
|
|
|
{% endfor %}
|
2021-09-17 15:36:00 +00:00
|
|
|
struct {{as_cppType(type.name)}} : ChainedStruct{{Out}} {
|
2020-01-15 09:54:42 +00:00
|
|
|
{{as_cppType(type.name)}}() {
|
|
|
|
sType = SType::{{type.name.CamelCase()}};
|
|
|
|
}
|
|
|
|
{% else %}
|
|
|
|
struct {{as_cppType(type.name)}} {
|
|
|
|
{% endif %}
|
2018-08-27 21:12:56 +00:00
|
|
|
{% if type.extensible %}
|
2021-09-17 15:36:00 +00:00
|
|
|
ChainedStruct{{Out}} {{const}} * nextInChain = nullptr;
|
2018-08-27 21:12:56 +00:00
|
|
|
{% endif %}
|
|
|
|
{% for member in type.members %}
|
2020-04-03 17:37:48 +00:00
|
|
|
{% set member_declaration = as_annotated_cppType(member) + render_cpp_default_value(member) %}
|
|
|
|
{% if type.chained and loop.first %}
|
2022-06-29 17:37:41 +00:00
|
|
|
//* Align the first member after ChainedStruct to match the C struct layout.
|
|
|
|
//* It has to be aligned both to its natural and ChainedStruct's alignment.
|
|
|
|
static constexpr size_t kFirstMemberAlignment = detail::ConstexprMax(alignof(ChainedStruct{{out}}), alignof({{decorate("", as_cppType(member.type.name), member)}}));
|
|
|
|
alignas(kFirstMemberAlignment) {{member_declaration}};
|
2020-04-03 17:37:48 +00:00
|
|
|
{% else %}
|
|
|
|
{{member_declaration}};
|
|
|
|
{% endif %}
|
2018-08-27 21:12:56 +00:00
|
|
|
{% endfor %}
|
|
|
|
};
|
|
|
|
|
|
|
|
{% endfor %}
|
2021-12-08 05:46:17 +00:00
|
|
|
|
|
|
|
// The operators of EnumClassBitmmasks in the dawn:: namespace need to be imported
|
|
|
|
// in the {{metadata.namespace}} namespace for Argument Dependent Lookup.
|
|
|
|
DAWN_IMPORT_BITMASK_OPERATORS
|
2021-12-07 00:46:35 +00:00
|
|
|
} // namespace {{metadata.namespace}}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-12-08 05:46:17 +00:00
|
|
|
namespace dawn {
|
|
|
|
{% for type in by_category["bitmask"] %}
|
|
|
|
template<>
|
|
|
|
struct IsDawnBitmask<{{metadata.namespace}}::{{as_cppType(type.name)}}> {
|
|
|
|
static constexpr bool enable = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
{% endfor %}
|
|
|
|
} // namespace dawn
|
|
|
|
|
2021-12-07 00:46:35 +00:00
|
|
|
#endif // {{API}}_CPP_H_
|