Flutter Impeller
render_pass_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"
11 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
13 #include "vulkan/vulkan_enums.hpp"
14 
15 namespace impeller {
16 namespace testing {
17 
18 TEST(RenderPassVK, DoesNotRedundantlySetStencil) {
19  std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
20  std::shared_ptr<Context> copy = context;
21  auto cmd_buffer = context->CreateCommandBuffer();
22 
23  RenderTargetAllocator allocator(context->GetResourceAllocator());
24  RenderTarget target = allocator.CreateOffscreenMSAA(*copy.get(), {1, 1}, 1);
25 
26  std::shared_ptr<RenderPass> render_pass =
27  cmd_buffer->CreateRenderPass(target);
28 
29  // Stencil reference set once at buffer start.
30  auto called_functions = GetMockVulkanFunctions(context->GetDevice());
31  EXPECT_EQ(std::count(called_functions->begin(), called_functions->end(),
32  "vkCmdSetStencilReference"),
33  1);
34 
35  // Duplicate stencil ref is not replaced.
36  render_pass->SetStencilReference(0);
37  render_pass->SetStencilReference(0);
38  render_pass->SetStencilReference(0);
39 
40  called_functions = GetMockVulkanFunctions(context->GetDevice());
41  EXPECT_EQ(std::count(called_functions->begin(), called_functions->end(),
42  "vkCmdSetStencilReference"),
43  1);
44 
45  // Different stencil value is updated.
46  render_pass->SetStencilReference(1);
47  called_functions = GetMockVulkanFunctions(context->GetDevice());
48  EXPECT_EQ(std::count(called_functions->begin(), called_functions->end(),
49  "vkCmdSetStencilReference"),
50  2);
51 }
52 
53 // Regression guard for the bug where `RenderPassVK::SetViewport` silently
54 // dropped the user's X and Y offsets and the depth range, only honoring
55 // width and height.
56 TEST(RenderPassVK, SetViewportPropagatesAllUserSuppliedFields) {
57  std::shared_ptr<ContextVK> context = MockVulkanContextBuilder().Build();
58  auto cmd_buffer = context->CreateCommandBuffer();
59 
60  RenderTargetAllocator allocator(context->GetResourceAllocator());
61  RenderTarget target = allocator.CreateOffscreenMSAA(*context, {100, 100},
62  /*mip_count=*/1);
63 
64  std::shared_ptr<RenderPass> render_pass =
65  cmd_buffer->CreateRenderPass(target);
66 
67  // The render pass constructor sets an initial full-target viewport. Capture
68  // a reference to the recorded viewport list before the user-driven call so
69  // we can isolate the call under test.
70  VkCommandBuffer raw_cmd_buffer =
72  const std::vector<VkViewport>& recorded =
73  GetRecordedViewports(raw_cmd_buffer);
74  ASSERT_EQ(recorded.size(), 1u); // Initial full-target viewport.
75 
76  render_pass->SetViewport(Viewport{
77  .rect = Rect::MakeXYWH(25, 10, 50, 80),
78  .depth_range = DepthRange{.z_near = 0.25f, .z_far = 0.75f},
79  });
80 
81  ASSERT_EQ(recorded.size(), 2u);
82  const VkViewport& vp = recorded[1];
83  EXPECT_FLOAT_EQ(vp.x, 25.0f);
84  // Y is computed for the negative-height Y-flip: `y + height` of the
85  // original rect.
86  EXPECT_FLOAT_EQ(vp.y, 90.0f);
87  EXPECT_FLOAT_EQ(vp.width, 50.0f);
88  EXPECT_FLOAT_EQ(vp.height, -80.0f);
89  EXPECT_FLOAT_EQ(vp.minDepth, 0.25f);
90  EXPECT_FLOAT_EQ(vp.maxDepth, 0.75f);
91 }
92 
93 } // namespace testing
94 } // namespace impeller
static CommandBufferVK & Cast(CommandBuffer &base)
Definition: backend_cast.h:13
vk::CommandBuffer GetCommandBuffer() const
Retrieve the native command buffer from this object.
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
virtual RenderTarget CreateOffscreenMSAA(const Context &context, ISize size, int mip_count, std::string_view label="Offscreen MSAA", RenderTarget::AttachmentConfigMSAA color_attachment_config=RenderTarget::kDefaultColorAttachmentConfigMSAA, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_msaa_texture=nullptr, const std::shared_ptr< Texture > &existing_color_resolve_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr, std::optional< PixelFormat > target_pixel_format=std::nullopt)
TEST(AllocationSizeTest, CanCreateTypedAllocations)
std::shared_ptr< ContextGLES > context
std::shared_ptr< RenderPass > render_pass
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136