• Create Account

## Where to calculate TBN matrix?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1cozzie  Members

4630
Like
0Likes
Like

Posted 23 May 2014 - 11:39 AM

Hi,

In my normal mapping implementation I currently calculate the TBN matrix in my vertex shader and pass it to the pixel shader.

There I take the normal map normal and using the TBN matrix transform it to world space, for lighting calculations.

All works fine.

Now I was wondering, in some implementations I've seen, people send the normal/binormal/tangent from the VS to the PS and build the TBN matrix in the pixel shader. Or somewhere in between, do the world transformation of those 3 vectors in the VS, send to PS and normalize there + build TBN matrix.

I've tried out both approaches but with no visible difference.

Can someone tell me why it would be beneficial to create the TBN matrix in a pixel shader instead of the vertex shader?

(the final normal is still for the pixel, because I transform the normal from the normal map in the pixel shader)

Note: when I calculate the TBN matrix in the VS, I also normalize the end resulting normal (normalize(mul(normalMapNormal, TBN)).

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

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

### #2phil_t  Members

7649
Like
3Likes
Like

Posted 23 May 2014 - 11:48 AM

Now I was wondering, in some implementations I've seen, people send the normal/binormal/tangent from the VS to the PS and build the TBN matrix in the pixel shader.

What do you mean by "build the TBN matrix"? Like this?:

float3x3 tbn = float3x3(input.Tangent, input.Binormal, input.Normal);

That's a no-op, it doesn't make a difference where you do it. It doesn't change anything or add any shader instructions - it's just syntactic sugar so you can do mul(normalMapNormal, tbn) instead of float3(dot(normalMapNormal, input.Tangent), dot(normalMapNormal, input.Binormal), dot(normalMapNormal, input.Normal).

### #3newtechnology  Members

1549
Like
1Likes
Like

Posted 23 May 2014 - 12:34 PM

Also binormal is wrongly used term, it is bitangent.

### #4cozzie  Members

4630
Like
0Likes
Like

Posted 23 May 2014 - 01:16 PM

Thanks.

That makes sense.

So basically it doesn't matter if I send 3 float3's from the VS to the PS or 1 float3x3.

And how about multiplying the VS input normal/tangent/bitangent with the world matrix?

I'd say I do that in the VS because then it doesn't need to be executed per pixel (and the result is the same I suppose?)

Same for normalizing the result of normal/tanget/bitangent multiplied with the world matrix.

Would you do those 2 actions in the VS?

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

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

### #5phil_t  Members

7649
Like
0Likes
Like

Posted 23 May 2014 - 01:26 PM

So basically it doesn't matter if I send 3 float3's from the VS to the PS or 1 float3x3.

Correct.

And how about multiplying the VS input normal/tangent/bitangent with the world matrix?
I'd say I do that in the VS because then it doesn't need to be executed per pixel (and the result is the same I suppose?)

Yes, exactly. Do it in the VS for this reason.

Same for normalizing the result of normal/tanget/bitangent multiplied with the world matrix.
Would you do those 2 actions in the VS?

Interpolated unit vectors don't retain their unit length (consider what would happen if they pointed 180 degrees from each other and were weighted 0.5 each). So you'll need to re-normalize in the pixel shader if you want correct results (you'll probably want to keep normalizing in the vertex shader too).

Edited by phil_t, 23 May 2014 - 01:27 PM.

### #6cozzie  Members

4630
Like
0Likes
Like

Posted 23 May 2014 - 04:11 PM

Thanks, I've got it in place.

Here it is (just the relevant code snippets):

struct VS_OUTPUT
{
float4 	Pos			: POSITION0;
float3 	NormalWorld		: TEXCOORD1;
float2 	TexCoord		: TEXCOORD2;
float3 	wPos			: TEXCOORD3;
float3 	ViewDir		: TEXCOORD4;
float3	BiNormalWorld	: TEXCOORD5;
float3	TangentWorld	: TEXCOORD6;
};

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.TexCoord = input.TexCoord;
Out.wPos = worldPosition.xyz;
Out.ViewDir = normalize(CameraPos - Out.wPos.xyz);

// Worldspace to Tangent space for normalMapping
Out.TangentWorld	= normalize(mul(input.Tangent, (float3x3)World));
Out.BiNormalWorld	= normalize(mul(input.Binormal, (float3x3)World));
Out.NormalWorld	= normalize(mul(input.Normal, (float3x3)World));

return Out;
}

// part of the pixel shader

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

float3x3 worldToTangent = float3x3(	normalize(input.TangentWorld),
normalize(input.BiNormalWorld),
normalize(input.NormalWorld));

float3 normalMap = normalize(2.0 * (tex2D(normalMapSampler, input.TexCoord).xyz) - 1.0);
normalMap = normalize(mul(normalMap, worldToTangent));



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

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

### #7newtechnology  Members

1549
Like
0Likes
Like

Posted 24 May 2014 - 12:32 PM

8 normalize calls and each one involves a square root. In the frank d. Luna's implementation (which i use), it is only 1 normalize call in pixel shader.

### #8osmanb  Members

2081
Like
0Likes
Like

Posted 24 May 2014 - 02:26 PM

Although it's not directly related to the TBN matrix, I want to point out that you can't compute the View vector in the vertex shader and interpolate that like you're doing. It doesn't interpolate linearly in world space. To get correct view vectors, you need to - unfortunately - send world space position in an interpolator, and then compute the view vector in the pixel shader.

### #9cozzie  Members

4630
Like
0Likes
Like

Posted 24 May 2014 - 06:14 PM

Thanks both.
@Newtechnology: how would you do that with just one normalize in the PS, you skip normalizing the vectors when they come into the PS? (normal, bitangent and tangent)

@OsmanB: but do I need the view vector per pixel instead of per vertex?

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

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

### #10Hodgman  Moderators

49414
Like
1Likes
Like

Posted 24 May 2014 - 06:25 PM

Thanks both.
@Newtechnology: how would you do that with just one normalize in the PS, you skip normalizing the vectors when they come into the PS? (normal, bitangent and tangent)

Yeah, you 'construct' the TBN matrix using non-normalized N, B and T variables. Then when you use this matrix to transform your normal-map value into a normal, it won't be normalized either -- so you normalize this final value only.
It's probably very slightly less accurate, but is much cheaper!

### #11newtechnology  Members

1549
Like
1Likes
Like

Posted 25 May 2014 - 12:34 AM

Thanks both.
@Newtechnology: how would you do that with just one normalize in the PS, you skip normalizing the vectors when they come into the PS? (normal, bitangent and tangent)

@OsmanB: but do I need the view vector per pixel instead of per vertex?

//---------------------------------------------------------------------------------------
// Transforms a normal map sample to world space.
//---------------------------------------------------------------------------------------
float3 NormalSampleToWorldSpace(float3 normalMapSample, float3 unitNormalW, float3 tangentW)
{
// Uncompress each component from [0,1] to [-1,1].
float3 normalT = 2.0f*normalMapSample - 1.0f;

// Build orthonormal basis.
float3 N = unitNormalW;
float3 T = normalize(tangentW - dot(tangentW, N)*N);
float3 B = cross(N, T);

float3x3 TBN = float3x3(T, B, N);

// Transform from tangent space to world space.
float3 bumpedNormalW = mul(normalT, TBN);

return bumpedNormalW;
}

float3 normalMapSample = gNormalMap.Sample(samLinear, pin.Tex).rgb;
float3 bumpedNormalW = NormalSampleToWorldSpace(normalMapSample, pin.NormalW, pin.TangentW);

this approach calculates bitangent in pixel shader.

Edited by newtechnology, 25 May 2014 - 12:40 AM.

### #12cozzie  Members

4630
Like
0Likes
Like

Posted 25 May 2014 - 03:01 AM

Thanks, in my case it would come down to this:

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

// more stuff

#ifdef USENORMALMAP
// Worldspace to Tangent space for normalMapping
Out.TangentWorld	= mul(input.Tangent, (float3x3)World);
Out.BiNormalWorld	= mul(input.Binormal, (float3x3)World);
Out.NormalWorld	= mul(input.Normal, (float3x3)World);
#else
Out.Normal = normalize(mul(input.Normal, (float3x3)World));
#endif

}

// and the PS

float4 PS_function(VS_OUTPUT input): COLOR0
{
// more stuff

#ifdef USENORMALMAP
float3x3 worldToTangent = float3x3(input.TangentWorld,
input.BiNormalWorld,
input.NormalWorld);

float3 finalNormal = normalize(2.0 * (tex2D(normalMapSampler, input.TexCoord).xyz) - 1.0);
finalNormal = normalize(mul(finalNormal, worldToTangent));

#else
float3 finalNormal = normalize(input.Normal);
#endif



I do indeed see no visual difference. Save me quite some normalizations now.

Maybe I can do the same when I don't use normal mapping, only normalize the normal in te PS, not in the VS also?

@OsmanB: thanks, I think I can pass the viewdir like this:

// in the VS

Out.ViewDir = CameraPos - Out.wPos.xyz;

// then in the ps

float3 ViewDir = normalize(input.ViewDir);



The result is so much nicer, and correct now probably

@Newtechnology: thanks, I tried it out sending just 2 out of 3 and calculating the bitangent in the PS, but I don't see the avantages yet (maybe that's about taste), I might do it when I'm gonna use DXT5 compressed normal maps where I leave 1 channel out and calculate it through the other 2. I've read that this gives higher quality results, didn't see that yet.

Edited by cozzie, 25 May 2014 - 03:32 AM.

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

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

### #13newtechnology  Members

1549
Like
0Likes
Like

Posted 25 May 2014 - 04:29 AM

@Newtechnology: thanks, I tried it out sending just 2 out of 3 and calculating the bitangent in the PS, but I don't see the avantages yet (maybe that's about taste), I might do it when I'm gonna use DXT5 compressed normal maps where I leave 1 channel out and calculate it through the other 2. I've read that this gives higher quality results, didn't see that yet.

you save memory by storing only tangent.

### #14cozzie  Members

4630
Like
0Likes
Like

Posted 25 May 2014 - 05:14 AM

Ok, but on the other hand it costs performance to calculate it every run of the pixelshader

Probably an example of possible 'pre-optimization', untill I would do some profiling

Edited by cozzie, 25 May 2014 - 05:14 AM.

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

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

### #15newtechnology  Members

1549
Like
0Likes
Like

Posted 25 May 2014 - 05:17 AM

Ok, but on the other hand it costs performance to calculate it every run of the pixelshader

Probably an example of possible 'pre-optimization', untill I would do some profiling

that calculation won't slow down any modern GPU. Although if you have 100 models each with 10,000 vertices, then also it [bitangent] would only take 0.99 MB of space.

Edited by newtechnology, 25 May 2014 - 05:22 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.