summaryrefslogtreecommitdiff
path: root/gsk/resources/vulkan/linear.vert.glsl
blob: 7a55c61380f97e93bf8cd02217c1629427461622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#version 420 core

#include "constants.glsl"

struct ColorStop {
  float offset;
  vec4 color;
};

layout(location = 0) in vec4 inRect;
layout(location = 1) in vec2 inStart;
layout(location = 2) in vec2 inEnd;
layout(location = 3) in int inRepeating;
layout(location = 4) in int inStopCount;
layout(location = 5) in vec4 inOffsets0;
layout(location = 6) in vec4 inOffsets1;
layout(location = 7) in vec4 inColors0;
layout(location = 8) in vec4 inColors1;
layout(location = 9) in vec4 inColors2;
layout(location = 10) in vec4 inColors3;
layout(location = 11) in vec4 inColors4;
layout(location = 12) in vec4 inColors5;
layout(location = 13) in vec4 inColors6;
layout(location = 14) in vec4 inColors7;

layout(location = 0) out float outGradientPos;
layout(location = 1) out flat int outRepeating;
layout(location = 2) out flat int outStopCount;
layout(location = 3) out flat ColorStop outStops[8];

out gl_PerVertex {
  vec4 gl_Position;
};

vec2 offsets[6] = { vec2(0.0, 0.0),
                    vec2(1.0, 0.0),
                    vec2(0.0, 1.0),
                    vec2(0.0, 1.0),
                    vec2(1.0, 0.0),
                    vec2(1.0, 1.0) };

vec4 intersect(vec4 a, vec4 b)
{
  a = vec4(a.xy, a.xy + a.zw);
  b = vec4(b.xy, b.xy + b.zw);
  vec4 result = vec4(max(a.xy, b.xy), min(a.zw, b.zw));
  if (any (greaterThanEqual (result.xy, result.zw)))
    return vec4(0.0,0.0,0.0,0.0);
  return vec4(result.xy, result.zw - result.xy);
}

float
get_gradient_pos (vec2 pos)
{
  pos = pos - inStart;
  vec2 grad = inEnd - inStart;

  return dot (pos, grad) / dot (grad, grad);
}

void main() {
  vec4 rect = intersect(inRect, push.clip_bounds);
  vec2 pos = rect.xy + rect.zw * offsets[gl_VertexIndex];
  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
  outGradientPos = get_gradient_pos (pos);
  outRepeating = inRepeating;
  outStopCount = inStopCount;
  outStops[0].offset = inOffsets0[0];
  outStops[0].color = inColors0 * vec4(inColors0.aaa, 1.0);
  outStops[1].offset = inOffsets0[1];
  outStops[1].color = inColors1 * vec4(inColors1.aaa, 1.0);
  outStops[2].offset = inOffsets0[2];
  outStops[2].color = inColors2 * vec4(inColors2.aaa, 1.0);
  outStops[3].offset = inOffsets0[3];
  outStops[3].color = inColors3 * vec4(inColors3.aaa, 1.0);
  outStops[4].offset = inOffsets1[0];
  outStops[4].color = inColors4 * vec4(inColors4.aaa, 1.0);
  outStops[5].offset = inOffsets1[1];
  outStops[5].color = inColors5 * vec4(inColors5.aaa, 1.0);
  outStops[6].offset = inOffsets1[2];
  outStops[6].color = inColors6 * vec4(inColors6.aaa, 1.0);
  outStops[7].offset = inOffsets1[3];
  outStops[7].color = inColors7 * vec4(inColors7.aaa, 1.0);
}