multiple objects performance issue

Started by
1 comment, last by directx user 11 years, 10 months ago
Hello, its me again.
After i sucessfully initialised my shader code i experienced some performance issues with drawing my scene which contains spherical geometrie. The issue seems to be the seperate drawing calls which tookes about 0,3ms. first i thought my graphics card would be too slow to give me the necessary performance to render 1800 spheres but then i increased the vertex-count of my "sphere" and the rendering tooked the same time.

Now i know that the Issue has to be the cpu-gpu-communication, but everything at all i cant imagine how a single draw call can cost that much time?... if i look at modern games having hundreds of models displayed at the same time.

A solution would be to use instancing but iam not really sure how i can use it at dx11, basicly all that should be done is telling the gpu to loop my indexbuffer content n times insteat of calling the drawing such often. The problem is i have absolutly no idea how to do that? Creating the whole indesbuffer containing 1800 times the same index-sequence seams to be very messy and unnecessary.

And if you use different models at the same time i guess a drawing call which takes 200 ms for 1000 different object buffers isnt really aceptable so how do YOU manage it?
Advertisement
If you're going to draw the same object 1800 times, you should use instancing.

You can still use the old 9_3 style 'stream instancing', but D3D10 introduced the super-useful, more generalized InstanceID semantic. Basically, you use this as an index into a texture buffer/constant buffer/etc. to fetch a per-instance transformation matrix.

Look up the Instancing10 and Skinning10 samples in the DXSDK.
thx for your reply, i now got the instancing working but theres anyway still a question that i want to ask:

As i set the vertexbuffers one for the model and the other one for the instance position etc., how does directx knows wich buffer is ment to be the geometrie buffer and which the instance buffer?

I also experienced a graphic bug with using this code:



struct instancedata
{
XMFLOAT3 Position;
};
struct texturedvertex
{
XMFLOAT3 Pos;
XMFLOAT2 uvkoord;
};
struct geometry
{
ID3D11Buffer* vertexbuffer;
int vertsanzahl;
ID3D11Buffer* indicesbuffer;
int indicesanzahl;
};
geometry geometrybuffer;
ID3D11Buffer* instancebuffer = NULL;
ID3D11InputLayout* vertexlayouttextured = NULL;


:::::
init buffers
:::::









inp layout:

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "POSITION", 1, DXGI_FORMAT_R32G32B32_FLOAT, 1, 20, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
};
hr = device->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &vertexlayouttextured );
if( FAILED( hr ) )
return hr;





rendering:






UINT trianglebuff = sizeof(texturedvertex);
UINT instancebuff = sizeof(instancedata);
UINT zerooffset = 0;

immediatecontext->IASetInputLayout( vertexlayouttextured );
immediatecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); //Triangle strip
immediatecontext->IASetVertexBuffers( 0, 1, &geometrybuffer.vertexbuffer ,&trianglebuff , &zerooffset ); //set the geometrybuffer
immediatecontext->IASetVertexBuffers( 1, 1, &instancebuffer ,&instancebuff , &zerooffset ); //set the instance buffer

immediatecontext->VSSetShader( vertexshader, NULL, 0 ); //set vertexshader
immediatecontext->VSSetConstantBuffers( 0, 1, &matrixbuffer ); //containing world view projection matrices
immediatecontext->PSSetShader( pixelshader, NULL, 0 ); //set pixelshader
immediatecontext->PSSetShaderResources(0,1,&textur);
//set textur

immediatecontext->Map( instancebuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
instancedata *data = ( instancedata* )MappedResource.pData;
//map the instance-data
for (int i = 0; i < sys.bodiesl();i++)
{
data.Position = XMFLOAT3((float)(sys.hks.Position.x / scaler),(float)(sys.hks.Position.y / scaler),(float)(sys.hks.Position.z / scaler));
}
immediatecontext->Unmap( instancebuffer, 0 );
//update positions

immediatecontext->DrawIndexedInstanced(geometrybuffer.indicesanzahl,sys.bodies(),0,0,0);
//draw as many instances as bodies









hlsl shader:



static const float PI = 3.14159265f;




cbuffer matrix : register( b0 ) //matrices
{
row_major matrix World;
row_major matrix View;
row_major matrix Projection;

};

Texture2D<float4> Textur : register(t0);
SamplerState Meshtexturesampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
//textures, sampler


struct VS_INPUT
{
float4 Pos : POSITION0;
float2 Textcoord : TEXCOORD0;
float4 instancepos : POSITION1;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float2 Textcoord : TEXCOORD0;

};
//input structures



PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;

input.Pos.x += input.instancepos.x;
input.Pos.y += input.instancepos.y;
input.Pos.z += input.instancepos.z;

output.Pos = mul(input.Pos,World);
output.Pos = mul(output.Pos,View);
output.Pos = mul(output.Pos,Projection);
output.Textcoord = input.Textcoord;

return output;
}


float4 PS( PS_INPUT input) : SV_Target
{
return Textur.Sample(Meshtexturesampler,input.Textcoord);
}


Now the problem is that the second instance always stays at the center of the scene but it should move i guess there are some errors in the input layout declaration?

This topic is closed to new replies.

Advertisement