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 "RV_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 public:
34  static RV_VKCommandBuffer* allocate(
35  RV_Instance* inst,
36  RV_VKCommandPool* cmd_pool);
37 
38  VkCommandBuffer getVkCmdBuf()
39  { UT_ASSERT_MSG(myIsRecording,
40  "Using VKCommandBuffer that is not recording: "
41  "check that you haven't flushed the buffer");
42  return myVkCmdBuf;
43  }
44  RV_VKCommandPool *getCmdPool()
45  { return myCmdPool; }
46 
47  void beginRecording();
48  void endRecording();
49 
50  void addWaitSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
51  void addSignalSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
52 
53  bool waitForFinish();
54 
55  // TODO: replace with lifecycle enum?
56  bool isRecording() const;
57  bool isExecuting();
58 
59  // Callbacks to be run when the CommandBuffer is finished running
60  // Won't be run immediately, immediate synchronization should use
61  // `waitForFinish()` or wait on a timeline semaphore
63 
65  {
66  // MUST be done executing
67  UT_ASSERT(!isExecuting());
68  if(isExecuting()) {
69  return;
70  }
71 
72  // run all callbacks
73  for (auto& callback : myCompletionCallbacks)
74  {
75  callback(inst);
76  }
77 
78  // clear list
79  myCompletionCallbacks.clear();
80  }
81 
83  {
84  // run all callbacks
85  for (auto& callback : mySubmissionCallbacks)
86  {
87  callback(inst);
88  }
89 
90  // clear list
91  mySubmissionCallbacks.clear();
92  }
93 
94  void addCompletionCallback(const Callback &callback)
95  {
96  myCompletionCallbacks.append(callback);
97  }
98 
99  void addSubmissionCallback(const Callback &callback)
100  {
101  mySubmissionCallbacks.append(callback);
102  }
103 
105 
106  // disable copy
107  RV_VKCommandBuffer(const RV_VKCommandBuffer&) = delete;
108  RV_VKCommandBuffer& operator=(const RV_VKCommandBuffer&) = delete;
109 
110 public:
111  // Rendering State
113  // TODO: Current Pipeline
114  // TODO: Vertex Buffer
115  // TODO: Index Buffer
116  // TODO: Transform Feedback Buffer
117 protected:
118  // main resources
119  VkCommandBuffer myVkCmdBuf;
120  class RV_VKCommandPool* myCmdPool;
121  RV_Instance* myInst = nullptr;
122 
123  // Creation info
125 
126  // Current status
127  bool myIsRecording = false;
128 
129  // Synchronization Primitives
131 
134 
137 
138  // Keep a list of callbacks
141 
143  RV_Instance* inst,
144  RV_VKCommandPool* cmd_pool,
145  VkCommandBuffer cmd_buf,
146  const VkCommandBufferAllocateInfo& info,
147  VkFence fence)
148  : myInst(inst)
149  , myCmdPool(cmd_pool)
150  , myVkCmdBuf(cmd_buf)
151  , myLevel(info.level)
152  , myCompleteFence(fence)
153  {
154  }
155 
156  friend class RV_Instance;
157  friend class RV_VKCommandPool;
158  friend class RV_VKQueue;
159 };
160 
161 // Helper class for callback that owns UniquePtr
162 // and destroys it when called
163 template<class T>
165 {
166 public:
168 
170  {
171  myObj.reset();
172  }
173 
175  : myObj(std::move(obj))
176  {}
177 };
178 
179 template<class T>
182 {
183  return RV_VKCommandBuffer::Callback{RV_DestroyPtrTask<T>(std::move(obj))};
184 }
185 
186 #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:36
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
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