Skip to content

Commit a48e096

Browse files
[Rendering] WIP;
1 parent c75c53d commit a48e096

File tree

16 files changed

+722
-509
lines changed

16 files changed

+722
-509
lines changed

Engine/Staple.Core/Rendering/Graphics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static void RenderGeometry(VertexBuffer vertex, IndexBuffer index,
8080

8181
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
8282

83-
RenderSystem.Submit(RenderSystem.GetViewPass(viewID), renderState, Mesh.TriangleCount(topology, indexCount), 1);
83+
RenderSystem.Submit(viewID, renderState, Mesh.TriangleCount(topology, indexCount), 1);
8484
}
8585
}
8686

Engine/Staple.Core/Rendering/Mesh/MeshCombineSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void SetupMaterial()
392392

393393
renderState.program = program;
394394

395-
RenderSystem.Submit(RenderSystem.GetViewPass(viewID), renderState, mesh.SubmeshTriangleCount(0), 1);
395+
RenderSystem.Submit(viewID, renderState, mesh.SubmeshTriangleCount(0), 1);
396396
}
397397
}
398398
}

Engine/Staple.Core/Rendering/Mesh/MeshRenderSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static void RenderMesh(Mesh mesh, Vector3 position, Quaternion rotation,
105105

106106
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
107107

108-
RenderSystem.Submit(RenderSystem.GetViewPass(viewID), renderState, Mesh.TriangleCount(mesh.MeshTopology, mesh.IndexCount), 1);
108+
RenderSystem.Submit(viewID, renderState, Mesh.TriangleCount(mesh.MeshTopology, mesh.IndexCount), 1);
109109
}
110110
}
111111

@@ -393,7 +393,7 @@ public void Submit(ushort viewID)
393393
{
394394
renderState.program = program;
395395

396-
RenderSystem.Submit(RenderSystem.GetViewPass(viewID), renderState, renderData.mesh.SubmeshTriangleCount(content.submeshIndex), 1);
396+
RenderSystem.Submit(viewID, renderState, renderData.mesh.SubmeshTriangleCount(content.submeshIndex), 1);
397397
}
398398
}
399399
}

Engine/Staple.Core/Rendering/RenderSystem/Backend/IRendererBackend.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ IShaderProgram CreateShaderVertexFragment(byte[] vertex, byte[] fragment,
4242

4343
IShaderProgram CreateShaderCompute(byte[] compute, ComputeShaderMetrics computeMetrics);
4444

45+
bool SupportsTextureFormat(TextureFormat format, TextureFlags flags);
46+
4547
ITexture CreateTextureAssetTexture(SerializableTexture asset, TextureFlags flags);
4648

4749
ITexture CreatePixelTexture(byte[] data, int width, int height, TextureFormat format, TextureFlags flags);

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/SDLGPUIndexBuffer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ public override void Update(Span<uint> data)
191191
return;
192192
}
193193

194+
var copyPass = SDL.SDL_BeginGPUCopyPass(command);
195+
196+
if (copyPass == nint.Zero)
197+
{
198+
return;
199+
}
200+
194201
var mapData = SDL.SDL_MapGPUTransferBuffer(device, transferBuffer, true);
195202

196203
unsafe
@@ -202,13 +209,6 @@ public override void Update(Span<uint> data)
202209

203210
SDL.SDL_UnmapGPUTransferBuffer(device, transferBuffer);
204211

205-
var copyPass = SDL.SDL_BeginGPUCopyPass(command);
206-
207-
if (copyPass == nint.Zero)
208-
{
209-
return;
210-
}
211-
212212
var location = new SDL.SDL_GPUTransferBufferLocation()
213213
{
214214
transfer_buffer = transferBuffer,

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/SDLGPURenderPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct StapleRenderData
3030

3131
public void BindPipeline(nint pipeline)
3232
{
33-
if (pipeline != lastGraphicsPipeline)
33+
//if (pipeline != lastGraphicsPipeline)
3434
{
3535
lastGraphicsPipeline = pipeline;
3636

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/SDLGPURendererBackend.cs

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Numerics;
5-
using System.Runtime.CompilerServices;
65
using System.Text;
76

87
namespace Staple.Internal;
@@ -14,6 +13,8 @@ internal class SDLGPURendererBackend : IRendererBackend
1413
private readonly Dictionary<int, nint> graphicsPipelines = [];
1514
private readonly Dictionary<TextureFlags, nint> textureSamplers = [];
1615
private Vector2Int renderSize;
16+
private ITexture depthTexture;
17+
private bool needsDepthTextureUpdate = false;
1718

1819
private nint commandBuffer;
1920
private nint swapchainTexture;
@@ -38,6 +39,34 @@ public bool TryGetCommandBuffer(out nint buffer)
3839
public bool SupportsLinearColorSpace => SDL.SDL_WindowSupportsGPUSwapchainComposition(device, window.window,
3940
SDL.SDL_GPUSwapchainComposition.SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR);
4041

42+
public TextureFormat? DepthStencilFormat
43+
{
44+
get
45+
{
46+
if(SupportsTextureFormat(TextureFormat.D32S8, TextureFlags.DepthStencilTarget))
47+
{
48+
return TextureFormat.D32S8;
49+
}
50+
51+
if(SupportsTextureFormat(TextureFormat.D24S8, TextureFlags.DepthStencilTarget))
52+
{
53+
return TextureFormat.D24S8;
54+
}
55+
56+
if (SupportsTextureFormat(TextureFormat.D24, TextureFlags.DepthStencilTarget))
57+
{
58+
return TextureFormat.D24;
59+
}
60+
61+
if (SupportsTextureFormat(TextureFormat.D16, TextureFlags.DepthStencilTarget))
62+
{
63+
return TextureFormat.D16;
64+
}
65+
66+
return null;
67+
}
68+
}
69+
4170
public bool Initialize(RendererType renderer, bool debug, IRenderWindow window, RenderModeFlags renderFlags)
4271
{
4372
if(window is not SDL3RenderWindow w)
@@ -89,6 +118,15 @@ public bool Initialize(RendererType renderer, bool debug, IRenderWindow window,
89118
return false;
90119
}
91120

121+
if(DepthStencilFormat.HasValue == false)
122+
{
123+
SDL.SDL_DestroyGPUDevice(device);
124+
125+
device = nint.Zero;
126+
127+
return false;
128+
}
129+
92130
if(SDL.SDL_ClaimWindowForGPUDevice(device, w.window) == false)
93131
{
94132
SDL.SDL_DestroyGPUDevice(device);
@@ -163,6 +201,8 @@ public void UpdateRenderMode(RenderModeFlags flags)
163201

164202
renderSize.X = w;
165203
renderSize.Y = h;
204+
205+
needsDepthTextureUpdate = true;
166206
}
167207

168208
public void UpdateViewport(int width, int height)
@@ -191,6 +231,12 @@ public void Destroy()
191231

192232
textureSamplers.Clear();
193233

234+
depthTexture?.Destroy();
235+
236+
depthTexture = null;
237+
238+
needsDepthTextureUpdate = true;
239+
194240
SDL.SDL_DestroyGPUDevice(device);
195241

196242
device = nint.Zero;
@@ -201,7 +247,7 @@ public void BeginFrame()
201247
{
202248
if(commandBuffer != nint.Zero)
203249
{
204-
SDL.SDL_CancelGPUCommandBuffer(commandBuffer);
250+
SDL.SDL_SubmitGPUCommandBuffer(commandBuffer);
205251

206252
commandBuffer = nint.Zero;
207253
}
@@ -230,6 +276,8 @@ public void BeginFrame()
230276

231277
swapchainWidth = (int)w;
232278
swapchainHeight = (int)h;
279+
280+
UpdateDepthTextureIfNeeded();
233281
}
234282

235283
public void EndFrame()
@@ -242,6 +290,23 @@ public void EndFrame()
242290
}
243291
}
244292

293+
private void UpdateDepthTextureIfNeeded()
294+
{
295+
if(needsDepthTextureUpdate == false ||
296+
swapchainWidth == 0 ||
297+
swapchainHeight == 0 ||
298+
DepthStencilFormat.HasValue == false)
299+
{
300+
return;
301+
}
302+
303+
needsDepthTextureUpdate = false;
304+
305+
depthTexture?.Destroy();
306+
307+
depthTexture = CreateEmptyTexture(swapchainWidth, swapchainHeight, DepthStencilFormat.Value, TextureFlags.DepthStencilTarget);
308+
}
309+
245310
public IRenderPass BeginRenderPass(RenderTarget target, CameraClearMode clear, Color clearColor, Vector4 viewport,
246311
Matrix4x4 view, Matrix4x4 projection)
247312
{
@@ -254,11 +319,34 @@ public IRenderPass BeginRenderPass(RenderTarget target, CameraClearMode clear, C
254319
var width = 0;
255320
var height = 0;
256321

322+
SDLGPUTexture depthTexture = null;
323+
257324
if (target == null)
258325
{
259326
texture = swapchainTexture;
260327
width = swapchainWidth;
261328
height = swapchainHeight;
329+
330+
depthTexture = this.depthTexture as SDLGPUTexture;
331+
332+
if (depthTexture is not SDLGPUTexture)
333+
{
334+
needsDepthTextureUpdate = true;
335+
336+
UpdateDepthTextureIfNeeded();
337+
338+
if (this.depthTexture is not SDLGPUTexture depth)
339+
{
340+
return null;
341+
}
342+
343+
depthTexture = depth;
344+
}
345+
346+
if (depthTexture?.Disposed ?? true)
347+
{
348+
return null;
349+
}
262350
}
263351
else
264352
{
@@ -270,7 +358,8 @@ public IRenderPass BeginRenderPass(RenderTarget target, CameraClearMode clear, C
270358
return null;
271359
}
272360

273-
if (texture == nint.Zero)
361+
if (texture == nint.Zero ||
362+
(depthTexture?.Disposed ?? true))
274363
{
275364
return null;
276365
}
@@ -293,7 +382,19 @@ public IRenderPass BeginRenderPass(RenderTarget target, CameraClearMode clear, C
293382
texture = texture,
294383
};
295384

296-
var renderPass = SDL.SDL_BeginGPURenderPass(commandBuffer, [colorTarget], 1, in Unsafe.NullRef<SDL.SDL_GPUDepthStencilTargetInfo>());
385+
var depthTarget = new SDL.SDL_GPUDepthStencilTargetInfo()
386+
{
387+
clear_depth = 0,
388+
load_op = clear switch
389+
{
390+
CameraClearMode.None => SDL.SDL_GPULoadOp.SDL_GPU_LOADOP_LOAD,
391+
_ => SDL.SDL_GPULoadOp.SDL_GPU_LOADOP_CLEAR,
392+
},
393+
store_op = SDL.SDL_GPUStoreOp.SDL_GPU_STOREOP_STORE,
394+
texture = depthTexture.texture,
395+
};
396+
397+
var renderPass = SDL.SDL_BeginGPURenderPass(commandBuffer, [colorTarget], 1, in depthTarget);
297398

298399
if (renderPass == nint.Zero)
299400
{
@@ -493,6 +594,17 @@ public IShaderProgram CreateShaderCompute(byte[] compute, ComputeShaderMetrics m
493594
}
494595
}
495596

597+
public bool SupportsTextureFormat(TextureFormat format, TextureFlags flags)
598+
{
599+
if(SDLGPUTexture.TryGetTextureFormat(format, flags, out var f) == false)
600+
{
601+
return false;
602+
}
603+
604+
return SDL.SDL_GPUTextureSupportsFormat(device, f, SDLGPUTexture.GetTextureType(flags),
605+
SDLGPUTexture.GetTextureUsage(flags));
606+
}
607+
496608
public nint GetSampler(TextureFlags flags)
497609
{
498610
var cleanFlags = TextureFlags.None;

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/SDLGPUTexture.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,6 @@ public void Update(Span<byte> data)
651651
return;
652652
}
653653

654-
var copyPass = SDL.SDL_BeginGPUCopyPass(command);
655-
656-
if (copyPass == nint.Zero)
657-
{
658-
return;
659-
}
660-
661654
if (transferBuffer == nint.Zero)
662655
{
663656
var info = new SDL.SDL_GPUTransferBufferCreateInfo()
@@ -668,12 +661,19 @@ public void Update(Span<byte> data)
668661

669662
transferBuffer = SDL.SDL_CreateGPUTransferBuffer(device, in info);
670663

671-
if(transferBuffer == nint.Zero)
664+
if (transferBuffer == nint.Zero)
672665
{
673666
return;
674667
}
675668
}
676669

670+
var copyPass = SDL.SDL_BeginGPUCopyPass(command);
671+
672+
if (copyPass == nint.Zero)
673+
{
674+
return;
675+
}
676+
677677
var mapData = SDL.SDL_MapGPUTransferBuffer(device, transferBuffer, true);
678678

679679
unsafe

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/SDLGPUVertexBuffer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public override void Update(nint data, int lengthInBytes)
130130
return;
131131
}
132132

133+
var copyPass = SDL.SDL_BeginGPUCopyPass(command);
134+
135+
if (copyPass == nint.Zero)
136+
{
137+
return;
138+
}
139+
133140
var mapData = SDL.SDL_MapGPUTransferBuffer(device, transferBuffer, true);
134141

135142
unsafe
@@ -142,13 +149,6 @@ public override void Update(nint data, int lengthInBytes)
142149

143150
SDL.SDL_UnmapGPUTransferBuffer(device, transferBuffer);
144151

145-
var copyPass = SDL.SDL_BeginGPUCopyPass(command);
146-
147-
if(copyPass == nint.Zero)
148-
{
149-
return;
150-
}
151-
152152
var location = new SDL.SDL_GPUTransferBufferLocation()
153153
{
154154
transfer_buffer = transferBuffer,

0 commit comments

Comments
 (0)