Depth buffer added.
This commit is contained in:
parent
ef6ca6a79e
commit
07f3a237b5
|
|
@ -50,3 +50,36 @@ Texture::Init(const Device *device, const vk::Extent2D extent, const bool isMipm
|
||||||
|
|
||||||
device->SetName(m_Image, name);
|
device->SetName(m_Image, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DepthImage::Init(const Device *device, vk::Extent2D extent, cstr name)
|
||||||
|
{
|
||||||
|
vk::ImageCreateInfo imageCreateInfo = {
|
||||||
|
.imageType = vk::ImageType::e2D,
|
||||||
|
.format = vk::Format::eD32Sfloat,
|
||||||
|
.extent = {.width = extent.width, .height = extent.height, .depth = 1},
|
||||||
|
.mipLevels = 1,
|
||||||
|
.arrayLayers = 1,
|
||||||
|
.samples = vk::SampleCountFlagBits::e1,
|
||||||
|
.tiling = vk::ImageTiling::eOptimal,
|
||||||
|
.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment,
|
||||||
|
.sharingMode = vk::SharingMode::eExclusive,
|
||||||
|
.initialLayout = vk::ImageLayout::eUndefined,
|
||||||
|
};
|
||||||
|
constexpr VmaAllocationCreateInfo allocationCreateInfo = {
|
||||||
|
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
|
||||||
|
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkImage image;
|
||||||
|
VmaAllocation allocation;
|
||||||
|
auto result = Cast<vk::Result>(vmaCreateImage(device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
|
||||||
|
&allocationCreateInfo, &image, &allocation, nullptr));
|
||||||
|
ERROR_IF(Failed(result), "Could not allocate buffer. Cause: {}", result) THEN_ABORT(result);
|
||||||
|
|
||||||
|
m_Image = image;
|
||||||
|
m_Allocation = allocation;
|
||||||
|
m_Extent = {extent.width, extent.height, 1};
|
||||||
|
|
||||||
|
device->SetName(m_Image, name);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,3 +23,8 @@ struct Texture : Image
|
||||||
{
|
{
|
||||||
void Init(const Device *device, vk::Extent2D extent, bool isMipmapped, cstr name = nullptr);
|
void Init(const Device *device, vk::Extent2D extent, bool isMipmapped, cstr name = nullptr);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct DepthImage : Image
|
||||||
|
{
|
||||||
|
void Init(const Device *device, vk::Extent2D extent, cstr name = nullptr);
|
||||||
|
};
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
Swapchain::Swapchain(const Window *window, const Device *device, NameString &&name)
|
Swapchain::Swapchain(const Window *window, const Device *device, NameString &&name)
|
||||||
: m_Device(device)
|
: m_Device(device)
|
||||||
, m_Name(std::move(name))
|
, m_Name(std::move(name))
|
||||||
|
, m_Format(vk::Format::eUndefined)
|
||||||
{
|
{
|
||||||
this->Create(window);
|
this->Create(window);
|
||||||
}
|
}
|
||||||
|
|
@ -176,6 +177,17 @@ Swapchain::Create(const Window *window)
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG("Swapchain {} Image Views created.", m_Name);
|
DEBUG("Swapchain {} Image Views created.", m_Name);
|
||||||
|
|
||||||
|
for (auto &callback : m_ResizeCallbacks)
|
||||||
|
{
|
||||||
|
callback(m_Extent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Swapchain::RegisterResizeCallback(FnResizeCallback &&callback)
|
||||||
|
{
|
||||||
|
m_ResizeCallbacks.emplace_back(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ struct Device;
|
||||||
|
|
||||||
struct Swapchain final
|
struct Swapchain final
|
||||||
{
|
{
|
||||||
|
using FnResizeCallback = eastl::function<void(vk::Extent2D)>;
|
||||||
|
|
||||||
const Device *m_Device;
|
const Device *m_Device;
|
||||||
vk::SwapchainKHR m_Swapchain;
|
vk::SwapchainKHR m_Swapchain;
|
||||||
NameString m_Name;
|
NameString m_Name;
|
||||||
|
|
@ -23,7 +25,10 @@ struct Swapchain final
|
||||||
eastl::fixed_vector<vk::Image, 4> m_Images;
|
eastl::fixed_vector<vk::Image, 4> m_Images;
|
||||||
eastl::fixed_vector<vk::ImageView, 4> m_ImageViews;
|
eastl::fixed_vector<vk::ImageView, 4> m_ImageViews;
|
||||||
|
|
||||||
|
eastl::vector<FnResizeCallback> m_ResizeCallbacks;
|
||||||
|
|
||||||
void Create(const Window *window);
|
void Create(const Window *window);
|
||||||
|
void RegisterResizeCallback(FnResizeCallback &&callback);
|
||||||
|
|
||||||
// Ctor/Dtor
|
// Ctor/Dtor
|
||||||
Swapchain(const Window *window, const Device *device, NameString &&name);
|
Swapchain(const Window *window, const Device *device, NameString &&name);
|
||||||
|
|
|
||||||
|
|
@ -420,6 +420,14 @@ main(int, char **)
|
||||||
.extent = swapchain.m_Extent,
|
.extent = swapchain.m_Extent,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
auto ResizeViewportScissor = [&viewport, &scissor](vk::Extent2D extent) {
|
||||||
|
viewport.y = Cast<f32>(extent.height);
|
||||||
|
viewport.width = Cast<f32>(extent.width);
|
||||||
|
viewport.height = -Cast<f32>(extent.height);
|
||||||
|
scissor.extent = extent;
|
||||||
|
};
|
||||||
|
swapchain.RegisterResizeCallback(ResizeViewportScissor);
|
||||||
|
|
||||||
vk::ImageSubresourceRange subresourceRange = {
|
vk::ImageSubresourceRange subresourceRange = {
|
||||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||||
.baseMipLevel = 0,
|
.baseMipLevel = 0,
|
||||||
|
|
@ -442,14 +450,57 @@ main(int, char **)
|
||||||
.subresourceRange = subresourceRange,
|
.subresourceRange = subresourceRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Frames
|
FrameManager frameManager = {&device, queueAllocation.m_Family, MAX_FRAMES_IN_FLIGHT};
|
||||||
eastl::fixed_vector<Frame, MAX_FRAMES_IN_FLIGHT> frames;
|
eastl::fixed_vector<DepthImage, MAX_FRAMES_IN_FLIGHT> depthImages(frameManager.m_FramesInFlight);
|
||||||
for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i)
|
eastl::fixed_vector<vk::ImageView, MAX_FRAMES_IN_FLIGHT> depthViews(frameManager.m_FramesInFlight);
|
||||||
|
|
||||||
|
vk::ImageSubresourceRange depthSubresourceRange = {
|
||||||
|
.aspectMask = vk::ImageAspectFlagBits::eDepth,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
u32 index = 0;
|
||||||
|
for (auto &depthImage : depthImages)
|
||||||
{
|
{
|
||||||
frames.emplace_back(&device, queueAllocation.m_Family, i);
|
auto name = fmt::format("Depth image {}", index);
|
||||||
|
depthImage.Init(&device, swapchain.m_Extent, name.c_str());
|
||||||
|
vk::ImageViewCreateInfo imageViewCreateInfo = {
|
||||||
|
.image = depthImage.m_Image,
|
||||||
|
.viewType = vk::ImageViewType::e2D,
|
||||||
|
.format = vk::Format::eD32Sfloat,
|
||||||
|
.components = vk::ComponentMapping{.r = vk::ComponentSwizzle::eIdentity},
|
||||||
|
.subresourceRange = depthSubresourceRange,
|
||||||
|
};
|
||||||
|
AbortIfFailed(device.m_Device.createImageView(&imageViewCreateInfo, nullptr, &depthViews[index]));
|
||||||
|
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameManager frameManager = {&device, queueAllocation.m_Family, MAX_FRAMES_IN_FLIGHT};
|
auto RecreateDepthBuffers = [&device, &depthImages, &depthViews, depthSubresourceRange](vk::Extent2D extent) {
|
||||||
|
for (auto &depthView : depthViews)
|
||||||
|
{
|
||||||
|
device.m_Device.destroy(depthView, nullptr);
|
||||||
|
}
|
||||||
|
u32 index = 0;
|
||||||
|
for (auto &depthImage : depthImages)
|
||||||
|
{
|
||||||
|
depthImage.Destroy(&device);
|
||||||
|
depthImage.Init(&device, extent, "Depth");
|
||||||
|
vk::ImageViewCreateInfo imageViewCreateInfo = {
|
||||||
|
.image = depthImage.m_Image,
|
||||||
|
.viewType = vk::ImageViewType::e2D,
|
||||||
|
.format = vk::Format::eD32Sfloat,
|
||||||
|
.components = vk::ComponentMapping{.r = vk::ComponentSwizzle::eIdentity},
|
||||||
|
.subresourceRange = depthSubresourceRange,
|
||||||
|
};
|
||||||
|
AbortIfFailed(device.m_Device.createImageView(&imageViewCreateInfo, nullptr, &depthViews[index]));
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
swapchain.RegisterResizeCallback(RecreateDepthBuffers);
|
||||||
|
|
||||||
Time::Init();
|
Time::Init();
|
||||||
|
|
||||||
|
|
@ -462,16 +513,12 @@ main(int, char **)
|
||||||
ubo.Write(&device, 0, sizeof camera, &camera);
|
ubo.Write(&device, 0, sizeof camera, &camera);
|
||||||
|
|
||||||
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &window);
|
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &window);
|
||||||
viewport.y = Cast<f32>(swapchain.m_Extent.height);
|
|
||||||
viewport.width = Cast<f32>(swapchain.m_Extent.width);
|
|
||||||
viewport.height = -Cast<f32>(swapchain.m_Extent.height);
|
|
||||||
scissor.extent = swapchain.m_Extent;
|
|
||||||
|
|
||||||
// Frame *currentFrame = &frames[frameIndex];
|
|
||||||
u32 imageIndex = currentFrame->m_ImageIdx;
|
u32 imageIndex = currentFrame->m_ImageIdx;
|
||||||
vk::ImageView currentImageView = swapchain.m_ImageViews[imageIndex];
|
vk::ImageView currentImageView = swapchain.m_ImageViews[imageIndex];
|
||||||
vk::Image currentImage = swapchain.m_Images[imageIndex];
|
vk::Image currentImage = swapchain.m_Images[imageIndex];
|
||||||
vk::CommandBuffer cmd = currentFrame->m_CommandBuffer;
|
vk::CommandBuffer cmd = currentFrame->m_CommandBuffer;
|
||||||
|
vk::ImageView currentDepthImageView = depthViews[currentFrame->m_FrameIdx];
|
||||||
|
|
||||||
topOfThePipeBarrier.image = currentImage;
|
topOfThePipeBarrier.image = currentImage;
|
||||||
renderToPresentBarrier.image = currentImage;
|
renderToPresentBarrier.image = currentImage;
|
||||||
|
|
@ -483,20 +530,32 @@ main(int, char **)
|
||||||
{}, 0, nullptr, 0, nullptr, 1, &topOfThePipeBarrier);
|
{}, 0, nullptr, 0, nullptr, 1, &topOfThePipeBarrier);
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
vk::RenderingAttachmentInfo attachmentInfo = {
|
eastl::array attachmentInfos = {
|
||||||
|
vk::RenderingAttachmentInfo{
|
||||||
.imageView = currentImageView,
|
.imageView = currentImageView,
|
||||||
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||||
.resolveMode = vk::ResolveModeFlagBits::eNone,
|
.resolveMode = vk::ResolveModeFlagBits::eNone,
|
||||||
.loadOp = vk::AttachmentLoadOp::eClear,
|
.loadOp = vk::AttachmentLoadOp::eClear,
|
||||||
.storeOp = vk::AttachmentStoreOp::eStore,
|
.storeOp = vk::AttachmentStoreOp::eStore,
|
||||||
.clearValue = vk::ClearColorValue{0.0f, 0.0f, 0.0f, 1.0f},
|
.clearValue = vk::ClearColorValue{0.0f, 0.0f, 0.0f, 1.0f},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
vk::RenderingAttachmentInfo depthAttachment = {
|
||||||
|
.imageView = currentDepthImageView,
|
||||||
|
.imageLayout = vk::ImageLayout::eDepthAttachmentOptimal,
|
||||||
|
.resolveMode = vk::ResolveModeFlagBits::eNone,
|
||||||
|
.loadOp = vk::AttachmentLoadOp::eClear,
|
||||||
|
.storeOp = vk::AttachmentStoreOp::eDontCare,
|
||||||
|
.clearValue = vk::ClearDepthStencilValue{.depth = 1.0f, .stencil = 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
vk::RenderingInfo renderingInfo = {
|
vk::RenderingInfo renderingInfo = {
|
||||||
.renderArea = {.extent = swapchain.m_Extent},
|
.renderArea = {.extent = swapchain.m_Extent},
|
||||||
.layerCount = 1,
|
.layerCount = 1,
|
||||||
.colorAttachmentCount = 1,
|
.colorAttachmentCount = Cast<u32>(attachmentInfos.size()),
|
||||||
.pColorAttachments = &attachmentInfo,
|
.pColorAttachments = attachmentInfos.data(),
|
||||||
|
.pDepthAttachment = &depthAttachment,
|
||||||
};
|
};
|
||||||
|
|
||||||
cmd.beginRendering(&renderingInfo);
|
cmd.beginRendering(&renderingInfo);
|
||||||
|
|
@ -533,6 +592,14 @@ main(int, char **)
|
||||||
|
|
||||||
AbortIfFailed(device.m_Device.waitIdle());
|
AbortIfFailed(device.m_Device.waitIdle());
|
||||||
|
|
||||||
|
for (auto &depthView : depthViews)
|
||||||
|
{
|
||||||
|
device.m_Device.destroy(depthView, nullptr);
|
||||||
|
}
|
||||||
|
for (auto &depthImage : depthImages)
|
||||||
|
{
|
||||||
|
depthImage.Destroy(&device);
|
||||||
|
}
|
||||||
device.m_Device.destroy(sampler, nullptr);
|
device.m_Device.destroy(sampler, nullptr);
|
||||||
device.m_Device.destroy(imageView, nullptr);
|
device.m_Device.destroy(imageView, nullptr);
|
||||||
ubo.Destroy(&device);
|
ubo.Destroy(&device);
|
||||||
|
|
@ -619,7 +686,7 @@ CreatePipeline(const Device *device, const Swapchain *swapchain)
|
||||||
.depthClampEnable = false,
|
.depthClampEnable = false,
|
||||||
.rasterizerDiscardEnable = false,
|
.rasterizerDiscardEnable = false,
|
||||||
.polygonMode = vk::PolygonMode::eFill,
|
.polygonMode = vk::PolygonMode::eFill,
|
||||||
.cullMode = vk::CullModeFlagBits::eBack,
|
.cullMode = vk::CullModeFlagBits::eNone,
|
||||||
.frontFace = vk::FrontFace::eCounterClockwise,
|
.frontFace = vk::FrontFace::eCounterClockwise,
|
||||||
.depthBiasEnable = false,
|
.depthBiasEnable = false,
|
||||||
.lineWidth = 1.0,
|
.lineWidth = 1.0,
|
||||||
|
|
@ -629,8 +696,9 @@ CreatePipeline(const Device *device, const Swapchain *swapchain)
|
||||||
.sampleShadingEnable = false,
|
.sampleShadingEnable = false,
|
||||||
};
|
};
|
||||||
vk::PipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo = {
|
vk::PipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo = {
|
||||||
.depthTestEnable = false,
|
.depthTestEnable = true,
|
||||||
.depthWriteEnable = false,
|
.depthWriteEnable = true,
|
||||||
|
.depthCompareOp = vk::CompareOp::eLess,
|
||||||
};
|
};
|
||||||
vk::PipelineColorBlendAttachmentState colorBlendAttachmentState = {
|
vk::PipelineColorBlendAttachmentState colorBlendAttachmentState = {
|
||||||
.blendEnable = false,
|
.blendEnable = false,
|
||||||
|
|
@ -663,6 +731,7 @@ CreatePipeline(const Device *device, const Swapchain *swapchain)
|
||||||
.viewMask = 0,
|
.viewMask = 0,
|
||||||
.colorAttachmentCount = 1,
|
.colorAttachmentCount = 1,
|
||||||
.pColorAttachmentFormats = &swapchain->m_Format,
|
.pColorAttachmentFormats = &swapchain->m_Format,
|
||||||
|
.depthAttachmentFormat = vk::Format::eD32Sfloat,
|
||||||
};
|
};
|
||||||
|
|
||||||
vk::GraphicsPipelineCreateInfo pipelineCreateInfo = {
|
vk::GraphicsPipelineCreateInfo pipelineCreateInfo = {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue