From d228ad3ddd89bb92a9b22b4488b7b5401ae7ce20 Mon Sep 17 00:00:00 2001 From: Kieran Hanrahan Date: Mon, 10 Nov 2014 23:46:50 +0000 Subject: [PATCH] OpenGL Updates - fix effects for both D3D & OGL I had to edit this via the GitHub editor. I finish up my changes, and for some reason the pcsx2 repository won't let me read or write to it, since the last merge. I checked my other repositories, and they're working fine. ?.? --- bin/shader.fx | 167 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 61 deletions(-) diff --git a/bin/shader.fx b/bin/shader.fx index b750b9f402..48d703d6d1 100644 --- a/bin/shader.fx +++ b/bin/shader.fx @@ -17,6 +17,7 @@ #ifndef SHADER_MODEL #define GLSL 1 +#extension GL_ARB_gpu_shader5 : enable #else #define GLSL 0 #endif @@ -38,7 +39,10 @@ #define float2 vec2 #define float3 vec3 #define float4 vec4 +#define float4x3 mat4x3 #define static +#define frac fract +#define mul(x, y) y * x #define lerp(x,y,s) mix(x,y,s) #define saturate(x) clamp(x, 0.0, 1.0) #define SamplerState sampler2D @@ -117,6 +121,15 @@ float4 sample_tex(SamplerState texSample, float2 t) #endif } +float4 sample_texLevel(SamplerState texSample, float2 t, float lod) +{ +#if (GLSL == 1) + return texture(texSample, t, lod); +#else + return Texture.Sample(texSample, t, lod); +#endif +} + /*------------------------------------------------------------------------------ [FXAA CODE SECTION] @@ -164,6 +177,22 @@ struct FxaaTex { SamplerState smpl; Texture2D tex; }; #define FxaaSat(x) saturate(x) #endif +#if (GLSL == 1) +#define FxaaBool bool +#define FxaaDiscard discard +#define FxaaSat(x) clamp(x, 0.0, 1.0) +#define FxaaTex sampler2D +#define FxaaTexTop(t, p) textureLod(t, p, 0.0) +#define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o) +#if (FXAA_GATHER4_ALPHA == 1) +// use #extension GL_ARB_gpu_shader5 : enable +#define FxaaTexAlpha4(t, p) textureGather(t, p, 3) +#define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3) +#define FxaaTexGreen4(t, p) textureGather(t, p, 1) +#define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1) +#endif +#endif + #define FXAA_QUALITY__P0 1.0 #define FXAA_QUALITY__P1 1.0 #define FXAA_QUALITY__P2 1.0 @@ -488,10 +517,16 @@ float4 FxaaPixelShader(float2 pos, FxaaTex tex, float2 fxaaRcpFrame, float fxaaS float4 FxaaPass(float4 FxaaColor, float2 texcoord) { FxaaTex tex; + + #if(GLSL == 1) + tex = TextureSampler; + vec2 PixelSize = textureSize(tex, 0); + FxaaColor = FxaaPixelShader(texcoord, tex, 1.0/PixelSize.xy, FxaaSubpixMax, FxaaEdgeThreshold, FxaaEdgeThresholdMin); + #else tex.tex = Texture; tex.smpl = TextureSampler; - FxaaColor = FxaaPixelShader(texcoord, tex, pixelSize.xy, FxaaSubpixMax, FxaaEdgeThreshold, FxaaEdgeThresholdMin); + #endif return FxaaColor; } @@ -661,8 +696,8 @@ float4 BicubicFilter(SamplerState texSample, float2 texcoord) float texelSizeX = pixelSize.x; float texelSizeY = pixelSize.y; - float4 nSum = (float4)0.0; - float4 nDenom = (float4)0.0; + float4 nSum = float4(0.0, 0.0, 0.0, 0.0); + float4 nDenom = float4(0.0, 0.0, 0.0, 0.0); float a = frac(texcoord.x * screenSize.x); float b = frac(texcoord.y * screenSize.y); @@ -766,14 +801,19 @@ float4 GaussianPass(float4 color, float2 texcoord) #if (BICUBLIC_SCALAR == 1) float4 BicubicScalar(in SamplerState tex, in float2 uv, in float2 texSize) { - float2 rec_nrCP = float2(1.0/texSize.x, 1.0/texSize.y); + float2 inputSize = float2(1.0/texSize.x, 1.0/texSize.y); float2 coord_hg = uv * texSize - 0.5; float2 index = floor(coord_hg); float2 f = coord_hg - index; + #if (GLSL == 1) + mat4 M = mat4( -1.0, 3.0,-3.0, 1.0, 3.0,-6.0, 3.0, 0.0, + -3.0, 0.0, 3.0, 0.0, 1.0, 4.0, 1.0, 0.0 ); + #else float4x4 M = { -1.0, 3.0,-3.0, 1.0, 3.0,-6.0, 3.0, 0.0, -3.0, 0.0, 3.0, 0.0, 1.0, 4.0, 1.0, 0.0 }; + #endif M /= 6.0; float4 wx = mul(float4(f.x*f.x*f.x, f.x*f.x, f.x, 1.0), M); @@ -793,10 +833,10 @@ float4 BicubicScalar(in SamplerState tex, in float2 uv, in float2 texSize) float2 coord01 = index + float2(h0.x, h1.y); float2 coord11 = index + h1; - coord00 = (coord00 + 0.5) * rec_nrCP; - coord10 = (coord10 + 0.5) * rec_nrCP; - coord01 = (coord01 + 0.5) * rec_nrCP; - coord11 = (coord11 + 0.5) * rec_nrCP; + coord00 = (coord00 + 0.5) * inputSize; + coord10 = (coord10 + 0.5) * inputSize; + coord01 = (coord01 + 0.5) * inputSize; + coord11 = (coord11 + 0.5) * inputSize; float4 tex00 = sample_texLevel(tex, coord00, 0); float4 tex10 = sample_texLevel(tex, coord10, 0); @@ -805,6 +845,7 @@ float4 BicubicScalar(in SamplerState tex, in float2 uv, in float2 texSize) tex00 = lerp(tex01, tex00, float4(g0.y, g0.y, g0.y, g0.y)); tex10 = lerp(tex11, tex10, float4(g0.y, g0.y, g0.y, g0.y)); + float4 res = lerp(tex10, tex00, float4(g0.x, g0.x, g0.x, g0.x)); return res; @@ -822,31 +863,34 @@ float4 BiCubicScalarPass(float4 color, float2 texcoord) ------------------------------------------------------------------------------*/ #if (LANCZOS_SCALAR == 1) -float3 pixel(float xpos, float ypos) +float3 PixelPos(float xpos, float ypos) { return sample_tex(TextureSampler, float2(xpos, ypos)).rgb; } -float3 line_run(float ypos, float4 xpos, float4 linetaps) -{ - return mul(linetaps, float4x3(pixel(xpos.x, ypos), pixel(xpos.y, ypos), - pixel(xpos.z, ypos), pixel(xpos.w, ypos))); -} - -float4 weight4(float x) +float4 WeightQuad(float x) { #define FIX(c) max(abs(c), 1e-5); const float PI = 3.1415926535897932384626433832795; - float4 sample = FIX(PI * float4(1.0 + x, x, 1.0 - x, 2.0 - x)); - float4 ret = sin(sample) * sin(sample / 2.0) / (sample * sample); + float4 weight = FIX(PI * float4(1.0 + x, x, 1.0 - x, 2.0 - x)); + float4 ret = sin(weight) * sin(weight / 2.0) / (weight * weight); return ret / dot(ret, float4(1.0, 1.0, 1.0, 1.0)); } -float4 LanczosScalar(float2 texcoord, float2 texSize) +float3 LineRun(float ypos, float4 xpos, float4 linetaps) { - float2 stepxy = 1.0 / texSize; + return mul(linetaps, float4x3( + PixelPos(xpos.x, ypos), + PixelPos(xpos.y, ypos), + PixelPos(xpos.z, ypos), + PixelPos(xpos.w, ypos))); +} + +float4 LanczosScalar(float2 texcoord, float2 inputSize) +{ + float2 stepxy = float2(1.0/inputSize.x, 1.0/inputSize.y); float2 pos = texcoord + stepxy; float2 f = frac(pos / stepxy); @@ -856,15 +900,15 @@ float4 LanczosScalar(float2 texcoord, float2 texSize) xystart.x + stepxy.x * 2.0, xystart.x + stepxy.x * 3.0); - float4 linetaps = weight4(f.x); - float4 columntaps = weight4(f.y); + float4 linetaps = WeightQuad(f.x); + float4 columntaps = WeightQuad(f.y); // final sum and weight normalization return float4(mul(columntaps, float4x3( - line_run(xystart.y, xpos, linetaps), - line_run(xystart.y + stepxy.y, xpos, linetaps), - line_run(xystart.y + stepxy.y * 2.0, xpos, linetaps), - line_run(xystart.y + stepxy.y * 3.0, xpos, linetaps))), 1.0); + LineRun(xystart.y, xpos, linetaps), + LineRun(xystart.y + stepxy.y, xpos, linetaps), + LineRun(xystart.y + stepxy.y * 2.0, xpos, linetaps), + LineRun(xystart.y + stepxy.y * 3.0, xpos, linetaps))), 1.0); } float4 LanczosScalarPass(float4 color, float2 texcoord) @@ -926,8 +970,8 @@ float4 SampleBicubic(in SamplerState texSample, in float2 texcoord) float texelSizeX = pixelSize.x * float(SharpenBias); float texelSizeY = pixelSize.y * float(SharpenBias); - float4 nSum = (float4)0.0; - float4 nDenom = (float4)0.0; + float4 nSum = float4(0.0, 0.0, 0.0, 0.0); + float4 nDenom = float4(0.0, 0.0, 0.0, 0.0); float a = frac(texcoord.x * screenSize.x); float b = frac(texcoord.y * screenSize.y); @@ -985,7 +1029,11 @@ float4 TexSharpenPass(float4 color, float2 texcoord) #if (PIXEL_VIBRANCE == 1) float4 VibrancePass(float4 color, float2 texcoord) { + #if (GLSL == 1) + float3 luma = float3(RGBLuminance(color.rgb)); + #else float luma = RGBLuminance(color.rgb); + #endif float colorMax = max(color.r, max(color.g, color.b)); float colorMin = min(color.r, min(color.g, color.b)); @@ -1171,11 +1219,7 @@ float3 ColorCorrection(float3 color) return saturate(color); } -#if (GLSL == 1) float4 TonemapPass(float4 color, float2 texcoord) -#else -float4 TonemapPass(float4 color, float2 texcoord) : COLOR0 -#endif { const float delta = 0.001f; const float wpoint = pow(1.002f, 2.0f); @@ -1204,6 +1248,7 @@ float4 TonemapPass(float4 color, float2 texcoord) : COLOR0 float3 XYZ = mul(RGB2XYZ, color.rgb); #endif + // XYZ -> Yxy conversion float3 Yxy; @@ -1247,6 +1292,7 @@ float4 TonemapPass(float4 color, float2 texcoord) : COLOR0 0.0556434,-0.2040259, 1.0572252 }; #endif + #if (GLSL == 1) color.rgb = XYZ2RGB * XYZ; #else @@ -1267,7 +1313,11 @@ float4 ContrastPass(float4 color, float2 texcoord) float CurveBlend = CurvesContrast; #if (CurveType != 2) + #if (GLSL == 1) + float3 luma = float3(RGBLuminance(color.rgb)); + #else float3 luma = (float3)RGBLuminance(color.rgb); + #endif float3 chroma = color.rgb - luma; #endif @@ -1329,17 +1379,17 @@ static const float3 thresholds = float3(5.0, 8.0, 6.0); float3 GetYUV(float3 rgb) { -#if (LumaConversion == 1) +#if (GLSL == 1) + mat3 RGB2YUV = mat3( + 0.2126, 0.09991, 0.615, + 0.7152, -0.33609, -0.55861, + 0.0722, 0.436, -0.05639 ); +#else float3x3 RGB2YUV = { 0.2126, 0.7152, 0.0722, -0.09991, -0.33609, 0.436, 0.615, -0.55861, -0.05639 }; -#else - float3x3 RGB2YUV = { - 0.299, 0.587, 0.114, - -0.14713, -0.28886f, 0.436, - 0.615, -0.51499, -0.10001 }; #endif return mul(RGB2YUV, rgb); @@ -1347,17 +1397,17 @@ float3 GetYUV(float3 rgb) float3 GetRGB(float3 yuv) { -#if (LumaConversion == 1) +#if (GLSL == 1) + mat3 YUV2RGB = mat3( + 1.000, 1.000, 1.000, + 0.000, -0.21482, 2.12798, + 1.28033, -0.38059, 0.000 ); +#else float3x3 YUV2RGB = { 1.000, 0.000, 1.28033, 1.000, -0.21482, -0.38059, 1.000, 2.12798, 0.000 }; -#else - float3x3 YUV2RGB = { - 1.000, 0.000, 1.13983, - 1.000, -0.39465, -0.58060, - 1.000, 2.03211, 0.000 }; #endif return mul(YUV2RGB, yuv); @@ -1445,7 +1495,7 @@ float RGBCVtoHUE(float3 RGB, float C, float V) float3 Delta = (V - RGB) / C; Delta.rgb -= Delta.brg; - Delta.rgb += float3(2, 4, 6); + Delta.rgb += float3(2.0, 4.0, 6.0); Delta.brg = step(V, RGB) * Delta.brg; float H; @@ -1455,7 +1505,7 @@ float RGBCVtoHUE(float3 RGB, float C, float V) float3 RGBtoHSV(float3 RGB) { - float3 HSV = 0; + float3 HSV = float3(0.0, 0.0, 0.0); HSV.z = max(RGB.r, max(RGB.g, RGB.b)); float M = min(RGB.r, min(RGB.g, RGB.b)); float C = HSV.z - M; @@ -1556,24 +1606,18 @@ float4 ColorGrading(float4 color, float2 texcoord) #if (SCANLINES == 1) float4 ScanlinesPass(float4 color, float2 texcoord, float4 fragcoord) { - #if (ScanlineType == 3) - float amount = ScanlineBrightness; - float intensity = ScanlineIntensity; - - float pos0 = ((texcoord.y + 1.0) * 170.0 * amount); - float pos1 = cos((frac(pos0 * ScanlineScale) - 0.5) * 3.1415926 * intensity) * 1.2; - - color = lerp(float4(0, 0, 0, 0), color, pos1); - #else - float4 intensity; + + #if (GLSL == 1) + fragcoord = gl_FragCoord; + #endif #if (ScanlineType == 0) - if (frac(fragcoord.y * 0.5) > ScanlineScale) + if (frac(fragcoord.y * 0.25) > ScanlineScale) #elif (ScanlineType == 1) - if (frac(fragcoord.x * 0.5) > ScanlineScale) + if (frac(fragcoord.x * 0.25) > ScanlineScale) #elif (ScanlineType == 2) - if (frac(fragcoord.x * 0.5) > ScanlineScale && frac(fragcoord.y * 0.5) > ScanlineScale) + if (frac(fragcoord.x * 0.25) > ScanlineScale && frac(fragcoord.y * 0.5) > ScanlineScale) #endif { intensity = float4(0.0, 0.0, 0.0, 0.0); @@ -1586,7 +1630,6 @@ float4 ScanlinesPass(float4 color, float2 texcoord, float4 fragcoord) float level = (4.0 - texcoord.x) * ScanlineIntensity; color = intensity * (0.5 - level) + color * 1.1; - #endif return color; } @@ -1687,11 +1730,13 @@ PS_OUTPUT ps_main(VS_OUTPUT input) { #if (GLSL == 1) float2 texcoord = PSin.t; + float4 position = PSin.p; float4 color = texture(TextureSampler, texcoord); #else PS_OUTPUT output; float2 texcoord = input.t; + float4 position = input.p; float4 color = sample_tex(TextureSampler, texcoord); #endif @@ -1728,7 +1773,7 @@ PS_OUTPUT ps_main(VS_OUTPUT input) #endif #if (SCANLINES == 1) - color = ScanlinesPass(color, texcoord, input.p); + color = ScanlinesPass(color, texcoord, position); #endif #if (BLENDED_BLOOM == 1)