Sign in to follow this  

Should I always use float4 ?

This topic is 663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by Endemoniada

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

This topic is 663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this