HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_VKCommandBuffer.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RV_VKCommandBuffer.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * Class for creating Vulkan Command Buffer
10  * and other resources used with it
11  */
12 
13 #ifndef RV_VKCommandBuffer_h
14 #define RV_VKCommandBuffer_h
15 
16 #include "RV_API.h"
17 
18 #include <VE/VE_VK.h>
19 #include "RV_Instance.h"
20 #include "RV_VKPipeline.h"
21 
22 #include <UT/UT_Array.h>
23 #include <UT/UT_Function.h>
24 #include <UT/UT_SharedPtr.h>
25 
26 #include <functional>
27 #include <utility>
28 
29 class RV_VKCommandPool;
30 class RV_VKQueue;
31 
33 {
34 public:
35  static RV_VKCommandBuffer* allocate(
36  RV_Instance* inst,
37  RV_VKCommandPool* cmd_pool);
38 
39  VkCommandBuffer getVkCmdBuf()
40  { UT_ASSERT_MSG(myIsRecording,
41  "Using VKCommandBuffer that is not recording: "
42  "check that you haven't flushed the buffer");
43  return myVkCmdBuf;
44  }
45  RV_VKCommandPool *getCmdPool()
46  { return myCmdPool; }
47 
48  void beginRecording();
49  void endRecording();
50 
51  void addWaitSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
52  void addSignalSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
53 
54  bool waitForFinish();
55 
56  // TODO: replace with lifecycle enum?
57  bool isRecording() const;
58  bool isExecuting();
59 
60  // Callbacks to be run when the CommandBuffer is finished running
61  // Won't be run immediately, immediate synchronization should use
62  // `waitForFinish()` or wait on a timeline semaphore
64 
66  {
67  // MUST be done executing
68  UT_ASSERT(!isExecuting());
69  if(isExecuting()) {
70  return;
71  }
72 
73  // run all callbacks
74  for (auto& callback : myCompletionCallbacks)
75  {
76  callback(inst);
77  }
78 
79  // clear list
80  myCompletionCallbacks.clear();
81  }
82 
84  {
85  // run all callbacks
86  for (auto& callback : mySubmissionCallbacks)
87  {
88  callback(inst);
89  }
90 
91  // clear list
92  mySubmissionCallbacks.clear();
93  }
94 
95  void addCompletionCallback(const Callback &callback)
96  {
97  myCompletionCallbacks.append(callback);
98  }
99 
100  void addSubmissionCallback(const Callback &callback)
101  {
102  mySubmissionCallbacks.append(callback);
103  }
104 
105  // Reset bound state to default, to force re-binding
106  // and setting dynamic state
108  {
109  myPipeState = RV_VKPipelineStateInfo{};
110  }
111 
113 
114  // disable copy
115  RV_VKCommandBuffer(const RV_VKCommandBuffer&) = delete;
117 
118 public:
119  // Rendering State
121  // TODO: Current Pipeline
122  // TODO: Vertex Buffer
123  // TODO: Index Buffer
124  // TODO: Transform Feedback Buffer
125 protected:
126  // main resources
127  VkCommandBuffer myVkCmdBuf;
128  class RV_VKCommandPool* myCmdPool;
129  RV_Instance* myInst = nullptr;
130 
131  // Creation info
133 
134  // Current status
135  bool myIsRecording = false;
136 
137  // Synchronization Primitives
139 
142 
145 
146  // Keep a list of callbacks
149 
151  RV_Instance* inst,
152  RV_VKCommandPool* cmd_pool,
153  VkCommandBuffer cmd_buf,
154  const VkCommandBufferAllocateInfo& info,
155  VkFence fence)
156  : myInst(inst)
157  , myCmdPool(cmd_pool)
158  , myVkCmdBuf(cmd_buf)
159  , myLevel(info.level)
160  , myCompleteFence(fence)
161  {
162  }
163 
164  friend class RV_Instance;
165  friend class RV_VKCommandPool;
166  friend class RV_VKQueue;
167 };
168 
169 // Helper class for callback that owns UniquePtr
170 // and destroys it when called
171 template<class T>
173 {
174 public:
176 
178  {
179  myObj.reset();
180  }
181 
183  : myObj(std::move(obj))
184  {}
185 };
186 
187 template<class T>
190 {
191  return RV_VKCommandBuffer::Callback{RV_DestroyPtrTask<T>(std::move(obj))};
192 }
193 
194 #endif
VkCommandBuffer getVkCmdBuf()
GLint level
Definition: glcorearb.h:108
void handleSubmissionCallbacks(RV_Instance *inst)
RV_VKCommandBuffer::Callback RVmakeDestroyPtrTask(UT_UniquePtr< T > obj)
VkFlags VkPipelineStageFlags
Definition: vulkan_core.h:2401
RV_VKCommandBuffer(RV_Instance *inst, RV_VKCommandPool *cmd_pool, VkCommandBuffer cmd_buf, const VkCommandBufferAllocateInfo &info, VkFence fence)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:159
void operator()(RV_Instance *)
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
UT_Array< VkPipelineStageFlags > myWaitStages
#define RV_API
Definition: RV_API.h:10
UT_Array< VkSemaphore > mySignalSems
std::function< T > UT_Function
Definition: UT_Function.h:37
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:38
void addCompletionCallback(const Callback &callback)
void handleCompletionCallbacks(RV_Instance *inst)
RV_VKPipelineStateInfo myPipeState
UT_Array< VkPipelineStageFlags > mySignalStages
void addSubmissionCallback(const Callback &callback)
UT_SharedPtr< T > myObj
UT_Array< Callback > mySubmissionCallbacks
VkCommandBuffer myVkCmdBuf
LeafData & operator=(const LeafData &)=delete
RV_VKCommandPool * getCmdPool()
UT_Array< VkSemaphore > myWaitSems
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
UT_Array< Callback > myCompletionCallbacks
VkCommandBufferLevel
Definition: vulkan_core.h:2098
UT_Function< void(RV_Instance *)> Callback
RV_DestroyPtrTask(UT_UniquePtr< T > obj)
class RV_VKCommandPool * myCmdPool