fix(graphic): resolve memory leaks and improve texture view management#573
Conversation
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 6 minutes and 52 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe changes introduce a Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/plugin/default-pipeline/src/system/GPUComponentManagement/OnDirectionalLightCreation.cpp (1)
22-22:⚠️ Potential issue | 🟡 MinorStatic
lightIndexcounter is never decremented, causing issues with light re-creation.The
lightIndexis incremented but never decremented when lights are destroyed. If a directional light is destroyed and a new one created,baseArrayLayerwill keep increasing, potentially exceeding the texture array bounds.Consider tracking available indices or resetting appropriately.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/plugin/default-pipeline/src/system/GPUComponentManagement/OnDirectionalLightCreation.cpp` at line 22, The static counter lightIndex in OnDirectionalLightCreation.cpp is only incremented and never returned when lights are destroyed, so change the allocation strategy to use a reusable pool: add a container (e.g., std::vector<uint32_t> freeLightIndices or std::stack<uint32_t> availableIndices) and modify the creation path in OnDirectionalLightCreation (where lightIndex is used to compute baseArrayLayer) to pop an index from availableIndices if non-empty, otherwise use and increment lightIndex; also add logic in the directional light destruction handler (or destructor for the directional light class) to push the freed index back into availableIndices so baseArrayLayer cannot grow past bounds. Ensure all references to lightIndex and baseArrayLayer in OnDirectionalLightCreation.cpp use the new allocation function to obtain indices.
🧹 Nitpick comments (4)
src/plugin/default-pipeline/src/resource/pass/Shadow.hpp (1)
93-93: Consider renamingshadowTextureViewto reflect ID semantics.This assignment now passes an ID (
depthTextureViewId), so keeping the source field name asshadowTextureViewcan be misleading for future call sites.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/plugin/default-pipeline/src/resource/pass/Shadow.hpp` at line 93, The field lightGPUComponent.shadowTextureView is misleading because it actually holds an ID assigned to outputs.depthBuffer->depthTextureViewId; rename the field to shadowTextureViewId across the LightGPUComponent (or whatever class/struct defines it) and update all usages (including the assignment outputs.depthBuffer->depthTextureViewId = lightGPUComponent.shadowTextureViewId) to reflect the ID semantics while keeping the underlying type unchanged; ensure any serialization, constructors, or comments referencing shadowTextureView are updated too.src/plugin/default-pipeline/src/component/GPUDirectionalLight.hpp (1)
20-21: Use= defaultfor the trivial destructor.The empty destructor should use
= defaultinstead of an empty body. This follows modern C++ best practices and allows the compiler to potentially optimize better.♻️ Proposed fix
- ~GPUDirectionalLight() {} - + ~GPUDirectionalLight() = default;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/plugin/default-pipeline/src/component/GPUDirectionalLight.hpp` around lines 20 - 21, Replace the trivial empty destructor definition "~GPUDirectionalLight() {}" in the GPUDirectionalLight class with a defaulted destructor using "= default;" (i.e., make ~GPUDirectionalLight() = default;) so the compiler can treat it as a defaulted special member; update the declaration/definition where "~GPUDirectionalLight()" appears in GPUDirectionalLight.hpp accordingly.src/plugin/graphic/src/resource/TextureView.hpp (2)
50-50: Addexplicitto the conversion operator.Implicit conversion operators can cause unintended type conversions and make code harder to reason about. Since
GetWebGPUView()already provides explicit access to the underlying handle, consider making this conversion explicit.♻️ Proposed fix
- operator wgpu::TextureView() const { return _webgpuView; } + explicit operator wgpu::TextureView() const { return _webgpuView; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/plugin/graphic/src/resource/TextureView.hpp` at line 50, Mark the conversion operator operator wgpu::TextureView() const as explicit to prevent implicit conversions; update the declaration/definition of that operator to be explicit and ensure existing call sites use either GetWebGPUView() or an explicit cast (e.g., static_cast<wgpu::TextureView>(...)) where needed.
3-17: Remove unnecessary includes.This header only defines a simple wrapper around
wgpu::TextureView. The includes forImage.hpp,GetBytesPerPixel.hpp,<array>,<bit>,<cstddef>,<cstdint>,<cstring>,<functional>,<glm/gtc/packing.hpp>,<glm/vec2.hpp>,<glm/vec4.hpp>, and<vector>appear to be copy-pasted fromTexture.hppand are not used here.♻️ Proposed fix
`#pragma` once -#include "exception/UnsupportedTextureFormatError.hpp" -#include "resource/Context.hpp" -#include "resource/Image.hpp" -#include "utils/GetBytesPerPixel.hpp" `#include` "utils/webgpu.hpp" -#include <array> -#include <bit> -#include <cstddef> -#include <cstdint> -#include <cstring> -#include <functional> -#include <glm/gtc/packing.hpp> -#include <glm/vec2.hpp> -#include <glm/vec4.hpp> -#include <vector> namespace Graphic::Resource {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/plugin/graphic/src/resource/TextureView.hpp` around lines 3 - 17, TextureView.hpp currently pulls in many unused headers copied from Texture.hpp; remove the unnecessary includes (Image.hpp, GetBytesPerPixel.hpp, <array>, <bit>, <cstddef>, <cstdint>, <cstring>, <functional>, <glm/gtc/packing.hpp>, <glm/vec2.hpp>, <glm/vec4.hpp>, and <vector>) and keep only the headers required for the simple wrapper around wgpu::TextureView (e.g., utils/webgpu.hpp and any minimal exception or forward-declaration headers actually referenced by the TextureView class); update the include list in TextureView.hpp so it only contains the minimal set needed by the TextureView symbol and any directly used types like UnsupportedTextureFormatError if it is used.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/plugin/graphic/src/resource/TextureView.hpp`:
- Line 24: The TextureView constructor should be marked explicit and must move
the rvalue parameter into the member to avoid an unintended copy; update the
TextureView(wgpu::TextureView &&textureView) constructor to be explicit and
initialize _webgpuView with std::move(textureView) (referencing the TextureView
constructor and the _webgpuView member) so the rvalue is forwarded correctly.
---
Outside diff comments:
In
`@src/plugin/default-pipeline/src/system/GPUComponentManagement/OnDirectionalLightCreation.cpp`:
- Line 22: The static counter lightIndex in OnDirectionalLightCreation.cpp is
only incremented and never returned when lights are destroyed, so change the
allocation strategy to use a reusable pool: add a container (e.g.,
std::vector<uint32_t> freeLightIndices or std::stack<uint32_t> availableIndices)
and modify the creation path in OnDirectionalLightCreation (where lightIndex is
used to compute baseArrayLayer) to pop an index from availableIndices if
non-empty, otherwise use and increment lightIndex; also add logic in the
directional light destruction handler (or destructor for the directional light
class) to push the freed index back into availableIndices so baseArrayLayer
cannot grow past bounds. Ensure all references to lightIndex and baseArrayLayer
in OnDirectionalLightCreation.cpp use the new allocation function to obtain
indices.
---
Nitpick comments:
In `@src/plugin/default-pipeline/src/component/GPUDirectionalLight.hpp`:
- Around line 20-21: Replace the trivial empty destructor definition
"~GPUDirectionalLight() {}" in the GPUDirectionalLight class with a defaulted
destructor using "= default;" (i.e., make ~GPUDirectionalLight() = default;) so
the compiler can treat it as a defaulted special member; update the
declaration/definition where "~GPUDirectionalLight()" appears in
GPUDirectionalLight.hpp accordingly.
In `@src/plugin/default-pipeline/src/resource/pass/Shadow.hpp`:
- Line 93: The field lightGPUComponent.shadowTextureView is misleading because
it actually holds an ID assigned to outputs.depthBuffer->depthTextureViewId;
rename the field to shadowTextureViewId across the LightGPUComponent (or
whatever class/struct defines it) and update all usages (including the
assignment outputs.depthBuffer->depthTextureViewId =
lightGPUComponent.shadowTextureViewId) to reflect the ID semantics while keeping
the underlying type unchanged; ensure any serialization, constructors, or
comments referencing shadowTextureView are updated too.
In `@src/plugin/graphic/src/resource/TextureView.hpp`:
- Line 50: Mark the conversion operator operator wgpu::TextureView() const as
explicit to prevent implicit conversions; update the declaration/definition of
that operator to be explicit and ensure existing call sites use either
GetWebGPUView() or an explicit cast (e.g., static_cast<wgpu::TextureView>(...))
where needed.
- Around line 3-17: TextureView.hpp currently pulls in many unused headers
copied from Texture.hpp; remove the unnecessary includes (Image.hpp,
GetBytesPerPixel.hpp, <array>, <bit>, <cstddef>, <cstdint>, <cstring>,
<functional>, <glm/gtc/packing.hpp>, <glm/vec2.hpp>, <glm/vec4.hpp>, and
<vector>) and keep only the headers required for the simple wrapper around
wgpu::TextureView (e.g., utils/webgpu.hpp and any minimal exception or
forward-declaration headers actually referenced by the TextureView class);
update the include list in TextureView.hpp so it only contains the minimal set
needed by the TextureView symbol and any directly used types like
UnsupportedTextureFormatError if it is used.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1ffeea6c-2933-421e-b3ae-989912bea1e9
📒 Files selected for processing (16)
src/plugin/default-pipeline/src/component/GPUDirectionalLight.hppsrc/plugin/default-pipeline/src/resource/pass/Shadow.hppsrc/plugin/default-pipeline/src/system/GPUComponentManagement/OnDirectionalLightCreation.cppsrc/plugin/graphic/src/Graphic.hppsrc/plugin/graphic/src/plugin/PluginGraphic.cppsrc/plugin/graphic/src/resource/AMultipleExecutionRenderPass.hppsrc/plugin/graphic/src/resource/ARenderPass.hppsrc/plugin/graphic/src/resource/ASingleExecutionRenderPass.hppsrc/plugin/graphic/src/resource/BindGroup.hppsrc/plugin/graphic/src/resource/Texture.hppsrc/plugin/graphic/src/resource/TextureView.hppsrc/plugin/graphic/src/resource/TextureViewContainer.hppsrc/plugin/graphic/src/system/shutdown/ReleaseTextureView.cppsrc/plugin/graphic/src/system/shutdown/ReleaseTextureView.hppsrc/plugin/graphic/tests/BindGroupTest.cppsrc/plugin/rmlui/src/utils/RenderInterface.cpp
|


Pull Request
Description
I fixed the memory leak when using other TextureView than the default one.
Related Issues (Put "None" if there are no related issues)
close #564
Type of Change
Please delete options that are not relevant.
Changes Made
List the main changes in this PR:
Testing
Describe the tests you ran to verify your changes. Please delete options that are not relevant.
xmake test)xmake check_leaks -v GBufferTestTest Environment
Screenshots/Videos (Put "None" if there are no related issues)
None
Documentation
Please delete options that are not relevant.
Checklist (Don't delete any options)
Breaking Changes (Put "None" if there are no related issues)
None
Additional Notes (Put "None" if there are no related issues)
None
Summary by CodeRabbit
Release Notes
Refactor