• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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"
19 #include "utils/WGPUHelpers.h"
20 
21 // Test that rendering to a subresource of a texture works.
22 class SubresourceRenderAttachmentTest : public DawnTest {
23     constexpr static uint32_t kRTSize = 2;
24 
25   protected:
26     enum class Type { Color, Depth, Stencil };
27 
DoSingleTest(Type type,wgpu::TextureFormat format,wgpu::Texture renderTarget,uint32_t textureSize,uint32_t baseArrayLayer,uint32_t baseMipLevel)28     void DoSingleTest(Type type,
29                       wgpu::TextureFormat format,
30                       wgpu::Texture renderTarget,
31                       uint32_t textureSize,
32                       uint32_t baseArrayLayer,
33                       uint32_t baseMipLevel) {
34         wgpu::TextureViewDescriptor renderTargetViewDesc;
35         renderTargetViewDesc.baseArrayLayer = baseArrayLayer;
36         renderTargetViewDesc.arrayLayerCount = 1;
37         renderTargetViewDesc.baseMipLevel = baseMipLevel;
38         renderTargetViewDesc.mipLevelCount = 1;
39         wgpu::TextureView renderTargetView = renderTarget.CreateView(&renderTargetViewDesc);
40 
41         RGBA8 expectedColor(0, 255, 0, 255);
42         float expectedDepth = 0.3f;
43         uint8_t expectedStencil = 7;
44 
45         utils::ComboRenderPassDescriptor renderPass = [&]() {
46             switch (type) {
47                 case Type::Color: {
48                     utils::ComboRenderPassDescriptor renderPass({renderTargetView});
49                     renderPass.cColorAttachments[0].clearColor = {
50                         static_cast<float>(expectedColor.r) / 255.f,
51                         static_cast<float>(expectedColor.g) / 255.f,
52                         static_cast<float>(expectedColor.b) / 255.f,
53                         static_cast<float>(expectedColor.a) / 255.f,
54                     };
55                     return renderPass;
56                 }
57                 case Type::Depth: {
58                     utils::ComboRenderPassDescriptor renderPass({}, renderTargetView);
59                     renderPass.cDepthStencilAttachmentInfo.clearDepth = expectedDepth;
60                     return renderPass;
61                 }
62                 case Type::Stencil: {
63                     utils::ComboRenderPassDescriptor renderPass({}, renderTargetView);
64                     renderPass.cDepthStencilAttachmentInfo.clearStencil = expectedStencil;
65                     return renderPass;
66                 }
67                 default:
68                     UNREACHABLE();
69             }
70         }();
71 
72         wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
73         wgpu::RenderPassEncoder passEncoder = commandEncoder.BeginRenderPass(&renderPass);
74         passEncoder.EndPass();
75         wgpu::CommandBuffer commands = commandEncoder.Finish();
76         queue.Submit(1, &commands);
77 
78         const uint32_t renderTargetSize = textureSize >> baseMipLevel;
79         switch (type) {
80             case Type::Color: {
81                 std::vector<RGBA8> expected(renderTargetSize * renderTargetSize, expectedColor);
82                 EXPECT_TEXTURE_EQ(expected.data(), renderTarget, {0, 0, baseArrayLayer},
83                                   {renderTargetSize, renderTargetSize}, baseMipLevel);
84                 break;
85             }
86             case Type::Depth: {
87                 std::vector<float> expected(renderTargetSize * renderTargetSize, expectedDepth);
88                 EXPECT_TEXTURE_EQ(expected.data(), renderTarget, {0, 0, baseArrayLayer},
89                                   {renderTargetSize, renderTargetSize}, baseMipLevel);
90                 break;
91             }
92             case Type::Stencil: {
93                 std::vector<uint8_t> expected(renderTargetSize * renderTargetSize, expectedStencil);
94                 EXPECT_TEXTURE_EQ(expected.data(), renderTarget, {0, 0, baseArrayLayer},
95                                   {renderTargetSize, renderTargetSize}, baseMipLevel,
96                                   wgpu::TextureAspect::StencilOnly);
97                 break;
98             }
99         }
100     }
101 
DoTest(Type type)102     void DoTest(Type type) {
103         constexpr uint32_t kArrayLayerCount = 5;
104         constexpr uint32_t kMipLevelCount = 4;
105 
106         wgpu::TextureFormat format;
107         switch (type) {
108             case Type::Color:
109                 format = wgpu::TextureFormat::RGBA8Unorm;
110                 break;
111             case Type::Depth:
112                 format = wgpu::TextureFormat::Depth32Float;
113                 break;
114             case Type::Stencil:
115                 format = wgpu::TextureFormat::Depth24PlusStencil8;
116                 break;
117             default:
118                 UNREACHABLE();
119         }
120 
121         constexpr uint32_t kTextureSize = kRTSize << (kMipLevelCount - 1);
122 
123         wgpu::TextureDescriptor renderTargetDesc;
124         renderTargetDesc.dimension = wgpu::TextureDimension::e2D;
125         renderTargetDesc.size.width = kTextureSize;
126         renderTargetDesc.size.height = kTextureSize;
127         renderTargetDesc.size.depthOrArrayLayers = kArrayLayerCount;
128         renderTargetDesc.sampleCount = 1;
129         renderTargetDesc.format = format;
130         renderTargetDesc.mipLevelCount = kMipLevelCount;
131         renderTargetDesc.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc;
132 
133         wgpu::Texture renderTarget = device.CreateTexture(&renderTargetDesc);
134 
135         // Test rendering into the first, middle, and last of each of array layer and mip level.
136         for (uint32_t arrayLayer : {0u, kArrayLayerCount / 2, kArrayLayerCount - 1u}) {
137             for (uint32_t mipLevel : {0u, kMipLevelCount / 2, kMipLevelCount - 1u}) {
138                 DoSingleTest(type, format, renderTarget, kTextureSize, arrayLayer, mipLevel);
139             }
140         }
141     }
142 };
143 
144 // Test rendering into a subresource of a color texture
TEST_P(SubresourceRenderAttachmentTest,ColorTexture)145 TEST_P(SubresourceRenderAttachmentTest, ColorTexture) {
146     DoTest(Type::Color);
147 }
148 
149 // Test rendering into a subresource of a depth texture
TEST_P(SubresourceRenderAttachmentTest,DepthTexture)150 TEST_P(SubresourceRenderAttachmentTest, DepthTexture) {
151     DoTest(Type::Depth);
152 }
153 
154 // Test rendering into a subresource of a stencil texture
TEST_P(SubresourceRenderAttachmentTest,StencilTexture)155 TEST_P(SubresourceRenderAttachmentTest, StencilTexture) {
156     // TODO(crbug.com/dawn/667): Work around the fact that some platforms are unable to read
157     // stencil.
158     DAWN_TEST_UNSUPPORTED_IF(HasToggleEnabled("disable_depth_stencil_read"));
159 
160     // TODO(crbug.com/dawn/704): Readback after clear via stencil copy does not work
161     // on some Intel drivers.
162     DAWN_SUPPRESS_TEST_IF(IsMetal() && IsIntel());
163 
164     DoTest(Type::Stencil);
165 }
166 
167 DAWN_INSTANTIATE_TEST(SubresourceRenderAttachmentTest,
168                       D3D12Backend(),
169                       D3D12Backend({}, {"use_d3d12_render_pass"}),
170                       MetalBackend(),
171                       OpenGLBackend(),
172                       OpenGLESBackend(),
173                       VulkanBackend());
174