Multithreading 使用共享ID3D11设备的多线程环境中的D2D1和D3D11互操作性问题
在D2D下,我使用ID2D1RenderTarget对象在单独的线程中绘制线和图像,甚至渲染到同一线程中的另一个ID2D1RenderTarget对象,它总是可以正常工作 <>但在需要支持多线程D3/Media基金会的情况下,我使用了全局ID3D3D11设备,并进行了D2D/D3D互操作。p> 如果只运行一个线程,它工作得很好;bug如果同时运行两个线程(每个线程都有一个CDxRender窗口),则会报告一个错误,我不知道如何解决:Multithreading 使用共享ID3D11设备的多线程环境中的D2D1和D3D11互操作性问题,multithreading,direct3d,direct2d,Multithreading,Direct3d,Direct2d,在D2D下,我使用ID2D1RenderTarget对象在单独的线程中绘制线和图像,甚至渲染到同一线程中的另一个ID2D1RenderTarget对象,它总是可以正常工作 但在需要支持多线程D3/Media基金会的情况下,我使用了全局ID3D3D11设备,并进行了D2D/D3D互操作。p> 如果只运行一个线程,它工作得很好;bug如果同时运行两个线程(每个线程都有一个CDxRender窗口),则会报告一个错误,我不知道如何解决: D2D DEBUG ERROR - An attempt to d
D2D DEBUG ERROR - An attempt to draw to an inaccessible target has been detected.
出现MFVerifDemo.exe调试断点
Then press F5, continue to run:
D3D11 ERROR: ID3D11DeviceContext::Draw: A Vertex Shader is always required when drawing, but none is currently bound. [ EXECUTION ERROR #341: DEVICE_DRAW_VERTEX_SHADER_NOT_SET]
D3D11 ERROR: ID3D11DeviceContext::Draw: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT]
D3D11: Removing Device.
1,使用d3d11createdevice创建全局变量_d3d11_device,并打包executecommandlist:
CComPtr<id3d11device> _d3d11_device
HRESULT CreateD3DDevice(ID3D11Device** d3d11_device)
{
CComPtr<ID3D11DeviceContext> d3d11_immedContex;
//Describe our Buffer
DXGI_MODE_DESC bufferDesc;
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
bufferDesc.Width = 0;
bufferDesc.Height = 0;
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
//Create our _swap_chain1
HRESULT hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE , NULL, D3D11_CREATE_DEVICE_DEBUG | D3D11_CREATE_DEVICE_BGRA_SUPPORT, NULL, NULL, D3D11_SDK_VERSION, d3d11_device, NULL, &d3d11_immedContex);
RETURN_ON_FAIL(hr);
CComPtr<ID3D11Multithread> D3DDevMT;
hr = (*d3d11_device)->QueryInterface(IID_PPV_ARGS(&D3DDevMT));
RETURN_ON_FAIL(hr);
D3DDevMT->SetMultithreadProtected(TRUE);
return hr;
}
void ExecuteCommandList(CComPtr<ID3D11DeviceContext> deferred_context)
{
CComPtr<ID3D11CommandList> command_list;
HRESULT hr = deferred_context->FinishCommandList(FALSE, &command_list);
RETURN_ON_FAIL2(hr);
CComPtr<ID3D11DeviceContext> immediate_context;
_d3d11_device->GetImmediateContext(&immediate_context);
if (immediate_context)
{
std::unique_lock <std::mutex> lck(_mutex);
immediate_context->ExecuteCommandList(command_list, FALSE);
}
}
5,VertexShader.hlsl:
struct PS_Input
{
float4 pos:SV_POSITION;
float4 color:COLOR;
float2 texCoord:TEXCOORD;
};
Texture2D objTexture :register(t0);
SamplerState objSamplerState;
float4 main(PS_Input input) : SV_TARGET
{
/*float4 retColor = 0;
retColor = objTexture.Sample(objSamplerState, input.texCoord) * input.color;
clip(retColor.a - 0.3);
return retColor;*/
return input.color;
}
struct VS_Input
{
float4 pos:POSITION;
float4 color:COLOR;
float2 texCoord:TEXCOORD;
};
struct VS_Output
{
float4 pos:SV_POSITION;
float4 color:COLOR;
float2 texCoord:TEXCOORD;
};
VS_Output main(VS_Input input)
{
VS_Output output;
output.pos = input.pos;/*mul(input.pos, WVP);*/
output.color = input.color;
output.texCoord = input.texCoord;
return output;
}
我看不到在代码中使用
ID3D11多线程。是否启用多线程操作保护?是的,创建ID3D11Device,然后查询ID3D11Multithread并设置MultithreadProtected(TRUE)。解决方案是通过idxgikeyedmutex共享,D2D和D3D在单个线程中互操作。实际上需要不需要全局和跨线程共享的ID3D11设备,我看不到在代码中使用ID3D11多线程。是否启用多线程操作保护?是的,创建ID3D11Device,然后查询ID3D11Multithread并设置MultithreadProtected(TRUE)。解决方案是通过idxgikeyedmutex共享,D2D和D3D在单个线程中互操作。实际上需要不需要全局和跨线程共享的ID3D11设备
struct PS_Input
{
float4 pos:SV_POSITION;
float4 color:COLOR;
float2 texCoord:TEXCOORD;
};
Texture2D objTexture :register(t0);
SamplerState objSamplerState;
float4 main(PS_Input input) : SV_TARGET
{
/*float4 retColor = 0;
retColor = objTexture.Sample(objSamplerState, input.texCoord) * input.color;
clip(retColor.a - 0.3);
return retColor;*/
return input.color;
}
struct VS_Input
{
float4 pos:POSITION;
float4 color:COLOR;
float2 texCoord:TEXCOORD;
};
struct VS_Output
{
float4 pos:SV_POSITION;
float4 color:COLOR;
float2 texCoord:TEXCOORD;
};
VS_Output main(VS_Input input)
{
VS_Output output;
output.pos = input.pos;/*mul(input.pos, WVP);*/
output.color = input.color;
output.texCoord = input.texCoord;
return output;
}