@lightningjs/renderer 0.7.1 → 0.7.3
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/COPYING +1 -0
- package/LICENSE +202 -202
- package/NOTICE +3 -3
- package/README.md +221 -221
- package/dist/exports/main-api.d.ts +1 -0
- package/dist/src/common/CommonTypes.d.ts +7 -0
- package/dist/src/core/CoreShaderManager.d.ts +13 -6
- package/dist/src/core/CoreShaderManager.js +6 -6
- package/dist/src/core/CoreShaderManager.js.map +1 -1
- package/dist/src/core/Stage.d.ts +2 -1
- package/dist/src/core/Stage.js +4 -0
- package/dist/src/core/Stage.js.map +1 -1
- package/dist/src/core/animations/CoreAnimation.js +3 -0
- package/dist/src/core/animations/CoreAnimation.js.map +1 -1
- package/dist/src/core/animations/CoreAnimationController.d.ts +8 -0
- package/dist/src/core/animations/CoreAnimationController.js +26 -0
- package/dist/src/core/animations/CoreAnimationController.js.map +1 -1
- package/dist/src/core/lib/ImageWorker.js +21 -21
- package/dist/src/core/lib/WebGlContextWrapper.d.ts +9 -0
- package/dist/src/core/lib/WebGlContextWrapper.js +12 -0
- package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
- package/dist/src/core/lib/textureCompression.d.ts +16 -0
- package/dist/src/core/lib/textureCompression.js +129 -0
- package/dist/src/core/lib/textureCompression.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +12 -0
- package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
- package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +47 -47
- package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
- package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
- package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
- package/dist/src/core/renderers/webgl/shaders/SdfShader.js +49 -49
- package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +5 -5
- package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
- package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
- package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
- package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
- package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +31 -31
- package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +13 -13
- package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
- package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.d.ts +4 -0
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +11 -0
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
- package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +10 -1
- package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +21 -7
- package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +2 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
- package/dist/src/core/textures/ImageTexture.js +5 -0
- package/dist/src/core/textures/ImageTexture.js.map +1 -1
- package/dist/src/core/textures/Texture.d.ts +28 -1
- package/dist/src/core/textures/Texture.js.map +1 -1
- package/dist/src/main-api/ICoreDriver.d.ts +2 -1
- package/dist/src/main-api/Inspector.d.ts +21 -0
- package/dist/src/main-api/Inspector.js +302 -0
- package/dist/src/main-api/Inspector.js.map +1 -0
- package/dist/src/main-api/RendererMain.d.ts +12 -0
- package/dist/src/main-api/RendererMain.js +19 -1
- package/dist/src/main-api/RendererMain.js.map +1 -1
- package/dist/src/render-drivers/main/MainCoreDriver.d.ts +2 -1
- package/dist/src/render-drivers/main/MainCoreDriver.js +7 -1
- package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
- package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +2 -1
- package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +7 -0
- package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -1
- package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +9 -1
- package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -1
- package/dist/tsconfig.dist.tsbuildinfo +1 -1
- package/exports/core-api.ts +102 -102
- package/exports/main-api.ts +60 -51
- package/exports/utils.ts +41 -41
- package/package.json +5 -3
- package/scripts/please-use-pnpm.js +13 -0
- package/src/common/CommonTypes.ts +113 -105
- package/src/common/EventEmitter.ts +77 -77
- package/src/common/IAnimationController.ts +29 -29
- package/src/core/CoreExtension.ts +32 -32
- package/src/core/CoreNode.ts +926 -926
- package/src/core/CoreShaderManager.ts +243 -210
- package/src/core/CoreTextNode.ts +391 -391
- package/src/core/CoreTextureManager.ts +326 -326
- package/src/core/Stage.ts +354 -342
- package/src/core/animations/AnimationManager.ts +38 -38
- package/src/core/animations/CoreAnimation.ts +181 -177
- package/src/core/animations/CoreAnimationController.ts +148 -117
- package/src/core/lib/ContextSpy.ts +41 -41
- package/src/core/lib/ImageWorker.ts +124 -124
- package/src/core/lib/Matrix3d.ts +290 -290
- package/src/core/lib/WebGlContextWrapper.ts +992 -965
- package/src/core/lib/textureCompression.ts +152 -0
- package/src/core/lib/utils.ts +241 -241
- package/src/core/platform.ts +46 -46
- package/src/core/renderers/CoreContextTexture.ts +30 -30
- package/src/core/renderers/CoreRenderOp.ts +22 -22
- package/src/core/renderers/CoreRenderer.ts +63 -63
- package/src/core/renderers/CoreShader.ts +41 -41
- package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +37 -37
- package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +231 -211
- package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +107 -107
- package/src/core/renderers/webgl/WebGlCoreRenderer.ts +520 -520
- package/src/core/renderers/webgl/WebGlCoreShader.ts +337 -337
- package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
- package/src/core/renderers/webgl/internal/RendererUtils.ts +131 -131
- package/src/core/renderers/webgl/internal/ShaderUtils.ts +136 -136
- package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
- package/src/core/renderers/webgl/shaders/DefaultShader.ts +95 -95
- package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
- package/src/core/renderers/webgl/shaders/DynamicShader.ts +474 -474
- package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +161 -161
- package/src/core/renderers/webgl/shaders/SdfShader.ts +174 -174
- package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +86 -86
- package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
- package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +33 -33
- package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +135 -135
- package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +145 -145
- package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
- package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +176 -176
- package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +159 -159
- package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +186 -186
- package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +121 -121
- package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +114 -114
- package/src/core/text-rendering/TextTextureRendererUtils.ts +189 -189
- package/src/core/text-rendering/TrFontManager.ts +96 -96
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +141 -127
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -165
- package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +169 -166
- package/src/core/text-rendering/font-face-types/TrFontFace.ts +105 -105
- package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +77 -77
- package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +751 -738
- package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +741 -707
- package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +775 -777
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +84 -84
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +393 -392
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -46
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +51 -51
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
- package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
- package/src/core/text-rendering/renderers/TextRenderer.ts +504 -504
- package/src/core/textures/ColorTexture.ts +86 -86
- package/src/core/textures/ImageTexture.ts +133 -124
- package/src/core/textures/NoiseTexture.ts +96 -96
- package/src/core/textures/SubTexture.ts +143 -143
- package/src/core/textures/Texture.ts +218 -186
- package/src/core/utils.ts +224 -224
- package/src/env.d.ts +7 -7
- package/src/main-api/ICoreDriver.ts +66 -61
- package/src/main-api/INode.ts +470 -470
- package/src/main-api/Inspector.ts +432 -0
- package/src/main-api/RendererMain.ts +649 -610
- package/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.ts +45 -45
- package/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.ts +154 -154
- package/src/main-api/texture-usage-trackers/TextureUsageTracker.ts +54 -54
- package/src/render-drivers/main/MainCoreDriver.ts +148 -133
- package/src/render-drivers/main/MainOnlyNode.ts +453 -453
- package/src/render-drivers/main/MainOnlyTextNode.ts +261 -261
- package/src/render-drivers/threadx/NodeStruct.ts +300 -300
- package/src/render-drivers/threadx/SharedNode.ts +97 -97
- package/src/render-drivers/threadx/TextNodeStruct.ts +211 -211
- package/src/render-drivers/threadx/ThreadXCoreDriver.ts +285 -273
- package/src/render-drivers/threadx/ThreadXMainAnimationController.ts +99 -99
- package/src/render-drivers/threadx/ThreadXMainNode.ts +178 -178
- package/src/render-drivers/threadx/ThreadXMainTextNode.ts +85 -85
- package/src/render-drivers/threadx/ThreadXRendererMessage.ts +110 -97
- package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +238 -238
- package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +149 -149
- package/src/render-drivers/threadx/worker/renderer.ts +151 -151
- package/src/render-drivers/utils.ts +57 -57
- package/src/utils.ts +207 -207
- package/dist/src/core/lib/WebGlContext.d.ts +0 -414
- package/dist/src/core/lib/WebGlContext.js +0 -640
- package/dist/src/core/lib/WebGlContext.js.map +0 -1
- package/dist/src/core/scene/Scene.d.ts +0 -59
- package/dist/src/core/scene/Scene.js +0 -106
- package/dist/src/core/scene/Scene.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.d.ts +0 -8
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js +0 -29
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.d.ts +0 -19
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js +0 -84
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.d.ts +0 -8
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js +0 -40
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.d.ts +0 -2
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js +0 -41
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.d.ts +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js +0 -4
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.d.ts +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js +0 -2
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +0 -20
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +0 -55
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +0 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.d.ts +0 -9
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js +0 -32
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js.map +0 -1
|
@@ -1,965 +1,992 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
4
|
-
|
|
5
|
-
import { assertTruthy } from '../../utils.js';
|
|
6
|
-
import { isWebGl2 } from '../renderers/webgl/internal/WebGlUtils.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Optimized WebGL Context Wrapper
|
|
10
|
-
*
|
|
11
|
-
* @remarks
|
|
12
|
-
* This class contains the subset of the WebGLRenderingContext & WebGL2RenderingContext
|
|
13
|
-
* API that is used by the renderer. Select high volume WebGL methods include
|
|
14
|
-
* caching optimizations to avoid making WebGL calls if the state is already set
|
|
15
|
-
* to the desired value.
|
|
16
|
-
*
|
|
17
|
-
* While most methods contained are direct passthroughs to the WebGL context,
|
|
18
|
-
* some methods combine multiple WebGL calls into one for convenience, modify
|
|
19
|
-
* arguments to be more convenient, or are replaced by more specific methods.
|
|
20
|
-
*
|
|
21
|
-
* Not all methods are optimized. Only methods that are called frequently
|
|
22
|
-
* and/or have a high cost are optimized.
|
|
23
|
-
*
|
|
24
|
-
* A subset of GLenum constants are also exposed as properties on this class
|
|
25
|
-
* for convenience.
|
|
26
|
-
*/
|
|
27
|
-
export class WebGlContextWrapper {
|
|
28
|
-
//#region Cached WebGL State
|
|
29
|
-
private activeTextureUnit = 0;
|
|
30
|
-
private texture2dUnits: Array<WebGLTexture | null>;
|
|
31
|
-
private texture2dParams: WeakMap<
|
|
32
|
-
WebGLTexture,
|
|
33
|
-
Record<number, number | undefined>
|
|
34
|
-
> = new WeakMap();
|
|
35
|
-
private scissorEnabled;
|
|
36
|
-
private scissorX: number;
|
|
37
|
-
private scissorY: number;
|
|
38
|
-
private scissorWidth: number;
|
|
39
|
-
private scissorHeight: number;
|
|
40
|
-
private blendEnabled;
|
|
41
|
-
private blendSrcRgb: number;
|
|
42
|
-
private blendDstRgb: number;
|
|
43
|
-
private blendSrcAlpha: number;
|
|
44
|
-
private blendDstAlpha: number;
|
|
45
|
-
private boundArrayBuffer: WebGLBuffer | null;
|
|
46
|
-
private boundElementArrayBuffer: WebGLBuffer | null;
|
|
47
|
-
private curProgram: WebGLProgram | null;
|
|
48
|
-
private programUniforms: WeakMap<
|
|
49
|
-
WebGLProgram,
|
|
50
|
-
Map<WebGLUniformLocation, any[]>
|
|
51
|
-
> = new WeakMap();
|
|
52
|
-
//#endregion Cached WebGL State
|
|
53
|
-
|
|
54
|
-
//#region Canvas
|
|
55
|
-
public readonly canvas;
|
|
56
|
-
//#endregion Canvas
|
|
57
|
-
|
|
58
|
-
//#region WebGL Enums
|
|
59
|
-
public readonly MAX_RENDERBUFFER_SIZE;
|
|
60
|
-
public readonly MAX_TEXTURE_SIZE;
|
|
61
|
-
public readonly MAX_VIEWPORT_DIMS;
|
|
62
|
-
public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
|
|
63
|
-
public readonly MAX_TEXTURE_IMAGE_UNITS;
|
|
64
|
-
public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
|
|
65
|
-
public readonly MAX_VERTEX_ATTRIBS;
|
|
66
|
-
public readonly MAX_VARYING_VECTORS;
|
|
67
|
-
public readonly MAX_VERTEX_UNIFORM_VECTORS;
|
|
68
|
-
public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
|
|
69
|
-
public readonly TEXTURE_MAG_FILTER;
|
|
70
|
-
public readonly TEXTURE_MIN_FILTER;
|
|
71
|
-
public readonly TEXTURE_WRAP_S;
|
|
72
|
-
public readonly TEXTURE_WRAP_T;
|
|
73
|
-
public readonly LINEAR;
|
|
74
|
-
public readonly CLAMP_TO_EDGE;
|
|
75
|
-
public readonly RGBA;
|
|
76
|
-
public readonly UNSIGNED_BYTE;
|
|
77
|
-
public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
|
|
78
|
-
public readonly FLOAT;
|
|
79
|
-
public readonly TRIANGLES;
|
|
80
|
-
public readonly UNSIGNED_SHORT;
|
|
81
|
-
public readonly ONE;
|
|
82
|
-
public readonly ONE_MINUS_SRC_ALPHA;
|
|
83
|
-
public readonly VERTEX_SHADER;
|
|
84
|
-
public readonly FRAGMENT_SHADER;
|
|
85
|
-
public readonly STATIC_DRAW;
|
|
86
|
-
public readonly COMPILE_STATUS;
|
|
87
|
-
public readonly LINK_STATUS;
|
|
88
|
-
public readonly DYNAMIC_DRAW;
|
|
89
|
-
//#endregion WebGL Enums
|
|
90
|
-
|
|
91
|
-
constructor(private gl: WebGLRenderingContext | WebGL2RenderingContext) {
|
|
92
|
-
// The following code extracts the current state of the WebGL context
|
|
93
|
-
// to our local JavaScript cached version of it. This is so we can
|
|
94
|
-
// avoid making WebGL calls if we don't need to.
|
|
95
|
-
// We could assume that the WebGL context is in a default state, but
|
|
96
|
-
// in the future we may want to support restoring a broken WebGL context
|
|
97
|
-
// and this will help with that.
|
|
98
|
-
this.activeTextureUnit =
|
|
99
|
-
(gl.getParameter(gl.ACTIVE_TEXTURE) as number) - gl.TEXTURE0;
|
|
100
|
-
const maxTextureUnits = gl.getParameter(
|
|
101
|
-
gl.MAX_TEXTURE_IMAGE_UNITS,
|
|
102
|
-
) as number;
|
|
103
|
-
// save current texture units
|
|
104
|
-
this.texture2dUnits = new Array<undefined>(maxTextureUnits)
|
|
105
|
-
.fill(undefined)
|
|
106
|
-
.map((_, i) => {
|
|
107
|
-
this.activeTexture(i);
|
|
108
|
-
return gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture;
|
|
109
|
-
});
|
|
110
|
-
// restore active texture unit
|
|
111
|
-
this.activeTexture(this.activeTextureUnit);
|
|
112
|
-
this.scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST);
|
|
113
|
-
|
|
114
|
-
const scissorBox = gl.getParameter(gl.SCISSOR_BOX) as [
|
|
115
|
-
number,
|
|
116
|
-
number,
|
|
117
|
-
number,
|
|
118
|
-
number,
|
|
119
|
-
];
|
|
120
|
-
this.scissorX = scissorBox[0];
|
|
121
|
-
this.scissorY = scissorBox[1];
|
|
122
|
-
this.scissorWidth = scissorBox[2];
|
|
123
|
-
this.scissorHeight = scissorBox[3];
|
|
124
|
-
|
|
125
|
-
this.blendEnabled = gl.isEnabled(gl.BLEND);
|
|
126
|
-
this.blendSrcRgb = gl.getParameter(gl.BLEND_SRC_RGB) as number;
|
|
127
|
-
this.blendDstRgb = gl.getParameter(gl.BLEND_DST_RGB) as number;
|
|
128
|
-
this.blendSrcAlpha = gl.getParameter(gl.BLEND_SRC_ALPHA) as number;
|
|
129
|
-
this.blendDstAlpha = gl.getParameter(gl.BLEND_DST_ALPHA) as number;
|
|
130
|
-
|
|
131
|
-
this.boundArrayBuffer = gl.getParameter(
|
|
132
|
-
gl.ARRAY_BUFFER_BINDING,
|
|
133
|
-
) as WebGLBuffer;
|
|
134
|
-
this.boundElementArrayBuffer = gl.getParameter(
|
|
135
|
-
gl.ELEMENT_ARRAY_BUFFER_BINDING,
|
|
136
|
-
) as WebGLBuffer;
|
|
137
|
-
|
|
138
|
-
this.curProgram = gl.getParameter(
|
|
139
|
-
gl.CURRENT_PROGRAM,
|
|
140
|
-
) as WebGLProgram | null;
|
|
141
|
-
|
|
142
|
-
this.canvas = gl.canvas;
|
|
143
|
-
|
|
144
|
-
// Extract GLenums
|
|
145
|
-
this.MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE;
|
|
146
|
-
this.MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE;
|
|
147
|
-
this.MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS;
|
|
148
|
-
this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
|
|
149
|
-
this.MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS;
|
|
150
|
-
this.MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
|
|
151
|
-
this.MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS;
|
|
152
|
-
this.MAX_VARYING_VECTORS = gl.MAX_VARYING_VECTORS;
|
|
153
|
-
this.MAX_VERTEX_UNIFORM_VECTORS = gl.MAX_VERTEX_UNIFORM_VECTORS;
|
|
154
|
-
this.MAX_FRAGMENT_UNIFORM_VECTORS = gl.MAX_FRAGMENT_UNIFORM_VECTORS;
|
|
155
|
-
this.TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER;
|
|
156
|
-
this.TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER;
|
|
157
|
-
this.TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S;
|
|
158
|
-
this.TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T;
|
|
159
|
-
this.LINEAR = gl.LINEAR;
|
|
160
|
-
this.CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE;
|
|
161
|
-
this.RGBA = gl.RGBA;
|
|
162
|
-
this.UNSIGNED_BYTE = gl.UNSIGNED_BYTE;
|
|
163
|
-
this.UNPACK_PREMULTIPLY_ALPHA_WEBGL = gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL;
|
|
164
|
-
this.FLOAT = gl.FLOAT;
|
|
165
|
-
this.TRIANGLES = gl.TRIANGLES;
|
|
166
|
-
this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
|
|
167
|
-
this.ONE = gl.ONE;
|
|
168
|
-
this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
|
|
169
|
-
this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
|
|
170
|
-
this.TRIANGLES = gl.TRIANGLES;
|
|
171
|
-
this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
|
|
172
|
-
this.VERTEX_SHADER = gl.VERTEX_SHADER;
|
|
173
|
-
this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
|
|
174
|
-
this.STATIC_DRAW = gl.STATIC_DRAW;
|
|
175
|
-
this.COMPILE_STATUS = gl.COMPILE_STATUS;
|
|
176
|
-
this.LINK_STATUS = gl.LINK_STATUS;
|
|
177
|
-
this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Returns true if the WebGL context is WebGL2
|
|
181
|
-
*
|
|
182
|
-
* @returns
|
|
183
|
-
*/
|
|
184
|
-
isWebGl2() {
|
|
185
|
-
return isWebGl2(this.gl);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* ```
|
|
190
|
-
* gl.activeTexture(textureUnit + gl.TEXTURE0);
|
|
191
|
-
* ```
|
|
192
|
-
*
|
|
193
|
-
* @remarks
|
|
194
|
-
* **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
|
|
195
|
-
*
|
|
196
|
-
* @param textureUnit
|
|
197
|
-
*/
|
|
198
|
-
activeTexture(textureUnit: number) {
|
|
199
|
-
const { gl } = this;
|
|
200
|
-
if (this.activeTextureUnit !== textureUnit) {
|
|
201
|
-
gl.activeTexture(textureUnit + gl.TEXTURE0);
|
|
202
|
-
this.activeTextureUnit = textureUnit;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* ```
|
|
208
|
-
* gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
209
|
-
* ```
|
|
210
|
-
* @remarks
|
|
211
|
-
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
212
|
-
*
|
|
213
|
-
* @param texture
|
|
214
|
-
*/
|
|
215
|
-
bindTexture(texture: WebGLTexture | null) {
|
|
216
|
-
const { gl, activeTextureUnit, texture2dUnits } = this;
|
|
217
|
-
|
|
218
|
-
if (texture2dUnits[activeTextureUnit] === texture) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
texture2dUnits[activeTextureUnit] = texture;
|
|
222
|
-
|
|
223
|
-
gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
private _getActiveTexture(): WebGLTexture | null {
|
|
227
|
-
const { activeTextureUnit, texture2dUnits } = this;
|
|
228
|
-
return texture2dUnits[activeTextureUnit]!;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* ```
|
|
233
|
-
* gl.texParameteri(gl.TEXTURE_2D, pname, param);
|
|
234
|
-
* ```
|
|
235
|
-
* @remarks
|
|
236
|
-
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
237
|
-
*
|
|
238
|
-
* @param pname
|
|
239
|
-
* @param param
|
|
240
|
-
* @returns
|
|
241
|
-
*/
|
|
242
|
-
texParameteri(pname: number, param: number) {
|
|
243
|
-
const { gl, texture2dParams } = this;
|
|
244
|
-
|
|
245
|
-
const activeTexture = this._getActiveTexture();
|
|
246
|
-
if (!activeTexture) {
|
|
247
|
-
throw new Error('No active texture');
|
|
248
|
-
}
|
|
249
|
-
let textureParams = texture2dParams.get(activeTexture);
|
|
250
|
-
if (!textureParams) {
|
|
251
|
-
textureParams = {};
|
|
252
|
-
texture2dParams.set(activeTexture, textureParams);
|
|
253
|
-
}
|
|
254
|
-
if (textureParams[pname] === param) {
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
textureParams[pname] = param;
|
|
258
|
-
gl.texParameteri(gl.TEXTURE_2D, pname, param);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* ```
|
|
263
|
-
* gl.texImage2D(
|
|
264
|
-
* gl.TEXTURE_2D,
|
|
265
|
-
* level,
|
|
266
|
-
* internalFormat,
|
|
267
|
-
* width,
|
|
268
|
-
* height,
|
|
269
|
-
* border,
|
|
270
|
-
* format,
|
|
271
|
-
* type,
|
|
272
|
-
* pixels,
|
|
273
|
-
* );
|
|
274
|
-
* ```
|
|
275
|
-
* @remarks
|
|
276
|
-
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
277
|
-
*
|
|
278
|
-
* @param level
|
|
279
|
-
* @param internalFormat
|
|
280
|
-
* @param width
|
|
281
|
-
* @param height
|
|
282
|
-
* @param border
|
|
283
|
-
* @param format
|
|
284
|
-
* @param type
|
|
285
|
-
* @param pixels
|
|
286
|
-
*/
|
|
287
|
-
texImage2D(
|
|
288
|
-
level: GLint,
|
|
289
|
-
internalformat: GLint,
|
|
290
|
-
width: GLsizei,
|
|
291
|
-
height: GLsizei,
|
|
292
|
-
border: GLint,
|
|
293
|
-
format: GLenum,
|
|
294
|
-
type: GLenum,
|
|
295
|
-
pixels: ArrayBufferView | null,
|
|
296
|
-
): void;
|
|
297
|
-
texImage2D(
|
|
298
|
-
level: GLint,
|
|
299
|
-
internalformat: GLint,
|
|
300
|
-
format: GLenum,
|
|
301
|
-
type: GLenum,
|
|
302
|
-
source: TexImageSource,
|
|
303
|
-
): void;
|
|
304
|
-
texImage2D(
|
|
305
|
-
level: any,
|
|
306
|
-
internalFormat: any,
|
|
307
|
-
widthOrFormat: any,
|
|
308
|
-
heightOrType: any,
|
|
309
|
-
borderOrSource: any,
|
|
310
|
-
format?: any,
|
|
311
|
-
type?: any,
|
|
312
|
-
pixels?: any,
|
|
313
|
-
) {
|
|
314
|
-
const { gl } = this;
|
|
315
|
-
if (format) {
|
|
316
|
-
gl.texImage2D(
|
|
317
|
-
gl.TEXTURE_2D,
|
|
318
|
-
level,
|
|
319
|
-
internalFormat,
|
|
320
|
-
widthOrFormat,
|
|
321
|
-
heightOrType,
|
|
322
|
-
borderOrSource,
|
|
323
|
-
format,
|
|
324
|
-
type,
|
|
325
|
-
pixels,
|
|
326
|
-
);
|
|
327
|
-
} else {
|
|
328
|
-
gl.texImage2D(
|
|
329
|
-
gl.TEXTURE_2D,
|
|
330
|
-
level,
|
|
331
|
-
internalFormat,
|
|
332
|
-
widthOrFormat,
|
|
333
|
-
heightOrType,
|
|
334
|
-
borderOrSource,
|
|
335
|
-
);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
* ```
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
gl.
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* ```
|
|
394
|
-
* gl.
|
|
395
|
-
* ```
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
* ```
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
* @param
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
* ```
|
|
421
|
-
*
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
*
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
* @
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
}
|
|
475
|
-
if (
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
*
|
|
539
|
-
*
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
gl
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
*
|
|
561
|
-
* gl.
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
* @param
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
* @param
|
|
594
|
-
* @param
|
|
595
|
-
* @param
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
*
|
|
619
|
-
*
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
* @param
|
|
623
|
-
* @
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
* @
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
* @
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
gl
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
*
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
* @
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
return gl.
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
/**
|
|
734
|
-
* ```
|
|
735
|
-
* gl.
|
|
736
|
-
* ```
|
|
737
|
-
*
|
|
738
|
-
* @param
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
*
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
* @param
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
gl
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
*
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
* ```
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
gl
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
*
|
|
802
|
-
*
|
|
803
|
-
* @param
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
gl
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
*
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
* ```
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
* ```
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
* ```
|
|
863
|
-
*
|
|
864
|
-
*
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
*
|
|
878
|
-
*
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
* @returns
|
|
892
|
-
*/
|
|
893
|
-
|
|
894
|
-
const { gl } = this;
|
|
895
|
-
return gl.
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
/**
|
|
899
|
-
* ```
|
|
900
|
-
* gl.
|
|
901
|
-
* ```
|
|
902
|
-
*
|
|
903
|
-
* @param
|
|
904
|
-
* @param
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
gl
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
*
|
|
916
|
-
*
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
4
|
+
|
|
5
|
+
import { assertTruthy } from '../../utils.js';
|
|
6
|
+
import { isWebGl2 } from '../renderers/webgl/internal/WebGlUtils.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Optimized WebGL Context Wrapper
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* This class contains the subset of the WebGLRenderingContext & WebGL2RenderingContext
|
|
13
|
+
* API that is used by the renderer. Select high volume WebGL methods include
|
|
14
|
+
* caching optimizations to avoid making WebGL calls if the state is already set
|
|
15
|
+
* to the desired value.
|
|
16
|
+
*
|
|
17
|
+
* While most methods contained are direct passthroughs to the WebGL context,
|
|
18
|
+
* some methods combine multiple WebGL calls into one for convenience, modify
|
|
19
|
+
* arguments to be more convenient, or are replaced by more specific methods.
|
|
20
|
+
*
|
|
21
|
+
* Not all methods are optimized. Only methods that are called frequently
|
|
22
|
+
* and/or have a high cost are optimized.
|
|
23
|
+
*
|
|
24
|
+
* A subset of GLenum constants are also exposed as properties on this class
|
|
25
|
+
* for convenience.
|
|
26
|
+
*/
|
|
27
|
+
export class WebGlContextWrapper {
|
|
28
|
+
//#region Cached WebGL State
|
|
29
|
+
private activeTextureUnit = 0;
|
|
30
|
+
private texture2dUnits: Array<WebGLTexture | null>;
|
|
31
|
+
private texture2dParams: WeakMap<
|
|
32
|
+
WebGLTexture,
|
|
33
|
+
Record<number, number | undefined>
|
|
34
|
+
> = new WeakMap();
|
|
35
|
+
private scissorEnabled;
|
|
36
|
+
private scissorX: number;
|
|
37
|
+
private scissorY: number;
|
|
38
|
+
private scissorWidth: number;
|
|
39
|
+
private scissorHeight: number;
|
|
40
|
+
private blendEnabled;
|
|
41
|
+
private blendSrcRgb: number;
|
|
42
|
+
private blendDstRgb: number;
|
|
43
|
+
private blendSrcAlpha: number;
|
|
44
|
+
private blendDstAlpha: number;
|
|
45
|
+
private boundArrayBuffer: WebGLBuffer | null;
|
|
46
|
+
private boundElementArrayBuffer: WebGLBuffer | null;
|
|
47
|
+
private curProgram: WebGLProgram | null;
|
|
48
|
+
private programUniforms: WeakMap<
|
|
49
|
+
WebGLProgram,
|
|
50
|
+
Map<WebGLUniformLocation, any[]>
|
|
51
|
+
> = new WeakMap();
|
|
52
|
+
//#endregion Cached WebGL State
|
|
53
|
+
|
|
54
|
+
//#region Canvas
|
|
55
|
+
public readonly canvas;
|
|
56
|
+
//#endregion Canvas
|
|
57
|
+
|
|
58
|
+
//#region WebGL Enums
|
|
59
|
+
public readonly MAX_RENDERBUFFER_SIZE;
|
|
60
|
+
public readonly MAX_TEXTURE_SIZE;
|
|
61
|
+
public readonly MAX_VIEWPORT_DIMS;
|
|
62
|
+
public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
|
|
63
|
+
public readonly MAX_TEXTURE_IMAGE_UNITS;
|
|
64
|
+
public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
|
|
65
|
+
public readonly MAX_VERTEX_ATTRIBS;
|
|
66
|
+
public readonly MAX_VARYING_VECTORS;
|
|
67
|
+
public readonly MAX_VERTEX_UNIFORM_VECTORS;
|
|
68
|
+
public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
|
|
69
|
+
public readonly TEXTURE_MAG_FILTER;
|
|
70
|
+
public readonly TEXTURE_MIN_FILTER;
|
|
71
|
+
public readonly TEXTURE_WRAP_S;
|
|
72
|
+
public readonly TEXTURE_WRAP_T;
|
|
73
|
+
public readonly LINEAR;
|
|
74
|
+
public readonly CLAMP_TO_EDGE;
|
|
75
|
+
public readonly RGBA;
|
|
76
|
+
public readonly UNSIGNED_BYTE;
|
|
77
|
+
public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
|
|
78
|
+
public readonly FLOAT;
|
|
79
|
+
public readonly TRIANGLES;
|
|
80
|
+
public readonly UNSIGNED_SHORT;
|
|
81
|
+
public readonly ONE;
|
|
82
|
+
public readonly ONE_MINUS_SRC_ALPHA;
|
|
83
|
+
public readonly VERTEX_SHADER;
|
|
84
|
+
public readonly FRAGMENT_SHADER;
|
|
85
|
+
public readonly STATIC_DRAW;
|
|
86
|
+
public readonly COMPILE_STATUS;
|
|
87
|
+
public readonly LINK_STATUS;
|
|
88
|
+
public readonly DYNAMIC_DRAW;
|
|
89
|
+
//#endregion WebGL Enums
|
|
90
|
+
|
|
91
|
+
constructor(private gl: WebGLRenderingContext | WebGL2RenderingContext) {
|
|
92
|
+
// The following code extracts the current state of the WebGL context
|
|
93
|
+
// to our local JavaScript cached version of it. This is so we can
|
|
94
|
+
// avoid making WebGL calls if we don't need to.
|
|
95
|
+
// We could assume that the WebGL context is in a default state, but
|
|
96
|
+
// in the future we may want to support restoring a broken WebGL context
|
|
97
|
+
// and this will help with that.
|
|
98
|
+
this.activeTextureUnit =
|
|
99
|
+
(gl.getParameter(gl.ACTIVE_TEXTURE) as number) - gl.TEXTURE0;
|
|
100
|
+
const maxTextureUnits = gl.getParameter(
|
|
101
|
+
gl.MAX_TEXTURE_IMAGE_UNITS,
|
|
102
|
+
) as number;
|
|
103
|
+
// save current texture units
|
|
104
|
+
this.texture2dUnits = new Array<undefined>(maxTextureUnits)
|
|
105
|
+
.fill(undefined)
|
|
106
|
+
.map((_, i) => {
|
|
107
|
+
this.activeTexture(i);
|
|
108
|
+
return gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture;
|
|
109
|
+
});
|
|
110
|
+
// restore active texture unit
|
|
111
|
+
this.activeTexture(this.activeTextureUnit);
|
|
112
|
+
this.scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST);
|
|
113
|
+
|
|
114
|
+
const scissorBox = gl.getParameter(gl.SCISSOR_BOX) as [
|
|
115
|
+
number,
|
|
116
|
+
number,
|
|
117
|
+
number,
|
|
118
|
+
number,
|
|
119
|
+
];
|
|
120
|
+
this.scissorX = scissorBox[0];
|
|
121
|
+
this.scissorY = scissorBox[1];
|
|
122
|
+
this.scissorWidth = scissorBox[2];
|
|
123
|
+
this.scissorHeight = scissorBox[3];
|
|
124
|
+
|
|
125
|
+
this.blendEnabled = gl.isEnabled(gl.BLEND);
|
|
126
|
+
this.blendSrcRgb = gl.getParameter(gl.BLEND_SRC_RGB) as number;
|
|
127
|
+
this.blendDstRgb = gl.getParameter(gl.BLEND_DST_RGB) as number;
|
|
128
|
+
this.blendSrcAlpha = gl.getParameter(gl.BLEND_SRC_ALPHA) as number;
|
|
129
|
+
this.blendDstAlpha = gl.getParameter(gl.BLEND_DST_ALPHA) as number;
|
|
130
|
+
|
|
131
|
+
this.boundArrayBuffer = gl.getParameter(
|
|
132
|
+
gl.ARRAY_BUFFER_BINDING,
|
|
133
|
+
) as WebGLBuffer;
|
|
134
|
+
this.boundElementArrayBuffer = gl.getParameter(
|
|
135
|
+
gl.ELEMENT_ARRAY_BUFFER_BINDING,
|
|
136
|
+
) as WebGLBuffer;
|
|
137
|
+
|
|
138
|
+
this.curProgram = gl.getParameter(
|
|
139
|
+
gl.CURRENT_PROGRAM,
|
|
140
|
+
) as WebGLProgram | null;
|
|
141
|
+
|
|
142
|
+
this.canvas = gl.canvas;
|
|
143
|
+
|
|
144
|
+
// Extract GLenums
|
|
145
|
+
this.MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE;
|
|
146
|
+
this.MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE;
|
|
147
|
+
this.MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS;
|
|
148
|
+
this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
|
|
149
|
+
this.MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS;
|
|
150
|
+
this.MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
|
|
151
|
+
this.MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS;
|
|
152
|
+
this.MAX_VARYING_VECTORS = gl.MAX_VARYING_VECTORS;
|
|
153
|
+
this.MAX_VERTEX_UNIFORM_VECTORS = gl.MAX_VERTEX_UNIFORM_VECTORS;
|
|
154
|
+
this.MAX_FRAGMENT_UNIFORM_VECTORS = gl.MAX_FRAGMENT_UNIFORM_VECTORS;
|
|
155
|
+
this.TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER;
|
|
156
|
+
this.TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER;
|
|
157
|
+
this.TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S;
|
|
158
|
+
this.TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T;
|
|
159
|
+
this.LINEAR = gl.LINEAR;
|
|
160
|
+
this.CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE;
|
|
161
|
+
this.RGBA = gl.RGBA;
|
|
162
|
+
this.UNSIGNED_BYTE = gl.UNSIGNED_BYTE;
|
|
163
|
+
this.UNPACK_PREMULTIPLY_ALPHA_WEBGL = gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL;
|
|
164
|
+
this.FLOAT = gl.FLOAT;
|
|
165
|
+
this.TRIANGLES = gl.TRIANGLES;
|
|
166
|
+
this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
|
|
167
|
+
this.ONE = gl.ONE;
|
|
168
|
+
this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
|
|
169
|
+
this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
|
|
170
|
+
this.TRIANGLES = gl.TRIANGLES;
|
|
171
|
+
this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
|
|
172
|
+
this.VERTEX_SHADER = gl.VERTEX_SHADER;
|
|
173
|
+
this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
|
|
174
|
+
this.STATIC_DRAW = gl.STATIC_DRAW;
|
|
175
|
+
this.COMPILE_STATUS = gl.COMPILE_STATUS;
|
|
176
|
+
this.LINK_STATUS = gl.LINK_STATUS;
|
|
177
|
+
this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Returns true if the WebGL context is WebGL2
|
|
181
|
+
*
|
|
182
|
+
* @returns
|
|
183
|
+
*/
|
|
184
|
+
isWebGl2() {
|
|
185
|
+
return isWebGl2(this.gl);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* ```
|
|
190
|
+
* gl.activeTexture(textureUnit + gl.TEXTURE0);
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
|
|
195
|
+
*
|
|
196
|
+
* @param textureUnit
|
|
197
|
+
*/
|
|
198
|
+
activeTexture(textureUnit: number) {
|
|
199
|
+
const { gl } = this;
|
|
200
|
+
if (this.activeTextureUnit !== textureUnit) {
|
|
201
|
+
gl.activeTexture(textureUnit + gl.TEXTURE0);
|
|
202
|
+
this.activeTextureUnit = textureUnit;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* ```
|
|
208
|
+
* gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
209
|
+
* ```
|
|
210
|
+
* @remarks
|
|
211
|
+
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
212
|
+
*
|
|
213
|
+
* @param texture
|
|
214
|
+
*/
|
|
215
|
+
bindTexture(texture: WebGLTexture | null) {
|
|
216
|
+
const { gl, activeTextureUnit, texture2dUnits } = this;
|
|
217
|
+
|
|
218
|
+
if (texture2dUnits[activeTextureUnit] === texture) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
texture2dUnits[activeTextureUnit] = texture;
|
|
222
|
+
|
|
223
|
+
gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private _getActiveTexture(): WebGLTexture | null {
|
|
227
|
+
const { activeTextureUnit, texture2dUnits } = this;
|
|
228
|
+
return texture2dUnits[activeTextureUnit]!;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* ```
|
|
233
|
+
* gl.texParameteri(gl.TEXTURE_2D, pname, param);
|
|
234
|
+
* ```
|
|
235
|
+
* @remarks
|
|
236
|
+
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
237
|
+
*
|
|
238
|
+
* @param pname
|
|
239
|
+
* @param param
|
|
240
|
+
* @returns
|
|
241
|
+
*/
|
|
242
|
+
texParameteri(pname: number, param: number) {
|
|
243
|
+
const { gl, texture2dParams } = this;
|
|
244
|
+
|
|
245
|
+
const activeTexture = this._getActiveTexture();
|
|
246
|
+
if (!activeTexture) {
|
|
247
|
+
throw new Error('No active texture');
|
|
248
|
+
}
|
|
249
|
+
let textureParams = texture2dParams.get(activeTexture);
|
|
250
|
+
if (!textureParams) {
|
|
251
|
+
textureParams = {};
|
|
252
|
+
texture2dParams.set(activeTexture, textureParams);
|
|
253
|
+
}
|
|
254
|
+
if (textureParams[pname] === param) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
textureParams[pname] = param;
|
|
258
|
+
gl.texParameteri(gl.TEXTURE_2D, pname, param);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* ```
|
|
263
|
+
* gl.texImage2D(
|
|
264
|
+
* gl.TEXTURE_2D,
|
|
265
|
+
* level,
|
|
266
|
+
* internalFormat,
|
|
267
|
+
* width,
|
|
268
|
+
* height,
|
|
269
|
+
* border,
|
|
270
|
+
* format,
|
|
271
|
+
* type,
|
|
272
|
+
* pixels,
|
|
273
|
+
* );
|
|
274
|
+
* ```
|
|
275
|
+
* @remarks
|
|
276
|
+
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
277
|
+
*
|
|
278
|
+
* @param level
|
|
279
|
+
* @param internalFormat
|
|
280
|
+
* @param width
|
|
281
|
+
* @param height
|
|
282
|
+
* @param border
|
|
283
|
+
* @param format
|
|
284
|
+
* @param type
|
|
285
|
+
* @param pixels
|
|
286
|
+
*/
|
|
287
|
+
texImage2D(
|
|
288
|
+
level: GLint,
|
|
289
|
+
internalformat: GLint,
|
|
290
|
+
width: GLsizei,
|
|
291
|
+
height: GLsizei,
|
|
292
|
+
border: GLint,
|
|
293
|
+
format: GLenum,
|
|
294
|
+
type: GLenum,
|
|
295
|
+
pixels: ArrayBufferView | null,
|
|
296
|
+
): void;
|
|
297
|
+
texImage2D(
|
|
298
|
+
level: GLint,
|
|
299
|
+
internalformat: GLint,
|
|
300
|
+
format: GLenum,
|
|
301
|
+
type: GLenum,
|
|
302
|
+
source: TexImageSource,
|
|
303
|
+
): void;
|
|
304
|
+
texImage2D(
|
|
305
|
+
level: any,
|
|
306
|
+
internalFormat: any,
|
|
307
|
+
widthOrFormat: any,
|
|
308
|
+
heightOrType: any,
|
|
309
|
+
borderOrSource: any,
|
|
310
|
+
format?: any,
|
|
311
|
+
type?: any,
|
|
312
|
+
pixels?: any,
|
|
313
|
+
) {
|
|
314
|
+
const { gl } = this;
|
|
315
|
+
if (format) {
|
|
316
|
+
gl.texImage2D(
|
|
317
|
+
gl.TEXTURE_2D,
|
|
318
|
+
level,
|
|
319
|
+
internalFormat,
|
|
320
|
+
widthOrFormat,
|
|
321
|
+
heightOrType,
|
|
322
|
+
borderOrSource,
|
|
323
|
+
format,
|
|
324
|
+
type,
|
|
325
|
+
pixels,
|
|
326
|
+
);
|
|
327
|
+
} else {
|
|
328
|
+
gl.texImage2D(
|
|
329
|
+
gl.TEXTURE_2D,
|
|
330
|
+
level,
|
|
331
|
+
internalFormat,
|
|
332
|
+
widthOrFormat,
|
|
333
|
+
heightOrType,
|
|
334
|
+
borderOrSource,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* ```
|
|
340
|
+
* gl.compressedTexImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, data);
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @remarks
|
|
344
|
+
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
345
|
+
*/
|
|
346
|
+
|
|
347
|
+
compressedTexImage2D(
|
|
348
|
+
level: GLint,
|
|
349
|
+
internalformat: GLenum,
|
|
350
|
+
width: GLsizei,
|
|
351
|
+
height: GLsizei,
|
|
352
|
+
border: GLint,
|
|
353
|
+
data?: ArrayBufferView,
|
|
354
|
+
): void {
|
|
355
|
+
const { gl } = this;
|
|
356
|
+
gl.compressedTexImage2D(
|
|
357
|
+
gl.TEXTURE_2D,
|
|
358
|
+
level,
|
|
359
|
+
internalformat,
|
|
360
|
+
width,
|
|
361
|
+
height,
|
|
362
|
+
border,
|
|
363
|
+
data as ArrayBufferView,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* ```
|
|
368
|
+
* gl.pixelStorei(pname, param);
|
|
369
|
+
* ```
|
|
370
|
+
*
|
|
371
|
+
* @param pname
|
|
372
|
+
* @param param
|
|
373
|
+
*/
|
|
374
|
+
pixelStorei(pname: GLenum, param: GLint | GLboolean) {
|
|
375
|
+
const { gl } = this;
|
|
376
|
+
gl.pixelStorei(pname, param);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* ```
|
|
381
|
+
* gl.generateMipmap(gl.TEXTURE_2D);
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* @remarks
|
|
385
|
+
* **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
|
|
386
|
+
*/
|
|
387
|
+
generateMipmap() {
|
|
388
|
+
const { gl } = this;
|
|
389
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* ```
|
|
394
|
+
* gl.createTexture();
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* @returns
|
|
398
|
+
*/
|
|
399
|
+
createTexture() {
|
|
400
|
+
const { gl } = this;
|
|
401
|
+
return gl.createTexture();
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* ```
|
|
406
|
+
* gl.deleteTexture(texture);
|
|
407
|
+
* ```
|
|
408
|
+
*
|
|
409
|
+
* @param texture
|
|
410
|
+
*/
|
|
411
|
+
deleteTexture(texture: WebGLTexture | null) {
|
|
412
|
+
const { gl } = this;
|
|
413
|
+
if (texture) {
|
|
414
|
+
this.texture2dParams.delete(texture);
|
|
415
|
+
}
|
|
416
|
+
gl.deleteTexture(texture);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* ```
|
|
421
|
+
* gl.viewport(x, y, width, height);
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
|
|
425
|
+
const { gl } = this;
|
|
426
|
+
gl.viewport(x, y, width, height);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* ```
|
|
431
|
+
* gl.clearColor(red, green, blue, alpha);
|
|
432
|
+
* ```
|
|
433
|
+
*
|
|
434
|
+
* @param red
|
|
435
|
+
* @param green
|
|
436
|
+
* @param blue
|
|
437
|
+
* @param alpha
|
|
438
|
+
*/
|
|
439
|
+
clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
|
|
440
|
+
const { gl } = this;
|
|
441
|
+
gl.clearColor(red, green, blue, alpha);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* ```
|
|
446
|
+
* gl["enable"|"disable"](gl.SCISSOR_TEST);
|
|
447
|
+
* ```
|
|
448
|
+
* @param enable
|
|
449
|
+
*/
|
|
450
|
+
setScissorTest(enable: boolean) {
|
|
451
|
+
const { gl, scissorEnabled } = this;
|
|
452
|
+
if (enable === scissorEnabled) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
if (enable) {
|
|
456
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
457
|
+
} else {
|
|
458
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
459
|
+
}
|
|
460
|
+
this.scissorEnabled = enable;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* ```
|
|
465
|
+
* gl.scissor(x, y, width, height);
|
|
466
|
+
* ```
|
|
467
|
+
*
|
|
468
|
+
* @param x
|
|
469
|
+
* @param y
|
|
470
|
+
* @param width
|
|
471
|
+
* @param height
|
|
472
|
+
*/
|
|
473
|
+
scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
|
|
474
|
+
const { gl, scissorX, scissorY, scissorWidth, scissorHeight } = this;
|
|
475
|
+
if (
|
|
476
|
+
x !== scissorX ||
|
|
477
|
+
y !== scissorY ||
|
|
478
|
+
width !== scissorWidth ||
|
|
479
|
+
height !== scissorHeight
|
|
480
|
+
) {
|
|
481
|
+
gl.scissor(x, y, width, height);
|
|
482
|
+
this.scissorX = x;
|
|
483
|
+
this.scissorY = y;
|
|
484
|
+
this.scissorWidth = width;
|
|
485
|
+
this.scissorHeight = height;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* ```
|
|
491
|
+
* gl["enable"|"disable"](gl.BLEND);
|
|
492
|
+
* ```
|
|
493
|
+
*
|
|
494
|
+
* @param blend
|
|
495
|
+
* @returns
|
|
496
|
+
*/
|
|
497
|
+
setBlend(blend: boolean) {
|
|
498
|
+
const { gl, blendEnabled } = this;
|
|
499
|
+
if (blend === blendEnabled) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
if (blend) {
|
|
503
|
+
gl.enable(gl.BLEND);
|
|
504
|
+
} else {
|
|
505
|
+
gl.disable(gl.BLEND);
|
|
506
|
+
}
|
|
507
|
+
this.blendEnabled = blend;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* ```
|
|
512
|
+
* gl.blendFunc(src, dst);
|
|
513
|
+
* ```
|
|
514
|
+
*
|
|
515
|
+
* @param src
|
|
516
|
+
* @param dst
|
|
517
|
+
*/
|
|
518
|
+
blendFunc(src: GLenum, dst: GLenum) {
|
|
519
|
+
const { gl, blendSrcRgb, blendDstRgb, blendSrcAlpha, blendDstAlpha } = this;
|
|
520
|
+
if (
|
|
521
|
+
src !== blendSrcRgb ||
|
|
522
|
+
dst !== blendDstRgb ||
|
|
523
|
+
src !== blendSrcAlpha ||
|
|
524
|
+
dst !== blendDstAlpha
|
|
525
|
+
) {
|
|
526
|
+
gl.blendFunc(src, dst);
|
|
527
|
+
this.blendSrcRgb = src;
|
|
528
|
+
this.blendDstRgb = dst;
|
|
529
|
+
this.blendSrcAlpha = src;
|
|
530
|
+
this.blendDstAlpha = dst;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* ```
|
|
536
|
+
* createBuffer();
|
|
537
|
+
* ```
|
|
538
|
+
*
|
|
539
|
+
* @returns
|
|
540
|
+
*/
|
|
541
|
+
createBuffer() {
|
|
542
|
+
const { gl } = this;
|
|
543
|
+
return gl.createBuffer();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* ```
|
|
548
|
+
* gl.clear(gl.COLOR_BUFFER_BIT);
|
|
549
|
+
* ```
|
|
550
|
+
*
|
|
551
|
+
* @remarks
|
|
552
|
+
* **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
|
|
553
|
+
*/
|
|
554
|
+
clear() {
|
|
555
|
+
const { gl } = this;
|
|
556
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* ```
|
|
561
|
+
* gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
562
|
+
* gl.bufferData(gl.ARRAY_BUFFER, data, usage);
|
|
563
|
+
* ```
|
|
564
|
+
*
|
|
565
|
+
* @remarks
|
|
566
|
+
* **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
|
|
567
|
+
*
|
|
568
|
+
* @param buffer
|
|
569
|
+
* @param data
|
|
570
|
+
* @param usage
|
|
571
|
+
*/
|
|
572
|
+
arrayBufferData(
|
|
573
|
+
buffer: WebGLBuffer | null,
|
|
574
|
+
data: ArrayBufferView,
|
|
575
|
+
usage: GLenum,
|
|
576
|
+
) {
|
|
577
|
+
const { gl, boundArrayBuffer } = this;
|
|
578
|
+
if (boundArrayBuffer !== buffer) {
|
|
579
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
580
|
+
this.boundArrayBuffer = buffer;
|
|
581
|
+
}
|
|
582
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, usage);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* ```
|
|
587
|
+
* gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
|
|
588
|
+
* gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
|
|
589
|
+
* ```
|
|
590
|
+
* @remarks
|
|
591
|
+
* **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
|
|
592
|
+
*
|
|
593
|
+
* @param buffer
|
|
594
|
+
* @param data
|
|
595
|
+
* @param usage
|
|
596
|
+
*/
|
|
597
|
+
elementArrayBufferData(
|
|
598
|
+
buffer: WebGLBuffer | null,
|
|
599
|
+
data: ArrayBufferView,
|
|
600
|
+
usage: GLenum,
|
|
601
|
+
) {
|
|
602
|
+
const { gl, boundElementArrayBuffer } = this;
|
|
603
|
+
if (boundElementArrayBuffer !== buffer) {
|
|
604
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
|
|
605
|
+
this.boundElementArrayBuffer = buffer;
|
|
606
|
+
}
|
|
607
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* ```
|
|
612
|
+
* gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
613
|
+
* gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
|
|
614
|
+
* ```
|
|
615
|
+
*
|
|
616
|
+
* @remarks
|
|
617
|
+
* **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
|
|
618
|
+
*
|
|
619
|
+
* @param buffer
|
|
620
|
+
* @param index
|
|
621
|
+
* @param size
|
|
622
|
+
* @param type
|
|
623
|
+
* @param normalized
|
|
624
|
+
* @param stride
|
|
625
|
+
* @param offset
|
|
626
|
+
*/
|
|
627
|
+
vertexAttribPointer(
|
|
628
|
+
buffer: WebGLBuffer,
|
|
629
|
+
index: GLuint,
|
|
630
|
+
size: GLint,
|
|
631
|
+
type: GLenum,
|
|
632
|
+
normalized: GLboolean,
|
|
633
|
+
stride: GLsizei,
|
|
634
|
+
offset: GLintptr,
|
|
635
|
+
) {
|
|
636
|
+
const { gl, boundArrayBuffer } = this;
|
|
637
|
+
if (boundArrayBuffer !== buffer) {
|
|
638
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
639
|
+
this.boundArrayBuffer = buffer;
|
|
640
|
+
}
|
|
641
|
+
gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* ```
|
|
646
|
+
* gl.useProgram(program);
|
|
647
|
+
* ```
|
|
648
|
+
*
|
|
649
|
+
* @param program
|
|
650
|
+
* @returns
|
|
651
|
+
*/
|
|
652
|
+
useProgram(program: WebGLProgram | null) {
|
|
653
|
+
const { gl, curProgram } = this;
|
|
654
|
+
if (curProgram === program) {
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
gl.useProgram(program);
|
|
658
|
+
this.curProgram = program;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
setUniform<T extends keyof UniformMethodMap>(
|
|
662
|
+
type: T,
|
|
663
|
+
location: WebGLUniformLocation,
|
|
664
|
+
...args: UniformMethodMap[T]
|
|
665
|
+
) {
|
|
666
|
+
const { gl, programUniforms } = this;
|
|
667
|
+
let uniforms = programUniforms.get(this.curProgram!);
|
|
668
|
+
if (!uniforms) {
|
|
669
|
+
uniforms = new Map();
|
|
670
|
+
programUniforms.set(this.curProgram!, uniforms);
|
|
671
|
+
}
|
|
672
|
+
const uniformArgs = uniforms.get(location);
|
|
673
|
+
if (!uniformArgs || !compareArrays(uniformArgs, args)) {
|
|
674
|
+
uniforms.set(location, args);
|
|
675
|
+
gl[type](location, ...(args as [never, never, never, never]));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* ```
|
|
681
|
+
* gl.getParameter(pname);
|
|
682
|
+
* ```
|
|
683
|
+
*
|
|
684
|
+
* @param pname
|
|
685
|
+
* @returns
|
|
686
|
+
*/
|
|
687
|
+
getParameter(pname: GLenum): any {
|
|
688
|
+
const { gl } = this;
|
|
689
|
+
return gl.getParameter(pname);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* ```
|
|
694
|
+
* gl.drawElements(mode, count, type, offset);
|
|
695
|
+
* ```
|
|
696
|
+
*
|
|
697
|
+
* @param mode
|
|
698
|
+
* @param count
|
|
699
|
+
* @param type
|
|
700
|
+
* @param offset
|
|
701
|
+
*/
|
|
702
|
+
drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
|
|
703
|
+
const { gl } = this;
|
|
704
|
+
gl.drawElements(mode, count, type, offset);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* ```
|
|
709
|
+
* gl.drawArrays(mode, first, count);
|
|
710
|
+
* ```
|
|
711
|
+
*
|
|
712
|
+
* @param name
|
|
713
|
+
* @returns
|
|
714
|
+
*/
|
|
715
|
+
getExtension(name: string) {
|
|
716
|
+
const { gl } = this;
|
|
717
|
+
return gl.getExtension(name);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* ```
|
|
722
|
+
* gl.createVertexArray();
|
|
723
|
+
* ```
|
|
724
|
+
*
|
|
725
|
+
* @returns
|
|
726
|
+
*/
|
|
727
|
+
createVertexArray() {
|
|
728
|
+
const { gl } = this;
|
|
729
|
+
assertTruthy(gl instanceof WebGL2RenderingContext);
|
|
730
|
+
return gl.createVertexArray();
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* ```
|
|
735
|
+
* gl.bindVertexArray(vertexArray);
|
|
736
|
+
* ```
|
|
737
|
+
*
|
|
738
|
+
* @param vertexArray
|
|
739
|
+
*/
|
|
740
|
+
bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
|
|
741
|
+
const { gl } = this;
|
|
742
|
+
assertTruthy(gl instanceof WebGL2RenderingContext);
|
|
743
|
+
gl.bindVertexArray(vertexArray);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* ```
|
|
748
|
+
* gl.getAttribLocation(program, name);
|
|
749
|
+
* ```
|
|
750
|
+
*
|
|
751
|
+
* @param program
|
|
752
|
+
* @param name
|
|
753
|
+
* @returns
|
|
754
|
+
*/
|
|
755
|
+
getAttribLocation(program: WebGLProgram, name: string) {
|
|
756
|
+
const { gl } = this;
|
|
757
|
+
return gl.getAttribLocation(program, name);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* ```
|
|
762
|
+
* gl.getUniformLocation(program, name);
|
|
763
|
+
* ```
|
|
764
|
+
*
|
|
765
|
+
* @param program
|
|
766
|
+
* @param name
|
|
767
|
+
* @returns
|
|
768
|
+
*/
|
|
769
|
+
getUniformLocation(program: WebGLProgram, name: string) {
|
|
770
|
+
const { gl } = this;
|
|
771
|
+
return gl.getUniformLocation(program, name);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* ```
|
|
776
|
+
* gl.enableVertexAttribArray(index);
|
|
777
|
+
* ```
|
|
778
|
+
*
|
|
779
|
+
* @param index
|
|
780
|
+
*/
|
|
781
|
+
enableVertexAttribArray(index: number) {
|
|
782
|
+
const { gl } = this;
|
|
783
|
+
gl.enableVertexAttribArray(index);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* ```
|
|
788
|
+
* gl.disableVertexAttribArray(index);
|
|
789
|
+
* ```
|
|
790
|
+
*
|
|
791
|
+
* @param index
|
|
792
|
+
*/
|
|
793
|
+
disableVertexAttribArray(index: number) {
|
|
794
|
+
const { gl } = this;
|
|
795
|
+
gl.disableVertexAttribArray(index);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* ```
|
|
800
|
+
* gl.createShader(type);
|
|
801
|
+
* ```
|
|
802
|
+
*
|
|
803
|
+
* @param type
|
|
804
|
+
* @returns
|
|
805
|
+
*/
|
|
806
|
+
createShader(type: number) {
|
|
807
|
+
const { gl } = this;
|
|
808
|
+
return gl.createShader(type);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* ```
|
|
813
|
+
* gl.compileShader(shader);
|
|
814
|
+
* ```
|
|
815
|
+
*
|
|
816
|
+
* @param shader
|
|
817
|
+
* @returns
|
|
818
|
+
*/
|
|
819
|
+
compileShader(shader: WebGLShader) {
|
|
820
|
+
const { gl } = this;
|
|
821
|
+
gl.compileShader(shader);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* ```
|
|
826
|
+
* gl.attachShader(program, shader);
|
|
827
|
+
* ```
|
|
828
|
+
*
|
|
829
|
+
* @param program
|
|
830
|
+
* @param shader
|
|
831
|
+
*/
|
|
832
|
+
attachShader(program: WebGLProgram, shader: WebGLShader) {
|
|
833
|
+
const { gl } = this;
|
|
834
|
+
gl.attachShader(program, shader);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* ```
|
|
839
|
+
* gl.linkProgram(program);
|
|
840
|
+
* ```
|
|
841
|
+
*
|
|
842
|
+
* @param program
|
|
843
|
+
*/
|
|
844
|
+
linkProgram(program: WebGLProgram) {
|
|
845
|
+
const { gl } = this;
|
|
846
|
+
gl.linkProgram(program);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* ```
|
|
851
|
+
* gl.deleteProgram(shader);
|
|
852
|
+
* ```
|
|
853
|
+
*
|
|
854
|
+
* @param shader
|
|
855
|
+
*/
|
|
856
|
+
deleteProgram(shader: WebGLProgram) {
|
|
857
|
+
const { gl } = this;
|
|
858
|
+
gl.deleteProgram(shader);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* ```
|
|
863
|
+
* gl.getShaderParameter(shader, pname);
|
|
864
|
+
* ```
|
|
865
|
+
*
|
|
866
|
+
* @param shader
|
|
867
|
+
* @param pname
|
|
868
|
+
*/
|
|
869
|
+
getShaderParameter(shader: WebGLShader, pname: GLenum) {
|
|
870
|
+
const { gl } = this;
|
|
871
|
+
return gl.getShaderParameter(shader, pname);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* ```
|
|
876
|
+
* gl.getShaderInfoLog(shader);
|
|
877
|
+
* ```
|
|
878
|
+
*
|
|
879
|
+
* @param shader
|
|
880
|
+
*/
|
|
881
|
+
getShaderInfoLog(shader: WebGLShader) {
|
|
882
|
+
const { gl } = this;
|
|
883
|
+
return gl.getShaderInfoLog(shader);
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* ```
|
|
888
|
+
* gl.createProgram();
|
|
889
|
+
* ```
|
|
890
|
+
*
|
|
891
|
+
* @returns
|
|
892
|
+
*/
|
|
893
|
+
createProgram() {
|
|
894
|
+
const { gl } = this;
|
|
895
|
+
return gl.createProgram();
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/**
|
|
899
|
+
* ```
|
|
900
|
+
* gl.getProgramParameter(program, pname);
|
|
901
|
+
* ```
|
|
902
|
+
*
|
|
903
|
+
* @param program
|
|
904
|
+
* @param pname
|
|
905
|
+
* @returns
|
|
906
|
+
*/
|
|
907
|
+
getProgramParameter(program: WebGLProgram, pname: GLenum) {
|
|
908
|
+
const { gl } = this;
|
|
909
|
+
return gl.getProgramParameter(program, pname);
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* ```
|
|
914
|
+
* gl.getProgramInfoLog(program);
|
|
915
|
+
* ```
|
|
916
|
+
*
|
|
917
|
+
* @param program
|
|
918
|
+
* @returns
|
|
919
|
+
*/
|
|
920
|
+
getProgramInfoLog(program: WebGLProgram) {
|
|
921
|
+
const { gl } = this;
|
|
922
|
+
return gl.getProgramInfoLog(program);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* ```
|
|
927
|
+
* gl.shaderSource(shader, source);
|
|
928
|
+
* ```
|
|
929
|
+
*
|
|
930
|
+
* @param shader
|
|
931
|
+
* @param source
|
|
932
|
+
*/
|
|
933
|
+
shaderSource(shader: WebGLShader, source: string) {
|
|
934
|
+
const { gl } = this;
|
|
935
|
+
gl.shaderSource(shader, source);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
/**
|
|
939
|
+
* ```
|
|
940
|
+
* gl.deleteShader(shader);
|
|
941
|
+
* ```
|
|
942
|
+
*
|
|
943
|
+
* @param shader
|
|
944
|
+
*/
|
|
945
|
+
deleteShader(shader: WebGLShader) {
|
|
946
|
+
const { gl } = this;
|
|
947
|
+
gl.deleteShader(shader);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
// prettier-ignore
|
|
952
|
+
type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
|
|
953
|
+
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
954
|
+
MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
|
|
955
|
+
? true
|
|
956
|
+
: false
|
|
957
|
+
: false;
|
|
958
|
+
|
|
959
|
+
// prettier-ignore
|
|
960
|
+
export type UniformMethodMap = {
|
|
961
|
+
[Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
|
|
962
|
+
location: WebGLUniformLocation | null,
|
|
963
|
+
...args: infer T
|
|
964
|
+
) => void
|
|
965
|
+
? T
|
|
966
|
+
: never;
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Compare two arrays for equality.
|
|
971
|
+
*
|
|
972
|
+
* @remarks
|
|
973
|
+
* This function will not try to compare nested arrays or Float32Arrays and
|
|
974
|
+
* instead will always return false when they are encountered.
|
|
975
|
+
*
|
|
976
|
+
* @param a
|
|
977
|
+
* @param b
|
|
978
|
+
* @returns
|
|
979
|
+
*/
|
|
980
|
+
export function compareArrays<T>(a: T[], b: T[]): boolean {
|
|
981
|
+
if (a.length !== b.length) {
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
return a.every((v, i) => {
|
|
985
|
+
// Don't bother to compare nested arrays or Float32Arrays
|
|
986
|
+
if (Array.isArray(v) || v instanceof Float32Array) {
|
|
987
|
+
return false;
|
|
988
|
+
} else {
|
|
989
|
+
return v === b[i];
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
}
|