What is the point of using D3DReflect instead of just setting a struct?

Started by
3 comments, last by MJP 11 years, 2 months ago

Hello,I was looking around articles where people discuss working with shader reflections and I wonder why is it even needed.I mean you can just do like in DirectX Toolkit: make the constant buffer class to be a template(the template being the struct with the constants) and put a Update(T& newData) where it maps the resource and updates it with data from newData.Isn't that 100 times simpler to use?Or am I missing something about the proper use of shader reflections.

Advertisement

The biggest down side to that approach is that you require knowledge of the exact shader files at design time so that you can create a struct of the appropriate type. If you are trying to rapid prototype some algorithms, this means you have to recompile something anytime your constant buffer layouts change. It also means that you have a struct type that is dependent on another file that isn't directly enforced or monitored by your build chain, which can lead to mismatches and difficult to debug issues.

In Hieroglyph 3, the reflection information is used at startup to determine what data needs to be packed into the constant buffers. This allows you to reconfigure the data as long as your renderer has access to the needed information. I use the type and name of the variables as the unique identifiers for the information, but you could use whatever methodology that you want. So if my engine already supports the setting of 'ViewPosition', then I can add that as a parameter to a shader file and automatically get the value without having to recompile the engine / application.

Both methods have their merits (as you pointed out, the loading process is significantly easier if you know ahead of time what the layout will be) but for my engine I value the ability to rapidly iterate on shader based algorithms!

What worries me is the string comparisons each frame,I think I'll have to make some hashing algorithm

EDIT: but wait,don't you also need to know the names of the shader when you set them by string,I mean you still have to know what's in the constants of the shader,otherwise you don't know what you should be setting

What worries me is the string comparisons each frame,I think I'll have to make some hashing algorithm

EDIT: but wait,don't you also need to know the names of the shader when you set them by string,I mean you still have to know what's in the constants of the shader,otherwise you don't know what you should be setting

That is precisely what the reflection information tells you. And the string comparisons are only performed when the shader is loaded at runtime - after that, direct references to the relevant data are acquired and reused from that point onward. There is no negative performance aspect to working with the data in this way. The only real difference is that you are copying data into a buffer via memcpy rather than setting the value of a struct member.

Like Jason mentioned it enables you to keep your application code somewhat generic, without having to strongly tie your C++ code to your shader code. Personally I haven't ever really seen the need for a fully generic shader constant system, since in most cases the engine needs knowledge of what values it's setting and so you're not going to avoid the code dependency. Plus for real games that need to scale to hundreds of draw calls you need to tightly control exactly how you update your constant buffers in order to avoid performance deficiencies. In our engine at work we try to compromise by using matching C++ structs and constant buffer layouts for all values that are set by the engine. Then for special materials that have runtime-animated properties, we put those properties in their own constant buffer and we build a hash table of the offsets of each property so that a certain value can quickly be looked up and set by hash.

The one case where we make heavy use of reflection is for textures. Our material shaders can potentially use many textures, but any individual permutation will only use a subset since most will get optimized away. So to handle this, our build pipeline reflects the shaders and builds a list of only the textures actually needed by a particular shader so that we can set them efficiently at runtime.

This topic is closed to new replies.

Advertisement