Skip to content

Commit 2b1fe3f

Browse files
[Rendering] Fix Lighting and fixed a bug with ResumePass's clearing flags;
1 parent 7a9dfec commit 2b1fe3f

File tree

6 files changed

+272
-92
lines changed

6 files changed

+272
-92
lines changed

Engine/Staple.Core/MessagePackGenerated/MessagePackGenerated.cs

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal static class GeneratedResolverGetFormatterHelper
5050

5151
static GeneratedResolverGetFormatterHelper()
5252
{
53-
lookup = new global::System.Collections.Generic.Dictionary<Type, int>(137)
53+
lookup = new global::System.Collections.Generic.Dictionary<Type, int>(138)
5454
{
5555
{ typeof(global::Staple.Internal.MeshAssetNode[]), 0 },
5656
{ typeof(global::Staple.VertexAttribute[]), 1 },
@@ -175,20 +175,21 @@ static GeneratedResolverGetFormatterHelper()
175175
{ typeof(global::Staple.Internal.ShaderUniformContainer), 120 },
176176
{ typeof(global::Staple.Internal.ShaderUniformField), 121 },
177177
{ typeof(global::Staple.Internal.ShaderUniformMapping), 122 },
178-
{ typeof(global::Staple.Internal.TextAssetMetadata), 123 },
179-
{ typeof(global::Staple.Internal.TextureMetadata), 124 },
180-
{ typeof(global::Staple.Internal.TextureMetadataOverride), 125 },
181-
{ typeof(global::Staple.Internal.TextureSpriteInfo), 126 },
182-
{ typeof(global::Staple.Internal.Vector2Holder), 127 },
183-
{ typeof(global::Staple.Internal.Vector3Holder), 128 },
184-
{ typeof(global::Staple.Internal.Vector4Holder), 129 },
185-
{ typeof(global::Staple.Internal.VertexFragmentShaderMetrics), 130 },
186-
{ typeof(global::Staple.LayerMask), 131 },
187-
{ typeof(global::Staple.Rect), 132 },
188-
{ typeof(global::Staple.RectFloat), 133 },
189-
{ typeof(global::Staple.Vector2Int), 134 },
190-
{ typeof(global::Staple.Vector3Int), 135 },
191-
{ typeof(global::Staple.Vector4Int), 136 },
178+
{ typeof(global::Staple.Internal.ShaderUniformTypeInfo), 123 },
179+
{ typeof(global::Staple.Internal.TextAssetMetadata), 124 },
180+
{ typeof(global::Staple.Internal.TextureMetadata), 125 },
181+
{ typeof(global::Staple.Internal.TextureMetadataOverride), 126 },
182+
{ typeof(global::Staple.Internal.TextureSpriteInfo), 127 },
183+
{ typeof(global::Staple.Internal.Vector2Holder), 128 },
184+
{ typeof(global::Staple.Internal.Vector3Holder), 129 },
185+
{ typeof(global::Staple.Internal.Vector4Holder), 130 },
186+
{ typeof(global::Staple.Internal.VertexFragmentShaderMetrics), 131 },
187+
{ typeof(global::Staple.LayerMask), 132 },
188+
{ typeof(global::Staple.Rect), 133 },
189+
{ typeof(global::Staple.RectFloat), 134 },
190+
{ typeof(global::Staple.Vector2Int), 135 },
191+
{ typeof(global::Staple.Vector3Int), 136 },
192+
{ typeof(global::Staple.Vector4Int), 137 },
192193
};
193194
}
194195

@@ -325,20 +326,21 @@ internal static object GetFormatter(Type t)
325326
case 120: return new MessagePack.Formatters.Staple.Internal.ShaderUniformContainerFormatter();
326327
case 121: return new MessagePack.Formatters.Staple.Internal.ShaderUniformFieldFormatter();
327328
case 122: return new MessagePack.Formatters.Staple.Internal.ShaderUniformMappingFormatter();
328-
case 123: return new MessagePack.Formatters.Staple.Internal.TextAssetMetadataFormatter();
329-
case 124: return new MessagePack.Formatters.Staple.Internal.TextureMetadataFormatter();
330-
case 125: return new MessagePack.Formatters.Staple.Internal.TextureMetadataOverrideFormatter();
331-
case 126: return new MessagePack.Formatters.Staple.Internal.TextureSpriteInfoFormatter();
332-
case 127: return new MessagePack.Formatters.Staple.Internal.Vector2HolderFormatter();
333-
case 128: return new MessagePack.Formatters.Staple.Internal.Vector3HolderFormatter();
334-
case 129: return new MessagePack.Formatters.Staple.Internal.Vector4HolderFormatter();
335-
case 130: return new MessagePack.Formatters.Staple.Internal.VertexFragmentShaderMetricsFormatter();
336-
case 131: return new MessagePack.Formatters.Staple.LayerMaskFormatter();
337-
case 132: return new MessagePack.Formatters.Staple.RectFormatter();
338-
case 133: return new MessagePack.Formatters.Staple.RectFloatFormatter();
339-
case 134: return new MessagePack.Formatters.Staple.Vector2IntFormatter();
340-
case 135: return new MessagePack.Formatters.Staple.Vector3IntFormatter();
341-
case 136: return new MessagePack.Formatters.Staple.Vector4IntFormatter();
329+
case 123: return new MessagePack.Formatters.Staple.Internal.ShaderUniformTypeInfoFormatter();
330+
case 124: return new MessagePack.Formatters.Staple.Internal.TextAssetMetadataFormatter();
331+
case 125: return new MessagePack.Formatters.Staple.Internal.TextureMetadataFormatter();
332+
case 126: return new MessagePack.Formatters.Staple.Internal.TextureMetadataOverrideFormatter();
333+
case 127: return new MessagePack.Formatters.Staple.Internal.TextureSpriteInfoFormatter();
334+
case 128: return new MessagePack.Formatters.Staple.Internal.Vector2HolderFormatter();
335+
case 129: return new MessagePack.Formatters.Staple.Internal.Vector3HolderFormatter();
336+
case 130: return new MessagePack.Formatters.Staple.Internal.Vector4HolderFormatter();
337+
case 131: return new MessagePack.Formatters.Staple.Internal.VertexFragmentShaderMetricsFormatter();
338+
case 132: return new MessagePack.Formatters.Staple.LayerMaskFormatter();
339+
case 133: return new MessagePack.Formatters.Staple.RectFormatter();
340+
case 134: return new MessagePack.Formatters.Staple.RectFloatFormatter();
341+
case 135: return new MessagePack.Formatters.Staple.Vector2IntFormatter();
342+
case 136: return new MessagePack.Formatters.Staple.Vector3IntFormatter();
343+
case 137: return new MessagePack.Formatters.Staple.Vector4IntFormatter();
342344
default: return null;
343345
}
344346
}
@@ -4897,7 +4899,7 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
48974899
writer.Write(value.size);
48984900
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.name, options);
48994901
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Serialize(ref writer, value.type, options);
4900-
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Serialize(ref writer, value.elementType, options);
4902+
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformTypeInfo>().Serialize(ref writer, value.elementType, options);
49014903
}
49024904

49034905
public global::Staple.Internal.ShaderUniformMapping Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
@@ -4932,7 +4934,61 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
49324934
____result.type = formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Deserialize(ref reader, options);
49334935
break;
49344936
case 5:
4935-
____result.elementType = formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Deserialize(ref reader, options);
4937+
____result.elementType = formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformTypeInfo>().Deserialize(ref reader, options);
4938+
break;
4939+
default:
4940+
reader.Skip();
4941+
break;
4942+
}
4943+
}
4944+
4945+
reader.Depth--;
4946+
return ____result;
4947+
}
4948+
}
4949+
4950+
public sealed class ShaderUniformTypeInfoFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::Staple.Internal.ShaderUniformTypeInfo>
4951+
{
4952+
4953+
public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::Staple.Internal.ShaderUniformTypeInfo value, global::MessagePack.MessagePackSerializerOptions options)
4954+
{
4955+
if (value == null)
4956+
{
4957+
writer.WriteNil();
4958+
return;
4959+
}
4960+
4961+
global::MessagePack.IFormatterResolver formatterResolver = options.Resolver;
4962+
writer.WriteArrayHeader(3);
4963+
formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Serialize(ref writer, value.type, options);
4964+
writer.Write(value.size);
4965+
formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::Staple.Internal.ShaderUniformField>>().Serialize(ref writer, value.fields, options);
4966+
}
4967+
4968+
public global::Staple.Internal.ShaderUniformTypeInfo Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
4969+
{
4970+
if (reader.TryReadNil())
4971+
{
4972+
return null;
4973+
}
4974+
4975+
options.Security.DepthStep(ref reader);
4976+
global::MessagePack.IFormatterResolver formatterResolver = options.Resolver;
4977+
var length = reader.ReadArrayHeader();
4978+
var ____result = new global::Staple.Internal.ShaderUniformTypeInfo();
4979+
4980+
for (int i = 0; i < length; i++)
4981+
{
4982+
switch (i)
4983+
{
4984+
case 0:
4985+
____result.type = formatterResolver.GetFormatterWithVerify<global::Staple.Internal.ShaderUniformType>().Deserialize(ref reader, options);
4986+
break;
4987+
case 1:
4988+
____result.size = reader.ReadInt32();
4989+
break;
4990+
case 2:
4991+
____result.fields = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::Staple.Internal.ShaderUniformField>>().Deserialize(ref reader, options);
49364992
break;
49374993
default:
49384994
reader.Skip();

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ public struct LightInstance
2121
[StructLayout(LayoutKind.Sequential, Pack = 0)]
2222
private struct LightDetails
2323
{
24-
public LightType type;
25-
public Vector3 position;
26-
public Vector3 diffuse;
27-
public Vector3 specular;
28-
public Vector3 spotDirection;
29-
public Vector3 padding;
24+
public Vector4 positionType;
25+
public Color diffuse;
26+
public Color specular;
3027
}
3128

3229
/// <summary>
@@ -63,7 +60,6 @@ private struct LightDetails
6360
.Add(VertexAttribute.TexCoord0, VertexAttributeType.Float4)
6461
.Add(VertexAttribute.TexCoord1, VertexAttributeType.Float4)
6562
.Add(VertexAttribute.TexCoord2, VertexAttributeType.Float4)
66-
.Add(VertexAttribute.TexCoord3, VertexAttributeType.Float4)
6763
.Build();
6864
});
6965

@@ -157,15 +153,13 @@ internal bool ApplyLightProperties(Material material, Vector3 cameraPosition,
157153
{
158154
void EnsureStorageBuffer(ref RenderState state)
159155
{
160-
lightDataBuffer ??= VertexBuffer.Create(cachedLights.AsSpan(), lightDataBufferLayout.Value, RenderBufferFlags.GraphicsRead);
156+
lightDataBuffer ??= VertexBuffer.Create(cachedLights, lightDataBufferLayout.Value, RenderBufferFlags.GraphicsRead);
161157

162158
if (lightDataBuffer == null)
163159
{
164160
return;
165161
}
166162

167-
lightDataBuffer.Update(cachedLights.AsSpan());
168-
169163
state.ApplyStorageBufferIfNeeded("StapleLights", lightDataBuffer);
170164
}
171165

@@ -189,7 +183,6 @@ void EnsureStorageBuffer(ref RenderState state)
189183
}
190184

191185
var lightAmbient = AmbientColor;
192-
var lightCountValue = targets.Length;
193186

194187
for (var i = 0; i < lightCount; i++)
195188
{
@@ -204,13 +197,22 @@ void EnsureStorageBuffer(ref RenderState state)
204197
p = -forward;
205198
}
206199

207-
cachedLights[i].type = light.type;
208-
cachedLights[i].position = p;
209-
cachedLights[i].diffuse = (Vector3)light.color;
210-
cachedLights[i].spotDirection = forward;
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;
211204
}
212205

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

215217
static bool HandlesValid(Span<ShaderHandle> handles)
216218
{
@@ -228,15 +230,14 @@ static bool HandlesValid(Span<ShaderHandle> handles)
228230
if (cachedInstancedMaterialInfo.TryGetValue(key, out var handles) == false || HandlesValid(handles) == false)
229231
{
230232
handles = [
231-
material.GetShaderHandle("StapleLightViewPosition"),
232-
material.GetShaderHandle("StapleLightAmbientColor"),
233-
material.GetShaderHandle("StapleLightCount")
233+
material.GetShaderHandle("StapleLightCountViewPosition"),
234+
material.GetShaderHandle("StapleLightAmbientColor")
234235
];
235236

236237
cachedInstancedMaterialInfo.AddOrSetKey(key, handles);
237238
}
238239

239-
if((handles?.Length ?? 0) != 3 ||
240+
if((handles?.Length ?? 0) != 2 ||
240241
HandlesValid(handles) == false)
241242
{
242243
EnsureStorageBuffer(ref state);
@@ -246,11 +247,9 @@ static bool HandlesValid(Span<ShaderHandle> handles)
246247

247248
var viewPosHandle = handles[0];
248249
var lightAmbientHandle = handles[1];
249-
var lightCountHandle = handles[2];
250250

251-
material.shader.SetVector3(material.ShaderVariantKey, viewPosHandle, cameraPosition);
251+
material.shader.SetVector4(material.ShaderVariantKey, viewPosHandle, new Vector4(lightCount, cameraPosition.X, cameraPosition.Y, cameraPosition.Z));
252252
material.shader.SetColor(material.ShaderVariantKey, lightAmbientHandle, lightAmbient);
253-
material.shader.SetFloat(material.ShaderVariantKey, lightCountHandle, lightCountValue);
254253

255254
EnsureStorageBuffer(ref state);
256255

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Staple.Internal;
1212
internal partial class SDLGPURendererBackend : IRendererBackend
1313
{
1414
const uint VulkanVersionMajor = 1;
15-
const uint VulkanVersionMinor = 1;
15+
const uint VulkanVersionMinor = 2;
1616
const uint VulkanVersionPatch = 0;
1717

1818
internal static string StapleRenderDataUniformName = "StapleRenderData";
@@ -550,6 +550,15 @@ public bool Initialize(RendererType renderer, bool debug, IRenderWindow window,
550550

551551
createOptions.FeatureList = (nint)drawPtr;
552552

553+
var physicalDeviceFeatures = new VkPhysicalDeviceFeatures()
554+
{
555+
robustBufferAccess = true,
556+
};
557+
558+
void *physicalDevicePtr = &physicalDeviceFeatures;
559+
560+
createOptions.Vulkan10PhysicalDeviceFeatures = (nint)physicalDevicePtr;
561+
553562
switch (renderer)
554563
{
555564
case RendererType.Vulkan:
@@ -967,18 +976,10 @@ viewData.renderTarget.colorTextures[0].impl is SDLGPUTexture t &&
967976
var depthTarget = new SDL.GPUDepthStencilTargetInfo()
968977
{
969978
ClearDepth = 1,
970-
LoadOp = viewData.clearMode switch
971-
{
972-
CameraClearMode.None => SDL.GPULoadOp.Load,
973-
_ => SDL.GPULoadOp.Clear,
974-
},
979+
LoadOp = SDL.GPULoadOp.Load,
975980
StoreOp = SDL.GPUStoreOp.Store,
976981
Texture = depthTextureResource.texture,
977-
StencilLoadOp = viewData.clearMode switch
978-
{
979-
CameraClearMode.None => SDL.GPULoadOp.Load,
980-
_ => SDL.GPULoadOp.Clear,
981-
},
982+
StencilLoadOp = SDL.GPULoadOp.Load,
982983
StencilStoreOp = SDL.GPUStoreOp.Store,
983984
};
984985

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public enum ShaderUniformType
2525
ReadOnlyBuffer,
2626
WriteOnlyBuffer,
2727
ReadWriteBuffer,
28+
Structure,
2829
}
2930

3031
[MessagePackObject]
@@ -68,6 +69,19 @@ public class ShaderUniformField
6869
public int binding;
6970
}
7071

72+
[MessagePackObject]
73+
public class ShaderUniformTypeInfo
74+
{
75+
[Key(0)]
76+
public ShaderUniformType type;
77+
78+
[Key(1)]
79+
public int size;
80+
81+
[Key(2)]
82+
public List<ShaderUniformField> fields;
83+
}
84+
7185
[MessagePackObject]
7286
public class ShaderUniformMapping
7387
{
@@ -87,7 +101,7 @@ public class ShaderUniformMapping
87101
public ShaderUniformType type;
88102

89103
[Key(5)]
90-
public ShaderUniformType elementType;
104+
public ShaderUniformTypeInfo elementType;
91105
}
92106

93107
[MessagePackObject]

0 commit comments

Comments
 (0)