Sign in to follow this  
Carradine

DirectX Save Render Target as a Separate Texture

Recommended Posts

I am having an issue beginning to implement cascading shadow maps.  I already have shadows already rendering, however when I try to render multiple shadow maps with the same code, I am unable to "capture" the current shadow map texture to use for the render loop.

 

For example I am trying to render 2 shadow maps using two different ortho views:

 

for( int lpShadow = 0; lpShadow < 2; lpShadow++ )
{


    if( lpShadow == 0 )
   {
        D3DXMatrixOrthoRH( &shadowProj, (float)(d3ddm.Width)/10,(float)(d3ddm.Height)/10, -60.0f, 60.0f );
    }
    else if( lpShadow == 1 )
    {
        D3DXMatrixOrthoRH( &shadowProj, (float)(d3ddm.Width)/40,(float)(d3ddm.Height)/40, -10.0f, 10.0f );
    }


    D3DXMATRIX matLightViewProj = shadowWorld * shadowView * shadowProj;
    g_pEffect->SetMatrix( "g_matLightViewProj", &matLightViewProj );


    // Grab the old render target and depth buffer
    g_pd3dDevice->GetRenderTarget( 0, &g_pOldColorRT );
    g_pd3dDevice->GetDepthStencilSurface( &g_pOldDepthRT );
    
    // Render the scene depth to the shadow map
    g_pd3dDevice->SetRenderTarget( 0, g_pShadowSurf );
    g_pd3dDevice->SetDepthStencilSurface( g_pShadowDepth );
    
    // Clear the viewport
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFFFFFFFF, 1.0f, 0 );


    //Main rendering code loop here....
    /////////
    /////////


    // Restore the render target and depth buffer
    g_pd3dDevice->SetRenderTarget( 0, g_pOldColorRT );
    g_pd3dDevice->SetDepthStencilSurface( g_pOldDepthRT );
    SAFE_RELEASE( g_pOldColorRT );
    SAFE_RELEASE( g_pOldDepthRT );


    //Then finally put the texture into the shader
    g_pEffect->SetTexture( "tShadowMap2", g_pShadowMap );


}

I initialize the textures and surfaces like so

if( FAILED( g_pd3dDevice->CreateTexture( VGlobal::SHADOW_MAP_SIZE, VGlobal::SHADOW_MAP_SIZE, 1, D3DUSAGE_RENDERTARGET,
D3DFMT_R32F, D3DPOOL_DEFAULT, &g_pShadowMap, NULL ) ) )
{
    MessageBox( hWnd, "Unable to create shadow map!", ERROR, MB_OK | MB_ICONERROR );
    return E_FAIL;
}


// Grab the texture's surface
g_pShadowMap->GetSurfaceLevel( 0, &g_pShadowSurf );

My issue is I am failing to understand how to loop through my Shadow Map rendering loop above, and extract the texture out of g_pShadowMap for each loop that I want to render.  I simply want to make it its own texture resource, use it in the shader for the current loop, then discard it at the end of the render process.  Right now it only uses the last surface as all of its shadow map textures which is quite obvious.

 

I have tried using StretchRect(), CopySurfaceToSurface(), many other types of copy commands, but I cannot seem to grasp how to simply copy a texture resource out of the shadow map render target, and make it a separate texture out of g_pShadowMap.

 

I have also tried to use multiple render targets for each loop, however I do not seem to understand or grasp how this is possible.  I also tried to make g_pShadowMap an array, but simply assigning the second element to the render target does not seem to work.

 

Thanks for any help!

 

Share this post


Link to post
Share on other sites

1. You do not check for errors. Function calls provide an indication of success or failure (many by returning an HRESULT enumeration) which you should check, particularly if something is not working the way you expect.

 

2. This looks suspicious.

 

  SAFE_RELEASE( g_pOldColorRT );
  SAFE_RELEASE( g_pOldDepthRT );

 

If your SAFE_RELEASE macro includes ".. x = NULL; ", you probably intend "if( g_pOldColorRT ) g_pOldColorRT->Release(); // Dont' set it to NULL!"

 

3. Have you created your device with debug? If so, what information are you getting from the debug runtime?

Edited by Buckeye

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this