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

WebGPURenderer: Add Offscreen Support #27520

Merged
merged 11 commits into from
Jan 8, 2024
37 changes: 26 additions & 11 deletions examples/jsm/capabilities/WebGPU.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
if ( window.GPUShaderStage === undefined ) {
if ( self.GPUShaderStage === undefined ) {

window.GPUShaderStage = { VERTEX: 1, FRAGMENT: 2, COMPUTE: 4 };
self.GPUShaderStage = { VERTEX: 1, FRAGMENT: 2, COMPUTE: 4 };

}

let isAvailable = false;
class WebGPU {

static isAvailable() {

return navigator.gpu !== undefined;

}


if ( navigator.gpu !== undefined ) {
static async requestStaticAdapter() {

const adapter = await navigator.gpu.requestAdapter();
if ( navigator.gpu ) {

if ( adapter !== null ) {
return await navigator.gpu.requestAdapter();

isAvailable = true;
}

return false;

}

RenaudRohlinger marked this conversation as resolved.
Show resolved Hide resolved
}
static async getStaticAdapter() {

class WebGPU {
if ( this._adapterPromise === undefined ) {

static isAvailable() {
this._adapterPromise = WebGPU.requestStaticAdapter();

return isAvailable;
}

return await this._adapterPromise;

}

Expand Down Expand Up @@ -50,4 +61,8 @@ class WebGPU {

}

// Initialize static properties.

WebGPU.getStaticAdapter();

export default WebGPU;
14 changes: 7 additions & 7 deletions examples/jsm/loaders/KTX2Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ class KTX2Loader extends Loader {

}

detectSupport( renderer ) {
async detectSupport( renderer ) {

if ( renderer.isWebGPURenderer === true ) {

this.workerConfig = {
astcSupported: renderer.hasFeature( 'texture-compression-astc' ),
etc1Supported: renderer.hasFeature( 'texture-compression-etc1' ),
etc2Supported: renderer.hasFeature( 'texture-compression-etc2' ),
dxtSupported: renderer.hasFeature( 'texture-compression-bc' ),
bptcSupported: renderer.hasFeature( 'texture-compression-bptc' ),
pvrtcSupported: renderer.hasFeature( 'texture-compression-pvrtc' )
astcSupported: await renderer.hasFeature( 'texture-compression-astc' ),
etc1Supported: await renderer.hasFeature( 'texture-compression-etc1' ),
etc2Supported: await renderer.hasFeature( 'texture-compression-etc2' ),
dxtSupported: await renderer.hasFeature( 'texture-compression-bc' ),
bptcSupported: await renderer.hasFeature( 'texture-compression-bptc' ),
pvrtcSupported: await renderer.hasFeature( 'texture-compression-pvrtc' )
};

} else {
Expand Down
15 changes: 3 additions & 12 deletions examples/jsm/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js';
import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js';
import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js';
import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js';

// statics

let _staticAdapter = null;

if ( navigator.gpu !== undefined ) {

_staticAdapter = await navigator.gpu.requestAdapter();

}
import WebGPU from '../../capabilities/WebGPU.js';

//

Expand Down Expand Up @@ -1071,9 +1062,9 @@ class WebGPUBackend extends Backend {

}

hasFeature( name ) {
async hasFeature( name ) {
RenaudRohlinger marked this conversation as resolved.
Show resolved Hide resolved

const adapter = this.adapter || _staticAdapter;
const adapter = this.adapter || await WebGPU.getStaticAdapter();

//

Expand Down
5 changes: 3 additions & 2 deletions examples/jsm/renderers/webgpu/WebGPURenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import WebGPU from '../../capabilities/WebGPU.js';
Fixed Show fixed Hide fixed

import Renderer from '../common/Renderer.js';
import WebGLBackend from '../webgl/WebGLBackend.js';
import WebGPUBackend from './WebGPUBackend.js';
import WebGPU from '../../capabilities/WebGPU.js';
/*
const debugHandler = {

Expand All @@ -22,7 +23,7 @@

let BackendClass;

if ( WebGPU.isAvailable() ) {
if ( navigator.gpu ) {

BackendClass = WebGPUBackend;

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getFormat } from '../utils/WebGPUTextureUtils.js';
import WGSLNodeParser from './WGSLNodeParser.js';

// GPUShaderStage is not defined in browsers not supporting WebGPU
const GPUShaderStage = window.GPUShaderStage;
const GPUShaderStage = self.GPUShaderStage;

const gpuShaderStageLib = {
'vertex': GPUShaderStage ? GPUShaderStage.VERTEX : 1,
Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_loader_gltf_compressed.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
controls.maxDistance = 6;
controls.update();

const ktx2Loader = new KTX2Loader()
const ktx2Loader = await new KTX2Loader()
.setTranscoderPath( 'jsm/libs/basis/' )
.detectSupport( renderer );

Expand Down
4 changes: 2 additions & 2 deletions examples/webgpu_morphtargets_face.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

init();

function init() {
async function init() {

if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {

Expand Down Expand Up @@ -79,7 +79,7 @@

container.appendChild( renderer.domElement );

const ktx2Loader = new KTX2Loader()
const ktx2Loader = await new KTX2Loader()
.setTranscoderPath( 'jsm/libs/basis/' )
.detectSupport( renderer );

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
textureDisplace.wrapS = THREE.RepeatWrapping;
textureDisplace.wrapT = THREE.RepeatWrapping;

const ktxLoader = new KTX2Loader()
const ktxLoader = await new KTX2Loader()
.setTranscoderPath( 'jsm/libs/basis/' )
.detectSupport( renderer );

Expand Down
Loading