@luma.gl/webgl2-polyfill
Advanced tools
Comparing version 7.0.0-alpha.8 to 7.0.0-alpha.11
@@ -1,5 +0,22 @@ | ||
// Installs polyfills to support a subset of WebGL2 APIs on WebGL1 contexts | ||
"use strict"; | ||
export { default as polyfillContext } from './polyfill-context'; | ||
export { default } from './polyfill-context'; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "polyfillContext", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polyfillContext.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "default", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polyfillContext.default; | ||
} | ||
}); | ||
var _polyfillContext = _interopRequireDefault(require("./polyfill-context")); | ||
//# sourceMappingURL=index.js.map |
@@ -1,14 +0,16 @@ | ||
// WebGL1/WebGL2 extension polyfill support | ||
// | ||
// Provides a function that creates polyfills for WebGL2 functions based | ||
// on available extensions and installs them on a supplied target (could be | ||
// the WebGLContext or its prototype, or a separate object). | ||
// | ||
// This is intended to be a stand-alone file with minimal dependencies, | ||
// easy to reuse or repurpose in other projects. | ||
"use strict"; | ||
/* eslint-disable camelcase, brace-style */ | ||
import GL from '@luma.gl/constants'; | ||
import { getParameterPolyfill } from './polyfill-get-parameter'; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = polyfillContext; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _polyfillGetParameter = require("./polyfill-get-parameter"); | ||
var _WEBGL_CONTEXT_POLYFI; | ||
function assert(condition, message) { | ||
@@ -20,17 +22,15 @@ if (!condition) { | ||
const OES_vertex_array_object = 'OES_vertex_array_object'; | ||
const ANGLE_instanced_arrays = 'ANGLE_instanced_arrays'; | ||
const WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
const EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
const EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
const EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
var OES_vertex_array_object = 'OES_vertex_array_object'; | ||
var ANGLE_instanced_arrays = 'ANGLE_instanced_arrays'; | ||
var WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
var EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
var EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
var EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
var ERR_VAO_NOT_SUPPORTED = 'VertexArray requires WebGL2 or OES_vertex_array_object extension'; // Return true if WebGL2 context | ||
const ERR_VAO_NOT_SUPPORTED = 'VertexArray requires WebGL2 or OES_vertex_array_object extension'; | ||
// Return true if WebGL2 context | ||
function isWebGL2(gl) { | ||
return gl && gl.TEXTURE_BINDING_3D === GL.TEXTURE_BINDING_3D; | ||
} | ||
return gl && 32874 === 32874; | ||
} // Return object with webgl2 flag and an extension | ||
// Return object with webgl2 flag and an extension | ||
function getExtensionData(gl, extension) { | ||
@@ -41,5 +41,3 @@ return { | ||
}; | ||
} | ||
// function mapExtensionConstant(gl, constant) { | ||
} // function mapExtensionConstant(gl, constant) { | ||
// switch (constant) { | ||
@@ -50,173 +48,207 @@ // case ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES: return GL.FRAGMENT_SHADER_DERIVATIVE_HINT; | ||
const WEBGL_CONTEXT_POLYFILLS = { | ||
// POLYFILL TABLE | ||
[OES_vertex_array_object]: { | ||
meta: { suffix: 'OES' }, | ||
// NEW METHODS | ||
createVertexArray: () => { | ||
assert(false, ERR_VAO_NOT_SUPPORTED); | ||
}, | ||
deleteVertexArray: () => {}, | ||
bindVertexArray: () => {}, | ||
isVertexArray: () => false | ||
var WEBGL_CONTEXT_POLYFILLS = (_WEBGL_CONTEXT_POLYFI = {}, (0, _defineProperty2.default)(_WEBGL_CONTEXT_POLYFI, OES_vertex_array_object, { | ||
meta: { | ||
suffix: 'OES' | ||
}, | ||
[ANGLE_instanced_arrays]: { | ||
meta: { | ||
suffix: 'ANGLE' | ||
// constants: { | ||
// VERTEX_ATTRIB_ARRAY_DIVISOR: 'VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE' | ||
// } | ||
}, | ||
vertexAttribDivisor(location, divisor) { | ||
// Accept divisor 0 even if instancing is not supported (0 = no instancing) | ||
assert(divisor === 0, 'WebGL instanced rendering not supported'); | ||
}, | ||
drawElementsInstanced: () => {}, | ||
drawArraysInstanced: () => {} | ||
// NEW METHODS | ||
createVertexArray: function createVertexArray() { | ||
assert(false, ERR_VAO_NOT_SUPPORTED); | ||
}, | ||
[WEBGL_draw_buffers]: { | ||
meta: { | ||
suffix: 'WEBGL' | ||
}, | ||
drawBuffers: () => { | ||
assert(false); | ||
} | ||
deleteVertexArray: function deleteVertexArray() {}, | ||
bindVertexArray: function bindVertexArray() {}, | ||
isVertexArray: function isVertexArray() { | ||
return false; | ||
} | ||
}), (0, _defineProperty2.default)(_WEBGL_CONTEXT_POLYFI, ANGLE_instanced_arrays, { | ||
meta: { | ||
suffix: 'ANGLE' // constants: { | ||
// VERTEX_ATTRIB_ARRAY_DIVISOR: 'VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE' | ||
// } | ||
}, | ||
[EXT_disjoint_timer_query]: { | ||
meta: { suffix: 'EXT' }, | ||
// WebGL1: Polyfills the WebGL2 Query API | ||
createQuery: () => { | ||
assert(false); | ||
}, | ||
deleteQuery: () => { | ||
assert(false); | ||
}, | ||
beginQuery: () => { | ||
assert(false); | ||
}, | ||
endQuery: () => {}, | ||
getQuery(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// The WebGL1 extension uses getQueryObject rather then getQueryParameter | ||
getQueryParameter(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// plus the additional `queryCounter` method | ||
queryCounter: () => {}, | ||
getQueryObject: () => {} | ||
vertexAttribDivisor: function vertexAttribDivisor(location, divisor) { | ||
// Accept divisor 0 even if instancing is not supported (0 = no instancing) | ||
assert(divisor === 0, 'WebGL instanced rendering not supported'); | ||
}, | ||
// WebGL2: Adds `queryCounter` to the query API | ||
[EXT_disjoint_timer_query_webgl2]: { | ||
meta: { suffix: 'EXT' }, | ||
// install `queryCounter` | ||
// `null` avoids overwriting WebGL1 `queryCounter` if the WebGL2 extension is not available | ||
queryCounter: null | ||
drawElementsInstanced: function drawElementsInstanced() {}, | ||
drawArraysInstanced: function drawArraysInstanced() {} | ||
}), (0, _defineProperty2.default)(_WEBGL_CONTEXT_POLYFI, WEBGL_draw_buffers, { | ||
meta: { | ||
suffix: 'WEBGL' | ||
}, | ||
OVERRIDES: { | ||
// Ensure readBuffer is a no-op | ||
readBuffer: (gl, originalFunc, attachment) => { | ||
if (isWebGL2(gl)) { | ||
originalFunc(attachment); | ||
} else { | ||
// assert(attachment !== GL_COLOR_ATTACHMENT0 && attachment !== GL_FRONT); | ||
} | ||
}, | ||
// Override for getVertexAttrib that returns sane values for non-WebGL1 constants | ||
getVertexAttrib: (gl, originalFunc, location, pname) => { | ||
// const gl = this; // eslint-disable-line | ||
const { webgl2, ext } = getExtensionData(gl, ANGLE_instanced_arrays); | ||
drawBuffers: function drawBuffers() { | ||
assert(false); | ||
} | ||
}), (0, _defineProperty2.default)(_WEBGL_CONTEXT_POLYFI, EXT_disjoint_timer_query, { | ||
meta: { | ||
suffix: 'EXT' | ||
}, | ||
// WebGL1: Polyfills the WebGL2 Query API | ||
createQuery: function createQuery() { | ||
assert(false); | ||
}, | ||
deleteQuery: function deleteQuery() { | ||
assert(false); | ||
}, | ||
beginQuery: function beginQuery() { | ||
assert(false); | ||
}, | ||
endQuery: function endQuery() {}, | ||
getQuery: function getQuery(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// The WebGL1 extension uses getQueryObject rather then getQueryParameter | ||
getQueryParameter: function getQueryParameter(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// plus the additional `queryCounter` method | ||
queryCounter: function queryCounter() {}, | ||
getQueryObject: function getQueryObject() {} | ||
}), (0, _defineProperty2.default)(_WEBGL_CONTEXT_POLYFI, EXT_disjoint_timer_query_webgl2, { | ||
meta: { | ||
suffix: 'EXT' | ||
}, | ||
// install `queryCounter` | ||
// `null` avoids overwriting WebGL1 `queryCounter` if the WebGL2 extension is not available | ||
queryCounter: null | ||
}), (0, _defineProperty2.default)(_WEBGL_CONTEXT_POLYFI, "OVERRIDES", { | ||
// Ensure readBuffer is a no-op | ||
readBuffer: function readBuffer(gl, originalFunc, attachment) { | ||
if (isWebGL2(gl)) { | ||
originalFunc(attachment); | ||
} else {// assert(attachment !== GL_COLOR_ATTACHMENT0 && attachment !== GL_FRONT); | ||
} | ||
}, | ||
// Override for getVertexAttrib that returns sane values for non-WebGL1 constants | ||
getVertexAttrib: function getVertexAttrib(gl, originalFunc, location, pname) { | ||
// const gl = this; // eslint-disable-line | ||
var _getExtensionData = getExtensionData(gl, ANGLE_instanced_arrays), | ||
webgl2 = _getExtensionData.webgl2, | ||
ext = _getExtensionData.ext; | ||
let result; | ||
var result; | ||
switch (pname) { | ||
// WebGL1 attributes will never be integer | ||
case 35069: | ||
result = !webgl2 ? false : undefined; | ||
break; | ||
// if instancing is not available, return 0 meaning divisor has not been set | ||
case 35070: | ||
result = !webgl2 && !ext ? 0 : undefined; | ||
break; | ||
default: | ||
} | ||
return result !== undefined ? result : originalFunc(location, pname); | ||
}, | ||
// Handle transform feedback and uniform block queries in WebGL1 | ||
getProgramParameter: function getProgramParameter(gl, originalFunc, program, pname) { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
// WebGL1 attributes will never be integer | ||
case GL.VERTEX_ATTRIB_ARRAY_INTEGER: | ||
result = !webgl2 ? false : undefined; | ||
break; | ||
// if instancing is not available, return 0 meaning divisor has not been set | ||
case GL.VERTEX_ATTRIB_ARRAY_DIVISOR: | ||
result = !webgl2 && !ext ? 0 : undefined; | ||
break; | ||
case 35967: | ||
return 35981; | ||
case 35971: | ||
return 0; | ||
case 35382: | ||
return 0; | ||
default: | ||
} | ||
} | ||
return result !== undefined ? result : originalFunc(location, pname); | ||
}, | ||
// Handle transform feedback and uniform block queries in WebGL1 | ||
getProgramParameter: (gl, originalFunc, program, pname) => { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
case GL.TRANSFORM_FEEDBACK_BUFFER_MODE: | ||
return GL.SEPARATE_ATTRIBS; | ||
case GL.TRANSFORM_FEEDBACK_VARYINGS: | ||
return 0; | ||
case GL.ACTIVE_UNIFORM_BLOCKS: | ||
return 0; | ||
default: | ||
} | ||
} | ||
return originalFunc(program, pname); | ||
}, | ||
getInternalformatParameter: (gl, originalFunc, target, format, pname) => { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
case GL.SAMPLES: | ||
return new Int32Array([0]); | ||
default: | ||
} | ||
} | ||
return gl.getInternalformatParameter(target, format, pname); | ||
}, | ||
getTexParameter(gl, originalFunc, target, pname) { | ||
return originalFunc(program, pname); | ||
}, | ||
getInternalformatParameter: function getInternalformatParameter(gl, originalFunc, target, format, pname) { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
case GL.TEXTURE_MAX_ANISOTROPY_EXT: | ||
const { extensions } = gl.luma; | ||
const ext = extensions[EXT_texture_filter_anisotropic]; | ||
pname = ext && ext.TEXTURE_MAX_ANISOTROPY_EXT || GL.TEXTURE_MAX_ANISOTROPY_EXT; | ||
break; | ||
case 32937: | ||
return new Int32Array([0]); | ||
default: | ||
} | ||
return originalFunc(target, pname); | ||
}, | ||
getParameter: getParameterPolyfill, | ||
hint(gl, originalFunc, pname, value) { | ||
// TODO - handle GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// switch (pname) { | ||
// case GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// } | ||
return originalFunc(pname, value); | ||
} | ||
return gl.getInternalformatParameter(target, format, pname); | ||
}, | ||
getTexParameter: function getTexParameter(gl, originalFunc, target, pname) { | ||
switch (pname) { | ||
case 34046: | ||
var extensions = gl.luma.extensions; | ||
var ext = extensions[EXT_texture_filter_anisotropic]; | ||
pname = ext && ext.TEXTURE_MAX_ANISOTROPY_EXT || 34046; | ||
break; | ||
default: | ||
} | ||
return originalFunc(target, pname); | ||
}, | ||
getParameter: _polyfillGetParameter.getParameterPolyfill, | ||
hint: function hint(gl, originalFunc, pname, value) { | ||
// TODO - handle GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// switch (pname) { | ||
// case GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// } | ||
return originalFunc(pname, value); | ||
} | ||
}; | ||
}), _WEBGL_CONTEXT_POLYFI); | ||
function initializeExtensions(gl) { | ||
gl.luma.extensions = {}; | ||
// `getSupportedExtensions` can return null when context is lost. | ||
const EXTENSIONS = gl.getSupportedExtensions() || []; | ||
for (const extension of EXTENSIONS) { | ||
gl.luma[extension] = gl.getExtension(extension); | ||
} | ||
} | ||
gl.luma.extensions = {}; // `getSupportedExtensions` can return null when context is lost. | ||
// Polyfills a single WebGL extension into the `target` object | ||
function polyfillExtension(gl, { extension, target, target2 }) { | ||
const defaults = WEBGL_CONTEXT_POLYFILLS[extension]; | ||
assert(defaults); | ||
var EXTENSIONS = gl.getSupportedExtensions() || []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
const { meta = {} } = defaults; | ||
const { suffix = '' } = meta; | ||
try { | ||
for (var _iterator = EXTENSIONS[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var extension = _step.value; | ||
gl.luma[extension] = gl.getExtension(extension); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} // Polyfills a single WebGL extension into the `target` object | ||
const ext = gl.getExtension(extension); | ||
Object.keys(defaults).forEach(key => { | ||
const extKey = `${key}${suffix}`; | ||
function polyfillExtension(gl, _ref) { | ||
var extension = _ref.extension, | ||
target = _ref.target, | ||
target2 = _ref.target2; | ||
var defaults = WEBGL_CONTEXT_POLYFILLS[extension]; | ||
assert(defaults); | ||
var _defaults$meta = defaults.meta, | ||
meta = _defaults$meta === void 0 ? {} : _defaults$meta; | ||
var _meta$suffix = meta.suffix, | ||
suffix = _meta$suffix === void 0 ? '' : _meta$suffix; | ||
var ext = gl.getExtension(extension); | ||
Object.keys(defaults).forEach(function (key) { | ||
var extKey = "".concat(key).concat(suffix); | ||
var polyfill = null; | ||
let polyfill = null; | ||
if (key === 'meta') { | ||
// ignore | ||
} else if (typeof gl[key] === 'function') { | ||
// WebGL2 implementation is already | ||
if (key === 'meta') {// ignore | ||
} else if (typeof gl[key] === 'function') {// WebGL2 implementation is already | ||
} else if (ext && typeof ext[extKey] === 'function') { | ||
// pick extension implemenentation,if available | ||
polyfill = (...args) => ext[extKey](...args); | ||
polyfill = function polyfill() { | ||
return ext[extKey].apply(ext, arguments); | ||
}; | ||
} else if (typeof defaults[key] === 'function') { | ||
@@ -232,12 +264,14 @@ // pick the mock implementation, if no implementation was detected | ||
}); | ||
} | ||
} // Install simple overrides (mostly get* functions) | ||
// Install simple overrides (mostly get* functions) | ||
function installOverrides(gl, { target, target2 }) { | ||
const { OVERRIDES } = WEBGL_CONTEXT_POLYFILLS; | ||
Object.keys(OVERRIDES).forEach(key => { | ||
function installOverrides(gl, _ref2) { | ||
var target = _ref2.target, | ||
target2 = _ref2.target2; | ||
var OVERRIDES = WEBGL_CONTEXT_POLYFILLS.OVERRIDES; | ||
Object.keys(OVERRIDES).forEach(function (key) { | ||
if (typeof OVERRIDES[key] === 'function') { | ||
// install an override, if no implementation was detected | ||
const originalFunc = gl[key] ? gl[key].bind(gl) : () => {}; | ||
const polyfill = OVERRIDES[key].bind(null, gl, originalFunc); | ||
var originalFunc = gl[key] ? gl[key].bind(gl) : function () {}; | ||
var polyfill = OVERRIDES[key].bind(null, gl, originalFunc); | ||
target[key] = polyfill; | ||
@@ -247,24 +281,35 @@ target2[key] = polyfill; | ||
}); | ||
} | ||
} // Registers polyfill or mock functions for all known extensions | ||
// Registers polyfill or mock functions for all known extensions | ||
export default function polyfillContext(gl) { | ||
function polyfillContext(gl) { | ||
// polyfillVertexArrayObject(WebGLRenderingContext, gl); | ||
gl.luma = gl.luma || {}; | ||
initializeExtensions(gl); | ||
if (!gl.luma.polyfilled) { | ||
for (const extension in WEBGL_CONTEXT_POLYFILLS) { | ||
for (var extension in WEBGL_CONTEXT_POLYFILLS) { | ||
if (extension !== 'overrides') { | ||
polyfillExtension(gl, { extension, target: gl.luma, target2: gl }); | ||
polyfillExtension(gl, { | ||
extension: extension, | ||
target: gl.luma, | ||
target2: gl | ||
}); | ||
} | ||
} | ||
installOverrides(gl, { target: gl.luma, target2: gl }); | ||
installOverrides(gl, { | ||
target: gl.luma, | ||
target2: gl | ||
}); | ||
gl.luma.polyfilled = true; | ||
} | ||
return gl; | ||
} | ||
/* global window, global */ | ||
/* global window, global */ | ||
const global_ = typeof global !== 'undefined' ? global : window; | ||
var global_ = typeof global !== 'undefined' ? global : window; | ||
global_.polyfillContext = polyfillContext; | ||
//# sourceMappingURL=polyfill-context.js.map |
@@ -1,121 +0,91 @@ | ||
/* eslint-disable no-inline-comments, max-len, camelcase */ | ||
import GL from '@luma.gl/constants'; | ||
"use strict"; | ||
const OES_element_index = 'OES_element_index'; | ||
const WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
const EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
const EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
const EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
const WEBGL_debug_renderer_info = 'WEBGL_debug_renderer_info'; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
const GL_FRAGMENT_SHADER_DERIVATIVE_HINT = 0x8b8b; | ||
const GL_DONT_CARE = 0x1100; | ||
const GL_GPU_DISJOINT_EXT = 0x8fbb; | ||
const GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84ff; | ||
const GL_UNMASKED_VENDOR_WEBGL = 0x9245; // vendor string of the graphics driver. | ||
const GL_UNMASKED_RENDERER_WEBGL = 0x9246; // renderer string of the graphics driver. | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getParameterPolyfill = getParameterPolyfill; | ||
const getWebGL2ValueOrZero = gl => !isWebGL2(gl) ? 0 : undefined; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
// if a function returns undefined in this table, | ||
// the original getParameter will be called, defeating the override | ||
const WEBGL_PARAMETERS = { | ||
[GL.READ_BUFFER]: gl => !isWebGL2(gl) ? GL.COLOR_ATTACHMENT0 : undefined, | ||
var _WEBGL_PARAMETERS; | ||
// WebGL2 context parameters | ||
[GL_FRAGMENT_SHADER_DERIVATIVE_HINT]: gl => !isWebGL2(gl) ? GL_DONT_CARE : undefined, | ||
/* eslint-disable no-inline-comments, max-len, camelcase */ | ||
var OES_element_index = 'OES_element_index'; | ||
var WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
var EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
var EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
var EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
var WEBGL_debug_renderer_info = 'WEBGL_debug_renderer_info'; | ||
var GL_FRAGMENT_SHADER_DERIVATIVE_HINT = 0x8b8b; | ||
var GL_DONT_CARE = 0x1100; | ||
var GL_GPU_DISJOINT_EXT = 0x8fbb; | ||
var GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84ff; | ||
var GL_UNMASKED_VENDOR_WEBGL = 0x9245; // vendor string of the graphics driver. | ||
[GL.RASTERIZER_DISCARD]: getWebGL2ValueOrZero, | ||
var GL_UNMASKED_RENDERER_WEBGL = 0x9246; // renderer string of the graphics driver. | ||
[GL.SAMPLES]: getWebGL2ValueOrZero, | ||
var getWebGL2ValueOrZero = function getWebGL2ValueOrZero(gl) { | ||
return !isWebGL2(gl) ? 0 : undefined; | ||
}; // if a function returns undefined in this table, | ||
// the original getParameter will be called, defeating the override | ||
// WebGL2 extension context parameters | ||
[GL_GPU_DISJOINT_EXT]: (gl, getParameter) => { | ||
const ext = isWebGL2(gl) ? gl.getExtension(EXT_disjoint_timer_query_webgl2) : gl.getExtension(EXT_disjoint_timer_query); | ||
return ext && ext.GPU_DISJOINT_EXT ? getParameter(ext.GPU_DISJOINT_EXT) : 0; | ||
}, | ||
// Extension fixed values | ||
[GL_UNMASKED_VENDOR_WEBGL]: (gl, getParameter) => { | ||
const ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_VENDOR_WEBGL || GL.VENDOR); | ||
}, | ||
var WEBGL_PARAMETERS = (_WEBGL_PARAMETERS = {}, (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 3074, function (gl) { | ||
return !isWebGL2(gl) ? 36064 : undefined; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, GL_FRAGMENT_SHADER_DERIVATIVE_HINT, function (gl) { | ||
return !isWebGL2(gl) ? GL_DONT_CARE : undefined; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35977, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 32937, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, GL_GPU_DISJOINT_EXT, function (gl, getParameter) { | ||
var ext = isWebGL2(gl) ? gl.getExtension(EXT_disjoint_timer_query_webgl2) : gl.getExtension(EXT_disjoint_timer_query); | ||
return ext && ext.GPU_DISJOINT_EXT ? getParameter(ext.GPU_DISJOINT_EXT) : 0; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, GL_UNMASKED_VENDOR_WEBGL, function (gl, getParameter) { | ||
var ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_VENDOR_WEBGL || 7936); | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, GL_UNMASKED_RENDERER_WEBGL, function (gl, getParameter) { | ||
var ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_RENDERER_WEBGL || 7937); | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, function (gl, getParameter) { | ||
var ext = gl.luma.extensions[EXT_texture_filter_anisotropic]; | ||
return ext ? getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1.0; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 32883, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35071, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 37447, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 36063, function (gl, getParameter) { | ||
if (!isWebGL2(gl)) { | ||
var ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL) : 0; | ||
} | ||
[GL_UNMASKED_RENDERER_WEBGL]: (gl, getParameter) => { | ||
const ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_RENDERER_WEBGL || GL.RENDERER); | ||
}, | ||
return undefined; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35379, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35374, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35377, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 34852, function (gl) { | ||
if (!isWebGL2(gl)) { | ||
var ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? ext.MAX_DRAW_BUFFERS_WEBGL : 0; | ||
} | ||
// Extension LIMITS | ||
[GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT]: (gl, getParameter) => { | ||
const ext = gl.luma.extensions[EXT_texture_filter_anisotropic]; | ||
return ext ? getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1.0; | ||
}, | ||
return undefined; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 36203, // Guess: per webglstats.com 99.6% of webgl2 supports 2147483647 | ||
function (gl) { | ||
return gl.getExtension(OES_element_index) ? 2147483647 : 65535; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 33001, // Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
function (gl) { | ||
return gl.getExtension(OES_element_index) ? 16777216 : 65535; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 33000, // Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
function (gl) { | ||
return 16777216; | ||
}), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 37157, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35373, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35657, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 36183, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 37137, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 34045, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35978, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35979, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35968, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35376, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35375, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35659, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 37154, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35371, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35658, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35076, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35077, getWebGL2ValueOrZero), (0, _defineProperty2.default)(_WEBGL_PARAMETERS, 35380, getWebGL2ValueOrZero), _WEBGL_PARAMETERS); // Return true if WebGL2 context | ||
// WebGL2 Limits | ||
[GL.MAX_3D_TEXTURE_SIZE]: getWebGL2ValueOrZero, | ||
[GL.MAX_ARRAY_TEXTURE_LAYERS]: getWebGL2ValueOrZero, | ||
[GL.MAX_CLIENT_WAIT_TIMEOUT_WEBGL]: getWebGL2ValueOrZero, | ||
[GL.MAX_COLOR_ATTACHMENTS]: (gl, getParameter) => { | ||
if (!isWebGL2(gl)) { | ||
const ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL) : 0; | ||
} | ||
return undefined; | ||
}, | ||
[GL.MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_COMBINED_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_DRAW_BUFFERS]: gl => { | ||
if (!isWebGL2(gl)) { | ||
const ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? ext.MAX_DRAW_BUFFERS_WEBGL : 0; | ||
} | ||
return undefined; | ||
}, | ||
[GL.MAX_ELEMENT_INDEX]: | ||
// Guess: per webglstats.com 99.6% of webgl2 supports 2147483647 | ||
gl => gl.getExtension(OES_element_index) ? 2147483647 : 65535, | ||
[GL.MAX_ELEMENTS_INDICES]: | ||
// Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
gl => gl.getExtension(OES_element_index) ? 16777216 : 65535, | ||
[GL.MAX_ELEMENTS_VERTICES]: | ||
// Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
gl => 16777216, | ||
[GL.MAX_FRAGMENT_INPUT_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_FRAGMENT_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_FRAGMENT_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_SAMPLES]: getWebGL2ValueOrZero, | ||
[GL.MAX_SERVER_WAIT_TIMEOUT]: getWebGL2ValueOrZero, | ||
[GL.MAX_TEXTURE_LOD_BIAS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_UNIFORM_BLOCK_SIZE]: getWebGL2ValueOrZero, | ||
[GL.MAX_UNIFORM_BUFFER_BINDINGS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VARYING_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_OUTPUT_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MIN_PROGRAM_TEXEL_OFFSET]: getWebGL2ValueOrZero, | ||
[GL.MAX_PROGRAM_TEXEL_OFFSET]: getWebGL2ValueOrZero, | ||
[GL.UNIFORM_BUFFER_OFFSET_ALIGNMENT]: getWebGL2ValueOrZero | ||
}; | ||
// Return true if WebGL2 context | ||
function isWebGL2(gl) { | ||
const GL_TEXTURE_BINDING_3D = 0x806a; | ||
return gl && gl.TEXTURE_BINDING_3D === GL_TEXTURE_BINDING_3D; | ||
} | ||
var GL_TEXTURE_BINDING_3D = 0x806a; | ||
return gl && 32874 === GL_TEXTURE_BINDING_3D; | ||
} // A "replacement" gl.getParameter that accepts "enums" from extensions and WebGL2 | ||
// and returns reasonably safe defaults | ||
// A "replacement" gl.getParameter that accepts "enums" from extensions and WebGL2 | ||
// and returns reasonably safe defaults | ||
export function getParameterPolyfill(gl, originalGetParameter, pname) { | ||
function getParameterPolyfill(gl, originalGetParameter, pname) { | ||
// Return mock limits (usually 0) for WebGL2 constants to ensure these | ||
// can be queries without error | ||
const limit = WEBGL_PARAMETERS[pname]; | ||
const value = typeof limit === 'function' ? limit(gl, originalGetParameter, pname) : limit; | ||
const result = value !== undefined ? value : originalGetParameter(pname); | ||
var limit = WEBGL_PARAMETERS[pname]; | ||
var value = typeof limit === 'function' ? limit(gl, originalGetParameter, pname) : limit; | ||
var result = value !== undefined ? value : originalGetParameter(pname); | ||
return result; | ||
} | ||
//# sourceMappingURL=polyfill-get-parameter.js.map |
// Installs polyfills to support a subset of WebGL2 APIs on WebGL1 contexts | ||
export { default as polyfillContext } from './polyfill-context'; | ||
export { default } from './polyfill-context'; | ||
//# sourceMappingURL=index.js.map |
@@ -11,3 +11,2 @@ // WebGL1/WebGL2 extension polyfill support | ||
/* eslint-disable camelcase, brace-style */ | ||
import GL from '@luma.gl/constants'; | ||
import { getParameterPolyfill } from './polyfill-get-parameter'; | ||
@@ -27,11 +26,9 @@ | ||
const EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
const ERR_VAO_NOT_SUPPORTED = 'VertexArray requires WebGL2 or OES_vertex_array_object extension'; // Return true if WebGL2 context | ||
const ERR_VAO_NOT_SUPPORTED = 'VertexArray requires WebGL2 or OES_vertex_array_object extension'; | ||
// Return true if WebGL2 context | ||
function isWebGL2(gl) { | ||
return gl && gl.TEXTURE_BINDING_3D === GL.TEXTURE_BINDING_3D; | ||
} | ||
return gl && 32874 === 32874; | ||
} // Return object with webgl2 flag and an extension | ||
// Return object with webgl2 flag and an extension | ||
function getExtensionData(gl, extension) { | ||
@@ -42,5 +39,3 @@ return { | ||
}; | ||
} | ||
// function mapExtensionConstant(gl, constant) { | ||
} // function mapExtensionConstant(gl, constant) { | ||
// switch (constant) { | ||
@@ -51,6 +46,9 @@ // case ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES: return GL.FRAGMENT_SHADER_DERIVATIVE_HINT; | ||
const WEBGL_CONTEXT_POLYFILLS = { | ||
// POLYFILL TABLE | ||
[OES_vertex_array_object]: { | ||
meta: { suffix: 'OES' }, | ||
meta: { | ||
suffix: 'OES' | ||
}, | ||
// NEW METHODS | ||
@@ -66,7 +64,8 @@ createVertexArray: () => { | ||
meta: { | ||
suffix: 'ANGLE' | ||
// constants: { | ||
suffix: 'ANGLE' // constants: { | ||
// VERTEX_ATTRIB_ARRAY_DIVISOR: 'VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE' | ||
// } | ||
}, | ||
vertexAttribDivisor(location, divisor) { | ||
@@ -76,2 +75,3 @@ // Accept divisor 0 even if instancing is not supported (0 = no instancing) | ||
}, | ||
drawElementsInstanced: () => {}, | ||
@@ -89,3 +89,5 @@ drawArraysInstanced: () => {} | ||
[EXT_disjoint_timer_query]: { | ||
meta: { suffix: 'EXT' }, | ||
meta: { | ||
suffix: 'EXT' | ||
}, | ||
// WebGL1: Polyfills the WebGL2 Query API | ||
@@ -102,5 +104,7 @@ createQuery: () => { | ||
endQuery: () => {}, | ||
getQuery(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// The WebGL1 extension uses getQueryObject rather then getQueryParameter | ||
@@ -110,2 +114,3 @@ getQueryParameter(handle, pname) { | ||
}, | ||
// plus the additional `queryCounter` method | ||
@@ -117,3 +122,5 @@ queryCounter: () => {}, | ||
[EXT_disjoint_timer_query_webgl2]: { | ||
meta: { suffix: 'EXT' }, | ||
meta: { | ||
suffix: 'EXT' | ||
}, | ||
// install `queryCounter` | ||
@@ -128,4 +135,3 @@ // `null` avoids overwriting WebGL1 `queryCounter` if the WebGL2 extension is not available | ||
originalFunc(attachment); | ||
} else { | ||
// assert(attachment !== GL_COLOR_ATTACHMENT0 && attachment !== GL_FRONT); | ||
} else {// assert(attachment !== GL_COLOR_ATTACHMENT0 && attachment !== GL_FRONT); | ||
} | ||
@@ -136,14 +142,19 @@ }, | ||
// const gl = this; // eslint-disable-line | ||
const { webgl2, ext } = getExtensionData(gl, ANGLE_instanced_arrays); | ||
const _getExtensionData = getExtensionData(gl, ANGLE_instanced_arrays), | ||
webgl2 = _getExtensionData.webgl2, | ||
ext = _getExtensionData.ext; | ||
let result; | ||
switch (pname) { | ||
// WebGL1 attributes will never be integer | ||
case GL.VERTEX_ATTRIB_ARRAY_INTEGER: | ||
case 35069: | ||
result = !webgl2 ? false : undefined; | ||
break; | ||
// if instancing is not available, return 0 meaning divisor has not been set | ||
case GL.VERTEX_ATTRIB_ARRAY_DIVISOR: | ||
case 35070: | ||
result = !webgl2 && !ext ? 0 : undefined; | ||
break; | ||
default: | ||
@@ -158,11 +169,15 @@ } | ||
switch (pname) { | ||
case GL.TRANSFORM_FEEDBACK_BUFFER_MODE: | ||
return GL.SEPARATE_ATTRIBS; | ||
case GL.TRANSFORM_FEEDBACK_VARYINGS: | ||
case 35967: | ||
return 35981; | ||
case 35971: | ||
return 0; | ||
case GL.ACTIVE_UNIFORM_BLOCKS: | ||
case 35382: | ||
return 0; | ||
default: | ||
} | ||
} | ||
return originalFunc(program, pname); | ||
@@ -173,21 +188,28 @@ }, | ||
switch (pname) { | ||
case GL.SAMPLES: | ||
case 32937: | ||
return new Int32Array([0]); | ||
default: | ||
} | ||
} | ||
return gl.getInternalformatParameter(target, format, pname); | ||
}, | ||
getTexParameter(gl, originalFunc, target, pname) { | ||
switch (pname) { | ||
case GL.TEXTURE_MAX_ANISOTROPY_EXT: | ||
const { extensions } = gl.luma; | ||
case 34046: | ||
const extensions = gl.luma.extensions; | ||
const ext = extensions[EXT_texture_filter_anisotropic]; | ||
pname = ext && ext.TEXTURE_MAX_ANISOTROPY_EXT || GL.TEXTURE_MAX_ANISOTROPY_EXT; | ||
pname = ext && ext.TEXTURE_MAX_ANISOTROPY_EXT || 34046; | ||
break; | ||
default: | ||
} | ||
return originalFunc(target, pname); | ||
}, | ||
getParameter: getParameterPolyfill, | ||
hint(gl, originalFunc, pname, value) { | ||
@@ -200,2 +222,3 @@ // TODO - handle GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
} | ||
} | ||
@@ -205,28 +228,30 @@ }; | ||
function initializeExtensions(gl) { | ||
gl.luma.extensions = {}; | ||
// `getSupportedExtensions` can return null when context is lost. | ||
gl.luma.extensions = {}; // `getSupportedExtensions` can return null when context is lost. | ||
const EXTENSIONS = gl.getSupportedExtensions() || []; | ||
for (const extension of EXTENSIONS) { | ||
gl.luma[extension] = gl.getExtension(extension); | ||
} | ||
} | ||
} // Polyfills a single WebGL extension into the `target` object | ||
// Polyfills a single WebGL extension into the `target` object | ||
function polyfillExtension(gl, { extension, target, target2 }) { | ||
function polyfillExtension(gl, { | ||
extension, | ||
target, | ||
target2 | ||
}) { | ||
const defaults = WEBGL_CONTEXT_POLYFILLS[extension]; | ||
assert(defaults); | ||
const { meta = {} } = defaults; | ||
const { suffix = '' } = meta; | ||
const _defaults$meta = defaults.meta, | ||
meta = _defaults$meta === void 0 ? {} : _defaults$meta; | ||
const _meta$suffix = meta.suffix, | ||
suffix = _meta$suffix === void 0 ? '' : _meta$suffix; | ||
const ext = gl.getExtension(extension); | ||
Object.keys(defaults).forEach(key => { | ||
const extKey = `${key}${suffix}`; | ||
let polyfill = null; | ||
let polyfill = null; | ||
if (key === 'meta') { | ||
// ignore | ||
} else if (typeof gl[key] === 'function') { | ||
// WebGL2 implementation is already | ||
if (key === 'meta') {// ignore | ||
} else if (typeof gl[key] === 'function') {// WebGL2 implementation is already | ||
} else if (ext && typeof ext[extKey] === 'function') { | ||
@@ -245,7 +270,10 @@ // pick extension implemenentation,if available | ||
}); | ||
} | ||
} // Install simple overrides (mostly get* functions) | ||
// Install simple overrides (mostly get* functions) | ||
function installOverrides(gl, { target, target2 }) { | ||
const { OVERRIDES } = WEBGL_CONTEXT_POLYFILLS; | ||
function installOverrides(gl, { | ||
target, | ||
target2 | ||
}) { | ||
const OVERRIDES = WEBGL_CONTEXT_POLYFILLS.OVERRIDES; | ||
Object.keys(OVERRIDES).forEach(key => { | ||
@@ -260,5 +288,5 @@ if (typeof OVERRIDES[key] === 'function') { | ||
}); | ||
} | ||
} // Registers polyfill or mock functions for all known extensions | ||
// Registers polyfill or mock functions for all known extensions | ||
export default function polyfillContext(gl) { | ||
@@ -268,17 +296,27 @@ // polyfillVertexArrayObject(WebGLRenderingContext, gl); | ||
initializeExtensions(gl); | ||
if (!gl.luma.polyfilled) { | ||
for (const extension in WEBGL_CONTEXT_POLYFILLS) { | ||
if (extension !== 'overrides') { | ||
polyfillExtension(gl, { extension, target: gl.luma, target2: gl }); | ||
polyfillExtension(gl, { | ||
extension, | ||
target: gl.luma, | ||
target2: gl | ||
}); | ||
} | ||
} | ||
installOverrides(gl, { target: gl.luma, target2: gl }); | ||
installOverrides(gl, { | ||
target: gl.luma, | ||
target2: gl | ||
}); | ||
gl.luma.polyfilled = true; | ||
} | ||
return gl; | ||
} | ||
/* global window, global */ | ||
/* global window, global */ | ||
const global_ = typeof global !== 'undefined' ? global : window; | ||
global_.polyfillContext = polyfillContext; | ||
//# sourceMappingURL=polyfill-context.js.map |
/* eslint-disable no-inline-comments, max-len, camelcase */ | ||
import GL from '@luma.gl/constants'; | ||
const OES_element_index = 'OES_element_index'; | ||
@@ -10,3 +8,2 @@ const WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
const WEBGL_debug_renderer_info = 'WEBGL_debug_renderer_info'; | ||
const GL_FRAGMENT_SHADER_DERIVATIVE_HINT = 0x8b8b; | ||
@@ -17,18 +14,15 @@ const GL_DONT_CARE = 0x1100; | ||
const GL_UNMASKED_VENDOR_WEBGL = 0x9245; // vendor string of the graphics driver. | ||
const GL_UNMASKED_RENDERER_WEBGL = 0x9246; // renderer string of the graphics driver. | ||
const getWebGL2ValueOrZero = gl => !isWebGL2(gl) ? 0 : undefined; | ||
const getWebGL2ValueOrZero = gl => !isWebGL2(gl) ? 0 : undefined; // if a function returns undefined in this table, | ||
// the original getParameter will be called, defeating the override | ||
// if a function returns undefined in this table, | ||
// the original getParameter will be called, defeating the override | ||
const WEBGL_PARAMETERS = { | ||
[GL.READ_BUFFER]: gl => !isWebGL2(gl) ? GL.COLOR_ATTACHMENT0 : undefined, | ||
[3074]: gl => !isWebGL2(gl) ? 36064 : undefined, | ||
// WebGL2 context parameters | ||
[GL_FRAGMENT_SHADER_DERIVATIVE_HINT]: gl => !isWebGL2(gl) ? GL_DONT_CARE : undefined, | ||
[GL.RASTERIZER_DISCARD]: getWebGL2ValueOrZero, | ||
[GL.SAMPLES]: getWebGL2ValueOrZero, | ||
[35977]: getWebGL2ValueOrZero, | ||
[32937]: getWebGL2ValueOrZero, | ||
// WebGL2 extension context parameters | ||
@@ -39,14 +33,11 @@ [GL_GPU_DISJOINT_EXT]: (gl, getParameter) => { | ||
}, | ||
// Extension fixed values | ||
[GL_UNMASKED_VENDOR_WEBGL]: (gl, getParameter) => { | ||
const ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_VENDOR_WEBGL || GL.VENDOR); | ||
return getParameter(ext && ext.UNMASKED_VENDOR_WEBGL || 7936); | ||
}, | ||
[GL_UNMASKED_RENDERER_WEBGL]: (gl, getParameter) => { | ||
const ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_RENDERER_WEBGL || GL.RENDERER); | ||
return getParameter(ext && ext.UNMASKED_RENDERER_WEBGL || 7937); | ||
}, | ||
// Extension LIMITS | ||
@@ -57,8 +48,7 @@ [GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT]: (gl, getParameter) => { | ||
}, | ||
// WebGL2 Limits | ||
[GL.MAX_3D_TEXTURE_SIZE]: getWebGL2ValueOrZero, | ||
[GL.MAX_ARRAY_TEXTURE_LAYERS]: getWebGL2ValueOrZero, | ||
[GL.MAX_CLIENT_WAIT_TIMEOUT_WEBGL]: getWebGL2ValueOrZero, | ||
[GL.MAX_COLOR_ATTACHMENTS]: (gl, getParameter) => { | ||
[32883]: getWebGL2ValueOrZero, | ||
[35071]: getWebGL2ValueOrZero, | ||
[37447]: getWebGL2ValueOrZero, | ||
[36063]: (gl, getParameter) => { | ||
if (!isWebGL2(gl)) { | ||
@@ -68,8 +58,9 @@ const ext = gl.getExtension(WEBGL_draw_buffers); | ||
} | ||
return undefined; | ||
}, | ||
[GL.MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_COMBINED_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_DRAW_BUFFERS]: gl => { | ||
[35379]: getWebGL2ValueOrZero, | ||
[35374]: getWebGL2ValueOrZero, | ||
[35377]: getWebGL2ValueOrZero, | ||
[34852]: gl => { | ||
if (!isWebGL2(gl)) { | ||
@@ -79,41 +70,38 @@ const ext = gl.getExtension(WEBGL_draw_buffers); | ||
} | ||
return undefined; | ||
}, | ||
[GL.MAX_ELEMENT_INDEX]: | ||
// Guess: per webglstats.com 99.6% of webgl2 supports 2147483647 | ||
[36203]: // Guess: per webglstats.com 99.6% of webgl2 supports 2147483647 | ||
gl => gl.getExtension(OES_element_index) ? 2147483647 : 65535, | ||
[GL.MAX_ELEMENTS_INDICES]: | ||
// Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
[33001]: // Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
gl => gl.getExtension(OES_element_index) ? 16777216 : 65535, | ||
[GL.MAX_ELEMENTS_VERTICES]: | ||
// Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
[33000]: // Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
gl => 16777216, | ||
[GL.MAX_FRAGMENT_INPUT_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_FRAGMENT_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_FRAGMENT_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_SAMPLES]: getWebGL2ValueOrZero, | ||
[GL.MAX_SERVER_WAIT_TIMEOUT]: getWebGL2ValueOrZero, | ||
[GL.MAX_TEXTURE_LOD_BIAS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_UNIFORM_BLOCK_SIZE]: getWebGL2ValueOrZero, | ||
[GL.MAX_UNIFORM_BUFFER_BINDINGS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VARYING_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_OUTPUT_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MIN_PROGRAM_TEXEL_OFFSET]: getWebGL2ValueOrZero, | ||
[GL.MAX_PROGRAM_TEXEL_OFFSET]: getWebGL2ValueOrZero, | ||
[GL.UNIFORM_BUFFER_OFFSET_ALIGNMENT]: getWebGL2ValueOrZero | ||
}; | ||
[37157]: getWebGL2ValueOrZero, | ||
[35373]: getWebGL2ValueOrZero, | ||
[35657]: getWebGL2ValueOrZero, | ||
[36183]: getWebGL2ValueOrZero, | ||
[37137]: getWebGL2ValueOrZero, | ||
[34045]: getWebGL2ValueOrZero, | ||
[35978]: getWebGL2ValueOrZero, | ||
[35979]: getWebGL2ValueOrZero, | ||
[35968]: getWebGL2ValueOrZero, | ||
[35376]: getWebGL2ValueOrZero, | ||
[35375]: getWebGL2ValueOrZero, | ||
[35659]: getWebGL2ValueOrZero, | ||
[37154]: getWebGL2ValueOrZero, | ||
[35371]: getWebGL2ValueOrZero, | ||
[35658]: getWebGL2ValueOrZero, | ||
[35076]: getWebGL2ValueOrZero, | ||
[35077]: getWebGL2ValueOrZero, | ||
[35380]: getWebGL2ValueOrZero | ||
}; // Return true if WebGL2 context | ||
// Return true if WebGL2 context | ||
function isWebGL2(gl) { | ||
const GL_TEXTURE_BINDING_3D = 0x806a; | ||
return gl && gl.TEXTURE_BINDING_3D === GL_TEXTURE_BINDING_3D; | ||
} | ||
return gl && 32874 === GL_TEXTURE_BINDING_3D; | ||
} // A "replacement" gl.getParameter that accepts "enums" from extensions and WebGL2 | ||
// and returns reasonably safe defaults | ||
// A "replacement" gl.getParameter that accepts "enums" from extensions and WebGL2 | ||
// and returns reasonably safe defaults | ||
export function getParameterPolyfill(gl, originalGetParameter, pname) { | ||
@@ -120,0 +108,0 @@ // Return mock limits (usually 0) for WebGL2 constants to ensure these |
// Installs polyfills to support a subset of WebGL2 APIs on WebGL1 contexts | ||
export { default as polyfillContext } from './polyfill-context'; | ||
export { default } from './polyfill-context'; | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,5 @@ | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
var _WEBGL_CONTEXT_POLYFI; | ||
// WebGL1/WebGL2 extension polyfill support | ||
@@ -11,3 +15,2 @@ // | ||
/* eslint-disable camelcase, brace-style */ | ||
import GL from '@luma.gl/constants'; | ||
import { getParameterPolyfill } from './polyfill-get-parameter'; | ||
@@ -21,17 +24,15 @@ | ||
const OES_vertex_array_object = 'OES_vertex_array_object'; | ||
const ANGLE_instanced_arrays = 'ANGLE_instanced_arrays'; | ||
const WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
const EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
const EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
const EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
var OES_vertex_array_object = 'OES_vertex_array_object'; | ||
var ANGLE_instanced_arrays = 'ANGLE_instanced_arrays'; | ||
var WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
var EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
var EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
var EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
var ERR_VAO_NOT_SUPPORTED = 'VertexArray requires WebGL2 or OES_vertex_array_object extension'; // Return true if WebGL2 context | ||
const ERR_VAO_NOT_SUPPORTED = 'VertexArray requires WebGL2 or OES_vertex_array_object extension'; | ||
// Return true if WebGL2 context | ||
function isWebGL2(gl) { | ||
return gl && gl.TEXTURE_BINDING_3D === GL.TEXTURE_BINDING_3D; | ||
} | ||
return gl && 32874 === 32874; | ||
} // Return object with webgl2 flag and an extension | ||
// Return object with webgl2 flag and an extension | ||
function getExtensionData(gl, extension) { | ||
@@ -42,5 +43,3 @@ return { | ||
}; | ||
} | ||
// function mapExtensionConstant(gl, constant) { | ||
} // function mapExtensionConstant(gl, constant) { | ||
// switch (constant) { | ||
@@ -51,173 +50,207 @@ // case ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES: return GL.FRAGMENT_SHADER_DERIVATIVE_HINT; | ||
const WEBGL_CONTEXT_POLYFILLS = { | ||
// POLYFILL TABLE | ||
[OES_vertex_array_object]: { | ||
meta: { suffix: 'OES' }, | ||
// NEW METHODS | ||
createVertexArray: () => { | ||
assert(false, ERR_VAO_NOT_SUPPORTED); | ||
}, | ||
deleteVertexArray: () => {}, | ||
bindVertexArray: () => {}, | ||
isVertexArray: () => false | ||
var WEBGL_CONTEXT_POLYFILLS = (_WEBGL_CONTEXT_POLYFI = {}, _defineProperty(_WEBGL_CONTEXT_POLYFI, OES_vertex_array_object, { | ||
meta: { | ||
suffix: 'OES' | ||
}, | ||
[ANGLE_instanced_arrays]: { | ||
meta: { | ||
suffix: 'ANGLE' | ||
// constants: { | ||
// VERTEX_ATTRIB_ARRAY_DIVISOR: 'VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE' | ||
// } | ||
}, | ||
vertexAttribDivisor(location, divisor) { | ||
// Accept divisor 0 even if instancing is not supported (0 = no instancing) | ||
assert(divisor === 0, 'WebGL instanced rendering not supported'); | ||
}, | ||
drawElementsInstanced: () => {}, | ||
drawArraysInstanced: () => {} | ||
// NEW METHODS | ||
createVertexArray: function createVertexArray() { | ||
assert(false, ERR_VAO_NOT_SUPPORTED); | ||
}, | ||
[WEBGL_draw_buffers]: { | ||
meta: { | ||
suffix: 'WEBGL' | ||
}, | ||
drawBuffers: () => { | ||
assert(false); | ||
} | ||
deleteVertexArray: function deleteVertexArray() {}, | ||
bindVertexArray: function bindVertexArray() {}, | ||
isVertexArray: function isVertexArray() { | ||
return false; | ||
} | ||
}), _defineProperty(_WEBGL_CONTEXT_POLYFI, ANGLE_instanced_arrays, { | ||
meta: { | ||
suffix: 'ANGLE' // constants: { | ||
// VERTEX_ATTRIB_ARRAY_DIVISOR: 'VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE' | ||
// } | ||
}, | ||
[EXT_disjoint_timer_query]: { | ||
meta: { suffix: 'EXT' }, | ||
// WebGL1: Polyfills the WebGL2 Query API | ||
createQuery: () => { | ||
assert(false); | ||
}, | ||
deleteQuery: () => { | ||
assert(false); | ||
}, | ||
beginQuery: () => { | ||
assert(false); | ||
}, | ||
endQuery: () => {}, | ||
getQuery(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// The WebGL1 extension uses getQueryObject rather then getQueryParameter | ||
getQueryParameter(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// plus the additional `queryCounter` method | ||
queryCounter: () => {}, | ||
getQueryObject: () => {} | ||
vertexAttribDivisor: function vertexAttribDivisor(location, divisor) { | ||
// Accept divisor 0 even if instancing is not supported (0 = no instancing) | ||
assert(divisor === 0, 'WebGL instanced rendering not supported'); | ||
}, | ||
// WebGL2: Adds `queryCounter` to the query API | ||
[EXT_disjoint_timer_query_webgl2]: { | ||
meta: { suffix: 'EXT' }, | ||
// install `queryCounter` | ||
// `null` avoids overwriting WebGL1 `queryCounter` if the WebGL2 extension is not available | ||
queryCounter: null | ||
drawElementsInstanced: function drawElementsInstanced() {}, | ||
drawArraysInstanced: function drawArraysInstanced() {} | ||
}), _defineProperty(_WEBGL_CONTEXT_POLYFI, WEBGL_draw_buffers, { | ||
meta: { | ||
suffix: 'WEBGL' | ||
}, | ||
OVERRIDES: { | ||
// Ensure readBuffer is a no-op | ||
readBuffer: (gl, originalFunc, attachment) => { | ||
if (isWebGL2(gl)) { | ||
originalFunc(attachment); | ||
} else { | ||
// assert(attachment !== GL_COLOR_ATTACHMENT0 && attachment !== GL_FRONT); | ||
} | ||
}, | ||
// Override for getVertexAttrib that returns sane values for non-WebGL1 constants | ||
getVertexAttrib: (gl, originalFunc, location, pname) => { | ||
// const gl = this; // eslint-disable-line | ||
const { webgl2, ext } = getExtensionData(gl, ANGLE_instanced_arrays); | ||
drawBuffers: function drawBuffers() { | ||
assert(false); | ||
} | ||
}), _defineProperty(_WEBGL_CONTEXT_POLYFI, EXT_disjoint_timer_query, { | ||
meta: { | ||
suffix: 'EXT' | ||
}, | ||
// WebGL1: Polyfills the WebGL2 Query API | ||
createQuery: function createQuery() { | ||
assert(false); | ||
}, | ||
deleteQuery: function deleteQuery() { | ||
assert(false); | ||
}, | ||
beginQuery: function beginQuery() { | ||
assert(false); | ||
}, | ||
endQuery: function endQuery() {}, | ||
getQuery: function getQuery(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// The WebGL1 extension uses getQueryObject rather then getQueryParameter | ||
getQueryParameter: function getQueryParameter(handle, pname) { | ||
return this.getQueryObject(handle, pname); | ||
}, | ||
// plus the additional `queryCounter` method | ||
queryCounter: function queryCounter() {}, | ||
getQueryObject: function getQueryObject() {} | ||
}), _defineProperty(_WEBGL_CONTEXT_POLYFI, EXT_disjoint_timer_query_webgl2, { | ||
meta: { | ||
suffix: 'EXT' | ||
}, | ||
// install `queryCounter` | ||
// `null` avoids overwriting WebGL1 `queryCounter` if the WebGL2 extension is not available | ||
queryCounter: null | ||
}), _defineProperty(_WEBGL_CONTEXT_POLYFI, "OVERRIDES", { | ||
// Ensure readBuffer is a no-op | ||
readBuffer: function readBuffer(gl, originalFunc, attachment) { | ||
if (isWebGL2(gl)) { | ||
originalFunc(attachment); | ||
} else {// assert(attachment !== GL_COLOR_ATTACHMENT0 && attachment !== GL_FRONT); | ||
} | ||
}, | ||
// Override for getVertexAttrib that returns sane values for non-WebGL1 constants | ||
getVertexAttrib: function getVertexAttrib(gl, originalFunc, location, pname) { | ||
// const gl = this; // eslint-disable-line | ||
var _getExtensionData = getExtensionData(gl, ANGLE_instanced_arrays), | ||
webgl2 = _getExtensionData.webgl2, | ||
ext = _getExtensionData.ext; | ||
let result; | ||
var result; | ||
switch (pname) { | ||
// WebGL1 attributes will never be integer | ||
case 35069: | ||
result = !webgl2 ? false : undefined; | ||
break; | ||
// if instancing is not available, return 0 meaning divisor has not been set | ||
case 35070: | ||
result = !webgl2 && !ext ? 0 : undefined; | ||
break; | ||
default: | ||
} | ||
return result !== undefined ? result : originalFunc(location, pname); | ||
}, | ||
// Handle transform feedback and uniform block queries in WebGL1 | ||
getProgramParameter: function getProgramParameter(gl, originalFunc, program, pname) { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
// WebGL1 attributes will never be integer | ||
case GL.VERTEX_ATTRIB_ARRAY_INTEGER: | ||
result = !webgl2 ? false : undefined; | ||
break; | ||
// if instancing is not available, return 0 meaning divisor has not been set | ||
case GL.VERTEX_ATTRIB_ARRAY_DIVISOR: | ||
result = !webgl2 && !ext ? 0 : undefined; | ||
break; | ||
case 35967: | ||
return 35981; | ||
case 35971: | ||
return 0; | ||
case 35382: | ||
return 0; | ||
default: | ||
} | ||
} | ||
return result !== undefined ? result : originalFunc(location, pname); | ||
}, | ||
// Handle transform feedback and uniform block queries in WebGL1 | ||
getProgramParameter: (gl, originalFunc, program, pname) => { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
case GL.TRANSFORM_FEEDBACK_BUFFER_MODE: | ||
return GL.SEPARATE_ATTRIBS; | ||
case GL.TRANSFORM_FEEDBACK_VARYINGS: | ||
return 0; | ||
case GL.ACTIVE_UNIFORM_BLOCKS: | ||
return 0; | ||
default: | ||
} | ||
} | ||
return originalFunc(program, pname); | ||
}, | ||
getInternalformatParameter: (gl, originalFunc, target, format, pname) => { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
case GL.SAMPLES: | ||
return new Int32Array([0]); | ||
default: | ||
} | ||
} | ||
return gl.getInternalformatParameter(target, format, pname); | ||
}, | ||
getTexParameter(gl, originalFunc, target, pname) { | ||
return originalFunc(program, pname); | ||
}, | ||
getInternalformatParameter: function getInternalformatParameter(gl, originalFunc, target, format, pname) { | ||
if (!isWebGL2(gl)) { | ||
switch (pname) { | ||
case GL.TEXTURE_MAX_ANISOTROPY_EXT: | ||
const { extensions } = gl.luma; | ||
const ext = extensions[EXT_texture_filter_anisotropic]; | ||
pname = ext && ext.TEXTURE_MAX_ANISOTROPY_EXT || GL.TEXTURE_MAX_ANISOTROPY_EXT; | ||
break; | ||
case 32937: | ||
return new Int32Array([0]); | ||
default: | ||
} | ||
return originalFunc(target, pname); | ||
}, | ||
getParameter: getParameterPolyfill, | ||
hint(gl, originalFunc, pname, value) { | ||
// TODO - handle GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// switch (pname) { | ||
// case GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// } | ||
return originalFunc(pname, value); | ||
} | ||
return gl.getInternalformatParameter(target, format, pname); | ||
}, | ||
getTexParameter: function getTexParameter(gl, originalFunc, target, pname) { | ||
switch (pname) { | ||
case 34046: | ||
var extensions = gl.luma.extensions; | ||
var ext = extensions[EXT_texture_filter_anisotropic]; | ||
pname = ext && ext.TEXTURE_MAX_ANISOTROPY_EXT || 34046; | ||
break; | ||
default: | ||
} | ||
return originalFunc(target, pname); | ||
}, | ||
getParameter: getParameterPolyfill, | ||
hint: function hint(gl, originalFunc, pname, value) { | ||
// TODO - handle GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// switch (pname) { | ||
// case GL.FRAGMENT_SHADER_DERIVATIVE_HINT: | ||
// } | ||
return originalFunc(pname, value); | ||
} | ||
}; | ||
}), _WEBGL_CONTEXT_POLYFI); | ||
function initializeExtensions(gl) { | ||
gl.luma.extensions = {}; | ||
// `getSupportedExtensions` can return null when context is lost. | ||
const EXTENSIONS = gl.getSupportedExtensions() || []; | ||
for (const extension of EXTENSIONS) { | ||
gl.luma[extension] = gl.getExtension(extension); | ||
} | ||
} | ||
gl.luma.extensions = {}; // `getSupportedExtensions` can return null when context is lost. | ||
// Polyfills a single WebGL extension into the `target` object | ||
function polyfillExtension(gl, { extension, target, target2 }) { | ||
const defaults = WEBGL_CONTEXT_POLYFILLS[extension]; | ||
assert(defaults); | ||
var EXTENSIONS = gl.getSupportedExtensions() || []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
const { meta = {} } = defaults; | ||
const { suffix = '' } = meta; | ||
try { | ||
for (var _iterator = EXTENSIONS[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var extension = _step.value; | ||
gl.luma[extension] = gl.getExtension(extension); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} // Polyfills a single WebGL extension into the `target` object | ||
const ext = gl.getExtension(extension); | ||
Object.keys(defaults).forEach(key => { | ||
const extKey = `${key}${suffix}`; | ||
function polyfillExtension(gl, _ref) { | ||
var extension = _ref.extension, | ||
target = _ref.target, | ||
target2 = _ref.target2; | ||
var defaults = WEBGL_CONTEXT_POLYFILLS[extension]; | ||
assert(defaults); | ||
var _defaults$meta = defaults.meta, | ||
meta = _defaults$meta === void 0 ? {} : _defaults$meta; | ||
var _meta$suffix = meta.suffix, | ||
suffix = _meta$suffix === void 0 ? '' : _meta$suffix; | ||
var ext = gl.getExtension(extension); | ||
Object.keys(defaults).forEach(function (key) { | ||
var extKey = "".concat(key).concat(suffix); | ||
var polyfill = null; | ||
let polyfill = null; | ||
if (key === 'meta') { | ||
// ignore | ||
} else if (typeof gl[key] === 'function') { | ||
// WebGL2 implementation is already | ||
if (key === 'meta') {// ignore | ||
} else if (typeof gl[key] === 'function') {// WebGL2 implementation is already | ||
} else if (ext && typeof ext[extKey] === 'function') { | ||
// pick extension implemenentation,if available | ||
polyfill = (...args) => ext[extKey](...args); | ||
polyfill = function polyfill() { | ||
return ext[extKey].apply(ext, arguments); | ||
}; | ||
} else if (typeof defaults[key] === 'function') { | ||
@@ -233,12 +266,14 @@ // pick the mock implementation, if no implementation was detected | ||
}); | ||
} | ||
} // Install simple overrides (mostly get* functions) | ||
// Install simple overrides (mostly get* functions) | ||
function installOverrides(gl, { target, target2 }) { | ||
const { OVERRIDES } = WEBGL_CONTEXT_POLYFILLS; | ||
Object.keys(OVERRIDES).forEach(key => { | ||
function installOverrides(gl, _ref2) { | ||
var target = _ref2.target, | ||
target2 = _ref2.target2; | ||
var OVERRIDES = WEBGL_CONTEXT_POLYFILLS.OVERRIDES; | ||
Object.keys(OVERRIDES).forEach(function (key) { | ||
if (typeof OVERRIDES[key] === 'function') { | ||
// install an override, if no implementation was detected | ||
const originalFunc = gl[key] ? gl[key].bind(gl) : () => {}; | ||
const polyfill = OVERRIDES[key].bind(null, gl, originalFunc); | ||
var originalFunc = gl[key] ? gl[key].bind(gl) : function () {}; | ||
var polyfill = OVERRIDES[key].bind(null, gl, originalFunc); | ||
target[key] = polyfill; | ||
@@ -248,5 +283,5 @@ target2[key] = polyfill; | ||
}); | ||
} | ||
} // Registers polyfill or mock functions for all known extensions | ||
// Registers polyfill or mock functions for all known extensions | ||
export default function polyfillContext(gl) { | ||
@@ -256,17 +291,27 @@ // polyfillVertexArrayObject(WebGLRenderingContext, gl); | ||
initializeExtensions(gl); | ||
if (!gl.luma.polyfilled) { | ||
for (const extension in WEBGL_CONTEXT_POLYFILLS) { | ||
for (var extension in WEBGL_CONTEXT_POLYFILLS) { | ||
if (extension !== 'overrides') { | ||
polyfillExtension(gl, { extension, target: gl.luma, target2: gl }); | ||
polyfillExtension(gl, { | ||
extension: extension, | ||
target: gl.luma, | ||
target2: gl | ||
}); | ||
} | ||
} | ||
installOverrides(gl, { target: gl.luma, target2: gl }); | ||
installOverrides(gl, { | ||
target: gl.luma, | ||
target2: gl | ||
}); | ||
gl.luma.polyfilled = true; | ||
} | ||
return gl; | ||
} | ||
/* global window, global */ | ||
/* global window, global */ | ||
const global_ = typeof global !== 'undefined' ? global : window; | ||
var global_ = typeof global !== 'undefined' ? global : window; | ||
global_.polyfillContext = polyfillContext; | ||
//# sourceMappingURL=polyfill-context.js.map |
@@ -1,121 +0,82 @@ | ||
/* eslint-disable no-inline-comments, max-len, camelcase */ | ||
import GL from '@luma.gl/constants'; | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
const OES_element_index = 'OES_element_index'; | ||
const WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
const EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
const EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
const EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
const WEBGL_debug_renderer_info = 'WEBGL_debug_renderer_info'; | ||
var _WEBGL_PARAMETERS; | ||
const GL_FRAGMENT_SHADER_DERIVATIVE_HINT = 0x8b8b; | ||
const GL_DONT_CARE = 0x1100; | ||
const GL_GPU_DISJOINT_EXT = 0x8fbb; | ||
const GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84ff; | ||
const GL_UNMASKED_VENDOR_WEBGL = 0x9245; // vendor string of the graphics driver. | ||
const GL_UNMASKED_RENDERER_WEBGL = 0x9246; // renderer string of the graphics driver. | ||
/* eslint-disable no-inline-comments, max-len, camelcase */ | ||
var OES_element_index = 'OES_element_index'; | ||
var WEBGL_draw_buffers = 'WEBGL_draw_buffers'; | ||
var EXT_disjoint_timer_query = 'EXT_disjoint_timer_query'; | ||
var EXT_disjoint_timer_query_webgl2 = 'EXT_disjoint_timer_query_webgl2'; | ||
var EXT_texture_filter_anisotropic = 'EXT_texture_filter_anisotropic'; | ||
var WEBGL_debug_renderer_info = 'WEBGL_debug_renderer_info'; | ||
var GL_FRAGMENT_SHADER_DERIVATIVE_HINT = 0x8b8b; | ||
var GL_DONT_CARE = 0x1100; | ||
var GL_GPU_DISJOINT_EXT = 0x8fbb; | ||
var GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84ff; | ||
var GL_UNMASKED_VENDOR_WEBGL = 0x9245; // vendor string of the graphics driver. | ||
const getWebGL2ValueOrZero = gl => !isWebGL2(gl) ? 0 : undefined; | ||
var GL_UNMASKED_RENDERER_WEBGL = 0x9246; // renderer string of the graphics driver. | ||
// if a function returns undefined in this table, | ||
var getWebGL2ValueOrZero = function getWebGL2ValueOrZero(gl) { | ||
return !isWebGL2(gl) ? 0 : undefined; | ||
}; // if a function returns undefined in this table, | ||
// the original getParameter will be called, defeating the override | ||
const WEBGL_PARAMETERS = { | ||
[GL.READ_BUFFER]: gl => !isWebGL2(gl) ? GL.COLOR_ATTACHMENT0 : undefined, | ||
// WebGL2 context parameters | ||
[GL_FRAGMENT_SHADER_DERIVATIVE_HINT]: gl => !isWebGL2(gl) ? GL_DONT_CARE : undefined, | ||
[GL.RASTERIZER_DISCARD]: getWebGL2ValueOrZero, | ||
var WEBGL_PARAMETERS = (_WEBGL_PARAMETERS = {}, _defineProperty(_WEBGL_PARAMETERS, 3074, function (gl) { | ||
return !isWebGL2(gl) ? 36064 : undefined; | ||
}), _defineProperty(_WEBGL_PARAMETERS, GL_FRAGMENT_SHADER_DERIVATIVE_HINT, function (gl) { | ||
return !isWebGL2(gl) ? GL_DONT_CARE : undefined; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 35977, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 32937, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, GL_GPU_DISJOINT_EXT, function (gl, getParameter) { | ||
var ext = isWebGL2(gl) ? gl.getExtension(EXT_disjoint_timer_query_webgl2) : gl.getExtension(EXT_disjoint_timer_query); | ||
return ext && ext.GPU_DISJOINT_EXT ? getParameter(ext.GPU_DISJOINT_EXT) : 0; | ||
}), _defineProperty(_WEBGL_PARAMETERS, GL_UNMASKED_VENDOR_WEBGL, function (gl, getParameter) { | ||
var ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_VENDOR_WEBGL || 7936); | ||
}), _defineProperty(_WEBGL_PARAMETERS, GL_UNMASKED_RENDERER_WEBGL, function (gl, getParameter) { | ||
var ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_RENDERER_WEBGL || 7937); | ||
}), _defineProperty(_WEBGL_PARAMETERS, GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, function (gl, getParameter) { | ||
var ext = gl.luma.extensions[EXT_texture_filter_anisotropic]; | ||
return ext ? getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1.0; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 32883, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35071, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 37447, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 36063, function (gl, getParameter) { | ||
if (!isWebGL2(gl)) { | ||
var ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL) : 0; | ||
} | ||
[GL.SAMPLES]: getWebGL2ValueOrZero, | ||
return undefined; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 35379, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35374, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35377, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 34852, function (gl) { | ||
if (!isWebGL2(gl)) { | ||
var ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? ext.MAX_DRAW_BUFFERS_WEBGL : 0; | ||
} | ||
// WebGL2 extension context parameters | ||
[GL_GPU_DISJOINT_EXT]: (gl, getParameter) => { | ||
const ext = isWebGL2(gl) ? gl.getExtension(EXT_disjoint_timer_query_webgl2) : gl.getExtension(EXT_disjoint_timer_query); | ||
return ext && ext.GPU_DISJOINT_EXT ? getParameter(ext.GPU_DISJOINT_EXT) : 0; | ||
}, | ||
return undefined; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 36203, // Guess: per webglstats.com 99.6% of webgl2 supports 2147483647 | ||
function (gl) { | ||
return gl.getExtension(OES_element_index) ? 2147483647 : 65535; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 33001, // Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
function (gl) { | ||
return gl.getExtension(OES_element_index) ? 16777216 : 65535; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 33000, // Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
function (gl) { | ||
return 16777216; | ||
}), _defineProperty(_WEBGL_PARAMETERS, 37157, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35373, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35657, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 36183, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 37137, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 34045, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35978, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35979, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35968, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35376, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35375, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35659, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 37154, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35371, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35658, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35076, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35077, getWebGL2ValueOrZero), _defineProperty(_WEBGL_PARAMETERS, 35380, getWebGL2ValueOrZero), _WEBGL_PARAMETERS); // Return true if WebGL2 context | ||
// Extension fixed values | ||
[GL_UNMASKED_VENDOR_WEBGL]: (gl, getParameter) => { | ||
const ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_VENDOR_WEBGL || GL.VENDOR); | ||
}, | ||
function isWebGL2(gl) { | ||
var GL_TEXTURE_BINDING_3D = 0x806a; | ||
return gl && 32874 === GL_TEXTURE_BINDING_3D; | ||
} // A "replacement" gl.getParameter that accepts "enums" from extensions and WebGL2 | ||
// and returns reasonably safe defaults | ||
[GL_UNMASKED_RENDERER_WEBGL]: (gl, getParameter) => { | ||
const ext = gl.getExtension(WEBGL_debug_renderer_info); | ||
return getParameter(ext && ext.UNMASKED_RENDERER_WEBGL || GL.RENDERER); | ||
}, | ||
// Extension LIMITS | ||
[GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT]: (gl, getParameter) => { | ||
const ext = gl.luma.extensions[EXT_texture_filter_anisotropic]; | ||
return ext ? getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1.0; | ||
}, | ||
// WebGL2 Limits | ||
[GL.MAX_3D_TEXTURE_SIZE]: getWebGL2ValueOrZero, | ||
[GL.MAX_ARRAY_TEXTURE_LAYERS]: getWebGL2ValueOrZero, | ||
[GL.MAX_CLIENT_WAIT_TIMEOUT_WEBGL]: getWebGL2ValueOrZero, | ||
[GL.MAX_COLOR_ATTACHMENTS]: (gl, getParameter) => { | ||
if (!isWebGL2(gl)) { | ||
const ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL) : 0; | ||
} | ||
return undefined; | ||
}, | ||
[GL.MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_COMBINED_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_DRAW_BUFFERS]: gl => { | ||
if (!isWebGL2(gl)) { | ||
const ext = gl.getExtension(WEBGL_draw_buffers); | ||
return ext ? ext.MAX_DRAW_BUFFERS_WEBGL : 0; | ||
} | ||
return undefined; | ||
}, | ||
[GL.MAX_ELEMENT_INDEX]: | ||
// Guess: per webglstats.com 99.6% of webgl2 supports 2147483647 | ||
gl => gl.getExtension(OES_element_index) ? 2147483647 : 65535, | ||
[GL.MAX_ELEMENTS_INDICES]: | ||
// Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
gl => gl.getExtension(OES_element_index) ? 16777216 : 65535, | ||
[GL.MAX_ELEMENTS_VERTICES]: | ||
// Guess: "Reasonably safe" per webglstats.com - could be higher/lower (on some mobile devices) | ||
gl => 16777216, | ||
[GL.MAX_FRAGMENT_INPUT_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_FRAGMENT_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_FRAGMENT_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_SAMPLES]: getWebGL2ValueOrZero, | ||
[GL.MAX_SERVER_WAIT_TIMEOUT]: getWebGL2ValueOrZero, | ||
[GL.MAX_TEXTURE_LOD_BIAS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS]: getWebGL2ValueOrZero, | ||
[GL.MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_UNIFORM_BLOCK_SIZE]: getWebGL2ValueOrZero, | ||
[GL.MAX_UNIFORM_BUFFER_BINDINGS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VARYING_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_OUTPUT_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_UNIFORM_BLOCKS]: getWebGL2ValueOrZero, | ||
[GL.MAX_VERTEX_UNIFORM_COMPONENTS]: getWebGL2ValueOrZero, | ||
[GL.MIN_PROGRAM_TEXEL_OFFSET]: getWebGL2ValueOrZero, | ||
[GL.MAX_PROGRAM_TEXEL_OFFSET]: getWebGL2ValueOrZero, | ||
[GL.UNIFORM_BUFFER_OFFSET_ALIGNMENT]: getWebGL2ValueOrZero | ||
}; | ||
// Return true if WebGL2 context | ||
function isWebGL2(gl) { | ||
const GL_TEXTURE_BINDING_3D = 0x806a; | ||
return gl && gl.TEXTURE_BINDING_3D === GL_TEXTURE_BINDING_3D; | ||
} | ||
// A "replacement" gl.getParameter that accepts "enums" from extensions and WebGL2 | ||
// and returns reasonably safe defaults | ||
export function getParameterPolyfill(gl, originalGetParameter, pname) { | ||
// Return mock limits (usually 0) for WebGL2 constants to ensure these | ||
// can be queries without error | ||
const limit = WEBGL_PARAMETERS[pname]; | ||
const value = typeof limit === 'function' ? limit(gl, originalGetParameter, pname) : limit; | ||
const result = value !== undefined ? value : originalGetParameter(pname); | ||
var limit = WEBGL_PARAMETERS[pname]; | ||
var value = typeof limit === 'function' ? limit(gl, originalGetParameter, pname) : limit; | ||
var result = value !== undefined ? value : originalGetParameter(pname); | ||
return result; | ||
} | ||
//# sourceMappingURL=polyfill-get-parameter.js.map |
{ | ||
"name": "@luma.gl/webgl2-polyfill", | ||
"version": "7.0.0-alpha.8", | ||
"version": "7.0.0-alpha.11", | ||
"description": "WebGL2 API Polyfills for WebGL1 WebGLRenderingContext", | ||
@@ -28,7 +28,3 @@ "license": "MIT", | ||
"build-esm": "BABEL_ENV=esm babel src --config-file ../../babel.config.js --out-dir dist/esm --source-maps --ignore 'node_modules/'", | ||
"build-es5": "BABEL_ENV=es5 babel src --config-file ../../babel.config.js --out-dir dist/es5 --source-maps --ignore 'node_modules/'", | ||
"cover": "../../scripts/test.sh cover", | ||
"prepublishOnly": "npm run build", | ||
"publish-prod": "npm run build && npm run test-fast && npm publish", | ||
"publish-beta": "npm run build && npm run test-fast && npm publish --tag beta" | ||
"build-es5": "BABEL_ENV=es5 babel src --config-file ../../babel.config.js --out-dir dist/es5 --source-maps --ignore 'node_modules/'" | ||
}, | ||
@@ -35,0 +31,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
142927
1428