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 53 bool guarded_ = false; 54 55 Device* device_ = nullptr; 56 CommandPool* pool_ = nullptr; 57 VkCommandBuffer command_ = VK_NULL_HANDLE; 58 VkFence fence_ = VK_NULL_HANDLE; 59 }; 60 61 /// Wrapper around a `CommandBuffer`. 62 /// 63 /// Usage follows the pattern: 64 /// ``` 65 /// CommandBufferGuard guard(cb); 66 /// if (!guard.IsRecording()) 67 /// return guard.GetResult(); 68 /// ... 69 /// Result r = guard.Submit(timeout); 70 /// if (!r.IsSuccess()) 71 /// return r; 72 /// ``` 73 class CommandBufferGuard { 74 public: 75 /// Creates a command buffer guard and sets the command buffer to recording. 76 explicit CommandBufferGuard(CommandBuffer* buffer); 77 ~CommandBufferGuard(); 78 79 /// Returns true if the command buffer was successfully set to recording. IsRecording()80 bool IsRecording() const { return result_.IsSuccess(); } 81 /// Returns the result object if the command buffer recording failed. GetResult()82 Result GetResult() { return result_; } 83 84 /// Submits and resets the internal command buffer. 85 Result Submit(uint32_t timeout_ms); 86 87 private: 88 Result result_; 89 CommandBuffer* buffer_; 90 }; 91 92 } // namespace vulkan 93 } // namespace amber 94 95 #endif // SRC_VULKAN_COMMAND_BUFFER_H_ 96