@luma.gl/gltools
Version:
WebGL2 API Polyfills for WebGL1 WebGLRenderingContext
372 lines (358 loc) • 11.5 kB
JavaScript
import { isWebGL2 } from '../utils/webgl-checks';
export const GL_PARAMETER_DEFAULTS = {
[]: false,
[]: new Float32Array([0, 0, 0, 0]),
[]: 32774,
[]: 32774,
[]: 1,
[]: 0,
[]: 1,
[]: 0,
[]: new Float32Array([0, 0, 0, 0]),
[]: [true, true, true, true],
[]: false,
[]: 1029,
[]: false,
[]: 1,
[]: 513,
[]: new Float32Array([0, 1]),
[]: true,
[]: true,
[]: null,
[]: 2305,
[]: 4352,
[]: 1,
[]: false,
[]: 0,
[]: 0,
[]: 1.0,
[]: false,
[]: false,
[]: new Int32Array([0, 0, 1024, 1024]),
[]: false,
[]: 0,
[]: 0xffffffff,
[]: 0xffffffff,
[]: 519,
[]: 0,
[]: 0xffffffff,
[]: 519,
[]: 0,
[]: 0xffffffff,
[]: 7680,
[]: 7680,
[]: 7680,
[]: 7680,
[]: 7680,
[]: 7680,
[]: [0, 0, 1024, 1024],
[]: 4,
[]: 4,
[]: false,
[]: false,
[]: 37444,
[]: 4352,
[]: null,
[]: false,
[]: 0,
[]: 0,
[]: 0,
[]: 0,
[]: 0,
[]: 0,
[]: 0,
[]: 0
};
const enable = (gl, value, key) => value ? gl.enable(key) : gl.disable(key);
const hint = (gl, value, key) => gl.hint(key, value);
const pixelStorei = (gl, value, key) => gl.pixelStorei(key, value);
const drawFramebuffer = (gl, value) => {
const target = isWebGL2(gl) ? 36009 : 36160;
return gl.bindFramebuffer(target, value);
};
const readFramebuffer = (gl, value) => {
return gl.bindFramebuffer(36008, value);
};
function isArray(array) {
return Array.isArray(array) || ArrayBuffer.isView(array);
}
export const GL_PARAMETER_SETTERS = {
[]: enable,
[]: (gl, value) => gl.blendColor(...value),
[]: 'blendEquation',
[]: 'blendEquation',
[]: 'blendFunc',
[]: 'blendFunc',
[]: 'blendFunc',
[]: 'blendFunc',
[]: (gl, value) => gl.clearColor(...value),
[]: (gl, value) => gl.colorMask(...value),
[]: enable,
[]: (gl, value) => gl.cullFace(value),
[]: enable,
[]: (gl, value) => gl.clearDepth(value),
[]: (gl, value) => gl.depthFunc(value),
[]: (gl, value) => gl.depthRange(...value),
[]: (gl, value) => gl.depthMask(value),
[]: enable,
[]: hint,
[]: drawFramebuffer,
[]: (gl, value) => gl.frontFace(value),
[]: hint,
[]: (gl, value) => gl.lineWidth(value),
[]: enable,
[]: 'polygonOffset',
[]: 'polygonOffset',
[]: enable,
[]: 'sampleCoverage',
[]: 'sampleCoverage',
[]: enable,
[]: (gl, value) => gl.scissor(...value),
[]: enable,
[]: (gl, value) => gl.clearStencil(value),
[]: (gl, value) => gl.stencilMaskSeparate(1028, value),
[]: (gl, value) => gl.stencilMaskSeparate(1029, value),
[]: 'stencilFuncFront',
[]: 'stencilFuncFront',
[]: 'stencilFuncFront',
[]: 'stencilFuncBack',
[]: 'stencilFuncBack',
[]: 'stencilFuncBack',
[]: 'stencilOpFront',
[]: 'stencilOpFront',
[]: 'stencilOpFront',
[]: 'stencilOpBack',
[]: 'stencilOpBack',
[]: 'stencilOpBack',
[]: (gl, value) => gl.viewport(...value),
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: readFramebuffer,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
[]: pixelStorei,
framebuffer: (gl, framebuffer) => {
const handle = framebuffer && 'handle' in framebuffer ? framebuffer.handle : framebuffer;
return gl.bindFramebuffer(36160, handle);
},
blend: (gl, value) => value ? gl.enable(3042) : gl.disable(3042),
blendColor: (gl, value) => gl.blendColor(...value),
blendEquation: (gl, args) => {
args = isArray(args) ? args : [args, args];
gl.blendEquationSeparate(...args);
},
blendFunc: (gl, args) => {
args = isArray(args) && args.length === 2 ? [...args, ...args] : args;
gl.blendFuncSeparate(...args);
},
clearColor: (gl, value) => gl.clearColor(...value),
clearDepth: (gl, value) => gl.clearDepth(value),
clearStencil: (gl, value) => gl.clearStencil(value),
colorMask: (gl, value) => gl.colorMask(...value),
cull: (gl, value) => value ? gl.enable(2884) : gl.disable(2884),
cullFace: (gl, value) => gl.cullFace(value),
depthTest: (gl, value) => value ? gl.enable(2929) : gl.disable(2929),
depthFunc: (gl, value) => gl.depthFunc(value),
depthMask: (gl, value) => gl.depthMask(value),
depthRange: (gl, value) => gl.depthRange(...value),
dither: (gl, value) => value ? gl.enable(3024) : gl.disable(3024),
derivativeHint: (gl, value) => {
gl.hint(35723, value);
},
frontFace: (gl, value) => gl.frontFace(value),
mipmapHint: (gl, value) => gl.hint(33170, value),
lineWidth: (gl, value) => gl.lineWidth(value),
polygonOffsetFill: (gl, value) => value ? gl.enable(32823) : gl.disable(32823),
polygonOffset: (gl, value) => gl.polygonOffset(...value),
sampleCoverage: (gl, value) => gl.sampleCoverage(...value),
scissorTest: (gl, value) => value ? gl.enable(3089) : gl.disable(3089),
scissor: (gl, value) => gl.scissor(...value),
stencilTest: (gl, value) => value ? gl.enable(2960) : gl.disable(2960),
stencilMask: (gl, value) => {
value = isArray(value) ? value : [value, value];
const [mask, backMask] = value;
gl.stencilMaskSeparate(1028, mask);
gl.stencilMaskSeparate(1029, backMask);
},
stencilFunc: (gl, args) => {
args = isArray(args) && args.length === 3 ? [...args, ...args] : args;
const [func, ref, mask, backFunc, backRef, backMask] = args;
gl.stencilFuncSeparate(1028, func, ref, mask);
gl.stencilFuncSeparate(1029, backFunc, backRef, backMask);
},
stencilOp: (gl, args) => {
args = isArray(args) && args.length === 3 ? [...args, ...args] : args;
const [sfail, dpfail, dppass, backSfail, backDpfail, backDppass] = args;
gl.stencilOpSeparate(1028, sfail, dpfail, dppass);
gl.stencilOpSeparate(1029, backSfail, backDpfail, backDppass);
},
viewport: (gl, value) => gl.viewport(...value)
};
function getValue(glEnum, values, cache) {
return values[glEnum] !== undefined ? values[glEnum] : cache[glEnum];
}
export const GL_COMPOSITE_PARAMETER_SETTERS = {
blendEquation: (gl, values, cache) => gl.blendEquationSeparate(getValue(32777, values, cache), getValue(34877, values, cache)),
blendFunc: (gl, values, cache) => gl.blendFuncSeparate(getValue(32969, values, cache), getValue(32968, values, cache), getValue(32971, values, cache), getValue(32970, values, cache)),
polygonOffset: (gl, values, cache) => gl.polygonOffset(getValue(32824, values, cache), getValue(10752, values, cache)),
sampleCoverage: (gl, values, cache) => gl.sampleCoverage(getValue(32938, values, cache), getValue(32939, values, cache)),
stencilFuncFront: (gl, values, cache) => gl.stencilFuncSeparate(1028, getValue(2962, values, cache), getValue(2967, values, cache), getValue(2963, values, cache)),
stencilFuncBack: (gl, values, cache) => gl.stencilFuncSeparate(1029, getValue(34816, values, cache), getValue(36003, values, cache), getValue(36004, values, cache)),
stencilOpFront: (gl, values, cache) => gl.stencilOpSeparate(1028, getValue(2964, values, cache), getValue(2965, values, cache), getValue(2966, values, cache)),
stencilOpBack: (gl, values, cache) => gl.stencilOpSeparate(1029, getValue(34817, values, cache), getValue(34818, values, cache), getValue(34819, values, cache))
};
export const GL_HOOKED_SETTERS = {
enable: (update, capability) => update({
[]: true
}),
disable: (update, capability) => update({
[]: false
}),
pixelStorei: (update, pname, value) => update({
[]: value
}),
hint: (update, pname, hint) => update({
[]: hint
}),
bindFramebuffer: (update, target, framebuffer) => {
switch (target) {
case 36160:
return update({
[]: framebuffer,
[]: framebuffer
});
case 36009:
return update({
[]: framebuffer
});
case 36008:
return update({
[]: framebuffer
});
default:
return null;
}
},
blendColor: (update, r, g, b, a) => update({
[]: new Float32Array([r, g, b, a])
}),
blendEquation: (update, mode) => update({
[]: mode,
[]: mode
}),
blendEquationSeparate: (update, modeRGB, modeAlpha) => update({
[]: modeRGB,
[]: modeAlpha
}),
blendFunc: (update, src, dst) => update({
[]: src,
[]: dst,
[]: src,
[]: dst
}),
blendFuncSeparate: (update, srcRGB, dstRGB, srcAlpha, dstAlpha) => update({
[]: srcRGB,
[]: dstRGB,
[]: srcAlpha,
[]: dstAlpha
}),
clearColor: (update, r, g, b, a) => update({
[]: new Float32Array([r, g, b, a])
}),
clearDepth: (update, depth) => update({
[]: depth
}),
clearStencil: (update, s) => update({
[]: s
}),
colorMask: (update, r, g, b, a) => update({
[]: [r, g, b, a]
}),
cullFace: (update, mode) => update({
[]: mode
}),
depthFunc: (update, func) => update({
[]: func
}),
depthRange: (update, zNear, zFar) => update({
[]: new Float32Array([zNear, zFar])
}),
depthMask: (update, mask) => update({
[]: mask
}),
frontFace: (update, face) => update({
[]: face
}),
lineWidth: (update, width) => update({
[]: width
}),
polygonOffset: (update, factor, units) => update({
[]: factor,
[]: units
}),
sampleCoverage: (update, value, invert) => update({
[]: value,
[]: invert
}),
scissor: (update, x, y, width, height) => update({
[]: new Int32Array([x, y, width, height])
}),
stencilMask: (update, mask) => update({
[]: mask,
[]: mask
}),
stencilMaskSeparate: (update, face, mask) => update({
[]: mask
}),
stencilFunc: (update, func, ref, mask) => update({
[]: func,
[]: ref,
[]: mask,
[]: func,
[]: ref,
[]: mask
}),
stencilFuncSeparate: (update, face, func, ref, mask) => update({
[]: func,
[]: ref,
[]: mask
}),
stencilOp: (update, fail, zfail, zpass) => update({
[]: fail,
[]: zfail,
[]: zpass,
[]: fail,
[]: zfail,
[]: zpass
}),
stencilOpSeparate: (update, face, fail, zfail, zpass) => update({
[]: fail,
[]: zfail,
[]: zpass
}),
viewport: (update, x, y, width, height) => update({
[]: [x, y, width, height]
})
};
const isEnabled = (gl, key) => gl.isEnabled(key);
export const GL_PARAMETER_GETTERS = {
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled,
[]: isEnabled
};
//# sourceMappingURL=webgl-parameter-tables.js.map