Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


[Cg] Can this perpixel phong lighting shader be optimised?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 JDX_John   Members   -  Reputation: 284

Like
0Likes
Like

Posted 19 February 2014 - 06:08 AM

This Cg shader works nicely and has already been partially optimised by pre-combining light & material values into the lightXXX parameters:

void lighting_phong_perpixel_VP(
in float4 position : POSITION,
in float3 normal : NORMAL,
in float2 texCoord0 : TEXCOORD0,
out float4 oPosition : POSITION,
out float4 objectPos : TEXCOORD0,
out float3 oNormal   : TEXCOORD1,
out float2 oTexCoord0: TEXCOORD2,
uniform float4x4 worldViewProj)
{
  oPosition = mul(worldViewProj, position);
  objectPos = position;
  oNormal = normal;
  oTexCoord0 = texCoord0;
}

void lighting_phong_perpixel_FP(
in float4 position  : TEXCOORD0,
in float3 normal    : TEXCOORD1,
in float2 uv0     : TEXCOORD2,
out float4 color     : COLOR,
uniform float3 lightAmbient,
uniform float3 lightDiffuse,
uniform float3 lightSpecular,
uniform float  shininess,
uniform float4 lightPosition,
uniform float3 eyePosition,
uniform sampler2D texture)
{
  float3 N = normalize(normal);
  float3 texColor = tex2D(texture, uv0).xyz;
  
  // calculate diffuse & specular lighting coefficients
  float3 L = normalize(lightPosition.xyz - (position * lightPosition.w));
  float3 V = normalize(eyePosition - position.xyz);
  float3 H = normalize(L + V);
  float4 lighting = lit(dot(N,L),dot(N,H),shininess);

  float3 ambient = texColor * lightAmbient;
  float3 diffuse = texColor * lightDiffuse * lighting.y;
  float3 specular = lightSpecular * lighting.z;

  color.rgb = ambient + diffuse + specular;
  color.a = 1;
}

I was thinking that I should somehow be able to multiply the vector result of lit() with a vector (texColor,texColor,1,1) in some sort of matrix, so ambient, diffuse & specular can be calculated in one operation. But I am rusty at matrices... can this be done and would it actually be faster?

 

Also, do I need to re-normalise the normal or can I expect it to be close enough as a result of interpolation? 

 

Note I know I could pull stuff into the VS but that's a separate topic, this is deliberately doing it all perpixel as very large polys are involved.


Edited by JDX_John, 19 February 2014 - 06:11 AM.

www.simulatedmedicine.com - medical simulation software

Looking to find experienced Ogre & shader developers/artists. PM me or contact through website with a contact email address if interested.


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7789

Like
0Likes
Like

Posted 19 February 2014 - 06:30 AM


float3 H = normalize(L + V);

L and V are already normlized, therefor:

float3 H = (L+V)*0.5;

... this was really embarrassing wacko.png

 

 

 


float3 ambient = texColor * lightAmbient;
float3 diffuse = texColor * lightDiffuse * lighting.y;
float3 specular = lightSpecular * lighting.z;

color.rgb = ambient + diffuse + specular;

You can save one mult here

color.rgb = texColor * (lightAmbient+lightDiffuse * lighting.y)+lightSpec*light.z;


Edited by Ashaman73, 20 February 2014 - 12:32 AM.


#3 JDX_John   Members   -  Reputation: 284

Like
2Likes
Like

Posted 19 February 2014 - 06:52 AM

If L = (1,0,0) and V = (0,1,0) then (L+V) * 0.5 = (.5,.5,0) which is NOT normalised.


www.simulatedmedicine.com - medical simulation software

Looking to find experienced Ogre & shader developers/artists. PM me or contact through website with a contact email address if interested.


#4 kauna   Crossbones+   -  Reputation: 2741

Like
0Likes
Like

Posted 19 February 2014 - 10:50 AM

Perform lighting in view space and the eye position will be always (0,0,0). May not be a huge advantage, since all the positions and normals must be in the same space too. 

 

Cheers!



#5 L. Spiro   Crossbones+   -  Reputation: 13924

Like
0Likes
Like

Posted 19 February 2014 - 11:39 AM

Your handling of ambient is wrong.  The texture color should be modulated with the diffuse component of the light only, not the ambient component.  If you want lights to have ambience (which they should not, generally) then another input should added on the object material for modulation with the light’s ambient.

 

Also, as mentioned, perform lighting in view space, not world space.

 

 

Most of your performance when using such a simple shader will actually be lost to simple things you are doing outside the shader such as not checking for redundant shader sets or uniform uploads.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 d000hg   Members   -  Reputation: 782

Like
1Likes
Like

Posted 20 February 2014 - 04:12 AM

I don't know if it affects your comments, but the ambient is the world ambient value not a per-light one. It's actually a pre-multiplied value world.ambientLightColor * material.ambient. But you're saying ambient (and presumably emissive if used) should be added directly to the final colour rather than combined with the texture?



#7 JDX_John   Members   -  Reputation: 284

Like
0Likes
Like

Posted 20 February 2014 - 04:16 AM

That was me by the way, old login.


www.simulatedmedicine.com - medical simulation software

Looking to find experienced Ogre & shader developers/artists. PM me or contact through website with a contact email address if interested.


#8 Hodgman   Moderators   -  Reputation: 30882

Like
1Likes
Like

Posted 20 February 2014 - 05:08 PM

BTW, this is a Blinn-Phong shader, not a Phong shader cool.png

Also, do I need to re-normalise the normal or can I expect it to be close enough as a result of interpolation?

If you're really desperate for optimisations (and you're actually pixel-shading bound), you can omit it... but the centers of poly's will become incorrectly darkened. The resulting artefact will be most noticable on large, curved meshes. Flat poly's (where all vert normals are the same) will still appear correct.

Note I know I could pull stuff into the VS but that's a separate topic, this is deliberately doing it all perpixel as very large polys are involved.

There's still some stuff that you could move to the VS, e.g. 
perVertL = lightPosition.xyz - (position * lightPosition.w);
perPixelL = normalize(perVertL);
However, that's such a small operation, that the cost of an extra interpolant may balance out so there is no net performance win...
 

Your handling of ambient is wrong.  The texture color should be modulated with the diffuse component of the light only, not the ambient component.  If you want lights to have ambience (which they should not, generally) then another input should added on the object material for modulation with the light’s ambient.

I'd say it's correct. Ambient light is just a hack for approximating bounced light, by using a magical light source that comes from everywhere. Generally we only calculate the diffuse contribution of this magic light (no ambient specular), so the ambient light value should be plugged into the diffuse BRDF, which is equal to the diffuse/albedo-texture colour.
In the physical world, there's no such material that is red if the path to the light source is straight, but blue if the path is indirect (bounces off reflectors), so there's no need for separate diffuse/albedo and ambient-albedo maps.

 



#9 Styves   Members   -  Reputation: 1051

Like
1Likes
Like

Posted 20 February 2014 - 08:41 PM

Read this. It should help you squeeze out some of your redundant shader instructions.


Edited by Styves, 20 February 2014 - 08:42 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS