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 3 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,11 @@ namespace ZEngine::Rendering::Specifications
COMPUTE
};

enum class ImageType : uint32_t
{
Flat2D,
Cubemap,
};

static VkImageViewType ImageViewTypeMap[] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_CUBE};
} // 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,31 @@ namespace ZEngine::Rendering::Specifications
LoadOperation LoadOp = LoadOperation::CLEAR;
const void* Data = nullptr;
};
}

struct Image2DBufferSpecification
{
uint32_t width;
uint32_t height;
ImageType image_view_type;
VkFormat image_format;
VkImageUsageFlags image_usage;
VkImageAspectFlagBits image_aspect_flag;
uint32_t layer_count = 1U;
VkImageCreateFlags image_create_flag_bit = 0;

Image2DBufferSpecification(
uint32_t w,
uint32_t h,
ImageType view_type,
VkFormat format,
VkImageUsageFlags usage,
VkImageAspectFlagBits aspect,
uint32_t layers = 1U,
VkImageCreateFlags create_flags = 0)
: width(w), height(h), image_view_type(view_type), image_format(format), image_usage(usage), image_aspect_flag(aspect), layer_count(layers),
image_create_flag_bit(create_flags)
{
}
};

} // namespace ZEngine::Rendering::Specifications
18 changes: 6 additions & 12 deletions ZEngine/src/Image2DBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@

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")
Expand All @@ -20,14 +13,15 @@ namespace ZEngine::Rendering::Buffers
m_width,
m_height,
VK_IMAGE_TYPE_2D,
format,
Specifications::ImageViewTypeMap[VALUE_FROM_SPEC_MAP(spec.image_view_type)],
spec.image_format,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_LAYOUT_UNDEFINED,
usage_flag_bit,
spec.image_usage,
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.image_aspect_flag, spec.layer_count, spec.image_create_flag_bit);
}

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_CUBE, VK_IMAGE_ASPECT_COLOR_BIT);
}

/*Swapchain framebuffer*/
Expand Down
5 changes: 3 additions & 2 deletions ZEngine/src/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ namespace ZEngine::Rendering::Textures

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_image_2d_buffer = CreateRef<Buffers::Image2DBuffer>(Specifications::Image2DBufferSpecification(
texture->m_width,
texture->m_height,
spec.IsCubemap ? Specifications::ImageType::Cubemap : Specifications::ImageType::Flat2D,
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);
spec.IsCubemap ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0));

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