Initialize WebGPU render pass, Implement ClearBackground in StandardRenderer

This commit is contained in:
2024-08-18 20:46:32 +02:00
parent b40af1200a
commit 43430e73a2
2 changed files with 65 additions and 12 deletions

View File

@@ -98,7 +98,8 @@ public class TestGame : Game
// _scene.BeginDraw(); // _scene.BeginDraw();
// _scene.EndDraw(); // _scene.EndDraw();
_renderer.BeginFrame(); _renderer.BeginFrame();
// _renderer.ClearBackground(Color.White); _renderer.ClearBackground(Color.Red);
_renderer.ClearBackground(Color.Blue);
_renderer.EndFrame(); _renderer.EndFrame();
} }
} }

View File

@@ -5,6 +5,7 @@ using Silk.NET.WebGPU;
using Silk.NET.Maths; using Silk.NET.Maths;
using Voile.Utils; using Voile.Utils;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace Voile.Rendering namespace Voile.Rendering
{ {
@@ -67,12 +68,14 @@ namespace Voile.Rendering
_window!.DoEvents(); _window!.DoEvents();
_window.DoUpdate(); _window.DoUpdate();
_window.DoRender(); _window.DoRender();
WgpuBeginFrame();
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ClearBackground(Color color) public override void ClearBackground(Color color)
{ {
throw new NotImplementedException(); _clearColor = color;
} }
public override void DrawText(Font font, string text, Color color) public override void DrawText(Font font, string text, Color color)
@@ -120,10 +123,7 @@ namespace Voile.Rendering
} }
/// <inheritdoc /> /// <inheritdoc />
public override void EndFrame() public override void EndFrame() => WgpuEndFrame();
{
}
/// <inheritdoc /> /// <inheritdoc />
public override void Shutdown() public override void Shutdown()
@@ -258,12 +258,15 @@ namespace Voile.Rendering
private unsafe void ConfigureSurface() private unsafe void ConfigureSurface()
{ {
SurfaceConfiguration configuration = new SurfaceConfiguration(); SurfaceConfiguration configuration = new SurfaceConfiguration
configuration.Device = _device; {
configuration.Width = (uint)_window!.Size.X; Device = _device,
configuration.Height = (uint)_window.Size.Y; Width = (uint)_window!.Size.X,
configuration.Format = Silk.NET.WebGPU.TextureFormat.Bgra8Unorm; Height = (uint)_window.Size.Y,
configuration.PresentMode = PresentMode.Fifo; Format = Silk.NET.WebGPU.TextureFormat.Bgra8Unorm,
PresentMode = PresentMode.Immediate,
Usage = TextureUsage.RenderAttachment
};
_wgpu!.SurfaceConfigure(_surface, configuration); _wgpu!.SurfaceConfigure(_surface, configuration);
} }
@@ -280,6 +283,42 @@ namespace Voile.Rendering
_logger.Info("WGPU Debug Callback Configured."); _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() private unsafe void ShutdownUnsafe()
{ {
_wgpu.DeviceDestroy(_device); _wgpu.DeviceDestroy(_device);
@@ -288,14 +327,27 @@ namespace Voile.Rendering
_wgpu.AdapterRelease(_adapter); _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 Vector2 _windowSize;
private IWindow? _window; private IWindow? _window;
private Color _clearColor = Color.Black;
private WebGPU? _wgpu; private WebGPU? _wgpu;
private unsafe Instance* _instance; private unsafe Instance* _instance;
private unsafe Surface* _surface; private unsafe Surface* _surface;
private unsafe Adapter* _adapter; private unsafe Adapter* _adapter;
private unsafe Device* _device; 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)); private Logger _logger = new(nameof(StandardRenderer));
} }