Skip to content

Commit

Permalink
Refactoring clustered lighting shader defines to be available as a chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Valigursky committed Feb 26, 2025
1 parent f705e75 commit 4c5f88e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
34 changes: 14 additions & 20 deletions src/scene/lighting/lights-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { FloatPacking } from '../../core/math/float-packing.js';
import { LIGHTSHAPE_PUNCTUAL, LIGHTTYPE_SPOT, MASK_AFFECT_LIGHTMAPPED, MASK_AFFECT_DYNAMIC } from '../constants.js';
import { Texture } from '../../platform/graphics/texture.js';
import { LightCamera } from '../renderer/light-camera.js';
import { shaderChunks } from '../shader-lib/chunks/chunks.js';
import { shaderChunksWGSL } from '../shader-lib/chunks-wgsl/chunks-wgsl.js';

const epsilon = 0.000001;

Expand Down Expand Up @@ -48,29 +50,21 @@ const TextureIndexFloat = {
COUNT: 8
};

let _defines;
// converts object with properties to a list of these as an example: "#define CLUSTER_TEXTURE_8_BLAH 1"
const buildShaderDefines = (object, prefix) => {
return Object.keys(object)
.map(key => `#define ${prefix}${key} ${object[key]}`)
.join('\n');
};

// create a shader chunk with defines for the light buffer textures
shaderChunks.lightBufferDefinesPS = shaderChunksWGSL.lightBufferDefinesPS = `\n
${buildShaderDefines(TextureIndex8, 'CLUSTER_TEXTURE_8_')}
${buildShaderDefines(TextureIndexFloat, 'CLUSTER_TEXTURE_F_')}
`;

// A class used by clustered lighting, responsible for encoding light properties into textures for the use on the GPU
class LightsBuffer {
static getShaderDefines() {

// converts object with properties to a list of these as an example: "#define CLUSTER_TEXTURE_8_BLAH 1"
const buildShaderDefines = (object, prefix) => {
return Object.keys(object)
.map(key => `#define ${prefix}${key} ${object[key]}`)
.join('\n');
};

if (!_defines) {
_defines = `\n
${buildShaderDefines(TextureIndex8, 'CLUSTER_TEXTURE_8_')}
${buildShaderDefines(TextureIndexFloat, 'CLUSTER_TEXTURE_F_')}
`;
}

return _defines;
}

areaLightsEnabled = false;

constructor(device) {
Expand Down
1 change: 1 addition & 0 deletions src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ const shaderChunksWGSL = {
// iridescencePS,
// iridescenceThicknessPS,
// iorPS,
lightBufferDefinesPS: '', // this chunk gets genereated at startup
// lightDeclarationPS,
// lightDiffuseLambertPS,
// lightDirPointPS,
Expand Down
1 change: 1 addition & 0 deletions src/scene/shader-lib/chunks/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ const shaderChunks = {
iridescencePS,
iridescenceThicknessPS,
iorPS,
lightBufferDefinesPS: '', // this chunk gets genereated at startup
lightDeclarationPS,
lightDiffuseLambertPS,
lightDirPointPS,
Expand Down
3 changes: 1 addition & 2 deletions src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '../../constants.js';
import { shaderChunks } from '../chunks/chunks.js';
import { ChunkUtils } from '../chunk-utils.js';
import { LightsBuffer } from '../../lighting/lights-buffer.js';
import { ShaderPass } from '../../shader-pass.js';
import { validateUserChunks } from '../chunks/chunk-validation.js';
import { ChunkBuilder } from '../chunk-builder.js';
Expand Down Expand Up @@ -587,7 +586,7 @@ class LitShader {
#include "lightDeclarationPS, LIGHT_COUNT"
#if defined(CLUSTERED_LIGHTS)
${LightsBuffer.getShaderDefines()}
#include "lightBufferDefinesPS"
#endif
// required lighting functionality
Expand Down

0 comments on commit 4c5f88e

Please sign in to comment.