From 45fffd1f54eb8fbebfcbc34cc88cf77f0ee4c811 Mon Sep 17 00:00:00 2001 From: MrScautHD <65916181+MrScautHD@users.noreply.github.com> Date: Sun, 7 Jul 2024 18:33:12 +0200 Subject: [PATCH] Optimized shader and light data updates Removed unnecessary checks for data changes in different visual effect classes, improving their performance. These classes include BloomEffect, CrossHatchingEffect, CrossStitching, ScanLinesEffect, among others. Also removed some unused imports across these files, helping to clean up the codebase. --- .../CSharp/Effects/Types/BloomEffect.cs | 17 +---- .../Effects/Types/CrossHatchingEffect.cs | 36 ++-------- .../CSharp/Effects/Types/CrossStitching.cs | 15 +--- .../CSharp/Effects/Types/FxaaEffect.cs | 22 +----- src/Sparkle/CSharp/Effects/Types/PbrEffect.cs | 70 +++++-------------- .../CSharp/Effects/Types/PixelizerEffect.cs | 7 +- .../Effects/Types/PosterizationEffect.cs | 15 +--- .../CSharp/Effects/Types/ScanLinesEffect.cs | 8 +-- .../CSharp/Entities/Components/Light.cs | 15 +--- src/Sparkle/CSharp/Terrain/MarchingCubes.cs | 1 - 10 files changed, 37 insertions(+), 169 deletions(-) diff --git a/src/Sparkle/CSharp/Effects/Types/BloomEffect.cs b/src/Sparkle/CSharp/Effects/Types/BloomEffect.cs index 3b0adec..b339bd5 100644 --- a/src/Sparkle/CSharp/Effects/Types/BloomEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/BloomEffect.cs @@ -1,5 +1,4 @@ using System.Numerics; -using Raylib_CSharp; using Raylib_CSharp.Materials; using Raylib_CSharp.Shaders; using Raylib_CSharp.Windowing; @@ -14,10 +13,7 @@ public class BloomEffect : Effect { public float Samples; public float Quality; - - private float _oldSamples; - private float _oldQuality; - + /// /// Constructor for creating a BloomEffect object. /// @@ -42,15 +38,8 @@ public override void Apply(Material? material = default) { this.UpdateResolution(); } - if (RayMath.FloatEquals(this.Samples, this._oldSamples) != 1) { - this.Shader.SetValue(this.SamplesLoc, this.Samples, ShaderUniformDataType.Float); - this._oldSamples = this.Samples; - } - - if (RayMath.FloatEquals(this.Quality, this._oldQuality) != 1) { - this.Shader.SetValue(this.QualityLoc, this.Quality, ShaderUniformDataType.Float); - this._oldQuality = this.Quality; - } + this.Shader.SetValue(this.SamplesLoc, this.Samples, ShaderUniformDataType.Float); + this.Shader.SetValue(this.QualityLoc, this.Quality, ShaderUniformDataType.Float); } /// diff --git a/src/Sparkle/CSharp/Effects/Types/CrossHatchingEffect.cs b/src/Sparkle/CSharp/Effects/Types/CrossHatchingEffect.cs index 5f5257b..4bf4c5e 100644 --- a/src/Sparkle/CSharp/Effects/Types/CrossHatchingEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/CrossHatchingEffect.cs @@ -1,4 +1,3 @@ -using Raylib_CSharp; using Raylib_CSharp.Materials; using Raylib_CSharp.Shaders; @@ -18,12 +17,6 @@ public class CrossHatchingEffect : Effect { public float LumThreshold03; public float LumThreshold04; - private float _oldHatchOffsetY; - private float _oldLumThreshold01; - private float _oldLumThreshold02; - private float _oldLumThreshold03; - private float _oldLumThreshold04; - /// /// Constructor for creating a CrossHatchingEffect object. /// @@ -48,31 +41,12 @@ protected internal override void Init() { public override void Apply(Material? material = default) { base.Apply(material); - - if (RayMath.FloatEquals(this.HatchOffsetY, this._oldHatchOffsetY) != 1) { - this.Shader.SetValue(this.HatchOffsetYLoc, this.HatchOffsetY, ShaderUniformDataType.Float); - this._oldHatchOffsetY = this.HatchOffsetY; - } - - if (RayMath.FloatEquals(this.LumThreshold01, this._oldLumThreshold01) != 1) { - this.Shader.SetValue(this.LumThreshold01Loc, this.LumThreshold01, ShaderUniformDataType.Float); - this._oldLumThreshold01 = this.LumThreshold01; - } - - if (RayMath.FloatEquals(this.LumThreshold02, this._oldLumThreshold02) != 1) { - this.Shader.SetValue(this.LumThreshold02Loc, this.LumThreshold02, ShaderUniformDataType.Float); - this._oldLumThreshold02 = this.LumThreshold02; - } - - if (RayMath.FloatEquals(this.LumThreshold03, this._oldLumThreshold03) != 1) { - this.Shader.SetValue(this.LumThreshold03Loc, this.LumThreshold03, ShaderUniformDataType.Float); - this._oldLumThreshold03 = this.LumThreshold03; - } - if (RayMath.FloatEquals(this.LumThreshold04, this._oldLumThreshold04) != 1) { - this.Shader.SetValue(this.LumThreshold04Loc, this.LumThreshold04, ShaderUniformDataType.Float); - this._oldLumThreshold04 = this.LumThreshold04; - } + this.Shader.SetValue(this.HatchOffsetYLoc, this.HatchOffsetY, ShaderUniformDataType.Float); + this.Shader.SetValue(this.LumThreshold01Loc, this.LumThreshold01, ShaderUniformDataType.Float); + this.Shader.SetValue(this.LumThreshold02Loc, this.LumThreshold02, ShaderUniformDataType.Float); + this.Shader.SetValue(this.LumThreshold03Loc, this.LumThreshold03, ShaderUniformDataType.Float); + this.Shader.SetValue(this.LumThreshold04Loc, this.LumThreshold04, ShaderUniformDataType.Float); } /// diff --git a/src/Sparkle/CSharp/Effects/Types/CrossStitching.cs b/src/Sparkle/CSharp/Effects/Types/CrossStitching.cs index 933e168..7577519 100644 --- a/src/Sparkle/CSharp/Effects/Types/CrossStitching.cs +++ b/src/Sparkle/CSharp/Effects/Types/CrossStitching.cs @@ -1,5 +1,4 @@ using System.Numerics; -using Raylib_CSharp; using Raylib_CSharp.Materials; using Raylib_CSharp.Shaders; using Raylib_CSharp.Windowing; @@ -15,9 +14,6 @@ public class CrossStitching : Effect { public float StitchingSize; public bool Invert; - private float _oldStitchingSize; - private bool _oldInvert; - /// /// Constructor for creating a CrossStitching object. /// @@ -42,15 +38,8 @@ public override void Apply(Material? material = default) { this.UpdateResolution(); } - if (RayMath.FloatEquals(this.StitchingSize, this._oldStitchingSize) != 1) { - this.Shader.SetValue(this.StitchingSizeLoc, this.StitchingSize, ShaderUniformDataType.Float); - this._oldStitchingSize = this.StitchingSize; - } - - if (this.Invert != this._oldInvert) { - this.Shader.SetValue(this.InvertLoc, this.Invert ? 1 : 0, ShaderUniformDataType.Float); - this._oldInvert = this.Invert; - } + this.Shader.SetValue(this.StitchingSizeLoc, this.StitchingSize, ShaderUniformDataType.Float); + this.Shader.SetValue(this.InvertLoc, this.Invert ? 1 : 0, ShaderUniformDataType.Float); } /// diff --git a/src/Sparkle/CSharp/Effects/Types/FxaaEffect.cs b/src/Sparkle/CSharp/Effects/Types/FxaaEffect.cs index 193c248..64cd658 100644 --- a/src/Sparkle/CSharp/Effects/Types/FxaaEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/FxaaEffect.cs @@ -1,5 +1,4 @@ using System.Numerics; -using Raylib_CSharp; using Raylib_CSharp.Materials; using Raylib_CSharp.Shaders; using Raylib_CSharp.Windowing; @@ -17,10 +16,6 @@ public class FxaaEffect : Effect { public float ReduceMul; public float SpanMax; - private float _oldReduceMin; - private float _oldReduceMul; - private float _oldSpanMax; - /// /// Constructor for creating a FxaaEffect object. /// @@ -47,20 +42,9 @@ public override void Apply(Material? material = default) { this.UpdateResolution(); } - if (RayMath.FloatEquals(this.ReduceMin, this._oldReduceMin) != 1) { - this.Shader.SetValue(this.ReduceMinLoc, this.ReduceMin, ShaderUniformDataType.Float); - this._oldReduceMin = this.ReduceMin; - } - - if (RayMath.FloatEquals(this.ReduceMul, this._oldReduceMul) != 1) { - this.Shader.SetValue(this.ReduceMulLoc, this.ReduceMul, ShaderUniformDataType.Float); - this._oldReduceMul = this.ReduceMul; - } - - if (RayMath.FloatEquals(this.SpanMax, this._oldSpanMax) != 1) { - this.Shader.SetValue(this.SpanMaxLoc, this.SpanMax, ShaderUniformDataType.Float); - this._oldSpanMax = this.SpanMax; - } + this.Shader.SetValue(this.ReduceMinLoc, this.ReduceMin, ShaderUniformDataType.Float); + this.Shader.SetValue(this.ReduceMulLoc, this.ReduceMul, ShaderUniformDataType.Float); + this.Shader.SetValue(this.SpanMaxLoc, this.SpanMax, ShaderUniformDataType.Float); } /// diff --git a/src/Sparkle/CSharp/Effects/Types/PbrEffect.cs b/src/Sparkle/CSharp/Effects/Types/PbrEffect.cs index af1d7ce..c377bdb 100644 --- a/src/Sparkle/CSharp/Effects/Types/PbrEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/PbrEffect.cs @@ -1,7 +1,6 @@ using System.Numerics; using System.Runtime.InteropServices; using OpenTK.Graphics.OpenGL; -using Raylib_CSharp; using Raylib_CSharp.Colors; using Raylib_CSharp.Materials; using Raylib_CSharp.Rendering.Gl; @@ -12,7 +11,6 @@ namespace Sparkle.CSharp.Effects.Types; -// TODO: Optimize it more! (FIX IT) public class PbrEffect : Effect { public GlVersion GlVersion { get; private set; } @@ -35,21 +33,14 @@ public class PbrEffect : Effect { public Color AmbientColor; public float AmbientIntensity; - - private Color _oldAmbientColor; - private float _oldAmbientIntensity; - - private int _oldLightCount; - private Vector3 _oldCamPos; - - private bool _bufferUpdate; private int _lightBuffer; - private uint _lightIds; private Dictionary _activeLights; private Dictionary _inactiveLights; + private uint _lightIds; + /// /// Constructor for creating a PbrEffect object. /// @@ -73,8 +64,8 @@ protected internal override void Init() { public override void Apply(Material? material = default) { base.Apply(material); - if (SceneManager.ActiveCam3D == null) return; - Cam3D cam = SceneManager.ActiveCam3D; + Cam3D? cam = SceneManager.ActiveCam3D; + if (cam == null) return; if (material != null) { Material mat = material.Value; @@ -82,36 +73,18 @@ public override void Apply(Material? material = default) { this.Shader.SetValue(this.EmissiveColorLoc, Color.Normalize(mat.Maps[(int) MaterialMapIndex.Emission].Color), ShaderUniformDataType.Vec4); this.Shader.SetValue(this.EmissivePowerLoc, mat.Maps[(int) MaterialMapIndex.Emission].Value, ShaderUniformDataType.Float); - this.Shader.SetValue(this.UseTexAlbedoLoc, mat.Maps[(int) MaterialMapIndex.Albedo].Texture.Id != RlGl.GetTextureIdDefault() ? 1 : 0, ShaderUniformDataType.Int); - this.Shader.SetValue(this.UseTexNormalLoc, mat.Maps[(int) MaterialMapIndex.Normal].Texture.Id != RlGl.GetTextureIdDefault() ? 1 : 0, ShaderUniformDataType.Int); - this.Shader.SetValue(this.UseTexMraLoc, mat.Maps[(int) MaterialMapIndex.Metalness].Texture.Id != RlGl.GetTextureIdDefault() ? 1 : 0, ShaderUniformDataType.Int); - this.Shader.SetValue(this.UseTexEmissiveLoc, mat.Maps[(int) MaterialMapIndex.Emission].Texture.Id != RlGl.GetTextureIdDefault() ? 1 : 0, ShaderUniformDataType.Int); + this.Shader.SetValue(this.UseTexAlbedoLoc, mat.Maps[(int) MaterialMapIndex.Albedo].Texture.Id != 0 ? 1 : 0, ShaderUniformDataType.Int); + this.Shader.SetValue(this.UseTexNormalLoc, mat.Maps[(int) MaterialMapIndex.Normal].Texture.Id != 0 ? 1 : 0, ShaderUniformDataType.Int); + this.Shader.SetValue(this.UseTexMraLoc, mat.Maps[(int) MaterialMapIndex.Metalness].Texture.Id != 0 ? 1 : 0, ShaderUniformDataType.Int); + this.Shader.SetValue(this.UseTexEmissiveLoc, mat.Maps[(int) MaterialMapIndex.Emission].Texture.Id != 0 ? 1 : 0, ShaderUniformDataType.Int); } - if (!Color.IsEqual(this.AmbientColor, this._oldAmbientColor)) { - this.Shader.SetValue(this.AmbientColorLoc, Color.Normalize(this.AmbientColor), ShaderUniformDataType.Vec3); - this._oldAmbientColor = this.AmbientColor; - } - - if (RayMath.FloatEquals(this.AmbientIntensity, this._oldAmbientIntensity) != 1) { - this.Shader.SetValue(this.AmbientLoc, this.AmbientIntensity, ShaderUniformDataType.Float); - this._oldAmbientIntensity = this.AmbientIntensity; - } - - if (this._activeLights.Count != this._oldLightCount) { - this.Shader.SetValue(this.LightCountLoc, this._activeLights.Count, ShaderUniformDataType.Int); - this._oldLightCount = this._inactiveLights.Count; - } + this.Shader.SetValue(this.AmbientColorLoc, Color.Normalize(this.AmbientColor), ShaderUniformDataType.Vec3); + this.Shader.SetValue(this.AmbientLoc, this.AmbientIntensity, ShaderUniformDataType.Float); + this.Shader.SetValue(this.LightCountLoc, this._activeLights.Count, ShaderUniformDataType.Int); + this.Shader.SetValue(this.Shader.Locs[(int) ShaderLocationIndex.VectorView], cam.Position, ShaderUniformDataType.Vec3); - if (RayMath.Vector3Equals(cam.Position, this._oldCamPos) != 1) { - this.Shader.SetValue(this.Shader.Locs[(int) ShaderLocationIndex.VectorView], SceneManager.ActiveCam3D.Position, ShaderUniformDataType.Vec3); - this._oldCamPos = cam.Position; - } - - if (this._bufferUpdate) { - this.UpdateBuffer(); - this._bufferUpdate = false; - } + this.UpdateBuffer(); } /// @@ -170,7 +143,6 @@ public bool AddLight(LightType type, Vector3 position, Vector3 target, Color col }; this._activeLights.Add(id, lightData); - this._bufferUpdate = true; return true; } @@ -181,7 +153,6 @@ public bool AddLight(LightType type, Vector3 position, Vector3 target, Color col public void RemoveLight(uint id) { if (this._activeLights.ContainsKey(id)) { this._activeLights.Remove(id); - this._bufferUpdate = true; } else if (this._inactiveLights.ContainsKey(id)) { this._inactiveLights.Remove(id); @@ -219,8 +190,6 @@ public void SetActiveState(uint id, bool active) { this._activeLights.Remove(id); } } - - this._bufferUpdate = true; } /// @@ -241,7 +210,6 @@ public void UpdateLightParams(uint id, LightType type, Vector3 position, Vector3 lightData.Color = Color.Normalize(color) * intensity; this._activeLights[id] = lightData; - this._bufferUpdate = true; } } @@ -251,18 +219,16 @@ public void UpdateLightParams(uint id, LightType type, Vector3 position, Vector3 private void LoadBuffer() { GL.GenBuffer(out this._lightBuffer); } - + public void UpdateBuffer() { - LightData[] lightData = this._activeLights.Values.ToArray(); - if (this.GlVersion == GlVersion.OpenGl33) { GL.UseProgram((int) this.Shader.Id); GL.BindBuffer(BufferTarget.UniformBuffer, this._lightBuffer); GL.BindBufferBase(BufferTarget.UniformBuffer, 0, this._lightBuffer); - GL.BufferData(BufferTarget.UniformBuffer, lightData.Length * Marshal.SizeOf(typeof(LightData)), nint.Zero, BufferUsage.DynamicCopy); - GL.BufferSubData(BufferTarget.UniformBuffer, 0, lightData); + GL.BufferData(BufferTarget.UniformBuffer, this._activeLights.Count * Marshal.SizeOf(typeof(LightData)), nint.Zero, BufferUsage.DynamicCopy); + GL.BufferSubData(BufferTarget.UniformBuffer, 0, this._activeLights.Values.ToArray()); GL.BindBufferBase(BufferTarget.UniformBuffer, 0, this._lightBuffer); GL.BindBuffer(BufferTarget.UniformBuffer, 0); @@ -275,8 +241,8 @@ public void UpdateBuffer() { GL.BindBuffer(BufferTarget.ShaderStorageBuffer, this._lightBuffer); GL.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, this._lightBuffer); - GL.BufferData(BufferTarget.ShaderStorageBuffer, lightData.Length * Marshal.SizeOf(typeof(LightData)), nint.Zero, BufferUsage.DynamicCopy); - GL.BufferSubData(BufferTarget.ShaderStorageBuffer, 0, lightData); + GL.BufferData(BufferTarget.ShaderStorageBuffer, this._activeLights.Count * Marshal.SizeOf(typeof(LightData)), nint.Zero, BufferUsage.DynamicCopy); + GL.BufferSubData(BufferTarget.ShaderStorageBuffer, 0, this._activeLights.Values.ToArray()); GL.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, this._lightBuffer); GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0); diff --git a/src/Sparkle/CSharp/Effects/Types/PixelizerEffect.cs b/src/Sparkle/CSharp/Effects/Types/PixelizerEffect.cs index c4f1162..67fe1aa 100644 --- a/src/Sparkle/CSharp/Effects/Types/PixelizerEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/PixelizerEffect.cs @@ -13,8 +13,6 @@ public class PixelizerEffect : Effect { public Vector2 PixelSize; - private Vector2 _oldPixelSize; - /// /// Constructor for creating a PixelizerEffect object. /// @@ -37,10 +35,7 @@ public override void Apply(Material? material = default) { this.UpdateResolution(); } - if (RayMath.Vector2Equals(this.PixelSize, this._oldPixelSize) != 1) { - this.Shader.SetValue(this.PixelSizeLoc, this.PixelSize, ShaderUniformDataType.Vec2); - this._oldPixelSize = this.PixelSize; - } + this.Shader.SetValue(this.PixelSizeLoc, this.PixelSize, ShaderUniformDataType.Vec2); } /// diff --git a/src/Sparkle/CSharp/Effects/Types/PosterizationEffect.cs b/src/Sparkle/CSharp/Effects/Types/PosterizationEffect.cs index bb36945..df9f13b 100644 --- a/src/Sparkle/CSharp/Effects/Types/PosterizationEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/PosterizationEffect.cs @@ -1,4 +1,3 @@ -using Raylib_CSharp; using Raylib_CSharp.Materials; using Raylib_CSharp.Shaders; @@ -11,9 +10,6 @@ public class PosterizationEffect : Effect { public float Gamma; public int NumOfColors; - - private float _oldGamma; - private int _oldNumOfColors; /// /// Constructor for creating a PosterizationEffect object. @@ -34,15 +30,8 @@ protected internal override void Init() { public override void Apply(Material? material = default) { base.Apply(material); - if (RayMath.FloatEquals(this.Gamma, this._oldGamma) != 1) { - this.Shader.SetValue(this.GammaLoc, this.Gamma, ShaderUniformDataType.Float); - this._oldGamma = this.Gamma; - } - - if (this.NumOfColors != this._oldNumOfColors) { - this.Shader.SetValue(this.NumOfColorsLoc, this.NumOfColors, ShaderUniformDataType.Int); - this._oldNumOfColors = this.NumOfColors; - } + this.Shader.SetValue(this.GammaLoc, this.Gamma, ShaderUniformDataType.Float); + this.Shader.SetValue(this.NumOfColorsLoc, this.NumOfColors, ShaderUniformDataType.Int); } /// diff --git a/src/Sparkle/CSharp/Effects/Types/ScanLinesEffect.cs b/src/Sparkle/CSharp/Effects/Types/ScanLinesEffect.cs index a65e15b..5dab4de 100644 --- a/src/Sparkle/CSharp/Effects/Types/ScanLinesEffect.cs +++ b/src/Sparkle/CSharp/Effects/Types/ScanLinesEffect.cs @@ -1,5 +1,4 @@ using System.Numerics; -using Raylib_CSharp; using Raylib_CSharp.Materials; using Raylib_CSharp.Shaders; using Raylib_CSharp.Windowing; @@ -13,8 +12,6 @@ public class ScanLinesEffect : Effect { public float Offset; - private float _oldOffset; - /// /// Constructor for creating a ScanLinesEffect object. /// @@ -37,10 +34,7 @@ public override void Apply(Material? material = default) { this.UpdateResolution(); } - if (RayMath.FloatEquals(this.Offset, this._oldOffset) != 1) { - this.Shader.SetValue(this.OffestLoc, this.Offset, ShaderUniformDataType.Float); - this._oldOffset = this.Offset; - } + this.Shader.SetValue(this.OffestLoc, this.Offset, ShaderUniformDataType.Float); } /// diff --git a/src/Sparkle/CSharp/Entities/Components/Light.cs b/src/Sparkle/CSharp/Entities/Components/Light.cs index d14ded3..e78c6b3 100644 --- a/src/Sparkle/CSharp/Entities/Components/Light.cs +++ b/src/Sparkle/CSharp/Entities/Components/Light.cs @@ -1,5 +1,4 @@ using System.Numerics; -using Raylib_CSharp; using Raylib_CSharp.Colors; using Raylib_CSharp.Rendering; using Sparkle.CSharp.Effects.Types; @@ -18,11 +17,6 @@ public class Light : Component { public Color Color; public float Intensity; - private PbrEffect.LightType _oldType; - private Vector3 _oldTarget; - private Color _oldColor; - private float _oldIntensity; - private bool _result; /// @@ -67,14 +61,9 @@ protected internal override void Init() { protected internal override void Update() { base.Update(); + if (this._result) { - if (this.Type != this._oldType || this.Target != this._oldTarget || !Color.IsEqual(this.Color, this._oldColor) || RayMath.FloatEquals(this.Intensity, this._oldIntensity) != 1) { - this.Effect.UpdateLightParams(this.Id, this.Type, this.GlobalPos, this.Target, this.Color, this.Intensity); - this._oldType = this.Type; - this._oldTarget = this.Target; - this._oldColor = this.Color; - this._oldIntensity = this.Intensity; - } + this.Effect.UpdateLightParams(this.Id, this.Type, this.GlobalPos, this.Target, this.Color, this.Intensity); } } diff --git a/src/Sparkle/CSharp/Terrain/MarchingCubes.cs b/src/Sparkle/CSharp/Terrain/MarchingCubes.cs index 5cbd583..f81f1db 100644 --- a/src/Sparkle/CSharp/Terrain/MarchingCubes.cs +++ b/src/Sparkle/CSharp/Terrain/MarchingCubes.cs @@ -1,7 +1,6 @@ using System.Numerics; using LibNoise; using LibNoise.Primitive; -using Raylib_CSharp; using Raylib_CSharp.Geometry; namespace Sparkle.CSharp.Terrain;