Skip to content

Commit

Permalink
Cleanup a little, remove defines
Browse files Browse the repository at this point in the history
  • Loading branch information
kristiker committed May 25, 2024
1 parent 4169b71 commit a8e047a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 78 deletions.
40 changes: 10 additions & 30 deletions GUI/Types/Renderer/PostProcessRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,19 @@ internal class PostProcessRenderer
{
private VrfGuiContext guiContext;
private int vao;
private Shader shader;

public RenderTexture BlueNoise;

// To prevent shader compilation stutter, we must keep both shader combinations loaded [Richard Leadbetter voice]
private Shader shaderNoLUT;
private Shader shaderLUT;

private readonly Random random = new();

public PostProcessRenderer(VrfGuiContext guiContext)
{
this.guiContext = guiContext;
}

public void Load()
{
// does number of samples ever change?
var NoLutArguments = new Dictionary<string, byte>();
var LUTArguments = new Dictionary<string, byte> { { "F_COLOR_CORRECTION_LUT", 1 } };
shaderNoLUT = guiContext.ShaderLoader.LoadShader("vrf.post_process", NoLutArguments);
shaderLUT = guiContext.ShaderLoader.LoadShader("vrf.post_process", LUTArguments);


shader = guiContext.ShaderLoader.LoadShader("vrf.post_process");
GL.CreateVertexArrays(1, out vao);
}

Expand All @@ -55,36 +46,25 @@ private void SetPostProcessUniforms(Shader shader, TonemapSettings TonemapSettin
// we should have a shared FullscreenQuadRenderer class
public void Render(PostProcessState postProcessState, Framebuffer colorBuffer, float tonemapScalar)
{
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

GL.Disable(EnableCap.Blend);
GL.Disable(EnableCap.CullFace);
GL.Disable(EnableCap.DepthTest);

var usesLut = (postProcessState.NumLutsActive > 0);

var shader = usesLut ? shaderLUT : shaderNoLUT;

GL.UseProgram(shader.Program);

// Bind textures
shader.SetTexture(0, "g_tColorBuffer", colorBuffer.Color);
shader.SetTexture(1, "g_tBlueNoise", BlueNoise);
if (usesLut)
{
// use to debug handle
shader.SetTexture(2, "g_tColorCorrection", postProcessState.ColorCorrectionLUT);
shader.SetUniform1("g_flColorCorrectionDefaultWeight", postProcessState.ColorCorrectionWeight);

var invDimensions = (1.0f / postProcessState.ColorCorrectionLutDimensions);
var invRange = new Vector2(1.0f - invDimensions, 0.5f * invDimensions);
shader.SetUniform2("g_vColorCorrectionColorRange", invRange);
}
shader.SetTexture(1, "g_tColorCorrection", postProcessState.ColorCorrectionLUT ?? guiContext.MaterialLoader.GetErrorTexture()); // todo: error postprocess texture
shader.SetTexture(2, "g_tBlueNoise", BlueNoise);

shader.SetUniform1("g_flToneMapScalarLinear", tonemapScalar);

SetPostProcessUniforms(shader, postProcessState.TonemapSettings);

var invDimensions = 1.0f / postProcessState.ColorCorrectionLutDimensions;
var invRange = new Vector2(1.0f - invDimensions, 0.5f * invDimensions);
shader.SetUniform2("g_vColorCorrectionColorRange", invRange);
shader.SetUniform1("g_flColorCorrectionDefaultWeight", (postProcessState.NumLutsActive > 0) ? postProcessState.ColorCorrectionWeight : 0f);

GL.BindVertexArray(vao);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);

Expand Down
16 changes: 8 additions & 8 deletions GUI/Types/Renderer/ScenePostProcessVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct PostProcessState()
public int ColorCorrectionLutDimensions { get; set; } = 32;
public int NumLutsActive { get; set; }
};

class SceneTonemapController : SceneNode
{
public ExposureSettings ControllerExposureSettings { get; set; }
Expand Down Expand Up @@ -169,7 +170,7 @@ class ScenePostProcessVolume : SceneNode
// Additionally, due to collision detection being An Absolute Pain, we can't
// use local post process volumes, and can only use the master.

//
//
public void LoadPostProcessResource(PostProcessing resource)
{
PostProcessingResource = resource;
Expand All @@ -188,16 +189,15 @@ public void LoadPostProcessResource(PostProcessing resource)
// Create color correction texture from raw data
if (resource.HasColorCorrection())
{
var dimensions = resource.GetColorCorrectionLUTDimension();
var data = resource.GetColorCorrectionLUT().Clone() as byte[];
var bytesPerPixel = 4; // currently a constant 4 byte per pixel, one per channel
var resolution = resource.GetColorCorrectionLUTDimension();
var data = resource.GetColorCorrectionLUT();

ColorCorrectionLutDimensions = dimensions;
ColorCorrectionLutDimensions = resolution;

ColorCorrectionLUT = new RenderTexture(TextureTarget.Texture3D, dimensions, dimensions, dimensions, 1);
ColorCorrectionLUT = new RenderTexture(TextureTarget.Texture3D, resolution, resolution, resolution, 1);
ColorCorrectionLUT.SetWrapMode(TextureWrapMode.ClampToEdge);
ColorCorrectionLUT.SetFiltering(TextureMinFilter.Linear, TextureMagFilter.Linear);
GL.TextureStorage3D(ColorCorrectionLUT.Handle, 1, SizedInternalFormat.Rgba8, dimensions, dimensions, dimensions);
GL.TextureStorage3D(ColorCorrectionLUT.Handle, 1, SizedInternalFormat.Rgba8, resolution, resolution, resolution);
// COLOR CORRECTION DEBUG
#if false
Log.Info(nameof(ScenePostProcessVolume), "CREATING COLOR CORRECTION TEXTURE");
Expand Down Expand Up @@ -225,7 +225,7 @@ public void LoadPostProcessResource(PostProcessing resource)
}
#endif

GL.TextureSubImage3D(ColorCorrectionLUT.Handle, 0, 0, 0, 0, dimensions, dimensions, dimensions, PixelFormat.Rgba, PixelType.UnsignedByte, data);
GL.TextureSubImage3D(ColorCorrectionLUT.Handle, 0, 0, 0, 0, resolution, resolution, resolution, PixelFormat.Rgba, PixelType.UnsignedByte, data);
}
}

Expand Down
60 changes: 22 additions & 38 deletions GUI/Types/Renderer/Shaders/post_processing.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

#include "common/utils.glsl"

//#define F_OUTPUT_LINEAR 0
#define F_COLOR_CORRECTION_LUT 0
#define F_HAS_BLUE_NOISE 0
#define F_IS_POST_HLA 0
//#define F_BLOOM 0
//#define F_OLD_TONEMAPPING_CURVE 0 // HLA, linear only. Used in linear viewmodes to disable the tonemapping curve

Expand All @@ -15,20 +12,6 @@
//uniform vec3 g_vNormalizedBloomStrengths;
//uniform vec3 g_vUnNormalizedBloomStrengths;




layout(location=0) uniform sampler2DMS g_tColorBuffer;

vec4 SampleColorBuffer(vec2 coords)
{
vec4 singleSampleColor = texelFetch(g_tColorBuffer, ivec2(coords.xy), int(gl_SampleID));
singleSampleColor.rgb = singleSampleColor.rgb / (max3(singleSampleColor.rgb) + 1.0);
return singleSampleColor;// / 2.8;
}



uniform float g_flToneMapScalarLinear;
uniform float g_flExposureBiasScaleFactor;
uniform float g_flShoulderStrength;
Expand All @@ -39,6 +22,17 @@ uniform float g_flToeNum;
uniform float g_flToeDenom;
uniform float g_flWhitePointScale;

uniform float g_flColorCorrectionDefaultWeight;
uniform vec2 g_vColorCorrectionColorRange = vec2(0.96875, 0.015625);

uniform vec4 g_vBlueNoiseDitherParams;

layout (location = 0) uniform sampler2DMS g_tColorBuffer;
layout (location = 1) uniform sampler3D g_tColorCorrection;
layout (location = 2) uniform sampler2D g_tBlueNoise;

layout (location = 0) out vec4 outputColor;

vec3 TonemapColor(vec3 vColor)
{
vColor *= (g_flToneMapScalarLinear * g_flExposureBiasScaleFactor) * 2.8;
Expand All @@ -55,25 +49,13 @@ vec3 TonemapColor(vec3 vColor)
return vTonemappedColor;
}



#if F_COLOR_CORRECTION_LUT

layout(location=2) uniform sampler3D g_tColorCorrection;
uniform float g_flColorCorrectionDefaultWeight;
uniform vec2 g_vColorCorrectionColorRange = vec2(0.96875, 0.015625);
vec3 ApplyColorCorrection(vec3 vColor)
{
vec3 scaledColor = saturate(vColor) * g_vColorCorrectionColorRange.x + g_vColorCorrectionColorRange.y;
vec3 ColorCorrectedColor = texture(g_tColorCorrection, scaledColor).rgb;
return mix(ColorCorrectedColor, vColor, g_flColorCorrectionDefaultWeight); // Probably for blending
}
#endif

layout(location=1) uniform sampler2D g_tBlueNoise;
uniform vec4 g_vBlueNoiseDitherParams;

//#if !F_OUTPUT_LINEAR
vec3 DitherColor(vec3 vColor)
{
vec2 blueNoiseCoords = gl_FragCoord.xy * g_vBlueNoiseDitherParams.z + g_vBlueNoiseDitherParams.xy;
Expand All @@ -85,11 +67,13 @@ vec3 DitherColor(vec3 vColor)
vec3 subPrecisionDither = (blueNoise - 0.5) * g_vBlueNoiseDitherParams.w;
return vColor + subPrecisionDither;
}
//#endif



layout(location=0) out vec4 outputColor;
vec4 SampleColorBuffer(vec2 coords)
{
vec4 singleSampleColor = texelFetch(g_tColorBuffer, ivec2(coords.xy), int(gl_SampleID));
singleSampleColor.rgb = singleSampleColor.rgb / (max3(singleSampleColor.rgb) + 1.0);
return singleSampleColor;
}

void main()
{
Expand All @@ -99,14 +83,14 @@ void main()
// Finally, convert from Linear to Gamma space
vColor.rgb = SrgbLinearToGamma(vColor.rgb);

#if F_COLOR_CORRECTION_LUT
vColor.rgb = ApplyColorCorrection(vColor.rgb);
#endif
const bool bUseLUT = g_flColorCorrectionDefaultWeight > 0.0;
if (bUseLUT)
{
vColor.rgb = ApplyColorCorrection(vColor.rgb);
}

//#if !F_OUTPUT_LINEAR
// Not present in CS2, replaced by a Film Grain setting
vColor.rgb = DitherColor(vColor.rgb);
//#endif

outputColor = vColor;
}
2 changes: 1 addition & 1 deletion GUI/Types/Renderer/Shaders/texture_decode.frag
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ vec3 CheckerboardPattern(vec2 vScreenCoords)
return vColor;
}

layout(location = 0) out vec4 vColorOutput;
layout (location = 0) out vec4 vColorOutput;

void main()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public KVObject GetLocalContrastParams()

public bool HasColorCorrection()
{
if ((Data as KVObject).Properties.TryGetValue("m_bHasColorCorrection", out var value))
if (Data.Properties.TryGetValue("m_bHasColorCorrection", out var value))
{
return (bool)value.Value;
}
Expand Down

0 comments on commit a8e047a

Please sign in to comment.