Make the prefix of proc table configurable in api.json

Add a metadata to configure the prefix of proc table and Make proc table
flexiable with the prefix and declared functions.

BUG=dawn:1201
Change-Id: Id28e5521506fa5dc8efca90a7883fbd3dd548e8d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/71526
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
fujunwei 2021-12-05 05:29:44 +00:00 committed by Dawn LUCI CQ
parent 1c7d3efcd0
commit 3a464767a5
7 changed files with 45 additions and 31 deletions

View File

@ -21,6 +21,7 @@
"api": "WebGPU", "api": "WebGPU",
"c_prefix": "WGPU", "c_prefix": "WGPU",
"namespace": "wgpu", "namespace": "wgpu",
"proc_table_prefix": "Dawn",
"copyright_year": "2019" "copyright_year": "2019"
}, },

View File

@ -28,6 +28,7 @@ The `"_metadata"` key in the JSON file is used by flexible templates for generat
- `"api"` a string, the name of the Web API - `"api"` a string, the name of the Web API
- `"namespace"` a string, the namespace of C++ wrapper - `"namespace"` a string, the namespace of C++ wrapper
- `"c_prefix"` (optional) a string, the prefix of C function and data type, it will default to upper-case of `"namespace"` if it's not provided. - `"c_prefix"` (optional) a string, the prefix of C function and data type, it will default to upper-case of `"namespace"` if it's not provided.
- `"proc_table_prefix"` a string, the prefix of proc table.
- `"copyright_year"` (optional) a string, templates will use the the year of copyright. - `"copyright_year"` (optional) a string, templates will use the the year of copyright.
The basic schema is that every entry is a thing with a `"category"` key what determines the sub-schema to apply to that thing. Categories and their sub-shema are defined below. Several parts of the schema use the concept of "record" which is a list of "record members" which are a combination of a type, a name and other metadata. For example the list of arguments of a function is a record. The list of structure members is a record. This combined concept is useful for the dawn_wire generator to generate code for structure and function calls in a very similar way. The basic schema is that every entry is a thing with a `"category"` key what determines the sub-schema to apply to that thing. Categories and their sub-shema are defined below. Several parts of the schema use the concept of "record" which is a list of "record members" which are a combination of a type, a name and other metadata. For example the list of arguments of a function is a record. The list of structure members is a record. This combined concept is useful for the dawn_wire generator to generate code for structure and function calls in a very similar way.

View File

@ -28,6 +28,7 @@ class Metadata:
self.api = metadata['api'] self.api = metadata['api']
self.namespace = metadata['namespace'] self.namespace = metadata['namespace']
self.c_prefix = metadata.get('c_prefix', self.namespace.upper()) self.c_prefix = metadata.get('c_prefix', self.namespace.upper())
self.proc_table_prefix = metadata['proc_table_prefix']
self.copyright_year = metadata.get('copyright_year', None) self.copyright_year = metadata.get('copyright_year', None)
@ -759,12 +760,13 @@ class MultiGeneratorFromDawnJSON(Generator):
api_file_name = metadata.api.lower() api_file_name = metadata.api.lower()
if 'dawn_headers' in targets: if 'dawn_headers' in targets:
prefix = metadata.proc_table_prefix.lower()
renders.append( renders.append(
FileRender('api.h', 'src/include/dawn/' + api_file_name + '.h', FileRender('api.h', 'src/include/dawn/' + api_file_name + '.h',
[RENDER_PARAMS_BASE, params_dawn])) [RENDER_PARAMS_BASE, params_dawn]))
renders.append( renders.append(
FileRender('dawn_proc_table.h', FileRender('dawn_proc_table.h',
'src/include/dawn/dawn_proc_table.h', 'src/include/dawn/' + prefix + '_proc_table.h',
[RENDER_PARAMS_BASE, params_dawn])) [RENDER_PARAMS_BASE, params_dawn]))
if 'dawncpp_headers' in targets: if 'dawncpp_headers' in targets:

View File

@ -12,8 +12,10 @@
//* 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_native/dawn_platform.h" {% set Prefix = metadata.proc_table_prefix %}
#include "dawn_native/DawnNative.h" {% set prefix = Prefix.lower() %}
#include "dawn_native/{{prefix}}_platform.h"
#include "dawn_native/{{Prefix}}Native.h"
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
@ -124,9 +126,10 @@ namespace dawn_native {
return result; return result;
} }
static DawnProcTable gProcTable = { static {{Prefix}}ProcTable gProcTable = {
NativeGetProcAddress, {% for function in by_category["function"] %}
NativeCreateInstance, Native{{as_cppType(function.name)}},
{% endfor %}
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{% for method in c_methods(type) %} {% for method in c_methods(type) %}
Native{{as_MethodSuffix(type.name, method.name)}}, Native{{as_MethodSuffix(type.name, method.name)}},
@ -134,7 +137,7 @@ namespace dawn_native {
{% endfor %} {% endfor %}
}; };
const DawnProcTable& GetProcsAutogen() { const {{Prefix}}ProcTable& GetProcsAutogen() {
return gProcTable; return gProcTable;
} }
} }

View File

@ -12,15 +12,17 @@
//* 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.
#ifndef DAWN_DAWN_PROC_TABLE_H_ {% set Prefix = metadata.proc_table_prefix %}
#define DAWN_DAWN_PROC_TABLE_H_ #ifndef DAWN_{{Prefix.upper()}}_PROC_TABLE_H_
#define DAWN_{{Prefix.upper()}}_PROC_TABLE_H_
#include "dawn/webgpu.h" #include "dawn/{{metadata.api.lower()}}.h"
// Note: Often allocated as a static global. Do not add a complex constructor. // Note: Often allocated as a static global. Do not add a complex constructor.
typedef struct DawnProcTable { typedef struct {{Prefix}}ProcTable {
WGPUProcGetProcAddress getProcAddress; {% for function in by_category["function"] %}
WGPUProcCreateInstance createInstance; {{as_cProc(None, function.name)}} {{as_varName(function.name)}};
{% endfor %}
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{% for method in c_methods(type) %} {% for method in c_methods(type) %}
@ -28,6 +30,6 @@ typedef struct DawnProcTable {
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
} DawnProcTable; } {{Prefix}}ProcTable;
#endif // DAWN_DAWN_PROC_TABLE_H_ #endif // DAWN_{{Prefix.upper()}}_PROC_TABLE_H_

View File

@ -1,11 +1,13 @@
#include "dawn/dawn_thread_dispatch_proc.h" {% set Prefix = metadata.proc_table_prefix %}
{% set prefix = Prefix.lower() %}
#include "dawn/{{prefix}}_thread_dispatch_proc.h"
#include <thread> #include <thread>
static DawnProcTable nullProcs; static {{Prefix}}ProcTable nullProcs;
thread_local DawnProcTable perThreadProcs; thread_local {{Prefix}}ProcTable perThreadProcs;
void dawnProcSetPerThreadProcs(const DawnProcTable* procs) { void {{prefix}}ProcSetPerThreadProcs(const {{Prefix}}ProcTable* procs) {
if (procs) { if (procs) {
perThreadProcs = *procs; perThreadProcs = *procs;
} else { } else {
@ -40,13 +42,14 @@ static WGPUInstance ThreadDispatchCreateInstance(WGPUInstanceDescriptor const *
{% endfor %} {% endfor %}
extern "C" { extern "C" {
DawnProcTable dawnThreadDispatchProcTable = { {{Prefix}}ProcTable {{prefix}}ThreadDispatchProcTable = {
ThreadDispatchGetProcAddress, {% for function in by_category["function"] %}
ThreadDispatchCreateInstance, ThreadDispatch{{as_cppType(function.name)}},
{% for type in by_category["object"] %} {% endfor %}
{% for method in c_methods(type) %} {% for type in by_category["object"] %}
ThreadDispatch{{as_MethodSuffix(type.name, method.name)}}, {% for method in c_methods(type) %}
{% endfor %} ThreadDispatch{{as_MethodSuffix(type.name, method.name)}},
{% endfor %} {% endfor %}
{% endfor %}
}; };
} }

View File

@ -159,16 +159,18 @@ namespace dawn_wire { namespace client {
return result; return result;
} }
static DawnProcTable gProcTable = { {% set Prefix = metadata.proc_table_prefix %}
ClientGetProcAddress, static {{Prefix}}ProcTable gProcTable = {
ClientCreateInstance, {% for function in by_category["function"] %}
Client{{as_cppType(function.name)}},
{% endfor %}
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{% for method in c_methods(type) %} {% for method in c_methods(type) %}
Client{{as_MethodSuffix(type.name, method.name)}}, Client{{as_MethodSuffix(type.name, method.name)}},
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
}; };
const DawnProcTable& GetProcs() { const {{Prefix}}ProcTable& GetProcs() {
return gProcTable; return gProcTable;
} }
}} // namespace dawn_wire::client }} // namespace dawn_wire::client