Skip to content

Commit edbfa0a

Browse files
[Rendering] Add support for array metadata on shaders;
[Rendering] Change Lighting to be a uniform for performance reasons;
1 parent c761fd3 commit edbfa0a

File tree

12 files changed

+221
-217
lines changed

12 files changed

+221
-217
lines changed

Engine/Staple.Core/MessagePackGenerated/MessagePackGenerated.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4831,12 +4831,13 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
48314831
}
48324832

48334833
global::MessagePack.IFormatterResolver formatterResolver = options.Resolver;
4834-
writer.WriteArrayHeader(5);
4834+
writer.WriteArrayHeader(6);
48354835
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.name, options);
48364836
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Serialize(ref writer, value.type, options);
48374837
writer.Write(value.offset);
48384838
writer.Write(value.size);
48394839
writer.Write(value.binding);
4840+
writer.Write(value.count);
48404841
}
48414842

48424843
public global::Staple.Internal.ShaderUniformField Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
@@ -4870,6 +4871,9 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
48704871
case 4:
48714872
____result.binding = reader.ReadInt32();
48724873
break;
4874+
case 5:
4875+
____result.count = reader.ReadInt32();
4876+
break;
48734877
default:
48744878
reader.Skip();
48754879
break;
@@ -4893,13 +4897,14 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
48934897
}
48944898

48954899
global::MessagePack.IFormatterResolver formatterResolver = options.Resolver;
4896-
writer.WriteArrayHeader(6);
4900+
writer.WriteArrayHeader(7);
48974901
formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::Staple.Internal.ShaderUniformField>>().Serialize(ref writer, value.fields, options);
48984902
writer.Write(value.binding);
48994903
writer.Write(value.size);
49004904
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.name, options);
49014905
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Serialize(ref writer, value.type, options);
49024906
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformTypeInfo>().Serialize(ref writer, value.elementType, options);
4907+
writer.Write(value.count);
49034908
}
49044909

49054910
public global::Staple.Internal.ShaderUniformMapping Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
@@ -4936,6 +4941,9 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
49364941
case 5:
49374942
____result.elementType = formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformTypeInfo>().Deserialize(ref reader, options);
49384943
break;
4944+
case 6:
4945+
____result.count = reader.ReadInt32();
4946+
break;
49394947
default:
49404948
reader.Skip();
49414949
break;

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

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

318318
renderer.mesh.SetActive(ref renderState, j);
319319

320-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
320+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
321321

322322
renderState.ApplyStorageBufferIfNeeded("StapleBoneMatrices", instance.boneBuffer);
323323

Engine/Staple.Core/Rendering/Graphics.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void RenderGeometry(VertexBuffer vertex, IndexBuffer index,
7676
return;
7777
}
7878

79-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
79+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
8080

8181
RenderSystem.Submit(renderState, Mesh.TriangleCount(topology, indexCount), 1);
8282
}
@@ -121,7 +121,7 @@ public static void RenderSimple<T>(Span<T> vertices, VertexLayout layout, Span<u
121121
return;
122122
}
123123

124-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
124+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
125125

126126
RenderSystem.Backend.RenderTransient(vertices, layout, indices, renderState);
127127
}
@@ -166,7 +166,7 @@ public static void RenderSimple<T>(Span<T> vertices, VertexLayout layout, Span<u
166166
return;
167167
}
168168

169-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
169+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
170170

171171
RenderSystem.Backend.RenderTransient(vertices, layout, indices, renderState);
172172
}

Engine/Staple.Core/Rendering/Lighting/LightSystem.cs

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ public struct LightInstance
1818
public Vector3 padding;
1919
}
2020

21-
[StructLayout(LayoutKind.Sequential, Pack = 0)]
22-
private struct LightDetails
23-
{
24-
public Vector4 positionType;
25-
public Color diffuse;
26-
public Color specular;
27-
}
28-
2921
/// <summary>
3022
/// Limit to 16 lights
3123
/// </summary>
@@ -48,21 +40,11 @@ private struct LightDetails
4840

4941
private readonly SceneQuery<Transform, Light> lightQuery = new();
5042

51-
private readonly LightDetails[] cachedLights = new LightDetails[MaxLights];
43+
private readonly Vector4[] cachedLightTypePositions = new Vector4[MaxLights];
44+
private readonly Color[] cachedLightDiffuse = new Color[MaxLights];
5245

5346
private readonly Dictionary<int, ShaderHandle[]> cachedInstancedMaterialInfo = [];
5447

55-
private VertexBuffer lightDataBuffer;
56-
57-
private readonly Lazy<VertexLayout> lightDataBufferLayout = new(() =>
58-
{
59-
return VertexLayoutBuilder.CreateNew()
60-
.Add(VertexAttribute.TexCoord0, VertexAttributeType.Float4)
61-
.Add(VertexAttribute.TexCoord1, VertexAttributeType.Float4)
62-
.Add(VertexAttribute.TexCoord2, VertexAttributeType.Float4)
63-
.Build();
64-
});
65-
6648
public bool UsesOwnRenderProcess => false;
6749

6850
public Type RelatedComponent => null;
@@ -149,27 +131,12 @@ public void ApplyMaterialLighting(Material material, MaterialLighting lighting)
149131
/// <param name="lighting">What lighting to use</param>
150132
/// <param name="state">The current rendering state</param>
151133
internal bool ApplyLightProperties(Material material, Vector3 cameraPosition,
152-
MaterialLighting lighting, ref RenderState state)
134+
MaterialLighting lighting)
153135
{
154-
void EnsureStorageBuffer(ref RenderState state)
155-
{
156-
lightDataBuffer ??= VertexBuffer.Create(cachedLights, lightDataBufferLayout.Value, RenderBufferFlags.GraphicsRead);
157-
158-
if (lightDataBuffer == null)
159-
{
160-
return;
161-
}
162-
163-
state.ApplyStorageBufferIfNeeded("StapleLights", lightDataBuffer);
164-
}
165-
166136
if (Enabled == false ||
167137
lighting == MaterialLighting.Unlit ||
168-
(material?.IsValid ?? false) == false ||
169-
state.shaderInstance?.program == null)
138+
(material?.IsValid ?? false) == false)
170139
{
171-
EnsureStorageBuffer(ref state);
172-
173140
return false;
174141
}
175142

@@ -197,21 +164,10 @@ void EnsureStorageBuffer(ref RenderState state)
197164
p = -forward;
198165
}
199166

200-
cachedLights[i].positionType = new((float)light.type, p.X, p.Y, p.Z);
201-
cachedLights[i].diffuse = light.color;
202-
cachedLights[i].specular = Color.White;
203-
//cachedLights[i].spotDirection = forward;
167+
cachedLightTypePositions[i] = new((float)light.type, p.X, p.Y, p.Z);
168+
cachedLightDiffuse[i] = light.color;
204169
}
205170

206-
EnsureStorageBuffer(ref state);
207-
208-
if(lightDataBuffer?.Disposed ?? true)
209-
{
210-
return false;
211-
}
212-
213-
lightDataBuffer.Update(cachedLights);
214-
215171
var key = HashCode.Combine(material.shader.Guid.GuidHash, material.ShaderVariantKey);
216172

217173
static bool HandlesValid(Span<ShaderHandle> handles)
@@ -231,27 +187,29 @@ static bool HandlesValid(Span<ShaderHandle> handles)
231187
{
232188
handles = [
233189
material.GetShaderHandle("StapleLightCountViewPosition"),
234-
material.GetShaderHandle("StapleLightAmbientColor")
190+
material.GetShaderHandle("StapleLightAmbientColor"),
191+
material.GetShaderHandle("StapleLightTypePosition"),
192+
material.GetShaderHandle("StapleLightDiffuse"),
235193
];
236194

237195
cachedInstancedMaterialInfo.AddOrSetKey(key, handles);
238196
}
239197

240-
if((handles?.Length ?? 0) != 2 ||
198+
if((handles?.Length ?? 0) != 4 ||
241199
HandlesValid(handles) == false)
242200
{
243-
EnsureStorageBuffer(ref state);
244-
245201
return false;
246202
}
247203

248204
var viewPosHandle = handles[0];
249205
var lightAmbientHandle = handles[1];
206+
var lightTypePositionHandle = handles[2];
207+
var lightDiffuseHandle = handles[3];
250208

251209
material.shader.SetVector4(material.ShaderVariantKey, viewPosHandle, new Vector4(lightCount, cameraPosition.X, cameraPosition.Y, cameraPosition.Z));
252210
material.shader.SetColor(material.ShaderVariantKey, lightAmbientHandle, lightAmbient);
253-
254-
EnsureStorageBuffer(ref state);
211+
material.shader.SetVector4(material.ShaderVariantKey, lightTypePositionHandle, cachedLightTypePositions);
212+
material.shader.SetColor(material.ShaderVariantKey, lightDiffuseHandle, cachedLightDiffuse);
255213

256214
return true;
257215
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void SetupMaterial()
366366
continue;
367367
}
368368

369-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
369+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
370370

371371
RenderSystem.Submit(renderState, mesh.SubmeshTriangleCount(0), 1);
372372
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static void RenderMesh(Mesh mesh, Vector3 position, Quaternion rotation,
108108
return;
109109
}
110110

111-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
111+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
112112

113113
RenderSystem.Submit(renderState, Mesh.TriangleCount(mesh.MeshTopology, mesh.IndexCount), 1);
114114
}
@@ -333,7 +333,7 @@ public void Submit()
333333
material.ApplyProperties(Material.ApplyMode.All, ref renderState);
334334

335335
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position,
336-
contents.instanceInfos.Contents[0].lighting, ref renderState);
336+
contents.instanceInfos.Contents[0].lighting);
337337

338338
if (material.IsShaderKeywordEnabled(Shader.InstancingKeyword))
339339
{

Engine/Staple.Core/Rendering/Shader/Shader.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ internal void AddUniform(DefaultUniform uniform, ShaderInstance instance)
282282
slot = uniform.slot,
283283
},
284284
handle = new(normalizedName),
285-
count = NormalizeUniformCount(uniform.name),
285+
count = 1,
286286
};
287287

288288
instance.uniforms.Add(normalizedName, u);
@@ -331,6 +331,7 @@ internal UniformInfo GetUniform(StringID name, ShaderInstance instance)
331331
type = uniform.type,
332332
},
333333
handle = name,
334+
count = uniform.count,
334335
};
335336
}
336337
}
@@ -347,6 +348,7 @@ internal UniformInfo GetUniform(StringID name, ShaderInstance instance)
347348
type = uniform.type,
348349
},
349350
handle = name,
351+
count = uniform.count,
350352
};
351353
}
352354
}
@@ -363,6 +365,24 @@ internal UniformInfo GetUniform(StringID name, ShaderInstance instance)
363365
type = pair.Value.type,
364366
},
365367
handle = pair.Key,
368+
count = pair.Value.count,
369+
};
370+
}
371+
}
372+
373+
foreach (var pair in instance.fragmentFields)
374+
{
375+
if (pair.Key == name)
376+
{
377+
return new()
378+
{
379+
uniform = new()
380+
{
381+
name = pair.Value.name,
382+
type = pair.Value.type,
383+
},
384+
handle = pair.Key,
385+
count = pair.Value.count,
366386
};
367387
}
368388
}

Engine/Staple.Core/Serialization/Shader/ShaderMetadata.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using MessagePack;
22
using System;
33
using System.Collections.Generic;
4-
using System.Linq;
54

65
namespace Staple.Internal;
76

@@ -26,6 +25,7 @@ public enum ShaderUniformType
2625
WriteOnlyBuffer,
2726
ReadWriteBuffer,
2827
Structure,
28+
Array,
2929
}
3030

3131
[MessagePackObject]
@@ -67,6 +67,9 @@ public class ShaderUniformField
6767

6868
[Key(4)]
6969
public int binding;
70+
71+
[Key(5)]
72+
public int count;
7073
}
7174

7275
[MessagePackObject]
@@ -102,6 +105,9 @@ public class ShaderUniformMapping
102105

103106
[Key(5)]
104107
public ShaderUniformTypeInfo elementType;
108+
109+
[Key(6)]
110+
public int count;
105111
}
106112

107113
[MessagePackObject]

0 commit comments

Comments
 (0)