Skip to content

Commit

Permalink
Feature cleanup (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Mar 12, 2019
1 parent 87ae85c commit 9a91b0a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 146 deletions.
7 changes: 4 additions & 3 deletions docs/api-reference/webgl/context/has-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ The WebGL standard comes with an elaborate "extension" system allowing applicati

Parameters to `hasFeatures`:

| `FEATURE` | WebGL2 | WebGL1 | Description |
| --- | --- | --- | --- |
| **General WebGL Features** | | | |
| `FEATURE` | WebGL2 | WebGL1 | Description |
| --- | --- | --- | --- |
| **General WebGL Features** | | | |
| `FEATURES.WEBGL2` | **YES** | **NO** | True for WebGL2 Context |
| `FEATURES.INSTANCED_RENDERING` | **YES** | * | Instanced rendering (via instanced vertex attributes) [`ANGLE_instanced_arrays`](https://developer.mozilla.org/en-US/docs/Web/API/ANGLE_instanced_arrays) |
| `FEATURES.VERTEX_ARRAY_OBJECT` | **YES** | * | `VertexArrayObjects` can be created [`OES_vertex_array_object`](https://developer.mozilla.org/en-US/docs/Web/API/OES_vertex_array_object) |
| `FEATURES.ELEMENT_INDEX_UINT32` | **YES** | * | 32 bit indices available for `GL.ELEMENT_ARRAY_BUFFER`s [`OES_element_index_uint`](https://developer.mozilla.org/en-US/docs/Web/API/OES_element_index_uint) |
Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export {
} from './webgl/context/context';
export {getContextInfo, getGLContextInfo, getContextLimits} from './webgl/features/limits';
export {getContextDebugInfo as glGetDebugInfo} from './webgl/debug/get-context-debug-info';
export {FEATURES, hasFeature, hasFeatures, getFeatures} from './webgl/features/features';
export {FEATURES} from './webgl/features/webgl-features-table';
export {hasFeature, hasFeatures, getFeatures} from './webgl/features/features';
export {default as canCompileGLGSExtension} from './webgl/features/check-glsl-extension';
export {
trackContextState,
Expand Down
65 changes: 33 additions & 32 deletions modules/core/src/webgl/features/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,57 @@ import WEBGL_FEATURES from './webgl-features-table';
import {isWebGL2} from '../utils';
import {assert} from '../../utils';

// Create a key-mirrored FEATURES array
// TODO - refactor, this has tree-shaking side effects
const FEATURES = {};
Object.keys(WEBGL_FEATURES).forEach(key => {
FEATURES[key] = key;
});
export {FEATURES};

// TODO - cache the value
function getFeature(gl, cap) {
const feature = WEBGL_FEATURES[cap];
assert(feature, cap);

// Get extension name from table
const extensionName = isWebGL2(gl) ? feature[1] || feature[0] : feature[0];

// Check if the value is dependent on checking an extension
const value =
typeof extensionName === 'string' ? Boolean(gl.getExtension(extensionName)) : extensionName;

assert(value === false || value === true);

return value;
}

// capability can be a WebGL extension name or a luma capability name
// Check one feature
export function hasFeature(gl, feature) {
return hasFeatures(gl, feature);
}

// Check one or more features
export function hasFeatures(gl, features) {
features = Array.isArray(features) ? features : [features];
return features.every(feature => {
return getFeature(gl, feature);
return isFeatureSupported(gl, feature);
});
}

// Return a list of supported features
export function getFeatures(gl) {
gl.luma = gl.luma || {};

if (!gl.luma.caps) {
gl.luma.caps = {};
gl.luma.caps.webgl2 = isWebGL2(gl);
for (const cap in WEBGL_FEATURES) {
gl.luma.caps[cap] = getFeature(gl, cap);
gl.luma.caps[cap] = isFeatureSupported(gl, cap);
}
}
return gl.luma.caps;
}

export const TEST_EXPORTS = {
WEBGL_FEATURES
};
// TODO - cache the value
function isFeatureSupported(gl, cap) {
const feature = WEBGL_FEATURES[cap];
assert(feature, cap);

// Get extension name from table
const featureDefinition = isWebGL2(gl) ? feature[1] || feature[0] : feature[0];

let isSupported;

// Check if the value is dependent on checking one or more extensions
if (typeof featureDefinition === 'function') {
isSupported = featureDefinition(gl);
} else if (Array.isArray(featureDefinition)) {
isSupported = true;
for (const extension of featureDefinition) {
isSupported = isSupported && Boolean(gl.getExtension(extension));
}
} else if (typeof featureDefinition === 'string') {
isSupported = Boolean(gl.getExtension(featureDefinition));
} else if (typeof featureDefinition === 'boolean') {
isSupported = featureDefinition;
} else {
assert(false);
}

return isSupported;
}
3 changes: 2 additions & 1 deletion modules/core/src/webgl/features/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {getContextInfo, getGLContextInfo, getContextLimits} from './limits';
export {FEATURES, hasFeature, hasFeatures, getFeatures} from './features';
export {FEATURES} from './webgl-features-table';
export {hasFeature, hasFeatures, getFeatures} from './features';

export {default as canCompileGLGSExtension} from './check-glsl-extension';
89 changes: 0 additions & 89 deletions modules/core/src/webgl/features/limits-duplicated.js

This file was deleted.

82 changes: 62 additions & 20 deletions modules/core/src/webgl/features/webgl-features-table.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,77 @@
import {isWebGL2} from '../utils';

// TODO - this should be the default export, test cases need updating
export const FEATURES = {
WEBGL2: 'WEBGL2',

// API SUPPORT
VERTEX_ARRAY_OBJECT: 'VERTEX_ARRAY_OBJECT',
TIMER_QUERY: 'TIMER_QUERY',
INSTANCED_RENDERING: 'INSTANCED_RENDERING',
MULTIPLE_RENDER_TARGETS: 'MULTIPLE_RENDER_TARGETS',

// FEATURES
ELEMENT_INDEX_UINT32: 'ELEMENT_INDEX_UINT32',
BLEND_EQUATION_MINMAX: 'BLEND_EQUATION_MINMAX',

// TEXTURES: '// TEXTURES', RENDERBUFFERS
COLOR_ENCODING_SRGB: 'COLOR_ENCODING_SRGB',

// TEXTURES
TEXTURE_DEPTH: 'TEXTURE_DEPTH',
TEXTURE_FLOAT: 'TEXTURE_FLOAT',
TEXTURE_HALF_FLOAT: 'TEXTURE_HALF_FLOAT',

TEXTURE_FILTER_LINEAR_FLOAT: 'TEXTURE_FILTER_LINEAR_FLOAT',
TEXTURE_FILTER_LINEAR_HALF_FLOAT: 'TEXTURE_FILTER_LINEAR_HALF_FLOAT',
TEXTURE_FILTER_ANISOTROPIC: 'TEXTURE_FILTER_ANISOTROPIC',

// FRAMEBUFFERS: '// FRAMEBUFFERS', TEXTURES AND RENDERBUFFERS
COLOR_ATTACHMENT_RGBA32F: 'COLOR_ATTACHMENT_RGBA32F',
COLOR_ATTACHMENT_FLOAT: 'COLOR_ATTACHMENT_FLOAT',
COLOR_ATTACHMENT_HALF_FLOAT: 'COLOR_ATTACHMENT_HALF_FLOAT',

// GLSL extensions
GLSL_FRAG_DATA: 'GLSL_FRAG_DATA',
GLSL_FRAG_DEPTH: 'GLSL_FRAG_DEPTH',
GLSL_DERIVATIVES: 'GLSL_DERIVATIVES',
GLSL_TEXTURE_LOD: 'GLSL_TEXTURE_LOD'
};

// Defines luma.gl "feature" names and semantics
export default {
[FEATURES.WEBGL2]: [gl => isWebGL2(gl)],

// API SUPPORT
VERTEX_ARRAY_OBJECT: ['OES_vertex_array_object', true],
TIMER_QUERY: ['EXT_disjoint_timer_query', 'EXT_disjoint_timer_query_webgl2'],
INSTANCED_RENDERING: ['ANGLE_instanced_arrays', true],
MULTIPLE_RENDER_TARGETS: ['WEBGL_draw_buffers', true],
[FEATURES.VERTEX_ARRAY_OBJECT]: ['OES_vertex_array_object', true],
[FEATURES.TIMER_QUERY]: ['EXT_disjoint_timer_query', 'EXT_disjoint_timer_query_webgl2'],
[FEATURES.INSTANCED_RENDERING]: ['ANGLE_instanced_arrays', true],
[FEATURES.MULTIPLE_RENDER_TARGETS]: ['WEBGL_draw_buffers', true],

// FEATURES
ELEMENT_INDEX_UINT32: ['OES_element_index_uint', true],
BLEND_EQUATION_MINMAX: ['EXT_blend_minmax', true],
[FEATURES.ELEMENT_INDEX_UINT32]: ['OES_element_index_uint', true],
[FEATURES.BLEND_EQUATION_MINMAX]: ['EXT_blend_minmax', true],

// TEXTURES, RENDERBUFFERS
COLOR_ENCODING_SRGB: ['EXT_sRGB', true],
[FEATURES.COLOR_ENCODING_SRGB]: ['EXT_sRGB', true],

// TEXTURES
TEXTURE_DEPTH: ['WEBGL_depth_texture', true],
TEXTURE_FLOAT: ['OES_texture_float', true],
TEXTURE_HALF_FLOAT: ['OES_texture_half_float', true],
[FEATURES.TEXTURE_DEPTH]: ['WEBGL_depth_texture', true],
[FEATURES.TEXTURE_FLOAT]: ['OES_texture_float', true],
[FEATURES.TEXTURE_HALF_FLOAT]: ['OES_texture_half_float', true],

TEXTURE_FILTER_LINEAR_FLOAT: ['OES_texture_float_linear'],
TEXTURE_FILTER_LINEAR_HALF_FLOAT: ['OES_texture_half_float_linear'],
TEXTURE_FILTER_ANISOTROPIC: ['EXT_texture_filter_anisotropic'],
[FEATURES.TEXTURE_FILTER_LINEAR_FLOAT]: ['OES_texture_float_linear'],
[FEATURES.TEXTURE_FILTER_LINEAR_HALF_FLOAT]: ['OES_texture_half_float_linear'],
[FEATURES.TEXTURE_FILTER_ANISOTROPIC]: ['EXT_texture_filter_anisotropic'],

// FRAMEBUFFERS, TEXTURES AND RENDERBUFFERS
COLOR_ATTACHMENT_RGBA32F: ['WEBGL_color_buffer_float', 'EXT_color_buffer_float'],
COLOR_ATTACHMENT_FLOAT: [false, 'EXT_color_buffer_float'],
COLOR_ATTACHMENT_HALF_FLOAT: [false, 'EXT_color_buffer_half_float'],
[FEATURES.COLOR_ATTACHMENT_RGBA32F]: ['WEBGL_color_buffer_float', 'EXT_color_buffer_float'],
[FEATURES.COLOR_ATTACHMENT_FLOAT]: [false, 'EXT_color_buffer_float'],
[FEATURES.COLOR_ATTACHMENT_HALF_FLOAT]: [false, 'EXT_color_buffer_half_float'],

// GLSL extensions
GLSL_FRAG_DATA: ['WEBGL_draw_buffers', true],
GLSL_FRAG_DEPTH: ['EXT_frag_depth', true],
GLSL_DERIVATIVES: ['OES_standard_derivatives', true],
GLSL_TEXTURE_LOD: ['EXT_shader_texture_lod', true]
[FEATURES.GLSL_FRAG_DATA]: ['WEBGL_draw_buffers', true],
[FEATURES.GLSL_FRAG_DEPTH]: ['EXT_frag_depth', true],
[FEATURES.GLSL_DERIVATIVES]: ['OES_standard_derivatives', true],
[FEATURES.GLSL_TEXTURE_LOD]: ['EXT_shader_texture_lod', true]
};
2 changes: 2 additions & 0 deletions modules/core/test/webgl/features/features.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {fixture} from 'test/setup';

// true: always supported in WebGL2, false: never supported in WebGL1
const WEBGL_FEATURES = {
WEBGL2: true,

// API SUPPORT
VERTEX_ARRAY_OBJECT: true,
INSTANCED_RENDERING: true,
Expand Down

0 comments on commit 9a91b0a

Please sign in to comment.