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/. */
6 Texture2D InputTexture : register(t0);
7 SamplerState InputSampler : register(s0);
8 Texture2D GradientTexture : register(t1);
9 SamplerState GradientSampler : register(s1);
11 cbuffer radialGradientConstants : register(b0)
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);
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);
26 float3x2 transform : packoffset(c3.x);
29 cbuffer conicGradientConstants : register(b0)
31 float2 center : packoffset(c0.x);
32 float angle : packoffset(c0.z);
33 float start_offset : packoffset(c0.w);
34 float end_offset : packoffset(c1.x);
36 // The next two values are used for a hack to compensate for an apparent
37 // bug in D2D where the GradientSampler SamplerState doesn't get the
38 // correct addressing modes.
39 float repeat_correct_conic : packoffset(c1.y);
40 float allow_odd_conic : packoffset(c1.z);
42 float3x2 transform_conic : packoffset(c2.x);
46 static const float M_PI = 3.14159265f;
48 float4 SampleConicGradientPS(
49 float4 clipSpaceOutput : SV_POSITION,
50 float4 sceneSpaceOutput : SCENE_POSITION,
51 float4 texelSpaceInput0 : TEXCOORD0
54 float2 p = float2(sceneSpaceOutput.x * transform_conic._11 + sceneSpaceOutput.y * transform_conic._21 + transform_conic._31,
55 sceneSpaceOutput.x * transform_conic._12 + sceneSpaceOutput.y * transform_conic._22 + transform_conic._32);
59 float vstart = start_offset;
60 float vend = end_offset;
61 float n = 1/(vend-vstart);
62 float current_angle = atan2(dir.y, dir.x)-angle;
63 float lambda = fmod(n*current_angle/M_PI/2+vend-vstart+.5,1);
64 float offset = lambda;
65 float4 output = GradientTexture.Sample(GradientSampler, float2(offset, 0.5));
67 output.rgb *= output.a;
68 // Multiply the output color by the input mask for the operation.
69 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy);
74 float4 SampleRadialGradientPS(
75 float4 clipSpaceOutput : SV_POSITION,
76 float4 sceneSpaceOutput : SCENE_POSITION,
77 float4 texelSpaceInput0 : TEXCOORD0
80 // Radial gradient painting is defined as the set of circles whose centers
81 // are described by C(t) = (C2 - C1) * t + C1; with radii
82 // R(t) = (R2 - R1) * t + R1; for R(t) > 0. This shader solves the
83 // quadratic equation that arises when calculating t for pixel (x, y).
85 // A more extensive derrivation can be found in the pixman radial gradient
88 float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31,
89 sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32);
90 float3 dp = float3(p - center1, radius1);
92 // dpx * dcx + dpy * dcy + r * dr
93 float B = dot(dp, diff);
95 float C = pow(dp.x, 2) + pow(dp.y, 2) - sq_radius1;
97 float det = pow(B, 2) - A * C;
99 float sqrt_det = sqrt(abs(det));
101 float2 t = (B + float2(sqrt_det, -sqrt_det)) / A;
103 float2 isValid = step(float2(-radius1, -radius1), t * diff.z);
105 float upper_t = lerp(t.y, t.x, isValid.x);
107 // Addressing mode bug work-around.. first let's see if we should consider odd repetitions separately.
108 float oddeven = abs(fmod(floor(upper_t), 2)) * allow_odd;
110 // Now let's calculate even or odd addressing in a branchless manner.
111 float upper_t_repeated = ((upper_t - floor(upper_t)) * (1.0f - oddeven)) + ((ceil(upper_t) - upper_t) * oddeven);
113 float4 output = GradientTexture.Sample(GradientSampler, float2(upper_t * (1.0f - repeat_correct) + upper_t_repeated * repeat_correct, 0.5));
115 output.rgb *= output.a;
116 // Multiply the output color by the input mask for the operation.
117 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy);
119 // In order to compile for PS_4_0_level_9_3 we need to be branchless.
120 // This is essentially returning nothing, i.e. bailing early if:
121 // det < 0 || max(isValid.x, isValid.y) <= 0
122 return output * abs(step(max(isValid.x, isValid.y), 0) - 1.0f) * step(0, det);
125 float4 SampleRadialGradientA0PS(
126 float4 clipSpaceOutput : SV_POSITION,
127 float4 sceneSpaceOutput : SCENE_POSITION,
128 float4 texelSpaceInput0 : TEXCOORD0
131 // This simpler shader is used for the degenerate case where A is 0,
132 // i.e. we're actually solving a linear equation.
134 float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31,
135 sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32);
136 float3 dp = float3(p - center1, radius1);
138 // dpx * dcx + dpy * dcy + r * dr
139 float B = dot(dp, diff);
141 float C = pow(dp.x, 2) + pow(dp.y, 2) - pow(radius1, 2);
143 float t = 0.5 * C / B;
145 // Addressing mode bug work-around.. first let's see if we should consider odd repetitions separately.
146 float oddeven = abs(fmod(floor(t), 2)) * allow_odd;
148 // Now let's calculate even or odd addressing in a branchless manner.
149 float t_repeated = ((t - floor(t)) * (1.0f - oddeven)) + ((ceil(t) - t) * oddeven);
151 float4 output = GradientTexture.Sample(GradientSampler, float2(t * (1.0f - repeat_correct) + t_repeated * repeat_correct, 0.5));
153 output.rgb *= output.a;
154 // Multiply the output color by the input mask for the operation.
155 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy);
157 // In order to compile for PS_4_0_level_9_3 we need to be branchless.
158 // This is essentially returning nothing, i.e. bailing early if:
159 // -radius1 >= t * diff.z
160 return output * abs(step(t * diff.z, -radius1) - 1.0f);