Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "tests/DawnTest.h" |
| 16 | |
| 17 | #include "common/Assert.h" |
| 18 | #include "utils/ComboRenderPipelineDescriptor.h" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 19 | #include "utils/WGPUHelpers.h" |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 20 | |
| 21 | class MultisampledRenderingTest : public DawnTest { |
| 22 | protected: |
Austin Eng | 40dc5d3 | 2020-05-15 22:06:35 +0000 | [diff] [blame] | 23 | void SetUp() override { |
| 24 | DawnTest::SetUp(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 25 | |
Ben Clayton | 47a8cf3 | 2021-03-30 13:13:17 +0000 | [diff] [blame] | 26 | // TODO(crbug.com/dawn/738): Test output is wrong with D3D12 + WARP. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 27 | DAWN_SUPPRESS_TEST_IF(IsD3D12() && IsWARP()); |
Ben Clayton | 47a8cf3 | 2021-03-30 13:13:17 +0000 | [diff] [blame] | 28 | |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 29 | InitTexturesForTest(); |
| 30 | } |
| 31 | |
| 32 | void InitTexturesForTest() { |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 33 | mMultisampledColorTexture = CreateTextureForRenderAttachment(kColorFormat, kSampleCount); |
Austin Eng | 8f9523e | 2020-06-19 16:21:33 +0000 | [diff] [blame] | 34 | mMultisampledColorView = mMultisampledColorTexture.CreateView(); |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 35 | mResolveTexture = CreateTextureForRenderAttachment(kColorFormat, 1); |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 36 | mResolveView = mResolveTexture.CreateView(); |
Austin Eng | 342c6ea | 2019-04-18 18:28:48 +0000 | [diff] [blame] | 37 | |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 38 | mDepthStencilTexture = CreateTextureForRenderAttachment(kDepthStencilFormat, kSampleCount); |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 39 | mDepthStencilView = mDepthStencilTexture.CreateView(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 42 | wgpu::RenderPipeline CreateRenderPipelineWithOneOutputForTest( |
| 43 | bool testDepth, |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 44 | uint32_t sampleMask = 0xFFFFFFFF, |
| 45 | bool alphaToCoverageEnabled = false, |
| 46 | bool flipTriangle = false) { |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 47 | const char* kFsOneOutputWithDepth = R"( |
| 48 | [[block]] struct U { |
| 49 | color : vec4<f32>; |
| 50 | depth : f32; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 51 | }; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 52 | [[group(0), binding(0)]] var<uniform> uBuffer : U; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 53 | |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 54 | struct FragmentOut { |
| 55 | [[location(0)]] color : vec4<f32>; |
| 56 | [[builtin(frag_depth)]] depth : f32; |
| 57 | }; |
| 58 | |
| 59 | [[stage(fragment)]] fn main() -> FragmentOut { |
| 60 | var output : FragmentOut; |
| 61 | output.color = uBuffer.color; |
| 62 | output.depth = uBuffer.depth; |
| 63 | return output; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 64 | })"; |
| 65 | |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 66 | const char* kFsOneOutputWithoutDepth = R"( |
| 67 | [[block]] struct U { |
| 68 | color : vec4<f32>; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 69 | }; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 70 | [[group(0), binding(0)]] var<uniform> uBuffer : U; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 71 | |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 72 | [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> { |
| 73 | return uBuffer.color; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 74 | })"; |
| 75 | |
| 76 | const char* fs = testDepth ? kFsOneOutputWithDepth : kFsOneOutputWithoutDepth; |
| 77 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 78 | return CreateRenderPipelineForTest(fs, 1, testDepth, sampleMask, alphaToCoverageEnabled, |
| 79 | flipTriangle); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 82 | wgpu::RenderPipeline CreateRenderPipelineWithTwoOutputsForTest( |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 83 | uint32_t sampleMask = 0xFFFFFFFF, |
| 84 | bool alphaToCoverageEnabled = false) { |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 85 | const char* kFsTwoOutputs = R"( |
| 86 | [[block]] struct U { |
| 87 | color0 : vec4<f32>; |
| 88 | color1 : vec4<f32>; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 89 | }; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 90 | [[group(0), binding(0)]] var<uniform> uBuffer : U; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 91 | |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 92 | struct FragmentOut { |
| 93 | [[location(0)]] color0 : vec4<f32>; |
| 94 | [[location(1)]] color1 : vec4<f32>; |
| 95 | }; |
| 96 | |
| 97 | [[stage(fragment)]] fn main() -> FragmentOut { |
| 98 | var output : FragmentOut; |
| 99 | output.color0 = uBuffer.color0; |
| 100 | output.color1 = uBuffer.color1; |
| 101 | return output; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 102 | })"; |
| 103 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 104 | return CreateRenderPipelineForTest(kFsTwoOutputs, 2, false, sampleMask, |
| 105 | alphaToCoverageEnabled); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 108 | wgpu::Texture CreateTextureForRenderAttachment(wgpu::TextureFormat format, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 109 | uint32_t sampleCount, |
| 110 | uint32_t mipLevelCount = 1, |
| 111 | uint32_t arrayLayerCount = 1) { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 112 | wgpu::TextureDescriptor descriptor; |
| 113 | descriptor.dimension = wgpu::TextureDimension::e2D; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 114 | descriptor.size.width = kWidth << (mipLevelCount - 1); |
| 115 | descriptor.size.height = kHeight << (mipLevelCount - 1); |
shrekshao | b00de7f | 2021-03-22 21:12:36 +0000 | [diff] [blame] | 116 | descriptor.size.depthOrArrayLayers = arrayLayerCount; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 117 | descriptor.sampleCount = sampleCount; |
| 118 | descriptor.format = format; |
| 119 | descriptor.mipLevelCount = mipLevelCount; |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 120 | descriptor.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 121 | return device.CreateTexture(&descriptor); |
| 122 | } |
| 123 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 124 | void EncodeRenderPassForTest(wgpu::CommandEncoder commandEncoder, |
| 125 | const wgpu::RenderPassDescriptor& renderPass, |
| 126 | const wgpu::RenderPipeline& pipeline, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 127 | const float* uniformData, |
| 128 | uint32_t uniformDataSize) { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 129 | wgpu::Buffer uniformBuffer = utils::CreateBufferFromData( |
| 130 | device, uniformData, uniformDataSize, wgpu::BufferUsage::Uniform); |
Austin Eng | 476a14a | 2019-12-03 21:18:05 +0000 | [diff] [blame] | 131 | wgpu::BindGroup bindGroup = utils::MakeBindGroup(device, pipeline.GetBindGroupLayout(0), |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 132 | {{0, uniformBuffer, 0, uniformDataSize}}); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 133 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 134 | wgpu::RenderPassEncoder renderPassEncoder = commandEncoder.BeginRenderPass(&renderPass); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 135 | renderPassEncoder.SetPipeline(pipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 136 | renderPassEncoder.SetBindGroup(0, bindGroup); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 137 | renderPassEncoder.Draw(3); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 138 | renderPassEncoder.EndPass(); |
| 139 | } |
| 140 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 141 | void EncodeRenderPassForTest(wgpu::CommandEncoder commandEncoder, |
| 142 | const wgpu::RenderPassDescriptor& renderPass, |
| 143 | const wgpu::RenderPipeline& pipeline, |
| 144 | const wgpu::Color& color) { |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 145 | const float uniformData[4] = {static_cast<float>(color.r), static_cast<float>(color.g), |
| 146 | static_cast<float>(color.b), static_cast<float>(color.a)}; |
| 147 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, uniformData, |
| 148 | sizeof(float) * 4); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 151 | utils::ComboRenderPassDescriptor CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 152 | std::initializer_list<wgpu::TextureView> colorViews, |
| 153 | std::initializer_list<wgpu::TextureView> resolveTargetViews, |
| 154 | wgpu::LoadOp colorLoadOp, |
| 155 | wgpu::LoadOp depthStencilLoadOp, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 156 | bool hasDepthStencilAttachment) { |
| 157 | ASSERT(colorViews.size() == resolveTargetViews.size()); |
| 158 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 159 | constexpr wgpu::Color kClearColor = {0.0f, 0.0f, 0.0f, 0.0f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 160 | constexpr float kClearDepth = 1.0f; |
| 161 | |
| 162 | utils::ComboRenderPassDescriptor renderPass(colorViews); |
| 163 | uint32_t i = 0; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 164 | for (const wgpu::TextureView& resolveTargetView : resolveTargetViews) { |
Corentin Wallez | a838c7d | 2019-09-20 22:59:47 +0000 | [diff] [blame] | 165 | renderPass.cColorAttachments[i].loadOp = colorLoadOp; |
| 166 | renderPass.cColorAttachments[i].clearColor = kClearColor; |
| 167 | renderPass.cColorAttachments[i].resolveTarget = resolveTargetView; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 168 | ++i; |
| 169 | } |
| 170 | |
| 171 | renderPass.cDepthStencilAttachmentInfo.clearDepth = kClearDepth; |
| 172 | renderPass.cDepthStencilAttachmentInfo.depthLoadOp = depthStencilLoadOp; |
| 173 | |
| 174 | if (hasDepthStencilAttachment) { |
Brandon Jones | 5e6a092 | 2021-04-17 01:51:53 +0000 | [diff] [blame] | 175 | renderPass.cDepthStencilAttachmentInfo.view = mDepthStencilView; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 176 | renderPass.depthStencilAttachment = &renderPass.cDepthStencilAttachmentInfo; |
| 177 | } |
| 178 | |
| 179 | return renderPass; |
| 180 | } |
| 181 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 182 | void VerifyResolveTarget(const wgpu::Color& inputColor, |
| 183 | wgpu::Texture resolveTexture, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 184 | uint32_t mipmapLevel = 0, |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 185 | uint32_t arrayLayer = 0, |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 186 | const float msaaCoverage = 0.5f) { |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 187 | // In this test we only check the pixel in the middle of the texture. |
| 188 | constexpr uint32_t kMiddleX = (kWidth - 1) / 2; |
| 189 | constexpr uint32_t kMiddleY = (kHeight - 1) / 2; |
| 190 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 191 | RGBA8 expectedColor = ExpectedMSAAColor(inputColor, msaaCoverage); |
Yunchao He | 0da94c3 | 2021-04-08 14:58:42 +0000 | [diff] [blame] | 192 | EXPECT_TEXTURE_EQ(&expectedColor, resolveTexture, {kMiddleX, kMiddleY, arrayLayer}, {1, 1}, |
| 193 | mipmapLevel); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | constexpr static uint32_t kWidth = 3; |
| 197 | constexpr static uint32_t kHeight = 3; |
| 198 | constexpr static uint32_t kSampleCount = 4; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 199 | constexpr static wgpu::TextureFormat kColorFormat = wgpu::TextureFormat::RGBA8Unorm; |
| 200 | constexpr static wgpu::TextureFormat kDepthStencilFormat = |
| 201 | wgpu::TextureFormat::Depth24PlusStencil8; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 202 | |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 203 | constexpr static uint32_t kFirstSampleMaskBit = 0x00000001; |
| 204 | constexpr static uint32_t kSecondSampleMaskBit = 0x00000002; |
| 205 | constexpr static uint32_t kThirdSampleMaskBit = 0x00000004; |
| 206 | constexpr static uint32_t kFourthSampleMaskBit = 0x00000008; |
| 207 | |
Austin Eng | 8f9523e | 2020-06-19 16:21:33 +0000 | [diff] [blame] | 208 | wgpu::Texture mMultisampledColorTexture; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 209 | wgpu::TextureView mMultisampledColorView; |
| 210 | wgpu::Texture mResolveTexture; |
| 211 | wgpu::TextureView mResolveView; |
| 212 | wgpu::Texture mDepthStencilTexture; |
| 213 | wgpu::TextureView mDepthStencilView; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 214 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 215 | wgpu::RenderPipeline CreateRenderPipelineForTest(const char* fs, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 216 | uint32_t numColorAttachments, |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 217 | bool hasDepthStencilAttachment, |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 218 | uint32_t sampleMask = 0xFFFFFFFF, |
| 219 | bool alphaToCoverageEnabled = false, |
| 220 | bool flipTriangle = false) { |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 221 | utils::ComboRenderPipelineDescriptor pipelineDescriptor; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 222 | |
| 223 | // Draw a bottom-right triangle. In standard 4xMSAA pattern, for the pixels on diagonal, |
| 224 | // only two of the samples will be touched. |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 225 | const char* vs = R"( |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 226 | [[stage(vertex)]] |
| 227 | fn main([[builtin(vertex_index)]] VertexIndex : u32) -> [[builtin(position)]] vec4<f32> { |
Ben Clayton | 36edf8d | 2021-06-17 11:25:09 +0000 | [diff] [blame] | 228 | var pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>( |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 229 | vec2<f32>(-1.0, 1.0), |
| 230 | vec2<f32>( 1.0, 1.0), |
| 231 | vec2<f32>( 1.0, -1.0) |
| 232 | ); |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 233 | return vec4<f32>(pos[VertexIndex], 0.0, 1.0); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 234 | })"; |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 235 | |
| 236 | // Draw a bottom-left triangle. |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 237 | const char* vsFlipped = R"( |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 238 | [[stage(vertex)]] |
| 239 | fn main([[builtin(vertex_index)]] VertexIndex : u32) -> [[builtin(position)]] vec4<f32> { |
Ben Clayton | 36edf8d | 2021-06-17 11:25:09 +0000 | [diff] [blame] | 240 | var pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>( |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 241 | vec2<f32>(-1.0, 1.0), |
| 242 | vec2<f32>( 1.0, 1.0), |
| 243 | vec2<f32>(-1.0, -1.0) |
| 244 | ); |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 245 | return vec4<f32>(pos[VertexIndex], 0.0, 1.0); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 246 | })"; |
| 247 | |
| 248 | if (flipTriangle) { |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 249 | pipelineDescriptor.vertex.module = utils::CreateShaderModule(device, vsFlipped); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 250 | } else { |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 251 | pipelineDescriptor.vertex.module = utils::CreateShaderModule(device, vs); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 252 | } |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 253 | |
Corentin Wallez | 7aec4ae | 2021-03-24 15:55:32 +0000 | [diff] [blame] | 254 | pipelineDescriptor.cFragment.module = utils::CreateShaderModule(device, fs); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 255 | |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 256 | if (hasDepthStencilAttachment) { |
Brandon Jones | bff9d3a | 2021-03-18 02:54:27 +0000 | [diff] [blame] | 257 | wgpu::DepthStencilState* depthStencil = |
| 258 | pipelineDescriptor.EnableDepthStencil(kDepthStencilFormat); |
| 259 | depthStencil->depthWriteEnabled = true; |
| 260 | depthStencil->depthCompare = wgpu::CompareFunction::Less; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Brandon Jones | bff9d3a | 2021-03-18 02:54:27 +0000 | [diff] [blame] | 263 | pipelineDescriptor.multisample.count = kSampleCount; |
| 264 | pipelineDescriptor.multisample.mask = sampleMask; |
| 265 | pipelineDescriptor.multisample.alphaToCoverageEnabled = alphaToCoverageEnabled; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 266 | |
Brandon Jones | bff9d3a | 2021-03-18 02:54:27 +0000 | [diff] [blame] | 267 | pipelineDescriptor.cFragment.targetCount = numColorAttachments; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 268 | for (uint32_t i = 0; i < numColorAttachments; ++i) { |
Brandon Jones | bff9d3a | 2021-03-18 02:54:27 +0000 | [diff] [blame] | 269 | pipelineDescriptor.cTargets[i].format = kColorFormat; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Brandon Jones | 41c87d9 | 2021-05-21 05:01:38 +0000 | [diff] [blame] | 272 | wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pipelineDescriptor); |
Austin Eng | 476a14a | 2019-12-03 21:18:05 +0000 | [diff] [blame] | 273 | return pipeline; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 274 | } |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 275 | |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 276 | RGBA8 ExpectedMSAAColor(const wgpu::Color color, const double msaaCoverage) { |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 277 | RGBA8 result; |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 278 | result.r = static_cast<uint8_t>(std::min(255.0, 256 * color.r * msaaCoverage)); |
| 279 | result.g = static_cast<uint8_t>(std::min(255.0, 256 * color.g * msaaCoverage)); |
| 280 | result.b = static_cast<uint8_t>(std::min(255.0, 256 * color.b * msaaCoverage)); |
| 281 | result.a = static_cast<uint8_t>(std::min(255.0, 256 * color.a * msaaCoverage)); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 282 | return result; |
| 283 | } |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | // Test using one multisampled color attachment with resolve target can render correctly. |
| 287 | TEST_P(MultisampledRenderingTest, ResolveInto2DTexture) { |
| 288 | constexpr bool kTestDepth = false; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 289 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 290 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 291 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 292 | |
Kai Ninomiya | 57fcd17 | 2021-01-28 07:03:45 +0000 | [diff] [blame] | 293 | // storeOp should not affect the result in the resolve target. |
| 294 | for (wgpu::StoreOp storeOp : {wgpu::StoreOp::Store, wgpu::StoreOp::Clear}) { |
| 295 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 296 | |
| 297 | // Draw a green triangle. |
| 298 | { |
| 299 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 300 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 301 | kTestDepth); |
| 302 | renderPass.cColorAttachments[0].storeOp = storeOp; |
| 303 | std::array<float, 4> kUniformData = {kGreen.r, kGreen.g, kGreen.b, kGreen.a}; |
| 304 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 305 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), |
| 306 | kSize); |
| 307 | } |
| 308 | |
| 309 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 310 | queue.Submit(1, &commandBuffer); |
| 311 | |
| 312 | VerifyResolveTarget(kGreen, mResolveTexture); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 313 | } |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Austin Eng | 8f9523e | 2020-06-19 16:21:33 +0000 | [diff] [blame] | 316 | // Test that a single-layer multisampled texture view can be created and resolved from. |
| 317 | TEST_P(MultisampledRenderingTest, ResolveFromSingleLayerArrayInto2DTexture) { |
| 318 | constexpr bool kTestDepth = false; |
| 319 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 320 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); |
| 321 | |
| 322 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Austin Eng | 8f9523e | 2020-06-19 16:21:33 +0000 | [diff] [blame] | 323 | |
| 324 | // Draw a green triangle. |
| 325 | { |
| 326 | wgpu::TextureViewDescriptor desc = {}; |
| 327 | desc.dimension = wgpu::TextureViewDimension::e2DArray; |
| 328 | desc.arrayLayerCount = 1; |
| 329 | |
| 330 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 331 | {mMultisampledColorTexture.CreateView(&desc)}, {mResolveView}, wgpu::LoadOp::Clear, |
| 332 | wgpu::LoadOp::Clear, kTestDepth); |
| 333 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 334 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Austin Eng | 8f9523e | 2020-06-19 16:21:33 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 338 | queue.Submit(1, &commandBuffer); |
| 339 | |
| 340 | VerifyResolveTarget(kGreen, mResolveTexture); |
| 341 | } |
| 342 | |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 343 | // Test multisampled rendering with depth test works correctly. |
| 344 | TEST_P(MultisampledRenderingTest, MultisampledRenderingWithDepthTest) { |
| 345 | constexpr bool kTestDepth = true; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 346 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 347 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 348 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 349 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
| 350 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.8f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 351 | |
| 352 | // In first render pass we draw a green triangle with depth value == 0.2f. |
| 353 | { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 354 | utils::ComboRenderPassDescriptor renderPass = |
| 355 | CreateComboRenderPassDescriptorForTest({mMultisampledColorView}, {mResolveView}, |
| 356 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, true); |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 357 | std::array<float, 5> kUniformData = {kGreen.r, kGreen.g, kGreen.b, kGreen.a, // Color |
| 358 | 0.2f}; // depth |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 359 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 360 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), kSize); |
| 361 | } |
| 362 | |
| 363 | // In second render pass we draw a red triangle with depth value == 0.5f. |
| 364 | // This red triangle should not be displayed because it is behind the green one that is drawn in |
| 365 | // the last render pass. |
| 366 | { |
| 367 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 368 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Load, wgpu::LoadOp::Load, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 369 | kTestDepth); |
| 370 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 371 | std::array<float, 5> kUniformData = {kRed.r, kRed.g, kRed.b, kRed.a, // color |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 372 | 0.5f}; // depth |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 373 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 374 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), kSize); |
| 375 | } |
| 376 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 377 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 378 | queue.Submit(1, &commandBuffer); |
| 379 | |
| 380 | // The color of the pixel in the middle of mResolveTexture should be green if MSAA resolve runs |
| 381 | // correctly with depth test. |
| 382 | VerifyResolveTarget(kGreen, mResolveTexture); |
| 383 | } |
| 384 | |
| 385 | // Test rendering into a multisampled color attachment and doing MSAA resolve in another render pass |
| 386 | // works correctly. |
| 387 | TEST_P(MultisampledRenderingTest, ResolveInAnotherRenderPass) { |
| 388 | constexpr bool kTestDepth = false; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 389 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 390 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 391 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 392 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 393 | |
| 394 | // In first render pass we draw a green triangle and do not set the resolve target. |
| 395 | { |
| 396 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 397 | {mMultisampledColorView}, {nullptr}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 398 | kTestDepth); |
| 399 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 400 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // In second render pass we ony do MSAA resolve with no draw call. |
| 404 | { |
| 405 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 406 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Load, wgpu::LoadOp::Load, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 407 | kTestDepth); |
| 408 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 409 | wgpu::RenderPassEncoder renderPassEncoder = commandEncoder.BeginRenderPass(&renderPass); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 410 | renderPassEncoder.EndPass(); |
| 411 | } |
| 412 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 413 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 414 | queue.Submit(1, &commandBuffer); |
| 415 | |
| 416 | VerifyResolveTarget(kGreen, mResolveTexture); |
| 417 | } |
| 418 | |
| 419 | // Test doing MSAA resolve into multiple resolve targets works correctly. |
| 420 | TEST_P(MultisampledRenderingTest, ResolveIntoMultipleResolveTargets) { |
Austin Eng | ed8a8c0 | 2021-06-04 22:23:56 +0000 | [diff] [blame] | 421 | // TODO(dawn:462): Issue in the D3D12 validation layers. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 422 | DAWN_SUPPRESS_TEST_IF(IsD3D12() && IsNvidia() && IsBackendValidationEnabled()); |
Bryan Bernhart | 1f16229 | 2020-07-27 19:50:21 +0000 | [diff] [blame] | 423 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 424 | wgpu::TextureView multisampledColorView2 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 425 | CreateTextureForRenderAttachment(kColorFormat, kSampleCount).CreateView(); |
| 426 | wgpu::Texture resolveTexture2 = CreateTextureForRenderAttachment(kColorFormat, 1); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 427 | wgpu::TextureView resolveView2 = resolveTexture2.CreateView(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 428 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 429 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 430 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithTwoOutputsForTest(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 431 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 432 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
| 433 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.8f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 434 | constexpr bool kTestDepth = false; |
| 435 | |
| 436 | // Draw a red triangle to the first color attachment, and a blue triangle to the second color |
| 437 | // attachment, and do MSAA resolve on two render targets in one render pass. |
| 438 | { |
| 439 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 440 | {mMultisampledColorView, multisampledColorView2}, {mResolveView, resolveView2}, |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 441 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, kTestDepth); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 442 | |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 443 | std::array<float, 8> kUniformData = { |
| 444 | static_cast<float>(kRed.r), static_cast<float>(kRed.g), |
| 445 | static_cast<float>(kRed.b), static_cast<float>(kRed.a), |
| 446 | static_cast<float>(kGreen.r), static_cast<float>(kGreen.g), |
| 447 | static_cast<float>(kGreen.b), static_cast<float>(kGreen.a)}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 448 | constexpr uint32_t kSize = sizeof(kUniformData); |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 449 | |
| 450 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), kSize); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 453 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 454 | queue.Submit(1, &commandBuffer); |
| 455 | |
| 456 | VerifyResolveTarget(kRed, mResolveTexture); |
| 457 | VerifyResolveTarget(kGreen, resolveTexture2); |
| 458 | } |
| 459 | |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 460 | // Test doing MSAA resolve on one multisampled texture twice works correctly. |
| 461 | TEST_P(MultisampledRenderingTest, ResolveOneMultisampledTextureTwice) { |
| 462 | constexpr bool kTestDepth = false; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 463 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 464 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 465 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 466 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 467 | |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 468 | wgpu::Texture resolveTexture2 = CreateTextureForRenderAttachment(kColorFormat, 1); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 469 | |
| 470 | // In first render pass we draw a green triangle and specify mResolveView as the resolve target. |
| 471 | { |
| 472 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 473 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 474 | kTestDepth); |
| 475 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 476 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // In second render pass we do MSAA resolve into resolveTexture2. |
| 480 | { |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 481 | wgpu::TextureView resolveView2 = resolveTexture2.CreateView(); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 482 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 483 | {mMultisampledColorView}, {resolveView2}, wgpu::LoadOp::Load, wgpu::LoadOp::Load, |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 484 | kTestDepth); |
| 485 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 486 | wgpu::RenderPassEncoder renderPassEncoder = commandEncoder.BeginRenderPass(&renderPass); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 487 | renderPassEncoder.EndPass(); |
| 488 | } |
| 489 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 490 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 865cad8 | 2019-04-09 08:04:59 +0000 | [diff] [blame] | 491 | queue.Submit(1, &commandBuffer); |
| 492 | |
| 493 | VerifyResolveTarget(kGreen, mResolveTexture); |
| 494 | VerifyResolveTarget(kGreen, resolveTexture2); |
| 495 | } |
| 496 | |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 497 | // Test using a layer of a 2D texture as resolve target works correctly. |
| 498 | TEST_P(MultisampledRenderingTest, ResolveIntoOneMipmapLevelOf2DTexture) { |
Austin Eng | ed8a8c0 | 2021-06-04 22:23:56 +0000 | [diff] [blame] | 499 | // TODO(dawn:462): Issue in the D3D12 validation layers. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 500 | DAWN_SUPPRESS_TEST_IF(IsD3D12() && IsBackendValidationEnabled()); |
Bryan Bernhart | 1f16229 | 2020-07-27 19:50:21 +0000 | [diff] [blame] | 501 | |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 502 | constexpr uint32_t kBaseMipLevel = 2; |
| 503 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 504 | wgpu::TextureViewDescriptor textureViewDescriptor; |
| 505 | textureViewDescriptor.dimension = wgpu::TextureViewDimension::e2D; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 506 | textureViewDescriptor.format = kColorFormat; |
| 507 | textureViewDescriptor.baseArrayLayer = 0; |
| 508 | textureViewDescriptor.arrayLayerCount = 1; |
| 509 | textureViewDescriptor.mipLevelCount = 1; |
| 510 | textureViewDescriptor.baseMipLevel = kBaseMipLevel; |
| 511 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 512 | wgpu::Texture resolveTexture = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 513 | CreateTextureForRenderAttachment(kColorFormat, 1, kBaseMipLevel + 1, 1); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 514 | wgpu::TextureView resolveView = resolveTexture.CreateView(&textureViewDescriptor); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 515 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 516 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 517 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 518 | constexpr bool kTestDepth = false; |
| 519 | |
| 520 | // Draw a green triangle and do MSAA resolve. |
| 521 | { |
| 522 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 523 | {mMultisampledColorView}, {resolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 524 | kTestDepth); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 525 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest(kTestDepth); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 526 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 529 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 530 | queue.Submit(1, &commandBuffer); |
| 531 | |
| 532 | VerifyResolveTarget(kGreen, resolveTexture, kBaseMipLevel, 0); |
| 533 | } |
| 534 | |
| 535 | // Test using a level or a layer of a 2D array texture as resolve target works correctly. |
| 536 | TEST_P(MultisampledRenderingTest, ResolveInto2DArrayTexture) { |
Austin Eng | ed8a8c0 | 2021-06-04 22:23:56 +0000 | [diff] [blame] | 537 | // TODO(dawn:462): Issue in the D3D12 validation layers. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 538 | DAWN_SUPPRESS_TEST_IF(IsD3D12() && IsBackendValidationEnabled()); |
Bryan Bernhart | 1f16229 | 2020-07-27 19:50:21 +0000 | [diff] [blame] | 539 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 540 | wgpu::TextureView multisampledColorView2 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 541 | CreateTextureForRenderAttachment(kColorFormat, kSampleCount).CreateView(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 542 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 543 | wgpu::TextureViewDescriptor baseTextureViewDescriptor; |
| 544 | baseTextureViewDescriptor.dimension = wgpu::TextureViewDimension::e2D; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 545 | baseTextureViewDescriptor.format = kColorFormat; |
| 546 | baseTextureViewDescriptor.arrayLayerCount = 1; |
| 547 | baseTextureViewDescriptor.mipLevelCount = 1; |
| 548 | |
| 549 | // Create resolveTexture1 with only 1 mipmap level. |
| 550 | constexpr uint32_t kBaseArrayLayer1 = 2; |
| 551 | constexpr uint32_t kBaseMipLevel1 = 0; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 552 | wgpu::Texture resolveTexture1 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 553 | CreateTextureForRenderAttachment(kColorFormat, 1, kBaseMipLevel1 + 1, kBaseArrayLayer1 + 1); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 554 | wgpu::TextureViewDescriptor resolveViewDescriptor1 = baseTextureViewDescriptor; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 555 | resolveViewDescriptor1.baseArrayLayer = kBaseArrayLayer1; |
| 556 | resolveViewDescriptor1.baseMipLevel = kBaseMipLevel1; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 557 | wgpu::TextureView resolveView1 = resolveTexture1.CreateView(&resolveViewDescriptor1); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 558 | |
| 559 | // Create resolveTexture2 with (kBaseMipLevel2 + 1) mipmap levels and resolve into its last |
| 560 | // mipmap level. |
| 561 | constexpr uint32_t kBaseArrayLayer2 = 5; |
| 562 | constexpr uint32_t kBaseMipLevel2 = 3; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 563 | wgpu::Texture resolveTexture2 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 564 | CreateTextureForRenderAttachment(kColorFormat, 1, kBaseMipLevel2 + 1, kBaseArrayLayer2 + 1); |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 565 | wgpu::TextureViewDescriptor resolveViewDescriptor2 = baseTextureViewDescriptor; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 566 | resolveViewDescriptor2.baseArrayLayer = kBaseArrayLayer2; |
| 567 | resolveViewDescriptor2.baseMipLevel = kBaseMipLevel2; |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 568 | wgpu::TextureView resolveView2 = resolveTexture2.CreateView(&resolveViewDescriptor2); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 569 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 570 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 571 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithTwoOutputsForTest(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 572 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 573 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
| 574 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.8f}; |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 575 | constexpr bool kTestDepth = false; |
| 576 | |
| 577 | // Draw a red triangle to the first color attachment, and a green triangle to the second color |
| 578 | // attachment, and do MSAA resolve on two render targets in one render pass. |
| 579 | { |
| 580 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 581 | {mMultisampledColorView, multisampledColorView2}, {resolveView1, resolveView2}, |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 582 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, kTestDepth); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 583 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 584 | std::array<float, 8> kUniformData = {kRed.r, kRed.g, kRed.b, kRed.a, // color1 |
| 585 | kGreen.r, kGreen.g, kGreen.b, kGreen.a}; // color2 |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 586 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 587 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), kSize); |
| 588 | } |
| 589 | |
Corentin Wallez | cab352c | 2019-10-28 13:27:36 +0000 | [diff] [blame] | 590 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
Jiawei Shao | 0bc168e | 2019-03-28 14:18:11 +0000 | [diff] [blame] | 591 | queue.Submit(1, &commandBuffer); |
| 592 | |
| 593 | VerifyResolveTarget(kRed, resolveTexture1, kBaseMipLevel1, kBaseArrayLayer1); |
| 594 | VerifyResolveTarget(kGreen, resolveTexture2, kBaseMipLevel2, kBaseArrayLayer2); |
| 595 | } |
| 596 | |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 597 | // Test using one multisampled color attachment with resolve target can render correctly |
| 598 | // with a non-default sample mask. |
| 599 | TEST_P(MultisampledRenderingTest, ResolveInto2DTextureWithSampleMask) { |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 600 | constexpr bool kTestDepth = false; |
| 601 | // The second and third samples are included, |
| 602 | // only the second one is covered by the triangle. |
| 603 | constexpr uint32_t kSampleMask = kSecondSampleMaskBit | kThirdSampleMaskBit; |
| 604 | constexpr float kMSAACoverage = 0.25f; |
| 605 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 606 | wgpu::RenderPipeline pipeline = |
| 607 | CreateRenderPipelineWithOneOutputForTest(kTestDepth, kSampleMask); |
| 608 | |
| 609 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 610 | |
| 611 | // Draw a green triangle. |
| 612 | { |
| 613 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 614 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 615 | kTestDepth); |
| 616 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 617 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 621 | queue.Submit(1, &commandBuffer); |
| 622 | |
| 623 | VerifyResolveTarget(kGreen, mResolveTexture, 0, 0, kMSAACoverage); |
| 624 | } |
| 625 | |
| 626 | // Test using one multisampled color attachment with resolve target can render correctly |
| 627 | // with the final sample mask empty. |
| 628 | TEST_P(MultisampledRenderingTest, ResolveInto2DTextureWithEmptyFinalSampleMask) { |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 629 | constexpr bool kTestDepth = false; |
| 630 | // The third and fourth samples are included, |
| 631 | // none of which is covered by the triangle. |
| 632 | constexpr uint32_t kSampleMask = kThirdSampleMaskBit | kFourthSampleMaskBit; |
| 633 | constexpr float kMSAACoverage = 0.00f; |
| 634 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 635 | wgpu::RenderPipeline pipeline = |
| 636 | CreateRenderPipelineWithOneOutputForTest(kTestDepth, kSampleMask); |
| 637 | |
| 638 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 639 | |
| 640 | // Draw a green triangle. |
| 641 | { |
| 642 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 643 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 644 | kTestDepth); |
| 645 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 646 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 650 | queue.Submit(1, &commandBuffer); |
| 651 | |
| 652 | VerifyResolveTarget(kGreen, mResolveTexture, 0, 0, kMSAACoverage); |
| 653 | } |
| 654 | |
| 655 | // Test doing MSAA resolve into multiple resolve targets works correctly with a non-default sample |
| 656 | // mask. |
| 657 | TEST_P(MultisampledRenderingTest, ResolveIntoMultipleResolveTargetsWithSampleMask) { |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 658 | wgpu::TextureView multisampledColorView2 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 659 | CreateTextureForRenderAttachment(kColorFormat, kSampleCount).CreateView(); |
| 660 | wgpu::Texture resolveTexture2 = CreateTextureForRenderAttachment(kColorFormat, 1); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 661 | wgpu::TextureView resolveView2 = resolveTexture2.CreateView(); |
| 662 | |
| 663 | // The first and fourth samples are included, |
| 664 | // only the first one is covered by the triangle. |
| 665 | constexpr uint32_t kSampleMask = kFirstSampleMaskBit | kFourthSampleMaskBit; |
| 666 | constexpr float kMSAACoverage = 0.25f; |
| 667 | |
| 668 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 669 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithTwoOutputsForTest(kSampleMask); |
| 670 | |
| 671 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
| 672 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.8f}; |
| 673 | constexpr bool kTestDepth = false; |
| 674 | |
| 675 | // Draw a red triangle to the first color attachment, and a blue triangle to the second color |
| 676 | // attachment, and do MSAA resolve on two render targets in one render pass. |
| 677 | { |
| 678 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 679 | {mMultisampledColorView, multisampledColorView2}, {mResolveView, resolveView2}, |
| 680 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, kTestDepth); |
| 681 | |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 682 | std::array<float, 8> kUniformData = {kRed.r, kRed.g, kRed.b, kRed.a, // color1 |
| 683 | kGreen.r, kGreen.g, kGreen.b, kGreen.a}; // color2 |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 684 | constexpr uint32_t kSize = sizeof(kUniformData); |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 685 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), kSize); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 689 | queue.Submit(1, &commandBuffer); |
| 690 | |
| 691 | VerifyResolveTarget(kRed, mResolveTexture, 0, 0, kMSAACoverage); |
| 692 | VerifyResolveTarget(kGreen, resolveTexture2, 0, 0, kMSAACoverage); |
| 693 | } |
| 694 | |
| 695 | // Test multisampled rendering with depth test works correctly with a non-default sample mask. |
| 696 | TEST_P(MultisampledRenderingTest, MultisampledRenderingWithDepthTestAndSampleMask) { |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 697 | constexpr bool kTestDepth = true; |
| 698 | // The second sample is included in the first render pass and it's covered by the triangle. |
| 699 | constexpr uint32_t kSampleMaskGreen = kSecondSampleMaskBit; |
| 700 | // The first and second samples are included in the second render pass, |
| 701 | // both are covered by the triangle. |
| 702 | constexpr uint32_t kSampleMaskRed = kFirstSampleMaskBit | kSecondSampleMaskBit; |
| 703 | constexpr float kMSAACoverage = 0.50f; |
| 704 | |
| 705 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 706 | wgpu::RenderPipeline pipelineGreen = |
| 707 | CreateRenderPipelineWithOneOutputForTest(kTestDepth, kSampleMaskGreen); |
| 708 | wgpu::RenderPipeline pipelineRed = |
| 709 | CreateRenderPipelineWithOneOutputForTest(kTestDepth, kSampleMaskRed); |
| 710 | |
| 711 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
| 712 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.8f}; |
| 713 | |
| 714 | // In first render pass we draw a green triangle with depth value == 0.2f. |
| 715 | // We will only write to the second sample. |
| 716 | { |
| 717 | utils::ComboRenderPassDescriptor renderPass = |
| 718 | CreateComboRenderPassDescriptorForTest({mMultisampledColorView}, {mResolveView}, |
| 719 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, true); |
| 720 | std::array<float, 5> kUniformData = {kGreen.r, kGreen.g, kGreen.b, kGreen.a, // Color |
| 721 | 0.2f}; // depth |
| 722 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 723 | EncodeRenderPassForTest(commandEncoder, renderPass, pipelineGreen, kUniformData.data(), |
| 724 | kSize); |
| 725 | } |
| 726 | |
| 727 | // In second render pass we draw a red triangle with depth value == 0.5f. |
| 728 | // We will only write to the first sample, since the second one is red with a smaller depth |
| 729 | // value. |
| 730 | { |
| 731 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 732 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Load, wgpu::LoadOp::Load, |
| 733 | kTestDepth); |
| 734 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 735 | std::array<float, 5> kUniformData = {kRed.r, kRed.g, kRed.b, kRed.a, // color |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 736 | 0.5f}; // depth |
| 737 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 738 | EncodeRenderPassForTest(commandEncoder, renderPass, pipelineRed, kUniformData.data(), |
| 739 | kSize); |
| 740 | } |
| 741 | |
| 742 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 743 | queue.Submit(1, &commandBuffer); |
| 744 | |
| 745 | constexpr wgpu::Color kHalfGreenHalfRed = {(kGreen.r + kRed.r) / 2.0, (kGreen.g + kRed.g) / 2.0, |
| 746 | (kGreen.b + kRed.b) / 2.0, |
| 747 | (kGreen.a + kRed.a) / 2.0}; |
| 748 | |
| 749 | // The color of the pixel in the middle of mResolveTexture should be half green and half |
| 750 | // red if MSAA resolve runs correctly with depth test. |
| 751 | VerifyResolveTarget(kHalfGreenHalfRed, mResolveTexture, 0, 0, kMSAACoverage); |
| 752 | } |
| 753 | |
| 754 | // Test using one multisampled color attachment with resolve target can render correctly |
| 755 | // with non-default sample mask and shader-output mask. |
| 756 | TEST_P(MultisampledRenderingTest, ResolveInto2DTextureWithSampleMaskAndShaderOutputMask) { |
Ben Clayton | bbc2354 | 2021-02-24 12:50:00 +0000 | [diff] [blame] | 757 | // TODO(github.com/KhronosGroup/SPIRV-Cross/issues/1626): SPIRV-Cross produces bad GLSL for |
| 758 | // unsigned SampleMask builtins |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 759 | DAWN_SUPPRESS_TEST_IF(HasToggleEnabled("use_tint_generator") && (IsOpenGL() || IsOpenGLES())); |
Austin Eng | 3080555 | 2020-12-08 16:49:34 +0000 | [diff] [blame] | 760 | |
Stephen White | 725e03b | 2021-02-10 15:14:05 +0000 | [diff] [blame] | 761 | // TODO(crbug.com/dawn/673): Work around or enforce via validation that sample variables are not |
Stephen White | 02fd17c | 2021-02-10 02:28:17 +0000 | [diff] [blame] | 762 | // supported on some platforms. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 763 | DAWN_TEST_UNSUPPORTED_IF(HasToggleEnabled("disable_sample_variables")); |
Stephen White | 0315c43 | 2021-01-26 12:53:38 +0000 | [diff] [blame] | 764 | |
Austin Eng | ed8a8c0 | 2021-06-04 22:23:56 +0000 | [diff] [blame] | 765 | // TODO(crbug.com/dawn/571): Fails on Metal / D3D12 because SPIRV-Cross produces bad shaders |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 766 | // for the SPIR-V outputted by Tint. Reenable once we use Tint's MSL / HLSL generators. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 767 | DAWN_SUPPRESS_TEST_IF(IsD3D12() || IsMetal()); |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 768 | |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 769 | constexpr bool kTestDepth = false; |
| 770 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 771 | |
| 772 | // The second and third samples are included in the shader-output mask. |
| 773 | // The first and third samples are included in the sample mask. |
| 774 | // Since we're now looking at a fully covered pixel, the rasterization mask |
| 775 | // includes all the samples. |
| 776 | // Thus the final mask includes only the third sample. |
| 777 | constexpr float kMSAACoverage = 0.25f; |
| 778 | constexpr uint32_t kSampleMask = kFirstSampleMaskBit | kThirdSampleMaskBit; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 779 | const char* fs = R"( |
| 780 | [[block]] struct U { |
| 781 | color : vec4<f32>; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 782 | }; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 783 | [[group(0), binding(0)]] var<uniform> uBuffer : U; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 784 | |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 785 | struct FragmentOut { |
| 786 | [[location(0)]] color : vec4<f32>; |
James Price | eae70b7 | 2021-04-19 15:29:49 +0000 | [diff] [blame] | 787 | [[builtin(sample_mask)]] sampleMask : u32; |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 788 | }; |
| 789 | |
| 790 | [[stage(fragment)]] fn main() -> FragmentOut { |
| 791 | var output : FragmentOut; |
| 792 | output.color = uBuffer.color; |
| 793 | output.sampleMask = 6u; |
| 794 | return output; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 795 | })"; |
| 796 | |
| 797 | wgpu::RenderPipeline pipeline = CreateRenderPipelineForTest(fs, 1, false, kSampleMask); |
| 798 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 799 | |
| 800 | // Draw a green triangle. |
| 801 | { |
| 802 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 803 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 804 | kTestDepth); |
| 805 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 806 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 810 | queue.Submit(1, &commandBuffer); |
| 811 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 812 | RGBA8 expectedColor = ExpectedMSAAColor(kGreen, kMSAACoverage); |
Yunchao He | 0da94c3 | 2021-04-08 14:58:42 +0000 | [diff] [blame] | 813 | EXPECT_TEXTURE_EQ(&expectedColor, mResolveTexture, {1, 0}, {1, 1}); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // Test doing MSAA resolve into multiple resolve targets works correctly with a non-default |
| 817 | // shader-output mask. |
| 818 | TEST_P(MultisampledRenderingTest, ResolveIntoMultipleResolveTargetsWithShaderOutputMask) { |
Ben Clayton | bbc2354 | 2021-02-24 12:50:00 +0000 | [diff] [blame] | 819 | // TODO(github.com/KhronosGroup/SPIRV-Cross/issues/1626): SPIRV-Cross produces bad GLSL for |
| 820 | // unsigned SampleMask builtins |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 821 | DAWN_SUPPRESS_TEST_IF(HasToggleEnabled("use_tint_generator") && (IsOpenGL() || IsOpenGLES())); |
Austin Eng | 3080555 | 2020-12-08 16:49:34 +0000 | [diff] [blame] | 822 | |
Stephen White | 725e03b | 2021-02-10 15:14:05 +0000 | [diff] [blame] | 823 | // TODO(crbug.com/dawn/673): Work around or enforce via validation that sample variables are not |
Stephen White | 02fd17c | 2021-02-10 02:28:17 +0000 | [diff] [blame] | 824 | // supported on some platforms. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 825 | DAWN_TEST_UNSUPPORTED_IF(HasToggleEnabled("disable_sample_variables")); |
Stephen White | 0315c43 | 2021-01-26 12:53:38 +0000 | [diff] [blame] | 826 | |
Austin Eng | ed8a8c0 | 2021-06-04 22:23:56 +0000 | [diff] [blame] | 827 | // TODO(crbug.com/dawn/571): Fails on Metal / D3D12 because SPIRV-Cross produces bad shaders |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 828 | // for the SPIR-V outputted by Tint. Reenable once we use Tint's MSL / HLSL generators. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 829 | DAWN_SUPPRESS_TEST_IF(IsD3D12() || IsMetal()); |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 830 | |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 831 | wgpu::TextureView multisampledColorView2 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 832 | CreateTextureForRenderAttachment(kColorFormat, kSampleCount).CreateView(); |
| 833 | wgpu::Texture resolveTexture2 = CreateTextureForRenderAttachment(kColorFormat, 1); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 834 | wgpu::TextureView resolveView2 = resolveTexture2.CreateView(); |
| 835 | |
| 836 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 837 | // The second and third samples are included in the shader-output mask, |
| 838 | // only the first one is covered by the triangle. |
| 839 | constexpr float kMSAACoverage = 0.25f; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 840 | const char* fs = R"( |
| 841 | [[block]] struct U { |
| 842 | color0 : vec4<f32>; |
| 843 | color1 : vec4<f32>; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 844 | }; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 845 | [[group(0), binding(0)]] var<uniform> uBuffer : U; |
Corentin Wallez | 5709060 | 2021-03-19 09:48:11 +0000 | [diff] [blame] | 846 | |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 847 | struct FragmentOut { |
| 848 | [[location(0)]] color0 : vec4<f32>; |
| 849 | [[location(1)]] color1 : vec4<f32>; |
James Price | eae70b7 | 2021-04-19 15:29:49 +0000 | [diff] [blame] | 850 | [[builtin(sample_mask)]] sampleMask : u32; |
Brandon Jones | e87ea2b | 2021-04-14 17:05:07 +0000 | [diff] [blame] | 851 | }; |
| 852 | |
| 853 | [[stage(fragment)]] fn main() -> FragmentOut { |
| 854 | var output : FragmentOut; |
| 855 | output.color0 = uBuffer.color0; |
| 856 | output.color1 = uBuffer.color1; |
| 857 | output.sampleMask = 6u; |
| 858 | return output; |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 859 | })"; |
| 860 | |
| 861 | wgpu::RenderPipeline pipeline = CreateRenderPipelineForTest(fs, 2, false); |
| 862 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.8f}; |
| 863 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.8f}; |
| 864 | constexpr bool kTestDepth = false; |
| 865 | |
| 866 | // Draw a red triangle to the first color attachment, and a blue triangle to the second color |
| 867 | // attachment, and do MSAA resolve on two render targets in one render pass. |
| 868 | { |
| 869 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 870 | {mMultisampledColorView, multisampledColorView2}, {mResolveView, resolveView2}, |
| 871 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, kTestDepth); |
| 872 | |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 873 | std::array<float, 8> kUniformData = {kRed.r, kRed.g, kRed.b, kRed.a, // color1 |
| 874 | kGreen.r, kGreen.g, kGreen.b, kGreen.a}; // color2 |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 875 | constexpr uint32_t kSize = sizeof(kUniformData); |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 876 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), kSize); |
Tomek Ponitka | 4d9cadd | 2020-07-23 11:30:56 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 880 | queue.Submit(1, &commandBuffer); |
| 881 | |
| 882 | VerifyResolveTarget(kRed, mResolveTexture, 0, 0, kMSAACoverage); |
| 883 | VerifyResolveTarget(kGreen, resolveTexture2, 0, 0, kMSAACoverage); |
| 884 | } |
| 885 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 886 | // Test using one multisampled color attachment with resolve target can render correctly |
| 887 | // with alphaToCoverageEnabled. |
| 888 | TEST_P(MultisampledRenderingTest, ResolveInto2DTextureWithAlphaToCoverage) { |
| 889 | constexpr bool kTestDepth = false; |
| 890 | constexpr uint32_t kSampleMask = 0xFFFFFFFF; |
| 891 | constexpr bool kAlphaToCoverageEnabled = true; |
| 892 | |
| 893 | // Setting alpha <= 0 must result in alpha-to-coverage mask being empty. |
| 894 | // Setting alpha = 0.5f should result in alpha-to-coverage mask including half the samples, |
| 895 | // but this is not guaranteed by the spec. The Metal spec seems to guarantee that this is |
| 896 | // indeed the case. |
| 897 | // Setting alpha >= 1 must result in alpha-to-coverage mask being full. |
| 898 | for (float alpha : {-1.0f, 0.0f, 0.5f, 1.0f, 2.0f}) { |
| 899 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 900 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest( |
| 901 | kTestDepth, kSampleMask, kAlphaToCoverageEnabled); |
| 902 | |
| 903 | const wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, alpha}; |
| 904 | |
| 905 | // Draw a green triangle. |
| 906 | { |
| 907 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 908 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 909 | kTestDepth); |
| 910 | |
| 911 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
| 912 | } |
| 913 | |
| 914 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 915 | queue.Submit(1, &commandBuffer); |
| 916 | |
| 917 | // For alpha = {0, 0.5, 1} we expect msaaCoverage to correspond to the value of alpha. |
| 918 | float msaaCoverage = alpha; |
| 919 | if (alpha < 0.0f) { |
| 920 | msaaCoverage = 0.0f; |
| 921 | } |
| 922 | if (alpha > 1.0f) { |
| 923 | msaaCoverage = 1.0f; |
| 924 | } |
| 925 | |
| 926 | RGBA8 expectedColor = ExpectedMSAAColor(kGreen, msaaCoverage); |
Yunchao He | 0da94c3 | 2021-04-08 14:58:42 +0000 | [diff] [blame] | 927 | EXPECT_TEXTURE_EQ(&expectedColor, mResolveTexture, {1, 0}, {1, 1}); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
| 931 | // Test doing MSAA resolve into multiple resolve targets works correctly with |
| 932 | // alphaToCoverage. The alphaToCoverage mask is computed based on the alpha |
Corentin Wallez | 49a1f72 | 2021-01-22 19:51:37 +0000 | [diff] [blame] | 933 | // component of the first color render attachment. |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 934 | TEST_P(MultisampledRenderingTest, ResolveIntoMultipleResolveTargetsWithAlphaToCoverage) { |
| 935 | wgpu::TextureView multisampledColorView2 = |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 936 | CreateTextureForRenderAttachment(kColorFormat, kSampleCount).CreateView(); |
| 937 | wgpu::Texture resolveTexture2 = CreateTextureForRenderAttachment(kColorFormat, 1); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 938 | wgpu::TextureView resolveView2 = resolveTexture2.CreateView(); |
| 939 | constexpr uint32_t kSampleMask = 0xFFFFFFFF; |
| 940 | constexpr float kMSAACoverage = 0.50f; |
| 941 | constexpr bool kAlphaToCoverageEnabled = true; |
| 942 | |
| 943 | // The alpha-to-coverage mask should not depend on the alpha component of the |
Corentin Wallez | 49a1f72 | 2021-01-22 19:51:37 +0000 | [diff] [blame] | 944 | // second color render attachment. |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 945 | // We test alpha = 0.51f and 0.99f instead of 0.50f and 1.00f because there are some rounding |
| 946 | // differences on QuadroP400 devices in that case. |
| 947 | for (float alpha : {0.0f, 0.51f, 0.99f}) { |
| 948 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 949 | wgpu::RenderPipeline pipeline = |
| 950 | CreateRenderPipelineWithTwoOutputsForTest(kSampleMask, kAlphaToCoverageEnabled); |
| 951 | |
| 952 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.51f}; |
| 953 | const wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, alpha}; |
| 954 | constexpr bool kTestDepth = false; |
| 955 | |
| 956 | // Draw a red triangle to the first color attachment, and a blue triangle to the second |
| 957 | // color attachment, and do MSAA resolve on two render targets in one render pass. |
| 958 | { |
| 959 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 960 | {mMultisampledColorView, multisampledColorView2}, {mResolveView, resolveView2}, |
| 961 | wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, kTestDepth); |
| 962 | |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 963 | std::array<float, 8> kUniformData = { |
| 964 | static_cast<float>(kRed.r), static_cast<float>(kRed.g), |
| 965 | static_cast<float>(kRed.b), static_cast<float>(kRed.a), |
| 966 | static_cast<float>(kGreen.r), static_cast<float>(kGreen.g), |
| 967 | static_cast<float>(kGreen.b), static_cast<float>(kGreen.a)}; |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 968 | constexpr uint32_t kSize = sizeof(kUniformData); |
Brandon Jones | 670858d | 2020-09-22 16:51:36 +0000 | [diff] [blame] | 969 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kUniformData.data(), |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 970 | kSize); |
| 971 | } |
| 972 | |
| 973 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 974 | queue.Submit(1, &commandBuffer); |
| 975 | |
| 976 | // Alpha to coverage affects both the color outputs, but the mask is computed |
| 977 | // using only the first one. |
| 978 | RGBA8 expectedRed = ExpectedMSAAColor(kRed, kMSAACoverage); |
| 979 | RGBA8 expectedGreen = ExpectedMSAAColor(kGreen, kMSAACoverage); |
Yunchao He | 0da94c3 | 2021-04-08 14:58:42 +0000 | [diff] [blame] | 980 | EXPECT_TEXTURE_EQ(&expectedRed, mResolveTexture, {1, 0}, {1, 1}); |
| 981 | EXPECT_TEXTURE_EQ(&expectedGreen, resolveTexture2, {1, 0}, {1, 1}); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
| 985 | // Test multisampled rendering with depth test works correctly with alphaToCoverage. |
| 986 | TEST_P(MultisampledRenderingTest, MultisampledRenderingWithDepthTestAndAlphaToCoverage) { |
Corentin Wallez | 6298b63 | 2020-10-19 18:35:39 +0000 | [diff] [blame] | 987 | // This test fails because Swiftshader is off-by-one with its ((a+b)/2 + (c+d)/2)/2 fast resolve |
| 988 | // algorithm. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 989 | DAWN_SUPPRESS_TEST_IF(IsSwiftshader() || IsANGLE()); |
Corentin Wallez | 6298b63 | 2020-10-19 18:35:39 +0000 | [diff] [blame] | 990 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 991 | constexpr bool kTestDepth = true; |
| 992 | constexpr uint32_t kSampleMask = 0xFFFFFFFF; |
| 993 | |
| 994 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 995 | wgpu::RenderPipeline pipelineGreen = |
| 996 | CreateRenderPipelineWithOneOutputForTest(kTestDepth, kSampleMask, true); |
| 997 | wgpu::RenderPipeline pipelineRed = |
| 998 | CreateRenderPipelineWithOneOutputForTest(kTestDepth, kSampleMask, false); |
| 999 | |
| 1000 | // We test alpha = 0.51f and 0.81f instead of 0.50f and 0.80f because there are some |
| 1001 | // rounding differences on QuadroP400 devices in that case. |
| 1002 | constexpr wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, 0.51f}; |
| 1003 | constexpr wgpu::Color kRed = {0.8f, 0.0f, 0.0f, 0.81f}; |
| 1004 | |
| 1005 | // In first render pass we draw a green triangle with depth value == 0.2f. |
| 1006 | // We will only write to half the samples since the alphaToCoverage mode |
| 1007 | // is enabled for that render pass. |
| 1008 | { |
| 1009 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 1010 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 1011 | kTestDepth); |
| 1012 | std::array<float, 5> kUniformData = {kGreen.r, kGreen.g, kGreen.b, kGreen.a, // Color |
| 1013 | 0.2f}; // depth |
| 1014 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 1015 | EncodeRenderPassForTest(commandEncoder, renderPass, pipelineGreen, kUniformData.data(), |
| 1016 | kSize); |
| 1017 | } |
| 1018 | |
| 1019 | // In second render pass we draw a red triangle with depth value == 0.5f. |
| 1020 | // We will write to all the samples since the alphaToCoverageMode is diabled for |
| 1021 | // that render pass. |
| 1022 | { |
| 1023 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 1024 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Load, wgpu::LoadOp::Load, |
| 1025 | kTestDepth); |
| 1026 | |
| 1027 | std::array<float, 5> kUniformData = {kRed.r, kRed.g, kRed.b, kRed.a, // color |
| 1028 | 0.5f}; // depth |
| 1029 | constexpr uint32_t kSize = sizeof(kUniformData); |
| 1030 | EncodeRenderPassForTest(commandEncoder, renderPass, pipelineRed, kUniformData.data(), |
| 1031 | kSize); |
| 1032 | } |
| 1033 | |
| 1034 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 1035 | queue.Submit(1, &commandBuffer); |
| 1036 | |
| 1037 | constexpr wgpu::Color kHalfGreenHalfRed = {(kGreen.r + kRed.r) / 2.0, (kGreen.g + kRed.g) / 2.0, |
| 1038 | (kGreen.b + kRed.b) / 2.0, |
| 1039 | (kGreen.a + kRed.a) / 2.0}; |
| 1040 | RGBA8 expectedColor = ExpectedMSAAColor(kHalfGreenHalfRed, 1.0f); |
| 1041 | |
Yunchao He | 0da94c3 | 2021-04-08 14:58:42 +0000 | [diff] [blame] | 1042 | EXPECT_TEXTURE_EQ(&expectedColor, mResolveTexture, {1, 0}, {1, 1}); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | // Test using one multisampled color attachment with resolve target can render correctly |
| 1046 | // with alphaToCoverageEnabled and a sample mask. |
| 1047 | TEST_P(MultisampledRenderingTest, ResolveInto2DTextureWithAlphaToCoverageAndSampleMask) { |
Corentin Wallez | 6298b63 | 2020-10-19 18:35:39 +0000 | [diff] [blame] | 1048 | // This test fails because Swiftshader is off-by-one with its ((a+b)/2 + (c+d)/2)/2 fast resolve |
| 1049 | // algorithm. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 1050 | DAWN_SUPPRESS_TEST_IF(IsSwiftshader() || IsANGLE()); |
Corentin Wallez | 6298b63 | 2020-10-19 18:35:39 +0000 | [diff] [blame] | 1051 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 1052 | // TODO(dawn:491): This doesn't work on Metal, because we're using both the shader-output |
| 1053 | // mask (emulting the sampleMask from RenderPipeline) and alpha-to-coverage at the same |
| 1054 | // time. See the issue: https://github.com/gpuweb/gpuweb/issues/959. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 1055 | DAWN_SUPPRESS_TEST_IF(IsMetal()); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 1056 | |
| 1057 | constexpr bool kTestDepth = false; |
| 1058 | constexpr float kMSAACoverage = 0.50f; |
| 1059 | constexpr uint32_t kSampleMask = kFirstSampleMaskBit | kThirdSampleMaskBit; |
| 1060 | constexpr bool kAlphaToCoverageEnabled = true; |
| 1061 | |
| 1062 | // For those values of alpha we expect the proportion of samples to be covered |
| 1063 | // to correspond to the value of alpha. |
| 1064 | // We're assuming in the case of alpha = 0.50f that the implementation |
| 1065 | // dependendent algorithm will choose exactly one of the first and third samples. |
| 1066 | for (float alpha : {0.0f, 0.50f, 1.00f}) { |
| 1067 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 1068 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest( |
| 1069 | kTestDepth, kSampleMask, kAlphaToCoverageEnabled); |
| 1070 | |
| 1071 | const wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, alpha - 0.01f}; |
| 1072 | |
| 1073 | // Draw a green triangle. |
| 1074 | { |
| 1075 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 1076 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 1077 | kTestDepth); |
| 1078 | |
| 1079 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
| 1080 | } |
| 1081 | |
| 1082 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 1083 | queue.Submit(1, &commandBuffer); |
| 1084 | |
| 1085 | RGBA8 expectedColor = ExpectedMSAAColor(kGreen, kMSAACoverage * alpha); |
Yunchao He | 0da94c3 | 2021-04-08 14:58:42 +0000 | [diff] [blame] | 1086 | EXPECT_TEXTURE_EQ(&expectedColor, mResolveTexture, {1, 0}, {1, 1}); |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | // Test using one multisampled color attachment with resolve target can render correctly |
| 1091 | // with alphaToCoverageEnabled and a rasterization mask. |
| 1092 | TEST_P(MultisampledRenderingTest, ResolveInto2DTextureWithAlphaToCoverageAndRasterizationMask) { |
Corentin Wallez | 6298b63 | 2020-10-19 18:35:39 +0000 | [diff] [blame] | 1093 | // This test fails because Swiftshader is off-by-one with its ((a+b)/2 + (c+d)/2)/2 fast resolve |
| 1094 | // algorithm. |
Jiawei Shao | 44fc6e3 | 2021-05-26 01:04:32 +0000 | [diff] [blame] | 1095 | DAWN_SUPPRESS_TEST_IF(IsSwiftshader() || IsANGLE()); |
Corentin Wallez | 6298b63 | 2020-10-19 18:35:39 +0000 | [diff] [blame] | 1096 | |
Tomek Ponitka | ab04da4 | 2020-07-29 11:44:41 +0000 | [diff] [blame] | 1097 | constexpr bool kTestDepth = false; |
| 1098 | constexpr float kMSAACoverage = 0.50f; |
| 1099 | constexpr uint32_t kSampleMask = 0xFFFFFFFF; |
| 1100 | constexpr bool kAlphaToCoverageEnabled = true; |
| 1101 | constexpr bool kFlipTriangle = true; |
| 1102 | |
| 1103 | // For those values of alpha we expect the proportion of samples to be covered |
| 1104 | // to correspond to the value of alpha. |
| 1105 | // We're assuming in the case of alpha = 0.50f that the implementation |
| 1106 | // dependendent algorithm will choose exactly one of the samples covered by the |
| 1107 | // triangle. |
| 1108 | for (float alpha : {0.0f, 0.50f, 1.00f}) { |
| 1109 | wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); |
| 1110 | wgpu::RenderPipeline pipeline = CreateRenderPipelineWithOneOutputForTest( |
| 1111 | kTestDepth, kSampleMask, kAlphaToCoverageEnabled, kFlipTriangle); |
| 1112 | |
| 1113 | const wgpu::Color kGreen = {0.0f, 0.8f, 0.0f, alpha - 0.01f}; |
| 1114 | |
| 1115 | // Draw a green triangle. |
| 1116 | { |
| 1117 | utils::ComboRenderPassDescriptor renderPass = CreateComboRenderPassDescriptorForTest( |
| 1118 | {mMultisampledColorView}, {mResolveView}, wgpu::LoadOp::Clear, wgpu::LoadOp::Clear, |
| 1119 | kTestDepth); |
| 1120 | |
| 1121 | EncodeRenderPassForTest(commandEncoder, renderPass, pipeline, kGreen); |
| 1122 | } |
| 1123 | |
| 1124 | wgpu::CommandBuffer commandBuffer = commandEncoder.Finish(); |
| 1125 | queue.Submit(1, &commandBuffer); |
| 1126 | |
| 1127 | VerifyResolveTarget(kGreen, mResolveTexture, 0, 0, kMSAACoverage * alpha); |
| 1128 | } |
| 1129 | } |
| 1130 | |
Jiawei Shao | 15d4c2e | 2019-04-26 07:52:57 +0000 | [diff] [blame] | 1131 | DAWN_INSTANTIATE_TEST(MultisampledRenderingTest, |
Austin Eng | 6c1d646 | 2020-02-25 16:23:17 +0000 | [diff] [blame] | 1132 | D3D12Backend(), |
| 1133 | D3D12Backend({}, {"use_d3d12_resource_heap_tier2"}), |
| 1134 | D3D12Backend({}, {"use_d3d12_render_pass"}), |
| 1135 | MetalBackend(), |
| 1136 | OpenGLBackend(), |
Stephen White | f31b78e | 2020-12-04 15:59:29 +0000 | [diff] [blame] | 1137 | OpenGLESBackend(), |
Austin Eng | 6c1d646 | 2020-02-25 16:23:17 +0000 | [diff] [blame] | 1138 | VulkanBackend(), |
| 1139 | MetalBackend({"emulate_store_and_msaa_resolve"}), |
| 1140 | MetalBackend({"always_resolve_into_zero_level_and_layer"}), |
| 1141 | MetalBackend({"always_resolve_into_zero_level_and_layer", |
Corentin Wallez | 84a5775 | 2019-12-05 14:02:11 +0000 | [diff] [blame] | 1142 | "emulate_store_and_msaa_resolve"})); |