PocketMatrix forums

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

Shaders

PsychoChris · 09 Nov 2006, 08:49 UTC
Hello,

the current shader implementation doesn't provide the coordinates of the pixel beeing processed. I thought about doing shader stuff manually by locking a surface and manipulating its pixels by hand.
Now i'm asking myself if this would have the same performance like using the given shader class or does it use some optimizations?

Greetings and thanks,
Chris
You could add the "x" and "y" coordinates to the shader framework... I didn't because then you will also need to pass in the width/height somehow. And adding so many parameters would complicate the shader / might hinder optimization.

It's worth a try... But to answer your question, no there is no specific optimizations.
PsychoChris · 14 Nov 2006, 04:35 UTC
I didn't because then you will also need to pass in the width/height somehow.

I don't get it why i would have to pass the width and height too.

The current pixel position is necessary for me for writing a light or a bump map shader. This will probably have great impact on the performance so i'm curious if it makes sense to do it anyway.


You could add the "x" and "y" coordinates to the shader framework...

How can i do that? Where do i get the coordinates from?

Greetings and thanks,
Chris
If you don't pass in the width/height, then you won't be able to tell if you are reading outside the surface. I guess it depends on what you are trying to achieve with your shaders... You might or might not need the width/height. But you will need to pass in the "x" and "y" coordinates for sure, since this is what you asked about =)

Simply take a look where the shaders are implemented and add "x" and "y" and any other parameteres you want. Try to compile, you should get errors in the rasterizers. Fix them and you should be mostly good.
PsychoChris · 14 Nov 2006, 20:56 UTC
I'm sorry for bothering you again, but i don't know how to insert the x and y coordinates into a shader. Main problem is that i don't know where to get the current pixel coordinates from. I guess i would need something like this:
Code:
struct CustomShader: binary_function<Pixel,Pixel,Pixel>
{
CustomShader( PixelShader* shader ) : m_shader(shader) {}

Pixel operator()( Pixel src, Pixel dest, Point pos ) const
{
// Do my stuff here...
// Put result into rocessed_pixel

return processed_pixel;
}

private:
PixelShader* m_shader;
};


But where and how do i provide the shader with the current pixel coordinates? I'm not even shure this is the way to do it...

Greetings,
Chris
Yes this is the right way. What I am saying is: do it, try to compile, and the compiler will tell you where you need to provide the (x,y) position.

I am just trying to help you through it instead of doing it for you (ie: I don't have the time to do it myself, nor have the need for it).
PsychoChris · 15 Nov 2006, 05:02 UTC
Thank you for your help. After digging deeper into the template stuff for pixel operations i finally managed to get pixel shader lights working.
I found some comments that you intended to change the template stuff and exchange it with new shader code. I'm curious: What exactly did you want to change?

Greetings,
Chris
Glad to hear you made it work!

As for what I wanted to change, I am not sure I remember. I think I wanted the ability to add an arbitrary number of parameters to the system so that you could easily provide things like... x,y.

=)
PsychoChris · 16 Nov 2006, 22:04 UTC
I don't know if i should laugh or cry after reading that... ;-)

btw: I send you some days an email through this board but didn't get a reply. Is the email functionality working or perhaps your email here out of date?

Greetings,
Chris
I did get an email from you asking about this x,y thing in shaders. I also saw the post here and decided to answer here. This way everyone benefits from the conversation.

If your email was something else, then I am afraid I did miss it.
PsychoChris · 17 Nov 2006, 01:47 UTC
After testing the modified shaders i found out that they are about 25% to 30% slower. I didn't use any performance eating light shaders, just the grayscale shader from the samples. So much performance loss only because passing a class Point parameter? Can this really have that much of impact?

Greetings,
Chris
PsychoChris wrote:
So much performance loss only because passing a class Point parameter? Can this really have that much of impact?


I'd say it's a good result, especially if you pass not just x and y, but the whole Point class.
PsychoChris · 18 Nov 2006, 02:28 UTC
It still surprises me that the amount of parameters passed, especially if its just one object of the Point class, influences performance so much. I will try out passing only x and y in different variations to see how much performance changes i can see.

Next problems i'm facing with the light shader are the slow floats and a sqrt for calculating the distance between two points like sqrt(p1.x*p1.x + p2.x*p2.x). Any ideas how this could be improved? Perhaps somehow without the need of floatingpoint calculations.

btw jaguard: Can you give me an idea when your current game will be finished? I'm waiting for it. :-)

Greetings,
Chris
still surprises me that the amount of parameters passed, especially if its just one object of the Point class, influences performance so much.

Well, of course. You will additionally pass Point class into every single pixel.

What actually puzzles me, is that Point class' sizeof() returns 8. It contains two functions and two operator overloadings - and I wonder where it's stored :).

I will try out passing only x and y in different variations to see how much performance changes i can see.

What may work is that you "pack" your x and y into one 16bit - you will then be sending 4 times less data for each pixel.

So your x and y will be limited to 255, but that should be enough for most of the cases.

btw jaguard: Can you give me an idea when your current game will be finished? I'm waiting for it.

When it's done you know :).
PsychoChris wrote:
Next problems i'm facing with the light shader are the slow floats and a sqrt for calculating the distance between two points like sqrt(p1.x*p1.x + p2.x*p2.x). Any ideas how this could be improved? Perhaps somehow without the need of floatingpoint calculations.


There's a nice sqrt table in the PocketHAL firework demo, you might try using that if you're not already.
PsychoChris · 23 Nov 2006, 17:53 UTC
@ Crayfish:

Thank you for the hint but i already came up with the same idea before. I wrote a tool that generates me tables for different purposes. The table in the fire sample would only help me in eliminating the sqrt. My two dimensional tables now also eliminate multiplications and return the precalculated values for the distances between two points. Together with some other improvements i got enough improvement on performance to suit my needs.

Greetings,
Chris
PsychoChris wrote:
Next problems i'm facing with the light shader are the slow floats and a sqrt for calculating the distance between two points like sqrt(p1.x*p1.x + p2.x*p2.x). Any ideas how this could be improved? Perhaps somehow without the need of floatingpoint calculations.


It could be better to avoid sqrt or sqrt table, if you just calculate p1.x*p1.x + p2.x*p2.x.
This is very handy when you compare the distance with some other number (raised in power of two of course).