Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Read Pixel From Screen ??

Author
Message
OminusPrime
9
Years of Service
User Offline
Joined: 30th Jun 2015
Location:
Posted: 2nd Dec 2016 00:02
Hi Everyone .
Please note this is Tier 1 .
I though this would be an easy one reading a pixel color from the screen but its turning out to be quite complex.

i understand that i can Create a memory block from an image then read the color from there . but i need this to be really fast . it seems like a lot to do just to read a single pixel.


ive been calculating large volumes of simulation data in shaders then out putting the results to a set of pixels for final processing and output.

if anyone knows of a way of reading a pixels data or otherwise getting information out of a shader back to AppGameKit that would be great.

Thks for any help.



BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Dec 2016 09:46
Quote: "ive been calculating large volumes of simulation data in shaders then out putting the results to a set of pixels for final processing and output."


Does this mean you already know the pixel values beforehand? Or does something else happen to them after this event?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
OminusPrime
9
Years of Service
User Offline
Joined: 30th Jun 2015
Location:
Posted: 2nd Dec 2016 14:15
Yep .
Here are some examples.

I Get data into the shader in the usual manner then i process it and output the results in 32 bit RGBA encoded Integers to the bitmap usually at position 0/0
although i'm sure i can output it anywhere.

i now need to read these pixels into AppGameKit i can decoded them back to the correct numbers .

Another example is Passing information from one frame to the next as you are rendering HDR High dynamic Range images.
I calculate the scene using HDR then i need to pass the Low and High levels from one frame to the next upcoming Frame.

so i output them as encoded Decimal at Pixel 1 and 2. I read them and pass them to the next frame Shader. where the HDR output is scaled by this amount. it works good and usually the two pixels are not noticeable. but in AppGameKit i'm unable to read the pixel data for passing it on.

there are many special effects that need to pass data from one frame to the next. I suppose i could pass in the entire last frame to the next frame but this seems like OVERKILL.


Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 2nd Dec 2016 15:05
Couldn't you sample them in the pixel shader and store them in the shader to be read back in AppGameKit?
OminusPrime
9
Years of Service
User Offline
Joined: 30th Jun 2015
Location:
Posted: 2nd Dec 2016 16:14
That would be great but how do i read the data into AGk ?? as far as i know AppGameKit doesnt have any way of doing that.?
There must be a way of getting data out of a shader. but i don't know how.
thats why i was thinking, just output my data to screen and sample it from there.


if anyone has any other ideas that may work i would appreciate that.




thks Again.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 5th Dec 2016 09:08
You're right. You can set a shader value, but not get it. (Unless it's there, but undocumented.)
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 5th Dec 2016 16:02
Once something is on the GPU it is difficult to get it back, this is because the CPU and GPU operate on different frames at the same time. Whilst the CPU is constructing frame 2 the GPU is still rendering frame 1, so if you need some data from frame 1 in order to construct frame 2 then the CPU will have to wait until the GPU is finished rendering frame 1 before the CPU can continue with frame 2. This then means that the GPU is left waiting for the CPU to finish constructing frame 2 before it can render frame 2. So you'd get a lot of stopping and starting of both the CPU and GPU which hurts performance.

However you could construct frame 3 with data from frame 1 since the GPU will have finished that and be processing frame 2 whilst you are constructing frame 3. To so this you'd have to create some kind of double buffer for the data you want to read back. For example you could have two images that you use to pass data back (the GPU writes to them with SetRenderToImage whilst the CPU reads from them with CreateMemblockFromImage), lets call them image 1 and image 2. During frame 1 the GPU writes to image 1, during frame 2 it writes to image 2, then during frame 3 it writes to image 1 again, and keeps swapping. Now whilst you are constructing frame 3 on the CPU you can CreateMemblockFromImage on image 1 and it should complete quickly since the GPU is not using it (it should be rendering frame 2, which is rendering information to image 2).

An even better approach would be to keep it all on the GPU as much as possible. For example render your scene to an image, then use a couple of shaders to calculate the min and max HDR values (one to do the min and one to do the max), and write the results to 1 pixel images (you may have to use a sequence of progressively smaller images until you get down to 1 pixel). Then use those 1 pixel images as inputs to the shader of the next frame. Note that you don't have to render your scene twice to do this, you can take the original rendered image and apply it to a quad to display it to the screen very quickly, and it will look as if it was rendered normally.
OminusPrime
9
Years of Service
User Offline
Joined: 30th Jun 2015
Location:
Posted: 6th Dec 2016 12:46
Thanks Paul. I,m Going to try these options out. i like option 2 the best. the one pixel frame . this seems to have the least amount of overhead. seems very efficient.
i know HDR usually required larger frame buffers and usually floating point at that. but i get similar results if not a perfect reproduction.


Login to post a reply

Server time is: 2024-11-24 22:46:56
Your offset time is: 2024-11-24 22:46:56