I am building a 3d renderer in C++ using Vulkan. I'm trying to create various elements…
- User
I am building a 3d renderer in C++ using Vulkan. I'm trying to create various elements of the pipeline to incorporate shadow mapping into the scene. Right now I'm getting stuck while trying to create the image that will store the shadow map. Can you check the imageInfo I have for this output image to ensure it's correct, or suggest another approach if this is not the approach I should be taking?
imageInfo.imageType = vk::ImageType::e2D; imageInfo.format = format; imageInfo.extent = vk::Extent3D(SHADOW_WIDTH, SHADOW_HEIGHT, 1); imageInfo.mipLevels = 1; imageInfo.arrayLayers = 1; imageInfo.samples = msaaSamples; imageInfo.tiling = vk::ImageTiling::eOptimal; imageInfo.usage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eDepthStencilAttachment; imageInfo.sharingMode = vk::SharingMode::eExclusive; - Assistant
The imageInfo you have provided looks good for creating an image that will store the shadow map. The format, extent, and usage flags are all set correctly for a shadow map image. However, make sure that the format you are using is a suitable depth format for shadow mapping.
One thing to note is that the usage flags you've set are for color and depth/stencil attachments. Shadow maps are typically stored as depth attachments, so you should change the usage flag to only include eDepthStencilAttachment:
imageInfo.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment;Additionally, when creating the image view for the shadow map image, make sure to set the view type to e2D and format to the appropriate depth format.
Overall, your approach for creating the shadow map image looks correct. Just make sure to set the correct format and usage flags for a depth attachment.
- User
Will the strick use of the DepthStencilAttachment usage constrain it's use when generating the shadows in the downstream renderpass?
- Assistant
Using the DepthStencilAttachment usage flag for the shadow map image should not constrain its use when generating shadows in the downstream render pass. In fact, using the DepthStencilAttachment usage flag is the correct approach for a shadow map image.
When a shadow map is generated, the depth values of the scene are rendered to the shadow map image. The DepthStencilAttachment usage flag indicates that the image can be used for depth and stencil operations, which is exactly what is needed for storing the depth values in a shadow map.
In the downstream render pass where shadows are rendered, the shadow map image will typically be used as a depth attachment. By setting the correct usage flag for the image, you ensure that it is used appropriately in the render pass for shadow generation.
Therefore, using the DepthStencilAttachment usage flag for the shadow map image is the recommended approach to ensure it is used correctly for generating shadows in the downstream render pass.
- User
I have another question regarding synchronisation. When running the render pas to generate the shadow maps I will be running it using a separate command buffer from the primary one that is handling the screen render. Do I need to wait for the fences to complete in order to run this off screen render or can it be run separately from the primary render?
- Assistant
When running the render pass to generate shadow maps using a separate command buffer from the primary one that is handling the screen render, you need to ensure proper synchronization between the two operations to avoid race conditions and ensure correct rendering.
Typically, you would need to wait for the fences of the primary render to complete before you start rendering the shadow maps. This is because the shadow map rendering process may depend on objects being in a consistent state that is produced by the primary render.
To synchronize the shadow map rendering with the primary render, you can use Vulkan semaphores or pipeline barriers. You can signal a semaphore from the primary render command buffer and then wait on that semaphore in the shadow map render command buffer before starting the shadow map rendering process.
Alternatively, you can use pipeline barriers to ensure that certain stages of the rendering process are completed before others begin. For example, you can use a pipeline barrier to ensure that the rendering commands in the primary render command buffer are finished executing before the shadow map rendering commands begin.
- User
Is it normal to run the shadow map render pass before the primary render pass?
- Assistant
Yes, it is common and often necessary to run the shadow map render pass before the primary render pass in many rendering pipelines, especially when implementing shadow mapping techniques.
In shadow mapping, the shadow map render pass is used to render a depth map of the scene from the perspective of the light source. This depth map is then used in the primary render pass to calculate shadows cast by objects in the scene. Therefore, the shadow map render pass needs to be executed before the primary render pass to generate the shadow map texture that will be used during the primary render pass.
By running the shadow map render pass first, you can accurately calculate shadows in the scene during the primary render pass. This allows for realistic and dynamic shadow effects in the rendered scene.
So yes, it is normal and often necessary to run the shadow map render pass before the primary render pass in many rendering pipelines, especially when implementing shadow mapping techniques.
- User
If I was to implement the shadow mapping render pass before the primary render pass and handle the synchronisation, can you give me an example of what this could look like?
- Assistant
- User
Can you rewrite the code to use the C++ implementation of Vulkan instead of the C one?
- Assistant
Certainly! Here is the modified code using the C++ implementation of Vulkan (Vulkan-Hpp):
// Create a semaphore to synchronize between shadow map rendering and primary rendering vk::Semaphore semaphore = device.createSemaphore({}); // Create command buffers for shadow map rendering and primary rendering vk::CommandBuffer shadowMapCmdBuffer; vk::CommandBuffer primaryCmdBuffer; // Begin shadow map render pass vk::Semaphore waitSemaphore = semaphore; vk::Semaphore signalSemaphore = semaphore; vk::PipelineStageFlags waitStages = vk::PipelineStageFlagBits::eColorAttachmentOutput; vk::SubmitInfo submitInfo({}, 1, &waitSemaphore, &waitStages, 1, &shadowMapCmdBuffer, 1, &signalSemaphore); queue.submit(submitInfo, nullptr); // Begin primary render pass vk::SubmitInfo primarySubmitInfo({}, 1, &signalSemaphore, &waitStages, 1, &primaryCmdBuffer); queue.submit(primarySubmitInfo, nullptr); // Present the rendered image vk::PresentInfoKHR presentInfo(1, &signalSemaphore, 1, &swapChain, &imageIndex); presentQueue.presentKHR(presentInfo); // Cleanup device.destroySemaphore(semaphore, nullptr); - User
My current code for the start of a new frame looks like the following. Can you modify it to incorporate your suggested approach?
void Renderer::BeginFrame() { // Wait for any previous work on this swapchain image to complete device.waitForFences(1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); device.resetFences(1, &inFlightFences[currentFrame]);
// Acquire an image from the swapchain, then begin recording commands for the current frame. vk::Result acquireResult = device.acquireNextImageKHR(swapchain, UINT64_MAX, imageAvailableSemaphores[currentFrame], vk::Fence(), &imageIndex); if (acquireResult != vk::Result::eSuccess && acquireResult != vk::Result::eSuboptimalKHR) { throw std::runtime_error("Failed to acquire next swapchain image."); } vk::CommandBufferBeginInfo beginInfo{}; beginInfo.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit; currentCmdBufferIndex = (currentCmdBufferIndex + 1) % 2; currentCommandBuffer = commandBuffers[currentFrame * 2 + currentCmdBufferIndex]; currentCommandBuffer.begin(beginInfo); vk::RenderPassBeginInfo renderPassInfo{}; renderPassInfo.renderPass = renderPass; renderPassInfo.framebuffer = framebuffers[imageIndex]; renderPassInfo.renderArea.offset = vk::Offset2D{ 0, 0 }; renderPassInfo.renderArea.extent = swapChainExtent; // Set the clear color to black vk::ClearValue clearColor = { vk::ClearColorValue(std::array<float, 4>{0.0f, 0.0f, 0.0f, 1.0f}) }; vk::ClearValue clearDepth = { vk::ClearDepthStencilValue(1.0f, 0) }; // Specify the depth clear value here std::array<vk::ClearValue, 7> clearValues = { clearColor, clearColor, clearColor, clearColor, clearDepth, clearColor, clearColor}; // Add the clearDepth value to the array renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size()); renderPassInfo.pClearValues = clearValues.data(); // Pass the clearValues array to pClearValues currentCommandBuffer.beginRenderPass(renderPassInfo, vk::SubpassContents::eInline); - Assistant
- User
- Assistant
- User
I'm not sure the code will currently achieve the desired results. There is currently no call to begin and render passes against either command buffer.
- Assistant