# Update only a part of a buffer?

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

## Recommended Posts

I wonder if it is possible to update only a part of a buffer?

So lets say I need a huge buffer. But most of the time I will only use the first few bytes of that buffer. Only randomly I might need more.

Currently I am just having two buffers one is small and one big. The big buffer only gets updated if there is actually that much data needed.

But now I thought why one has to make it so complicated. Shouldn't there be a way to update only a part of a big buffer? (Of course it should not be slower than having two buffers) If so, any advice where i should look?

And if there is no way to update only a part of a structured buffer I have another question:

Can you have a pointer to a buffer inside HLSL? And if so how would be the syntax?

Like if I want to do something like this:

StructuredBuffer<Structure> Small : register(t0);
StructuredBuffer<Structure> Big: register(t1);

Structure * MyBuffer;

If (whatever) {
MyBuffer = &Small;
} else {
MyBuffer = &Big;
}

RunSomeAlgorithmWithBuffer(MyBuffer);
Edited by me_12

##### Share on other sites
Use UpdateSubresource. You probably need to play a bit with the pDstBox parameters (docs are enigmatic about non-texture resources). IIRC for raw buffers you only use the x dimension (using byte size) and set both y and z range to 0-1. Similarly, the pitches can be set to 0. Don't know if this is the same for structured buffers.

Update: Yep, works the same for structured buffers. The range must be a multiple of the structure size (which makes actually sense). Also, your buffer must not be D3D11_USAGE_DYNAMIC but D3D11_USAGE_DEFAULT. Edited by unbird

##### Share on other sites

Use UpdateSubresource. You probably need to play a bit with the pDstBox parameters (docs are enigmatic about non-texture resources). IIRC for raw buffers you only use the x dimension (using byte size) and set both y and z range to 0-1. Similarly, the pitches can be set to 0. Don't know if this is the same for structured buffers.

Update: Yep, works the same for structured buffers. The range must be a multiple of the structure size (which makes actually sense). Also, your buffer must not be D3D11_USAGE_DYNAMIC but D3D11_USAGE_DEFAULT.

Updating i.e. 16 Bytes of a huge buffer with this method would be the same as updating a 16 byte buffer completely? (If you have any experiences here)

##### Share on other sites

Thank you so far. I have just tested it with:

D3D11_BOX destRegion;
destRegion.left = 0;
destRegion.right = iArrayEntries* sizeof(PARTICLE_SPAWN_DISTRIBUTION);
destRegion.top = 0;
destRegion.bottom = 1;
destRegion.front = 0;
destRegion.back = 1;

m_pRenderAPI->m_pdevicecontext->UpdateSubresource(m_pbParticleDistribution, 0, &destRegion, aParticleDistribution, 0, 0);

Seems to work. Yet no idea about the performance though. I just hope that it does what it says and only copies those few bytes over to the GPU.

Edited by me_12

##### Share on other sites
It should, I checked earlier with PIX (you should do that, too). Glad to hear you got it.

Performance ? Use different approaches and profile .

Alternatives (just throwing some ideas, never tried myself):
• Use a dynamic buffer and partially update with Map and D3D11_MAP_WRITE_DISCARD/D3D11_MAP_WRITE_NO_OVERWRITE. This is more involved to setup (search the forums/google), so be warned. Then apply a array offset in your shader (to the position where you just updated).
• Use (several) staging buffers and CopySubResourceRegion
Additional note: That conditional buffer selection in your original post is IMO unnecessary. Use one slot only and just bind the buffer you want. Edited by unbird