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

chore(tests): Update tests to use device instead of gl (v9) #7482

Merged
merged 2 commits into from Dec 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions modules/aggregation-layers/src/grid-aggregation-layer.ts
Expand Up @@ -108,14 +108,13 @@ export default abstract class GridAggregationLayer<
allocateResources(numRow, numCol) {
if (this.state.numRow !== numRow || this.state.numCol !== numCol) {
const dataBytes = numCol * numRow * 4 * 4;
const gl = this.context.gl;
const {weights} = this.state;
for (const name in weights) {
const weight = weights[name];
if (weight.aggregationBuffer) {
weight.aggregationBuffer.delete();
}
weight.aggregationBuffer = new Buffer(gl, {
weight.aggregationBuffer = new Buffer(this.context.device, {
byteLength: dataBytes,
accessor: {
size: 4,
Expand Down
2 changes: 1 addition & 1 deletion modules/aggregation-layers/src/grid-layer/grid-layer.ts
Expand Up @@ -90,7 +90,7 @@ export default class GridLayer<DataT = any, ExtraPropsT = {}> extends CompositeL
// cpu aggregation is requested
return false;
}
if (!GPUGridAggregator.isSupported(this.context.gl)) {
if (!GPUGridAggregator.isSupported(this.context.device)) {
return false;
}
if (lowerPercentile !== 0 || upperPercentile !== 100) {
Expand Down
@@ -1,3 +1,4 @@
import {Device} from '@luma.gl/api';
import GL from '@luma.gl/constants';
import {isWebGL2} from '@luma.gl/core';

Expand Down Expand Up @@ -79,11 +80,11 @@ export function getTextureCoordinates(point: number[], bounds: number[]) {
}

// Returns format and type for creating texture objects
export function getTextureParams({gl, floatTargetSupport}) {
export function getTextureParams({device: Device, floatTargetSupport}) {
return floatTargetSupport
? {
// format: should be RGBA32F on WebGL2 (float textures), RGBA in WebGL1 for float or non float textures
format: isWebGL2(gl) ? GL.RGBA32F : GL.RGBA,
format: device.isWebGL2 ? GL.RGBA32F : GL.RGBA,
type: GL.FLOAT
}
: {
Expand Down
11 changes: 5 additions & 6 deletions modules/aggregation-layers/src/heatmap-layer/heatmap-layer.ts
Expand Up @@ -207,8 +207,7 @@ export default class HeatmapLayer<DataT = any, ExtraPropsT = {}> extends Aggrega
};

initializeState() {
const {gl} = this.context;
if (!hasFeatures(gl, REQUIRED_FEATURES)) {
if (!hasFeatures(this.context.device, REQUIRED_FEATURES)) {
this.setState({supported: false});
log.error(`HeatmapLayer: ${this.id} is not supported on this browser`)();
return;
Expand Down Expand Up @@ -388,12 +387,12 @@ export default class HeatmapLayer<DataT = any, ExtraPropsT = {}> extends Aggrega
}

_setupTextureParams() {
const {gl} = this.context;
const {device} = this.context;
const {weightsTextureSize} = this.props;

const textureSize = Math.min(weightsTextureSize, getParameters(gl, gl.MAX_TEXTURE_SIZE));
const floatTargetSupport = hasFeatures(gl, FLOAT_TARGET_FEATURES);
const {format, type} = getTextureParams({gl, floatTargetSupport});
const textureSize = Math.min(weightsTextureSize, getParameters(device, GL.MAX_TEXTURE_SIZE));
const floatTargetSupport = hasFeatures(device, FLOAT_TARGET_FEATURES);
const {format, type} = getTextureParams({device, floatTargetSupport});
const weightsScale = floatTargetSupport ? 1 : 1 / 255;
this.setState({textureSize, format, type, weightsScale});
if (!floatTargetSupport) {
Expand Down
Expand Up @@ -55,9 +55,7 @@ export default class ScreenGridCellLayer<DataT = any, ExtraPropsT = {}> extends
static defaultProps = defaultProps;

static isSupported(device: Device) {
// @ts-expect-error
const gl = device.gl as WebGLRenderingContext;
return hasFeatures(gl, [FEATURES.TEXTURE_FLOAT]);
return hasFeatures(device, [FEATURES.TEXTURE_FLOAT]);
}

state!: Layer['state'] & {
Expand Down
Expand Up @@ -37,7 +37,7 @@ 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';
import GridAggregationLayer, {GridAggregationLayerProps} from '../grid-aggregation-layer';
import {getFloatTexture} from '../utils/resource-utils.js';
import {getFloatTexture} from '../utils/resource-utils';

const defaultProps: DefaultProps<ScreenGridLayerProps> = {
...ScreenGridCellLayer.defaultProps,
Expand Down
Expand Up @@ -50,7 +50,7 @@ import AGGREGATE_TO_GRID_FS from './aggregate-to-grid-fs.glsl';
import AGGREGATE_ALL_VS from './aggregate-all-vs.glsl';
import AGGREGATE_ALL_FS from './aggregate-all-fs.glsl';
import TRANSFORM_MEAN_VS from './transform-mean-vs.glsl';
import {getFloatTexture, getFramebuffer} from './../resource-utils.js';
import {getFloatTexture, getFramebuffer} from './../resource-utils';

const BUFFER_NAMES = ['aggregationBuffer', 'maxMinBuffer', 'minBuffer', 'maxBuffer'];
const ARRAY_BUFFER_MAP = {
Expand Down Expand Up @@ -109,9 +109,7 @@ export default class GPUGridAggregator {
}

static isSupported(device: Device) {
// @ts-expect-error
const gl = device.gl as WebGLRenderingContext;
return hasFeatures(gl, REQUIRED_FEATURES);
return hasFeatures(device, REQUIRED_FEATURES);
}

// DEBUG ONLY
Expand Down Expand Up @@ -159,19 +157,15 @@ export default class GPUGridAggregator {
};

device: Device;
/** @deprecated */
gl: WebGLRenderingContext;
_hasGPUSupport: boolean;

constructor(device: Device, opts = {}) {
this.id = opts.id || 'gpu-grid-aggregator';
this.device = device;

// @ts-expect-error;
this.gl = device.gl as WebGLRenderingContext;
this._hasGPUSupport =
isWebGL2(this.gl) && // gl_InstanceID usage in min/max calculation shaders
hasFeatures(this.gl, [
// gl_InstanceID usage in min/max calculation shaders
this._hasGPUSupport = this.device.info.type === 'webgl2' &&
hasFeatures(this.device, [
FEATURES.BLEND_EQUATION_MINMAX, // set min/max blend modes
FEATURES.COLOR_ATTACHMENT_RGBA32F, // render to float texture
FEATURES.TEXTURE_FLOAT // sample from a float texture
Expand Down Expand Up @@ -458,7 +452,7 @@ export default class GPUGridAggregator {
if (this.meanTransform) {
this.meanTransform.update(transformOptions);
} else {
this.meanTransform = getMeanTransform(gl, transformOptions);
this.meanTransform = getMeanTransform(this.device, transformOptions);
}
this.meanTransform.run({
parameters: {
Expand Down
16 changes: 9 additions & 7 deletions modules/arcgis/src/commons.js → modules/arcgis/src/commons.ts
@@ -1,14 +1,16 @@
/* eslint-disable no-invalid-this */

import type {Device} from '@luma.gl/api';
import GL from '@luma.gl/constants';
import {Deck} from '@deck.gl/core';
import {Model, Buffer, Framebuffer, instrumentGLContext, withParameters} from '@luma.gl/core';

export function initializeResources(gl) {
instrumentGLContext(gl);
export function initializeResources(device: Device) {
instrumentGLContext(device);

this.buffer = new Buffer(gl, new Int8Array([-1, -1, 1, -1, -1, 1, 1, 1]));
this.buffer = new Buffer(device, new Int8Array([-1, -1, 1, -1, -1, 1, 1, 1]));

this.model = new Model(gl, {
this.model = new Model(device, {
vs: `
attribute vec2 a_pos;
varying vec2 v_texcoord;
Expand All @@ -31,10 +33,10 @@ export function initializeResources(gl) {
a_pos: this.buffer
},
vertexCount: 4,
drawMode: gl.TRIANGLE_STRIP
drawMode: GL.TRIANGLE_STRIP
});

this.deckFbo = new Framebuffer(gl, {width: 1, height: 1});
this.deckFbo = new Framebuffer(device, {width: 1, height: 1});

this.deckInstance = new Deck({
// The view state will be set dynamically to track the MapView current extent.
Expand All @@ -44,7 +46,7 @@ export function initializeResources(gl) {
controller: false,

// We use the same WebGL context as the ArcGIS API for JavaScript.
gl,
gl: device.gl,

// We need depth testing in general; we don't know what layers might be added to the deck.
parameters: {
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/effects/lighting/lighting-effect.ts
Expand Up @@ -33,7 +33,7 @@ export default class LightingEffect implements Effect {
shadowColor: number[] = DEFAULT_SHADOW_COLOR;

private shadow: boolean;
private ambientLight?: AmbientLight;
private ambientLight?: AmbientLight | null = null;
private directionalLights: DirectionalLight[] = [];
private pointLights: PointLight[] = [];
private shadowPasses: ShadowPass[] = [];
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class LightingEffect implements Effect {
getModuleParameters(layer: Layer) {
const parameters: {
lightSources?: {
ambientLight?: AmbientLight;
ambientLight?: AmbientLight | null;
directionalLights: DirectionalLight[];
pointLights: PointLight[];
};
Expand Down
4 changes: 1 addition & 3 deletions modules/core/src/lib/attribute/data-column.ts
Expand Up @@ -146,11 +146,9 @@ export default class DataColumn<Options, State> implements IShaderAttribute {
if (doublePrecision) {
bufferType = GL.FLOAT;
} else if (!logicalType && opts.isIndexed) {
// @ts-expect-error
const gl = device.gl as WebGLRenderingContext;

bufferType =
gl && hasFeature(gl, FEATURES.ELEMENT_INDEX_UINT32) ? GL.UNSIGNED_INT : GL.UNSIGNED_SHORT;
device && hasFeature(device, FEATURES.ELEMENT_INDEX_UINT32) ? GL.UNSIGNED_INT : GL.UNSIGNED_SHORT;
} else {
bufferType = logicalType || GL.FLOAT;
}
Expand Down
11 changes: 3 additions & 8 deletions modules/core/src/lib/deck-picker.ts
Expand Up @@ -19,12 +19,7 @@
// THE SOFTWARE.

import type {Device} from '@luma.gl/api';
import {
Framebuffer,
Texture2D,
isWebGL2,
readPixelsToArray
} from '@luma.gl/core';
import {Framebuffer, Texture2D, isWebGL2, readPixelsToArray} from '@luma.gl/core';
import GL from '@luma.gl/constants';
import PickLayersPass, {PickingColorDecoder} from '../passes/pick-layers-pass';
import {getClosestObject, getUniqueObjects, PickedPixel} from './picking/query-object';
Expand Down Expand Up @@ -363,14 +358,14 @@ export default class DeckPicker {
// Convert from canvas top-left to WebGL bottom-left coordinates
// And compensate for pixelRatio
const pixelRatio = this.device.canvasContext.cssToDeviceRatio();
const leftTop = this.device.canvasContext.cssToDevicePixels([x, y], true);
const leftTop = this.device.canvasContext.cssToDevicePixels([x, y], true);

// take left and top (y inverted in device pixels) from start location
const deviceLeft = leftTop.x;
const deviceTop = leftTop.y + leftTop.height;

// take right and bottom (y inverted in device pixels) from end location
const rightBottom = this.device.canvasContext.cssToDevicePixels([x + width, y + height], true);
const rightBottom = this.device.canvasContext.cssToDevicePixels([x + width, y + height], true);
const deviceRight = rightBottom.x + rightBottom.width;
const deviceBottom = rightBottom.y;

Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/lib/deck.ts
Expand Up @@ -893,6 +893,7 @@ export default class Deck {

// if external context...
if (!this.canvas) {
debugger
this.canvas = gl.canvas;
// @ts-expect-error - Currently luma.gl v9 does not expose these options
instrumentGLContext(gl, {enable: true, copyState: true});
Expand Down Expand Up @@ -945,8 +946,7 @@ export default class Deck {
const viewport = this.viewManager.getViewports()[0];

// Note: avoid React setState due GL animation loop / setState timing issue
this.layerManager = new LayerManager({
device: this.device,
this.layerManager = new LayerManager(this.device, {
deck: this,
stats: this.stats,
viewport,
Expand Down
12 changes: 8 additions & 4 deletions modules/core/src/lib/layer-manager.ts
Expand Up @@ -57,8 +57,6 @@ export type LayerContext = {
export type LayersList = (Layer | undefined | false | null | LayersList)[];

export type LayerManagerProps = {
// Apparently LayerManager is supposed to be instantiatable without gl context?
device: Device | null;
deck?: Deck;
stats?: Stats;
viewport?: Viewport;
Expand All @@ -75,8 +73,14 @@ export default class LayerManager {
private _nextLayers: LayersList | null = null;
private _debug: boolean = false;

/**
* @param device
* @param param1
*/
// eslint-disable-next-line
constructor({device, deck, stats, viewport, timeline}: LayerManagerProps) {
constructor(device: Device, props: LayerManagerProps) {
const {deck, stats, viewport, timeline} = props || {};

// Currently deck.gl expects the DeckGL.layers array to be different
// whenever React rerenders. If the same layers array is used, the
// LayerManager's diffing algorithm will generate a fatal error and
Expand All @@ -95,7 +99,7 @@ export default class LayerManager {
layerManager: this,
device,
// @ts-expect-error
gl: device && device.gl,
gl: device?.gl,
deck,
// Enabling luma.gl Program caching using private API (_cachePrograms)
pipelineFactory: (device && createProgramManager(device))!,
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/lib/resource/resource-manager.ts
Expand Up @@ -26,7 +26,7 @@ export default class ResourceManager {
this._context = {
device: props.device,
// @ts-expect-error
gl: props.device.gl,
gl: props.device?.gl,
resourceManager: this
};
this._resources = {};
Expand Down
5 changes: 3 additions & 2 deletions modules/core/src/passes/layers-pass.ts
Expand Up @@ -382,10 +382,11 @@ function getGLViewport(
const useTarget = target && target.id !== 'default-framebuffer';

const pixelRatio =
(moduleParameters && moduleParameters.devicePixelRatio) || device.canvasContext.cssToDeviceRatio();
(moduleParameters && moduleParameters.devicePixelRatio) ||
device.canvasContext.cssToDeviceRatio();

// Default framebuffer is used when writing to canvas
const [,drawingBufferHeight] = device.canvasContext.getDrawingBufferSize();
const [, drawingBufferHeight] = device.canvasContext.getDrawingBufferSize();
const height = useTarget ? target.height : drawingBufferHeight;

// Convert viewport top-left CSS coordinates to bottom up WebGL coordinates
Expand Down
5 changes: 2 additions & 3 deletions modules/core/src/passes/screen-pass.ts
Expand Up @@ -31,9 +31,8 @@ export default class ScreenPass extends Pass {
}

render(params: ScreenPassRenderOptions): void {
// @ts-expect-error
const gl = device.gl as WebGLRenderingContext;
setParameters(this.device, {viewport: [0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight]});
const [drawingBufferWidth, drawingBufferHeight] = this.device.canvasContext.getDrawingBufferSize()
setParameters(this.device, {viewport: [0, 0, drawingBufferWidth, drawingBufferHeight]});

// TODO change to device when luma.gl is fixed
withParameters(this.device, {framebuffer: params.outputBuffer, clearColor: [0, 0, 0, 0]}, () =>
Expand Down
5 changes: 1 addition & 4 deletions modules/core/src/passes/shadow-pass.ts
Expand Up @@ -28,10 +28,7 @@ export default class ShadowPass extends LayersPass {
}
});

// @ts-expect-error
const gl = device.gl as WebGLRenderingContext;

this.depthBuffer = new Renderbuffer(gl, {
this.depthBuffer = new Renderbuffer(device, {
format: GL.DEPTH_COMPONENT16,
width: 1,
height: 1
Expand Down