Skip to content

Commit fa9b54d

Browse files
committed
Update samples according to webgpu specs + use SDL3 for windowing.
1 parent 29cf536 commit fa9b54d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+865
-1046
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ csharp_space_between_square_brackets = false
157157
csharp_style_namespace_declarations=file_scoped:error
158158

159159
# License header
160-
file_header_template = Copyright © Amer Koleci and Contributors.\nLicensed under the MIT License (MIT). See LICENSE in the repository root for more information.
160+
file_header_template = Copyright (c) Amer Koleci and Contributors.\nLicensed under the MIT License (MIT). See LICENSE in the repository root for more information.
161161

162162
# C++ Files
163163
[*.{cpp,h,in}]

Alimer.Bindings.WebGPU.Native.targets

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
<None Include="$(RepoRootPath)src/Alimer.Bindings.WebGPU/runtimes/linux-x64/native/libwgpu_native.so" Link="libwgpu_native.so" Visible="False" CopyToOutputDirectory="PreserveNewest" />
1616
</ItemGroup>
1717
</When>
18+
<When Condition="($([MSBuild]::IsOsPlatform('linux')) and '$(RuntimeIdentifier)'=='') or '$(RuntimeIdentifier)'=='linux-arm64'">
19+
<ItemGroup>
20+
<None Include="$(RepoRootPath)src/Alimer.Bindings.WebGPU/runtimes/linux-arm64/native/libwgpu_native.so" Link="libwgpu_native.so" Visible="False" CopyToOutputDirectory="PreserveNewest" />
21+
</ItemGroup>
22+
</When>
1823
<When Condition="($([MSBuild]::IsOsPlatform('osx')) and '$(RuntimeIdentifier)'=='') or '$(RuntimeIdentifier)'=='osx-x64'">
1924
<ItemGroup>
2025
<None Include="$(RepoRootPath)src/Alimer.Bindings.WebGPU/runtimes/osx-x64/native/libwgpu_native.dylib" Link="libwgpu_native.dylib" Visible="False" CopyToOutputDirectory="PreserveNewest" />

Directory.Build.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<PropertyGroup>
13-
<VersionPrefix>1.3.1</VersionPrefix>
13+
<VersionPrefix>1.3.2</VersionPrefix>
1414
<VersionSuffix Condition="'$(VersionSuffix)' == ''"></VersionSuffix>
1515
</PropertyGroup>
1616

@@ -32,7 +32,7 @@
3232
</PropertyGroup>
3333

3434
<PropertyGroup>
35-
<Authors>Amer Koleci and Contributors</Authors>
35+
<Authors>Amer Koleci</Authors>
3636
<Copyright>Copyright (c) Amer Koleci and Contributors</Copyright>
3737
<Product>Alimer.Bindings.WebGPU</Product>
3838
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Directory.Packages.props

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
10+
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
15-
<PackageVersion Include="NUnit" Version="3.13.3" />
16-
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
17-
14+
<!-- Generator -->
1815
<PackageVersion Include="CppAst" Version="0.14.0" />
19-
<PackageVersion Include="Ultz.Native.GLFW" Version="3.3.9.1" />
16+
17+
<!-- Samples -->
18+
<PackageVersion Include="Alimer.Bindings.SDL" Version="3.4.2" />
19+
20+
<!-- Tests -->
21+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
22+
<PackageVersion Include="NUnit" Version="4.0.1" />
23+
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
2024
</ItemGroup>
2125

2226
</Project>

samples/01-ClearScreen/01-ClearScreen.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<IsPackable>false</IsPackable>
7-
<RootNamespace>ClearScreen</RootNamespace>
7+
<RootNamespace>Alimer.WebGPU.Samples</RootNamespace>
88
</PropertyGroup>
99

1010
<Import Project="$(RepoRootPath)Alimer.Bindings.WebGPU.Native.targets" />

samples/01-ClearScreen/Program.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright © Amer Koleci and Contributors.
1+
// Copyright (c) Amer Koleci and Contributors.
22
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
33

4-
using Alimer.WebGPU.SampleFramework;
54
using WebGPU;
65
using static WebGPU.WebGPU;
76

8-
namespace ClearScreen;
7+
namespace Alimer.WebGPU.Samples;
98

109
public static unsafe class Program
1110
{
@@ -38,18 +37,19 @@ protected override void OnTick()
3837
_graphicsDevice!.RenderFrame(OnDraw);
3938
}
4039

41-
private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTextureView)
40+
private void OnDraw(WGPUCommandEncoder encoder, WGPUTexture target)
4241
{
4342
float g = _green + 0.001f;
4443
if (g > 1.0f)
4544
g = 0.0f;
4645
_green = g;
4746

47+
WGPUTextureView targetView = wgpuTextureCreateView(target, null);
4848

4949
WGPURenderPassColorAttachment renderPassColorAttachment = new();
5050
// The attachment is tighed to the view returned by the swap chain, so that
5151
// the render pass draws directly on screen.
52-
renderPassColorAttachment.view = swapChainTextureView;
52+
renderPassColorAttachment.view = targetView;
5353
// Not relevant here because we do not use multi-sampling
5454
renderPassColorAttachment.resolveTarget = WGPUTextureView.Null;
5555
renderPassColorAttachment.loadOp = WGPULoadOp.Clear;
@@ -66,7 +66,6 @@ private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTexture
6666
depthStencilAttachment = null,
6767

6868
// We do not use timers for now neither
69-
timestampWriteCount = 0,
7069
timestampWrites = null
7170
};
7271

@@ -75,6 +74,8 @@ private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTexture
7574
WGPURenderPassEncoder renderPass = wgpuCommandEncoderBeginRenderPass(encoder, &renderPassDesc);
7675

7776
wgpuRenderPassEncoderEnd(renderPass);
77+
78+
wgpuTextureViewReference(targetView);
7879
}
7980
}
8081
}

samples/02-DrawTriangle/02-DrawTriangle.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<IsPackable>false</IsPackable>
7-
<RootNamespace>DrawTriangle</RootNamespace>
7+
<RootNamespace>Alimer.WebGPU.Samples</RootNamespace>
88
</PropertyGroup>
99

1010
<Import Project="$(RepoRootPath)Alimer.Bindings.WebGPU.Native.targets" />

samples/02-DrawTriangle/Program.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// Copyright © Amer Koleci and Contributors.
1+
// Copyright (c) Amer Koleci and Contributors.
22
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
33

44
using System.Numerics;
5-
using Alimer.WebGPU.SampleFramework;
65
using WebGPU;
76
using static WebGPU.WebGPU;
87

9-
namespace DrawTriangle;
10-
8+
namespace Alimer.WebGPU.Samples;
119
public static unsafe class Program
1210
{
1311
public static void Main()
@@ -135,11 +133,11 @@ protected override void Initialize()
135133

136134
wgpuShaderModuleRelease(shaderModule);
137135

138-
ReadOnlySpan<VertexPositionColor> vertexData = stackalloc VertexPositionColor[] {
136+
ReadOnlySpan<VertexPositionColor> vertexData = [
139137
new(new Vector3(0.0f, 0.5f, 0.5f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f)),
140138
new(new Vector3(0.5f, -0.5f, 0.5f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f)),
141139
new(new Vector3(-0.5f, -0.5f, 0.5f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)),
142-
};
140+
];
143141
_vertexBuffer = wgpuDeviceCreateBuffer(_graphicsDevice.Device, WGPUBufferUsage.Vertex | WGPUBufferUsage.CopyDst, vertexData.Length * VertexPositionColor.SizeInBytes);
144142
wgpuQueueWriteBuffer(_graphicsDevice.Queue, _vertexBuffer, vertexData);
145143
}
@@ -161,12 +159,14 @@ protected override void OnTick()
161159
_graphicsDevice!.RenderFrame(OnDraw);
162160
}
163161

164-
private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTextureView)
162+
private void OnDraw(WGPUCommandEncoder encoder, WGPUTexture target)
165163
{
164+
WGPUTextureView targetView = wgpuTextureCreateView(target, null);
165+
166166
WGPURenderPassColorAttachment renderPassColorAttachment = new();
167167
// The attachment is tighed to the view returned by the swap chain, so that
168168
// the render pass draws directly on screen.
169-
renderPassColorAttachment.view = swapChainTextureView;
169+
renderPassColorAttachment.view = targetView;
170170
// Not relevant here because we do not use multi-sampling
171171
renderPassColorAttachment.resolveTarget = WGPUTextureView.Null;
172172
renderPassColorAttachment.loadOp = WGPULoadOp.Clear;
@@ -183,7 +183,6 @@ private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTexture
183183
depthStencilAttachment = null,
184184

185185
// We do not use timers for now neither
186-
timestampWriteCount = 0,
187186
timestampWrites = null
188187
};
189188

@@ -197,6 +196,8 @@ private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTexture
197196
wgpuRenderPassEncoderDraw(renderPass, 3);
198197

199198
wgpuRenderPassEncoderEnd(renderPass);
199+
200+
wgpuTextureViewReference(targetView);
200201
}
201202
}
202203
}

samples/03-DrawIndexedQuad/03-DrawIndexedQuad.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<IsPackable>false</IsPackable>
7-
<RootNamespace>DrawTriangle</RootNamespace>
7+
<RootNamespace>Alimer.WebGPU.Samples</RootNamespace>
88
</PropertyGroup>
99

1010
<Import Project="$(RepoRootPath)Alimer.Bindings.WebGPU.Native.targets" />

samples/03-DrawIndexedQuad/Program.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// Copyright © Amer Koleci and Contributors.
1+
// Copyright (c) Amer Koleci and Contributors.
22
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
33

44
using System.Numerics;
5-
using Alimer.WebGPU.SampleFramework;
65
using WebGPU;
76
using static WebGPU.WebGPU;
87

9-
namespace DrawTriangle;
8+
namespace Alimer.WebGPU.Samples;
109

1110
public static unsafe class Program
1211
{
@@ -136,12 +135,12 @@ protected override void Initialize()
136135

137136
wgpuShaderModuleRelease(shaderModule);
138137

139-
ReadOnlySpan<VertexPositionColor> vertexData = stackalloc VertexPositionColor[] {
138+
ReadOnlySpan<VertexPositionColor> vertexData = [
140139
new(new Vector3(-0.5f, 0.5f, 0.5f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f)),
141140
new(new Vector3(0.5f, 0.5f, 0.5f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f)),
142141
new(new Vector3(0.5f, -0.5f, 0.5f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)),
143142
new(new Vector3(-0.5f, -0.5f, 0.5f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f))
144-
};
143+
];
145144
_vertexBuffer = wgpuDeviceCreateBuffer(_graphicsDevice.Device, WGPUBufferUsage.Vertex | WGPUBufferUsage.CopyDst, vertexData.Length * VertexPositionColor.SizeInBytes);
146145
wgpuQueueWriteBuffer(_graphicsDevice.Queue, _vertexBuffer, vertexData);
147146

@@ -177,12 +176,14 @@ protected override void OnTick()
177176
_graphicsDevice!.RenderFrame(OnDraw);
178177
}
179178

180-
private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTextureView)
179+
private void OnDraw(WGPUCommandEncoder encoder, WGPUTexture target)
181180
{
181+
WGPUTextureView targetView = wgpuTextureCreateView(target, null);
182+
182183
WGPURenderPassColorAttachment renderPassColorAttachment = new();
183184
// The attachment is tighed to the view returned by the swap chain, so that
184185
// the render pass draws directly on screen.
185-
renderPassColorAttachment.view = swapChainTextureView;
186+
renderPassColorAttachment.view = targetView;
186187
// Not relevant here because we do not use multi-sampling
187188
renderPassColorAttachment.resolveTarget = WGPUTextureView.Null;
188189
renderPassColorAttachment.loadOp = WGPULoadOp.Clear;
@@ -199,7 +200,6 @@ private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTexture
199200
depthStencilAttachment = null,
200201

201202
// We do not use timers for now neither
202-
timestampWriteCount = 0,
203203
timestampWrites = null
204204
};
205205

@@ -214,6 +214,7 @@ private void OnDraw(WGPUCommandEncoder encoder, WGPUTextureView swapChainTexture
214214
wgpuRenderPassEncoderDrawIndexed(renderPass, 6);
215215

216216
wgpuRenderPassEncoderEnd(renderPass);
217+
wgpuTextureViewReference(targetView);
217218
}
218219
}
219220
}

samples/Alimer.WebGPU.SampleFramework/Alimer.WebGPU.SampleFramework.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
55
<IsPackable>false</IsPackable>
6-
<RootNamespace>Alimer.WebGPU.SampleFramework</RootNamespace>
6+
<RootNamespace>Alimer.WebGPU.Samples</RootNamespace>
77
</PropertyGroup>
88

99
<ItemGroup>
1010
<ProjectReference Include="..\..\src\Alimer.Bindings.WebGPU\Alimer.Bindings.WebGPU.csproj" />
11-
<PackageReference Include="Ultz.Native.GLFW" />
11+
12+
<PackageReference Include="Alimer.Bindings.SDL" />
1213
</ItemGroup>
1314
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
// Copyright © Amer Koleci and Contributors.
1+
// Copyright (c) Amer Koleci and Contributors.
22
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
33

4-
using System.Runtime.InteropServices;
5-
using static Alimer.WebGPU.SampleFramework.GLFW;
4+
using SDL;
5+
using static SDL.SDL;
66

7-
namespace Alimer.WebGPU.SampleFramework;
7+
namespace Alimer.WebGPU.Samples;
88

99
public abstract class Application : IDisposable
1010
{
1111
private bool _closeRequested = false;
1212

1313
protected unsafe Application()
1414
{
15-
if (!glfwInit())
15+
if (SDL_Init(SDL_InitFlags.Video) != 0)
1616
{
17-
throw new PlatformNotSupportedException("GLFW is not supported");
17+
var error = SDL_GetErrorString();
18+
throw new Exception($"Failed to start SDL2: {error}");
1819
}
1920

20-
glfwSetErrorCallback(&GlfwError);
21-
22-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
23-
{
24-
glfwInitHint(InitHintBool.CocoaChDirResources, false);
25-
}
26-
27-
glfwWindowHint((int)WindowHintClientApi.ClientApi, 0);
21+
SDL_LogSetOutputFunction(Log_SDL);
2822

2923
// Create main window.
3024
MainWindow = new Window(Name, 1280, 720);
@@ -47,15 +41,35 @@ protected virtual void OnTick()
4741
{
4842
}
4943

50-
public void Run()
44+
public unsafe void Run()
5145
{
5246
Initialize();
47+
MainWindow.Show();
48+
49+
bool running = true;
5350

54-
while (!MainWindow.ShoudClose &&
55-
!_closeRequested)
51+
while (running && !_closeRequested)
5652
{
53+
SDL_Event evt;
54+
while (SDL_PollEvent(&evt))
55+
{
56+
if (evt.type == SDL_EventType.Quit)
57+
{
58+
running = false;
59+
break;
60+
}
61+
62+
if (evt.type == SDL_EventType.WindowCloseRequested && evt.window.windowID == MainWindow.Id)
63+
{
64+
running = false;
65+
break;
66+
}
67+
}
68+
69+
if (!running)
70+
break;
71+
5772
OnTick();
58-
glfwPollEvents();
5973
}
6074
}
6175

@@ -64,9 +78,16 @@ protected virtual void OnDraw(int width, int height)
6478

6579
}
6680

67-
[UnmanagedCallersOnly]
68-
private static unsafe void GlfwError(int code, sbyte* message)
81+
//[UnmanagedCallersOnly]
82+
private static void Log_SDL(SDL_LogCategory category, SDL_LogPriority priority, string description)
6983
{
70-
throw new Exception(new string(message));
84+
if (priority >= SDL_LogPriority.Error)
85+
{
86+
Log.Error($"[{priority}] SDL: {description}");
87+
}
88+
else
89+
{
90+
Log.Info($"[{priority}] SDL: {description}");
91+
}
7192
}
7293
}

0 commit comments

Comments
 (0)