Rework dawn/native/ProcTable.cpp's template to better handle functions.

This is a small rework to try to make the template better handle
functions that aren't associated with an object, when during an
unrelated patch I got confused that APICreateInstance didn't appear in
dawn::native.

Change-Id: If5a2aa3f9e348d1847e48fec4e90e5966ddd489d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86530
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2022-04-12 16:46:01 +00:00 committed by Dawn LUCI CQ
parent a5f5f5a1aa
commit b2fdd6402d
5 changed files with 71 additions and 43 deletions

View File

@ -76,6 +76,43 @@ namespace {{native_namespace}} {
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
{% for function in by_category["function"] if function.name.canonical_case() != "get proc address" %}
{{as_cType(function.return_type.name)}} Native{{function.name.CamelCase()}}(
{%- for arg in function.arguments -%}
{%- if not loop.first %}, {% endif -%}
{{as_annotated_cType(arg)}}
{%- endfor -%}
) {
{% for arg in function.arguments %}
{% set varName = as_varName(arg.name) %}
{% if arg.type.category in ["enum", "bitmask"] and arg.annotation == "value" %}
auto {{varName}}_ = static_cast<{{as_frontendType(arg.type)}}>({{varName}});
{% elif arg.annotation != "value" or arg.type.category == "object" %}
auto {{varName}}_ = reinterpret_cast<{{decorate("", as_frontendType(arg.type), arg)}}>({{varName}});
{% else %}
auto {{varName}}_ = {{as_varName(arg.name)}};
{% endif %}
{%- endfor-%}
{% if function.return_type.name.canonical_case() != "void" %}
auto result =
{%- endif %}
API{{function.name.CamelCase()}}(
{%- for arg in function.arguments -%}
{%- if not loop.first %}, {% endif -%}
{{as_varName(arg.name)}}_
{%- endfor -%}
);
{% if function.return_type.name.canonical_case() != "void" %}
{% if function.return_type.category == "object" %}
return ToAPI(result);
{% else %}
return result;
{% endif %}
{% endif %}
}
{% endfor %}
namespace { namespace {
{% set c_prefix = metadata.c_prefix %} {% set c_prefix = metadata.c_prefix %}
@ -92,46 +129,31 @@ namespace {{native_namespace}} {
} // anonymous namespace } // anonymous namespace
{% for function in by_category["function"] %} WGPUProc NativeGetProcAddress(WGPUDevice, const char* procName) {
{{as_cType(function.return_type.name)}} Native{{as_cppType(function.name)}}( if (procName == nullptr) {
{%- for arg in function.arguments -%} return nullptr;
{% if not loop.first %}, {% endif %}{{as_annotated_cType(arg)}}
{%- endfor -%}
) {
{% if function.name.canonical_case() == "get proc address" %}
if (procName == nullptr) {
return nullptr;
}
const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName,
[](const ProcEntry &a, const char *b) -> bool {
return strcmp(a.name, b) < 0;
}
);
if (entry != &sProcMap[sProcMapSize] && strcmp(entry->name, procName) == 0) {
return entry->proc;
}
// Special case the free-standing functions of the API.
// TODO(dawn:1238) Checking string one by one is slow, it needs to be optimized.
{% for function in by_category["function"] %}
if (strcmp(procName, "{{as_cMethod(None, function.name)}}") == 0) {
return reinterpret_cast<{{c_prefix}}Proc>(Native{{as_cppType(function.name)}});
}
{% endfor %}
return nullptr;
{% else %}
return ToAPI({{as_cppType(function.return_type.name)}}Base::Create(
{%- for arg in function.arguments -%}
FromAPI({% if not loop.first %}, {% endif %}{{as_varName(arg.name)}})
{%- endfor -%}
));
{% endif %}
} }
{% endfor %} const ProcEntry* entry = std::lower_bound(&sProcMap[0], &sProcMap[sProcMapSize], procName,
[](const ProcEntry &a, const char *b) -> bool {
return strcmp(a.name, b) < 0;
}
);
if (entry != &sProcMap[sProcMapSize] && strcmp(entry->name, procName) == 0) {
return entry->proc;
}
// Special case the free-standing functions of the API.
// TODO(dawn:1238) Checking string one by one is slow, it needs to be optimized.
{% for function in by_category["function"] %}
if (strcmp(procName, "{{as_cMethod(None, function.name)}}") == 0) {
return reinterpret_cast<{{c_prefix}}Proc>(Native{{as_cppType(function.name)}});
}
{% endfor %}
return nullptr;
}
std::vector<const char*> GetProcMapNamesForTestingInternal() { std::vector<const char*> GetProcMapNamesForTestingInternal() {
std::vector<const char*> result; std::vector<const char*> result;

View File

@ -12,7 +12,6 @@
//* 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.
#include "dawn/common/Log.h"
#include "dawn/wire/client/ApiObjects.h" #include "dawn/wire/client/ApiObjects.h"
#include "dawn/wire/client/Client.h" #include "dawn/wire/client/Client.h"
@ -172,6 +171,7 @@ namespace dawn::wire::client {
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
}; };
const {{Prefix}}ProcTable& GetProcs() { const {{Prefix}}ProcTable& GetProcs() {
return gProcTable; return gProcTable;
} }

View File

@ -188,7 +188,7 @@ namespace dawn::native {
// Instance // Instance
Instance::Instance(const WGPUInstanceDescriptor* desc) Instance::Instance(const WGPUInstanceDescriptor* desc)
: mImpl(InstanceBase::Create(reinterpret_cast<const InstanceDescriptor*>(desc))) { : mImpl(APICreateInstance(reinterpret_cast<const InstanceDescriptor*>(desc))) {
} }
Instance::~Instance() { Instance::~Instance() {

View File

@ -93,10 +93,14 @@ namespace dawn::native {
} // anonymous namespace } // anonymous namespace
InstanceBase* APICreateInstance(const InstanceDescriptor* descriptor) {
return InstanceBase::Create().Detach();
}
// InstanceBase // InstanceBase
// static // static
InstanceBase* InstanceBase::Create(const InstanceDescriptor* descriptor) { Ref<InstanceBase> InstanceBase::Create(const InstanceDescriptor* descriptor) {
Ref<InstanceBase> instance = AcquireRef(new InstanceBase); Ref<InstanceBase> instance = AcquireRef(new InstanceBase);
static constexpr InstanceDescriptor kDefaultDesc = {}; static constexpr InstanceDescriptor kDefaultDesc = {};
if (descriptor == nullptr) { if (descriptor == nullptr) {
@ -105,7 +109,7 @@ namespace dawn::native {
if (instance->ConsumedError(instance->Initialize(descriptor))) { if (instance->ConsumedError(instance->Initialize(descriptor))) {
return nullptr; return nullptr;
} }
return instance.Detach(); return instance;
} }
// TODO(crbug.com/dawn/832): make the platform an initialization parameter of the instance. // TODO(crbug.com/dawn/832): make the platform an initialization parameter of the instance.

View File

@ -39,11 +39,13 @@ namespace dawn::native {
using BackendsBitset = ityp::bitset<wgpu::BackendType, kEnumCount<wgpu::BackendType>>; using BackendsBitset = ityp::bitset<wgpu::BackendType, kEnumCount<wgpu::BackendType>>;
InstanceBase* APICreateInstance(const InstanceDescriptor* descriptor);
// This is called InstanceBase for consistency across the frontend, even if the backends don't // This is called InstanceBase for consistency across the frontend, even if the backends don't
// specialize this class. // specialize this class.
class InstanceBase final : public RefCounted { class InstanceBase final : public RefCounted {
public: public:
static InstanceBase* Create(const InstanceDescriptor* descriptor = nullptr); static Ref<InstanceBase> Create(const InstanceDescriptor* descriptor = nullptr);
void APIRequestAdapter(const RequestAdapterOptions* options, void APIRequestAdapter(const RequestAdapterOptions* options,
WGPURequestAdapterCallback callback, WGPURequestAdapterCallback callback,