Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 51c8625

Browse files
committed
redid the tests
1 parent 46b1453 commit 51c8625

File tree

8 files changed

+208
-141
lines changed

8 files changed

+208
-141
lines changed

impeller/renderer/backend/vulkan/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impeller_component("vulkan") {
5252
"fence_waiter_vk.h",
5353
"formats_vk.cc",
5454
"formats_vk.h",
55+
"pass_bindings_cache.cc",
5556
"pass_bindings_cache.h",
5657
"pipeline_cache_vk.cc",
5758
"pipeline_cache_vk.h",

impeller/renderer/backend/vulkan/command_encoder_vk.h

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818

1919
namespace impeller {
2020

21-
namespace testing {
22-
class BlitCommandVkTest_BlitCopyTextureToTextureCommandVK_Test;
23-
class BlitCommandVkTest_BlitCopyTextureToBufferCommandVK_Test;
24-
class BlitCommandVkTest_BlitCopyBufferToTextureCommandVK_Test;
25-
class BlitCommandVkTest_BlitGenerateMipmapCommandVK_Test;
26-
} // namespace testing
27-
2821
class ContextVK;
2922
class DeviceBuffer;
3023
class Buffer;
@@ -37,6 +30,12 @@ class CommandEncoderVK {
3730
public:
3831
using SubmitCallback = std::function<void(bool)>;
3932

33+
// Visible for testing.
34+
CommandEncoderVK(const std::weak_ptr<const DeviceHolder>& device_holder,
35+
const std::shared_ptr<QueueVK>& queue,
36+
const std::shared_ptr<CommandPoolVK>& pool,
37+
std::shared_ptr<FenceWaiterVK> fence_waiter);
38+
4039
~CommandEncoderVK();
4140

4241
bool IsValid() const;
@@ -68,26 +67,13 @@ class CommandEncoderVK {
6867

6968
private:
7069
friend class ContextVK;
71-
friend class ::impeller::testing::
72-
BlitCommandVkTest_BlitCopyTextureToTextureCommandVK_Test;
73-
friend class ::impeller::testing::
74-
BlitCommandVkTest_BlitCopyTextureToBufferCommandVK_Test;
75-
friend class ::impeller::testing::
76-
BlitCommandVkTest_BlitGenerateMipmapCommandVK_Test;
77-
friend class ::impeller::testing::
78-
BlitCommandVkTest_BlitCopyBufferToTextureCommandVK_Test;
7970

8071
std::weak_ptr<const DeviceHolder> device_holder_;
8172
std::shared_ptr<QueueVK> queue_;
8273
std::shared_ptr<FenceWaiterVK> fence_waiter_;
8374
std::shared_ptr<TrackedObjectsVK> tracked_objects_;
8475
bool is_valid_ = false;
8576

86-
CommandEncoderVK(const std::weak_ptr<const DeviceHolder>& device_holder,
87-
const std::shared_ptr<QueueVK>& queue,
88-
const std::shared_ptr<CommandPoolVK>& pool,
89-
std::shared_ptr<FenceWaiterVK> fence_waiter);
90-
9177
void Reset();
9278

9379
FML_DISALLOW_COPY_AND_ASSIGN(CommandEncoderVK);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 "impeller/renderer/backend/vulkan/pass_bindings_cache.h"
6+
7+
namespace impeller {
8+
void PassBindingsCache::bindPipeline(vk::CommandBuffer command_buffer,
9+
vk::PipelineBindPoint pipeline_bind_point,
10+
vk::Pipeline pipeline) {
11+
switch (pipeline_bind_point) {
12+
case vk::PipelineBindPoint::eGraphics:
13+
if (graphics_pipeline_.has_value() &&
14+
graphics_pipeline_.value() == pipeline) {
15+
return;
16+
}
17+
graphics_pipeline_ = pipeline;
18+
break;
19+
case vk::PipelineBindPoint::eCompute:
20+
if (compute_pipeline_.has_value() &&
21+
compute_pipeline_.value() == pipeline) {
22+
return;
23+
}
24+
compute_pipeline_ = pipeline;
25+
break;
26+
default:
27+
break;
28+
}
29+
command_buffer.bindPipeline(pipeline_bind_point, pipeline);
30+
}
31+
32+
void PassBindingsCache::setStencilReference(vk::CommandBuffer command_buffer,
33+
vk::StencilFaceFlags face_mask,
34+
uint32_t reference) {
35+
if (stencil_face_flags_.has_value() &&
36+
face_mask == stencil_face_flags_.value() &&
37+
reference == stencil_reference_) {
38+
return;
39+
}
40+
stencil_face_flags_ = face_mask;
41+
stencil_reference_ = reference;
42+
command_buffer.setStencilReference(face_mask, reference);
43+
}
44+
45+
void PassBindingsCache::setScissor(vk::CommandBuffer command_buffer,
46+
uint32_t first_scissor,
47+
uint32_t scissor_count,
48+
const vk::Rect2D* scissors) {
49+
if (first_scissor == 0 && scissor_count == 1) {
50+
if (scissors_.has_value() && scissors_.value() == scissors[0]) {
51+
return;
52+
}
53+
scissors_ = scissors[0];
54+
}
55+
command_buffer.setScissor(first_scissor, scissor_count, scissors);
56+
}
57+
58+
void PassBindingsCache::setViewport(vk::CommandBuffer command_buffer,
59+
uint32_t first_viewport,
60+
uint32_t viewport_count,
61+
const vk::Viewport* viewports) {
62+
if (first_viewport == 0 && viewport_count == 1) {
63+
// Note that this is doing equality checks on floating point numbers.
64+
if (viewport_.has_value() && viewport_.value() == viewports[0]) {
65+
return;
66+
}
67+
viewport_ = viewports[0];
68+
}
69+
command_buffer.setViewport(first_viewport, viewport_count, viewports);
70+
}
71+
72+
} // namespace impeller

impeller/renderer/backend/vulkan/pass_bindings_cache.h

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,25 @@
1010

1111
namespace impeller {
1212

13-
template <typename CommandBuffer>
1413
class PassBindingsCache {
1514
public:
16-
void bindPipeline(CommandBuffer command_buffer,
15+
void bindPipeline(vk::CommandBuffer command_buffer,
1716
vk::PipelineBindPoint pipeline_bind_point,
18-
vk::Pipeline pipeline) {
19-
switch (pipeline_bind_point) {
20-
case vk::PipelineBindPoint::eGraphics:
21-
if (graphics_pipeline_.has_value() &&
22-
graphics_pipeline_.value() == pipeline) {
23-
return;
24-
}
25-
graphics_pipeline_ = pipeline;
26-
break;
27-
case vk::PipelineBindPoint::eCompute:
28-
if (compute_pipeline_.has_value() &&
29-
compute_pipeline_.value() == pipeline) {
30-
return;
31-
}
32-
compute_pipeline_ = pipeline;
33-
break;
34-
default:
35-
break;
36-
}
37-
command_buffer.bindPipeline(pipeline_bind_point, pipeline);
38-
}
17+
vk::Pipeline pipeline);
3918

40-
void setStencilReference(CommandBuffer command_buffer,
19+
void setStencilReference(vk::CommandBuffer command_buffer,
4120
vk::StencilFaceFlags face_mask,
42-
uint32_t reference) {
43-
if (stencil_face_flags_.has_value() &&
44-
face_mask == stencil_face_flags_.value() &&
45-
reference == stencil_reference_) {
46-
return;
47-
}
48-
stencil_face_flags_ = face_mask;
49-
stencil_reference_ = reference;
50-
command_buffer.setStencilReference(face_mask, reference);
51-
}
21+
uint32_t reference);
5222

53-
void setScissor(CommandBuffer command_buffer,
23+
void setScissor(vk::CommandBuffer command_buffer,
5424
uint32_t first_scissor,
5525
uint32_t scissor_count,
56-
const vk::Rect2D* scissors) {
57-
if (first_scissor == 0 && scissor_count == 1) {
58-
if (scissors_.has_value() && scissors_.value() == scissors[0]) {
59-
return;
60-
}
61-
scissors_ = scissors[0];
62-
}
63-
command_buffer.setScissor(first_scissor, scissor_count, scissors);
64-
}
26+
const vk::Rect2D* scissors);
6527

66-
void setViewport(CommandBuffer command_buffer,
28+
void setViewport(vk::CommandBuffer command_buffer,
6729
uint32_t first_viewport,
6830
uint32_t viewport_count,
69-
const vk::Viewport* viewports) {
70-
if (first_viewport == 0 && viewport_count == 1) {
71-
// Note that this is doing equality checks on floating point numbers.
72-
if (viewport_.has_value() && viewport_.value() == viewports[0]) {
73-
return;
74-
}
75-
viewport_ = viewports[0];
76-
}
77-
command_buffer.setViewport(first_viewport, viewport_count, viewports);
78-
}
31+
const vk::Viewport* viewports);
7932

8033
private:
8134
// bindPipeline

impeller/renderer/backend/vulkan/pass_bindings_cache_unittests.cc

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,91 @@
33
// found in the LICENSE file.
44

55
#include "flutter/testing/testing.h"
6+
#include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
67
#include "impeller/renderer/backend/vulkan/pass_bindings_cache.h"
8+
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
79

810
namespace impeller {
911
namespace testing {
1012

1113
namespace {
12-
struct Tallies {
13-
int32_t bindPipeline_count = 0;
14-
int32_t setStencilReference_count = 0;
15-
int32_t setScissor_count = 0;
16-
int32_t setViewport_count = 0;
17-
};
18-
19-
class MockCommandBuffer {
20-
public:
21-
MockCommandBuffer() : tallies_(new Tallies()) {}
22-
23-
void bindPipeline(vk::PipelineBindPoint pipeline_bind_point,
24-
vk::Pipeline pipeline) {
25-
tallies_->bindPipeline_count += 1;
26-
}
27-
28-
void setStencilReference(vk::StencilFaceFlags face_mask, uint32_t reference) {
29-
tallies_->setStencilReference_count += 1;
30-
}
31-
32-
void setScissor(uint32_t first_scissor,
33-
uint32_t scissor_count,
34-
const vk::Rect2D* scissors) {
35-
tallies_->setScissor_count += 1;
36-
}
37-
38-
void setViewport(uint32_t first_viewport,
39-
uint32_t viewport_count,
40-
const vk::Viewport* viewports) {
41-
tallies_->setViewport_count += 1;
14+
int32_t CountStringViewInstances(const std::vector<std::string>& strings,
15+
std::string_view target) {
16+
int32_t count = 0;
17+
for (const std::string& str : strings) {
18+
if (str == target) {
19+
count++;
20+
}
4221
}
43-
44-
std::shared_ptr<Tallies> tallies_;
45-
};
22+
return count;
23+
}
4624
} // namespace
4725

4826
TEST(PassBindingsCacheTest, bindPipeline) {
49-
PassBindingsCache<MockCommandBuffer> cache;
50-
MockCommandBuffer buffer;
27+
auto context = CreateMockVulkanContext();
28+
PassBindingsCache cache;
29+
auto pool = CommandPoolVK::GetThreadLocal(context.get());
30+
CommandEncoderVK encoder(context->GetDeviceHolder(),
31+
context->GetGraphicsQueue(), pool,
32+
context->GetFenceWaiter());
33+
auto buffer = encoder.GetCommandBuffer();
5134
VkPipeline vk_pipeline = reinterpret_cast<VkPipeline>(0xfeedface);
5235
vk::Pipeline pipeline(vk_pipeline);
53-
ASSERT_EQ(buffer.tallies_->bindPipeline_count, 0);
5436
cache.bindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline);
55-
ASSERT_EQ(buffer.tallies_->bindPipeline_count, 1);
5637
cache.bindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline);
57-
ASSERT_EQ(buffer.tallies_->bindPipeline_count, 1);
38+
std::shared_ptr<std::vector<std::string>> functions =
39+
GetMockVulkanFunctions(context->GetDevice());
40+
EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdBindPipeline"), 1);
5841
}
5942

6043
TEST(PassBindingsCacheTest, setStencilReference) {
61-
PassBindingsCache<MockCommandBuffer> cache;
62-
MockCommandBuffer buffer;
63-
ASSERT_EQ(buffer.tallies_->setStencilReference_count, 0);
44+
auto context = CreateMockVulkanContext();
45+
PassBindingsCache cache;
46+
auto pool = CommandPoolVK::GetThreadLocal(context.get());
47+
CommandEncoderVK encoder(context->GetDeviceHolder(),
48+
context->GetGraphicsQueue(), pool,
49+
context->GetFenceWaiter());
50+
auto buffer = encoder.GetCommandBuffer();
6451
cache.setStencilReference(
6552
buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123);
66-
ASSERT_EQ(buffer.tallies_->setStencilReference_count, 1);
6753
cache.setStencilReference(
6854
buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123);
69-
ASSERT_EQ(buffer.tallies_->setStencilReference_count, 1);
55+
std::shared_ptr<std::vector<std::string>> functions =
56+
GetMockVulkanFunctions(context->GetDevice());
57+
EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdSetStencilReference"),
58+
1);
7059
}
7160

7261
TEST(PassBindingsCacheTest, setScissor) {
73-
PassBindingsCache<MockCommandBuffer> cache;
74-
MockCommandBuffer buffer;
62+
auto context = CreateMockVulkanContext();
63+
PassBindingsCache cache;
64+
auto pool = CommandPoolVK::GetThreadLocal(context.get());
65+
CommandEncoderVK encoder(context->GetDeviceHolder(),
66+
context->GetGraphicsQueue(), pool,
67+
context->GetFenceWaiter());
68+
auto buffer = encoder.GetCommandBuffer();
7569
vk::Rect2D scissors;
76-
ASSERT_EQ(buffer.tallies_->setScissor_count, 0);
7770
cache.setScissor(buffer, 0, 1, &scissors);
78-
ASSERT_EQ(buffer.tallies_->setScissor_count, 1);
7971
cache.setScissor(buffer, 0, 1, &scissors);
80-
ASSERT_EQ(buffer.tallies_->setScissor_count, 1);
72+
std::shared_ptr<std::vector<std::string>> functions =
73+
GetMockVulkanFunctions(context->GetDevice());
74+
EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdSetScissor"), 1);
8175
}
8276

8377
TEST(PassBindingsCacheTest, setViewport) {
84-
PassBindingsCache<MockCommandBuffer> cache;
85-
MockCommandBuffer buffer;
78+
auto context = CreateMockVulkanContext();
79+
PassBindingsCache cache;
80+
auto pool = CommandPoolVK::GetThreadLocal(context.get());
81+
CommandEncoderVK encoder(context->GetDeviceHolder(),
82+
context->GetGraphicsQueue(), pool,
83+
context->GetFenceWaiter());
84+
auto buffer = encoder.GetCommandBuffer();
8685
vk::Viewport viewports;
87-
ASSERT_EQ(buffer.tallies_->setViewport_count, 0);
8886
cache.setViewport(buffer, 0, 1, &viewports);
89-
ASSERT_EQ(buffer.tallies_->setViewport_count, 1);
9087
cache.setViewport(buffer, 0, 1, &viewports);
91-
ASSERT_EQ(buffer.tallies_->setViewport_count, 1);
88+
std::shared_ptr<std::vector<std::string>> functions =
89+
GetMockVulkanFunctions(context->GetDevice());
90+
EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdSetViewport"), 1);
9291
}
9392

9493
} // namespace testing

impeller/renderer/backend/vulkan/render_pass_vk.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,10 @@ static bool AllocateAndBindDescriptorSets(const ContextVK& context,
415415
return true;
416416
}
417417

418-
static void SetViewportAndScissor(
419-
const Command& command,
420-
const vk::CommandBuffer& cmd_buffer,
421-
PassBindingsCache<vk::CommandBuffer>& cmd_buffer_cache,
422-
const ISize& target_size) {
418+
static void SetViewportAndScissor(const Command& command,
419+
const vk::CommandBuffer& cmd_buffer,
420+
PassBindingsCache& cmd_buffer_cache,
421+
const ISize& target_size) {
423422
// Set the viewport.
424423
const auto& vp = command.viewport.value_or<Viewport>(
425424
{.rect = Rect::MakeSize(target_size)});
@@ -440,12 +439,11 @@ static void SetViewportAndScissor(
440439
cmd_buffer_cache.setScissor(cmd_buffer, 0, 1, &scissor);
441440
}
442441

443-
static bool EncodeCommand(
444-
const Context& context,
445-
const Command& command,
446-
CommandEncoderVK& encoder,
447-
PassBindingsCache<vk::CommandBuffer>& command_buffer_cache,
448-
const ISize& target_size) {
442+
static bool EncodeCommand(const Context& context,
443+
const Command& command,
444+
CommandEncoderVK& encoder,
445+
PassBindingsCache& command_buffer_cache,
446+
const ISize& target_size) {
449447
if (command.vertex_count == 0u || command.instance_count == 0u) {
450448
return true;
451449
}

0 commit comments

Comments
 (0)