import '../../../assert/js/assert.js';
import '../../../globals.js';
// Copyright 2023-2024, University of Colorado Boulder
/**
 * A stack of values (effectively vec4s) that are used by RenderInstructions to evaluate a RenderProgram.
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import { alpenglow } from '../imports.js';
// TODO: We'll plan to use Float32Array to test WebGPU compatibility and epsilons
const ExecutionArrayType = Float64Array;
const STACK_SIZE = 16;
const DATA_SIZE = STACK_SIZE * 4;
export default class RenderExecutionStack {
    constructor() {
        this.data = new ExecutionArrayType(DATA_SIZE);
        this.dataLength = 0;
    }
    reset() {
        this.dataLength = 0;
    }
    getLength() {
        return this.dataLength / 4;
    }
    getCurrentIndex() {
        return this.dataLength;
    }
    getLastIndex() {
        return this.dataLength - 4;
    }
    push(vector) {
        const index = this.dataLength;
        this.dataLength += 4;
        assert && assert(this.dataLength <= DATA_SIZE, 'Stack overflow');
        this.data[index] = vector.x;
        this.data[index + 1] = vector.y;
        this.data[index + 2] = vector.z;
        this.data[index + 3] = vector.w;
        return vector;
    }
    pushValues(x, y, z, w) {
        const index = this.dataLength;
        this.dataLength += 4;
        assert && assert(this.dataLength <= DATA_SIZE, 'Stack overflow');
        this.data[index] = x;
        this.data[index + 1] = y;
        this.data[index + 2] = z;
        this.data[index + 3] = w;
    }
    // We're padding for now (for simplicity)
    pushNumber(n) {
        const index = this.dataLength;
        this.dataLength += 4;
        assert && assert(this.dataLength <= DATA_SIZE, 'Stack overflow');
        this.data[index] = n;
    }
    popInto(vector) {
        this.dataLength -= 4;
        const index = this.dataLength;
        assert && assert(index >= 0, 'Stack underflow');
        vector.setXYZW(this.data[index], this.data[index + 1], this.data[index + 2], this.data[index + 3]);
        return vector;
    }
    // Just returns the X component
    popNumber() {
        this.dataLength -= 4;
        const index = this.dataLength;
        assert && assert(index >= 0, 'Stack underflow');
        return this.data[index];
    }
    writeTop(vector) {
        const index = this.dataLength - 4;
        assert && assert(index >= 0, 'Stack underflow');
        this.data[index] = vector.x;
        this.data[index + 1] = vector.y;
        this.data[index + 2] = vector.z;
        this.data[index + 3] = vector.w;
        return vector;
    }
    writeTopValues(x, y, z, w) {
        const index = this.dataLength - 4;
        assert && assert(index >= 0, 'Stack underflow');
        this.data[index] = x;
        this.data[index + 1] = y;
        this.data[index + 2] = z;
        this.data[index + 3] = w;
    }
    readTop(vector) {
        const index = this.dataLength - 4;
        assert && assert(index >= 0, 'Stack underflow');
        vector.setXYZW(this.data[index], this.data[index + 1], this.data[index + 2], this.data[index + 3]);
        return vector;
    }
}
alpenglow.register('RenderExecutionStack', RenderExecutionStack);
//# sourceMappingURL=RenderExecutionStack.js.map