# HLSL Vertex Shader input types and their alignment

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

## Recommended Posts

Hi everyone,

I'm having no luck locating the following information on MSDN (or anywhere in that matter) and I was hoping that someone could help me.

First of all - is there somewhere a specification of what HLSL types are allowed as inputs to vertex shader (a.k.a. vertex attributes)?
Secondly - is there a specification how the above types are suppose to be aligned in the memory on the application side, so that everything works out?

##### Share on other sites
Which versions of Direct3D and HLSL are you using?
For D3D11, the formats that can be used for vertex data are listed here.
For D3D9, they're listed here, but you also have to check the device caps to make sure at runtime. Also, the vertex-declaration types and the HLSL types don't have to match; they'll be cast.

I wasn't aware of any alignment requirements, but the fact that D3D11's element-offset variable is called "[font=courier new,courier,monospace]AlignedByteOffset[/font]" implies there are
Also, it allows you to use D3D11_APPEND_ALIGNED_ELEMENT to specify that you want D3D to figure out the correct offset including padding, but there seems no way to query the automatically configured value of "[font=courier new,courier,monospace]AlignedByteOffset[/font]" after creating your input layout, which means you wouldn't know how to lay out your vertex buffer!?
That is interesting... As a guess, I would assume alignment requirements might be the per-component size of the element, e.g. for [font=courier new,courier,monospace]DXGI_FORMAT_R32G32_FLOAT[/font] alignment would be 4 bytes. Edited by Hodgman

##### Share on other sites

Which versions of Direct3D and HLSL are you using?
For D3D11, the formats that can be used for vertex data are listed here.
For D3D9, they're listed here, but you also have to check the device caps to make sure at runtime. Also, the vertex-declaration types and the HLSL types don't have to match; they'll be cast.

Hm... I had someting else in mind - what HLSL types are allowed in the Shader code. So - can I have 'float4' in the input struct? Yes as far as I know. Can I have a 'sampler'? Probably not. Some specification that can answer this questions is what I'm looking for.

I wasn't aware of any alignment requirements, but the fact that D3D11's element-offset variable is called "[font=courier new,courier,monospace]AlignedByteOffset[/font]" implies there are
Also, it allows you to use D3D11_APPEND_ALIGNED_ELEMENT to specify that you want D3D to figure out the correct offset including padding, but there seems no way to query the automatically configured value of "[font=courier new,courier,monospace]AlignedByteOffset[/font]" after creating your input layout, which means you wouldn't know how to lay out your vertex buffer!?
That is interesting... As a guess, I would assume alignment requirements might be the per-component size of the element, e.g. for [font=courier new,courier,monospace]DXGI_FORMAT_R32G32_FLOAT[/font] alignment would be 4 bytes.

Hmm... This helps a bit. I know that if the VS input is for example:
 struct VSInput { float a : A, float b : B, float4 pos : POSITION } 

Then I need 8 bytes of padding after the 'B' data in vertex buffer, which means that position must be 16 bytes aligned I guess?

##### Share on other sites
AFAIK (someone correct me if I'm wrong here), the order of your attributes in your "[font=courier new,courier,monospace]VSInput[/font]" struct doesn't have to match up with the order of your attributes in memory.
e.g. in memory, you could have [font=courier new,courier,monospace]pos[/font], then [font=courier new,courier,monospace]a[/font], then [font=courier new,courier,monospace]b[/font]. Or, you could have [font=courier new,courier,monospace]pos[/font] and [font=courier new,courier,monospace]b[/font] in one buffer, and [font=courier new,courier,monospace]a[/font] in another buffer.

If your structure was a cbuffer, then yes, the in-memory layout would have to match the HLSL layout. However, HLSL vertex structures are different -- they don't actually exist in memory, they're created at the start of execution of the vertex shader by the "Input Assembler", which reads all the attributes out of the vertex streams.

The "input layout" object is the glue that tells the Input Assembler how to read values out of memory and assemble them into an instance of this structure. When you create an input layout, each element specifies it's layout in memory (which stream, what offset into the stream, what format), and it's location in the HLSL vertex structure (semantic name+index).

I had someting else in mind - what HLSL types are allowed in the Shader code. So - can I have 'float4' in the input struct? Yes as far as I know. Can I have a 'sampler'? Probably not. Some specification that can answer this questions is what I'm looking for.
You can have any type that can be mapped to one of the formats in the "Input assembler vertex buffer resources" I linked to.
e.g. [font=courier new,courier,monospace]DXGI_FORMAT_R32G32B32A32_FLOAT[/font] can represent a [font=courier new,courier,monospace]float4[/font], but there is no format in the list that can represent a [font=courier new,courier,monospace]sampler[/font]. Edited by Hodgman

##### Share on other sites
There's no specific alignment requirements beyond what's implied by the formats you use for your various elements. If you have an element that uses DXGI_FORMAT_R32_FLOAT, then the next element can be 4 bytes after it (since the format implies that the element will be 4 bytes).

The type in your HLSL shader has to match the type specified by the format. If you you use a FLOAT DXGI format, then you have to use the "float" type in your shader. By the same token you need to use "uint" for "UINT" formats, "int" for "SINT" formats, and "float" for "UNORM" and "SNORM" formats.

##### Share on other sites
Thanks for the answers, so I guess this blog post is totally wrong?

EDIT: Just remembered this was about constant buffers. Carry on.