diff --git a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll index 19962a1..488f3e6 100644 Binary files a/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll and b/Assets/uDesktopDuplication/Plugins/x86_64/uDesktopDuplication.dll differ diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp new file mode 100644 index 0000000..87c2847 --- /dev/null +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.cpp @@ -0,0 +1,96 @@ +#pragma once + +#include +#include + +#include "IUnityInterface.h" +#include "IUnityGraphicsD3D11.h" +#include "Device.h" +#include "Debug.h" + +#pragma comment(lib, "d3d11.lib") + +using namespace Microsoft::WRL; + + + +IsolatedD3D11Device::IsolatedD3D11Device() +{ +} + + +IsolatedD3D11Device::~IsolatedD3D11Device() +{ +} + + +HRESULT IsolatedD3D11Device::Create(const ComPtr& adapter) +{ + const auto driverType = adapter ? + D3D_DRIVER_TYPE_UNKNOWN : + D3D_DRIVER_TYPE_HARDWARE; + const auto flags = + D3D11_CREATE_DEVICE_BGRA_SUPPORT; // D2D Compatible + // | D3D11_CREATE_DEVICE_VIDEO_SUPPORT // MediaFoundation + const D3D_FEATURE_LEVEL featureLevelsRequested[] = + { + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, + D3D_FEATURE_LEVEL_9_3, + D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1 + }; + const UINT numLevelsRequested = sizeof(featureLevelsRequested) / sizeof(D3D_FEATURE_LEVEL); + D3D_FEATURE_LEVEL featureLevelsSupported; + + return D3D11CreateDevice( + adapter.Get(), + driverType, + nullptr, + flags, + featureLevelsRequested, + numLevelsRequested, + D3D11_SDK_VERSION, + &device_, + &featureLevelsSupported, + nullptr); +} + + +ComPtr IsolatedD3D11Device::GetDevice() +{ + return device_; +} + + +ComPtr IsolatedD3D11Device::GetCompatibleSharedTexture(const ComPtr& src) +{ + D3D11_TEXTURE2D_DESC srcDesc; + src->GetDesc(&srcDesc); + + // check if the format and size of the current texture are same as the source one + if (cachedTexture_) + { + D3D11_TEXTURE2D_DESC targetDesc; + cachedTexture_->GetDesc(&targetDesc); + if (targetDesc.Format == srcDesc.Format && + targetDesc.Width == srcDesc.Width && + targetDesc.Height == srcDesc.Height) + { + return cachedTexture_; + } + cachedTexture_ = nullptr; + } + + // for sharing this texture with unity device + srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; + + if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &cachedTexture_))) + { + Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed."); + return nullptr; + } + + return cachedTexture_; +} \ No newline at end of file diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h new file mode 100644 index 0000000..4b6af1e --- /dev/null +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Device.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + + +// Thraed safe self created ID3D11Device from specified adapter +class IsolatedD3D11Device +{ +public: + IsolatedD3D11Device(); + ~IsolatedD3D11Device(); + + HRESULT Create(const Microsoft::WRL::ComPtr& adapter); + Microsoft::WRL::ComPtr GetDevice(); + Microsoft::WRL::ComPtr GetCompatibleSharedTexture( + const Microsoft::WRL::ComPtr& src); + +private: + Microsoft::WRL::ComPtr device_; + Microsoft::WRL::ComPtr cachedTexture_; +}; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp index cd26c6b..99763e4 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp @@ -1,85 +1,15 @@ #include #include #include +#include "Monitor.h" #include "Debug.h" #include "Cursor.h" #include "MonitorManager.h" -#include "Monitor.h" - -#pragma comment(lib, "d3d11.lib") +#include "Device.h" using namespace Microsoft::WRL; -/// -/// Thraed safe self created ID3D11Device from specified adapter -/// -class IsolatedD3D11Device -{ - ComPtr m_device; - ComPtr m_copyTarget; - -public: - ComPtr GetDevice()const { return m_device; } - - bool CreateDevice(const ComPtr &adapter) - { - UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT // D2D Compatible - //| D3D11_CREATE_DEVICE_SINGLETHREADED - //| D3D11_CREATE_DEVICE_VIDEO_SUPPORT // MediaFoundation - ; - - D3D_FEATURE_LEVEL FeatureLevelsRequested[6] = { - D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, - D3D_FEATURE_LEVEL_9_2, D3D_FEATURE_LEVEL_9_1 }; - - UINT numLevelsRequested = sizeof(FeatureLevelsRequested) / sizeof(D3D_FEATURE_LEVEL); - D3D_FEATURE_LEVEL FeatureLevelsSupported; - - if (FAILED(D3D11CreateDevice(adapter.Get() - , adapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, nullptr - , flags - , FeatureLevelsRequested, numLevelsRequested - , D3D11_SDK_VERSION - , &m_device - , &FeatureLevelsSupported - , nullptr - ))) { - return false; - } - return true; - } - - ComPtr CreateCopyTargetWithSharedFlag(const ComPtr &src) - { - D3D11_TEXTURE2D_DESC desc; - src->GetDesc(&desc); - - if (m_copyTarget) { - D3D11_TEXTURE2D_DESC target_desc; - m_copyTarget->GetDesc(&target_desc); - if ( - target_desc.Format == desc.Format - && target_desc.Width == desc.Width - && target_desc.Height == desc.Height - ) { - // already created - return m_copyTarget; - } - } - - //desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE; - desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; // for share texture with unity device - - if (FAILED(m_device->CreateTexture2D(&desc, NULL, &m_copyTarget))) - { - return nullptr; - } - return m_copyTarget; - } -}; - struct QueueItem { @@ -151,7 +81,9 @@ void Monitor::Initialize( m_pIsolated = std::make_shared(); m_textureQueue = std::make_shared(); - if (!m_pIsolated->CreateDevice(adapter)) { + if (FAILED(m_pIsolated->Create(adapter))) + { + Debug::Error("Monitor::Initialize() => IsolatedD3D11Device::Create() failed."); return; } @@ -535,7 +467,7 @@ void Monitor::DuplicateAndCopyLoop() } // copy target - auto copyTarget = m_pIsolated->CreateCopyTargetWithSharedFlag(texture); + auto copyTarget = m_pIsolated->GetCompatibleSharedTexture(texture); if (!copyTarget) { return; } diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj b/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj index 637e1d4..df33c95 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj @@ -141,6 +141,7 @@ copy /Y "$(SolutionDir)$(Platform)\$(Configuration)\$(TargetName).pdb" "$(Soluti + @@ -149,6 +150,7 @@ copy /Y "$(SolutionDir)$(Platform)\$(Configuration)\$(TargetName).pdb" "$(Soluti + diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj.filters b/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj.filters index 48179e2..eb8cef5 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj.filters +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/uDesktopDuplication.vcxproj.filters @@ -20,6 +20,7 @@ + @@ -28,5 +29,6 @@ + \ No newline at end of file