Skip to content

Commit dc3b1a9

Browse files
authored
Force passing an Instance* into surface creation (#1320)
1 parent ab0016b commit dc3b1a9

File tree

6 files changed

+46
-54
lines changed

6 files changed

+46
-54
lines changed

generator.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,11 +2204,11 @@
22042204
"namespace": "Silk.NET.WebGPU",
22052205
"extensionsNamespace": "Silk.NET.WebGPU.Extensions",
22062206
"nameContainer": {
2207-
"linux-x64": "libwgpu.so",
2208-
"win-x64": "libwgpu.dll",
2209-
"win-x86": "libwgpu.dll",
2210-
"osx-x64": "libwgpu.dylib",
2211-
"android": "libwgpu.so",
2207+
"linux-x64": "libwgpu_native.so",
2208+
"win-x64": "libwgpu_native.dll",
2209+
"win-x86": "libwgpu_native.dll",
2210+
"osx-x64": "libwgpu_native.dylib",
2211+
"android": "libwgpu_native.so",
22122212
"className": "WebGPULibraryNameContainer"
22132213
},
22142214
"typeMaps": [

src/Lab/Experiments/WebGPUTest/Program.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace WebGPUTest;
1010

1111
public static unsafe class Program
1212
{
13+
public static Instance* instance;
1314
public static Adapter* adapter;
1415
public static Device* device;
1516
public static void Main(string[] args)
@@ -22,25 +23,13 @@ public static void Main(string[] args)
2223

2324
wgpu.TryGetDeviceExtension(null, out Wgpu wgpuSpecific);
2425

25-
var requestAdapterOptions = new RequestAdapterOptions();
26-
wgpu.InstanceRequestAdapter(null, &requestAdapterOptions, new PfnRequestAdapterCallback(RequestAdapterCallback), null);
26+
InstanceDescriptor instanceDescriptor = new InstanceDescriptor();
27+
instance = wgpu.CreateInstance(&instanceDescriptor);
2728

28-
var deviceDescriptor = new DeviceDescriptor
29-
{
30-
Label = (byte*) SilkMarshal.StringToPtr("Device"), //TODO: free this
31-
DefaultQueue = new QueueDescriptor(),
32-
};
33-
var requiredLimits = stackalloc RequiredLimits[1];
34-
requiredLimits[0] = new RequiredLimits
35-
{
36-
Limits = new Limits
37-
{
38-
MaxBindGroups = 1
39-
}
40-
};
41-
deviceDescriptor.RequiredLimits = requiredLimits;
29+
var requestAdapterOptions = new RequestAdapterOptions();
30+
wgpu.InstanceRequestAdapter(instance, &requestAdapterOptions, new PfnRequestAdapterCallback(RequestAdapterCallback), null);
4231

43-
wgpu.AdapterRequestDevice(adapter, &deviceDescriptor, new PfnRequestDeviceCallback(RequestDeviceCallback), null);
32+
wgpu.AdapterRequestDevice(adapter, null, new PfnRequestDeviceCallback(RequestDeviceCallback), null);
4433

4534
SetErrorCallback(wgpu);
4635

@@ -184,7 +173,10 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
184173
wgpu.BufferMapAsync(stagingBuffer, MapMode.Read, 0, numbersSize, new PfnBufferMapCallback(
185174
(arg0, data) =>
186175
{
187-
Console.WriteLine($"status: {arg0}");
176+
if(arg0 != BufferMapAsyncStatus.Success)
177+
{
178+
throw new Exception($"Unable to map buffer! status: {arg0}");
179+
}
188180

189181
var times = (uint*) wgpu.BufferGetMappedRange(stagingBuffer, 0, numbersSize); ;
190182

@@ -202,11 +194,20 @@ private static void ReadBufferMap(BufferMapAsyncStatus arg0, void* arg1)
202194

203195
private static void RequestDeviceCallback(RequestDeviceStatus arg0, Device* received, byte* arg2, void* arg3)
204196
{
197+
if(arg0 != RequestDeviceStatus.Success)
198+
{
199+
throw new Exception($"Unable to get WebGPU Device! status: {arg0} message: {SilkMarshal.PtrToString((nint)arg2)}");
200+
}
205201
device = received;
206202
}
207203

208204
private static unsafe void RequestAdapterCallback(RequestAdapterStatus arg0, Adapter* received, byte* arg2, void* userdata)
209205
{
206+
if(arg0 != RequestAdapterStatus.Success)
207+
{
208+
throw new Exception($"Unable to get WebGPU Adapter! status: {arg0} message: {SilkMarshal.PtrToString((nint)arg2)}");
209+
}
210+
210211
adapter = received;
211212
}
212213

src/Lab/Experiments/WebGPUTexturedQuad/Program.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public Vertex(Vector2 position, Vector2 texCoord)
3232
private static WebGPUDisposal _WebGpuDisposal = null!;
3333
private static IWindow? _Window;
3434

35+
private static Instance* _Instance;
3536
private static Surface* _Surface;
3637
private static Adapter* _Adapter;
3738
private static Device* _Device;
@@ -87,7 +88,10 @@ private static void WindowOnLoad()
8788

8889
_WebGpuDisposal = new WebGPUDisposal(wgpu);
8990

90-
_Surface = _Window.CreateWebGPUSurface(wgpu);
91+
InstanceDescriptor instanceDescriptor = new InstanceDescriptor();
92+
_Instance = wgpu.CreateInstance(instanceDescriptor);
93+
94+
_Surface = _Window.CreateWebGPUSurface(wgpu, _Instance);
9195

9296
{ //Get adapter
9397
var requestAdapterOptions = new RequestAdapterOptions
@@ -97,7 +101,7 @@ private static void WindowOnLoad()
97101

98102
wgpu.InstanceRequestAdapter
99103
(
100-
null,
104+
_Instance,
101105
requestAdapterOptions,
102106
new PfnRequestAdapterCallback((_, adapter1, _, _) => _Adapter = adapter1),
103107
null
@@ -114,19 +118,10 @@ private static void WindowOnLoad()
114118
PrintAdapterFeatures();
115119

116120
{ //Get device
117-
var requiredLimits = stackalloc RequiredLimits[1];
118-
requiredLimits->Limits.MaxBindGroups = 2;
119-
120-
var deviceDescriptor = new DeviceDescriptor
121-
{
122-
RequiredLimits = requiredLimits,
123-
DefaultQueue = new QueueDescriptor()
124-
};
125-
126121
wgpu.AdapterRequestDevice
127122
(
128123
_Adapter,
129-
deviceDescriptor,
124+
null,
130125
new PfnRequestDeviceCallback((_, device1, _, _) => _Device = device1),
131126
null
132127
);
@@ -364,7 +359,8 @@ private static void WindowOnLoad()
364359
var bindGroupEntry = new BindGroupEntry
365360
{
366361
Binding = 0,
367-
Buffer = _ProjectionMatrixBuffer
362+
Buffer = _ProjectionMatrixBuffer,
363+
Size = (ulong) sizeof(Matrix4x4)
368364
};
369365

370366
_ProjectionMatrixBindGroup = wgpu.DeviceCreateBindGroup

src/Lab/Experiments/WebGPUTriangle/Program.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static unsafe class Program
1818
private static WebGPUDisposal _WebGpuDisposal = null!;
1919
private static IWindow? _Window;
2020

21+
private static Instance* _Instance;
2122
private static Surface* _Surface;
2223
private static Adapter* _Adapter;
2324
private static Device* _Device;
@@ -74,7 +75,10 @@ private static void WindowOnLoad()
7475

7576
_WebGpuDisposal = new WebGPUDisposal(wgpu);
7677

77-
_Surface = _Window.CreateWebGPUSurface(wgpu);
78+
InstanceDescriptor instanceDescriptor = new InstanceDescriptor();
79+
_Instance = wgpu.CreateInstance(&instanceDescriptor);
80+
81+
_Surface = _Window.CreateWebGPUSurface(wgpu, _Instance);
7882

7983
{ //Get adapter
8084
var requestAdapterOptions = new RequestAdapterOptions
@@ -84,7 +88,7 @@ private static void WindowOnLoad()
8488

8589
wgpu.InstanceRequestAdapter
8690
(
87-
null,
91+
_Instance,
8892
requestAdapterOptions,
8993
new PfnRequestAdapterCallback((_, adapter1, _, _) => _Adapter = adapter1),
9094
null
@@ -96,19 +100,10 @@ private static void WindowOnLoad()
96100
PrintAdapterFeatures();
97101

98102
{ //Get device
99-
var requiredLimits = stackalloc RequiredLimits[1];
100-
requiredLimits->Limits.MaxBindGroups = 1;
101-
102-
var deviceDescriptor = new DeviceDescriptor
103-
{
104-
RequiredLimits = requiredLimits,
105-
DefaultQueue = new QueueDescriptor()
106-
};
107-
108103
wgpu.AdapterRequestDevice
109104
(
110105
_Adapter,
111-
deviceDescriptor,
106+
null,
112107
new PfnRequestDeviceCallback((_, device1, _, _) => _Device = device1),
113108
null
114109
);

src/WebGPU/Silk.NET.WebGPU/WebGPULibraryNameContainer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ namespace Silk.NET.WebGPU
1111
internal class WebGPULibraryNameContainer : SearchPathContainer
1212
{
1313
/// <inheritdoc />
14-
public override string Linux => "libwgpu.so";
14+
public override string Linux => "libwgpu_native.so";
1515

1616
/// <inheritdoc />
17-
public override string MacOS => "libwgpu.dylib";
17+
public override string MacOS => "libwgpu_native.dylib";
1818

1919
/// <inheritdoc />
20-
public override string Android => "libwgpu.so";
20+
public override string Android => "libwgpu_native.so";
2121

2222
/// <inheritdoc />
2323
public override string IOS => "";
2424

2525
/// <inheritdoc />
26-
public override string Windows64 => "libwgpu.dll";
26+
public override string Windows64 => "libwgpu_native.dll";
2727

2828
/// <inheritdoc />
29-
public override string Windows86 => "libwgpu.dll";
29+
public override string Windows86 => "libwgpu_native.dll";
3030
}
3131
}

src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class WebGPUSurface
2020
/// <param name="view">The window to get the surface from</param>
2121
/// <returns>A newly created Surface*/</returns>
2222
/// <exception cref="NotSupportedException">Throws when an unsupported platform is detected.</exception>
23-
public static unsafe Surface* CreateWebGPUSurface(this INativeWindowSource view, NET.WebGPU.WebGPU wgpu, Instance* instance = null)
23+
public static unsafe Surface* CreateWebGPUSurface(this INativeWindowSource view, NET.WebGPU.WebGPU wgpu, Instance* instance)
2424
{
2525
var descriptor = new SurfaceDescriptor();
2626

0 commit comments

Comments
 (0)