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

Change Attribute.type to VertexType from WebGL constants #8572

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import {GL} from '@luma.gl/constants';
import {LineLayer, SolidPolygonLayer} from '@deck.gl/layers';
import {generateContours} from './contour-utils';
import {
Expand Down Expand Up @@ -185,7 +184,7 @@ export default class ContourLayer<
[POSITION_ATTRIBUTE_NAME]: {
size: 3,
accessor: 'getPosition',
type: GL.DOUBLE,
type: 'float64',
fp64: this.use64bitPositions()
},
// this attribute is used in gpu aggregation path only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import {GL} from '@luma.gl/constants';
import {GridCellLayer} from '@deck.gl/layers';
import {Accessor, AccessorFunction, Color, Position, Material, DefaultProps} from '@deck.gl/core';

Expand Down Expand Up @@ -261,7 +260,7 @@ export default class CPUGridLayer<
};
const attributeManager = this.getAttributeManager()!;
attributeManager.add({
positions: {size: 3, type: GL.DOUBLE, accessor: 'getPosition'}
positions: {size: 3, type: 'float64', accessor: 'getPosition'}
});
// color and elevation attributes can't be added as attributes
// they are calcualted using 'getValue' accessor that takes an array of pints.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export default class GPUGridLayer<
[POSITION_ATTRIBUTE_NAME]: {
size: 3,
accessor: 'getPosition',
type: GL.DOUBLE,
type: 'float64',
fp64: this.use64bitPositions()
},
color: {size: 3, accessor: 'getColorWeight'},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
getTextureCoordinates
} from './heatmap-layer-utils';
import {Buffer, DeviceFeature, Texture, TextureProps, TextureFormat} from '@luma.gl/core';
import {GL} from '@luma.gl/constants';
import {TextureTransform, TextureTransformProps} from '@luma.gl/engine';
import {
Accessor,
Expand Down Expand Up @@ -370,7 +369,7 @@
_setupAttributes() {
const attributeManager = this.getAttributeManager()!;
attributeManager.add({
positions: {size: 3, type: GL.DOUBLE, accessor: 'getPosition'},
positions: {size: 3, type: 'float64', accessor: 'getPosition'},
weights: {size: 1, accessor: 'getWeight'}
});
this.setState({positionAttributeName: 'positions'});
Expand Down Expand Up @@ -470,7 +469,7 @@
}

_updateMaxWeightValue() {
const {maxWeightTransform, textureSize} = this.state;

Check warning on line 472 in modules/aggregation-layers/src/heatmap-layer/heatmap-layer.ts

View workflow job for this annotation

GitHub Actions / test-node

'textureSize' is assigned a value but never used

maxWeightTransform!.run({
parameters: {viewport: [0, 0, 1, 1]},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
DefaultProps
} from '@deck.gl/core';
import {ColumnLayer} from '@deck.gl/layers';
import {GL} from '@luma.gl/constants';

import {defaultColorRange} from '../utils/color-utils';

Expand Down Expand Up @@ -272,7 +271,7 @@ export default class HexagonLayer<DataT, ExtraPropsT extends {} = {}> extends Ag
};
const attributeManager = this.getAttributeManager()!;
attributeManager.add({
positions: {size: 3, type: GL.DOUBLE, accessor: 'getPosition'}
positions: {size: 3, type: 'float64', accessor: 'getPosition'}
});
// color and elevation attributes can't be added as attributes
// they are calculated using 'getValue' accessor that takes an array of pints.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
DefaultProps
} from '@deck.gl/core';
import type {Buffer, Texture} from '@luma.gl/core';
import {GL} from '@luma.gl/constants';
import GPUGridAggregator from '../utils/gpu-grid-aggregation/gpu-grid-aggregator';
import {AGGREGATION_OPERATION, getValueFunc} from '../utils/aggregation-operation-utils';
import ScreenGridCellLayer from './screen-grid-cell-layer';
Expand Down Expand Up @@ -182,7 +181,7 @@ export default class ScreenGridLayer<
[POSITION_ATTRIBUTE_NAME]: {
size: 3,
accessor: 'getPosition',
type: GL.DOUBLE,
type: 'float64',
fp64: this.use64bitPositions()
},
// this attribute is used in gpu aggregation path only
Expand Down
7 changes: 2 additions & 5 deletions modules/carto/src/layers/raster-layer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {GL} from '@luma.gl/constants';
import {
Accessor,
CompositeLayer,
Expand Down Expand Up @@ -48,16 +47,14 @@ class RasterColumnLayer extends ColumnLayer {
},
instanceFillColors: {
size: this.props.colorFormat.length,
type: GL.UNSIGNED_BYTE,
normalized: true,
type: 'unorm8',
transition: true,
accessor: 'getFillColor',
defaultValue: [0, 0, 0, 255]
},
instanceLineColors: {
size: this.props.colorFormat.length,
type: GL.UNSIGNED_BYTE,
normalized: true,
type: 'unorm8',
transition: true,
accessor: 'getLineColor',
defaultValue: [255, 255, 255, 255]
Expand Down
68 changes: 41 additions & 27 deletions modules/core/src/lib/attribute/data-column.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/* eslint-disable complexity */
import type {Device} from '@luma.gl/core';
import {Buffer, BufferLayout, BufferAttributeLayout} from '@luma.gl/core';
import {GL} from '@luma.gl/constants';
import {Buffer, BufferLayout, BufferAttributeLayout, VertexType} from '@luma.gl/core';

import {
glArrayFromType,
typedArrayFromDataType,
getBufferAttributeLayout,
getStride,
getGLTypeFromTypedArray
dataTypeFromTypedArray
} from './gl-utils';
import typedArrayManager from '../../utils/typed-array-manager';
import {toDoublePrecisionArray} from '../../utils/math-utils';
import log from '../../utils/log';

import type {TypedArray, NumericArray, TypedArrayConstructor} from '../../types/types';

export type DataType = Exclude<VertexType, 'float16'>;
export type LogicalDataType = DataType | 'float64';

export type BufferAccessor = {
/** A WebGL data type, see [vertexAttribPointer](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer#parameters). */
type?: number;
/** Vertex data type. */
type?: DataType;
/** The number of elements per vertex attribute. */
size?: number;
/** 1 if instanced. */
Expand All @@ -26,9 +28,6 @@ export type BufferAccessor = {
offset?: number;
/** The offset between the beginning of consecutive vertex attributes, in bytes. */
stride?: number;
/** Whether data values should be normalized. Note that all color attributes in deck.gl layers are normalized by default. */
normalized?: boolean;
integer?: boolean;
};

export type ShaderAttributeOptions = Partial<BufferAccessor> & {
Expand Down Expand Up @@ -90,19 +89,25 @@ function resolveDoublePrecisionShaderAttributes(
}

export type DataColumnOptions<Options> = Options &
BufferAccessor & {
Omit<BufferAccessor, 'type'> & {
id?: string;
vertexOffset?: number;
fp64?: boolean;
logicalType?: number;
/** Vertex data type.
* @default 'float32'
*/
type?: LogicalDataType;
/** Internal API, use `type` instead */
logicalType?: LogicalDataType;
isIndexed?: boolean;
defaultValue?: number | number[];
};

export type DataColumnSettings<Options> = DataColumnOptions<Options> & {
type: number;
type: DataType;
size: number;
logicalType?: number;
logicalType?: LogicalDataType;
normalized: boolean;
bytesPerElement: number;
defaultValue: number[];
defaultType: TypedArrayConstructor;
Expand Down Expand Up @@ -135,26 +140,26 @@ export default class DataColumn<Options, State> {
this.size = opts.size || 1;

const logicalType = opts.logicalType || opts.type;
const doublePrecision = logicalType === GL.DOUBLE;
const doublePrecision = logicalType === 'float64';

let {defaultValue} = opts;
defaultValue = Number.isFinite(defaultValue)
? [defaultValue]
: defaultValue || new Array(this.size).fill(0);

let bufferType: number;
let bufferType: DataType;
if (doublePrecision) {
bufferType = GL.FLOAT;
bufferType = 'float32';
} else if (!logicalType && opts.isIndexed) {
bufferType = GL.UNSIGNED_INT;
bufferType = 'uint32';
} else {
bufferType = logicalType || GL.FLOAT;
bufferType = logicalType || 'float32';
}

// This is the attribute type defined by the layer
// If an external buffer is provided, this.type may be overwritten
// But we always want to use defaultType for allocation
let defaultType = glArrayFromType(logicalType || bufferType || GL.FLOAT);
let defaultType = typedArrayFromDataType(logicalType || bufferType);
this.doublePrecision = doublePrecision;

// `fp64: false` tells a double-precision attribute to allocate Float32Arrays
Expand All @@ -172,6 +177,7 @@ export default class DataColumn<Options, State> {
defaultValue: defaultValue as number[],
logicalType,
type: bufferType,
normalized: bufferType.includes('norm'),
size: this.size,
bytesPerElement: defaultType.BYTES_PER_ELEMENT
};
Expand Down Expand Up @@ -361,8 +367,16 @@ export default class DataColumn<Options, State> {
const accessor: DataColumnSettings<Options> = {...this.settings, ...opts};

if (ArrayBuffer.isView(opts.value)) {
const is64Bit = this.doublePrecision && opts.value instanceof Float64Array;
accessor.type = opts.type || (is64Bit ? GL.FLOAT : getGLTypeFromTypedArray(opts.value));
if (!opts.type) {
// Deduce data type
const is64Bit = this.doublePrecision && opts.value instanceof Float64Array;
if (is64Bit) {
accessor.type = 'float32';
} else {
const type = dataTypeFromTypedArray(opts.value);
accessor.type = accessor.normalized ? (type.replace('int', 'norm') as DataType) : type;
}
}
accessor.bytesPerElement = opts.value.BYTES_PER_ELEMENT;
accessor.stride = getStride(accessor);
}
Expand Down Expand Up @@ -509,20 +523,20 @@ export default class DataColumn<Options, State> {
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer
normalizeConstant(value: NumericArray): NumericArray {
/* eslint-disable complexity */
switch (this.settings.type as GL) {
case GL.BYTE:
switch (this.settings.type) {
case 'snorm8':
// normalize [-128, 127] to [-1, 1]
return new Float32Array(value).map(x => ((x + 128) / 255) * 2 - 1);

case GL.SHORT:
case 'snorm16':
// normalize [-32768, 32767] to [-1, 1]
return new Float32Array(value).map(x => ((x + 32768) / 65535) * 2 - 1);

case GL.UNSIGNED_BYTE:
case 'unorm8':
// normalize [0, 255] to [0, 1]
return new Float32Array(value).map(x => x / 255);

case GL.UNSIGNED_SHORT:
case 'unorm16':
// normalize [0, 65535] to [0, 1]
return new Float32Array(value).map(x => x / 65535);

Expand Down Expand Up @@ -596,7 +610,7 @@ export default class DataColumn<Options, State> {
...this._buffer?.props,
id: this.id,
usage: isIndexed ? Buffer.INDEX : Buffer.VERTEX,
indexType: isIndexed ? ((type as GL) === GL.UNSIGNED_SHORT ? 'uint16' : 'uint32') : undefined,
indexType: isIndexed ? (type as 'uint16' | 'uint32') : undefined,
byteLength
});

Expand Down
Loading
Loading