Specular light color (float vs float3)

Started by
21 comments, last by cozzie 10 years, 8 months ago

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).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

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. :)

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

:) 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.

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)

And in the pixel shader:


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];

		}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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. if(a,b,c,d) will actually just evaluate if(d), so in your case if(0.0f). This is false, so the whole if-clause will never be entered, no matter what's in MatSpec.

Try if(any(MatSpec)) instead.

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.. rolleyes.gif

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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);

The above contains 3 MADs, your original had 4 adds and 3 MULs (assuming I counted properly). That's 4 instructions less even when there is no specular. diffuseAcc and specularAcc are 2 float3 variables defined before lighting. Just add your lighting contribution to them in your branches instead of at the end (ex: diffuseDir & diffusePoint). This way you can avoid the extra add in case only one light is used and your final output remains a set of MADs.

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! smile.png

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 smile.png

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement