// Pyre Neural BRDF — bare-metal MLP inference
// Runs the trained material network as a fragment shader.
// No libc, no heap. Just fused multiply-add.
// N8toriousAi, 2026.

#ifndef PYRE_NEURAL_H
#define PYRE_NEURAL_H

#include "neural_brdf_weights.h"  // generated by train_brdf.py
#include "pyre_math.h"
#include "pyre_raster.h"

// ReLU
static inline float pyre_relu(float x) {
    return x > 0.0f ? x : 0.0f;
}

// Sigmoid via lookup table (no libm, fast, accurate enough)
// Table covers [-8, 8], 256 entries
static float pyre_sigmoid_table[256];
static int pyre_sigmoid_ready = 0;

static void pyre_sigmoid_init(void) {
    if (pyre_sigmoid_ready) return;
    for (int i = 0; i < 256; i++) {
        float x = -8.0f + (16.0f * (float)i) / 255.0f;
        // compute sigmoid via continued fraction — accurate
        // 1/(1+e^-x): use e^-x = 1/e^x, e^x via exp2
        // simpler: iterative refinement
        float ex;
        if (x >= 0) {
            // e^-x for x>=0: use series on -x (converges, |x|<=8 split)
            float ax = x;
            // range reduce: e^-x = (e^(-x/8))^8
            float h = -ax / 8.0f;
            float e_h = 1.0f + h + h*h/2.0f + h*h*h/6.0f + h*h*h*h/24.0f
                      + h*h*h*h*h/120.0f + h*h*h*h*h*h/720.0f;
            ex = e_h*e_h; ex *= ex; ex *= ex; // ^8
        } else {
            float ax = -x;
            float h = ax / 8.0f;
            float e_h = 1.0f + h + h*h/2.0f + h*h*h/6.0f + h*h*h*h/24.0f
                      + h*h*h*h*h/120.0f + h*h*h*h*h*h/720.0f;
            ex = e_h*e_h; ex *= ex; ex *= ex; // e^|x| = e^(-x) for x<0
        }
        pyre_sigmoid_table[i] = 1.0f / (1.0f + ex);
    }
    pyre_sigmoid_ready = 1;
}

static inline float pyre_sigmoid(float x) {
    if (!pyre_sigmoid_ready) pyre_sigmoid_init();
    if (x <= -8.0f) return 0.0f;
    if (x >=  8.0f) return 1.0f;
    float t = (x + 8.0f) * (255.0f / 16.0f);
    int i = (int)t;
    if (i >= 255) return 1.0f;
    float f = t - (float)i;
    return pyre_sigmoid_table[i] * (1.0f - f) + pyre_sigmoid_table[i+1] * f;
}

// Neural fragment shader.
// Input: surface normal (world), light dir, view dir.
// Output: RGB shaded by the learned Harakiri material.
static inline pyre_rgb pyre_neural_shade(vec3 n, vec3 light_dir, vec3 view_dir) {
    // input layer: nx,ny,nz, lx,ly,lz, vx,vy
    float in[8] = {n.x, n.y, n.z,
                   light_dir.x, light_dir.y, light_dir.z,
                   view_dir.x, view_dir.y};

    // hidden 1: 8 -> 16
    float h1[16];
    for (int i = 0; i < 16; i++) {
        float s = brdf_b1[i];
        for (int j = 0; j < 8; j++)
            s += brdf_w1[i*8 + j] * in[j];
        h1[i] = pyre_relu(s);
    }

    // hidden 2: 16 -> 16
    float h2[16];
    for (int i = 0; i < 16; i++) {
        float s = brdf_b2[i];
        for (int j = 0; j < 16; j++)
            s += brdf_w2[i*16 + j] * h1[j];
        h2[i] = pyre_relu(s);
    }

    // output: 16 -> 3
    pyre_rgb out;
    float r = brdf_b3[0], g = brdf_b3[1], b = brdf_b3[2];
    for (int j = 0; j < 16; j++) {
        r += brdf_w3[0*16 + j] * h2[j];
        g += brdf_w3[1*16 + j] * h2[j];
        b += brdf_w3[2*16 + j] * h2[j];
    }
    out.r = (unsigned char)(pyre_sigmoid(r) * 255.0f);
    out.g = (unsigned char)(pyre_sigmoid(g) * 255.0f);
    out.b = (unsigned char)(pyre_sigmoid(b) * 255.0f);
    return out;
}

#endif
