From 68a1e5038f6b5ece09306d856c13aa2e4c04113b Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Thu, 20 Apr 2023 11:12:47 +0200 Subject: [PATCH] WebGPUTextures: Add `DepthStencilFormat` support. --- .../jsm/renderers/webgpu/WebGPUBindings.js | 6 +-- .../renderers/webgpu/WebGPUSampledTexture.js | 4 +- .../jsm/renderers/webgpu/WebGPUTextures.js | 39 +++++++++++++++---- examples/jsm/renderers/webgpu/constants.js | 6 +++ 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/examples/jsm/renderers/webgpu/WebGPUBindings.js b/examples/jsm/renderers/webgpu/WebGPUBindings.js index f0b5a24a8d708d..07d75b54ef4d15 100644 --- a/examples/jsm/renderers/webgpu/WebGPUBindings.js +++ b/examples/jsm/renderers/webgpu/WebGPUBindings.js @@ -234,11 +234,11 @@ class WebGPUBindings { } else if ( binding.texture.isVideoTexture ) { - binding.textureGPU = this.textures.getVideoDefaultTexture(); + binding.textureGPU = this.textures.getDefaultVideoTexture(); } else if ( binding.texture.isDepthTexture ) { - binding.textureGPU = this.textures.getDepthDefaultTexture(); + binding.textureGPU = this.textures.getDefaultDepthTexture(); } else { @@ -248,7 +248,7 @@ class WebGPUBindings { } - const resource = binding.textureGPU instanceof GPUTexture ? binding.textureGPU.createView( { dimension: binding.dimension } ) : binding.textureGPU; + const resource = binding.textureGPU instanceof GPUTexture ? binding.textureGPU.createView( { aspect: binding.aspect, dimension: binding.dimension } ) : binding.textureGPU; entries.push( { binding: bindingPoint, resource } ); diff --git a/examples/jsm/renderers/webgpu/WebGPUSampledTexture.js b/examples/jsm/renderers/webgpu/WebGPUSampledTexture.js index 194563dc38879c..8355e0e5f34fe5 100644 --- a/examples/jsm/renderers/webgpu/WebGPUSampledTexture.js +++ b/examples/jsm/renderers/webgpu/WebGPUSampledTexture.js @@ -1,5 +1,5 @@ import WebGPUBinding from './WebGPUBinding.js'; -import { GPUBindingType, GPUTextureViewDimension } from './constants.js'; +import { GPUBindingType, GPUTextureViewDimension, GPUTextureAspect } from './constants.js'; class WebGPUSampledTexture extends WebGPUBinding { @@ -16,6 +16,8 @@ class WebGPUSampledTexture extends WebGPUBinding { this.type = GPUBindingType.SampledTexture; this.visibility = GPUShaderStage.FRAGMENT; + this.aspect = texture.isDepthTexture ? GPUTextureAspect.DepthOnly : GPUTextureAspect.All; + this.textureGPU = null; // set by the renderer } diff --git a/examples/jsm/renderers/webgpu/WebGPUTextures.js b/examples/jsm/renderers/webgpu/WebGPUTextures.js index c27511f27fab4a..f177a0328faa52 100644 --- a/examples/jsm/renderers/webgpu/WebGPUTextures.js +++ b/examples/jsm/renderers/webgpu/WebGPUTextures.js @@ -1,8 +1,8 @@ -import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension } from './constants.js'; +import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension, GPUFeatureName } from './constants.js'; import { VideoTexture, CubeTexture, Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping, RGB_ETC2_Format, RGBA_ETC2_EAC_Format, - RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthTexture, + RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthStencilFormat, DepthTexture, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format, - RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, UnsignedIntType, UnsignedShortType + RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, UnsignedIntType, UnsignedShortType, UnsignedInt248Type } from 'three'; import WebGPUTextureUtils from './WebGPUTextureUtils.js'; @@ -37,13 +37,11 @@ class WebGPUTextures { } - getDepthDefaultTexture() { + getDefaultDepthTexture() { if ( this.depthDefaultTexture === null ) { const depthTexture = new DepthTexture(); - depthTexture.minFilter = NearestFilter; - depthTexture.magFilter = NearestFilter; depthTexture.image.width = 1; depthTexture.image.height = 1; @@ -75,7 +73,7 @@ class WebGPUTextures { } - getVideoDefaultTexture() { + getDefaultVideoTexture() { if ( this.defaultVideoTexture === null ) { @@ -828,6 +826,33 @@ class WebGPUTextures { break; + case DepthStencilFormat: + + switch ( type ) { + + case UnsignedInt248Type: + formatGPU = GPUTextureFormat.Depth24PlusStencil8; + break; + + case FloatType: + + if ( this.device.features.has( GPUFeatureName.Depth32FloatStencil8 ) === false ) { + + console.error( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' ); + + } + + formatGPU = GPUTextureFormat.Depth32FloatStencil8; + + break; + + default: + console.error( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.', type ); + + } + + break; + default: console.error( 'WebGPURenderer: Unsupported texture format.', format ); diff --git a/examples/jsm/renderers/webgpu/constants.js b/examples/jsm/renderers/webgpu/constants.js index a80a1cd594750a..96b929a3a2137b 100644 --- a/examples/jsm/renderers/webgpu/constants.js +++ b/examples/jsm/renderers/webgpu/constants.js @@ -290,6 +290,12 @@ export const GPUTextureViewDimension = { ThreeD: '3d' }; +export const GPUTextureAspect = { + All: 'all', + StencilOnly: 'stencil-only', + DepthOnly: 'depth-only' +}; + export const GPUInputStepMode = { Vertex: 'vertex', Instance: 'instance'