Skip to content

Commit

Permalink
Fix D3D11 push buffer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 24, 2017
1 parent bb9181b commit ce81826
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion GPU/D3D11/D3D11Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class PushBufferD3D11 {
public:
PushBufferD3D11(ID3D11Device *device, size_t size, D3D11_BIND_FLAG bindFlags) {
PushBufferD3D11(ID3D11Device *device, size_t size, D3D11_BIND_FLAG bindFlags) : size_(size) {
D3D11_BUFFER_DESC desc{};
desc.BindFlags = bindFlags;
desc.ByteWidth = (UINT)size;
Expand All @@ -47,6 +47,12 @@ class PushBufferD3D11 {
uint8_t *BeginPush(ID3D11DeviceContext *context, UINT *offset, size_t size, int align = 16) {
D3D11_MAPPED_SUBRESOURCE map;
pos_ = (pos_ + align - 1) & ~(align - 1);
if (pos_ + size > size_) {
// Wrap! Note that with this method, since we return the same buffer as before, you have to do the draw immediately after,
// can't defer like in Vulkan. We instead let the driver handle the invalidation etc.
pos_ = 0;
nextMapDiscard_ = true;
}
context->Map(buffer_, 0, nextMapDiscard_ ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE, 0, &map);
nextMapDiscard_ = false;
*offset = (UINT)pos_;
Expand All @@ -61,6 +67,7 @@ class PushBufferD3D11 {
private:
ID3D11Buffer *buffer_ = nullptr;
size_t pos_ = 0;
size_t size_;
bool nextMapDiscard_ = false;
};

Expand Down

0 comments on commit ce81826

Please sign in to comment.