Skip to content

Commit

Permalink
Added delegates, commands are using delegates now, but we need to add…
Browse files Browse the repository at this point in the history
… the parameters to them.
  • Loading branch information
jcant0n committed Jul 2, 2020
1 parent 0c5b53c commit 038f079
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 23 deletions.
40 changes: 20 additions & 20 deletions WebGPUGen/WaveEngine.Bindings.WebGPU/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static IntPtr wgpuCreateInstance(WGPUInstanceDescriptor* descriptor)
=> wgpuCreateInstance_ptr(descriptor);

[UnmanagedFunctionPointer(CallConv)]
private delegate IntPtr wgpuGetProcAddressDelegate(IntPtr device, char* procName);
private delegate WGPUProc wgpuGetProcAddressDelegate(IntPtr device, char* procName);
private static wgpuGetProcAddressDelegate wgpuGetProcAddress_ptr;
public static IntPtr wgpuGetProcAddress(IntPtr device, char* procName)
public static WGPUProc wgpuGetProcAddress(IntPtr device, char* procName)
=> wgpuGetProcAddress_ptr(device, procName);

[UnmanagedFunctionPointer(CallConv)]
Expand All @@ -28,9 +28,9 @@ public static void wgpuAdapterGetProperties(IntPtr adapter, WGPUAdapterPropertie
=> wgpuAdapterGetProperties_ptr(adapter, properties);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuAdapterRequestDeviceDelegate(IntPtr adapter, WGPUDeviceDescriptor* descriptor, IntPtr callback, void* userdata);
private delegate void wgpuAdapterRequestDeviceDelegate(IntPtr adapter, WGPUDeviceDescriptor* descriptor, WGPURequestDeviceCallback callback, void* userdata);
private static wgpuAdapterRequestDeviceDelegate wgpuAdapterRequestDevice_ptr;
public static void wgpuAdapterRequestDevice(IntPtr adapter, WGPUDeviceDescriptor* descriptor, IntPtr callback, void* userdata)
public static void wgpuAdapterRequestDevice(IntPtr adapter, WGPUDeviceDescriptor* descriptor, WGPURequestDeviceCallback callback, void* userdata)
=> wgpuAdapterRequestDevice_ptr(adapter, descriptor, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand All @@ -52,15 +52,15 @@ public static void wgpuBufferDestroy(IntPtr buffer)
=> wgpuBufferGetMappedRange_ptr(buffer);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuBufferMapReadAsyncDelegate(IntPtr buffer, IntPtr callback, void* userdata);
private delegate void wgpuBufferMapReadAsyncDelegate(IntPtr buffer, WGPUBufferMapReadCallback callback, void* userdata);
private static wgpuBufferMapReadAsyncDelegate wgpuBufferMapReadAsync_ptr;
public static void wgpuBufferMapReadAsync(IntPtr buffer, IntPtr callback, void* userdata)
public static void wgpuBufferMapReadAsync(IntPtr buffer, WGPUBufferMapReadCallback callback, void* userdata)
=> wgpuBufferMapReadAsync_ptr(buffer, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuBufferMapWriteAsyncDelegate(IntPtr buffer, IntPtr callback, void* userdata);
private delegate void wgpuBufferMapWriteAsyncDelegate(IntPtr buffer, WGPUBufferMapWriteCallback callback, void* userdata);
private static wgpuBufferMapWriteAsyncDelegate wgpuBufferMapWriteAsync_ptr;
public static void wgpuBufferMapWriteAsync(IntPtr buffer, IntPtr callback, void* userdata)
public static void wgpuBufferMapWriteAsync(IntPtr buffer, WGPUBufferMapWriteCallback callback, void* userdata)
=> wgpuBufferMapWriteAsync_ptr(buffer, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand Down Expand Up @@ -274,9 +274,9 @@ public static IntPtr wgpuDeviceGetDefaultQueue(IntPtr device)
=> wgpuDeviceGetDefaultQueue_ptr(device);

[UnmanagedFunctionPointer(CallConv)]
private delegate bool wgpuDevicePopErrorScopeDelegate(IntPtr device, IntPtr callback, void* userdata);
private delegate bool wgpuDevicePopErrorScopeDelegate(IntPtr device, WGPUErrorCallback callback, void* userdata);
private static wgpuDevicePopErrorScopeDelegate wgpuDevicePopErrorScope_ptr;
public static bool wgpuDevicePopErrorScope(IntPtr device, IntPtr callback, void* userdata)
public static bool wgpuDevicePopErrorScope(IntPtr device, WGPUErrorCallback callback, void* userdata)
=> wgpuDevicePopErrorScope_ptr(device, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand All @@ -286,15 +286,15 @@ public static void wgpuDevicePushErrorScope(IntPtr device, WGPUErrorFilter filte
=> wgpuDevicePushErrorScope_ptr(device, filter);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuDeviceSetDeviceLostCallbackDelegate(IntPtr device, IntPtr callback, void* userdata);
private delegate void wgpuDeviceSetDeviceLostCallbackDelegate(IntPtr device, WGPUDeviceLostCallback callback, void* userdata);
private static wgpuDeviceSetDeviceLostCallbackDelegate wgpuDeviceSetDeviceLostCallback_ptr;
public static void wgpuDeviceSetDeviceLostCallback(IntPtr device, IntPtr callback, void* userdata)
public static void wgpuDeviceSetDeviceLostCallback(IntPtr device, WGPUDeviceLostCallback callback, void* userdata)
=> wgpuDeviceSetDeviceLostCallback_ptr(device, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuDeviceSetUncapturedErrorCallbackDelegate(IntPtr device, IntPtr callback, void* userdata);
private delegate void wgpuDeviceSetUncapturedErrorCallbackDelegate(IntPtr device, WGPUErrorCallback callback, void* userdata);
private static wgpuDeviceSetUncapturedErrorCallbackDelegate wgpuDeviceSetUncapturedErrorCallback_ptr;
public static void wgpuDeviceSetUncapturedErrorCallback(IntPtr device, IntPtr callback, void* userdata)
public static void wgpuDeviceSetUncapturedErrorCallback(IntPtr device, WGPUErrorCallback callback, void* userdata)
=> wgpuDeviceSetUncapturedErrorCallback_ptr(device, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand All @@ -304,9 +304,9 @@ public static ulong wgpuFenceGetCompletedValue(IntPtr fence)
=> wgpuFenceGetCompletedValue_ptr(fence);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuFenceOnCompletionDelegate(IntPtr fence, ulong value, IntPtr callback, void* userdata);
private delegate void wgpuFenceOnCompletionDelegate(IntPtr fence, ulong value, WGPUFenceOnCompletionCallback callback, void* userdata);
private static wgpuFenceOnCompletionDelegate wgpuFenceOnCompletion_ptr;
public static void wgpuFenceOnCompletion(IntPtr fence, ulong value, IntPtr callback, void* userdata)
public static void wgpuFenceOnCompletion(IntPtr fence, ulong value, WGPUFenceOnCompletionCallback callback, void* userdata)
=> wgpuFenceOnCompletion_ptr(fence, value, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand All @@ -322,9 +322,9 @@ public static void wgpuInstanceProcessEvents(IntPtr instance)
=> wgpuInstanceProcessEvents_ptr(instance);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuInstanceRequestAdapterDelegate(IntPtr instance, WGPURequestAdapterOptions* options, IntPtr callback, void* userdata);
private delegate void wgpuInstanceRequestAdapterDelegate(IntPtr instance, WGPURequestAdapterOptions* options, WGPURequestAdapterCallback callback, void* userdata);
private static wgpuInstanceRequestAdapterDelegate wgpuInstanceRequestAdapter_ptr;
public static void wgpuInstanceRequestAdapter(IntPtr instance, WGPURequestAdapterOptions* options, IntPtr callback, void* userdata)
public static void wgpuInstanceRequestAdapter(IntPtr instance, WGPURequestAdapterOptions* options, WGPURequestAdapterCallback callback, void* userdata)
=> wgpuInstanceRequestAdapter_ptr(instance, options, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand Down Expand Up @@ -556,9 +556,9 @@ public static IntPtr wgpuRenderPipelineGetBindGroupLayout(IntPtr renderPipeline,
=> wgpuRenderPipelineGetBindGroupLayout_ptr(renderPipeline, groupIndex);

[UnmanagedFunctionPointer(CallConv)]
private delegate void wgpuSurfaceGetPreferredFormatDelegate(IntPtr surface, IntPtr adapter, IntPtr callback, void* userdata);
private delegate void wgpuSurfaceGetPreferredFormatDelegate(IntPtr surface, IntPtr adapter, WGPUSurfaceGetPreferredFormatCallback callback, void* userdata);
private static wgpuSurfaceGetPreferredFormatDelegate wgpuSurfaceGetPreferredFormat_ptr;
public static void wgpuSurfaceGetPreferredFormat(IntPtr surface, IntPtr adapter, IntPtr callback, void* userdata)
public static void wgpuSurfaceGetPreferredFormat(IntPtr surface, IntPtr adapter, WGPUSurfaceGetPreferredFormatCallback callback, void* userdata)
=> wgpuSurfaceGetPreferredFormat_ptr(surface, adapter, callback, userdata);

[UnmanagedFunctionPointer(CallConv)]
Expand Down
23 changes: 23 additions & 0 deletions WebGPUGen/WaveEngine.Bindings.WebGPU/Delegates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace WaveEngine.Bindings.WebGPU
{
public unsafe delegate void WGPUBufferMapReadCallback();

public unsafe delegate void WGPUBufferMapWriteCallback();

public unsafe delegate void WGPUDeviceLostCallback();

public unsafe delegate void WGPUErrorCallback();

public unsafe delegate void WGPUFenceOnCompletionCallback();

public unsafe delegate void WGPURequestAdapterCallback();

public unsafe delegate void WGPURequestDeviceCallback();

public unsafe delegate void WGPUSurfaceGetPreferredFormatCallback();

public unsafe delegate void WGPUProc();

}
38 changes: 36 additions & 2 deletions WebGPUGen/WebGPUGen/CsCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,47 @@ private CsCodeGenerator()

public void Generate(CppCompilation compilation, string outputPath)
{
Helpers.TypedefList = compilation.Typedefs.Where(t => t.TypeKind == CppTypeKind.Typedef).Select(t => t.Name).ToList();
Helpers.TypedefList = compilation.Typedefs
.Where(t => t.TypeKind == CppTypeKind.Typedef
&& t.ElementType is CppPointerType
&& ((CppPointerType)t.ElementType).ElementType.TypeKind != CppTypeKind.Function)
.Select(t => t.Name).ToList();

GenerateDelegates(compilation, outputPath);
GenerateEnums(compilation, outputPath);
GenerateStructs(compilation, outputPath);
GenerateCommmands(compilation, outputPath);
}

private void GenerateDelegates(CppCompilation compilation, string outputPath)
{
Debug.WriteLine("Generating Delegates...");

var delegates = compilation.Typedefs
.Where(t => t.TypeKind == CppTypeKind.Typedef
&& t.ElementType is CppPointerType
&& ((CppPointerType)t.ElementType).ElementType.TypeKind == CppTypeKind.Function)
.ToList();

using (StreamWriter file = File.CreateText(Path.Combine(outputPath, "Delegates.cs")))
{
file.WriteLine("using System;\n");
file.WriteLine("namespace WaveEngine.Bindings.WebGPU");
file.WriteLine("{");

foreach (var funcPointer in delegates)
{
CppFunctionType pointerType = ((CppPointerType)funcPointer.ElementType).ElementType as CppFunctionType;

file.Write($"\tpublic unsafe delegate {pointerType.ReturnType} {funcPointer.Name}(");

file.Write(");\n\n");
}

file.WriteLine("}");
}
}

private void GenerateCommmands(CppCompilation compilation, string outputPath)
{
Debug.WriteLine("Generating Commands...");
Expand Down Expand Up @@ -96,7 +130,7 @@ private void GenerateStructs(CppCompilation compilation, string outputPath)
file.WriteLine("{");

var structs = compilation.Classes.Where(c => c.ClassKind == CppClassKind.Struct && c.IsDefinition == true);

foreach (var structure in structs)
{
file.WriteLine("\t[StructLayout(LayoutKind.Sequential)]");
Expand Down
3 changes: 2 additions & 1 deletion WebGPUGen/WebGPUGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ static void Main(string[] args)
Defines =
{
"WGPU_SHARED_LIBRARY",
"_WIN32"
"_WIN32",
"WGPU_SKIP_PROCS"
}
};

Expand Down

0 comments on commit 038f079

Please sign in to comment.