@luma.gl/webgl 8.6.0-alpha.3 → 8.6.0-alpha.4

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.
@@ -0,0 +1,140 @@
1
+ // luma.gl, MIT license
2
+ // LEGACY luma.gl v8 API for WebGLRendering context
3
+ // DEPRECATED API - may be removed in luma.gl v9 or v10.
4
+
5
+ /* eslint-disable quotes */
6
+ import GL from '@luma.gl/constants';
7
+ import {WebGLDevice, WebGLDeviceProps} from '@luma.gl/webgl';
8
+ import {FEATURES} from './features';
9
+
10
+ export type GLContextOptions = WebGLDeviceProps & {
11
+ throwOnError?: boolean; // If set to false, return `null` if context creation fails.
12
+ };
13
+
14
+ /** @deprecated Use `new WebGLDevice()` or `luma.createDevice()` */
15
+ export function createGLContext(options?: GLContextOptions): WebGLRenderingContext | null {
16
+ const webglDevice = new WebGLDevice(options);
17
+ // Note: OK to return the context, it holds on to the device
18
+ return webglDevice.gl;
19
+ }
20
+
21
+ /** @deprecated Use `WebGLDevice.attach()` */
22
+ export function instrumentGLContext(
23
+ gl: WebGLRenderingContext | WebGL2RenderingContext,
24
+ options?: GLContextOptions
25
+ ): WebGLRenderingContext {
26
+ const webglDevice = WebGLDevice.attach(gl, options);
27
+ return webglDevice.gl;
28
+ }
29
+
30
+ /**
31
+ * Resize the canvas' drawing buffer.
32
+ *
33
+ * Can match the canvas CSS size, and optionally also consider devicePixelRatio
34
+ * Can be called every frame
35
+ *
36
+ * Regardless of size, the drawing buffer will always be scaled to the viewport, but
37
+ * for best visual results, usually set to either:
38
+ * canvas CSS width x canvas CSS height
39
+ * canvas CSS width * devicePixelRatio x canvas CSS height * devicePixelRatio
40
+ * See http://webgl2fundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
41
+ *
42
+ * resizeGLContext(gl, {width, height, useDevicePixels})
43
+ * @deprecated Use WebGLDevice.resize()
44
+ */
45
+ export function resizeGLContext(
46
+ gl: WebGLRenderingContext,
47
+ options?: {
48
+ width?: number;
49
+ height?: number;
50
+ useDevicePixels?: boolean | number;
51
+ }
52
+ ) {
53
+ const webglDevice = WebGLDevice.attach(gl);
54
+ webglDevice.resize(options);
55
+ }
56
+
57
+ /**
58
+ * Check one or more features
59
+ * @deprecated Use `WebGLDevice.features.has()`
60
+ */
61
+ export function hasFeatures(gl: WebGLRenderingContext, features: string | string[]): boolean {
62
+ const webglDevice = WebGLDevice.attach(gl);
63
+ const normalizedFeatures = Array.isArray(features) ? features : [features];
64
+ const deviceFeatures = normalizedFeatures.map(feature => getDeviceFeature(feature));
65
+ return deviceFeatures.every((feature) => webglDevice.webglFeatures.has(feature));
66
+ }
67
+
68
+ function getDeviceFeature(feature) {
69
+ return feature.toLowerCase().replace('webgl-', '').replace('-', '_');
70
+ }
71
+
72
+ /**
73
+ * Check one feature
74
+ * @deprecated Use `WebGLDevice.features`
75
+ */
76
+ export function hasFeature(gl: WebGLRenderingContext, feature: string): boolean {
77
+ return hasFeatures(gl, feature);
78
+ }
79
+
80
+ /**
81
+ * Return a map of supported features
82
+ * @deprecated Use `WebGLDevice.features`
83
+ */
84
+ export function getFeatures(gl: WebGLRenderingContext): Record<string, boolean> {
85
+ const webglDevice = WebGLDevice.attach(gl);
86
+ const featureMap: Record<string, boolean> = {};
87
+ for (const feature in FEATURES) {
88
+ featureMap[feature] = webglDevice.webglFeatures.has(feature);
89
+ }
90
+ return featureMap;
91
+ }
92
+
93
+ /**
94
+ * Provides strings identifying the GPU vendor and driver.
95
+ * https://www.khronos.org/registry/webgl/extensions/WEBGL_debug_renderer_info/
96
+ *
97
+ * @deprecated Use `WebGLDevice.info`
98
+ */
99
+ export function getContextDebugInfo(gl: WebGLRenderingContext): {
100
+ vendor: string;
101
+ renderer: string;
102
+ vendorMasked: string;
103
+ rendererMasked: string;
104
+ version: string;
105
+ } {
106
+ const webglDevice = WebGLDevice.attach(gl);
107
+ const info = webglDevice.info;
108
+ return {
109
+ ...webglDevice.info,
110
+ vendorMasked: webglDevice.info.vendor,
111
+ rendererMasked: webglDevice.info.renderer
112
+ };
113
+ }
114
+
115
+ /** @deprecated Use `WebGLDevice.info` */
116
+ export function getGLContextInfo(gl) {
117
+ const info = getContextDebugInfo(gl);
118
+ return {
119
+ [GL.UNMASKED_VENDOR_WEBGL]: info.vendor,
120
+ [GL.UNMASKED_RENDERER_WEBGL]: info.renderer,
121
+ [GL.VENDOR]: info.vendorMasked,
122
+ [GL.RENDERER]: info.rendererMasked,
123
+ [GL.VERSION]: info.version,
124
+ };
125
+ }
126
+
127
+ /** @deprecated Use `WebGLDevice.info` and `WebGLDevice.limits` */
128
+ export function getContextInfo(gl) {
129
+ return {
130
+ ...getContextDebugInfo(gl),
131
+ ...getContextLimits(gl),
132
+ info: getGLContextInfo(gl),
133
+ };
134
+ }
135
+
136
+ /** @deprecated Use `WebGLDevice.limits` */
137
+ export function getContextLimits(gl) {
138
+ const webglDevice = WebGLDevice.attach(gl);
139
+ return {limits: webglDevice.webglLimits};
140
+ }
@@ -0,0 +1,159 @@
1
+ // Feature detection for WebGL
2
+ // Provides a function that enables simple checking of which WebGL features are
3
+ // available in an WebGL1 or WebGL2 environment.
4
+
5
+ import GL from '@luma.gl/constants';
6
+ import {isWebGL2} from '@luma.gl/webgl';
7
+ import {assert} from '@luma.gl/api';
8
+
9
+ // TODO - this should be the default export, test cases need updating
10
+ export const FEATURES = {
11
+ WEBGL2: 'WEBGL2',
12
+
13
+ // API SUPPORT
14
+ VERTEX_ARRAY_OBJECT: 'VERTEX_ARRAY_OBJECT',
15
+ TIMER_QUERY: 'TIMER_QUERY',
16
+ INSTANCED_RENDERING: 'INSTANCED_RENDERING',
17
+ MULTIPLE_RENDER_TARGETS: 'MULTIPLE_RENDER_TARGETS',
18
+
19
+ // FEATURES
20
+ ELEMENT_INDEX_UINT32: 'ELEMENT_INDEX_UINT32',
21
+
22
+ // BLENDING
23
+ BLEND_EQUATION_MINMAX: 'BLEND_EQUATION_MINMAX',
24
+ FLOAT_BLEND: 'FLOAT_BLEND',
25
+
26
+ // TEXTURES: '// TEXTURES', RENDERBUFFERS
27
+ COLOR_ENCODING_SRGB: 'COLOR_ENCODING_SRGB',
28
+
29
+ // TEXTURES
30
+ TEXTURE_DEPTH: 'TEXTURE_DEPTH',
31
+ TEXTURE_FLOAT: 'TEXTURE_FLOAT',
32
+ TEXTURE_HALF_FLOAT: 'TEXTURE_HALF_FLOAT',
33
+
34
+ TEXTURE_FILTER_LINEAR_FLOAT: 'TEXTURE_FILTER_LINEAR_FLOAT',
35
+ TEXTURE_FILTER_LINEAR_HALF_FLOAT: 'TEXTURE_FILTER_LINEAR_HALF_FLOAT',
36
+ TEXTURE_FILTER_ANISOTROPIC: 'TEXTURE_FILTER_ANISOTROPIC',
37
+
38
+ // FRAMEBUFFERS: '// FRAMEBUFFERS', TEXTURES AND RENDERBUFFERS
39
+ COLOR_ATTACHMENT_RGBA32F: 'COLOR_ATTACHMENT_RGBA32F',
40
+ COLOR_ATTACHMENT_FLOAT: 'COLOR_ATTACHMENT_FLOAT',
41
+ COLOR_ATTACHMENT_HALF_FLOAT: 'COLOR_ATTACHMENT_HALF_FLOAT',
42
+
43
+ // GLSL extensions
44
+ GLSL_FRAG_DATA: 'GLSL_FRAG_DATA',
45
+ GLSL_FRAG_DEPTH: 'GLSL_FRAG_DEPTH',
46
+ GLSL_DERIVATIVES: 'GLSL_DERIVATIVES',
47
+ GLSL_TEXTURE_LOD: 'GLSL_TEXTURE_LOD'
48
+ };
49
+
50
+ /** Extract all WebGL features */
51
+ export function getWebGLFeatures(gl: WebGLRenderingContext): Set<string> {
52
+ // Enable EXT_float_blend first: https://developer.mozilla.org/en-US/docs/Web/API/EXT_float_blend
53
+ gl.getExtension('EXT_color_buffer_float');
54
+ gl.getExtension('WEBGL_color_buffer_float');
55
+ gl.getExtension('EXT_float_blend');
56
+
57
+ const features = new Set<string>();
58
+ for (const feature in WEBGL_FEATURES) {
59
+ if (isFeatureSupported(gl, feature)) {
60
+ features.add(feature);
61
+ }
62
+ }
63
+ return features;
64
+ }
65
+
66
+ function isFeatureSupported(gl: WebGLRenderingContext, cap: string): boolean {
67
+ const feature = WEBGL_FEATURES[cap];
68
+ assert(feature, cap);
69
+
70
+ const [webgl1Feature, webgl2Feature] = feature;
71
+
72
+ // Get extension name from table
73
+ const featureDefinition = isWebGL2(gl) ? webgl2Feature : webgl1Feature;
74
+
75
+ if (cap === FEATURES.COLOR_ATTACHMENT_RGBA32F && !isWebGL2(gl)) {
76
+ return checkFloat32ColorAttachment(gl);
77
+ }
78
+
79
+ // Check if the value is dependent on checking one or more extensions
80
+ if (typeof featureDefinition === 'string') {
81
+ return Boolean(gl.getExtension(featureDefinition));
82
+ }
83
+
84
+ return featureDefinition;
85
+ }
86
+
87
+
88
+ // function to test if Float 32 bit format texture can be bound as color attachment
89
+ function checkFloat32ColorAttachment(gl: WebGLRenderingContext) {
90
+ let texture: WebGLTexture;
91
+ let framebuffer: WebGLFramebuffer;
92
+ try {
93
+ const texture = gl.createTexture();
94
+ gl.bindTexture(GL.TEXTURE_2D, texture);
95
+
96
+ const level = 0;
97
+ const internalFormat = gl.RGBA;
98
+ const width = 1;
99
+ const height = 1;
100
+ const border = 0;
101
+ const srcFormat = gl.RGBA;
102
+ const srcType = gl.UNSIGNED_BYTE;
103
+ const pixel = new Uint8Array([0, 0, 255, 255]); // opaque blue
104
+ gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
105
+ width, height, border, srcFormat, srcType,
106
+ pixel);
107
+
108
+ const framebuffer = gl.createFramebuffer();
109
+ gl.bindFramebuffer(GL.FRAMEBUFFER, framebuffer);
110
+ gl.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, GL.TEXTURE_2D, texture, 0);
111
+ const status = gl.checkFramebufferStatus(GL.FRAMEBUFFER) === GL.FRAMEBUFFER_COMPLETE;
112
+ return status;
113
+ } finally {
114
+ gl.deleteTexture(texture);
115
+ gl.deleteFramebuffer(framebuffer);
116
+ }
117
+ }
118
+
119
+ // Defines luma.gl "feature" names and semantics
120
+ // Format: 'feature-name: [WebGL1 support, WebGL2 support] / [WebGL1 and WebGL2 support]', when support is 'string' it is the name of the extension
121
+ const WEBGL_FEATURES: Record<string, [boolean | string, boolean | string]> = {
122
+ [FEATURES.WEBGL2]: [false, true],
123
+
124
+ // API SUPPORT
125
+ [FEATURES.VERTEX_ARRAY_OBJECT]: ['OES_vertex_array_object', true],
126
+ [FEATURES.TIMER_QUERY]: ['EXT_disjoint_timer_query', 'EXT_disjoint_timer_query_webgl2'],
127
+ [FEATURES.INSTANCED_RENDERING]: ['ANGLE_instanced_arrays', true],
128
+ [FEATURES.MULTIPLE_RENDER_TARGETS]: ['WEBGL_draw_buffers', true],
129
+
130
+ // FEATURES
131
+ [FEATURES.ELEMENT_INDEX_UINT32]: ['OES_element_index_uint', true],
132
+
133
+ // BLENDING
134
+ [FEATURES.BLEND_EQUATION_MINMAX]: ['EXT_blend_minmax', true],
135
+ [FEATURES.FLOAT_BLEND]: ['EXT_float_blend', 'EXT_float_blend'],
136
+
137
+ // TEXTURES, RENDERBUFFERS
138
+ [FEATURES.COLOR_ENCODING_SRGB]: ['EXT_sRGB', true],
139
+
140
+ // TEXTURES
141
+ [FEATURES.TEXTURE_DEPTH]: ['WEBGL_depth_texture', true],
142
+ [FEATURES.TEXTURE_FLOAT]: ['OES_texture_float', true],
143
+ [FEATURES.TEXTURE_HALF_FLOAT]: ['OES_texture_half_float', true],
144
+
145
+ [FEATURES.TEXTURE_FILTER_LINEAR_FLOAT]: ['OES_texture_float_linear', 'OES_texture_float_linear'],
146
+ [FEATURES.TEXTURE_FILTER_LINEAR_HALF_FLOAT]: ['OES_texture_half_float_linear', 'OES_texture_half_float_linear'],
147
+ [FEATURES.TEXTURE_FILTER_ANISOTROPIC]: ['EXT_texture_filter_anisotropic', 'EXT_texture_filter_anisotropic'],
148
+
149
+ // FRAMEBUFFERS, TEXTURES AND RENDERBUFFERS
150
+ [FEATURES.COLOR_ATTACHMENT_RGBA32F]: [false, 'EXT_color_buffer_float'], // Note override check
151
+ [FEATURES.COLOR_ATTACHMENT_FLOAT]: [false, 'EXT_color_buffer_float'],
152
+ [FEATURES.COLOR_ATTACHMENT_HALF_FLOAT]: ['EXT_color_buffer_half_float', 'EXT_color_buffer_half_float'],
153
+
154
+ // GLSL extensions
155
+ [FEATURES.GLSL_FRAG_DATA]: ['WEBGL_draw_buffers', true],
156
+ [FEATURES.GLSL_FRAG_DEPTH]: ['EXT_frag_depth', true],
157
+ [FEATURES.GLSL_DERIVATIVES]: ['OES_standard_derivatives', true],
158
+ [FEATURES.GLSL_TEXTURE_LOD]: ['EXT_shader_texture_lod', true]
159
+ };
package/src/index.ts CHANGED
@@ -110,6 +110,9 @@ export {getDebugTableForUniforms} from './debug/debug-uniforms';
110
110
  export {getDebugTableForVertexArray} from './debug/debug-vertex-array';
111
111
  export {getDebugTableForProgramConfiguration} from './debug/debug-program-configuration';
112
112
 
113
+ // HELPERS - EXPERIMENTAL
114
+ export {getProgramBindings} from './helpers/get-program-bindings';
115
+
113
116
  // DEPRECATED
114
117
 
115
118
  // Deprecated re-exports
@@ -117,15 +120,23 @@ export {lumaStats} from './init';
117
120
  export {log, assert, uid, isObjectEmpty} from '@luma.gl/api';
118
121
  export {setPathPrefix, loadFile, loadImage} from '@luma.gl/api';
119
122
 
120
- // export {
121
- // getContextInfo, getGLContextInfo, getContextLimits,
122
- // FEATURES,
123
- // getFeatures,
124
- // canCompileGLGSExtension
125
- // } from '@luma.gl/gltools';
126
-
127
- // HELPERS - EXPERIMENTAL
128
- export {getProgramBindings} from './helpers/get-program-bindings';
123
+ // GLTOOLS
124
+ export type {GLContextOptions} from './_deprecated/context-api';
125
+ export {
126
+ createGLContext,
127
+ instrumentGLContext,
128
+ resizeGLContext,
129
+ hasFeature,
130
+ hasFeatures,
131
+ getFeatures,
132
+ getContextInfo,
133
+ getGLContextInfo,
134
+ getContextLimits,
135
+ getContextDebugInfo
136
+ } from './_deprecated/context-api';
137
+
138
+ // Features
139
+ export {FEATURES} from './_deprecated/features';
129
140
 
130
141
  // REMOVED in v8.7
131
142
  // getShaderInfo,