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

@@ -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();
}
/// <inheritdoc />
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
}
/// <inheritdoc />
public override void EndFrame()
{
}
public override void EndFrame() => WgpuEndFrame();
/// <inheritdoc />
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));
}