• Create Account

Banner advertising on our site currently available from just \$5!

# copying back buffer not working

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Catwheel  Members   -  Reputation: 125

Like
0Likes
Like

Posted 15 August 2014 - 12:35 AM

I'm pretty new to direct x, if you've seen my other posts, you probably know that. I'm trying to copy the middle section of the back buffer to a different surface and then copy that back to the top half of the back buffer. My plan later on is to do this twice, copying the back buffer to 2 different surfaces, then modifying those surfaces, and finally copying them back to the back buffer in a sort of splitscreen fashion (so that one surface copies to the top half of the back buffer, and the other surface copies to the bottom half of the back buffer).

Here's my code, which seems pretty sound to me:

        //rendering stuff
m_lpDX->m_lpDevice->EndScene();
IDirect3DSurface9 * frame1 = NULL;
IDirect3DSurface9 * pBackBuffer = NULL;
RECT r1;
SetRect(&r1, GetHeight()/4, 0, GetWidth(), ((GetHeight()*3)/4)-1);

m_lpDX->m_lpDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
m_lpDX->m_lpDevice->CreateOffscreenPlainSurface(GetWidth(),GetHeight()/2,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&frame1,NULL);// - CAT
D3DLOCKED_RECT frame1Lock;
D3DLOCKED_RECT pBackBufferLock;
frame1->LockRect(&frame1Lock,NULL,NULL);
pBackBuffer->LockRect(&pBackBufferLock,NULL,NULL);

r1.top = 0;
r1.left = 0;
frame1->UnlockRect();
pBackBuffer->UnlockRect();
frame1->Release();
pBackBuffer->Release();


Trouble is, nothing changes. The regular back buffer still shows. Does anyone have any trouble shooting ideas?

### #2unbird  Crossbones+   -  Reputation: 6526

Like
2Likes
Like

Posted 15 August 2014 - 08:23 AM

Check all HRESULT returned by API calls with the SUCCEEDED/FAILED macroes for failure.

From a quick glance: According to docs you can't use UpdateSurface when the surfaces are locked (why would you anyway, unless you want to copy manually).

### #3Buckeye  GDNet+   -  Reputation: 8287

Like
2Likes
Like

Posted 15 August 2014 - 09:36 AM

First: take unbird's advice. The sequence you posted could (and probably does) have several errors.

1. Is the format of frame1 the same as the backbuffer?

SetRect(&r1, GetHeight()/4, 0, GetWidth(), ((GetHeight()*3)/4)-1);

2. The rectangle you specify looks strange - you set the rect left x value to the GetHeight()/4, and set the right x to GetWidth()?? If GetWidth() is the width of the backbuffer, that makes the width of the surface greater than the width of the backbuffer.

The source surface must have been created with D3DPOOL_SYSTEMMEM. (i.e., in your case, both surfaces)
The destination surface must have been created with D3DPOOL_DEFAULT. (i.e., in your case, both surfaces)
The source and dest rects must fit within the surface.
The source format must match the dest format.

I haven't worked in DX9 for a while, but I think you're going to have to try GetRenderTargetData to start with, to get the backbuffer (video memory) into system memory. Then go from there, possibly with StretchRect, likely needing 2 or 3 surfaces overall.

Edited by Buckeye, 15 August 2014 - 10:02 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

### #4Catwheel  Members   -  Reputation: 125

Like
0Likes
Like

Posted 15 August 2014 - 01:48 PM

OKay so let me get this straight before I get started.

The process should look like this:

		m_lpDX->m_lpDevice->EndScene();
IDirect3DSurface9 * frame1 = NULL;
IDirect3DSurface9 * frame2 = NULL;
IDirect3DSurface9 * pBackBuffer = NULL;
IDirect3DSurface9 * pActualBackBuffer = NULL;

RECT r1;
SetRect(&r1, 0, GetHeight()/4, GetWidth(), ((GetHeight()*3)/4));

//get back buffer and initialize frame surfaces
m_lpDX->m_lpDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
m_lpDX->m_lpDevice->GetRenderTargetData(pBackBuffer,pActualBackBuffer);
m_lpDX->m_lpDevice->CreateOffscreenPlainSurface(GetWidth(),GetHeight()/2,D3DFMT_/*format matching back buffer*/,D3DPOOL_DEFAULT,&frame1,NULL);// - CAT
m_lpDX->m_lpDevice->CreateOffscreenPlainSurface(GetWidth(),GetHeight()/2,D3DFMT_/*format matching back buffer*/,D3DPOOL_DEFAULT,&frame2,NULL);// - CAT

//copy middle of back buffer to the two surfaces
m_lpDX->m_lpDevice->StretchRect(pActualBackBuffer,&r1,frame1,NULL,D3DTEXF_NONE);
m_lpDX->m_lpDevice->StretchRect(pActualBackBuffer,&r1,frame2,NULL,D3DTEXF_NONE);

//some operations would go here later to modify the surfaces for a left and right eye view

//copy frame surfaces back to the back buffer to be presented later.
RECT d1;
SetRect(&d1, 0, 0, GetWidth(), GetHeight()/2);
m_lpDX->m_lpDevice->StretchRect(frame1,NULL,pActualBackBuffer,&d1,D3DTEXF_NONE);
RECT d2;
SetRect(&d2, 0, GetHeight()/2, GetWidth(), GetHeight());
m_lpDX->m_lpDevice->StretchRect(frame2,NULL,pActualBackBuffer,&d2,D3DTEXF_NONE);

//release pointers
frame1->Release();
frame2->Release();
pBackBuffer->Release();
pActualBackBuffer->Release();


You'll notice that I did not check for the HResults, that is because I have a different question having to do with those before I write them in.

So I checked out the article that Unbird linked. I opened up the directx control panel and set it up for debug mode, however nothing came out. After googling I found that I needed to declare     #define D3D_DEBUG_INFO  so I did that, in this fashion:

//#ifdef _DEBUG
#define D3D_DEBUG_INFO  // declare this before including d3d9.h
//#endif

#include <d3d9.h>
#include <d3dx9.h>


I'm not really sure where I'm supposed to retrieve the debug info though. Alternatively, I would settle for just being able to output some info, like with println(); but the program I am altering simply becomes a plugin for another piece of software. Therefore, i don't know where to find the output. I don't have a lot of experience with the command line, but I am wondering "Is there something there I could do?"

EDIT: This has to do with my question about HRESULT, which is essentially, "How can I make some custom text output that I can reference, while in this plugin?"

Lastly, I just wanted to say, I'm a senior college student with no prior graphics programming experience. Is this project too demanding for someone from my position? Further, do you have any general advice for someone in my position?

Edited by Catwheel, 15 August 2014 - 01:53 PM.

### #5Buckeye  GDNet+   -  Reputation: 8287

Like
2Likes
Like

Posted 15 August 2014 - 02:36 PM

#if defined(DEBUG) | defined(_DEBUG)
#ifndef D3D_DEBUG_INFO
#define D3D_DEBUG_INFO
#endif

Just FYI, I don't know if the code you posted is correct or not.

As mentioned in unbird's referenced article, debugview may serve your purpose with regard to debug output if you're not using Visual Studio.

This has to do with my question about HRESULT, which is essentially, "How can I make some custom text output that I can reference, while in this plugin?"

If the app has a console window (which it should, if it doesn't), std::cout ( or printf() ) would work. Or launch the app from a cmd window. Otherwise, take a look at OutputDebugString(). The latter outputs c-strings, which requires you to format your output string if you want to output numbers. Again, if you're not launching it from Visual Studio, you'll have to find a debug message monitor like debugview.

I'm a senior college student with no prior graphics programming experience. Is this project too demanding for someone from my position? Further, do you have any general advice for someone in my position?

With regard to graphics programming experience - you're getting some now, huh?!

Too demanding? If you're not working from within Visual Studio, you may have a long row to hoe. But you seem to be willing to give things a try, and you've got gamedev. Go for it! I would suggest, however, that you read the documentation for each function you use, if you're not familiar with it and/or not positive of the input/output requirements. And you can google for example code in a lot of cases. Or, at worst, find out what won't work.

EDIT: an alternative, depending on how much time you have to work on your project - download Visual Studio (you can get a free version, maybe even a full-blown version being a college student). Create a version of your plugin as a standalone, test it thoroughly there and create another version as the plugin. Actually, you may save time overall by doing that. At worst, you appear to be interested in that type of programming so get into Visual Studio.

Edited by Buckeye, 15 August 2014 - 02:42 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

### #6Catwheel  Members   -  Reputation: 125

Like
0Likes
Like

Posted 15 August 2014 - 03:50 PM

With regard to graphics programming experience - you're getting some now, huh?!

I'm not exactly sure what the inflection is here. Are you saying this is too early for a programmer to learn graphics programming or too late, or are you stated that I am currently getting experience? I don't mean to sound condescending I'm just wondering what you meant and where that means I am now (I.E. ahead of the curve, behind, doesn't matter where)

EDIT: an alternative, depending on how much time you have to work on your project - download Visual Studio (you can get a free version, maybe even a full-blown version being a college student). Create a version of your plugin as a standalone, test it thoroughly there and create another version as the plugin. Actually, you may save time overall by doing that. At worst, you appear to be interested in that type of programming so get into Visual Studio.

I have been using visual c++ 2008 express edition. It was recommended by my client, because it can compile the byte code within 1KB. I do have Visual studio somewhere on this computer and I think that it is a full version (because I'm a student). I guess I should be giving that a try? I will try it.

This has to do with my question about HRESULT, which is essentially, "How can I make some custom text output that I can reference, while in this plugin?"

If the app has a console window (which it should, if it doesn't), std::cout ( or printf() ) would work. Or launch the app from a cmd window. Otherwise, take a look at OutputDebugString(). The latter outputs c-strings, which requires you to format your output string if you want to output numbers. Again, if you're not launching it from Visual Studio, you'll have to find a debug message monitor like debugview.

For clarity's sake let me say that back as I understood it to make sure I have it right. If I use Visual Studio it should give me a debug message monitor. How do I access this monitor? Do I need to do anything special with my .dll file? I really have no idea how to test it, except to build it and launch the main app, and then use the plugin to see what happens. An alternative quicker or more custom-message-friendly testing method would be showered with my love.

EDIT: What did you think of my process I outlined in my previous post? Did it look appropriate?

Edited by Catwheel, 15 August 2014 - 04:16 PM.

### #7Buckeye  GDNet+   -  Reputation: 8287

Like
1Likes
Like

Posted 15 August 2014 - 04:31 PM

are you stat[ing] that I am currently getting experience?

That's pretty much it. Just sayin' you've jumped into some relatively deep water and appear to still be breathing. With no programming experience, you're making some pretty good guesses in your coding. They don't work, but they're good guesses.

I guess I should be giving that a try? I will try it.

Definitely. With intellisense and on-board help (click on some word in your code and hit F1) it'll make life a bit easier. And, as suggested, you may want to write a basic DX9 engine and test your routines that way. I'm guessing you have the June 2010 SDK and you could steal a DX9 base engine from one of the examples. If you do that, the debug output goes to the Visual Studio output window automatically when you run your app from VS. With regard to other running apps, I'm afraid I don't know if VS monitors other debug output.

I don't know, but suspect, that debugview or the like will monitor messages to a debug output from a DLL. Also, I've never tried it, but I would think if you launch the main app from a command prompt window that std::cout << "some message\n" or printf("debug message %d", msgNum) could provide you info.

What did you think of my process I outlined? Did it look appropriate?

Without doing a lot of DX9 review and lookups of the docs, I couldn't say. As mentioned, review the docs for functions. A quick look at GetRenderTargetData indicates that what you've got won't work. You don't create the surface before you use it to receive the data. Dx9 (and 10 and 11, for that matter) have requirements for function arguments listed. Don't guess at them, read the docs. Or, as we use to say at work, RTFM!

Just scanning the code without looking anything up, you get the render target data (which, though you call it that, is not the "actual" backbuffer). That's just copied data. You do some manipulations with it, but nothing goes back to the backbuffer.

Edited by Buckeye, 15 August 2014 - 04:36 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

### #8Adam_42  Crossbones+   -  Reputation: 2786

Like
1Likes
Like

Posted 15 August 2014 - 04:57 PM

You can implement split screen without doing any copying of render targets.

The way you to that is to use viewports - the default viewport you get when you set a render target is a full size one, but you can change that to cover a different portion of the render target to do things like split screen.

The main advantage of that approach is that it will perform better than copying. Especially if your copies go back and forth to system memory instead of staying on the GPU.

If you really need to copy bits of render target around, I'd suggest using StretchRect() with the help of GetSurfaceLevel(). That will avoid the copying to and from system memory.

### #9JohnnyCode  Members   -  Reputation: 400

Like
0Likes
Like

Posted 20 August 2014 - 10:10 AM

render targets are a prety plentyfull resources, don't play with them too bad

(it is a writing of gpu station, forget about storing them over cpu, issuing back driver ordered commands)

in stead, only thing you can do-- has to be issued to gpu toward gpu memory objects.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS