# Specular light color (float vs float3)

## Recommended Posts

cozzie    5029

Hi,

I'm playing around with specular lights and now in doubt if I should multiply the specular term for directional and point lights, by the color of the light. If I understand correctly, then:

- for non-metals, specular highlights are 99% of the time 'white'

- for metals, the specular highlights are mostly the color the material itself

With this in mind my first thought would be to have 2 different shaders.

But I might have found a reasonable solution with relatively high performance benefits, where I need only single float for the specular component instead of float3's (what I would need when I multiply them all with the lightcolor).

Basically:

- calculate specular terms for directional lights and point lights (single floats, no specific color)

- in calculation the final pixel color I do:

return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (MatSpec * (specularDir + specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);



Where I basically use the specular material value that determines the color of the highlights.

Say for glass I set it to 1.0 / 1.0 / 1.0 and for metal, to the actual color of the metal.

The only down side here is that I don't let the specular highlights be dependent of the actual light colors (IF it's a down side..).

What do you think?

##### Share on other sites
Styves    1792

IIRC, Crysis 2 used greyscale specular on PS3 to avoid some tech limitations (pretty sure this was solved for Crysis 3 though). They mention it in this paper (I think it's this one, I can't check since I don't have a ppt viewer on this machine and don't have time to install one, gotta go! :P). As long as you don't have wildly different colored lights and strong specs, it should look fine.

##### Share on other sites
MJP    19754

Specular highlights are always dependent on the color of the light. This doesn't change for metals vs. non-metals.

##### Share on other sites
cozzie    5029

OK. But how would you put that into the equation then?

(if the final specular highlight always takes over the material specular color)

##### Share on other sites
REF_Cracker    966

Hi Cozzie,

Read some articles on Physically Based Shading and you'll get a good idea of how all this works.

These two blog posts are a great starting point

http://seblagarde.wordpress.com/2011/08/17/hello-world/
http://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/

As well as

Crafting Physically Motivated Shading Models for Game Development (Naty Hoffman)

There's a fair amount of information about this out there. It'll take a bit of reading but once you grind through it you'll understand a lot more about how it all fits together.

Greyscale specular was used mostly in light prepass renderers to cut down on bandwidth by storing only diffuse in rgb and specular in the alpha channel of the accumulation buffer.

##### Share on other sites
cowsarenotevil    3004
- for non-metals, specular highlights are 99% of the time 'white'

- for metals, the specular highlights are mostly the color the material itself

I'd change these "rules" to

-for non-metals, the specular highlights are the color of the light

-for metals, the specular highlights are the color of the light multiplied by the color of the material

The truth is that even this corrected version isn't based on any hard theory; it's just that, in practice, specular highlights on non-metals (e.g. plastic, skin, wood) are generally actually caused by a layer of "clear" material on top of the material, be it oil, varnish, etc.

For instance, if you have a shiny ceramic object with gold leaf/trim, there'll be a difference depending on whether the gold is on top of the varnish; if the gold is on top, the gold parts will only have gold-tinted reflections, whereas if the varnish is on top, you'll see something like the sum of the gold-tinted reflections and the un-tinted reflections (which are also present on the ceramic part).

##### Share on other sites
Styves    1792

Specular highlights are always dependent on the color of the light. This doesn't change for metals vs. non-metals.

- for non-metals, specular highlights are 99% of the time 'white'

- for metals, the specular highlights are mostly the color the material itself

I'd change these "rules" to

-for non-metals, the specular highlights are the color of the light

-for metals, the specular highlights are the color of the light multiplied by the color of the material

The truth is that even this corrected version isn't based on any hard theory; it's just that, in practice, specular highlights on non-metals (e.g. plastic, skin, wood) are generally actually caused by a layer of "clear" material on top of the material, be it oil, varnish, etc.

For instance, if you have a shiny ceramic object with gold leaf/trim, there'll be a difference depending on whether the gold is on top of the varnish; if the gold is on top, the gold parts will only have gold-tinted reflections, whereas if the varnish is on top, you'll see something like the sum of the gold-tinted reflections and the un-tinted reflections (which are also present on the ceramic part).

I was going to say the same things but I reread the ops post and realized that we're all reading it wrong. OP isn't saying that materials have white specular highlights, they're saying that 99% of the time their lights are white causing white specular highlights. I think the original message was lost in formatting.

At least that's what I understood. If all of OPs lights will be white, then it's totally fine to go with a greyscale specular term (diffuse alpha type stuff) because there isn't any color to reflect anyway.

Edited by Styves

##### Share on other sites
cowsarenotevil    3004

...

...

I was going to say the same things but I reread the ops post and realized that we're all reading it wrong. OP isn't saying that materials have white specular highlights, they're saying that 99% of the time their lights are white causing white specular highlights. I think the original message was lost in formatting.

At least that's what I understood. If all of OPs lights will be white, then it's totally fine to go with a greyscale specular term (diffuse alpha type stuff) because there isn't any color to reflect anyway.

Well, he also said "(...) in doubt if I should multiply the specular term for directional and point lights, by the color of the light." In any case I hope now he has enough information to figure out what he was looking for.

##### Share on other sites
cozzie    5029

Thanks.

I think I've got it.

To be sure, I've now assumed that:

- specular highlights for non-metals take the color of the actual light

- specular highlights for metals take the color of the specular material color

The non-metal variant is now (for point lights):

specularPoint = pow(saturate(dot(h, normal)), input.SpecPower) * att;

// calculating final color
return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (PointLightColInt * specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);



And the metals variant:

specularPoint = pow(saturate(dot(h, normal)), input.SpecPower) * att;

// calculating final color
return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (MatSpec * specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);


Any suggestions/ remarks?

(please not that specular for directional light is not included yet, that's working it out later, first getting the basics right, before changing for multiple lights, point & directional)

Edited by cozzie

##### Share on other sites
MJP    19754

No that is still not right. It's really simple:

Metals: specular albedo is RGB

Non-metals: specular albedo is monochrome

The lighting is exactly the same in both cases: specular * lightInensity * specularAlbedo. You don't even need different shaders, for non-metals you just always have R, G, and B set to the same value in your material definition.

Edited by MJP

##### Share on other sites
cozzie    5029

Thanks.

I was trying to see if it's possible without using the color of the light, because that would save me 2 float3's.

Because the specular intensity is just a single float:

float diffIntPoint = saturate(dot(normal, lightDir) * att);
diffusePoint += saturate(diffIntPoint * PointLightColInt[i]);	// float3



In the end pixel calculation is now have:

return float4((saturate(AmbientColInt + (MatDiff * (diffuseDir + diffusePoint)) + (MatSpec * (specularDir + specularPoint)) + MatEmi) * textureColor.rgb), textureColor.a);



How should I define specularAlbedo within the shader with 2 'variants'.

I think I should use the MaterialSpec color to distinguish metals / non metals, but I don't understand how to define 'RGB' and 'monochrome' (distinguish them with 1 calculation).

##### Share on other sites
Styves    1792

You don't need to distinguish anything - there are no variants. Monochrome just means the 3 values of your color are the same (255,255,255 for example). Nothing more to it - just pass in a float3 from your material into your shader and use that. Cutting it down to a single float probably isn't going to help much anyway.

...

...

I was going to say the same things but I reread the ops post and realized that we're all reading it wrong. OP isn't saying that materials have white specular highlights, they're saying that 99% of the time their lights are white causing white specular highlights. I think the original message was lost in formatting.

At least that's what I understood. If all of OPs lights will be white, then it's totally fine to go with a greyscale specular term (diffuse alpha type stuff) because there isn't any color to reflect anyway.

Well, he also said "(...) in doubt if I should multiply the specular term for directional and point lights, by the color of the light." In any case I hope now he has enough information to figure out what he was looking for.

I must've totally missed that. Guess my initial thought was right. :)

Edited by Styves

##### Share on other sites
cozzie    5029
Thanks, the good news is that I've got it setup right then already.

If the float's and more become an issue I can always bring back the number of lights, make a version without specular. Or maybe think about a deffered shader.

Thanks all for the help and understanding it better.

##### Share on other sites
Styves    1792

:) Glad you got it working.

If you want, you can try a static branch to detect whether the material uses specular or not. On most modern hardware this branch should be free because the constant is known at run-time. That way you don't need to make an entirely new shader or add permutations to it.

##### Share on other sites
cozzie    5029

Hi Styves,

I've tried your suggestion and did the following, is this what you mean?

Maybe there's a better way to check if the vector/float3 is 0,0,0.

I didn't profile yet (with pix) if there's an advantage in doing the if check/ branch, compared to calculating the specular values without result.

In my code/ engine:

		if(mMaterials[mc].d3dMaterial.Specular.r != 0.0f &&
mMaterials[mc].d3dMaterial.Specular.g != 0.0f &&
mMaterials[mc].d3dMaterial.Specular.b != 0.0f)
{
D3dcolorToFloat4Array(mMaterials[mc].d3dMaterial.Specular, mMaterials[mc].specularF);
}



(specularF is the float arrat that feeds the shader constant)

uniform extern float4	MatSpec		: MATERIAL_SPECULAR = {0.0f, 0.0f, 0.0f, 0.0f};

for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir	+= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]));

if(MatSpec != 0.0f, 0.0f, 0.0f, 0.0f)
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);
specularDir += (pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i]);
}
}

// and for point lights:

if(MatSpec != 0.0f, 0.0f, 0.0f, 0.0f)
{
float3 h = normalize(lightDir + input.ViewDir);

specularPoint += pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i];

}


Edited by cozzie

##### Share on other sites
cozzie    5029

Update; although it compiles, my specular highlights are gone now, probably something with checking the MatSpec float4 array

##### Share on other sites
unbird    8336
Ugh, comma operator. I'm not ugh-ing about your lapse, but rather that such an ugly thing even exists. You don't even get a compiler warning here.

E.g. [tt]if(a,b,c,d)[/tt] will actually just evaluate [tt]if(d)[/tt], so in your case [tt]if(0.0f)[/tt]. This is false, so the whole if-clause will never be entered, no matter what's in [tt]MatSpec[/tt].

##### Share on other sites
cozzie    5029

Thanks, I've got working using the "if(any(..." statement.

Not sure though what the performance gain is, I'll profile it later.

Here's the result, might you have remarks..

The engine code:

		// only save specular material for shader constant when needed
// shader checks 'if(any)' and saves cycles when no specular needed
if(mMaterials[mc].d3dMaterial.Specular.r != 0 &&
mMaterials[mc].d3dMaterial.Specular.g != 0 &&
mMaterials[mc].d3dMaterial.Specular.b != 0)
{
D3dcolorToFloat4Array(mMaterials[mc].d3dMaterial.Specular, mMaterials[mc].specularF);
}
else
{
mMaterials[mc].specularF[0] = 0.0f;
mMaterials[mc].specularF[1] = 0.0f;
mMaterials[mc].specularF[2] = 0.0f;
mMaterials[mc].specularF[3] = 0.0f;
}



Relevant parts of the (pixel) shader:

/** 	DIRECTIONAL LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/
float3 diffuseDir = 0.0f;
float3 specularDir = 0.0f;

for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir	+= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]));

if(any(MatSpec))
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);
specularDir += (pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i]);
}
}


		if(any(MatSpec))
{
float3 h = normalize(lightDir + input.ViewDir);

specularPoint += pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i];
}



In the calculation of the final pixel color I still take the specular components into account.

I might also do the check there (if any) and have 2 final pixel color calculations, don't think that's that big of a gain.

##### Share on other sites
Styves    1792

I don't think it's a problem. If you reorganize your final color output into a series of MADs (a * b + c) then you're only adding a single instruction. Actually, in this case you're actually removing 4 instructions. :)

For exmaple:

// calculating final color
return float4(textureColor.rgb * (MatDiff * diffuseAcc + AmbientColInt) + (MatSpec * specularAcc + MatEmi), textureColor.a);



FYI, your diffuse color map shouldn't be applied to your specular component, only the diffuse lighting (and if it makes sense for you, the emissive). So I've moved the texture color in this code for that purpose. If you want texture on emissive then you can pre-multiply it by the diffuse color elsewhere or by a separate "emissive" texture.

You also don't need the saturate - if you're rendering to LDR then the light will clamp itself, if you're rendering to HDR then you'll want brighter lights anyway.

MADs, ADDs and MULs are all 1 instruction (pretty sure actually that all 3 are actually MADs as far as the GPU is concerned), so be sure to use as many MADS as you can to save some instructions!

Edited by Styves

##### Share on other sites
cozzie    5029

Thanks, I think I've got it.

So summarize:

- combining 2 multiplications + 1 add is done more efficiently on the GPU, so making 'sets' is better for performance

- I can combine the diffuse and specular light float3's for diffuse total and specular total (diffuseAcc/ specularAcc), for both the directional and point lights

I've made the changes and it's all working nice.

Here's my resulting pixelshader, how does it look?

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
float3 normal = normalize(input.Normal);

float3 diffuseAcc = 0.0f;
float3 specularAcc = 0.0f;

/** 	DIRECTIONAL LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/
for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseAcc	+= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]));

if(any(MatSpec))
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);
specularAcc += (pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i]);
}
}

/** 	POINT LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/

for(int i=0;i<MaxPointLights;++i)
{
float3 lightDir = normalize(PointLightPos[i] - input.wPos);

// PER PIXEL ATTENUATION
float dist = length(PointLightPos[i] - input.wPos);

float att = saturate(1 - ((dist - PointLightFPRange[i]) / (PointLightRange[i] - PointLightFPRange[i])));
att *= att;	// optional, not correct for full power range !?!?

// DIFFUSE

float diffIntPoint = saturate(dot(normal, lightDir) * att);
diffuseAcc += diffIntPoint * PointLightColInt[i];		// float3

// SPECULAR; USING BLINN HALF ANGLE

if(any(MatSpec))
{
float3 h = normalize(lightDir + input.ViewDir);
specularAcc += pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i];
}
}

/**	FINAL PIXEL COLOR **/

return float4(textureColor.rgb * (MatDiff * diffuseAcc + AmbientColInt) + (MatSpec * specularAcc + MatEmi), textureColor.a);
}


Ps.; I might be able to raise the max. number of point lights from 3 to 4 for my Shader Model 2.0 versions of my shaders, because of less instructions

On the optimization side;

- I could use one float2 for point light range and full power range, and save another 'instruction'/constant

Do you see any other small things to improve?

I think I've moved as much as possible from the PX to the VS and pre-calculate as much as possible/ acceptable on the CPU side.

Edited by cozzie

##### Share on other sites
Styves    1792

Here's a bit more, you can get a bit of optimizations by reusing your length calculation for the direction normalization.

float3 lightDir = (PointLightPos[i] - input.wPos);

// PER PIXEL ATTENUATION
float dist = length(lightDir);
lightDir = lightDir / (dist + 0.001); // small bias to avoid division by 0 - check to see if it's necessary for you.

Also, to avoid the following:

att *= att;


You can use the squared distance instead of a linear one, it might give you a better falloff without this instruction (and is cheaper then length).

// PER PIXEL ATTENUATION
float dist = dot(lightDir, lightDir);
lightDir /= sqrt(dist) + 0.001; // small bias to avoid division by 0 - check to see if it's necessary for you.

float att = saturate(1 - ((dist - PointLightFPRange[i]) / (PointLightRange[i] - PointLightFPRange[i])));


The sqrt that's needed might be a bit heavier but you're still not doing the full normalize code, you don't need the att*att and you get the right falloff. This is something you'd need to try and see if it works for you. The first tip however is definitely a win.

The last thing is that the shader compiler is smart, but sometimes it misses stuff. So it's best to make sure your code is layed out properly. So for example your diffuse could be this instead, which should guarantee the use of a MAD in the compiled shader rather than an ADD and MUL.

diffuseAcc = diffIntPoint * PointLightColInt[i] + diffuseAcc;

specularAcc = (pow(saturate(dot(h, normal)), MatSpecPower) * att) * PointLightColInt[i] + specularAcc;


This should maybe help a bit. Splitting multiplications based on vector count can also help, so multiply everything that is a scalar first then multiply it by the vector one, that way you don't mix the vector/scalar math together too much and the GPU has a bit of an easier time dealing with it.

Edited by Styves

##### Share on other sites
cozzie    5029

Cool, thanks.

I've put through the first change for the lightdir normalization, the attenuation I left unchanged, because it became to 'round'.
Below are 2 screenshots with the difference.

I have also made 1 float2 for pointlight range (x) and fp range (y).

Gonna put through the changes to all my effect variants now (with 'x'/'y' numbers of lights) and work on a nice demo.

Here's the new PS:

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
float3 normal = normalize(input.Normal);

float3 diffuseAcc = 0.0f;
float3 specularAcc = 0.0f;

/** 	DIRECTIONAL LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/
for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseAcc	= saturate(DirLightColInt[i] * dot(normal,  DirLightDir[i]) + diffuseAcc);

if(any(MatSpec))
{
float3 lightdir = normalize(DirLightDir[i] - input.wPos);
float3 h = normalize(lightdir + input.ViewDir);

specularAcc = pow(saturate(dot(h, normal)), MatSpecPower) * DirLightColInt[i] + specularAcc;
}
}

/** 	POINT LIGHTS - PER PIXEL (DIFFUSE & SPECULAR) **/

for(int i=0;i<MaxPointLights;++i)
{
float3 lightDir = (PointLightPos[i] - input.wPos);

// PER PIXEL ATTENUATION
float dist = length(lightDir);
lightDir = lightDir / (dist + 0.001);

float att = saturate(1 - ((dist - PointLightRange[i].y) / (PointLightRange[i].x - PointLightRange[i].y)));
att *= att;	// optional, not correct for full power range !?!?

// DIFFUSE

float diffIntPoint = saturate(dot(normal, lightDir) * att);
diffuseAcc = diffIntPoint * PointLightColInt[i] + diffuseAcc;	// 1 MAD

// SPECULAR; USING BLINN HALF ANGLE

if(any(MatSpec))
{
float3 h = normalize(lightDir + input.ViewDir);

specularAcc = pow(saturate(dot(h, normal)), MatSpecPower) * att * PointLightColInt[i] + specularAcc;
}
}

/**	FINAL PIXEL COLOR **/

return float4(textureColor.rgb * (MatDiff * diffuseAcc + AmbientColInt) + (MatSpec * specularAcc + MatEmi), textureColor.a);
}



Attenuation with only the normalization optimized (keeping this one :)):

Attenuation with also the other change done (sqrt, no att*att):

##### Share on other sites
cozzie    5029

Cool, with this new knowledge I've also managed to kick my SM2 shader up to 4 point lights instead of 3 :)

No specular yet, maybe something to dig into for the future. But honestly, people should be able to have hardware running SM3

/*******************************************************/
/*******************************************************/

VS_OUTPUT VS_function(VS_INPUT input)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

float4 worldPosition = mul(input.Pos, World);
Out.Pos = mul(worldPosition, ViewProj);

Out.Normal = mul(input.Normal, (float3x3)World);	// VS input = already normalized
Out.TexCoord = input.TexCoord;
Out.wPos = worldPosition.xyz;

/** 	DIRECTIONAL LIGHTS - PER VERTEX (DIFFUSE) **/
float3 diffuseDir = 0.0f;

for(int i=0;i<MaxDirectionalLights;i++)
{
diffuseDir	= saturate(DirLightColInt[i] * dot(Out.Normal,  DirLightDir[i]) + diffuseDir);
}
Out.DiffuseDir = diffuseDir;

return Out;
}

/*******************************************************/
/*******************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
float3 normal = normalize(input.Normal);

/** 	POINT LIGHTS - PER PIXEL (DIFFUSE) **/

float3 diffuseAcc = input.DiffuseDir;

for(int i=0;i<MaxPointLights;++i)
{
float3 lightDir = PointLightPos[i] - input.wPos;

// PER PIXEL ATTENUATION
float dist = length(lightDir);
lightDir = lightDir / (dist + 0.001);

float att = saturate(1 - ((dist - PointLightRange[i].y) / (PointLightRange[i].x - PointLightRange[i].y)));

// DIFFUSE

float diffIntPoint = saturate(dot(normal, lightDir) * att);
diffuseAcc = diffIntPoint * PointLightColInt[i] + diffuseAcc;
}

/**	FINAL PIXEL COLOR **/

return float4(textureColor.rgb * (MatDiff * diffuseAcc + AmbientColInt), textureColor.a);
}