HLSL Render To Texture

Started by
2 comments, last by kajjait 16 years, 11 months ago
Right now im simply trying to clear a texture color using a pixel shader using shader model 2.0. i can get the render to texture to work using clear i can clear it to whatever color i want, but my pixel shader isnt doing anything. what its doing is rendering both passes with the same world / view / proj matrices, and the pixel shader is returning 1.0 green for the color, but all i get is the quad with my clear color. here is my code, i would greatly appreciate any help or links to a good sample. thanks surface / texture creation

m_pDevice->CreateTexture(m_nWindowWidth, m_nWindowHeight, 1, D3DUSAGE_RENDERTARGET,
D3DFMT_A16B16G16R16F,
D3DPOOL_DEFAULT,
&m_pTexRender,
NULL);

m_pTexRender->GetSurfaceLevel(0, &m_pTexSurface);




Rendering
 
        m_pDevice->SetVertexDeclaration(m_pCubeDecl);
	m_pDevice->SetStreamSource(0, m_pCubeVB, 0, sizeof(tCubeVert));

	//snag the old color buffer
	IDirect3DSurface9 *oldColorBuffer = 0;
	m_pDevice->GetRenderTarget(0, &oldColorBuffer);

	unsigned int nPasses = 0;
	m_pEffect->SetTechnique("Pong");
	m_pEffect->Begin(&nPasses, 0);


	//render to texture pass	
	m_pEffect->SetMatrix("World", &matWorld);
	m_pEffect->SetMatrix("View", &matView);
	m_pEffect->SetMatrix("Projection", &matProj);

	//set texture surface as render target
	m_pDevice->SetRenderTarget(0, m_pTexSurface);
	m_pDevice->BeginScene();
	                m_pDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,255,255),1.0f,0);
	
	m_pEffect->BeginPass(0);
	m_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
	m_pEffect->EndPass();
	m_pDevice->EndScene();

	m_pEffect->SetMatrix("World", &matWorld);
	m_pEffect->SetMatrix("View", &matView);
	m_pEffect->SetMatrix("Projection", &matProj);
	
	m_pDevice->SetTransform(D3DTS_VIEW, &matView);
	m_pDevice->SetTransform(D3DTS_WORLD, &matWorld);
	m_pDevice->SetTransform(D3DTS_PROJECTION, &matProj );

	m_pDevice->SetRenderTarget(0, oldColorBuffer);
	m_pDevice->BeginScene();
	m_pDevice->Clear ( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER |     D3DCLEAR_STENCIL, m_dwClearColor, 1.0f, 0);


	m_pEffect->SetTexture("baseMap", m_pTexRender);
	m_pEffect->BeginPass(1);
	m_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
	m_pEffect->EndPass();
	m_pEffect->End();

	
	m_pDevice->EndScene();

	//Flip Back Buffer
	m_pDevice->Present( NULL, NULL, NULL, NULL );
}




hlsl shader


//calculate this outside later...
matrix World			: WORLD;
matrix Projection		: PROJECTION;
matrix View			: VIEW;
matrix WorldView		: WORLDVIEW;
matrix WorldViewProject		: WORLDVIEWPROJECT;
texture baseMap;


sampler BaseMap = sampler_state
{
    Texture = (baseMap);
    magfilter = linear; 
    minfilter = linear; 
    mipfilter = linear; 
    AddressU = wrap; 
    AddressV = wrap;  
};

struct VS_INPUT
{
	float4 pos 		: POSITION;
	float2 texCoord		: TEXCOORD0;
};

struct VS_OUTPUT
{
        float4	pos    		: POSITION;
	float2	texCoord	: TEXCOORD0;

};

VS_OUTPUT VSRenderToTexture( VS_INPUT vs_in)

{
        VS_OUTPUT Out		= (VS_OUTPUT)0;
    
        //move this to outside the shader later...
	WorldView		= mul(World, View);
	WorldViewProject	= mul(WorldView, Projection);
	
	Out.pos			= mul(vs_in.pos, WorldViewProject);
	
        return Out;
}

float4 PSRenderToTexture( VS_OUTPUT ps_in) : COLOR
{
        return float4(0.0f, 1.0f, 0.0f, 1.0f);
}

VS_OUTPUT VSCube( VS_INPUT vs_in)

{
        VS_OUTPUT Out		= (VS_OUTPUT)0;
    
        WorldView		= mul(World, View);
	WorldViewProject	= mul(WorldView, Projection);

	Out.pos			= mul(vs_in.pos, WorldViewProject);
	Out.texCoord		= vs_in.texCoord;
    
        return Out;
}

float4 PSCube( VS_OUTPUT ps_in) : COLOR
{
	return tex2D(BaseMap, ps_in.texCoord);
}


technique Pong
{
    pass RenderToTex
    {
        VertexShader = compile vs_2_0 VSRenderToTexture();
        PixelShader  = compile ps_2_0 PSRenderToTexture();
    }
    
    pass RenderCube
    {
        VertexShader = compile vs_2_0 VSCube();
        PixelShader  = compile ps_2_0 PSCube();
       
    }
}





[Edited by - kajjait on May 21, 2007 2:10:27 PM]
Advertisement
PIX is showing some funky stuff going on, my pixel color is getting overwritten somehow, i guess this is a good clue as to what is going on...

PIX Screenshot

(yes my clear color for the texture surface is white)
Just as guess. It looks like that your Z-Buffer is still active when you render to your texture. As you don’t clear it before you draw it still contain the values from the last primary render target call. Therefore it could be possible that the Z-test kills your pixel.
Quote:Original post by Demirug
Just as guess. It looks like that your Z-Buffer is still active when you render to your texture. As you don’t clear it before you draw it still contain the values from the last primary render target call. Therefore it could be possible that the Z-test kills your pixel.



I disabled ZWRITEENABLE, and ZENABLE render states, cleared the z buffer, and added ZEnable = false in the effect just to be extra extra sure, but its still not working

BUT! i was missing a clear at the top of my render which fixed that problem in pix where the old values were showing up... but the part that still confuses me is why the initial framebuffer value in pix is the color that my pixel shader returns, shouldnt it have been the clear color which was rendered on the quad??


**fixed it**

sigh... it was bad texture coordinates all along, i had changed a few of the texture coordinates earlier on trying to fix this and forgot to set them back. when i fixed that clear problem it was probably working the whole time, just had bad tex coords. DOH!!

[Edited by - kajjait on May 21, 2007 4:22:42 PM]

This topic is closed to new replies.

Advertisement