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
Merged
Changes from 5 commits
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
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
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,72 @@ | ||
| // 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 "impeller/renderer/backend/vulkan/pass_bindings_cache.h" | ||
|
|
||
| namespace impeller { | ||
| void PassBindingsCache::bindPipeline(vk::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 PassBindingsCache::setStencilReference(vk::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 PassBindingsCache::setScissor(vk::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 PassBindingsCache::setViewport(vk::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); | ||
| } | ||
|
|
||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // 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 { | ||
|
|
||
| class PassBindingsCache { | ||
| public: | ||
| void bindPipeline(vk::CommandBuffer command_buffer, | ||
| vk::PipelineBindPoint pipeline_bind_point, | ||
| vk::Pipeline pipeline); | ||
|
|
||
| void setStencilReference(vk::CommandBuffer command_buffer, | ||
| vk::StencilFaceFlags face_mask, | ||
| uint32_t reference); | ||
|
|
||
| void setScissor(vk::CommandBuffer command_buffer, | ||
| uint32_t first_scissor, | ||
| uint32_t scissor_count, | ||
| const vk::Rect2D* scissors); | ||
|
|
||
| void setViewport(vk::CommandBuffer command_buffer, | ||
| uint32_t first_viewport, | ||
| uint32_t viewport_count, | ||
| const vk::Viewport* 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 | ||
94 changes: 94 additions & 0 deletions
94
impeller/renderer/backend/vulkan/pass_bindings_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,94 @@ | ||
| // 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_encoder_vk.h" | ||
| #include "impeller/renderer/backend/vulkan/pass_bindings_cache.h" | ||
| #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h" | ||
|
|
||
| namespace impeller { | ||
| namespace testing { | ||
|
|
||
| namespace { | ||
| int32_t CountStringViewInstances(const std::vector<std::string>& strings, | ||
| std::string_view target) { | ||
| int32_t count = 0; | ||
| for (const std::string& str : strings) { | ||
| if (str == target) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| } // namespace | ||
|
|
||
| TEST(PassBindingsCacheTest, bindPipeline) { | ||
| auto context = CreateMockVulkanContext(); | ||
| PassBindingsCache cache; | ||
| auto pool = CommandPoolVK::GetThreadLocal(context.get()); | ||
| CommandEncoderVK encoder(context->GetDeviceHolder(), | ||
| context->GetGraphicsQueue(), pool, | ||
| context->GetFenceWaiter()); | ||
| auto buffer = encoder.GetCommandBuffer(); | ||
| VkPipeline vk_pipeline = reinterpret_cast<VkPipeline>(0xfeedface); | ||
| vk::Pipeline pipeline(vk_pipeline); | ||
| cache.bindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline); | ||
| cache.bindPipeline(buffer, vk::PipelineBindPoint::eGraphics, pipeline); | ||
| std::shared_ptr<std::vector<std::string>> functions = | ||
| GetMockVulkanFunctions(context->GetDevice()); | ||
| EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdBindPipeline"), 1); | ||
| } | ||
|
|
||
| TEST(PassBindingsCacheTest, setStencilReference) { | ||
| auto context = CreateMockVulkanContext(); | ||
| PassBindingsCache cache; | ||
| auto pool = CommandPoolVK::GetThreadLocal(context.get()); | ||
| CommandEncoderVK encoder(context->GetDeviceHolder(), | ||
| context->GetGraphicsQueue(), pool, | ||
| context->GetFenceWaiter()); | ||
| auto buffer = encoder.GetCommandBuffer(); | ||
| cache.setStencilReference( | ||
| buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123); | ||
| cache.setStencilReference( | ||
| buffer, vk::StencilFaceFlagBits::eVkStencilFrontAndBack, 123); | ||
| std::shared_ptr<std::vector<std::string>> functions = | ||
| GetMockVulkanFunctions(context->GetDevice()); | ||
| EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdSetStencilReference"), | ||
| 1); | ||
| } | ||
|
|
||
| TEST(PassBindingsCacheTest, setScissor) { | ||
| auto context = CreateMockVulkanContext(); | ||
| PassBindingsCache cache; | ||
| auto pool = CommandPoolVK::GetThreadLocal(context.get()); | ||
| CommandEncoderVK encoder(context->GetDeviceHolder(), | ||
| context->GetGraphicsQueue(), pool, | ||
| context->GetFenceWaiter()); | ||
| auto buffer = encoder.GetCommandBuffer(); | ||
| vk::Rect2D scissors; | ||
| cache.setScissor(buffer, 0, 1, &scissors); | ||
| cache.setScissor(buffer, 0, 1, &scissors); | ||
| std::shared_ptr<std::vector<std::string>> functions = | ||
| GetMockVulkanFunctions(context->GetDevice()); | ||
| EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdSetScissor"), 1); | ||
| } | ||
|
|
||
| TEST(PassBindingsCacheTest, setViewport) { | ||
| auto context = CreateMockVulkanContext(); | ||
| PassBindingsCache cache; | ||
| auto pool = CommandPoolVK::GetThreadLocal(context.get()); | ||
| CommandEncoderVK encoder(context->GetDeviceHolder(), | ||
| context->GetGraphicsQueue(), pool, | ||
| context->GetFenceWaiter()); | ||
| auto buffer = encoder.GetCommandBuffer(); | ||
| vk::Viewport viewports; | ||
| cache.setViewport(buffer, 0, 1, &viewports); | ||
| cache.setViewport(buffer, 0, 1, &viewports); | ||
| std::shared_ptr<std::vector<std::string>> functions = | ||
| GetMockVulkanFunctions(context->GetDevice()); | ||
| EXPECT_EQ(CountStringViewInstances(*functions, "vkCmdSetViewport"), 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
Oops, something went wrong.
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.
Nit:
CapitalCase.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.
I only did it so that they match verbatim the functions they are caching for (ie
vk::CommandBuffer::bindPipeline).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.
I like being consistent in newly authored code. And besides, the stuff in libc++ is
underscore_case. So having different conventions for code and calls in different components is a thing already.In any case, I don't have a strong opinion about this. But if you are ambivalent about it, I'd rather go with sticking to the style guide.
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