From 43430e73a2f44c8e89190adc6f89a4562741f6bd Mon Sep 17 00:00:00 2001 From: dnesov Date: Sun, 18 Aug 2024 20:46:32 +0200 Subject: [PATCH] Initialize WebGPU render pass, Implement ClearBackground in StandardRenderer --- TestGame/TestGame.cs | 3 +- Voile/Source/Rendering/StandardRenderer.cs | 74 ++++++++++++++++++---- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index dace329..51163cc 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -98,7 +98,8 @@ public class TestGame : Game // _scene.BeginDraw(); // _scene.EndDraw(); _renderer.BeginFrame(); - // _renderer.ClearBackground(Color.White); + _renderer.ClearBackground(Color.Red); + _renderer.ClearBackground(Color.Blue); _renderer.EndFrame(); } } diff --git a/Voile/Source/Rendering/StandardRenderer.cs b/Voile/Source/Rendering/StandardRenderer.cs index d9de609..875e1b8 100644 --- a/Voile/Source/Rendering/StandardRenderer.cs +++ b/Voile/Source/Rendering/StandardRenderer.cs @@ -5,6 +5,7 @@ using Silk.NET.WebGPU; using Silk.NET.Maths; using Voile.Utils; using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; namespace Voile.Rendering { @@ -67,12 +68,14 @@ namespace Voile.Rendering _window!.DoEvents(); _window.DoUpdate(); _window.DoRender(); + + WgpuBeginFrame(); } /// public override void ClearBackground(Color color) { - throw new NotImplementedException(); + _clearColor = color; } public override void DrawText(Font font, string text, Color color) @@ -120,10 +123,7 @@ namespace Voile.Rendering } /// - public override void EndFrame() - { - - } + public override void EndFrame() => WgpuEndFrame(); /// public override void Shutdown() @@ -258,12 +258,15 @@ namespace Voile.Rendering private unsafe void ConfigureSurface() { - SurfaceConfiguration configuration = new SurfaceConfiguration(); - configuration.Device = _device; - configuration.Width = (uint)_window!.Size.X; - configuration.Height = (uint)_window.Size.Y; - configuration.Format = Silk.NET.WebGPU.TextureFormat.Bgra8Unorm; - configuration.PresentMode = PresentMode.Fifo; + SurfaceConfiguration configuration = new SurfaceConfiguration + { + Device = _device, + Width = (uint)_window!.Size.X, + Height = (uint)_window.Size.Y, + Format = Silk.NET.WebGPU.TextureFormat.Bgra8Unorm, + PresentMode = PresentMode.Immediate, + Usage = TextureUsage.RenderAttachment + }; _wgpu!.SurfaceConfigure(_surface, configuration); } @@ -280,6 +283,42 @@ namespace Voile.Rendering _logger.Info("WGPU Debug Callback Configured."); } + private unsafe void WgpuBeginFrame() + { + _queue = _wgpu.DeviceGetQueue(_device); + + _commandEncoder = _wgpu.DeviceCreateCommandEncoder(_device, null); + + _wgpu.SurfaceGetCurrentTexture(_surface, ref _surfaceTexture); + _surfaceTextureView = _wgpu.TextureCreateView(_surfaceTexture.Texture, null); + + RenderPassColorAttachment* colorAttachments = stackalloc RenderPassColorAttachment[1]; + colorAttachments[0].View = _surfaceTextureView; + colorAttachments[0].LoadOp = LoadOp.Clear; + colorAttachments[0].ClearValue = VoileColorToWebGPUColor(_clearColor); + colorAttachments[0].StoreOp = StoreOp.Store; + + _renderPassDescriptor.ColorAttachments = colorAttachments; + _renderPassDescriptor.ColorAttachmentCount = 1; + + _renderPassEncoder = _wgpu.CommandEncoderBeginRenderPass(_commandEncoder, _renderPassDescriptor); + } + + private unsafe void WgpuEndFrame() + { + _wgpu.RenderPassEncoderEnd(_renderPassEncoder); + var commandBuffer = _wgpu.CommandEncoderFinish(_commandEncoder, null); + + _wgpu.QueueSubmit(_queue, 1, &commandBuffer); + _wgpu.SurfacePresent(_surface); + + _wgpu.TextureViewRelease(_surfaceTextureView); + _wgpu.TextureRelease(_surfaceTexture.Texture); + _wgpu.RenderPassEncoderRelease(_renderPassEncoder); + _wgpu.CommandBufferRelease(commandBuffer); + _wgpu.CommandEncoderRelease(_commandEncoder); + } + private unsafe void ShutdownUnsafe() { _wgpu.DeviceDestroy(_device); @@ -288,14 +327,27 @@ namespace Voile.Rendering _wgpu.AdapterRelease(_adapter); } + private Silk.NET.WebGPU.Color VoileColorToWebGPUColor(Color color) + { + return new Silk.NET.WebGPU.Color(color.R, color.G, color.B, color.A); + } + private Vector2 _windowSize; private IWindow? _window; + private Color _clearColor = Color.Black; + private WebGPU? _wgpu; private unsafe Instance* _instance; private unsafe Surface* _surface; private unsafe Adapter* _adapter; private unsafe Device* _device; + private unsafe Queue* _queue; + private SurfaceTexture _surfaceTexture; + private unsafe TextureView* _surfaceTextureView; + private unsafe CommandEncoder* _commandEncoder; + private RenderPassDescriptor _renderPassDescriptor; + private unsafe RenderPassEncoder* _renderPassEncoder; private Logger _logger = new(nameof(StandardRenderer)); }