Home » Community » Forums » » Creating a GLSL Library
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Creating a GLSL Library
Post Reply 
Very useful, thanks for sharing!

 User Rating: 1743   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Im wondering if the code presented is efficient enough to be used in games? I believe SM2 cards have a huge slowdown with if statements which seem to be used all over the place in this article.

 User Rating: 1185   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

First, thanks for the article :)

Second, I've to hint at this (AFAIK) error :(
Quote:
Creating a GLSL Library, page The Phong Lighting Shader
OpenGL uses some cues to determine what type of light is being passed to it. Directional lights are assumed to have a position where the w value corresponds to 1.0. A Point Light has a w value of 0.0 in its position and a value of 180.0 for their spotCutoff value.

I'm pretty sure that it is the other way around: Point lights have w==1 in their GL_POSITION parameter, and directional lights have w==0 in their GL_POSITION parameter. That is the typical (normalized) distinction between points and directions. I assume it is not reverse in GLSL!?

 User Rating: 1742   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Thanks for the feedback on the article.

Quote:
Original post by GamerSg
Im wondering if the code presented is efficient enough to be used in games? I believe SM2 cards have a huge slowdown with if statements which seem to be used all over the place in this article.


I honestly didn't run too many tests on various hardware to figure out the performance of the shader so your mileage may vary. Multiple lights seemed to choke the 6600 Go I was developing on, but were handled much better by a 7800 I had available to test on.

The article was more about piecing together the lighting equations in a general purpose way, and to understand what they do, so I didn't take speed into account.

For example the ShaderGen program will generate code specifically for the lights that are enabled and determine their type so there are no conditional statements. If you already know what type of lights will be used in your scene just move the calculateLighting function to the fragment code where main resides and do it in a similar fashion as ShaderGen.

And with regard to the if statements in the functions technically the if statements should result in the GPU having fewer operations to do. The operations within the if statements can be moved outside of them and you'll get the same results. So you may want to make those optimizations for an older card.

Quote:
Original post by haegarr
I'm pretty sure that it is the other way around: Point lights have w==1 in their GL_POSITION parameter, and directional lights have w==0 in their GL_POSITION parameter. That is the typical (normalized) distinction between points and directions. I assume it is not reverse in GLSL!?


You're correct this is a typo that I didn't catch in the article. The code for the library is correct though and when w == 0 the directional light is used in PhongLighting.frag.

I'll see if I can't get that fixed.

Any more feedback is greatly appreciated.

[Edited by - donny dont on October 31, 2007 4:45:38 PM]

 User Rating: 1041   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Awesome article.

One question: what could be the best way to unroll the loops found in calculateLighting() in order to make the library work with glsl 1.2?


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Sorry for the late reply its been awhile since this article was published.

void calculateLighting(in vec3 N, in vec3 V, in float shininess,
                       inout vec4 ambient, inout vec4 diffuse, inout vec4 specular)
{
	// Check light 0
	if (gl_LightSource[0].position.w == 0.0)
		directionalLight(0, N, shininess, ambient, diffuse, specular);
	else if (gl_LightSource[0].spotCutoff == 180.0)
		pointLight(0, N, V, shininess, ambient, diffuse, specular);
	else
		 spotLight(0, N, V, shininess, ambient, diffuse, specular);

	// Copy the above and switch out 0 for the index for each light
	....
}




Rather than using the loop you can just replicate the code over again. Since its a little repetitive it might be better to just create a macro. I haven't played around with macros in GLSL but assuming they behave the same as C/C++ the following should work.

#define CALCULATE_INDIVIDUAL_LIGHT(i)
if (gl_LightSource[i].position.w == 0.0)
    directionalLight(i, N, shininess, ambient, diffuse, specular);
else if (gl_LightSource[i].spotCutoff == 180.0)
    pointLight(i, N, V, shininess, ambient, diffuse, specular);
else
     spotLight(i, N, V, shininess, ambient, diffuse, specular);



NOTE: I didn't include the \ to continue the macro since it made everything display on one line so don't forget to add them back.

Hope this helps.

 User Rating: 1041   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: