@luma.gl/engine
Version:
3D Engine Components for luma.gl
117 lines (103 loc) • 3.36 kB
JavaScript
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/**
* Gets fragment shader source for a shader pass sub pass
* @param options
* @returns
*/
export function getFragmentShaderForRenderPass(options) {
const { shaderPass, action, shadingLanguage } = options;
switch (action) {
case 'filter':
const filterFunc = `${shaderPass.name}_filterColor_ext`;
return shadingLanguage === 'wgsl'
? getFilterShaderWGSL(filterFunc)
: getFilterShaderGLSL(filterFunc);
case 'sample':
const samplerFunc = `${shaderPass.name}_sampleColor`;
return shadingLanguage === 'wgsl'
? getSamplerShaderWGSL(samplerFunc)
: getSamplerShaderGLSL(samplerFunc);
default:
throw new Error(`${shaderPass.name} no fragment shader generated for shader pass`);
}
}
/** Get a filtering WGSL fragment shader */
function getFilterShaderWGSL(func) {
return /* wgsl */ `\
// Binding 0:1 is reserved for shader passes
var<uniform> brightnessContrast : brightnessContrastUniforms;
var texture: texture_2d<f32>;
var sampler: sampler;
struct FragmentInputs = {
fragUV: vec2f,
fragPosition: vec4f,
fragCoordinate: vec4f
};
fn fragmentMain(inputs: FragmentInputs) -> vec4f {
let texSize = textureDimensions(texture, 0);
var fragColor = textureSample(texture, sampler, fragUV);
fragColor = ${func}(gl_FragColor, texSize, texCoord);
return fragColor;
}
`;
}
/** Get a sampling WGSL fragment shader */
function getSamplerShaderWGSL(func) {
return /* wgsl */ `\
// Binding 0:1 is reserved for shader passes
var<uniform> brightnessContrast : brightnessContrastUniforms;
var texture: texture_2d<f32>;
var sampler: sampler;
struct FragmentInputs = {
fragUV: vec2f,
fragPosition: vec4f,
fragCoordinate: vec4f
};
fn fragmentMain(inputs: FragmentInputs) -> vec4f {
let texSize = textureDimensions(texture, 0);
var fragColor = textureSample(texture, sampler, fragUV);
fragColor = ${func}(gl_FragColor, texSize, texCoord);
return fragColor;
}
`;
}
/** Get a filtering GLSL fragment shader */
function getFilterShaderGLSL(func) {
return /* glsl */ `\
#version 300 es
uniform sampler2D sourceTexture;
in vec2 position;
in vec2 coordinate;
in vec2 uv;
out vec4 fragColor;
void main() {
vec2 texCoord = coordinate;
ivec2 iTexSize = textureSize(sourceTexture, 0);
vec2 texSize = vec2(float(iTexSize.x), float(iTexSize.y));
fragColor = texture(sourceTexture, texCoord);
fragColor = ${func}(fragColor, texSize, texCoord);
}
`;
}
/** Get a sampling GLSL fragment shader */
function getSamplerShaderGLSL(func) {
return /* glsl */ `\
#version 300 es
uniform sampler2D sourceTexture;
in vec2 position;
in vec2 coordinate;
in vec2 uv;
out vec4 fragColor;
void main() {
vec2 texCoord = coordinate;
ivec2 iTexSize = textureSize(sourceTexture, 0);
vec2 texSize = vec2(float(iTexSize.x), float(iTexSize.y));
fragColor = ${func}(sourceTexture, texSize, texCoord);
}
`;
}
//# sourceMappingURL=get-fragment-shader.js.map