aboutsummaryrefslogtreecommitdiffstats
path: root/source/render/shaders/ui.vert
blob: 2d765c3212f1287ab34cd6618c0b0e0df831fd4e (plain) (blame)
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
#version 450

// NOTE: one instance = one quad. no vertex buffer for the corners, they come from
// gl_VertexIndex (triangle strip, 4 vertices). everything is in physical pixels.

layout(location = 0) in vec4  in_rect;    // x0 y0 x1 y1
layout(location = 1) in uvec2 in_data;    // packed per kind, see DrawInstance in render_draw.h
layout(location = 2) in uvec2 in_colors;  // top, bottom (rgba8, r in the low byte)

layout(push_constant) uniform PushConstants {
	vec2  viewport;
	float text_gamma;
	float inv_atlas;  // 1 / atlas size in pixels
} pc;

layout(location = 0) out vec2 v_local;  // position relative to the rect center
layout(location = 1) out vec2 v_half;   // half size of the rect
layout(location = 2) out vec2 v_uv;
layout(location = 3) flat out vec4 v_params;
layout(location = 4) out vec4 v_color;  // premultiplied

void main()
{
	vec2 corner = vec2(float(gl_VertexIndex & 1), float(gl_VertexIndex >> 1));

	// NOTE: unpack. kind is the top byte of data.y for every kind
	float kind = float(in_data.y >> 24);
	vec4  params;
	vec4  uv;
	if(kind == 1.0) {
		vec2 at   = vec2(float(in_data.x & 0xFFFFu), float(in_data.x >> 16));
		vec2 size = vec2(float(in_data.y & 0xFFFu), float((in_data.y >> 12) & 0xFFFu));
		uv        = vec4(at, at + size) * pc.inv_atlas;
		params    = vec4(0.0, 0.0, 0.0, kind);
	} else {
		vec2 radius_border = unpackHalf2x16(in_data.x);
		float softness     = unpackHalf2x16(in_data.y & 0xFFFFu).x;
		uv                 = vec4(0.0);
		params             = vec4(radius_border, softness, kind);
	}

	// NOTE: grow the quad by the softness so blurred shadows and the aa fringe aren't cut off
	float pad = (kind == 1.0) ? 0.0 : params.z + 1.0;
	// NOTE: x0 > x1 is allowed and mirrors the shape (left pointing triangles), so take min/max
	vec2  p0  = min(in_rect.xy, in_rect.zw) - pad;
	vec2  p1  = max(in_rect.xy, in_rect.zw) + pad;
	vec2  pos = mix(p0, p1, corner);

	vec2 center = (in_rect.xy + in_rect.zw) * 0.5;
	v_local     = pos - center;
	v_half      = (in_rect.zw - in_rect.xy) * 0.5;
	v_uv        = mix(uv.xy, uv.zw, corner);
	v_params    = params;

	float t     = clamp((pos.y - p0.y - pad) / max(p1.y - p0.y - 2.0 * pad, 1.0), 0.0, 1.0);
	vec4  color = mix(unpackUnorm4x8(in_colors.x), unpackUnorm4x8(in_colors.y), t);
	v_color     = vec4(color.rgb * color.a, color.a);

	gl_Position = vec4(pos / pc.viewport * 2.0 - 1.0, 0.0, 1.0);
}