Skip to main content
gamedev.net
🔒 Locked

AmbientCube & Array indices problem

Started by spek 3 replies 850+ views
Original Post
spek
spek
Hi, I was interested in the HL2 ambientCube solution for lighting props in the world. From a certain point, they measure 6 points (up, left, etc.), just like a cubeMap. Since it are only 6 colors, they pass them as a simple array instead of a cubeMap. They get the ambient color for an object with this code:

float3 PixelShaderAmbientLight( const float3 worldNormal, const float3 cAmbientCube[6] )
{
	float3 nSquared = worldNormal * worldNormal;
	int3 isNegative = ( worldNormal < 0.0 );
	float3 linearColor;
	linearColor = nSquared.x * cAmbientCube[isNegative.x] +
	              nSquared.y * cAmbientCube[isNegative.y+2] +
	              nSquared.z * cAmbientCube[isNegative.z+4];
	return linearColor;
}
Would this be faster than doing it with a cubeMap texture? Anyway, I tried the same, but I can't get the shader compiled. Cg sais "profile requires index expression to be comple-time constant" I think I understand the message, the indices in that function are variable. I also read somewhere that GPU's aren't really capable of doing this because of the SIMD architecture. Don't know if that's true, but... HL2 is working on my computer (and also on my older card). Is this really a hardware problem, is Cg outdated/not capable, or something else? Like I said, maybe I could also use an ordinary cubeMap. But maybe that's slower? [edit] Yet another way would be coding it like this:

  if ( worldNormal.x < 0 )  
    xColor = cAmbientCube[0]; else
    xColor = cAmbientCube[1];
  ...
  return nSquared.x * xColor + nSquared.y * yColor ... etc.
But my mother always told me to avoud if statements in shaders... Greetings, Rick [Edited by - spek on August 24, 2006 6:31:51 AM]
MePHyst0
MePHyst0
What kind of profile are you compiling your shader codea against? Have you tryed switching to a newer profile? Also, did the code that you described at the end of your post(the if- variant) compile?
spek
spek
Hi,

I'm using fragment profile 40. The "if" code compiled indeed. That worked good, but maybe the HL2 variant is faster. But on a second thought...
"int3 isNegative = ( normal < 0.0 )"
Won't that be 3 "if" statements either? Maybe HLSL compiles it in such a way that the indices aren't really variable either... I don't know.

Greetings,
Rick
MePHyst0
MePHyst0
Try to compile it agains the arbfp1 profile.
spek
spek
That didn't work either. Well, the performance isn't that bad with the "if" code, but I wonder if the usage of "isNegative = ( normal < 0.0 )" is (much) faster...

Greetings,
Rick

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.