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",
"c_prefix": "WGPU",
"namespace": "wgpu",
"proc_table_prefix": "Dawn",
"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
- `"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.
- `"proc_table_prefix"` a string, the prefix of proc table.
- `"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.

View File

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

View File

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

View File

@ -12,15 +12,17 @@
//* See the License for the specific language governing permissions and
//* limitations under the License.
#ifndef DAWN_DAWN_PROC_TABLE_H_
#define DAWN_DAWN_PROC_TABLE_H_
{% set Prefix = metadata.proc_table_prefix %}
#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.
typedef struct DawnProcTable {
WGPUProcGetProcAddress getProcAddress;
WGPUProcCreateInstance createInstance;
typedef struct {{Prefix}}ProcTable {
{% for function in by_category["function"] %}
{{as_cProc(None, function.name)}} {{as_varName(function.name)}};
{% endfor %}
{% for type in by_category["object"] %}
{% for method in c_methods(type) %}
@ -28,6 +30,6 @@ typedef struct DawnProcTable {
{% 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>
static DawnProcTable nullProcs;
thread_local DawnProcTable perThreadProcs;
static {{Prefix}}ProcTable nullProcs;
thread_local {{Prefix}}ProcTable perThreadProcs;
void dawnProcSetPerThreadProcs(const DawnProcTable* procs) {
void {{prefix}}ProcSetPerThreadProcs(const {{Prefix}}ProcTable* procs) {
if (procs) {
perThreadProcs = *procs;
} else {
@ -40,13 +42,14 @@ static WGPUInstance ThreadDispatchCreateInstance(WGPUInstanceDescriptor const *
{% endfor %}
extern "C" {
DawnProcTable dawnThreadDispatchProcTable = {
ThreadDispatchGetProcAddress,
ThreadDispatchCreateInstance,
{% for type in by_category["object"] %}
{% for method in c_methods(type) %}
ThreadDispatch{{as_MethodSuffix(type.name, method.name)}},
{% endfor %}
{% endfor %}
{{Prefix}}ProcTable {{prefix}}ThreadDispatchProcTable = {
{% for function in by_category["function"] %}
ThreadDispatch{{as_cppType(function.name)}},
{% endfor %}
{% for type in by_category["object"] %}
{% for method in c_methods(type) %}
ThreadDispatch{{as_MethodSuffix(type.name, method.name)}},
{% endfor %}
{% endfor %}
};
}

View File

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