spearate IsolatedD3D11Device into different files #10

This commit is contained in:
hecomi
2017-01-05 19:03:28 +09:00
parent 1053b30d7e
commit 46cc789be5
6 changed files with 128 additions and 74 deletions
@@ -0,0 +1,96 @@
#pragma once
#include <queue>
#include <d3d11.h>
#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<IDXGIAdapter>& 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<ID3D11Device> IsolatedD3D11Device::GetDevice()
{
return device_;
}
ComPtr<ID3D11Texture2D> IsolatedD3D11Device::GetCompatibleSharedTexture(const ComPtr<ID3D11Texture2D>& 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_;
}
@@ -0,0 +1,22 @@
#pragma once
#include <d3d11.h>
#include <wrl/client.h>
// Thraed safe self created ID3D11Device from specified adapter
class IsolatedD3D11Device
{
public:
IsolatedD3D11Device();
~IsolatedD3D11Device();
HRESULT Create(const Microsoft::WRL::ComPtr<IDXGIAdapter>& adapter);
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
Microsoft::WRL::ComPtr<ID3D11Texture2D> GetCompatibleSharedTexture(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src);
private:
Microsoft::WRL::ComPtr<ID3D11Device> device_;
Microsoft::WRL::ComPtr<ID3D11Texture2D> cachedTexture_;
};
@@ -1,85 +1,15 @@
#include <d3d11.h>
#include <ShellScalingAPI.h>
#include <queue>
#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<ID3D11Device> m_device;
ComPtr<ID3D11Texture2D> m_copyTarget;
public:
ComPtr<ID3D11Device> GetDevice()const { return m_device; }
bool CreateDevice(const ComPtr<IDXGIAdapter> &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<ID3D11Texture2D> CreateCopyTargetWithSharedFlag(const ComPtr<ID3D11Texture2D> &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<IsolatedD3D11Device>();
m_textureQueue = std::make_shared<TextureQueue>();
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;
}
@@ -141,6 +141,7 @@ copy /Y "$(SolutionDir)$(Platform)\$(Configuration)\$(TargetName).pdb" "$(Soluti
<ItemGroup>
<ClCompile Include="Common.cpp" />
<ClCompile Include="Debug.cpp" />
<ClCompile Include="Device.cpp" />
<ClCompile Include="MonitorManager.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Monitor.cpp" />
@@ -149,6 +150,7 @@ copy /Y "$(SolutionDir)$(Platform)\$(Configuration)\$(TargetName).pdb" "$(Soluti
<ItemGroup>
<ClInclude Include="Common.h" />
<ClInclude Include="Debug.h" />
<ClInclude Include="Device.h" />
<ClInclude Include="MonitorManager.h" />
<ClInclude Include="include\IUnityGraphics.h" />
<ClInclude Include="include\IUnityGraphicsD3D11.h" />
@@ -20,6 +20,7 @@
<ClInclude Include="Common.h" />
<ClInclude Include="MonitorManager.h" />
<ClInclude Include="Debug.h" />
<ClInclude Include="Device.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Monitor.cpp" />
@@ -28,5 +29,6 @@
<ClCompile Include="MonitorManager.cpp" />
<ClCompile Include="Common.cpp" />
<ClCompile Include="Debug.cpp" />
<ClCompile Include="Device.cpp" />
</ItemGroup>
</Project>