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

[WIP] switch to WebGPU #4939

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
398 changes: 150 additions & 248 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/model-viewer-effects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"@rollup/plugin-replace": "^6.0.1",
"@types/mocha": "^10.0.9",
"@types/pngjs": "^6.0.1",
"@types/three": "^0.169.0",
"@types/three": "^0.170.0",
"@ungap/event-target": "^0.2.3",
"@web/test-runner": "^0.19.0",
"@web/test-runner-playwright": "^0.11.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/model-viewer-effects/src/test/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ export function screenshot(element: ModelViewerElement): Uint8Array {
if (!renderer)
throw new Error('Invalid element provided');

const screenshotContext = renderer.threeRenderer.getContext();
const screenshotContext =
renderer.threeRenderer.getContext() as unknown as WebGL2RenderingContext;
const width = screenshotContext.drawingBufferWidth;
const height = screenshotContext.drawingBufferHeight;

Expand Down
2 changes: 1 addition & 1 deletion packages/model-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"@rollup/plugin-replace": "^6.0.1",
"@types/mocha": "^10.0.9",
"@types/pngjs": "^6.0.1",
"@types/three": "^0.169.0",
"@types/three": "^0.170.0",
"@ungap/event-target": "^0.2.3",
"@web/test-runner": "^0.19.0",
"@web/test-runner-playwright": "^0.11.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/model-viewer/src/features/scene-graph/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ export class Image extends ThreeDOMElement implements ImageInterface {
threeRenderer.render(scene, camera);
threeRenderer.setRenderTarget(null);

const buffer = new Uint8Array(width * height * 4);
threeRenderer.readRenderTargetPixels(
renderTarget, 0, 0, width, height, buffer);
const buffer = await threeRenderer.readRenderTargetPixelsAsync(
renderTarget, 0, 0, width, height);

blobCanvas.width = width;
blobCanvas.height = height;
Expand Down
3 changes: 2 additions & 1 deletion packages/model-viewer/src/model-viewer-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ export default class ModelViewerElementBase extends ReactiveElement {
* @param effectComposer An EffectComposer from `pmndrs/postprocessing`
*/
registerEffectComposer(effectComposer: EffectComposerInterface) {
effectComposer.setRenderer(this[$renderer].threeRenderer);
effectComposer.setRenderer(
this[$renderer].threeRenderer as unknown as WebGLRenderer);
effectComposer.setMainCamera(this[$scene].getCamera());
effectComposer.setMainScene(this[$scene]);
this[$scene].effectRenderer = effectComposer;
Expand Down
47 changes: 24 additions & 23 deletions packages/model-viewer/src/test/model-viewer-base-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {expect} from 'chai';

import {$renderer, $scene, $userInputElement} from '../model-viewer-base.js';
import {ModelViewerElement} from '../model-viewer.js';
import {Renderer} from '../three-components/Renderer.js';
// import {Renderer} from '../three-components/Renderer.js';
import {timePasses, waitForEvent} from '../utilities.js';

import {assetPath, until} from './helpers.js';
Expand Down Expand Up @@ -116,28 +116,29 @@ suite('ModelViewerElementBase', () => {
expect((event as any).detail.type).to.be.eq('loadfailure');
});

test('when losing the GL context, dispatches an error event', async () => {
const {threeRenderer, canvas3D} = Renderer.singleton;

canvas3D.addEventListener('webglcontextlost', function(event) {
event.preventDefault();
Renderer.resetSingleton();
}, false);

const errorEventDispatches = waitForEvent(element, 'error');
// We make a best effort to simulate the real scenario here, but
// for some cases like headless Chrome WebGL might be disabled,
// so we simulate the scenario.
// @see https://threejs.org/docs/index.html#api/en/renderers/WebGLRenderer.forceContextLoss
if (threeRenderer.getContext() != null) {
threeRenderer.forceContextLoss();
} else {
threeRenderer.domElement.dispatchEvent(
new CustomEvent('webglcontextlost'));
}
const event = await errorEventDispatches;
expect((event as any).detail.type).to.be.equal('webglcontextlost');
});
// test('when losing the GL context, dispatches an error event', async ()
// => {
// const {threeRenderer, canvas3D} = Renderer.singleton;

// canvas3D.addEventListener('webglcontextlost', function(event) {
// event.preventDefault();
// Renderer.resetSingleton();
// }, false);

// const errorEventDispatches = waitForEvent(element, 'error');
// // We make a best effort to simulate the real scenario here, but
// // for some cases like headless Chrome WebGL might be disabled,
// // so we simulate the scenario.
// // @see https://threejs.org/docs/index.html#api/en/renderers/WebGLRenderer.forceContextLoss
// if (threeRenderer.getContext() != null) {
// threeRenderer.forceContextLoss();
// } else {
// threeRenderer.domElement.dispatchEvent(
// new CustomEvent('webglcontextlost'));
// }
// const event = await errorEventDispatches;
// expect((event as any).detail.type).to.be.equal('webglcontextlost');
// });

suite('capturing screenshots', () => {
let width: number;
Expand Down
9 changes: 6 additions & 3 deletions packages/model-viewer/src/test/model-viewer-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,22 @@ suite('ModelViewerElement', () => {

test('Metal roughness sphere with generated lighting', async () => {
await setupLighting(element);
const screenshotContext = element[$renderer].threeRenderer.getContext();
const screenshotContext = element[$renderer].threeRenderer.getContext() as
unknown as WebGL2RenderingContext;
testFidelity(screenshotContext);
});

test('Metal roughness sphere with HDR lighting', async () => {
await setupLighting(element, SUNRISE_HDR_PATH);
const screenshotContext = element[$renderer].threeRenderer.getContext();
const screenshotContext = element[$renderer].threeRenderer.getContext() as
unknown as WebGL2RenderingContext;
testFidelity(screenshotContext);
});

test('Metal roughness sphere with LDR lighting', async () => {
await setupLighting(element, SUNRISE_LDR_PATH);
const screenshotContext = element[$renderer].threeRenderer.getContext();
const screenshotContext = element[$renderer].threeRenderer.getContext() as
unknown as WebGL2RenderingContext;
testFidelity(screenshotContext);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/

import {expect} from 'chai';
import {Cache, CubeReflectionMapping, EquirectangularReflectionMapping, WebGLRenderer} from 'three';
import {Cache, CubeReflectionMapping, EquirectangularReflectionMapping} from 'three';
import {WebGPURenderer} from 'three/webgpu';

import TextureUtils from '../../three-components/TextureUtils.js';
import {assetPath} from '../helpers.js';
Expand All @@ -26,14 +27,14 @@ const EQUI_URL = assetPath('environments/spruit_sunrise_1k_LDR.jpg');
const HDR_EQUI_URL = assetPath('environments/spruit_sunrise_1k_HDR.hdr');

suite('TextureUtils', () => {
let threeRenderer: WebGLRenderer;
let threeRenderer: WebGPURenderer;

suiteSetup(() => {
// The threeRenderer can retain state, so these tests have the possibility
// of getting different results in different orders. However, our use of the
// threeRenderer *should* always return its state to what it was before to
// avoid this kind of problem (and many other headaches).
threeRenderer = new WebGLRenderer({canvas});
threeRenderer = new WebGPURenderer({canvas});
threeRenderer.debug.checkShaderErrors = true;
});

Expand Down
11 changes: 5 additions & 6 deletions packages/model-viewer/src/three-components/ARRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import {BoxGeometry, BufferGeometry, Event as ThreeEvent, EventDispatcher, Line, Matrix4, Mesh, PerspectiveCamera, Quaternion, Vector3, WebGLRenderer, XRControllerEventType, XRTargetRaySpace} from 'three';
import {XREstimatedLight} from 'three/examples/jsm/webxr/XREstimatedLight.js';
import {WebGPURenderer} from 'three/webgpu';

import {CameraChangeDetails, ControlsInterface} from '../features/controls.js';
import {$currentBackground, $currentEnvironmentMap} from '../features/environment.js';
Expand Down Expand Up @@ -73,17 +74,15 @@ export const ARTracking: {[index: string]: ARTracking} = {
NOT_TRACKING: 'not-tracking'
};

export interface ARTrackingEvent extends ThreeEvent {
export interface ARTrackingEvent extends ThreeEvent{
status: ARTracking,
}

interface UserData {
turning: boolean, box: Mesh, line: Line
}

interface Controller extends XRTargetRaySpace {
userData: UserData
}
interface Controller extends XRTargetRaySpace{userData: UserData}

interface XRControllerEvent {
type: XRControllerEventType, data: XRInputSource, target: Controller
Expand Down Expand Up @@ -150,7 +149,7 @@ export class ARRenderer extends EventDispatcher<

constructor(private renderer: Renderer) {
super();
this.threeRenderer = renderer.threeRenderer;
this.threeRenderer = renderer.threeRenderer as unknown as WebGLRenderer;
this.threeRenderer.xr.enabled = true;
}

Expand Down Expand Up @@ -1029,7 +1028,7 @@ export class ARRenderer extends EventDispatcher<
this.renderer.preRender(scene, time, delta);
this.lastTick = time;

scene.renderShadow(this.threeRenderer);
scene.renderShadow(this.threeRenderer as unknown as WebGPURenderer);
}

this.threeRenderer.render(scene, scene.getCamera());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* limitations under the License.
*/

import {EventDispatcher, Texture, WebGLRenderer} from 'three';
import {EventDispatcher, Texture} from 'three';
import {MeshoptDecoder} from 'three/examples/jsm/libs/meshopt_decoder.module.js';
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js';
import {GLTF, GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
import {KTX2Loader} from 'three/examples/jsm/loaders/KTX2Loader.js';
import {WebGPURenderer} from 'three/webgpu';

import ModelViewerElementBase from '../model-viewer-base.js';
import {CacheEvictionPolicy} from '../utilities/cache-eviction-policy.js';
Expand Down Expand Up @@ -109,7 +110,7 @@ export class CachingGLTFLoader<T extends GLTFInstanceConstructor =
return meshoptDecoderLocation;
}

static initializeKTX2Loader(renderer: WebGLRenderer) {
static initializeKTX2Loader(renderer: WebGPURenderer) {
ktx2Loader.detectSupport(renderer);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/model-viewer/src/three-components/ModelScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* limitations under the License.
*/

import {ACESFilmicToneMapping, AnimationAction, AnimationActionLoopStyles, AnimationClip, AnimationMixer, AnimationMixerEventMap, Box3, Camera, Euler, Event as ThreeEvent, LoopPingPong, LoopRepeat, Material, Matrix3, Mesh, Object3D, PerspectiveCamera, Raycaster, Scene, Sphere, Texture, ToneMapping, Triangle, Vector2, Vector3, WebGLRenderer, XRTargetRaySpace} from 'three';
import {ACESFilmicToneMapping, AnimationAction, AnimationActionLoopStyles, AnimationClip, AnimationMixer, AnimationMixerEventMap, Box3, Camera, Euler, Event as ThreeEvent, LoopPingPong, LoopRepeat, Material, Matrix3, Mesh, Object3D, PerspectiveCamera, Raycaster, Scene, Sphere, Texture, ToneMapping, Triangle, Vector2, Vector3, XRTargetRaySpace} from 'three';
import {CSS2DRenderer} from 'three/examples/jsm/renderers/CSS2DRenderer.js';
import {reduceVertices} from 'three/examples/jsm/utils/SceneUtils.js';
import {WebGPURenderer} from 'three/webgpu';

import {$currentGLTF, $model, $originalGltfJson} from '../features/scene-graph.js';
import {$nodeFromIndex, $nodeFromPoint} from '../features/scene-graph/model.js';
Expand Down Expand Up @@ -801,7 +802,7 @@ export class ModelScene extends Scene {
}
}

renderShadow(renderer: WebGLRenderer) {
renderShadow(renderer: WebGPURenderer) {
const shadow = this.shadow;
if (shadow != null && shadow.needsUpdate == true) {
shadow.render(renderer, this);
Expand Down
15 changes: 9 additions & 6 deletions packages/model-viewer/src/three-components/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* limitations under the License.
*/

import {ACESFilmicToneMapping, Event, EventDispatcher, NeutralToneMapping, Vector2, WebGLRenderer} from 'three';
import {ACESFilmicToneMapping, Event, EventDispatcher, NeutralToneMapping, Vector2} from 'three';
import {WebGPURenderer} from 'three/webgpu';

import {$updateEnvironment} from '../features/environment.js';
import {ModelViewerGlobalConfig} from '../features/loading.js';
Expand Down Expand Up @@ -94,7 +95,7 @@ export class Renderer extends
}
}

public threeRenderer!: WebGLRenderer;
public threeRenderer!: WebGPURenderer;
public canvas3D: HTMLCanvasElement;
public textureUtils: TextureUtils|null;
public arRenderer: ARRenderer;
Expand Down Expand Up @@ -141,19 +142,21 @@ export class Renderer extends
this.canvas3D.classList.add('show');

try {
this.threeRenderer = new WebGLRenderer({
this.threeRenderer = new WebGPURenderer({
canvas: this.canvas3D,
alpha: true,
antialias: true,
powerPreference: options.powerPreference as WebGLPowerPreference,
preserveDrawingBuffer: true,
powerPreference: options.powerPreference as GPUPowerPreference,
});
this.threeRenderer.autoClear = true;
this.threeRenderer.setPixelRatio(1); // handle pixel ratio externally

this.threeRenderer.debug = {
checkShaderErrors: !!options.debug,
onShaderError: null
onShaderError: null,
getShaderAsync: async () => {
return {fragmentShader: null, vertexShader: null};
},
};

// ACESFilmicToneMapping appears to be the most "saturated",
Expand Down
11 changes: 6 additions & 5 deletions packages/model-viewer/src/three-components/Shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* limitations under the License.
*/

import {BackSide, Box3, Material, Mesh, MeshBasicMaterial, MeshDepthMaterial, Object3D, OrthographicCamera, PlaneGeometry, RenderTargetOptions, RGBAFormat, Scene, ShaderMaterial, Vector3, WebGLRenderer, WebGLRenderTarget} from 'three';
import {BackSide, Box3, Material, Mesh, MeshBasicMaterial, MeshDepthMaterial, Object3D, OrthographicCamera, PlaneGeometry, RenderTargetOptions, RGBAFormat, Scene, ShaderMaterial, Vector3, WebGLRenderTarget} from 'three';
import {HorizontalBlurShader} from 'three/examples/jsm/shaders/HorizontalBlurShader.js';
import {VerticalBlurShader} from 'three/examples/jsm/shaders/VerticalBlurShader.js';
import {lerp} from 'three/src/math/MathUtils.js';
import {WebGPURenderer} from 'three/webgpu';

import {ModelScene} from './ModelScene.js';

Expand Down Expand Up @@ -271,7 +272,7 @@ export class Shadow extends Object3D {
return 0.001 * this.maxDimension;
}

render(renderer: WebGLRenderer, scene: Scene) {
render(renderer: WebGPURenderer, scene: Scene) {
// this.cameraHelper.visible = false;

// force the depthMaterial to everything
Expand Down Expand Up @@ -304,7 +305,7 @@ export class Shadow extends Object3D {
// this.cameraHelper.visible = true;
}

blurShadow(renderer: WebGLRenderer) {
blurShadow(renderer: WebGPURenderer) {
const {
camera,
horizontalBlurMaterial,
Expand All @@ -321,7 +322,7 @@ export class Shadow extends Object3D {
horizontalBlurMaterial.uniforms.tDiffuse.value = this.renderTarget!.texture;

renderer.setRenderTarget(renderTargetBlur);
renderer.render(blurPlane, camera);
renderer.render(blurPlane as unknown as Scene, camera);

// blur vertically and draw in the main renderTarget
blurPlane.material = verticalBlurMaterial;
Expand All @@ -330,7 +331,7 @@ export class Shadow extends Object3D {
this.renderTargetBlur!.texture;

renderer.setRenderTarget(renderTarget);
renderer.render(blurPlane, camera);
renderer.render(blurPlane as unknown as Scene, camera);

blurPlane.visible = false;
}
Expand Down
11 changes: 7 additions & 4 deletions packages/model-viewer/src/three-components/TextureUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import {GainMapDecoderMaterial, HDRJPGLoader, QuadRenderer} from '@monogrid/gainmap-js';
import {BackSide, BoxGeometry, CubeCamera, CubeTexture, DataTexture, EquirectangularReflectionMapping, HalfFloatType, LinearSRGBColorSpace, Loader, Mesh, NoBlending, NoToneMapping, RGBAFormat, Scene, ShaderMaterial, SRGBColorSpace, Texture, TextureLoader, Vector3, WebGLCubeRenderTarget, WebGLRenderer} from 'three';
import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader.js';
import {WebGPURenderer} from 'three/webgpu';

import {deserializeUrl, timePasses} from '../utilities.js';

Expand Down Expand Up @@ -49,7 +50,7 @@ export default class TextureUtils {
private blurMaterial: ShaderMaterial|null = null;
private blurScene: Scene|null = null;

constructor(private threeRenderer: WebGLRenderer) {
constructor(private threeRenderer: WebGPURenderer) {
}

private ldrLoader(withCredentials: boolean): TextureLoader {
Expand All @@ -62,7 +63,8 @@ export default class TextureUtils {

private imageLoader(withCredentials: boolean): HDRJPGLoader {
if (this._imageLoader == null) {
this._imageLoader = new HDRJPGLoader(this.threeRenderer);
this._imageLoader =
new HDRJPGLoader(this.threeRenderer as unknown as WebGLRenderer);
}
this._imageLoader.setWithCredentials(withCredentials);
return this._imageLoader;
Expand Down Expand Up @@ -238,7 +240,7 @@ export default class TextureUtils {
renderer.toneMapping = NoToneMapping;
renderer.outputColorSpace = LinearSRGBColorSpace;

cubeCamera.update(renderer, scene);
cubeCamera.update(renderer as unknown as WebGLRenderer, scene);

this.blurCubemap(cubeTarget, GENERATED_SIGMA);

Expand Down Expand Up @@ -341,7 +343,8 @@ export default class TextureUtils {
blurUniforms['dTheta'].value = radiansPerPixel;

const cubeCamera = new CubeCamera(0.1, 100, targetOut);
cubeCamera.update(this.threeRenderer, this.blurScene!);
cubeCamera.update(
this.threeRenderer as unknown as WebGLRenderer, this.blurScene!);
}

private getBlurShader(maxSamples: number) {
Expand Down
Loading
Loading