Binding buffers and then updating them

Started by
15 comments, last by noodleBowl 6 years, 5 months ago

I got a quick question about buffers when it comes to DirectX 11. If I bind a buffer using a command like:


IASetVertexBuffers
IASetIndexBuffer
VSSetConstantBuffers
PSSetConstantBuffers

 and then later on I update that bound buffer's data using commands like Map/Unmap or any of the other update commands.

Do I need to rebind the buffer again in order for my update to take effect? If I dont rebind is that really bad as in I get a performance hit? My thought process behind this is that if the buffer is already bound why do I need to rebind it? I'm using that same buffer it is just different data

 

Advertisement

You don't need to rebind. There may be a hit if you Map a buffer that is still in use by the GPU, depending on the flags. Choose wisely between Discard and No Overwrite. Discard will only stall if you Map it a lot without letting frames complete. It's a good choice if you only Map that buffer once a frame. No Overwrite should not stall, but may corrupt if you overwrite in-use data. It's good for streaming purposes where you never go backwards. Remember that this still counts across frames, you should Discard once before No Overwrite, or else set GPU fences.

If you're lost at this stage, take a look here: https://msdn.microsoft.com/en-us/library/windows/desktop/dn508285(v=vs.85).aspx

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
16 hours ago, Promit said:

Discard will only stall if you Map it a lot without letting frames complete. It's a good choice if you only Map that buffer once a frame. No Overwrite should not stall, but may corrupt if you overwrite in-use data. It's good for streaming purposes where you never go backwards. Remember that this still counts across frames, you should Discard once before No Overwrite, or else set GPU fences.

In the article you linked there is a part where they go over using D3D11_MAP_WRITE_NO_OVERWRITE and D3D11_MAP_WRITE_DISCARD when mapping. Where you use D3D11_MAP_WRITE_DISCARD starting off and then successive map/unmap calls should use D3D11_MAP_WRITE_NO_OVERWRITE until your buffer becomes full again and you switch back to the D3D11_MAP_WRITE_DISCARD flag.

So now that has me wondering should I be placing everything in one large buffer? That way I don't have keep rebinding all these different buffers. Something like:


//The big buffer was already bound at this point (the intialization point)
for(std::vector<Renderable>::iterator renderable = renderables.begin(); renderable != renderables.end(); ++renderable)
{
	if(bigBufferIsNotFull)
	{
		D3D11_MAPPED_SUBRESOURCE resource = Map(bigBuffer, D3D11_MAP_WRITE_NO_OVERWRITE)
		memcpy(resource.pData, (*renderable).data, (*renderable).dataSize); //(*renderable).data in this case is a vector or array of vertex data
		Unmap();
	}
	else //Our big buffer is full
	{
		D3D11_MAPPED_SUBRESOURCE resource = Map(bigBuffer, D3D11_MAP_WRITE_DISCARD)
		memcpy(resource.pData, (*renderable).data, (*renderable).dataSize); //(*renderable).data in this case is a vector or array of vertex data
		Unmap();
	}
	Draw((*renderable).vertexCount);
}

Opposed to:


for(std::vector<Renderable>::iterator renderable = renderables.begin(); renderable != renderables.end(); ++renderable)
{
  	//(*renderable).buffer is a ID3D11Buffer and at some point (renderable creation) the buffer was mapped with the needed data
  	BindVertexBuffer((*renderable).buffer); 
  	Draw((*renderable).vertexCount);
}

 

41 minutes ago, noodleBowl said:

So now that has me wondering should I be placing everything in one large buffer? That way I don't have keep rebinding all these different buffers.

Yes, with caveats. You can only Discard a buffer so many times* before the driver stalls. Think of it like this: when you are doing this, your buffer is actually three buffers internally. Each time you discard, it switches from buffer 0 to 1, 1 to 2, 2 to 3, 3 to 0. But if that switch to 0 arrives while 0 is still in use, you're going to stall. So the buffer has to be big enough that you don't do this more than once or maybe twice a frame in order for the GPU to clear pending operations. It's best to make the buffer big enough to accommodate an entire frame's rendering in one go with the occasional spill. 

* The limit doesn't apply to constant buffers, which are magical buffers that can service thousands of discards per frame.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
2 hours ago, Promit said:

Yes, with caveats

What is kind of tripping me up is how this big buffer exists. In the sense that I could have multiple vertex types like this


class VertexTypeA
{
	Vector3 pos;
	Color color;
}

class VertexTypeB
{
	Vector3 pos;
	Color color;
	Vector2 texCoord
}

class VertexTypeC
{
	//Some data to represent VertexTypeC
}

But surly these can't go all into the same buffer. I would need a VertexTypeA buffer, VertexTypeB buffer, VertexTypeC buffer, etc right? 

In the real world, are there renders that solely focus on one object to render? EG: You have a SpriteRenderer and its only job is to render sprites, then you have a CarRenderer and its only ever going to render cars, a CharacterRenderer that only renders characters so on and so forth. Or is there some single renderer entity that does everything (I really don't think this is the case)?

Maybe a combo, where everything is fed into single renderer but then its really delegated out to the sub renderers (SpriteRenderer, CarRenderer, etc) to handle what you are actually wanting to draw?

4 hours ago, noodleBowl said:

In the real world, are there renders that solely focus on one object to render? EG: You have a SpriteRenderer and its only job is to render sprites, then you have a CarRenderer and its only ever going to render cars, a CharacterRenderer that only renders characters so on and so forth. Or is there some single renderer entity that does everything (I really don't think this is the case)?

You can partition your objects based on what data you need in your vertices. Like you said, sprites will probably be different from static objects: sprite vertices can have a position, pair of texture coordinates and color, whereas a static object vertex could have a position, pair of texture coordinates and normal. In the end, you will have few partitions. You could use some kind of "subrenderer" for each partition like you described it or at least for the partitions which are very different. Partitions which use for instance a different vertex shader, but the same pixel shader (the same lighting calculations for instance) could be handled by the same "subrenderer" which just switches the vertex shader or sorts based on the vertex shader beforehand.

Note, however, that a dynamic rigid (or deformable) object is more abstract than a concrete "car". So if the car is not the main focus by itself in your game, you do not necessarily need special car-only treatment in your game engine.

🧙

15 hours ago, matt77hias said:

You can partition your objects based on what data you need in your vertices ... In the end, you will have few partitions. You could use some kind of "subrenderer" for each partition like you described it or at least for the partitions which are very different. 

Note, however, that a dynamic rigid (or deformable) object is more abstract than a concrete "car". So if the car is not the main focus by itself in your game, you do not necessarily need special car-only treatment in your game engine.

So its not necessarily a renderer per thing (sprite, car, etc), but really a renderer per vertex type (VertexTypeA renderer, VertexTypeB renderer, or etc) and or task (lighting)

 

Previously I also I had some psudeo code like:


D3D11_MAPPED_SUBRESOURCE resource = Map(bigBuffer, D3D11_MAP_WRITE_NO_OVERWRITE)
memcpy(resource.pData, (*renderable).data, (*renderable).dataSize); //(*renderable).data in this case is a vector or array of vertex data
Unmap();

And the renderable's data was in a std::vector or array. But is it possible to have the vertex data already in a ID3D11Buffer and then copy this data directly into the big buffer owned by the renderer? Or would this be really bad because it could trigger a read from the GPU and that in turn would be super slow and cause stalling

How should I be copying data over into the renderer's big buffer? Is it really just a simple memcpy from a std::vector (renderable's vertex data) into the ID3D11Buffer (renderers buffer) using map/unmap

8 hours ago, noodleBowl said:

So its not necessarily a renderer per thing (sprite, car, etc), but really a renderer per vertex type (VertexTypeA renderer, VertexTypeB renderer, or etc) and or task (lighting)

Sort of. You can render static objects, skeleton animated objects, sprites, etc.

For static objects you can use a single vertex layout or a few vertex layouts (if you want to pre-compute tangents for some of them for instance). Your renderer for static objects knows which layouts are supported for static objects and is capable of selecting the appropriate one for each static object. Then you can for example sort static objects based on the layout and render all static objects with the same layout consecutively.

🧙

11 hours ago, matt77hias said:

For static objects you can use a single vertex layout or a few vertex layouts (if you want to pre-compute tangents for some of them for instance). Your renderer for static objects knows which layouts are supported for static objects and is capable of selecting the appropriate one for each static object. Then you can for example sort static objects based on the layout and render all static objects with the same layout consecutively.

Maybe I'm missing something here, but wouldn't a different input layout essentially mean a different vertex type?


//Vertex Type 1
D3D11_INPUT_ELEMENT_DESC layout1[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

//Vertex Type 2
D3D11_INPUT_ELEMENT_DESC layout1[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"TEXCOORDS", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

 

You can have a single vertex shader that takes positions + colours as inputs, but create three different input layouts so that you can use that one shader with three different storage formats, e.g.


D3D11_INPUT_ELEMENT_DESC layout1[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
//maps to:
struct Layout1Stream0 { float posiion[3]; float color[4]; };


D3D11_INPUT_ELEMENT_DESC layout2[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
//maps to:
struct Layout2Stream0 { float posiion[3]; };
struct Layout2Stream1 { float color[4]; };


D3D11_INPUT_ELEMENT_DESC layout3[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
//maps to:
struct Layout3Stream0 { float posiion[3]; unsigned char color[4]; };

This topic is closed to new replies.

Advertisement