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 %}
{% 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 {
{% set c_prefix = metadata.c_prefix %}
@@ -92,46 +129,31 @@ namespace {{native_namespace}} {
} // anonymous namespace
{% for function in by_category["function"] %}
{{as_cType(function.return_type.name)}} Native{{as_cppType(function.name)}}(
{%- for arg in function.arguments -%}
{% 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 %}
WGPUProc NativeGetProcAddress(WGPUDevice, const char* procName) {
if (procName == nullptr) {
return nullptr;
}
{% 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*> result;

View File

@@ -12,7 +12,6 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
#include "dawn/common/Log.h"
#include "dawn/wire/client/ApiObjects.h"
#include "dawn/wire/client/Client.h"
@@ -172,6 +171,7 @@ namespace dawn::wire::client {
{% endfor %}
{% endfor %}
};
const {{Prefix}}ProcTable& GetProcs() {
return gProcTable;
}