Skip to content

Commit

Permalink
Correct size calculations in GLPushBuffer. I don't see how this faile…
Browse files Browse the repository at this point in the history
…d as badly as in that crash report though...
  • Loading branch information
hrydgard committed May 3, 2023
1 parent d56e27a commit 5724bbd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Common/GPU/OpenGL/GLRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ bool GLPushBuffer::AddBuffer() {
if (!info.localMemory)
return false;
info.buffer = render_->CreateBuffer(target_, size_, GL_DYNAMIC_DRAW);
info.size = size_;
buf_ = buffers_.size();
buffers_.push_back(info);
return true;
Expand All @@ -605,7 +606,6 @@ void GLPushBuffer::Destroy(bool onRenderThread) {
} else {
render_->DeleteBuffer(info.buffer);
}

FreeAlignedMemory(info.localMemory);
}
buffers_.clear();
Expand Down Expand Up @@ -652,18 +652,31 @@ void GLPushBuffer::Defragment() {
}

// Okay, we have more than one. Destroy them all and start over with a larger one.
size_t newSize = size_ * buffers_.size();

// When calling AddBuffer, we sometimes increase size_. So if we allocated multiple buffers in a frame,
// they won't all have the same size. Sum things up properly.
size_t newSize = 0;
for (int i = 0; i < (int)buffers_.size(); i++) {
newSize += buffers_[i].size;
}

Destroy(false);

size_ = newSize;
// Set some sane but very free limits. If there's another spike, we'll just allocate more anyway.
size_ = std::min(std::max(newSize, (size_t)65536), (size_t)(512 * 1024 * 1024));
bool res = AddBuffer();
_assert_msg_(res, "AddBuffer failed");
}

size_t GLPushBuffer::GetTotalSize() const {
size_t sum = 0;
if (buffers_.size() > 1)
sum += size_ * (buffers_.size() - 1);
// When calling AddBuffer, we sometimes increase size_. So if we allocated multiple buffers in a frame,
// they won't all have the same size. Sum things up properly.
if (buffers_.size() > 1) {
for (int i = 0; i < (int)buffers_.size() - 1; i++) {
sum += buffers_[i].size;
}
}
sum += offset_;
return sum;
}
Expand Down
1 change: 1 addition & 0 deletions Common/GPU/OpenGL/GLRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class GLPushBuffer {
uint8_t *localMemory = nullptr;
uint8_t *deviceMemory = nullptr;
size_t flushOffset = 0;
size_t size;
};

GLPushBuffer(GLRenderManager *render, GLuint target, size_t size);
Expand Down

0 comments on commit 5724bbd

Please sign in to comment.