SpoutDirectX.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. spoutDirectX.h
  3. DirectX functions to manage DirectX 11 texture sharing
  4. Copyright (c) 2014 - 2015, Lynn Jarvis. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  13. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #pragma once
  23. #ifndef __spoutDirectX__
  24. #define __spoutDirectX__
  25. #include "SpoutCommon.h"
  26. #include <windowsx.h>
  27. #include <d3d9.h>
  28. #include <d3d11.h>
  29. #include <DXGI.h> // LJ DEBUG
  30. #include <string>
  31. #include <iostream>
  32. #pragma comment (lib, "d3d9.lib")
  33. #pragma comment (lib, "d3d11.lib")
  34. #pragma comment (lib, "DXGI.lib")
  35. using namespace std;
  36. class SPOUT_DLLEXP spoutDirectX {
  37. public:
  38. spoutDirectX();
  39. ~spoutDirectX();
  40. // DX9
  41. IDirect3D9Ex* CreateDX9object(); // Create a DirectX9 object
  42. IDirect3DDevice9Ex* CreateDX9device(IDirect3D9Ex* pD3D, HWND hWnd); // Create a DirectX9 device
  43. bool CreateSharedDX9Texture(IDirect3DDevice9Ex* pDevice, unsigned int width, unsigned int height, D3DFORMAT format, LPDIRECT3DTEXTURE9 &dxTexture, HANDLE &dxShareHandle);
  44. // LJ DEBUG : TODO
  45. // bool ReadSharedDX9texture(IDirect3DDevice9Ex* pDevice, LPDIRECT3DTEXTURE9 &dxTexture, HANDLE &dxShareHandle, unsigned char *pixels, unsigned int width, unsigned int height);
  46. // DX11
  47. ID3D11Device* CreateDX11device(); // Create a DX11 device
  48. bool CreateSharedDX11Texture(ID3D11Device* pDevice, unsigned int width, unsigned int height, DXGI_FORMAT format, ID3D11Texture2D** pSharedTexture, HANDLE &dxShareHandle);
  49. bool OpenDX11shareHandle(ID3D11Device* pDevice, ID3D11Texture2D** ppSharedTexture, HANDLE dxShareHandle);
  50. void CloseDX11();
  51. // Output adapter selection
  52. int GetNumAdapters(); // Get the number of graphics adapters in the system
  53. bool GetAdapterName(int index, char *adaptername, int maxchars); // Get an adapter name
  54. bool SetAdapter(int index); // Set required graphics adapter for output
  55. int GetAdapter(); // Get the current adapter index
  56. bool GetAdapterInfo(char *renderdescription, char *displaydescription, int maxchars);
  57. // Registry read/write
  58. // 20.11.15 - moved from interop class
  59. bool ReadDwordFromRegistry(DWORD *pValue, const char *subkey, const char *valuename);
  60. bool WriteDwordToRegistry(DWORD dwValue, const char *subkey, const char *valuename);
  61. // Mutex locks for DirectX 9 shared texture access
  62. bool CreateAccessMutex(const char *name, HANDLE &hAccessMutex);
  63. void CloseAccessMutex(HANDLE &hAccessMutex);
  64. bool CheckAccess(HANDLE hAccessMutex, ID3D11Texture2D* pSharedTexture = NULL);
  65. void AllowAccess(HANDLE hAccessMutex, ID3D11Texture2D* pSharedTexture = NULL);
  66. // LJ DEBUG
  67. // Receiver access mutex - created by a receiver
  68. //
  69. // Sender :
  70. // o CheckReceiverAccess
  71. // o write texture
  72. // o AllowReceiverAcess
  73. //
  74. // Receiver :
  75. // o CheckSenderAccess (CheckAccess)
  76. // o read texture
  77. // o AllowSenderAccess (AllowAccess)
  78. //
  79. // Common :
  80. // CreateAccessMutex
  81. // CloseAccessMutex
  82. // CreateReceiverAccessMutex
  83. // CloseRecieverAccessMutex
  84. //
  85. bool CreateReceiverAccessMutex(const char *name, HANDLE &hAccessMutex);
  86. void CloseReceiverAccessMutex(HANDLE &hAccessMutex);
  87. bool CheckReceiverAccess(HANDLE hAccessMutex);
  88. void AllowReceiverAccess(HANDLE hAccessMutex);
  89. // For debugging only - to toggle texture access locks disable/enable
  90. bool bUseAccessLocks;
  91. protected:
  92. IDXGIAdapter* GetAdapterPointer(int index); // Get adapter pointer for DirectX 11
  93. int g_AdapterIndex; // Used for DX9
  94. IDXGIAdapter* g_pAdapterDX11;
  95. ID3D11DeviceContext* g_pImmediateContext;
  96. D3D_DRIVER_TYPE g_driverType;
  97. D3D_FEATURE_LEVEL g_featureLevel;
  98. };
  99. #endif