// BUBO + Pyre — bare-metal GPU demo
// Renders 3D on bare metal, dumps frames over UART as PPM.
// N8toriousAi, 2026.

#include "pyre/pyre_api.h"
#include "uart.h"

#define GPU_W 400
#define GPU_H 400

// framebuffer in .bss (no malloc on bare metal)
static pyre_rgb gpu_color[GPU_W * GPU_H];
static float   gpu_depth[GPU_W * GPU_H];
static pyre_ctx gpu_ctx;
static int gpu_ready = 0;

// ---- cube geometry ----
static pyre_vertex cube[36];
static int cube_n = 0;

static void push_tri(vec3 a, vec3 b, vec3 c, pyre_rgb col) {
    if (cube_n + 3 > 36) return;
    cube[cube_n++] = (pyre_vertex){a, col};
    cube[cube_n++] = (pyre_vertex){b, col};
    cube[cube_n++] = (pyre_vertex){c, col};
}

static void build_cube(void) {
    if (cube_n > 0) return;
    pyre_rgb crimson  = {255, 45, 85};
    pyre_rgb ember    = {217, 64, 20};
    pyre_rgb gold     = {212, 175, 55};
    pyre_rgb dark     = {40, 12, 18};
    pyre_rgb top      = {200, 50, 60};
    float s = 0.8f;
    vec3 v[8] = {
        v3(-s,-s,-s), v3(s,-s,-s), v3(s,s,-s), v3(-s,s,-s),
        v3(-s,-s, s), v3(s,-s, s), v3(s,s, s), v3(-s,s, s),
    };
    push_tri(v[4], v[5], v[6], crimson); push_tri(v[4], v[6], v[7], crimson);
    push_tri(v[1], v[0], v[3], ember);   push_tri(v[1], v[3], v[2], ember);
    push_tri(v[5], v[1], v[2], gold);    push_tri(v[5], v[2], v[6], gold);
    push_tri(v[0], v[4], v[7], dark);   push_tri(v[0], v[7], v[3], dark);
    push_tri(v[7], v[6], v[2], top);    push_tri(v[7], v[2], v[3], top);
    push_tri(v[0], v[1], v[5], dark);   push_tri(v[0], v[5], v[4], dark);
}

void pyre_bubo_init(void) {
    build_cube();
    pyre_init(&gpu_ctx, GPU_W, GPU_H, gpu_color, gpu_depth);
    pyre_set_view(&gpu_ctx, m4_lookat(v3(0, 0.6f, 3.2f), v3(0,0,0), v3(0,1,0)));
    pyre_set_proj(&gpu_ctx, m4_perspective(55.0f, 1.0f, 0.1f, 100.0f));
    gpu_ready = 1;
}

// render cube at rotation angle (radians), returns tris drawn
int pyre_bubo_render(float angle_rad) {
    if (!gpu_ready) pyre_bubo_init();
    pyre_set_model(&gpu_ctx, m4_rot_y(angle_rad));
    pyre_rgb bg = {8, 6, 10};
    pyre_clear(&gpu_ctx, bg);
    return pyre_draw_tris(&gpu_ctx, cube, cube_n);
}

// ---- UART helpers ----
static void uart_puts_hex(unsigned char v) {
    const char *h = "0123456789ABCDEF";
    uart_putc(h[(v >> 4) & 0xF]);
    uart_putc(h[v & 0xF]);
}

static void uart_put_dec(int v) {
    char buf[12]; int i = 0;
    if (v == 0) { uart_putc('0'); return; }
    if (v < 0) { uart_putc('-'); v = -v; }
    while (v > 0 && i < 11) { buf[i++] = '0' + (v % 10); v /= 10; }
    while (i > 0) uart_putc(buf[--i]);
}

// dump framebuffer as binary PPM over UART
void pyre_bubo_dump_ppm(void) {
    if (!gpu_ready) pyre_bubo_init();
    uart_puts("P6\n");
    uart_put_dec(GPU_W); uart_putc(' ');
    uart_put_dec(GPU_H); uart_putc('\n');
    uart_puts("255\n");
    // raw bytes
    for (int i = 0; i < GPU_W * GPU_H; i++) {
        uart_putc((char)gpu_color[i].r);
        uart_putc((char)gpu_color[i].g);
        uart_putc((char)gpu_color[i].b);
    }
    uart_putc('\n');
}

void pyre_bubo_info(void) {
    uart_puts("Pyre GPU v0.1 on BUBO bare metal\n");
    uart_puts("  framebuffer: ");
    uart_put_dec(GPU_W); uart_putc('x'); uart_put_dec(GPU_H);
    uart_puts("  (");
    uart_put_dec(GPU_W * GPU_H * 3 / 1024);
    uart_puts(" KB color + ");
    uart_put_dec(GPU_W * GPU_H * 4 / 1024);
    uart_puts(" KB depth)\n");
    uart_puts("  pipeline: vertex xform -> barycentric raster -> depth test\n");
    uart_puts("  shading: flat + Gouraud, backface cull\n");
}
