mirror of
https://github.com/gnif/LookingGlass.git
synced 2024-11-22 05:27:20 +00:00
[host] added format converter class
This commit is contained in:
parent
58c3b37e49
commit
2019766989
@ -21,12 +21,13 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#include <stdint.h>
|
||||
|
||||
#define KVMFR_HEADER_MAGIC "[[KVMFR]]"
|
||||
#define KVMFR_HEADER_VERSION 6
|
||||
#define KVMFR_HEADER_VERSION 7
|
||||
|
||||
typedef enum FrameType
|
||||
{
|
||||
FRAME_TYPE_INVALID ,
|
||||
FRAME_TYPE_ARGB , // ABGR interleaved: A,R,G,B 32bpp
|
||||
FRAME_TYPE_NV12 , // NV12
|
||||
FRAME_TYPE_H264 , // H264 compressed
|
||||
FRAME_TYPE_MAX , // sentinel value
|
||||
}
|
||||
|
@ -120,6 +120,7 @@ bool DXGI::Initialize(CaptureOptions * options)
|
||||
D3D_FEATURE_LEVEL_9_1
|
||||
};
|
||||
|
||||
#define DEBUG 1
|
||||
#if DEBUG
|
||||
#define CREATE_FLAGS (D3D11_CREATE_DEVICE_DEBUG)
|
||||
#else
|
||||
@ -147,30 +148,29 @@ bool DXGI::Initialize(CaptureOptions * options)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool h264 = false;
|
||||
|
||||
m_frameType = FRAME_TYPE_ARGB;
|
||||
for(CaptureOptions::const_iterator it = m_options->cbegin(); it != m_options->cend(); ++it)
|
||||
{
|
||||
if (_stricmp(*it, "h264") == 0) h264 = true;
|
||||
if (_stricmp(*it, "h264") == 0) m_frameType = FRAME_TYPE_H264;
|
||||
if (_stricmp(*it, "nv12") == 0) m_frameType = FRAME_TYPE_NV12;
|
||||
}
|
||||
|
||||
if (h264)
|
||||
{
|
||||
if (m_frameType == FRAME_TYPE_H264)
|
||||
DEBUG_WARN("Enabling experimental H.264 compression");
|
||||
m_frameType = FRAME_TYPE_H264;
|
||||
if (!InitH264Capture())
|
||||
{
|
||||
DeInitialize();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
bool ok = false;
|
||||
switch (m_frameType)
|
||||
{
|
||||
m_frameType = FRAME_TYPE_ARGB;
|
||||
if (!InitRawCapture())
|
||||
{
|
||||
DeInitialize();
|
||||
return false;
|
||||
}
|
||||
case FRAME_TYPE_ARGB: ok = InitRawCapture (); break;
|
||||
case FRAME_TYPE_NV12: ok = InitNV12Capture(); break;
|
||||
case FRAME_TYPE_H264: ok = InitH264Capture(); break;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
DeInitialize();
|
||||
return false;
|
||||
}
|
||||
|
||||
IDXGIDevicePtr dxgi;
|
||||
@ -231,15 +231,45 @@ bool DXGI::InitRawCapture()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DXGI::InitH264Capture()
|
||||
bool DXGI::InitNV12Capture()
|
||||
{
|
||||
m_h264 = new MFT::H264();
|
||||
if (!m_h264->Initialize(m_device, m_width, m_height))
|
||||
D3D11_TEXTURE2D_DESC texDesc;
|
||||
ZeroMemory(&texDesc, sizeof(texDesc));
|
||||
texDesc.Width = m_width;
|
||||
texDesc.Height = m_height;
|
||||
texDesc.MipLevels = 1;
|
||||
texDesc.ArraySize = 1;
|
||||
texDesc.SampleDesc.Count = 1;
|
||||
texDesc.SampleDesc.Quality = 0;
|
||||
texDesc.Usage = D3D11_USAGE_STAGING;
|
||||
texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
texDesc.BindFlags = 0;
|
||||
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
texDesc.MiscFlags = 0;
|
||||
|
||||
HRESULT status = m_device->CreateTexture2D(&texDesc, NULL, &m_texture);
|
||||
if (FAILED(status))
|
||||
{
|
||||
delete m_h264;
|
||||
m_h264 = NULL;
|
||||
DEBUG_WINERROR("Failed to create texture", status);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_textureConverter = new TextureConverter();
|
||||
if (!m_textureConverter->Initialize(m_deviceContext, m_device, m_width, m_height, DXGI_FORMAT_NV12))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DXGI::InitH264Capture()
|
||||
{
|
||||
m_textureConverter = new TextureConverter();
|
||||
if (!m_textureConverter->Initialize(m_deviceContext, m_device, m_width, m_height, DXGI_FORMAT_NV12))
|
||||
return false;
|
||||
|
||||
m_h264 = new MFT::H264();
|
||||
if (!m_h264->Initialize(m_device, m_width, m_height))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -252,6 +282,12 @@ void DXGI::DeInitialize()
|
||||
m_h264 = NULL;
|
||||
}
|
||||
|
||||
if (m_textureConverter)
|
||||
{
|
||||
delete m_textureConverter;
|
||||
m_textureConverter = NULL;
|
||||
}
|
||||
|
||||
ReleaseFrame();
|
||||
|
||||
if (m_pointer)
|
||||
@ -500,6 +536,57 @@ GrabStatus Capture::DXGI::GrabFrameRaw(FrameInfo & frame, struct CursorInfo & cu
|
||||
return GRAB_STATUS_OK;
|
||||
}
|
||||
|
||||
GrabStatus Capture::DXGI::GrabFrameNV12(struct FrameInfo & frame, struct CursorInfo & cursor)
|
||||
{
|
||||
GrabStatus result;
|
||||
ID3D11Texture2DPtr texture;
|
||||
bool timeout;
|
||||
|
||||
result = GrabFrameTexture(frame, cursor, texture, timeout);
|
||||
if (timeout)
|
||||
return GRAB_STATUS_TIMEOUT;
|
||||
|
||||
if (result != GRAB_STATUS_OK)
|
||||
return result;
|
||||
|
||||
if (!m_textureConverter->Convert(texture))
|
||||
{
|
||||
SafeRelease(&texture);
|
||||
return GRAB_STATUS_ERROR;
|
||||
}
|
||||
|
||||
if (m_surfaceMapped)
|
||||
{
|
||||
m_deviceContext->Unmap(m_texture, 0);
|
||||
m_surfaceMapped = false;
|
||||
}
|
||||
|
||||
m_deviceContext->CopyResource(m_texture, texture);
|
||||
SafeRelease(&texture);
|
||||
|
||||
result = ReleaseFrame();
|
||||
if (result != GRAB_STATUS_OK)
|
||||
return result;
|
||||
|
||||
HRESULT status;
|
||||
status = m_deviceContext->Map(m_texture, 0, D3D11_MAP_READ, 0, &m_mapping);
|
||||
if (FAILED(status))
|
||||
{
|
||||
DEBUG_WINERROR("Failed to map the texture", status);
|
||||
DeInitialize();
|
||||
return GRAB_STATUS_ERROR;
|
||||
}
|
||||
m_surfaceMapped = true;
|
||||
|
||||
frame.pitch = m_mapping.RowPitch;
|
||||
frame.stride = m_mapping.RowPitch >> 2;
|
||||
|
||||
const unsigned int size = m_height * m_mapping.RowPitch;
|
||||
memcpySSE(frame.buffer, m_mapping.pData, LG_MIN(size, frame.bufferSize));
|
||||
|
||||
return GRAB_STATUS_OK;
|
||||
}
|
||||
|
||||
GrabStatus Capture::DXGI::GrabFrameH264(struct FrameInfo & frame, struct CursorInfo & cursor)
|
||||
{
|
||||
while(true)
|
||||
@ -515,22 +602,24 @@ GrabStatus Capture::DXGI::GrabFrameH264(struct FrameInfo & frame, struct CursorI
|
||||
bool timeout;
|
||||
|
||||
result = GrabFrameTexture(frame, cursor, texture, timeout);
|
||||
if (timeout)
|
||||
{
|
||||
// FIXME: this is wrong, we need to encode the last frame again
|
||||
return GRAB_STATUS_TIMEOUT;
|
||||
}
|
||||
|
||||
if (result != GRAB_STATUS_OK)
|
||||
{
|
||||
ReleaseFrame();
|
||||
return result;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!timeout)
|
||||
{
|
||||
if (!m_textureConverter->Convert(texture))
|
||||
{
|
||||
SafeRelease(&texture);
|
||||
return GRAB_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_h264->ProvideFrame(texture))
|
||||
return GRAB_STATUS_ERROR;
|
||||
|
||||
SafeRelease(&texture);
|
||||
ReleaseFrame();
|
||||
}
|
||||
|
||||
@ -549,8 +638,10 @@ GrabStatus DXGI::GrabFrame(struct FrameInfo & frame, struct CursorInfo & cursor)
|
||||
frame.width = m_width;
|
||||
frame.height = m_height;
|
||||
|
||||
if (m_frameType == FRAME_TYPE_H264)
|
||||
return GrabFrameH264(frame, cursor);
|
||||
else
|
||||
return GrabFrameRaw(frame, cursor);
|
||||
switch (m_frameType)
|
||||
{
|
||||
case FRAME_TYPE_ARGB: return GrabFrameRaw (frame, cursor);
|
||||
case FRAME_TYPE_NV12: return GrabFrameNV12(frame, cursor);
|
||||
case FRAME_TYPE_H264: return GrabFrameH264(frame, cursor);
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
#include "ICapture.h"
|
||||
#include "Com.h"
|
||||
#include "TextureConverter.h"
|
||||
#include "MFT/H264.h"
|
||||
|
||||
#define W32_LEAN_AND_MEAN
|
||||
@ -59,11 +60,13 @@ namespace Capture
|
||||
|
||||
private:
|
||||
bool InitRawCapture();
|
||||
bool InitNV12Capture();
|
||||
bool InitH264Capture();
|
||||
|
||||
GrabStatus GrabFrameTexture(struct FrameInfo & frame, struct CursorInfo & cursor, ID3D11Texture2DPtr & texture, bool & timeout);
|
||||
GrabStatus ReleaseFrame();
|
||||
GrabStatus GrabFrameRaw (struct FrameInfo & frame, struct CursorInfo & cursor);
|
||||
GrabStatus GrabFrameNV12 (struct FrameInfo & frame, struct CursorInfo & cursor);
|
||||
GrabStatus GrabFrameH264 (struct FrameInfo & frame, struct CursorInfo & cursor);
|
||||
|
||||
CaptureOptions * m_options;
|
||||
@ -83,6 +86,7 @@ namespace Capture
|
||||
ID3D11Texture2DPtr m_texture;
|
||||
D3D11_MAPPED_SUBRESOURCE m_mapping;
|
||||
bool m_surfaceMapped;
|
||||
TextureConverter * m_textureConverter;
|
||||
MFT::H264 * m_h264;
|
||||
|
||||
BYTE * m_pointer;
|
||||
|
49
host/Com.h
49
host/Com.h
@ -23,27 +23,36 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#include <d3d11.h>
|
||||
#include <mftransform.h>
|
||||
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIFactory1 , __uuidof(IDXGIFactory1 ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11Device , __uuidof(ID3D11Device ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11DeviceContext , __uuidof(ID3D11DeviceContext ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIDevice , __uuidof(IDXGIDevice ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIOutput1 , __uuidof(IDXGIOutput1 ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIOutput , __uuidof(IDXGIOutput ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIAdapter1 , __uuidof(IDXGIAdapter1 ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIOutputDuplication, __uuidof(IDXGIOutputDuplication));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11Texture2D , __uuidof(ID3D11Texture2D ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIResource , __uuidof(IDXGIResource ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIFactory1 , __uuidof(IDXGIFactory1 ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11Device , __uuidof(ID3D11Device ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11DeviceContext , __uuidof(ID3D11DeviceContext ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIDevice , __uuidof(IDXGIDevice ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIOutput1 , __uuidof(IDXGIOutput1 ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIOutput , __uuidof(IDXGIOutput ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIAdapter1 , __uuidof(IDXGIAdapter1 ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIOutputDuplication , __uuidof(IDXGIOutputDuplication ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11Texture2D , __uuidof(ID3D11Texture2D ));
|
||||
_COM_SMARTPTR_TYPEDEF(IDXGIResource , __uuidof(IDXGIResource ));
|
||||
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D10Multithread , __uuidof(ID3D10Multithread ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFActivate , __uuidof(IMFActivate ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFAttributes , __uuidof(IMFAttributes ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFDXGIDeviceManager , __uuidof(IMFDXGIDeviceManager ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFTransform , __uuidof(IMFTransform ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFMediaEventGenerator, __uuidof(IMFMediaEventGenerator));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFMediaType , __uuidof(IMFMediaType ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFSample , __uuidof(IMFSample ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFMediaBuffer , __uuidof(IMFMediaBuffer ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMF2DBuffer , __uuidof(IMF2DBuffer ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D10Multithread , __uuidof(ID3D10Multithread ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFActivate , __uuidof(IMFActivate ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFAttributes , __uuidof(IMFAttributes ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFDXGIDeviceManager , __uuidof(IMFDXGIDeviceManager ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFTransform , __uuidof(IMFTransform ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFMediaEventGenerator , __uuidof(IMFMediaEventGenerator ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFMediaType , __uuidof(IMFMediaType ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFSample , __uuidof(IMFSample ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMFMediaBuffer , __uuidof(IMFMediaBuffer ));
|
||||
_COM_SMARTPTR_TYPEDEF(IMF2DBuffer , __uuidof(IMF2DBuffer ));
|
||||
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11RenderTargetView , __uuidof(ID3D11RenderTargetView ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11ShaderResourceView, __uuidof(ID3D11ShaderResourceView));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11DepthStencilView , __uuidof(ID3D11DepthStencilView ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11InputLayout , __uuidof(ID3D11InputLayout ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11VertexShader , __uuidof(ID3D11VertexShader ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11PixelShader , __uuidof(ID3D11PixelShader ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11SamplerState , __uuidof(ID3D11SamplerState ));
|
||||
_COM_SMARTPTR_TYPEDEF(ID3D11Buffer , __uuidof(ID3D11Buffer ));
|
||||
|
||||
|
||||
template <class T> void SafeRelease(T **ppT)
|
||||
|
@ -58,7 +58,6 @@ MFT::H264::~H264()
|
||||
bool MFT::H264::Initialize(ID3D11DevicePtr device, unsigned int width, unsigned int height)
|
||||
{
|
||||
DeInitialize();
|
||||
|
||||
HRESULT status;
|
||||
|
||||
MFT_REGISTER_TYPE_INFO typeInfo;
|
||||
@ -348,12 +347,7 @@ bool MFT::H264::GetFrame(void * buffer, const size_t bufferSize, unsigned int &
|
||||
}
|
||||
|
||||
DWORD outStatus;
|
||||
MFT_OUTPUT_DATA_BUFFER outDataBuffer;
|
||||
outDataBuffer.dwStreamID = 0;
|
||||
outDataBuffer.dwStatus = 0;
|
||||
outDataBuffer.pEvents = NULL;
|
||||
outDataBuffer.pSample = NULL;
|
||||
|
||||
MFT_OUTPUT_DATA_BUFFER outDataBuffer = { 0 };
|
||||
status = m_mfTransform->ProcessOutput(0, 1, &outDataBuffer, &outStatus);
|
||||
if (FAILED(status))
|
||||
{
|
||||
@ -362,17 +356,17 @@ bool MFT::H264::GetFrame(void * buffer, const size_t bufferSize, unsigned int &
|
||||
}
|
||||
|
||||
IMFMediaBufferPtr mb;
|
||||
MFCreateAlignedMemoryBuffer((DWORD)bufferSize, MF_128_BYTE_ALIGNMENT, &mb);
|
||||
outDataBuffer.pSample->CopyToBuffer(mb);
|
||||
SafeRelease(&outDataBuffer.pEvents);
|
||||
SafeRelease(&outDataBuffer.pSample);
|
||||
outDataBuffer.pSample->ConvertToContiguousBuffer(&mb);
|
||||
|
||||
BYTE *pixels;
|
||||
DWORD maxLen, curLen;
|
||||
mb->Lock(&pixels, &maxLen, &curLen);
|
||||
memcpySSE(buffer, pixels, curLen);
|
||||
mb->Lock(&pixels, NULL, &curLen);
|
||||
memcpy(buffer, pixels, curLen);
|
||||
mb->Unlock();
|
||||
|
||||
SafeRelease(&mb);
|
||||
SafeRelease(&outDataBuffer.pSample);
|
||||
SafeRelease(&outDataBuffer.pEvents);
|
||||
|
||||
dataLen = curLen;
|
||||
return true;
|
||||
|
3163
host/Shaders/BGRAtoNV12.h
Normal file
3163
host/Shaders/BGRAtoNV12.h
Normal file
File diff suppressed because it is too large
Load Diff
26
host/Shaders/BGRAtoNV12.hlsl
Normal file
26
host/Shaders/BGRAtoNV12.hlsl
Normal file
@ -0,0 +1,26 @@
|
||||
Texture2D texTexture;
|
||||
SamplerState texSampler;
|
||||
|
||||
struct VS
|
||||
{
|
||||
float4 pos : SV_Position;
|
||||
float2 tex : TEXCOORD;
|
||||
};
|
||||
|
||||
float4 RGBtoYUV(float4 rgba)
|
||||
{
|
||||
float4 yuva;
|
||||
yuva.r = rgba.r * 0.2126 + 0.7152 * rgba.g + 0.0722 * rgba.b;
|
||||
yuva.g = (rgba.b - yuva.r) / 1.8556;
|
||||
yuva.b = (rgba.r - yuva.r) / 1.5748;
|
||||
yuva.a = rgba.a;
|
||||
yuva.gb += 0.5;
|
||||
return yuva;
|
||||
}
|
||||
|
||||
float4 main(VS input) : SV_TARGET
|
||||
{
|
||||
const float4 rgba = texTexture.Sample(texSampler, input.tex);
|
||||
const float4 yuva = RGBtoYUV(rgba);
|
||||
return yuva;
|
||||
}
|
2232
host/Shaders/Vertex.h
Normal file
2232
host/Shaders/Vertex.h
Normal file
File diff suppressed because it is too large
Load Diff
16
host/Shaders/Vertex.hlsl
Normal file
16
host/Shaders/Vertex.hlsl
Normal file
@ -0,0 +1,16 @@
|
||||
struct VS
|
||||
{
|
||||
float4 pos : POSITION;
|
||||
float2 tex : TEXCOORD;
|
||||
};
|
||||
|
||||
struct PS
|
||||
{
|
||||
float4 pos : SV_Position;
|
||||
float2 tex : TEXCOORD;
|
||||
};
|
||||
|
||||
PS main(VS input)
|
||||
{
|
||||
return input;
|
||||
}
|
344
host/TextureConverter.cpp
Normal file
344
host/TextureConverter.cpp
Normal file
@ -0,0 +1,344 @@
|
||||
/*
|
||||
Looking Glass - KVM FrameRelay (KVMFR) Client
|
||||
Copyright (C) 2017 Geoffrey McRae <geoff@hostfission.com>
|
||||
https://looking-glass.hostfission.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "TextureConverter.h"
|
||||
#include "common\debug.h"
|
||||
|
||||
#include "Shaders\Vertex.h"
|
||||
#include "Shaders\BGRAtoNV12.h"
|
||||
|
||||
TextureConverter::TextureConverter()
|
||||
{
|
||||
}
|
||||
|
||||
TextureConverter::~TextureConverter()
|
||||
{
|
||||
DeInitialize();
|
||||
}
|
||||
|
||||
bool TextureConverter::Initialize(
|
||||
ID3D11DeviceContextPtr deviceContext,
|
||||
ID3D11DevicePtr device,
|
||||
const unsigned int width,
|
||||
const unsigned int height,
|
||||
DXGI_FORMAT format
|
||||
)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_TEXTURE2D_DESC texDesc;
|
||||
D3D11_RENDER_TARGET_VIEW_DESC viewDesc;
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC shaderDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
|
||||
m_deviceContext = deviceContext;
|
||||
m_device = device;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_format = format;
|
||||
|
||||
switch (format)
|
||||
{
|
||||
case DXGI_FORMAT_NV12:
|
||||
result = device->CreatePixelShader(g_BGRAtoNV12, sizeof(g_BGRAtoNV12), NULL, &m_pixelShader);
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unsupported format");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the pixel shader");
|
||||
return false;
|
||||
}
|
||||
|
||||
const D3D11_INPUT_ELEMENT_DESC inputDesc[] =
|
||||
{
|
||||
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT , 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
|
||||
};
|
||||
|
||||
result = device->CreateInputLayout(inputDesc, _countof(inputDesc), g_Vertex, sizeof(g_Vertex), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the input layout");
|
||||
return false;
|
||||
}
|
||||
|
||||
result = device->CreateVertexShader(g_Vertex, sizeof(g_Vertex), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the vertex shader");
|
||||
return false;
|
||||
}
|
||||
|
||||
ZeroMemory(&texDesc , sizeof(texDesc ));
|
||||
ZeroMemory(&viewDesc , sizeof(viewDesc ));
|
||||
ZeroMemory(&shaderDesc , sizeof(shaderDesc ));
|
||||
ZeroMemory(&samplerDesc, sizeof(samplerDesc));
|
||||
|
||||
texDesc.Width = width;
|
||||
texDesc.Height = height;
|
||||
texDesc.MipLevels = 1;
|
||||
texDesc.ArraySize = 1;
|
||||
texDesc.SampleDesc.Count = 1;
|
||||
texDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
texDesc.CPUAccessFlags = 0;
|
||||
texDesc.MiscFlags = 0;
|
||||
|
||||
texDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
||||
texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
||||
result = device->CreateTexture2D(&texDesc, NULL, &m_depthTexture);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the depth/stencil texture");
|
||||
return false;
|
||||
}
|
||||
|
||||
result = device->CreateDepthStencilView(m_depthTexture, NULL, &m_depthView);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the depth stencil view");
|
||||
return false;
|
||||
}
|
||||
|
||||
texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
texDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
|
||||
result = device->CreateTexture2D(&texDesc, NULL, &m_targetTexture);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the render texture");
|
||||
return false;
|
||||
}
|
||||
|
||||
viewDesc.Format = texDesc.Format;
|
||||
viewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
||||
viewDesc.Texture2D.MipSlice = 0;
|
||||
result = device->CreateRenderTargetView(m_targetTexture, &viewDesc, &m_renderView);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create the render view");
|
||||
return false;
|
||||
}
|
||||
|
||||
shaderDesc.Format = texDesc.Format;
|
||||
shaderDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
shaderDesc.Texture2D.MostDetailedMip = 0;
|
||||
shaderDesc.Texture2D.MipLevels = 1;
|
||||
|
||||
result = device->CreateShaderResourceView(m_targetTexture, &shaderDesc, &m_shaderView);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create shader resource view");
|
||||
return false;
|
||||
}
|
||||
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
|
||||
samplerDesc.BorderColor[0] = 1.0f;
|
||||
samplerDesc.BorderColor[1] = 1.0f;
|
||||
samplerDesc.BorderColor[2] = 1.0f;
|
||||
samplerDesc.MinLOD = -FLT_MAX;
|
||||
samplerDesc.MaxLOD = FLT_MAX;
|
||||
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_samplerState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create sampler state");
|
||||
return false;
|
||||
}
|
||||
|
||||
return IntializeBuffers();
|
||||
}
|
||||
|
||||
bool TextureConverter::IntializeBuffers()
|
||||
{
|
||||
struct VS_INPUT * verticies;
|
||||
unsigned long * indicies;
|
||||
HRESULT result;
|
||||
D3D11_BUFFER_DESC vertexDesc, indexDesc;
|
||||
D3D11_SUBRESOURCE_DATA vertexData, indexData;
|
||||
|
||||
m_vertexCount = 4;
|
||||
m_indexCount = 4;
|
||||
|
||||
verticies = new struct VS_INPUT[m_vertexCount];
|
||||
if (!verticies)
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("new failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
indicies = new unsigned long[m_indexCount];
|
||||
if (!indicies)
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("new failure");
|
||||
return false;
|
||||
}
|
||||
|
||||
verticies[0].pos = DirectX::XMFLOAT3(-1.0f, -1.0f, 0.5f); //BL
|
||||
verticies[1].pos = DirectX::XMFLOAT3(-1.0f, 1.0f, 0.5f); //TL
|
||||
verticies[2].pos = DirectX::XMFLOAT3( 1.0f, -1.0f, 0.5f); //BR
|
||||
verticies[3].pos = DirectX::XMFLOAT3( 1.0f, 1.0f, 0.5f); //TR
|
||||
verticies[0].tex = DirectX::XMFLOAT2(0.0f, 1.0f);
|
||||
verticies[1].tex = DirectX::XMFLOAT2(0.0f, 0.0f);
|
||||
verticies[2].tex = DirectX::XMFLOAT2(1.0f, 1.0f);
|
||||
verticies[3].tex = DirectX::XMFLOAT2(1.0f, 0.0f);
|
||||
indicies[0] = 0;
|
||||
indicies[1] = 1;
|
||||
indicies[2] = 2;
|
||||
indicies[3] = 3;
|
||||
|
||||
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
vertexDesc.ByteWidth = sizeof(struct VS_INPUT) * m_vertexCount;
|
||||
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
vertexDesc.CPUAccessFlags = 0;
|
||||
vertexDesc.MiscFlags = 0;
|
||||
vertexDesc.StructureByteStride = 0;
|
||||
|
||||
vertexData.pSysMem = verticies;
|
||||
vertexData.SysMemPitch = 0;
|
||||
vertexData.SysMemSlicePitch = 0;
|
||||
|
||||
result = m_device->CreateBuffer(&vertexDesc, &vertexData, &m_vertexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
delete[] indicies;
|
||||
delete[] verticies;
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create vertex buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
indexDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
indexDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
|
||||
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
indexDesc.CPUAccessFlags = 0;
|
||||
indexDesc.MiscFlags = 0;
|
||||
indexDesc.StructureByteStride = 0;
|
||||
|
||||
indexData.pSysMem = indicies;
|
||||
indexData.SysMemPitch = 0;
|
||||
indexData.SysMemSlicePitch = 0;
|
||||
|
||||
result = m_device->CreateBuffer(&indexDesc, &indexData, &m_indexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
delete[] indicies;
|
||||
delete[] verticies;
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create index buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
delete[] indicies;
|
||||
delete[] verticies;
|
||||
return true;
|
||||
}
|
||||
|
||||
void TextureConverter::DeInitialize()
|
||||
{
|
||||
SafeRelease(&m_samplerState );
|
||||
SafeRelease(&m_indexBuffer );
|
||||
SafeRelease(&m_vertexBuffer );
|
||||
SafeRelease(&m_shaderView );
|
||||
SafeRelease(&m_renderView );
|
||||
SafeRelease(&m_depthTexture );
|
||||
SafeRelease(&m_targetTexture);
|
||||
SafeRelease(&m_vertexShader );
|
||||
SafeRelease(&m_pixelShader );
|
||||
SafeRelease(&m_layout );
|
||||
}
|
||||
|
||||
bool TextureConverter::Convert(ID3D11Texture2DPtr &texture)
|
||||
{
|
||||
unsigned int stride;
|
||||
unsigned int offset;
|
||||
float color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
HRESULT result;
|
||||
D3D11_TEXTURE2D_DESC texDesc;
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC shaderDesc;
|
||||
ID3D11ShaderResourceViewPtr textureView;
|
||||
texture->GetDesc(&texDesc);
|
||||
shaderDesc.Format = texDesc.Format;
|
||||
shaderDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
shaderDesc.Texture2D.MostDetailedMip = 0;
|
||||
shaderDesc.Texture2D.MipLevels = 1;
|
||||
|
||||
result = m_device->CreateShaderResourceView(texture, &shaderDesc, &textureView);
|
||||
if (FAILED(result))
|
||||
{
|
||||
DeInitialize();
|
||||
DEBUG_ERROR("Failed to create shader resource view");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
ID3D11Buffer *buffers [] = { m_vertexBuffer.GetInterfacePtr() };
|
||||
ID3D11RenderTargetView *renderViews [] = { m_renderView .GetInterfacePtr() };
|
||||
ID3D11SamplerState *samplerStates[] = { m_samplerState.GetInterfacePtr() };
|
||||
ID3D11ShaderResourceView *shaderViews [] = { textureView .GetInterfacePtr() };
|
||||
D3D11_VIEWPORT viewPorts [] = { {
|
||||
0.0f, 0.0f,
|
||||
(float)m_width, (float)m_height,
|
||||
0.0f, 1.0f
|
||||
} };
|
||||
|
||||
m_deviceContext->OMSetRenderTargets(1, renderViews, NULL);
|
||||
m_deviceContext->ClearRenderTargetView(m_renderView, color);
|
||||
m_deviceContext->ClearDepthStencilView(m_depthView , D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
|
||||
|
||||
stride = sizeof(VS_INPUT);
|
||||
offset = 0;
|
||||
|
||||
m_deviceContext->RSSetViewports (_countof(viewPorts), viewPorts);
|
||||
m_deviceContext->IASetInputLayout (m_layout);
|
||||
m_deviceContext->IASetVertexBuffers (0, _countof(buffers), buffers, &stride, &offset);
|
||||
m_deviceContext->IASetIndexBuffer (m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
|
||||
m_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
||||
m_deviceContext->VSSetShader (m_vertexShader, NULL, 0);
|
||||
m_deviceContext->PSSetSamplers (0, _countof(samplerStates), samplerStates);
|
||||
m_deviceContext->PSSetShaderResources (0, _countof(shaderViews ), shaderViews );
|
||||
m_deviceContext->PSSetShader (m_pixelShader , NULL, 0);
|
||||
|
||||
m_deviceContext->DrawIndexed(m_indexCount, 0, 0);
|
||||
|
||||
SafeRelease(&textureView);
|
||||
SafeRelease(&texture);
|
||||
texture = m_targetTexture;
|
||||
return true;
|
||||
}
|
73
host/TextureConverter.h
Normal file
73
host/TextureConverter.h
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
Looking Glass - KVM FrameRelay (KVMFR) Client
|
||||
Copyright (C) 2017 Geoffrey McRae <geoff@hostfission.com>
|
||||
https://looking-glass.hostfission.com
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Com.h"
|
||||
|
||||
#include <DirectXMath.h>
|
||||
|
||||
class TextureConverter
|
||||
{
|
||||
public:
|
||||
TextureConverter();
|
||||
~TextureConverter();
|
||||
|
||||
bool Initialize(
|
||||
ID3D11DeviceContextPtr deviceContext,
|
||||
ID3D11DevicePtr device,
|
||||
const unsigned int width,
|
||||
const unsigned int height,
|
||||
DXGI_FORMAT format
|
||||
);
|
||||
|
||||
void DeInitialize();
|
||||
|
||||
bool Convert(ID3D11Texture2DPtr &texture);
|
||||
|
||||
private:
|
||||
struct VS_INPUT
|
||||
{
|
||||
DirectX::XMFLOAT3 pos;
|
||||
DirectX::XMFLOAT2 tex;
|
||||
};
|
||||
|
||||
bool IntializeBuffers();
|
||||
|
||||
ID3D11DeviceContextPtr m_deviceContext;
|
||||
ID3D11DevicePtr m_device;
|
||||
unsigned int m_width, m_height;
|
||||
DXGI_FORMAT m_format;
|
||||
|
||||
ID3D11Texture2DPtr m_targetTexture;
|
||||
ID3D11Texture2DPtr m_depthTexture;
|
||||
ID3D11DepthStencilViewPtr m_depthView;
|
||||
ID3D11RenderTargetViewPtr m_renderView;
|
||||
ID3D11ShaderResourceViewPtr m_shaderView;
|
||||
ID3D11InputLayoutPtr m_layout;
|
||||
ID3D11VertexShaderPtr m_vertexShader;
|
||||
ID3D11PixelShaderPtr m_pixelShader;
|
||||
ID3D11SamplerStatePtr m_samplerState;
|
||||
|
||||
ID3D11BufferPtr m_vertexBuffer;
|
||||
unsigned int m_vertexCount;
|
||||
ID3D11BufferPtr m_indexBuffer;
|
||||
unsigned int m_indexCount;
|
||||
};
|
||||
|
@ -337,6 +337,7 @@
|
||||
<ClCompile Include="ivshmem.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="MFT\H264.cpp" />
|
||||
<ClCompile Include="TextureConverter.cpp" />
|
||||
<ClCompile Include="Service.cpp" />
|
||||
<ClCompile Include="TraceUtil.cpp" />
|
||||
</ItemGroup>
|
||||
@ -349,6 +350,7 @@
|
||||
<ClInclude Include="ICapture.h" />
|
||||
<ClInclude Include="ivshmem.h" />
|
||||
<ClInclude Include="MFT\H264.h" />
|
||||
<ClInclude Include="TextureConverter.h" />
|
||||
<ClInclude Include="Service.h" />
|
||||
<ClInclude Include="TraceUtil.h" />
|
||||
<ClInclude Include="Util.h" />
|
||||
@ -356,6 +358,19 @@
|
||||
<ItemGroup>
|
||||
<MASM Include="..\common\memcpySSE.asm" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<FxCompile Include="Shaders\BGRAtoNV12.hlsl">
|
||||
<FileType>HLSL</FileType>
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||
<VariableName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">g_BGRAtoNV12</VariableName>
|
||||
<HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Shaders\BGRAtoNV12.h</HeaderFileOutput>
|
||||
</FxCompile>
|
||||
<FxCompile Include="Shaders\Vertex.hlsl">
|
||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
|
||||
<VariableName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">g_Vertex</VariableName>
|
||||
<HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Shaders\Vertex.h</HeaderFileOutput>
|
||||
</FxCompile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
|
||||
|
@ -25,6 +25,9 @@
|
||||
<Filter Include="Header Files\MFT">
|
||||
<UniqueIdentifier>{fead4000-1954-4480-8ee7-b817d7042761}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Shaders">
|
||||
<UniqueIdentifier>{afb06b60-4959-4447-92ec-75cc07995a8f}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ivshmem.cpp">
|
||||
@ -54,6 +57,9 @@
|
||||
<ClCompile Include="MFT\H264.cpp">
|
||||
<Filter>Source Files\MFT</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="TextureConverter.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ivshmem.h">
|
||||
@ -89,10 +95,21 @@
|
||||
<ClInclude Include="Com.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TextureConverter.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<MASM Include="..\common\memcpySSE.asm">
|
||||
<Filter>Source Files</Filter>
|
||||
</MASM>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<FxCompile Include="Shaders\BGRAtoNV12.hlsl">
|
||||
<Filter>Source Files\Shaders</Filter>
|
||||
</FxCompile>
|
||||
<FxCompile Include="Shaders\Vertex.hlsl">
|
||||
<Filter>Source Files\Shaders</Filter>
|
||||
</FxCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user