Stratos: Punto de Encuentro de Desarrolladores

¡Bienvenido a Stratos!

Acceder

Foros





Problema con Multi Render Targets (MRT) y DirectX (HLSL)

Iniciado por arkangel2803, 06 de Febrero de 2009, 02:53:01 PM

« anterior - próximo »

arkangel2803

Hola a todos

La verdad es que he estado buscando por foros y google y no he encontrado mucho que me ayude a solucionar el problema que tengo
Intentare exponerlo lo mas claro posible, tengo la teoria de que debe ser una tonteria pero no la encuentro :( y de momento en foros como gamedev no me han ayudado.

Creacion del Render Target:
---------------------------------------
Código (cpp) [Seleccionar]

HRESULT hr;
hr = D3DXCreateTexture( m_Device,
inWidth, //Hanchura
inHeight, //Altura
1, //mip levels
D3DUSAGE_RENDERTARGET,
m_ColorFormat,
D3DPOOL_DEFAULT,
&m_tTexture);
if(hr != D3D_OK)
{
return (false);
}

hr = m_tTexture->GetSurfaceLevel(0, &m_Surface);
assert (hr == D3D_OK);

        m_bUseDepthBuffer = ( m_StencilZBufferFormat != D3DFMT_UNKNOWN );
hr = D3DXCreateRenderToSurface( m_Device,
desc.Width,
desc.Height,
desc.Format,
m_bUseDepthBuffer,
m_StencilZBufferFormat,
&m_RenderToSurface
       );
return (hr == D3D_OK);



- inWidth y inHeight son inicializados al tamaño del backbuffer.
- El formato del pixel es D3DFMT_A8R8G8B8,D3DFMT_D24X8
- El formato del Backbuffer es D3DFMT_X8R8G8B8

Render Loop:
------------------
Código (cpp) [Seleccionar]

HRESULT Hr;
IDirect3DSurface9 *BackBuffer;


// Clear the backbuffer and the zbuffer
        m_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(64,128,200), 1.0f, 0 );

m_pD3DDevice->GetRenderTarget(0, &BackBuffer);
m_pD3DDevice->SetRenderTarget(0,m_RT_LDR.GetSurface());
m_pD3DDevice->SetRenderTarget(1,m_RT_HDR.GetSurface());
m_pD3DDevice->SetRenderTarget(2,m_RT_Normal.GetSurface());

// Begin the scene
       if( SUCCEEDED( m_pD3DDevice->BeginScene() ) )
       {

FillRenderSlots();

//Renderizado de mallas solidas (sin transparencias)
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);

RenderSlots(inElapsedTime,&m_vRenderSlotsSolid);

//Renderizado de mallas con alpha (con transparencias)
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
       m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_BLENDOP,D3DBLENDOP_ADD);

RenderSlots(inElapsedTime,&m_vRenderSlotsAlpha);

m_HelpText.Render();

//Renderizamos las lineas del HelperLine
RenderLines();

        // End the scene
        m_pD3DDevice->EndScene();
    }
m_pD3DDevice->SetRenderTarget(0, BackBuffer);
m_pD3DDevice->SetRenderTarget(1,NULL);
m_pD3DDevice->SetRenderTarget(2,NULL);

m_RT_LDR.DumpSurface(0,0,m_iBackBufferWidth,m_iBackBufferHeight);
m_RT_LDR.SaveToFile("RT_LDR");
m_RT_HDR.SaveToFile("RT_HDR");
m_RT_Normal.SaveToFile("RT_Normal");

// Present the backbuffer contents to the display
    m_pD3DDevice->Present( NULL, NULL, NULL, NULL );





- RenderTarget.GetSurface() retorna un puntero a la surface de ese RT.
- Salvo la direccion del backbuffer para volvero a poner luego en su sitio.
- RenderSlots(...) renderiza las mallas, uno para las mallas solidas y otro para las mallas transparentes
- Cuando todo esta renderizado, vuelvo a poner el backbuffer en su sitio y pongo NULL en los otros render targets
- RenderTarget.DumpSurface(...) vuelca la superfici del RT en el backbuffer
- RenderTarget.SaveToFile(...) salva una surface en un archivo *.bmp


Shader code:
------------
Código (cpp) [Seleccionar]

struct RETURN_MRT
{
float4 OutColor0 : COLOR0;
float4 OutColor1 : COLOR1;
float4 OutColor2 : COLOR2;
};

// ...

VS_OUTPUT RenderSceneVS (
                          float4 inPos : POSITION,
                          float3 inNormal : NORMAL,
                          float2 inTex0 : TEXCOORD0,
                          float2 inTex1 : TEXCOORD1,
                          float3 inTangent : TEXCOORD2,
                          float3 inBinormal : TEXCOORD3
)
{
VS_OUTPUT output;

        // ...
        return output;
}

RETURN_MRT RenderScenePS(
                          float4 inTex0m10 : TEXCOORD0,
                          float4 inTexL1L2 : TEXCOORD1,
                          float4 inTexDistL1L2 : TEXCOORD2,
                          float3 pos_world : TEXCOORD3,
                          float3 tangent : TANGENT,
                          float3 norm : NORMAL,
                          float3 binormal : BINORMAL
)
{
LIGHT_INFO i;
RETURN_MRT Ret;

        //...

        Ret.OutColor0 = i.Result_LDR;
Ret.OutColor1 = i.Result_HDR;
Ret.OutColor2 = i.NormalPixel.xyzz;

        return (Ret);
}



- Devuelvo una estructura que contiene diferentes variables COLOR[n] para poder hacer un output de varios colores a la vez
- Cuando en el codigo c++/DirectX hago un SetRenderTarget(...) toda la geometria y objetos desaparecen, quedandose la pantalla con el color del Clear(...)

Lo dicho, creo que hay algo que esta mal pero no puedo encotnrarlo, a ver si me podeis hechar un cable

Gracias por vuestro tiempo

LLORENS