Sign in to follow this  
barsiwek

HLSL Vertex Shader input types and their alignment

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 this post


Link to post
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 [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff471325(v=vs.85).aspx"]here[/url].
For D3D9, they're listed [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172533(v=vs.85).aspx"]here[/url], 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 [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
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 [i]is[/i] 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 this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1353812417' post='5003867']
Which versions of Direct3D and HLSL are you using?
For D3D11, the formats that can be used for vertex data are listed [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff471325(v=vs.85).aspx"]here[/url].
For D3D9, they're listed [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172533(v=vs.85).aspx"]here[/url], 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.
[/quote]

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.

[quote name='Hodgman' timestamp='1353812417' post='5003867']
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 [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
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 [i]is[/i] 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.
[/quote]

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

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 this post


Link to post
Share on other sites
AFAIK ([i]someone correct me if I'm wrong here[/i]), 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 ([i]which stream, what offset into the stream, what format[/i]), and it's location in the HLSL vertex structure ([i]semantic name+index[/i]).
[quote name='barsiwek' timestamp='1353815684' post='5003875']
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.
[/quote]You can have any type that can be mapped to one of the formats in the "[i]Input assembler vertex buffer resources[/i]" 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 this post


Link to post
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 this post


Link to post
Share on other sites

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