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_VKPipeline.h"
20 
21 #include <UT/UT_Array.h>
22 #include <UT/UT_Function.h>
23 #include <UT/UT_SharedPtr.h>
24 
25 #include <functional>
26 #include <utility>
27 
28 class RV_Instance;
30 class RV_VKBuffer;
31 class RV_VKCommandPool;
32 class RV_VKImage;
34 class RV_VKQueue;
35 
37 {
38 public:
39  static RV_VKCommandBuffer* allocate(
40  RV_Instance* inst,
41  RV_VKCommandPool* cmd_pool);
42 
43  VkCommandBuffer getVkCmdBuf()
44  { UT_ASSERT_MSG(myIsRecording,
45  "Using VKCommandBuffer that is not recording: "
46  "check that you haven't flushed the buffer");
47  return myVkCmdBuf;
48  }
49  RV_VKCommandPool *getCmdPool()
50  { return myCmdPool; }
51 
52  void beginRecording();
53  void endRecording();
54 
55  bool waitForFinish();
56 
57  void addWaitSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
58  void addSignalSemaphore(VkSemaphore sem, VkPipelineStageFlags stage);
59 
60  // TODO: replace with lifecycle enum?
61  bool isRecording() const;
62  bool isExecuting();
63 
64  // Callbacks to be run when the CommandBuffer is finished running
65  // Won't be run immediately, immediate synchronization should use
66  // `waitForFinish()` or wait on a timeline semaphore
68 
70  {
71  // MUST be done executing
72  UT_ASSERT(!isExecuting());
73  if(isExecuting()) {
74  return;
75  }
76 
77  // run all callbacks
78  for (auto& callback : myCompletionCallbacks)
79  {
80  callback(inst);
81  }
82 
83  // clear list
84  myCompletionCallbacks.clear();
85  }
86 
88  {
89  // run all callbacks
90  for (auto& callback : mySubmissionCallbacks)
91  {
92  callback(inst);
93  }
94 
95  // clear list
96  mySubmissionCallbacks.clear();
97  }
98 
99  void addCompletionCallback(const Callback &callback)
100  {
101  myCompletionCallbacks.append(callback);
102  }
103 
104  void addSubmissionCallback(const Callback &callback)
105  {
106  mySubmissionCallbacks.append(callback);
107  }
108 
109  // Reset bound state to default, to force re-binding
110  // and setting dynamic state
112  {
113  myPipeState = RV_VKPipelineStateInfo{};
114  }
115 
116 
117  // Functions used for logging and validation
118 
119  // Add a log to the CB, will be written out on errors
121  { myLogEntries.append(log); }
122  // record VkImage as being used by this CB
123  void logImageUsage(const RV_VKImage* img);
124  // record VkBuffer as being used by this CB
125  void logBufferUsage(const RV_VKBuffer* buf);
126  // record VkAccelerationStructureKHR as being used by this CB
127  void logAccelStructUsage(const RV_VKAccelerationStructure* accel_struct);
128  // record VkDescriptorSet as being used by this CB
129  void logSetUsage(const RV_ShaderVariableSet* set);
130 
132 
133  // disable copy
134  RV_VKCommandBuffer(const RV_VKCommandBuffer&) = delete;
136 
137 public:
138  // Rendering State
140  // TODO: Current Pipeline
141  // TODO: Vertex Buffer
142  // TODO: Index Buffer
143  // TODO: Transform Feedback Buffer
144 protected:
145  // main resources
146  VkCommandBuffer myVkCmdBuf;
147  class RV_VKCommandPool* myCmdPool;
148  RV_Instance* myInst = nullptr;
149 
150  // Creation info
152 
153  // Current status
154  bool myIsRecording = false;
155 
156  // Synchronization Primitives
158 
161 
164 
165  // Keep a list of callbacks
168 
169  // Extra info used for validation, logging, debugging
170  // log to be printed on error
172  {
173  // Id to validate for resource
175  // Label for debugging when resource is invalid
177  // Indication of when resource was bound
179  };
180  bool validateUsedResources(RV_Instance* inst, FILE* out = nullptr) const;
185  fpreal mySubmitTime = 0.f;
187 
189  RV_Instance* inst,
190  RV_VKCommandPool* cmd_pool,
191  VkCommandBuffer cmd_buf,
192  const VkCommandBufferAllocateInfo& info,
193  VkFence fence)
194  : myInst(inst)
195  , myCmdPool(cmd_pool)
196  , myVkCmdBuf(cmd_buf)
197  , myLevel(info.level)
198  , myCompleteFence(fence)
199  {
200  }
201 
202  friend class RV_Instance;
203  friend class RV_VKCommandPool;
204  friend class RV_VKQueue;
205 };
206 
207 // Helper class for callback that owns UniquePtr
208 // and destroys it when called
209 template<class T>
211 {
212 public:
214 
216  {
217  myObj.reset();
218  }
219 
221  : myObj(std::move(obj))
222  {}
223 };
224 
225 template<class T>
228 {
229  return RV_VKCommandBuffer::Callback{RV_DestroyPtrTask<T>(std::move(obj))};
230 }
231 
232 #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: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
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:44
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:278
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
A vulkan buffer object.
Definition: RV_VKBuffer.h:81
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