@@ -11,137 +11,28 @@ index 50638e2456..efa42711e6 100644
1111- set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0" CACHE STRING "Minimum macOS version" FORCE)
1212- endif ()
1313\ No newline at end of file
14- diff --git a/src/emdawnwebgpu/CMakeLists.txt b/src/emdawnwebgpu/CMakeLists.txt
15- index 6e8ae37593..633af91eef 100644
16- --- a/src/emdawnwebgpu/CMakeLists.txt
17- +++ b/src/emdawnwebgpu/CMakeLists.txt
18- @@ -77,9 +77,17 @@ if (${DAWN_ENABLE_EMSCRIPTEN})
19- "${arg_UNPARSED_ARGUMENTS}")
20- endif()
21-
22- + # since Emscripten 4.0.3, file gen_struct_info.py is moved to outside of directory maint.
23- + if (EXISTS "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/gen_struct_info.py")
24- + set(EM_GEN_STRUCT_INFO_SCRIPT "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/gen_struct_info.py")
25- + elseif (EXISTS "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/maint/gen_struct_info.py")
26- + set(EM_GEN_STRUCT_INFO_SCRIPT "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/maint/gen_struct_info.py")
27- + else()
28- + message(FATAL_ERROR "Dawn: Failed to locate file gen_struct_info.py from Emscripten.")
29- + endif()
30- set(ARGS
31- ${Python3_EXECUTABLE}
32- - "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/maint/gen_struct_info.py"
33- + "${EM_GEN_STRUCT_INFO_SCRIPT}"
34- -q
35- "${EM_BUILD_GEN_DIR}/struct_info_webgpu.json"
36- "-I=${EM_BUILD_GEN_DIR}/include"
37- diff --git a/src/emdawnwebgpu/README.md b/src/emdawnwebgpu/README.md
38- index efd6491cd6..8ebc5d28b6 100644
39- --- a/src/emdawnwebgpu/README.md
40- +++ b/src/emdawnwebgpu/README.md
41- @@ -56,7 +56,7 @@ Set up the build directory using emcmake
42- mkdir out/cmake-wasm
43- cd out/cmake-wasm
44-
45- - # Make sure the path is to the source checkout of Emscripten, not emsdk's release.
46- + # If using Emscripten v4.0.2 or lower, make sure the path is to the source checkout of Emscripten, not emsdk's release.
47- emcmake cmake -GNinja -DDAWN_EMSCRIPTEN_TOOLCHAIN="path/to/emscripten" ../..
48-
49- ninja
5014diff --git a/third_party/emdawnwebgpu/webgpu.cpp b/third_party/emdawnwebgpu/webgpu.cpp
51- index f1c5a7d50e..16f2495712 100644
15+ index 5bfac41dcc..71a153daaa 100644
5216--- a/third_party/emdawnwebgpu/webgpu.cpp
5317+++ b/third_party/emdawnwebgpu/webgpu.cpp
54- @@ -131,7 +131,6 @@ class RefCounted : NonMovable {
55- bool Release() {
56- if (mRefCount.fetch_sub(1u, std::memory_order_release) == 1u) {
57- std::atomic_thread_fence(std::memory_order_acquire);
58- - emwgpuDelete(this);
59- return true;
60- }
61- return false;
62- @@ -234,6 +233,7 @@ class Ref {
63- static void Release(T value) {
64- if (value != nullptr && value->RefCounted::Release()) {
65- delete value;
66- + emwgpuDelete(value);
67- }
68- }
69-
70- @@ -641,7 +641,8 @@ struct WGPUAdapterImpl final : public EventSource, public RefCounted {
71- struct WGPUBufferImpl final : public EventSource,
72- public RefCountedWithExternalCount {
73- public:
74- - WGPUBufferImpl(const EventSource* source, bool mappedAtCreation);
75- + WGPUBufferImpl(const EventSource* source, bool mappedAtCreation, bool isExternal);
18+ @@ -692,6 +692,7 @@ struct WGPUBufferImpl final : public EventSource,
19+ WGPUBufferImpl(const EventSource* source, bool mappedAtCreation);
20+ // Injection constructor used when we already have a backing Buffer.
21+ WGPUBufferImpl(const EventSource* source, WGPUBufferMapState mapState);
7622+ ~WGPUBufferImpl();
7723
7824 void Destroy();
7925 const void* GetConstMappedRange(size_t offset, size_t size);
80- @@ -671,6 +672,7 @@ struct WGPUBufferImpl final : public EventSource,
81- };
82- MapRequest mPendingMapRequest;
83- WGPUBufferMapState mMapState;
84- + bool mIsExternal;
85- };
86-
87- struct WGPUQueueImpl final : public EventSource, public RefCounted {
88- @@ -1164,11 +1166,15 @@ WGPUAdapter emwgpuCreateAdapter(const EventSource* source) {
89-
90- WGPUBuffer emwgpuCreateBuffer(const EventSource* source,
91- bool mappedAtCreation = false) {
92- - return new WGPUBufferImpl(source, mappedAtCreation);
93- + return new WGPUBufferImpl(source, mappedAtCreation, true);
94- }
95-
96- WGPUDevice emwgpuCreateDevice(const EventSource* source, WGPUQueue queue) {
97- - return new WGPUDeviceImpl(source, queue);
98- + // This function is only called from JS via `importJsDevice()`, which
99- + // needs to increment the external ref count to fix the behavior.
100- + WGPUDeviceImpl* device = new WGPUDeviceImpl(source, queue);
101- + device->AddExternalRef();
102- + return device;
103- }
104-
105- WGPUQueue emwgpuCreateQueue(const EventSource* source) {
106- @@ -1275,15 +1281,22 @@ WGPUAdapterImpl::WGPUAdapterImpl(const EventSource* source)
107- // WGPUBuffer implementations.
108- // ----------------------------------------------------------------------------
109-
110- - WGPUBufferImpl::WGPUBufferImpl(const EventSource* source, bool mappedAtCreation)
111- + WGPUBufferImpl::WGPUBufferImpl(const EventSource* source, bool mappedAtCreation, bool isExternal)
112- : EventSource(source),
113- mMapState(mappedAtCreation ? WGPUBufferMapState_Mapped
114- - : WGPUBufferMapState_Unmapped) {
115- + : WGPUBufferMapState_Unmapped),
116- + mIsExternal(isExternal) {
117- if (mappedAtCreation) {
118- mPendingMapRequest = {kNullFutureId, WGPUMapMode_Write};
119- }
120- }
26+ @@ -1361,6 +1362,12 @@ WGPUBufferImpl::WGPUBufferImpl(const EventSource* source,
27+ RefCountedWithExternalCount(kImportedFromJS),
28+ mMapState(mapState) {}
12129
12230+ WGPUBufferImpl::~WGPUBufferImpl() {
123- + if (!mIsExternal ) {
31+ + if (!IsImported() ) {
12432+ Destroy();
12533+ }
12634+ }
12735+
12836 void WGPUBufferImpl::Destroy() {
12937 emwgpuBufferDestroy(this);
13038 AbortPendingMap("Buffer was destroyed before mapping was resolved.");
131- @@ -1504,6 +1517,7 @@ WGPUFuture WGPUShaderModuleImpl::GetCompilationInfo(
132- void wgpu##Name##Release(WGPU##Name o) { \
133- if (o->Release()) { \
134- delete o; \
135- + emwgpuDelete(o); \
136- } \
137- }
138- WGPU_OBJECTS(DEFINE_WGPU_DEFAULT_ADDREF_RELEASE)
139- @@ -1638,7 +1652,7 @@ void wgpuBufferUnmap(WGPUBuffer buffer) {
140-
141- WGPUBuffer wgpuDeviceCreateBuffer(WGPUDevice device,
142- const WGPUBufferDescriptor* descriptor) {
143- - WGPUBuffer buffer = new WGPUBufferImpl(device, descriptor->mappedAtCreation);
144- + WGPUBuffer buffer = new WGPUBufferImpl(device, descriptor->mappedAtCreation, false);
145- emwgpuDeviceCreateBuffer(device, descriptor, buffer);
146- return buffer;
147- }
0 commit comments