Skip to content

Commit 3449d18

Browse files
authored
Image2DBufferSpecification for Image ViewType (#377)
* Image2DBufferSpecification * added Image2DBufferSpecification constructor * code review * code review changes
1 parent d8a3e6e commit 3449d18

File tree

8 files changed

+102
-40
lines changed

8 files changed

+102
-40
lines changed

ZEngine/include/ZEngine/Hardwares/VulkanDevice.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ namespace ZEngine::Hardwares
133133
uint32_t width,
134134
uint32_t height,
135135
VkImageType image_type,
136+
VkImageViewType image_view_type,
136137
VkFormat image_format,
137138
VkImageTiling image_tiling,
138139
VkImageLayout image_initial_layout,
@@ -147,7 +148,12 @@ namespace ZEngine::Hardwares
147148
static VkSampler CreateImageSampler();
148149
static VkFormat FindSupportedFormat(const std::vector<VkFormat>& format_collection, VkImageTiling image_tiling, VkFormatFeatureFlags feature_flags);
149150
static VkFormat FindDepthFormat();
150-
static VkImageView CreateImageView(VkImage image, VkFormat image_format, VkImageAspectFlagBits image_aspect_flag, uint32_t layer_count = 1U);
151+
static VkImageView CreateImageView(
152+
VkImage image,
153+
VkFormat image_format,
154+
VkImageViewType image_view_type,
155+
VkImageAspectFlagBits image_aspect_flag,
156+
uint32_t layer_count = 1U);
151157
static VkFramebuffer CreateFramebuffer(
152158
const std::vector<VkImageView>& attachments,
153159
const VkRenderPass& render_pass,

ZEngine/include/ZEngine/Rendering/Buffers/Image2DBuffer.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
#pragma once
22
#include <vulkan/vulkan.h>
33
#include <Hardwares/VulkanDevice.h>
4+
#include <Rendering/Specifications/TextureSpecification.h>
45

56
namespace ZEngine::Rendering::Buffers
67
{
78
struct Image2DBuffer : public Helpers::RefCounted
89
{
910
Image2DBuffer() = default;
10-
Image2DBuffer(
11-
uint32_t width,
12-
uint32_t height,
13-
VkFormat format,
14-
VkImageUsageFlags usage_flag_bit,
15-
VkImageAspectFlagBits image_aspect_flag_bit,
16-
uint32_t layer_count = 1U,
17-
VkImageCreateFlags image_create_flag_bit = 0);
11+
Image2DBuffer(const Specifications::Image2DBufferSpecification& spec);
1812
~Image2DBuffer();
1913

2014
Hardwares::BufferImage& GetBuffer();

ZEngine/include/ZEngine/Rendering/Specifications/FormatSpecification.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,39 @@ namespace ZEngine::Rendering::Specifications
112112
COMPUTE
113113
};
114114

115+
enum class ImageViewType : uint32_t
116+
{
117+
TYPE_1D = 0,
118+
TYPE_2D,
119+
TYPE_3D,
120+
TYPE_CUBE
121+
};
122+
123+
static VkImageViewType ImageViewTypeMap[] = {
124+
VK_IMAGE_VIEW_TYPE_1D,
125+
VK_IMAGE_VIEW_TYPE_2D,
126+
VK_IMAGE_VIEW_TYPE_3D,
127+
VK_IMAGE_VIEW_TYPE_CUBE,
128+
VK_IMAGE_VIEW_TYPE_1D_ARRAY,
129+
VK_IMAGE_VIEW_TYPE_2D_ARRAY,
130+
VK_IMAGE_VIEW_TYPE_CUBE_ARRAY,
131+
};
132+
133+
enum class ImageCreateFlag
134+
{
135+
NONE = 0,
136+
SPARSE_BINDING_BIT,
137+
SPARSE_RESIDENCY_BIT,
138+
SPARSE_ALIASED_BIT,
139+
MUTABLE_FORMAT_BIT,
140+
CUBE_COMPATIBLE_BIT
141+
};
142+
143+
static VkImageCreateFlagBits ImageCreateFlagMap[]{
144+
VkImageCreateFlagBits(0),
145+
VK_IMAGE_CREATE_SPARSE_BINDING_BIT,
146+
VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
147+
VK_IMAGE_CREATE_SPARSE_ALIASED_BIT,
148+
VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT,
149+
VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT};
115150
} // namespace ZEngine::Rendering::Specifications

ZEngine/include/ZEngine/Rendering/Specifications/TextureSpecification.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
2-
#include <ZEngineDef.h>
32
#include <Rendering/Specifications/FormatSpecification.h>
3+
#include <ZEngineDef.h>
44

55
namespace ZEngine::Rendering::Specifications
66
{
@@ -19,4 +19,26 @@ namespace ZEngine::Rendering::Specifications
1919
LoadOperation LoadOp = LoadOperation::CLEAR;
2020
const void* Data = nullptr;
2121
};
22-
}
22+
23+
enum class ImageBufferUsageType
24+
{
25+
CUBEMAP = 0,
26+
SINGLE_2D_IMAGE,
27+
SINGLE_3D_IMAGE,
28+
ARRAYOF_2D_IMAGE
29+
};
30+
31+
struct Image2DBufferSpecification
32+
{
33+
uint32_t Width;
34+
uint32_t Height;
35+
ImageViewType ImageViewType = ImageViewType::TYPE_2D;
36+
ImageBufferUsageType BufferUsageType;
37+
VkFormat ImageFormat;
38+
VkImageUsageFlags ImageUsage;
39+
VkImageAspectFlagBits ImageAspectFlag;
40+
uint32_t LayerCount = 1U;
41+
ImageCreateFlag ImageCreateFlag = ImageCreateFlag::NONE;
42+
};
43+
44+
} // namespace ZEngine::Rendering::Specifications

ZEngine/src/Image2DBuffer.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,35 @@
33

44
namespace ZEngine::Rendering::Buffers
55
{
6-
Image2DBuffer::Image2DBuffer(
7-
uint32_t width,
8-
uint32_t height,
9-
VkFormat format,
10-
VkImageUsageFlags usage_flag_bit,
11-
VkImageAspectFlagBits image_aspect_flag_bit,
12-
uint32_t layer_count,
13-
VkImageCreateFlags image_create_flag_bit)
14-
: m_width(width), m_height(height)
6+
Image2DBuffer::Image2DBuffer(const Specifications::Image2DBufferSpecification& spec) : m_width(spec.Width), m_height(spec.Height)
157
{
168
ZENGINE_VALIDATE_ASSERT(m_width > 0, "Image width must be greater then zero")
179
ZENGINE_VALIDATE_ASSERT(m_height > 0, "Image height must be greater then zero")
1810

11+
Specifications::ImageViewType image_view_type = Specifications::ImageViewType::TYPE_2D;
12+
Specifications::ImageCreateFlag image_create_flag = Specifications::ImageCreateFlag::NONE;
13+
14+
if (spec.BufferUsageType == Specifications::ImageBufferUsageType::CUBEMAP)
15+
{
16+
image_view_type = Specifications::ImageViewType::TYPE_CUBE;
17+
image_create_flag = Specifications::ImageCreateFlag::CUBE_COMPATIBLE_BIT;
18+
}
19+
1920
m_buffer_image = Hardwares::VulkanDevice::CreateImage(
2021
m_width,
2122
m_height,
2223
VK_IMAGE_TYPE_2D,
23-
format,
24+
Specifications::ImageViewTypeMap[VALUE_FROM_SPEC_MAP(image_view_type)],
25+
spec.ImageFormat,
2426
VK_IMAGE_TILING_OPTIMAL,
2527
VK_IMAGE_LAYOUT_UNDEFINED,
26-
usage_flag_bit,
28+
spec.ImageUsage,
2729
VK_SHARING_MODE_EXCLUSIVE,
2830
VK_SAMPLE_COUNT_1_BIT,
2931
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
30-
image_aspect_flag_bit, layer_count, image_create_flag_bit);
32+
spec.ImageAspectFlag,
33+
spec.LayerCount,
34+
Specifications::ImageCreateFlagMap[VALUE_FROM_SPEC_MAP(image_create_flag)]);
3135
}
3236

3337
Image2DBuffer::~Image2DBuffer()

ZEngine/src/Swapchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ namespace ZEngine::Rendering
275275

276276
for (size_t i = 0; i < m_image_view_collection.size(); ++i)
277277
{
278-
m_image_view_collection[i] = Hardwares::VulkanDevice::CreateImageView(m_image_collection[i], m_surface_format.format, VK_IMAGE_ASPECT_COLOR_BIT);
278+
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);
279279
}
280280

281281
/*Swapchain framebuffer*/

ZEngine/src/Texture2D.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace ZEngine::Rendering::Textures
114114

115115
Ref<Texture2D> Texture2D::Create(const Specifications::TextureSpecification& spec)
116116
{
117-
Ref<Texture2D> texture = CreateRef<Texture2D>();
117+
Ref<Texture2D> texture = CreateRef<Texture2D>();
118118
texture->m_specification = spec;
119119
FillAsVulkanImage(texture, spec);
120120
return texture;
@@ -181,16 +181,18 @@ namespace ZEngine::Rendering::Textures
181181
uint32_t image_usage_attachment =
182182
(spec.Format == Specifications::ImageFormat::DEPTH_STENCIL_FROM_DEVICE) ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT : VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
183183

184-
VkFormat image_format = (spec.Format == Specifications::ImageFormat::DEPTH_STENCIL_FROM_DEVICE) ? Hardwares::VulkanDevice::FindDepthFormat()
185-
: Specifications::ImageFormatMap[static_cast<uint32_t>(spec.Format)];
186-
texture->m_image_2d_buffer = CreateRef<Buffers::Image2DBuffer>(
187-
texture->m_width,
188-
texture->m_height,
189-
image_format,
190-
VkImageUsageFlagBits(image_usage_attachment | transfert_bit | sampled_bit | storage_bit),
191-
VkImageAspectFlagBits(image_aspect),
192-
spec.LayerCount,
193-
spec.IsCubemap ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0);
184+
VkFormat image_format = (spec.Format == Specifications::ImageFormat::DEPTH_STENCIL_FROM_DEVICE) ? Hardwares::VulkanDevice::FindDepthFormat()
185+
: Specifications::ImageFormatMap[static_cast<uint32_t>(spec.Format)];
186+
Specifications::Image2DBufferSpecification buffer_spec;
187+
buffer_spec.Width = texture->m_width;
188+
buffer_spec.Height = texture->m_height;
189+
buffer_spec.BufferUsageType = spec.IsCubemap ? Specifications::ImageBufferUsageType::CUBEMAP : Specifications::ImageBufferUsageType::SINGLE_2D_IMAGE;
190+
buffer_spec.ImageFormat = image_format;
191+
buffer_spec.ImageUsage = VkImageUsageFlagBits(image_usage_attachment | transfert_bit | sampled_bit | storage_bit);
192+
buffer_spec.ImageAspectFlag = VkImageAspectFlagBits(image_aspect);
193+
buffer_spec.LayerCount = spec.LayerCount;
194+
195+
texture->m_image_2d_buffer = CreateRef<Buffers::Image2DBuffer>(std::move(buffer_spec));
194196

195197
if (spec.PerformTransition)
196198
{

ZEngine/src/VulkanDevice.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ namespace ZEngine::Hardwares
960960
uint32_t width,
961961
uint32_t height,
962962
VkImageType image_type,
963+
VkImageViewType image_view_type,
963964
VkFormat image_format,
964965
VkImageTiling image_tiling,
965966
VkImageLayout image_initial_layout,
@@ -996,7 +997,7 @@ namespace ZEngine::Hardwares
996997
vmaCreateImage(s_vma_allocator, &image_create_info, &allocation_create_info, &(buffer_image.Handle), &(buffer_image.Allocation), nullptr) == VK_SUCCESS,
997998
"Failed to create buffer");
998999

999-
buffer_image.ViewHandle = CreateImageView(buffer_image.Handle, image_format, image_aspect_flag, layer_count);
1000+
buffer_image.ViewHandle = CreateImageView(buffer_image.Handle, image_format, image_view_type, image_aspect_flag, layer_count);
10001001
buffer_image.Sampler = CreateImageSampler();
10011002
return buffer_image;
10021003
}
@@ -1059,16 +1060,14 @@ namespace ZEngine::Hardwares
10591060
{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);
10601061
}
10611062

1062-
VkImageView VulkanDevice::CreateImageView(VkImage image, VkFormat image_format, VkImageAspectFlagBits image_aspect_flag, uint32_t layer_count)
1063+
VkImageView VulkanDevice::CreateImageView(VkImage image, VkFormat image_format, VkImageViewType image_view_type, VkImageAspectFlagBits image_aspect_flag, uint32_t layer_count)
10631064
{
10641065
VkImageView image_view{VK_NULL_HANDLE};
10651066
VkImageViewCreateInfo image_view_create_info = {};
10661067
image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
10671068
image_view_create_info.format = image_format;
10681069
image_view_create_info.image = image;
1069-
image_view_create_info.viewType = (image_format == VK_FORMAT_R32G32B32A32_SFLOAT)
1070-
? VK_IMAGE_VIEW_TYPE_CUBE
1071-
: VK_IMAGE_VIEW_TYPE_2D; // ToDo : We should better abstract the creation of ImageView.. introduce Image2DBufferSpecification ?
1070+
image_view_create_info.viewType = image_view_type;
10721071
image_view_create_info.components.r = VK_COMPONENT_SWIZZLE_R;
10731072
image_view_create_info.components.g = VK_COMPONENT_SWIZZLE_G;
10741073
image_view_create_info.components.b = VK_COMPONENT_SWIZZLE_B;

0 commit comments

Comments
 (0)