@luma.gl/shadertools
Version:
Shader module system for luma.gl
120 lines (104 loc) • 3.3 kB
JavaScript
import {getContextInfo, hasFeatures, canCompileGLGSExtension, FEATURES} from '../utils/webgl-info';
export function getPlatformShaderDefines(gl) {
const debugInfo = getContextInfo(gl);
switch (debugInfo.gpuVendor.toLowerCase()) {
case 'nvidia':
return `\
// Nvidia optimizes away the calculation necessary for emulated fp64
`;
case 'intel':
return `\
// Intel optimizes away the calculation necessary for emulated fp64
// Intel's built-in 'tan' function doesn't have acceptable precision
// Intel GPU doesn't have full 32 bits precision in same cases, causes overflow
`;
case 'amd':
// AMD Does not eliminate fp64 code
return `\
`;
default:
// We don't know what GPU it is, could be that the GPU driver or
// browser is not implementing UNMASKED_RENDERER constant and not
// reporting a correct name
return `\
// Prevent driver from optimizing away the calculation necessary for emulated fp64
// Intel's built-in 'tan' function doesn't have acceptable precision
// Intel GPU doesn't have full 32 bits precision in same cases, causes overflow
`;
}
}
export function getVersionDefines(gl, glslVersion, isFragment) {
// Add shadertools defines to let shaders portably v1/v3 check for features
let versionDefines = `\
// DEPRECATED FLAGS, remove in v9
`;
if (hasFeatures(gl, FEATURES.GLSL_FRAG_DEPTH)) {
versionDefines += `\
// FRAG_DEPTH => gl_FragDepth is available
`;
}
if (
hasFeatures(gl, FEATURES.GLSL_DERIVATIVES) &&
canCompileGLGSExtension(gl, FEATURES.GLSL_DERIVATIVES)
) {
versionDefines += `\
// DERIVATIVES => dxdF, dxdY and fwidth are available
`;
}
if (
hasFeatures(gl, FEATURES.GLSL_FRAG_DATA) &&
canCompileGLGSExtension(gl, FEATURES.GLSL_FRAG_DATA, {behavior: 'require'})
) {
versionDefines += `\
// DRAW_BUFFERS => gl_FragData[] is available
`;
}
if (hasFeatures(gl, FEATURES.GLSL_TEXTURE_LOD)) {
versionDefines += `\
// TEXTURE_LOD => texture2DLod etc are available
`;
}
return versionDefines;
}