Files
il/resources/public/js/cljs-runtime/module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$GpuTextureSystem.js
2026-07-01 22:38:29 -06:00

142 lines
9.2 KiB
JavaScript

shadow$provide.module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$GpuTextureSystem = function(global, require, module, exports) {
var adapter = require("module$node_modules$pixi_DOT_js$lib$environment$adapter");
global = require("module$node_modules$pixi_DOT_js$lib$extensions$Extensions");
var GCManagedHash = require("module$node_modules$pixi_DOT_js$lib$utils$data$GCManagedHash"), UniformGroup = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$shader$UniformGroup"), CanvasPool = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$CanvasPool"), BindGroup = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$shader$BindGroup"), gpuUploadBufferImageResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$uploaders$gpuUploadBufferImageResource"),
gpuUploadCompressedTextureResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$uploaders$gpuUploadCompressedTextureResource"), gpuUploadCubeTextureResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$uploaders$gpuUploadCubeTextureResource"), gpuUploadImageSource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$uploaders$gpuUploadImageSource"), gpuUploadVideoSource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$uploaders$gpuUploadVideoSource"),
GpuMipmapGenerator = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$utils$GpuMipmapGenerator");
"use strict";
class GPUTextureGpuData {
constructor(gpuTexture) {
this.textureView = null;
this.gpuTexture = gpuTexture;
}
destroy() {
this.gpuTexture.destroy();
this.gpuTexture = this.textureView = null;
}
}
const node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$GpuTextureSystem$classdecl$var23 = class {
constructor(renderer) {
this._gpuSamplers = Object.create(null);
this._bindGroupHash = Object.create(null);
this._renderer = renderer;
renderer.gc.addCollection(this, "_bindGroupHash", "hash");
this._managedTextures = new GCManagedHash.GCManagedHash({renderer, type:"resource", onUnload:this.onSourceUnload.bind(this), name:"gpuTextureSource"});
renderer = {image:gpuUploadImageSource.gpuUploadImageResource, buffer:gpuUploadBufferImageResource.gpuUploadBufferImageResource, video:gpuUploadVideoSource.gpuUploadVideoResource, compressed:gpuUploadCompressedTextureResource.gpuUploadCompressedTextureResource, ...node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$GpuTextureSystem$classdecl$var23.uploadExtensions};
this._uploads = {...renderer, cube:gpuUploadCubeTextureResource.createGpuUploadCubeTextureResource(renderer)};
}
get managedTextures() {
return Object.values(this._managedTextures.items);
}
contextChange(gpu) {
this._gpu = gpu;
}
initSource(source) {
return source._gpuData[this._renderer.uid]?.gpuTexture || this._initSource(source);
}
_initSource(source) {
source.autoGenerateMipmaps && (source.mipLevelCount = Math.floor(Math.log2(Math.max(source.pixelWidth, source.pixelHeight))) + 1);
if (1 < source.sampleCount) {
var usage = GPUTextureUsage.RENDER_ATTACHMENT;
source.transient && this._renderer.device.extensions.transientAttachment && (usage |= GPUTextureUsage.TRANSIENT_ATTACHMENT);
} else {
usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, "compressed" !== source.uploadMethodId && (usage |= GPUTextureUsage.RENDER_ATTACHMENT, usage |= GPUTextureUsage.COPY_SRC);
}
const blockData = gpuUploadCompressedTextureResource.blockDataMap[source.format] || {blockBytes:4, blockWidth:1, blockHeight:1};
usage = this._gpu.device.createTexture({label:source.label, size:{width:Math.ceil(source.pixelWidth / blockData.blockWidth) * blockData.blockWidth, height:Math.ceil(source.pixelHeight / blockData.blockHeight) * blockData.blockHeight, depthOrArrayLayers:source.arrayLayerCount}, format:source.format, sampleCount:source.sampleCount, mipLevelCount:source.mipLevelCount, dimension:source.dimension, usage});
source._gpuData[this._renderer.uid] = new GPUTextureGpuData(usage);
this._managedTextures.add(source) && (source.on("update", this.onSourceUpdate, this), source.on("resize", this.onSourceResize, this), source.on("updateMipmaps", this.onUpdateMipmaps, this));
this.onSourceUpdate(source);
return usage;
}
onSourceUpdate(source) {
const gpuTexture = this.getGpuSource(source);
if (gpuTexture && (this._uploads[source.uploadMethodId] && this._uploads[source.uploadMethodId].upload(source, gpuTexture, this._gpu), source.autoGenerateMipmaps && 1 < source.mipLevelCount)) {
this.onUpdateMipmaps(source);
}
}
onUpdateMipmaps(source) {
this._mipmapGenerator || (this._mipmapGenerator = new GpuMipmapGenerator.GpuMipmapGenerator(this._gpu.device));
source = this.getGpuSource(source);
this._mipmapGenerator.generateMipmap(source);
}
onSourceUnload(source) {
source.off("update", this.onSourceUpdate, this);
source.off("resize", this.onSourceResize, this);
source.off("updateMipmaps", this.onUpdateMipmaps, this);
}
onSourceResize(source) {
source._gcLastUsed = this._renderer.gc.now;
const gpuData = source._gpuData[this._renderer.uid], gpuTexture = gpuData?.gpuTexture;
if (!gpuTexture) {
this.initSource(source);
} else if (gpuTexture.width !== source.pixelWidth || gpuTexture.height !== source.pixelHeight) {
gpuData.destroy(), this._bindGroupHash[source.uid] = null, source._gpuData[this._renderer.uid] = null, this.initSource(source);
}
}
_initSampler(sampler) {
this._gpuSamplers[sampler._resourceId] = this._gpu.device.createSampler(sampler);
return this._gpuSamplers[sampler._resourceId];
}
getGpuSampler(sampler) {
return this._gpuSamplers[sampler._resourceId] || this._initSampler(sampler);
}
getGpuSource(source) {
source._gcLastUsed = this._renderer.gc.now;
return source._gpuData[this._renderer.uid]?.gpuTexture || this.initSource(source);
}
getTextureBindGroup(texture) {
return this._bindGroupHash[texture.uid] || this._createTextureBindGroup(texture);
}
_createTextureBindGroup(texture) {
const source = texture.source;
this._bindGroupHash[texture.uid] = new BindGroup.BindGroup({0:source, 1:source.style, 2:new UniformGroup.UniformGroup({uTextureMatrix:{type:"mat3x3\x3cf32\x3e", value:texture.textureMatrix.mapCoord}})});
return this._bindGroupHash[texture.uid];
}
getTextureView(texture) {
texture = texture.source;
texture._gcLastUsed = this._renderer.gc.now;
let gpuData = texture._gpuData[this._renderer.uid];
gpuData || (this.initSource(texture), gpuData = texture._gpuData[this._renderer.uid]);
gpuData.textureView || (gpuData.textureView = gpuData.gpuTexture.createView({dimension:texture.viewDimension}));
return gpuData.textureView;
}
generateCanvas(texture) {
const renderer = this._renderer, commandEncoder = renderer.gpu.device.createCommandEncoder(), canvas = adapter.DOMAdapter.get().createCanvas();
canvas.width = texture.source.pixelWidth;
canvas.height = texture.source.pixelHeight;
const context = canvas.getContext("webgpu");
context.configure({device:renderer.gpu.device, usage:GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC, format:adapter.DOMAdapter.get().getNavigator().gpu.getPreferredCanvasFormat(), alphaMode:"premultiplied"});
commandEncoder.copyTextureToTexture({texture:renderer.texture.getGpuSource(texture.source), origin:{x:0, y:0}}, {texture:context.getCurrentTexture()}, {width:canvas.width, height:canvas.height});
renderer.gpu.device.queue.submit([commandEncoder.finish()]);
return canvas;
}
getPixels(texture) {
var webGPUCanvas = this.generateCanvas(texture);
texture = CanvasPool.CanvasPool.getOptimalCanvasAndContext(webGPUCanvas.width, webGPUCanvas.height);
const context = texture.context;
context.drawImage(webGPUCanvas, 0, 0);
const {width, height} = webGPUCanvas;
webGPUCanvas = context.getImageData(0, 0, width, height);
webGPUCanvas = new Uint8ClampedArray(webGPUCanvas.data.buffer);
CanvasPool.CanvasPool.returnCanvasAndContext(texture);
return {pixels:webGPUCanvas, width, height};
}
destroy() {
this._managedTextures.destroy();
for (const k of Object.keys(this._bindGroupHash)) {
this._bindGroupHash[Number(k)]?.destroy();
}
this._bindGroupHash = this._gpuSamplers = this._mipmapGenerator = this._gpu = this._renderer = null;
}
};
require = node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$GpuTextureSystem$classdecl$var23;
require.extension = {type:[global.ExtensionType.WebGPUSystem], name:"texture"};
require.uploadExtensions = Object.create(null);
global.extensions.handleByMap(global.ExtensionType.TextureUploaderWebGPU, require.uploadExtensions);
exports.GPUTextureGpuData = GPUTextureGpuData;
exports.GpuTextureSystem = require;
};
//# sourceMappingURL=module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$texture$GpuTextureSystem.js.map