import { I as warn, Y as uid, d as getAttributeInfoFromFormat, et as eventemitter3_default, n as Buffer, r as BufferUsage } from "./Geometry-D1ym7sCJ.mjs";
import { C as STENCIL_MODES, k as unsafeEvalSupported } from "./RenderTargetSystem-DqasEjth.mjs";
//#region node_modules/pixi.js/lib/rendering/high-shader/shader-bits/localUniformBit.mjs
const localUniformBit = {
	name: "local-uniform-bit",
	vertex: {
		header: `

            struct LocalUniforms {
                uTransformMatrix:mat3x3<f32>,
                uColor:vec4<f32>,
                uRound:f32,
            }

            @group(1) @binding(0) var<uniform> localUniforms : LocalUniforms;
        `,
		main: `
            vColor *= localUniforms.uColor;
            modelMatrix *= localUniforms.uTransformMatrix;
        `,
		end: `
            if(localUniforms.uRound == 1)
            {
                vPosition = vec4(roundPixels(vPosition.xy, globalUniforms.uResolution), vPosition.zw);
            }
        `
	}
};
const localUniformBitGroup2 = {
	...localUniformBit,
	vertex: {
		...localUniformBit.vertex,
		header: localUniformBit.vertex.header.replace("group(1)", "group(2)")
	}
};
const localUniformBitGl = {
	name: "local-uniform-bit",
	vertex: {
		header: `

            uniform mat3 uTransformMatrix;
            uniform vec4 uColor;
            uniform float uRound;
        `,
		main: `
            vColor *= uColor;
            modelMatrix = uTransformMatrix;
        `,
		end: `
            if(uRound == 1.)
            {
                gl_Position.xy = roundPixels(gl_Position.xy, uResolution);
            }
        `
	}
};
//#endregion
//#region node_modules/pixi.js/lib/rendering/high-shader/shader-bits/textureBit.mjs
const textureBit = {
	name: "texture-bit",
	vertex: {
		header: `

        struct TextureUniforms {
            uTextureMatrix:mat3x3<f32>,
        }

        @group(2) @binding(2) var<uniform> textureUniforms : TextureUniforms;
        `,
		main: `
            uv = (textureUniforms.uTextureMatrix * vec3(uv, 1.0)).xy;
        `
	},
	fragment: {
		header: `
            @group(2) @binding(0) var uTexture: texture_2d<f32>;
            @group(2) @binding(1) var uSampler: sampler;


        `,
		main: `
            outColor = textureSample(uTexture, uSampler, vUV);
        `
	}
};
const textureBitGl = {
	name: "texture-bit",
	vertex: {
		header: `
            uniform mat3 uTextureMatrix;
        `,
		main: `
            uv = (uTextureMatrix * vec3(uv, 1.0)).xy;
        `
	},
	fragment: {
		header: `
        uniform sampler2D uTexture;


        `,
		main: `
            outColor = texture(uTexture, vUV);
        `
	}
};
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/gl/shader/program/ensureAttributes.mjs
function ensureAttributes(geometry, extractedData) {
	for (const i in geometry.attributes) {
		const attribute = geometry.attributes[i];
		const attributeData = extractedData[i];
		if (attributeData) {
			attribute.format ?? (attribute.format = attributeData.format);
			attribute.offset ?? (attribute.offset = attributeData.offset);
			attribute.instance ?? (attribute.instance = attributeData.instance);
		} else warn(`Attribute ${i} is not present in the shader, but is present in the geometry. Unable to infer attribute details.`);
	}
	ensureStartAndStride(geometry);
}
function ensureStartAndStride(geometry) {
	const { buffers, attributes } = geometry;
	const tempStride = {};
	const tempStart = {};
	for (const j in buffers) {
		const buffer = buffers[j];
		tempStride[buffer.uid] = 0;
		tempStart[buffer.uid] = 0;
	}
	for (const j in attributes) {
		const attribute = attributes[j];
		tempStride[attribute.buffer.uid] += getAttributeInfoFromFormat(attribute.format).stride;
	}
	for (const j in attributes) {
		const attribute = attributes[j];
		attribute.stride ?? (attribute.stride = tempStride[attribute.buffer.uid]);
		attribute.start ?? (attribute.start = tempStart[attribute.buffer.uid]);
		tempStart[attribute.buffer.uid] += getAttributeInfoFromFormat(attribute.format).stride;
	}
}
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/gpu/state/GpuStencilModesToPixi.mjs
const GpuStencilModesToPixi = [];
GpuStencilModesToPixi[STENCIL_MODES.NONE] = void 0;
GpuStencilModesToPixi[STENCIL_MODES.DISABLED] = {
	stencilWriteMask: 0,
	stencilReadMask: 0
};
GpuStencilModesToPixi[STENCIL_MODES.RENDERING_MASK_ADD] = {
	stencilFront: {
		compare: "equal",
		passOp: "increment-clamp"
	},
	stencilBack: {
		compare: "equal",
		passOp: "increment-clamp"
	}
};
GpuStencilModesToPixi[STENCIL_MODES.RENDERING_MASK_REMOVE] = {
	stencilFront: {
		compare: "equal",
		passOp: "decrement-clamp"
	},
	stencilBack: {
		compare: "equal",
		passOp: "decrement-clamp"
	}
};
GpuStencilModesToPixi[STENCIL_MODES.MASK_ACTIVE] = {
	stencilWriteMask: 0,
	stencilFront: {
		compare: "equal",
		passOp: "keep"
	},
	stencilBack: {
		compare: "equal",
		passOp: "keep"
	}
};
GpuStencilModesToPixi[STENCIL_MODES.INVERSE_MASK_ACTIVE] = {
	stencilWriteMask: 0,
	stencilFront: {
		compare: "not-equal",
		passOp: "keep"
	},
	stencilBack: {
		compare: "not-equal",
		passOp: "keep"
	}
};
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/shared/shader/UboSystem.mjs
var UboSystem = class {
	constructor(adaptor) {
		/** Cache of uniform buffer layouts and sync functions, so we don't have to re-create them */
		this._syncFunctionHash = /* @__PURE__ */ Object.create(null);
		this._adaptor = adaptor;
		this._systemCheck();
	}
	/**
	* Overridable function by `pixi.js/unsafe-eval` to silence
	* throwing an error if platform doesn't support unsafe-evals.
	* @private
	*/
	_systemCheck() {
		if (!unsafeEvalSupported()) throw new Error("Current environment does not allow unsafe-eval, please use pixi.js/unsafe-eval module to enable support.");
	}
	ensureUniformGroup(uniformGroup) {
		const uniformData = this.getUniformGroupData(uniformGroup);
		uniformGroup.buffer || (uniformGroup.buffer = new Buffer({
			data: new Float32Array(uniformData.layout.size / 4),
			usage: BufferUsage.UNIFORM | BufferUsage.COPY_DST
		}));
	}
	getUniformGroupData(uniformGroup) {
		return this._syncFunctionHash[uniformGroup._signature] || this._initUniformGroup(uniformGroup);
	}
	_initUniformGroup(uniformGroup) {
		const uniformGroupSignature = uniformGroup._signature;
		let uniformData = this._syncFunctionHash[uniformGroupSignature];
		if (!uniformData) {
			const elements = Object.keys(uniformGroup.uniformStructures).map((i) => uniformGroup.uniformStructures[i]);
			const layout = this._adaptor.createUboElements(elements);
			const syncFunction = this._generateUboSync(layout.uboElements);
			uniformData = this._syncFunctionHash[uniformGroupSignature] = {
				layout,
				syncFunction
			};
		}
		return this._syncFunctionHash[uniformGroupSignature];
	}
	_generateUboSync(uboElements) {
		return this._adaptor.generateUboSync(uboElements);
	}
	syncUniformGroup(uniformGroup, data, offset) {
		const uniformGroupData = this.getUniformGroupData(uniformGroup);
		uniformGroup.buffer || (uniformGroup.buffer = new Buffer({
			data: new Float32Array(uniformGroupData.layout.size / 4),
			usage: BufferUsage.UNIFORM | BufferUsage.COPY_DST
		}));
		let dataInt32 = null;
		if (!data) {
			data = uniformGroup.buffer.data;
			dataInt32 = uniformGroup.buffer.dataInt32;
		}
		offset || (offset = 0);
		uniformGroupData.syncFunction(uniformGroup.uniforms, data, dataInt32, offset);
		return true;
	}
	updateUniformGroup(uniformGroup) {
		if (uniformGroup.isStatic && !uniformGroup._dirtyId) return false;
		uniformGroup._dirtyId = 0;
		const synced = this.syncUniformGroup(uniformGroup);
		uniformGroup.buffer.update();
		return synced;
	}
	destroy() {
		this._syncFunctionHash = null;
	}
};
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/shared/shader/utils/uniformParsers.mjs
const uniformParsers = [
	{
		type: "mat3x3<f32>",
		test: (data) => {
			return data.value.a !== void 0;
		},
		ubo: `
            var matrix = uv[name].toArray(true);
            data[offset] = matrix[0];
            data[offset + 1] = matrix[1];
            data[offset + 2] = matrix[2];
            data[offset + 4] = matrix[3];
            data[offset + 5] = matrix[4];
            data[offset + 6] = matrix[5];
            data[offset + 8] = matrix[6];
            data[offset + 9] = matrix[7];
            data[offset + 10] = matrix[8];
        `,
		uniform: `
            gl.uniformMatrix3fv(ud[name].location, false, uv[name].toArray(true));
        `
	},
	{
		type: "vec4<f32>",
		test: (data) => data.type === "vec4<f32>" && data.size === 1 && data.value.width !== void 0,
		ubo: `
            v = uv[name];
            data[offset] = v.x;
            data[offset + 1] = v.y;
            data[offset + 2] = v.width;
            data[offset + 3] = v.height;
        `,
		uniform: `
            cv = ud[name].value;
            v = uv[name];
            if (cv[0] !== v.x || cv[1] !== v.y || cv[2] !== v.width || cv[3] !== v.height) {
                cv[0] = v.x;
                cv[1] = v.y;
                cv[2] = v.width;
                cv[3] = v.height;
                gl.uniform4f(ud[name].location, v.x, v.y, v.width, v.height);
            }
        `
	},
	{
		type: "vec2<f32>",
		test: (data) => data.type === "vec2<f32>" && data.size === 1 && data.value.x !== void 0,
		ubo: `
            v = uv[name];
            data[offset] = v.x;
            data[offset + 1] = v.y;
        `,
		uniform: `
            cv = ud[name].value;
            v = uv[name];
            if (cv[0] !== v.x || cv[1] !== v.y) {
                cv[0] = v.x;
                cv[1] = v.y;
                gl.uniform2f(ud[name].location, v.x, v.y);
            }
        `
	},
	{
		type: "vec4<f32>",
		test: (data) => data.type === "vec4<f32>" && data.size === 1 && data.value.red !== void 0,
		ubo: `
            v = uv[name];
            data[offset] = v.red;
            data[offset + 1] = v.green;
            data[offset + 2] = v.blue;
            data[offset + 3] = v.alpha;
        `,
		uniform: `
            cv = ud[name].value;
            v = uv[name];
            if (cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue || cv[3] !== v.alpha) {
                cv[0] = v.red;
                cv[1] = v.green;
                cv[2] = v.blue;
                cv[3] = v.alpha;
                gl.uniform4f(ud[name].location, v.red, v.green, v.blue, v.alpha);
            }
        `
	},
	{
		type: "vec3<f32>",
		test: (data) => data.type === "vec3<f32>" && data.size === 1 && data.value.red !== void 0,
		ubo: `
            v = uv[name];
            data[offset] = v.red;
            data[offset + 1] = v.green;
            data[offset + 2] = v.blue;
        `,
		uniform: `
            cv = ud[name].value;
            v = uv[name];
            if (cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue) {
                cv[0] = v.red;
                cv[1] = v.green;
                cv[2] = v.blue;
                gl.uniform3f(ud[name].location, v.red, v.green, v.blue);
            }
        `
	}
];
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/shared/shader/utils/createUboSyncFunction.mjs
function createUboSyncFunction(uboElements, parserCode, arrayGenerationFunction, singleSettersMap) {
	const funcFragments = [`
        var v = null;
        var v2 = null;
        var t = 0;
        var index = 0;
        var name = null;
        var arrayOffset = null;
    `];
	let prev = 0;
	for (let i = 0; i < uboElements.length; i++) {
		const uboElement = uboElements[i];
		const name = uboElement.data.name;
		let parsed = false;
		let offset = 0;
		for (let j = 0; j < uniformParsers.length; j++) if (uniformParsers[j].test(uboElement.data)) {
			offset = uboElement.offset / 4;
			funcFragments.push(`name = "${name}";`, `offset += ${offset - prev};`, uniformParsers[j][parserCode] || uniformParsers[j].ubo);
			parsed = true;
			break;
		}
		if (!parsed) if (uboElement.data.size > 1) {
			offset = uboElement.offset / 4;
			funcFragments.push(arrayGenerationFunction(uboElement, offset - prev));
		} else {
			const template = singleSettersMap[uboElement.data.type];
			offset = uboElement.offset / 4;
			funcFragments.push(`
                    v = uv.${name};
                    offset += ${offset - prev};
                    ${template};
                `);
		}
		prev = offset;
	}
	const fragmentSrc = funcFragments.join("\n");
	return new Function("uv", "data", "dataInt32", "offset", fragmentSrc);
}
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/shared/shader/utils/uboSyncFunctions.mjs
function loopMatrix(col, row) {
	return `
        for (let i = 0; i < ${col * row}; i++) {
            data[offset + (((i / ${col})|0) * 4) + (i % ${col})] = v[i];
        }
    `;
}
const uboSyncFunctionsSTD40 = {
	f32: `
        data[offset] = v;`,
	i32: `
        dataInt32[offset] = v;`,
	"vec2<f32>": `
        data[offset] = v[0];
        data[offset + 1] = v[1];`,
	"vec3<f32>": `
        data[offset] = v[0];
        data[offset + 1] = v[1];
        data[offset + 2] = v[2];`,
	"vec4<f32>": `
        data[offset] = v[0];
        data[offset + 1] = v[1];
        data[offset + 2] = v[2];
        data[offset + 3] = v[3];`,
	"vec2<i32>": `
        dataInt32[offset] = v[0];
        dataInt32[offset + 1] = v[1];`,
	"vec3<i32>": `
        dataInt32[offset] = v[0];
        dataInt32[offset + 1] = v[1];
        dataInt32[offset + 2] = v[2];`,
	"vec4<i32>": `
        dataInt32[offset] = v[0];
        dataInt32[offset + 1] = v[1];
        dataInt32[offset + 2] = v[2];
        dataInt32[offset + 3] = v[3];`,
	"mat2x2<f32>": `
        data[offset] = v[0];
        data[offset + 1] = v[1];
        data[offset + 4] = v[2];
        data[offset + 5] = v[3];`,
	"mat3x3<f32>": `
        data[offset] = v[0];
        data[offset + 1] = v[1];
        data[offset + 2] = v[2];
        data[offset + 4] = v[3];
        data[offset + 5] = v[4];
        data[offset + 6] = v[5];
        data[offset + 8] = v[6];
        data[offset + 9] = v[7];
        data[offset + 10] = v[8];`,
	"mat4x4<f32>": `
        for (let i = 0; i < 16; i++) {
            data[offset + i] = v[i];
        }`,
	"mat3x2<f32>": loopMatrix(3, 2),
	"mat4x2<f32>": loopMatrix(4, 2),
	"mat2x3<f32>": loopMatrix(2, 3),
	"mat4x3<f32>": loopMatrix(4, 3),
	"mat2x4<f32>": loopMatrix(2, 4),
	"mat3x4<f32>": loopMatrix(3, 4)
};
const uboSyncFunctionsWGSL = {
	...uboSyncFunctionsSTD40,
	"mat2x2<f32>": `
        data[offset] = v[0];
        data[offset + 1] = v[1];
        data[offset + 2] = v[2];
        data[offset + 3] = v[3];
    `
};
//#endregion
//#region node_modules/pixi.js/lib/rendering/renderers/shared/buffer/BufferResource.mjs
var BufferResource = class extends eventemitter3_default {
	/**
	* Create a new Buffer Resource.
	* @param options - The options for the buffer resource
	* @param options.buffer - The underlying buffer that this resource is using
	* @param options.offset - The offset of the buffer this resource is using.
	* If not provided, then it will use the offset of the buffer.
	* @param options.size - The size of the buffer this resource is using.
	* If not provided, then it will use the size of the buffer.
	*/
	constructor({ buffer, offset, size }) {
		super();
		/**
		* emits when the underlying buffer has changed shape (i.e. resized)
		* letting the renderer know that it needs to discard the old buffer on the GPU and create a new one
		* @event change
		*/
		/** a unique id for this uniform group used through the renderer */
		this.uid = uid("buffer");
		/**
		* a resource type, used to identify how to handle it when its in a bind group / shader resource
		* @internal
		*/
		this._resourceType = "bufferResource";
		/**
		* used internally to know if a uniform group was used in the last render pass
		* @internal
		*/
		this._touched = 0;
		/**
		* the resource id used internally by the renderer to build bind group keys
		* @internal
		*/
		this._resourceId = uid("resource");
		/**
		* A cheeky hint to the GL renderer to let it know this is a BufferResource
		* @internal
		*/
		this._bufferResource = true;
		/**
		* Has the Buffer resource been destroyed?
		* @readonly
		*/
		this.destroyed = false;
		this.buffer = buffer;
		this.offset = offset | 0;
		this.size = size;
		this.buffer.on("change", this.onBufferChange, this);
	}
	onBufferChange() {
		this._resourceId = uid("resource");
		this.emit("change", this);
	}
	/**
	* Destroys this resource. Make sure the underlying buffer is not used anywhere else
	* if you want to destroy it as well, or code will explode
	* @param destroyBuffer - Should the underlying buffer be destroyed as well?
	*/
	destroy(destroyBuffer = false) {
		this.destroyed = true;
		if (destroyBuffer) this.buffer.destroy();
		this.emit("change", this);
		this.buffer = null;
		this.removeAllListeners();
	}
};
//#endregion
export { uniformParsers as a, ensureAttributes as c, localUniformBit as d, localUniformBitGl as f, createUboSyncFunction as i, textureBit as l, uboSyncFunctionsSTD40 as n, UboSystem as o, localUniformBitGroup2 as p, uboSyncFunctionsWGSL as r, GpuStencilModesToPixi as s, BufferResource as t, textureBitGl as u };

//# sourceMappingURL=BufferResource-HQp9psYo.mjs.map