This project is read-only.

Passing structs to shaders via the ParameterManagerDX11

Apr 24, 2013 at 6:42 PM
I noticed that none of your shaders that are used in your applications/demos use custom structs in any of the constant buffers. I also noticed that the ParameterManagerDX11 doesn't support passing structs to shaders (see below). Was this on purpose? Do you plan to support this later on? Thanks!
struct DirectionalLight
    float4 AmbientColor;
    float4 DiffuseColor;
    float4 SpecularColor;

cbuffer cbPerFrame
    DirectionalLight DirLight;
Apr 24, 2013 at 11:23 PM
This was a conscious decision early on in the development of the engine. When I wrote the code for reflecting the signatures of each of the constant buffers that a shader required, using structs seemed like an unnecessary complication that I didn't want to have to work on since I could easily get around it (by declaring all of the variables in the uppermost level of the constant buffer).

I thought about it for a little while today, and I think it would be possible to implement this within the existing framework. It would require adding another parameter type, and then adding the code to allow it to be set in the parameter manager. Is this something that you would like to see?

I have also been mulling over an overhaul of the implementation of the parameter system. Currently there is some usage of void pointers, which I think can be eliminated through the use of templates in the place of the polymorphic methods that are there now. This is probably going to be something longer term, but the recent bug that was pointed out by @TheOutsourcer shows the weakness of using casting for such a system...
Apr 25, 2013 at 4:05 PM
That makes since. The more I thought about it after I messaged you, the more I realized that using structs is convenient, but setting the top level variables like you said accomplishes the same thing. Everything gets mapped and sent to the GPU at once anyways. Structs just allow you to compact everything.

You know, I was thinking the same thing. Prior to messaging you, I started to add a new StructVariable class and update the ParameterManagerDX11 class to accommodate the new type. I then updated the ConstantBufferDX11 class to allow updating a struct. The only part I didn't like out of all of this is that I had to use a void* pointer. Like you mentioned, it did seem too complicated to do in order to support structs, so I just backed out the changes. I think your parameter system works perfectly. No need to add support for structs. I appreciate you asking if I'd like to see that feature supported though ;).

I think a templated system could work, but that would eliminate the need for the other typed classes (i.e. MatrixParameterDX11, SamplerParameterDX11, VectorParameterDX11). Since you have native types that match each of those parameter classes, the choice is clear for users of your framework.

Tip: A FloatParameterDX11, IntegarParameterDX11, and BooleanParameterDX11 would fit nicely with the other parameter classes in Hieroglyph. I added support for those types in my engine :).