gfx/2d/ShadersD2D1.hlsl

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial