Question

I am rendering a scene to a texture, then using that texture and its depthBuffer in subsequent renderings to apply some effects (Depth Of Field, Bloom, etc...).

Here is the problem: I manage to set the DepthStencilBuffer to a custom surface and render my scene to a texture. The scene is rendered perfectly, and there is no depth problems. This being said, when using the texture (to which the custom surface is bound), all depth values are exactly 1.0

I have suspected the call to device->clear to affect the wrong surface, but even when disabling them, it doesn't fix anything.

Here is the actual code :

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, &currentSurface);

LPDIRECT3DSURFACE9 currentDepthBuffer;
device->GetDepthStencilSurface( &currentDepthBuffer );

///// 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) );

And here are the init of the texture and surfaces, because the error could be there :

// 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

Was it helpful?

Solution

If I read your code correctly you clear mDepthSurface twice: first in INITIAL HDR RENDER section, then in DOF EFFECT. You either need to do a SetDepthStencilSurface before second clear, or not setting D3DCLEAR_ZBUFFER in that call. Otherwise second clear discards whatever is rendered by rootNode->Render() and at effect start you have all clear z-buffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top