HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_VKFramebuffer.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_VKFramebuffer.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * Class for representing list of attachments as a framebuffer in Vulkan
10  */
11 #ifndef RV_VKFramebuffer_h
12 #define RV_VKFramebuffer_h
13 
14 #include "RV_API.h"
15 
16 #include <UT/UT_Array.h>
17 #include <UT/UT_Rect.h>
18 #include <UT/UT_NonCopyable.h>
19 #include <UT/UT_UniquePtr.h>
20 
21 #include "RV_VK.h"
22 #include "RV_Instance.h"
23 #include "RV_Type.h"
24 #include "RV_VKEnum.h"
25 
26 class RV_Instance;
27 class RV_Render;
28 class RV_Framebuffer;
29 class RV_VKCommandBuffer;
30 class RV_VKFramebuffer;
31 class RV_VKImage;
32 class RV_VKImageView;
33 class RV_VKRenderPass;
34 
35 class rv_VKAttachment;
37 
38 // info about the format of a renderpass i.e. the
39 // info required to know if two render passes are
40 // compatible:
41 // - attachment formats and samples
42 // - creation flags
43 // - subpass descriptions
44 // - (layouts do not matter)
46 {
47  bool myIsDynamicRendering = false;
49 
50  // NOTE: using same sample count for all attachments
51  int mySamples = 1;
55 
56  // TODO: multiview rendering
57  // uint32 myViewMask = 0;
58  // NOTE: not bothering with subpasses at the moment
59  // int mySubPassCount = 1;
60 
61  bool operator==(const RV_RenderPassFormatInfo& other) const
62  {
65  && mySamples == other.mySamples
66  && myColorFormats == other.myColorFormats
69  }
70 
71  bool operator!=(const RV_RenderPassFormatInfo& other) const
72  {
73  return !(*this == other);
74  }
75 };
76 
77 //#define USE_FRAMEBUFFER_CREATE_INFO
78 //
79 // VKFramebuffer doesn't represent an actual
80 // Vulkan object, and doesn't necessarily
81 // need to use a createInfo class --
82 // which allows detaching/attaching after
83 // creation
84 
85 #if !defined(USE_FRAMEBUFFER_CREATE_INFO)
86 
88 {
89 public:
90  static UT_UniquePtr<RV_Framebuffer> create(
91  RV_Instance* inst,
92  int w,
93  int h,
94  int samples = 1);
95 
96  RV_Framebuffer(int w, int h, int samples = 1);
97  virtual ~RV_Framebuffer();
98 
99  virtual bool beginRendering(
100  RV_Instance *inst,
101  RV_VKCommandBuffer *cb,
102  RV_ImageOp img_op = RV_IMAGE_LOAD,
104 
105  virtual void endRendering(
106  RV_Instance *inst,
107  RV_VKCommandBuffer *cb) = 0;
108 
109  virtual bool fillPipelineInfo(RV_RenderPassFormatInfo& info) = 0;
110 
111  UT_DimRect getRenderRegion() const;
112 
113  // Update Framebuffer format state
114  void setSize(int w, int h)
115  {
116  UT_ASSERT(w > 0 && h > 0);
117  myWidth = w; myHeight = h;
118  }
119  int getWidth() { return myWidth;}
120  int getHeight() { return myHeight;}
121 
122  void setSampleCount(int samples);
123  int getSampleCount() { return mySamples; }
124 
125  void setClearColor(UT_Vector4F c) { myClearColor = c; }
126  UT_Vector4F getClearColor() const { return myClearColor; }
127 
128  void setClearInt(UT_Vector4i i) { myClearInt = i; }
129  UT_Vector4i getClearInt() const { return myClearInt; }
130 
131  void setClearDepth(fpreal32 d) { myClearDepth = d; }
132  fpreal32 getClearDepth() const { return myClearDepth; }
133 
134  void getColorImages(UT_Array<const RV_VKImage*> &out_list);
135  RV_VKImage* getDepthImage();
136 
137  // Update Target info
138  bool attachImage(RV_Render* r,
139  RV_VKImage* img,
140  RV_AttachmentType buffer_type,
141  int buffer_target = 0,
142  int level = 0,
143  int layer = 0);
144 
145  RV_VKImage* createImage(RV_Render* r,
146  RV_GPUType type, int vec_size,
147  RV_AttachmentType attach_type,
148  int buffer_target = 0);
149 
150  void detachColorImage(RV_Render *r, int buffer_target = 0);
151  void detachDepthImage(RV_Render *r);
152 
153 protected:
154 
155  bool fillPipelineAttachments(RV_RenderPassFormatInfo& out_info);
157  { return myColorAttachments; }
159  { return myDepthAttachment; }
160 
161  virtual void attachmentsChanged();
162 
163  // typed to match VK structs
164  uint32_t myWidth = 0;
165  uint32_t myHeight = 0;
166 
167  uint32_t mySamples = 1;
168 
169  UT_Vector4F myClearColor = UT_Vector4F(0.f, 0.f, 0.f, 0.f);
170  UT_Vector4i myClearInt = UT_Vector4i(0, 0, 0, 0);
171  fpreal32 myClearDepth = 1.f;
172 
173  int myDrawMask = 0;
174 
177 };
178 
180 {
181 public:
182  RV_DynamicFramebuffer(int w, int h, int samples = 1);
183  ~RV_DynamicFramebuffer() override;
184 
186 
187  bool fillPipelineInfo(RV_RenderPassFormatInfo& info) override;
188 
189  // Act on framebuffer
190  bool beginRendering(
191  RV_Instance *inst,
192  RV_VKCommandBuffer *cb,
193  RV_ImageOp img_op = RV_IMAGE_LOAD,
195 
196  void endRendering(
197  RV_Instance *inst,
198  RV_VKCommandBuffer *cb) override;
199 };
200 
202 {
203 public:
204  RV_RenderPassFramebuffer(int w, int h, int samples = 1);
205  ~RV_RenderPassFramebuffer() override;
206 
207  bool fillPipelineInfo(RV_RenderPassFormatInfo& info) override;
208 
209  bool beginRendering(
210  RV_Instance *inst,
211  RV_VKCommandBuffer *cb,
212  RV_ImageOp img_op = RV_IMAGE_LOAD,
214 
215  void endRendering(
216  RV_Instance *inst,
217  RV_VKCommandBuffer *cb) override;
218 private:
219  void attachmentsChanged() override;
220 
221  bool myIsDirty = false;
222 
223  RV_ImageOp myLastLoadOp = RV_IMAGE_LOAD;
224  RV_RenderPassFormatInfo myLastFormat;
225 
226  UT_UniquePtr<RV_VKRenderPass> myRenderPass;
227  UT_UniquePtr<RV_VKFramebuffer> myFramebuffer;
228 };
229 
231 {
232 public:
233  VkFramebuffer getVkFramebuffer() const { return myVkFramebuffer; }
234 
235  RV_VKFramebuffer(RV_Instance* inst, VkFramebuffer vk_handle)
236  : myInst(inst), myVkFramebuffer(vk_handle)
237  {}
238 
240  : myInst(other.myInst), myVkFramebuffer(other.myVkFramebuffer)
241  {
242  other.myInst = nullptr;
243  other.myVkFramebuffer = 0;
244  }
245 
246  // Disable move operator (since it would leave `rhs`) in null state
247  RV_VKFramebuffer& operator=(RV_VKFramebuffer&& rhs) noexcept = delete;
248 
249  void destroy()
250  {
251  UT_ASSERT(myInst || !myVkFramebuffer);
252  if (myInst && myInst->getDevice())
253  {
255  myInst->getDevice(), myVkFramebuffer, nullptr);
256  }
257  }
258 
259  virtual ~RV_VKFramebuffer() { destroy(); }
260 private:
261  RV_Instance* myInst = nullptr;
262  VkFramebuffer myVkFramebuffer = 0;
263 };
264 
266 {
267 public:
268  VkRenderPass getVkRenderPass() const { return myVkRenderPass; }
269 
270  RV_VKRenderPass(RV_Instance* inst, VkRenderPass vk_handle)
271  : myInst(inst), myVkRenderPass(vk_handle)
272  {}
273 
275  : myInst(other.myInst), myVkRenderPass(other.myVkRenderPass)
276  {
277  other.myInst = nullptr;
278  other.myVkRenderPass = 0;
279  }
280 
281  // Disable move operator (since it would leave `rhs`) in null state
282  RV_VKRenderPass& operator=(RV_VKRenderPass&& rhs) noexcept = delete;
283 
284  void destroy()
285  {
286  UT_ASSERT(myInst || !myVkRenderPass);
287  if (myInst && myInst->getDevice())
288  {
290  myInst->getDevice(), myVkRenderPass, nullptr);
291  }
292  }
293 
294  virtual ~RV_VKRenderPass() { destroy(); }
295 private:
296  RV_Instance* myInst = nullptr;
297  VkRenderPass myVkRenderPass = 0;
298 };
299 
300 #else
301 struct rv_AttachmentInfo
302 {
303  RV_VKImage* img = nullptr;
304 
306 
307  uint32_t mip_level;
308  uint32_t array_layer;
309  uint32_t output_num;
310 
311  bool is_init() const { return (img != nullptr || format != VK_FORMAT_UNDEFINED); }
312 };
313 
314 class RV_API RV_VKFramebufferInfo
315 {
316 public:
317  RV_VKFramebufferInfo(int width = 0, int height = 0, int samples = 1)
318  : myHeight(height), myWidth(width), mySamples(samples)
319  {
320  UT_ASSERT(width >= 0 && height >=0 && samples >= 1);
321  }
322 
323  // setup format used for image creation
324  void setSize(int w, int h) { myWidth = w; myHeight = h;}
325  int getWidth() { return myWidth; }
326  int getHeight() { return myHeight; }
327 
328  void setSampleCount(int samples) { mySamples = samples; }
329  int getSampleCount();
330 
331  // Attaches Image to framebuffer
332  void attachImage(
333  RV_VKImage* img,
334  RV_AttachmentType buffer_type,
335  int buffer_target = 0,
336  int level = 0,
337  int layer = 0 );
338 
339  void detachImage(int buffer_target);
340 
341  // Create image matching FB output, and automatically attach it.
342  // Created image must be owned by caller, will not be destroyed by FB
343  // NOTE: RE_ equivalent includes flags for enabling mip-mapping
344  // and rectangular mapping on image .. left out since they're unused
345  RV_VKImage* createImage(RV_Instance* inst,
346  RV_GPUType type, int vec_size,
347  RV_AttachmentType attach_type = RV_COLOR_BUFFER,
348  int buffer_target = 0);
349 
350 private:
351  uint32_t myWidth = 0;
352  uint32_t myHeight = 0;
353  uint32_t mySamples = 1;
354 
355  UT_Array<rv_AttachmentInfo> myColorAttachments;
356  rv_AttachmentInfo myDepthAttachment;
357 
358  friend RV_Framebuffer;
359 };
360 
361 // Framebuffer to be used with dynamic rendering
362 // Keeps a list of attachments, and can start
363 // a dynamic render pass using them
365 {
366 public:
367  static
368  RV_Framebuffer* createFramebuffer(
369  uint32_t width, uint32_t height,
370  int32_t offset_x, int32_t offset_y,
371  UT_Array<rv_VKAttachmentPtr> &color_attachments,
372  rv_VKAttachmentPtr depth_stencil_attachment);
373  static
374  RV_Framebuffer* createFramebuffer(
375  RV_Instance* inst, const RV_VKFramebufferInfo &info);
376 
377  ~RV_Framebuffer();
378 
379  RV_Framebuffer(const RV_Framebuffer&) = delete;
380 
381  // - transitions images into needed layout (if not already in them)
382  // - begins the renderbuffer
383  bool beginRendering(
384  RV_Instance* inst,
385  RV_VKCommandBuffer* cb,
386  RV_ImageOp img_op,
388 
389 private:
390 
392  uint32_t width, uint32_t height,
393  int32_t offset_x, int32_t offset_y,
394  UT_Array<rv_VKAttachmentPtr>& color_attachments,
395  rv_VKAttachmentPtr depth_stencil_attachment);
396 
397  VkRect2D myRenderRegion;
398  UT_Array<rv_VKAttachmentPtr> myColorAttachments;
399  rv_VKAttachmentPtr myDepthAttachment;
400 };
401 
402 #endif
403 #endif
void setSize(int w, int h)
void setClearColor(UT_Vector4F c)
bool operator==(const RV_RenderPassFormatInfo &other) const
bool operator!=(const RV_RenderPassFormatInfo &other) const
RV_VKFramebuffer(RV_VKFramebuffer &&other)
RV_ImageOp
Definition: RV_Type.h:390
GLint level
Definition: glcorearb.h:108
virtual ~RV_VKFramebuffer()
VKAPI_ATTR void VKAPI_CALL vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAllocationCallbacks *pAllocator)
float fpreal32
Definition: SYS_Types.h:200
const UT_Array< rv_VKAttachmentPtr > & colorAttachments() const
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
rv_VKAttachmentPtr myDepthAttachment
RV_AttachmentType
Definition: RV_Type.h:382
UT_Vector4T< int32 > UT_Vector4i
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLfloat f
Definition: glcorearb.h:1926
std::array< T, N > UT_FixedArray
Definition: UT_FixedArray.h:19
VKAPI_ATTR void VKAPI_CALL vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks *pAllocator)
UT_Array< rv_VKAttachmentPtr > myColorAttachments
UT_Vector4i getClearInt() const
void setClearInt(UT_Vector4i i)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
#define RV_API
Definition: RV_API.h:10
virtual ~RV_VKRenderPass()
virtual bool fillPipelineInfo(RV_RenderPassFormatInfo &info)=0
VkRenderPass getVkRenderPass() const
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:36
RV_VKRenderPass(RV_Instance *inst, VkRenderPass vk_handle)
GLint void * img
Definition: glcorearb.h:556
GLsizei samples
Definition: glcorearb.h:1298
const rv_VKAttachmentPtr & depthAttachment() const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
void setClearDepth(fpreal32 d)
RV_VKFramebuffer(RV_Instance *inst, VkFramebuffer vk_handle)
UT_Vector4T< fpreal32 > UT_Vector4F
virtual void attachmentsChanged()
RV_GPUType
Definition: RV_Type.h:37
VkFormat
Definition: vulkan_core.h:1386
GLint GLsizei width
Definition: glcorearb.h:103
VkDevice getDevice()
Get the raw vulkan device assocated with this instance.
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
RV_VKFramebuffer & operator=(RV_VKFramebuffer &&rhs) noexcept=delete
virtual bool beginRendering(RV_Instance *inst, RV_VKCommandBuffer *cb, RV_ImageOp img_op=RV_IMAGE_LOAD, RV_RenderPassType type=RV_PASS_WHOLE)=0
GLboolean r
Definition: glcorearb.h:1222
RV_RenderPassType
Definition: RV_Type.h:397
RV_VKRenderPass(RV_VKRenderPass &&other)
UT_Vector4F getClearColor() const
type
Definition: core.h:1059
fpreal32 getClearDepth() const
UT_FixedArray< VkFormat, 8 > myColorFormats
RV_VKRenderPass & operator=(RV_VKRenderPass &&rhs) noexcept=delete
VkFramebuffer getVkFramebuffer() const
virtual void endRendering(RV_Instance *inst, RV_VKCommandBuffer *cb)=0
RAII wrapper class for VkImageView.
Definition: RV_VKImage.h:243
UT_UniquePtr< rv_VKAttachment > rv_VKAttachmentPtr