1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#include shared,rect,render_task,gpu_cache,gradient
6
7#define PI                  3.141592653589793
8
9varying vec2 v_pos;
10
11flat varying vec2 v_center;
12flat varying float v_start_offset;
13flat varying float v_offset_scale;
14flat varying float v_angle;
15
16#ifdef WR_VERTEX_SHADER
17
18#define EXTEND_MODE_REPEAT 1
19
20PER_INSTANCE in vec4 aTaskRect;
21PER_INSTANCE in vec2 aCenter;
22PER_INSTANCE in vec2 aScale;
23PER_INSTANCE in float aStartOffset;
24PER_INSTANCE in float aEndOffset;
25PER_INSTANCE in float aAngle;
26PER_INSTANCE in int aExtendMode;
27PER_INSTANCE in int aGradientStopsAddress;
28
29void main(void) {
30    // Store 1/d where d = end_offset - start_offset
31    // If d = 0, we can't get its reciprocal. Instead, just use a zero scale.
32    float d = aEndOffset - aStartOffset;
33    v_offset_scale = d != 0.0 ? 1.0 / d : 0.0;
34
35    vec2 pos = mix(aTaskRect.xy, aTaskRect.zw, aPosition.xy);
36    gl_Position = uTransform * vec4(pos, 0.0, 1.0);
37
38    v_angle = PI / 2.0 - aAngle;
39    v_start_offset = aStartOffset * v_offset_scale;
40
41    // v_pos and v_center are in a coordinate space relative to the task rect
42    // (so they are independent of the task origin).
43    v_center = aCenter * v_offset_scale;
44    v_pos = (aTaskRect.zw - aTaskRect.xy) * aPosition.xy * v_offset_scale * aScale;
45
46    v_gradient_repeat = float(aExtendMode == EXTEND_MODE_REPEAT);
47    v_gradient_address = aGradientStopsAddress;
48}
49#endif
50
51
52#ifdef WR_FRAGMENT_SHADER
53
54void main(void) {
55    // Use inverse trig to find the angle offset from the relative position.
56    vec2 current_dir = v_pos - v_center;
57    float current_angle = atan(current_dir.y, current_dir.x) + v_angle;
58    float offset = fract(current_angle / (2.0 * PI)) * v_offset_scale - v_start_offset;
59
60    oFragColor = sample_gradient(offset);
61}
62
63#endif
64