Original Post
I've been thinking about how to organize my shaders: even in the simple case of a specular lighting shader there may be several techniques.. Pure specular lighting, specular with a diffuse map, specular with texture and normal map, pure specular with shadows and no textures and so on. One could theoretically treat each particular combination as an entirely different shader, but for the sake of code duplication (and readability) maybe it's better not. Now, in a "real" game, the choice of which technique to use could be dictated by which option the player has chosen in the "option screen". For example if the player chose "no shadows" then only techniques with no shadows will have to be used. Similarly if he chooses "high shadows quality" then the engine should employ VSM (I'm moving my first step in the shadows domain (!), i hope they can be considered high quality enough) with an high resolution enough shadow map. Now, how should the engine handle all of this? I can think of two solutions, but since I'm not that experienced in this field I'm interested in hearing what you might have to say. The simplest solution would simply be to keep an hard-coded "map" of techniques linked to particular combinations of options. Like: if OptionA is true and OptionB is true then use TechniqueX for EffectY. But this sounds rather unelegant. The other solution could be to dynamically build the technique name from the option chosen. If SpecularBump is eneabled and shadows are disabled then the resulting technique may be called "SpecularBumpUnshadowed".. Obviously one would have to follow the naming scheme in the shaders, but it seems to be that this scheme wouldn't break as often as the first one. Anyway, are there better solutions? Another questions that I added concerned the use of #include directives in shaders. Again, for the sake of code duplication and readability, I was considering moving light calculations and other common variables and functions into included files. Since I plan to use EffectPool (my current implementation does not yet take advantage of it), many matrices would be common to many effects. I am already using the same names across all of my shaders. The question that I have is the following. Suppose that I have the default texture declaration for shadow maps into one of the included file, along with its accompanying sampler declaration. Suppose now that I'm using for an effect a technique that does not use shadow maps. Would that texture declaration in the compiled HLSL file waste some memory? Does some memory allocation process occur when the compiler finds one of those declaration? Or maybe does it only occur when the value is actually assigned? Or better yet, the compiler is smart enough to check whether that variable will even be utilized at all? I think it's the latter, because while debugging with PIX I have noticed that some statements seem to be only executed when they are needed even if they are the first lines in the shader. I think this is maybe a non issue, but one never knows.. Thanks in advance :)