@luma.gl/engine
Version:
3D Engine Components for luma.gl
130 lines (107 loc) • 3.42 kB
text/typescript
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {ShaderPass} from '@luma.gl/shadertools';
/**
* Gets fragment shader source for a shader pass sub pass
* @param options
* @returns
*/
export function getFragmentShaderForRenderPass(options: {
shaderPass: ShaderPass;
action: 'filter' | 'sample';
shadingLanguage: 'wgsl' | 'glsl';
}): string {
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: string) {
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: string) {
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: string) {
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: string) {
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);
}
`;
}