This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] Added cache for command buffers in vulkan #42793
Merged
gaaclarke
merged 6 commits into
flutter-team-archive:main
from
gaaclarke:command_buffer_cache
Jun 13, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
264e3aa
[Impeller] Added cache for command buffers in vulkan
gaaclarke 46b1453
chinmay feedback1
gaaclarke 51c8625
redid the tests
gaaclarke 2041de3
lint and license
gaaclarke 3acfc04
license
gaaclarke 6addb4e
case fix
gaaclarke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <optional> | ||
|
|
||
| #include "flutter/impeller/renderer/backend/vulkan/vk.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| template <typename CommandBuffer> | ||
| class CommandBufferCache { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call this PassBindingsCache like the Metal backend. This class doesn't seem to cache command buffers at all.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done.
"Pass bindings" isn't really accurate either since it is caching anything that is added to the command buffer, not just bindings. I agree, lets keep it consistent though. |
||
| public: | ||
| void bindPipeline(CommandBuffer command_buffer, | ||
| vk::PipelineBindPoint pipeline_bind_point, | ||
| vk::Pipeline pipeline) { | ||
| switch (pipeline_bind_point) { | ||
| case vk::PipelineBindPoint::eGraphics: | ||
| if (graphics_pipeline_.has_value() && | ||
| graphics_pipeline_.value() == pipeline) { | ||
| return; | ||
| } | ||
| graphics_pipeline_ = pipeline; | ||
| break; | ||
| case vk::PipelineBindPoint::eCompute: | ||
| if (compute_pipeline_.has_value() && | ||
| compute_pipeline_.value() == pipeline) { | ||
| return; | ||
| } | ||
| compute_pipeline_ = pipeline; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| command_buffer.bindPipeline(pipeline_bind_point, pipeline); | ||
| } | ||
|
|
||
| void setStencilReference(CommandBuffer command_buffer, | ||
| vk::StencilFaceFlags face_mask, | ||
| uint32_t reference) { | ||
| if (stencil_face_flags_.has_value() && | ||
| face_mask == stencil_face_flags_.value() && | ||
| reference == stencil_reference_) { | ||
| return; | ||
| } | ||
| stencil_face_flags_ = face_mask; | ||
| stencil_reference_ = reference; | ||
| command_buffer.setStencilReference(face_mask, reference); | ||
| } | ||
|
|
||
| void setScissor(CommandBuffer command_buffer, | ||
| uint32_t first_scissor, | ||
| uint32_t scissor_count, | ||
| const vk::Rect2D* scissors) { | ||
| if (first_scissor == 0 && scissor_count == 1) { | ||
| if (scissors_.has_value() && scissors_.value() == scissors[0]) { | ||
| return; | ||
| } | ||
| scissors_ = scissors[0]; | ||
| } | ||
| command_buffer.setScissor(first_scissor, scissor_count, scissors); | ||
| } | ||
|
|
||
| void setViewport(CommandBuffer command_buffer, | ||
| uint32_t first_viewport, | ||
| uint32_t viewport_count, | ||
| const vk::Viewport* viewports) { | ||
| if (first_viewport == 0 && viewport_count == 1) { | ||
| // Note that this is doing equality checks on floating point numbers. | ||
| if (viewport_.has_value() && viewport_.value() == viewports[0]) { | ||
| return; | ||
| } | ||
| viewport_ = viewports[0]; | ||
| } | ||
| command_buffer.setViewport(first_viewport, viewport_count, viewports); | ||
| } | ||
|
|
||
| private: | ||
| // bindPipeline | ||
| std::optional<vk::Pipeline> graphics_pipeline_; | ||
| std::optional<vk::Pipeline> compute_pipeline_; | ||
| // setStencilReference | ||
| std::optional<vk::StencilFaceFlags> stencil_face_flags_; | ||
| uint32_t stencil_reference_ = 0; | ||
| // setScissor | ||
| std::optional<vk::Rect2D> scissors_; | ||
| // setViewport | ||
| std::optional<vk::Viewport> viewport_; | ||
| }; | ||
|
|
||
| } // namespace impeller | ||
95 changes: 95 additions & 0 deletions
95
impeller/renderer/backend/vulkan/command_buffer_cache_unittests.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/testing/testing.h" | ||
| #include "impeller/renderer/backend/vulkan/command_buffer_cache.h" | ||
|
|
||
| namespace impeller { | ||
| namespace testing { | ||
|
|
||
| namespace { | ||
| struct Tallies { | ||
| int32_t bindPipeline_count = 0; | ||
| int32_t setStencilReference_count = 0; | ||
| int32_t setScissor_count = 0; | ||
| int32_t setViewport_count = 0; | ||
| }; | ||
|
|
||
| class MockCommandBuffer { | ||
| public: | ||
| MockCommandBuffer() : tallies_(new Tallies()) {} | ||
|
|
||
| void bindPipeline(vk::PipelineBindPoint pipeline_bind_point, | ||
| vk::Pipeline pipeline) { | ||
| tallies_->bindPipeline_count += 1; | ||
| } | ||
|
|
||
| void setStencilReference(vk::StencilFaceFlags face_mask, uint32_t reference) { | ||
| tallies_->setStencilReference_count += 1; | ||
| } | ||
|
|
||
| void setScissor(uint32_t first_scissor, | ||
| uint32_t scissor_count, | ||
| const vk::Rect2D* scissors) { | ||
| tallies_->setScissor_count += 1; | ||
| } | ||
|
|
||
| void setViewport(uint32_t first_viewport, | ||
| uint32_t viewport_count, | ||
| const vk::Viewport* viewports) { | ||
| tallies_->setViewport_count += 1; | ||
| } | ||
|
|
||
| std::shared_ptr<Tallies> tallies_; | ||
| }; | ||
| } // namespace | ||
|
|
||
| TEST(CommandBufferCacheTest, bindPipeline) { | ||
| CommandBufferCache<MockCommandBuffer> cache; | ||
| MockCommandBuffer buffer; | ||
| VkPipeline vk_pipeline = reinterpret_cast<VkPipeline>(0xfeedface); | ||
| vk::Pipeline pipeline(vk_pipeline); | ||
| ASSERT_EQ(buffer.tallies_->bindPipeline_count, 0); | ||
| cache.bindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline); | ||
| ASSERT_EQ(buffer.tallies_->bindPipeline_count, 1); | ||
| cache.bindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline); | ||
| ASSERT_EQ(buffer.tallies_->bindPipeline_count, 1); | ||
| } | ||
|
|
||
| TEST(CommandBufferCacheTest, setStencilReference) { | ||
| CommandBufferCache<MockCommandBuffer> cache; | ||
| MockCommandBuffer buffer; | ||
| ASSERT_EQ(buffer.tallies_->setStencilReference_count, 0); | ||
| cache.setStencilReference( | ||
| buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123); | ||
| ASSERT_EQ(buffer.tallies_->setStencilReference_count, 1); | ||
| cache.setStencilReference( | ||
| buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123); | ||
| ASSERT_EQ(buffer.tallies_->setStencilReference_count, 1); | ||
| } | ||
|
|
||
| TEST(CommandBufferCacheTest, setScissor) { | ||
| CommandBufferCache<MockCommandBuffer> cache; | ||
| MockCommandBuffer buffer; | ||
| vk::Rect2D scissors; | ||
| ASSERT_EQ(buffer.tallies_->setScissor_count, 0); | ||
| cache.setScissor(buffer, 0, 1, &scissors); | ||
| ASSERT_EQ(buffer.tallies_->setScissor_count, 1); | ||
| cache.setScissor(buffer, 0, 1, &scissors); | ||
| ASSERT_EQ(buffer.tallies_->setScissor_count, 1); | ||
| } | ||
|
|
||
| TEST(CommandBufferCacheTest, setViewport) { | ||
| CommandBufferCache<MockCommandBuffer> cache; | ||
| MockCommandBuffer buffer; | ||
| vk::Viewport viewports; | ||
| ASSERT_EQ(buffer.tallies_->setViewport_count, 0); | ||
| cache.setViewport(buffer, 0, 1, &viewports); | ||
| ASSERT_EQ(buffer.tallies_->setViewport_count, 1); | ||
| cache.setViewport(buffer, 0, 1, &viewports); | ||
| ASSERT_EQ(buffer.tallies_->setViewport_count, 1); | ||
| } | ||
|
|
||
| } // namespace testing | ||
| } // namespace impeller |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason this is templated is to serve the mock. This is unfortunate as it affects our ability to OOL this if needed and makes the code harder to read. Barring another way to test it that doesn't use templates, I'd rather leave the test out and make this simpler (and OOLed). If we mess up our bindings, I'm sure higher level tests will catch this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.