Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_imageloader.inl
Go to the documentation of this file.
1#pragma once
2#include "../osi/foray_env.hpp"
6#include "../core/foray_managedimage.hpp"
7
8using namespace std::filesystem;
9
10namespace foray::util {
11
12 template <VkFormat FORMAT>
14 {
15 VkFormatProperties properties;
16 vkGetPhysicalDeviceFormatProperties(context->VkbPhysicalDevice->physical_device, FORMAT, &properties);
17 if((properties.linearTilingFeatures & VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_TRANSFER_DST_BIT) > 0)
18 {
19 return true;
20 }
21 return false;
22 }
23
24 template <VkFormat FORMAT>
26 {
27 // Reset all members
28 Destroy();
29
30 mInfo.Utf8Path = utf8path;
31
32 path fspath = utf8path;
33 if(fspath.has_extension())
34 {
35 mInfo.Extension = osi::ToUtf8Path(fspath.extension());
36 }
37 if(fspath.has_filename())
38 {
39 mInfo.Name = osi::ToUtf8Path(fspath.filename());
40 }
41
42 if(!exists(fspath))
43 {
44 return false;
45 }
46
47 if(mInfo.Extension == ".exr")
48 {
49 return PopulateImageInfo_TinyExr();
50 }
51 else
52 {
53 return PopulateImageInfo_Stb();
54 }
55 }
56
57 template <VkFormat FORMAT>
59 {
60 if(!mInfo.Valid)
61 {
62 logger()->warn("Image Loader: Invalid Image Info!");
63 return false;
64 }
65
66 if(mInfo.Extension == ".exr")
67 {
68 return Load_TinyExr();
69 }
70 else
71 {
72 return Load_Stb();
73 }
74 }
75
76 template <VkFormat FORMAT>
78 {
79 if(mCustomLoaderInfoDeleter && mCustomLoaderInfo)
80 {
81 mCustomLoaderInfoDeleter(mCustomLoaderInfo);
82 }
83 mCustomLoaderInfo = nullptr;
84 mCustomLoaderInfoDeleter = nullptr;
85 mRawData.clear();
86
87 // Inplace new requires explicit destructor call
88 mInfo.~ImageInfo();
89 new(&mInfo) ImageInfo();
90 }
91
92 template <VkFormat FORMAT>
93 inline void ImageLoader<FORMAT>::InitManagedImage(core::Context* context, core::ManagedImage* image, core::ManagedImage::CreateInfo& ci, VkImageLayout afterwrite) const
94 {
95 if(!mInfo.Valid || !mRawData.size())
96 {
97 return;
98 }
99
100 UpdateManagedImageCI(ci);
101
102 image->Create(context, ci);
103 WriteManagedImageData(image, afterwrite);
104 }
105
106 template <VkFormat FORMAT>
108 {
109 if(!mInfo.Valid || !mRawData.size())
110 {
111 return;
112 }
113
114 UpdateManagedImageCI(ci);
115
116 image->Create(context, ci);
117 WriteManagedImageData(cmdBuffer, image, afterwrite);
118 }
119
120 template <VkFormat FORMAT>
122 {
123 ci.ImageCI.format = FORMAT;
124 ci.ImageCI.initialLayout = VkImageLayout::VK_IMAGE_LAYOUT_UNDEFINED;
125 ci.ImageCI.usage = ci.ImageCI.usage | VkImageUsageFlagBits::VK_IMAGE_USAGE_TRANSFER_DST_BIT;
126 ci.ImageCI.extent = VkExtent3D{.width = mInfo.Extent.width, .height = mInfo.Extent.height, .depth = 1};
127 ci.ImageCI.imageType = VkImageType::VK_IMAGE_TYPE_2D;
128 }
129
130 template <VkFormat FORMAT>
131 inline void ImageLoader<FORMAT>::WriteManagedImageData(core::ManagedImage* image, VkImageLayout afterwrite) const
132 {
133 image->WriteDeviceLocalData(mRawData.data(), mRawData.size(), afterwrite);
134 }
135
136 template <VkFormat FORMAT>
137 inline void ImageLoader<FORMAT>::WriteManagedImageData(core::HostSyncCommandBuffer& cmdBuffer, core::ManagedImage* image, VkImageLayout afterwrite) const
138 {
139 image->WriteDeviceLocalData(cmdBuffer, mRawData.data(), mRawData.size(), afterwrite);
140 }
141
142
143} // namespace foray
Extension of the commandbuffer wrapper for temporary host synchronized command buffer execution.
Definition foray_commandbuffer.hpp:41
Wraps allocation and lifetime functionality of VkImage.
Definition foray_managedimage.hpp:13
virtual void Create(Context *context, const CreateInfo &createInfo)
Allocates image, creates image, creates imageview.
void WriteDeviceLocalData(const void *data, size_t size, VkImageLayout layoutAfterWrite, VkBufferImageCopy &imageCopy)
Creates a staging buffer, writes staging buffer, transitions image layout to transfer destination opt...
Utf8 encoded path wrapper.
Definition foray_env.hpp:28
Definition foray_imageloader.hpp:34
void WriteManagedImageData(core::ManagedImage *image, VkImageLayout afterwrite) const
Definition foray_imageloader.inl:131
void UpdateManagedImageCI(core::ManagedImage::CreateInfo &ci) const
Definition foray_imageloader.inl:121
void Destroy()
Cleans up the loader.
Definition foray_imageloader.inl:77
bool Load()
Loads the file into CPU memory (Init first!)
Definition foray_imageloader.inl:58
static bool sFormatSupported(core::Context *context)
Checks if format the loader was initialized in supports linear tiling transfer and shader read.
Definition foray_imageloader.inl:13
bool Init(const osi::Utf8Path &utf8path)
Inits the image loader.
Definition foray_imageloader.inl:25
void InitManagedImage(core::Context *context, core::ManagedImage *image, core::ManagedImage::CreateInfo &ci, VkImageLayout afterwrite=VkImageLayout::VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) const
Definition foray_imageloader.inl:93
std::string ToUtf8Path(std::filesystem::path path)
Convert std::filesystem::path type to UTF8 path.
Definition foray_dualbuffer.hpp:5
spdlog::logger * logger()
Gives access to a global available logger object. The logger writes to console & to a file next to th...
Non owning context object.
Definition foray_context.hpp:16
vkb::PhysicalDevice * VkbPhysicalDevice
A Vkb PhysicalDevice.
Definition foray_context.hpp:24
Combines all structs used for initialization.
Definition foray_managedimage.hpp:20
VkImageCreateInfo ImageCI
Vulkan Image CreateInfo.
Definition foray_managedimage.hpp:22