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(test-utils): Update tests to use async device creation #2226

Merged
merged 6 commits into from
Sep 1, 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
6 changes: 4 additions & 2 deletions modules/constants/test/webgl-constants.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) vis.gl contributors

import test from 'tape-promise/tape';
import {getTestDevices} from '@luma.gl/test-utils';
import {getWebGLTestDevice} from '@luma.gl/test-utils';

import {GL} from '@luma.gl/constants';

Expand All @@ -13,7 +13,9 @@ test('@luma.gl/constants', t => {
});

test('@luma.gl/constants#WebGL2RenderingContext comparison', async t => {
for (const device of await getTestDevices('webgl')) {
const webglDevice = await getWebGLTestDevice();

for (const device of [webglDevice]) {
// @ts-ignore
const gl = device.gl;
let count = 0;
Expand Down
10 changes: 8 additions & 2 deletions modules/core/src/adapter/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ export type DeviceProps = {
_disabledFeatures?: Partial<Record<DeviceFeature, boolean>>;
/** WebGL specific - Initialize all features on startup */
_initializeFeatures?: boolean;
/** Enable shader caching (via ShaderFactory) */
_cacheShaders?: boolean;
/** Enable shader caching (via PipelineFactory) */
_cachePipelines?: boolean;
/** Never destroy cached shaders and pipelines */
_factoryDestroyPolicy?: 'unused' | 'never';
_cacheDestroyPolicy?: 'unused' | 'never';
/** Resource default overrides */
_resourceDefaults?: {
texture?: Partial<TextureProps>;
Expand Down Expand Up @@ -283,7 +287,9 @@ export abstract class Device {
onError: (error: Error) => log.error(error.message)(),

_requestMaxLimits: true,
_factoryDestroyPolicy: 'unused',
_cacheShaders: false,
_cachePipelines: false,
_cacheDestroyPolicy: 'unused',
// TODO - Change these after confirming things work as expected
_initializeFeatures: true,
_disabledFeatures: {
Expand Down
6 changes: 4 additions & 2 deletions modules/core/test/adapter-utils/is-uniform-value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import {isUniformValue} from '@luma.gl/core/adapter-utils/is-uniform-value';
import {WEBGLSampler, WEBGLTexture} from '@luma.gl/webgl';
import {webglDevice as device} from '@luma.gl/test-utils';
import {getWebGLTestDevice} from '@luma.gl/test-utils';
import test from 'tape-promise/tape';

test('isUniformValue', t => {
test('isUniformValue', async t => {
const device = await getWebGLTestDevice();

t.ok(isUniformValue(3), 'Number is uniform value');
t.ok(isUniformValue(3.412), 'Number is uniform value');
t.ok(isUniformValue(0), 'Number is uniform value');
Expand Down
10 changes: 7 additions & 3 deletions modules/core/test/adapter/device-helpers/device-features.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
// Copyright (c) vis.gl contributors

import test from 'tape-promise/tape';
import {webglDevice} from '@luma.gl/test-utils';
import {getWebGLTestDevice} from '@luma.gl/test-utils';
import {DeviceFeature} from '@luma.gl/core';

// TODO - we are not actually testing any features
const WEBGL2_ALWAYS_FEATURES: DeviceFeature[] = [];
const WEBGL2_NEVER_FEATURES: DeviceFeature[] = [];

test('WebGLDevice#features (unknown features)', t => {
test('WebGLDevice#features (unknown features)', async t => {
const webglDevice = await getWebGLTestDevice();

// @ts-expect-error
t.notOk(webglDevice.features.has('unknown'), 'features.has should return false');
// @ts-expect-error
t.notOk(webglDevice.features.has(''), 'features.has should return false');
t.end();
});

test('WebGLDevice#hasFeatures (WebGL)', t => {
test('WebGLDevice#hasFeatures (WebGL)', async t => {
const webglDevice = await getWebGLTestDevice();

for (const feature of WEBGL2_ALWAYS_FEATURES) {
t.equal(webglDevice.features.has(feature), true, `${feature} is always supported under WebGL`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,101 @@
// Copyright (c) vis.gl contributors

import test, {Test} from 'tape-promise/tape';
import {webglDevice} from '@luma.gl/test-utils';
import {getWebGLTestDevice} from '@luma.gl/test-utils';

import {Parameters} from '@luma.gl/core';
import {GL, GLParameters} from '@luma.gl/constants';
import {setDeviceParameters, getGLParameters, resetGLParameters} from '@luma.gl/webgl';

// Settings test, could be beneficial to not reuse a context
const {gl} = webglDevice;
import {setDeviceParameters, getGLParameters, resetGLParameters, WebGLDevice} from '@luma.gl/webgl';

// const stringify = (v) => JSON.stringify(ArrayBuffer.isView(v) ? Array.apply([], v) : v);

const getGLParameter = (parameter: keyof GLParameters): any => {
const getGLParameter = (gl: WebGL2RenderingContext, parameter: keyof GLParameters): any => {
const parameters = getGLParameters(gl, [parameter]);
return parameters[parameter];
};

test('setDeviceParameters#cullMode', t => {
test('setDeviceParameters#cullMode', async t => {
const webglDevice = await getWebGLTestDevice();
const gl = webglDevice.gl;

resetGLParameters(gl);

t.deepEqual(getGLParameter(GL.CULL_FACE), false, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.CULL_FACE), false, 'got expected value');

setDeviceParameters(webglDevice, {cullMode: 'front'});
t.deepEqual(getGLParameter(GL.CULL_FACE), true, 'got expected value');
t.deepEqual(getGLParameter(GL.CULL_FACE_MODE), GL.FRONT, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.CULL_FACE), true, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.CULL_FACE_MODE), GL.FRONT, 'got expected value');

setDeviceParameters(webglDevice, {cullMode: 'back'});
t.deepEqual(getGLParameter(GL.CULL_FACE), true, 'got expected value');
t.deepEqual(getGLParameter(GL.CULL_FACE_MODE), GL.BACK, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.CULL_FACE), true, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.CULL_FACE_MODE), GL.BACK, 'got expected value');

setDeviceParameters(webglDevice, {cullMode: 'none'});
t.deepEqual(getGLParameter(GL.CULL_FACE), false, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.CULL_FACE), false, 'got expected value');

t.end();
});

test('setDeviceParameters#frontFace', t => {
test('setDeviceParameters#frontFace', async t => {
const webglDevice = await getWebGLTestDevice();
const gl = webglDevice.gl;

resetGLParameters(gl);

t.deepEqual(getGLParameter(GL.FRONT_FACE), GL.CCW, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.FRONT_FACE), GL.CCW, 'got expected value');

setDeviceParameters(webglDevice, {frontFace: 'cw'});
t.deepEqual(getGLParameter(GL.FRONT_FACE), GL.CW, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.FRONT_FACE), GL.CW, 'got expected value');

setDeviceParameters(webglDevice, {frontFace: 'ccw'});
t.deepEqual(getGLParameter(GL.FRONT_FACE), GL.CCW, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.FRONT_FACE), GL.CCW, 'got expected value');

t.end();
});

test('setDeviceParameters#depthWriteEnabled', t => {
test('setDeviceParameters#depthWriteEnabled', async t => {
const webglDevice = await getWebGLTestDevice();
const gl = webglDevice.gl;

resetGLParameters(gl);

t.deepEqual(getGLParameter(GL.DEPTH_WRITEMASK), true, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.DEPTH_WRITEMASK), true, 'got expected value');

setDeviceParameters(webglDevice, {depthWriteEnabled: false});
t.deepEqual(getGLParameter(GL.DEPTH_WRITEMASK), false, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.DEPTH_WRITEMASK), false, 'got expected value');

setDeviceParameters(webglDevice, {depthWriteEnabled: true});
t.deepEqual(getGLParameter(GL.DEPTH_WRITEMASK), true, 'got expected value');
t.deepEqual(getGLParameter(gl, GL.DEPTH_WRITEMASK), true, 'got expected value');

t.end();
});

test('setDeviceParameters#depthWriteEnabled', t => {
testClauses(t, 'depthWriteEnabled', [
// type TestClause = {check: GLParameters} | {set: Parameters};
type TestClause = {check?: GLParameters; set?: Parameters};

function testClauses(t: Test, device: WebGLDevice, name: string, clauses: TestClause[]): void {
const gl = device.gl;

resetGLParameters(device.gl);

for (const clause of clauses) {
if (clause.check) {
const values = getGLParameters(gl, clause.check);
for (const [key, value] of Object.entries(clause.check)) {
t.deepEqual(values[key], value, `got expected value for ${name}`);
}
}

if (clause.set) {
setDeviceParameters(device, clause.set);
}
}
}

test('setDeviceParameters#depthWriteEnabled', async t => {
const webglDevice = await getWebGLTestDevice();

testClauses(t, webglDevice, 'depthWriteEnabled', [
{check: {[GL.DEPTH_WRITEMASK]: true}},
{set: {depthWriteEnabled: false}},
{check: {[GL.DEPTH_WRITEMASK]: false}},
Expand All @@ -91,25 +121,3 @@ test.skip('setDeviceParameters#depthClearValue', t => {

t.end();
});

// HELPERS

// type TestClause = {check: GLParameters} | {set: Parameters};
type TestClause = {check?: GLParameters; set?: Parameters};

function testClauses(t: Test, name: string, clauses: TestClause[]): void {
resetGLParameters(gl);

for (const clause of clauses) {
if (clause.check) {
const values = getGLParameters(gl, clause.check);
for (const [key, value] of Object.entries(clause.check)) {
t.deepEqual(values[key], value, `got expected value for ${name}`);
}
}

if (clause.set) {
setDeviceParameters(webglDevice, clause.set);
}
}
}
4 changes: 2 additions & 2 deletions modules/core/test/adapter/resources/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable no-continue */

import test from 'tape-promise/tape';
import {getTestDevices, webglDevice} from '@luma.gl/test-utils';
import {getTestDevices, getWebGLTestDevice} from '@luma.gl/test-utils';

import {TypedArray} from '@math.gl/types';
import {Buffer} from '@luma.gl/core';
Expand Down Expand Up @@ -202,7 +202,7 @@ test('Buffer#debugData', async t => {
// WEBGL specific tests

test('WEBGLBuffer#construction', async t => {
await getTestDevices();
const webglDevice = await getWebGLTestDevice();

let buffer;

Expand Down
14 changes: 10 additions & 4 deletions modules/core/test/adapter/resources/command-encoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import test, {Test} from 'tape-promise/tape';
import {Buffer, Device, TextureFormat} from '@luma.gl/core';
import {webglDevice as device} from '@luma.gl/test-utils';
import {getWebGLTestDevice} from '@luma.gl/test-utils';

const EPSILON = 1e-6;
const {abs} = Math;

test('CommandBuffer#copyBufferToBuffer', async t => {
const device = await getWebGLTestDevice();

const sourceData = new Float32Array([1, 2, 3]);
const sourceBuffer = device.createBuffer({data: sourceData});
const destinationData = new Float32Array([4, 5, 6]);
Expand Down Expand Up @@ -113,6 +115,8 @@ const COPY_TEXTURE_TO_BUFFER_FIXTURES: CopyTextureToBufferFixture[] = [
];

test('CommandBuffer#copyTextureToBuffer', async t => {
const device = await getWebGLTestDevice();

for (const fixture of COPY_TEXTURE_TO_BUFFER_FIXTURES) {
await testCopyTextureToBuffer(t, device, {...fixture});
await testCopyTextureToBuffer(t, device, {
Expand Down Expand Up @@ -186,7 +190,9 @@ async function readAsyncF32(source: Buffer): Promise<Float32Array> {
return new Float32Array(buffer, byteOffset, byteLength / Float32Array.BYTES_PER_ELEMENT);
}

test('CommandEncoder#copyTextureToTexture', t => {
test('CommandEncoder#copyTextureToTexture', async t => {
const device = await getWebGLTestDevice();

// for (const device of await getTestDevices()) {
testCopyToTexture(t, device, {isSubCopy: false, sourceIsFramebuffer: false});
// testCopyToTexture(t, device, {isSubCopy: false, sourceIsFramebuffer: true});
Expand Down Expand Up @@ -359,14 +365,14 @@ function testCopyToArray(t: Test, device: Device) {
});
}

test('WebGL1#CopyAndBlit readPixelsToArray', t => {
test('WebGL1#CopyAndBlit readPixelsToArray', async t => {
for (const device of getWebGLTestDevices()) {
testCopyToArray(t, device);
}
t.end();
});

test('WebGL2#CopyAndBlit readPixels', t => {
test('WebGL2#CopyAndBlit readPixels', async t => {
for (const device of getWebGLTestDevices()) {
testCopyToArray(t, device);
}
Expand Down
8 changes: 5 additions & 3 deletions modules/core/test/adapter/resources/compute-pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) vis.gl contributors

import test from 'tape-promise/tape';
import {webgpuDevice, getTestDevices} from '@luma.gl/test-utils';
import {getWebGPUTestDevice} from '@luma.gl/test-utils';
import {ComputePipeline, Buffer} from '@luma.gl/core';

const source = /* WGSL*/ `\
Expand All @@ -18,7 +18,8 @@ const source = /* WGSL*/ `\
`;

test.skip('ComputePipeline#construct/delete', async t => {
await getTestDevices();
const webgpuDevice = await getWebGPUTestDevice();

if (webgpuDevice) {
const shader = webgpuDevice.createShader({source});
const computePipeline = webgpuDevice.createComputePipeline({shader});
Expand All @@ -32,7 +33,8 @@ test.skip('ComputePipeline#construct/delete', async t => {
});

test('ComputePipeline#compute', async t => {
await getTestDevices();
const webgpuDevice = await getWebGPUTestDevice();

if (webgpuDevice) {
const shader = webgpuDevice.createShader({source});
const computePipeline = webgpuDevice.createComputePipeline({
Expand Down
6 changes: 4 additions & 2 deletions modules/core/test/adapter/resources/sampler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) vis.gl contributors

import test, {Test} from 'tape-promise/tape';
import {webglDevice} from '@luma.gl/test-utils';
import {getWebGLTestDevice} from '@luma.gl/test-utils';

import {Device, Sampler} from '@luma.gl/core';

Expand Down Expand Up @@ -66,7 +66,9 @@ export const SAMPLER_PARAMETERS = {
*/
};

test('WebGL#Sampler setParameters', t => {
test('WebGL#Sampler setParameters', async t => {
const webglDevice = await getWebGLTestDevice();

testSampler(t, webglDevice);
testSampler(t, webglDevice);
// testSampler(t, webgpuDevice);
Expand Down
Loading
Loading