Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Screen Capture, Modify, and Draw Question


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.

  • You cannot reply to this topic
5 replies to this topic

#1 KellySmith   Members   -  Reputation: 113

Like
0Likes
Like

Posted 20 December 2013 - 01:30 PM

Hello Everyone,
 
I am building an application that needs to capture the current screen, perform pixel-wise analysis of the captured data, and ideally draw a modified version of the data back onto the screen.  Currently I have successfully found a way to capture pixel data from the screen and store it in memory using DirectX 9 and accessing the front buffer.  I am not necessarily married to DirectX 9, however it has the advantage of being already installed on the target audience's computers.
 
// Requires the D3D9 device to be initialized.
void grabScreen(const int WIDTH, const int HEIGHT, BYTE* pBits){

	d3ddev->CreateOffscreenPlainSurface(WIDTH, HEIGHT,
		D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);
	d3ddev->GetFrontBufferData(0, pSurface);
	//D3DXSaveSurfaceToFile(L"Desktop.jpg", D3DXIFF_JPG, pSurface, NULL, NULL);


	D3DLOCKED_RECT lockedRect;
	pSurface->LockRect(&lockedRect, NULL,
		D3DLOCK_NO_DIRTY_UPDATE |
		D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
	for (int i = 0; i < HEIGHT; i++)
	{
		memcpy((BYTE*)pBits + i * WIDTH * 32 / 8,
			   (BYTE*)lockedRect.pBits + i * lockedRect.Pitch,
			   WIDTH * 32 / 8);
	}
	pSurface->UnlockRect();

}
 
Next I pass *pBits through my analysis and modification routines to get what I would like to then be drawn back to the screen in some capacity.  I am not looking to replace the screen necessarily, and am in fact operating on a subset of the actual screen.  I have been looking all over the internet for a way to draw this pixel map back to the screen, however I cannot find a way.  In the past, I have used a Pixel Buffer Object in OpenGL to achieve a similar result, but cannot find a way to do it in DirectX.
 
My initial thought was to put it back into a surface and then render that surface:
bool ScreenManager::generateSurface(BYTE *pData, UINT width, UINT height, IDirect3DSurface9* pSurface){

	LPDIRECT3DDEVICE9 d3ddev = *this->device;

	d3ddev->CreateOffscreenPlainSurface(width, height,
		D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);

	D3DLOCKED_RECT lockedRect;
	pSurface->LockRect(&lockedRect, NULL,
		D3DLOCK_NOSYSLOCK);

	int bytesPerPixel = pixelSize / 8;
	for (int i = 0; i < height; i++)
	{
		memcpy(
			(BYTE*)lockedRect.pBits + i * lockedRect.Pitch,
			(BYTE*)pData + i * width * bytesPerPixel,
			width * bytesPerPixel);
	}

	pSurface->UnlockRect();

	return true;
}
But, I cannot find a way to directly render a surface.   I think I am probably missing some fundamental understanding of how DirectX works, and I apologize for my ignorance.
 
Any and all help would be greatly appreciated!
 
Thanks,
Kelly

Sponsor:

#2 hossainiir   Members   -  Reputation: 1020

Like
0Likes
Like

Posted 22 December 2013 - 01:08 AM

hi,

I wrote an application that take the screen and did some extra work to the pixels and then render it to the target,but i'm using directx 11.In Dx11 this work is very simple, u must create a resource(texture) and call map then do your works over pixels and then unmap the resource.

i hope this helps u.


خلیج تا ابد فارس

Persian Gulf


#3 KellySmith   Members   -  Reputation: 113

Like
0Likes
Like

Posted 25 December 2013 - 03:21 PM

Unfortunately, I don't think that I can require DX11 for the application, however the concept of using a texture to display my pixels on an arbitrary plane might be the right way to accomplish this. I will see if I can find a way to do this in DX9. Thanks for the tip smile.png

#4 MJP   Moderators   -  Reputation: 11565

Like
0Likes
Like

Posted 25 December 2013 - 08:16 PM

Reading back GPU-written data on he CPU kill the parallelism between the two processors, and causes both of them to stall for long periods of time. It's a great way to absolutely kill your peformance.

 

What are you actually trying to accomplish here?



#5 hossainiir   Members   -  Reputation: 1020

Like
0Likes
Like

Posted 28 December 2013 - 12:37 AM

Reading back GPU-written data on he CPU kill the parallelism between the two processors, and causes both of them to stall for long periods of time. It's a great way to absolutely kill your peformance.

 

What are you actually trying to accomplish here?

I want to write some date to a texture, what is the best procedure that i do not need to reading back GPU-written data?

Thanks.


خلیج تا ابد فارس

Persian Gulf


#6 KellySmith   Members   -  Reputation: 113

Like
0Likes
Like

Posted 02 January 2014 - 12:33 PM

Reading back GPU-written data on he CPU kill the parallelism between the two processors, and causes both of them to stall for long periods of time. It's a great way to absolutely kill your peformance.
 
What are you actually trying to accomplish here?


I need the raw pixel data of the screen so that I can perform real-time image analysis on it. Perhaps at some point I could optimize with a GPGPU solution like OpenCL and try to avoid the performance hit of copying memory between the device twice, but currently it is inherent to the problem at hand. Essentially, I am doing real time image recognition and tracking. Achieving sustained 30fps would be awesome, but not absolutely necessary.

I found a less than perfect solution by hooking my DX9 code up to a C# WPF application with a DLL and can easily convert my BYTE[] into a Bitmap and display it on the screen. I am easily achieving 30 frames per second right now, but have not created all of my image processing yet. Ideally, I would like to do the rendering by directly working with DX9 from the DLL and just using the WPF window for display, but this has allowed me to continue prototyping the other parts of my application in the meantime.




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