"Dynamic Struct"

/
0 Comments
Here I present what I like to call a "Dynamic Struct".
The idea is to have a struct that you can define at run-time, and get the elements by a string.
What's the use, you might ask? Well, my reason to do this is to have a way to define a constant buffer in a text file, load and update it in the C++ side, and pass it to the GPU.
So, with that in mind, here's a partial implementation( partial in that it has just a couple types, but it's easy to extend ), and how to use it.

Code:


And to use it:


Or:


It should be noted that I'm using this in production code, with just a few changes, like clean-ups, proper ctors, etc.
Speed-wise, it shouldn't be that bad. I use a std::map instead of a std::hash_map, as constant buffer and structures in general tend to have few items, and a hash map suffers in that conditions. Also is O(1) in the creation of each element, so you can add all the elements TYPES you want( C++ 11 and lambda functions make it possible to throw the old switch() case away in a clearer and might even faster way. Functional programming to the rescue!)

PS: uint is just a typedef for unsigned int. Also XMFLOAT4X4 and XMFLOAT3 are from the DirectX Math Library ( DirectX 11).


You may also like

No comments:

Pages

Powered by Blogger.