Shaders and thechniques, how many per each?

Hello everyone,
I find myself with a shader wich takes a pointcloud and output a modified grid, it calculates flat normals, fit the uv coordinates with the kinect2 uv remapping, can be used to threshold the output vertices creating a geometry with less vertices than the input one, and some little texture mappings.

I want to create techniques where the UV is not applied, or the normals are not calculated or both, just because I need different outputs for different uses in the same patch.

Is it better to create a shader with N techniques (like 9) or is better to split the code and create different shaders, one with flat normals, one with the threshold, one with both, one without both?

So, is better a shader with 9 similar techniques or 3 or 4 shaders with few thechniques each?

What do you think? What do you know?

i’d say its perfectly fine to make one shader as long as the techniques do similar things. this has the advantage that you can easily share methods between the techniques… so each step of the calculation is one separate function and the shaders in the techniques itself are only combinations of the functions.

but you can also make an .fxh file and share common code in that file to several shaders…

i don’t know how much the code differs between the different use cases…

you could also think about a modular system like a geometry shader which reduces the points, then other shaders which display the geometry in some way… this has the advantage that you have to do the geometry calculation only once and the other shaders all get the output of that one, could safe a lot of performance…

Got it. Thanks.

Actually this is a geometry shader :)

Maybe is because I don’t know how the shader is “loaded” in the pipeline, I wonder, having in the patch, let say, 4 duplicated shader node wich use 5 different Techniques is worst or better than having 4 different shaders?

The code is similar and I can easily create some function and some methods share the Vertex Shader or the Geometry shader.

I can’t really say what’s best as to performance, but I guess you have some of the dx11 query nodes for that. In terms of sanity as long as you are using shader model 5 you might also check out interfaces. Here’s one I’m just putting in an fxh file now for example:

//////////////////////////////////////////////////
/* geomtry indexing interface
	place in shader:

iIndexBuffer myValueIndexing <string linkclass="Vertex,Triangle,Instance";>;

i = myValueIndexing.Get(iid, vid);


*/
interface iIndexBuffer
{
   float Get(float iid, float vid);
};


class cVid : iIndexBuffer
{
   float Get(float iid, float vid)
	{
		return vid;
	}
}; 

class cPid : iIndexBuffer
{
   float Get(float iid, float vid)
	{
		return floor(vid / 3);  //IDs for triangles
	}
}; 

class cIid : iIndexBuffer
{
   float Get(float iid, float vid)
	{
		return iid;
	}
}; 

cVid Vertex;
cPid Triangle;
cIid Instance;



///////////////////////////////////////////////////

That’s deep for me at the moment but after reading http://http.developer.nvidia.com/GPUGems/gpugems_ch32.html make more sense.
Thank you very much

@esnho

its the same performance either way. if a technique is not selected it will not consume any GPU power. so only the active shader/technique will be calculated. so make sure that the shader is not doing stuff twice… then you should be fine.

That is true, but is a bit of a narrow view, which ignore the fact that shaders have dependencies that needs to be satisfied, so while this shader itself might not run, you still need to evaluate upwards dependencies, and that might take (a lot) of time. So that is technically not true as soon as you look at the bigger picture.

Shader nodes (in both dx9 and dx11) evaluate all upwards dependencies by default, they don’t select upon what they need or not, so let’s consider the following case (I will take a simple blend shader with as an example):

  • Technique 1 : Blend texture 1 with texture 2
  • Technique 2 : Blend texture 1 with texture 3
  • Technique 3 : Blend texture 2 with texture 3
  • Technique 4 : Blend all 3 textures

So in that case your shader has 3 texture inputs which need to be satisfied in order to run. If you use technique 1, texture 3 still needs to be satisfied, so you would lose performance in comparison with a dedicated shader (with only 2 textures) and a switch (which effectively cuts a dependency requirement).

So if all your techniques use the same resources only, you can be safe to say that a single effect with techniques has no performance cost, but if your techniques start to use a different set of resources, that is not true anymore.