Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(shadertools): Support boolean shader defines only (v9.2) #2190

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/engine/src/compute/computation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type ComputationProps = Omit<ComputePipelineProps, 'shader'> & {
/** shadertool shader modules (added to shader code) */
modules?: ShaderModule[];
/** Shadertool module defines (configures shader code)*/
defines?: Record<string, string | number | boolean>;
defines?: Record<string, boolean>;
// TODO - injections, hooks etc?

/** Shader inputs, used to generated uniform buffers and bindings */
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type ModelProps = Omit<RenderPipelineProps, 'vs' | 'fs' | 'bindings'> & {
/** shadertool shader modules (added to shader code) */
modules?: ShaderModule[];
/** Shadertool module defines (configures shader code)*/
defines?: Record<string, string | number | boolean>;
defines?: Record<string, boolean>;
// TODO - injections, hooks etc?

/** Shader inputs, used to generated uniform buffers and bindings */
Expand Down
36 changes: 18 additions & 18 deletions modules/gltf/src/pbr/parse-pbr-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ParsePBRMaterialOptions = {
};

export type ParsedPBRMaterial = {
readonly defines: Record<string, number | boolean>;
readonly defines: Record<string, boolean>;
readonly bindings: Partial<PBRMaterialBindings>;
readonly uniforms: Partial<PBRProjectionProps & PBRMaterialUniforms>;
readonly parameters: Parameters;
Expand Down Expand Up @@ -53,8 +53,8 @@ export function parsePBRMaterial(
const parsedMaterial: ParsedPBRMaterial = {
defines: {
// TODO: Use EXT_sRGB if available (Standard in WebGL 2.0)
MANUAL_SRGB: 1,
SRGB_FAST_APPROXIMATION: 1
MANUAL_SRGB: true,
SRGB_FAST_APPROXIMATION: true
},
bindings: {},
uniforms: {
Expand All @@ -69,7 +69,7 @@ export function parsePBRMaterial(
};

// TODO - always available
parsedMaterial.defines.USE_TEX_LOD = 1;
parsedMaterial.defines.USE_TEX_LOD = true;

const {imageBasedLightingEnvironment} = options;
if (imageBasedLightingEnvironment) {
Expand All @@ -82,18 +82,18 @@ export function parsePBRMaterial(
}

if (options?.pbrDebug) {
parsedMaterial.defines.PBR_DEBUG = 1;
parsedMaterial.defines.PBR_DEBUG = true;
// Override final color for reference app visualization of various parameters in the lighting equation.
parsedMaterial.uniforms.scaleDiffBaseMR = [0, 0, 0, 0];
parsedMaterial.uniforms.scaleFGDSpec = [0, 0, 0, 0];
}

if (attributes.NORMAL) parsedMaterial.defines.HAS_NORMALS = 1;
if (attributes.TANGENT && options?.useTangents) parsedMaterial.defines.HAS_TANGENTS = 1;
if (attributes.TEXCOORD_0) parsedMaterial.defines.HAS_UV = 1;
if (attributes.NORMAL) parsedMaterial.defines.HAS_NORMALS = true;
if (attributes.TANGENT && options?.useTangents) parsedMaterial.defines.HAS_TANGENTS = true;
if (attributes.TEXCOORD_0) parsedMaterial.defines.HAS_UV = true;

if (options?.imageBasedLightingEnvironment) parsedMaterial.defines.USE_IBL = 1;
if (options?.lights) parsedMaterial.defines.USE_LIGHTS = 1;
if (options?.imageBasedLightingEnvironment) parsedMaterial.defines.USE_IBL = true;
if (options?.lights) parsedMaterial.defines.USE_LIGHTS = true;

if (material) {
parseMaterial(device, material, parsedMaterial);
Expand Down Expand Up @@ -147,7 +147,7 @@ function parseMaterial(device: Device, material, parsedMaterial: ParsedPBRMateri
switch (material.alphaMode) {
case 'MASK':
const {alphaCutoff = 0.5} = material;
parsedMaterial.defines.ALPHA_CUTOFF = 1;
parsedMaterial.defines.ALPHA_CUTOFF = true;
parsedMaterial.uniforms.alphaCutoff = alphaCutoff;
break;
case 'BLEND':
Expand Down Expand Up @@ -242,7 +242,7 @@ function addTexture(
...textureOptions
});
parsedMaterial.bindings[uniformName] = texture;
if (define) parsedMaterial.defines[define] = 1;
if (define) parsedMaterial.defines[define] = true;
parsedMaterial.generatedTextures.push(texture);
}

Expand All @@ -253,7 +253,7 @@ function addTexture(
export class PBRMaterialParser {
readonly device: Device;

readonly defines: Record<string, number | boolean>;
readonly defines: Record<string, boolean>;
readonly bindings: Record<string, Binding>;
readonly uniforms: Record<string, any>;
readonly parameters: Record<string, any>;
Expand All @@ -268,12 +268,12 @@ export class PBRMaterialParser {

this.defines = {
// TODO: Use EXT_sRGB if available (Standard in WebGL 2.0)
MANUAL_SRGB: 1,
SRGB_FAST_APPROXIMATION: 1
MANUAL_SRGB: true,
SRGB_FAST_APPROXIMATION: true
};

if (this.device.features.has('glsl-texture-lod')) {
this.defines.USE_TEX_LOD = 1;
this.defines.USE_TEX_LOD = true;
}

this.uniforms = {
Expand Down Expand Up @@ -354,8 +354,8 @@ export class PBRMaterialParser {
}
if (material.alphaMode === 'MASK') {
const {alphaCutoff = 0.5} = material;
this.defines.ALPHA_CUTOFF = 1;
this.uniforms.alphaCutoff = alphaCutoff;
this.defines.ALPHA_CUTOFF = true;
this.uniforms.u_AlphaCutoff = alphaCutoff;
} else if (material.alphaMode === 'BLEND') {
log.warn('BLEND alphaMode might not work well because it requires mesh sorting')();
Object.assign(this.parameters, {
Expand Down
15 changes: 8 additions & 7 deletions modules/shadertools/src/lib/shader-assembly/assemble-shaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import {ShaderHook, normalizeShaderHooks, getShaderHooks} from './shader-hooks';
import {assert} from '../utils/assert';
import {getShaderInfo} from '../glsl-utils/get-shader-info';

/** Define map */
export type ShaderDefine = string | number | boolean;

const INJECT_SHADER_DECLARATIONS = `\n\n${DECLARATION_INJECT_MARKER}\n`;

/**
Expand Down Expand Up @@ -48,7 +45,9 @@ export type AssembleShaderOptions = {
/** Modules to be injected */
modules?: ShaderModule[];
/** Defines to be injected */
defines?: Record<string, ShaderDefine>;
defines?: Record<string, boolean>;
/** GLSL only: Overrides to be injected. In WGSL these are supplied during Pipeline creation time */
constants?: Record<string, number>;
/** Hook functions */
hookFunctions?: (ShaderHook | string)[];
/** Code injections */
Expand All @@ -68,7 +67,9 @@ type AssembleStageOptions = {
/** Modules to be injected */
modules: any[];
/** Defines to be injected */
defines?: Record<string, ShaderDefine>;
defines?: Record<string, boolean>;
/** GLSL only: Overrides to be injected. In WGSL these are supplied during Pipeline creation time */
constants?: Record<string, number>;
/** Hook functions */
hookFunctions?: (ShaderHook | string)[];
/** Code injections */
Expand Down Expand Up @@ -280,7 +281,7 @@ function assembleShaderGLSL(
language?: 'glsl' | 'wgsl';
stage: 'vertex' | 'fragment';
modules: ShaderModule[];
defines?: Record<string, ShaderDefine>;
defines?: Record<string, boolean>;
hookFunctions?: any[];
inject?: Record<string, string | ShaderInjection>;
prologue?: boolean;
Expand Down Expand Up @@ -461,7 +462,7 @@ export function assembleGetUniforms(modules: ShaderModule[]) {
*/

/** Generates application defines from an object of key value pairs */
function getApplicationDefines(defines: Record<string, ShaderDefine> = {}): string {
function getApplicationDefines(defines: Record<string, boolean> = {}): string {
let sourceText = '';
for (const define in defines) {
const value = defines[define];
Expand Down
2 changes: 1 addition & 1 deletion modules/shadertools/src/lib/shader-module/shader-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type ShaderModule<
prevUniforms?: UniformsT
) => Partial<UniformsT & BindingsT>;

defines?: Record<string, string | number>;
defines?: Record<string, boolean>;
/** Injections */
inject?: Record<string, string | {injection: string; order: number}>;
dependencies?: ShaderModule<any, any>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ export const lights = {
fs: lightingShader,
getUniforms,
defines: {
MAX_LIGHTS: 3
// MAX_LIGHTS: 3
}
};
2 changes: 1 addition & 1 deletion modules/shadertools/src/modules-webgl1/lighting/pbr/pbr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const pbr = {
vs,
fs,
defines: {
LIGHTING_FRAGMENT: 1
LIGHTING_FRAGMENT: true
},
dependencies: [lights],
getUniforms: (props: any) => props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const gouraudMaterial: ShaderModule<GouraudMaterialProps> = {
vs: PHONG_FS.replace('phongMaterial', 'gouraudMaterial'),
fs: PHONG_VS.replace('phongMaterial', 'gouraudMaterial'),
defines: {
LIGHTING_VERTEX: 1
LIGHTING_VERTEX: true
},
dependencies: [lighting],
uniformTypes: {
Expand Down
4 changes: 2 additions & 2 deletions modules/shadertools/src/modules/lighting/lights/lighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {lightingUniformsWGSL} from './lighting-uniforms-wgsl';
import type {NumberArray3} from '@math.gl/core';

/** Max number of supported lights (in addition to ambient light */
const MAX_LIGHTS = 3;
const MAX_LIGHTS = 5;

/** Whether to divide */
const COLOR_FACTOR = 255.0;
Expand Down Expand Up @@ -85,7 +85,7 @@ export const lighting = {
name: 'lighting',

defines: {
MAX_LIGHTS
// MAX_LIGHTS
},

uniformTypes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ export const pbrMaterial = {
fs,

defines: {
LIGHTING_FRAGMENT: 1
// TODO defining these as 0 breaks shader
// HAS_NORMALMAP: 0
// HAS_EMISSIVEMAP: 0,
// HAS_OCCLUSIONMAP: 0,
// HAS_BASECOLORMAP: 0,
// HAS_METALROUGHNESSMAP: 0,
// ALPHA_CUTOFF: 0
// USE_IBL: 0
// PBR_DEBUG: 0
LIGHTING_FRAGMENT: true,
HAS_NORMALMAP: false,
HAS_EMISSIVEMAP: false,
HAS_OCCLUSIONMAP: false,
HAS_BASECOLORMAP: false,
HAS_METALROUGHNESSMAP: false,
ALPHA_CUTOFF: false,
USE_IBL: false,
PBR_DEBUG: false
},
getUniforms: props => props,
uniformTypes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const phongMaterial: ShaderModule<PhongMaterialProps> = {
vs: PHONG_VS,
fs: PHONG_FS,
defines: {
LIGHTING_FRAGMENT: 1
LIGHTING_FRAGMENT: true
},
uniformTypes: {
ambient: 'f32',
Expand Down
38 changes: 19 additions & 19 deletions modules/shadertools/test/lib/shader-assembler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,25 @@ test('ShaderAssembler#hooks', t => {
t.ok(injectVs.indexOf('color *= 0.1') > -1, 'argument injection code included in shader hook');
t.ok(injectFs.indexOf('color += 0.1') > -1, 'argument injection code included in shader hook');

const injectDefineProgram1 = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
inject: {
'vs:LUMAGL_pickColor': 'color *= 0.1;'
}
});

const injectDefineProgram2 = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
defines: {
'vs:LUMAGL_pickColor': 'color *= 0.1;'
}
});

t.ok(injectDefineProgram1 !== injectDefineProgram2, 'Injects and defines hashed separately.');
// const injectDefineProgram1 = shaderAssembler.assembleGLSLShaderPair({
// platformInfo,
// vs,
// fs,
// inject: {
// 'vs:LUMAGL_pickColor': 'color *= 0.1;'
// }
// });

// const injectDefineProgram2 = shaderAssembler.assembleGLSLShaderPair({
// platformInfo,
// vs,
// fs,
// defines: {
// 'vs:LUMAGL_pickColor': 'color *= 0.1;'
// }
// });

// t.ok(injectDefineProgram1 !== injectDefineProgram2, 'Injects and defines hashed separately.');

t.end();
});
Expand Down
Loading