HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_OGLInteropTextureBase.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_OGLInteropTextureBase.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * Texture usable in Vulkan and OpenGL
10  */
11 
12 
13 #ifndef RV_OGLInteropTextureBase_h
14 #define RV_OGLInteropTextureBase_h
15 
16 #include "RV_API.h"
17 
18 #include <VE/VE_VK.h>
19 #include "RV_VKImage.h"
20 #include "RV_TypePtrs.h"
21 
22 #include <atomic>
23 #include <glcorearb.h>
24 
25 #include <RE/RE_Texture.h>
26 #include <UT/UT_UniquePtr.h>
27 
28 
29 #ifdef WIN32
30  #include <windows.h>
31  typedef HANDLE handle;
32 #else
33 // export handles
34 // NOTE: vulkan + GL function signatures use `int`,
35 // in `xx_external_fd` functions, rather than
36 // sized type .. using `int` to match
37  typedef int handle;
38 #endif
39 
40 class RE_RenderContext;
41 
42 class RV_VKCommandBuffer;
43 class RV_VKQueue;
46 
48 class VE_Memory;
49 
50 // RV_VKInteropImageCreateInfo
51 // Create info for a VkImage which can be exported.
53 {
54 public:
55  bool fillCreateInfo() override;
56 
58  {
60  }
61 
62  RV_VKInteropImageCreateInfo() = default;
63  RV_VKImageCreateInfo *clone() const override
64  {
65  return new RV_VKInteropImageCreateInfo(*this);
66  }
68 
69 protected:
71 
72 public:
73 
75 };
76 
77 // RV_OGLInteropTextureBase
78 /// Set of resources needed to share a texture between
79 /// OpenGL and Vulkan. Can create a vulkan image,
80 /// or recieve an alloacted memory. Creates semaphores
81 /// for sync, and exports the handles for the memory and
82 /// semaphores then imports them into an openGL context.
84 {
85 public:
86  struct SemaphoreSet
87  {
88  VkSemaphore vkHandle;
91  };
92 
94  {
97  } myCurOwner;
98 
100  virtual ~RV_OGLInteropTextureBase();
101 
102  // Returns a new texture with no attributes of the given type.
103  static RV_OGLInteropTexturePtr create(RE_TextureDimension dim);
104 
105  // Default usage state is GL. Change to Vulkan if rendering or uploading via
106  // VK to the texture first.
108  { myCurOwner = owner; }
109 
111  {
112  UT_ASSERT(myCurOwner == RV_VULKAN);
113  return myRvImg.get();
114  }
115 
116  virtual RE_Texture* getRe() = 0;
117 
118  RV_OGLInteropOwner getCurrentRenderType() const { return myCurOwner; }
119 
120  RE_TextureID getGLID();
121 
123  {
124  return SemaphoreSet{
125  myVkSemGLToVk, /* vkHandle*/
126  myShareSemGLToVk, /* shareHandle */
127  myOglSemGLToVk, /* oglHandle */
128  };
129  }
130 
132  {
133  return SemaphoreSet{
134  myVkSemVkToGL, /* vkHandle*/
135  myShareSemVkToGL, /* shareHandle */
136  myOglSemVkToGL, /* oglHandle */
137  };
138  }
139 
140  bool beginTransferToVk(RE_Render *r);
141  bool finishTransferToVk(RV_Instance *r,
142  RV_VKCommandBuffer* cb);
143 
144  bool beginTransferToGL(RV_Instance *r,
145  RV_VKCommandBuffer* cb);
146  bool finishTransferToGL(RE_Render *r);
147 
148 
149  bool submitTransferToGL(RE_RenderContext r);
150 
151  bool submitTransferToVk(RE_RenderContext r);
152 
153  bool allocateImage(RV_Instance* inst,
154  RV_ImageDim image_type, VkFormat format,
155  size_t width, size_t height, size_t depth=1,
156  int levels =1, int layers =1, int samples=1);
157 
158  bool checkImageParams(RV_Instance* inst,
159  RV_ImageDim image_type, VkFormat format,
160  size_t width, size_t height, size_t depth=1,
161  int levels =1, int layers =1, int samples=1);
162 
163 
164 
165 protected:
166  // Checks whether the image will created with this create
167  // info will be exportable
168  static bool queryMemoryHandleValid(
169  RV_Instance *inst,
171 
172  // Assigns memory backing image -- called from allocateImage
173  // if image was allocated internally
174  // Doesn't take ownership if given external memory
175  // external memory should be destroyed AFTER this object
176  bool assignMemory(RV_Instance *inst, VE_Memory* mem);
177 
178  void deleteImage(RV_Instance* inst);
179 
180  // Vulkan Image -- may be NULL,
181  // if initialized with externally allocated memory
184 
185  VkDeviceMemory myVkMem = VK_NULL_HANDLE;
186  VkDeviceSize myMemSize = 0;
187  VkDeviceSize myMemOffset = 0;
188  VkImage myVkImg = VK_NULL_HANDLE;
189 
191  VkAccessFlags myLastAccess = 0;
192  VkPipelineStageFlags myLastStage = 0;
193 
194  // transfer must be committed by submitting the command buffer
195  // before work on the GL side can be done
196  // -- tacked atomically since they may be accessed form
197  // Vulkan or GL flag
198  std::atomic_bool myIsTransferToGLWaiting = false;
199 
200 #if 0 // TODO: allow multiple GL->VK sems to be signaled with a single signal+flush op
201  // Note: not locking access to this member,
202  // it will always be accessed from the
203  // GL thread
204  std::atomic_bool myIsTransferToVkWaiting = false;
205  static void commitAllTransfersToVk(RE_Render* r);
206  static std::list<std::atomic_bool*> theWaitingVkTransfers;
207 #endif
208 
209  // semaphores from Vulkan perspective
210  VkSemaphore myVkSemVkToGL = VK_NULL_HANDLE;
211  VkSemaphore myVkSemGLToVk = VK_NULL_HANDLE;
212 
213  bool allocateSemaphores(RV_Instance* inst);
214  void deleteSemaphores(RV_Instance* inst);
215 
216  int myRERenderID = -1;
217 
218  // semaphores from OS perspective
219  handle myShareSemVkToGL = 0;
220  handle myShareSemGLToVk = 0;
221 
222  // semaphores from OpenGL perspective
223  GLuint myOglSemVkToGL = 0;
224  GLuint myOglSemGLToVk = 0;
225 
226  handle myShareMem = 0;
227 
228  // Memory from GL perspective
229  GLuint myOglMem = 0;
230  GLuint myOglID = 0;
231 
232  bool importMemory(RE_Render *r);
233  bool importOglSemaphores(RE_Render *r);
234  void deleteMemory(RE_Render *r);
235  void deleteOglSemaphores(RE_Render *r);
236 
237  void deleteMemoryHandle();
238  void deleteSemaphoreHandles();
239 };
240 
241 /// Transfer a list of interop textures from VK to GL usage
243  RE_RenderContext rc,
245 
246 /// Transfer a list of interop textures from GL to VK usage
248  RE_RenderContext rc,
250 
251 #endif
#define VK_NULL_HANDLE
Definition: vulkan_core.h:45
static const VkExternalMemoryImageCreateInfo myExImgInfo
unsigned int GLuint
Definition: cl.hpp:167
VkFlags VkPipelineStageFlags
Definition: vulkan_core.h:2401
RE_TextureDimension
Temporary container for either a RV_Render and an RE_Render.
RV_VKImageCreateInfo * clone() const override
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
virtual bool fillCreateInfo()
RV_API void RVtransferToVK(RE_RenderContext rc, UT_Array< RV_OGLInteropTextureBase * > tex_list)
Transfer a list of interop textures from GL to VK usage.
unsigned int RE_TextureID
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
#define RV_API
Definition: RV_API.h:10
RV_MemType getAllocType(RV_Instance *inst) override
Setup allocation info for memory.
RV_API void RVtransferToGL(RE_RenderContext rc, UT_Array< RV_OGLInteropTextureBase * > tex_list)
Transfer a list of interop textures from VK to GL usage.
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:38
GLsizei levels
Definition: glcorearb.h:2224
GLsizei samples
Definition: glcorearb.h:1298
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
UT_UniquePtr< RV_VKImage > myRvImg
UT_UniquePtr< RV_OGLInteropTextureBase > RV_OGLInteropTexturePtr
VkFlags VkAccessFlags
Definition: vulkan_core.h:2155
VkImageLayout
Definition: vulkan_core.h:1250
void setCurrentState(RV_OGLInteropOwner owner)
VkFormat
Definition: vulkan_core.h:1386
GLint GLsizei width
Definition: glcorearb.h:103
uint64_t VkDeviceSize
Definition: vulkan_core.h:95
RV_MemType
Definition: RV_Type.h:102
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
GLboolean r
Definition: glcorearb.h:1222
RV_ImageDim
Definition: RV_Type.h:111
RV_OGLInteropOwner getCurrentRenderType() const