SpoutGLDXinterop.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. spoutGLDXinterop.h
  3. LJ - leadedge@adam.com.au
  4. Functions to manage texture sharing using the NVIDIA GL/DX opengl extensions
  5. https://www.opengl.org/registry/specs/NV/DX_interop.txt
  6. Copyright (c) 2014-2015, Lynn Jarvis. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. 1. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright notice,
  12. this list of conditions and the following disclaimer in the documentation
  13. and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  15. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #pragma once
  25. #ifndef __spoutGLDXinterop__ // standard way as well
  26. #define __spoutGLDXinterop__
  27. #include "SpoutCommon.h"
  28. #include "spoutGLextensions.h"
  29. #include "spoutDirectX.h"
  30. #include "spoutSenderNames.h"
  31. #include "SpoutMemoryShare.h"
  32. #include <windowsx.h>
  33. #include <d3d9.h> // DX9
  34. #include <d3d11.h> // DX11
  35. #include <gl/gl.h>
  36. #include <gl/glu.h> // For glerror
  37. #include <shlwapi.h> // for path functions
  38. // LJ DEBUG
  39. // #include <d3dx9.h> // Needed for D3DXLoadSurfaceFromSurface()
  40. // #pragma comment(lib, "d3dx9.lib")
  41. class SPOUT_DLLEXP spoutGLDXinterop {
  42. public:
  43. spoutGLDXinterop();
  44. ~spoutGLDXinterop();
  45. // Initialization functions
  46. bool LoadGLextensions(); // Load required opengl extensions
  47. bool CreateInterop(HWND hWnd, const char* sendername, unsigned int width, unsigned int height, DWORD dwFormat, bool bReceive = true);
  48. bool CheckInterop(HWND hWnd); // Check for successful open of the interop
  49. void CleanupInterop(bool bExit = false); // Cleanup with flag to avoid unknown crash bug
  50. void setSharedMemoryName(char* sharedMemoryName, bool bReceive = true);
  51. bool getSharedInfo(char* sharedMemoryName, SharedTextureInfo* info);
  52. bool setSharedInfo(char* sharedMemoryName, SharedTextureInfo* info);
  53. bool WriteTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO=0);
  54. bool ReadTexture (GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=false, GLuint HostFBO=0);
  55. bool WriteTexturePixels(unsigned char *pixels, unsigned int width, unsigned int height, GLenum glFormat = GL_RGBA, bool bAlignment = true);
  56. bool ReadTexturePixels (unsigned char *pixels, unsigned int width, unsigned int height, GLenum glFormat = GL_RGBA, GLuint HostFBO=0);
  57. bool WriteDword(GLuint TextureID, GLuint TextureTarget, DWORD dwValue);
  58. bool ReadDword (GLuint TextureID, GLuint TextureTarget, DWORD &dwValue, unsigned int width, unsigned int height, bool bInvert = false);
  59. unsigned __int32 iLastFrame;
  60. bool InitOpenGL();
  61. bool CloseOpenGL();
  62. #ifdef USE_PBO_EXTENSIONS
  63. bool LoadTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, unsigned char *data);
  64. #endif
  65. bool BindSharedTexture();
  66. bool UnBindSharedTexture();
  67. bool DrawSharedTexture(float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = true);
  68. bool DrawToSharedTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = false, GLuint HostFBO = 0);
  69. // DX9
  70. bool bUseDX9; // Use DX11 (default) or DX9
  71. bool UseDX9(bool bDX9);
  72. bool isDX9();
  73. // Set and get flags only
  74. void SetDX9(bool bDX9);
  75. bool GetDX9();
  76. bool bUseMemory; // Use memoryshare
  77. bool GetMemoryShareMode();
  78. bool SetMemoryShareMode(bool bMem = true);
  79. D3DFORMAT DX9format; // the DX9 texture format to be used
  80. void SetDX9format(D3DFORMAT textureformat);
  81. int GetNumAdapters(); // Get the number of graphics adapters in the system
  82. bool GetAdapterName(int index, char *adaptername, int maxchars); // Get an adapter name
  83. bool SetAdapter(int index); // Set required graphics adapter for output
  84. int GetAdapter(); // Get the SpoutDirectX global adapter index
  85. bool GetHostPath(const char *sendername, char *hostpath, int maxchars); // The path of the host that produced the sender
  86. bool CreateDX9interop(unsigned int width, unsigned int height, DWORD dwFormat, bool bReceive = true);
  87. bool OpenDirectX9(HWND hWnd); // Initialize and prepare DirectX9
  88. void CleanupDX9();
  89. // DX11
  90. DXGI_FORMAT DX11format; // the DX11 texture format to be used
  91. void SetDX11format(DXGI_FORMAT textureformat); // set format by user
  92. bool CreateDX11interop(unsigned int width, unsigned int height, DWORD dwFormat, bool bReceive);
  93. bool OpenDirectX11(); // Initialize and prepare DirectX11
  94. bool DX11available(); // Test for DX11 by attempting to open a device
  95. void CleanupDX11();
  96. // Common
  97. bool OpenDirectX(HWND hWnd, bool bDX9);
  98. void CleanupDirectX();
  99. HANDLE LinkGLDXtextures(void* pDXdevice, void* pSharedTexture, HANDLE dxShareHandle, GLuint glTextureID);
  100. // Utilities
  101. bool GLDXcompatible();
  102. int GetVerticalSync();
  103. bool SetVerticalSync(bool bSync = true);
  104. bool GetAdapterInfo(char *renderadapter,
  105. char *renderdescription, char *renderversion,
  106. char *displaydescription, char *displayversion,
  107. int maxsize, bool &bUseDX9);
  108. bool CheckSpout2004(); // LJ DEBUG - 2.005 transition utility TODO : remove
  109. // Registry read/write
  110. // 20.11.15 - moved to SpoutDirectX class
  111. // bool ReadDwordFromRegistry(DWORD *pValue, const char *subkey, const char *valuename);
  112. // bool WriteDwordToRegistry(DWORD dwValue, const char *subkey, const char *valuename);
  113. // Spout objects
  114. spoutMemoryShare memoryshare; // Memory sharing
  115. spoutSenderNames senders; // Sender management
  116. spoutDirectX spoutdx; // DirectX class
  117. // Locks for gl/dx interop functions
  118. HRESULT LockInteropObject(HANDLE hDevice, HANDLE *hObject);
  119. HRESULT UnlockInteropObject(HANDLE hDevice, HANDLE *hObject);
  120. // Memoryshare functions
  121. bool WriteMemory (GLuint TexID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert = false);
  122. bool ReadMemory (GLuint TexID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert = false);
  123. bool WriteMemoryPixels (unsigned char *pixels, unsigned int width, unsigned int height);
  124. bool ReadMemoryPixels (unsigned char *pixels, unsigned int width, unsigned int height);
  125. bool DrawSharedMemory (float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = false);
  126. bool DrawToSharedMemory(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, float max_x = 1.0, float max_y = 1.0, float aspect = 1.0, bool bInvert = false, GLuint HostFBO = 0);
  127. GLuint GetGLtextureID();
  128. // GLuint GetTextureWidth();
  129. // GLuint GetTextureHeight();
  130. // Global texture and fbo used for texture sharing
  131. GLuint m_glTexture; // the OpenGL texture linked to the shared DX texture
  132. GLuint m_fbo; // General fbo used for texture transfers
  133. // public for external access
  134. IDirect3DDevice9Ex* m_pDevice; // DX9 device
  135. LPDIRECT3DTEXTURE9 m_dxTexture; // the shared DX9 texture
  136. HANDLE m_dxShareHandle; // the shared DX texture handle
  137. ID3D11Device* g_pd3dDevice; // DX11 device
  138. ID3D11Texture2D* g_pSharedTexture; // The shared DX11 texture
  139. void GLerror();
  140. void PrintFBOstatus(GLenum status); // debug
  141. protected:
  142. bool m_bInitialized; // this instance initialized flag
  143. bool bExtensionsLoaded; // extensions have been loaded
  144. bool bFBOavailable; // fbo extensions available
  145. bool bBLITavailable; // fbo blit extensions available
  146. bool bPBOavailable; // pbo extensions available
  147. bool bSWAPavailable; // swap extensions available
  148. HWND m_hWnd; // parent window
  149. HANDLE m_hSharedMemory; // handle to the texture info shared memory
  150. SharedTextureInfo m_TextureInfo; // local texture info structure
  151. GLuint m_TexID; // Local texture used for memoryshare functions
  152. unsigned int m_TexWidth; // width and height of local texture
  153. unsigned int m_TexHeight; // for testing changes of memoryshare sender size
  154. // DX11
  155. ID3D11DeviceContext* g_pImmediateContext;
  156. D3D_DRIVER_TYPE g_driverType;
  157. D3D_FEATURE_LEVEL g_featureLevel;
  158. // DX9
  159. IDirect3D9Ex* m_pD3D; // DX9 object
  160. HANDLE m_hInteropDevice; // handle to the DX/GL interop device
  161. HANDLE m_hInteropObject; // handle to the DX/GL interop object (the shared texture)
  162. HANDLE m_hAccessMutex; // Texture access mutex lock handle
  163. // LJ DEBUG
  164. HANDLE m_hReceiverAccessMutex; // Texture access mutex lock handle
  165. bool getSharedTextureInfo(const char* sharedMemoryName);
  166. bool setSharedTextureInfo(const char* sharedMemoryName);
  167. bool OpenDeviceKey(const char* key, int maxsize, char *description, char *version);
  168. void trim(char * s);
  169. bool InitTexture(GLuint &texID, GLenum GLformat, unsigned int width, unsigned int height);
  170. };
  171. #endif