• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JDX_John

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

8 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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?

1

Share this post


Link to post
Share on other sites

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.

 

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0