Skip to content

Commit 2872604

Browse files
author
devsh
committed
fix bugs in ex 61:
- correct aspect masks on image views - wrong renderpass given to imgui - handle virtual window getting minimized - imguizmo not updating - imgui not drawing offscreen image
1 parent 64e7b26 commit 2872604

File tree

1 file changed

+70
-43
lines changed

1 file changed

+70
-43
lines changed

61_UI/main.cpp

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
9292
{},
9393
IGPURenderpass::SCreationParams::SubpassesEnd
9494
};
95+
subpasses[0].depthStencilAttachment = {{.render={.attachmentIndex=0,.layout=IGPUImage::LAYOUT::ATTACHMENT_OPTIMAL}}};
9596
subpasses[0].colorAttachments[0] = {.render={.attachmentIndex=0,.layout=IGPUImage::LAYOUT::ATTACHMENT_OPTIMAL}};
9697
params.subpasses = subpasses;
9798

@@ -137,17 +138,20 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
137138
return logFail("Failed to create Scene Renderpass!");
138139
}
139140
m_renderer = CSimpleDebugRenderer::create(m_assetMgr.get(),m_renderpass.get(),0,m_scene.get());
141+
// we'll only display one thing at a time
142+
m_renderer->m_instances.resize(1);
140143

141144
// Create ImGUI
142145
{
146+
auto scRes = static_cast<CDefaultSwapchainFramebuffers*>(m_surface->getSwapchainResources());
143147
ext::imgui::UI::SCreationParameters params = {};
144148
params.resources.texturesInfo = {.setIx=0u,.bindingIx=TexturesImGUIBindingIndex};
145149
params.resources.samplersInfo = {.setIx=0u,.bindingIx=1u};
146150
params.utilities = m_utils;
147151
params.transfer = getTransferUpQueue();
148152
params.pipelineLayout = ext::imgui::UI::createDefaultPipelineLayout(m_utils->getLogicalDevice(),params.resources.texturesInfo,params.resources.samplersInfo,MaxImGUITextures);
149153
params.assetManager = make_smart_refctd_ptr<IAssetManager>(smart_refctd_ptr(m_system));
150-
params.renderpass = m_renderpass;
154+
params.renderpass = smart_refctd_ptr<IGPURenderpass>(scRes->getRenderpass());
151155
params.subpassIx = 0u;
152156
params.pipelineCache = nullptr;
153157
interface.imGUI = ext::imgui::UI::create(std::move(params));
@@ -196,17 +200,13 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
196200
return true;
197201
}
198202

199-
inline void beginRenderpass(IGPUCommandBuffer* cb, const IGPUCommandBuffer::SRenderpassBeginInfo& info)
203+
//
204+
virtual inline bool onAppTerminated()
200205
{
201-
cb->beginRenderPass(info,IGPUCommandBuffer::SUBPASS_CONTENTS::INLINE);
202-
cb->setScissor(0,1,&info.renderArea);
203-
const SViewport viewport = {
204-
.x = 0,
205-
.y = 0,
206-
.width = static_cast<float>(info.renderArea.extent.width),
207-
.height = static_cast<float>(info.renderArea.extent.height)
208-
};
209-
cb->setViewport(0u,1u,&viewport);
206+
SubAllocatedDescriptorSet::value_type fontAtlasDescIx = ext::imgui::UI::FontAtlasTexId;
207+
IGPUDescriptorSet::SDropDescriptorSet dummy[1];
208+
interface.subAllocDS->multi_deallocate(dummy,TexturesImGUIBindingIndex,1,&fontAtlasDescIx);
209+
return device_base_t::onAppTerminated();
210210
}
211211

212212
inline IQueue::SSubmitInfo::SSemaphoreInfo renderFrame(const std::chrono::microseconds nextPresentationTimestamp) override
@@ -226,14 +226,16 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
226226
cb->begin(IGPUCommandBuffer::USAGE::ONE_TIME_SUBMIT_BIT);
227227
// clear to black for both things
228228
const IGPUCommandBuffer::SClearColorValue clearValue = { .float32 = {0.f,0.f,0.f,1.f} };
229+
if (m_framebuffer)
229230
{
230231
cb->beginDebugMarker("UISampleApp Scene Frame");
231232
{
233+
const IGPUCommandBuffer::SClearDepthStencilValue farValue = { .depth=0.f };
232234
const IGPUCommandBuffer::SRenderpassBeginInfo renderpassInfo =
233235
{
234236
.framebuffer = m_framebuffer.get(),
235237
.colorClearValues = &clearValue,
236-
.depthStencilClearValues = nullptr,
238+
.depthStencilClearValues = &farValue,
237239
.renderArea = {
238240
.offset = {0,0},
239241
.extent = {virtualWindowRes[0],virtualWindowRes[1]}
@@ -254,7 +256,9 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
254256
const auto viewParams = CSimpleDebugRenderer::SViewParams(viewMatrix,viewProjMatrix);
255257

256258
// tear down scene every frame
257-
m_renderer->m_instances[0].packedGeo = m_renderer->getInitParams().geoms.data()+interface.gcIndex;
259+
auto& instance = m_renderer->m_instances[0];
260+
memcpy(&instance.world,&interface.model,sizeof(instance.world));
261+
instance.packedGeo = m_renderer->getInitParams().geoms.data()+interface.gcIndex;
258262
m_renderer->render(cb,viewParams);
259263
}
260264
cb->endRenderPass();
@@ -468,42 +472,65 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
468472
}});
469473
if (!m_device->allocate(image->getMemoryReqs(),image.get()).isValid())
470474
return nullptr;
471-
return m_device->createImageView({
475+
IGPUImageView::SCreationParams params = {
472476
.image = std::move(image),
473477
.viewType = IGPUImageView::ET_2D,
474-
.format = format,
475-
.subresourceRange = {
476-
.aspectMask = isDepthOrStencilFormat(format) ? IGPUImage::EAF_DEPTH_BIT:IGPUImage::EAF_COLOR_BIT,
477-
}
478-
});
478+
.format = format
479+
};
480+
params.subresourceRange.aspectMask = isDepthOrStencilFormat(format) ? IGPUImage::EAF_DEPTH_BIT:IGPUImage::EAF_COLOR_BIT;
481+
return m_device->createImageView(std::move(params));
479482
};
480-
481-
m_renderColorView = createImageAndView(finalSceneRenderFormat);
482-
auto depthView = createImageAndView(sceneRenderDepthFormat);
483-
m_framebuffer = m_device->createFramebuffer({ {
484-
.renderpass = m_renderpass,
485-
.depthStencilAttachments = &depthView.get(),
486-
.colorAttachments = &m_renderColorView.get(),
487-
.width = resolution.x,
488-
.height = resolution.y
489-
}});
483+
484+
smart_refctd_ptr<IGPUImageView> colorView;
485+
// detect window minimization
486+
if (resolution.x<0x4000 && resolution.y<0x4000)
487+
{
488+
colorView = createImageAndView(finalSceneRenderFormat);
489+
auto depthView = createImageAndView(sceneRenderDepthFormat);
490+
m_framebuffer = m_device->createFramebuffer({ {
491+
.renderpass = m_renderpass,
492+
.depthStencilAttachments = &depthView.get(),
493+
.colorAttachments = &colorView.get(),
494+
.width = resolution.x,
495+
.height = resolution.y
496+
}});
497+
}
498+
else
499+
m_framebuffer = nullptr;
490500

491501
// release previous slot and its image
492502
interface.subAllocDS->multi_deallocate(0,1,&interface.renderColorViewDescIndex,{.semaphore=m_semaphore.get(),.value=m_realFrameIx});
493503
//
494-
interface.subAllocDS->multi_allocate(0,1,&interface.renderColorViewDescIndex);
495-
// update descriptor set
496-
IGPUDescriptorSet::SDescriptorInfo info = {};
497-
info.desc = m_renderColorView;
498-
info.info.image.imageLayout = IGPUImage::LAYOUT::READ_ONLY_OPTIMAL;
499-
const IGPUDescriptorSet::SWriteDescriptorSet write = {
500-
.dstSet = interface.subAllocDS->getDescriptorSet(),
501-
.binding = TexturesImGUIBindingIndex,
502-
.arrayElement = interface.renderColorViewDescIndex,
503-
.count = 1,
504-
.info = &info
504+
if (colorView)
505+
{
506+
interface.subAllocDS->multi_allocate(0,1,&interface.renderColorViewDescIndex);
507+
// update descriptor set
508+
IGPUDescriptorSet::SDescriptorInfo info = {};
509+
info.desc = colorView;
510+
info.info.image.imageLayout = IGPUImage::LAYOUT::READ_ONLY_OPTIMAL;
511+
const IGPUDescriptorSet::SWriteDescriptorSet write = {
512+
.dstSet = interface.subAllocDS->getDescriptorSet(),
513+
.binding = TexturesImGUIBindingIndex,
514+
.arrayElement = interface.renderColorViewDescIndex,
515+
.count = 1,
516+
.info = &info
517+
};
518+
m_device->updateDescriptorSets({&write,1},{});
519+
}
520+
interface.transformParams.sceneTexDescIx = interface.renderColorViewDescIndex;
521+
}
522+
523+
inline void beginRenderpass(IGPUCommandBuffer* cb, const IGPUCommandBuffer::SRenderpassBeginInfo& info)
524+
{
525+
cb->beginRenderPass(info,IGPUCommandBuffer::SUBPASS_CONTENTS::INLINE);
526+
cb->setScissor(0,1,&info.renderArea);
527+
const SViewport viewport = {
528+
.x = 0,
529+
.y = 0,
530+
.width = static_cast<float>(info.renderArea.extent.width),
531+
.height = static_cast<float>(info.renderArea.extent.height)
505532
};
506-
m_device->updateDescriptorSets({&write,1},{});
533+
cb->setViewport(0u,1u,&viewport);
507534
}
508535

509536
// Maximum frames which can be simultaneously submitted, used to cycle through our per-frame resources like command buffers
@@ -518,7 +545,6 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
518545
smart_refctd_ptr<CGeometryCreatorScene> m_scene;
519546
smart_refctd_ptr<IGPURenderpass> m_renderpass;
520547
smart_refctd_ptr<CSimpleDebugRenderer> m_renderer;
521-
smart_refctd_ptr<IGPUImageView> m_renderColorView;
522548
smart_refctd_ptr<IGPUFramebuffer> m_framebuffer;
523549
//
524550
smart_refctd_ptr<ISemaphore> m_semaphore;
@@ -706,6 +732,7 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
706732
sceneResolution = EditTransform(imguizmoM16InOut.view.pointer(), imguizmoM16InOut.projection.pointer(), imguizmoM16InOut.model.pointer(), transformParams);
707733
}
708734

735+
model = core::transpose(imguizmoM16InOut.model).extractSub3x4();
709736
// to Nabla + update camera & model matrices
710737
// TODO: make it more nicely, extract:
711738
// - Position by computing inverse of the view matrix and grabbing its translation
@@ -835,8 +862,8 @@ class UISampleApp final : public MonoWindowApplication, public BuiltinResourcesA
835862
//
836863
Camera camera = Camera(core::vectorSIMDf(0, 0, 0), core::vectorSIMDf(0, 0, 0), core::matrix4SIMD());
837864
// mutables
838-
std::string_view objectName;
839865
core::matrix3x4SIMD model;
866+
std::string_view objectName;
840867
TransformRequestParams transformParams;
841868
uint16_t2 sceneResolution = {1280,720};
842869
float fov = 60.f, zNear = 0.1f, zFar = 10000.f, moveSpeed = 1.f, rotateSpeed = 1.f;

0 commit comments

Comments
 (0)