SFML community forums

Help => Graphics => Topic started by: sparkzi on July 15, 2013, 09:34:29 pm

Title: Compare images using shaders
Post by: sparkzi on July 15, 2013, 09:34:29 pm
Hallo,
I has following problem to solve:
I want to compare (pixel-per-pixel) two textures (first generated from many draw shapes, second read from disc) using fragment shader. Unfortunately i'm new in graphics and open gl and i need some help. My idea was:
I believe that in result I will have one number - difference between two images.
But I have few questions:

I will appreciate any help :)
Title: Re: Compare images using shaders
Post by: Groogy on July 15, 2013, 09:45:35 pm
well it is not unheard of. When you do HDR you do a luminosity calculation on each pixel then you calculate an average luminosity by down-sampling until you have a 1x1 texture. So I wouldn't say you are doing anything super weird really.

Yes you can run three different pixel shaders using SFML, though not at the same time, because that's not how shaders work.

Yes you will have to read from the texture if you need the value for CPU computations. There is no way to output the value to an output parameter other than an output texture. You could perhaps use CUDA or something but then you can't use SFML for it.
Title: Re: Compare images using shaders
Post by: sparkzi on July 15, 2013, 09:49:51 pm
I'm glad to hear that :) Could you please give me any hint how to achieve my aim(it will be easier to start looking for something particular instead of "HDR open gl" ;))>
Title: Re: Compare images using shaders
Post by: Groogy on July 15, 2013, 09:53:27 pm
Well no not really? It's just fundamental Shaders. It's not like HDR uses some special trick.
You do your calculation and store that value in a texture and read the value. No magic :)
Title: Re: Compare images using shaders
Post by: sparkzi on July 16, 2013, 07:20:22 am
Thank you :)
It is possible to use more than one shader during drawing sprite in SFML?
Title: Re: Compare images using shaders
Post by: Hanmac on July 16, 2013, 08:00:03 am
Thank you :)
It is possible to use more than one shader during drawing sprite in SFML?

i found two ways:

http://en.sfml-dev.org/forums/index.php?topic=12032.msg83676#msg83676 (http://en.sfml-dev.org/forums/index.php?topic=12032.msg83676#msg83676) uses drawing Sprites

http://en.sfml-dev.org/forums/index.php?topic=7672.msg51053#msg51053 (http://en.sfml-dev.org/forums/index.php?topic=7672.msg51053#msg51053) uses TextureParameter


but i cant say wich one is better

Title: Re: Compare images using shaders
Post by: sparkzi on July 16, 2013, 09:53:55 am
Thanks :)   I have another question, after all my calculations I will be interested in reading difference result from GPU to CPU. It will be one number only and I want to to make reading very fast, could you please give me hint how to do correctly? I thought about following solution (in las shader): shader will be run on 1x1 texture and will have  texture parameter (1x height) - it will "sum" all pixels from texture parameter and then write result to 1x1 texture. Then i will read 1x1 texture to CPU using SFML. It will be fast solution or there are any better?
Title: Re: Compare images using shaders
Post by: Laurent on July 16, 2013, 10:17:51 am
Using a 1x1 texture looks like the best solution to read from GPU to CPU.
Title: Re: Compare images using shaders
Post by: sparkzi on July 16, 2013, 01:59:51 pm
During computing difference between images I recive quite big numbers: it is possible to store my original result number in 1x1 texture or I have to scale it to 0-1 range?
Title: Re: Compare images using shaders
Post by: Groogy on July 16, 2013, 02:39:58 pm
Using SFML the value is between 0-1, SFML doesn't support different texture formats yet *frowns in Laurent's general direction*
Title: Re: Compare images using shaders
Post by: Laurent on July 16, 2013, 02:53:17 pm
Quote
frowns in Laurent's general direction
Bah... a single pixel in current format is 32-bits, which is enough to store more than 4 billions of different values. Should be precise enough for this purpose :P
Title: Re: Compare images using shaders
Post by: Nexus on July 16, 2013, 07:18:55 pm
Direct texture download incurs a blocking function call on both CPU and GPU. You should consider pixel buffer objects (PBOs) for fast readback.

Check out these resources:
Title: Re: Compare images using shaders
Post by: Groogy on July 16, 2013, 07:47:47 pm
Quote
frowns in Laurent's general direction
Bah... a single pixel in current format is 32-bits, which is enough to store more than 4 billions of different values. Should be precise enough for this purpose :P

Problem is that he can only get out a color value between 0 and 255 integers unless he writes OpenGL specific code.

Edit: Or well he could encode the value in all the channels(RGBA) then he could represent a bigger number.
Title: Re: Compare images using shaders
Post by: Laurent on July 16, 2013, 08:21:37 pm
Quote
Or well he could encode the value in all the channels(RGBA) then he could represent a bigger number.
That's what I meant.
Title: Re: Compare images using shaders
Post by: sparkzi on July 18, 2013, 04:49:52 pm
Using SFML the value is between 0-1, SFML doesn't support different texture formats yet *frowns in Laurent's general direction*

That means that if I will write my own "reading from GPU" code (using OpenGL) I can use more values than 255 for each channel(0-1f)?
Title: Re: Compare images using shaders
Post by: Nexus on July 18, 2013, 07:29:26 pm
The amount of bytes you can use to store a single pixel doesn't depend on the "reading from GPU code", but on the texture format. But of course it's also possible to use more than one pixel, if 32 bit is not enough.