#ifndef RENDER_DRAW_H #define RENDER_DRAW_H // NOTE: the whole ui and diagram canvas are one array of 32 byte instances, drawn with one // pipeline in as few instanced draws as there are clip rects. the vertex shader builds the quad, // the fragment shader evaluates exact SDFs (rounded boxes, circles, diamonds, arbitrary-angle // solid/dashed capsules, oriented UML arrowheads, and atlas glyphs). typedef enum DrawKind { DRAW_KIND_RECT = 0, DRAW_KIND_GLYPH = 1, DRAW_KIND_TRIANGLE = 2, DRAW_KIND_CIRCLE = 3, DRAW_KIND_LINE = 4, DRAW_KIND_DIAMOND = 5, DRAW_KIND_ARROW = 6, } DrawKind; typedef enum DrawArrowStyle { DRAW_ARROW_TRIANGLE = 0, // filled if border == 0, hollow if border > 0 DRAW_ARROW_CHEVRON = 1, // open UML arrow (border = stroke thickness) DRAW_ARROW_DIAMOND = 2, // filled (composition) if border == 0, hollow (aggregation) if border > 0 } DrawArrowStyle; typedef struct DrawInstance DrawInstance; // data, by kind (the kind is always data[1] >> 24): // glyph: data[0] = atlas x | atlas y << 16, data[1] = w | h << 12 | 1 << 24 // shape: data[0] = half(radius) | half(border) << 16, data[1] = half(softness) | kind << 24 // line: data[0] = half(half_w) | half(dash_period)<<16, data[1] = half(softness) | phase_u8<<16 | 4<<24 // arrow: data[0] = half(half_w) | half(border) << 16, data[1] = half(softness) | style_u8<<16 | 6<<24 struct DrawInstance { f32 rect[4]; // physical pixels x0 y0 x1 y1 (or segment/arrow endpoints) u32 data[2]; u32 colors[2]; // top, bottom, packed for unpackUnorm4x8 }; static_assert(sizeof(DrawInstance) == 32, "instance layout must match ui.vert"); typedef struct DrawBatch DrawBatch; struct DrawBatch { u32 first; u32 count; s32 clip[4]; // x y w h, physical }; typedef struct Rect Rect; struct Rect { f32 x0, y0, x1, y1; }; #define DRAW_MAX_INSTANCES (1u << 17) // 131k instances (4MB), handles 2000+ node diagrams in 1 draw #define DRAW_MAX_BATCHES 2048 #define DRAW_MAX_CLIPS 16 typedef struct DrawList DrawList; struct DrawList { DrawInstance *instances; // the frame's mapped gpu buffer while rendering (vk_frame_begin), else own DrawInstance *own; u32 count; u32 peak; DrawBatch batches[DRAW_MAX_BATCHES]; u32 batch_count; Rect clips[DRAW_MAX_CLIPS]; u32 clip_depth; f32 scale; // logical -> physical f32 rounding; // multiplies every rect corner radius, 0 = square corners (theme) f32 text_gamma; u32 width; // physical u32 height; }; //- colors are written 0xRRGGBBAA in code internal inline u32 color_pack(u32 rgba) { return ((rgba >> 24) & 0xFF) | (((rgba >> 16) & 0xFF) << 8) | (((rgba >> 8) & 0xFF) << 16) | ((rgba & 0xFF) << 24); } internal inline u32 color_alpha(u32 rgba, f32 alpha) { u32 a = (u32)((f32)(rgba & 0xFF) * Clamp(alpha, 0.0f, 1.0f) + 0.5f); return (rgba & 0xFFFFFF00u) | a; } internal u32 color_lerp(u32 a, u32 b, f32 t); internal inline Rect rect_make(f32 x, f32 y, f32 w, f32 h) { return Rect{x, y, x + w, y + h}; } internal inline f32 rect_w(Rect r) { return r.x1 - r.x0; } internal inline f32 rect_h(Rect r) { return r.y1 - r.y0; } internal inline b32 rect_contains(Rect r, f32 x, f32 y) { return x >= r.x0 && x < r.x1 && y >= r.y0 && y < r.y1; } internal inline Rect rect_shrink(Rect r, f32 amount) { return Rect{r.x0 + amount, r.y0 + amount, r.x1 - amount, r.y1 - amount}; } internal Rect rect_intersect(Rect a, Rect b); internal void draw_init(Arena *arena, DrawList *list); internal void draw_begin(DrawList *list, u32 width, u32 height, f32 scale); internal void draw_clip_push(DrawList *list, Rect clip); internal void draw_clip_pop(DrawList *list); internal b32 draw_clip_visible(DrawList *list, Rect rect); internal void draw_rect(DrawList *list, Rect rect, u32 color, f32 radius); internal void draw_rect_gradient(DrawList *list, Rect rect, u32 top, u32 bottom, f32 radius); internal void draw_border(DrawList *list, Rect rect, u32 color, f32 radius, f32 thickness); internal void draw_shadow(DrawList *list, Rect rect, u32 color, f32 radius, f32 blur); internal void draw_triangle(DrawList *list, Rect rect, u32 color); internal void draw_circle(DrawList *list, f32 cx, f32 cy, f32 r, u32 color); internal void draw_square(DrawList *list, f32 cx, f32 cy, f32 half, u32 color); internal void draw_ring(DrawList *list, f32 cx, f32 cy, f32 r, f32 thickness, u32 color); internal void draw_diamond(DrawList *list, Rect rect, u32 color, f32 border); internal void draw_line(DrawList *list, f32 x0, f32 y0, f32 x1, f32 y1, f32 thickness, u32 color); internal void draw_line_dashed(DrawList *list, f32 x0, f32 y0, f32 x1, f32 y1, f32 thickness, f32 dash_period, f32 dash_phase, u32 color); internal void draw_arrow(DrawList *list, f32 tail_x, f32 tail_y, f32 tip_x, f32 tip_y, f32 half_width, f32 border, DrawArrowStyle style, u32 color); internal void draw_bezier(DrawList *list, f32 x0, f32 y0, f32 cx0, f32 cy0, f32 cx1, f32 cy1, f32 x1, f32 y1, f32 thickness, f32 dash_period, f32 dash_phase, u32 color); internal void draw_glyph_physical(DrawList *list, f32 x0, f32 y0, f32 x1, f32 y1, u32 atlas_x, u32 atlas_y, u32 color); #endif // RENDER_DRAW_H