Skip to content

Commit e6b8583

Browse files
committed
It runs now, IMGUI is happier.
1 parent 6256f77 commit e6b8583

File tree

4 files changed

+144
-30
lines changed

4 files changed

+144
-30
lines changed

attachments/simple_engine/renderer.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ class Renderer {
258258
vk::raii::PipelineLayout lightingPipelineLayout = nullptr;
259259
vk::raii::Pipeline lightingPipeline = nullptr;
260260

261+
// Pipeline rendering create info structures (for proper lifetime management)
262+
vk::PipelineRenderingCreateInfo mainPipelineRenderingCreateInfo;
263+
vk::PipelineRenderingCreateInfo pbrPipelineRenderingCreateInfo;
264+
vk::PipelineRenderingCreateInfo lightingPipelineRenderingCreateInfo;
265+
261266
// Compute pipeline
262267
vk::raii::PipelineLayout computePipelineLayout = nullptr;
263268
vk::raii::Pipeline computePipeline = nullptr;
@@ -279,10 +284,9 @@ class Renderer {
279284
vk::raii::DeviceMemory depthImageMemory = nullptr;
280285
vk::raii::ImageView depthImageView = nullptr;
281286

282-
// Descriptor set layout and pool
287+
// Descriptor set layouts (declared before pools and sets)
283288
vk::raii::DescriptorSetLayout descriptorSetLayout = nullptr;
284289
vk::raii::DescriptorSetLayout pbrDescriptorSetLayout = nullptr;
285-
vk::raii::DescriptorPool descriptorPool = nullptr;
286290

287291
// Mesh resources
288292
struct MeshResources {
@@ -306,7 +310,7 @@ class Renderer {
306310
// Default texture resources (used when no texture is provided)
307311
TextureResources defaultTextureResources;
308312

309-
// Entity resources
313+
// Entity resources (contains descriptor sets - must be declared before descriptor pool)
310314
struct EntityResources {
311315
std::vector<vk::raii::Buffer> uniformBuffers;
312316
std::vector<vk::raii::DeviceMemory> uniformBuffersMemory;
@@ -315,6 +319,9 @@ class Renderer {
315319
};
316320
std::unordered_map<Entity*, EntityResources> entityResources;
317321

322+
// Descriptor pool (declared after entity resources to ensure proper destruction order)
323+
vk::raii::DescriptorPool descriptorPool = nullptr;
324+
318325
// Current frame index
319326
uint32_t currentFrame = 0;
320327

attachments/simple_engine/renderer_pipelines.cpp

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,24 @@ bool Renderer::createGraphicsPipeline() {
223223
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
224224

225225
// Create pipeline rendering info
226-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
226+
vk::Format depthFormat = findDepthFormat();
227+
std::cout << "Creating main graphics pipeline with depth format: " << static_cast<int>(depthFormat) << std::endl;
228+
229+
// Initialize member variable for proper lifetime management
230+
mainPipelineRenderingCreateInfo = vk::PipelineRenderingCreateInfo{
231+
.sType = vk::StructureType::ePipelineRenderingCreateInfo,
232+
.pNext = nullptr,
227233
.colorAttachmentCount = 1,
228234
.pColorAttachmentFormats = &swapChainImageFormat,
229-
.depthAttachmentFormat = findDepthFormat()
235+
.depthAttachmentFormat = depthFormat,
236+
.stencilAttachmentFormat = vk::Format::eUndefined
230237
};
231238

232239
// Create graphics pipeline
233240
vk::GraphicsPipelineCreateInfo pipelineInfo{
234-
.pNext = &pipelineRenderingCreateInfo,
241+
.sType = vk::StructureType::eGraphicsPipelineCreateInfo,
242+
.pNext = &mainPipelineRenderingCreateInfo,
243+
.flags = vk::PipelineCreateFlags{},
235244
.stageCount = 2,
236245
.pStages = shaderStages,
237246
.pVertexInputState = &vertexInputInfo,
@@ -242,7 +251,11 @@ bool Renderer::createGraphicsPipeline() {
242251
.pDepthStencilState = &depthStencil,
243252
.pColorBlendState = &colorBlending,
244253
.pDynamicState = &dynamicState,
245-
.layout = *pipelineLayout
254+
.layout = *pipelineLayout,
255+
.renderPass = nullptr,
256+
.subpass = 0,
257+
.basePipelineHandle = nullptr,
258+
.basePipelineIndex = -1
246259
};
247260

248261
graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo);
@@ -411,15 +424,23 @@ bool Renderer::createPBRPipeline() {
411424
pbrPipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
412425

413426
// Create pipeline rendering info
414-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
427+
vk::Format depthFormat = findDepthFormat();
428+
429+
// Initialize member variable for proper lifetime management
430+
pbrPipelineRenderingCreateInfo = vk::PipelineRenderingCreateInfo{
431+
.sType = vk::StructureType::ePipelineRenderingCreateInfo,
432+
.pNext = nullptr,
415433
.colorAttachmentCount = 1,
416434
.pColorAttachmentFormats = &swapChainImageFormat,
417-
.depthAttachmentFormat = findDepthFormat()
435+
.depthAttachmentFormat = depthFormat,
436+
.stencilAttachmentFormat = vk::Format::eUndefined
418437
};
419438

420439
// Create graphics pipeline
421440
vk::GraphicsPipelineCreateInfo pipelineInfo{
422-
.pNext = &pipelineRenderingCreateInfo,
441+
.sType = vk::StructureType::eGraphicsPipelineCreateInfo,
442+
.pNext = &pbrPipelineRenderingCreateInfo,
443+
.flags = vk::PipelineCreateFlags{},
423444
.stageCount = 2,
424445
.pStages = shaderStages,
425446
.pVertexInputState = &vertexInputInfo,
@@ -430,7 +451,11 @@ bool Renderer::createPBRPipeline() {
430451
.pDepthStencilState = &depthStencil,
431452
.pColorBlendState = &colorBlending,
432453
.pDynamicState = &dynamicState,
433-
.layout = *pbrPipelineLayout
454+
.layout = *pbrPipelineLayout,
455+
.renderPass = nullptr,
456+
.subpass = 0,
457+
.basePipelineHandle = nullptr,
458+
.basePipelineIndex = -1
434459
};
435460

436461
pbrGraphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo);
@@ -565,15 +590,23 @@ bool Renderer::createLightingPipeline() {
565590
lightingPipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
566591

567592
// Create pipeline rendering info
568-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
593+
vk::Format depthFormat = findDepthFormat();
594+
595+
// Initialize member variable for proper lifetime management
596+
lightingPipelineRenderingCreateInfo = vk::PipelineRenderingCreateInfo{
597+
.sType = vk::StructureType::ePipelineRenderingCreateInfo,
598+
.pNext = nullptr,
569599
.colorAttachmentCount = 1,
570600
.pColorAttachmentFormats = &swapChainImageFormat,
571-
.depthAttachmentFormat = findDepthFormat()
601+
.depthAttachmentFormat = depthFormat,
602+
.stencilAttachmentFormat = vk::Format::eUndefined
572603
};
573604

574605
// Create graphics pipeline
575606
vk::GraphicsPipelineCreateInfo pipelineInfo{
576-
.pNext = &pipelineRenderingCreateInfo,
607+
.sType = vk::StructureType::eGraphicsPipelineCreateInfo,
608+
.pNext = &lightingPipelineRenderingCreateInfo,
609+
.flags = vk::PipelineCreateFlags{},
577610
.stageCount = 2,
578611
.pStages = shaderStages,
579612
.pVertexInputState = &vertexInputInfo,
@@ -584,7 +617,11 @@ bool Renderer::createLightingPipeline() {
584617
.pDepthStencilState = &depthStencil,
585618
.pColorBlendState = &colorBlending,
586619
.pDynamicState = &dynamicState,
587-
.layout = *lightingPipelineLayout
620+
.layout = *lightingPipelineLayout,
621+
.renderPass = nullptr,
622+
.subpass = 0,
623+
.basePipelineHandle = nullptr,
624+
.basePipelineIndex = -1
588625
};
589626

590627
lightingPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo);

attachments/simple_engine/renderer_rendering.cpp

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ bool Renderer::createSyncObjects() {
204204
renderFinishedSemaphores.clear();
205205
inFlightFences.clear();
206206

207-
imageAvailableSemaphores.reserve(MAX_FRAMES_IN_FLIGHT);
208-
renderFinishedSemaphores.reserve(MAX_FRAMES_IN_FLIGHT);
207+
// Create semaphores per swapchain image to avoid reuse issues
208+
size_t swapchainImageCount = swapChainImages.size();
209+
imageAvailableSemaphores.reserve(swapchainImageCount);
210+
renderFinishedSemaphores.reserve(swapchainImageCount);
211+
212+
// Keep fences per frame in flight for CPU-GPU synchronization
209213
inFlightFences.reserve(MAX_FRAMES_IN_FLIGHT);
210214

211215
// Create semaphore and fence info
@@ -214,10 +218,14 @@ bool Renderer::createSyncObjects() {
214218
.flags = vk::FenceCreateFlagBits::eSignaled
215219
};
216220

217-
// Create semaphores and fences for each frame in flight
218-
for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
221+
// Create semaphores for each swapchain image
222+
for (size_t i = 0; i < swapchainImageCount; i++) {
219223
imageAvailableSemaphores.emplace_back(device, semaphoreInfo);
220224
renderFinishedSemaphores.emplace_back(device, semaphoreInfo);
225+
}
226+
227+
// Create fences for each frame in flight
228+
for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
221229
inFlightFences.emplace_back(device, fenceInfo);
222230
}
223231

@@ -298,7 +306,9 @@ void Renderer::Render(const std::vector<Entity*>& entities, CameraComponent* cam
298306

299307
// Acquire the next image from the swap chain
300308
uint32_t imageIndex;
301-
auto result = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[currentFrame]);
309+
// Use currentFrame for semaphore indexing to ensure consistency
310+
uint32_t semaphoreIndex = currentFrame % imageAvailableSemaphores.size();
311+
auto result = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[semaphoreIndex]);
302312
imageIndex = result.second;
303313

304314
// Check if the swap chain needs to be recreated
@@ -326,6 +336,33 @@ void Renderer::Render(const std::vector<Entity*>& entities, CameraComponent* cam
326336
// Update rendering area
327337
renderingInfo.setRenderArea(vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
328338

339+
// Transition swapchain image layout for rendering
340+
vk::ImageMemoryBarrier renderBarrier{
341+
.srcAccessMask = vk::AccessFlagBits::eNone,
342+
.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
343+
.oldLayout = vk::ImageLayout::eUndefined,
344+
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
345+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
346+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
347+
.image = swapChainImages[imageIndex],
348+
.subresourceRange = {
349+
.aspectMask = vk::ImageAspectFlagBits::eColor,
350+
.baseMipLevel = 0,
351+
.levelCount = 1,
352+
.baseArrayLayer = 0,
353+
.layerCount = 1
354+
}
355+
};
356+
357+
commandBuffers[currentFrame].pipelineBarrier(
358+
vk::PipelineStageFlagBits::eTopOfPipe,
359+
vk::PipelineStageFlagBits::eColorAttachmentOutput,
360+
vk::DependencyFlags{},
361+
{},
362+
{},
363+
renderBarrier
364+
);
365+
329366
// Begin dynamic rendering with vk::raii
330367
commandBuffers[currentFrame].beginRendering(renderingInfo);
331368

@@ -409,27 +446,54 @@ void Renderer::Render(const std::vector<Entity*>& entities, CameraComponent* cam
409446
// End dynamic rendering
410447
commandBuffers[currentFrame].endRendering();
411448

449+
// Transition swapchain image layout for presentation
450+
vk::ImageMemoryBarrier imageBarrier{
451+
.srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
452+
.dstAccessMask = vk::AccessFlagBits::eNone,
453+
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
454+
.newLayout = vk::ImageLayout::ePresentSrcKHR,
455+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
456+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
457+
.image = swapChainImages[imageIndex],
458+
.subresourceRange = {
459+
.aspectMask = vk::ImageAspectFlagBits::eColor,
460+
.baseMipLevel = 0,
461+
.levelCount = 1,
462+
.baseArrayLayer = 0,
463+
.layerCount = 1
464+
}
465+
};
466+
467+
commandBuffers[currentFrame].pipelineBarrier(
468+
vk::PipelineStageFlagBits::eColorAttachmentOutput,
469+
vk::PipelineStageFlagBits::eBottomOfPipe,
470+
vk::DependencyFlags{},
471+
{},
472+
{},
473+
imageBarrier
474+
);
475+
412476
// End command buffer
413477
commandBuffers[currentFrame].end();
414478

415479
// Submit command buffer
416480
vk::PipelineStageFlags waitStages[] = {vk::PipelineStageFlagBits::eColorAttachmentOutput};
417481
vk::SubmitInfo submitInfo{
418482
.waitSemaphoreCount = 1,
419-
.pWaitSemaphores = &*imageAvailableSemaphores[currentFrame],
483+
.pWaitSemaphores = &*imageAvailableSemaphores[semaphoreIndex],
420484
.pWaitDstStageMask = waitStages,
421485
.commandBufferCount = 1,
422486
.pCommandBuffers = &*commandBuffers[currentFrame],
423487
.signalSemaphoreCount = 1,
424-
.pSignalSemaphores = &*renderFinishedSemaphores[currentFrame]
488+
.pSignalSemaphores = &*renderFinishedSemaphores[semaphoreIndex]
425489
};
426490

427491
graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]);
428492

429493
// Present the image
430494
vk::PresentInfoKHR presentInfo{
431495
.waitSemaphoreCount = 1,
432-
.pWaitSemaphores = &*renderFinishedSemaphores[currentFrame],
496+
.pWaitSemaphores = &*renderFinishedSemaphores[semaphoreIndex],
433497
.swapchainCount = 1,
434498
.pSwapchains = &*swapChain,
435499
.pImageIndices = &imageIndex

attachments/simple_engine/renderer_utils.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ vk::Format Renderer::findSupportedFormat(const std::vector<vk::Format>& candidat
4949

5050
// Find depth format
5151
vk::Format Renderer::findDepthFormat() {
52-
vk::Format depthFormat = findSupportedFormat(
53-
{vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint},
54-
vk::ImageTiling::eOptimal,
55-
vk::FormatFeatureFlagBits::eDepthStencilAttachment
56-
);
57-
std::cout << "Found depth format: " << static_cast<int>(depthFormat) << std::endl;
58-
return depthFormat;
52+
try {
53+
vk::Format depthFormat = findSupportedFormat(
54+
{vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint},
55+
vk::ImageTiling::eOptimal,
56+
vk::FormatFeatureFlagBits::eDepthStencilAttachment
57+
);
58+
std::cout << "Found depth format: " << static_cast<int>(depthFormat) << std::endl;
59+
return depthFormat;
60+
} catch (const std::exception& e) {
61+
std::cerr << "Failed to find supported depth format, falling back to D32_SFLOAT: " << e.what() << std::endl;
62+
// Fallback to D32_SFLOAT which is widely supported
63+
return vk::Format::eD32Sfloat;
64+
}
5965
}
6066

6167
// Check if format has stencil component

0 commit comments

Comments
 (0)