HLSL Vertex Shader input types and their alignment
#1 Members - Reputation: 114
Posted 24 November 2012 - 08:19 PM
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?
#2 Moderators - Reputation: 13471
Posted 24 November 2012 - 09:00 PM
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.
#3 Members - Reputation: 114
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?
#4 Moderators - Reputation: 13471
Posted 24 November 2012 - 11:16 PM
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).
You can have any type that can be mapped to one of the formats in the "Input assembler vertex buffer resources" I linked to.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.
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.
#5 Moderators - Reputation: 5415
Posted 25 November 2012 - 01:02 AM
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.






