Skip to content

Commit

Permalink
Merge 320c12a into 0b46676
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer committed Jun 20, 2024
2 parents 0b46676 + 320c12a commit f9d5f7e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 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
12 changes: 6 additions & 6 deletions modules/engine/src/shader-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) vis.gl contributors

import type {UniformValue, Texture, Sampler} from '@luma.gl/core';
import {log} from '@luma.gl/core';
import {log, splitUniformsAndBindings} from '@luma.gl/core';
// import type {ShaderUniformType, UniformValue, UniformFormat, UniformInfoDevice, Texture, Sampler} from '@luma.gl/core';
import {_resolveModules, ShaderModuleInstance} from '@luma.gl/shadertools';

Expand Down Expand Up @@ -104,17 +104,17 @@ export class ShaderInputs<
}

const oldUniforms = this.moduleUniforms[moduleName];
const uniforms =
const oldBindings = this.moduleBindings[moduleName];
const uniformsAndBindings =
module.getUniforms?.(moduleProps, this.moduleUniforms[moduleName]) || (moduleProps as any);

const {uniforms, bindings} = splitUniformsAndBindings(uniformsAndBindings);
// console.error(uniforms)
this.moduleUniforms[moduleName] = {...oldUniforms, ...uniforms};
this.moduleBindings[moduleName] = {...oldBindings, ...bindings};
// this.moduleUniformsChanged ||= moduleName;

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

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

Expand Down
29 changes: 29 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,31 @@ 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, colorTexture}) => ({color, 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 f9d5f7e

Please sign in to comment.