-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate files to use Emscripten bindings from outside Emscripten
We are stabilizing webgpu.h, and that process requires many (mostly small) breaking changes to the header. Right now, some projects are relying on Emscripten's and Dawn's implementations of webgpu.h to be in sync, but this creates challenges because we need to make many separate breaking changes. This change forks Emscripten's WebGPU bindings into Dawn (in //third_party) in such a way that they don't have to be checked into the Emscripten repository. This way we can keep Dawn's bindings and Dawn's fork of the Emscripten bindings in sync (approximately) in the Dawn repository. Projects depending on Emscripten and Dawn to be in sync should use this fork instead, until we've finished the webgpu.h changes and contributed the bindings back up to Emscripten in one big breaking release that should have minimal to no breakage afterward. For ease of diffing this commit overwrites the verbatim copies of library_{,html5_}webgpu.js added in the previous commit, with a few updates to bring it in sync with Dawn (though it may not be fully in sync - it has only been minimally tested so far). Example of using the resulting generated files from outside GN: https://github.com/kainino0x/webgpu-cross-platform-demo/compare/dawnwasm The generated files can only be generated using the GN build; CMake rules haven't been implemented, and the existing Emscripten build rules for CMake have been removed. Note, since these bindings were designed to live inside Emscripten, they rely on some Emscripten internals. Symbols in rough order from less stable to more stable: - `$stackSave` - changed recently - `$stackRestore` - changed recently - `$callUserCallback` - `$stringToUTF8OnStack` - `$stringToNewUTF8` - `$stringToUTF8` - `$lengthBytesUTF8` - `$readI53FromI64` - `$findCanvasEventTarget` - `$warnOnce` Generator internals, these seem stable: - `mergeInto` - `C_STRUCTS` - `LibraryManager.library` The build rules also depend on: - the location of `gen_struct_info.py` (which recently changed from `tools/` to `tools/maint/`) Bug: 346806934 Change-Id: I6ca10ea37bc7307d4d9571dcdc696e7d025663d3 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/193722 Reviewed-by: Loko Kung <lokokung@google.com> Auto-Submit: Kai Ninomiya <kainino@chromium.org> Reviewed-by: dan sinclair <dsinclair@chromium.org> Commit-Queue: dan sinclair <dsinclair@chromium.org>
- Loading branch information
Showing
23 changed files
with
851 additions
and
844 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
generator/templates/emdawnwebgpu/library_webgpu_enum_tables.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//* Copyright 2024 The Dawn & Tint Authors | ||
//* | ||
//* Redistribution and use in source and binary forms, with or without | ||
//* modification, are permitted provided that the following conditions are met: | ||
//* | ||
//* 1. Redistributions of source code must retain the above copyright notice, this | ||
//* list of conditions and the following disclaimer. | ||
//* | ||
//* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
//* this list of conditions and the following disclaimer in the documentation | ||
//* and/or other materials provided with the distribution. | ||
//* | ||
//* 3. Neither the name of the copyright holder nor the names of its | ||
//* contributors may be used to endorse or promote products derived from | ||
//* this software without specific prior written permission. | ||
//* | ||
//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
//* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
//* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
//* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
//* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
//* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
//* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
//* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
//* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
//* | ||
//* | ||
//* This generator is used to produce the number-to-string mappings for | ||
//* Emscripten's library_webgpu.js. | ||
//* https://github.com/emscripten-core/emscripten/blob/main/src/library_webgpu.js | ||
//* | ||
{{'{{{'}} | ||
globalThis.__HAVE_EMDAWNWEBGPU_ENUM_TABLES = true; | ||
|
||
// Constant values used at code-generation time in an Emscripten build. | ||
// These will not appear in the final build result, so we can just dump | ||
// every enum here without affecting binary size. | ||
globalThis.WEBGPU_ENUM_CONSTANT_TABLES = { | ||
{% for type in by_category["enum"] + by_category["bitmask"] %} | ||
{{type.name.CamelCase()}}: { | ||
{% for value in type.values %} | ||
'{{value.name.CamelCase()}}': {{value.value}}, | ||
{% endfor %} | ||
}, | ||
{% endfor %} | ||
}; | ||
|
||
// Maps from enum string back to enum number, for callbacks. | ||
// These appear in the final build result so should be kept minimal. | ||
globalThis.WEBGPU_STRING_TO_INT_TABLES = ` | ||
{% for type in by_category["enum"] if type.json_data.get("emscripten_string_to_int", False) %} | ||
Int_{{type.name.CamelCase()}}: { | ||
{% for value in type.values if value.json_data.get("emscripten_string_to_int", True) %} | ||
{% if type.name.name == 'device lost reason' and value.name.name == 'unknown' %} | ||
'undefined': {{value.value}}, // For older browsers | ||
{% endif %} | ||
{{as_jsEnumValue(value)}}: {{value.value}}, | ||
{% endfor %} | ||
}, | ||
{% endfor %} | ||
Int_PreferredFormat: { | ||
{% for value in types['texture format'].values if value.name.name in ['RGBA8 unorm', 'BGRA8 unorm'] %} | ||
{{as_jsEnumValue(value)}}: {{value.value}}, | ||
{% endfor %} | ||
}, | ||
`; | ||
|
||
// Maps from enum number to enum string. | ||
// These appear in the final build result so should be kept minimal. | ||
globalThis.WEBGPU_INT_TO_STRING_TABLES = ` | ||
{% for type in by_category["enum"] if not type.json_data.get("emscripten_no_enum_table") %} | ||
{{type.name.CamelCase()}}: {% if type.contiguousFromZero -%} | ||
[ | ||
{% for value in type.values %} | ||
{{as_jsEnumValue(value)}}, | ||
{% endfor %} | ||
] | ||
{%- else -%} | ||
{ | ||
{% for value in type.values %} | ||
{{value.value}}: {{as_jsEnumValue(value)}}, | ||
{% endfor %} | ||
} | ||
{%- endif -%} | ||
, | ||
{% endfor %} | ||
`; | ||
|
||
null; | ||
{{'}}}'}} |
77 changes: 77 additions & 0 deletions
77
generator/templates/emdawnwebgpu/library_webgpu_generated_sig_info.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//* Copyright 2024 The Dawn & Tint Authors | ||
//* | ||
//* Redistribution and use in source and binary forms, with or without | ||
//* modification, are permitted provided that the following conditions are met: | ||
//* | ||
//* 1. Redistributions of source code must retain the above copyright notice, this | ||
//* list of conditions and the following disclaimer. | ||
//* | ||
//* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
//* this list of conditions and the following disclaimer in the documentation | ||
//* and/or other materials provided with the distribution. | ||
//* | ||
//* 3. Neither the name of the copyright holder nor the names of its | ||
//* contributors may be used to endorse or promote products derived from | ||
//* this software without specific prior written permission. | ||
//* | ||
//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
//* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
//* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
//* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
//* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
//* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
//* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
//* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
//* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
//* | ||
//* | ||
//* This generator is used to produce the Wasm signatures for any C functions implemented in JS. | ||
//* It is equivalent to the emscripten_webgpu_* and wgpu* signatures in: | ||
//* https://github.com/emscripten-core/emscripten/blob/main/src/library_sigs.js | ||
//* | ||
{{'{{{'}} | ||
globalThis.__HAVE_EMDAWNWEBGPU_SIG_INFO = true; | ||
null; | ||
{{'}}}'}} | ||
{%- macro render_function_or_method_sig(object_or_none, function) -%} | ||
{%- if not has_wasmType(function.return_type, function.arguments) -%} | ||
// | ||
{%- endif -%} | ||
{{as_cMethod(object_or_none.name if object_or_none else None, function.name)}}__sig: ' | ||
{{- as_wasmType(function.return_type) -}} | ||
{%- if object_or_none -%} | ||
{{- as_wasmType(object_or_none) -}} | ||
{%- endif -%} | ||
{%- for arg in function.arguments -%} | ||
{{- as_wasmType(arg) -}} | ||
{%- endfor -%} | ||
', | ||
{%- endmacro -%} | ||
const webgpuSigs = { | ||
// emscripten_webgpu_* | ||
emscripten_webgpu_get_device__sig: 'p', | ||
emscripten_webgpu_release_js_handle__sig: 'vi', | ||
{% for type in by_category["object"] %} | ||
emscripten_webgpu_export_{{type.name.snake_case()}}__sig: 'ip', | ||
emscripten_webgpu_import_{{type.name.snake_case()}}__sig: 'pi', | ||
{% endfor %} | ||
|
||
// wgpu* | ||
{% for function in by_category["function"] %} | ||
{{render_function_or_method_sig(None, function)}} | ||
{% endfor %} | ||
{% for type in by_category["object"] %} | ||
{% for method in c_methods(type) %} | ||
{{render_function_or_method_sig(type, method)}} | ||
{% endfor %} | ||
{% endfor %} | ||
}; | ||
|
||
// Delete all of the WebGPU sig info from Emscripten's builtins first. | ||
for (const k of Object.keys(LibraryManager.library)) { | ||
if (k.endsWith('__sig') && (k.startsWith('emscripten_webgpu_') || k.startsWith('wgpu'))) { | ||
delete LibraryManager.library[k]; | ||
} | ||
} | ||
mergeInto(LibraryManager.library, webgpuSigs, {allowMissing: true}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.