相对于 opengl 的保存到FBO里面 dx11 里面需要用到 Render To Texture--RTT
渲染到纹理,顾名思义就是把渲染目标从帧缓存变成一个纹理。
这样就可以把一个场景渲染后在进行Post Process,做出现在流行的各种特效。
另外在利用GPU做通用计算的时候程序也是通过RTT和GPU交换数据的。
一般情况下需要用以下方式来创建render target ,target对于的 pbackbuffer 就是对应着屏幕上渲染的部分
HRESULT result;
// CREATE RENDER TARGET VIEW
result = pSwapChain->GetBuffer(NULL, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBufferTex));
if (FAILED(result))
{
return false;
}
D3D11_TEXTURE2D_DESC BBDesc;
ZeroMemory(&BBDesc, sizeof(D3D11_TEXTURE2D_DESC));
pBackBufferTex->GetDesc(&BBDesc);
D3D11_RENDER_TARGET_VIEW_DESC RTVDesc;
ZeroMemory(&RTVDesc, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
RTVDesc.Format = BBDesc.Format;
RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
//RTVDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
RTVDesc.Texture2D.MipSlice = 0;
result = pDevice->CreateRenderTargetView(pBackBufferTex, &RTVDesc, &pRenderTargetView);
if (FAILED(result))
{
MyDebug(_T("ERROR"));
}
Memory::SafeRelease(pBackBufferTex);
在vr模式里面,就需要代替这里的render target 使用以下代码 新建一个 target 对应的纹理 然后把纹理和target view 绑定到一起就可以
D3D11_TEXTURE2D_DESC textureDesc;
HRESULT result;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
// Initialize the render target texture description.
ZeroMemory(&textureDesc, sizeof(textureDesc));
// Setup the render target texture description.
textureDesc.Width = textureWidth;
textureDesc.Height = textureHeight;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
// Create the render target texture.
result = device->CreateTexture2D(&textureDesc, NULL, &m_renderTargetTexture);
if(FAILED(result))
{
return false;
}
// Setup the description of the render target view.
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
// Create the render target view.
result = device->CreateRenderTargetView(m_renderTargetTexture, &renderTargetViewDesc, &m_renderTargetView);
if(FAILED(result))
{
return false;
}
其他的在绘制时注意切换 target view 模式就行
deviceContext->OMSetRenderTargets(1, &m_renderTargetView, depthStencilView);
dx11 版本的vive 的hellovr 实现