Reland "Add a per-thread proc table using thread local storage"

This is a reland of b04a92f01b
with the deletion of a duplicate exported function in dawn_wire that
was causing a compilation failure on Windows.

Original change's description:
> Add a per-thread proc table using thread local storage
>
> In situations where both dawn_wire and dawn_native are used on separate
> threads (Chrome with --single-process or --in-process-gpu), it's
> desirable to have a per-thread proc table so that the WebGPU C++ API can
> still be used. This eliminates classes of bugs with manual
> reference/release errors.
>
> This also changes many of the GetProcs functions to return const
> references to the static proc tables known at compile time, instead of a
> copy.
>
> Bug: none
> Change-Id: I8775bb715b312dd9476a1903fbd797d4b1302614
> Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/29240
> Reviewed-by: Stephen White <senorblanco@chromium.org>
> Reviewed-by: Corentin Wallez <cwallez@chromium.org>
> Commit-Queue: Austin Eng <enga@chromium.org>

Bug: none
Change-Id: Id90e5372132cd93a2f8631c8185d0e71b01bc1af
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/29443
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
This commit is contained in:
Austin Eng
2020-10-06 16:13:42 +00:00
committed by Commit Bot service account
parent e85652b61d
commit 16e01affcb
20 changed files with 247 additions and 42 deletions

View File

@@ -687,6 +687,10 @@ class MultiGeneratorFromDawnJSON(Generator):
renders.append(
FileRender('dawn_proc.c', 'src/dawn/dawn_proc.c',
[base_params, api_params]))
renders.append(
FileRender('dawn_thread_dispatch_proc.cpp',
'src/dawn/dawn_thread_dispatch_proc.cpp',
[base_params, api_params]))
if 'dawncpp' in targets:
renders.append(

View File

@@ -135,16 +135,17 @@ namespace dawn_native {
return result;
}
DawnProcTable GetProcsAutogen() {
DawnProcTable table;
table.getProcAddress = NativeGetProcAddress;
table.createInstance = NativeCreateInstance;
static DawnProcTable gProcTable = {
NativeGetProcAddress,
NativeCreateInstance,
{% for type in by_category["object"] %}
{% for method in c_methods(type) %}
table.{{as_varName(type.name, method.name)}} = Native{{as_MethodSuffix(type.name, method.name)}};
Native{{as_MethodSuffix(type.name, method.name)}},
{% endfor %}
{% endfor %}
return table;
}
};
const DawnProcTable& GetProcsAutogen() {
return gProcTable;
}
}

View File

@@ -17,6 +17,7 @@
#include "dawn/webgpu.h"
// Note: Often allocated as a static global. Do not add a complex constructor.
typedef struct DawnProcTable {
WGPUProcGetProcAddress getProcAddress;
WGPUProcCreateInstance createInstance;

View File

@@ -0,0 +1,52 @@
#include "dawn/dawn_thread_dispatch_proc.h"
#include <thread>
static DawnProcTable nullProcs;
thread_local DawnProcTable perThreadProcs;
void dawnProcSetPerThreadProcs(const DawnProcTable* procs) {
if (procs) {
perThreadProcs = *procs;
} else {
perThreadProcs = nullProcs;
}
}
static WGPUProc ThreadDispatchGetProcAddress(WGPUDevice device, const char* procName) {
return perThreadProcs.getProcAddress(device, procName);
}
static WGPUInstance ThreadDispatchCreateInstance(WGPUInstanceDescriptor const * descriptor) {
return perThreadProcs.createInstance(descriptor);
}
{% for type in by_category["object"] %}
{% for method in c_methods(type) %}
static {{as_cType(method.return_type.name)}} ThreadDispatch{{as_MethodSuffix(type.name, method.name)}}(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_annotated_cType(arg)}}
{%- endfor -%}
) {
{% if method.return_type.name.canonical_case() != "void" %}return {% endif %}
perThreadProcs.{{as_varName(type.name, method.name)}}({{as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_varName(arg.name)}}
{%- endfor -%}
);
}
{% endfor %}
{% 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 %}
};
}

View File

@@ -293,21 +293,16 @@ namespace dawn_wire { namespace client {
return result;
}
//* Some commands don't have a custom wire format, but need to be handled manually to update
//* some client-side state tracking. For these we have two functions:
//* - An autogenerated Client{{suffix}} method that sends the command on the wire
//* - A manual ProxyClient{{suffix}} method that will be inserted in the proctable instead of
//* the autogenerated one, and that will have to call Client{{suffix}}
DawnProcTable GetProcs() {
DawnProcTable table;
table.getProcAddress = ClientGetProcAddress;
table.createInstance = ClientCreateInstance;
static DawnProcTable gProcTable = {
ClientGetProcAddress,
ClientCreateInstance,
{% for type in by_category["object"] %}
{% for method in c_methods(type) %}
{% set suffix = as_MethodSuffix(type.name, method.name) %}
table.{{as_varName(type.name, method.name)}} = Client{{suffix}};
Client{{as_MethodSuffix(type.name, method.name)}},
{% endfor %}
{% endfor %}
return table;
};
const DawnProcTable& GetProcs() {
return gProcTable;
}
}} // namespace dawn_wire::client