# DX11 Instancing not working correctly

This topic is 1350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am attempting to just draw a few thousand instanced basic spheres that will move around in the scene. Also I need to eventually use both DrawIndexed and DrawIndexedInstanced or DrawInstanced, and beable to change each instances rotation as well but don't need to worry about that atm.

The main question is just how to get the instanced spheres to draw? Currently the instancing version of my program is drawing one flat sphere at position (0,0,0)

There are no compile or run time errors in the code it just doesn't work correctly. Below is what I think is the relevant code. Thanks!

Vertex.cpp

const D3D11_INPUT_ELEMENT_DESC InputLayoutDesc::PosNormal[6] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"WORLD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1},
{"WORLD", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
{"WORLD", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
{"WORLD", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
};

#pragma endregion

#pragma region InputLayouts

ID3D11InputLayout* InputLayouts::PosNormal = 0;

void InputLayouts::InitAll(ID3D11Device* device)
{
//
// PosNormal
//

D3DX11_PASS_DESC passDesc;
Effects::BasicFX->Light1Tech->GetPassByIndex(0)->GetDesc(&passDesc);
HR(device->CreateInputLayout(InputLayoutDesc::PosNormal, 6, passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize, &PosNormal));
}

void InputLayouts::DestroyAll()
{
ReleaseCOM(PosNormal);
}


From Basic.fx file

cbuffer cbPerFrame
{
DirectionalLight gDirLights[3];
float3 gEyePosW;

float  gFogStart;
float  gFogRange;
float4 gFogColor;
};

cbuffer cbPerObject
{
float4x4 gWorld;
float4x4 gWorldInvTranspose;
float4x4 gWorldViewProj;
float4x4 gTexTransform;
Material gMaterial;
};

// Nonnumeric values cannot be added to a cbuffer.
Texture2D gDiffuseMap;

SamplerState samAnisotropic
{
Filter = ANISOTROPIC;
MaxAnisotropy = 4;

};
struct VertexIn
{
float3 PosL    : POSITION;
float3 NormalL : NORMAL;
row_major float4x4 World  : WORLD;
uint InstanceId : SV_InstanceID;
};

struct VertexOut
{
float4 PosH    : SV_POSITION;
float3 PosW    : POSITION;
float3 NormalW : NORMAL;
};

VertexOut VS(VertexIn vin)
{
VertexOut vout;

// Transform to world space space.
vout.PosW    = mul(float4(vin.PosL, 1.0f), vin.World).xyz;
vout.NormalW = mul(vin.NormalL, (float3x3)vin.World);

// Transform to homogeneous clip space.
vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);

return vout;
}


Creating buffer instanced object's locations

    D3D11_BUFFER_DESC instBuffDesc;
ZeroMemory( &instBuffDesc, sizeof(instBuffDesc) );
instBuffDesc.Usage = D3D11_USAGE_DEFAULT;
instBuffDesc.ByteWidth = sizeof(InstancedData) * OBJECTCOUNT;
instBuffDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instBuffDesc.CPUAccessFlags = 0;
instBuffDesc.MiscFlags = 0;
HR(md3dDevice->CreateBuffer( &instBuffDesc, NULL, &segmentInstanceBuffer));


        //Copy Data to buffers
for(int i = 0; i < OBJECTCOUNT; ++i)
{
//XMStoreFloat3(&objVector[i], XMVectorSet(segList[i].loc[0], segList[i].loc[1], segList[i].loc[2], 0.0f));
XMMATRIX sphereOffset = XMMatrixTranslation(segList[i].loc[0], segList[i].loc[1], segList[i].loc[2]);
XMStoreFloat4x4(&objVector[i].World, sphereOffset);
}
md3dImmediateContext->UpdateSubresource( segmentInstanceBuffer, 0, NULL, &objVector, 0, 0);


Relevant code from the Draw (I think)

   UINT strides[2] = {sizeof(Vertex::PosNormal), sizeof(InstancedData)};
UINT offsets[2] = {0,0};

md3dImmediateContext->IASetInputLayout(InputLayouts::PosNormal);
ID3D11Buffer* vbs[2] = {mSphereVB, segmentInstanceBuffer};
md3dImmediateContext->IASetVertexBuffers(0, 2, vbs, strides, offsets);
md3dImmediateContext->IASetIndexBuffer(mSphereIB, DXGI_FORMAT_R32_UINT, 0);

worldInvTranspose = MathHelper::InverseTranspose(world);

Effects::BasicFX->SetWorld(world);
Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
Effects::BasicFX->SetWorldViewProj(worldViewProj);
Effects::BasicFX->SetMaterial(mSphereMat);

activeTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
md3dImmediateContext->DrawIndexedInstanced(mSphereIndexCount, OBJECTCOUNT, 0, 0, 0);


Let me know if there is any more code that I should show, Thanks!

##### Share on other sites

You forgot to use the instance world matrix here:

 // Transform to homogeneous clip space.
vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);


It should be more like this:

 // Transform to homogeneous clip space.
vout.PosH = mul(float4(vout.PosW, 1.0f), gViewProj);

Edited by Mona2000

##### Share on other sites
When I changed it to :
 // Transform to homogeneous clip space.
vout.PosH = mul(float4(vout.PosW, 1.0f), gViewProj);


I had the shape drawn everywhere (not a sphere any more, covered screen)

When I changed it to this :
// Transform to homogeneous clip space.
vout.PosH = mul(float4(vin.PosL, 1.0f), gViewProj);


It is no longer a flat, and looks like a normal sphere, but there is only one stationary.

I know for sure the location data is being created. I, cant seem to verify the information going to the Buffer (no symbol for the dll, and cant seem to download) but I dont think that's the issue.

Im confused why I lost reputation, but I still have a problem...

So if this is correct why does is mess up all my visuals?

vout.PosH = mul(float4(vout.PosW, 1.0f), gViewProj);

Edited by Sobe118

##### Share on other sites

I just re did the project a bit to fix the issue.

solved

• 11
• 9
• 16
• 16
• 10
• ### Similar Content

• I wanted to see how others are currently handling descriptor heap updates and management.
I've read a few articles and there tends to be three major strategies :
1 ) You split up descriptor heaps per shader stage ( i.e one for vertex shader , pixel , hull, etc)
2) You have one descriptor heap for an entire pipeline
3) You split up descriptor heaps for update each update frequency (i.e EResourceSet_PerInstance , EResourceSet_PerPass , EResourceSet_PerMaterial, etc)
The benefits of the first two approaches is that it makes it easier to port current code, and descriptor / resource descriptor management and updating tends to be easier to manage, but it seems to be not as efficient.
The benefits of the third approach seems to be that it's the most efficient because you only manage and update objects when they change.

• hi,
until now i use typical vertexshader approach for skinning with a Constantbuffer containing the transform matrix for the bones and an the vertexbuffer containing bone index and bone weight.
Now i have implemented realtime environment  probe cubemaping so i have to render my scene from many point of views and the time for skinning takes too long because it is recalculated for every side of the cubemap.
For Info i am working on Win7 an therefore use one Shadermodel 5.0 not 5.x that have more options, or is there a way to use 5.x in Win 7
My Graphic Card is Directx 12 compatible NVidia GTX 960
the member turanszkij has posted a good for me understandable compute shader. ( for Info: in his engine he uses an optimized version of it )
Now my questions
is it possible to feed the compute shader with my orignial vertexbuffer or do i have to copy it in several ByteAdressBuffers as implemented in the following code ?
the same question is about the constant buffer of the matrixes
my more urgent question is how do i feed my normal pipeline with the result of the compute Shader which are 2 RWByteAddressBuffers that contain position an normal
for example i could use 2 vertexbuffer bindings
1 containing only the uv coordinates
2.containing position and normal
How do i copy from the RWByteAddressBuffers to the vertexbuffer ?

(Code from turanszkij )
Here is my shader implementation for skinning a mesh in a compute shader:

• Hi, can someone please explain why this is giving an assertion EyePosition!=0 exception?

It looks like DirectX doesnt want the 2nd parameter to be a zero vector in the assertion, but I passed in a zero vector with this exact same code in another program and it ran just fine. (Here is the version of the code that worked - note XMLoadFloat3(&m_lookAt) parameter value is (0,0,0) at runtime - I debugged it - but it throws no exceptions.
and here is the repo with the alternative version of the code that is working with a value of (0,0,0) for the second parameter.

• Hi, can somebody please tell me in clear simple steps how to debug and step through an hlsl shader file?
I already did Debug > Start Graphics Debugging > then captured some frames from Visual Studio and
double clicked on the frame to open it, but no idea where to go from there.

I've been searching for hours and there's no information on this, not even on the Microsoft Website!
They say "open the  Graphics Pixel History window" but there is no such window!
Then they say, in the "Pipeline Stages choose Start Debugging"  but the Start Debugging option is nowhere to be found in the whole interface.
Also, how do I even open the hlsl file that I want to set a break point in from inside the Graphics Debugger?

All I want to do is set a break point in a specific hlsl file, step thru it, and see the data, but this is so unbelievably complicated