@luma.gl/webgl 9.0.0-alpha.23 → 9.0.0-alpha.24
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/dist/adapter/converters/texture-formats.d.ts +2 -2
- package/dist/adapter/converters/texture-formats.d.ts.map +1 -1
- package/dist/adapter/device-helpers/device-limits.d.ts +1 -1
- package/dist/adapter/device-helpers/device-limits.d.ts.map +1 -1
- package/dist/adapter/helpers/get-shader-info.d.ts +1 -1
- package/dist/adapter/helpers/get-shader-info.d.ts.map +1 -1
- package/dist/adapter/objects/webgl-renderbuffer.d.ts +1 -1
- package/dist/adapter/objects/webgl-renderbuffer.d.ts.map +1 -1
- package/dist/adapter/objects/webgl-vertex-array-object.d.ts +1 -1
- package/dist/adapter/objects/webgl-vertex-array-object.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-command-buffer.d.ts +5 -5
- package/dist/adapter/resources/webgl-command-buffer.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-framebuffer.d.ts +2 -2
- package/dist/adapter/resources/webgl-framebuffer.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-render-pipeline.js +2 -1
- package/dist/adapter/resources/webgl-render-pipeline.js.map +1 -1
- package/dist/adapter/resources/webgl-texture.d.ts +5 -5
- package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
- package/dist/classic/buffer.d.ts +1 -1
- package/dist/classic/buffer.d.ts.map +1 -1
- package/dist/classic/clear.d.ts +22 -0
- package/dist/classic/clear.d.ts.map +1 -0
- package/dist/classic/clear.js +88 -0
- package/dist/classic/clear.js.map +1 -0
- package/dist/classic/copy-and-blit.d.ts +45 -0
- package/dist/classic/copy-and-blit.d.ts.map +1 -0
- package/dist/classic/copy-and-blit.js +136 -0
- package/dist/classic/copy-and-blit.js.map +1 -0
- package/dist/classic/format-utils.d.ts +3 -0
- package/dist/classic/format-utils.d.ts.map +1 -0
- package/dist/classic/format-utils.js +38 -0
- package/dist/classic/format-utils.js.map +1 -0
- package/dist/context/context/create-browser-context.d.ts +17 -19
- package/dist/context/context/create-browser-context.d.ts.map +1 -1
- package/dist/context/context/create-browser-context.js +4 -4
- package/dist/context/context/create-browser-context.js.map +1 -1
- package/dist/context/context/create-headless-context.d.ts +1 -1
- package/dist/context/context/create-headless-context.d.ts.map +1 -1
- package/dist/context/debug/spector.d.ts +1 -3
- package/dist/context/debug/spector.d.ts.map +1 -1
- package/dist/context/debug/webgl-developer-tools.d.ts +1 -1
- package/dist/context/debug/webgl-developer-tools.d.ts.map +1 -1
- package/dist/context/parameters/webgl-parameter-tables.d.ts +1 -1
- package/dist/context/parameters/webgl-parameter-tables.d.ts.map +1 -1
- package/dist/dist.dev.js +220 -5
- package/dist/index.cjs +191 -6
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist.min.js +22 -22
- package/package.json +5 -5
- package/src/adapter/resources/webgl-render-pipeline.ts +1 -1
- package/src/classic/clear.ts +110 -0
- package/src/classic/copy-and-blit.ts +189 -0
- package/src/classic/format-utils.ts +43 -0
- package/src/context/context/create-browser-context.ts +37 -30
- package/src/index.ts +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luma.gl/webgl",
|
|
3
|
-
"version": "9.0.0-alpha.
|
|
3
|
+
"version": "9.0.0-alpha.24",
|
|
4
4
|
"description": "WebGL2 adapter for the luma.gl API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@babel/runtime": "^7.0.0",
|
|
47
|
-
"@luma.gl/api": "9.0.0-alpha.
|
|
48
|
-
"@luma.gl/constants": "9.0.0-alpha.
|
|
47
|
+
"@luma.gl/api": "9.0.0-alpha.24",
|
|
48
|
+
"@luma.gl/constants": "9.0.0-alpha.24",
|
|
49
49
|
"@probe.gl/env": "^4.0.2"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@luma.gl/test-utils": "9.0.0-alpha.
|
|
52
|
+
"@luma.gl/test-utils": "9.0.0-alpha.24"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "3707968c288edbfef7ab4060195493ca8f00e7d5"
|
|
55
55
|
}
|
|
@@ -359,7 +359,7 @@ export class WEBGLRenderPipeline extends RenderPipeline {
|
|
|
359
359
|
_applyUniforms() {
|
|
360
360
|
for (const uniformLayout of this.layout.uniforms || []) {
|
|
361
361
|
const {name, location, type, textureUnit} = uniformLayout;
|
|
362
|
-
const value = this.uniforms[name]
|
|
362
|
+
const value = this.uniforms[name] ?? textureUnit;
|
|
363
363
|
if (value !== undefined) {
|
|
364
364
|
setUniform(this.device.gl, location, type, value);
|
|
365
365
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {Device, Framebuffer, assert} from '@luma.gl/api';
|
|
2
|
+
import {WebGLDevice} from '../adapter/webgl-device';
|
|
3
|
+
import {withParameters} from '../context/state-tracker/with-parameters';
|
|
4
|
+
|
|
5
|
+
// Should collapse during minification
|
|
6
|
+
const GL_DEPTH_BUFFER_BIT = 0x00000100;
|
|
7
|
+
const GL_STENCIL_BUFFER_BIT = 0x00000400;
|
|
8
|
+
const GL_COLOR_BUFFER_BIT = 0x00004000;
|
|
9
|
+
|
|
10
|
+
const GL_COLOR = 0x1800;
|
|
11
|
+
const GL_DEPTH = 0x1801;
|
|
12
|
+
const GL_STENCIL = 0x1802;
|
|
13
|
+
const GL_DEPTH_STENCIL = 0x84f9;
|
|
14
|
+
|
|
15
|
+
// Should disappear if asserts are removed
|
|
16
|
+
const ERR_ARGUMENTS = 'clear: bad arguments';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Optionally clears depth, color and stencil buffers
|
|
20
|
+
* @deprecated Set clear color when creating a RenderPass.
|
|
21
|
+
*/
|
|
22
|
+
export function clear(
|
|
23
|
+
gl: Device | WebGLRenderingContext,
|
|
24
|
+
options?: {framebuffer?: Framebuffer; color?: any; depth?: any; stencil?: any}
|
|
25
|
+
): void {
|
|
26
|
+
const device = WebGLDevice.attach(gl);
|
|
27
|
+
const {framebuffer = null, color = null, depth = null, stencil = null} = options || {};
|
|
28
|
+
const parameters: any = {};
|
|
29
|
+
|
|
30
|
+
if (framebuffer) {
|
|
31
|
+
parameters.framebuffer = framebuffer;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let clearFlags = 0;
|
|
35
|
+
|
|
36
|
+
if (color) {
|
|
37
|
+
clearFlags |= GL_COLOR_BUFFER_BIT;
|
|
38
|
+
if (color !== true) {
|
|
39
|
+
parameters.clearColor = color;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (depth) {
|
|
44
|
+
clearFlags |= GL_DEPTH_BUFFER_BIT;
|
|
45
|
+
if (depth !== true) {
|
|
46
|
+
parameters.clearDepth = depth;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (stencil) {
|
|
51
|
+
clearFlags |= GL_STENCIL_BUFFER_BIT;
|
|
52
|
+
if (depth !== true) {
|
|
53
|
+
parameters.clearStencil = depth;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
assert(clearFlags !== 0, ERR_ARGUMENTS);
|
|
58
|
+
|
|
59
|
+
// Temporarily set any clear "colors" and call clear
|
|
60
|
+
withParameters(device.gl, parameters, () => {
|
|
61
|
+
device.gl.clear(clearFlags);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* WebGL2 - clear a specific drawing buffer
|
|
67
|
+
* @deprecated Set clear color when creating a RenderPass
|
|
68
|
+
*/
|
|
69
|
+
export function clearBuffer(
|
|
70
|
+
gl: Device | WebGLRenderingContext,
|
|
71
|
+
options?: {framebuffer?: Framebuffer; buffer?: any; drawBuffer?: any; value?: any}
|
|
72
|
+
) {
|
|
73
|
+
const device = WebGLDevice.attach(gl);
|
|
74
|
+
|
|
75
|
+
const {framebuffer = null, buffer = GL_COLOR, drawBuffer = 0, value = [0, 0, 0, 0]} = options || {};
|
|
76
|
+
withParameters(device.gl2, {framebuffer}, () => {
|
|
77
|
+
// Method selection per OpenGL ES 3 docs
|
|
78
|
+
switch (buffer) {
|
|
79
|
+
case GL_COLOR:
|
|
80
|
+
switch (value.constructor) {
|
|
81
|
+
case Int32Array:
|
|
82
|
+
device.gl2.clearBufferiv(buffer, drawBuffer, value);
|
|
83
|
+
break;
|
|
84
|
+
case Uint32Array:
|
|
85
|
+
device.gl2.clearBufferuiv(buffer, drawBuffer, value);
|
|
86
|
+
break;
|
|
87
|
+
case Float32Array:
|
|
88
|
+
default:
|
|
89
|
+
device.gl2.clearBufferfv(buffer, drawBuffer, value);
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
|
|
93
|
+
case GL_DEPTH:
|
|
94
|
+
device.gl2.clearBufferfv(GL_DEPTH, 0, [value]);
|
|
95
|
+
break;
|
|
96
|
+
|
|
97
|
+
case GL_STENCIL:
|
|
98
|
+
device.gl2.clearBufferiv(GL_STENCIL, 0, [value]);
|
|
99
|
+
break;
|
|
100
|
+
|
|
101
|
+
case GL_DEPTH_STENCIL:
|
|
102
|
+
const [depth, stencil] = value;
|
|
103
|
+
device.gl2.clearBufferfi(GL_DEPTH_STENCIL, 0, depth, stencil);
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
default:
|
|
107
|
+
assert(false, ERR_ARGUMENTS);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// luma.gl, MIT license
|
|
2
|
+
import {assert, Texture, Framebuffer, FramebufferProps} from '@luma.gl/api';
|
|
3
|
+
import GL from '@luma.gl/constants';
|
|
4
|
+
|
|
5
|
+
import {ClassicBuffer as Buffer} from './buffer';
|
|
6
|
+
import {WEBGLTexture} from '../adapter/resources/webgl-texture';
|
|
7
|
+
import {WEBGLFramebuffer} from '../adapter/resources/webgl-framebuffer';
|
|
8
|
+
import {withParameters} from '../context/state-tracker/with-parameters';
|
|
9
|
+
import {getGLTypeFromTypedArray, getTypedArrayFromGLType} from './typed-array-utils';
|
|
10
|
+
import {glFormatToComponents, glTypeToBytes} from './format-utils';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Copies data from a type or a Texture object into ArrayBuffer object.
|
|
14
|
+
* App can provide targetPixelArray or have it auto allocated by this method
|
|
15
|
+
* newly allocated by this method unless provided by app.
|
|
16
|
+
* @deprecated Use CommandEncoder.copyTextureToBuffer and Buffer.read
|
|
17
|
+
* @note Slow requires roundtrip to GPU
|
|
18
|
+
*
|
|
19
|
+
* @param source
|
|
20
|
+
* @param options
|
|
21
|
+
* @returns pixel array,
|
|
22
|
+
*/
|
|
23
|
+
export function readPixelsToArray(
|
|
24
|
+
source: Framebuffer | Texture,
|
|
25
|
+
options?: {
|
|
26
|
+
sourceX?: number;
|
|
27
|
+
sourceY?: number;
|
|
28
|
+
sourceFormat?: number;
|
|
29
|
+
sourceAttachment?: number;
|
|
30
|
+
target?: Uint8Array | Uint16Array | Float32Array;
|
|
31
|
+
// following parameters are auto deduced if not provided
|
|
32
|
+
sourceWidth?: number;
|
|
33
|
+
sourceHeight?: number;
|
|
34
|
+
sourceType?: number;
|
|
35
|
+
}
|
|
36
|
+
): Uint8Array | Uint16Array | Float32Array {
|
|
37
|
+
const {sourceX = 0, sourceY = 0, sourceFormat = GL.RGBA} = options || {};
|
|
38
|
+
let {
|
|
39
|
+
sourceAttachment = GL.COLOR_ATTACHMENT0, // TODO - support gl.readBuffer
|
|
40
|
+
target = null,
|
|
41
|
+
// following parameters are auto deduced if not provided
|
|
42
|
+
sourceWidth,
|
|
43
|
+
sourceHeight,
|
|
44
|
+
sourceType
|
|
45
|
+
} = options || {};
|
|
46
|
+
|
|
47
|
+
const {framebuffer, deleteFramebuffer} = getFramebuffer(source);
|
|
48
|
+
assert(framebuffer);
|
|
49
|
+
const {gl, handle} = framebuffer as WEBGLFramebuffer;
|
|
50
|
+
sourceWidth = sourceWidth || framebuffer.width;
|
|
51
|
+
sourceHeight = sourceHeight || framebuffer.height;
|
|
52
|
+
|
|
53
|
+
// TODO - Set and unset gl.readBuffer
|
|
54
|
+
if (sourceAttachment === GL.COLOR_ATTACHMENT0 && handle === null) {
|
|
55
|
+
sourceAttachment = GL.FRONT;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const attachment = sourceAttachment - GL.COLOR_ATTACHMENT0;
|
|
59
|
+
// assert(attachments[sourceAttachment]);
|
|
60
|
+
|
|
61
|
+
// Deduce the type from color attachment if not provided.
|
|
62
|
+
sourceType = sourceType || (framebuffer.colorAttachments[attachment] as WEBGLTexture).type;
|
|
63
|
+
|
|
64
|
+
// Deduce type and allocated pixelArray if needed
|
|
65
|
+
target = getPixelArray(target, sourceType, sourceFormat, sourceWidth, sourceHeight);
|
|
66
|
+
|
|
67
|
+
// Pixel array available, if necessary, deduce type from it.
|
|
68
|
+
sourceType = sourceType || getGLTypeFromTypedArray(target);
|
|
69
|
+
|
|
70
|
+
const prevHandle = gl.bindFramebuffer(GL.FRAMEBUFFER, handle);
|
|
71
|
+
gl.readPixels(sourceX, sourceY, sourceWidth, sourceHeight, sourceFormat, sourceType, target);
|
|
72
|
+
// @ts-expect-error
|
|
73
|
+
gl.bindFramebuffer(GL.FRAMEBUFFER, prevHandle || null);
|
|
74
|
+
if (deleteFramebuffer) {
|
|
75
|
+
framebuffer.destroy();
|
|
76
|
+
}
|
|
77
|
+
return target;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Copies data from a Framebuffer or a Texture object into a Buffer object.
|
|
82
|
+
* NOTE: doesn't wait for copy to be complete, it programs GPU to perform a DMA transffer.
|
|
83
|
+
* @param source
|
|
84
|
+
* @param options
|
|
85
|
+
*/
|
|
86
|
+
export function readPixelsToBuffer(
|
|
87
|
+
source: Framebuffer | Texture,
|
|
88
|
+
options?: {
|
|
89
|
+
sourceX?: number;
|
|
90
|
+
sourceY?: number;
|
|
91
|
+
sourceFormat?: number;
|
|
92
|
+
target?: Buffer; // A new Buffer object is created when not provided.
|
|
93
|
+
targetByteOffset?: number; // byte offset in buffer object
|
|
94
|
+
// following parameters are auto deduced if not provided
|
|
95
|
+
sourceWidth?: number;
|
|
96
|
+
sourceHeight?: number;
|
|
97
|
+
sourceType?: number;
|
|
98
|
+
}
|
|
99
|
+
): Buffer {
|
|
100
|
+
const {sourceX = 0, sourceY = 0, sourceFormat = GL.RGBA, targetByteOffset = 0} = options || {};
|
|
101
|
+
// following parameters are auto deduced if not provided
|
|
102
|
+
let {target, sourceWidth, sourceHeight, sourceType} = options || {};
|
|
103
|
+
const {framebuffer, deleteFramebuffer} = getFramebuffer(source);
|
|
104
|
+
assert(framebuffer);
|
|
105
|
+
sourceWidth = sourceWidth || framebuffer.width;
|
|
106
|
+
sourceHeight = sourceHeight || framebuffer.height;
|
|
107
|
+
|
|
108
|
+
// Asynchronous read (PIXEL_PACK_BUFFER) is WebGL2 only feature
|
|
109
|
+
const webglFramebuffer = framebuffer as WEBGLFramebuffer;
|
|
110
|
+
const gl2 = webglFramebuffer.device.assertWebGL2();
|
|
111
|
+
|
|
112
|
+
// deduce type if not available.
|
|
113
|
+
sourceType = sourceType || (target ? target.type : GL.UNSIGNED_BYTE);
|
|
114
|
+
|
|
115
|
+
if (!target) {
|
|
116
|
+
// Create new buffer with enough size
|
|
117
|
+
const components = glFormatToComponents(sourceFormat);
|
|
118
|
+
const byteCount = glTypeToBytes(sourceType);
|
|
119
|
+
const byteLength = targetByteOffset + sourceWidth * sourceHeight * components * byteCount;
|
|
120
|
+
target = new Buffer(gl2, {byteLength, accessor: {type: sourceType, size: components}});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// @ts-expect-error
|
|
124
|
+
target.bind({target: GL.PIXEL_PACK_BUFFER});
|
|
125
|
+
withParameters(gl2, {framebuffer}, () => {
|
|
126
|
+
gl2.readPixels(
|
|
127
|
+
sourceX,
|
|
128
|
+
sourceY,
|
|
129
|
+
sourceWidth,
|
|
130
|
+
sourceHeight,
|
|
131
|
+
sourceFormat,
|
|
132
|
+
sourceType,
|
|
133
|
+
targetByteOffset
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
target.unbind({target: GL.PIXEL_PACK_BUFFER});
|
|
137
|
+
if (deleteFramebuffer) {
|
|
138
|
+
framebuffer.destroy();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return target;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function getFramebuffer(source: Texture | Framebuffer): {
|
|
145
|
+
framebuffer: Framebuffer;
|
|
146
|
+
deleteFramebuffer: boolean;
|
|
147
|
+
} {
|
|
148
|
+
if (!(source instanceof Framebuffer)) {
|
|
149
|
+
return {framebuffer: toFramebuffer(source), deleteFramebuffer: true};
|
|
150
|
+
}
|
|
151
|
+
return {framebuffer: source, deleteFramebuffer: false};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Wraps a given texture into a framebuffer object, that can be further used
|
|
156
|
+
* to read data from the texture object.
|
|
157
|
+
*/
|
|
158
|
+
export function toFramebuffer(texture: Texture, props?: FramebufferProps): Framebuffer {
|
|
159
|
+
const {device, width, height, id} = texture;
|
|
160
|
+
const framebuffer = device.createFramebuffer({
|
|
161
|
+
...props,
|
|
162
|
+
id: `framebuffer-for-${id}`,
|
|
163
|
+
width,
|
|
164
|
+
height,
|
|
165
|
+
colorAttachments: [
|
|
166
|
+
texture
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
return framebuffer;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function getPixelArray(
|
|
174
|
+
pixelArray,
|
|
175
|
+
type,
|
|
176
|
+
format,
|
|
177
|
+
width: number,
|
|
178
|
+
height: number
|
|
179
|
+
): Uint8Array | Uint16Array | Float32Array {
|
|
180
|
+
if (pixelArray) {
|
|
181
|
+
return pixelArray;
|
|
182
|
+
}
|
|
183
|
+
// Allocate pixel array if not already available, using supplied type
|
|
184
|
+
type = type || GL.UNSIGNED_BYTE;
|
|
185
|
+
const ArrayType = getTypedArrayFromGLType(type, {clamped: false});
|
|
186
|
+
const components = glFormatToComponents(format);
|
|
187
|
+
// TODO - check for composite type (components = 1).
|
|
188
|
+
return new ArrayType(width * height * components) as Uint8Array | Uint16Array | Float32Array;
|
|
189
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {assert} from '@luma.gl/api';
|
|
2
|
+
import GL from '@luma.gl/constants';
|
|
3
|
+
|
|
4
|
+
// Returns number of components in a specific readPixels WebGL format
|
|
5
|
+
export function glFormatToComponents(format) {
|
|
6
|
+
switch (format) {
|
|
7
|
+
case GL.ALPHA:
|
|
8
|
+
case GL.R32F:
|
|
9
|
+
case GL.RED:
|
|
10
|
+
return 1;
|
|
11
|
+
case GL.RG32F:
|
|
12
|
+
case GL.RG:
|
|
13
|
+
return 2;
|
|
14
|
+
case GL.RGB:
|
|
15
|
+
case GL.RGB32F:
|
|
16
|
+
return 3;
|
|
17
|
+
case GL.RGBA:
|
|
18
|
+
case GL.RGBA32F:
|
|
19
|
+
return 4;
|
|
20
|
+
// TODO: Add support for additional WebGL2 formats
|
|
21
|
+
default:
|
|
22
|
+
assert(false);
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Return byte count for given readPixels WebGL type
|
|
28
|
+
export function glTypeToBytes(type) {
|
|
29
|
+
switch (type) {
|
|
30
|
+
case GL.UNSIGNED_BYTE:
|
|
31
|
+
return 1;
|
|
32
|
+
case GL.UNSIGNED_SHORT_5_6_5:
|
|
33
|
+
case GL.UNSIGNED_SHORT_4_4_4_4:
|
|
34
|
+
case GL.UNSIGNED_SHORT_5_5_5_1:
|
|
35
|
+
return 2;
|
|
36
|
+
case GL.FLOAT:
|
|
37
|
+
return 4;
|
|
38
|
+
// TODO: Add support for additional WebGL2 types
|
|
39
|
+
default:
|
|
40
|
+
assert(false);
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* ContextProps
|
|
5
|
-
* @param webgl2 Set to false to not create a WebGL2 context (force webgl1)
|
|
6
|
-
* @param webgl1 set to false to not create a WebGL1 context (fail if webgl2 not available)
|
|
7
|
-
* @param onContextLost
|
|
8
|
-
* @param onContextRestored
|
|
9
|
-
*
|
|
10
|
-
* BROWSER CONTEXT PARAMETERS
|
|
11
|
-
* @param debug Instrument context (at the expense of performance).
|
|
12
|
-
* @param alpha Default render target has an alpha buffer.
|
|
13
|
-
* @param depth Default render target has a depth buffer of at least 16 bits.
|
|
14
|
-
* @param stencil Default render target has a stencil buffer of at least 8 bits.
|
|
15
|
-
* @param antialias Boolean that indicates whether or not to perform anti-aliasing.
|
|
16
|
-
* @param premultipliedAlpha Boolean that indicates that the page compositor will assume the drawing buffer contains colors with pre-multiplied alpha.
|
|
17
|
-
* @param preserveDrawingBuffer Default render target buffers will not be automatically cleared and will preserve their values until cleared or overwritten
|
|
18
|
-
* @param failIfMajorPerformanceCaveat Do not create if the system performance is low.
|
|
19
|
-
*/
|
|
5
|
+
* @param webgl2 Set to false to not create a WebGL2 context (force webgl1)
|
|
6
|
+
* @param webgl1 set to false to not create a WebGL1 context (fail if webgl2 not available)
|
|
7
|
+
* @param onContextLost
|
|
8
|
+
* @param onContextRestored
|
|
9
|
+
*
|
|
10
|
+
* BROWSER CONTEXT PARAMETERS
|
|
11
|
+
* @param debug Instrument context (at the expense of performance).
|
|
12
|
+
* @param alpha Default render target has an alpha buffer.
|
|
13
|
+
* @param depth Default render target has a depth buffer of at least 16 bits.
|
|
14
|
+
* @param stencil Default render target has a stencil buffer of at least 8 bits.
|
|
15
|
+
* @param antialias Boolean that indicates whether or not to perform anti-aliasing.
|
|
16
|
+
* @param premultipliedAlpha Boolean that indicates that the page compositor will assume the drawing buffer contains colors with pre-multiplied alpha.
|
|
17
|
+
* @param preserveDrawingBuffer Default render target buffers will not be automatically cleared and will preserve their values until cleared or overwritten
|
|
18
|
+
* @param failIfMajorPerformanceCaveat Do not create if the system performance is low.
|
|
19
|
+
*/
|
|
20
20
|
type ContextProps = {
|
|
21
21
|
type?: 'webgl' | 'webgl1' | 'webgl2' | string;
|
|
22
22
|
webgl1?: boolean;
|
|
@@ -27,10 +27,10 @@ type ContextProps = {
|
|
|
27
27
|
desynchronized?: boolean; // hints the user agent to reduce the latency by desynchronizing the canvas paint cycle from the event loop
|
|
28
28
|
antialias?: boolean; // indicates whether or not to perform anti-aliasing.
|
|
29
29
|
depth?: boolean; // indicates that the drawing buffer has a depth buffer of at least 16 bits.
|
|
30
|
-
failIfMajorPerformanceCaveat?: boolean
|
|
31
|
-
powerPreference?: 'default' |
|
|
32
|
-
premultipliedAlpha?: boolean
|
|
33
|
-
preserveDrawingBuffer?: boolean // buffers will not be cleared and will preserve their values until cleared or overwritten by the author.
|
|
30
|
+
failIfMajorPerformanceCaveat?: boolean; // indicates if a context will be created if the system performance is low or if no hardware GPU is available.
|
|
31
|
+
powerPreference?: 'default' | 'high-performance' | 'low-power';
|
|
32
|
+
premultipliedAlpha?: boolean; // page compositor will assume the drawing buffer contains colors with pre-multiplied alpha.
|
|
33
|
+
preserveDrawingBuffer?: boolean; // buffers will not be cleared and will preserve their values until cleared or overwritten by the author.
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
const DEFAULT_CONTEXT_PROPS: ContextProps = {
|
|
@@ -40,24 +40,27 @@ const DEFAULT_CONTEXT_PROPS: ContextProps = {
|
|
|
40
40
|
// eslint-disable-next-line no-console
|
|
41
41
|
onContextLost: () => console.error('WebGL context lost'),
|
|
42
42
|
// eslint-disable-next-line no-console
|
|
43
|
-
onContextRestored: () => console.info('WebGL context restored')
|
|
43
|
+
onContextRestored: () => console.info('WebGL context restored')
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* Create a WebGL context for a canvas
|
|
48
48
|
* Note calling this multiple time on the same canvas does return the same context
|
|
49
|
-
* @param canvas A canvas element or offscreen canvas
|
|
49
|
+
* @param canvas A canvas element or offscreen canvas
|
|
50
50
|
*/
|
|
51
|
-
export function createBrowserContext(
|
|
51
|
+
export function createBrowserContext(
|
|
52
|
+
canvas: HTMLCanvasElement | OffscreenCanvas,
|
|
53
|
+
props: ContextProps
|
|
54
|
+
): WebGLRenderingContext {
|
|
52
55
|
props = {...DEFAULT_CONTEXT_PROPS, ...props};
|
|
53
56
|
|
|
54
57
|
// Try to extract any extra information about why context creation failed
|
|
55
58
|
let errorMessage = null;
|
|
56
|
-
const onCreateError =
|
|
59
|
+
const onCreateError = error => (errorMessage = error.statusMessage || errorMessage);
|
|
57
60
|
canvas.addEventListener('webglcontextcreationerror', onCreateError, false);
|
|
58
61
|
|
|
59
62
|
// Create the desired context
|
|
60
|
-
let gl: WebGLRenderingContext | null = null;
|
|
63
|
+
let gl: WebGLRenderingContext | WebGL2RenderingContext | null = null;
|
|
61
64
|
|
|
62
65
|
if (props.type === 'webgl2') {
|
|
63
66
|
props = {...props, webgl1: false};
|
|
@@ -67,11 +70,11 @@ export function createBrowserContext(canvas: HTMLCanvasElement | OffscreenCanvas
|
|
|
67
70
|
}
|
|
68
71
|
|
|
69
72
|
// Prefer webgl2 over webgl1 if both are acceptable
|
|
70
|
-
if (props.webgl2) {
|
|
71
|
-
gl =
|
|
73
|
+
if (!gl && props.webgl2) {
|
|
74
|
+
gl = canvas.getContext('webgl2', props) as WebGL2RenderingContext;
|
|
72
75
|
}
|
|
73
|
-
if (props.webgl1) {
|
|
74
|
-
gl =
|
|
76
|
+
if (!gl && props.webgl1) {
|
|
77
|
+
gl = canvas.getContext('webgl', props) as WebGLRenderingContext;
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
// TODO are we removing this listener before giving it a chance to fire?
|
|
@@ -88,12 +91,16 @@ export function createBrowserContext(canvas: HTMLCanvasElement | OffscreenCanvas
|
|
|
88
91
|
if (props.onContextLost) {
|
|
89
92
|
// Carefully extract and wrap callbacks to prevent addEventListener from rebinding them.
|
|
90
93
|
const {onContextLost} = props;
|
|
91
|
-
canvas.addEventListener('webglcontextlost',(event: Event) => onContextLost(event), false);
|
|
94
|
+
canvas.addEventListener('webglcontextlost', (event: Event) => onContextLost(event), false);
|
|
92
95
|
}
|
|
93
96
|
if (props.onContextRestored) {
|
|
94
97
|
// Carefully extract and wrap callbacks to prevent addEventListener from rebinding them.
|
|
95
98
|
const {onContextRestored} = props;
|
|
96
|
-
canvas.addEventListener(
|
|
99
|
+
canvas.addEventListener(
|
|
100
|
+
'webglcontextrestored',
|
|
101
|
+
(event: Event) => onContextRestored(event),
|
|
102
|
+
false
|
|
103
|
+
);
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
return gl;
|
package/src/index.ts
CHANGED
|
@@ -76,4 +76,9 @@ export {polyfillContext} from './context/polyfill/polyfill-context';
|
|
|
76
76
|
export {getShaderLayout, getProgramBindings} from './adapter/helpers/get-shader-layout';
|
|
77
77
|
export {convertGLToTextureFormat, _checkFloat32ColorAttachment} from './adapter/converters/texture-formats';
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
// TEST EXPORTS
|
|
80
|
+
export {TEXTURE_FORMATS as _TEXTURE_FORMATS} from './adapter/converters/texture-formats';
|
|
81
|
+
|
|
82
|
+
// DEPRECATED EXPORTS
|
|
83
|
+
export {clear} from './classic/clear';
|
|
84
|
+
export {readPixelsToBuffer, readPixelsToArray} from './classic/copy-and-blit';
|