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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add types to project module
  • Loading branch information
felixpalmer committed Jul 24, 2024
commit ac0b5f3d231f09dc8363a0c26d70d61ac00a61bf
3 changes: 2 additions & 1 deletion modules/core/src/effects/lighting/camera-light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {PointLight} from './point-light';
import {getUniformsFromViewport} from '../../shaderlib/project/viewport-uniforms';
import type Layer from '../../lib/layer';
import {NumArray16} from '../../shaderlib/misc/uniform-types';

export default class CameraLight extends PointLight {
getProjectedLight({layer}: {layer: Layer}): PointLight {
Expand All @@ -10,7 +11,7 @@ export default class CameraLight extends PointLight {
const {coordinateSystem, coordinateOrigin, modelMatrix} = layer.props;
const {cameraPosition} = getUniformsFromViewport({
viewport,
modelMatrix,
modelMatrix: modelMatrix as NumArray16,
coordinateSystem,
coordinateOrigin
});
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/passes/screen-pass-uniforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uniform screenUniforms {
} screen;
`;

type ScreenProps = {
export type ScreenProps = {
texSrc: TextureView;
texSize: [number, number];
};
Expand All @@ -26,4 +26,4 @@ export const screenUniforms = {

type ResolvedUniformTypes = ShaderModule<ScreenProps>['uniformTypes'];
type ResolvedUniformTypes2 = ShaderModule<ScreenProps, RenamedUniforms>['uniformTypes'];
type ResolvedBindings = ShaderModule<ScreenProps>['bindings'];
type ResolvedBindings = NonNullable<ShaderModule<ScreenProps>['bindings']>;
2 changes: 1 addition & 1 deletion modules/core/src/shaderlib/misc/uniform-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type NumArray12 = [
number,
number
];
type NumArray16 = [
export type NumArray16 = [
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
number,
number,
number,
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/shaderlib/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import geometry from '../misc/geometry';
import projectShader from './project.glsl';
import {getUniformsFromViewport} from './viewport-uniforms';

import type {ProjectModuleSettings} from './viewport-uniforms';
import type {ProjectModuleSettings, ProjectUniforms} from './viewport-uniforms';
import {UniformTypes} from '../misc/uniform-types';

const INITIAL_MODULE_OPTIONS = {};

Expand All @@ -45,7 +46,6 @@ export default {
commonUnitsPerMeter: 'vec3<f32>',
projectionMode: 'i32',
scale: 'f32',

commonUnitsPerWorldUnit: 'vec3<f32>',
commonUnitsPerWorldUnit2: 'vec3<f32>',
center: 'vec4<f32>',
Expand All @@ -58,5 +58,5 @@ export default {
coordinateOrigin: 'vec3<f32>',
commonOrigin: 'vec3<f32>',
pseudoMeters: 'f32'
}
} as const satisfies UniformTypes<ProjectUniforms>
} as const satisfies ShaderModule<ProjectModuleSettings>;
20 changes: 10 additions & 10 deletions modules/core/src/shaderlib/project/viewport-uniforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ import memoize from '../../utils/memoize';

import type Viewport from '../../viewports/viewport';
import type {CoordinateSystem} from '../../lib/constants';
import type {NumericArray} from '../../types/types';
import type {NumArray16} from '../misc/uniform-types';

type Vec3 = [number, number, number];
type Vec4 = [number, number, number, number];

// To quickly set a vector to zero
const ZERO_VECTOR: Vec4 = [0, 0, 0, 0];
// 4x4 matrix that drops 4th component of vector
const VECTOR_TO_POINT_MATRIX = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0];
const IDENTITY_MATRIX = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
const VECTOR_TO_POINT_MATRIX: NumArray16 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0];
const IDENTITY_MATRIX: NumArray16 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
const DEFAULT_PIXELS_PER_UNIT2: Vec3 = [0, 0, 0];
const DEFAULT_COORDINATE_ORIGIN: Vec3 = [0, 0, 0];

Expand Down Expand Up @@ -127,8 +127,8 @@ function calculateMatrixAndOffset(
coordinateSystem: CoordinateSystem,
coordinateOrigin: Vec3
): {
viewMatrix: NumericArray;
viewProjectionMatrix: NumericArray;
viewMatrix: NumArray16;
viewProjectionMatrix: NumArray16;
projectionCenter: Vec4;
originCommon: Vec4;
cameraPosCommon: Vec3;
Expand Down Expand Up @@ -177,8 +177,8 @@ function calculateMatrixAndOffset(
}

return {
viewMatrix,
viewProjectionMatrix,
viewMatrix: viewMatrix as NumArray16,
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
viewProjectionMatrix: viewProjectionMatrix as NumArray16,
projectionCenter,
originCommon,
cameraPosCommon,
Expand Down Expand Up @@ -209,8 +209,8 @@ export type ProjectUniforms = {
scale: number;
wrapLongitude: boolean;

viewProjectionMatrix: NumericArray;
modelMatrix: NumericArray;
viewProjectionMatrix: NumArray16;
modelMatrix: NumArray16;

// This is for lighting calculations
cameraPosition: Vec3;
Expand All @@ -219,7 +219,7 @@ export type ProjectUniforms = {
export type ProjectModuleSettings = {
viewport: Viewport;
devicePixelRatio?: number;
modelMatrix?: NumericArray | null;
modelMatrix?: NumArray16 | null;
coordinateSystem?: CoordinateSystem;
coordinateOrigin?: Vec3;
autoWrapLongitude?: boolean;
Expand Down
7 changes: 4 additions & 3 deletions modules/core/src/shaderlib/shader-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export type UniformValue = number | boolean | number[];
import {UniformTypes} from './misc/uniform-types';

// Helper types
type FilterKeysByType<T> = {[K in keyof T]: T[K] extends UniformValue ? K : never}[keyof T];
type UniformsOnly<T> = {[K in FilterKeysByType<T>]: T[K]};
type FilterKeysByType<T, U> = {[K in keyof T]: T[K] extends U ? K : never}[keyof T];
type BindingsOnly<T> = {[K in FilterKeysByType<T, BindingValue>]: T[K]};
type UniformsOnly<T> = {[K in FilterKeysByType<T, UniformValue>]: T[K]};

/**
* A shader module definition object
Expand All @@ -26,7 +27,7 @@ export type ShaderModule<
UniformValue | BindingValue
>,
UniformTypesT extends Record<string, UniformFormat> = UniformTypes<UniformsOnly<PropsT>>,
BindingsT extends Record<string, BindingValue> = Record<string, BindingValue>
BindingsT extends Record<string, BindingValue> = BindingsOnly<PropsT>
> = {
/** Used for type inference not for values */
props?: PropsT;
Expand Down