Skip to content

Commit a44fd3a

Browse files
[Rendering] Minor tweaks to shaders and render state;
1 parent d4cba49 commit a44fd3a

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal struct RenderState
1818
public InstanceBuffer instanceBuffer;
1919
public (int, VertexBuffer)[] readOnlyBuffers;
2020
public (int, VertexBuffer)[] readWriteBuffers;
21+
public (int, VertexBuffer)[] writeOnlyBuffers;
2122
public int startVertex;
2223
public int startIndex;
2324
public int indexCount;
@@ -60,21 +61,29 @@ internal readonly int StateKey
6061
}
6162
}
6263

64+
if (writeOnlyBuffers != null)
65+
{
66+
foreach (var t in writeOnlyBuffers)
67+
{
68+
hashCode.Add(t);
69+
}
70+
}
71+
6372
if (textures != null)
6473
{
6574
foreach(var t in textures)
6675
{
67-
hashCode.Add(t.GetHashCode());
76+
hashCode.Add(t);
6877
}
6978
}
7079

71-
if(renderTarget != null && renderTarget.Disposed == false)
80+
if((renderTarget?.Disposed ?? true) == false)
7281
{
7382
for(var i = 0; i < renderTarget.ColorTextureCount; i++)
7483
{
7584
var texture = renderTarget.GetColorTexture(i);
7685

77-
if(texture == null || texture.Disposed || texture.impl is not SDLGPUTexture t)
86+
if((texture?.Disposed ?? true) || texture.impl is not SDLGPUTexture t)
7887
{
7988
continue;
8089
}
@@ -83,8 +92,7 @@ internal readonly int StateKey
8392
}
8493

8594
{
86-
if (renderTarget.DepthTexture != null &&
87-
renderTarget.DepthTexture.Disposed == false &&
95+
if ((renderTarget.DepthTexture?.Disposed ?? true) == false &&
8896
renderTarget.DepthTexture.impl is SDLGPUTexture t)
8997
{
9098
hashCode.Add(t.handle);

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Drawing;
34
using System.Linq;
45
using System.Numerics;
56
using System.Runtime.CompilerServices;
@@ -374,9 +375,26 @@ internal static bool CanStoreUniformData((int, byte[]) data, int size)
374375
return data.Item1 >= 0 && data.Item1 + size <= data.Item2.Length;
375376
}
376377

378+
private static void SetValueInternal(Span<byte> source, (int, byte[])? vertexData, (int, byte[])? fragmentData)
379+
{
380+
if (vertexData != null && CanStoreUniformData(vertexData.Value, source.Length))
381+
{
382+
var target = new Span<byte>(vertexData.Value.Item2, vertexData.Value.Item1, source.Length);
383+
384+
source.CopyTo(target);
385+
}
386+
387+
if (fragmentData != null && CanStoreUniformData(fragmentData.Value, source.Length))
388+
{
389+
var target = new Span<byte>(fragmentData.Value.Item2, fragmentData.Value.Item1, source.Length);
390+
391+
source.CopyTo(target);
392+
}
393+
}
394+
377395
private void SetValue<T>(string variantKey, ShaderHandle handle, T value) where T: unmanaged
378396
{
379-
if (TryGetUniformData(variantKey, handle, out var uniform, out var vertexData, out var fragmentData) == false)
397+
if (TryGetUniformData(variantKey, handle, out _, out var vertexData, out var fragmentData) == false)
380398
{
381399
return;
382400
}
@@ -387,19 +405,7 @@ private void SetValue<T>(string variantKey, ShaderHandle handle, T value) where
387405
{
388406
var source = new Span<byte>(&value, size);
389407

390-
if (vertexData != null && CanStoreUniformData(vertexData.Value, size))
391-
{
392-
var target = new Span<byte>(vertexData.Value.Item2, vertexData.Value.Item1, size);
393-
394-
source.CopyTo(target);
395-
}
396-
397-
if (fragmentData != null && CanStoreUniformData(fragmentData.Value, size))
398-
{
399-
var target = new Span<byte>(fragmentData.Value.Item2, fragmentData.Value.Item1, size);
400-
401-
source.CopyTo(target);
402-
}
408+
SetValueInternal(source, vertexData, fragmentData);
403409
}
404410
}
405411

@@ -418,19 +424,7 @@ private void SetValue<T>(string variantKey, ShaderHandle handle, ReadOnlySpan<T>
418424
var size = Marshal.SizeOf<T>() * count;
419425
var source = new Span<byte>(ptr, size);
420426

421-
if (vertexData != null && CanStoreUniformData(vertexData.Value, size))
422-
{
423-
var target = new Span<byte>(vertexData.Value.Item2, vertexData.Value.Item1, size);
424-
425-
source.CopyTo(target);
426-
}
427-
428-
if (fragmentData != null && CanStoreUniformData(fragmentData.Value, size))
429-
{
430-
var target = new Span<byte>(fragmentData.Value.Item2, fragmentData.Value.Item1, size);
431-
432-
source.CopyTo(target);
433-
}
427+
SetValueInternal(source, vertexData, fragmentData);
434428
}
435429
}
436430
}

0 commit comments

Comments
 (0)