Skip to content

Commit ce7fff4

Browse files
committed
Fix function pointers to work on WASM
1 parent 8124d0f commit ce7fff4

5 files changed

Lines changed: 24 additions & 29 deletions

File tree

WebGPUGen/Evergine.Bindings.WebGPU/Evergine.Bindings.WebGPU.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
2929
</ItemGroup>
3030

31+
<ItemGroup>
32+
<Folder Include="runtimes\browser-wasm\" />
33+
</ItemGroup>
34+
3135
</Project>

WebGPUGen/Evergine.Bindings.WebGPU/Generated/Commands.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static unsafe partial class WebGPUNative
1111
public static extern WGPUInstance wgpuCreateInstance(WGPUInstanceDescriptor* descriptor);
1212

1313
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuGetProcAddress")]
14-
public static extern delegate* unmanaged<void> wgpuGetProcAddress(WGPUDevice device, char* procName);
14+
public static extern IntPtr wgpuGetProcAddress(WGPUDevice device, char* procName);
1515

1616
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuAdapterEnumerateFeatures")]
1717
public static extern uint wgpuAdapterEnumerateFeatures(WGPUAdapter adapter, WGPUFeatureName* features);
@@ -26,7 +26,7 @@ public static unsafe partial class WebGPUNative
2626
public static extern bool wgpuAdapterHasFeature(WGPUAdapter adapter, WGPUFeatureName feature);
2727

2828
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuAdapterRequestDevice")]
29-
public static extern void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPUDeviceDescriptor* descriptor, delegate* unmanaged<WGPURequestDeviceStatus, WGPUDevice, char*, void*, void> callback, void* userdata);
29+
public static extern void wgpuAdapterRequestDevice(WGPUAdapter adapter, WGPUDeviceDescriptor* descriptor, IntPtr callback, void* userdata);
3030

3131
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuAdapterReference")]
3232
public static extern void wgpuAdapterReference(WGPUAdapter adapter);
@@ -68,7 +68,7 @@ public static unsafe partial class WebGPUNative
6868
public static extern WGPUBufferUsage wgpuBufferGetUsage(WGPUBuffer buffer);
6969

7070
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuBufferMapAsync")]
71-
public static extern void wgpuBufferMapAsync(WGPUBuffer buffer, WGPUMapMode mode, uint offset, uint size, delegate* unmanaged<WGPUBufferMapAsyncStatus, void*, void> callback, void* userdata);
71+
public static extern void wgpuBufferMapAsync(WGPUBuffer buffer, WGPUMapMode mode, uint offset, uint size, IntPtr callback, void* userdata);
7272

7373
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuBufferSetLabel")]
7474
public static extern void wgpuBufferSetLabel(WGPUBuffer buffer, char* label);
@@ -209,7 +209,7 @@ public static unsafe partial class WebGPUNative
209209
public static extern WGPUComputePipeline wgpuDeviceCreateComputePipeline(WGPUDevice device, WGPUComputePipelineDescriptor* descriptor);
210210

211211
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuDeviceCreateComputePipelineAsync")]
212-
public static extern void wgpuDeviceCreateComputePipelineAsync(WGPUDevice device, WGPUComputePipelineDescriptor* descriptor, delegate* unmanaged<WGPUCreatePipelineAsyncStatus, WGPUComputePipeline, char*, void*, void> callback, void* userdata);
212+
public static extern void wgpuDeviceCreateComputePipelineAsync(WGPUDevice device, WGPUComputePipelineDescriptor* descriptor, IntPtr callback, void* userdata);
213213

214214
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuDeviceCreatePipelineLayout")]
215215
public static extern WGPUPipelineLayout wgpuDeviceCreatePipelineLayout(WGPUDevice device, WGPUPipelineLayoutDescriptor* descriptor);
@@ -224,7 +224,7 @@ public static unsafe partial class WebGPUNative
224224
public static extern WGPURenderPipeline wgpuDeviceCreateRenderPipeline(WGPUDevice device, WGPURenderPipelineDescriptor* descriptor);
225225

226226
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuDeviceCreateRenderPipelineAsync")]
227-
public static extern void wgpuDeviceCreateRenderPipelineAsync(WGPUDevice device, WGPURenderPipelineDescriptor* descriptor, delegate* unmanaged<WGPUCreatePipelineAsyncStatus, WGPURenderPipeline, char*, void*, void> callback, void* userdata);
227+
public static extern void wgpuDeviceCreateRenderPipelineAsync(WGPUDevice device, WGPURenderPipelineDescriptor* descriptor, IntPtr callback, void* userdata);
228228

229229
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuDeviceCreateSampler")]
230230
public static extern WGPUSampler wgpuDeviceCreateSampler(WGPUDevice device, WGPUSamplerDescriptor* descriptor);
@@ -257,7 +257,7 @@ public static unsafe partial class WebGPUNative
257257
public static extern void wgpuDeviceSetLabel(WGPUDevice device, char* label);
258258

259259
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuDeviceSetUncapturedErrorCallback")]
260-
public static extern void wgpuDeviceSetUncapturedErrorCallback(WGPUDevice device, delegate* unmanaged<WGPUErrorType, char*, void*, void> callback, void* userdata);
260+
public static extern void wgpuDeviceSetUncapturedErrorCallback(WGPUDevice device, IntPtr callback, void* userdata);
261261

262262
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuDeviceReference")]
263263
public static extern void wgpuDeviceReference(WGPUDevice device);
@@ -272,7 +272,7 @@ public static unsafe partial class WebGPUNative
272272
public static extern void wgpuInstanceProcessEvents(WGPUInstance instance);
273273

274274
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuInstanceRequestAdapter")]
275-
public static extern void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPURequestAdapterOptions* options, delegate* unmanaged<WGPURequestAdapterStatus, WGPUAdapter, char*, void*, void> callback, void* userdata);
275+
public static extern void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPURequestAdapterOptions* options, IntPtr callback, void* userdata);
276276

277277
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuInstanceReference")]
278278
public static extern void wgpuInstanceReference(WGPUInstance instance);
@@ -308,7 +308,7 @@ public static unsafe partial class WebGPUNative
308308
public static extern void wgpuQuerySetRelease(WGPUQuerySet querySet);
309309

310310
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuQueueOnSubmittedWorkDone")]
311-
public static extern void wgpuQueueOnSubmittedWorkDone(WGPUQueue queue, ulong signalValue, delegate* unmanaged<WGPUQueueWorkDoneStatus, void*, void> callback, void* userdata);
311+
public static extern void wgpuQueueOnSubmittedWorkDone(WGPUQueue queue, ulong signalValue, IntPtr callback, void* userdata);
312312

313313
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuQueueSetLabel")]
314314
public static extern void wgpuQueueSetLabel(WGPUQueue queue, char* label);
@@ -470,7 +470,7 @@ public static unsafe partial class WebGPUNative
470470
public static extern void wgpuSamplerRelease(WGPUSampler sampler);
471471

472472
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuShaderModuleGetCompilationInfo")]
473-
public static extern void wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, delegate* unmanaged<WGPUCompilationInfoRequestStatus, WGPUCompilationInfo*, void*, void> callback, void* userdata);
473+
public static extern void wgpuShaderModuleGetCompilationInfo(WGPUShaderModule shaderModule, IntPtr callback, void* userdata);
474474

475475
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl, EntryPoint = "wgpuShaderModuleSetLabel")]
476476
public static extern void wgpuShaderModuleSetLabel(WGPUShaderModule shaderModule, char* label);

WebGPUGen/Evergine.Bindings.WebGPU/WebGPUNative.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public static unsafe partial class WebGPUNative
99

1010
[DllImport("wgpu_native")]
1111
public static extern WGPUDevice emscripten_webgpu_get_device();
12-
13-
[DllImport("wgpu_native", CallingConvention = CallingConvention.Cdecl)]
14-
public static extern void wgpuBufferMapAsyncWithoutCallback(WGPUBuffer buffer, WGPUMapMode mode, uint offset, uint size, void* userdata);
1512
}
1613
}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
#include "emscripten.h"
22
#include "emscripten/html5.h"
3-
#include "emscripten/html5_webgpu.h"
4-
#include <webgpu/webgpu.h>
5-
6-
void myWGPUBufferMapCallback(WGPUBufferMapAsyncStatus status, void* userdata) {
7-
*((WGPUBufferMapAsyncStatus*)userdata) = status;
8-
}
9-
10-
void wgpuBufferMapAsyncWithoutCallback(WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size, void* userdata) {
11-
wgpuBufferMapAsync(buffer, mode, offset, size, myWGPUBufferMapCallback, userdata);
12-
}
3+
#include "emscripten/html5_webgpu.h"

WebGPUGen/WebGPUGen/Helpers.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,18 @@ public static string NormalizeConstantValue(string constantValue)
299299

300300
private static string GetDelegatePointer(CppTypedef type)
301301
{
302-
CppFunctionType pointerType = ((CppPointerType)type.ElementType).ElementType as CppFunctionType;
303302
StringBuilder result = new StringBuilder();
304303

305-
result.Append("delegate* unmanaged<");
306-
for (int i = 0; i < pointerType.Parameters.Count; i++)
307-
{
308-
result.Append(ConvertToCSharpType(pointerType.Parameters[i].Type) + ", ");
309-
}
310-
result.Append(ConvertToCSharpType(pointerType.ReturnType) + ">");
304+
// WASM doesn't support function pointers yet :( ...
305+
////CppFunctionType pointerType = ((CppPointerType)type.ElementType).ElementType as CppFunctionType;
306+
////result.Append("delegate* unmanaged<");
307+
////for (int i = 0; i < pointerType.Parameters.Count; i++)
308+
////{
309+
//// result.Append(ConvertToCSharpType(pointerType.Parameters[i].Type) + ", ");
310+
////}
311+
////result.Append(ConvertToCSharpType(pointerType.ReturnType) + ">");
312+
313+
result.Append("IntPtr");
311314

312315
return result.ToString();
313316
}

0 commit comments

Comments
 (0)