Sign in to follow this  
silvermace

OpenGL Direct3D 8: Copy Back buffer to Texture.

Recommended Posts

silvermace    634
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
vcGamer    100
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
jollyjeffers    1570
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
silvermace    634
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
HanSoL0    1210
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  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now