   Geometry Instancing API
  Hello,I'm trying to draw up a comparison between constants based instancing and hardware based instancing. The middleware engine I frequently use has constants based support and I'm trying to write up a hardware based class for it's community to mull over. The engine gives me access to it's internal DirectX objects so I'm able to grab the DX device and some mesh data. I've gotten pretty far, I have it rendering stuff with a shader. The problem is that the geometry is broken and it doesn't appear to be positioning the instances correctly( it is also criminally slow, about 4fps on a 9400M with 520 instances -- constants is averaging 80fps with the same number ).I have been looking at several sources of information for the past week. I have GPU Gems 2 open on my desk with an entire chapter dedicated to instancing, I have the DirectX SDK sample and an old MDX sample from 2005. I'm not entirely sure where I'm going wrong, but using a middleware engine 99% of the time has not helped( vertex buffers and the like are fine but I'm probably getting them wrong somewhere, these are usually hidden from me ).Here is how I'm creating the vertex buffer for my instancing data. At the moment it is just one float3( positionx, positiony and positionz ). You can also see at the top of the function for the actual geometry I am grabbing the vertex and index buffers directly from a mesh( this seems to work fine if I just render them out without any of the actual instancing code ).void HardwareInstancer::BuildBuffer(){//Setup the geometry and instance buffers.//....//Grab the mesh vertex and index buffer.geomBase->GetVertexBuffer(&VB);geomBase->GetIndexBuffer(&IB);//Create the instance buffer.m_Device->CreateVertexBuffer(iElementCount * sizeof(InstanceData), 0, 0, D3DPOOL_MANAGED, &ID, 0);InstanceData *vData = NULL; //As void.//Lock the buffer before we fill it with data.ID->Lock(0, NULL, (void**)&vData, 0);//Fill the instance data.for(int i = 0; i < iElementCount; i++){InstanceData Data;Data.x   = IData.at(i).x;Data.y   = IData.at(i).y;Data.z   = IData.at(i).z;*vData = Data, vData++;}//Unlock the buffer.ID->Unlock();//------------------------------------------------------}The vertex element declaration might be incorrect.D3DVERTEXELEMENT9 decl[] ={ //Stream 0. Standard data.{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},  //Stream 1. Instance data.{1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1}, D3DDECL_END() };I'm rendering it out in the following fashion. I've looked over both the DX and the MDX samples and this appears to be correct?. The GPU Gems 2 chapter is weird here however, for both SetStreamSource calls it uses the same vertex buffer -- I think this is a misprint.void HardwareInstancer::RenderElements(){m_Device->SetVertexDeclaration(gdecl);//Set the stream to our geometry.m_Device->SetStreamSource(0, VB, 0, geomBase->GetNumBytesPerVertex());m_Device->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA | iElementCount);//Set the stream to instances.m_Device->SetStreamSource(1, ID, 0, sizeof(InstanceData));m_Device->SetStreamSourceFreq(1, D3DSTREAMSOURCE_INSTANCEDATA | 1);m_Device->SetIndices(IB);shadeHardware->SetMatrix("v", &View);shadeHardware->SetMatrix("p", &Projection);shadeHardware->SetTexture("d", DT);shadeHardware->SetTechnique("render");shadeHardware->Begin(0, 0);shadeHardware->BeginPass(0);m_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, geomBase->GetNumVertices(), 0, geomBase->GetNumFaces());shadeHardware->EndPass();shadeHardware->End();m_Device->SetStreamSourceFreq(0, 1);m_Device->SetStreamSourceFreq(1, 1);}I've uploaded a couple screenshots so you can get a visual read on the problem.Constants: http://esoteric-float.co.uk/screenshots/bugs/constants.jpgHardware : http://esoteric-float.co.uk/screenshots/bugs/hardware.jpgThe hardware instances have the same positions as the constants ones. You can also see that in the hardware version one of the mushrooms renders in full, whereas the rest of them don't.I've been stumped on this for 4 days and I don't seem to be able to progress past it. Any help or advice is most welcome.Thanks.   
  I cannot find any obvious mistakes in the code.And there must be a misprint in that book regarding SetStreamSource.My guess would be that the problem is in the shader or something outside the code presented.Btw, why dont you change *vData = Data, vData++;tovData = Data;To make it look clearer.;  I can't believe I forgot the shader.float4x4 w; //World. float4x4 v; //View.float4x4 p; //Projection.texture  d; //Diffuse/Colour texture map.sampler2D ds = sampler_state{Texture   = (d);MIPFILTER = LINEAR;MAGFILTER = LINEAR;MINFILTER = LINEAR;};struct a2v{//Stream 0. Vertex information.float4 p : POSITION0;float2 uv: TEXCOORD0;float3 n : NORMAL;//Stream 1. Instance information.float4 Pos: COLOR1;};struct v2f{float4 p  : POSITION;float2 uv : TEXCOORD0;};void vp(in a2v a2vin, out v2f v2fout){float4 newPosition  = mul(a2vin.p, w);float4 InstPosition = mul(a2vin.Pos, w);newPosition.x += InstPosition.x;newPosition.y += InstPosition.y;newPosition.z += InstPosition.z;v2fout.p  = mul(newPosition, mul(v, p));v2fout.uv = a2vin.uv;}float4 fp(in v2f v2fin): COLOR0{float4 pixel = tex2D(ds, v2fin.uv);return pixel;}technique render{pass pass0{//ZENABLE= TRUE;//ZWRITEENABLE= TRUE;//CULLMODE    = NONE;vertexshader= compile vs_3_0 vp();pixelshader = compile ps_3_0 fp();}}