Repository navigation
Expand file tree
/
Copy pathTextureCache.cpp
More file actions
138 lines (117 loc) · 5.54 KB
/
Copy pathTextureCache.cpp
File metadata and controls
138 lines (117 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "TextureCache.h"
#include "DefaultTexture.h"
#include "Renderer.h"
#include "Log.h"
#include "StaticMeshData.h"
#define STB_IMAGE_IMPLEMENTATION
#include "ThirdParty/stb_image.h"
namespace Magic
{
TextureCache* GTextureCache = nullptr;
namespace
{
// constexpr VkFormat g_textureFormat = VK_FORMAT_R8G8B8A8_UNORM;
constexpr VkFormat colorTextureFormat = VK_FORMAT_R8G8B8A8_SRGB;
constexpr VkFormat dataTextureFormat = VK_FORMAT_R8G8B8A8_UNORM;
}
int TextureCache::GetOrUpload(const std::string& resolvedPath, const TextureData& textureData, const unsigned char* pixels, TextureType type)
{
std::lock_guard lock(m_mutex);
const auto cachedTexture = m_textures.find(resolvedPath);
if (cachedTexture != m_textures.end())
{
Logger::Info(std::format("Reusing texture '{}'", resolvedPath));
return cachedTexture->second.bindlessSlot;
}
if (GRenderer->m_bindlessManager.IsBindlessArrayFull())
{
Logger::Err("Bindless texture array is full");
return DefaultTexture::g_defaultTextureImageBindlessSlot;
}
const VkExtent3D extent {
.width = static_cast<uint32_t>(textureData.width),
.height = static_cast<uint32_t>(textureData.height),
.depth = 1
};
const VkFormat format = type == TextureType::Color ? colorTextureFormat : dataTextureFormat;
const VkImageCreateInfo imageCreateInfo = DefaultImageCreateInfo(format, extent, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_TYPE_2D);
CachedTexture newTexture;
newTexture.image = GRenderer->UploadImage(pixels, textureData.numChannels, imageCreateInfo);
const VkImageViewCreateInfo imageViewCreateInfo = DefaultImageViewCreateInfo(newTexture.image.image, format, VkComponentMapping{ VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A }, VK_IMAGE_ASPECT_COLOR_BIT);
newTexture.image.view = GRenderer->CreateViewForAllocatedImage(imageViewCreateInfo);
newTexture.bindlessSlot = GRenderer->m_bindlessManager.AddToBindlessTextureArray(newTexture.image);
const int bindlessSlot = newTexture.bindlessSlot;
m_textures.emplace(resolvedPath, std::move(newTexture));
return bindlessSlot;
}
static std::vector<unsigned char> LoadTextureData(const std::filesystem::path& texturePath, TextureData& textureData)
{
int textureWidth = 0;
int textureHeight = 0;
int numChannels = 0;
int desiredChannels = 4; // TODO: non-zero if we want to force a certain number of channels
stbi_uc* textureData_stbi = stbi_load(reinterpret_cast<char const *>(texturePath.u8string().c_str()), &textureWidth, &textureHeight, &numChannels, desiredChannels);
std::vector<unsigned char> pixels;
if (!textureData_stbi)
{
Logger::Err(std::format("Could not load texture file: {}", texturePath.string()));
std::exit(1);
}
else
{
auto textureDataSizeBytes = textureWidth * textureHeight * desiredChannels * sizeof(stbi_uc);
pixels.assign(textureData_stbi, textureData_stbi + textureDataSizeBytes);
// test
// if (!DoDaPointersSameData(pixels.data(), textureData_stbi, textureDataSizeBytes)) { Logger::Err("uh oh"); std::abort(); }
stbi_image_free(textureData_stbi);
}
textureData.width = textureWidth;
textureData.height = textureHeight;
textureData.numChannels = desiredChannels;
// textureData.baseTextureDataOffset = staticMeshDataData.textureData.size();
// textureData.sourcePath = texturePath.lexically_normal().string();
return pixels;
}
int TextureCache::GetOrUploadNEW(const std::filesystem::path& filePath, TextureType type)
{
std::lock_guard lock(m_mutex);
const std::string filePathStr = filePath.string();
const auto cachedTexture = m_textures.find(filePathStr);
if (cachedTexture != m_textures.end())
{
Logger::Info(std::format("Reusing texture '{}'", filePathStr));
return cachedTexture->second.bindlessSlot;
}
if (GRenderer->m_bindlessManager.IsBindlessArrayFull())
{
Logger::Err("Bindless texture array is full");
return DefaultTexture::g_defaultTextureImageBindlessSlot;
}
TextureData textureData;
auto pixels = LoadTextureData(filePath, textureData);
const VkExtent3D extent {
.width = static_cast<uint32_t>(textureData.width),
.height = static_cast<uint32_t>(textureData.height),
.depth = 1
};
const VkFormat format = type == TextureType::Color ? colorTextureFormat : dataTextureFormat;
const VkImageCreateInfo imageCreateInfo = DefaultImageCreateInfo(format, extent, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_TYPE_2D);
CachedTexture newTexture;
newTexture.image = GRenderer->UploadImage(pixels.data(), textureData.numChannels, imageCreateInfo);
const VkImageViewCreateInfo imageViewCreateInfo = DefaultImageViewCreateInfo(newTexture.image.image, format, VkComponentMapping{ VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A }, VK_IMAGE_ASPECT_COLOR_BIT);
newTexture.image.view = GRenderer->CreateViewForAllocatedImage(imageViewCreateInfo);
newTexture.bindlessSlot = GRenderer->m_bindlessManager.AddToBindlessTextureArray(newTexture.image);
const int bindlessSlot = newTexture.bindlessSlot;
m_textures.emplace(filePathStr, std::move(newTexture));
return bindlessSlot;
}
void TextureCache::Destroy()
{
std::lock_guard lock(m_mutex);
for (const auto& [filePath, texture] : m_textures)
{
GRenderer->DestroyImage(texture.image);
}
m_textures.clear();
}
}