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 "RV_VK.h"
19 #include "RV_VKImage.h"
20 
21 #include <atomic>
22 #include <glcorearb.h>
23 
24 #include <RE/RE_Texture.h>
25 #include <UT/UT_UniquePtr.h>
26 
27 
28 #ifdef WIN32
29  #include <windows.h>
30  typedef HANDLE handle;
31 #else
32 // export handles
33 // NOTE: vulkan + GL function signatures use `int`,
34 // in `xx_external_fd` functions, rather than
35 // sized type .. using `int` to match
36  typedef int handle;
37 #endif
38 
39 class RE_RenderContext;
40 
41 class RV_VKCommandBuffer;
42 class RV_VKQueue;
45 
47 
48 // RV_VKInteropImageCreateInfo
49 // Create info for a VkImage which can be exported.
51 {
52 public:
53  bool fillCreateInfo() override;
54 
56  {
58  }
59 
60  RV_VKInteropImageCreateInfo() = default;
61  RV_VKImageCreateInfo *clone() const override
62  {
63  return new RV_VKInteropImageCreateInfo(*this);
64  }
66 
67 protected:
69 
70 public:
71 
73 };
74 
75 // RV_OGLInteropTextureBase
76 /// Set of resources needed to share a texture between
77 /// OpenGL and Vulkan. Can create a vulkan image,
78 /// or recieve an alloacted memory. Creates semaphores
79 /// for sync, and exports the handles for the memory and
80 /// semaphores then imports them into an openGL context.
82 {
83 public:
84  struct SemaphoreSet
85  {
86  VkSemaphore vkHandle;
89  };
90 
92  {
95  } myCurOwner;
96 
98  virtual ~RV_OGLInteropTextureBase();
99 
100  // Returns a new texture with no attributes of the given type.
101  static RV_OGLInteropTexturePtr create(RE_TextureDimension dim);
102 
103  // Default usage state is GL. Change to Vulkan if rendering or uploading via
104  // VK to the texture first.
106  { myCurOwner = owner; }
107 
109  {
110  UT_ASSERT(myCurOwner == RV_VULKAN);
111  return myRvImg.get();
112  }
113 
114  virtual RE_Texture* getRe() = 0;
115 
116  RV_OGLInteropOwner getCurrentRenderType() const { return myCurOwner; }
117 
118  RE_TextureID getGLID();
119 
121  {
122  return SemaphoreSet{
123  myVkSemGLToVk, /* vkHandle*/
124  myShareSemGLToVk, /* shareHandle */
125  myOglSemGLToVk, /* oglHandle */
126  };
127  }
128 
130  {
131  return SemaphoreSet{
132  myVkSemVkToGL, /* vkHandle*/
133  myShareSemVkToGL, /* shareHandle */
134  myOglSemVkToGL, /* oglHandle */
135  };
136  }
137 
138  bool beginTransferToVk(RE_Render *r);
139  bool finishTransferToVk(RV_Instance *r,
140  RV_VKCommandBuffer* cb);
141 
142  bool beginTransferToGL(RV_Instance *r,
143  RV_VKCommandBuffer* cb);
144  bool finishTransferToGL(RE_Render *r);
145 
146 
147  bool submitTransferToGL(RE_RenderContext r);
148 
149  bool submitTransferToVk(RE_RenderContext r);
150 
151  bool allocateImage(RV_Instance* inst,
152  RV_ImageDim image_type, VkFormat format,
153  size_t width, size_t height, size_t depth=1,
154  int levels =1, int layers =1, int samples=1);
155 
156  bool checkImageParams(RV_Instance* inst,
157  RV_ImageDim image_type, VkFormat format,
158  size_t width, size_t height, size_t depth=1,
159  int levels =1, int layers =1, int samples=1);
160 
161 
162 
163 protected:
164  // Checks whether the image will created with this create
165  // info will be exportable
166  static bool queryMemoryHandleValid(
167  RV_Instance *inst,
169 
170  // Assigns memory backing image -- called from allocateImage
171  // if image was allocated internally
172  // Doesn't take ownership if given external memory
173  // external memory should be destroyed AFTER this object
174  bool assignMemory(RV_Instance *inst, RV_VKMemory* mem);
175 
176  void deleteImage(RV_Instance* inst);
177 
178  // Vulkan Image -- may be NULL,
179  // if initialized with externally allocated memory
181  RV_VKMemory* myRvMem;
182 
183  VkDeviceMemory myVkMem = VK_NULL_HANDLE;
184  VkDeviceSize myMemSize = 0;
185  VkDeviceSize myMemOffset = 0;
186  VkImage myVkImg = VK_NULL_HANDLE;
187 
189  VkAccessFlags myLastAccess = 0;
190  VkPipelineStageFlags myLastStage = 0;
191 
192  // transfer must be committed by submitting the command buffer
193  // before work on the GL side can be done
194  // -- tacked atomically since they may be accessed form
195  // Vulkan or GL flag
196  std::atomic_bool myIsTransferToGLWaiting = false;
197 
198 #if 0 // TODO: allow multiple GL->VK sems to be signaled with a single signal+flush op
199  // Note: not locking access to this member,
200  // it will always be accessed from the
201  // GL thread
202  std::atomic_bool myIsTransferToVkWaiting = false;
203  static void commitAllTransfersToVk(RE_Render* r);
204  static std::list<std::atomic_bool*> theWaitingVkTransfers;
205 #endif
206 
207  // semaphores from Vulkan perspective
208  VkSemaphore myVkSemVkToGL = VK_NULL_HANDLE;
209  VkSemaphore myVkSemGLToVk = VK_NULL_HANDLE;
210 
211  bool allocateSemaphores(RV_Instance* inst);
212  void deleteSemaphores(RV_Instance* inst);
213 
214  int myRERenderID = -1;
215 
216  // semaphores from OS perspective
217  handle myShareSemVkToGL = 0;
218  handle myShareSemGLToVk = 0;
219 
220  // semaphores from OpenGL perspective
221  GLuint myOglSemVkToGL = 0;
222  GLuint myOglSemGLToVk = 0;
223 
224  handle myShareMem = 0;
225 
226  // Memory from GL perspective
227  GLuint myOglMem = 0;
228  GLuint myOglID = 0;
229 
230  bool importMemory(RE_Render *r);
231  bool importOglSemaphores(RE_Render *r);
232  void deleteMemory(RE_Render *r);
233  void deleteOglSemaphores(RE_Render *r);
234 };
235 
236 /// Transfer a list of interop textures from VK to GL usage
238  RE_RenderContext rc,
240 
241 /// Transfer a list of interop textures from GL to VK usage
243  RE_RenderContext rc,
245 
246 #endif
bool fillCreateInfo() override
#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
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:36
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