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

222 lines
15 KiB
JavaScript

shadow$provide.module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$GlTextureSystem = 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"), Texture = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$Texture"), GlTexture = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$GlTexture"), glUploadBufferImageResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$uploaders$glUploadBufferImageResource"), glUploadCompressedTextureResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$uploaders$glUploadCompressedTextureResource"),
glUploadCubeTextureResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$uploaders$glUploadCubeTextureResource"), glUploadImageResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$uploaders$glUploadImageResource"), glUploadVideoResource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$uploaders$glUploadVideoResource"), applyStyleParams = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$utils$applyStyleParams"),
mapFormatToGlFormat = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$utils$mapFormatToGlFormat"), mapFormatToGlInternalFormat = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$utils$mapFormatToGlInternalFormat"), mapFormatToGlType = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$utils$mapFormatToGlType"), mapViewDimensionToGlTarget = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$utils$mapViewDimensionToGlTarget");
require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$utils$unpremultiplyAlpha");
"use strict";
const node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$GlTextureSystem$classdecl$var27 = class {
constructor(renderer) {
this._glSamplers = Object.create(null);
this._boundTextures = [];
this._activeTextureLocation = -1;
this._boundSamplers = Object.create(null);
this._useSeparateSamplers = this._premultiplyAlpha = !1;
this._renderer = renderer;
this._managedTextures = new GCManagedHash.GCManagedHash({renderer, type:"resource", onUnload:this.onSourceUnload.bind(this), name:"glTexture"});
renderer = {image:glUploadImageResource.glUploadImageResource, buffer:glUploadBufferImageResource.glUploadBufferImageResource, video:glUploadVideoResource.glUploadVideoResource, compressed:glUploadCompressedTextureResource.glUploadCompressedTextureResource, ...node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$GlTextureSystem$classdecl$var27.uploadExtensions};
this._uploads = {...renderer, cube:glUploadCubeTextureResource.createGlUploadCubeTextureResource(renderer)};
}
get managedTextures() {
return Object.values(this._managedTextures.items);
}
contextChange(gl) {
this._gl = gl;
this._mapFormatToInternalFormat || (this._mapFormatToInternalFormat = mapFormatToGlInternalFormat.mapFormatToGlInternalFormat(gl, this._renderer.context.extensions), this._mapFormatToType = mapFormatToGlType.mapFormatToGlType(gl), this._mapFormatToFormat = mapFormatToGlFormat.mapFormatToGlFormat(gl), this._mapViewDimensionToGlTarget = mapViewDimensionToGlTarget.mapViewDimensionToGlTarget(gl));
this._managedTextures.removeAll(!0);
this._glSamplers = Object.create(null);
this._boundSamplers = Object.create(null);
this._premultiplyAlpha = !1;
for (gl = 0; 16 > gl; gl++) {
this.bind(Texture.Texture.EMPTY, gl);
}
}
initSource(source) {
this.bind(source);
}
bind(texture, location = 0) {
const source = texture.source;
texture ? (this.bindSource(source, location), this._useSeparateSamplers && this._bindSampler(source.style, location)) : (this.bindSource(null, location), this._useSeparateSamplers && this._bindSampler(null, location));
}
bindSource(source, location = 0) {
const gl = this._gl;
source._gcLastUsed = this._renderer.gc.now;
this._boundTextures[location] !== source && (this._boundTextures[location] = source, this._activateLocation(location), source || (source = Texture.Texture.EMPTY.source), source = this.getGlSource(source), gl.bindTexture(source.target, source.texture));
}
_bindSampler(style, location = 0) {
const gl = this._gl;
style ? (style = this._getGlSampler(style), this._boundSamplers[location] !== style && (this._boundSamplers[location] = style, gl.bindSampler(location, style))) : (this._boundSamplers[location] = null, gl.bindSampler(location, null));
}
unbind(texture) {
texture = texture.source;
const boundTextures = this._boundTextures, gl = this._gl;
for (let i = 0; i < boundTextures.length; i++) {
if (boundTextures[i] === texture) {
this._activateLocation(i);
const glTexture = this.getGlSource(texture);
gl.bindTexture(glTexture.target, null);
boundTextures[i] = null;
}
}
}
_activateLocation(location) {
this._activeTextureLocation !== location && (this._activeTextureLocation = location, this._gl.activeTexture(this._gl.TEXTURE0 + location));
}
_initSource(source) {
const gl = this._gl, glTexture = new GlTexture.GlTexture(gl.createTexture());
glTexture.type = this._mapFormatToType[source.format];
glTexture.internalFormat = this._mapFormatToInternalFormat[source.format];
glTexture.format = this._mapFormatToFormat[source.format];
glTexture.target = this._mapViewDimensionToGlTarget[source.viewDimension];
if (null === glTexture.target) {
throw Error(`Unsupported view dimension: ${source.viewDimension} with this webgl version: ${this._renderer.context.webGLVersion}`);
}
"cube" === source.uploadMethodId && (glTexture.target = gl.TEXTURE_CUBE_MAP);
source.autoGenerateMipmaps && (this._renderer.context.supports.nonPowOf2mipmaps || source.isPowerOfTwo) && (source.mipLevelCount = Math.floor(Math.log2(Math.max(source.width, source.height))) + 1);
source._gpuData[this._renderer.uid] = glTexture;
this._managedTextures.add(source) && (source.on("update", this.onSourceUpdate, this), source.on("resize", this.onSourceUpdate, this), source.on("styleChange", this.onStyleChange, this), source.on("updateMipmaps", this.onUpdateMipmaps, this));
this.onSourceUpdate(source);
this.updateStyle(source, !1);
return glTexture;
}
onStyleChange(source) {
this.updateStyle(source, !1);
}
updateStyle(source, firstCreation) {
const gl = this._gl, glTexture = this.getGlSource(source);
gl.bindTexture(glTexture.target, glTexture.texture);
this._boundTextures[this._activeTextureLocation] = source;
applyStyleParams.applyStyleParams(source.style, gl, 1 < source.mipLevelCount, this._renderer.context.extensions.anisotropicFiltering, "texParameteri", glTexture.target, !this._renderer.context.supports.nonPowOf2wrapping && !source.isPowerOfTwo, firstCreation);
}
onSourceUnload(source, contextLost = !1) {
const glTexture = source._gpuData[this._renderer.uid];
glTexture && (contextLost || (this.unbind(source), this._gl.deleteTexture(glTexture.texture)), source.off("update", this.onSourceUpdate, this), source.off("resize", this.onSourceUpdate, this), source.off("styleChange", this.onStyleChange, this), source.off("updateMipmaps", this.onUpdateMipmaps, this));
}
onSourceUpdate(source) {
const gl = this._gl, glTexture = this.getGlSource(source);
gl.bindTexture(glTexture.target, glTexture.texture);
this._boundTextures[this._activeTextureLocation] = source;
const premultipliedAlpha = "premultiply-alpha-on-upload" === source.alphaMode;
this._premultiplyAlpha !== premultipliedAlpha && (this._premultiplyAlpha = premultipliedAlpha, gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultipliedAlpha));
if (this._uploads[source.uploadMethodId]) {
this._uploads[source.uploadMethodId].upload(source, glTexture, gl, this._renderer.context.webGLVersion);
} else if (glTexture.target === gl.TEXTURE_2D) {
this._initEmptyTexture2D(glTexture, source);
} else if (glTexture.target === gl.TEXTURE_2D_ARRAY) {
this._initEmptyTexture2DArray(glTexture, source);
} else if (glTexture.target === gl.TEXTURE_CUBE_MAP) {
this._initEmptyTextureCube(glTexture, source);
} else {
throw Error("[GlTextureSystem] Unsupported texture target for empty allocation.");
}
this._applyMipRange(glTexture, source);
if (source.autoGenerateMipmaps && 1 < source.mipLevelCount) {
this.onUpdateMipmaps(source, !1);
}
}
onUpdateMipmaps(source, bind = !0) {
bind && this.bindSource(source, 0);
source = this.getGlSource(source);
this._gl.generateMipmap(source.target);
}
_initEmptyTexture2D(glTexture, source) {
const gl = this._gl;
gl.texImage2D(gl.TEXTURE_2D, 0, glTexture.internalFormat, source.pixelWidth, source.pixelHeight, 0, glTexture.format, glTexture.type, null);
let w = Math.max(source.pixelWidth >> 1, 1), h = Math.max(source.pixelHeight >> 1, 1);
for (let level = 1; level < source.mipLevelCount; level++) {
gl.texImage2D(gl.TEXTURE_2D, level, glTexture.internalFormat, w, h, 0, glTexture.format, glTexture.type, null), w = Math.max(w >> 1, 1), h = Math.max(h >> 1, 1);
}
}
_initEmptyTexture2DArray(glTexture, source) {
if (2 !== this._renderer.context.webGLVersion) {
throw Error("[GlTextureSystem] TEXTURE_2D_ARRAY requires WebGL2.");
}
const gl2 = this._gl, depth = Math.max(source.arrayLayerCount | 0, 1);
gl2.texImage3D(gl2.TEXTURE_2D_ARRAY, 0, glTexture.internalFormat, source.pixelWidth, source.pixelHeight, depth, 0, glTexture.format, glTexture.type, null);
let w = Math.max(source.pixelWidth >> 1, 1), h = Math.max(source.pixelHeight >> 1, 1);
for (let level = 1; level < source.mipLevelCount; level++) {
gl2.texImage3D(gl2.TEXTURE_2D_ARRAY, level, glTexture.internalFormat, w, h, depth, 0, glTexture.format, glTexture.type, null), w = Math.max(w >> 1, 1), h = Math.max(h >> 1, 1);
}
}
_initEmptyTextureCube(glTexture, source) {
const gl = this._gl;
for (var face$jscomp$0 = 0; 6 > face$jscomp$0; face$jscomp$0++) {
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face$jscomp$0, 0, glTexture.internalFormat, source.pixelWidth, source.pixelHeight, 0, glTexture.format, glTexture.type, null);
}
face$jscomp$0 = Math.max(source.pixelWidth >> 1, 1);
let h = Math.max(source.pixelHeight >> 1, 1);
for (let level = 1; level < source.mipLevelCount; level++) {
for (let face = 0; 6 > face; face++) {
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, level, glTexture.internalFormat, face$jscomp$0, h, 0, glTexture.format, glTexture.type, null);
}
face$jscomp$0 = Math.max(face$jscomp$0 >> 1, 1);
h = Math.max(h >> 1, 1);
}
}
_applyMipRange(glTexture, source) {
if (2 === this._renderer.context.webGLVersion && !(1 >= source.mipLevelCount)) {
var gl = this._gl;
source = Math.max((source.mipLevelCount | 0) - 1, 0);
gl.texParameteri(glTexture.target, gl.TEXTURE_BASE_LEVEL, 0);
gl.texParameteri(glTexture.target, gl.TEXTURE_MAX_LEVEL, source);
}
}
_initSampler(style) {
const gl = this._gl, glSampler = this._gl.createSampler();
this._glSamplers[style._resourceId] = glSampler;
applyStyleParams.applyStyleParams(style, gl, 1 < this._boundTextures[this._activeTextureLocation].mipLevelCount, this._renderer.context.extensions.anisotropicFiltering, "samplerParameteri", glSampler, !1, !0);
return this._glSamplers[style._resourceId];
}
_getGlSampler(sampler) {
return this._glSamplers[sampler._resourceId] || this._initSampler(sampler);
}
getGlSource(source) {
source._gcLastUsed = this._renderer.gc.now;
return source._gpuData[this._renderer.uid] || this._initSource(source);
}
generateCanvas(texture) {
const {pixels, width, height} = this.getPixels(texture);
texture = adapter.DOMAdapter.get().createCanvas();
texture.width = width;
texture.height = height;
const ctx = texture.getContext("2d");
if (ctx) {
const imageData = ctx.createImageData(width, height);
imageData.data.set(pixels);
ctx.putImageData(imageData, 0, 0);
}
return texture;
}
getPixels(texture) {
const resolution = texture.source.resolution, frame = texture.frame, width = Math.max(Math.round(frame.width * resolution), 1), height = Math.max(Math.round(frame.height * resolution), 1), pixels = new Uint8Array(4 * width * height);
var renderer = this._renderer;
texture = renderer.renderTarget.getRenderTarget(texture);
texture = renderer.renderTarget.getGpuRenderTarget(texture);
renderer = renderer.gl;
renderer.bindFramebuffer(renderer.FRAMEBUFFER, texture.resolveTargetFramebuffer);
renderer.readPixels(Math.round(frame.x * resolution), Math.round(frame.y * resolution), width, height, renderer.RGBA, renderer.UNSIGNED_BYTE, pixels);
return {pixels:new Uint8ClampedArray(pixels.buffer), width, height};
}
destroy() {
this._managedTextures.destroy();
this._renderer = this._uploads = this._mapFormatToFormat = this._mapFormatToType = this._mapFormatToInternalFormat = this._boundSamplers = this._boundTextures = this._glSamplers = null;
}
resetState() {
this._activeTextureLocation = -1;
this._boundTextures.fill(Texture.Texture.EMPTY.source);
this._boundSamplers = Object.create(null);
const gl = this._gl;
this._premultiplyAlpha = !1;
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, this._premultiplyAlpha);
}
};
require = node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$GlTextureSystem$classdecl$var27;
require.extension = {type:[global.ExtensionType.WebGLSystem], name:"texture"};
require.uploadExtensions = Object.create(null);
global.extensions.handleByMap(global.ExtensionType.TextureUploaderWebGL, require.uploadExtensions);
exports.GlTextureSystem = require;
};
//# sourceMappingURL=module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$texture$GlTextureSystem.js.map