• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
barsiwek

HLSL Vertex Shader input types and their alignment

5 posts in this topic

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?
0

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
0

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?
0

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
0

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

Share this post


Link to post
Share on other sites
Thanks for the answers, so I guess [url=http://blog.signalsondisplay.com/?p=244]this[/url] blog post is totally wrong?

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

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  
Followers 0