diff --git a/src/dawn_native/Format.cpp b/src/dawn_native/Format.cpp index 926c47f9e4..b5d8c84521 100644 --- a/src/dawn_native/Format.cpp +++ b/src/dawn_native/Format.cpp @@ -304,6 +304,7 @@ namespace dawn_native { // because its size isn't well defined, is it 4, 5 or 8? AddMultiAspectFormat(wgpu::TextureFormat::Depth24PlusStencil8, Aspect::Depth | Aspect::Stencil, wgpu::TextureFormat::Depth24Plus, wgpu::TextureFormat::Stencil8, true, true); + // TODO(dawn:690): Implement Depth16Unorm, Depth24UnormStencil8, Depth32FloatStencil8. // BC compressed formats bool isBCFormatSupported = device->IsExtensionEnabled(Extension::TextureCompressionBC); diff --git a/src/dawn_native/Texture.cpp b/src/dawn_native/Texture.cpp index a56e4a8891..9b89880aaf 100644 --- a/src/dawn_native/Texture.cpp +++ b/src/dawn_native/Texture.cpp @@ -301,6 +301,13 @@ namespace dawn_native { return DAWN_VALIDATION_ERROR("Compressed texture must be 2D"); } + // Depth/stencil formats are valid for 2D textures only. Metal has this limit. And D3D12 + // doesn't support depth/stencil formats on 3D textures. + if (descriptor->dimension != wgpu::TextureDimension::e2D && + (format->aspects & (Aspect::Depth | Aspect::Stencil)) != 0) { + return DAWN_VALIDATION_ERROR("Depth/stencil formats are valid for 2D textures only"); + } + DAWN_TRY(ValidateTextureSize(descriptor, format)); return {}; diff --git a/src/tests/unittests/validation/TextureValidationTests.cpp b/src/tests/unittests/validation/TextureValidationTests.cpp index 6c90b09f24..9fd2eec1ab 100644 --- a/src/tests/unittests/validation/TextureValidationTests.cpp +++ b/src/tests/unittests/validation/TextureValidationTests.cpp @@ -401,6 +401,33 @@ namespace { } } + // Test that depth/stencil formats are invalid for 3D texture + TEST_F(TextureValidationTest, DepthStencilFormatsFor3D) { + wgpu::TextureDescriptor descriptor = CreateDefaultTextureDescriptor(); + + wgpu::TextureDimension dimensions[] = { + wgpu::TextureDimension::e1D, + wgpu::TextureDimension::e3D, + }; + + // TODO(dawn:690): Uncomment these depth/stencil formats after we implement them in Dawn. + wgpu::TextureFormat depthStencilFormats[] = { + wgpu::TextureFormat::Depth32Float, wgpu::TextureFormat::Depth24Plus, + wgpu::TextureFormat::Stencil8, wgpu::TextureFormat::Depth24PlusStencil8, + // wgpu::TextureFormat::Depth16Unorm, + // wgpu::TextureFormat::Depth24UnormStencil8, + // wgpu::TextureFormat::Depth32FloatStencil8, + }; + + for (wgpu::TextureDimension dimension : dimensions) { + for (wgpu::TextureFormat format : depthStencilFormats) { + descriptor.format = format; + descriptor.dimension = dimension; + ASSERT_DEVICE_ERROR(device.CreateTexture(&descriptor)); + } + } + } + // Test that it is valid to destroy a texture TEST_F(TextureValidationTest, DestroyTexture) { wgpu::TextureDescriptor descriptor = CreateDefaultTextureDescriptor();