Sign in to follow this  
flip_mo_kid

Rendering to an offscreen surface

Recommended Posts

flip_mo_kid    151
Hi, I'm stuck on something and I know its a simple mistake that when somebody tells me the answer I'm going to think Embarassed whooooppsss, but for the life of me I can't see what I'm doing wrong at the moment. I have a class (with key code extracted below from the .h and .cpp file) and I want to get and set the render targets (set is always called before get). The compiler says its ok but i get run time errors with access violations. Problem happens with set, but I thought I'd put get down because errors will probably come up there too. I've gone through with a debugger but I can't figure out what is wrong with it. Thanks
 cclass {
 
 // Vars
 LPD3DXRENDERTOSURFACE renderSurface[5];
 LPDIRECT3DTEXTURE9 dynamicTexture[5];
 LPDIRECT3DSURFACE9 textureSurface[5];
 
   ...
}
 
void cclass::getRenderSurface(LPD3DXRENDERTOSURFACE* _a, LPDIRECT3DTEXTURE9* _b, LPDIRECT3DSURFACE9* _c, int _index)
{
 _a = &renderSurface[_index];
 _b = &dynamicTexture[_index];
 _c = &textureSurface[_index];
}
 
void cclass::setRenderSurface(LPD3DXRENDERTOSURFACE* _a, LPDIRECT3DTEXTURE9* _b, LPDIRECT3DSURFACE9* _c, int _index)
{
 renderSurface[_index] = *_a;
 dynamicTexture[_index] = *_b;
 textureSurface[_index] = *_c;
} 
and the thingy that uses it goes like this
 .
.
.
  LPD3DXRENDERTOSURFACE renderSurface = NULL;
  LPDIRECT3DTEXTURE9 dynamicTexture = NULL;
  LPDIRECT3DSURFACE9 textureSurface = NULL;
 
  for(int i=0; i<5; i++)
  {
   HRESULT hr;
 
   hr = D3DXCreateTexture( Device,  
         Pixels,  
         Pixels,  
         1,  
         D3DUSAGE_RENDERTARGET,  
         D3DFMT_A8R8G8B8,  
         D3DPOOL_DEFAULT,  
         &dynamicTexture );
 
   if( FAILED(hr) )
   {
    MessageBox(NULL,"Failed to create a texture with the D3DUSAGE_RENDERTARGET usage flag set!",
      "ERROR",MB_OK|MB_ICONEXCLAMATION);
    exit(-1);
   }
 
   // Create an off-screen "render to" surface...
 
   D3DSURFACE_DESC desc;
   dynamicTexture->GetSurfaceLevel( 0, &textureSurface );
   textureSurface->GetDesc( &desc );
 
   hr = D3DXCreateRenderToSurface( Device,  
           desc.Width,  
           desc.Height,  
           desc.Format,  
           TRUE,  
           D3DFMT_D16,  
           &renderSurface );
 
   if( FAILED(hr) )
   {
    MessageBox(NULL,"Failed to create the off-screen render surface!",
      "ERROR",MB_OK|MB_ICONEXCLAMATION);
    exit(-1);
   }
 
   h[i]->setRenderSurface(&renderSurface, &dynamicTexture, &textureSurface, i);
  }
.
.
. 
[Edited by - flip_mo_kid on March 29, 2005 1:38:09 PM]

Share this post


Link to post
Share on other sites
S1CA    1418
1a) If you aren't already, run with the debug version of the Direct3D runtime enabled and the debug output level fairly high (see the forum FAQ for details).

1b) Also, link with d3dx9d.lib rather than d3dx9.lib so you get the debug version of the D3DX library.

1c) Run in the debugger and check your debug output window to see if any Direct3D or D3DX functions are unhappy.


2a) When you say "I've gone through with a debugger", exactly which line does the debugger stop on when you get the access violation?

2b) Have you tried single-stepping through the code to see that the code is behaving as you expect ?


3a) If the code dies on the [i]"h->setRenderSurface(&renderSurface, &dynamicTexture, &textureSurface, i);" line, then can you post the code where "h" is declared, and where the array of "cclass" objects are initialised?

3b) If "h" is something like "cclass* h[5];", then that's 5 pointers declared, but what do you have them pointing to? Have you perhaps forgotten to "new cclass" for each of the 5 objects ?


4) Small suggestion: there's no need (or indeed benefit) to pass the parameters to setRenderSurface() and getRenderSurface() as pointers; passing them by value would make the code much clearer (and can even be a tiny performance benefit in this case).

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