• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SRC_VULKAN_COMMAND_BUFFER_H_
16 #define SRC_VULKAN_COMMAND_BUFFER_H_
17 
18 #include "amber/result.h"
19 #include "amber/vulkan_header.h"
20 
21 namespace amber {
22 namespace vulkan {
23 
24 /// Command buffer states.
25 enum class CommandBufferState : uint8_t {
26   kInitial = 0,
27   kRecording,
28   kExecutable,
29   kPending,
30   kInvalid,
31 };
32 
33 class CommandBufferGuard;
34 class CommandPool;
35 class Device;
36 
37 /// Wrapper around a Vulkan command buffer. This is designed to not be used
38 /// directly, but should always be used through the `CommandBufferGuard` class.
39 class CommandBuffer {
40  public:
41   CommandBuffer(Device* device, CommandPool* pool);
42   ~CommandBuffer();
43 
44   Result Initialize();
GetVkCommandBuffer()45   VkCommandBuffer GetVkCommandBuffer() const { return command_; }
46 
47  private:
48   friend CommandBufferGuard;
49 
50   Result BeginRecording();
51   Result SubmitAndReset(uint32_t timeout_ms);
52   void Reset();
53 
54   bool guarded_ = false;
55 
56   Device* device_ = nullptr;
57   CommandPool* pool_ = nullptr;
58   VkCommandBuffer command_ = VK_NULL_HANDLE;
59   VkFence fence_ = VK_NULL_HANDLE;
60 };
61 
62 /// Wrapper around a `CommandBuffer`.
63 ///
64 /// Usage follows the pattern:
65 /// ```
66 /// CommandBufferGuard guard(cb);
67 /// if (!guard.IsRecording())
68 ///   return guard.GetResult();
69 /// ...
70 /// Result r = guard.Submit(timeout);
71 /// if (!r.IsSuccess())
72 ///   return r;
73 /// ```
74 class CommandBufferGuard {
75  public:
76   /// Creates a command buffer guard and sets the command buffer to recording.
77   explicit CommandBufferGuard(CommandBuffer* buffer);
78   ~CommandBufferGuard();
79 
80   /// Returns true if the command buffer was successfully set to recording.
IsRecording()81   bool IsRecording() const { return result_.IsSuccess(); }
82   /// Returns the result object if the command buffer recording failed.
GetResult()83   Result GetResult() { return result_; }
84 
85   /// Submits and resets the internal command buffer.
86   Result Submit(uint32_t timeout_ms);
87 
88  private:
89   Result result_;
90   CommandBuffer* buffer_;
91 };
92 
93 }  // namespace vulkan
94 }  // namespace amber
95 
96 #endif  // SRC_VULKAN_COMMAND_BUFFER_H_
97