Flutter Impeller
command_buffer_vk.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 
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "fml/logging.h"
19 
20 namespace impeller {
21 
22 CommandBufferVK::CommandBufferVK(
23  std::weak_ptr<const Context> context,
24  std::weak_ptr<const DeviceHolderVK> device_holder,
25  std::shared_ptr<TrackedObjectsVK> tracked_objects,
26  std::shared_ptr<FenceWaiterVK> fence_waiter)
27  : CommandBuffer(std::move(context)),
28  device_holder_(std::move(device_holder)),
29  tracked_objects_(std::move(tracked_objects)),
30  fence_waiter_(std::move(fence_waiter)) {}
31 
33 
34 void CommandBufferVK::SetLabel(const std::string& label) const {
35  auto context = context_.lock();
36  if (!context) {
37  return;
38  }
39  ContextVK::Cast(*context).SetDebugName(GetCommandBuffer(), label);
40 }
41 
42 bool CommandBufferVK::IsValid() const {
43  return true;
44 }
45 
46 bool CommandBufferVK::OnSubmitCommands(CompletionCallback callback) {
47  FML_UNREACHABLE()
48 }
49 
50 void CommandBufferVK::OnWaitUntilScheduled() {}
51 
52 std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
53  RenderTarget target) {
54  auto context = context_.lock();
55  if (!context) {
56  return nullptr;
57  }
58  auto pass =
59  std::shared_ptr<RenderPassVK>(new RenderPassVK(context, //
60  target, //
61  shared_from_this() //
62  ));
63  if (!pass->IsValid()) {
64  return nullptr;
65  }
66  return pass;
67 }
68 
69 std::shared_ptr<BlitPass> CommandBufferVK::OnCreateBlitPass() {
70  if (!IsValid()) {
71  return nullptr;
72  }
73  auto pass = std::shared_ptr<BlitPassVK>(new BlitPassVK(shared_from_this()));
74  if (!pass->IsValid()) {
75  return nullptr;
76  }
77  return pass;
78 }
79 
80 std::shared_ptr<ComputePass> CommandBufferVK::OnCreateComputePass() {
81  if (!IsValid()) {
82  return nullptr;
83  }
84  auto context = context_.lock();
85  if (!context) {
86  return nullptr;
87  }
88  auto pass =
89  std::shared_ptr<ComputePassVK>(new ComputePassVK(context, //
90  shared_from_this() //
91  ));
92  if (!pass->IsValid()) {
93  return nullptr;
94  }
95  return pass;
96 }
97 
99  InsertDebugMarker("QueueSubmit");
100 
101  auto command_buffer = GetCommandBuffer();
102  tracked_objects_->GetGPUProbe().RecordCmdBufferEnd(command_buffer);
103 
104  auto status = command_buffer.end();
105  if (status != vk::Result::eSuccess) {
106  VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(status);
107  return false;
108  }
109  return true;
110 }
111 
112 vk::CommandBuffer CommandBufferVK::GetCommandBuffer() const {
113  if (tracked_objects_) {
114  return tracked_objects_->GetCommandBuffer();
115  }
116  return {};
117 }
118 
119 bool CommandBufferVK::Track(std::shared_ptr<SharedObjectVK> object) {
120  if (!IsValid()) {
121  return false;
122  }
123  tracked_objects_->Track(std::move(object));
124  return true;
125 }
126 
127 bool CommandBufferVK::Track(const std::shared_ptr<const DeviceBuffer>& buffer) {
128  if (!IsValid()) {
129  return false;
130  }
131  tracked_objects_->Track(buffer);
132  return true;
133 }
134 
136  const std::shared_ptr<const DeviceBuffer>& buffer) const {
137  if (!IsValid()) {
138  return false;
139  }
140  return tracked_objects_->IsTracking(buffer);
141 }
142 
143 bool CommandBufferVK::Track(std::shared_ptr<const TextureSourceVK> texture) {
144  if (!IsValid()) {
145  return false;
146  }
147  tracked_objects_->Track(std::move(texture));
148  return true;
149 }
150 
151 bool CommandBufferVK::Track(const std::shared_ptr<const Texture>& texture) {
152  if (!IsValid()) {
153  return false;
154  }
155  if (!texture) {
156  return true;
157  }
158  return Track(TextureVK::Cast(*texture).GetTextureSource());
159 }
160 
162  const std::shared_ptr<const Texture>& texture) const {
163  if (!IsValid()) {
164  return false;
165  }
166  std::shared_ptr<const TextureSourceVK> source =
167  TextureVK::Cast(*texture).GetTextureSource();
168  return tracked_objects_->IsTracking(source);
169 }
170 
171 fml::StatusOr<vk::DescriptorSet> CommandBufferVK::AllocateDescriptorSets(
172  const vk::DescriptorSetLayout& layout,
173  const ContextVK& context) {
174  if (!IsValid()) {
175  return fml::Status(fml::StatusCode::kUnknown, "command encoder invalid");
176  }
177 
178  return tracked_objects_->GetDescriptorPool().AllocateDescriptorSets(layout,
179  context);
180 }
181 
182 void CommandBufferVK::PushDebugGroup(std::string_view label) const {
183  if (!HasValidationLayers()) {
184  return;
185  }
186  vk::DebugUtilsLabelEXT label_info;
187  label_info.pLabelName = label.data();
188  if (auto command_buffer = GetCommandBuffer()) {
189  command_buffer.beginDebugUtilsLabelEXT(label_info);
190  }
191 }
192 
194  if (!HasValidationLayers()) {
195  return;
196  }
197  if (auto command_buffer = GetCommandBuffer()) {
198  command_buffer.endDebugUtilsLabelEXT();
199  }
200 }
201 
202 void CommandBufferVK::InsertDebugMarker(std::string_view label) const {
203  if (!HasValidationLayers()) {
204  return;
205  }
206  vk::DebugUtilsLabelEXT label_info;
207  label_info.pLabelName = label.data();
208  if (auto command_buffer = GetCommandBuffer()) {
209  command_buffer.insertDebugUtilsLabelEXT(label_info);
210  }
211 }
212 
213 } // namespace impeller
gpu_tracer_vk.h
impeller::CommandBufferVK::InsertDebugMarker
void InsertDebugMarker(std::string_view label) const
Insert a new debug marker.
Definition: command_buffer_vk.cc:202
compute_pass_vk.h
impeller::CommandBufferVK::Track
bool Track(std::shared_ptr< SharedObjectVK > object)
Ensure that [object] is kept alive until this command buffer completes execution.
Definition: command_buffer_vk.cc:119
render_pass_vk.h
impeller::CommandBufferVK::IsTracking
bool IsTracking(const std::shared_ptr< const DeviceBuffer > &texture) const
Definition: command_buffer_vk.cc:135
impeller::TextureVK::GetTextureSource
std::shared_ptr< const TextureSourceVK > GetTextureSource() const
Definition: texture_vk.cc:154
impeller::CommandBufferVK::GetCommandBuffer
vk::CommandBuffer GetCommandBuffer() const
Retrieve the native command buffer from this object.
Definition: command_buffer_vk.cc:112
command_buffer_vk.h
impeller::CommandBuffer::context_
std::weak_ptr< const Context > context_
Definition: command_buffer.h:94
impeller::CommandBufferVK::~CommandBufferVK
~CommandBufferVK() override
impeller::CommandBufferVK::EndCommandBuffer
bool EndCommandBuffer() const
End recording of the current command buffer.
Definition: command_buffer_vk.cc:98
impeller::CommandBufferVK::AllocateDescriptorSets
fml::StatusOr< vk::DescriptorSet > AllocateDescriptorSets(const vk::DescriptorSetLayout &layout, const ContextVK &context)
Allocate a new descriptor set for the given [layout].
Definition: command_buffer_vk.cc:171
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:109
blit_pass_vk.h
texture_vk.h
impeller::ContextVK
Definition: context_vk.h:42
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
command_buffer.h
impeller::CommandBufferVK::PopDebugGroup
void PopDebugGroup() const
Pop the previous debug group.
Definition: command_buffer_vk.cc:193
std
Definition: comparable.h:95
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
render_target.h
context_vk.h
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:46
impeller
Definition: allocation.cc:12
impeller::CommandBufferVK::PushDebugGroup
void PushDebugGroup(std::string_view label) const
Push a debug group.
Definition: command_buffer_vk.cc:182