• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# HLSL Vertex Shader input types and their alignment

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.

5 replies to this topic

### #1barsiwek  Members

Posted 24 November 2012 - 08:19 PM

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?

### #2Hodgman  Moderators

Posted 24 November 2012 - 09:00 PM

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 "AlignedByteOffset" 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 "AlignedByteOffset" 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 DXGI_FORMAT_R32G32_FLOAT alignment would be 4 bytes.

Edited by Hodgman, 24 November 2012 - 09:00 PM.

### #3barsiwek  Members

Posted 24 November 2012 - 09:54 PM

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 "AlignedByteOffset" 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 "AlignedByteOffset" 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 DXGI_FORMAT_R32G32_FLOAT 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?

### #4Hodgman  Moderators

Posted 24 November 2012 - 11:16 PM

AFAIK (someone correct me if I'm wrong here), the order of your attributes in your "VSInput" struct doesn't have to match up with the order of your attributes in memory.
e.g. in memory, you could have pos, then a, then b. Or, you could have pos and b in one buffer, and a 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. DXGI_FORMAT_R32G32B32A32_FLOAT can represent a float4, but there is no format in the list that can represent a sampler.

Edited by Hodgman, 24 November 2012 - 11:23 PM.

### #5MJP  Moderators

Posted 25 November 2012 - 01:02 AM

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.

### #6barsiwek  Members

Posted 25 November 2012 - 05:35 AM

Thanks for the answers, so I guess this blog post is totally wrong?

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

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.