Skip to content

Commit

Permalink
Use UBOs in project module (#8782)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer committed May 7, 2024
1 parent 1a9dd6f commit ff9246e
Show file tree
Hide file tree
Showing 57 changed files with 357 additions and 273 deletions.
2 changes: 1 addition & 1 deletion docs/api-reference/core/project.md
Expand Up @@ -151,6 +151,6 @@ Returns a matrix that rotates any vector defined in the default common space to

## Remarks

* For consistent results, the screen space pixels are logical pixels, not device pixels, i.e. functions in the project module multiply `pixels` with `project_uDevicePixelRatio`.
* For consistent results, the screen space pixels are logical pixels, not device pixels, i.e. functions in the project module multiply `pixels` with `project.devicePixelRatio`.
* The pixels offsets will be divided by the `w` coordinate of `gl_Position`. This is simply the GPUs standard treatment of any coordinate. This means that there will be more pixels closer to the camera and less pixels further away from the camer. Setting the `focalDistance` uniform controls this.
* To avoid pixel sizes scaling with distance from camera, simply set `focalDistance` to 1 and multiply clipspace offset with `gl_Position.w`
2 changes: 1 addition & 1 deletion docs/api-reference/core/project64.md
Expand Up @@ -5,7 +5,7 @@ The `project64` shader module is an extension of the [project](./project.md) sha

## getUniforms

The uniforms needed by `project64` are extracted from the `project` module uniforms `project_uViewProjectionMatrix` and `project_uScale`.
The uniforms needed by `project64` are extracted from the `project` module uniforms `project.viewProjectionMatrix` and `project.scale`.


## GLSL Uniforms
Expand Down
2 changes: 1 addition & 1 deletion docs/developer-guide/custom-layers/subclassed-layers.md
Expand Up @@ -225,7 +225,7 @@ void main(void) {
/* replaced uniform 'radiusPixels' with 'instanceRadiusPixels' */
gl_Position.xy += project_pixel_size_to_clipspace(positions.xy * instanceRadiusPixels);
vec3 lightColor = lighting_getLightColor(instanceColors.rgb, project_uCameraPosition, position_commonspace.xyz, project_normal(instanceNormals));
vec3 lightColor = lighting_getLightColor(instanceColors.rgb, project.cameraPosition, position_commonspace.xyz, project_normal(instanceNormals));
vColor = vec4(lightColor, instanceColors.a * opacity) / 255.0;
Expand Down
Expand Up @@ -39,7 +39,7 @@ out vec4 vColor;
// offset_direction is -1 (left) or 1 (right)
vec2 getExtrusionOffset(vec2 line_clipspace, float offset_direction) {
// normalized direction of the line
vec2 dir_screenspace = normalize(line_clipspace * project_uViewportSize);
vec2 dir_screenspace = normalize(line_clipspace * project.viewportSize);
// rotate by 90 degrees
dir_screenspace = vec2(-dir_screenspace.y, dir_screenspace.x);
Expand Down
2 changes: 1 addition & 1 deletion examples/website/plot/plot-layer/grid-vertex.glsl.ts
Expand Up @@ -38,7 +38,7 @@ out float shouldDiscard;
// determines if the grid line is behind or in front of the center
float frontFacing(vec3 v) {
vec4 v_clipspace = project_uViewProjectionMatrix * project_uModelMatrix * vec4(v, 0.0);
vec4 v_clipspace = project.viewProjectionMatrix * project.modelMatrix * vec4(v, 0.0);
return step(v_clipspace.z, 0.0);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/website/plot/plot-layer/label-vertex.glsl.ts
Expand Up @@ -50,7 +50,7 @@ float sum3(vec3 v) {
// determines if the grid line is behind or in front of the center
float frontFacing(vec3 v) {
vec4 v_clipspace = project_uViewProjectionMatrix * project_uModelMatrix * vec4(v, 0.0);
vec4 v_clipspace = project.viewProjectionMatrix * project.modelMatrix * vec4(v, 0.0);
return step(v_clipspace.z, 0.0);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/aggregation-layers/package.json
Expand Up @@ -38,8 +38,8 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.11",
"@luma.gl/shadertools": "^9.0.11",
"@luma.gl/constants": "^9.0.12",
"@luma.gl/shadertools": "^9.0.12",
"@math.gl/web-mercator": "^4.0.0",
"d3-hexbin": "^0.2.1"
},
Expand Down
Expand Up @@ -134,7 +134,7 @@ void main(void) {
vec3 normals_commonspace = project_normal(normals);
if (extruded) {
vec3 lightColor = lighting_getLightColor(color.rgb, project_uCameraPosition, geometry.position.xyz, normals_commonspace);
vec3 lightColor = lighting_getLightColor(color.rgb, project.cameraPosition, geometry.position.xyz, normals_commonspace);
vColor = vec4(lightColor, color.a * opacity) / 255.;
} else {
vColor = vec4(color.rgb, color.a * opacity) / 255.;
Expand Down
Expand Up @@ -20,7 +20,15 @@

import {Texture} from '@luma.gl/core';
import {Model, Geometry} from '@luma.gl/engine';
import {Layer, LayerProps, log, picking, UpdateParameters, DefaultProps} from '@deck.gl/core';
import {
Layer,
LayerProps,
log,
project32,
picking,
UpdateParameters,
DefaultProps
} from '@deck.gl/core';
import {defaultColorRange, colorRangeToFlatArray} from '../utils/color-utils';
import vs from './screen-grid-layer-vertex.glsl';
import fs from './screen-grid-layer-fragment.glsl';
Expand Down Expand Up @@ -59,7 +67,7 @@ export default class ScreenGridCellLayer<DataT = any, ExtraPropsT extends {} = {
};

getShaders(): {vs: string; fs: string; modules: ShaderModule[]} {
return {vs, fs, modules: [picking as ShaderModule]};
return {vs, fs, modules: [project32, picking]};
}

initializeState() {
Expand Down
2 changes: 1 addition & 1 deletion modules/arcgis/package.json
Expand Up @@ -36,7 +36,7 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.11",
"@luma.gl/constants": "^9.0.12",
"esri-loader": "^3.7.0"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions modules/carto/package.json
Expand Up @@ -47,9 +47,9 @@
"@loaders.gl/mvt": "^4.2.0",
"@loaders.gl/schema": "^4.2.0",
"@loaders.gl/tiles": "^4.2.0",
"@luma.gl/constants": "^9.0.11",
"@luma.gl/core": "^9.0.11",
"@luma.gl/shadertools": "^9.0.11",
"@luma.gl/constants": "^9.0.12",
"@luma.gl/core": "^9.0.12",
"@luma.gl/shadertools": "^9.0.12",
"@math.gl/web-mercator": "^4.0.0",
"@types/d3-array": "^3.0.2",
"@types/d3-color": "^1.4.2",
Expand Down
6 changes: 3 additions & 3 deletions modules/carto/src/layers/raster-layer-vertex.glsl.ts
Expand Up @@ -46,8 +46,8 @@ void main(void) {
// Important to set geometry.position before using project_ methods below
// as geometry.worldPosition is not set (we don't know our lat/long)
geometry.position = vec4(common_position, 0.0, 1.0);
if (project_uProjectionMode == PROJECTION_MODE_WEB_MERCATOR_AUTO_OFFSET) {
geometry.position.xyz -= project_uCommonOrigin;
if (project.projectionMode == PROJECTION_MODE_WEB_MERCATOR_AUTO_OFFSET) {
geometry.position.xyz -= project.commonOrigin;
}
// calculate elevation, if 3d not enabled set to 0
Expand Down Expand Up @@ -86,7 +86,7 @@ void main(void) {
position_commonspace = geometry.position;
vColor = vec4(color.rgb, color.a * opacity);
#else
vec3 lightColor = lighting_getLightColor(color.rgb, project_uCameraPosition, geometry.position.xyz, geometry.normal);
vec3 lightColor = lighting_getLightColor(color.rgb, project.cameraPosition, geometry.position.xyz, geometry.normal);
vColor = vec4(lightColor, color.a * opacity);
#endif
} else {
Expand Down
10 changes: 5 additions & 5 deletions modules/core/package.json
Expand Up @@ -42,11 +42,11 @@
"dependencies": {
"@loaders.gl/core": "^4.2.0",
"@loaders.gl/images": "^4.2.0",
"@luma.gl/constants": "^9.0.11",
"@luma.gl/core": "^9.0.11",
"@luma.gl/engine": "^9.0.11",
"@luma.gl/shadertools": "^9.0.11",
"@luma.gl/webgl": "^9.0.11",
"@luma.gl/constants": "^9.0.12",
"@luma.gl/core": "^9.0.12",
"@luma.gl/engine": "^9.0.12",
"@luma.gl/shadertools": "^9.0.12",
"@luma.gl/webgl": "^9.0.12",
"@math.gl/core": "^4.0.0",
"@math.gl/sun": "^4.0.0",
"@math.gl/web-mercator": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/effects/lighting/camera-light.ts
Expand Up @@ -8,15 +8,15 @@ export default class CameraLight extends PointLight {
const {projectedLight} = this;
const viewport = layer.context.viewport;
const {coordinateSystem, coordinateOrigin, modelMatrix} = layer.props;
const {project_uCameraPosition} = getUniformsFromViewport({
const {cameraPosition} = getUniformsFromViewport({
viewport,
modelMatrix,
coordinateSystem,
coordinateOrigin
});
projectedLight.color = this.color;
projectedLight.intensity = this.intensity;
projectedLight.position = project_uCameraPosition;
projectedLight.position = cameraPosition;
return projectedLight;
}
}
7 changes: 6 additions & 1 deletion modules/core/src/lib/layer.ts
Expand Up @@ -1065,8 +1065,13 @@ export default abstract class Layer<PropsT extends {} = {}> extends Component<
// TODO/ib - hack move to luma Model.draw
if (moduleParameters) {
const {isActive, isAttribute} = moduleParameters.picking;
const {viewport, devicePixelRatio, coordinateSystem, coordinateOrigin} = moduleParameters;
const {modelMatrix} = this.props;
this.setModuleParameters(moduleParameters);
this.setShaderModuleProps({picking: {isActive, isAttribute}});
this.setShaderModuleProps({
picking: {isActive, isAttribute},
project: {viewport, devicePixelRatio, modelMatrix, coordinateSystem, coordinateOrigin}
});
}

// Apply polygon offset to avoid z-fighting
Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/shaderlib/index.ts
Expand Up @@ -21,12 +21,13 @@
import {ShaderAssembler} from '@luma.gl/shadertools';

import {gouraudLighting, phongLighting} from '@luma.gl/shadertools';
import geometry from './misc/geometry';
import project from './project/project';
import project32 from './project32/project32';
import shadow from './shadow/shadow';
import picking from './picking/picking';

const DEFAULT_MODULES = [project];
const DEFAULT_MODULES = [geometry];

const SHADER_HOOKS = [
'vs:DECKGL_FILTER_SIZE(inout vec3 size, VertexGeometry geometry)',
Expand Down

0 comments on commit ff9246e

Please sign in to comment.