简体   繁体   中英

Partially updating D3D11 constant buffer

In my spare time, I am working on a 3D engine using D3D11. To get the 3D effect, I use the typical model view projection matrix multiplication in my HLSL shaders. These matrices are uploaded to a d3d11 constant buffer. The projection matrix only changes when the viewport is resized but the model and view matrix can change on a frame per frame basis (when the model or camera is moved). These changes in the matrices must be uploaded to the same constant buffer in order to be used in the shaders. When uploading these changes, the projection matrix is (generally) not changed so I do not want to reupload this matrix. So in short, I need to partially update my constant buffer by only updating specific parts (offsets) in my buffer.

In openGL, we have uniform buffers and these work (I think) in the same way as a d3d11 constant buffer does. However, if you want to update a specific part of the uniform buffer, you can use the openGL function glBufferSubData. I tried looking for a similar way to do this in D3D11 but I did not found anything. I did find someone with a similar issue but he was using D3D11.1. link to original post: How to partially update constant buffer in DirectX 11.1 . Someone also said that in D3D11, you need to upload all the data in to the constant buffer if you want to change a specifi portion. But this could result in keeping an entire copy of my buffer on the CPU side (RAM). There must be a better way right?

tldr; How can I update my d3d11 constant buffer on a specific offset?

Why not have multiple constant buffers? That would allow you to have one that rarely if ever changes while others can be changed with each frame. I'm pretty sure I've seen that used in some of the samples (though I didn't go looking).

You have several options to manage those.

The simplest ones is to create one constant buffer per update rate (so you would have one buffer for world matrices, which changes per object, one for view which would update once per frame, and one for projection which updates very sporadically.

That said, I never found the fact of splitting view and projection matrices to be that beneficial (I mean we speak about a 64 bytes extra data update per frame, which is really nothing, specially considering constant buffers are aligned on a 256 bytes boundary).

This is also even more true if you will start to add shadow maps (if you do frustum fitting your shadow camera projection matrix can change every frame).

Also if you plan to other post effects later on (like ao, depth of field...), you will also need to add extra data for camera (position, view inverse, view projection inverse, projection inverse), so it's generally more beneficial to update all that camera data once per frame (I normally use a reserved cbuffer slot for camera buffer, so I don't have to rebind it all the time).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM