yandex-music-rs/shaders/gl/ui_sdf.vert
2026-11-27 13:17:37 +03:00

82 lines
2.8 KiB
GLSL

#version 330 core
// Strict OpenGL 3.3 Core Uber-SDF Instanced Quad Vertex Shader
// Zero SSBOs, zero OpenGL 4.x features: reads UiInstance via per-instance VBO attributes (divisor = 1).
uniform vec2 u_screen_size;
uniform float u_scale_factor;
layout(location = 0) in vec4 a_rect; // [x, y, w, h] or [x0, y0, x1, y1] for MODE_LINE
layout(location = 1) in vec4 a_radii; // [tl, tr, br, bl]
layout(location = 2) in vec4 a_uv_or_params; // [u0, v0, u1, v1] | shadow [ox, oy, blur, spread] | grad [dx, dy, 0, 0]
layout(location = 3) in vec4 a_clip_rect; // [x, y, w, h] (w <= 0 => no clip)
layout(location = 4) in uvec4 a_colors; // [color_start, color_end, border_color, reserved]
layout(location = 5) in vec4 a_metrics; // [border_width, clip_radius, extra0, extra1]
layout(location = 6) in uvec4 a_meta; // [mode, texture_id, flags, reserved]
const uint MODE_GLYPH = 2u;
const uint MODE_SHADOW = 5u;
const uint MODE_LINE = 6u;
out vec2 v_frag_pos;
flat out vec4 v_rect;
flat out vec4 v_radii;
flat out vec4 v_uv_or_params;
flat out vec4 v_clip_rect;
flat out uvec4 v_colors;
flat out vec4 v_metrics;
flat out uvec4 v_meta;
void main() {
vec2 corners[6] = vec2[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)
);
vec2 corner = corners[gl_VertexID];
float inv_scale = 1.0 / max(u_scale_factor, 1.0);
uint mode = a_meta.x;
vec2 bmin = a_rect.xy;
vec2 bmax = a_rect.xy + a_rect.zw;
if (mode == MODE_SHADOW) {
float ox = a_uv_or_params.x;
float oy = a_uv_or_params.y;
float blur = max(a_uv_or_params.z, 0.5);
float spread = a_uv_or_params.w;
float margin = blur * 3.0 + max(spread, 0.0) + 2.0 * inv_scale;
bmin = a_rect.xy + vec2(ox, oy) - vec2(margin);
bmax = a_rect.xy + a_rect.zw + vec2(ox, oy) + vec2(margin);
} else if (mode == MODE_LINE) {
vec2 a = a_rect.xy;
vec2 b = a_rect.zw;
float pad = a_metrics.x * 0.5 + 1.5 * inv_scale;
bmin = min(a, b) - vec2(pad);
bmax = max(a, b) + vec2(pad);
} else if (mode != MODE_GLYPH) {
float max_r = max(max(a_radii.x, a_radii.y), max(a_radii.z, a_radii.w));
if (max_r > 0.001 || a_metrics.x > 0.001) {
float pad = 1.0 * inv_scale;
bmin -= vec2(pad);
bmax += vec2(pad);
}
}
vec2 p = mix(bmin, bmax, corner);
vec2 sz = max(u_screen_size, vec2(1.0));
float ndc_x = (p.x / sz.x) * 2.0 - 1.0;
float ndc_y = 1.0 - (p.y / sz.y) * 2.0;
gl_Position = vec4(ndc_x, ndc_y, 0.0, 1.0);
v_frag_pos = p;
v_rect = a_rect;
v_radii = a_radii;
v_uv_or_params = a_uv_or_params;
v_clip_rect = a_clip_rect;
v_colors = a_colors;
v_metrics = a_metrics;
v_meta = a_meta;
}