// Pyre GPU — software rasterizer
// Barycentric triangle rasterization with depth buffer.
// Bare-metal compatible: caller provides framebuffer memory.
// N8toriousAi, 2026.

#ifndef PYRE_RASTER_H
#define PYRE_RASTER_H

#include "pyre_math.h"

typedef struct { unsigned char r, g, b; } pyre_rgb;

typedef struct {
    int width, height;
    pyre_rgb *color;   // width*height
    float    *depth;   // width*height
} pyre_fb;

static inline void pyre_fb_clear(pyre_fb *fb, pyre_rgb clear) {
    int n = fb->width * fb->height;
    for (int i = 0; i < n; i++) {
        fb->color[i] = clear;
        fb->depth[i] = 1.0f;
    }
}

// edge function
static inline float pyre_edge(float ax, float ay, float bx, float by,
                              float cx, float cy) {
    return (cx - ax) * (by - ay) - (cy - ay) * (bx - ax);
}

// Draw one triangle. Vertices in screen space (pixels), z in 0..1.
// Flat-shaded with a single color. Returns pixels drawn.
static inline int pyre_tri(pyre_fb *fb,
                           float x0, float y0, float z0,
                           float x1, float y1, float z1,
                           float x2, float y2, float z2,
                           pyre_rgb color) {
    // bounding box
    float minx = x0, maxx = x0, miny = y0, maxy = y0;
    if (x1 < minx) minx = x1; if (x1 > maxx) maxx = x1;
    if (x2 < minx) minx = x2; if (x2 > maxx) maxx = x2;
    if (y1 < miny) miny = y1; if (y1 > maxy) maxy = y1;
    if (y2 < miny) miny = y2; if (y2 > maxy) maxy = y2;

    int ix0 = (int)minx; if (ix0 < 0) ix0 = 0;
    int iy0 = (int)miny; if (iy0 < 0) iy0 = 0;
    int ix1 = (int)maxx + 1; if (ix1 > fb->width)  ix1 = fb->width;
    int iy1 = (int)maxy + 1; if (iy1 > fb->height) iy1 = fb->height;

    float area = pyre_edge(x0, y0, x1, y1, x2, y2);
    if (area == 0.0f) return 0;
    // backface cull: y-flip makes front faces CW in screen space
    if (area < 0.0f) return 0;

    int drawn = 0;
    for (int y = iy0; y < iy1; y++) {
        for (int x = ix0; x < ix1; x++) {
            float px = (float)x + 0.5f;
            float py = (float)y + 0.5f;
            float w0 = pyre_edge(x1, y1, x2, y2, px, py);
            float w1 = pyre_edge(x2, y2, x0, y0, px, py);
            float w2 = pyre_edge(x0, y0, x1, y1, px, py);
            if (w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f) {
                w0 /= area; w1 /= area; w2 /= area;
                float z = w0*z0 + w1*z1 + w2*z2;
                int idx = y * fb->width + x;
                if (z < fb->depth[idx]) {
                    fb->depth[idx] = z;
                    fb->color[idx] = color;
                    drawn++;
                }
            }
        }
    }
    return drawn;
}

// Draw triangle with per-vertex colors (Gouraud interpolation)
static inline int pyre_tri_gouraud(pyre_fb *fb,
                           float x0, float y0, float z0, pyre_rgb c0,
                           float x1, float y1, float z1, pyre_rgb c1,
                           float x2, float y2, float z2, pyre_rgb c2) {
    float minx = x0, maxx = x0, miny = y0, maxy = y0;
    if (x1 < minx) minx = x1; if (x1 > maxx) maxx = x1;
    if (x2 < minx) minx = x2; if (x2 > maxx) maxx = x2;
    if (y1 < miny) miny = y1; if (y1 > maxy) maxy = y1;
    if (y2 < miny) miny = y2; if (y2 > maxy) maxy = y2;

    int ix0 = (int)minx; if (ix0 < 0) ix0 = 0;
    int iy0 = (int)miny; if (iy0 < 0) iy0 = 0;
    int ix1 = (int)maxx + 1; if (ix1 > fb->width)  ix1 = fb->width;
    int iy1 = (int)maxy + 1; if (iy1 > fb->height) iy1 = fb->height;

    float area = pyre_edge(x0, y0, x1, y1, x2, y2);
    if (area == 0.0f) return 0;
    if (area < 0.0f) return 0;

    int drawn = 0;
    for (int y = iy0; y < iy1; y++) {
        for (int x = ix0; x < ix1; x++) {
            float px = (float)x + 0.5f;
            float py = (float)y + 0.5f;
            float w0 = pyre_edge(x1, y1, x2, y2, px, py);
            float w1 = pyre_edge(x2, y2, x0, y0, px, py);
            float w2 = pyre_edge(x0, y0, x1, y1, px, py);
            if (w0 >= 0.0f && w1 >= 0.0f && w2 >= 0.0f) {
                w0 /= area; w1 /= area; w2 /= area;
                float z = w0*z0 + w1*z1 + w2*z2;
                int idx = y * fb->width + x;
                if (z < fb->depth[idx]) {
                    fb->depth[idx] = z;
                    pyre_rgb c;
                    c.r = (unsigned char)(w0*c0.r + w1*c1.r + w2*c2.r);
                    c.g = (unsigned char)(w0*c0.g + w1*c1.g + w2*c2.g);
                    c.b = (unsigned char)(w0*c0.b + w1*c1.b + w2*c2.b);
                    fb->color[idx] = c;
                    drawn++;
                }
            }
        }
    }
    return drawn;
}

#endif
