1/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6Texture2D InputTexture : register(t0); 7SamplerState InputSampler : register(s0); 8Texture2D GradientTexture : register(t1); 9SamplerState GradientSampler : register(s1); 10 11cbuffer constants : register(b0) 12{ 13 // Precalculate as much as we can! 14 float3 diff : packoffset(c0.x); 15 float2 center1 : packoffset(c1.x); 16 float A : packoffset(c1.z); 17 float radius1 : packoffset(c1.w); 18 float sq_radius1 : packoffset(c2.x); 19 20 // The next two values are used for a hack to compensate for an apparent 21 // bug in D2D where the GradientSampler SamplerState doesn't get the 22 // correct addressing modes. 23 float repeat_correct : packoffset(c2.y); 24 float allow_odd : packoffset(c2.z); 25 26 float3x2 transform : packoffset(c3.x); 27} 28 29float4 SampleRadialGradientPS( 30 float4 clipSpaceOutput : SV_POSITION, 31 float4 sceneSpaceOutput : SCENE_POSITION, 32 float4 texelSpaceInput0 : TEXCOORD0 33 ) : SV_Target 34{ 35 // Radial gradient painting is defined as the set of circles whose centers 36 // are described by C(t) = (C2 - C1) * t + C1; with radii 37 // R(t) = (R2 - R1) * t + R1; for R(t) > 0. This shader solves the 38 // quadratic equation that arises when calculating t for pixel (x, y). 39 // 40 // A more extensive derrivation can be found in the pixman radial gradient 41 // code. 42 43 float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31, 44 sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32); 45 float3 dp = float3(p - center1, radius1); 46 47 // dpx * dcx + dpy * dcy + r * dr 48 float B = dot(dp, diff); 49 50 float C = pow(dp.x, 2) + pow(dp.y, 2) - sq_radius1; 51 52 float det = pow(B, 2) - A * C; 53 54 float sqrt_det = sqrt(abs(det)); 55 56 float2 t = (B + float2(sqrt_det, -sqrt_det)) / A; 57 58 float2 isValid = step(float2(-radius1, -radius1), t * diff.z); 59 60 float upper_t = lerp(t.y, t.x, isValid.x); 61 62 // Addressing mode bug work-around.. first let's see if we should consider odd repetitions separately. 63 float oddeven = abs(fmod(floor(upper_t), 2)) * allow_odd; 64 65 // Now let's calculate even or odd addressing in a branchless manner. 66 float upper_t_repeated = ((upper_t - floor(upper_t)) * (1.0f - oddeven)) + ((ceil(upper_t) - upper_t) * oddeven); 67 68 float4 output = GradientTexture.Sample(GradientSampler, float2(upper_t * (1.0f - repeat_correct) + upper_t_repeated * repeat_correct, 0.5)); 69 // Premultiply 70 output.rgb *= output.a; 71 // Multiply the output color by the input mask for the operation. 72 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy); 73 74 // In order to compile for PS_4_0_level_9_3 we need to be branchless. 75 // This is essentially returning nothing, i.e. bailing early if: 76 // det < 0 || max(isValid.x, isValid.y) <= 0 77 return output * abs(step(max(isValid.x, isValid.y), 0) - 1.0f) * step(0, det); 78}; 79 80float4 SampleRadialGradientA0PS( 81 float4 clipSpaceOutput : SV_POSITION, 82 float4 sceneSpaceOutput : SCENE_POSITION, 83 float4 texelSpaceInput0 : TEXCOORD0 84 ) : SV_Target 85{ 86 // This simpler shader is used for the degenerate case where A is 0, 87 // i.e. we're actually solving a linear equation. 88 89 float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31, 90 sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32); 91 float3 dp = float3(p - center1, radius1); 92 93 // dpx * dcx + dpy * dcy + r * dr 94 float B = dot(dp, diff); 95 96 float C = pow(dp.x, 2) + pow(dp.y, 2) - pow(radius1, 2); 97 98 float t = 0.5 * C / B; 99 100 // Addressing mode bug work-around.. first let's see if we should consider odd repetitions separately. 101 float oddeven = abs(fmod(floor(t), 2)) * allow_odd; 102 103 // Now let's calculate even or odd addressing in a branchless manner. 104 float t_repeated = ((t - floor(t)) * (1.0f - oddeven)) + ((ceil(t) - t) * oddeven); 105 106 float4 output = GradientTexture.Sample(GradientSampler, float2(t * (1.0f - repeat_correct) + t_repeated * repeat_correct, 0.5)); 107 // Premultiply 108 output.rgb *= output.a; 109 // Multiply the output color by the input mask for the operation. 110 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy); 111 112 // In order to compile for PS_4_0_level_9_3 we need to be branchless. 113 // This is essentially returning nothing, i.e. bailing early if: 114 // -radius1 >= t * diff.z 115 return output * abs(step(t * diff.z, -radius1) - 1.0f); 116}; 117 118