我正在将场景渲染到纹理,然后在后续渲染中使用该纹理及其 depthBuffer 来应用一些效果(景深、绽放等)。
这是问题所在:我设法将 DepthStencilBuffer 设置为自定义表面并将我的场景渲染为纹理。场景渲染完美,没有深度问题。话虽如此,当使用纹理(自定义表面绑定到的)时,所有深度值都是 1.0
我怀疑对 device->clear 的调用会影响错误的表面,但即使禁用它们,它也无法解决任何问题。
这是实际的代码:
HRESULT hr = S_FALSE;
VCND3D* renderer = static_cast<VCND3D*>( VCNRenderCore::GetInstance() );
LPDIRECT3DDEVICE9 device = renderer->GetD3DDevice();
//device->EndScene();
VCNDXShaderCore* shaderCore = static_cast<VCNDXShaderCore*>( VCNDXShaderCore::GetInstance() );
VCNNode* rootNode = VCNNodeCore::GetInstance()->GetRootNode();
LPDIRECT3DSURFACE9 currentSurface;
device->GetRenderTarget(0, ¤tSurface);
LPDIRECT3DSURFACE9 currentDepthBuffer;
device->GetDepthStencilSurface( ¤tDepthBuffer );
///// INITIAL HDR RENDER : //////////////////////////////////////
hr = device->SetRenderTarget( 0, mInitialHDRSurface );
VCN_ASSERT( SUCCEEDED(hr) );
hr = device->SetDepthStencilSurface( mDepthSurface );
VCN_ASSERT( SUCCEEDED(hr) );
device->Clear( 0,
NULL,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(100,100,100),
1.0f,
0 );
//device->BeginScene();
rootNode->Render();
//device->EndScene();
//hr = device->UpdateSurface( currentDepthBuffer, NULL, mDepthSurface, NULL );
//VCN_ASSERT( SUCCEEDED(hr) );
//////////// DOF EFFECT : /////////////////////////////////////////////////
hr = device->SetRenderTarget( 0, currentSurface );
VCN_ASSERT( SUCCEEDED(hr) );
device->SetRenderState(D3DRS_COLORWRITEENABLE,
D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
device->Clear( 0,
NULL,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(100,100,100),
1.0f,
0 );
//device->BeginScene();
// Set DOF shader
VCNDXShader* dofShader = shaderCore->GetShader( sidDof );
// Field of view to cover full screen :
hr = device->SetFVF(D3DFVF_SCREEN);
VCN_ASSERT( SUCCEEDED(hr) );
// select the vertex buffer to display
hr = device->SetStreamSource(0, mScreenVertexBuffer, 0, sizeof(SCREENVERTEX));
VCN_ASSERT( SUCCEEDED(hr) );
// Draw the Dof result
hr = dofShader->GetEffect()->SetTechnique( "BaseTechnique" );
VCN_ASSERT( SUCCEEDED(hr) );
hr = dofShader->GetEffect()->SetTexture( "gInputImageTexture", mInitialHDRTexture );
VCN_ASSERT( SUCCEEDED(hr) );
hr = dofShader->GetEffect()->SetTexture( "gDepthTexture", mDepthTexture );
VCN_ASSERT( SUCCEEDED(hr) );
hr = dofShader->GetEffect()->SetFloat( "gMinDepth", 3.0f );
VCN_ASSERT( SUCCEEDED(hr) );
hr = dofShader->GetEffect()->SetFloat( "gMaxDepth", 7.0f );
VCN_ASSERT( SUCCEEDED(hr) );
dofShader->RenderPrimitive( 2, D3DPT_TRIANGLESTRIP );
hr = device->SetDepthStencilSurface( currentDepthBuffer );
VCN_ASSERT( SUCCEEDED(hr) );
这是纹理和表面的初始化,因为可能存在错误:
// Texture for the initial HDR Render (Before effects)
hr = device->CreateTexture(res.x, res.y, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &mInitialHDRTexture, 0);
VCN_ASSERT_MSG( SUCCEEDED(hr), _T("Unable to create initial HDR Render Texture") );
// Extract the associated surface for rendering
hr = mInitialHDRTexture->GetSurfaceLevel(0, &mInitialHDRSurface);
VCN_ASSERT( SUCCEEDED(hr) );
// Texture with depth of the scene
hr = device->CreateTexture(res.x, res.y, 1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D32F_LOCKABLE, D3DPOOL_DEFAULT, &mDepthTexture, 0);
VCN_ASSERT_MSG( SUCCEEDED(hr), _T("Unable to create depth texture") );
// Extract the associated surface for rendering
hr = mDepthTexture->GetSurfaceLevel(0, &mDepthSurface);
VCN_ASSERT( SUCCEEDED(hr) );
Note that no ASSERT is getting triggered (which is exactly what is pissing me off, I would like some errors as an indication of where to look)
Nic