Jump to content

  • Log In with Google      Sign In   
  • Create Account


Constant Buffers and multiple objects question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Firelark   Members   -  Reputation: 105

Like
0Likes
Like

Posted 19 April 2012 - 11:40 AM

Hello hello

I have an effect to be used on several objects. Is it ok to have one buffer object that you can update between rendering? Or is this bad for performance? I read here that mapping resources before the command buffer is finished with them will hurt performance.

Instead is it better to have one buffer object (ID3D10Buffer in this case) for each object to be rendered by this effect?

Below is a simplified exaple of what I mean, I hope it makes sence Posted Image

This code is using one buffer object (ID3D10Buffer) which it initiates with the WVP matrix of the first scene object. It then renders the first scene object. Then it updates the buffer and renders the second scene object. If the command buffer is not finished with the buffer when I map it, will that cause a stall? Or is this handled behind the scene since I'm using D3D10_MAP_WRITE_DISCARD?

Thanks
Karl


ID3D10Effect *pFX;

// create effect and technique, etc....
//.... done

// Matrix structures
Matrix mWVP1 = GetWVPmatrix(my3Dobj1);
Matrix mWVP2 = GetWVPmatrix(my3Dobj2);

// Retrieve the constant buffer
ID3D10EffectConstantBuffer *pCBuf = pFX->GetConstantBufferByName(“perObj”);

// Create a buffer
D3D10_BUFFER_DESC cbDesc;
cbDesc.Usage = D3D10_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.ByteWidth = sizeof(Matrix);
D3D10_SUBRESOURCE_DATA initData;
initData.SysMemPitch = 0;
initData.SysMemSlicePitch = 0;
initData.pSysMem = reinterpret_cast<void*>(&mWVP1);

ID3D10Buffer *pBuf;
deviceObj->CreateBuffer(&cbDesc, &initData, &pBuf);

// Assign pBuf to the constant buffer
pCBuf->SetConstantBuffer(pBuf);

// Render my3Dobj1....
// ...rendering my3Dobj1 done!

// Update pBuf with my3Dobj2s WVP matrix
void *memBuf = NULL;
if(S_OK == pBuf->Map(D3D10_MAP_WRITE_DISCARD, 0, &memBuf)
{
	 if(memBuf)
	 {
			memcpy_s(memBuf, sizeof(Matrix), reinterpret_cast<void*>(&mWVP2), sizeof(Matrix));
	 }

	 pBuf->Unmap();
}

// Assign pBuf to the constant buffer again
pCBuf->SetConstantBuffer(pBuf);

// Render my3Dobj2....
// ...rendering my3Dobj2done!



Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7574

Like
1Likes
Like

Posted 19 April 2012 - 02:15 PM

MAP_WRITE_DISCARD will handle it behind the scenes for you - rather than stalling until the pipeline is done with the buffer it just gives you a fresh block of memory that you can write to. I've done some testing with 400 objects, using (a) a single constant buffer with MAP_WRITE_DISCARD, and (b) 400 separate constant buffers, one for each object. End result was that MAP_WRITE_DISCARD was more efficient by a long margin - the overhead of changing constant buffers was well in excess of the overhead of discarding and updating a single buffer.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Firelark   Members   -  Reputation: 105

Like
0Likes
Like

Posted 19 April 2012 - 11:38 PM

Thanks mhagain. That's how I thought it worked. I got a bit unsure though. I reckon having a lot of different buffers also wastes video memory, right?

#4 kauna   Crossbones+   -  Reputation: 2279

Like
0Likes
Like

Posted 20 April 2012 - 03:16 AM

There are lots of approaches to handle number of buffers vs number of updates.

Currently I'm using a single buffer object where I store all the object transformations in the beginning of the frame. So, I need to call the map/discard only once per frame for all the objects. Also, all the objects are sorted in the buffer so that I may use instancing with it. Of course, the buffer has to be big enough for all the data to work correctly and in a simple manner. Also, as far as I know, there may be some performance penalties to use a buffer object instead of a constant buffer.

The technique is described in some Frostbite engine documentations.

Cheers!

#5 Firelark   Members   -  Reputation: 105

Like
0Likes
Like

Posted 20 April 2012 - 08:16 AM

That's very interesting kauna, I'll definitivly look into that!
I'm currently not that familjar with using buffers yet, other than for constant buffers and vertex and index buffers.

Why do you use buffers rather than constant buffers in that way? Sorry for my ignorance! :)

Also isn't the performance penalties, you are talking about, to do with what kind of usage flag is set when creating the buffer?

For example; if you create a buffer with a using flag of D3D10_USAGE_DYNAMIC, a cpu flag of D3D10_CPU_ACCESS_WRITE and you map it with D3D10_MAP_WRITE_DISCARD, is there a difference in performance whether you use that buffer as a constant buffer or as a shader resource?

Or is it an issue of mapping when the bind flag is D3D10_BIND_SHADER_RESOURCE rather than D3D10_BIND_CONSTANT_BUFFER?

#6 Quat   Members   -  Reputation: 403

Like
0Likes
Like

Posted 20 April 2012 - 09:44 AM

MAP_WRITE_DISCARD will handle it behind the scenes for you - rather than stalling until the pipeline is done with the buffer it just gives you a fresh block of memory that you can write to. I've done some testing with 400 objects, using (a) a single constant buffer with MAP_WRITE_DISCARD, and (b) 400 separate constant buffers, one for each object. End result was that MAP_WRITE_DISCARD was more efficient by a long margin - the overhead of changing constant buffers was well in excess of the overhead of discarding and updating a single buffer.


Can you give more details how you did your test? The d3d10 FAQ suggests using constant buffers per-object so that you can share them between your shadow map pass and forward map pass.

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

#7 zerorepent   Members   -  Reputation: 663

Like
0Likes
Like

Posted 20 April 2012 - 02:29 PM

Another thing to consider is if you want to use the deferred context (In case you use dx11, or plan to use dx11 that is). You need one buffer per object in that case, since the data-updating of the constant-buffers aren't "deferred". At least that's how I've understood it.

#8 kauna   Crossbones+   -  Reputation: 2279

Like
0Likes
Like

Posted 21 April 2012 - 01:15 PM

Why do you use buffers rather than constant buffers in that way? Sorry for my ignorance!

Also isn't the performance penalties, you are talking about, to do with what kind of usage flag is set when creating the buffer?


Maximum size of a constant buffer 4096 * float4 (64kb). A buffer object may be up to 128mb of size. One constant buffer can't hold that much of data, although it is enough for typical use.

Practically, accessing the buffer in the vertex shader is same as reading a texture. Texture fetches has been typically a slightly more expensive than constant buffer fetch. So this is the minor performance hit.

http://developer.amd.com/gpu_assets/GDC11_DX11inBF3.pdf Look here for implementation details.

Best regards!

#9 Firelark   Members   -  Reputation: 105

Like
0Likes
Like

Posted 22 April 2012 - 11:07 PM

Maximum size of a constant buffer 4096 * float4 (64kb). A buffer object may be up to 128mb of size. One constant buffer can't hold that much of data, although it is enough for typical use.

Practically, accessing the buffer in the vertex shader is same as reading a texture. Texture fetches has been typically a slightly more expensive than constant buffer fetch. So this is the minor performance hit.

http://developer.amd...1_DX11inBF3.pdf Look here for implementation details.

Best regards!


Ah that makes sense. And thanks for the link.

#10 mhagain   Crossbones+   -  Reputation: 7574

Like
0Likes
Like

Posted 23 April 2012 - 01:29 AM


MAP_WRITE_DISCARD will handle it behind the scenes for you - rather than stalling until the pipeline is done with the buffer it just gives you a fresh block of memory that you can write to. I've done some testing with 400 objects, using (a) a single constant buffer with MAP_WRITE_DISCARD, and (b) 400 separate constant buffers, one for each object. End result was that MAP_WRITE_DISCARD was more efficient by a long margin - the overhead of changing constant buffers was well in excess of the overhead of discarding and updating a single buffer.


Can you give more details how you did your test? The d3d10 FAQ suggests using constant buffers per-object so that you can share them between your shadow map pass and forward map pass.

http://msdn.microsof...onstant_buffers

Just drew 400 objects in each mode.

The FAQ is valid for the use case outlined, yes. Obviously if just using a single buffer isn't suitable for what you want to do, then you must use a buffer per object. But if you can get away with it, the single buffer approach should be faster. As always - no hard and fast rules. Tune and modify for your own program's needs.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS