tweak shared texture.

This commit is contained in:
hecomi
2017-01-07 23:02:48 +09:00
8 changed files with 105 additions and 41 deletions
@@ -7,6 +7,7 @@
#include "IUnityGraphicsD3D11.h" #include "IUnityGraphicsD3D11.h"
#include "Device.h" #include "Device.h"
#include "Debug.h" #include "Debug.h"
#include "Common.h"
#pragma comment(lib, "d3d11.lib") #pragma comment(lib, "d3d11.lib")
@@ -14,9 +15,39 @@ using namespace Microsoft::WRL;
IsolatedD3D11Device::IsolatedD3D11Device(UINT cachedTextureNum) Microsoft::WRL::ComPtr<ID3D11Texture2D> SharedTextureWrapper::Get()
: cachedTextures_(cachedTextureNum)
{ {
return pointer_;
}
Microsoft::WRL::ComPtr<ID3D11Texture2D> SharedTextureWrapper::Lock()
{
if (locked_) return nullptr;
locked_ = true;
return pointer_;
}
void SharedTextureWrapper::Unlock()
{
locked_ = false;
}
bool SharedTextureWrapper::IsLocked() const
{
return locked_;
}
IsolatedD3D11Device::IsolatedD3D11Device(UINT cachedTextureNum)
{
for (UINT i = 0; i < cachedTextureNum; ++i)
{
cachedSharedTextures_.push_back(std::make_shared<SharedTextureWrapper>());
}
} }
@@ -65,42 +96,42 @@ ComPtr<ID3D11Device> IsolatedD3D11Device::GetDevice()
} }
ComPtr<ID3D11Texture2D> IsolatedD3D11Device::GetCompatibleSharedTexture( std::shared_ptr<SharedTextureWrapper> IsolatedD3D11Device::GetCompatibleSharedTexture(
const ComPtr<ID3D11Texture2D>& src, const ComPtr<ID3D11Texture2D>& src,
UINT index) UINT index)
{ {
if (index < 0 || index >= cachedTextures_.size()) if (index < 0 || index >= cachedSharedTextures_.size())
{ {
Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => ", index, " is out of cachedTextures range."); Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => ", index, " is out of cachedTextures range.");
return nullptr; return nullptr;
} }
auto& cachedTexture = cachedTextures_.at(index); auto& sharedTextureWrapper = cachedSharedTextures_.at(index);
auto& texture = sharedTextureWrapper->pointer_; // raw pointer
D3D11_TEXTURE2D_DESC srcDesc;
src->GetDesc(&srcDesc);
// check if the format and size of the current texture are same as the source one // check if the format and size of the current texture are same as the source one
if (cachedTexture) D3D11_TEXTURE2D_DESC srcDesc;
src->GetDesc(&srcDesc);
if (texture)
{ {
D3D11_TEXTURE2D_DESC targetDesc; D3D11_TEXTURE2D_DESC targetDesc;
cachedTexture->GetDesc(&targetDesc); texture->GetDesc(&targetDesc);
if (targetDesc.Format == srcDesc.Format && if (targetDesc.Format == srcDesc.Format &&
targetDesc.Width == srcDesc.Width && targetDesc.Width == srcDesc.Width &&
targetDesc.Height == srcDesc.Height) targetDesc.Height == srcDesc.Height)
{ {
return cachedTexture; return sharedTextureWrapper;
} }
} }
// for sharing this texture with unity device // for sharing this texture with unity device
srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &cachedTexture))) if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &texture)))
{ {
Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed."); Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed.");
return nullptr; return nullptr;
} }
return cachedTexture; return sharedTextureWrapper;
} }
@@ -1,10 +1,28 @@
#pragma once #pragma once
#include <atomic>
#include <vector> #include <vector>
#include <memory>
#include <d3d11.h> #include <d3d11.h>
#include <wrl/client.h> #include <wrl/client.h>
// Shared texture wrapper to manager locking state
class SharedTextureWrapper
{
friend class IsolatedD3D11Device;
public:
Microsoft::WRL::ComPtr<ID3D11Texture2D> Get();
Microsoft::WRL::ComPtr<ID3D11Texture2D> Lock();
void Unlock();
bool IsLocked() const;
private:
std::atomic<bool> locked_ = false;
Microsoft::WRL::ComPtr<ID3D11Texture2D> pointer_;
};
// Thraed safe self created ID3D11Device from specified adapter // Thraed safe self created ID3D11Device from specified adapter
class IsolatedD3D11Device class IsolatedD3D11Device
{ {
@@ -14,11 +32,11 @@ public:
HRESULT Create(const Microsoft::WRL::ComPtr<IDXGIAdapter>& adapter); HRESULT Create(const Microsoft::WRL::ComPtr<IDXGIAdapter>& adapter);
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice(); Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
Microsoft::WRL::ComPtr<ID3D11Texture2D> GetCompatibleSharedTexture( std::shared_ptr<SharedTextureWrapper> GetCompatibleSharedTexture(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src, const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src,
UINT index); UINT index);
private: private:
Microsoft::WRL::ComPtr<ID3D11Device> device_; Microsoft::WRL::ComPtr<ID3D11Device> device_;
std::vector<Microsoft::WRL::ComPtr<ID3D11Texture2D>> cachedTextures_; std::vector<std::shared_ptr<SharedTextureWrapper>> cachedSharedTextures_;
}; };
@@ -162,7 +162,12 @@ void Duplicator::Start()
}); });
const auto timeout = static_cast<UINT>(frameMilliSeconds); const auto timeout = static_cast<UINT>(frameMilliSeconds);
if (!Duplicate(timeout)) break; Duplicate(timeout);
if (state_ != State::Running)
{
break;
}
} }
if (state_ == State::Running) if (state_ == State::Running)
@@ -229,9 +234,9 @@ const Duplicator::Frame& Duplicator::GetLastFrame() const
} }
bool Duplicator::Duplicate(UINT timeout) void Duplicator::Duplicate(UINT timeout)
{ {
if (!dupl_ || !device_) return false; if (!dupl_ || !device_) return;
ComPtr<IDXGIResource> resource; ComPtr<IDXGIResource> resource;
DXGI_OUTDUPL_FRAME_INFO frameInfo; DXGI_OUTDUPL_FRAME_INFO frameInfo;
@@ -247,31 +252,32 @@ bool Duplicator::Duplicate(UINT timeout)
// it is necessary to re-initialize monitors. // it is necessary to re-initialize monitors.
Debug::Log("Duplicator::Duplicate() => DXGI_ERROR_ACCESS_LOST."); Debug::Log("Duplicator::Duplicate() => DXGI_ERROR_ACCESS_LOST.");
state_ = State::AccessLost; state_ = State::AccessLost;
return false; break;
} }
case DXGI_ERROR_WAIT_TIMEOUT: case DXGI_ERROR_WAIT_TIMEOUT:
{ {
// This often occurs when timeout value is small and it is not problem. // This often occurs when timeout value is small and it is not problem.
// Debug::Log("Duplicator::Duplicate() => DXGI_ERROR_WAIT_TIMEOUT."); // Debug::Log("Duplicator::Duplicate() => DXGI_ERROR_WAIT_TIMEOUT.");
return true; break;
} }
case DXGI_ERROR_INVALID_CALL: case DXGI_ERROR_INVALID_CALL:
{ {
Debug::Error("Duplicator::Duplicate() => DXGI_ERROR_INVALID_CALL."); Debug::Error("Duplicator::Duplicate() => DXGI_ERROR_INVALID_CALL.");
return false; break;
} }
case E_INVALIDARG: case E_INVALIDARG:
{ {
Debug::Error("Duplicator::Duplicate() => E_INVALIDARG."); Debug::Error("Duplicator::Duplicate() => E_INVALIDARG.");
return false; break;
} }
default: default:
{ {
state_ = State::Unknown; state_ = State::Unknown;
Debug::Error("Duplicator::Duplicate() => Unknown Error."); Debug::Error("Duplicator::Duplicate() => Unknown Error.");
return false; break;
} }
} }
return;
} }
ScopedReleaser releaser([this] ScopedReleaser releaser([this]
@@ -305,15 +311,21 @@ bool Duplicator::Duplicate(UINT timeout)
ComPtr<ID3D11Texture2D> texture; ComPtr<ID3D11Texture2D> texture;
if (FAILED(resource.As(&texture))) if (FAILED(resource.As(&texture)))
{ {
return false; Debug::Error("Duplicator::Duplicate() => IDXGIResource could not be converted to ID3D11Texture2D.");
return;
} }
auto sharedTexture = device_->GetCompatibleSharedTexture(texture, frame_ % 2); auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, lastFrameId_ % 2);
if (!sharedTexture) if (!sharedTextureWrapper)
{ {
return false; Debug::Error("Duplicator::Duplicate() => Shared texture is null.");
return;
} }
auto sharedTexture = sharedTextureWrapper->Lock();
if (!sharedTexture) return;
ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); });
ComPtr<ID3D11DeviceContext> context; ComPtr<ID3D11DeviceContext> context;
device_->GetDevice()->GetImmediateContext(&context); device_->GetDevice()->GetImmediateContext(&context);
context->CopyResource(sharedTexture.Get(), texture.Get()); context->CopyResource(sharedTexture.Get(), texture.Get());
@@ -325,14 +337,12 @@ bool Duplicator::Duplicate(UINT timeout)
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
lastFrame_ = Frame lastFrame_ = Frame
{ {
frame_++, lastFrameId_++,
sharedTexture, sharedTextureWrapper,
frameInfo, frameInfo,
metaData_ metaData_
}; };
} }
return true;
} }
@@ -44,8 +44,8 @@ public:
struct Frame struct Frame
{ {
UINT frame; UINT id;
Microsoft::WRL::ComPtr<ID3D11Texture2D> texture; std::shared_ptr<class SharedTextureWrapper> texture;
DXGI_OUTDUPL_FRAME_INFO info; DXGI_OUTDUPL_FRAME_INFO info;
Metadata metaData; Metadata metaData;
}; };
@@ -67,7 +67,7 @@ private:
void InitializeDuplication(); void InitializeDuplication();
void CheckUnityAdapter(); void CheckUnityAdapter();
bool Duplicate(UINT timeout); void Duplicate(UINT timeout);
void UpdateCursor( void UpdateCursor(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture, const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture,
@@ -82,7 +82,7 @@ private:
std::shared_ptr<class IsolatedD3D11Device> device_; std::shared_ptr<class IsolatedD3D11Device> device_;
Microsoft::WRL::ComPtr<IDXGIOutputDuplication> dupl_; Microsoft::WRL::ComPtr<IDXGIOutputDuplication> dupl_;
Frame lastFrame_; Frame lastFrame_;
UINT frame_ = 0; UINT lastFrameId_ = 0;
volatile bool shouldRun_ = false; volatile bool shouldRun_ = false;
std::thread thread_; std::thread thread_;
@@ -71,15 +71,19 @@ void Monitor::Finalize()
void Monitor::Render() void Monitor::Render()
{ {
const auto& frame = duplicator_->GetLastFrame(); const auto& frame = duplicator_->GetLastFrame();
const auto& texture = frame.texture;
if (!texture) return; if (frame.id == lastFrameId_) return;
lastFrameId_ = frame.id;
const auto& sharedTextureWrapper = frame.texture;
auto sharedTexture = sharedTextureWrapper->Lock();
if (!sharedTexture) return;
ScopedReleaser releaser([&] { sharedTextureWrapper->Unlock(); });
// Get texture
if (unityTexture_) if (unityTexture_)
{ {
D3D11_TEXTURE2D_DESC srcDesc, dstDesc; D3D11_TEXTURE2D_DESC srcDesc, dstDesc;
texture->GetDesc(&srcDesc); sharedTexture->GetDesc(&srcDesc);
unityTexture_->GetDesc(&dstDesc); unityTexture_->GetDesc(&dstDesc);
if (srcDesc.Width != dstDesc.Width || if (srcDesc.Width != dstDesc.Width ||
srcDesc.Height != dstDesc.Height) srcDesc.Height != dstDesc.Height)
@@ -93,7 +97,7 @@ void Monitor::Render()
{ {
ComPtr<ID3D11DeviceContext> context; ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context); GetDevice()->GetImmediateContext(&context);
context->CopyResource(unityTexture_, texture.Get()); context->CopyResource(unityTexture_, sharedTexture.Get());
} }
} }
@@ -70,6 +70,7 @@ private:
MONITORINFOEX monitorInfo_; MONITORINFOEX monitorInfo_;
std::shared_ptr<class Duplicator> duplicator_; std::shared_ptr<class Duplicator> duplicator_;
UINT lastFrameId_ = -1;
ID3D11Texture2D* unityTexture_ = nullptr; ID3D11Texture2D* unityTexture_ = nullptr;
Microsoft::WRL::ComPtr<ID3D11Texture2D> textureForGetPixels_; Microsoft::WRL::ComPtr<ID3D11Texture2D> textureForGetPixels_;