From c8b83b6e0b59ca4b8b061d2205e41839252b41ef Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Sat, 23 Aug 2025 12:34:57 +0200 Subject: [PATCH 1/2] Fix non-working depth buffer --- attachments/35_gltf_ktx.cpp | 43 +++++++++++++++++++- attachments/36_multiple_objects.cpp | 62 +++++++++++++++++++++++------ 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/attachments/35_gltf_ktx.cpp b/attachments/35_gltf_ktx.cpp index b8db78b4..d733c682 100644 --- a/attachments/35_gltf_ktx.cpp +++ b/attachments/35_gltf_ktx.cpp @@ -673,7 +673,12 @@ class VulkanApplication { pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo); - vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainSurfaceFormat.format }; + vk::Format depthFormat = findDepthFormat(); + vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ + .colorAttachmentCount = 1, + .pColorAttachmentFormats = &swapChainSurfaceFormat.format, + .depthAttachmentFormat = depthFormat + }; vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo, .stageCount = 2, .pStages = shaderStages, @@ -1183,6 +1188,31 @@ class VulkanApplication { vk::PipelineStageFlagBits2::eTopOfPipe, vk::PipelineStageFlagBits2::eColorAttachmentOutput ); + vk::ImageMemoryBarrier2 depthBarrier = { + .srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe, + .srcAccessMask = {}, + .dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests, + .dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite, + .oldLayout = vk::ImageLayout::eUndefined, + .newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = depthImage, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eDepth, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1 + } + }; + vk::DependencyInfo depthDependencyInfo = { + .dependencyFlags = {}, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &depthBarrier + }; + commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo); + vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f); vk::RenderingAttachmentInfo attachmentInfo = { .imageView = *swapChainImageViews[imageIndex], @@ -1191,11 +1221,20 @@ class VulkanApplication { .storeOp = vk::AttachmentStoreOp::eStore, .clearValue = clearColor }; + vk::ClearValue clearDepth = vk::ClearDepthStencilValue{ 1.0f, 0 }; + vk::RenderingAttachmentInfo depthAttachmentInfo{ + .imageView = *depthImageView, + .imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, + .loadOp = vk::AttachmentLoadOp::eClear, + .storeOp = vk::AttachmentStoreOp::eDontCare, + .clearValue = clearDepth + }; vk::RenderingInfo renderingInfo = { .renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent }, .layerCount = 1, .colorAttachmentCount = 1, - .pColorAttachments = &attachmentInfo + .pColorAttachments = &attachmentInfo, + .pDepthAttachment = &depthAttachmentInfo }; commandBuffers[currentFrame].beginRendering(renderingInfo); commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline); diff --git a/attachments/36_multiple_objects.cpp b/attachments/36_multiple_objects.cpp index d23221cc..aa200ec1 100644 --- a/attachments/36_multiple_objects.cpp +++ b/attachments/36_multiple_objects.cpp @@ -700,8 +700,8 @@ class VulkanApplication { .depthClampEnable = vk::False, .rasterizerDiscardEnable = vk::False, .polygonMode = vk::PolygonMode::eFill, - .cullMode = vk::CullModeFlagBits::eBack, // Re-enabled culling for better performance - .frontFace = vk::FrontFace::eClockwise, // Keeping Clockwise for glTF + .cullMode = vk::CullModeFlagBits::eBack, + .frontFace = vk::FrontFace::eCounterClockwise, .depthBiasEnable = vk::False }; rasterizer.lineWidth = 1.0f; @@ -736,8 +736,13 @@ class VulkanApplication { vk::PipelineLayoutCreateInfo pipelineLayoutInfo{ .setLayoutCount = 1, .pSetLayouts = &*descriptorSetLayout, .pushConstantRangeCount = 0 }; pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo); - - vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainSurfaceFormat.format }; + + vk::Format depthFormat = findDepthFormat(); + vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ + .colorAttachmentCount = 1, + .pColorAttachmentFormats = &swapChainSurfaceFormat.format, + .depthAttachmentFormat = depthFormat + }; vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo, .stageCount = 2, .pStages = shaderStages, @@ -1011,10 +1016,7 @@ class VulkanApplication { Vertex vertex{}; const float* pos = reinterpret_cast(&posBuffer.data[posBufferView.byteOffset + posAccessor.byteOffset + i * 12]); - // glTF uses a right-handed coordinate system with Y-up - // Vulkan uses a right-handed coordinate system with Y-down - // We need to flip the Y coordinate - vertex.pos = {pos[0], -pos[1], pos[2]}; + vertex.pos = {pos[0], pos[1], pos[2]}; if (hasTexCoords) { const float* texCoord = reinterpret_cast(&texCoordBuffer->data[texCoordBufferView->byteOffset + texCoordAccessor->byteOffset + i * 8]); @@ -1097,17 +1099,17 @@ class VulkanApplication { void setupGameObjects() { // Object 1 - Center gameObjects[0].position = {0.0f, 0.0f, 0.0f}; - gameObjects[0].rotation = {0.0f, 0.0f, 0.0f}; + gameObjects[0].rotation = {0.0f, glm::radians(-90.0f), 0.0f}; gameObjects[0].scale = {1.0f, 1.0f, 1.0f}; // Object 2 - Left gameObjects[1].position = {-2.0f, 0.0f, -1.0f}; - gameObjects[1].rotation = {0.0f, glm::radians(45.0f), 0.0f}; + gameObjects[1].rotation = {0.0f, glm::radians(-45.0f), 0.0f}; gameObjects[1].scale = {0.75f, 0.75f, 0.75f}; // Object 3 - Right gameObjects[2].position = {2.0f, 0.0f, -1.0f}; - gameObjects[2].rotation = {0.0f, glm::radians(-45.0f), 0.0f}; + gameObjects[2].rotation = {0.0f, glm::radians(45.0f), 0.0f}; gameObjects[2].scale = {0.75f, 0.75f, 0.75f}; } @@ -1275,6 +1277,31 @@ class VulkanApplication { vk::PipelineStageFlagBits2::eTopOfPipe, vk::PipelineStageFlagBits2::eColorAttachmentOutput ); + vk::ImageMemoryBarrier2 depthBarrier = { + .srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe, + .srcAccessMask = {}, + .dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests, + .dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite, + .oldLayout = vk::ImageLayout::eUndefined, + .newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = depthImage, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eDepth, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1 + } + }; + vk::DependencyInfo depthDependencyInfo = { + .dependencyFlags = {}, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &depthBarrier + }; + commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo); + vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f); vk::RenderingAttachmentInfo attachmentInfo = { .imageView = *swapChainImageViews[imageIndex], @@ -1283,11 +1310,20 @@ class VulkanApplication { .storeOp = vk::AttachmentStoreOp::eStore, .clearValue = clearColor }; + vk::ClearValue clearDepth = vk::ClearDepthStencilValue{ 1.0f, 0 }; + vk::RenderingAttachmentInfo depthAttachmentInfo{ + .imageView = *depthImageView, + .imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, + .loadOp = vk::AttachmentLoadOp::eClear, + .storeOp = vk::AttachmentStoreOp::eDontCare, + .clearValue = clearDepth + }; vk::RenderingInfo renderingInfo = { .renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent }, .layerCount = 1, .colorAttachmentCount = 1, - .pColorAttachments = &attachmentInfo + .pColorAttachments = &attachmentInfo, + .pDepthAttachment = &depthAttachmentInfo }; commandBuffers[currentFrame].beginRendering(renderingInfo); commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline); @@ -1387,6 +1423,8 @@ class VulkanApplication { // Camera and projection matrices (shared by all objects) glm::mat4 view = glm::lookAt(glm::vec3(2.0f, 2.0f, 6.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 proj = glm::perspective(glm::radians(45.0f), static_cast(swapChainExtent.width) / static_cast(swapChainExtent.height), 0.1f, 20.0f); + proj[1][1] *= -1; + // Update uniform buffers for each object for (auto& gameObject : gameObjects) { // Apply continuous rotation to the object based on frame time From 00d96886fa8d19bb8af3211187613c9840d61f5e Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Sat, 23 Aug 2025 14:45:30 +0200 Subject: [PATCH 2/2] Trying to fix things on android --- attachments/35_gltf_ktx.cpp | 2 +- attachments/36_multiple_objects.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/attachments/35_gltf_ktx.cpp b/attachments/35_gltf_ktx.cpp index d733c682..d452ae0e 100644 --- a/attachments/35_gltf_ktx.cpp +++ b/attachments/35_gltf_ktx.cpp @@ -1197,7 +1197,7 @@ class VulkanApplication { .newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = depthImage, + .image = *depthImage, .subresourceRange = { .aspectMask = vk::ImageAspectFlagBits::eDepth, .baseMipLevel = 0, diff --git a/attachments/36_multiple_objects.cpp b/attachments/36_multiple_objects.cpp index aa200ec1..e2a37731 100644 --- a/attachments/36_multiple_objects.cpp +++ b/attachments/36_multiple_objects.cpp @@ -1286,7 +1286,7 @@ class VulkanApplication { .newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = depthImage, + .image = *depthImage, .subresourceRange = { .aspectMask = vk::ImageAspectFlagBits::eDepth, .baseMipLevel = 0,