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 #include "RV_Instance.h"
18 
19 #include <VE/VE_VK.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_Instance;
31 class RV_VKBuffer;
32 class RV_VKCommandPool;
33 class RV_VKImage;
35 class RV_VKQueue;
36 
38 {
39 public:
40  static RV_VKCommandBuffer* allocate(
41  RV_Instance* inst,
42  RV_VKCommandPool* cmd_pool);
43 
44  VkCommandBuffer getVkCmdBuf()
45  { UT_ASSERT_MSG(myIsRecording,
46  "Using VKCommandBuffer that is not recording: "
47  "check that you haven't flushed the buffer");
48  return myVkCmdBuf;
49  }
50  RV_VKCommandPool *getCmdPool()
51  { return myCmdPool; }
52 
53  void beginRecording();
54  void endRecording();
55 
56  bool waitForFinish();
57 
58  void addWaitSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
59  void addSignalSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
60 
61  // TODO: replace with lifecycle enum?
62  bool isRecording() const;
63  bool isExecuting();
64 
65  // Callbacks to be run when the CommandBuffer is finished running
66  // Won't be run immediately, immediate synchronization should use
67  // `waitForFinish()` or wait on a timeline semaphore
69 
71  {
72  // MUST be done executing
73  UT_ASSERT(!isExecuting());
74  if(isExecuting()) {
75  return;
76  }
77 
78  // run all callbacks
79  for (auto& callback : myCompletionCallbacks)
80  {
81  callback(inst);
82  }
83 
84  // clear list
85  myCompletionCallbacks.clear();
86  }
87 
89  {
90  // run all callbacks
91  for (auto& callback : mySubmissionCallbacks)
92  {
93  callback(inst);
94  }
95 
96  // clear list
97  mySubmissionCallbacks.clear();
98  }
99 
100  void addCompletionCallback(const Callback &callback)
101  {
102  myCompletionCallbacks.append(callback);
103  }
104 
105  void addSubmissionCallback(const Callback &callback)
106  {
107  mySubmissionCallbacks.append(callback);
108  }
109 
110  // Reset bound state to default, to force re-binding
111  // and setting dynamic state
113  {
114  myPipeState = RV_VKPipelineStateInfo{};
115  }
116 
117 
118  // Functions used for logging and validation
119 
120  // Add a log to the CB, will be written out on errors
122  { myLogEntries.append(log); }
123  // record VkImage as being used by this CB
124  void logImageUsage(const RV_VKImage* img);
125  // record VkBuffer as being used by this CB
126  void logBufferUsage(const RV_VKBuffer* buf);
127  // record VkAccelerationStructureKHR as being used by this CB
128  void logAccelStructUsage(const RV_VKAccelerationStructure* accel_struct);
129  // record VkDescriptorSet as being used by this CB
130  void logSetUsage(const RV_ShaderVariableSet* set);
131 
133 
134  // disable copy
135  RV_VKCommandBuffer(const RV_VKCommandBuffer&) = delete;
137 
138 public:
139  // Rendering State
141  // TODO: Current Pipeline
142  // TODO: Vertex Buffer
143  // TODO: Index Buffer
144  // TODO: Transform Feedback Buffer
145 protected:
146  // main resources
147  VkCommandBuffer myVkCmdBuf;
148  class RV_VKCommandPool* myCmdPool;
149  RV_Instance* myInst = nullptr;
150 
151  // Creation info
153 
154  // Current status
155  bool myIsRecording = false;
156 
157  // Synchronization Primitives
159 
162 
165 
166  // Keep a list of callbacks
169 
170  // Extra info used for validation, logging, debugging
171  // log to be printed on error
173  {
174  // Id to validate for resource
176  // Label for debugging when resource is invalid
178  // Indication of when resource was bound
180  };
181  bool validateUsedResources(RV_Instance* inst, FILE* out = nullptr) const;
186  fpreal mySubmitTime = 0.f;
188 
190  RV_Instance* inst,
191  RV_VKCommandPool* cmd_pool,
192  VkCommandBuffer cmd_buf,
193  const VkCommandBufferAllocateInfo& info,
194  VkFence fence)
195  : myInst(inst)
196  , myCmdPool(cmd_pool)
197  , myVkCmdBuf(cmd_buf)
198  , myLevel(info.level)
199  , myCompleteFence(fence)
200  {
201  }
202 
203  friend class RV_Instance;
204  friend class RV_VKCommandPool;
205  friend class RV_VKQueue;
206 };
207 
208 // Helper class for callback that owns UniquePtr
209 // and destroys it when called
210 
211 // default impl of templated var
212 template<typename, typename = void>
213 constexpr bool has_destroy_func = false;
214 
215 // template specialization, to give true value if
216 // T has member func `void destroy(RV_Instance*)`
217 template <typename T>
218 constexpr bool has_destroy_func<T,
219  std::void_t<decltype(std::declval<T>().destroy(
220  std::declval<RV_Instance*>()))>>
221  = true;
222 
223 template<class T>
225 {
226 public:
228 
230  {
231  if constexpr (has_destroy_func<T>)
232  {
233  // destroy object before reseting ptr, if fcn exists.
234  // Ensures object is destroyed, and not just queued
235  // for deletion later again
236  if (inst)
237  myObj->destroy(inst);
238  }
239  myObj.reset();
240  }
241 
243  : myObj(std::move(obj))
244  {}
245 };
246 
247 template<class T>
250 {
251  return RV_VKCommandBuffer::Callback{RV_DestroyPtrTask<T>(std::move(obj))};
252 }
253 
254 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
UT_Array< ResourceInfo > myAccelStructIDs
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
UT_Array< ResourceInfo > myBufferIDs
UT_Array< ResourceInfo > myImageIDs
VkCommandBuffer getVkCmdBuf()
void appendLog(const UT_StringHolder &log)
int64 exint
Definition: SYS_Types.h:125
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:168
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
constexpr bool has_destroy_func
UT_Array< VkPipelineStageFlags > myWaitStages
constexpr auto set(type rhs) -> int
Definition: core.h:610
#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:48
GLint void * img
Definition: glcorearb.h:556
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
fpreal64 fpreal
Definition: SYS_Types.h:283
LeafData & operator=(const LeafData &)=delete
RV_VKCommandPool * getCmdPool()
UT_Array< VkSemaphore > myWaitSems
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
UT_Array< Callback > myCompletionCallbacks
void operator()(RV_Instance *inst)
A vulkan buffer object.
Definition: RV_VKBuffer.h:75
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7905
UT_Array< ResourceInfo > mySetIDs
UT_Array< UT_StringHolder > myLogEntries
VkCommandBufferLevel
Definition: vulkan_core.h:2098
UT_Function< void(RV_Instance *)> Callback
RV_DestroyPtrTask(UT_UniquePtr< T > obj)
class RV_VKCommandPool * myCmdPool