Sign in to follow this  
silvermace

OpenGL Direct3D 8: Copy Back buffer to Texture.

Recommended Posts

Hi all, I'm an OpenGL programmer (and have been all my life), but this current project means I have to use Direct3D 8.1 (ie. Not 9+) I am currently trying to copy the current back-buffer surface to a texture, but its not at all working. I dont want to re-render my entire scene to a texture, just copy whats on the back-buffer to a texture then present it on a screen-aligned quad. I first got a wierd texture whcih seemed to be sceene related (colors changed etc) but now i currently (with a bit of fiddling) get nothing, and its laggy, somthing aweful! Currently quad doesnt even show up on screen, even with the null texture, any help would be appreciated! here is my code thus far: header:
class G13_PostProcessor
{
	// for the screen aligned quad
	LPDIRECT3DVERTEXBUFFER8	m_VB;
	LPDIRECT3DINDEXBUFFER8	m_IB;

	// render target textures
	LPDIRECT3DTEXTURE8 m_ScreenTexture;
	LPDIRECT3DSURFACE8 m_ScreenSurface;

	LPDIRECT3DTEXTURE8 m_ScreenTextureTemp1;
	LPDIRECT3DSURFACE8 m_ScreenSurfaceTemp1;

	LPDIRECT3DTEXTURE8 m_ScreenTextureTemp2;
	LPDIRECT3DSURFACE8 m_ScreenSurfaceTemp2;

	DWORD m_Height;
	DWORD m_Width;
	DWORD m_Format;

	// shaders that we will be using
	DWORD m_ShaderVSDefault;
	DWORD m_ShaderPSBlurX;
	DWORD m_ShaderPSBlurY;
	DWORD m_ShaderPSCombine;

	bool isInitialized;

public:
	G13_PostProcessor();
	virtual ~G13_PostProcessor();

	virtual void Init( LPDIRECT3DDEVICE8 d3dDevice, DWORD height, DWORD width );
	virtual void UpdateRTT( LPDIRECT3DDEVICE8 d3dDevice, LPDIRECT3DSURFACE8 srcSurf );
	virtual void Render( LPDIRECT3DDEVICE8 d3dDevice );
};


implementation:

#include "G13_PostProcess.h"
#include <d3dx8.h>

//------------------------------------------------------------------------------------------------
//constants
#define D3DFVF_CUSTOMVERTEX	(D3DFVF_XYZ | D3DFVF_TEX1)

//structures
struct VERTEX_XYZUV
{
	float x, y, z;
	float u, v;
};

VERTEX_XYZUV quadVerts[] = 
			{
				{  0.0f,	 768.0f,	0.0f,	0.0f, 0.0f	},
				{  0.0f,	 0.0f,		0.0f,	0.0f, 1.0f	},
				{  1024.0f,  768.0f,	0.0f,	1.0f, 0.0f	},
				{  1024.0f,  0.0f,		0.0f,	1.0f, 1.0f	}
			};

//------------------------------------------------------------------------------------------------
// base constructor and destructor
//------------------------------------------------------------------------------------------------
G13_PostProcessor::G13_PostProcessor() 
	: isInitialized(false)
{
}

G13_PostProcessor::~G13_PostProcessor()
{
}

//------------------------------------------------------------------------------------------------
// initialize the post processor 
//------------------------------------------------------------------------------------------------

void G13_PostProcessor::Init( LPDIRECT3DDEVICE8 d3dDevice, DWORD height, DWORD width )
{
	LPDIRECT3DSURFACE8 backBuffer;
	D3DSURFACE_DESC backBufferDesc;

	d3dDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer );
	backBuffer->GetDesc( &backBufferDesc );

        // currently the game runs at 1024x768
        // and here the height and width == 1024
	HRESULT a = d3dDevice->CreateTexture( height, width, 0, D3DUSAGE_RENDERTARGET, 
								backBufferDesc.Format, D3DPOOL_DEFAULT, &m_ScreenTexture );

	m_ScreenTexture->GetSurfaceLevel( 0, &m_ScreenSurface );

	HRESULT b = d3dDevice->CreateVertexBuffer( sizeof(quadVerts), D3DUSAGE_WRITEONLY, 
								D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_VB );

	if( FAILED(a) || FAILED(b) ) 
	{
		MessageBox( NULL, "Failed to create Render Targets", "Error", MB_ICONERROR|MB_OK );
		return;
	}
	
	BYTE* data;
	m_VB->Lock( 0, sizeof(data), (BYTE**)&data, 0 );
		memcpy( data, quadVerts, sizeof(quadVerts) );
	m_VB->Unlock();

	isInitialized = true;
}

//------------------------------------------------------------------------------------------------
// update the render-target surfaces
//------------------------------------------------------------------------------------------------

void G13_PostProcessor::UpdateRTT( LPDIRECT3DDEVICE8 d3dDevice, LPDIRECT3DSURFACE8 srcSurf )
{
	if( !isInitialized )
		return;

	/* tried this, got a kinda result texture looked like fractals no coherent image.
	if( FAILED( d3dDevice->CopyRects( srcSurf, NULL, 0, m_ScreenSurface, NULL ) ) ) 
	{
		MessageBox( NULL, "Failed to create Render Targets", "Error", MB_ICONERROR|MB_OK );
		PostQuitMessage(0);
		return;
	}
	*/

	D3DXLoadSurfaceFromSurface( m_ScreenSurface, NULL, NULL, srcSurf, NULL, NULL, 
								D3DX_FILTER_NONE, 0 );
}

//------------------------------------------------------------------------------------------------
// do the final processing and present the final post-processed image
//------------------------------------------------------------------------------------------------

void G13_PostProcessor::Render( LPDIRECT3DDEVICE8 d3dDevice )
{
	static D3DXMATRIX matTran, matProj;

	if( !isInitialized )
		return;

	D3DXMatrixIdentity( &matTran );
	D3DXMatrixIdentity( &matProj );
	D3DXMatrixTranslation( &matTran, 320, 240, 0 );
	D3DXMatrixOrthoRH( &matProj, 640, 480, -1, 1 );

	d3dDevice->BeginScene();

		d3dDevice->SetPixelShader( NULL );
		d3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );

		d3dDevice->SetTransform(D3DTS_WORLD, &matTran);
		d3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

		d3dDevice->SetTexture( 0, m_ScreenTexture );
		d3dDevice->SetStreamSource( 0, m_VB, sizeof(VERTEX_XYZUV) );
		d3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	d3dDevice->EndScene();
}

if anyone can see anything that could be causing any issues, pleeeaaase let me know.

Share this post


Link to post
Share on other sites
hi
the most common reason for this problem is the mismatch between your texture and your back buffer format, which I suppose is not the case with you, as you have used back buffer format (at least it seems so), otherwise you may have need to change your texture format manually, something you should be familiar with as an OpenGL programmer.
But if it didn't help at all, try rendering your scene again and this time on a texture as your render target, but you can speed up this process by turning off some advanced and computationally expensive features as anisotropy, aliasing, mip-mapping and even alpha blending operations since the textures aren't going to be visible all the time and within the same distance as your real scene.
Anyway I hope you can help your problem and use DirectX more and more!

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
I am currently trying to copy the current back-buffer surface
to a texture, but its not at all working. I dont want to re-render my entire
scene to a texture, just copy whats on the back-buffer to a texture then
present it on a screen-aligned quad.

uuhhm, why not just render it to a texture the first time round? [smile]

From what I understand of your description:

Render scene to back buffer -> Copy back buffer to texture -> Render texture back to screen

right?

Why not do:

Render scene to texture -> Render texture to screen

??

Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Quote:
Original post by silvermace
I am currently trying to copy the current back-buffer surface
to a texture, but its not at all working. I dont want to re-render my entire
scene to a texture, just copy whats on the back-buffer to a texture then
present it on a screen-aligned quad.

uuhhm, why not just render it to a texture the first time round? [smile]

From what I understand of your description:

Render scene to back buffer -> Copy back buffer to texture -> Render texture back to screen

right?

Why not do:

Render scene to texture -> Render texture to screen

??

Jack
good point. [smile] actually im a bit reluctant to do this because
its i dont really know my way around the engine (yet). but it looks like the
simplest alternative thus far.

Share this post


Link to post
Share on other sites
Hey silvermace,

Check the GooeyRenderedGraphic class (particularly the Create() and PreRender() methods) in G13_Gooey.cpp for an example on how to render straight to the texture. It's pretty straight forward. [smile]

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