   "big" fx files
  Hi,I've got a dillema here,I have a vertex buffer which consists of different objects, let's say quads.These quads can be of, let's say 50 different colors. Is it smarter to pass the color for the quads in each vertex or to pass an ID of the quad in the vertex and determine the color by using something like a switch or if/elseif/else statement in the effect file.Thanks in advance,Tigran   
  If you can use D3D10 and up, you could use the combination of geometry shaders and native integer data handling to implement such a scenario with ease - quads could be assembled from 4 floats (in a single incoming vertex) in GS, and you could assign a discrete integer color index (or a real color value) to each vertex.//SM4 HLSL//this vertex structure encapsulates all data for a screen-aligned quad: struct quadvtx{  float4 rectangle;  int colorIndex; //or float4 color};For earlier hardware, it may be best to assemble the quad buffer manually on the CPU side. This kind of scenario could be realized by using hardware instancing available in D3D9 hardware too, but it is not optimal when the common dataset between the instances is as small as a single quad.   ;  Thanks for the reply,I'm using d3d9 (VS/PS 2.0)It's just adding only an index takes less memory (say 3 bytes less), but then i have to implement an if statement for the effect file.So what would be more efficient?I'll take a look at hardware instancing, but I think it requires SM 3.0   ;  you can use shader instancing and upload the values to a shader constant:float4 colors [maxBatch] : register(c16);effect->SetVectorArray("colors", &colors[0], numInstances);now add an index to your vertex declaration:struct vertex{D3DXVECTOR3 pos;float instance;};D3DVERTEXELEMENT9 g_VertexElemShader[] ={{ 0, 0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,  0 },{ 0, 3 * 4,D3DDECLTYPE_FLOAT1,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,  0 },D3DDECL_END()};now read the values in your vertex shader:struct VS_INPUT{float3 pos: POSITION;float  instance : TEXCOORD0;};VS_OUTPUT vs_instance(VS_INPUT in){VS_OUTPUT out;out.col = colors[in.instance].xyz;out.pos= mul(float4(in.pos.xyz, 1.0f), MatWVP);return Out;}you can only save a limited number of floats in the shader constants, like 240 or something, so you have to do multiple batches if you have more quads than that. Check out the Instancing example in the SDK for more details.   ;  Thanks, this looks really usefull...So, does this require shader model 2.0 or 3.0And what does the : register(c16) mean?   ;  VS 2.0 will do itthe "register" keyword just assigns the variable to a particular shader registerto be honest, i just copied and pasted that part from the shader instancing example and never bothered changing it since it works. but here's more about the register keyword register (DirectX HLSL)Edit: Here's the explanation from the SDK"Technique 2: Shader Instancing (with Draw Call Batching)This is the most efficient technique that does not use the hardware to perform the instancing. One call to IDirect3DDevice9::DrawIndexedPrimitive handles multiple primitives at once. The instance data is stored in a system memory array, which is then copied into the vertex shader's constants at draw time. Since vs_2_0 only guarantees 256 constants, it is not possible for the entire array of instance data to fit at once. This sample's .fx file can only batch-process 120 constants (one float4 is required for box position, and one float4 is required for box color). Rendering is performed in the sample's OnRenderShaderInstancing method.int nRenderBoxes = min( nRemainingBoxes, g_nNumBatchInstance ); ... pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, nRenderBoxes * 4 * 6, 0, nRenderBoxes * 6 * 2 ); This method would also work on a vs_1_1 part, although not as efficiently because vs_1_1 only guarantees 96 constants. This means that batching is nearly three times more efficient on vs_2_0 hardware than on vs_1_1 hardware, but even on vs_1_1 hardware, batching still outperforms no batching."   ;  Well, untill now, I've been rendering this way to reduce draw calls.I create a mesh, access the vertex/index/attribute buffers and stuff it with 5000 quads.Then, I use the drawsubset function of the ID3DXMesh object.Is this less efficient?And is that stuff with the register, not the same as defining a uniform extern array? or is it faster/better in some other way.And if I use the instancing method you explained, do i have to use a DrawIndexedPrimitive or is the drawsubset method of the ID3DXMesh object sufficient.Thanks in advance,Tigran   ;  with this little amount of geometry you probably won't get a big performance boost from instancing.i use shader instancing to move and color a few hundred cubes (light volumes) per frame. This is considerably faster than a draw call for each cube. I haven't compared it to using a dynamic vertex buffer though. That might work as well but could need more memory.so i think you gain the most improvement if you use instancing on dynamic geometry except if youre extremely memory limited and can't save a color for each vertex.but then again i don't consider myself an expert on graphics programming at all so somebody else might provide more accurate information than me.> And if I use the instancing method you explained, do i have to use a DrawIndexedPrimitive or is the drawsubset method of the ID3DXMesh object sufficient.DrawSubset should do the job as all it does is is setting your buffers and call DrawIndexedPrimitive as far as i know. personally i use ID3DXMesh only for some of the optimization functions and then convert them to my own mesh type which uses my own buffer classes which are optimized for the specific needs of the mesh.   ;  what you could do as well is using a texture to store your instance data. you can store 4096 rgba values in a 64x64 texture (or you use a 4096x1 1D texture). Then you access the texture in your pixel shader using the index of your instance. You would have to lock the texture to modify the instance data, or double buffer it, but that shouldnt be too bad for such a small texture. Of course this would only modify your pixel and can't be used for transforming geometry (at least in vs_20 i think).   ;  Thank you very much, scope.You've answered all my questions and gave me a few very interesting ideas :PI'll go ahead and rate you as high as I can :D   