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