Commit 2ed7cd07 authored by Jérémy VIGNELLES's avatar Jérémy VIGNELLES
Browse files

Test without VLC, should be orange, but is blue

parent 56da0717
......@@ -38,22 +38,10 @@ struct render_context
ID3D11Device *d3device;
ID3D11DeviceContext *d3dctx;
ID3D11Texture2D *swapchain[2];
int currentSwapchainBuffer;
ID3D11RenderTargetView *swapchainRenderTarget;
/* our vertex/pixel shader */
ID3D11VertexShader *pVS;
ID3D11PixelShader *pPS;
ID3D11InputLayout *pShadersInputLayout;
UINT vertexBufferStride;
ID3D11Buffer *pVertexBuffer;
UINT quadIndexCount;
ID3D11Buffer *pIndexBuffer;
ID3D11SamplerState *samplerState;
/* texture VLC renders into */
ID3D11Texture2D *texture;
......@@ -101,52 +89,6 @@ void Resize_cb( void *opaque,
void (*report_size_change)(void *report_opaque, unsigned width, unsigned height),
void *report_opaque );
static const char *shaderStr = "\
Texture2D shaderTexture;\n\
SamplerState samplerState;\n\
struct PS_INPUT\n\
{\n\
float4 position : SV_POSITION;\n\
float4 textureCoord : TEXCOORD0;\n\
};\n\
\n\
float4 PShader(PS_INPUT In) : SV_TARGET\n\
{\n\
return shaderTexture.Sample(samplerState, In.textureCoord);\n\
}\n\
\n\
struct VS_INPUT\n\
{\n\
float4 position : POSITION;\n\
float4 textureCoord : TEXCOORD0;\n\
};\n\
\n\
struct VS_OUTPUT\n\
{\n\
float4 position : SV_POSITION;\n\
float4 textureCoord : TEXCOORD0;\n\
};\n\
\n\
VS_OUTPUT VShader(VS_INPUT In)\n\
{\n\
return In;\n\
}\n\
";
struct SHADER_INPUT {
struct {
FLOAT x;
FLOAT y;
FLOAT z;
} position;
struct {
FLOAT x;
FLOAT y;
} texture;
};
RenderAPI* CreateRenderAPI_D3D11()
{
return new RenderAPI_D3D11();
......@@ -213,246 +155,10 @@ void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInt
}
}
void RenderAPI_D3D11::CreateResources(struct render_context *ctx, ID3D11Device *d3device, ID3D11DeviceContext *d3dctx)
{
DEBUG("Entering CreateResources \n");
HRESULT hr;
assert(ctx != nullptr);
ZeroMemory(ctx, sizeof(*ctx));
InitializeCriticalSection(&ctx->sizeLock);
ctx->width = Width;
ctx->height = Height;
ctx->d3device = d3device;
ctx->d3dctx = d3dctx;
UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT; /* needed for hardware decoding */
creationFlags |= D3D11_CREATE_DEVICE_DEBUG; //TODO: remove for release mode
hr = D3D11CreateDevice(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
creationFlags,
NULL,
NULL,
D3D11_SDK_VERSION,
&ctx->d3deviceVLC,
NULL,
&ctx->d3dctxVLC);
if(FAILED(hr))
{
DEBUG("FAILED to create d3d11 device and context \n");
abort();
}
DEBUG("Configuring multithread \n");
/* The ID3D11Device must have multithread protection */
ID3D10Multithread *pMultithread;
hr = ctx->d3deviceVLC->QueryInterface(__uuidof(ID3D10Multithread), (void **)&pMultithread);
if (SUCCEEDED(hr)) {
pMultithread->SetMultithreadProtected(TRUE);
pMultithread->Release();
}
DEBUG("Creating the pseudo swapchain");
D3D11_TEXTURE2D_DESC texDesc = { 0 };
texDesc.MipLevels = 1;
texDesc.SampleDesc.Count = 1;
texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_NTHANDLE;
texDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.CPUAccessFlags = 0;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.Height = SCREEN_HEIGHT;
texDesc.Width = SCREEN_WIDTH;
for(int i = 0; i < 2; i++) {
hr = ctx->d3device->CreateTexture2D( &texDesc, NULL, &(ctx->swapchain[i]) );
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("d3device->CreateTexture2D FAILED %s \n", error.ErrorMessage());
abort();
}
}
ctx->d3device->CreateRenderTargetView(ctx->swapchain[0], NULL, &ctx->swapchainRenderTarget);
DEBUG("Compile shaders \n");
ID3D10Blob *VS, *PS, *pErrBlob;
char *err;
hr = D3DCompile(shaderStr, strlen(shaderStr),
NULL, NULL, NULL, "VShader", "vs_4_0", 0, 0, &VS, &pErrBlob);
err = pErrBlob ? (char*)pErrBlob->GetBufferPointer() : NULL;
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("D3DCompile VShader FAILED %s %s \n", error.ErrorMessage(), err);
abort();
}
hr = D3DCompile(shaderStr, strlen(shaderStr),
NULL, NULL, NULL, "PShader", "ps_4_0", 0, 0, &PS, &pErrBlob);
err = pErrBlob ? (char*)pErrBlob->GetBufferPointer() : NULL;
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("D3DCompile PShader FAILED %s %s \n", error.ErrorMessage(), err);
abort();
}
hr = ctx->d3device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &ctx->pVS);
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("CreateVertexShader FAILED %s \n", error.ErrorMessage());
abort();
}
hr = ctx->d3device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &ctx->pPS);
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("CreatePixelShader FAILED %s \n", error.ErrorMessage());
abort();
}
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{ "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},
};
hr = ctx->d3device->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &ctx->pShadersInputLayout);
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("CreateInputLayout FAILED %s \n", error.ErrorMessage());
abort();
}
SHADER_INPUT OurVertices[] =
{
{BORDER_LEFT, BORDER_BOTTOM, 0.0f, 0.0f, 1.0f},
{BORDER_RIGHT, BORDER_BOTTOM, 0.0f, 1.0f, 1.0f},
{BORDER_RIGHT, BORDER_TOP, 0.0f, 1.0f, 0.0f},
{BORDER_LEFT, BORDER_TOP, 0.0f, 0.0f, 0.0f},
};
DEBUG("Create buffers \n");
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(OurVertices);
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
DEBUG("Vertex Buffer \n");
ctx->d3device->CreateBuffer(&bd, NULL, &ctx->pVertexBuffer);
ctx->vertexBufferStride = sizeof(OurVertices[0]);
D3D11_MAPPED_SUBRESOURCE ms;
hr = ctx->d3dctx->Map(ctx->pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("Map VertexBuffer FAILED %s \n", error.ErrorMessage());
abort();
}
memcpy(ms.pData, OurVertices, sizeof(OurVertices));
ctx->d3dctx->Unmap(ctx->pVertexBuffer, NULL);
ctx->quadIndexCount = 6;
DEBUG("Index buffer \n");
D3D11_BUFFER_DESC quadDesc = { };
quadDesc.Usage = D3D11_USAGE_DYNAMIC;
quadDesc.ByteWidth = sizeof(WORD) * ctx->quadIndexCount;
quadDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
quadDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
ctx->d3device->CreateBuffer(&quadDesc, NULL, &ctx->pIndexBuffer);
ctx->d3dctx->Map(ctx->pIndexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
WORD *triangle_pos = static_cast<WORD*>(ms.pData);
triangle_pos[0] = 3;
triangle_pos[1] = 1;
triangle_pos[2] = 0;
triangle_pos[3] = 2;
triangle_pos[4] = 1;
triangle_pos[5] = 3;
ctx->d3dctx->Unmap(ctx->pIndexBuffer, NULL);
DEBUG("Create sampler \n");
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = ctx->d3device->CreateSamplerState(&sampDesc, &ctx->samplerState);
if (FAILED(hr))
{
_com_error error(hr);
DEBUG("CreateSamplerState FAILED %s \n", error.ErrorMessage());
abort();
}
DEBUG("Exiting CreateResources.\n");
}
void RenderAPI_D3D11::ReleaseResources(struct render_context *ctx)
{
DEBUG("Entering ReleaseResources.\n");
ctx->d3deviceVLC->Release();
ctx->d3dctxVLC->Release();
ctx->samplerState->Release();
ctx->textureRenderTarget->Release();
ctx->textureShaderInput->Release();
ctx->texture->Release();
ctx->pShadersInputLayout->Release();
ctx->pVS->Release();
ctx->pPS->Release();
ctx->pIndexBuffer->Release();
ctx->pVertexBuffer->Release();
ctx->swapchain[0]->Release();
ctx->swapchain[1]->Release();
ctx->swapchainRenderTarget->Release();
ctx->d3dctx->Release();
ctx->d3device->Release();
}
bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libvlc_video_output_cfg_t *out )
void Update(render_context* ctx, UINT width, UINT height)
{
DEBUG("Entering UpdateOutput_cb.\n");
struct render_context *ctx = static_cast<struct render_context *>( opaque );
assert(ctx != nullptr);
HRESULT hr;
DXGI_FORMAT renderFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
HRESULT hr;
DEBUG("start releasing d3d objects.\n");
if (ctx->texture)
......@@ -483,14 +189,13 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
texDesc.CPUAccessFlags = 0;
texDesc.ArraySize = 1;
texDesc.Format = renderFormat;
texDesc.Height = cfg->height;
texDesc.Width = cfg->width;
texDesc.Height = height;
texDesc.Width = width;
hr = ctx->d3device->CreateTexture2D( &texDesc, NULL, &ctx->texture );
if (FAILED(hr))
{
DEBUG("CreateTexture2D FAILED \n");
return false;
}
else
{
......@@ -499,7 +204,7 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
IDXGIResource1* sharedResource = NULL;
hr = ctx->texture->QueryInterface(__uuidof(IDXGIResource1), (void **)&sharedResource);
hr = ctx->texture->QueryInterface(&sharedResource);
if(FAILED(hr))
{
DEBUG("get IDXGIResource1 FAILED \n");
......@@ -507,7 +212,6 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
}
// ctx->texture->QueryInterface(&IID_ID3D11Resource1, (LPVOID*) &sharedResource);
hr = sharedResource->CreateSharedHandle(NULL, DXGI_SHARED_RESOURCE_READ, NULL, &ctx->sharedHandled);
if(FAILED(hr))
{
......@@ -519,7 +223,7 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
sharedResource->Release();
ID3D11Device1* d3d11VLC1;
hr = ctx->d3deviceVLC->QueryInterface(__uuidof(ID3D11Device1), (void**)&d3d11VLC1);
hr = ctx->d3deviceVLC->QueryInterface(&d3d11VLC1);
if(FAILED(hr))
{
_com_error error(hr);
......@@ -527,7 +231,7 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
abort();
}
hr = d3d11VLC1->OpenSharedResource1(ctx->sharedHandled, __uuidof(ID3D11Resource), (void**)&ctx->textureVLC);
hr = d3d11VLC1->OpenSharedResource1(ctx->sharedHandled, __uuidof(ID3D11Texture2D), (void**)&ctx->textureVLC);
if(FAILED(hr))
{
_com_error error(hr);
......@@ -550,7 +254,6 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
if (FAILED(hr))
{
DEBUG("CreateShaderResourceView FAILED \n");
return false;
}
else
{
......@@ -559,15 +262,103 @@ bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libv
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
ZeroMemory(&renderTargetViewDesc, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
renderTargetViewDesc.Format = texDesc.Format,
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D,
renderTargetViewDesc.Format = texDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
ID3D11RenderTargetView* renderTarget;
FLOAT blueRGBA[] = { 0.0f, 0.0f, 1.0f, 1.0f };
ctx->d3device->CreateRenderTargetView(ctx->texture, &renderTargetViewDesc, &renderTarget);
ctx->d3dctx->ClearRenderTargetView(renderTarget, blueRGBA);
renderTarget->Release();
//*
hr = ctx->d3deviceVLC->CreateRenderTargetView(ctx->textureVLC, &renderTargetViewDesc, &ctx->textureRenderTarget);
/*/
hr = ctx->d3device->CreateRenderTargetView(ctx->texture, &renderTargetViewDesc, &ctx->textureRenderTarget);
//*/
if (FAILED(hr))
{
DEBUG("CreateRenderTargetView FAILED \n");
return false;
}
}
void RenderAPI_D3D11::CreateResources(struct render_context *ctx, ID3D11Device *d3device, ID3D11DeviceContext *d3dctx)
{
DEBUG("Entering CreateResources \n");
HRESULT hr;
assert(ctx != nullptr);
ZeroMemory(ctx, sizeof(*ctx));
InitializeCriticalSection(&ctx->sizeLock);
ctx->width = Width;
ctx->height = Height;
ctx->d3device = d3device;
ctx->d3dctx = d3dctx;
UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT; /* needed for hardware decoding */
creationFlags |= D3D11_CREATE_DEVICE_DEBUG; //TODO: remove for release mode
hr = D3D11CreateDevice(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
creationFlags,
NULL,
NULL,
D3D11_SDK_VERSION,
&ctx->d3deviceVLC,
NULL,
&ctx->d3dctxVLC);
if(FAILED(hr))
{
DEBUG("FAILED to create d3d11 device and context \n");
abort();
}
DEBUG("Configuring multithread \n");
/* The ID3D11Device must have multithread protection */
ID3D10Multithread *pMultithread;
hr = ctx->d3device->QueryInterface(&pMultithread);
if (SUCCEEDED(hr)) {
pMultithread->SetMultithreadProtected(TRUE);
pMultithread->Release();
}
ctx->quadIndexCount = 6;
Update(ctx, SCREEN_WIDTH, SCREEN_HEIGHT);
DEBUG("Exiting CreateResources.\n");
}
void RenderAPI_D3D11::ReleaseResources(struct render_context *ctx)
{
DEBUG("Entering ReleaseResources.\n");
ctx->d3deviceVLC->Release();
ctx->d3dctxVLC->Release();
ctx->textureRenderTarget->Release();
ctx->textureShaderInput->Release();
ctx->texture->Release();
ctx->pVertexBuffer->Release();
ctx->d3dctx->Release();
ctx->d3device->Release();
}
bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libvlc_video_output_cfg_t *out )
{
DEBUG("Entering UpdateOutput_cb.\n");
struct render_context *ctx = static_cast<struct render_context *>( opaque );
assert(ctx != nullptr);
DXGI_FORMAT renderFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
Update(ctx, cfg->width, cfg->height);
DEBUG("Done \n");
out->surface_format = renderFormat;
......@@ -594,6 +385,7 @@ bool StartRendering_cb( void *opaque, bool enter, const libvlc_video_direct3d_hd
struct render_context *ctx = static_cast<struct render_context *>( opaque );
if ( enter )
{
DEBUG("StartRendering enter ");
static const FLOAT blackRGBA[4] = {0.5f, 0.5f, 0.0f, 1.0f};
/* force unbinding the input texture, otherwise we get:
......@@ -603,11 +395,13 @@ bool StartRendering_cb( void *opaque, bool enter, const libvlc_video_direct3d_hd
//ctx->d3dctx->Flush();
ctx->d3dctxVLC->ClearRenderTargetView( ctx->textureRenderTarget, blackRGBA);
DEBUG("out \n");
return true;
}
DEBUG("StartRendering end ");
/* render into the swapchain */
static const FLOAT orangeRGBA[4] = {1.0f, 0.5f, 0.0f, 1.0f};
/*static const FLOAT orangeRGBA[4] = {1.0f, 0.5f, 0.0f, 1.0f};
ctx->d3dctx->OMSetRenderTargets(1, &ctx->swapchainRenderTarget, NULL);
ctx->d3dctx->ClearRenderTargetView(ctx->swapchainRenderTarget, orangeRGBA);
......@@ -630,13 +424,13 @@ bool StartRendering_cb( void *opaque, bool enter, const libvlc_video_direct3d_hd
D3D11_VIEWPORT viewport = { };
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = ctx->width;
viewport.Height = ctx->height;
viewport.Width = SCREEN_WIDTH;
viewport.Height = SCREEN_HEIGHT;
ctx->d3dctx->RSSetViewports(1, &viewport);
ctx->d3dctx->DrawIndexed(ctx->quadIndexCount, 0, 0);
ctx->d3dctx->DrawIndexed(ctx->quadIndexCount, 0, 0);*/
DEBUG("out \n");
return true;
}
......@@ -688,10 +482,20 @@ void Resize_cb( void *opaque,
void* RenderAPI_D3D11::getVideoFrame(bool* out_updated)
{
DEBUG("Entering getVideoFrame \n");
*out_updated = true;
//Context.d3dctx->OMSetRenderTargets(1, &Context.textureRenderTarget, NULL);
static const FLOAT orangeRGBA[4] = {1.0f, 0.5f, 0.0f, 1.0f};
//*
Context.d3dctxVLC->OMSetRenderTargets(1, &Context.textureRenderTarget, NULL);
Context.d3dctxVLC->ClearRenderTargetView(Context.textureRenderTarget, orangeRGBA);
/*/
Context.d3dctx->ClearRenderTargetView(Context.textureRenderTarget, orangeRGBA);
//*/
return Context.textureShaderInput;
/*
*out_updated = Context.updated;
Context.updated = false;
return (void*)Context.textureShaderInput;
return (void*)Context.textureShaderInput;*/
// std::lock_guard<std::mutex> lock(text_lock);
// if (out_updated)
......
......@@ -39,7 +39,7 @@ static void* Hwnd;
extern "C" libvlc_media_player_t* UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
CreateAndInitMediaPlayer(libvlc_instance_t* libvlc)
{
if(libvlc == NULL)
/*if(libvlc == NULL)
{
DEBUG("libvlc is NULL, aborting...");
return NULL;
......@@ -54,13 +54,13 @@ CreateAndInitMediaPlayer(libvlc_instance_t* libvlc)
return NULL;
}
mp = libvlc_media_player_new(inst);
mp = libvlc_media_player_new(inst);*/
RenderAPI* s_CurrentAPI;
if (mp == NULL) {
/*if (mp == NULL) {
DEBUG("Error initializing media player");
goto err;
}
}*/
DEBUG("Calling... Initialize Render API \n");
......@@ -84,11 +84,11 @@ CreateAndInitMediaPlayer(libvlc_instance_t* libvlc)
s_CurrentAPI->ProcessDeviceEvent(kUnityGfxDeviceEventInitialize, s_UnityInterfaces);
DEBUG("Calling... setVlcContext s_CurrentAPI=%p mp=%p", s_CurrentAPI, mp);
s_CurrentAPI->setVlcContext(mp);
//s_CurrentAPI->setVlcContext(mp);
contexts[mp] = s_CurrentAPI;
contexts[NULL] = s_CurrentAPI;
return mp;
return NULL;
err:
if ( mp ) {
// Stop playing
......@@ -104,10 +104,7 @@ err:
extern "C" void* UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
getVideoFrameVLC (libvlc_media_player_t* mp, bool * updated)
{
if(mp == NULL)
return nullptr;
RenderAPI* s_CurrentAPI = contexts.find(mp)->second;
RenderAPI* s_CurrentAPI = contexts.find(NULL)->second;
if (!s_CurrentAPI) {
DEBUG("Error, no Render API");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment