aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/D3D1X/Shaders/DistortionChroma_ps.psh
blob: c69fce0c4a9eaa609b4793e7f01651428295a9a6 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/************************************************************************************

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); }

void SampleStep(float2 oTexCoord0, float2 oTexCoord1, float2 oTexCoord2, float colorWeight, float2 texOffset,
				inout float3 totalColor, inout float totalWeight)
{
	float3 newColor = 0;
	newColor.r += Texture.Sample(Linear, oTexCoord0 + texOffset).r;
	newColor.g += Texture.Sample(Linear, oTexCoord1 + texOffset).g;
	newColor.b += Texture.Sample(Linear, oTexCoord2 + texOffset).b;
	newColor = ToLinear(newColor);

	totalColor += newColor * colorWeight;
	totalWeight += colorWeight;
}

float3 ApplyHqAa(float2 oTexCoord0, float2 oTexCoord1, float2 oTexCoord2)
{
	float2 texWidth = fwidth(oTexCoord1);
	float2 texStep = texWidth * AaDerivativeMult;

	float totalWeight = 0;
	float3 totalColor = 0;

	// center sample
	SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, 4, 0, totalColor, totalWeight);

	float3 smplExp = 1.0 / 3.0;
	float3 smplWgt = 1.0;

	SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, smplWgt.x, -1.000 * smplExp.x * texStep, totalColor, totalWeight);
	//SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, smplWgt.y, -1.250 * smplExp.y * texStep, totalColor, totalWeight);
	SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, smplWgt.z, -1.875 * smplExp.z * texStep, totalColor, totalWeight);
	SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, smplWgt.z,  1.875 * smplExp.z * texStep, totalColor, totalWeight);
	//SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, smplWgt.y,  1.250 * smplExp.y * texStep, totalColor, totalWeight);
	SampleStep(oTexCoord0, oTexCoord1, oTexCoord2, smplWgt.x,  1.000 * smplExp.x * texStep, totalColor, totalWeight);

	return ToGamma(totalColor.rgb / totalWeight);
}

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)
{
#define USE_ANISO 0

#if USE_ANISO	// enable "SampleMode hqFilter = (distortionCaps ... " in code
				// Using anisotropic sampling - requires sRGB sampling

	#if 1	// feeding in proper ddx & ddy does not yield better visuals
		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
		float2 uvDerivX = ddx(oTexCoord1);
		float2 uvDerivY = ddy(oTexCoord1);
		float ResultR = Texture.SampleGrad(Linear, oTexCoord0, uvDerivX, uvDerivY).r;
		float ResultG = Texture.SampleGrad(Linear, oTexCoord1, uvDerivX, uvDerivY).g;
		float ResultB = Texture.SampleGrad(Linear, oTexCoord2, uvDerivX, uvDerivY).b;
		float3 newColor = float3(ResultR, ResultG, ResultB);
	#endif

#else

	float3 newColor;
	// High quality anti-aliasing in distortion
	if(AaDerivativeMult > 0)
	{
		newColor = ApplyHqAa(oTexCoord0, oTexCoord1, oTexCoord2);
	}
	else
	{
		float ResultR = Texture.Sample(Linear, oTexCoord0).r;
		float ResultG = Texture.Sample(Linear, oTexCoord1).g;
		float ResultB = Texture.Sample(Linear, oTexCoord2).b;
		newColor = float3(ResultR, ResultG, ResultB);
	}

#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);
	}
}