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  : CommandBuffer(std::move(context)),
27  device_holder_(std::move(device_holder)),
28  tracked_objects_(std::move(tracked_objects)) {}
29 
30 CommandBufferVK::~CommandBufferVK() = default;
31 
32 void CommandBufferVK::SetLabel(std::string_view label) const {
33 #ifdef IMPELLER_DEBUG
34  auto context = context_.lock();
35  if (!context) {
36  return;
37  }
38  ContextVK::Cast(*context).SetDebugName(GetCommandBuffer(), label);
39 #endif // IMPELLER_DEBUG
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::OnWaitUntilCompleted() {}
51 
52 void CommandBufferVK::OnWaitUntilScheduled() {}
53 
54 std::shared_ptr<RenderPass> CommandBufferVK::OnCreateRenderPass(
55  RenderTarget target) {
56  auto context = context_.lock();
57  if (!context) {
58  return nullptr;
59  }
60  auto pass =
61  std::shared_ptr<RenderPassVK>(new RenderPassVK(context, //
62  target, //
63  shared_from_this() //
64  ));
65  if (!pass->IsValid()) {
66  return nullptr;
67  }
68  return pass;
69 }
70 
71 std::shared_ptr<BlitPass> CommandBufferVK::OnCreateBlitPass() {
72  if (!IsValid()) {
73  return nullptr;
74  }
75  auto context = context_.lock();
76  if (!context) {
77  return nullptr;
78  }
79  auto pass = std::shared_ptr<BlitPassVK>(new BlitPassVK(
80  shared_from_this(), ContextVK::Cast(*context).GetWorkarounds()));
81  if (!pass->IsValid()) {
82  return nullptr;
83  }
84  return pass;
85 }
86 
87 std::shared_ptr<ComputePass> CommandBufferVK::OnCreateComputePass() {
88  if (!IsValid()) {
89  return nullptr;
90  }
91  auto context = context_.lock();
92  if (!context) {
93  return nullptr;
94  }
95  auto pass =
96  std::shared_ptr<ComputePassVK>(new ComputePassVK(context, //
97  shared_from_this() //
98  ));
99  if (!pass->IsValid()) {
100  return nullptr;
101  }
102  return pass;
103 }
104 
105 bool CommandBufferVK::EndCommandBuffer() const {
106  InsertDebugMarker("QueueSubmit");
107 
108  auto command_buffer = GetCommandBuffer();
109  tracked_objects_->GetGPUProbe().RecordCmdBufferEnd(command_buffer);
110 
111  auto status = command_buffer.end();
112  if (status != vk::Result::eSuccess) {
113  VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(status);
114  return false;
115  }
116  return true;
117 }
118 
119 vk::CommandBuffer CommandBufferVK::GetCommandBuffer() const {
120  if (tracked_objects_) {
121  return tracked_objects_->GetCommandBuffer();
122  }
123  return {};
124 }
125 
126 bool CommandBufferVK::Track(const std::shared_ptr<SharedObjectVK>& object) {
127  if (!IsValid()) {
128  return false;
129  }
130  tracked_objects_->Track(object);
131  return true;
132 }
133 
134 bool CommandBufferVK::Track(const std::shared_ptr<const DeviceBuffer>& buffer) {
135  if (!IsValid()) {
136  return false;
137  }
138  tracked_objects_->Track(buffer);
139  return true;
140 }
141 
142 bool CommandBufferVK::Track(
143  const std::shared_ptr<const TextureSourceVK>& texture) {
144  if (!IsValid()) {
145  return false;
146  }
147  tracked_objects_->Track(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 
161 fml::StatusOr<vk::DescriptorSet> CommandBufferVK::AllocateDescriptorSets(
162  const vk::DescriptorSetLayout& layout,
163  PipelineKey pipeline_key,
164  const ContextVK& context) {
165  if (!IsValid()) {
166  return fml::Status(fml::StatusCode::kUnknown, "command encoder invalid");
167  }
168 
169  return tracked_objects_->GetDescriptorPool().AllocateDescriptorSets(
170  layout, pipeline_key, context);
171 }
172 
173 void CommandBufferVK::PushDebugGroup(std::string_view label) const {
174  if (!HasValidationLayers()) {
175  return;
176  }
177  vk::DebugUtilsLabelEXT label_info;
178  label_info.pLabelName = label.data();
179  if (auto command_buffer = GetCommandBuffer()) {
180  command_buffer.beginDebugUtilsLabelEXT(label_info);
181  }
182 }
183 
184 void CommandBufferVK::PopDebugGroup() const {
185  if (!HasValidationLayers()) {
186  return;
187  }
188  if (auto command_buffer = GetCommandBuffer()) {
189  command_buffer.endDebugUtilsLabelEXT();
190  }
191 }
192 
193 void CommandBufferVK::InsertDebugMarker(std::string_view label) const {
194  if (!HasValidationLayers()) {
195  return;
196  }
197  vk::DebugUtilsLabelEXT label_info;
198  label_info.pLabelName = label.data();
199  if (auto command_buffer = GetCommandBuffer()) {
200  command_buffer.insertDebugUtilsLabelEXT(label_info);
201  }
202 }
203 
204 DescriptorPoolVK& CommandBufferVK::GetDescriptorPool() const {
205  return tracked_objects_->GetDescriptorPool();
206 }
207 
208 } // namespace impeller
A per-frame descriptor pool. Descriptors from this pool don't need to be freed individually....
int64_t PipelineKey
Definition: pipeline.h:21
bool HasValidationLayers()
Definition: context_vk.cc:53
Definition: comparable.h:95
#define VALIDATION_LOG
Definition: validation.h:91