• Advertisement
Sign in to follow this  

Post processing pixel shader not clearing old data

This topic is 3862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm a noob at using pixel shaders and the code that I'm using Release's any buffers that I'm using and I don't understand why I'm getting corroded looking images. I'm using DirectX 9 ps_2_0 on MSVS. The problem is I'm trying to gray out my screen of 2D sprites, this grayed out look is to show that the game is in a pause state. When I do pause the game old images are drawn to the screen and the existing ones seem to be corroded. If I’m not in the pause state everything is fine. Another thing if any of the images move since the last time I pressed pause, the new location of that image is grayed out and the old one appears. Here’s my render code:
<code>
	bool bDeviceReset = m_pD3D->GetDirect3DDevice()->TestCooperativeLevel() == D3DERR_DEVICENOTRESET;<br />
	if (bDeviceReset)<br />
	{<br />
		m_pRenderTarget->Release();<br />
	}<br />
<br />
	m_pD3D->Clear(0, 0, 0);<br />
	if (bDeviceReset)<br />
	{<br />
		HRESULT hr = m_pD3D->GetDirect3DDevice()->CreateTexture(m_nWindowWidth,<br />
			m_nWindowHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,<br />
			D3DPOOL_DEFAULT, &m_pRenderTarget, 0);<br />
		// Recreate the render target<br />
		if (hr != D3D_OK)<br />
		{<br />
			if (hr == D3DERR_INVALIDCALL)<br />
				int i = 1;<br />
			else if (hr == D3DERR_OUTOFVIDEOMEMORY)<br />
				int i = 1;<br />
			else if (hr == D3DERR_NOTAVAILABLE)<br />
				int i = 1;<br />
			else if (hr == E_OUTOFMEMORY)<br />
				int i = 1;	<br />
			MessageBox(0, "Failed to re-create texture", "Error", MB_OK | MB_ICONEXCLAMATION);<br />
		}<br />
	}<br />
<br />
	m_pD3D->DeviceBegin();<br />
	m_pD3D->SpriteBegin();<br />
<br />
	// Remember old back buffer<br />
	m_pD3D->GetDirect3DDevice()->GetRenderTarget(0, &m_pBackBuffer);<br />
	<br />
	// Set the render target<br />
	LPDIRECT3DSURFACE9 pSurface = 0;<br />
	m_pRenderTarget->GetSurfaceLevel(0, &pSurface);<br />
<br />
	m_pD3D->GetDirect3DDevice()->SetRenderTarget(0, pSurface);<br />
	pSurface->Release();<br />
<br />
	m_pObjManager->RenderObjects();<br />
<br />
	m_pD3D->SpriteEnd();<br />
	m_pD3D->DeviceEnd();<br />
<br />
	//////////////////////////////////////////////////////////////<br />
	// Set the render target and draw the target to the screen.<br />
	m_pD3D->GetDirect3DDevice()->SetRenderTarget(0, m_pBackBuffer);<br />
	m_pBackBuffer->Release();<br />
<br />
	//////////////////////////////////////////////////////////////<br />
	// Second render loop<br />
	//////////////////////////////////////////////////////////////<br />
	m_pD3D->Clear(0, 0, 0);<br />
	m_pD3D->DeviceBegin();<br />
	m_pD3D->SpriteBegin();<br />
<br />
	m_psPixelShader.Begin();<br />
	// Draw render target to backbuffer<br />
	m_pD3D->GetSprite()->Draw(m_pRenderTarget, 0, 0, 0, 0xFFFFFFFF);<br />
	<br />
	m_pD3D->SpriteEnd();<br />
	<br />
	m_psPixelShader.End();<br />
	<br />
	m_pD3D->DeviceEnd();<br />
<br />
	// About time!! Do it!<br />
	m_pD3D->Present();
</code>
And the pixel shader:
sampler2D g_samSrcColor;

float4 ps_main( float2 Tex : TEXCOORD0 ) : COLOR0
{
float4 Color;

// Grey
Color.a = 1.0f;
Color = tex2D( g_samSrcColor, Tex.xy);
Color.rgb = (Color.r+Color.g+Color.b)/3.0f;

if (Color.r == Color.g && Color.r == Color.b)
{
}
else if (Color.r >= Color.g && Color.r >= Color.b)
{
Color.b = Color.g = Color.r;
}
else if (Color.g >= Color.b)
{
Color.b = Color.r = Color.g;
}
else
{
Color.r = Color.g = Color.b;
}

    return Color;
}
If I need to clarify anything please just let me know. BTW, I'm also a noob at posting code to forums, any hints on how to do this properly would really help, thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement