1

我们如何将一个 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);

编辑:添加了代码片段。

4

1 回答 1

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);
于 2011-05-23T05:23:55.117 回答