michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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: Texture2D InputTexture : register(t0); michael@0: SamplerState InputSampler : register(s0); michael@0: Texture2D GradientTexture : register(t1); michael@0: SamplerState GradientSampler : register(s1); michael@0: michael@0: cbuffer constants : register(b0) michael@0: { michael@0: // Precalculate as much as we can! michael@0: float3 diff : packoffset(c0.x); michael@0: float2 center1 : packoffset(c1.x); michael@0: float A : packoffset(c1.z); michael@0: float radius1 : packoffset(c1.w); michael@0: float sq_radius1 : packoffset(c2.x); michael@0: float3x2 transform : packoffset(c3.x); michael@0: } michael@0: michael@0: float4 SampleRadialGradientPS( michael@0: float4 clipSpaceOutput : SV_POSITION, michael@0: float4 sceneSpaceOutput : SCENE_POSITION, michael@0: float4 texelSpaceInput0 : TEXCOORD0 michael@0: ) : SV_Target michael@0: { michael@0: // Radial gradient painting is defined as the set of circles whose centers michael@0: // are described by C(t) = (C2 - C1) * t + C1; with radii michael@0: // R(t) = (R2 - R1) * t + R1; for R(t) > 0. This shader solves the michael@0: // quadratic equation that arises when calculating t for pixel (x, y). michael@0: // michael@0: // A more extensive derrivation can be found in the pixman radial gradient michael@0: // code. michael@0: michael@0: float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31, michael@0: sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32); michael@0: float3 dp = float3(p - center1, radius1); michael@0: michael@0: // dpx * dcx + dpy * dcy + r * dr michael@0: float B = dot(dp, diff); michael@0: michael@0: float C = pow(dp.x, 2) + pow(dp.y, 2) - sq_radius1; michael@0: michael@0: float det = pow(B, 2) - A * C; michael@0: michael@0: float sqrt_det = sqrt(abs(det)); michael@0: michael@0: float2 t = (B + float2(sqrt_det, -sqrt_det)) / A; michael@0: michael@0: float2 isValid = step(float2(-radius1, -radius1), t * diff.z); michael@0: michael@0: float upper_t = lerp(t.y, t.x, isValid.x); michael@0: michael@0: float4 output = GradientTexture.Sample(GradientSampler, float2(upper_t, 0.5)); michael@0: // Premultiply michael@0: output.rgb *= output.a; michael@0: // Multiply the output color by the input mask for the operation. michael@0: output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy); michael@0: michael@0: // In order to compile for PS_4_0_level_9_3 we need to be branchless. michael@0: // This is essentially returning nothing, i.e. bailing early if: michael@0: // det < 0 || max(isValid.x, isValid.y) <= 0 michael@0: return output * abs(step(max(isValid.x, isValid.y), 0) - 1.0f) * step(0, det); michael@0: }; michael@0: michael@0: float4 SampleRadialGradientA0PS( michael@0: float4 clipSpaceOutput : SV_POSITION, michael@0: float4 sceneSpaceOutput : SCENE_POSITION, michael@0: float4 texelSpaceInput0 : TEXCOORD0 michael@0: ) : SV_Target michael@0: { michael@0: // This simpler shader is used for the degenerate case where A is 0, michael@0: // i.e. we're actually solving a linear equation. michael@0: michael@0: float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31, michael@0: sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32); michael@0: float3 dp = float3(p - center1, radius1); michael@0: michael@0: // dpx * dcx + dpy * dcy + r * dr michael@0: float B = dot(dp, diff); michael@0: michael@0: float C = pow(dp.x, 2) + pow(dp.y, 2) - pow(radius1, 2); michael@0: michael@0: float t = 0.5 * C / B; michael@0: michael@0: float4 output = GradientTexture.Sample(GradientSampler, float2(t, 0.5)); michael@0: // Premultiply michael@0: output.rgb *= output.a; michael@0: // Multiply the output color by the input mask for the operation. michael@0: output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy); michael@0: michael@0: // In order to compile for PS_4_0_level_9_3 we need to be branchless. michael@0: // This is essentially returning nothing, i.e. bailing early if: michael@0: // -radius1 >= t * diff.z michael@0: return output * abs(step(t * diff.z, -radius1) - 1.0f); michael@0: };