将2D纹理的一部分复制到另一个Direct3D11

时间:2011-05-17 05:29:29

标签: visual-c++ direct3d direct2d

我们如何将一个Direct3D 11 2D纹理的任意多边形区域复制到另一个纹理?我尝试过使用ID3D11DeviceContext :: CopySubresourceRegion方法,但它只复制了矩形部分。我使用Visual C ++。

pKeyedMutex11->AcquireSync(1, INFINITE);
pImmediateContext->CopySubresourceRegion( pBackBuffer11, 0, max(size.width-turned,0.0f), 0, 0, pSharedTexture11, 0, &sourceRegion );            
pKeyedMutex11->ReleaseSync(0);

pKeyedMutex11_2->AcquireSync(1, INFINITE);
pImmediateContext->CopySubresourceRegion( pBackBuffer11, 0, max(size_1.width - 2*turned,0.0f) , 0, 0, pSharedTexture11_2, 0, &sourceRegion_2 );         
pKeyedMutex11_2->ReleaseSync(0);

pKeyedMutex11_1->AcquireSync(1, INFINITE);
        // Copy the content from the shared texture to the back-buffer          
pImmediateContext->CopySubresourceRegion( pBackBuffer11, 0, 0, 0, 0, pSharedTexture11_1, 0, &sourceRegion_1 );
pKeyedMutex11_1->ReleaseSync(0);

编辑:添加了代码段。

1 个答案:

答案 0 :(得分:1)

我不知道是否存在复制多边形区域的直接方法。但你可以使用 模板缓冲区。将任意多边形绘制到模板缓冲区。并通过应用模板将整个纹理复制到另一个纹理。你会得到同样的效果。

编辑::引文已添加

您可以查看本教程。 http://www.rastertek.com/dx11tut03.html

现在我们需要设置深度模板描述。这允许我们控制Direct3D将为每个像素执行何种类型的深度测试。

// Initialize the description of the stencil state.
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));

// Set up the description of the stencil state.
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;

depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing.
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing.
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

填写说明后,我们现在可以创建深度模板状态。

// Create the depth stencil state.
result = m_device->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState);
if(FAILED(result))
{
    return false;
}

使用创建的深度模板状态,我们现在可以设置它以使其生效。请注意,我们使用设备上下文来设置它。

// Set the depth stencil state.
m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1);

我们需要创建的下一件事是深度模板缓冲区视图的描述。我们这样做是为了让Direct3D知道使用深度缓冲区作为深度模板纹理。填写完描述后,我们再调用CreateDepthStencilView函数来创建它。

// Initailze the depth stencil view.
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

// Set up the depth stencil view description.
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;

// Create the depth stencil view.
result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView);
if(FAILED(result))
{
    return false;
}

创建后,我们现在可以调用OMSetRenderTargets。这会将渲染目标视图和深度模板缓冲区绑定到输出渲染管道。这样,管道渲染的图形将被绘制到我们之前创建的后台缓冲区。将图形写入后缓冲区后,我们可以将其交换到前面并在用户屏幕上显示我们的图形。

// Bind the render target view and depth stencil buffer to the output render pipeline.
m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView);