Skip to content

Commit

Permalink
Optimized shader and light data updates
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MrScautHD committed Jul 7, 2024
1 parent 723ab00 commit 45fffd1
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 169 deletions.
17 changes: 3 additions & 14 deletions src/Sparkle/CSharp/Effects/Types/BloomEffect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Numerics;
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;
Expand All @@ -14,10 +13,7 @@ public class BloomEffect : Effect {

public float Samples;
public float Quality;

private float _oldSamples;
private float _oldQuality;


/// <summary>
/// Constructor for creating a BloomEffect object.
/// </summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
36 changes: 5 additions & 31 deletions src/Sparkle/CSharp/Effects/Types/CrossHatchingEffect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;

Expand All @@ -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;

/// <summary>
/// Constructor for creating a CrossHatchingEffect object.
/// </summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
15 changes: 2 additions & 13 deletions src/Sparkle/CSharp/Effects/Types/CrossStitching.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Numerics;
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;
Expand All @@ -15,9 +14,6 @@ public class CrossStitching : Effect {
public float StitchingSize;
public bool Invert;

private float _oldStitchingSize;
private bool _oldInvert;

/// <summary>
/// Constructor for creating a CrossStitching object.
/// </summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
22 changes: 3 additions & 19 deletions src/Sparkle/CSharp/Effects/Types/FxaaEffect.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Numerics;
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;
Expand All @@ -17,10 +16,6 @@ public class FxaaEffect : Effect {
public float ReduceMul;
public float SpanMax;

private float _oldReduceMin;
private float _oldReduceMul;
private float _oldSpanMax;

/// <summary>
/// Constructor for creating a FxaaEffect object.
/// </summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
70 changes: 18 additions & 52 deletions src/Sparkle/CSharp/Effects/Types/PbrEffect.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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; }
Expand All @@ -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<uint, LightData> _activeLights;
private Dictionary<uint, LightData> _inactiveLights;

private uint _lightIds;

/// <summary>
/// Constructor for creating a PbrEffect object.
/// </summary>
Expand All @@ -73,45 +64,27 @@ 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;
this.Shader.SetValue(this.TilingLoc, new Vector2(mat.Param[0], mat.Param[1]), ShaderUniformDataType.Vec2);
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();
}

/// <summary>
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -219,8 +190,6 @@ public void SetActiveState(uint id, bool active) {
this._activeLights.Remove(id);
}
}

this._bufferUpdate = true;
}

/// <summary>
Expand All @@ -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;
}
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions src/Sparkle/CSharp/Effects/Types/PixelizerEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class PixelizerEffect : Effect {

public Vector2 PixelSize;

private Vector2 _oldPixelSize;

/// <summary>
/// Constructor for creating a PixelizerEffect object.
/// </summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
15 changes: 2 additions & 13 deletions src/Sparkle/CSharp/Effects/Types/PosterizationEffect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;

Expand All @@ -11,9 +10,6 @@ public class PosterizationEffect : Effect {

public float Gamma;
public int NumOfColors;

private float _oldGamma;
private int _oldNumOfColors;

/// <summary>
/// Constructor for creating a PosterizationEffect object.
Expand All @@ -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);
}

/// <summary>
Expand Down
Loading

0 comments on commit 45fffd1

Please sign in to comment.