Skip to content

Commit 29a0d5a

Browse files
committed
Use inverse multiply in resolve
1 parent 36ccb34 commit 29a0d5a

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

GUI/Controls/GLViewerControl.cs

+22-15
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,9 @@ protected virtual SKBitmap ReadPixelsToBitmap()
156156
var bitmap = new SKBitmap(GLDefaultFramebuffer.Width, GLDefaultFramebuffer.Height, SKColorType.Bgra8888, SKAlphaType.Opaque);
157157
var pixels = bitmap.GetPixels(out var length);
158158

159-
if (MainFramebuffer != GLDefaultFramebuffer)
160-
{
161-
var (w, h) = (GLControl.Width, GLControl.Height);
162-
GL.BlitNamedFramebuffer(MainFramebuffer.FboHandle, GLDefaultFramebuffer.FboHandle, 0, 0, w, h, 0, 0, w, h, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest);
163-
}
159+
BlitFramebufferMainToDefault();
164160

165-
GL.Flush();
166-
GL.Finish();
161+
GLDefaultFramebuffer.Bind(FramebufferTarget.ReadFramebuffer);
167162
GL.ReadPixels(0, 0, GLDefaultFramebuffer.Width, GLDefaultFramebuffer.Height, PixelFormat.Bgra, PixelType.UnsignedByte, pixels);
168163

169164
// Flip y
@@ -528,13 +523,7 @@ private void Draw()
528523
}
529524
}
530525

531-
if (MainFramebuffer != GLDefaultFramebuffer)
532-
{
533-
MainFramebuffer.Bind(FramebufferTarget.ReadFramebuffer);
534-
GLDefaultFramebuffer.Bind(FramebufferTarget.DrawFramebuffer);
535-
536-
FramebufferBlit(MainFramebuffer, GLDefaultFramebuffer);
537-
}
526+
BlitFramebufferMainToDefault();
538527

539528
if (Settings.Config.DisplayFps != 0)
540529
{
@@ -549,6 +538,19 @@ private void Draw()
549538
GLControl.Invalidate();
550539
}
551540

541+
private void BlitFramebufferMainToDefault()
542+
{
543+
if (MainFramebuffer == GLDefaultFramebuffer)
544+
{
545+
return; // not required
546+
}
547+
548+
MainFramebuffer.Bind(FramebufferTarget.ReadFramebuffer);
549+
GLDefaultFramebuffer.Bind(FramebufferTarget.DrawFramebuffer);
550+
551+
FramebufferBlit(MainFramebuffer, GLDefaultFramebuffer);
552+
}
553+
552554
/// <summary>
553555
/// Multisampling resolve, postprocess the image & convert to gamma.
554556
/// </summary>
@@ -583,7 +585,11 @@ private void HandleResize()
583585
}
584586

585587
GLDefaultFramebuffer.Resize(w, h);
586-
MainFramebuffer.Resize(w, h, NumSamples);
588+
589+
if (MainFramebuffer != GLDefaultFramebuffer)
590+
{
591+
MainFramebuffer.Resize(w, h, NumSamples);
592+
}
587593

588594
if (MainFramebuffer.InitialStatus == FramebufferErrorCode.FramebufferUndefined)
589595
{
@@ -596,6 +602,7 @@ private void HandleResize()
596602

597603
DisposeFramebuffer();
598604
MainFramebuffer = GLDefaultFramebuffer;
605+
GL.Enable(EnableCap.FramebufferSrgb);
599606
}
600607
}
601608

GUI/Types/Renderer/Shaders/background.frag

+15-14
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ void main() {
5757
float sky = pow(smoothstep(0.45, 0.6, upAmount), 2.0);
5858
float highSky = pow(smoothstep(0.6, 0.85, upAmount), 3.0);
5959

60-
vec3 SKY_C = vec3(128.0, 128.0, 128.0) / 255.0;
61-
vec3 SKY_ALT_C = vec3(55.0, 55.0, 55.0) / 255.0;
62-
vec3 HORIZON_C = vec3(55.0, 55.0, 55.0) / 255.0;
63-
vec3 HORIZON_ALT_C = vec3(75.0, 75.0, 75.0) / 255.0;
64-
vec3 GROUND_C = vec3(33.0, 33.0, 33.0) / 255.0;
65-
vec3 GROUND_ALT_C = vec3(44.0, 40.0, 44.0) / 255.0;
60+
vec3 SKY_C = vec3(128.0, 128.0, 128.0);
61+
vec3 SKY_ALT_C = vec3(55.0, 55.0, 55.0);
62+
vec3 HORIZON_C = vec3(55.0, 55.0, 55.0);
63+
vec3 HORIZON_ALT_C = vec3(75.0, 75.0, 75.0);
64+
vec3 GROUND_C = vec3(33.0, 33.0, 33.0);
65+
vec3 GROUND_ALT_C = vec3(44.0, 40.0, 44.0);
6666

6767
if (g_bShowLightBackground) {
68-
SKY_C = vec3(99.0, 161.0, 196.0) / 255.0;
69-
SKY_ALT_C = vec3(160.0, 187.0, 245.0) / 255.0;
70-
HORIZON_C = vec3(161.0, 164.0, 132.0) / 255.0;
71-
HORIZON_ALT_C = vec3(130.0, 184.0, 194.0) / 255.0;
72-
GROUND_C = vec3(60.0, 126.0, 104.0) / 255.0;
73-
GROUND_ALT_C = vec3(62.0, 95.0, 103.0) / 255.0;
68+
SKY_C = vec3(99.0, 161.0, 196.0);
69+
SKY_ALT_C = vec3(160.0, 187.0, 245.0);
70+
HORIZON_C = vec3(161.0, 164.0, 132.0);
71+
HORIZON_ALT_C = vec3(130.0, 184.0, 194.0);
72+
GROUND_C = vec3(60.0, 126.0, 104.0);
73+
GROUND_ALT_C = vec3(62.0, 95.0, 103.0);
7474
}
7575

7676
// mixing the alternative colors to go through their alt version
@@ -85,7 +85,8 @@ void main() {
8585
vec3 skyToHighSky = mix(horizonToSky, SKY_C, highSky);
8686

8787
// add a minute amount of noise to hide color banding
88-
vec3 color = skyToHighSky + (1.0/255.0) * gradientNoise(gl_FragCoord.xy) - (0.5/255.0);
88+
float noise = gradientNoise(gl_FragCoord.xy);
89+
vec3 color = skyToHighSky + noise - 0.5;
8990

90-
outputColor = vec4(SrgbGammaToLinear(color), 1.0);
91+
outputColor = vec4(SrgbGammaToLinear(color / 255f), 1.0);
9192
}

GUI/Types/Renderer/Shaders/common/utils.glsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ vec3 SrgbLinearToGamma( vec3 vLinearColor )
147147
{
148148
// 15 asm instructions
149149
vec3 vLinearSegment = vLinearColor.rgb * 12.92;
150-
vec3 vExpSegment = ( 1.055 * pow( vLinearColor.rgb, vec3 ( 1.0 / 2.4, 1.0 / 2.4, 1.0 / 2.4 ) ) ) - 0.055;
150+
vec3 vExpSegment = ( 1.055 * pow( vLinearColor.rgb, vec3 ( 1.0 / 2.4) )) - 0.055;
151151

152152
vec3 vGammaColor = vec3(( vLinearColor.r <= 0.0031308 ) ? vLinearSegment.r : vExpSegment.r,
153153
( vLinearColor.g <= 0.0031308 ) ? vLinearSegment.g : vExpSegment.g,

GUI/Types/Renderer/Shaders/post_processing.frag

+9-5
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,20 @@ vec3 DitherColor(vec3 vColor)
7272

7373
vec4 SampleColorBuffer(vec2 coords)
7474
{
75-
const int NumSamples = g_nNumSamplesMSAA;
76-
vec4 colorSum = vec4(0.0);
75+
const int NumSamples = int(g_nNumSamplesMSAA);
76+
const float InvNumSamples = 1.0 / float(g_nNumSamplesMSAA);
77+
78+
vec4 vColorMSAA = vec4(0.0);
7779

7880
for (int i = 0; i < NumSamples; i++)
7981
{
8082
vec4 sampleColor = texelFetch(g_tColorBuffer, ivec2(coords.xy), i);
81-
colorSum += sampleColor.rgba;
83+
sampleColor = clamp(sampleColor, vec4(0), vec4(65504));
84+
85+
vColorMSAA += sampleColor.rgba * InvNumSamples;
8286
}
8387

84-
return colorSum / float(NumSamples);
88+
return vColorMSAA;
8589
}
8690

8791
void main()
@@ -99,7 +103,7 @@ void main()
99103
}
100104

101105
// Not present in CS2, done in msaa_resolve_ps instead
102-
//vColor.rgb = DitherColor(vColor.rgb);
106+
vColor.rgb = DitherColor(vColor.rgb);
103107

104108
outputColor = vColor;
105109
}

0 commit comments

Comments
 (0)