Jump to content
  • Advertisement
Sign in to follow this  
funkeejeffounet

OpenGL GLSL - Loop in a fragment shader

This topic is 4903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am currently developping a scientific visualisation software in OpenGL, GLUT and GLSL. My graphic card is a GeForce 6600 256MB. As all the NV4x should support the new ps3.0 and vs3.0, I am using a loop in my fragment shader(a static one, so my stop condition is a constant). Unfortunately, it seems that the code is running in software(I must a have a frame every 5 seconds) and I can't figure out why, is there a lack of support for GLSL and the ps3.0 in the drivers? Please if someone has experienced some trouble using loops in shaders, I would appreciate some help. Thanks in advance, Jeff.

Share this post


Link to post
Share on other sites
Advertisement
Loop termination condition value should be a constant. It cannot loop more that 256 times. Other than that I can't think of anyother reason. I've used loops but had no problems. Can you post the fragment shader code ?

Share this post


Link to post
Share on other sites
Well actually I thought that the size of the fragment shader was limited, so maybe once unrolled my loop takes too much instructions(there is a atan in it).

Here is the code :

varying vec3 var_position;
uniform sampler2D pbuffer;
uniform sampler3D plasma_texture;

void main (void)
{
int i;
vec4 old_position;
vec3 position, pas;
float s, t, r, result, voxel;
position = var_position;
old_position = texture2D(pbuffer, vec2(gl_FragCoord));
if (gl_FragCoord.z <= old_position.a)
discard;
pas = (vec3(old_position) - position) / 11.0;
result = 0.0;
for (i=0; i<10; i++)
{
s = length(vec2(position.x, position.z)) - 4.5;
t = position.y + 0.5;
r = atan(position.z, position.x);
if (r < 0.0)
r = 3.14159 - r;
r = r / 6.2832;*/
voxel = float(texture3D(plasma_texture, vec3(s, t, r)));
result += voxel;//float(texture3D(plasma_texture, vec3(s, t, r)));
position += pas;
}
result /= 11.0;
gl_FragColor = vec4(1.0);//result);
}


Even when the loop teminaison is 1 the code is slow.

I take any advice, so give me up any ideas :), thanks.

Cheers Jeff.

Share this post


Link to post
Share on other sites
Interesting, the code *looks* fine. Have you checked the output of the compilation and linking stages using glGetInfoLog? If you're lucky there might be some warnings to point you in the right direction.

Share this post


Link to post
Share on other sites
if (gl_FragCoord.z <= old_position.a)
discard;

--> Usually the if/else statements break the flow and make the programs slow so try not to use them.

r = atan(position.z, position.x);

--> This should not slow you down a lot.

if (r < 0.0) --> Again an if statement!
r = 3.14159 - r;
r = r / 6.2832;*/ --> what is the "*/" doing here ?

voxel = float(texture3D(plasma_texture, vec3(s, t, r)));

--> texture3D() returns a four component vector, how are you type casting it to float ? What is your intention ?

What graphics card and drivers are you using ?
Are you creating a floating point pbuffer ?

Share this post


Link to post
Share on other sites
Quote:
Original post by dimensionX
--> Usually the if/else statements break the flow and make the programs slow so try not to use them.

Yes, but not by the extent to which he's describing. At least, assuming he's not trying to render a ridiculous number of polygons with this fragment shader.

Speaking of which funkeejeffounet, just what type of scene are you rendering and how much of it is using this shader?

Quote:
r = r / 6.2832;*/ --> what is the "*/" doing here ?

Didn't notice that. I'm guessing the compiler would be spitting out a warning and ignoring it.

Quote:
voxel = float(texture3D(plasma_texture, vec3(s, t, r)));

--> texture3D() returns a four component vector, how are you type casting it to float ? What is your intention ?

Type casting to float will take the first component of the vector.

Share this post


Link to post
Share on other sites
I must have the if statement so I'll keep them, I guess it won't slow down too much the code though...

For the texture, you can specify any type of textures nowadays in openGL, and they are not forced to be tight to graphics data(thanks to the shaders). So mine have the internal structure of 8 unsigned bits, and I cast them to float so I can do a division with accuracy on it(however i could cast to float after the add operations).

For the "*/", it was just that I deleted some comments so the code will be more readable on the forum.

Besides, the shaders compile well, but i have got no info in the log. On my ATI 9600XT, the log always tell me if the code will run in software or not, it is pretty handy in fact. That is why I bought a 6600 so that i could use loops in my shaders, but here are the results...

Really weird one here, hope i ll have this fix up. Any ideas? :-)

Cheers, Jeff.

Share this post


Link to post
Share on other sites
Don't go by the OpenGL specification. So far there is no perfect GLSL compiler expect one provided by 3D Labs. When you query a texture, even though the texture is unsigned byte, the value returned by any texture query command is always float.

You din't answer my question, Are you using floating point pbuffers ?

Some of the cards even though they don't complain while creating a floating point pbuffer but they terribly slow down, since the hardware does not support it and tries to emulate in software.

Share this post


Link to post
Share on other sites
Hi dimensionX,

Well yes I am using a float pbuffer, but I checked the attributes of my pixel format concerning the acceleration(WGL_ACCELERATION_ARB) and I get a WGL_FULL_ACCELERATION_ARB so my pbuffer is fully supported by my 6600. It doesn't seems to be the origin of my problem.

More importantly, I found on the net some issues about the drivers concerning the GLSL. I am currently using the newest version from nvidias site(version 71.84), what is your version of it?
Although look at this thread. I can't run the demo(it is overflowing my RAM), so I am beginning to think that the problem really comes from windows or my graphic driver : http://www.delphi3d.net/forums/viewtopic.php?t=434

Thanks for your help,
Cheers, Jeff.

Share this post


Link to post
Share on other sites
Ok I resolved my issue.

First of all the 71.84 drivers are buggy, I switched to the beta ones(76.10) and everything works great now.

The float pbuffet isn't a speed bottleneck, but I could notice a huge performance boost by using 16bits floats rather than 32bits floats in my pixel format descriptor.
I strongely suggest everyone not using 32bits float pbuffers on Nvidia cards. As for ATI, when using my radeon 9600, switching from 16 bits to 32 bits wasn't really noticeable.

I really hope the next official Nvidia driver release will get better cause even with the beta driver, I get a 49% score with "GLSL parser test"(from 3DLabs), guess they still have a lot of work to do before fully supporting GLSL...

Cheers, Jeff.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!