roleplay/client/shader_src/ui.vert

131 lines
2.6 KiB
GLSL

#version 450
2024-10-14 14:16:59 -06:00
#extension GL_EXT_buffer_reference : require
2024-10-18 18:34:31 -06:00
struct String {
vec2 pos;
vec4 color;
float size;
uint offset;
uint length;
};
layout(std430, buffer_reference) readonly buffer StringList {
String s[];
2024-10-14 14:16:59 -06:00
};
struct Drawable {
2024-10-18 18:34:31 -06:00
vec2 pos;
vec2 size;
vec4 color;
uint type;
uint code;
2024-10-14 21:32:21 -06:00
};
layout(std430, buffer_reference) readonly buffer DrawableList {
Drawable d[];
2024-10-18 18:34:31 -06:00
};
layout(std430, buffer_reference) readonly buffer CodeList {
uint c[];
};
struct DrawCommand {
uint vertex_count;
uint instance_count;
uint fist_vertex;
uint first_instance;
};
struct DispatchCommand {
uint x;
uint y;
uint z;
};
layout(std430, buffer_reference) buffer UILayer {
StringList strings;
CodeList codes;
DrawableList drawables;
2024-10-18 18:34:31 -06:00
DrawCommand draw;
DispatchCommand dispatch;
2024-10-18 18:34:31 -06:00
uint font_index;
uint drawable_count;
2024-10-18 18:34:31 -06:00
};
struct Symbol {
int top;
uint left;
2024-10-14 14:16:59 -06:00
uint width;
uint height;
2024-10-18 18:34:31 -06:00
uint advance;
};
layout(std430, buffer_reference) buffer SymbolList {
Symbol s[];
};
2024-10-18 18:34:31 -06:00
struct Font {
SymbolList symbols;
uint num_symbols;
uint width;
uint height;
};
2024-10-18 18:34:31 -06:00
layout(std430, buffer_reference) buffer FontList {
Font f[];
};
2024-10-18 18:34:31 -06:00
layout(std430, buffer_reference) buffer UIContext {
FontList fonts;
2024-10-18 18:34:31 -06:00
mat4 screen;
};
2024-10-18 18:34:31 -06:00
layout(std430, push_constant) uniform PushConstant {
UIContext context;
UILayer layer;
} pc;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec2 fragUV;
2024-10-16 20:29:19 -06:00
layout(location = 2) out uint code;
layout(location = 3) out uint index;
layout(location = 4) out uint type;
const vec2 square[6] = {
vec2(0.0, 0.0),
vec2(1.0, 0.0),
vec2(0.0, 1.0),
vec2(1.0, 0.0),
vec2(1.0, 1.0),
vec2(0.0, 1.0),
};
void main() {
Drawable drawable = pc.layer.drawables.d[gl_InstanceIndex];
if(drawable.type == 0) {
// Rect
gl_Position = pc.context.screen * vec4(square[gl_VertexIndex] * drawable.size + drawable.pos, 0.0, 1.0);
} else if(drawable.type == 1){
// Glyph
Font font = pc.context.fonts.f[pc.layer.font_index];
Symbol symbol = font.symbols.s[drawable.code];
float fragU = square[gl_VertexIndex].x * symbol.width/font.width;
float fragV = square[gl_VertexIndex].y * symbol.height/font.height;
float x = (square[gl_VertexIndex].x * symbol.width + symbol.left) * drawable.size.x / font.width;
float y = (square[gl_VertexIndex].y * symbol.height - symbol.top) * drawable.size.x / font.height;
fragUV = vec2(fragU, fragV);
gl_Position = pc.context.screen * vec4(vec2(x, y) + drawable.pos, 0.0, 1.0);
}
fragColor = drawable.color;
code = drawable.code;
type = drawable.type;
index = pc.layer.font_index;
}