From e18dd99a5877cda96a9383a405bf0f571081e3af Mon Sep 17 00:00:00 2001
From: zeromus <zeromus@users.noreply.github.com>
Date: Mon, 27 Sep 2021 22:30:57 -0400
Subject: [PATCH] add bsnes-gamma shader. fixes #2942

---
 Assets/Shaders/BizHawk/bsnes-gamma.cgp  |  4 +++
 Assets/Shaders/BizHawk/bsnes-gamma.glsl | 47 +++++++++++++++++++++++++
 Assets/Shaders/BizHawk/bsnes-gamma.hlsl | 44 +++++++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 Assets/Shaders/BizHawk/bsnes-gamma.cgp
 create mode 100644 Assets/Shaders/BizHawk/bsnes-gamma.glsl
 create mode 100644 Assets/Shaders/BizHawk/bsnes-gamma.hlsl

diff --git a/Assets/Shaders/BizHawk/bsnes-gamma.cgp b/Assets/Shaders/BizHawk/bsnes-gamma.cgp
new file mode 100644
index 0000000000..2649ae2a00
--- /dev/null
+++ b/Assets/Shaders/BizHawk/bsnes-gamma.cgp
@@ -0,0 +1,4 @@
+shaders = 1
+
+shader0 = bsnes-gamma.cg
+scale0 = 2
diff --git a/Assets/Shaders/BizHawk/bsnes-gamma.glsl b/Assets/Shaders/BizHawk/bsnes-gamma.glsl
new file mode 100644
index 0000000000..085eb8a3b9
--- /dev/null
+++ b/Assets/Shaders/BizHawk/bsnes-gamma.glsl
@@ -0,0 +1,47 @@
+//https://raw.githubusercontent.com/Themaister/Emulator-Shader-Pack/master/Cg/TV/gamma.cg
+
+/*
+   Author: Themaister
+   License: Public domain
+*/
+
+// Shader that replicates gamma-ramp of bSNES/Higan.
+
+#ifdef VERTEX
+uniform mat4 modelViewProj;
+
+void main()
+{
+	gl_Position = modelViewProj * gl_Vertex;
+	gl_TexCoord[0] = gl_MultiTexCoord0;
+}
+
+#endif
+
+#ifdef FRAGMENT
+
+uniform sampler2D s_p;
+
+// Tweakables.
+#define saturation 1.0
+#define gamma 1.5
+#define luminance 1.0
+
+vec3 grayscale(vec3 col)
+{
+	// Non-conventional way to do grayscale,
+	// but bSNES uses this as grayscale value.
+	float v = dot(col, vec3(0.3333,0.3333,0.3333));
+	return vec3(v,v,v);
+}
+
+
+void main()
+{
+	vec3 res = texture2D(s_p,gl_TexCoord[0].xy).xyz;
+	res = mix(grayscale(res), res, saturation); // Apply saturation
+	res = pow(res, vec3(gamma,gamma,gamma)); // Apply gamma
+	gl_FragColor = vec4(clamp(res * luminance,0.0,1.0), 1.0);
+}
+
+#endif
diff --git a/Assets/Shaders/BizHawk/bsnes-gamma.hlsl b/Assets/Shaders/BizHawk/bsnes-gamma.hlsl
new file mode 100644
index 0000000000..03f2c3bfe6
--- /dev/null
+++ b/Assets/Shaders/BizHawk/bsnes-gamma.hlsl
@@ -0,0 +1,44 @@
+//https://raw.githubusercontent.com/Themaister/Emulator-Shader-Pack/master/Cg/TV/gamma.cg
+
+/*
+   Author: Themaister
+   License: Public domain
+*/
+
+// Shader that replicates gamma-ramp of bSNES/Higan.
+
+void main_vertex
+(
+   float4 position : POSITION,
+   out float4 oPosition : POSITION,
+   uniform float4x4 modelViewProj,
+
+   float2 tex : TEXCOORD,
+   out float2 oTex : TEXCOORD
+)
+{
+   oPosition = mul(modelViewProj, position);
+   oTex = tex;
+}
+
+// Tweakables.
+#define saturation 1.0
+#define gamma 1.5
+#define luminance 1.0
+
+float3 grayscale(float3 col)
+{
+	// Non-conventional way to do grayscale,
+	// but bSNES uses this as grayscale value.
+	float v = dot(col, float3(0.3333,0.3333,0.3333));
+	return float3(v,v,v);
+}
+
+float4 main_fragment(float2 tex : TEXCOORD, uniform sampler2D s0 : TEXUNIT0) : COLOR
+{
+	float3 res = tex2D(s0, tex).xyz;
+	res = lerp(grayscale(res), res, saturation); // Apply saturation
+	res = pow(res, float3(gamma,gamma,gamma)); // Apply gamma
+	return float4(saturate(res * luminance), 1.0);
+}
+