Jump to content
  • Advertisement
Sign in to follow this  
stu_pidd_cow

Instancing woes...

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm attempting to do instancing with D3D11, but I'm getting this error:
 
D3D11 WARNING: ID3D11DeviceContext::DrawInstanced: Vertex Buffer at the input vertex slot 0 is not big enough for what the Draw*() call expects to traverse. This is OK, as reading off the end of the Buffer is defined to return 0. However the developer probably did not intend to make use of this behavior.  [ EXECUTION WARNING #356: DEVICE_DRAW_VERTEX_BUFFER_TOO_SMALL]
 
Input layout:
D3D11_INPUT_ELEMENT_DESC* Layout = new D3D11_INPUT_ELEMENT_DESC[3];

Layout[0].SemanticName = "POSITION";
Layout[0].SemanticIndex = 0;
Layout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
Layout[0].InputSlot = 0;
Layout[0].AlignedByteOffset = 0;
Layout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Layout[0].InstanceDataStepRate = 0;

Layout[1].SemanticName = "TEXCOORD";
Layout[1].SemanticIndex = 0;
Layout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
Layout[1].InputSlot = 0;
Layout[1].AlignedByteOffset = 12;
Layout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Layout[1].InstanceDataStepRate = 0;

Layout[2].SemanticName = "INSTANCEPOS";
Layout[2].SemanticIndex = 0;
Layout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
Layout[2].InputSlot = 1;
Layout[2].AlignedByteOffset = 0;
Layout[2].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
Layout[2].InstanceDataStepRate = 1;

Setting up the instance buffer:

// Set up the description of the instance buffer.
D3D11_BUFFER_DESC InstanceBufferDesc;
ZeroMemory(&InstanceBufferDesc, sizeof(InstanceBufferDesc));
InstanceBufferDesc.Usage = D3D11_USAGE_DEFAULT;
InstanceBufferDesc.ByteWidth = 60; // 5 instances
InstanceBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
InstanceBufferDesc.CPUAccessFlags = 0;
InstanceBufferDesc.MiscFlags = 0;
InstanceBufferDesc.StructureByteStride = 0;

// Create the instance buffer.
ID3D11Buffer* instancebuffer = nullptr;
HRESULT result = D3DDevice->CreateBuffer(&InstanceBufferDesc, nullptr, &instancebuffer);

Setting the mesh/buffers:

//instancesize = 12;
//instancedatasize = 60;
//D3DXVECTOR3 instancedata[5]; // lots of goodness in here

// set the vertex and index buffers
unsigned int strides[2] = {D3DMesh->VertexSize, instancesize};
unsigned int offsets[2] = {0, 0};
ID3D11Buffer* bufferPointers[2] = {D3DMesh->VertexBuffer, InstanceBuffer};

D3DContext->IASetVertexBuffers( 0, 2, bufferPointers, strides, offsets );
D3DContext->IASetIndexBuffer( D3DMesh->IndexBuffer, DXGI_FORMAT_R16_UINT, 0 );

Drawing:

//instancecount = 5
D3DContext->DrawInstanced(D3DMesh->IndexCount, instancecount, 0, 0);

Vertex shader input:

struct VS_INPUT
{
float3 position : POSITION;
float2 texcoord : TEXCOORD;
float3 objposition : INSTANCEPOS;
};

When I look at the instance buffer in the graphics debugger, it says that all of the values are 0 (I assume it's not updating properly). My mesh is just a square made up of 2 triangles, but the graphics debugger says that only a single triangle with a line poking out of it is being drawn (I can show a picture if it helps).

 
Any ideas to what's causing this?
 
Thanks.

Share this post


Link to post
Share on other sites
Advertisement

Check the HRESULT value of :

D3DDevice->CreateBuffer(&InstanceBufferDesc, nullptr, &instancebuffer);

I don't think "INSTANCEPOS" is a valid input semantic, so first of try changing it to "TEXCOORD".

 

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx

 

You'll also want to modify your vertex shader input structure:

struct VS_INPUT
{
float3 position : POSITION;
float2 texcoord : TEXCOORD0;
float3 objposition : TEXCOORD1;
};
Edited by Orangeatang

Share this post


Link to post
Share on other sites

Also, when you create your instance buffer :

HRESULT result = D3DDevice->CreateBuffer(&InstanceBufferDesc, nullptr, &instancebuffer);

You're not supplying it with any instance data. You need to supply a D3D11_SUBRESOURCE_DATA structure (as the second parameter) filled with you instanced data (which in this case, I assume, are the positions you'd like to render the mesh at).

Edited by Orangeatang

Share this post


Link to post
Share on other sites

The error references to the vertex buffer at slot 0 which should contain your mesh vertex data. Check that your mesh vertex buffer is created correctly and that your index buffer doesn't index outside of the vertex buffer. 

 

Under D3D11 you may use what ever semantic name.

 

Cheers!

Share this post


Link to post
Share on other sites

Thanks for the responses.

 

The vertex, index and instance buffers are all created successfully (ie they return non-null and without errors/warnings). VS's graphics debugger says that both the vertex (mesh) buffer and the index buffer have the values I want there. The instance buffer does not, it has only all zeros. The index values are definitely valid and works fine without instancing.

 

When creating the vertex data, I don't need to leave space for the instance data in the vertex (mesh) memory, right?

Share this post


Link to post
Share on other sites


When creating the vertex data, I don't need to leave space for the instance data in the vertex (mesh) memory, right?

No, you just have to add the instanced data members to the end of your shader's input declaration

Share this post


Link to post
Share on other sites

 


When creating the vertex data, I don't need to leave space for the instance data in the vertex (mesh) memory, right?

No, you just have to add the instanced data members to the end of your shader's input declaration

 

Yeah, so it seems like I'm doing everything right... except the results don't agree with me.sad.png

Share this post


Link to post
Share on other sites

What does your vertex buffer structure look like? 

 

Edit : Forget that - it's constant buffers that have to be 16-byte aligned. Apologies :)

Edited by Orangeatang

Share this post


Link to post
Share on other sites

What does your vertex buffer structure look like? Is it 16-byte aligned?

 

Nope. Should it be?

struct GfxVertex
{
MthVector3 Position;
MthVector2 TexCoord;
};
Edited by stu_pidd_cow

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!