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

ShaderModule type update #9044

Merged
merged 18 commits into from
Jul 29, 2024
Merged

ShaderModule type update #9044

merged 18 commits into from
Jul 29, 2024

Conversation

felixpalmer
Copy link
Collaborator

@felixpalmer felixpalmer commented Jul 24, 2024

Background

Following on from previous discussion, this new type for ShaderModule allows cross-checking of the uniformTypes without requiring an additional as const satisfies clause.

This PR requires visgl/luma.gl#2137

Changes

Generic parameters

The current type has PropsT, UniformsT & BindingsT, with the latter two not being used much.

The new type constructs these generic types from the Props type by filtering the via the UniformsOnly and BindingsOnly helpers.

Type cross checking

The uniformTypes now uses the UniformTypes<UniformsT> to cross check the types between the passed in props and the type expected by the GPU. In the cases where it is not a simple mapping, a custom UniformsT can be passed (e.g. the project module)
1

Example

export type ScreenProps = {
  texSrc: TextureView;
  texSize: [number, number];
};

type RenamedUniforms = {
  bounds: [number, number, number, number];
};

export const screenUniforms = {
  name: 'screen',
  fs: uniformBlock,
  uniformTypes: {
    texSize: 'vec2<f32>'
  }
} as const satisfies ShaderModule<ScreenProps>;

type ResolvedUniformTypes = NonNullable<ShaderModule<ScreenProps>['uniformTypes']>;
// -> type ResolvedUniformTypes = {texSize: "vec2<f32>" | "vec2<i32>" | "vec2<u32>"}

type ResolvedUniformTypes2 = NonNullable<ShaderModule<ScreenProps, RenamedUniforms>['uniformTypes']>;
// -> type ResolvedUniformTypes2 = {bounds: "vec4<f32>" | "vec4<i32>" | "vec4<u32>" | "mat2x2<f32>"}

type ResolvedBindings = NonNullable<ShaderModule<ScreenProps>['bindings']>;
// -> type ResolvedBindings = {
//      texSrc: {
//        location: number;
//        type: "texture" | "sampler" | "uniforms";
//     };
//   }

@coveralls
Copy link

coveralls commented Jul 24, 2024

Coverage Status

coverage: 89.306% (-0.001%) from 89.307%
when pulling dfdb4f8 on felix/shader-module-type
into 5ce18d5 on master.

Copy link
Collaborator

@ibgreen ibgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

modules/core/src/passes/screen-pass-uniforms.ts Outdated Show resolved Hide resolved
modules/core/src/passes/screen-pass-uniforms.ts Outdated Show resolved Hide resolved
modules/core/src/shaderlib/shader-module.ts Outdated Show resolved Hide resolved
modules/core/src/shaderlib/shader-module.ts Outdated Show resolved Hide resolved
modules/core/src/shaderlib/shader-module.ts Outdated Show resolved Hide resolved
@felixpalmer felixpalmer changed the title RFC: ShaderModule type ShaderModule type update Jul 25, 2024
@felixpalmer felixpalmer requested a review from ibgreen July 25, 2024 12:00
modules/core/src/lib/deck.ts Outdated Show resolved Hide resolved
@felixpalmer felixpalmer merged commit 479b849 into master Jul 29, 2024
4 checks passed
@felixpalmer felixpalmer deleted the felix/shader-module-type branch July 29, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants