From df963e1f77e2d616117081e47d967ca2bb71857c Mon Sep 17 00:00:00 2001 From: Huong Xuan Nguyen Date: Mon, 18 Nov 2024 21:27:20 -0500 Subject: [PATCH 1/6] Fix compilation in react --- src/renderers/webgpu/WebGPUBackend.js | 2 +- src/renderers/webgpu/nodes/WGSLNodeBuilder.js | 6 +++--- src/renderers/webgpu/utils/WebGPUUtils.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/renderers/webgpu/WebGPUBackend.js b/src/renderers/webgpu/WebGPUBackend.js index 9d695aae17a193..fac3ed691819b1 100644 --- a/src/renderers/webgpu/WebGPUBackend.js +++ b/src/renderers/webgpu/WebGPUBackend.js @@ -64,7 +64,7 @@ class WebGPUBackend extends Backend { powerPreference: parameters.powerPreference }; - const adapter = await navigator.gpu.requestAdapter( adapterOptions ); + const adapter = typeof navigator === 'undefined' ? false : await navigator.gpu.requestAdapter( adapterOptions ); if ( adapter === null ) { diff --git a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js index aec75e74d231ec..ad4a7eeb24a42b 100644 --- a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +++ b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js @@ -16,7 +16,7 @@ import { GPUBufferBindingType, GPUStorageTextureAccess } from '../utils/WebGPUCo import { NoColorSpace, FloatType } from '../../../constants.js'; // GPUShaderStage is not defined in browsers not supporting WebGPU -const GPUShaderStage = self.GPUShaderStage; +const GPUShaderStage = ( typeof self !== 'undefined' ) ? self.GPUShaderStage : { VERTEX: 1, FRAGMENT: 2, COMPUTE: 4 }; const gpuShaderStageLib = { 'vertex': GPUShaderStage ? GPUShaderStage.VERTEX : 1, @@ -123,7 +123,7 @@ const wgslMethods = { // WebGPU issue: does not support pow() with negative base on Windows -if ( /Windows/g.test( navigator.userAgent ) ) { +if ( typeof navigator === 'undefined' ? false : /Windows/g.test( navigator.userAgent ) ) { wgslPolyfill.pow_float = new CodeNode( 'fn tsl_pow_float( a : f32, b : f32 ) -> f32 { return select( -pow( -a, b ), pow( a, b ), a > 0.0 ); }' ); wgslPolyfill.pow_vec2 = new CodeNode( 'fn tsl_pow_vec2( a : vec2f, b : vec2f ) -> vec2f { return vec2f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ) ); }', [ wgslPolyfill.pow_float ] ); @@ -141,7 +141,7 @@ if ( /Windows/g.test( navigator.userAgent ) ) { let diagnostics = ''; -if ( /Firefox|Deno/g.test( navigator.userAgent ) !== true ) { +if ( ( typeof navigator === 'undefined' ? false : /Firefox|Deno/g.test( navigator.userAgent ) ) !== true ) { diagnostics += 'diagnostic( off, derivative_uniformity );\n'; diff --git a/src/renderers/webgpu/utils/WebGPUUtils.js b/src/renderers/webgpu/utils/WebGPUUtils.js index f7ba94556d98e4..7c7601ad5ca112 100644 --- a/src/renderers/webgpu/utils/WebGPUUtils.js +++ b/src/renderers/webgpu/utils/WebGPUUtils.js @@ -113,13 +113,13 @@ class WebGPUUtils { // TODO: Remove this check when Quest 34.5 is out // https://github.com/mrdoob/three.js/pull/29221/files#r1731833949 - if ( navigator.userAgent.includes( 'Quest' ) ) { + if ( typeof navigator === 'undefined' ? false : navigator.userAgent.includes( 'Quest' ) ) { return GPUTextureFormat.BGRA8Unorm; } else { - return navigator.gpu.getPreferredCanvasFormat(); + return typeof navigator === 'undefined' ? false : navigator.gpu.getPreferredCanvasFormat(); } From 1cb218319d018db1bf317e43c2adfbb47d637681 Mon Sep 17 00:00:00 2001 From: Huong Xuan Nguyen Date: Tue, 19 Nov 2024 09:34:04 -0500 Subject: [PATCH 2/6] Cleanup fixes --- src/renderers/webgpu/WebGPUBackend.js | 2 +- src/renderers/webgpu/nodes/WGSLNodeBuilder.js | 4 +-- src/renderers/webgpu/utils/WebGPUUtils.js | 27 ++++++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/renderers/webgpu/WebGPUBackend.js b/src/renderers/webgpu/WebGPUBackend.js index fac3ed691819b1..c3874db25e0cf9 100644 --- a/src/renderers/webgpu/WebGPUBackend.js +++ b/src/renderers/webgpu/WebGPUBackend.js @@ -64,7 +64,7 @@ class WebGPUBackend extends Backend { powerPreference: parameters.powerPreference }; - const adapter = typeof navigator === 'undefined' ? false : await navigator.gpu.requestAdapter( adapterOptions ); + const adapter = ( typeof navigator !== 'undefined' ) ? await navigator.gpu.requestAdapter( adapterOptions ) : {}; if ( adapter === null ) { diff --git a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js index ad4a7eeb24a42b..b50a01a4aa2092 100644 --- a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +++ b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js @@ -123,7 +123,7 @@ const wgslMethods = { // WebGPU issue: does not support pow() with negative base on Windows -if ( typeof navigator === 'undefined' ? false : /Windows/g.test( navigator.userAgent ) ) { +if ( typeof navigator !== 'undefined' && /Windows/g.test( navigator.userAgent ) ) { wgslPolyfill.pow_float = new CodeNode( 'fn tsl_pow_float( a : f32, b : f32 ) -> f32 { return select( -pow( -a, b ), pow( a, b ), a > 0.0 ); }' ); wgslPolyfill.pow_vec2 = new CodeNode( 'fn tsl_pow_vec2( a : vec2f, b : vec2f ) -> vec2f { return vec2f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ) ); }', [ wgslPolyfill.pow_float ] ); @@ -141,7 +141,7 @@ if ( typeof navigator === 'undefined' ? false : /Windows/g.test( navigator.userA let diagnostics = ''; -if ( ( typeof navigator === 'undefined' ? false : /Firefox|Deno/g.test( navigator.userAgent ) ) !== true ) { +if ( ( typeof navigator !== 'undefined' && /Firefox|Deno/g.test( navigator.userAgent ) ) !== true ) { diagnostics += 'diagnostic( off, derivative_uniformity );\n'; diff --git a/src/renderers/webgpu/utils/WebGPUUtils.js b/src/renderers/webgpu/utils/WebGPUUtils.js index 7c7601ad5ca112..699d9445b12fc2 100644 --- a/src/renderers/webgpu/utils/WebGPUUtils.js +++ b/src/renderers/webgpu/utils/WebGPUUtils.js @@ -112,17 +112,26 @@ class WebGPUUtils { // TODO: Remove this check when Quest 34.5 is out // https://github.com/mrdoob/three.js/pull/29221/files#r1731833949 - - if ( typeof navigator === 'undefined' ? false : navigator.userAgent.includes( 'Quest' ) ) { - - return GPUTextureFormat.BGRA8Unorm; - + + if ( typeof navigator !== 'undefined' ) { + + if ( navigator.userAgent.includes( 'Quest' ) ) { + + return GPUTextureFormat.BGRA8Unorm; + + } else { + + return navigator.gpu.getPreferredCanvasFormat(); + + } + } else { - - return typeof navigator === 'undefined' ? false : navigator.gpu.getPreferredCanvasFormat(); - + + + return GPUTextureFormat.BGRA8Unorm; + } - + } } From 0b093670140a5772bff04a1e551e64a84f7916da Mon Sep 17 00:00:00 2001 From: Huong Xuan Nguyen Date: Tue, 19 Nov 2024 09:38:05 -0500 Subject: [PATCH 3/6] Fix space --- src/renderers/webgpu/utils/WebGPUUtils.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/renderers/webgpu/utils/WebGPUUtils.js b/src/renderers/webgpu/utils/WebGPUUtils.js index 699d9445b12fc2..0718b9640b70bb 100644 --- a/src/renderers/webgpu/utils/WebGPUUtils.js +++ b/src/renderers/webgpu/utils/WebGPUUtils.js @@ -112,26 +112,25 @@ class WebGPUUtils { // TODO: Remove this check when Quest 34.5 is out // https://github.com/mrdoob/three.js/pull/29221/files#r1731833949 - + if ( typeof navigator !== 'undefined' ) { - + if ( navigator.userAgent.includes( 'Quest' ) ) { - + return GPUTextureFormat.BGRA8Unorm; - + } else { - + return navigator.gpu.getPreferredCanvasFormat(); - + } - + } else { - - + return GPUTextureFormat.BGRA8Unorm; - + } - + } } From 78800ae213a53d1ac02fe8f98ea721079c3c2de7 Mon Sep 17 00:00:00 2001 From: Huong Xuan Nguyen Date: Tue, 19 Nov 2024 09:40:09 -0500 Subject: [PATCH 4/6] Fix space --- src/renderers/webgpu/nodes/WGSLNodeBuilder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js index b50a01a4aa2092..11d7f45fea24bf 100644 --- a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +++ b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js @@ -123,7 +123,7 @@ const wgslMethods = { // WebGPU issue: does not support pow() with negative base on Windows -if ( typeof navigator !== 'undefined' && /Windows/g.test( navigator.userAgent ) ) { +if ( typeof navigator !== 'undefined' && /Windows/g.test( navigator.userAgent ) ) { wgslPolyfill.pow_float = new CodeNode( 'fn tsl_pow_float( a : f32, b : f32 ) -> f32 { return select( -pow( -a, b ), pow( a, b ), a > 0.0 ); }' ); wgslPolyfill.pow_vec2 = new CodeNode( 'fn tsl_pow_vec2( a : vec2f, b : vec2f ) -> vec2f { return vec2f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ) ); }', [ wgslPolyfill.pow_float ] ); From a32f4873e2856c5c624bd3ff29db622443cccb20 Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Tue, 19 Nov 2024 22:17:35 +0100 Subject: [PATCH 5/6] Update WebGPUBackend.js Use `null` as fallback when navigator isn't available. --- src/renderers/webgpu/WebGPUBackend.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/webgpu/WebGPUBackend.js b/src/renderers/webgpu/WebGPUBackend.js index c3874db25e0cf9..eba50888132d85 100644 --- a/src/renderers/webgpu/WebGPUBackend.js +++ b/src/renderers/webgpu/WebGPUBackend.js @@ -64,7 +64,7 @@ class WebGPUBackend extends Backend { powerPreference: parameters.powerPreference }; - const adapter = ( typeof navigator !== 'undefined' ) ? await navigator.gpu.requestAdapter( adapterOptions ) : {}; + const adapter = ( typeof navigator !== 'undefined' ) ? await navigator.gpu.requestAdapter( adapterOptions ) : null; if ( adapter === null ) { From 07c95d6dff0cf5dbdeee440ef422dde239b8014d Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Sun, 24 Nov 2024 10:30:21 +0100 Subject: [PATCH 6/6] Update WebGPUUtils.js Revert. --- src/renderers/webgpu/utils/WebGPUUtils.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/renderers/webgpu/utils/WebGPUUtils.js b/src/renderers/webgpu/utils/WebGPUUtils.js index 0718b9640b70bb..f7ba94556d98e4 100644 --- a/src/renderers/webgpu/utils/WebGPUUtils.js +++ b/src/renderers/webgpu/utils/WebGPUUtils.js @@ -113,21 +113,13 @@ class WebGPUUtils { // TODO: Remove this check when Quest 34.5 is out // https://github.com/mrdoob/three.js/pull/29221/files#r1731833949 - if ( typeof navigator !== 'undefined' ) { + if ( navigator.userAgent.includes( 'Quest' ) ) { - if ( navigator.userAgent.includes( 'Quest' ) ) { - - return GPUTextureFormat.BGRA8Unorm; - - } else { - - return navigator.gpu.getPreferredCanvasFormat(); - - } + return GPUTextureFormat.BGRA8Unorm; } else { - return GPUTextureFormat.BGRA8Unorm; + return navigator.gpu.getPreferredCanvasFormat(); }