   Shaders and Metaprogramming
  Comments for the article Shaders and Metaprogramming
  Reading this, I just had to try something similar in C++.class Shader;Shader* current = 0;//This cute little hack lets me set the current shader inside a shaders constructer.//Hopefully before the parameters are constructed, but I doubt that's guaranteed...class CurrentSetter{public:CurrentSetter(Shader* shader){current = shader;}};class ShaderParam;class Shader{public:Shader() : hack(this){}void AddParam(ShaderParam* param){params.push_back(param);}void Bind();CurrentSetter hack;std::vector<ShaderParam*> params;};class ShaderParam{public:ShaderParam(std::string name){this->name = name;assert(current);current->AddParam(this);}void Bind(){PrintName();}void PrintName(){std::cout << name << std::endl;}private:std::string name;};void Shader::Bind(){for (unsigned int i = 0; i < params.size(); i++){params->Bind();}}#define BOUND_PARAMETER(type, name) class type##_##name : public type {public: type##_##name () : type ( #name ) {}}; type##_##name nameclass TestShader : public Shader{public:BOUND_PARAMETER(ShaderParam, myParam);};int main(){TestShader test;test.Bind();test.myParam.PrintName();return 0;}It's just a proof of concept, and I've barely tested it, and there must be a better way, and I think I'm going to be sick. But it worked, and that's the important thing right? I never did get much practice at writing macros...   ;  Promit, This is a brilliant solution to an annoying problem. At leaast, it complies to my definition of an annoying problem, and it also complies to my definition of a brilliant solution (simple, effective, and well designed).(Saved it for future reference...)   ;  Wouldn't it be easier simply to use enumerations and a dictionary to solve this problem? Off the top of my head I'm thinking something along the line of: public enum Constants{    Model,    View,    Proj,    nConstants}private static Dictionary<Constants, Matrix> ConstantDict;internal static EffectHandle[] LoadParameters(Effect effect){    EffectHandle[] parameters = new EffectHandle[(int)Constants.nConstants];    for (int i = 0; i < (int)Constants.nConstants; ++i)    {   parameters = effect.GetParameter(null, ((Constants)i).ToString());    }    return parameters;}internal static void SetMatrix(Constants con, Matrix m){    ConstantDict[con] = m;}internal void SetConstants(Effect effect, EffectHandle[] parameters){    for (int i = 0; i < (int)Constants.nConstants; ++i)    {    // The condition after && isn't really nessesary   if (parameters != null && ConstantDict.ContainsKey((Constants)i))  effect.SetValue(parameters, ConstantDict[(Constants)i]);    }}This way you don't have to use any dynamic code generation or stuff like that. This, of course, is by no means complete but you get the idea. Please tell me if I completely misunderstood something. Regards,Per Rasmussen   ;  You only handle matrices; the metaprogrammed code can handle any type you care to add support for. The sample code does three different types...Matrix, Vector4, and I forget what the last one is in that version, maybe Texture. And you can add support for more types fairly easily (the Effect interface supports quite a lot of different options). Plus you've added a dictionary lookup to actually get the parameter. Doesn't sound like a lot, but multiply it by every constant parameter for every shader you use 60 times a second and it really can add up.   ;  Hmmm, I think you are missing my point. I was only asking if this wouldn't be easier with enumerations instead of metaprogrammed code. I then posted sample code to illuminate my idea. I even explicitly stated the code was NOT complete. I kinda thought that readers would have the imagination to figure out how to elaborate on my code to include other data types. Concerning the dictionary, then you are right in the claim that a dictionary lookup takes more cycles than an array lookup, albiet it isn't anything worth mentioning IMHO. It is, however, quite possibly to implement my idea with arrays instead. I simply used a dictionary out of (bad?) habit.Regards,Per Rasmussen   ;  Interesting little trick. The problem it adresses though isn't anything particularly important, so I don't know if all the thought put into it was worth it...   ;  Reading the title, I though for a moment that you were using expression templates to compile shaders on the fly. Very nice work, though, so I'm not completely disapponted [smile]   