feat(viz): renderer shapes/iconos/flechas/edge-styles (issue 0049f)

graph_renderer 1.5.0:
- 6 shapes SDF (circle, square, diamond, hex, triangle, rounded square)
  con dispatch en fragment shader y AA via fwidth.
- Atlas opcional de iconos Tabler bakeado por graph_icons; el shader
  compone overlay desde un uniform vec4 u_icon_uvs[256]. Setter publico
  graph_renderer_set_icon_atlas(r, tex, uv_table, count).
- Aristas direccionales: 6 vertices por arista (line + chevron de la
  flecha) en una sola draw call; segmento principal acortado por el
  radio del nodo target.
- Edge styles solid/dashed/dotted via descarte por arc_length en el
  fragment shader; las lineas del chevron son siempre solidas.

graph_icons 1.0.0 (nuevo):
- Atlas RGBA8 512x512 = grid 16x16 (256 iconos max) bakeado con
  stb_truetype desde tabler-icons.ttf.
- API: graph_icons_build/texture/region/uv_table/destroy. icon_id es
  1-based; 0 reservado para "sin icono".
- Hook FN_GRAPH_ICONS_SKIP_GL=1 para tests sin contexto GL.

Demo demos_graph_styles en primitives_gallery: 6 EntityTypes (uno por
shape) con icono Tabler representativo + 3 RelationTypes (knows/uses/
owns) con flechas direccionales y los 3 estilos.

test_graph_icons: 6 casos cubriendo bake, regiones 1-indexed, uv_table
consistente, layout en grid 16x16, validacion de count fuera de rango,
y verificacion de alpha != 0 en las celdas tras bake.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-29 23:01:49 +02:00
parent eb2078ac9a
commit ac11300335
14 changed files with 1180 additions and 174 deletions
+354 -170
View File
@@ -21,40 +21,41 @@
// demos que aun no construyen tablas EntityType.
// ---------------------------------------------------------------------------
static const uint32_t k_fallback_palette[10] = {
0xFF4CAF50u, // green
0xFFF44336u, // red
0xFF2196F3u, // blue
0xFFFF9800u, // orange
0xFF9C27B0u, // purple
0xFF00BCD4u, // cyan
0xFFFFEB3Bu, // yellow
0xFFE91E63u, // pink
0xFF795548u, // brown
0xFF607D8Bu, // blue-grey
0xFF4CAF50u, 0xFFF44336u, 0xFF2196F3u, 0xFFFF9800u, 0xFF9C27B0u,
0xFF00BCD4u, 0xFFFFEB3Bu, 0xFFE91E63u, 0xFF795548u, 0xFF607D8Bu,
};
// Maximo de iconos que cabe en el uniform array del shader. 256 es lo que
// genera `graph_icons` (grid 16×16 en 512×512). Subirlo requiere mas budget
// de uniforms (vec4×N → 4 floats por entrada) y aun cabe holgado en el
// limite GL 3.30 de 1024 vec4 por bloque.
static constexpr int k_max_icons = 256;
// ---------------------------------------------------------------------------
// Per-instance / per-vertex data layouts
// ---------------------------------------------------------------------------
// Tier 1 packing: el color va como uint32 unico en lugar de 4 floats. Reduce
// el bandwidth de upload en 60% para nodos (28 → 16 bytes/instance) y 50%
// para aristas (24 → 12 bytes/vertex), y elimina la conversion ABGR→4floats
// en CPU (los uint32 ya tienen el layout de unpackUnorm4x8 en little-endian).
struct NodeInstance { // 16 bytes
float x, y; // world position
float size; // diameter
uint32_t color; // packed RGBA8
// 0049f: NodeInstance crece de 16 a 24 bytes para llevar shape + icon_id.
// `shape_icon` empaqueta shape (8 bits bajos) + icon_id (16 bits siguientes);
// los UVs del icono no viajan por instancia — el shader los busca en un
// `uniform vec4 u_icon_uvs[256]` indexado por icon_id-1. Asi conservamos
// bandwidth aunque haya muchos nodos con el mismo icono.
struct NodeInstance { // 24 bytes (alineado a 4)
float x, y; // 8
float size; // 4 (= diametro en pixels world-space)
uint32_t color; // 4
uint32_t shape_icon; // 4 — (shape & 0xFF) | (icon_id << 8)
uint32_t pad_; // 4 — relleno explicito; reservado para flags futuros
};
// Tier 2 (issue 0049d): aristas via vertex pulling. El buffer es estatico —
// solo `(source_idx, target_idx, color, flags)` por arista, 16 bytes — y
// se reuploads solo cuando cambia el grafo. El vertex shader hace fetch de
// las posiciones desde un TBO RG32F que SI se actualiza por frame.
struct EdgeStatic { // 16 bytes
uint32_t source; // index into nodes
uint32_t target; // index into nodes
uint32_t color; // packed RGBA8 (sin pre-multiplicar — el shader aplica edge_alpha)
uint32_t flags; // reservado para flechas/styles futuros
// 0049f: EdgeStatic crece de 16 a 20 bytes para llevar style + flags reales.
// `style_flags`: flags (low 8 bits) | style (next 8 bits). El resto sigue
// siendo source/target/color como en 0049d.
struct EdgeStatic { // 20 bytes
uint32_t source; // index into nodes
uint32_t target; // index into nodes
uint32_t color; // packed RGBA8
uint32_t style_flags; // (flags & 0xFF) | (style << 8)
uint32_t pad_; // pad a multiplo de 4 — actualmente sin uso
};
// ---------------------------------------------------------------------------
@@ -63,17 +64,22 @@ struct EdgeStatic { // 16 bytes
struct GraphRenderer {
unsigned int fbo;
unsigned int texture;
unsigned int rbo; // depth/stencil renderbuffer
unsigned int rbo;
int width, height;
// Node rendering (instanced quads)
unsigned int node_vao, node_quad_vbo, node_instance_vbo;
unsigned int node_shader;
int node_u_viewport_loc;
int node_u_scale_loc;
int node_u_translate_loc;
int node_u_outline_loc;
int node_u_node_px_loc;
int node_u_icon_atlas_loc;
int node_u_has_icons_loc;
int node_u_icon_uvs_loc;
// Edge rendering (vertex pulling — issue 0049d)
// edge_vao : VAO con atributos por-instancia (divisor=1) leyendo de edge_static_vbo
// edge_vbo : buffer estatico (uno por grafo) con (source, target, color, flags)
// node_pos_buf / node_pos_tex : TBO RG32F que el vertex shader muestrea via texelFetch
// Edge rendering (vertex pulling con 6 vertices/instancia para flecha)
unsigned int edge_vao, edge_vbo;
unsigned int edge_shader;
unsigned int node_pos_buf;
@@ -84,31 +90,30 @@ struct GraphRenderer {
int edge_u_alpha_loc;
int edge_u_node_pos_loc;
// Streaming buffer capacities (in bytes). Grow x2 cuando used > capacity.
// Mantenemos el VBO orphaned con glBufferData(NULL, capacity) y luego
// hacemos glBufferSubData con los bytes realmente usados — evita el
// sync stall del driver y reduce las reallocaciones a O(log N).
// Streaming buffer capacities (in bytes).
size_t node_vbo_capacity;
size_t node_pos_capacity; // bytes del TBO RG32F
size_t edge_static_capacity; // bytes del buffer estatico de aristas
size_t node_pos_capacity;
size_t edge_static_capacity;
// CPU staging buffers — se reusan entre frames; crecen igual que el VBO.
// CPU staging
NodeInstance* node_staging;
size_t node_staging_cap; // en NodeInstances, no bytes
float* node_pos_staging; // 2 floats (x,y) por nodo
size_t node_pos_staging_cap; // en floats
size_t node_staging_cap;
float* node_pos_staging;
size_t node_pos_staging_cap;
EdgeStatic* edge_static_staging;
size_t edge_static_staging_cap; // en EdgeStatic
size_t edge_static_staging_cap;
// Cache para detectar cambios del grafo y reuploadear el edge_vbo
// estatico solo entonces. Identificamos el grafo por (puntero, count);
// basta para los flujos actuales (graph_viewport recrea el array al
// recargar). Cuando GraphData gane un campo `revision` se sustituira.
// Edge cache (reupload solo cuando cambia el grafo)
const void* cached_edges_ptr;
int cached_edge_count; // edges del grafo en el ultimo upload
int cached_edges_drawn; // edges realmente subidos (post-filtro)
int cached_edge_count;
int cached_edges_drawn;
bool edges_uploaded;
// Icon atlas binding (0 = sin iconos)
unsigned int icon_atlas_tex;
float icon_uvs[k_max_icons * 4];
int icon_uv_count;
GraphRendererConfig config;
};
@@ -116,27 +121,28 @@ struct GraphRenderer {
// Shader sources
// ---------------------------------------------------------------------------
// Node vertex shader — instanced unit quad
// a_color es uint32 packeado (R,G,B,A) — unpackUnorm4x8 esta en GLSL 4.20+,
// pero en core 3.30 lo hacemos manualmente con bit shifts. Eso mantiene
// compatibilidad con drivers que no exponen GL 4.x sin tener que tocar
// fn_framework.
// Node vertex shader — instanced unit quad, ahora con shape + icon_id.
// Pasamos el texto del shape al fragment para que despache el SDF correcto;
// los UVs del icono se buscan en `u_icon_uvs[icon_id-1]`.
static const char* k_node_vert = R"(
#version 330 core
// Quad corners [-0.5, 0.5]
layout(location = 0) in vec2 a_quad;
layout(location = 0) in vec2 a_quad;
// Per-instance: world position, size, packed RGBA8 color.
layout(location = 1) in vec2 a_pos;
layout(location = 2) in float a_size;
layout(location = 3) in uint a_color;
layout(location = 4) in uint a_shape_icon;
out vec2 v_uv;
out vec4 v_color;
out vec2 v_uv;
out vec4 v_color;
flat out uint v_shape;
flat out uint v_icon_id;
flat out vec4 v_icon_uv;
uniform vec2 u_viewport; // (width, height) in pixels
uniform float u_scale; // cam_zoom
uniform vec2 u_translate; // (tx, ty) in pixels
uniform vec2 u_viewport;
uniform float u_scale;
uniform vec2 u_translate;
uniform vec4 u_icon_uvs[256];
vec4 unpack_rgba8(uint c) {
return vec4(
@@ -153,64 +159,145 @@ void main() {
vec2 ndc = (screen / u_viewport) * 2.0 - 1.0;
ndc.y = -ndc.y;
gl_Position = vec4(ndc, 0.0, 1.0);
v_uv = a_quad + 0.5;
v_color = unpack_rgba8(a_color);
v_uv = a_quad + 0.5;
v_color = unpack_rgba8(a_color);
v_shape = a_shape_icon & 0xFFu;
v_icon_id = (a_shape_icon >> 8) & 0xFFFFu;
if (v_icon_id != 0u) {
v_icon_uv = u_icon_uvs[int(v_icon_id) - 1];
} else {
v_icon_uv = vec4(0.0);
}
}
)";
// Node fragment shader — SDF circle with outline
// Node fragment shader — SDF dispatch + opcional icon overlay.
// Para mantener la calidad del AA usamos `fwidth(d)` en lugar del
// `1.5/u_node_px` viejo: sirve igual a cualquier zoom y se queda nitido en
// los bordes complejos (hexagono, triangulo).
static const char* k_node_frag = R"(
#version 330 core
in vec2 v_uv;
in vec4 v_color;
flat in uint v_shape;
flat in uint v_icon_id;
flat in vec4 v_icon_uv;
out vec4 frag_color;
uniform float u_outline_px; // outline width in uv units
uniform float u_node_px; // node diameter in pixels (= size * zoom)
uniform float u_outline_px;
uniform float u_node_px;
uniform sampler2D u_icon_atlas;
uniform int u_has_icons;
float sdf_circle(vec2 uv) {
return length(uv - 0.5) - 0.5;
}
float sdf_square(vec2 uv) {
vec2 d = abs(uv - 0.5) - 0.5;
return max(d.x, d.y);
}
float sdf_diamond(vec2 uv) {
vec2 d = abs(uv - 0.5);
return d.x + d.y - 0.5;
}
// Hexagono regular alineado horizontalmente; SDF derivado del clasico de
// Inigo Quilez adaptado al cuadrado [0,1]^2. Inscribimos el hex dentro del
// circulo de radio 0.5 para que sus vertices toquen los bordes — asi
// area visual ~ a la del circulo del mismo `size`.
float sdf_hex(vec2 uv) {
vec2 p = abs(uv - 0.5);
const vec2 k = vec2(0.866025404, 0.5);
p -= 2.0 * min(dot(k, p), 0.0) * k;
p -= vec2(clamp(p.x, -k.y * 0.5, k.y * 0.5), 0.5);
return length(p) * sign(p.y) - (0.5 * 0.866025404);
}
// Triangulo equilatero apuntando hacia arriba dentro de [0,1]^2.
float sdf_triangle(vec2 uv) {
const float k = 1.732050808; // sqrt(3)
vec2 p = uv - vec2(0.5, 0.5);
p.x = abs(p.x) - 0.5;
p.y = p.y + 0.5 / k;
if (p.x + k * p.y > 0.0) p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
p.x -= clamp(p.x, -1.0, 0.0);
return -length(p) * sign(p.y);
}
float sdf_rrect(vec2 uv) {
float r = 0.18;
vec2 d = abs(uv - 0.5) - (0.5 - r);
return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - r;
}
float pick_sdf(uint shape, vec2 uv) {
// shape 0 = SHAPE_USE_TYPE: el CPU resuelve antes; aqui no debe llegar.
// 1=circle 2=square 3=diamond 4=hex 5=triangle 6=rounded_square
if (shape == 1u) return sdf_circle(uv);
else if (shape == 2u) return sdf_square(uv);
else if (shape == 3u) return sdf_diamond(uv);
else if (shape == 4u) return sdf_hex(uv);
else if (shape == 5u) return sdf_triangle(uv);
else if (shape == 6u) return sdf_rrect(uv);
return sdf_circle(uv); // default robusto si shape mal codificado
}
void main() {
float dist = length(v_uv - 0.5);
float r = 0.5;
float fwidth_uv = 1.5 / max(u_node_px, 1.0);
float alpha = 1.0 - smoothstep(r - fwidth_uv, r, dist);
if (alpha < 0.001) discard;
float d = pick_sdf(v_shape, v_uv);
float aa = max(fwidth(d), 0.001);
float fill_alpha = 1.0 - smoothstep(-aa, 0.0, d);
if (fill_alpha < 0.001) discard;
// Outline: anillo exterior — la anchura en uv viene de outline_px / node_px.
float outline_uv = u_outline_px / max(u_node_px, 1.0);
float outline = smoothstep(r - outline_uv - fwidth_uv, r - outline_uv, dist);
float outline = smoothstep(-outline_uv - aa, -outline_uv, d);
vec3 fill = v_color.rgb;
vec3 outline_col = mix(fill, vec3(1.0), 0.6);
vec3 color = mix(fill, outline_col, outline);
frag_color = vec4(color, v_color.a * alpha);
vec3 col = mix(fill, outline_col, outline);
// Overlay del icono (solo si hay atlas + icon_id != 0). El icono se
// tintamos sumando blanco modulado por su alpha — el resultado sigue
// siendo legible sobre cualquier color de fondo del nodo.
if (u_has_icons != 0 && v_icon_id != 0u) {
vec2 atlas_uv = mix(v_icon_uv.xy, v_icon_uv.zw, v_uv);
vec4 ic = texture(u_icon_atlas, atlas_uv);
col = mix(col, vec3(1.0), ic.a * 0.85);
}
frag_color = vec4(col, v_color.a * fill_alpha);
}
)";
// Edge vertex shader — vertex pulling (issue 0049d).
// El buffer de aristas es estatico: solo indices y color. Las posiciones
// vienen del TBO `u_node_pos` (RG32F, vec2 por nodo). gl_VertexID indica si
// dibujamos el endpoint source (0) o target (1). Asi eliminamos el upload
// de `12 floats × E` por frame que dominaba el coste de aristas.
// Edge vertex shader — vertex pulling, ahora 6 vertices por arista para
// soportar flecha en aristas EF_DIRECTED:
// gl_VertexID 0 (line src→tip), 1 (tip)
// gl_VertexID 2 (tip), 3 (back_left)
// gl_VertexID 4 (tip), 5 (back_right)
// Si no esta directed, los vertices 2..5 se colapsan al tip (lineas
// degeneradas no visibles).
//
// Nota: usamos divisor=1 en los 4 atributos y `glDrawArraysInstanced(LINES,
// 0, 2, edge_count)` — cada instancia rinde una linea de 2 vertices, los
// atributos se mantienen constantes en la instancia y `gl_VertexID` cicla
// 0..1 dentro de ella.
//
// `samplerBuffer` y `texelFetch(samplerBuffer, int)` estan en GLSL 1.40+;
// 330 core nos vale (no necesitamos 4.30 — el issue exageraba).
// Para dashed/dotted: pasamos `arc_length` interpolado (en pixels) al
// fragment shader; este descarta segun style.
static const char* k_edge_vert = R"(
#version 330 core
layout(location = 0) in uint a_source;
layout(location = 1) in uint a_target;
layout(location = 2) in uint a_color;
// location 3 (flags) reservado en el buffer (16B alignment) pero no leido aqui.
layout(location = 3) in uint a_style_flags;
uniform samplerBuffer u_node_pos;
uniform vec2 u_viewport;
uniform float u_scale;
uniform vec2 u_translate;
uniform float u_alpha; // edge_alpha
uniform float u_alpha;
out vec4 v_color;
flat out uint v_style;
flat out uint v_segment; // 0=line, 1=arrow
out float v_arc;
vec4 unpack_rgba8(uint c) {
return vec4(
@@ -221,27 +308,97 @@ vec4 unpack_rgba8(uint c) {
) * (1.0 / 255.0);
}
vec2 to_screen(vec2 wpos) {
return wpos * u_scale + u_translate;
}
vec2 to_ndc(vec2 screen) {
vec2 ndc = (screen / u_viewport) * 2.0 - 1.0;
return vec2(ndc.x, -ndc.y);
}
void main() {
int idx = (gl_VertexID & 1) == 0 ? int(a_source) : int(a_target);
vec2 wpos = texelFetch(u_node_pos, idx).xy;
vec2 screen = wpos * u_scale + u_translate;
vec2 ndc = (screen / u_viewport) * 2.0 - 1.0;
ndc.y = -ndc.y;
gl_Position = vec4(ndc, 0.0, 1.0);
int vid = gl_VertexID;
uint flags = a_style_flags & 0xFFu;
uint style = (a_style_flags >> 8) & 0xFFu;
bool directed = (flags & 1u) != 0u; // EF_DIRECTED == 1
vec2 wsrc = texelFetch(u_node_pos, int(a_source)).xy;
vec2 wtgt = texelFetch(u_node_pos, int(a_target)).xy;
vec2 ssrc = to_screen(wsrc);
vec2 stgt = to_screen(wtgt);
vec2 dir = stgt - ssrc;
float seg_len = length(dir);
vec2 dir_n = (seg_len > 0.0001) ? dir / seg_len : vec2(1.0, 0.0);
vec2 perp = vec2(-dir_n.y, dir_n.x);
// Acortamos el segmento principal si la arista es directed para que la
// flecha no se incruste en el nodo target. Tamano fijo en pixels = 10.
float arrow_px = 10.0;
vec2 tip = stgt;
vec2 line_end = directed ? (stgt - dir_n * arrow_px * 0.5) : stgt;
vec2 spos;
float arc;
uint segment;
if (vid <= 1) {
// Linea principal source→line_end.
segment = 0u;
if (vid == 0) { spos = ssrc; arc = 0.0; }
else { spos = line_end; arc = length(line_end - ssrc); }
} else {
segment = 1u;
arc = 0.0;
if (!directed) {
// Sin flecha: degenerado en el tip — sin pintar.
spos = tip;
} else {
// Triangulo de la flecha en 2 lineas (chevron):
// (tip, back_left) y (tip, back_right)
vec2 back = tip - dir_n * arrow_px;
vec2 left_p = back + perp * arrow_px * 0.5;
vec2 right_p = back - perp * arrow_px * 0.5;
if (vid == 2) spos = tip;
else if (vid == 3) spos = left_p;
else if (vid == 4) spos = tip;
else spos = right_p;
}
}
gl_Position = vec4(to_ndc(spos), 0.0, 1.0);
vec4 c = unpack_rgba8(a_color);
c.a *= u_alpha;
v_color = c;
v_color = c;
v_style = style;
v_segment = segment;
v_arc = arc;
}
)";
// Edge fragment shader
// Edge fragment shader — descarta segun style + arc_length para producir
// dashed (period 8 px, duty 0.5) o dotted (period 4 px, duty 0.25). Las
// lineas de la flecha (segment==1) se renderizan siempre solidas.
static const char* k_edge_frag = R"(
#version 330 core
in vec4 v_color;
flat in uint v_style;
flat in uint v_segment;
in float v_arc;
out vec4 frag_color;
void main() {
if (v_segment == 0u) {
// EDGE_USE_TYPE=0, EDGE_SOLID=1, EDGE_DASHED=2, EDGE_DOTTED=3
if (v_style == 2u) {
if (mod(v_arc, 8.0) > 4.0) discard;
} else if (v_style == 3u) {
if (mod(v_arc, 4.0) > 1.0) discard;
}
}
frag_color = v_color;
}
)";
@@ -256,7 +413,7 @@ static unsigned int compile_shader(GLenum type, const char* src) {
int ok;
glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
if (!ok) {
char buf[512];
char buf[1024];
glGetShaderInfoLog(s, sizeof(buf), nullptr, buf);
fprintf(stderr, "[graph_renderer] shader compile error: %s\n", buf);
}
@@ -273,7 +430,7 @@ static unsigned int link_program(const char* vert_src, const char* frag_src) {
int ok;
glGetProgramiv(prog, GL_LINK_STATUS, &ok);
if (!ok) {
char buf[512];
char buf[1024];
glGetProgramInfoLog(prog, sizeof(buf), nullptr, buf);
fprintf(stderr, "[graph_renderer] program link error: %s\n", buf);
}
@@ -315,10 +472,6 @@ static void destroy_fbo(GraphRenderer* r) {
// ---------------------------------------------------------------------------
// Capacity-tracked streaming helpers
// ---------------------------------------------------------------------------
// Doblar la capacidad cada vez que el upload supera el VBO. Asi las
// reallocaciones quedan en O(log N) en el peor caso y en >0 en el regimen
// estable. Capacidad inicial razonable: 4096 nodos / aristas (segun el .md
// del issue) — la primera llamada paga el redimensionado si hay mas.
static size_t grow_capacity(size_t current, size_t needed, size_t initial) {
size_t cap = current > 0 ? current : initial;
while (cap < needed) cap *= 2;
@@ -348,11 +501,12 @@ GraphRenderer* graph_renderer_create(int width, int height, const GraphRendererC
r->cached_edge_count = 0;
r->cached_edges_drawn = 0;
r->edges_uploaded = false;
r->icon_atlas_tex = 0;
r->icon_uv_count = 0;
std::memset(r->icon_uvs, 0, sizeof(r->icon_uvs));
// --- FBO ---
create_fbo(r);
// --- Node VAO ---
static const float quad_verts[8] = {
-0.5f, -0.5f,
0.5f, -0.5f,
@@ -363,42 +517,39 @@ GraphRenderer* graph_renderer_create(int width, int height, const GraphRendererC
glGenVertexArrays(1, &r->node_vao);
glBindVertexArray(r->node_vao);
// Quad VBO (location 0)
glGenBuffers(1, &r->node_quad_vbo);
glBindBuffer(GL_ARRAY_BUFFER, r->node_quad_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_verts), quad_verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
// Instance VBO — layout: NodeInstance (x, y, size, color_u32)
glGenBuffers(1, &r->node_instance_vbo);
glBindBuffer(GL_ARRAY_BUFFER, r->node_instance_vbo);
glEnableVertexAttribArray(1); // pos (2 float)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
sizeof(NodeInstance),
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(NodeInstance),
(void*)offsetof(NodeInstance, x));
glVertexAttribDivisor(1, 1);
glEnableVertexAttribArray(2); // size (1 float)
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE,
sizeof(NodeInstance),
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(NodeInstance),
(void*)offsetof(NodeInstance, size));
glVertexAttribDivisor(2, 1);
glEnableVertexAttribArray(3); // color (1 uint32) — IPointer, no normalizado
glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT,
sizeof(NodeInstance),
glEnableVertexAttribArray(3);
glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, sizeof(NodeInstance),
(void*)offsetof(NodeInstance, color));
glVertexAttribDivisor(3, 1);
glEnableVertexAttribArray(4);
glVertexAttribIPointer(4, 1, GL_UNSIGNED_INT, sizeof(NodeInstance),
(void*)offsetof(NodeInstance, shape_icon));
glVertexAttribDivisor(4, 1);
glBindVertexArray(0);
// --- Edge VAO (vertex pulling, divisor=1 sobre el buffer estatico) ---
glGenVertexArrays(1, &r->edge_vao);
glBindVertexArray(r->edge_vao);
glGenBuffers(1, &r->edge_vbo);
glBindBuffer(GL_ARRAY_BUFFER, r->edge_vbo);
// (source, target, color, flags) — los 4 con divisor=1.
glEnableVertexAttribArray(0);
glVertexAttribIPointer(0, 1, GL_UNSIGNED_INT, sizeof(EdgeStatic),
(void*)offsetof(EdgeStatic, source));
@@ -411,15 +562,15 @@ GraphRenderer* graph_renderer_create(int width, int height, const GraphRendererC
glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, sizeof(EdgeStatic),
(void*)offsetof(EdgeStatic, color));
glVertexAttribDivisor(2, 1);
// location 3 reservado en el buffer pero no enabled — el shader actual
// no lo lee. Mantenemos el slot para futuros estilos/flechas.
glEnableVertexAttribArray(3);
glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, sizeof(EdgeStatic),
(void*)offsetof(EdgeStatic, style_flags));
glVertexAttribDivisor(3, 1);
glBindVertexArray(0);
// --- TBO de posiciones de nodos (RG32F, vec2 por nodo) ---
glGenBuffers(1, &r->node_pos_buf);
glBindBuffer(GL_TEXTURE_BUFFER, r->node_pos_buf);
// Reservamos capacidad inicial; se redimensiona en draw segun N.
glBufferData(GL_TEXTURE_BUFFER, 4096 * 2 * sizeof(float), nullptr, GL_STREAM_DRAW);
r->node_pos_capacity = 4096 * 2 * sizeof(float);
@@ -429,12 +580,18 @@ GraphRenderer* graph_renderer_create(int width, int height, const GraphRendererC
glBindTexture(GL_TEXTURE_BUFFER, 0);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
// --- Shaders ---
r->node_shader = link_program(k_node_vert, k_node_frag);
r->edge_shader = link_program(k_edge_vert, k_edge_frag);
// Cachear locations de uniforms del edge shader (issue 0049d): se
// resuelven una vez en lugar de glGetUniformLocation cada frame.
r->node_u_viewport_loc = glGetUniformLocation(r->node_shader, "u_viewport");
r->node_u_scale_loc = glGetUniformLocation(r->node_shader, "u_scale");
r->node_u_translate_loc = glGetUniformLocation(r->node_shader, "u_translate");
r->node_u_outline_loc = glGetUniformLocation(r->node_shader, "u_outline_px");
r->node_u_node_px_loc = glGetUniformLocation(r->node_shader, "u_node_px");
r->node_u_icon_atlas_loc = glGetUniformLocation(r->node_shader, "u_icon_atlas");
r->node_u_has_icons_loc = glGetUniformLocation(r->node_shader, "u_has_icons");
r->node_u_icon_uvs_loc = glGetUniformLocation(r->node_shader, "u_icon_uvs");
r->edge_u_viewport_loc = glGetUniformLocation(r->edge_shader, "u_viewport");
r->edge_u_scale_loc = glGetUniformLocation(r->edge_shader, "u_scale");
r->edge_u_translate_loc = glGetUniformLocation(r->edge_shader, "u_translate");
@@ -471,21 +628,37 @@ void graph_renderer_resize(GraphRenderer* r, int width, int height) {
create_fbo(r);
}
void graph_renderer_set_icon_atlas(GraphRenderer* r,
unsigned int texture_id,
const float* uv_table,
int count) {
if (!r) return;
r->icon_atlas_tex = texture_id;
r->icon_uv_count = (count > k_max_icons) ? k_max_icons : (count < 0 ? 0 : count);
if (r->icon_uv_count > 0 && uv_table) {
std::memcpy(r->icon_uvs, uv_table,
(size_t)r->icon_uv_count * 4 * sizeof(float));
}
// Limpia las entradas no usadas para evitar UVs basura si el shader las
// sample por error. Costo: O(k_max_icons) — irrelevante.
if (r->icon_uv_count < k_max_icons) {
std::memset(r->icon_uvs + r->icon_uv_count * 4, 0,
(size_t)(k_max_icons - r->icon_uv_count) * 4 * sizeof(float));
}
}
unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
float cam_x, float cam_y, float cam_zoom) {
if (!r) return 0;
// --- Save GL state ---
GLint prev_fbo;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
GLint prev_viewport[4];
glGetIntegerv(GL_VIEWPORT, prev_viewport);
// --- Bind FBO ---
glBindFramebuffer(GL_FRAMEBUFFER, r->fbo);
glViewport(0, 0, r->width, r->height);
// Clear with bg_color (interpreted as RGBA8 packed — same memory layout)
uint8_t br, bg, bb, ba;
unpack_rgba8(r->config.bg_color, br, bg, bb, ba);
glClearColor(br / 255.0f, bg / 255.0f, bb / 255.0f, ba / 255.0f);
@@ -494,13 +667,10 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// View transform: world -> screen pixels
float scale = cam_zoom;
float tx = -cam_x * scale + (float)r->width * 0.5f;
float ty = -cam_y * scale + (float)r->height * 0.5f;
// Frustum cull AABB en world coords. Margen del 10% para que un nodo o
// arista a punto de entrar en pantalla no haga pop-in al moverse.
float half_w = ((float)r->width * 0.5f) / std::max(scale, 0.0001f);
float half_h = ((float)r->height * 0.5f) / std::max(scale, 0.0001f);
const float margin = 0.10f;
@@ -510,9 +680,7 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
float vy1 = cam_y + half_h * (1.0f + margin);
// ----------------------------------------------------------------
// Subir posiciones de nodos al TBO (vec2 por nodo). Lo necesitamos
// tanto si dibujamos aristas (vertex pulling) como antes de dibujar
// nodos — pero se calcula una sola vez por frame.
// Subir posiciones de nodos al TBO.
// ----------------------------------------------------------------
bool tbo_ready = false;
if (graph.node_count > 0 && graph.nodes) {
@@ -532,18 +700,15 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
4096 * 2 * sizeof(float));
}
glBindBuffer(GL_TEXTURE_BUFFER, r->node_pos_buf);
// Orphan + subdata: misma estrategia que en 0049c, evita stall.
glBufferData(GL_TEXTURE_BUFFER, (GLsizeiptr)r->node_pos_capacity, nullptr, GL_STREAM_DRAW);
glBufferSubData(GL_TEXTURE_BUFFER, 0, (GLsizeiptr)used_bytes, r->node_pos_staging);
// glTexBuffer ya esta vinculado al buffer en create — el view sigue
// valido tras orphan: GL_TEXTURE_BUFFER referencia al BO por nombre.
glBindBuffer(GL_TEXTURE_BUFFER, 0);
tbo_ready = true;
}
// ----------------------------------------------------------------
// Aristas via vertex pulling. El buffer estatico solo se reupload
// cuando el grafo cambia — detectamos con (puntero, count).
// Aristas via vertex pulling. 6 vertices por arista (line + arrow).
// El buffer estatico se reupload solo cuando cambia el grafo.
// ----------------------------------------------------------------
if (tbo_ready && graph.edge_count > 0 && graph.edges) {
const bool graph_changed =
@@ -552,9 +717,6 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
|| r->cached_edge_count != graph.edge_count;
if (graph_changed) {
// (Re)build el buffer estatico. Skipeamos aristas con indices
// fuera de rango — pueden aparecer durante una recarga parcial
// del grafo y no queremos que el GPU lea fuera del TBO.
if ((size_t)graph.edge_count > r->edge_static_staging_cap) {
size_t new_cap = grow_capacity(r->edge_static_staging_cap,
(size_t)graph.edge_count, 8192);
@@ -568,17 +730,17 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
if (e.source >= (uint32_t)graph.node_count) continue;
if (e.target >= (uint32_t)graph.node_count) continue;
if (!(e.flags & EF_VISIBLE)) continue;
// Saltamos aristas cuyos endpoints no estan visibles —
// el shader las pintaria igualmente (las posiciones siguen
// en el TBO) pero la aristas tendrian apariencia conectando
// "vacios" si los nodos estan ocultos por NF_VISIBLE off.
if (!(graph.nodes[e.source].flags & NF_VISIBLE)) continue;
if (!(graph.nodes[e.target].flags & NF_VISIBLE)) continue;
uint32_t col = resolve_edge_color(e, graph.rel_types,
graph.rel_type_count);
if (col == 0u) col = pack_rgba8(0x88, 0x88, 0x88, 0xFF);
r->edge_static_staging[out++] = { e.source, e.target, col, 0u };
uint8_t style = resolve_edge_style(e, graph.rel_types,
graph.rel_type_count);
uint32_t style_flags = ((uint32_t)e.flags & 0xFFu)
| ((uint32_t)style << 8);
r->edge_static_staging[out++] = { e.source, e.target, col, style_flags, 0u };
}
if (out > 0) {
const size_t used_bytes = out * sizeof(EdgeStatic);
@@ -606,7 +768,6 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
glUniform2f(r->edge_u_translate_loc, tx, ty);
glUniform1f(r->edge_u_alpha_loc, r->config.edge_alpha);
// Bind TBO al sampler u_node_pos en la texture unit 0.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_BUFFER, r->node_pos_tex);
glUniform1i(r->edge_u_node_pos_loc, 0);
@@ -614,20 +775,18 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
glLineWidth(r->config.edge_width);
glBindVertexArray(r->edge_vao);
// Una "instancia" = 1 linea (2 vertices). gl_VertexID dentro
// de la instancia es 0 o 1 → elige endpoint source o target.
glDrawArraysInstanced(GL_LINES, 0, 2, (GLsizei)r->cached_edges_drawn);
// 6 vertices por instancia: 2 linea + 4 chevron de la flecha.
glDrawArraysInstanced(GL_LINES, 0, 6, (GLsizei)r->cached_edges_drawn);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_BUFFER, 0);
}
} else if (graph.edge_count == 0) {
// Si el caller borra todas las aristas, invalidamos el cache para
// que el siguiente upload reconstruya el buffer.
r->edges_uploaded = false;
}
// ----------------------------------------------------------------
// Draw nodes (instanced quads, frustum-culled)
// Draw nodes (instanced quads, frustum-culled). Empaqueta shape e
// icon_id por instancia; el shader despacha el SDF y aplica overlay.
// ----------------------------------------------------------------
if (graph.node_count > 0 && graph.nodes) {
if ((size_t)graph.node_count > r->node_staging_cap) {
@@ -641,16 +800,12 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
const GraphNode& n = graph.nodes[i];
if (!(n.flags & NF_VISIBLE)) continue;
float sz = resolve_node_size(n, graph.types, graph.type_count);
float sz = resolve_node_size(n, graph.types, graph.type_count);
if (sz <= 0.0f) sz = 4.0f;
float half = sz * 0.5f;
// AABB del nodo: centro ± half. Skip si fuera del viewport.
if (n.x + half < vx0 || n.x - half > vx1) continue;
if (n.y + half < vy0 || n.y - half > vy1) continue;
// Apariencia: 1) override del nodo, 2) EntityType, 3) fallback
// indexado por type_id (paleta de 10 — sustituye al community
// del modelo v1).
uint32_t ncol;
if (n.color_override != 0u) {
ncol = n.color_override;
@@ -659,8 +814,22 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
} else {
ncol = k_fallback_palette[n.type_id % 10];
}
// 0049f anadira el dispatch de shape; por ahora todos circulos.
r->node_staging[visible++] = { n.x, n.y, sz, ncol };
uint8_t shape = resolve_node_shape(n, graph.types, graph.type_count);
if (shape == SHAPE_USE_TYPE) shape = SHAPE_CIRCLE;
// icon_id solo viene del EntityType (los nodos no tienen override
// de icono en el modelo actual). 0 = sin overlay.
uint16_t icon_id = 0;
if (graph.types && n.type_id < (uint16_t)graph.type_count) {
icon_id = graph.types[n.type_id].icon_id;
}
if (icon_id > r->icon_uv_count) icon_id = 0; // fuera de tabla
uint32_t shape_icon = ((uint32_t)shape & 0xFFu)
| ((uint32_t)icon_id << 8);
r->node_staging[visible++] = { n.x, n.y, sz, ncol, shape_icon, 0u };
}
if (visible > 0) {
@@ -671,26 +840,41 @@ unsigned int graph_renderer_draw(GraphRenderer* r, const GraphData& graph,
}
glUseProgram(r->node_shader);
glUniform2f(glGetUniformLocation(r->node_shader, "u_viewport"),
(float)r->width, (float)r->height);
glUniform1f(glGetUniformLocation(r->node_shader, "u_scale"), scale);
glUniform2f(glGetUniformLocation(r->node_shader, "u_translate"), tx, ty);
glUniform1f(glGetUniformLocation(r->node_shader, "u_outline_px"), r->config.node_outline);
glUniform2f(r->node_u_viewport_loc, (float)r->width, (float)r->height);
glUniform1f(r->node_u_scale_loc, scale);
glUniform2f(r->node_u_translate_loc, tx, ty);
glUniform1f(r->node_u_outline_loc, r->config.node_outline);
float avg_px = 8.0f * scale;
glUniform1f(r->node_u_node_px_loc, avg_px);
// Subimos siempre la tabla de UVs — son 256 vec4 = 4KB, peanuts.
glUniform4fv(r->node_u_icon_uvs_loc, k_max_icons, r->icon_uvs);
const int has_icons = (r->icon_atlas_tex != 0 && r->icon_uv_count > 0) ? 1 : 0;
glUniform1i(r->node_u_has_icons_loc, has_icons);
if (has_icons) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, r->icon_atlas_tex);
glUniform1i(r->node_u_icon_atlas_loc, 1);
}
glBindVertexArray(r->node_vao);
glBindBuffer(GL_ARRAY_BUFFER, r->node_instance_vbo);
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)r->node_vbo_capacity, nullptr, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, (GLsizeiptr)used_bytes, r->node_staging);
float avg_px = 8.0f * scale; // estimacion para el AA del SDF
glUniform1f(glGetUniformLocation(r->node_shader, "u_node_px"), avg_px);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, (GLsizei)visible);
glBindVertexArray(0);
if (has_icons) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
}
}
}
// --- Restore GL state ---
glDisable(GL_BLEND);
glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)prev_fbo);
glViewport(prev_viewport[0], prev_viewport[1], prev_viewport[2], prev_viewport[3]);