Skip to content

Commit 33ffb9c

Browse files
[Core] Removed IntLookupCache;
1 parent b8ce726 commit 33ffb9c

File tree

5 files changed

+32
-108
lines changed

5 files changed

+32
-108
lines changed

Engine/Core/Rendering/Shader/Shader.cs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ internal class ShaderInstance
100100

101101
internal readonly Dictionary<string, ShaderInstance> instances = [];
102102

103-
private UniformInfo[] uniforms = [];
104-
private readonly IntLookupCache<int> uniformIndices = new();
103+
private readonly Dictionary<int, UniformInfo> uniforms = [];
105104

106105
private int usedTextureStages = 0;
107106

@@ -221,11 +220,11 @@ internal unsafe bool Create()
221220
}
222221
}
223222

224-
if (uniforms.Length > 0)
223+
if (uniforms.Count > 0)
225224
{
226-
foreach (var uniform in uniforms)
225+
foreach (var pair in uniforms)
227226
{
228-
uniform.Create();
227+
pair.Value.Create();
229228
}
230229
}
231230
else
@@ -262,16 +261,7 @@ internal void AddUniform(string name, ShaderUniformType type)
262261
var nameHash = name.GetHashCode();
263262
var normalizedHash = normalizedName.GetHashCode();
264263

265-
var uniformIndex = uniformIndices.IndexOf(nameHash);
266-
267-
if(uniformIndex >= 0)
268-
{
269-
return;
270-
}
271-
272-
uniformIndex = uniformIndices.IndexOf(normalizedHash);
273-
274-
if (uniformIndex >= 0)
264+
if(uniforms.ContainsKey(nameHash) || uniforms.ContainsKey(normalizedHash))
275265
{
276266
return;
277267
}
@@ -295,16 +285,11 @@ internal void AddUniform(string name, ShaderUniformType type)
295285
usedTextureStages++;
296286
}
297287

298-
var i = uniforms.Length;
288+
uniforms.Add(normalizedHash, u);
299289

300-
uniformIndices.Add(normalizedHash, i);
301-
uniforms = uniforms.Concat([u]).ToArray();
302-
303-
if (uniformIndices.IndexOf(nameHash) < 0)
290+
if (uniforms.ContainsKey(nameHash) == false)
304291
{
305-
uniformIndices.Add(nameHash, i);
306-
307-
uniforms = uniforms.Concat([new()
292+
uniforms.Add(nameHash, new()
308293
{
309294
count = u.count,
310295
isAlias = true,
@@ -315,7 +300,7 @@ internal void AddUniform(string name, ShaderUniformType type)
315300
name = u.uniform.name,
316301
type = type,
317302
},
318-
}]).ToArray();
303+
});
319304
}
320305
}
321306
}
@@ -335,14 +320,12 @@ internal bgfx.StateFlags BlendingFlag
335320

336321
internal UniformInfo GetUniform(int hash)
337322
{
338-
if (Disposed)
323+
if (Disposed || uniforms.TryGetValue(hash, out var uniform) == false)
339324
{
340325
return null;
341326
}
342327

343-
var index = uniformIndices.IndexOf(hash);
344-
345-
return index >= 0 ? uniforms[uniformIndices[index]] : null;
328+
return uniform;
346329
}
347330

348331
internal ShaderHandle GetUniformHandle(int hash)
@@ -351,7 +334,7 @@ internal ShaderHandle GetUniformHandle(int hash)
351334

352335
if(uniform == null)
353336
{
354-
return null;
337+
return default;
355338
}
356339

357340
return new(uniform);
@@ -369,7 +352,7 @@ public void SetFloat(ShaderHandle handle, float value)
369352
return;
370353
}
371354

372-
var uniform = handle?.uniform;
355+
var uniform = handle.uniform;
373356

374357
if (uniform == null)
375358
{
@@ -396,7 +379,7 @@ public void SetVector2(ShaderHandle handle, Vector2 value)
396379
return;
397380
}
398381

399-
var uniform = handle?.uniform;
382+
var uniform = handle.uniform;
400383

401384
if (uniform == null)
402385
{
@@ -423,7 +406,7 @@ public void SetVector2(ShaderHandle handle, ReadOnlySpan<Vector2> value)
423406
return;
424407
}
425408

426-
var uniform = handle?.uniform;
409+
var uniform = handle.uniform;
427410

428411
if (uniform == null)
429412
{
@@ -458,7 +441,7 @@ public void SetVector3(ShaderHandle handle, Vector3 value)
458441
return;
459442
}
460443

461-
var uniform = handle?.uniform;
444+
var uniform = handle.uniform;
462445

463446
if (uniform == null)
464447
{
@@ -485,7 +468,7 @@ public void SetVector3(ShaderHandle handle, ReadOnlySpan<Vector3> value)
485468
return;
486469
}
487470

488-
var uniform = handle?.uniform;
471+
var uniform = handle.uniform;
489472

490473
if (uniform == null)
491474
{
@@ -520,7 +503,7 @@ public void SetVector4(ShaderHandle handle, Vector4 value)
520503
return;
521504
}
522505

523-
var uniform = handle?.uniform;
506+
var uniform = handle.uniform;
524507

525508
if (uniform == null)
526509
{
@@ -545,7 +528,7 @@ public void SetVector4(ShaderHandle handle, ReadOnlySpan<Vector4> value)
545528
return;
546529
}
547530

548-
var uniform = handle?.uniform;
531+
var uniform = handle.uniform;
549532

550533
if (uniform == null)
551534
{
@@ -573,7 +556,7 @@ public void SetColor(ShaderHandle handle, Color value)
573556
return;
574557
}
575558

576-
var uniform = handle?.uniform;
559+
var uniform = handle.uniform;
577560

578561
if (uniform == null)
579562
{
@@ -600,7 +583,7 @@ public void SetColor(ShaderHandle handle, ReadOnlySpan<Color> value)
600583
return;
601584
}
602585

603-
var uniform = handle?.uniform;
586+
var uniform = handle.uniform;
604587

605588
if (uniform == null)
606589
{
@@ -629,7 +612,7 @@ public void SetTexture(ShaderHandle handle, Texture value, TextureFlags override
629612
return;
630613
}
631614

632-
var uniform = handle?.uniform;
615+
var uniform = handle.uniform;
633616

634617
if (uniform == null)
635618
{
@@ -654,7 +637,7 @@ public void SetMatrix3x3(ShaderHandle handle, Matrix3x3 value)
654637
return;
655638
}
656639

657-
var uniform = handle?.uniform;
640+
var uniform = handle.uniform;
658641

659642
if (uniform == null)
660643
{
@@ -679,7 +662,7 @@ public void SetMatrix3x3(ShaderHandle handle, ReadOnlySpan<Matrix3x3> value)
679662
return;
680663
}
681664

682-
var uniform = handle?.uniform;
665+
var uniform = handle.uniform;
683666

684667
if (uniform == null)
685668
{
@@ -707,7 +690,7 @@ public void SetMatrix4x4(ShaderHandle handle, Matrix4x4 value)
707690
return;
708691
}
709692

710-
var uniform = handle?.uniform;
693+
var uniform = handle.uniform;
711694

712695
if (uniform == null)
713696
{
@@ -732,7 +715,7 @@ public void SetMatrix4x4(ShaderHandle handle, ReadOnlySpan<Matrix4x4> value)
732715
return;
733716
}
734717

735-
var uniform = handle?.uniform;
718+
var uniform = handle.uniform;
736719

737720
if (uniform == null)
738721
{
@@ -773,8 +756,10 @@ internal void Destroy()
773756
}
774757
}
775758

776-
foreach (var uniform in uniforms)
759+
foreach (var pair in uniforms)
777760
{
761+
var uniform = pair.Value;
762+
778763
if(uniform.isAlias)
779764
{
780765
continue;

Engine/Core/Rendering/Shader/ShaderHandle.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@
55
/// Direct access to a shader uniform, used for caching.
66
/// </summary>
77
/// <param name="uniform">The uniform to store</param>
8-
internal class ShaderHandle(Shader.UniformInfo uniform)
9-
{
10-
public readonly Shader.UniformInfo uniform = uniform;
11-
}
8+
internal readonly record struct ShaderHandle(Shader.UniformInfo uniform);

Engine/Core/StapleCore.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@
322322
<Compile Include="Utilities\DictionaryExtensions.cs" />
323323
<Compile Include="Utilities\ExpandableContainer.cs" />
324324
<Compile Include="Utilities\GuidGenerator.cs" />
325-
<Compile Include="Utilities\IntLookupCache.cs" />
326325
<Compile Include="Utilities\ObjectCreation.cs" />
327326
<Compile Include="Utilities\Observables\IObservableBox.cs" />
328327
<Compile Include="Utilities\Observables\ObservableBox.cs" />

Engine/Core/Utilities/IntLookupCache.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

Engine/Editor/StapleEditor+Render.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ static void ExecuteBlock(object source, Action execute)
189189
{
190190
renderable.isVisible = renderable.enabled && renderable.forceRenderingOff == false;
191191

192+
/*
192193
if(renderable.isVisible)
193194
{
194195
renderable.isVisible = renderable.isVisible && frustumCuller.AABBTest(renderable.bounds) != FrustumAABBResult.Invisible;
195196
}
197+
*/
196198

197199
ReplaceEntityBodyIfNeeded(entity, transform, renderable.localBounds);
198200
}

0 commit comments

Comments
 (0)