Advertisement

Input Layout question

Started by May 14, 2018 10:03 AM
5 comments, last by Hodgman 6 years, 8 months ago

Hi Guys,

I understand how to create input layouts etc... But I am wondering is it at all possible to derive an input layout from a shader and create the input layout directly from this? (Rather than manually specifying the input layout format?)

Thanks in advance :)

 

Yeah, if you reflect on your vertex shaders, you can discover which attributes it consumes, e.g. the asm dump from one of my shaders includes:


// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyz         0     NONE   float   xyz 
// TEXCOORD                 0   xy          1     NONE   float   xy  
// NORMAL                   0   xyz         2     NONE   float   xyz 
// TANGENT                  0   xyz         3     NONE   float   xyz 
// POINTCLOUDINDEX          0   x           4     NONE    uint   x   

As well as this though, you need to know the structure of how your vertices are stored in RAM. Once you have both bits of information, you can automatically match them to each other and generate an input layout.

Advertisement

It is doable, but maybe not the best practice. You can use D3DReflect API to retrieve shader source information from the compiled blob. From it you can retrieve your vertex shader inputs and semantics. But you should watch out because vertex buffers will use type conversion to convert between memory layout and how the shaders will view them, and you can only deduce that from the input semantic string. So make sure that they are consistent and decide on the CPU side what kind of vertex packing you want for a given semantic by comparing strings.

Also, as far as I know, you can't use the shader reflection or shader compilation APIs when you release on the Windows Store.

I had the same question some time ago. My conclusions where that it's a good idea to use D3DReflect, but that it's also a good idea to define a number of combinations you want to support, because in the end most likely you also need the structs with the data (loading mesh data and storing it somewhere etc.). If you want I can paste some of the code on how I did it, let me know,

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Thanks for the replies, guys. Gives me a couple of things to think about trying.

I wonder if the creation of the input layout requires the full shader to compare against, or whether it would succeed with just a valid 'input section'.

If the latter is the case, we might be able to create an 'fvf blob' on the fly by 'engineering' a valid shader input.

2 hours ago, lonewolff said:

I wonder if the creation of the input layout requires the full shader to compare against, or whether it would succeed with just a valid 'input section'.

You can share input layouts between multiple different vertex shaders, as long as they all have the same "input section" as you call it :)

I actually (ab)use this feature and pre-compile one shader for each of my vertex-input-structures, who's only use is being passed to CreateInputLayout. These fake shaders are basically empty, besides reading the required vertex input attributes (and making sure those reads aren't optimized out, by summing them and returning the result).

This topic is closed to new replies.

Advertisement