@lightningjs/renderer 3.0.0-beta10 → 3.0.0-beta11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -202
- package/NOTICE +3 -3
- package/README.md +133 -133
- package/dist/src/core/CoreTextNode.d.ts +3 -0
- package/dist/src/core/CoreTextNode.js +45 -10
- package/dist/src/core/CoreTextNode.js.map +1 -1
- package/dist/src/core/platform.d.ts +10 -0
- package/dist/src/core/platform.js +81 -0
- package/dist/src/core/platform.js.map +1 -0
- package/dist/src/core/renderers/CoreShader.d.ts +9 -0
- package/dist/src/core/renderers/CoreShader.js +28 -0
- package/dist/src/core/renderers/CoreShader.js.map +1 -0
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +33 -0
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +250 -0
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +16 -0
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +124 -0
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
- package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +13 -0
- package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +113 -192
- package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -1
- package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +0 -2
- package/dist/src/core/renderers/canvas/internal/ColorUtils.js +0 -14
- package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -1
- package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
- package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js +43 -0
- package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.d.ts +12 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +58 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.d.ts +9 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +38 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +56 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +239 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.d.ts +34 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js +114 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +133 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +616 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreShader.d.ts +83 -0
- package/dist/src/core/renderers/webgl/WebGlCoreShader.js +233 -0
- package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -0
- package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +35 -35
- package/dist/src/core/renderers/webgl/shaders/DefaultShader.d.ts +9 -0
- package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +87 -0
- package/dist/src/core/renderers/webgl/shaders/DefaultShader.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.d.ts +10 -0
- package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +119 -0
- package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/DynamicShader.d.ts +29 -0
- package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +413 -0
- package/dist/src/core/renderers/webgl/shaders/DynamicShader.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.d.ts +28 -0
- package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +131 -0
- package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/SdfShader.d.ts +47 -0
- package/dist/src/core/renderers/webgl/shaders/SdfShader.js +160 -0
- package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.d.ts +31 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +71 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.d.ts +30 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +58 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.d.ts +31 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +71 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.d.ts +31 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +71 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.d.ts +31 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +71 -0
- package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.d.ts +9 -0
- package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js +136 -0
- package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.d.ts +36 -0
- package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +85 -0
- package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.d.ts +45 -0
- package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +104 -0
- package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.d.ts +22 -0
- package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +45 -0
- package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.d.ts +58 -0
- package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +80 -0
- package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.d.ts +35 -0
- package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +129 -0
- package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.d.ts +39 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +116 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.d.ts +61 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +127 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.d.ts +40 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +71 -0
- package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js.map +1 -0
- package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.d.ts +115 -0
- package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js +61 -0
- package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js.map +1 -0
- package/dist/src/core/shaders/templates/shaderUtils.d.ts +5 -0
- package/dist/src/core/shaders/templates/shaderUtils.js +41 -0
- package/dist/src/core/shaders/templates/shaderUtils.js.map +1 -0
- package/dist/src/core/shaders/webgl/Border.js +82 -82
- package/dist/src/core/shaders/webgl/Default.js +47 -47
- package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
- package/dist/src/core/shaders/webgl/HolePunch.js +32 -32
- package/dist/src/core/shaders/webgl/LinearGradient.js +36 -36
- package/dist/src/core/shaders/webgl/RadialGradient.js +33 -33
- package/dist/src/core/shaders/webgl/Rounded.js +71 -71
- package/dist/src/core/shaders/webgl/RoundedWithBorder.js +111 -111
- package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +130 -130
- package/dist/src/core/shaders/webgl/RoundedWithShadow.js +54 -54
- package/dist/src/core/shaders/webgl/SdfShader.js +62 -62
- package/dist/src/core/shaders/webgl/Shadow.js +83 -83
- package/dist/src/core/shaders/webgl/Spinner.d.ts +1 -0
- package/dist/src/core/shaders/webgl/Spinner.js +2 -0
- package/dist/src/core/shaders/webgl/Spinner.js.map +1 -0
- package/dist/src/core/text-rendering/CanvasFontHandler.d.ts +16 -0
- package/dist/src/core/text-rendering/CanvasFontHandler.js +29 -0
- package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -1
- package/dist/src/core/text-rendering/SdfFontHandler.d.ts +15 -0
- package/dist/src/core/text-rendering/SdfFontHandler.js +34 -2
- package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -1
- package/dist/src/core/text-rendering/TextRenderer.d.ts +2 -0
- package/dist/src/core/text-rendering/TextTextureRendererUtils.js.map +1 -1
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +2 -2
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +0 -5
- package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.d.ts +1 -7
- package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +2 -50
- package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +3 -2
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +83 -42
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.d.ts +1 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +8 -66
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +4 -14
- package/dist/src/core/text-rendering/renderers/TextRenderer.js +0 -3
- package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/sdf/PeekableGenerator.d.ts +12 -0
- package/dist/src/core/text-rendering/sdf/PeekableGenerator.js +61 -0
- package/dist/src/core/text-rendering/sdf/PeekableGenerator.js.map +1 -0
- package/dist/src/core/text-rendering/sdf/SimpleFontShaper.d.ts +45 -0
- package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js +69 -0
- package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js.map +1 -0
- package/dist/src/main-api/DynamicShaderController.d.ts +29 -0
- package/dist/src/main-api/DynamicShaderController.js +58 -0
- package/dist/src/main-api/DynamicShaderController.js.map +1 -0
- package/dist/src/main-api/ShaderController.d.ts +31 -0
- package/dist/src/main-api/ShaderController.js +37 -0
- package/dist/src/main-api/ShaderController.js.map +1 -0
- package/dist/tsconfig.dist.tsbuildinfo +1 -1
- package/exports/canvas-shaders.ts +28 -28
- package/exports/canvas.ts +45 -45
- package/exports/index.ts +82 -82
- package/exports/inspector.ts +24 -24
- package/exports/utils.ts +50 -50
- package/exports/webgl-shaders.ts +28 -28
- package/exports/webgl.ts +52 -52
- package/package.json +2 -1
- package/src/common/CommonTypes.ts +146 -146
- package/src/common/EventEmitter.ts +77 -77
- package/src/common/IAnimationController.ts +92 -92
- package/src/common/IEventEmitter.ts +28 -28
- package/src/core/CoreNode.test.ts +202 -202
- package/src/core/CoreNode.ts +2491 -2491
- package/src/core/CoreShaderManager.ts +188 -188
- package/src/core/CoreTextNode.ts +483 -443
- package/src/core/CoreTextureManager.ts +565 -565
- package/src/core/Stage.ts +906 -906
- package/src/core/TextureMemoryManager.ts +445 -445
- package/src/core/animations/AnimationManager.ts +38 -38
- package/src/core/animations/CoreAnimation.ts +291 -291
- package/src/core/animations/CoreAnimationController.ts +166 -166
- package/src/core/lib/ContextSpy.ts +41 -41
- package/src/core/lib/ImageWorker.ts +286 -286
- package/src/core/lib/Matrix3d.ts +244 -244
- package/src/core/lib/RenderCoords.ts +71 -71
- package/src/core/lib/WebGlContextWrapper.ts +1381 -1381
- package/src/core/lib/colorCache.ts +20 -20
- package/src/core/lib/colorParser.ts +85 -85
- package/src/core/lib/textureCompression.ts +152 -152
- package/src/core/lib/textureSvg.ts +78 -78
- package/src/core/lib/utils.ts +412 -412
- package/src/core/lib/validateImageBitmap.ts +87 -87
- package/src/core/platforms/Platform.ts +77 -77
- package/src/core/platforms/web/WebPlatform.ts +121 -121
- package/src/core/renderers/CoreContextTexture.ts +43 -43
- package/src/core/renderers/CoreRenderOp.ts +22 -22
- package/src/core/renderers/CoreRenderer.ts +110 -110
- package/src/core/renderers/CoreShaderNode.ts +175 -175
- package/src/core/renderers/CoreShaderProgram.ts +23 -23
- package/src/core/renderers/canvas/CanvasRenderer.ts +283 -283
- package/src/core/renderers/canvas/CanvasShaderNode.ts +96 -96
- package/src/core/renderers/canvas/CanvasTexture.ts +156 -156
- package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +91 -91
- package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +50 -50
- package/src/core/renderers/webgl/WebGlCtxTexture.ts +310 -310
- package/src/core/renderers/webgl/WebGlRenderOp.ts +167 -167
- package/src/core/renderers/webgl/WebGlRenderer.ts +747 -747
- package/src/core/renderers/webgl/WebGlShaderNode.ts +435 -435
- package/src/core/renderers/webgl/WebGlShaderProgram.ts +341 -341
- package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
- package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
- package/src/core/renderers/webgl/internal/ShaderUtils.ts +281 -281
- package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
- package/src/core/shaders/canvas/Border.ts +75 -75
- package/src/core/shaders/canvas/HolePunch.ts +62 -62
- package/src/core/shaders/canvas/LinearGradient.ts +71 -71
- package/src/core/shaders/canvas/RadialGradient.ts +99 -99
- package/src/core/shaders/canvas/Rounded.ts +55 -55
- package/src/core/shaders/canvas/RoundedWithBorder.ts +74 -74
- package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +90 -90
- package/src/core/shaders/canvas/RoundedWithShadow.ts +70 -70
- package/src/core/shaders/canvas/Shadow.ts +52 -52
- package/src/core/shaders/canvas/utils/render.ts +151 -151
- package/src/core/shaders/templates/BorderTemplate.ts +115 -115
- package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
- package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
- package/src/core/shaders/templates/RadialGradientTemplate.ts +81 -81
- package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
- package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
- package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
- package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
- package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
- package/src/core/shaders/utils.ts +46 -46
- package/src/core/shaders/webgl/Border.ts +116 -116
- package/src/core/shaders/webgl/Default.ts +89 -89
- package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
- package/src/core/shaders/webgl/HolePunch.ts +75 -75
- package/src/core/shaders/webgl/LinearGradient.ts +82 -82
- package/src/core/shaders/webgl/RadialGradient.ts +85 -85
- package/src/core/shaders/webgl/Rounded.ts +117 -117
- package/src/core/shaders/webgl/RoundedWithBorder.ts +155 -155
- package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +175 -175
- package/src/core/shaders/webgl/RoundedWithShadow.ts +98 -98
- package/src/core/shaders/webgl/SdfShader.ts +134 -134
- package/src/core/shaders/webgl/Shadow.ts +115 -115
- package/src/core/text-rendering/CanvasFontHandler.ts +210 -176
- package/src/core/text-rendering/CanvasTextRenderer.ts +622 -622
- package/src/core/text-rendering/SdfFontHandler.ts +554 -517
- package/src/core/text-rendering/SdfTextRenderer.ts +466 -466
- package/src/core/text-rendering/TextRenderer.ts +406 -404
- package/src/core/text-rendering/Utils.ts +257 -257
- package/src/core/text-rendering/canvas/Settings.ts +99 -99
- package/src/core/text-rendering/canvas/Utils.test.ts +206 -206
- package/src/core/text-rendering/canvas/Utils.ts +178 -178
- package/src/core/text-rendering/canvas/calculateRenderInfo.ts +299 -299
- package/src/core/text-rendering/canvas/draw.ts +165 -165
- package/src/core/text-rendering/sdf/Utils.test.ts +402 -402
- package/src/core/text-rendering/sdf/Utils.ts +436 -436
- package/src/core/text-rendering/sdf/index.ts +20 -20
- package/src/core/textures/ColorTexture.ts +102 -102
- package/src/core/textures/ImageTexture.ts +418 -418
- package/src/core/textures/NoiseTexture.ts +104 -104
- package/src/core/textures/RenderTexture.ts +85 -85
- package/src/core/textures/SubTexture.ts +205 -205
- package/src/core/textures/Texture.ts +381 -381
- package/src/core/utils.ts +227 -227
- package/src/env.d.ts +7 -7
- package/src/main-api/INode.ts +100 -100
- package/src/main-api/Inspector.ts +567 -567
- package/src/main-api/Renderer.ts +873 -873
- package/src/main-api/utils.ts +45 -45
- package/src/utils.ts +267 -267
- package/COPYING +0 -1
|
@@ -1,310 +1,310 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
-
* following copyright and licenses apply:
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2023 Comcast Cable Communications Management, LLC.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import type { Dimensions } from '../../../common/CommonTypes.js';
|
|
21
|
-
import type { TextureMemoryManager } from '../../TextureMemoryManager.js';
|
|
22
|
-
import type { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
|
|
23
|
-
import type { Texture } from '../../textures/Texture.js';
|
|
24
|
-
import { CoreContextTexture } from '../CoreContextTexture.js';
|
|
25
|
-
import { isHTMLImageElement } from './internal/RendererUtils.js';
|
|
26
|
-
|
|
27
|
-
const TRANSPARENT_TEXTURE_DATA = new Uint8Array([0, 0, 0, 0]);
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* A wrapper around a WebGLTexture that handles loading the texture data
|
|
31
|
-
* from a Texture source and uploading it to the GPU as well as freeing
|
|
32
|
-
* the uploaded texture.
|
|
33
|
-
*
|
|
34
|
-
* @remarks
|
|
35
|
-
* When accessing the ctxTexture property, the texture will be loaded if
|
|
36
|
-
* it hasn't been already. ctxTexture will always return a valid WebGLTexture
|
|
37
|
-
* and trigger the loading/uploading of the texture's data if it hasn't been
|
|
38
|
-
* loaded yet.
|
|
39
|
-
*/
|
|
40
|
-
export class WebGlCtxTexture extends CoreContextTexture {
|
|
41
|
-
protected _nativeCtxTexture: WebGLTexture | null = null;
|
|
42
|
-
private _w = 0;
|
|
43
|
-
private _h = 0;
|
|
44
|
-
|
|
45
|
-
constructor(
|
|
46
|
-
protected glw: WebGlContextWrapper,
|
|
47
|
-
memManager: TextureMemoryManager,
|
|
48
|
-
textureSource: Texture,
|
|
49
|
-
) {
|
|
50
|
-
super(memManager, textureSource);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get ctxTexture(): WebGLTexture | null {
|
|
54
|
-
if (this.state === 'freed') {
|
|
55
|
-
this.load();
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
return this._nativeCtxTexture;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get w() {
|
|
62
|
-
return this._w;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
get h() {
|
|
66
|
-
return this._h;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Load the texture data from the Texture source and upload it to the GPU
|
|
71
|
-
*
|
|
72
|
-
* @remarks
|
|
73
|
-
* This method is called automatically when accessing the ctxTexture property
|
|
74
|
-
* if the texture hasn't been loaded yet. But it can also be called manually
|
|
75
|
-
* to force the texture to be pre-loaded prior to accessing the ctxTexture
|
|
76
|
-
* property.
|
|
77
|
-
*/
|
|
78
|
-
async load(): Promise<void> {
|
|
79
|
-
// If the texture is already loading or loaded, return resolved promise
|
|
80
|
-
if (this.state === 'loading' || this.state === 'loaded') {
|
|
81
|
-
return Promise.resolve();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
this.state = 'loading';
|
|
85
|
-
this.textureSource.setState('loading');
|
|
86
|
-
|
|
87
|
-
// Await the native texture creation to ensure GPU buffer is fully allocated
|
|
88
|
-
this._nativeCtxTexture = this.createNativeCtxTexture();
|
|
89
|
-
|
|
90
|
-
if (this._nativeCtxTexture === null) {
|
|
91
|
-
this.state = 'failed';
|
|
92
|
-
const error = new Error('Could not create WebGL Texture');
|
|
93
|
-
this.textureSource.setState('failed', error);
|
|
94
|
-
console.error('Could not create WebGL Texture');
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
const { width, height } = await this.onLoadRequest();
|
|
100
|
-
|
|
101
|
-
// If the texture has been freed while loading, return early.
|
|
102
|
-
// Type assertion needed because state could change during async operations
|
|
103
|
-
if ((this.state as string) === 'freed') {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
this.state = 'loaded';
|
|
108
|
-
this._w = width;
|
|
109
|
-
this._h = height;
|
|
110
|
-
// Update the texture source's width and height so that it can be used
|
|
111
|
-
// for rendering.
|
|
112
|
-
this.textureSource.setState('loaded', { width, height });
|
|
113
|
-
|
|
114
|
-
// cleanup source texture data next tick
|
|
115
|
-
// This is done using queueMicrotask to ensure it runs after the current
|
|
116
|
-
// event loop tick, allowing the texture to be fully loaded and bound
|
|
117
|
-
// to the GL context before freeing the source data.
|
|
118
|
-
// This is important to avoid issues with the texture data being
|
|
119
|
-
// freed while the texture is still being loaded or used.
|
|
120
|
-
queueMicrotask(() => {
|
|
121
|
-
this.textureSource.freeTextureData();
|
|
122
|
-
});
|
|
123
|
-
} catch (err: unknown) {
|
|
124
|
-
// If the texture has been freed while loading, return early.
|
|
125
|
-
// Type assertion needed because state could change during async operations
|
|
126
|
-
if ((this.state as string) === 'freed') {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
this.state = 'failed';
|
|
131
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
132
|
-
this.textureSource.setState('failed', error);
|
|
133
|
-
this.textureSource.freeTextureData();
|
|
134
|
-
console.error(err);
|
|
135
|
-
throw error; // Re-throw to propagate the error
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Called when the texture data needs to be loaded and uploaded to a texture
|
|
141
|
-
*/
|
|
142
|
-
async onLoadRequest(): Promise<Dimensions> {
|
|
143
|
-
const { glw } = this;
|
|
144
|
-
const textureData = this.textureSource.textureData;
|
|
145
|
-
if (textureData === null || this._nativeCtxTexture === null) {
|
|
146
|
-
throw new Error(
|
|
147
|
-
'Texture data or native texture is null ' + this.textureSource.type,
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Set to a 1x1 transparent texture
|
|
152
|
-
glw.texImage2D(0, glw.RGBA, 1, 1, 0, glw.RGBA, glw.UNSIGNED_BYTE, null);
|
|
153
|
-
this.setTextureMemUse(TRANSPARENT_TEXTURE_DATA.byteLength);
|
|
154
|
-
|
|
155
|
-
let width = 0;
|
|
156
|
-
let height = 0;
|
|
157
|
-
|
|
158
|
-
glw.activeTexture(0);
|
|
159
|
-
|
|
160
|
-
const tdata = textureData.data;
|
|
161
|
-
const format = glw.RGBA;
|
|
162
|
-
const formatBytes = 4;
|
|
163
|
-
const memoryPadding = 1.1; // Add padding to account for GPU Padding
|
|
164
|
-
|
|
165
|
-
// If textureData is null, the texture is empty (0, 0) and we don't need to
|
|
166
|
-
// upload any data to the GPU.
|
|
167
|
-
if (
|
|
168
|
-
(typeof ImageBitmap !== 'undefined' && tdata instanceof ImageBitmap) ||
|
|
169
|
-
tdata instanceof ImageData ||
|
|
170
|
-
// not using typeof HTMLImageElement due to web worker
|
|
171
|
-
isHTMLImageElement(tdata)
|
|
172
|
-
) {
|
|
173
|
-
width = tdata.width;
|
|
174
|
-
height = tdata.height;
|
|
175
|
-
glw.bindTexture(this._nativeCtxTexture);
|
|
176
|
-
glw.pixelStorei(
|
|
177
|
-
glw.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
|
|
178
|
-
!!textureData.premultiplyAlpha,
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
glw.texImage2D(0, format, format, glw.UNSIGNED_BYTE, tdata);
|
|
182
|
-
|
|
183
|
-
this.setTextureMemUse(height * width * formatBytes * memoryPadding);
|
|
184
|
-
} else if (tdata === null) {
|
|
185
|
-
width = 0;
|
|
186
|
-
height = 0;
|
|
187
|
-
// Reset to a 1x1 transparent texture
|
|
188
|
-
glw.bindTexture(this._nativeCtxTexture);
|
|
189
|
-
|
|
190
|
-
glw.texImage2D(
|
|
191
|
-
0,
|
|
192
|
-
format,
|
|
193
|
-
1,
|
|
194
|
-
1,
|
|
195
|
-
0,
|
|
196
|
-
format,
|
|
197
|
-
glw.UNSIGNED_BYTE,
|
|
198
|
-
TRANSPARENT_TEXTURE_DATA,
|
|
199
|
-
);
|
|
200
|
-
this.setTextureMemUse(TRANSPARENT_TEXTURE_DATA.byteLength);
|
|
201
|
-
} else if ('mipmaps' in tdata && tdata.mipmaps) {
|
|
202
|
-
const { mipmaps, width = 0, height = 0, type, glInternalFormat } = tdata;
|
|
203
|
-
const view =
|
|
204
|
-
type === 'ktx'
|
|
205
|
-
? new DataView(mipmaps[0] ?? new ArrayBuffer(0))
|
|
206
|
-
: (mipmaps[0] as unknown as ArrayBufferView);
|
|
207
|
-
|
|
208
|
-
glw.bindTexture(this._nativeCtxTexture);
|
|
209
|
-
|
|
210
|
-
glw.compressedTexImage2D(0, glInternalFormat, width, height, 0, view);
|
|
211
|
-
glw.texParameteri(glw.TEXTURE_WRAP_S, glw.CLAMP_TO_EDGE);
|
|
212
|
-
glw.texParameteri(glw.TEXTURE_WRAP_T, glw.CLAMP_TO_EDGE);
|
|
213
|
-
glw.texParameteri(glw.TEXTURE_MAG_FILTER, glw.LINEAR);
|
|
214
|
-
glw.texParameteri(glw.TEXTURE_MIN_FILTER, glw.LINEAR);
|
|
215
|
-
|
|
216
|
-
this.setTextureMemUse(view.byteLength);
|
|
217
|
-
} else if (tdata && tdata instanceof Uint8Array) {
|
|
218
|
-
// Color Texture
|
|
219
|
-
width = 1;
|
|
220
|
-
height = 1;
|
|
221
|
-
|
|
222
|
-
glw.bindTexture(this._nativeCtxTexture);
|
|
223
|
-
glw.pixelStorei(
|
|
224
|
-
glw.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
|
|
225
|
-
!!textureData.premultiplyAlpha,
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
glw.texImage2D(
|
|
229
|
-
0,
|
|
230
|
-
format,
|
|
231
|
-
width,
|
|
232
|
-
height,
|
|
233
|
-
0,
|
|
234
|
-
format,
|
|
235
|
-
glw.UNSIGNED_BYTE,
|
|
236
|
-
tdata,
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
this.setTextureMemUse(width * height * formatBytes);
|
|
240
|
-
} else {
|
|
241
|
-
console.error(
|
|
242
|
-
`WebGlCoreCtxTexture.onLoadRequest: Unexpected textureData returned`,
|
|
243
|
-
textureData,
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return {
|
|
248
|
-
width,
|
|
249
|
-
height,
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Free the WebGLTexture from the GPU
|
|
255
|
-
*
|
|
256
|
-
* @returns
|
|
257
|
-
*/
|
|
258
|
-
free() {
|
|
259
|
-
if (this.state === 'freed') {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
this.state = 'freed';
|
|
264
|
-
this.textureSource.setState('freed');
|
|
265
|
-
this._w = 0;
|
|
266
|
-
this._h = 0;
|
|
267
|
-
|
|
268
|
-
if (this._nativeCtxTexture !== null) {
|
|
269
|
-
this.glw.deleteTexture(this._nativeCtxTexture);
|
|
270
|
-
this.setTextureMemUse(0);
|
|
271
|
-
this._nativeCtxTexture = null;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// if the texture still has source data, free it
|
|
275
|
-
this.textureSource.freeTextureData();
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Create native context texture asynchronously
|
|
280
|
-
*
|
|
281
|
-
* @remarks
|
|
282
|
-
* When this method resolves, the returned texture will be bound to the GL context state
|
|
283
|
-
* and fully ready for use. This ensures proper GPU resource allocation timing.
|
|
284
|
-
*
|
|
285
|
-
* @returns Promise that resolves to the native WebGL texture or null on failure
|
|
286
|
-
*/
|
|
287
|
-
protected createNativeCtxTexture(): WebGLTexture | null {
|
|
288
|
-
const { glw } = this;
|
|
289
|
-
|
|
290
|
-
const nativeTexture = glw.createTexture();
|
|
291
|
-
if (!nativeTexture) {
|
|
292
|
-
return null;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// On initial load request, create a 1x1 transparent texture to use until
|
|
296
|
-
// the texture data is finally loaded.
|
|
297
|
-
glw.activeTexture(0);
|
|
298
|
-
glw.bindTexture(nativeTexture);
|
|
299
|
-
|
|
300
|
-
// linear texture filtering
|
|
301
|
-
glw.texParameteri(glw.TEXTURE_MAG_FILTER, glw.LINEAR);
|
|
302
|
-
glw.texParameteri(glw.TEXTURE_MIN_FILTER, glw.LINEAR);
|
|
303
|
-
|
|
304
|
-
// texture wrapping method
|
|
305
|
-
glw.texParameteri(glw.TEXTURE_WRAP_S, glw.CLAMP_TO_EDGE);
|
|
306
|
-
glw.texParameteri(glw.TEXTURE_WRAP_T, glw.CLAMP_TO_EDGE);
|
|
307
|
-
|
|
308
|
-
return nativeTexture;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
+
* following copyright and licenses apply:
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { Dimensions } from '../../../common/CommonTypes.js';
|
|
21
|
+
import type { TextureMemoryManager } from '../../TextureMemoryManager.js';
|
|
22
|
+
import type { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
|
|
23
|
+
import type { Texture } from '../../textures/Texture.js';
|
|
24
|
+
import { CoreContextTexture } from '../CoreContextTexture.js';
|
|
25
|
+
import { isHTMLImageElement } from './internal/RendererUtils.js';
|
|
26
|
+
|
|
27
|
+
const TRANSPARENT_TEXTURE_DATA = new Uint8Array([0, 0, 0, 0]);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A wrapper around a WebGLTexture that handles loading the texture data
|
|
31
|
+
* from a Texture source and uploading it to the GPU as well as freeing
|
|
32
|
+
* the uploaded texture.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* When accessing the ctxTexture property, the texture will be loaded if
|
|
36
|
+
* it hasn't been already. ctxTexture will always return a valid WebGLTexture
|
|
37
|
+
* and trigger the loading/uploading of the texture's data if it hasn't been
|
|
38
|
+
* loaded yet.
|
|
39
|
+
*/
|
|
40
|
+
export class WebGlCtxTexture extends CoreContextTexture {
|
|
41
|
+
protected _nativeCtxTexture: WebGLTexture | null = null;
|
|
42
|
+
private _w = 0;
|
|
43
|
+
private _h = 0;
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
protected glw: WebGlContextWrapper,
|
|
47
|
+
memManager: TextureMemoryManager,
|
|
48
|
+
textureSource: Texture,
|
|
49
|
+
) {
|
|
50
|
+
super(memManager, textureSource);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get ctxTexture(): WebGLTexture | null {
|
|
54
|
+
if (this.state === 'freed') {
|
|
55
|
+
this.load();
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return this._nativeCtxTexture;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get w() {
|
|
62
|
+
return this._w;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get h() {
|
|
66
|
+
return this._h;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Load the texture data from the Texture source and upload it to the GPU
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* This method is called automatically when accessing the ctxTexture property
|
|
74
|
+
* if the texture hasn't been loaded yet. But it can also be called manually
|
|
75
|
+
* to force the texture to be pre-loaded prior to accessing the ctxTexture
|
|
76
|
+
* property.
|
|
77
|
+
*/
|
|
78
|
+
async load(): Promise<void> {
|
|
79
|
+
// If the texture is already loading or loaded, return resolved promise
|
|
80
|
+
if (this.state === 'loading' || this.state === 'loaded') {
|
|
81
|
+
return Promise.resolve();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.state = 'loading';
|
|
85
|
+
this.textureSource.setState('loading');
|
|
86
|
+
|
|
87
|
+
// Await the native texture creation to ensure GPU buffer is fully allocated
|
|
88
|
+
this._nativeCtxTexture = this.createNativeCtxTexture();
|
|
89
|
+
|
|
90
|
+
if (this._nativeCtxTexture === null) {
|
|
91
|
+
this.state = 'failed';
|
|
92
|
+
const error = new Error('Could not create WebGL Texture');
|
|
93
|
+
this.textureSource.setState('failed', error);
|
|
94
|
+
console.error('Could not create WebGL Texture');
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const { width, height } = await this.onLoadRequest();
|
|
100
|
+
|
|
101
|
+
// If the texture has been freed while loading, return early.
|
|
102
|
+
// Type assertion needed because state could change during async operations
|
|
103
|
+
if ((this.state as string) === 'freed') {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.state = 'loaded';
|
|
108
|
+
this._w = width;
|
|
109
|
+
this._h = height;
|
|
110
|
+
// Update the texture source's width and height so that it can be used
|
|
111
|
+
// for rendering.
|
|
112
|
+
this.textureSource.setState('loaded', { width, height });
|
|
113
|
+
|
|
114
|
+
// cleanup source texture data next tick
|
|
115
|
+
// This is done using queueMicrotask to ensure it runs after the current
|
|
116
|
+
// event loop tick, allowing the texture to be fully loaded and bound
|
|
117
|
+
// to the GL context before freeing the source data.
|
|
118
|
+
// This is important to avoid issues with the texture data being
|
|
119
|
+
// freed while the texture is still being loaded or used.
|
|
120
|
+
queueMicrotask(() => {
|
|
121
|
+
this.textureSource.freeTextureData();
|
|
122
|
+
});
|
|
123
|
+
} catch (err: unknown) {
|
|
124
|
+
// If the texture has been freed while loading, return early.
|
|
125
|
+
// Type assertion needed because state could change during async operations
|
|
126
|
+
if ((this.state as string) === 'freed') {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.state = 'failed';
|
|
131
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
132
|
+
this.textureSource.setState('failed', error);
|
|
133
|
+
this.textureSource.freeTextureData();
|
|
134
|
+
console.error(err);
|
|
135
|
+
throw error; // Re-throw to propagate the error
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Called when the texture data needs to be loaded and uploaded to a texture
|
|
141
|
+
*/
|
|
142
|
+
async onLoadRequest(): Promise<Dimensions> {
|
|
143
|
+
const { glw } = this;
|
|
144
|
+
const textureData = this.textureSource.textureData;
|
|
145
|
+
if (textureData === null || this._nativeCtxTexture === null) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
'Texture data or native texture is null ' + this.textureSource.type,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Set to a 1x1 transparent texture
|
|
152
|
+
glw.texImage2D(0, glw.RGBA, 1, 1, 0, glw.RGBA, glw.UNSIGNED_BYTE, null);
|
|
153
|
+
this.setTextureMemUse(TRANSPARENT_TEXTURE_DATA.byteLength);
|
|
154
|
+
|
|
155
|
+
let width = 0;
|
|
156
|
+
let height = 0;
|
|
157
|
+
|
|
158
|
+
glw.activeTexture(0);
|
|
159
|
+
|
|
160
|
+
const tdata = textureData.data;
|
|
161
|
+
const format = glw.RGBA;
|
|
162
|
+
const formatBytes = 4;
|
|
163
|
+
const memoryPadding = 1.1; // Add padding to account for GPU Padding
|
|
164
|
+
|
|
165
|
+
// If textureData is null, the texture is empty (0, 0) and we don't need to
|
|
166
|
+
// upload any data to the GPU.
|
|
167
|
+
if (
|
|
168
|
+
(typeof ImageBitmap !== 'undefined' && tdata instanceof ImageBitmap) ||
|
|
169
|
+
tdata instanceof ImageData ||
|
|
170
|
+
// not using typeof HTMLImageElement due to web worker
|
|
171
|
+
isHTMLImageElement(tdata)
|
|
172
|
+
) {
|
|
173
|
+
width = tdata.width;
|
|
174
|
+
height = tdata.height;
|
|
175
|
+
glw.bindTexture(this._nativeCtxTexture);
|
|
176
|
+
glw.pixelStorei(
|
|
177
|
+
glw.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
|
|
178
|
+
!!textureData.premultiplyAlpha,
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
glw.texImage2D(0, format, format, glw.UNSIGNED_BYTE, tdata);
|
|
182
|
+
|
|
183
|
+
this.setTextureMemUse(height * width * formatBytes * memoryPadding);
|
|
184
|
+
} else if (tdata === null) {
|
|
185
|
+
width = 0;
|
|
186
|
+
height = 0;
|
|
187
|
+
// Reset to a 1x1 transparent texture
|
|
188
|
+
glw.bindTexture(this._nativeCtxTexture);
|
|
189
|
+
|
|
190
|
+
glw.texImage2D(
|
|
191
|
+
0,
|
|
192
|
+
format,
|
|
193
|
+
1,
|
|
194
|
+
1,
|
|
195
|
+
0,
|
|
196
|
+
format,
|
|
197
|
+
glw.UNSIGNED_BYTE,
|
|
198
|
+
TRANSPARENT_TEXTURE_DATA,
|
|
199
|
+
);
|
|
200
|
+
this.setTextureMemUse(TRANSPARENT_TEXTURE_DATA.byteLength);
|
|
201
|
+
} else if ('mipmaps' in tdata && tdata.mipmaps) {
|
|
202
|
+
const { mipmaps, width = 0, height = 0, type, glInternalFormat } = tdata;
|
|
203
|
+
const view =
|
|
204
|
+
type === 'ktx'
|
|
205
|
+
? new DataView(mipmaps[0] ?? new ArrayBuffer(0))
|
|
206
|
+
: (mipmaps[0] as unknown as ArrayBufferView);
|
|
207
|
+
|
|
208
|
+
glw.bindTexture(this._nativeCtxTexture);
|
|
209
|
+
|
|
210
|
+
glw.compressedTexImage2D(0, glInternalFormat, width, height, 0, view);
|
|
211
|
+
glw.texParameteri(glw.TEXTURE_WRAP_S, glw.CLAMP_TO_EDGE);
|
|
212
|
+
glw.texParameteri(glw.TEXTURE_WRAP_T, glw.CLAMP_TO_EDGE);
|
|
213
|
+
glw.texParameteri(glw.TEXTURE_MAG_FILTER, glw.LINEAR);
|
|
214
|
+
glw.texParameteri(glw.TEXTURE_MIN_FILTER, glw.LINEAR);
|
|
215
|
+
|
|
216
|
+
this.setTextureMemUse(view.byteLength);
|
|
217
|
+
} else if (tdata && tdata instanceof Uint8Array) {
|
|
218
|
+
// Color Texture
|
|
219
|
+
width = 1;
|
|
220
|
+
height = 1;
|
|
221
|
+
|
|
222
|
+
glw.bindTexture(this._nativeCtxTexture);
|
|
223
|
+
glw.pixelStorei(
|
|
224
|
+
glw.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
|
|
225
|
+
!!textureData.premultiplyAlpha,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
glw.texImage2D(
|
|
229
|
+
0,
|
|
230
|
+
format,
|
|
231
|
+
width,
|
|
232
|
+
height,
|
|
233
|
+
0,
|
|
234
|
+
format,
|
|
235
|
+
glw.UNSIGNED_BYTE,
|
|
236
|
+
tdata,
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
this.setTextureMemUse(width * height * formatBytes);
|
|
240
|
+
} else {
|
|
241
|
+
console.error(
|
|
242
|
+
`WebGlCoreCtxTexture.onLoadRequest: Unexpected textureData returned`,
|
|
243
|
+
textureData,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
width,
|
|
249
|
+
height,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Free the WebGLTexture from the GPU
|
|
255
|
+
*
|
|
256
|
+
* @returns
|
|
257
|
+
*/
|
|
258
|
+
free() {
|
|
259
|
+
if (this.state === 'freed') {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
this.state = 'freed';
|
|
264
|
+
this.textureSource.setState('freed');
|
|
265
|
+
this._w = 0;
|
|
266
|
+
this._h = 0;
|
|
267
|
+
|
|
268
|
+
if (this._nativeCtxTexture !== null) {
|
|
269
|
+
this.glw.deleteTexture(this._nativeCtxTexture);
|
|
270
|
+
this.setTextureMemUse(0);
|
|
271
|
+
this._nativeCtxTexture = null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// if the texture still has source data, free it
|
|
275
|
+
this.textureSource.freeTextureData();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Create native context texture asynchronously
|
|
280
|
+
*
|
|
281
|
+
* @remarks
|
|
282
|
+
* When this method resolves, the returned texture will be bound to the GL context state
|
|
283
|
+
* and fully ready for use. This ensures proper GPU resource allocation timing.
|
|
284
|
+
*
|
|
285
|
+
* @returns Promise that resolves to the native WebGL texture or null on failure
|
|
286
|
+
*/
|
|
287
|
+
protected createNativeCtxTexture(): WebGLTexture | null {
|
|
288
|
+
const { glw } = this;
|
|
289
|
+
|
|
290
|
+
const nativeTexture = glw.createTexture();
|
|
291
|
+
if (!nativeTexture) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// On initial load request, create a 1x1 transparent texture to use until
|
|
296
|
+
// the texture data is finally loaded.
|
|
297
|
+
glw.activeTexture(0);
|
|
298
|
+
glw.bindTexture(nativeTexture);
|
|
299
|
+
|
|
300
|
+
// linear texture filtering
|
|
301
|
+
glw.texParameteri(glw.TEXTURE_MAG_FILTER, glw.LINEAR);
|
|
302
|
+
glw.texParameteri(glw.TEXTURE_MIN_FILTER, glw.LINEAR);
|
|
303
|
+
|
|
304
|
+
// texture wrapping method
|
|
305
|
+
glw.texParameteri(glw.TEXTURE_WRAP_S, glw.CLAMP_TO_EDGE);
|
|
306
|
+
glw.texParameteri(glw.TEXTURE_WRAP_T, glw.CLAMP_TO_EDGE);
|
|
307
|
+
|
|
308
|
+
return nativeTexture;
|
|
309
|
+
}
|
|
310
|
+
}
|