michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: typedef float4 rect; michael@0: michael@0: float4x4 mLayerTransform : register(vs, c0); michael@0: float4x4 mProjection : register(vs, c4); michael@0: float4 vRenderTargetOffset : register(vs, c8); michael@0: rect vTextureCoords : register(vs, c9); michael@0: rect vLayerQuad : register(vs, c10); michael@0: rect vMaskQuad : register(vs, c11); michael@0: michael@0: float4 fLayerColor : register(ps, c0); michael@0: float fLayerOpacity : register(ps, c1); michael@0: michael@0: sampler sSampler : register(ps, s0); michael@0: michael@0: Texture2D tRGB; michael@0: Texture2D tY; michael@0: Texture2D tCb; michael@0: Texture2D tCr; michael@0: Texture2D tRGBWhite; michael@0: // Always bind this to slot 3 since this is always available! michael@0: Texture2D tMask : register(ps, t3); michael@0: michael@0: struct VS_INPUT { michael@0: float2 vPosition : POSITION; michael@0: }; michael@0: michael@0: struct VS_OUTPUT { michael@0: float4 vPosition : SV_Position; michael@0: float2 vTexCoords : TEXCOORD0; michael@0: }; michael@0: michael@0: struct VS_MASK_OUTPUT { michael@0: float4 vPosition : SV_Position; michael@0: float2 vTexCoords : TEXCOORD0; michael@0: float2 vMaskCoords : TEXCOORD1; michael@0: }; michael@0: michael@0: struct VS_MASK_3D_OUTPUT { michael@0: float4 vPosition : SV_Position; michael@0: float2 vTexCoords : TEXCOORD0; michael@0: float3 vMaskCoords : TEXCOORD1; michael@0: }; michael@0: michael@0: struct PS_OUTPUT { michael@0: float4 vSrc; michael@0: float4 vAlpha; michael@0: }; michael@0: michael@0: float2 TexCoords(const float2 aPosition) michael@0: { michael@0: float2 result; michael@0: const float2 size = vTextureCoords.zw; michael@0: result.x = vTextureCoords.x + aPosition.x * size.x; michael@0: result.y = vTextureCoords.y + aPosition.y * size.y; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: SamplerState LayerTextureSamplerLinear michael@0: { michael@0: Filter = MIN_MAG_MIP_LINEAR; michael@0: AddressU = Clamp; michael@0: AddressV = Clamp; michael@0: }; michael@0: michael@0: float4 TransformedPosition(float2 aInPosition) michael@0: { michael@0: // the current vertex's position on the quad michael@0: float4 position = float4(0, 0, 0, 1); michael@0: michael@0: // We use 4 component floats to uniquely describe a rectangle, by the structure michael@0: // of x, y, width, height. This allows us to easily generate the 4 corners michael@0: // of any rectangle from the 4 corners of the 0,0-1,1 quad that we use as the michael@0: // stream source for our LayerQuad vertex shader. We do this by doing: michael@0: // Xout = x + Xin * width michael@0: // Yout = y + Yin * height michael@0: float2 size = vLayerQuad.zw; michael@0: position.x = vLayerQuad.x + aInPosition.x * size.x; michael@0: position.y = vLayerQuad.y + aInPosition.y * size.y; michael@0: michael@0: position = mul(mLayerTransform, position); michael@0: michael@0: return position; michael@0: } michael@0: michael@0: float4 VertexPosition(float4 aTransformedPosition) michael@0: { michael@0: float4 result; michael@0: result.w = aTransformedPosition.w; michael@0: result.xyz = aTransformedPosition.xyz / aTransformedPosition.w; michael@0: result -= vRenderTargetOffset; michael@0: result.xyz *= result.w; michael@0: michael@0: result = mul(mProjection, result); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: VS_OUTPUT LayerQuadVS(const VS_INPUT aVertex) michael@0: { michael@0: VS_OUTPUT outp; michael@0: float4 position = TransformedPosition(aVertex.vPosition); michael@0: michael@0: outp.vPosition = VertexPosition(position); michael@0: outp.vTexCoords = TexCoords(aVertex.vPosition.xy); michael@0: michael@0: return outp; michael@0: } michael@0: michael@0: VS_MASK_OUTPUT LayerQuadMaskVS(const VS_INPUT aVertex) michael@0: { michael@0: VS_MASK_OUTPUT outp; michael@0: float4 position = TransformedPosition(aVertex.vPosition); michael@0: michael@0: outp.vPosition = VertexPosition(position); michael@0: michael@0: // calculate the position on the mask texture michael@0: outp.vMaskCoords.x = (position.x - vMaskQuad.x) / vMaskQuad.z; michael@0: outp.vMaskCoords.y = (position.y - vMaskQuad.y) / vMaskQuad.w; michael@0: michael@0: outp.vTexCoords = TexCoords(aVertex.vPosition.xy); michael@0: michael@0: return outp; michael@0: } michael@0: michael@0: VS_MASK_3D_OUTPUT LayerQuadMask3DVS(const VS_INPUT aVertex) michael@0: { michael@0: VS_MASK_3D_OUTPUT outp; michael@0: float4 position = TransformedPosition(aVertex.vPosition); michael@0: michael@0: outp.vPosition = VertexPosition(position); michael@0: michael@0: // calculate the position on the mask texture michael@0: position.xyz /= position.w; michael@0: outp.vMaskCoords.x = (position.x - vMaskQuad.x) / vMaskQuad.z; michael@0: outp.vMaskCoords.y = (position.y - vMaskQuad.y) / vMaskQuad.w; michael@0: // We use the w coord to do non-perspective correct interpolation: michael@0: // the quad might be transformed in 3D, in which case it will have some michael@0: // perspective. The graphics card will do perspective-correct interpolation michael@0: // of the texture, but our mask is already transformed and so we require michael@0: // linear interpolation. Therefore, we must correct the interpolation michael@0: // ourselves, we do this by multiplying all coords by w here, and dividing by michael@0: // w in the pixel shader (post-interpolation), we pass w in outp.vMaskCoords.z. michael@0: // See http://en.wikipedia.org/wiki/Texture_mapping#Perspective_correctness michael@0: outp.vMaskCoords.z = 1; michael@0: outp.vMaskCoords *= position.w; michael@0: michael@0: outp.vTexCoords = TexCoords(aVertex.vPosition.xy); michael@0: michael@0: return outp; michael@0: } michael@0: michael@0: float4 RGBAShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target michael@0: { michael@0: float2 maskCoords = aVertex.vMaskCoords; michael@0: float mask = tMask.Sample(sSampler, maskCoords).a; michael@0: return tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity * mask; michael@0: } michael@0: michael@0: float4 RGBAShaderMask3D(const VS_MASK_3D_OUTPUT aVertex) : SV_Target michael@0: { michael@0: float2 maskCoords = aVertex.vMaskCoords.xy / aVertex.vMaskCoords.z; michael@0: float mask = tMask.Sample(LayerTextureSamplerLinear, maskCoords).a; michael@0: return tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity * mask; michael@0: } michael@0: michael@0: float4 RGBShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target michael@0: { michael@0: float4 result; michael@0: result = tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity; michael@0: result.a = fLayerOpacity; michael@0: michael@0: float2 maskCoords = aVertex.vMaskCoords; michael@0: float mask = tMask.Sample(sSampler, maskCoords).a; michael@0: return result * mask; michael@0: } michael@0: michael@0: float4 CalculateYCbCrColor(const float2 aTexCoords) michael@0: { michael@0: float4 yuv; michael@0: float4 color; michael@0: michael@0: yuv.r = tCr.Sample(sSampler, aTexCoords).a - 0.5; michael@0: yuv.g = tY.Sample(sSampler, aTexCoords).a - 0.0625; michael@0: yuv.b = tCb.Sample(sSampler, aTexCoords).a - 0.5; michael@0: michael@0: color.r = yuv.g * 1.164 + yuv.r * 1.596; michael@0: color.g = yuv.g * 1.164 - 0.813 * yuv.r - 0.391 * yuv.b; michael@0: color.b = yuv.g * 1.164 + yuv.b * 2.018; michael@0: color.a = 1.0f; michael@0: michael@0: return color; michael@0: } michael@0: michael@0: float4 YCbCrShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target michael@0: { michael@0: float2 maskCoords = aVertex.vMaskCoords; michael@0: float mask = tMask.Sample(sSampler, maskCoords).a; michael@0: michael@0: return CalculateYCbCrColor(aVertex.vTexCoords) * fLayerOpacity * mask; michael@0: } michael@0: michael@0: PS_OUTPUT ComponentAlphaShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target michael@0: { michael@0: PS_OUTPUT result; michael@0: michael@0: result.vSrc = tRGB.Sample(sSampler, aVertex.vTexCoords); michael@0: result.vAlpha = 1.0 - tRGBWhite.Sample(sSampler, aVertex.vTexCoords) + result.vSrc; michael@0: result.vSrc.a = result.vAlpha.g; michael@0: michael@0: float2 maskCoords = aVertex.vMaskCoords; michael@0: float mask = tMask.Sample(sSampler, maskCoords).a; michael@0: result.vSrc *= fLayerOpacity * mask; michael@0: result.vAlpha *= fLayerOpacity * mask; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: float4 SolidColorShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target michael@0: { michael@0: float2 maskCoords = aVertex.vMaskCoords; michael@0: float mask = tMask.Sample(sSampler, maskCoords).a; michael@0: return fLayerColor * mask; michael@0: } michael@0: michael@0: /* michael@0: * Un-masked versions michael@0: ************************************************************* michael@0: */ michael@0: float4 RGBAShader(const VS_OUTPUT aVertex) : SV_Target michael@0: { michael@0: return tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity; michael@0: } michael@0: michael@0: float4 RGBShader(const VS_OUTPUT aVertex) : SV_Target michael@0: { michael@0: float4 result; michael@0: result = tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity; michael@0: result.a = fLayerOpacity; michael@0: return result; michael@0: } michael@0: michael@0: float4 YCbCrShader(const VS_OUTPUT aVertex) : SV_Target michael@0: { michael@0: return CalculateYCbCrColor(aVertex.vTexCoords) * fLayerOpacity; michael@0: } michael@0: michael@0: PS_OUTPUT ComponentAlphaShader(const VS_OUTPUT aVertex) : SV_Target michael@0: { michael@0: PS_OUTPUT result; michael@0: michael@0: result.vSrc = tRGB.Sample(sSampler, aVertex.vTexCoords); michael@0: result.vAlpha = 1.0 - tRGBWhite.Sample(sSampler, aVertex.vTexCoords) + result.vSrc; michael@0: result.vSrc.a = result.vAlpha.g; michael@0: result.vSrc *= fLayerOpacity; michael@0: result.vAlpha *= fLayerOpacity; michael@0: return result; michael@0: } michael@0: michael@0: float4 SolidColorShader(const VS_OUTPUT aVertex) : SV_Target michael@0: { michael@0: return fLayerColor; michael@0: }