How to handle input layouts in DX11?
Members - Reputation: 413
Posted 12 March 2012 - 03:43 AM
Simplifying things a little, I have an Effect class which is responsible for how to draw something, and amongst other things holds the vertex shader to use. I also have a Element class which holds the geometry to draw amongst other things (vertex buffer, index buffer etc).
The problem is that only my Element class knows about the format of the vertex buffers if uses so it's really the only object that can create and set the input layout, However in order to do this it needs to know which vertex shader is being used for that draw call, and in fact needs to create a different input layout for each shader that can be used to draw it, as well as to know at run time which shader is being used so it can select which input layout is the appropriate one.
I can make my Element object cache the ID3D11InputLayout in a map indexed on vertex structure type, and vertex shader type, and share that map between all of the elements rather than create the input layout each time it's asked to draw something (which I suspect would be horribly inefficient anyway...) but that just seems inelegant and mixes together the responsibilities for the Effect and the Element far too much.
So how do people handle selecting which input layout to use, creating the ID3D11InputLayout objects, and so on? Is there something I'm missing, or is my overall design flawed and could be altered?
Thanks for any suggestions
Moderators - Reputation: 47470
Posted 12 March 2012 - 04:56 AM
Members - Reputation: 413
Posted 12 March 2012 - 05:00 AM
(I don't like my terminology for Element, I plan to change that to something more descriptive!)
Before issuing a draw-call, I look at the currently bound Element's vertex description and the currently bound Effect's vertex description (to use your terminology) and then use a 2D look-up-table to fetch the appropriate input layout to use.
Do you calculate all the possible combinations in advance, or generate and cache them when needed?
I was trying to avoid my drawing code having to know anything about the geometry that it was asking the element to draw, but I guess it's unavoidable because of the coupling between the two things
Crossbones+ - Reputation: 6312
Posted 12 March 2012 - 06:15 PM
So you could use a preprocess to generate the layouts that you will need and store them in a script, or just load them up as you need them (this is what Hieroglyph 3 does).
Jason Zink :: DirectX MVP
Direct3D 11 engine on CodePlex: Hieroglyph 3
Direct3D Books: Practical Rendering and Computation with Direct3D 11, Programming Vertex, Geometry, and Pixel Shaders
Articles: Dual-Paraboloid Mapping Article :: Parallax Occlusion Mapping Article (original):: Fast Silhouettes Article
Games: Lunar Rift