@@ -18,7 +18,7 @@ index 6e8ae37593..633af91eef 100644
18
18
@@ -77,9 +77,17 @@ if (${DAWN_ENABLE_EMSCRIPTEN})
19
19
"${arg_UNPARSED_ARGUMENTS}")
20
20
endif()
21
-
21
+
22
22
+ # since Emscripten 4.0.3, file gen_struct_info.py is moved to outside of directory maint.
23
23
+ if (EXISTS "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/gen_struct_info.py")
24
24
+ set(EM_GEN_STRUCT_INFO_SCRIPT "${DAWN_EMSCRIPTEN_TOOLCHAIN}/tools/gen_struct_info.py")
@@ -34,3 +34,114 @@ index 6e8ae37593..633af91eef 100644
34
34
-q
35
35
"${EM_BUILD_GEN_DIR}/struct_info_webgpu.json"
36
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
50
+ diff --git a/third_party/emdawnwebgpu/webgpu.cpp b/third_party/emdawnwebgpu/webgpu.cpp
51
+ index f1c5a7d50e..16f2495712 100644
52
+ --- a/third_party/emdawnwebgpu/webgpu.cpp
53
+ +++ 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);
76
+ + ~WGPUBufferImpl();
77
+
78
+ void Destroy();
79
+ 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
+ }
121
+
122
+ + WGPUBufferImpl::~WGPUBufferImpl() {
123
+ + if (!mIsExternal) {
124
+ + Destroy();
125
+ + }
126
+ + }
127
+ +
128
+ void WGPUBufferImpl::Destroy() {
129
+ emwgpuBufferDestroy(this);
130
+ 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