Flutter Impeller
render_pass_builder_vk_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/testing/testing.h" // IWYU pragma: keep
6 #include "gtest/gtest.h"
9 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
10 #include "vulkan/vulkan_enums.hpp"
11 
12 namespace impeller {
13 namespace testing {
14 
15 TEST(RenderPassBuilder, CreatesRenderPassWithNoDepthStencil) {
17  auto const context = MockVulkanContextBuilder().Build();
18 
19  // Create a single color attachment with a transient depth stencil.
23 
24  auto render_pass = builder.Build(context->GetDevice());
25 
26  EXPECT_TRUE(!!render_pass);
27  EXPECT_FALSE(builder.GetDepthStencil().has_value());
28 }
29 
30 TEST(RenderPassBuilder, RenderPassWithLoadOpUsesCurrentLayout) {
32  auto const context = MockVulkanContextBuilder().Build();
33 
37  vk::ImageLayout::eColorAttachmentOptimal);
38 
39  auto render_pass = builder.Build(context->GetDevice());
40 
41  EXPECT_TRUE(!!render_pass);
42 
43  std::optional<vk::AttachmentDescription> maybe_color = builder.GetColor0();
44  ASSERT_TRUE(maybe_color.has_value());
45  if (!maybe_color.has_value()) {
46  return;
47  }
48  vk::AttachmentDescription color = maybe_color.value();
49 
50  EXPECT_EQ(color.initialLayout, vk::ImageLayout::eColorAttachmentOptimal);
51  EXPECT_EQ(color.finalLayout, vk::ImageLayout::eShaderReadOnlyOptimal);
52  EXPECT_EQ(color.loadOp, vk::AttachmentLoadOp::eLoad);
53  EXPECT_EQ(color.storeOp, vk::AttachmentStoreOp::eStore);
54 }
55 
56 TEST(RenderPassBuilder, CreatesRenderPassWithCombinedDepthStencil) {
58  auto const context = MockVulkanContextBuilder().Build();
59 
60  // Create a single color attachment with a transient depth stencil.
63  StoreAction::kStore, vk::ImageLayout::eGeneral);
67 
68  auto render_pass = builder.Build(context->GetDevice());
69 
70  EXPECT_TRUE(!!render_pass);
71 
72  std::optional<vk::AttachmentDescription> maybe_color = builder.GetColor0();
73  ASSERT_TRUE(maybe_color.has_value());
74  if (!maybe_color.has_value()) {
75  return;
76  }
77  vk::AttachmentDescription color = maybe_color.value();
78 
79  EXPECT_EQ(color.initialLayout, vk::ImageLayout::eUndefined);
80  EXPECT_EQ(color.finalLayout, vk::ImageLayout::eShaderReadOnlyOptimal);
81  EXPECT_EQ(color.loadOp, vk::AttachmentLoadOp::eClear);
82  EXPECT_EQ(color.storeOp, vk::AttachmentStoreOp::eStore);
83 
84  std::optional<vk::AttachmentDescription> maybe_depth_stencil =
85  builder.GetDepthStencil();
86  ASSERT_TRUE(maybe_depth_stencil.has_value());
87  if (!maybe_depth_stencil.has_value()) {
88  return;
89  }
90  vk::AttachmentDescription depth_stencil = maybe_depth_stencil.value();
91 
92  EXPECT_EQ(depth_stencil.initialLayout, vk::ImageLayout::eUndefined);
93  EXPECT_EQ(depth_stencil.finalLayout,
94  vk::ImageLayout::eDepthStencilAttachmentOptimal);
95  EXPECT_EQ(depth_stencil.loadOp, vk::AttachmentLoadOp::eDontCare);
96  EXPECT_EQ(depth_stencil.storeOp, vk::AttachmentStoreOp::eDontCare);
97  EXPECT_EQ(depth_stencil.stencilLoadOp, vk::AttachmentLoadOp::eDontCare);
98  EXPECT_EQ(depth_stencil.stencilStoreOp, vk::AttachmentStoreOp::eDontCare);
99 }
100 
101 TEST(RenderPassBuilder, CreatesRenderPassWithOnlyStencil) {
103  auto const context = MockVulkanContextBuilder().Build();
104 
105  // Create a single color attachment with a transient depth stencil.
111 
112  auto render_pass = builder.Build(context->GetDevice());
113 
114  EXPECT_TRUE(!!render_pass);
115 
116  std::optional<vk::AttachmentDescription> maybe_depth_stencil =
117  builder.GetDepthStencil();
118  ASSERT_TRUE(maybe_depth_stencil.has_value());
119  if (!maybe_depth_stencil.has_value()) {
120  return;
121  }
122  vk::AttachmentDescription depth_stencil = maybe_depth_stencil.value();
123 
124  EXPECT_EQ(depth_stencil.initialLayout, vk::ImageLayout::eUndefined);
125  EXPECT_EQ(depth_stencil.finalLayout,
126  vk::ImageLayout::eDepthStencilAttachmentOptimal);
127  EXPECT_EQ(depth_stencil.loadOp, vk::AttachmentLoadOp::eDontCare);
128  EXPECT_EQ(depth_stencil.storeOp, vk::AttachmentStoreOp::eDontCare);
129  EXPECT_EQ(depth_stencil.stencilLoadOp, vk::AttachmentLoadOp::eDontCare);
130  EXPECT_EQ(depth_stencil.stencilStoreOp, vk::AttachmentStoreOp::eDontCare);
131 }
132 
133 TEST(RenderPassBuilder, CreatesMSAAResolveWithCorrectStore) {
135  auto const context = MockVulkanContextBuilder().Build();
136 
137  // Create an MSAA color attachment.
141 
142  auto render_pass = builder.Build(context->GetDevice());
143 
144  EXPECT_TRUE(!!render_pass);
145 
146  auto maybe_color = builder.GetColor0();
147  ASSERT_TRUE(maybe_color.has_value());
148  if (!maybe_color.has_value()) {
149  return;
150  }
151  vk::AttachmentDescription color = maybe_color.value();
152 
153  // MSAA Texture.
154  EXPECT_EQ(color.initialLayout, vk::ImageLayout::eUndefined);
155  EXPECT_EQ(color.finalLayout, vk::ImageLayout::eGeneral);
156  EXPECT_EQ(color.loadOp, vk::AttachmentLoadOp::eClear);
157  EXPECT_EQ(color.storeOp, vk::AttachmentStoreOp::eDontCare);
158 
159  auto maybe_resolve = builder.GetColor0Resolve();
160  ASSERT_TRUE(maybe_resolve.has_value());
161  if (!maybe_resolve.has_value()) {
162  return;
163  }
164  vk::AttachmentDescription resolve = maybe_resolve.value();
165 
166  // MSAA Resolve Texture.
167  EXPECT_EQ(resolve.initialLayout, vk::ImageLayout::eUndefined);
168  EXPECT_EQ(resolve.finalLayout, vk::ImageLayout::eShaderReadOnlyOptimal);
169  EXPECT_EQ(resolve.loadOp, vk::AttachmentLoadOp::eClear);
170  EXPECT_EQ(resolve.storeOp, vk::AttachmentStoreOp::eStore);
171 }
172 
173 } // namespace testing
174 } // namespace impeller
std::optional< vk::AttachmentDescription > GetColor0() const
RenderPassBuilderVK & SetDepthStencilAttachment(PixelFormat format, SampleCount sample_count, LoadAction load_action, StoreAction store_action)
RenderPassBuilderVK & SetStencilAttachment(PixelFormat format, SampleCount sample_count, LoadAction load_action, StoreAction store_action)
const std::optional< vk::AttachmentDescription > & GetDepthStencil() const
RenderPassBuilderVK & SetColorAttachment(size_t index, PixelFormat format, SampleCount sample_count, LoadAction load_action, StoreAction store_action, vk::ImageLayout current_layout=vk::ImageLayout::eUndefined, bool is_swapchain=false)
vk::UniqueRenderPass Build(const vk::Device &device) const
std::optional< vk::AttachmentDescription > GetColor0Resolve() const
TEST(AllocationSizeTest, CanCreateTypedAllocations)