Skip to content

Commit

Permalink
chore(shadertools): Support boolean defines only
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Aug 19, 2024
1 parent 4f33300 commit 22d9b4c
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 61 deletions.
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
34 changes: 17 additions & 17 deletions modules/gltf/src/pbr/parse-pbr-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type ParsePBRMaterialOptions = {
};

export type ParsedPBRMaterial = {
readonly defines: Record<string, number | boolean>;
readonly defines: Record<string, boolean>;
readonly bindings: Record<string, Binding>;
readonly uniforms: Record<string, any>;
readonly parameters: Parameters;
Expand Down Expand Up @@ -52,8 +52,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 @@ -68,7 +68,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 @@ -81,18 +81,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.u_ScaleDiffBaseMR = [0, 0, 0, 0];
parsedMaterial.uniforms.u_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 @@ -140,7 +140,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.u_AlphaCutoff = alphaCutoff;
break;
case 'BLEND':
Expand Down Expand Up @@ -235,7 +235,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 @@ -246,7 +246,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 @@ -261,12 +261,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 @@ -347,7 +347,7 @@ export class PBRMaterialParser {
}
if (material.alphaMode === 'MASK') {
const {alphaCutoff = 0.5} = material;
this.defines.ALPHA_CUTOFF = 1;
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')();
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 @@ function getShaderNameDefine(options: {
}

/** 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 @@ -58,7 +58,7 @@ export type ShaderModule<
/** uniform buffers, textures, samplers, storage, ... */
bindings?: Record<keyof BindingsT, {location: number; type: 'texture' | 'sampler' | 'uniforms'}>;

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 @@ -26,7 +26,7 @@ export const gouraudMaterial = {
vs: GOURAUD_VS,
fs: GOURAUD_FS,
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 @@ -8,7 +8,7 @@ import {lightingUniformsGLSL} from './lighting-uniforms-glsl';
import {lightingUniformsWGSL} from './lighting-uniforms-wgsl';

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

/** Whether to divide */
const COLOR_FACTOR = 255.0;
Expand Down Expand Up @@ -76,7 +76,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 @@ -107,15 +107,15 @@ export const pbrMaterial = {
fs,

defines: {
LIGHTING_FRAGMENT: 1,
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
},

uniformTypes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const phongMaterial = {
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

0 comments on commit 22d9b4c

Please sign in to comment.