Should I always use float4 ?

Started by
4 comments, last by PhillipHamlyn 8 years, 2 months ago

Hi guys,

I'm still not sure about what variable types to use for buffers and shaders. It's easier to explain with code:


 
// vertex layout
 
D3D11_INPUT_ELEMENT_DESC layout[] = {
 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
 { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
 

I use a float3 for the position, should it instead be float4 ? Also, I like to use a float4 for the uv so I can use the .zw components for custom data but what if I only use the .xy components, should it then be float2 ?

The same question applies to the shader itself:


 
struct VS_INPUT
{
 float3 position : POSITION;
 float4 uv : TEXCOORD0;
};

Should these exactly match the layout (like they do here) ?

This has always confused me. Thanks.

Advertisement
There's no 1 true answer.
In general using float4's does the trick because multiplying with 4x4 matrices is most efficient.

I always balance between that and what I need to achieve what I want. For material properties I use float3's (conventional shading), maybe when I start with PBR that will be float4's also.

The 4th component is also useful to distinguish points and vectors (for correct multiplication etc).

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

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

Float3 is fine for position, there's no need to load your VB with 1s and waste space. Also you don't need to pack extra data into zw, you can make it its own attribute instead.

To transform a float3 just wrap it like float4(v, 1)

I use a float3 for the position, should it instead be float4 ?

Your vertex layouts should usually be as small as possible. In my experience, vertex shaders are very often bottlenecked by memory read speeds, making memory layout of your buffers the biggest performance factor.
Often this means using 8/10/16 bit formats for some attributes (usually position needs to be 32 bit, without fancy schemes in addition to the format change :wink:)

Should these exactly match the layout (like they do here) ?

Yes. If they don't match, it's at wosrt an error, and at best a massive performance pitfall.

The vertex attributes do not make any sence to have any form of alignment themselfs, do not confuse this with verticies alignment (4 bytes multiple at very least,16/32/64 recomended).

The float4 alignment is though yet very good for uniform variables , as float3 or float set in uniforms is not the best option, this is perhaps what got you confused about the float4 aligning.

I recenty converted from XNA4 to SharpDX11 and had a nasty shock because of the need to byte-block align all my constant buffers. Its the only case where I think you need to pad your input to the shader to use space which is "larger than needed" by your application. Because your question is about VB specifically I realise this does not directly relate to your query, but worth pointing out.,

This topic is closed to new replies.

Advertisement