Skip to content

Commit 8b2fc9f

Browse files
committed
Plugin example: Fix some access violations
1 parent 38fa684 commit 8b2fc9f

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

examples/renderlib/rendering/d3d11.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ bool D3D11::initialize() {
5959
}
6060

6161
void D3D11::render_imgui() {
62+
auto draw_data = ImGui::GetDrawData();
63+
64+
if (draw_data == nullptr) {
65+
return;
66+
}
67+
6268
ComPtr<ID3D11DeviceContext> context{};
6369
float clear_color[]{0.0f, 0.0f, 0.0f, 0.0f};
6470

@@ -67,14 +73,23 @@ void D3D11::render_imgui() {
6773
device->GetImmediateContext(&context);
6874
context->ClearRenderTargetView(this->rt_rtv.Get(), clear_color);
6975
context->OMSetRenderTargets(1, this->rt_rtv.GetAddressOf(), NULL);
70-
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
76+
ImGui_ImplDX11_RenderDrawData(draw_data);
7177

7278
// Set the back buffer to be the render target.
7379
context->OMSetRenderTargets(1, this->bb_rtv.GetAddressOf(), nullptr);
74-
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
80+
ImGui_ImplDX11_RenderDrawData(draw_data);
7581
}
7682

7783
void D3D11::render_imgui_vr(ID3D11DeviceContext* context, ID3D11RenderTargetView* rtv) {
84+
if (context == nullptr || rtv == nullptr) {
85+
return;
86+
}
87+
88+
auto draw_data = ImGui::GetDrawData();
89+
if (draw_data == nullptr) {
90+
return;
91+
}
92+
7893
context->OMSetRenderTargets(1, &rtv, NULL);
79-
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
94+
ImGui_ImplDX11_RenderDrawData(draw_data);
8095
}

examples/renderlib/rendering/d3d12.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ bool D3D12::initialize() {
118118
return true;
119119
}
120120

121-
void D3D12::render_imgui() {
121+
void D3D12::render_imgui() {
122+
auto draw_data = ImGui::GetDrawData();
123+
124+
if (draw_data == nullptr) {
125+
return;
126+
}
127+
122128
auto& cmd = this->cmds[this->frame_count++ % this->cmds.size()];
123129

124130
if (cmd.fence_event != nullptr && cmd.fence != nullptr && cmd.fence->GetCompletedValue() < cmd.fence_value) {
@@ -166,7 +172,7 @@ void D3D12::render_imgui() {
166172
rts[0] = this->get_cpu_rtv(device, (D3D12::RTV)bb_index);
167173
cmd.list->OMSetRenderTargets(1, rts, FALSE, NULL);
168174
cmd.list->SetDescriptorHeaps(1, this->srv_desc_heap.GetAddressOf());
169-
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), cmd.list.Get());
175+
ImGui_ImplDX12_RenderDrawData(draw_data, cmd.list.Get());
170176
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
171177
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
172178
cmd.list->ResourceBarrier(1, &barrier);

0 commit comments

Comments
 (0)