Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

igl | opengl | Fix memory leak in RenderPipelineState::activeAttributesLocations_ #187

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/igl/opengl/RenderCommandAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,11 @@ void RenderCommandAdapter::clearDependentResources(
clearFragmentTexture();
}

if (!newStateOpenGL || !curStateOpenGL->matchesVertexInputState(*newStateOpenGL)) {
// We do need to clear vertex attributes, when pipelinestate is modified.
// If we don't, subsequent draw calls might try to read from these locations
// and crashes might happen.
unbindVertexAttributes();
if (curStateOpenGL && newStateOpenGL){
newStateOpenGL->savePrePipelineStateAttributesLocations(*curStateOpenGL);
}

if (!newStateOpenGL || !curStateOpenGL->matchesVertexInputState(*newStateOpenGL)) {
// Don't reuse previously set vertex buffers.
clearVertexBuffers();
}
Expand Down Expand Up @@ -346,6 +345,7 @@ void RenderCommandAdapter::willDraw() {

// Vertex Buffers must be bound before pipelineState->bind()
if (pipelineState) {
pipelineState->clearActiveAttributesLocations();
for (size_t bufferIndex = 0; bufferIndex < IGL_VERTEX_BUFFER_MAX; ++bufferIndex) {
if (IS_DIRTY(vertexBuffersDirty_, bufferIndex)) {
auto& bufferState = vertexBuffers_[bufferIndex];
Expand All @@ -355,6 +355,7 @@ void RenderCommandAdapter::willDraw() {
CLEAR_DIRTY(vertexBuffersDirty_, bufferIndex);
}
}
pipelineState->unbindPrePipelineVertexAttributes();
if (isDirty(StateMask::PIPELINE)) {
pipelineState->bind();
clearDirty(StateMask::PIPELINE);
Expand Down
11 changes: 11 additions & 0 deletions src/igl/opengl/RenderPipelineState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ void RenderPipelineState::bindVertexAttributes(size_t bufferIndex, size_t buffer
IGL_DEBUG_ASSERT(location < sMaxNumVertexAttribs);
activeAttributesLocations_.push_back(location);

prePipelineStateAttributesLocations_.erase(std::remove(prePipelineStateAttributesLocations_.begin(),
prePipelineStateAttributesLocations_.end(), location),
prePipelineStateAttributesLocations_.end());

getContext().enableVertexAttribArray(location);
const auto& attribute = attribList[i];
getContext().vertexAttribPointer(
Expand Down Expand Up @@ -317,6 +321,13 @@ void RenderPipelineState::unbindVertexAttributes() {
activeAttributesLocations_.clear();
}

void RenderPipelineState::unbindPrePipelineVertexAttributes() {
for (const auto& l : prePipelineStateAttributesLocations_) {
getContext().disableVertexAttribArray(l);
}
prePipelineStateAttributesLocations_.clear();
}

// Looks up the location the of the specified texture unit via its name,
// bind the unit to the location, then activate the unit.
//
Expand Down
11 changes: 11 additions & 0 deletions src/igl/opengl/RenderPipelineState.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class RenderPipelineState final : public WithContext, public IRenderPipelineStat
return static_cast<ShaderStages*>(desc_.shaderStages.get());
}

void savePrePipelineStateAttributesLocations(RenderPipelineState & prePipelineState){
prePipelineStateAttributesLocations_ = std::move(prePipelineState.activeAttributesLocations_);
}

void clearActiveAttributesLocations(){
activeAttributesLocations_.clear();
}

void unbindPrePipelineVertexAttributes();

private:
// Tracks a list of attribute locations associated with a bufferIndex
std::vector<int> bufferAttribLocations_[IGL_VERTEX_BUFFER_MAX];
Expand All @@ -84,6 +94,7 @@ class RenderPipelineState final : public WithContext, public IRenderPipelineStat
std::array<GLint, IGL_TEXTURE_SAMPLERS_MAX> unitSamplerLocationMap_{};
std::unordered_map<int, size_t> uniformBlockBindingMap_;
std::array<GLboolean, 4> colorMask_ = {GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE};
std::vector<int> prePipelineStateAttributesLocations_;
std::vector<int> activeAttributesLocations_;
BlendMode blendMode_ = {GL_FUNC_ADD, GL_FUNC_ADD, GL_ONE, GL_ZERO, GL_ONE, GL_ZERO};
bool blendEnabled_ = false;
Expand Down