Skip to content

Commit

Permalink
ShaderModule type update (#9044)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer authored Jul 29, 2024
1 parent 5ce18d5 commit 479b849
Show file tree
Hide file tree
Showing 49 changed files with 200 additions and 193 deletions.
4 changes: 2 additions & 2 deletions modules/aggregation-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.15",
"@luma.gl/shadertools": "^9.0.15",
"@luma.gl/constants": "^9.0.17",
"@luma.gl/shadertools": "^9.0.17",
"@math.gl/web-mercator": "^4.0.0",
"d3-hexbin": "^0.2.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export default class GPUGridCellLayer extends Layer<Required<_GPUGridCellLayerPr
gridOriginLow,
gridOffset,
gridOffsetLow,
// @ts-ignore
colorRange,
elevationRange
});
Expand Down
2 changes: 1 addition & 1 deletion modules/arcgis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.15",
"@luma.gl/constants": "^9.0.17",
"esri-loader": "^3.7.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions modules/carto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"@loaders.gl/mvt": "^4.2.0",
"@loaders.gl/schema": "^4.2.0",
"@loaders.gl/tiles": "^4.2.0",
"@luma.gl/core": "^9.0.15",
"@luma.gl/shadertools": "^9.0.15",
"@luma.gl/core": "^9.0.17",
"@luma.gl/shadertools": "^9.0.17",
"@math.gl/web-mercator": "^4.0.0",
"@types/d3-array": "^3.0.2",
"@types/d3-color": "^1.4.2",
Expand Down
13 changes: 3 additions & 10 deletions modules/carto/src/layers/heatmap-tile-layer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {ShaderModule} from '@luma.gl/shadertools';
import type {ShaderModule} from '@luma.gl/shadertools';
import {getResolution} from 'quadbin';

import {
Accessor,
CompositeLayer,
CompositeLayerProps,
DefaultProps,
Layer,
UniformTypes
} from '@deck.gl/core';
import {Accessor, CompositeLayer, CompositeLayerProps, DefaultProps, Layer} from '@deck.gl/core';
import {SolidPolygonLayer} from '@deck.gl/layers';

import {HeatmapProps, heatmap} from './heatmap';
Expand Down Expand Up @@ -38,7 +31,7 @@ const densityUniforms = {
vs: uniformBlock,
uniformTypes: {
factor: 'f32'
} as const satisfies UniformTypes<Required<DensityProps>>
}
} as const satisfies ShaderModule<DensityProps>;

// Modified polygon layer to draw offscreen and output value expected by heatmap
Expand Down
5 changes: 2 additions & 3 deletions modules/carto/src/layers/heatmap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {ShaderPass} from '@luma.gl/shadertools';
import {Color} from '@deck.gl/core';
const glsl = (s: TemplateStringsArray) => `${s}`;

/**
Expand Down Expand Up @@ -115,7 +114,7 @@ vec4 heatmap_sampleColor(sampler2D source, vec2 texSize, vec2 texCoord) {
}
`;

const defaultColorRange: Color[] = [
const defaultColorRange: [number, number, number][] = [
[255, 255, 178],
[254, 217, 118],
[254, 178, 76],
Expand All @@ -142,7 +141,7 @@ export type HeatmapProps = {
*
* @default `6-class YlOrRd` - [colorbrewer](http://colorbrewer2.org/#type=sequential&scheme=YlOrRd&n=6)
*/
colorRange: Color[];
colorRange: [number, number, number][];
/**
* Value that is multiplied with the total weight at a pixel to obtain the final weight. A value larger than 1 biases the output color towards the higher end of the spectrum, and a value less than 1 biases the output color towards the lower end of the spectrum.
*/
Expand Down
10 changes: 5 additions & 5 deletions modules/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"dependencies": {
"@loaders.gl/core": "^4.2.0",
"@loaders.gl/images": "^4.2.0",
"@luma.gl/constants": "^9.0.15",
"@luma.gl/core": "^9.0.15",
"@luma.gl/engine": "^9.0.15",
"@luma.gl/shadertools": "^9.0.15",
"@luma.gl/webgl": "^9.0.15",
"@luma.gl/constants": "^9.0.17",
"@luma.gl/core": "^9.0.17",
"@luma.gl/engine": "^9.0.17",
"@luma.gl/shadertools": "^9.0.17",
"@luma.gl/webgl": "^9.0.17",
"@math.gl/core": "^4.0.0",
"@math.gl/sun": "^4.0.0",
"@math.gl/web-mercator": "^4.0.0",
Expand Down
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/lib/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,11 @@ export default class Deck<ViewsT extends ViewOrViews = null> {
this.effectManager!.addDefaultEffect(effect);
}

_addDefaultShaderModule(module: ShaderModule) {
_addDefaultShaderModule(module: ShaderModule<Record<string, unknown>>) {
this.layerManager!.addDefaultShaderModule(module);
}

_removeDefaultShaderModule(module: ShaderModule) {
_removeDefaultShaderModule(module: ShaderModule<Record<string, unknown>>) {
this.layerManager?.removeDefaultShaderModule(module);
}

Expand Down
14 changes: 8 additions & 6 deletions modules/core/src/lib/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import type {LayerContext} from './layer-manager';
import type {BinaryAttribute} from './attribute/attribute';
import {RenderPass} from '@luma.gl/core';
import {PickingProps} from '@luma.gl/shadertools';
import {ProjectModuleSettings} from '../shaderlib/project/viewport-uniforms';
import {ProjectProps} from '../shaderlib/project/viewport-uniforms';

const TRACE_CHANGE_FLAG = 'layer.changeFlag';
const TRACE_INITIALIZE = 'layer.initialize';
Expand Down Expand Up @@ -401,11 +401,11 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component<

// Returns the picking color that doesn't match any subfeature
// Use if some graphics do not belong to any pickable subfeature
encodePickingColor(i, target: number[] = []): number[] {
encodePickingColor(i, target: number[] = []): [number, number, number] {
target[0] = (i + 1) & 255;
target[1] = ((i + 1) >> 8) & 255;
target[2] = (((i + 1) >> 8) >> 8) & 255;
return target;
return target as [number, number, number];
}

// Returns the index corresponding to a picking color that doesn't match any subfeature
Expand Down Expand Up @@ -750,7 +750,7 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component<

// If the attribute is larger than the cache, resize the cache and populate the missing chunk
const newCacheSize = Math.floor(pickingColorCache.length / 4);
const pickingColor = [];
const pickingColor: [number, number, number] = [0, 0, 0];
for (let i = cacheSize; i < newCacheSize; i++) {
this.encodePickingColor(i, pickingColor);
pickingColorCache[i * 4 + 0] = pickingColor[0];
Expand Down Expand Up @@ -1110,7 +1110,7 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component<
modelMatrix,
coordinateSystem,
coordinateOrigin
} as ProjectModuleSettings
} as ProjectProps
});
}

Expand Down Expand Up @@ -1265,10 +1265,12 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component<
/** Update picking module parameters to highlight the hovered object */
protected _updateAutoHighlight(info: PickingInfo): void {
const picking: PickingProps = {
// @ts-ignore
highlightedObjectColor: info.picked ? info.color : null
};
const {highlightColor} = this.props;
if (info.picked && typeof highlightColor === 'function') {
// @ts-ignore
picking.highlightColor = highlightColor(info);
}
this.setShaderModuleProps({picking});
Expand Down Expand Up @@ -1309,7 +1311,7 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component<
const picking: PickingProps = {};

if (Array.isArray(highlightColor)) {
picking.highlightColor = highlightColor;
picking.highlightColor = highlightColor as [number, number, number];
}

// highlightedObjectIndex will overwrite any settings from auto highlighting.
Expand Down
12 changes: 3 additions & 9 deletions modules/core/src/passes/screen-pass-uniforms.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import type {TextureView} from '@luma.gl/core';
import {ShaderModule} from '@luma.gl/shadertools';
import {UniformTypes} from '../shaderlib/misc/uniform-types';
import type {ShaderModule} from '@luma.gl/shadertools';

const uniformBlock = `\
uniform screenUniforms {
vec2 texSize;
} screen;
`;

type ScreenBindingProps = {
export type ScreenProps = {
texSrc: TextureView;
};

type ScreenUniformProps = {
texSize: [number, number];
};

export type ScreenProps = ScreenBindingProps & ScreenUniformProps;

export const screenUniforms = {
name: 'screen',
fs: uniformBlock,
uniformTypes: {
texSize: 'vec2<f32>'
} as const satisfies UniformTypes<Required<ScreenUniformProps>>
}
} as const satisfies ShaderModule<ScreenProps>;
2 changes: 1 addition & 1 deletion modules/core/src/shaderlib/misc/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ float smoothedge(float edge, float x) {
}
`;

export default {name: 'geometry', vs, fs} as ShaderModule;
export default {name: 'geometry', vs, fs} as const satisfies ShaderModule;
5 changes: 2 additions & 3 deletions modules/core/src/shaderlib/misc/layer-uniforms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ShaderModule} from '@luma.gl/shadertools';
import {UniformTypes} from './uniform-types';
import type {ShaderModule} from '@luma.gl/shadertools';

const uniformBlock = `\
uniform layerUniforms {
Expand All @@ -17,5 +16,5 @@ export const layerUniforms = {
fs: uniformBlock,
uniformTypes: {
opacity: 'f32'
} as const satisfies UniformTypes<LayerProps>
}
} as const satisfies ShaderModule<LayerProps>;
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 = [
number,
number,
number,
Expand Down
7 changes: 3 additions & 4 deletions modules/core/src/shaderlib/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import geometry from '../misc/geometry';
import projectShader from './project.glsl';
import {getUniformsFromViewport} from './viewport-uniforms';

import type {ProjectModuleSettings} from './viewport-uniforms';
import type {ProjectProps, ProjectUniforms} from './viewport-uniforms';

const INITIAL_MODULE_OPTIONS = {};

function getUniforms(opts: ProjectModuleSettings | {} = INITIAL_MODULE_OPTIONS) {
function getUniforms(opts: ProjectProps | {} = INITIAL_MODULE_OPTIONS) {
if ('viewport' in opts) {
return getUniformsFromViewport(opts);
}
Expand All @@ -45,7 +45,6 @@ export default {
commonUnitsPerMeter: 'vec3<f32>',
projectionMode: 'i32',
scale: 'f32',

commonUnitsPerWorldUnit: 'vec3<f32>',
commonUnitsPerWorldUnit2: 'vec3<f32>',
center: 'vec4<f32>',
Expand All @@ -59,4 +58,4 @@ export default {
commonOrigin: 'vec3<f32>',
pseudoMeters: 'f32'
}
} as const satisfies ShaderModule<ProjectModuleSettings>;
} as const satisfies ShaderModule<ProjectProps, ProjectUniforms, {}>;
24 changes: 12 additions & 12 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,
viewProjectionMatrix: viewProjectionMatrix as NumArray16,
projectionCenter,
originCommon,
cameraPosCommon,
Expand Down Expand Up @@ -209,17 +209,17 @@ export type ProjectUniforms = {
scale: number;
wrapLongitude: boolean;

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

// This is for lighting calculations
cameraPosition: Vec3;
};

export type ProjectModuleSettings = {
export type ProjectProps = {
viewport: Viewport;
devicePixelRatio?: number;
modelMatrix?: NumericArray | null;
modelMatrix?: NumArray16 | null;
coordinateSystem?: CoordinateSystem;
coordinateOrigin?: Vec3;
autoWrapLongitude?: boolean;
Expand All @@ -242,7 +242,7 @@ export function getUniformsFromViewport({
coordinateSystem = COORDINATE_SYSTEM.DEFAULT,
coordinateOrigin = DEFAULT_COORDINATE_ORIGIN,
autoWrapLongitude = false
}: ProjectModuleSettings): ProjectUniforms {
}: ProjectProps): ProjectUniforms {
if (coordinateSystem === COORDINATE_SYSTEM.DEFAULT) {
coordinateSystem = viewport.isGeospatial
? COORDINATE_SYSTEM.LNGLAT
Expand Down
7 changes: 5 additions & 2 deletions modules/core/src/shaderlib/shadow/shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ export default {
color = shadow_filterShadowColor(color);
`
},
getUniforms: (opts: {drawToShadowMap?: boolean; shadowMaps?: unknown[]} = {}, context = {}) => {
getUniforms: (
opts: {drawToShadowMap?: boolean; shadowMaps?: unknown[]} = {},
context: any = {}
) => {
if (
'viewport' in opts &&
(opts.drawToShadowMap || (opts.shadowMaps && opts.shadowMaps.length > 0))
Expand All @@ -269,4 +272,4 @@ export default {
}
return {};
}
} as ShaderModule<ShadowModuleSettings>;
} as const satisfies ShaderModule<ShadowModuleSettings>;
Loading

0 comments on commit 479b849

Please sign in to comment.