From 8f9523eb38cef6f758fb9a9bd9516010254299e6 Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Fri, 19 Jun 2020 16:21:33 +0000 Subject: [PATCH] Add support for multisampled sampling This CL adds support for multisampled sampling on the D3D12 backend. This was already working on other backends. It also adds tests that all of the sample locations are correct. Bug: dawn:431 Change-Id: I6849e5e2d708ad4824e6db2665d668d43a4ef5ea Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/23245 Commit-Queue: Austin Eng Reviewed-by: Corentin Wallez --- src/common/BUILD.gn | 3 +- src/common/Compiler.h | 6 + src/dawn_native/BindGroupLayout.cpp | 60 +++- src/dawn_native/BindGroupLayout.h | 3 + src/dawn_native/PipelineLayout.cpp | 9 +- src/dawn_native/d3d12/TextureD3D12.cpp | 67 +++-- src/dawn_native/opengl/PipelineGL.cpp | 2 +- src/dawn_native/opengl/TextureGL.cpp | 7 +- src/tests/BUILD.gn | 1 + src/tests/DawnTest.h | 8 + .../end2end/MultisampledRenderingTests.cpp | 33 ++- .../end2end/MultisampledSamplingTests.cpp | 267 ++++++++++++++++++ .../validation/BindGroupValidationTests.cpp | 181 ++++++++++-- .../GetBindGroupLayoutValidationTests.cpp | 4 - .../RenderPipelineValidationTests.cpp | 26 ++ 15 files changed, 605 insertions(+), 72 deletions(-) create mode 100644 src/tests/end2end/MultisampledSamplingTests.cpp diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn index 59f6ae418d..59a2305722 100644 --- a/src/common/BUILD.gn +++ b/src/common/BUILD.gn @@ -96,15 +96,16 @@ config("dawn_internal") { "-Wc++11-narrowing", "-Wdeprecated-copy", "-Wextra-semi-stmt", + "-Wimplicit-fallthrough", "-Winconsistent-missing-destructor-override", "-Winvalid-offsetof", "-Wmissing-field-initializers", + "-Wnon-c-typedef-for-linkage", "-Wpessimizing-move", "-Wreturn-std-move-in-c++11", "-Wshadow-field", "-Wstrict-prototypes", "-Wtautological-unsigned-zero-compare", - "-Wnon-c-typedef-for-linkage", ] if (is_win) { diff --git a/src/common/Compiler.h b/src/common/Compiler.h index 8e425c9058..49e6db4c5e 100644 --- a/src/common/Compiler.h +++ b/src/common/Compiler.h @@ -107,4 +107,10 @@ extern void __cdecl __debugbreak(void); # define DAWN_FORCE_INLINE inline #endif +#if defined(__clang__) +# define DAWN_FALLTHROUGH [[clang::fallthrough]] +#else +# define DAWN_FALLTHROUGH +#endif + #endif // COMMON_COMPILER_H_ diff --git a/src/dawn_native/BindGroupLayout.cpp b/src/dawn_native/BindGroupLayout.cpp index e10f7e2b90..97c647e337 100644 --- a/src/dawn_native/BindGroupLayout.cpp +++ b/src/dawn_native/BindGroupLayout.cpp @@ -129,15 +129,60 @@ namespace dawn_native { case wgpu::TextureViewDimension::e2D: case wgpu::TextureViewDimension::e2DArray: case wgpu::TextureViewDimension::e3D: - case wgpu::TextureViewDimension::Undefined: return {}; + case wgpu::TextureViewDimension::Undefined: default: UNREACHABLE(); return {}; } } + MaybeError ValidateBindingCanBeMultisampled(wgpu::BindingType bindingType, + wgpu::TextureViewDimension viewDimension) { + switch (bindingType) { + case wgpu::BindingType::ReadonlyStorageTexture: + case wgpu::BindingType::WriteonlyStorageTexture: + return DAWN_VALIDATION_ERROR("Storage textures may not be multisampled"); + + case wgpu::BindingType::SampledTexture: + break; + + case wgpu::BindingType::StorageBuffer: + case wgpu::BindingType::UniformBuffer: + case wgpu::BindingType::ReadonlyStorageBuffer: + case wgpu::BindingType::Sampler: + case wgpu::BindingType::ComparisonSampler: + case wgpu::BindingType::StorageTexture: + default: + UNREACHABLE(); + return {}; + } + + switch (viewDimension) { + case wgpu::TextureViewDimension::e2D: + break; + + case wgpu::TextureViewDimension::e2DArray: + return DAWN_VALIDATION_ERROR("2D array textures may not be multisampled"); + + case wgpu::TextureViewDimension::Cube: + case wgpu::TextureViewDimension::CubeArray: + return DAWN_VALIDATION_ERROR("Cube textures may not be multisampled"); + + case wgpu::TextureViewDimension::e3D: + return DAWN_VALIDATION_ERROR("3D textures may not be multisampled"); + + case wgpu::TextureViewDimension::e1D: + case wgpu::TextureViewDimension::Undefined: + default: + UNREACHABLE(); + return {}; + } + + return {}; + } + MaybeError ValidateBindGroupLayoutDescriptor(DeviceBase* device, const BindGroupLayoutDescriptor* descriptor) { if (descriptor->nextInChain != nullptr) { @@ -155,8 +200,10 @@ namespace dawn_native { DAWN_TRY(ValidateBindingType(entry.type)); DAWN_TRY(ValidateTextureComponentType(entry.textureComponentType)); + wgpu::TextureViewDimension viewDimension = wgpu::TextureViewDimension::e2D; if (entry.viewDimension != wgpu::TextureViewDimension::Undefined) { DAWN_TRY(ValidateTextureViewDimension(entry.viewDimension)); + viewDimension = entry.viewDimension; } if (bindingsSet.count(bindingNumber) != 0) { @@ -167,7 +214,11 @@ namespace dawn_native { DAWN_TRY(ValidateStorageTextureFormat(device, entry.type, entry.storageTextureFormat)); - DAWN_TRY(ValidateStorageTextureViewDimension(entry.type, entry.viewDimension)); + DAWN_TRY(ValidateStorageTextureViewDimension(entry.type, viewDimension)); + + if (entry.multisampled) { + DAWN_TRY(ValidateBindingCanBeMultisampled(entry.type, viewDimension)); + } switch (entry.type) { case wgpu::BindingType::UniformBuffer: @@ -194,11 +245,6 @@ namespace dawn_native { return DAWN_VALIDATION_ERROR("storage textures aren't supported (yet)"); } - if (entry.multisampled) { - return DAWN_VALIDATION_ERROR( - "BindGroupLayoutEntry::multisampled must be false (for now)"); - } - bindingsSet.insert(bindingNumber); } diff --git a/src/dawn_native/BindGroupLayout.h b/src/dawn_native/BindGroupLayout.h index 9cb7e59802..8c0061c984 100644 --- a/src/dawn_native/BindGroupLayout.h +++ b/src/dawn_native/BindGroupLayout.h @@ -46,6 +46,9 @@ namespace dawn_native { MaybeError ValidateStorageTextureViewDimension(wgpu::BindingType bindingType, wgpu::TextureViewDimension dimension); + MaybeError ValidateBindingCanBeMultisampled(wgpu::BindingType bindingType, + wgpu::TextureViewDimension viewDimension); + // Bindings are specified as a |BindingNumber| in the BindGroupLayoutDescriptor. // These numbers may be arbitrary and sparse. Internally, Dawn packs these numbers // into a packed range of |BindingIndex| integers. diff --git a/src/dawn_native/PipelineLayout.cpp b/src/dawn_native/PipelineLayout.cpp index a4c1833105..418ff18e82 100644 --- a/src/dawn_native/PipelineLayout.cpp +++ b/src/dawn_native/PipelineLayout.cpp @@ -145,10 +145,6 @@ namespace dawn_native { BindingNumber bindingNumber = it.first; const ShaderModuleBase::ShaderBindingInfo& bindingInfo = it.second; - if (bindingInfo.multisampled) { - return DAWN_VALIDATION_ERROR("Multisampled textures not supported (yet)"); - } - BindGroupLayoutEntry bindingSlot; bindingSlot.binding = static_cast(bindingNumber); @@ -159,6 +155,11 @@ namespace dawn_native { DAWN_TRY(ValidateStorageTextureViewDimension(bindingInfo.type, bindingInfo.viewDimension)); + if (bindingInfo.multisampled) { + DAWN_TRY(ValidateBindingCanBeMultisampled(bindingInfo.type, + bindingInfo.viewDimension)); + } + bindingSlot.visibility = GetShaderStageVisibilityWithBindingType(bindingInfo.type); diff --git a/src/dawn_native/d3d12/TextureD3D12.cpp b/src/dawn_native/d3d12/TextureD3D12.cpp index 5a6b600f6a..7b86367c62 100644 --- a/src/dawn_native/d3d12/TextureD3D12.cpp +++ b/src/dawn_native/d3d12/TextureD3D12.cpp @@ -992,35 +992,50 @@ namespace dawn_native { namespace d3d12 { // Currently we always use D3D12_TEX2D_ARRAY_SRV because we cannot specify base array layer // and layer count in D3D12_TEX2D_SRV. For 2D texture views, we treat them as 1-layer 2D // array textures. + // Multisampled textures may only be one array layer, so we use + // D3D12_SRV_DIMENSION_TEXTURE2DMS. // https://docs.microsoft.com/en-us/windows/desktop/api/d3d12/ns-d3d12-d3d12_tex2d_srv // https://docs.microsoft.com/en-us/windows/desktop/api/d3d12/ns-d3d12-d3d12_tex2d_array_srv // TODO(jiawei.shao@intel.com): support more texture view dimensions. - // TODO(jiawei.shao@intel.com): support creating SRV on multisampled textures. - switch (descriptor->dimension) { - case wgpu::TextureViewDimension::e2D: - case wgpu::TextureViewDimension::e2DArray: - ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D); - mSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; - mSrvDesc.Texture2DArray.ArraySize = descriptor->arrayLayerCount; - mSrvDesc.Texture2DArray.FirstArraySlice = descriptor->baseArrayLayer; - mSrvDesc.Texture2DArray.MipLevels = descriptor->mipLevelCount; - mSrvDesc.Texture2DArray.MostDetailedMip = descriptor->baseMipLevel; - mSrvDesc.Texture2DArray.PlaneSlice = 0; - mSrvDesc.Texture2DArray.ResourceMinLODClamp = 0; - break; - case wgpu::TextureViewDimension::Cube: - case wgpu::TextureViewDimension::CubeArray: - ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D); - ASSERT(descriptor->arrayLayerCount % 6 == 0); - mSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY; - mSrvDesc.TextureCubeArray.First2DArrayFace = descriptor->baseArrayLayer; - mSrvDesc.TextureCubeArray.NumCubes = descriptor->arrayLayerCount / 6; - mSrvDesc.TextureCubeArray.MostDetailedMip = descriptor->baseMipLevel; - mSrvDesc.TextureCubeArray.MipLevels = descriptor->mipLevelCount; - mSrvDesc.TextureCubeArray.ResourceMinLODClamp = 0; - break; - default: - UNREACHABLE(); + if (GetTexture()->IsMultisampledTexture()) { + switch (descriptor->dimension) { + case wgpu::TextureViewDimension::e2DArray: + ASSERT(texture->GetArrayLayers() == 1); + DAWN_FALLTHROUGH; + case wgpu::TextureViewDimension::e2D: + ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D); + mSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMS; + break; + default: + UNREACHABLE(); + } + } else { + switch (descriptor->dimension) { + case wgpu::TextureViewDimension::e2D: + case wgpu::TextureViewDimension::e2DArray: + ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D); + mSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; + mSrvDesc.Texture2DArray.ArraySize = descriptor->arrayLayerCount; + mSrvDesc.Texture2DArray.FirstArraySlice = descriptor->baseArrayLayer; + mSrvDesc.Texture2DArray.MipLevels = descriptor->mipLevelCount; + mSrvDesc.Texture2DArray.MostDetailedMip = descriptor->baseMipLevel; + mSrvDesc.Texture2DArray.PlaneSlice = 0; + mSrvDesc.Texture2DArray.ResourceMinLODClamp = 0; + break; + case wgpu::TextureViewDimension::Cube: + case wgpu::TextureViewDimension::CubeArray: + ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D); + ASSERT(descriptor->arrayLayerCount % 6 == 0); + mSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY; + mSrvDesc.TextureCubeArray.First2DArrayFace = descriptor->baseArrayLayer; + mSrvDesc.TextureCubeArray.NumCubes = descriptor->arrayLayerCount / 6; + mSrvDesc.TextureCubeArray.MostDetailedMip = descriptor->baseMipLevel; + mSrvDesc.TextureCubeArray.MipLevels = descriptor->mipLevelCount; + mSrvDesc.TextureCubeArray.ResourceMinLODClamp = 0; + break; + default: + UNREACHABLE(); + } } } diff --git a/src/dawn_native/opengl/PipelineGL.cpp b/src/dawn_native/opengl/PipelineGL.cpp index 41dab2df65..41728be93e 100644 --- a/src/dawn_native/opengl/PipelineGL.cpp +++ b/src/dawn_native/opengl/PipelineGL.cpp @@ -219,7 +219,7 @@ namespace dawn_native { namespace opengl { } const std::vector& PipelineGL::GetTextureUnitsForTextureView(GLuint index) const { - ASSERT(index < mUnitsForSamplers.size()); + ASSERT(index < mUnitsForTextures.size()); return mUnitsForTextures[index]; } diff --git a/src/dawn_native/opengl/TextureGL.cpp b/src/dawn_native/opengl/TextureGL.cpp index 029ecca737..ee64bcda57 100644 --- a/src/dawn_native/opengl/TextureGL.cpp +++ b/src/dawn_native/opengl/TextureGL.cpp @@ -46,11 +46,15 @@ namespace dawn_native { namespace opengl { } GLenum TargetForTextureViewDimension(wgpu::TextureViewDimension dimension, + uint32_t arrayLayerCount, uint32_t sampleCount) { switch (dimension) { case wgpu::TextureViewDimension::e2D: return (sampleCount > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; case wgpu::TextureViewDimension::e2DArray: + if (arrayLayerCount == 1) { + return (sampleCount > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; + } ASSERT(sampleCount == 1); return GL_TEXTURE_2D_ARRAY; case wgpu::TextureViewDimension::Cube: @@ -387,7 +391,8 @@ namespace dawn_native { namespace opengl { TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) : TextureViewBase(texture, descriptor), mOwnsHandle(false) { - mTarget = TargetForTextureViewDimension(descriptor->dimension, texture->GetSampleCount()); + mTarget = TargetForTextureViewDimension(descriptor->dimension, descriptor->arrayLayerCount, + texture->GetSampleCount()); if (!UsageNeedsTextureView(texture->GetUsage())) { mHandle = 0; diff --git a/src/tests/BUILD.gn b/src/tests/BUILD.gn index 69a01727a3..1723019593 100644 --- a/src/tests/BUILD.gn +++ b/src/tests/BUILD.gn @@ -282,6 +282,7 @@ source_set("dawn_end2end_tests_sources") { "end2end/GpuMemorySynchronizationTests.cpp", "end2end/IndexFormatTests.cpp", "end2end/MultisampledRenderingTests.cpp", + "end2end/MultisampledSamplingTests.cpp", "end2end/NonzeroBufferCreationTests.cpp", "end2end/NonzeroTextureCreationTests.cpp", "end2end/ObjectCachingTests.cpp", diff --git a/src/tests/DawnTest.h b/src/tests/DawnTest.h index 3fd3f20122..36e9e945fc 100644 --- a/src/tests/DawnTest.h +++ b/src/tests/DawnTest.h @@ -46,6 +46,14 @@ AddBufferExpectation(__FILE__, __LINE__, buffer, offset, sizeof(uint32_t) * count, \ new ::detail::ExpectEq(expected, count)) +#define EXPECT_BUFFER_FLOAT_EQ(expected, buffer, offset) \ + AddBufferExpectation(__FILE__, __LINE__, buffer, offset, sizeof(uint32_t), \ + new ::detail::ExpectEq(expected)) + +#define EXPECT_BUFFER_FLOAT_RANGE_EQ(expected, buffer, offset, count) \ + AddBufferExpectation(__FILE__, __LINE__, buffer, offset, sizeof(uint32_t) * count, \ + new ::detail::ExpectEq(expected, count)) + // Test a pixel of the mip level 0 of a 2D texture. #define EXPECT_PIXEL_RGBA8_EQ(expected, texture, x, y) \ AddTextureExpectation(__FILE__, __LINE__, texture, x, y, 1, 1, 0, 0, sizeof(RGBA8), \ diff --git a/src/tests/end2end/MultisampledRenderingTests.cpp b/src/tests/end2end/MultisampledRenderingTests.cpp index 305ce26082..9192fca408 100644 --- a/src/tests/end2end/MultisampledRenderingTests.cpp +++ b/src/tests/end2end/MultisampledRenderingTests.cpp @@ -27,8 +27,8 @@ class MultisampledRenderingTest : public DawnTest { } void InitTexturesForTest() { - mMultisampledColorView = - CreateTextureForOutputAttachment(kColorFormat, kSampleCount).CreateView(); + mMultisampledColorTexture = CreateTextureForOutputAttachment(kColorFormat, kSampleCount); + mMultisampledColorView = mMultisampledColorTexture.CreateView(); mResolveTexture = CreateTextureForOutputAttachment(kColorFormat, 1); mResolveView = mResolveTexture.CreateView(); @@ -173,6 +173,7 @@ class MultisampledRenderingTest : public DawnTest { constexpr static wgpu::TextureFormat kDepthStencilFormat = wgpu::TextureFormat::Depth24PlusStencil8; + wgpu::Texture mMultisampledColorTexture; wgpu::TextureView mMultisampledColorView; wgpu::Texture mResolveTexture; wgpu::TextureView mResolveView; @@ -242,6 +243,34 @@ TEST_P(MultisampledRenderingTest, ResolveInto2DTexture) { VerifyResolveTarget(kGreen, mResolveTexture); } +// Test that a single-layer multisampled texture view can be created and resolved from. +TEST_P(MultisampledRenderingTest, ResolveFromSingleLayerArrayInto2DTexture) { + constexpr bool kTestDepth = false; + wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); + wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); + + constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; + constexpr uint32_t kSize = sizeof(kGreen); + + // Draw a green triangle. + { + wgpu::TextureViewDescriptor desc = {}; + desc.dimension = wgpu::TextureViewDimension::e2DArray; + desc.arrayLayerCount = 1; + + utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( + {mMultisampledColorTexture.CreateView(&desc)}, {mResolveView}, wgpu::LoadOp::Clear, + wgpu::LoadOp::Clear, kTestDepth); + + EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, &kGreen.r, kSize); + } + + wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); + queue.Submit(1, &commandBuffer); + + VerifyResolveTarget(kGreen, mResolveTexture); +} + // Test multisampled rendering with depth test works correctly. TEST_P(MultisampledRenderingTest, MultisampledRenderingWithDepthTest) { constexpr bool kTestDepth = true; diff --git a/src/tests/end2end/MultisampledSamplingTests.cpp b/src/tests/end2end/MultisampledSamplingTests.cpp new file mode 100644 index 0000000000..8c017d0fc0 --- /dev/null +++ b/src/tests/end2end/MultisampledSamplingTests.cpp @@ -0,0 +1,267 @@ +// Copyright 2020 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "tests/DawnTest.h" + +#include "common/Math.h" +#include "utils/ComboRenderPipelineDescriptor.h" +#include "utils/WGPUHelpers.h" + +namespace { + // https://github.com/gpuweb/gpuweb/issues/108 + // Vulkan, Metal, and D3D11 have the same standard multisample pattern. D3D12 is the same as + // D3D11 but it was left out of the documentation. + // {0.375, 0.125}, {0.875, 0.375}, {0.125 0.625}, {0.625, 0.875} + // In this test, we store them in -1 to 1 space because it makes it + // simpler to upload vertex data. Y is flipped because there is a flip between clip space and + // rasterization space. + static constexpr std::array, 4> kSamplePositions = { + {{0.375 * 2 - 1, 1 - 0.125 * 2}, + {0.875 * 2 - 1, 1 - 0.375 * 2}, + {0.125 * 2 - 1, 1 - 0.625 * 2}, + {0.625 * 2 - 1, 1 - 0.875 * 2}}}; +} // anonymous namespace + +class MultisampledSamplingTest : public DawnTest { + protected: + static constexpr wgpu::TextureFormat kColorFormat = wgpu::TextureFormat::R8Unorm; + static constexpr wgpu::TextureFormat kDepthFormat = wgpu::TextureFormat::Depth32Float; + + static constexpr wgpu::TextureFormat kDepthOutFormat = wgpu::TextureFormat::R32Float; + static constexpr uint32_t kSampleCount = 4; + + // Render pipeline for drawing to a multisampled color and depth attachment. + wgpu::RenderPipeline drawPipeline; + + // A compute pipeline to texelFetch the sample locations and output the results to a buffer. + wgpu::ComputePipeline checkSamplePipeline; + + void SetUp() override { + DawnTest::SetUp(); + { + utils::ComboRenderPipelineDescriptor desc(device); + + desc.vertexStage.module = + utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, + R"(#version 450 + layout(location=0) in vec2 pos; + void main() { + gl_Position = vec4(pos, 0.0, 1.0); + })"); + + desc.cFragmentStage.module = + utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, + R"(#version 450 + layout(location = 0) out float fragColor; + void main() { + fragColor = 1.0; + gl_FragDepth = 0.7; + })"); + + desc.cVertexState.vertexBufferCount = 1; + desc.cVertexState.cVertexBuffers[0].attributeCount = 1; + desc.cVertexState.cVertexBuffers[0].arrayStride = 2 * sizeof(float); + desc.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float2; + + desc.cDepthStencilState.format = kDepthFormat; + desc.cDepthStencilState.depthWriteEnabled = true; + desc.depthStencilState = &desc.cDepthStencilState; + + desc.sampleCount = kSampleCount; + desc.colorStateCount = 1; + desc.cColorStates[0].format = kColorFormat; + + desc.primitiveTopology = wgpu::PrimitiveTopology::TriangleStrip; + + drawPipeline = device.CreateRenderPipeline(&desc); + } + { + wgpu::ComputePipelineDescriptor desc = {}; + desc.computeStage.entryPoint = "main"; + desc.computeStage.module = + utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, + R"(#version 450 + layout(set = 0, binding = 0) uniform sampler sampler0; + layout(set = 0, binding = 1) uniform texture2DMS texture0; + layout(set = 0, binding = 2) uniform texture2DMS texture1; + + layout(set = 0, binding = 3, std430) buffer Results { + float colorSamples[4]; + float depthSamples[4]; + }; + + void main() { + for (int i = 0; i < 4; ++i) { + colorSamples[i] = + texelFetch(sampler2DMS(texture0, sampler0), ivec2(0, 0), i).x; + + depthSamples[i] = + texelFetch(sampler2DMS(texture1, sampler0), ivec2(0, 0), i).x; + } + })"); + + checkSamplePipeline = device.CreateComputePipeline(&desc); + } + } +}; + +// Test that the multisampling sample positions are correct. This test works by drawing a +// thin quad multiple times from left to right and from top to bottom on a 1x1 canvas. +// Each time, the quad should cover a single sample position. +// After drawing, a compute shader fetches all of the samples (both color and depth), +// and we check that only the one covered has data. +// We "scan" the vertical and horizontal dimensions separately to check that the triangle +// must cover both the X and Y coordinates of the sample position (no false positives if +// it covers the X position but not the Y, or vice versa). +TEST_P(MultisampledSamplingTest, SamplePositions) { + static constexpr wgpu::Extent3D kTextureSize = {1, 1, 1}; + + wgpu::Texture colorTexture; + { + wgpu::TextureDescriptor desc = {}; + desc.usage = wgpu::TextureUsage::Sampled | wgpu::TextureUsage::OutputAttachment; + desc.size = kTextureSize; + desc.format = kColorFormat; + desc.sampleCount = kSampleCount; + colorTexture = device.CreateTexture(&desc); + } + + wgpu::Texture depthTexture; + { + wgpu::TextureDescriptor desc = {}; + desc.usage = wgpu::TextureUsage::Sampled | wgpu::TextureUsage::OutputAttachment; + desc.size = kTextureSize; + desc.format = kDepthFormat; + desc.sampleCount = kSampleCount; + depthTexture = device.CreateTexture(&desc); + } + + static constexpr float kQuadWidth = 0.075; + std::vector vBufferData; + + // Add vertices for vertical quads + for (uint32_t s = 0; s < kSampleCount; ++s) { + // clang-format off + vBufferData.insert(vBufferData.end(), { + kSamplePositions[s][0] - kQuadWidth, -1.0, + kSamplePositions[s][0] - kQuadWidth, 1.0, + kSamplePositions[s][0] + kQuadWidth, -1.0, + kSamplePositions[s][0] + kQuadWidth, 1.0, + }); + // clang-format on + } + + // Add vertices for horizontal quads + for (uint32_t s = 0; s < kSampleCount; ++s) { + // clang-format off + vBufferData.insert(vBufferData.end(), { + -1.0, kSamplePositions[s][1] - kQuadWidth, + -1.0, kSamplePositions[s][1] + kQuadWidth, + 1.0, kSamplePositions[s][1] - kQuadWidth, + 1.0, kSamplePositions[s][1] + kQuadWidth, + }); + // clang-format on + } + + wgpu::Buffer vBuffer = utils::CreateBufferFromData( + device, vBufferData.data(), static_cast(vBufferData.size() * sizeof(float)), + wgpu::BufferUsage::Vertex); + + static constexpr uint32_t kQuadNumBytes = 8 * sizeof(float); + + wgpu::SamplerDescriptor samplerDesc = {}; + wgpu::Sampler sampler = device.CreateSampler(&samplerDesc); + wgpu::TextureView colorView = colorTexture.CreateView(); + wgpu::TextureView depthView = depthTexture.CreateView(); + + static constexpr uint64_t kResultSize = 4 * sizeof(float) + 4 * sizeof(float); + uint64_t alignedResultSize = Align(kResultSize, 256); + + wgpu::BufferDescriptor outputBufferDesc = {}; + outputBufferDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc; + outputBufferDesc.size = alignedResultSize * 8; + wgpu::Buffer outputBuffer = device.CreateBuffer(&outputBufferDesc); + + wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); + for (uint32_t iter = 0; iter < 2; ++iter) { + for (uint32_t sample = 0; sample < kSampleCount; ++sample) { + uint32_t sampleOffset = (iter * kSampleCount + sample); + + utils::ComboRenderPassDescriptor renderPass({colorView}, depthView); + renderPass.cDepthStencilAttachmentInfo.clearDepth = 0.f; + + wgpu::RenderPassEncoder renderPassEncoder = commandEncoder.BeginRenderPass(&renderPass); + renderPassEncoder.SetPipeline(drawPipeline); + renderPassEncoder.SetVertexBuffer(0, vBuffer, kQuadNumBytes * sampleOffset, + kQuadNumBytes); + renderPassEncoder.Draw(4); + renderPassEncoder.EndPass(); + + wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass(); + computePassEncoder.SetPipeline(checkSamplePipeline); + computePassEncoder.SetBindGroup( + 0, utils::MakeBindGroup( + device, checkSamplePipeline.GetBindGroupLayout(0), + {{0, sampler}, + {1, colorView}, + {2, depthView}, + {3, outputBuffer, alignedResultSize * sampleOffset, kResultSize}})); + computePassEncoder.Dispatch(1); + computePassEncoder.EndPass(); + } + } + + wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); + queue.Submit(1, &commandBuffer); + + std::array expectedData; + + expectedData = {1, 0, 0, 0, 0.7, 0, 0, 0}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 0 * alignedResultSize, 8) + << "vertical sample 0"; + + expectedData = {0, 1, 0, 0, 0, 0.7, 0, 0}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 1 * alignedResultSize, 8) + << "vertical sample 1"; + + expectedData = {0, 0, 1, 0, 0, 0, 0.7, 0}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 2 * alignedResultSize, 8) + << "vertical sample 2"; + + expectedData = {0, 0, 0, 1, 0, 0, 0, 0.7}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 3 * alignedResultSize, 8) + << "vertical sample 3"; + + expectedData = {1, 0, 0, 0, 0.7, 0, 0, 0}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 4 * alignedResultSize, 8) + << "horizontal sample 0"; + + expectedData = {0, 1, 0, 0, 0, 0.7, 0, 0}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 5 * alignedResultSize, 8) + << "horizontal sample 1"; + + expectedData = {0, 0, 1, 0, 0, 0, 0.7, 0}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 6 * alignedResultSize, 8) + << "horizontal sample 2"; + + expectedData = {0, 0, 0, 1, 0, 0, 0, 0.7}; + EXPECT_BUFFER_FLOAT_RANGE_EQ(expectedData.data(), outputBuffer, 7 * alignedResultSize, 8) + << "horizontal sample 3"; +} + +DAWN_INSTANTIATE_TEST(MultisampledSamplingTest, + D3D12Backend(), + MetalBackend(), + OpenGLBackend(), + VulkanBackend()); diff --git a/src/tests/unittests/validation/BindGroupValidationTests.cpp b/src/tests/unittests/validation/BindGroupValidationTests.cpp index 42213cae3d..17ab3d7b72 100644 --- a/src/tests/unittests/validation/BindGroupValidationTests.cpp +++ b/src/tests/unittests/validation/BindGroupValidationTests.cpp @@ -727,6 +727,50 @@ TEST_F(BindGroupLayoutValidationTest, DynamicBufferNumberLimit) { } } +TEST_F(BindGroupLayoutValidationTest, MultisampledTextures) { + // Multisampled 2D texture works. + utils::MakeBindGroupLayout( + device, { + {0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, true, + wgpu::TextureViewDimension::e2D}, + }); + + // Multisampled 2D (defaulted) texture works. + utils::MakeBindGroupLayout( + device, { + {0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, true, + wgpu::TextureViewDimension::Undefined}, + }); + + // Multisampled 2D array texture is invalid. + ASSERT_DEVICE_ERROR(utils::MakeBindGroupLayout( + device, { + {0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, true, + wgpu::TextureViewDimension::e2DArray}, + })); + + // Multisampled cube texture is invalid. + ASSERT_DEVICE_ERROR(utils::MakeBindGroupLayout( + device, { + {0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, true, + wgpu::TextureViewDimension::Cube}, + })); + + // Multisampled cube array texture is invalid. + ASSERT_DEVICE_ERROR(utils::MakeBindGroupLayout( + device, { + {0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, true, + wgpu::TextureViewDimension::CubeArray}, + })); + + // Multisampled 3D texture is invalid. + ASSERT_DEVICE_ERROR(utils::MakeBindGroupLayout( + device, { + {0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, true, + wgpu::TextureViewDimension::e3D}, + })); +} + constexpr uint64_t kBufferSize = 3 * kMinDynamicBufferOffsetAlignment + 8; constexpr uint32_t kBindingSize = 9; @@ -1364,7 +1408,10 @@ class BindGroupLayoutCompatibilityTest : public ValidationTest { return device.CreateBuffer(&bufferDescriptor); } - wgpu::RenderPipeline CreateRenderPipeline(std::vector bindGroupLayout) { + wgpu::RenderPipeline CreateFSRenderPipeline( + const char* fsShader, + std::vector bindGroupLayout) { + wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( #version 450 @@ -1372,17 +1419,7 @@ class BindGroupLayoutCompatibilityTest : public ValidationTest { })"); wgpu::ShaderModule fsModule = - utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( - #version 450 - layout(std140, set = 0, binding = 0) buffer SBuffer { - vec2 value2; - } sBuffer; - layout(std140, set = 1, binding = 0) readonly buffer RBuffer { - vec2 value3; - } rBuffer; - layout(location = 0) out vec4 fragColor; - void main() { - })"); + utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fsShader); wgpu::PipelineLayoutDescriptor descriptor; descriptor.bindGroupLayoutCount = bindGroupLayout.size(); @@ -1395,23 +1432,26 @@ class BindGroupLayoutCompatibilityTest : public ValidationTest { return device.CreateRenderPipeline(&pipelineDescriptor); } + wgpu::RenderPipeline CreateRenderPipeline(std::vector bindGroupLayout) { + return CreateFSRenderPipeline(R"( + #version 450 + layout(std140, set = 0, binding = 0) buffer SBuffer { + vec2 value2; + } sBuffer; + layout(std140, set = 1, binding = 0) readonly buffer RBuffer { + vec2 value3; + } rBuffer; + layout(location = 0) out vec4 fragColor; + void main() { + })", + std::move(bindGroupLayout)); + } + wgpu::ComputePipeline CreateComputePipeline( + const char* shader, std::vector bindGroupLayout) { wgpu::ShaderModule csModule = - utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"( - #version 450 - const uint kTileSize = 4; - const uint kInstances = 11; - - layout(local_size_x = kTileSize, local_size_y = kTileSize, local_size_z = 1) in; - layout(std140, set = 0, binding = 0) buffer SBuffer { - float value2; - } dst; - layout(std140, set = 1, binding = 0) readonly buffer RBuffer { - readonly float value3; - } rdst; - void main() { - })"); + utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, shader); wgpu::PipelineLayoutDescriptor descriptor; descriptor.bindGroupLayoutCount = bindGroupLayout.size(); @@ -1425,6 +1465,25 @@ class BindGroupLayoutCompatibilityTest : public ValidationTest { return device.CreateComputePipeline(&csDesc); } + + wgpu::ComputePipeline CreateComputePipeline( + std::vector bindGroupLayout) { + return CreateComputePipeline(R"( + #version 450 + const uint kTileSize = 4; + const uint kInstances = 11; + + layout(local_size_x = kTileSize, local_size_y = kTileSize, local_size_z = 1) in; + layout(std140, set = 0, binding = 0) buffer SBuffer { + float value2; + } dst; + layout(std140, set = 1, binding = 0) readonly buffer RBuffer { + readonly float value3; + } rdst; + void main() { + })", + std::move(bindGroupLayout)); + } }; // Test that it is valid to pass a writable storage buffer in the pipeline layout when the shader @@ -1459,6 +1518,76 @@ TEST_F(BindGroupLayoutCompatibilityTest, ROStorageInBGLWithRWStorageInShader) { ASSERT_DEVICE_ERROR(CreateComputePipeline({bgl0, bgl1})); } +TEST_F(BindGroupLayoutCompatibilityTest, TextureViewDimension) { + constexpr char kTexture2DShader[] = R"( + #version 450 + layout(set = 0, binding = 0) uniform texture2D texture; + void main() { + })"; + + // Render: Test that 2D texture with 2D view dimension works + CreateFSRenderPipeline( + kTexture2DShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2D}})}); + + // Render: Test that 2D texture with 2D array view dimension is invalid + ASSERT_DEVICE_ERROR(CreateFSRenderPipeline( + kTexture2DShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2DArray}})})); + + // Compute: Test that 2D texture with 2D view dimension works + CreateComputePipeline( + kTexture2DShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2D}})}); + + // Compute: Test that 2D texture with 2D array view dimension is invalid + ASSERT_DEVICE_ERROR(CreateComputePipeline( + kTexture2DShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2DArray}})})); + + constexpr char kTexture2DArrayShader[] = R"( + #version 450 + layout(set = 0, binding = 0) uniform texture2DArray texture; + void main() { + })"; + + // Render: Test that 2D texture array with 2D array view dimension works + CreateFSRenderPipeline( + kTexture2DArrayShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2DArray}})}); + + // Render: Test that 2D texture array with 2D view dimension is invalid + ASSERT_DEVICE_ERROR(CreateFSRenderPipeline( + kTexture2DArrayShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2D}})})); + + // Compute: Test that 2D texture array with 2D array view dimension works + CreateComputePipeline( + kTexture2DArrayShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2DArray}})}); + + // Compute: Test that 2D texture array with 2D view dimension is invalid + ASSERT_DEVICE_ERROR(CreateComputePipeline( + kTexture2DArrayShader, + {utils::MakeBindGroupLayout( + device, {{0, wgpu::ShaderStage::Compute, wgpu::BindingType::SampledTexture, false, + false, wgpu::TextureViewDimension::e2D}})})); +} + class BindingsValidationTest : public BindGroupLayoutCompatibilityTest { public: void TestRenderPassBindings(const wgpu::BindGroup* bg, diff --git a/src/tests/unittests/validation/GetBindGroupLayoutValidationTests.cpp b/src/tests/unittests/validation/GetBindGroupLayoutValidationTests.cpp index 821958c47e..c14a3d24a3 100644 --- a/src/tests/unittests/validation/GetBindGroupLayoutValidationTests.cpp +++ b/src/tests/unittests/validation/GetBindGroupLayoutValidationTests.cpp @@ -256,9 +256,6 @@ TEST_F(GetBindGroupLayoutTests, Multisampled) { EXPECT_EQ(device.CreateBindGroupLayout(&desc).Get(), pipeline.GetBindGroupLayout(0).Get()); } - // TODO: Support multisampling - GTEST_SKIP() << "Multisampling unimplemented"; -#if 0 { binding.multisampled = true; wgpu::RenderPipeline pipeline = RenderPipelineFromFragmentShader(R"( @@ -268,7 +265,6 @@ TEST_F(GetBindGroupLayoutTests, Multisampled) { void main() {})"); EXPECT_EQ(device.CreateBindGroupLayout(&desc).Get(), pipeline.GetBindGroupLayout(0).Get()); } -#endif } // Test that texture view dimension matches the shader. diff --git a/src/tests/unittests/validation/RenderPipelineValidationTests.cpp b/src/tests/unittests/validation/RenderPipelineValidationTests.cpp index 5f1dfd5122..a513ee0064 100644 --- a/src/tests/unittests/validation/RenderPipelineValidationTests.cpp +++ b/src/tests/unittests/validation/RenderPipelineValidationTests.cpp @@ -486,3 +486,29 @@ TEST_F(RenderPipelineValidationTest, StorageBufferInVertexShaderNoLayout) { descriptor.cFragmentStage.module = fsModule; ASSERT_DEVICE_ERROR(device.CreateRenderPipeline(&descriptor)); } + +// Test that a pipeline with defaulted layout may not have multisampled array textures +// TODO(enga): Also test multisampled cube, cube array, and 3D. These have no GLSL keywords. +TEST_F(RenderPipelineValidationTest, MultisampledTexture) { + utils::ComboRenderPipelineDescriptor descriptor(device); + descriptor.layout = nullptr; + descriptor.cFragmentStage.module = fsModule; + + // Base case works. + descriptor.vertexStage.module = + utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( + #version 450 + layout(set = 0, binding = 0) uniform texture2DMS texture; + void main() { + })"); + device.CreateRenderPipeline(&descriptor); + + // texture2DMSArray invalid + descriptor.vertexStage.module = + utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( + #version 450 + layout(set = 0, binding = 0) uniform texture2DMSArray texture; + void main() { + })"); + ASSERT_DEVICE_ERROR(device.CreateRenderPipeline(&descriptor)); +}