Skip to content

Commit 622d23b

Browse files
[Rendering] Fixes for shader handling of textures, storage textures, and storage buffers;
1 parent c2ae728 commit 622d23b

File tree

10 files changed

+146
-57
lines changed

10 files changed

+146
-57
lines changed

BuiltinResources/Hidden/Shaders/Default/SolidColor.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ End Common
2525

2626
Begin Vertex
2727

28-
[[vk::binding(StapleBufferIndexCount, StapleUniformBufferSet)]]
28+
[[vk::binding(StapleUniformBufferStart, StapleUniformBufferSet)]]
2929
cbuffer Uniforms
3030
{
3131
float4 mainColor;

BuiltinResources/Hidden/Shaders/Default/Standard.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ End Instancing
3838

3939
Begin Common
4040

41-
[[vk::binding(StapleBufferIndexCount, StapleUniformBufferSet)]]
41+
[[vk::binding(StapleUniformBufferStart, StapleUniformBufferSet)]]
4242
cbuffer Uniforms
4343
{
4444
float3 viewPosition;
@@ -140,7 +140,7 @@ End Vertex
140140

141141
Begin Fragment
142142

143-
[[vk::binding(0, StapleSamplerBufferSet)]]
143+
[[vk::binding(0, StapleSamplerStorageBufferSet)]]
144144
cbuffer Textures
145145
{
146146
Sampler2D diffuseTexture;

BuiltinResources/Hidden/Shaders/Sprite/Sprite.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ End Input
1616

1717
Begin Common
1818

19-
[[vk::binding(StapleBufferIndexCount, StapleUniformBufferSet)]]
19+
[[vk::binding(StapleUniformBufferStart, StapleUniformBufferSet)]]
2020
cbuffer Uniforms
2121
{
2222
float4 mainColor;
@@ -58,7 +58,7 @@ End Vertex
5858

5959
Begin Fragment
6060

61-
[[vk::binding(0, StapleSamplerBufferSet)]]
61+
[[vk::binding(0, StapleSamplerStorageBufferSet)]]
6262
cbuffer Textures
6363
{
6464
Sampler2D mainTexture;

BuiltinResources/Hidden/Shaders/UI/imgui.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ End Vertex
5757

5858
Begin Fragment
5959

60-
[[vk::binding(0, StapleSamplerBufferSet)]]
60+
[[vk::binding(0, StapleSamplerStorageBufferSet)]]
6161
cbuffer Uniforms
6262
{
6363
Sampler2D mainTexture;

Engine/Staple.Core/Rendering/Animation/SkinnedMeshRenderSystem.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Numerics;
54

@@ -10,8 +9,6 @@ namespace Staple.Internal;
109
/// </summary>
1110
public class SkinnedMeshRenderSystem : IRenderSystem
1211
{
13-
private const int SkinningBufferIndex = 1;
14-
1512
/// <summary>
1613
/// Info for rendering
1714
/// </summary>

Engine/Staple.Core/Rendering/RenderSystem/Backend/Impls/SDLGPU/Commands/SDLGPURenderCommand.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void Update(IRendererBackend rendererBackend)
2020
state.shader == null ||
2121
program is not SDLGPUShaderProgram shader ||
2222
shader.Type != ShaderType.VertexFragment ||
23+
state.shader.instances.TryGetValue(state.shaderVariant, out var shaderInstance) == false ||
2324
state.vertexBuffer is not SDLGPUVertexBuffer vertex ||
2425
backend.TryGetVertexBuffer(vertex.handle, out var vertexBuffer) == false ||
2526
vertex.layout is not SDLGPUVertexLayout vertexLayout ||
@@ -94,7 +95,14 @@ buffer.Item2 is not SDLGPUVertexBuffer v ||
9495
buffers.Add(resource.buffer);
9596
}
9697

97-
SDL.BindGPUVertexStorageBuffers(renderPass, 0, buffers.ToArray(), (uint)buffers.Count);
98+
if(buffers.Count > 0)
99+
{
100+
var bufferArray = buffers.ToArray();
101+
102+
SDL.BindGPUVertexStorageBuffers(renderPass,
103+
(uint)(shaderInstance.vertexShaderMetrics.samplerCount + shaderInstance.vertexShaderMetrics.storageTextureCount),
104+
bufferArray, (uint)buffers.Count);
105+
}
98106
}
99107

100108
unsafe

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Evergine.Bindings.Vulkan;
2-
using SDL3;
1+
using SDL3;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -1209,8 +1208,6 @@ List<VertexAttribute> GetMissingAttributes()
12091208
{
12101209
var shaderAttributes = new List<SDL.GPUVertexAttribute>();
12111210

1212-
var missingAttributes = new List<VertexAttribute>();
1213-
12141211
for (var i = 0; i < instance.attributes.Length; i++)
12151212
{
12161213
var attributeIndex = vertexLayout.vertexAttributes.IndexOf(instance.attributes[i]);
@@ -1230,6 +1227,8 @@ List<VertexAttribute> GetMissingAttributes()
12301227

12311228
var attribute = vertexLayout.attributes[attributeIndex];
12321229

1230+
attributeIndex = instance.attributes.IndexOf(vertexLayout.vertexAttributes[attributeIndex]);
1231+
12331232
shaderAttributes.Add(new()
12341233
{
12351234
BufferSlot = 0,
@@ -1239,14 +1238,6 @@ List<VertexAttribute> GetMissingAttributes()
12391238
});
12401239
}
12411240

1242-
if(missingAttributes.Count > 0)
1243-
{
1244-
Log.Error($"Failed to render: vertex attributes {string.Join(", ", missingAttributes.Select(x => x.ToString().ToUpperInvariant()))} " +
1245-
"were not declared in the vertex layout!");
1246-
1247-
return false;
1248-
}
1249-
12501241
var attributesSpan = CollectionsMarshal.AsSpan(shaderAttributes);
12511242

12521243
fixed (SDL.GPUVertexAttribute* attributes = attributesSpan)

Engine/Staple.Editor/StapleEditor+Render.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,47 +152,45 @@ public void RenderScene()
152152
{
153153
var (components, renderables) = pair.Value;
154154

155-
if (renderables.Count == 0)
156-
{
157-
continue;
158-
}
159-
160155
try
161156
{
162157
pair.Key.Preprocess(components.ToArray(), camera, cameraTransform);
163158

164-
foreach (var (entity, transform, renderable) in renderables)
159+
if(renderables.Count > 0)
165160
{
166-
if (renderable.enabled)
161+
foreach (var (entity, transform, renderable) in renderables)
167162
{
168-
renderable.isVisible = renderable.enabled &&
169-
renderable.forceRenderingOff == false &&
170-
renderable.cullingState != CullingState.Invisible;
171-
172-
if (renderable.isVisible)
163+
if (renderable.enabled)
173164
{
174-
if (renderable.cullingState == CullingState.None)
165+
renderable.isVisible = renderable.enabled &&
166+
renderable.forceRenderingOff == false &&
167+
renderable.cullingState != CullingState.Invisible;
168+
169+
if (renderable.isVisible)
175170
{
176-
renderable.isVisible = camera.IsVisible(renderable.bounds);
171+
if (renderable.cullingState == CullingState.None)
172+
{
173+
renderable.isVisible = camera.IsVisible(renderable.bounds);
177174

178-
renderable.cullingState = renderable.isVisible ? CullingState.Visible : CullingState.Invisible;
175+
renderable.cullingState = renderable.isVisible ? CullingState.Visible : CullingState.Invisible;
176+
}
179177
}
180-
}
181178

182-
if (renderable.isVisible == false)
183-
{
184-
RenderSystem.RenderStats.culledDrawCalls++;
185-
}
179+
if (renderable.isVisible == false)
180+
{
181+
RenderSystem.RenderStats.culledDrawCalls++;
182+
}
186183

187-
if (transform.ChangedThisFrame)
184+
if (transform.ChangedThisFrame)
185+
{
186+
ReplaceEntityBodyIfNeeded(entity, renderable.bounds);
187+
}
188+
}
189+
else
188190
{
189-
ReplaceEntityBodyIfNeeded(entity, renderable.bounds);
191+
ClearEntityBody(entity);
190192
}
191193
}
192-
else
193-
{
194-
ClearEntityBody(entity);
195-
}
196194
}
197195

198196
pair.Key.Process(components.ToArray(), camera, cameraTransform);

Tools/Baker/Baker+Shaders.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,73 @@ byte[] ProcessShader(string shaderFileName, List<string> extraDefines,
457457
}
458458

459459
process.Close();
460+
461+
try
462+
{
463+
var text = File.ReadAllText(reflectionJsonFileName);
464+
465+
reflectionData = JsonConvert.DeserializeObject<ShaderReflectionData>(text);
466+
467+
//For debugging
468+
//File.Copy(reflectionJsonFileName, $"{outputFile}.reflection.json", true);
469+
}
470+
catch (Exception e)
471+
{
472+
shaderMetrics = null;
473+
reflectionData = null;
474+
475+
Console.WriteLine($"Failed to process reflection: {e}");
476+
477+
File.Delete(reflectionJsonFileName);
478+
479+
return null;
480+
}
481+
482+
var metrics = reflectionData.ToContainer();
483+
484+
var textureCount = metrics.textures.Count;
485+
var storageTextureCount = 0; //TODO
486+
var storageBufferCount = metrics.storageBuffers.Count;
487+
var uniformBufferCount = metrics.uniforms.Count;
488+
489+
defineString += $" -DSTAPLE_TEXTURE_COUNT={textureCount}" +
490+
$" -DSTAPLE_STORAGE_TEXTURE_COUNT={storageTextureCount}" +
491+
$" -DSTAPLE_STORAGE_BUFFER_COUNT={storageBufferCount}" +
492+
$" -DSTAPLE_UNIFORM_BUFFER_COUNT={uniformBufferCount}";
493+
494+
process = new Process
495+
{
496+
StartInfo = new ProcessStartInfo
497+
{
498+
FileName = shaderTranspilerPath,
499+
Arguments = $"-o \"{outShaderFileNameTranspiled}\" -profile sm_6_0 -target spirv {stage} {entry} {defineString} {shaderInclude} -reflection-json \"{reflectionJsonFileName}\" \"{shaderFileName}\"",
500+
UseShellExecute = false,
501+
RedirectStandardOutput = true,
502+
CreateNoWindow = true,
503+
}
504+
};
505+
506+
Utilities.ExecuteAndCollectProcess(process, null);
507+
508+
if (process.ExitCode != 0)
509+
{
510+
shaderMetrics = null;
511+
reflectionData = null;
512+
513+
Console.WriteLine($"Arguments: {process.StartInfo.Arguments}");
514+
515+
try
516+
{
517+
File.Delete(outShaderFileNameTranspiled);
518+
}
519+
catch (Exception)
520+
{
521+
}
522+
523+
return null;
524+
}
525+
526+
process.Close();
460527
}
461528

462529
try

Tools/ShaderIncludes/Staple.slang

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,58 @@ module Staple;
33
__include StapleMath;
44
__include StapleLighting;
55

6-
#define STAPLE_SKINNING_STAGE_INDEX 2
6+
#ifdef STAPLE_TEXTURE_COUNT
7+
public static const int StapleTextureCount = STAPLE_TEXTURE_COUNT;
8+
#else
9+
public static const int StapleTextureCount = 0;
10+
#endif
711

8-
#ifdef SKINNING
9-
public static const int StapleBufferIndexCount = 3;
12+
#ifdef STAPLE_STORAGE_TEXTURE_COUNT
13+
public static const int StapleStorageTextureStart = StapleTextureCount;
14+
public static const int StapleStorageTextureCount = StapleStorageTextureStart + STAPLE_STORAGE_TEXTURE_COUNT;
15+
#else
16+
public static const int StapleStorageTextureStart = 1;
17+
public static const int StapleStorageTextureCount = 1;
18+
#endif
19+
20+
#ifdef STAPLE_STORAGE_BUFFER_COUNT
21+
public static const int StapleStorageBufferStart = StapleStorageTextureCount;
22+
public static const int StapleStorageBufferCount = StapleStorageBufferStart + STAPLE_STORAGE_BUFFER_COUNT;
23+
#else
24+
public static const int StapleStorageBufferStart = 2;
25+
public static const int StapleStorageBufferCount = 2;
26+
#endif
27+
28+
#ifdef STAPLE_UNIFORM_BUFFER_COUNT
29+
public static const int StapleUniformBufferCount = STAPLE_UNIFORM_BUFFER_COUNT;
1030
#else
11-
public static const int StapleBufferIndexCount = 2;
31+
public static const int StapleUniformBufferCount = 0;
1232
#endif
1333

1434
#ifdef STAPLE_VERTEX_SHADER
15-
public static const int StapleSamplerBufferSet = 0;
35+
public static const int StapleSamplerStorageBufferSet = 0;
1636
public static const int StapleUniformBufferSet = 1;
1737
#elif defined(STAPLE_FRAGMENT_SHADER)
18-
public static const int StapleSamplerBufferSet = 2;
38+
public static const int StapleSamplerStorageBufferSet = 2;
1939
public static const int StapleUniformBufferSet = 3;
2040
#elif defined(STAPLE_COMPUTE_SHADER)
21-
public static const int StapleSamplerBufferSet = 0;
41+
public static const int StapleSamplerStorageBufferSet = 0;
2242
public static const int StapleReadWriteBufferSet = 1;
2343
public static const int StapleUniformBufferSet = 2;
2444
#else
25-
public static const int StapleSamplerBufferSet = 0;
45+
public static const int StapleSamplerStorageBufferSet = 0;
2646
public static const int StapleReadWriteBufferSet = 0;
2747
public static const int StapleUniformBufferSet = 0;
2848
#endif
2949

50+
public static const int StapleUniformBufferStart = 2;
51+
52+
#ifdef SKINNING
53+
public static const int StapleInternalStorageBufferCount = 1;
54+
#else
55+
public static const int StapleInternalStorageBufferCount = 0;
56+
#endif
57+
3058
[[vk::binding(0, StapleUniformBufferSet)]]
3159
public cbuffer StapleRenderData
3260
{
@@ -42,7 +70,7 @@ public cbuffer StapleFragmentRenderData
4270
};
4371

4472
#ifdef SKINNING
45-
[[vk::binding(STAPLE_SKINNING_STAGE_INDEX, StapleUniformBufferSet)]]
73+
[[vk::binding(StapleStorageBufferStart, StapleSamplerStorageBufferSet)]]
4674
public StructuredBuffer<float4> StapleBoneMatrices;
4775
#endif
4876

0 commit comments

Comments
 (0)