Skip to content

Graphics: (d3d12 pt I) report configured backend and unify D3D12 device#208

Merged
Segfaultd merged 1 commit into
MafiaHub:developfrom
Kheartz:d3d12-renderer-backend
Jun 15, 2026
Merged

Graphics: (d3d12 pt I) report configured backend and unify D3D12 device#208
Segfaultd merged 1 commit into
MafiaHub:developfrom
Kheartz:d3d12-renderer-backend

Conversation

@Kheartz

@Kheartz Kheartz commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Part 1/N:

The renderer never set _backend, so GetBackendType() always returned the D3D11 default and D3D12 consumers silently built no-op views. Set it from the configuration at Init.

The D3D12 backend created its descriptor heaps and RTVs from the swapchain's device but stored opts.d3d12.device, so resources later created through GetDevice() could come from a different device and be rejected by D3D12. Use the swapchain device as the single device.

With a device, the next PR will propose to allocate an additional pool D3D12 descriptors for our CEF webview textures (max 64)

Summary by CodeRabbit

  • Refactor

    • Improved D3D12 backend initialization to derive the device from the swap chain and initialize graphics resources from that source.
    • Updated D3D12 shutdown to conditionally release the device and clear it after cleanup.
    • Renderer now caches the selected graphics backend along with the full configuration.
  • Bug Fixes

    • Backend now starts with deterministic default values for device and context to avoid uninitialized-state behavior before configuration.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 893af4ee-bd45-4d5b-bcc2-f890934c93fe

📥 Commits

Reviewing files that changed from the base of the PR and between 89c2627 and 5fd25f9.

📒 Files selected for processing (3)
  • code/framework/src/graphics/backend/backend.h
  • code/framework/src/graphics/backend/d3d12.cpp
  • code/framework/src/graphics/renderer.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
  • code/framework/src/graphics/backend/backend.h
  • code/framework/src/graphics/renderer.cpp
  • code/framework/src/graphics/backend/d3d12.cpp

Walkthrough

Backend template members are now value-initialized to deterministic defaults. D3D12Backend changes device initialization to source context and device from new origins: context from options and device from the swap chain via COM guard. Device cleanup is made explicit with null-check release. Renderer additionally caches the selected backend enum in a member field.

Changes

Backend Device Initialization and Caching

Layer / File(s) Summary
Backend template member value-initialization
code/framework/src/graphics/backend/backend.h
Backend template class now value-initializes protected _device and _context members using {}, ensuring deterministic initialization across all specializations.
D3D12Backend device initialization from swap chain
code/framework/src/graphics/backend/d3d12.cpp
D3D12Backend::Init assigns _context from opts.d3d12.deviceContext and retrieves _device from swapChain->GetDevice(...) via a Microsoft::WRL::ComPtr guard instead of opts.d3d12.device, then transfers ownership to the member via Detach().
D3D12Backend device cleanup
code/framework/src/graphics/backend/d3d12.cpp
D3D12Backend::Shutdown now explicitly releases _device with a null check and sets it to nullptr in the resource cleanup sequence.
Renderer backend field caching
code/framework/src/graphics/renderer.cpp
Renderer::Init now caches the selected backend enum in _backend member field alongside the existing configuration storage.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐇 Device from swap chains, not from the air,
Context and cleanup, handled with care!
The backend is cached for speedy recall,
Initialization flows smooth through it all! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: reporting the configured backend and unifying D3D12 device management in the graphics backend.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@code/framework/src/graphics/backend/d3d12.cpp`:
- Around line 23-27: The swapChain->GetDevice() call returns a COM interface
with an incremented reference count that must be released to avoid memory leaks.
Add a Release() call to _device in the Shutdown() method to properly balance the
reference count obtained during Init(). Additionally, to prevent leaks from
early returns in Init() (such as the return false at line 25), ensure that any
early exit properly handles reference management, or structure the code so that
_device is only assigned after all initialization steps that could fail have
completed successfully.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c9fdd052-bdd1-4696-9f5e-92790f1af363

📥 Commits

Reviewing files that changed from the base of the PR and between c74fea4 and cb8c1a1.

📒 Files selected for processing (2)
  • code/framework/src/graphics/backend/d3d12.cpp
  • code/framework/src/graphics/renderer.cpp

Comment thread code/framework/src/graphics/backend/d3d12.cpp Outdated
@Kheartz Kheartz force-pushed the d3d12-renderer-backend branch from cb8c1a1 to f672e3f Compare June 15, 2026 05:27

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
code/framework/src/graphics/backend/d3d12.cpp (1)

78-81: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Resource leak: orphaned command allocator.

The allocator created at line 79 is never used and never released. The frame context allocators are created separately in the loop at lines 83-87, making this allocation dead code that leaks a COM object on every Init() call.

Remove this unused allocation:

🐛 Proposed fix
         {
-            ID3D12CommandAllocator *allocator {nullptr};
-            if (pD3DDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator)) != S_OK) {
-                return false;
-            }
-
             for (size_t i = 0; i < _frameBufferCount; i++) {
                 if (pD3DDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&_frameContext[i]._commandAllocator)) != S_OK) {
                     return false;
                 }
             }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@code/framework/src/graphics/backend/d3d12.cpp` around lines 78 - 81, Remove
the unused command allocator creation block (the allocator variable declaration
and the pD3DDevice->CreateCommandAllocator call in the if statement) as it is
dead code that never gets used or released. The frame context allocators are
created separately in the subsequent loop at lines 83-87, making this allocation
a COM object resource leak that occurs on every Init() call.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@code/framework/src/graphics/backend/d3d12.cpp`:
- Around line 78-81: Remove the unused command allocator creation block (the
allocator variable declaration and the pD3DDevice->CreateCommandAllocator call
in the if statement) as it is dead code that never gets used or released. The
frame context allocators are created separately in the subsequent loop at lines
83-87, making this allocation a COM object resource leak that occurs on every
Init() call.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 936ee22b-6787-45eb-9eb3-08af8c066627

📥 Commits

Reviewing files that changed from the base of the PR and between cb8c1a1 and f672e3f.

📒 Files selected for processing (2)
  • code/framework/src/graphics/backend/d3d12.cpp
  • code/framework/src/graphics/renderer.cpp
✅ Files skipped from review due to trivial changes (1)
  • code/framework/src/graphics/renderer.cpp

@Kheartz

Kheartz commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

@Segfaultd allocator leak coderabbit flagged seems legit, but not caused by these changes. Lemme know if you still want it addressed here.

@Segfaultd

Copy link
Copy Markdown
Member

@Kheartz I'm ok with the changes, but please reduce the amount of block comments that your AI is putting everywhere. Most of them are explaining obvious stuffs :-)

@Kheartz Kheartz force-pushed the d3d12-renderer-backend branch from f672e3f to 200d093 Compare June 15, 2026 10:52

@Segfaultd Segfaultd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline comment.

Comment thread code/framework/src/graphics/backend/d3d12.cpp Outdated
@Kheartz Kheartz force-pushed the d3d12-renderer-backend branch 2 times, most recently from 9d7e9e7 to 89c2627 Compare June 15, 2026 14:43
The renderer never set _backend, so GetBackendType() always returned
the D3D11 default and D3D12 consumers silently built no-op views.
Set it from the configuration at Init.

The D3D12 backend created its descriptor heaps and RTVs from the
swapchain's device but stored opts.d3d12.device, so resources later
created through GetDevice() could come from a different device and be
rejected by D3D12. Use the swapchain device as the single device.

GetDevice() AddRefs the returned device; own that reference in a
ComPtr so every Init early-out frees it (Renderer::Init drops the
backend without calling Shutdown on failure) and Detach it to _device
only on success, where Shutdown releases it.
@Kheartz Kheartz force-pushed the d3d12-renderer-backend branch from 4726649 to 5fd25f9 Compare June 15, 2026 15:15
@Segfaultd Segfaultd merged commit 560fb1f into MafiaHub:develop Jun 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants