SpiecsEngine
 
Loading...
Searching...
No Matches

◆ LoadBin()

bool Spices::TextureLoader::LoadBin ( const std::string & fileName,
const std::string & it,
Texture2D * outTexture )
staticprivate

Function of load a ktx file.

Parameters
[in]fileNamektx file name.
[in]itfile direct folder.
[in]outTexturePointer of texture.
Returns
Returns true if load file succeed.

Instance the VulkanImage as Texture2D Resource.

Instance the Resource.

Transform Image Layout from undefined to transfer_dst.

Copy Memory to Image.

Instance a staginBuffer.

Copy the data from texture bytes to staginBuffer.

Transform Image Layout from undefined to transfer_dst.

Copy the data from staginBuffer to Image.

Transform Image Layout from transfer_dst to shaderread.

Create Image View.

Create Image Sampler.

Release ktx image data.

Instance the VulkanImage as Texture2D Resource.

Instance the Resource.

Transform Image Layout from undefined to transfer_dst.

Copy Memory to Image.

Instance a staginBuffer.

Copy the data from texture bytes to staginBuffer.

Transform Image Layout from undefined to transfer_dst.

Copy the data from staginBuffer to Image.

Transform Image Layout from transfer_dst to shaderread.

Create Image View.

Create Image Sampler.

Release ktx image data.

Definition at line 105 of file TextureLoader.cpp.

106 {
108
109 std::string binPath;
110 if (it == "")
111 {
112 binPath = fileName;
113 }
114 else
115 {
116 std::vector<std::string> splitString = StringLibrary::SplitString(fileName, '.');
117 binPath = it + binTexturePath + splitString[0] + ".ktx";
118 }
119
120 ktxTexture2* texture = nullptr;
121 Transcoder::LoadFromKTX(binPath, texture);
122
126 outTexture->m_Resource = std::make_shared<VulkanImage>(VulkanRenderBackend::GetState());
127 auto resourceptr = outTexture->GetResource<VulkanImage>();
128
129 VkImageUsageFlags usage = VK_IMAGE_USAGE_SAMPLED_BIT; // Can be Used for Sample
130 bool hostCopy = VulkanImage::IsHostCopyable(resourceptr->m_VulkanState, static_cast<VkFormat>(texture->vkFormat));
131 usage |= hostCopy ? VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT : VK_IMAGE_USAGE_TRANSFER_DST_BIT;
132
136 resourceptr->CreateImage(
137 resourceptr->m_VulkanState ,
138 fileName ,
139 static_cast<VkImageType>(texture->numDimensions - 1),
140 texture->baseWidth ,
141 texture->baseHeight ,
142 texture->baseDepth , // 1 layers.
143 VK_SAMPLE_COUNT_1_BIT , // No MASS.
144 static_cast<VkFormat>(texture->vkFormat) , // Compress Format.
145 VK_IMAGE_TILING_OPTIMAL , // Tiling Optimal.
146 usage ,
147 0 ,
148 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ,
149 texture->numLevels
150 );
151
152 if (hostCopy)
153 {
157 resourceptr->TransitionImageLayout(
158 resourceptr->m_Format,
159 VK_IMAGE_LAYOUT_UNDEFINED,
160 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
161 );
162
163 std::vector<VkMemoryToImageCopyEXT> copies;
164 for (uint32_t mip_level = 0; mip_level < texture->numLevels; mip_level++)
165 {
166 VkMemoryToImageCopyEXT memoryCopy{};
167 memoryCopy.sType = VK_STRUCTURE_TYPE_MEMORY_TO_IMAGE_COPY_EXT;
168 memoryCopy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
169 memoryCopy.imageSubresource.mipLevel = mip_level;
170 memoryCopy.imageSubresource.baseArrayLayer = 0;
171 memoryCopy.imageSubresource.layerCount = 1;
172 memoryCopy.imageExtent.width = std::max((uint32_t)1, texture->baseWidth >> mip_level);
173 memoryCopy.imageExtent.height = std::max((uint32_t)1, texture->baseHeight >> mip_level);
174 memoryCopy.imageExtent.depth = 1;
175 memoryCopy.pHostPointer = texture->pData + Transcoder::GetMipmapOffset(texture, mip_level);
176
177 copies.push_back(memoryCopy);
178 }
179
183 resourceptr->CopyMemoryToImageHost(copies);
184 }
185 else
186 {
190 VulkanBuffer stagingBuffer(
191 resourceptr->m_VulkanState,
192 "StagingBuffer" ,
193 texture->dataSize ,
194 VK_BUFFER_USAGE_TRANSFER_SRC_BIT ,
195 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
196 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
197 );
198
202 stagingBuffer.WriteToBuffer(texture->pData);
203
207 resourceptr->TransitionImageLayout(
208 resourceptr->m_Format ,
209 VK_IMAGE_LAYOUT_UNDEFINED ,
210 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
211 );
212
213 std::vector<VkBufferImageCopy> regions;
214 for (uint32_t mip_level = 0; mip_level < texture->numLevels; mip_level++)
215 {
216 VkBufferImageCopy region {};
217 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
218 region.imageSubresource.mipLevel = mip_level;
219 region.imageSubresource.baseArrayLayer = 0;
220 region.imageSubresource.layerCount = 1;
221 region.imageExtent.width = std::max((uint32_t)1, texture->baseWidth >> mip_level);
222 region.imageExtent.height = std::max((uint32_t)1, texture->baseHeight >> mip_level);
223 region.imageExtent.depth = 1;
224 region.bufferOffset = Transcoder::GetMipmapOffset(texture, mip_level);
225
226 regions.push_back(region);
227 }
228
232 resourceptr->CopyBufferToImage(
233 stagingBuffer.Get() ,
234 resourceptr->m_Image ,
235 static_cast<uint32_t>(resourceptr->m_Width) ,
236 static_cast<uint32_t>(resourceptr->m_Height),
237 regions
238 );
239
243 resourceptr->TransitionImageLayout(
244 resourceptr->m_Format ,
245 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL ,
246 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
247 );
248 }
249
253 resourceptr->CreateImageView(
254 resourceptr->m_Format ,
255 VK_IMAGE_VIEW_TYPE_2D ,
256 VK_IMAGE_ASPECT_COLOR_BIT
257 );
258
262 resourceptr->CreateSampler();
263
268
269 return true;
270 }
#define SPICES_PROFILE_ZONE
static std::vector< std::string > SplitString(const std::string &input, char delimiter)
Split a string to a string vector container use a char.
static ktx_size_t GetMipmapOffset(ktxTexture2 *texture, uint32_t mipLevel)
Get Mipmap data offset.
static bool DestroyktxTexture2(ktxTexture2 *texture)
Destroy a ktx file.
static bool LoadFromKTX(const std::string &filePath, ktxTexture2 *&texture)
Load a ktx file.
bool IsHostCopyable() const
Check if this image format can copy from host to gpu directly.
static VulkanState & GetState()
Get VulkanState in use.
const std::string binTexturePath

References Spices::VulkanRenderBackend::GetState().

Referenced by Load().