gfx/2d/ShadersD2D1.hlsl

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:7becd66174aa
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
6 Texture2D InputTexture : register(t0);
7 SamplerState InputSampler : register(s0);
8 Texture2D GradientTexture : register(t1);
9 SamplerState GradientSampler : register(s1);
10
11 cbuffer 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 float3x2 transform : packoffset(c3.x);
20 }
21
22 float4 SampleRadialGradientPS(
23 float4 clipSpaceOutput : SV_POSITION,
24 float4 sceneSpaceOutput : SCENE_POSITION,
25 float4 texelSpaceInput0 : TEXCOORD0
26 ) : SV_Target
27 {
28 // Radial gradient painting is defined as the set of circles whose centers
29 // are described by C(t) = (C2 - C1) * t + C1; with radii
30 // R(t) = (R2 - R1) * t + R1; for R(t) > 0. This shader solves the
31 // quadratic equation that arises when calculating t for pixel (x, y).
32 //
33 // A more extensive derrivation can be found in the pixman radial gradient
34 // code.
35
36 float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31,
37 sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32);
38 float3 dp = float3(p - center1, radius1);
39
40 // dpx * dcx + dpy * dcy + r * dr
41 float B = dot(dp, diff);
42
43 float C = pow(dp.x, 2) + pow(dp.y, 2) - sq_radius1;
44
45 float det = pow(B, 2) - A * C;
46
47 float sqrt_det = sqrt(abs(det));
48
49 float2 t = (B + float2(sqrt_det, -sqrt_det)) / A;
50
51 float2 isValid = step(float2(-radius1, -radius1), t * diff.z);
52
53 float upper_t = lerp(t.y, t.x, isValid.x);
54
55 float4 output = GradientTexture.Sample(GradientSampler, float2(upper_t, 0.5));
56 // Premultiply
57 output.rgb *= output.a;
58 // Multiply the output color by the input mask for the operation.
59 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy);
60
61 // In order to compile for PS_4_0_level_9_3 we need to be branchless.
62 // This is essentially returning nothing, i.e. bailing early if:
63 // det < 0 || max(isValid.x, isValid.y) <= 0
64 return output * abs(step(max(isValid.x, isValid.y), 0) - 1.0f) * step(0, det);
65 };
66
67 float4 SampleRadialGradientA0PS(
68 float4 clipSpaceOutput : SV_POSITION,
69 float4 sceneSpaceOutput : SCENE_POSITION,
70 float4 texelSpaceInput0 : TEXCOORD0
71 ) : SV_Target
72 {
73 // This simpler shader is used for the degenerate case where A is 0,
74 // i.e. we're actually solving a linear equation.
75
76 float2 p = float2(sceneSpaceOutput.x * transform._11 + sceneSpaceOutput.y * transform._21 + transform._31,
77 sceneSpaceOutput.x * transform._12 + sceneSpaceOutput.y * transform._22 + transform._32);
78 float3 dp = float3(p - center1, radius1);
79
80 // dpx * dcx + dpy * dcy + r * dr
81 float B = dot(dp, diff);
82
83 float C = pow(dp.x, 2) + pow(dp.y, 2) - pow(radius1, 2);
84
85 float t = 0.5 * C / B;
86
87 float4 output = GradientTexture.Sample(GradientSampler, float2(t, 0.5));
88 // Premultiply
89 output.rgb *= output.a;
90 // Multiply the output color by the input mask for the operation.
91 output *= InputTexture.Sample(InputSampler, texelSpaceInput0.xy);
92
93 // In order to compile for PS_4_0_level_9_3 we need to be branchless.
94 // This is essentially returning nothing, i.e. bailing early if:
95 // -radius1 >= t * diff.z
96 return output * abs(step(t * diff.z, -radius1) - 1.0f);
97 };

mercurial