#ifndef RENDER_DRAW_H #define RENDER_DRAW_H // NOTE: the whole ui is one array of 32 byte instances (four per 128 byte line on the m2), drawn // with one pipeline in as few instanced draws as there are clip rects. the vertex shader builds // the quad, the fragment shader evaluates an sdf, so a rounded rect with a border and a soft // shadow is still one instance. typedef enum DrawKind { DRAW_KIND_RECT = 0, DRAW_KIND_GLYPH = 1, DRAW_KIND_TRIANGLE = 2, DRAW_KIND_CIRCLE = 3, } DrawKind; typedef struct DrawInstance DrawInstance; // data, by kind (the kind is always data[1] >> 24): // glyph: data[0] = atlas x | atlas y << 16 (pixels), data[1] = w | h << 12 | kind << 24 // shape: data[0] = half(radius) | half(border) << 16, data[1] = half(softness) | kind << 24 struct DrawInstance { f32 rect[4]; // physical pixels x0 y0 x1 y1 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 << 14) // 512KB per copy, a full screen of tables is ~6k #define DRAW_MAX_BATCHES 1024 // one per clip change; tables clip per panel, not per cell #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; // most instances any frame used, shown in settings 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; // glyph coverage curve, pushed to the shader 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); // NOTE: glyph quads are already physical and pixel snapped // (atlas_x, atlas_y) is the bitmap's top left in the atlas, its size is the quad's 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