Skip to content

Commit

Permalink
feat(engine): ShaderModule.getBindings()
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer committed Jun 13, 2024
1 parent 0b46676 commit 0e9d738
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions modules/engine/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ export class Model {
/** Update uniform buffers from the model's shader inputs */
updateShaderInputs(): void {
this._uniformStore.setUniforms(this.shaderInputs.getUniformValues());
this.setBindings(this.shaderInputs.getBindings());
// TODO - this is already tracked through buffer/texture update times?
this.setNeedsRedraw('shaderInputs');
}
Expand Down
6 changes: 3 additions & 3 deletions modules/engine/src/shader-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ShaderModuleInputs<
type: 'texture' | 'sampler' | 'uniforms';
}
>;
getBindings?: (settings: Partial<PropsT>, prevBindings?: BindingsT) => BindingsT;

uniformTypes?: any;
};
Expand Down Expand Up @@ -112,9 +113,8 @@ export class ShaderInputs<

// console.log(`setProps(${String(moduleName)}`, moduleName, this.moduleUniforms[moduleName])

// TODO - Get Module bindings
// const bindings = module.getBindings?.(moduleProps);
// this.moduleUniforms[moduleName] = bindings;
const bindings = module.getBindings?.(moduleProps);
this.moduleBindings[moduleName] = bindings;
}
}

Expand Down
30 changes: 30 additions & 0 deletions modules/engine/test/shader-inputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {picking} from '../../shadertools/src/index';
// import {_ShaderInputs as ShaderInputs} from '@luma.gl/engine';
import {ShaderInputs} from '../src/shader-inputs';
import {ShaderModule} from '@luma.gl/shadertools';
import {Texture} from '@luma.gl/core';

test('ShaderInputs#picking', t => {
const shaderInputsUntyped = new ShaderInputs({picking});
Expand Down Expand Up @@ -92,3 +93,32 @@ test('ShaderInputs#dependencies', t => {

t.end();
});

test('ShaderInputs#bindings', t => {
type CustomProps = {color: number[]; colorTexture: Texture};
const custom: ShaderModule<CustomProps> = {
name: 'custom',
uniformTypes: {color: 'vec3<f32>'},
uniformPropTypes: {color: {value: [0, 0, 0]}},
getUniforms: ({color}) => ({color}),
getBindings: ({colorTexture}) => ({colorTexture})
};

const shaderInputs = new ShaderInputs<{
custom: CustomProps;
}>({custom});

const MOCK_TEXTURE = 'MOCK_TEXTURE' as unknown as Texture;
shaderInputs.setProps({
custom: {color: [255, 0, 0], colorTexture: MOCK_TEXTURE}
});
t.deepEqual(shaderInputs.moduleUniforms.custom.color, [255, 0, 0], 'custom color updated');
t.equal(shaderInputs.moduleBindings.custom.colorTexture, MOCK_TEXTURE, 'colorTexture updated');

const uniformValues = shaderInputs.getUniformValues();
const bindings = shaderInputs.getBindings();
t.deepEqual(uniformValues, {custom: {color: [255, 0, 0]}}, 'uniformValues correct');
t.deepEqual(bindings, {colorTexture: 'MOCK_TEXTURE'}, 'bindings correct');

t.end();
});
4 changes: 3 additions & 1 deletion modules/shadertools/src/lib/shader-module/shader-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {NumberArray} from '@math.gl/types';
import {UniformFormat} from '../../types';
import {PropType} from '../filters/prop-types';
import {Sampler, Texture} from '@luma.gl/core';

export type UniformValue = number | boolean | Readonly<NumberArray>; // Float32Array> | Readonly<Int32Array> | Readonly<Uint32Array> | Readonly<number[]>;

Expand All @@ -19,7 +20,7 @@ export type UniformInfo = {
export type ShaderModule<
PropsT extends Record<string, unknown> = Record<string, unknown>,
UniformsT extends Record<string, UniformValue> = Record<string, UniformValue>,
BindingsT extends Record<string, unknown> = {}
BindingsT extends Record<string, Texture | Sampler> = {}
> = {
/** Used for type inference not for values */
props?: Required<PropsT>;
Expand All @@ -43,6 +44,7 @@ export type ShaderModule<

/** uniform buffers, textures, samplers, storage, ... */
bindings?: Record<keyof BindingsT, {location: number; type: 'texture' | 'sampler' | 'uniforms'}>;
getBindings?: (settings?: any, prevBindings?: any) => Record<string, Texture | Sampler>;

defines?: Record<string, string | number>;
/** Injections */
Expand Down

0 comments on commit 0e9d738

Please sign in to comment.