PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Pixel Shader Question

Hello all. I'm trying to do my first pixel shader, one to fade out a sprite. I've done the following:

Code:

// These are globals:
int FadeValue = 0;
struct FadeShader : public PixelShader {
Pixel operator() (Pixel src, Pixel dest) const {
int colR = src & RED_MASK;
int colG = src & GREEN_MASK;
int colB = src & BLUE_MASK;
colR = colR - FadeValue;
colG = colG - FadeValue;
colB = colB - FadeValue;
if (colR < 0) { colR = 0; }
if (colG < 0) { colG = 0; }
if (colB < 0) { colB = 0; }
return (Pixel(RGB(colR, colG, colB)));
}
};
FadeShader fadeShader;
// Then in GameInit():
DisplayDevice* display = GetDisplay();
display->SetPixelShader(&fadeShader;);
// Then in GameLoop():
display->Blit(10, 10, myImage);
if (FadeValue < 255) {
FadeValue++;
}

So, my logic is that my shader should get the R, G and B components of each pixel during rendering, subtract FadeValue from each, if it winds up less than 0 then set it to 0, and then return a pixel based on those RGB values.

What I'm getting is not that... It looks more like a flashing than a fade. Anyone spot my problem? Thanks!
Hi,

Not sure if this is the problem, but try changing your return from the shader to use the Color function:

Code:
return (Color(colR, colG, colB));


BTW, you can get the same effect by using SetBlending and starting FadeValue at 255 and going down to 0.

Code:
display->SetBlending(FadeValue)


Pam
Thanks Pam, your suggestion actually does make it look closer to right, although it's still not doing what I expect. It's now doing a psychedelic color-cycle (I gotta think that's useful somewhere though! :) ).

Also, I tried the blending code you suggested, which I had previously played with but didn't like the result... It didn't work as you wrote, I'm not sure why.
Do you clear the screen in the GameLoop prior to the Blit?

I'm pretty sure the PixelShader applies to the clear also, so you have to SetDefaultShader before you Clear.

Pam
Well, it still didn't work with the shader, but your suggestion did put me on to something that worked...

When I was using SetBlending, I was NOT clearing the screen each time. If I do SetBlending to 255, clear the screen, then SetBlending to the fade value, then plot my image, it works as expected.

I can't say I quite understand why that is, but it is working, so I'll understand later :)

I'd still like to know what was wrong with my pixel shader though... It seems pretty straight-forward. By the way, I did try resetting to the default shader and then clearing the screen, then doing my custom blitting, but that didn't make any difference either.

Thanks again Pam! :)
fzammetti wrote:
When I was using SetBlending, I was NOT clearing the screen each time. If I do SetBlending to 255, clear the screen, then SetBlending to the fade value, then plot my image, it works as expected.

I can't say I quite understand why that is, but it is working, so I'll understand later :)


The reason it works this way is that the Clear() function is affected by the current PixelShader. The SetBlending() function uses a PixelShader that does Alpha Blending. I've found that SetBlending(255) is equivalent to SetDefaultShader(), both turn off the shading/blending.

So, if you Clear() to Black, zero, and your PixelShader/Blending is set to say 128, then the Black color is alpha blended on top of whatever is already in the buffer. I've actually used this technique to fade a whole image to black. SetBlending to something like 50, then Clear(0) a bunch of times.

I may have time on Monday to try out your PixelShader and see what it is doing.

Glad I could help!

Pam
Cool! That's the one fact that was confusing me: Blending is accomplished with a PixelFader. I didn't realize that, so I was getting confused. Now it all makes perfect sense (well, as perfect as anything in this world can I suppose!) Have a great weekend Pam!
While I'm at it, here are the two PixelShaders I use.

This one does additive Blending (used for my Particle Engine)

Code:
struct AddShader : public PixelShader
{
Pixel operator()( Pixel src, Pixel dest ) const
{
register unsigned int tdest = 0;
register unsigned int t;

t = (src & RED_MASK) + (dest & RED_MASK);
if(t > RED_MASK) t = RED_MASK;
tdest |= t;

t = (src & GREEN_MASK) + (dest & GREEN_MASK);
if(t > GREEN_MASK) t = GREEN_MASK;
tdest |= t;

t = (src & BLUE_MASK) + (dest & BLUE_MASK);
if(t > BLUE_MASK) t = BLUE_MASK;
tdest |= t;

return ((Pixel)tdest);
}
};


This is my version of Alpha Blending. I hardcoded alpha to a value I liked for my use. I'm sure the built-in one in PF is faster than mine. You can see it in raster2d.cpp in the PF internal stuff.

Code:
struct BlendShader : public PixelShader
{
Pixel operator()( Pixel src, Pixel dest ) const
{
register unsigned int tdest = 0;
register unsigned int alpha = 50;
register unsigned int ialpha = 256 - alpha; //inverse alpha

tdest = (RED_MASK & (((src & RED_MASK) * alpha + (dest & RED_MASK) * ialpha) >>8)) |
(GREEN_MASK & (((src & GREEN_MASK) * alpha + (dest & GREEN_MASK) * ialpha) >>8)) |
(BLUE_MASK & (((src & BLUE_MASK) * alpha + (dest & BLUE_MASK) * ialpha) >>8));

return ((Pixel)tdest);
}
};


Pam
fzammetti wrote:
I'd still like to know what was wrong with my pixel shader though... It seems pretty straight-forward. By the way, I did try resetting to the default shader and then clearing the screen, then doing my custom blitting, but that didn't make any difference either.


Ok, it finally hit me.

(src & RED_MASK) results in the 5 left-most bits, representing the red component, followed by 11 zeros. If you make a color like this

Code:
src = Color(255,0,0)


You end up with src = 1111100000000000.

and (src & RED_MASK) = 1111100000000000 as well.

What you need to do in your original code is to just OR the values back together.

Code:
return(colR | colG | colB)


Pam
Sorry Pam... I was totally with your logic, made perfect sense, but it still doesn't seem to be giving the correct result. Very odd...
fzammetti wrote:
Sorry Pam... I was totally with your logic, made perfect sense, but it still doesn't seem to be giving the correct result. Very odd...


Wooops! The part I left out is that you need to shift your FadeValue up into the same range. See, you were subtracting a FadeValue of 1-255 from a Red value of say, 63488 (1111100000000000), because this overflows the expected range of 0-255 for the Color() function, it wraps around in the palette and you get the funky colors you saw. Here's a version I tested that seems to work, by shifting FadeValue appropriately.

Code:
struct FadeShader : public PixelShader {
Pixel operator() (Pixel src, Pixel dest) const {
int colR = src & RED_MASK;
int colG = src & GREEN_MASK;
int colB = src & BLUE_MASK;
colR = colR - (FadeValue << RED_SHIFT);
colG = colG - (FadeValue << GREEN_SHIFT);
colB = colB - (FadeValue << BLUE_SHIFT);
if (colR < 0) { colR = 0; }
if (colG < 0) { colG = 0; }
if (colB < 0) { colB = 0; }
return (colR | colG | colB);
}
};


Alternatively, you could shift ColR, and ColG down.

Pam
Damn, I should have caught that.

When I stepped through in debug I did noticed the larger values than I expected values and it just didn't dawn on me why it was that way. D'oh!

Hey, what cam I say, I worked all day and came home to my 3-year old wanting to wrestle with me! :)

Thanks for all the help Pam, it is much appreciated!
Friggin Joe · 15 Jan 2003, 02:51 UTC
Just outta curiosity, who won the wrestling match?

Thanks for the question and answers guys, I learned something myself.
fzammetti · 15 Jan 2003, 03:00 UTC — The winner is...
I won. Well, technically Pam won, but I'm taking credit for it ;)

Seriously though, she did find the solution, posted above, it did work in the end.

I can also report that, after all the effort, I got better results by simply using the blending setting. I'm not sure why the shader didn't give as good a results (not even sure what was different, but you can tell watching it), and other parts of the project are more important, so I'll figure it out some other time.

I would however suggest that if anyone needs to do fade effects, blending is probably the easier and better way to do it (Pam can correct me if I'm wrong, but I think blending is just a function of the default shader anyway?).

The game I'm working on will be out in March, you can see where I used it then (it's pretty obvious and right at the start).
fzammetti´s game is going to invade our pocket pcs pretty soon! :D

Some other game made by someone may infestate our pocket pcs soon too :P



Hail to the PocketFrog Spawns!!! 8)
You know, someone should write "Attack Of The Mutant Frogs" as a technology demo of PocketFrog. Wish I had the time! :)

Hey, I happen to have another pixel shader question. I don't know why I can't get the hang of this... Fortunately I keep working around it, but it seems like the logical way to do it... Anyway...

Trying to write what should be a simple greyscale shader. I figured, add the RGB values, divide by three and set RGB to that value (I know the algorithm is correct since I did it with my own library). Here's my shader code:

Code:

struct GreyscaleShader : public PixelShader {
Pixel operator() (Pixel src, Pixel dest) const {
int colR = src & RED_MASK;
int colG = src & GREEN_MASK;
int colB = src & BLUE_MASK;
int finalCol = colR + colG + colB / 3;
return (colR | colG | colB);
}
};


Now, before Pam comes and slaps me for blatant stupidity (which I likely deserve, but I digress...), I did trying shifting the values back as in the fade shader solution. No good.

So, time to play spot the stupid programmer mistake! What's not right here?

I think a collection of various shaders in the PF Developer's Corner file exchange might be in order (once I open it obviously)
In the code you posted, you forgot to return finalCol, and you want to shift the values down before you average them to get something like:

finalCol=((color&red_mask)>>10 + (color&green_mask)>>5 + (blue&blue_mask))/3;
finalCol+=(finalCol<<10 + finalcol<<5);
return finalCol;


The other reason that it may look "odd" when you use your fader is that you're decreasing the colours all by the same delta.

Consider a colour that's comprised of:

Red: 31
Green: 15
Blue: 15

And your decrement is 5 for all three colours. You're just going to be left with a mid-intensity red
:
Red: 16
Green: 0
Blue: 0

after 3 decrements, not the:

Red: 16
Green: 7
Blue: 7
(poor accuracy but nevermind) that you would get by decrementing the intensities proportionally to their start intensity.

Cheers,

Ref.
Pam wrote:
Code:
struct FadeShader : public PixelShader {
Pixel operator() (Pixel src, Pixel dest) const {
int colR = src & RED_MASK;
int colG = src & GREEN_MASK;
int colB = src & BLUE_MASK;
colR = colR - (FadeValue << RED_SHIFT);
colG = colG - (FadeValue << GREEN_SHIFT);
colB = colB - (FadeValue << BLUE_SHIFT);
if (colR < 0) { colR = 0; }
if (colG < 0) { colG = 0; }
if (colB < 0) { colB = 0; }
return (colR | colG | colB);
}
};


The shader has a slightly greenish tint due to green having one more bit; this fixes it:

Code:
colG = colG - (FadeValue << (GREEN_SHIFT + 1));


Oh, and here's a grayscale shader that works:

Code:

struct GrayShader : public PixelShader {
Pixel operator() (Pixel src, Pixel dest) const {
register int R = src & RED_MASK;
register int G = src & GREEN_MASK;
register int B = src & BLUE_MASK;


R = R >> RED_SHIFT;
G = G >> (GREEN_SHIFT + 1);
B = B >> BLUE_SHIFT;

register int fillColor = (R + G + B) / 3;

return (fillColor << RED_SHIFT |
fillColor << (GREEN_SHIFT + 1) |
fillColor << BLUE_SHIFT);
}
};



One reason why your shader might be wrong is that instead of adding all the colors and dividing them by three, it adds red and green and then adds blue divided by 3. Remember that order of operations is important.
fzammetti wrote:
You know, someone should write "Attack Of The Mutant Frogs" as a technology demo of PocketFrog. Wish I had the time! :)


Thanks for a game idea... no, seriously, I'm not kidding. I have nothing else to do, so I'll just do that. I think PocketFrog needs a few more demos.
Hey, I want writing credits! :) (Just kidding)

I agree, more example source code is good, no matter what it is.

I plan on taking some of the things I've done for my current game project and making short little samples out of them, but that's a month or two away (gotta focus on getting the game done!). I will almost certainly release the full source as well, but probably not for six months or so, until the sales have seriously declined basically.

But "PocketFrog Episode I: Attack Of The Mutant Frogs", I look forward to it!
Yeah... sadly, though, the mutant frogs won't be rendered using faulty pixel shading. :D
Speaking of samples... would someone be so kind as to post the support code for a shader. Like the calls you would use in your gameLoop to implement a shader. I noticed the SetPixelShader function but perhaps an example using the FadeShader would be helpful to me.

Thanks!
display->SetPixelShader(&yourShader;);

yourShader is simply an instance of your pixel shader... display is, well, you know!

From that point on, until you change it, every pixel rendered will be processed by your pixel shader.
If you want, I can whip up a quick shader demo, although I tend to make crappy code.
OK, finished, might be of help:

http://www.probabilitas.com/files/shader.zip

Just a quick demo of 3 lame pixel shaders... tap the stylus to cycle between them.
Cool little demo! :D
I have not touched the pixel shader until now, you just freed me from exaustive testings on how to make it work 8)

I had quite some trouble to make it compile, but after messing with the pocketfrog make workspace I managed to make it build :wink:
Conan · 08 Mar 2003, 08:15 UTC — thanks for the demo
Thanks for the fun demo. This will save lots of us some time.
Another way to do fades:-
I use blending with a filled rectangle to fade out a screen. Just a loop with the SetBlending(int) going from 10 upwards

Code:
m_screen.Set(0,0,239,319);
int blend = 10;// start blend amount
for (int x = 0; x < 16; ++x)
{
display->SetBlending(blend);//transparent rect
display->FillRect(m_screen,Color(35,0,2));
display->Update();
blend+=2;// change blend amount
// Sleep(2);// pause in between fades
}
display->SetBlending(0xFF);//backto no alpha blend


The sample code only fades a part of the screen based on the settings for the m_screen Rect
You don't even need to do anything that complicated under some circumstances... Assuming you want to fade everything on the screen...

Code:

// A global variables...
int fadeVal = 255;
// In GameLoop...
display->SetBlending(255);
display->Clear(Color(0, 0, 0));
display->SetBlending(fadeValue);
// Now draw whatever is on the screen
if (fadeValue > 0) {
fadeValue = fadeValue - 5;
}

I haven't tried, but I'm almost certaint hat if you wanted to fade just a couple of items off the screen, just draw the non-fading images before you do the second SetBlending. For instance, with the Blit demo, if you wanted to fade the chicken and drum and all that off without affecting the background, just draw the background before the second SetBlending and it should do just that. In that case you can also do away with the Clear call, since blitting the non-fading background would basically bave the same effect.
I don´t know if this is already mentioned here but.. this is how you can go back to the default shader:
display->SetDefaultShader();
Thanks, I was wondering about that...