aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/D3D1X/Shaders/DistortionChroma_ps.psh
blob: b3e405b713fb2f1166f37515b94f1078e5f13f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/************************************************************************************

Filename    :   DistortionChroma_ps.psh

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

Texture2D Texture : register(t0);
Texture2D LastTexture : register(t1);
SamplerState Linear : register(s0);

float2 OverdriveScales;
float AaDerivativeMult;

// Fast approximate gamma to linear conversion when averaging colors
float3 ToLinear(float3 inColor) { return inColor * inColor; }
float3 ToGamma(float3 inColor)	{ return sqrt(inColor); }

float3 ApplyHqAa(float3 inColor, float2 oTexCoord0, float2 oTexCoord1, float2 oTexCoord2)
{
	float2 uvDeriv = float2(ddx(oTexCoord1.x), ddy(oTexCoord1.y)) * AaDerivativeMult;
		
	float ResultR = Texture.Sample(Linear, oTexCoord0 + float2(-uvDeriv.x, 0)).r;
	float ResultG = Texture.Sample(Linear, oTexCoord1 + float2(-uvDeriv.x, 0)).g;
	float ResultB = Texture.Sample(Linear, oTexCoord2 + float2(-uvDeriv.x, 0)).b;
	float3 newColor0 = ToLinear(float3(ResultR, ResultG, ResultB));
	
	ResultR = Texture.Sample(Linear, oTexCoord0 + float2(uvDeriv.x, 0)).r;
	ResultG = Texture.Sample(Linear, oTexCoord1 + float2(uvDeriv.x, 0)).g;
	ResultB = Texture.Sample(Linear, oTexCoord2 + float2(uvDeriv.x, 0)).b;
	float3 newColor1 = ToLinear(float3(ResultR, ResultG, ResultB));

	ResultR = Texture.Sample(Linear, oTexCoord0 + float2(0, uvDeriv.y)).r;
	ResultG = Texture.Sample(Linear, oTexCoord1 + float2(0, uvDeriv.y)).g;
	ResultB = Texture.Sample(Linear, oTexCoord2 + float2(0, uvDeriv.y)).b;
	float3 newColor2 = ToLinear(float3(ResultR, ResultG, ResultB));

	ResultR = Texture.Sample(Linear, oTexCoord0 + float2(0, -uvDeriv.y)).r;
	ResultG = Texture.Sample(Linear, oTexCoord1 + float2(0, -uvDeriv.y)).g;
	ResultB = Texture.Sample(Linear, oTexCoord2 + float2(0, -uvDeriv.y)).b;
	float3 newColor3 = ToLinear(float3(ResultR, ResultG, ResultB));

	// apply gamma correct averaging
	float3 outColor = ToLinear(inColor) + newColor0 + newColor1 + newColor2 + newColor3;
	outColor = ToGamma(outColor * 0.2);
	return outColor;
}

void   main(in float4 oPosition  : SV_Position,
            in float  oColor     : COLOR,
            in float2 oTexCoord0 : TEXCOORD0,
            in float2 oTexCoord1 : TEXCOORD1,
            in float2 oTexCoord2 : TEXCOORD2,
			out float4 outColor0 : SV_Target0,
			out float4 outColor1 : SV_Target1)
{
#if 0	// enable "SampleMode hqFilter = (distortionCaps ... " in code
	// Using anisotropic sampling - requires sRGB sampling
	float2 uvDeriv = float2(ddx(oTexCoord1.x), ddy(oTexCoord1.y));
	float ResultR = Texture.SampleGrad(Linear, oTexCoord0, uvDeriv.x, uvDeriv.y).r;
	float ResultG = Texture.SampleGrad(Linear, oTexCoord1, uvDeriv.x, uvDeriv.y).g;
	float ResultB = Texture.SampleGrad(Linear, oTexCoord2, uvDeriv.x, uvDeriv.y).b;
	float3 newColor = float3(ResultR, ResultG, ResultB);
#else
	float ResultR = Texture.Sample(Linear, oTexCoord0).r;
	float ResultG = Texture.Sample(Linear, oTexCoord1).g;
	float ResultB = Texture.Sample(Linear, oTexCoord2).b;
	float3 newColor = float3(ResultR, ResultG, ResultB);

	// High quality anti-aliasing in distortion
	if(AaDerivativeMult > 0)
	{
		newColor = ApplyHqAa(newColor, oTexCoord0, oTexCoord1, oTexCoord2);
	}
#endif

	newColor = newColor * oColor.xxx;
	outColor0 = float4(newColor, 1.0);
	outColor1 = outColor0;
	
	// pixel luminance overdrive
	if(OverdriveScales.x > 0)
	{
		float3 oldColor = LastTexture.Load(int3(oPosition.xy, 0)).rgb;
		
		float3 adjustedScales;
		adjustedScales.x = newColor.x > oldColor.x ? OverdriveScales.x : OverdriveScales.y;
		adjustedScales.y = newColor.y > oldColor.y ? OverdriveScales.x : OverdriveScales.y;
		adjustedScales.z = newColor.z > oldColor.z ? OverdriveScales.x : OverdriveScales.y;
		
		float3 overdriveColor = saturate(newColor + (newColor - oldColor) * adjustedScales);
		outColor1 = float4(overdriveColor, 1.0);
	}
}