Skip to content

Image2DBufferSpecification for Image ViewType #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ZEngine/include/ZEngine/Hardwares/VulkanDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ namespace ZEngine::Hardwares
uint32_t width,
uint32_t height,
VkImageType image_type,
VkImageViewType image_view_type,
VkFormat image_format,
VkImageTiling image_tiling,
VkImageLayout image_initial_layout,
Expand All @@ -147,7 +148,12 @@ namespace ZEngine::Hardwares
static VkSampler CreateImageSampler();
static VkFormat FindSupportedFormat(const std::vector<VkFormat>& format_collection, VkImageTiling image_tiling, VkFormatFeatureFlags feature_flags);
static VkFormat FindDepthFormat();
static VkImageView CreateImageView(VkImage image, VkFormat image_format, VkImageAspectFlagBits image_aspect_flag, uint32_t layer_count = 1U);
static VkImageView CreateImageView(
VkImage image,
VkFormat image_format,
VkImageViewType image_view_type,
VkImageAspectFlagBits image_aspect_flag,
uint32_t layer_count = 1U);
static VkFramebuffer CreateFramebuffer(
const std::vector<VkImageView>& attachments,
const VkRenderPass& render_pass,
Expand Down
10 changes: 2 additions & 8 deletions ZEngine/include/ZEngine/Rendering/Buffers/Image2DBuffer.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#pragma once
#include <vulkan/vulkan.h>
#include <Hardwares/VulkanDevice.h>
#include <Rendering/Specifications/TextureSpecification.h>

namespace ZEngine::Rendering::Buffers
{
struct Image2DBuffer : public Helpers::RefCounted
{
Image2DBuffer() = default;
Image2DBuffer(
uint32_t width,
uint32_t height,
VkFormat format,
VkImageUsageFlags usage_flag_bit,
VkImageAspectFlagBits image_aspect_flag_bit,
uint32_t layer_count = 1U,
VkImageCreateFlags image_create_flag_bit = 0);
Image2DBuffer(const Specifications::Image2DBufferSpecification& spec);
~Image2DBuffer();

Hardwares::BufferImage& GetBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,39 @@ namespace ZEngine::Rendering::Specifications
COMPUTE
};

enum class ImageViewType : uint32_t
{
TYPE_1D = 0,
TYPE_2D,
TYPE_3D,
TYPE_CUBE
};

static VkImageViewType ImageViewTypeMap[] = {
VK_IMAGE_VIEW_TYPE_1D,
VK_IMAGE_VIEW_TYPE_2D,
VK_IMAGE_VIEW_TYPE_3D,
VK_IMAGE_VIEW_TYPE_CUBE,
VK_IMAGE_VIEW_TYPE_1D_ARRAY,
VK_IMAGE_VIEW_TYPE_2D_ARRAY,
VK_IMAGE_VIEW_TYPE_CUBE_ARRAY,
};

enum class ImageCreateFlag
{
NONE = 0,
SPARSE_BINDING_BIT,
SPARSE_RESIDENCY_BIT,
SPARSE_ALIASED_BIT,
MUTABLE_FORMAT_BIT,
CUBE_COMPATIBLE_BIT
};

static VkImageCreateFlagBits ImageCreateFlagMap[]{
VkImageCreateFlagBits(0),
VK_IMAGE_CREATE_SPARSE_BINDING_BIT,
VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
VK_IMAGE_CREATE_SPARSE_ALIASED_BIT,
VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT,
VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT};
} // namespace ZEngine::Rendering::Specifications
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <ZEngineDef.h>
#include <Rendering/Specifications/FormatSpecification.h>
#include <ZEngineDef.h>

namespace ZEngine::Rendering::Specifications
{
Expand All @@ -19,4 +19,26 @@ namespace ZEngine::Rendering::Specifications
LoadOperation LoadOp = LoadOperation::CLEAR;
const void* Data = nullptr;
};
}

enum class ImageBufferUsageType
{
CUBEMAP = 0,
SINGLE_2D_IMAGE,
SINGLE_3D_IMAGE,
ARRAYOF_2D_IMAGE
};

struct Image2DBufferSpecification
{
uint32_t Width;
uint32_t Height;
ImageViewType ImageViewType = ImageViewType::TYPE_2D;
ImageBufferUsageType BufferUsageType;
VkFormat ImageFormat;
VkImageUsageFlags ImageUsage;
VkImageAspectFlagBits ImageAspectFlag;
uint32_t LayerCount = 1U;
ImageCreateFlag ImageCreateFlag = ImageCreateFlag::NONE;
};

} // namespace ZEngine::Rendering::Specifications
28 changes: 16 additions & 12 deletions ZEngine/src/Image2DBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@

namespace ZEngine::Rendering::Buffers
{
Image2DBuffer::Image2DBuffer(
uint32_t width,
uint32_t height,
VkFormat format,
VkImageUsageFlags usage_flag_bit,
VkImageAspectFlagBits image_aspect_flag_bit,
uint32_t layer_count,
VkImageCreateFlags image_create_flag_bit)
: m_width(width), m_height(height)
Image2DBuffer::Image2DBuffer(const Specifications::Image2DBufferSpecification& spec) : m_width(spec.Width), m_height(spec.Height)
{
ZENGINE_VALIDATE_ASSERT(m_width > 0, "Image width must be greater then zero")
ZENGINE_VALIDATE_ASSERT(m_height > 0, "Image height must be greater then zero")

Specifications::ImageViewType image_view_type = Specifications::ImageViewType::TYPE_2D;
Specifications::ImageCreateFlag image_create_flag = Specifications::ImageCreateFlag::NONE;

if (spec.BufferUsageType == Specifications::ImageBufferUsageType::CUBEMAP)
{
image_view_type = Specifications::ImageViewType::TYPE_CUBE;
image_create_flag = Specifications::ImageCreateFlag::CUBE_COMPATIBLE_BIT;
}

m_buffer_image = Hardwares::VulkanDevice::CreateImage(
m_width,
m_height,
VK_IMAGE_TYPE_2D,
format,
Specifications::ImageViewTypeMap[VALUE_FROM_SPEC_MAP(image_view_type)],
spec.ImageFormat,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_LAYOUT_UNDEFINED,
usage_flag_bit,
spec.ImageUsage,
VK_SHARING_MODE_EXCLUSIVE,
VK_SAMPLE_COUNT_1_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
image_aspect_flag_bit, layer_count, image_create_flag_bit);
spec.ImageAspectFlag,
spec.LayerCount,
Specifications::ImageCreateFlagMap[VALUE_FROM_SPEC_MAP(image_create_flag)]);
}

Image2DBuffer::~Image2DBuffer()
Expand Down
2 changes: 1 addition & 1 deletion ZEngine/src/Swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ namespace ZEngine::Rendering

for (size_t i = 0; i < m_image_view_collection.size(); ++i)
{
m_image_view_collection[i] = Hardwares::VulkanDevice::CreateImageView(m_image_collection[i], m_surface_format.format, VK_IMAGE_ASPECT_COLOR_BIT);
m_image_view_collection[i] = Hardwares::VulkanDevice::CreateImageView(m_image_collection[i], m_surface_format.format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
}

/*Swapchain framebuffer*/
Expand Down
24 changes: 13 additions & 11 deletions ZEngine/src/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace ZEngine::Rendering::Textures

Ref<Texture2D> Texture2D::Create(const Specifications::TextureSpecification& spec)
{
Ref<Texture2D> texture = CreateRef<Texture2D>();
Ref<Texture2D> texture = CreateRef<Texture2D>();
texture->m_specification = spec;
FillAsVulkanImage(texture, spec);
return texture;
Expand Down Expand Up @@ -181,16 +181,18 @@ namespace ZEngine::Rendering::Textures
uint32_t image_usage_attachment =
(spec.Format == Specifications::ImageFormat::DEPTH_STENCIL_FROM_DEVICE) ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT : VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;

VkFormat image_format = (spec.Format == Specifications::ImageFormat::DEPTH_STENCIL_FROM_DEVICE) ? Hardwares::VulkanDevice::FindDepthFormat()
: Specifications::ImageFormatMap[static_cast<uint32_t>(spec.Format)];
texture->m_image_2d_buffer = CreateRef<Buffers::Image2DBuffer>(
texture->m_width,
texture->m_height,
image_format,
VkImageUsageFlagBits(image_usage_attachment | transfert_bit | sampled_bit | storage_bit),
VkImageAspectFlagBits(image_aspect),
spec.LayerCount,
spec.IsCubemap ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0);
VkFormat image_format = (spec.Format == Specifications::ImageFormat::DEPTH_STENCIL_FROM_DEVICE) ? Hardwares::VulkanDevice::FindDepthFormat()
: Specifications::ImageFormatMap[static_cast<uint32_t>(spec.Format)];
Specifications::Image2DBufferSpecification buffer_spec;
buffer_spec.Width = texture->m_width;
buffer_spec.Height = texture->m_height;
buffer_spec.BufferUsageType = spec.IsCubemap ? Specifications::ImageBufferUsageType::CUBEMAP : Specifications::ImageBufferUsageType::SINGLE_2D_IMAGE;
buffer_spec.ImageFormat = image_format;
buffer_spec.ImageUsage = VkImageUsageFlagBits(image_usage_attachment | transfert_bit | sampled_bit | storage_bit);
buffer_spec.ImageAspectFlag = VkImageAspectFlagBits(image_aspect);
buffer_spec.LayerCount = spec.LayerCount;

texture->m_image_2d_buffer = CreateRef<Buffers::Image2DBuffer>(std::move(buffer_spec));

if (spec.PerformTransition)
{
Expand Down
9 changes: 4 additions & 5 deletions ZEngine/src/VulkanDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ namespace ZEngine::Hardwares
uint32_t width,
uint32_t height,
VkImageType image_type,
VkImageViewType image_view_type,
VkFormat image_format,
VkImageTiling image_tiling,
VkImageLayout image_initial_layout,
Expand Down Expand Up @@ -996,7 +997,7 @@ namespace ZEngine::Hardwares
vmaCreateImage(s_vma_allocator, &image_create_info, &allocation_create_info, &(buffer_image.Handle), &(buffer_image.Allocation), nullptr) == VK_SUCCESS,
"Failed to create buffer");

buffer_image.ViewHandle = CreateImageView(buffer_image.Handle, image_format, image_aspect_flag, layer_count);
buffer_image.ViewHandle = CreateImageView(buffer_image.Handle, image_format, image_view_type, image_aspect_flag, layer_count);
buffer_image.Sampler = CreateImageSampler();
return buffer_image;
}
Expand Down Expand Up @@ -1059,16 +1060,14 @@ namespace ZEngine::Hardwares
{VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT}, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
}

VkImageView VulkanDevice::CreateImageView(VkImage image, VkFormat image_format, VkImageAspectFlagBits image_aspect_flag, uint32_t layer_count)
VkImageView VulkanDevice::CreateImageView(VkImage image, VkFormat image_format, VkImageViewType image_view_type, VkImageAspectFlagBits image_aspect_flag, uint32_t layer_count)
{
VkImageView image_view{VK_NULL_HANDLE};
VkImageViewCreateInfo image_view_create_info = {};
image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
image_view_create_info.format = image_format;
image_view_create_info.image = image;
image_view_create_info.viewType = (image_format == VK_FORMAT_R32G32B32A32_SFLOAT)
? VK_IMAGE_VIEW_TYPE_CUBE
: VK_IMAGE_VIEW_TYPE_2D; // ToDo : We should better abstract the creation of ImageView.. introduce Image2DBufferSpecification ?
image_view_create_info.viewType = image_view_type;
image_view_create_info.components.r = VK_COMPONENT_SWIZZLE_R;
image_view_create_info.components.g = VK_COMPONENT_SWIZZLE_G;
image_view_create_info.components.b = VK_COMPONENT_SWIZZLE_B;
Expand Down