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

Started by
7 comments, last by Styves 10 years, 1 month ago

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.

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.

Advertisement

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;

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.

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!

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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?

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.

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.

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

This topic is closed to new replies.

Advertisement