Skip to content

Commit c6554fe

Browse files
[Renderer] [Engine] Texture use count & display in GUI
1 parent ffe215e commit c6554fe

8 files changed

Lines changed: 87 additions & 16 deletions

File tree

Engine/MauEng/Private/GUI/ImGUILayer.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,42 @@ namespace MauEng
262262

263263
ImGui::EndTable();
264264
}
265+
266+
if (ImGui::BeginTable("TextureTable", 1, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
267+
{
268+
auto const& info{ RENDERER.GetTextureMap() };
269+
270+
ImGui::TableSetupColumn("Textures");
271+
ImGui::TableHeadersRow();
272+
273+
for (auto const& mat : info)
274+
{
275+
ImGui::TableNextRow();
276+
ImGui::TableSetColumnIndex(0);
277+
278+
if (ImGui::TreeNode(mat.first.c_str()))
279+
{
280+
ImGui::Indent();
281+
282+
// Display mesh details as plain text or another table
283+
auto const loadedID{ mat.second.textureID };
284+
if (loadedID == UINT32_MAX)
285+
{
286+
ImGui::TextColored(ImVec4(1, 0, 0, 1), "Invalid Texture ID");
287+
}
288+
else
289+
{
290+
ImGui::Text("Texture ID: %zu", mat.second.textureID);
291+
ImGui::Text("Use Count: %zu", mat.second.useCount);
292+
}
293+
294+
ImGui::Unindent();
295+
ImGui::TreePop();
296+
}
297+
}
298+
299+
ImGui::EndTable();
300+
}
265301
ImGui::End();
266302
}
267303
}

Engine/Renderer/Private/NullRenderer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ namespace MauRen
5555
return info;
5656
}
5757

58+
virtual [[nodiscard]] std::unordered_map<std::string, struct LoadedTextureInfo> const& GetTextureMap() const noexcept override
59+
{
60+
static auto temp{ std::unordered_map<std::string, LoadedTextureInfo>{}};
61+
62+
return temp;
63+
}
64+
5865

5966
NullRenderer(NullRenderer const&) = delete;
6067
NullRenderer(NullRenderer&&) = delete;

Engine/Renderer/Private/Vulkan/Assets/VulkanTextureManager.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace MauRen
4949
return INVALID_TEXTURE_ID;
5050
}
5151

52-
return it->second;
52+
return it->second.textureID;
5353
}
5454

5555
uint32_t VulkanTextureManager::LoadOrGetTexture(VulkanCommandPoolManager& cmdPoolManager, VulkanDescriptorContext& descriptorContext, std::string const& textureName, bool isNorm) noexcept
@@ -60,18 +60,25 @@ namespace MauRen
6060
{
6161
return INVALID_TEXTURE_ID;
6262
}
63+
std::string cleanPath{ textureName };
64+
std::string const prefix{ "Resources/Models/" };
65+
if (cleanPath.starts_with(prefix))
66+
{
67+
cleanPath.erase(0, prefix.size());
68+
}
6369

64-
auto const it{ m_TextureIDMap.find(textureName) };
70+
auto const it{ m_TextureIDMap.find(cleanPath) };
6571
if (it != end(m_TextureIDMap))
6672
{
67-
return it->second;
73+
it->second.useCount++;
74+
return it->second.textureID;
6875
}
6976

7077
VulkanImage textureImage{ CreateTextureImage(cmdPoolManager, textureName, isNorm)};
7178
descriptorContext.BindTexture(m_Textures.size(), textureImage.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
7279

7380
m_Textures.emplace_back(std::move(textureImage));
74-
m_TextureIDMap[textureName] = m_Textures.size() - 1;
81+
m_TextureIDMap[cleanPath] = { static_cast<uint32_t>(m_Textures.size() - 1), 1 };
7582

7683
return m_Textures.size() - 1;
7784
}
@@ -84,18 +91,25 @@ namespace MauRen
8491
{
8592
return INVALID_TEXTURE_ID;
8693
}
94+
std::string cleanPath{ textureName };
95+
std::string const prefix{ "Resources/Models/" };
96+
if (cleanPath.starts_with(prefix))
97+
{
98+
cleanPath.erase(0, prefix.size());
99+
}
87100

88-
auto const it = m_TextureIDMap.find(textureName);
101+
auto const it = m_TextureIDMap.find(cleanPath);
89102
if (it != m_TextureIDMap.end())
90103
{
91-
return it->second;
104+
it->second.useCount++;
105+
return it->second.textureID;
92106
}
93107

94108
VulkanImage textureImage{ CreateTextureImage(cmdPoolManager, embTex, isNorm) };
95109
descriptorContext.BindTexture(m_Textures.size(), textureImage.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
96110

97111
m_Textures.emplace_back(std::move(textureImage));
98-
m_TextureIDMap[textureName] = m_Textures.size() - 1;
112+
m_TextureIDMap[cleanPath] = { static_cast<uint32_t>(m_Textures.size() - 1), 1 };
99113

100114
return m_Textures.size() - 1;
101115
}
@@ -144,41 +158,41 @@ namespace MauRen
144158
descriptorContext.BindTexture(static_cast<uint32_t>(std::size(m_Textures)), defaultWhiteTexture.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
145159

146160
m_Textures.emplace_back(std::move(defaultWhiteTexture));
147-
m_TextureIDMap["__DefaultWhite"] = static_cast<uint32_t>(std::size(m_Textures) - 1);
161+
m_TextureIDMap["__DefaultWhite"] = { static_cast<uint32_t>(std::size(m_Textures) - 1) , 0};
148162

149163
// 1
150164
auto defaultGrayTexture{ Create1x1Texture(cmdPoolManager, glm::vec4(.5f), false) };
151165
descriptorContext.BindTexture(static_cast<uint32_t>(std::size(m_Textures)), defaultGrayTexture.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
152166
m_Textures.emplace_back(std::move(defaultGrayTexture));
153-
m_TextureIDMap["__DefaultGray"] = static_cast<uint32_t>(std::size(m_Textures) - 1);
167+
m_TextureIDMap["__DefaultGray"] = { static_cast<uint32_t>(std::size(m_Textures) - 1) , 0 };
154168

155169
// 2
156170
auto defaultNormalTexture{ Create1x1Texture(cmdPoolManager, glm::vec4(0.5f, 0.5f, 1.0f, 1.0f), false) };
157171
descriptorContext.BindTexture(static_cast<uint32_t>(std::size(m_Textures)), defaultNormalTexture.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
158172

159173
m_Textures.emplace_back(std::move(defaultNormalTexture));
160-
m_TextureIDMap["__DefaultNormal"] = static_cast<uint32_t>(std::size(m_Textures) - 1);
174+
m_TextureIDMap["__DefaultNormal"] = { static_cast<uint32_t>(std::size(m_Textures) - 1) , 0 };
161175

162176
// 3
163177
auto defaultBlackTexture{ Create1x1Texture(cmdPoolManager, glm::vec4(0.0f), false) };
164178
descriptorContext.BindTexture(static_cast<uint32_t>(std::size(m_Textures)), defaultBlackTexture.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
165179

166180
m_Textures.emplace_back(std::move(defaultBlackTexture));
167-
m_TextureIDMap["__DefaultBlack"] = static_cast<uint32_t>(std::size(m_Textures) - 1);
181+
m_TextureIDMap["__DefaultBlack"] = { static_cast<uint32_t>(std::size(m_Textures) - 1) , 0 };
168182

169183
// 4
170184
auto defaultMetalnessTexture{ Create1x1Texture(cmdPoolManager, glm::vec4(1.0f, 1.0f, 0.f, 1.0f), false) };
171185
descriptorContext.BindTexture(static_cast<uint32_t>(std::size(m_Textures)), defaultMetalnessTexture.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
172186

173187
m_Textures.emplace_back(std::move(defaultMetalnessTexture));
174-
m_TextureIDMap["__DefaultMetalness"] = static_cast<uint32_t>(std::size(m_Textures) - 1);
188+
m_TextureIDMap["__DefaultMetalness"] = { static_cast<uint32_t>(std::size(m_Textures) - 1) , 0 };
175189

176190
// 5
177191
auto invalidTexture{ Create1x1Texture(cmdPoolManager, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f), false) };
178192
descriptorContext.BindTexture(static_cast<uint32_t>(std::size(m_Textures)), invalidTexture.imageViews[0], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
179193

180194
m_Textures.emplace_back(std::move(invalidTexture));
181-
m_TextureIDMap["__DefaultInvalid"] = static_cast<uint32_t>(std::size(m_Textures) - 1);
195+
m_TextureIDMap["__DefaultInvalid"] = { static_cast<uint32_t>(std::size(m_Textures) - 1) , 0 };
182196

183197
}
184198

@@ -188,7 +202,6 @@ namespace MauRen
188202

189203
ME_ASSERT(std::filesystem::exists(path));
190204

191-
auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() };
192205
Image const img{ path };
193206

194207
VkDeviceSize const imageSize{ static_cast<uint32_t>(img.width * img.height * 4) };

Engine/Renderer/Private/Vulkan/Assets/VulkanTextureManager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <unordered_map>
55

6+
#include "BindlessData.h"
67
#include "VulkanImage.h"
78
#include "Assets/Material.h"
89

@@ -27,14 +28,16 @@ namespace MauRen
2728

2829
[[nodiscard]] VkSampler GetTextureSampler() const noexcept { return m_TextureSampler; }
2930

31+
[[nodiscard]] std::unordered_map<std::string, LoadedTextureInfo>const& GetTextureMap() const noexcept { return m_TextureIDMap; }
32+
3033
VulkanTextureManager(VulkanTextureManager const&) = delete;
3134
VulkanTextureManager(VulkanTextureManager&&) = delete;
3235
VulkanTextureManager& operator=(VulkanTextureManager const&) = delete;
3336
VulkanTextureManager& operator=(VulkanTextureManager const&&) = delete;
3437
private:
3538
// Texture name, ID
3639
// ID maps directly to the ID in the vulkan buffer & should be reflected in the material
37-
std::unordered_map<std::string, uint32_t> m_TextureIDMap;
40+
std::unordered_map<std::string, LoadedTextureInfo> m_TextureIDMap;
3841

3942
// Vector of all the texture images - this is a 1:1 with the buffer on the GPU
4043
std::vector<VulkanImage> m_Textures;

Engine/Renderer/Private/Vulkan/VulkanRenderer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ namespace MauRen
315315
};
316316
}
317317

318+
std::unordered_map<std::string, struct LoadedTextureInfo> const& VulkanRenderer::GetTextureMap() const noexcept
319+
{
320+
return VulkanMaterialManager::GetInstance().GetTextureManager()->GetTextureMap();
321+
}
322+
318323
std::pair<std::unordered_map<std::string, struct LoadedMeshes_PathInfo> const&, std::vector<struct MeshData> const&>
319324
VulkanRenderer::GetRendererMeshInfo()
320325
{

Engine/Renderer/Private/Vulkan/VulkanRenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace MauRen
6565

6666
virtual [[nodiscard]] std::pair<std::unordered_map<std::string, struct LoadedMeshes_PathInfo> const&, std::vector<struct MeshData>const&> GetRendererMeshInfo() override;
6767
virtual [[nodiscard]] MaterialRendererInfo GetMaterialRendererInfo() const noexcept override;
68-
68+
virtual [[nodiscard]] std::unordered_map<std::string, struct LoadedTextureInfo> const& GetTextureMap() const noexcept override;
6969
VulkanRenderer(VulkanRenderer const&) = delete;
7070
VulkanRenderer(VulkanRenderer&&) = delete;
7171
VulkanRenderer& operator=(VulkanRenderer const&) = delete;

Engine/Renderer/Shared/BindlessData.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace MauRen
3737
uint32_t materialID{ INVALID_MATERIAL_ID };
3838
uint32_t useCount{ 0 };
3939
};
40+
struct LoadedTextureInfo
41+
{
42+
uint32_t textureID{ INVALID_TEXTURE_ID };
43+
uint32_t useCount{ 0 };
44+
};
4045

4146
// (GPU-side resource - CPU copy)
4247
// Per instance data

Engine/Renderer/Shared/Renderer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ namespace MauRen
4141

4242
virtual [[nodiscard]] std::pair<std::unordered_map<std::string, struct LoadedMeshes_PathInfo> const&, std::vector<struct MeshData>const&> GetRendererMeshInfo() = 0;
4343
virtual [[nodiscard]] struct MaterialRendererInfo GetMaterialRendererInfo() const noexcept = 0;
44+
virtual [[nodiscard]] std::unordered_map<std::string, struct LoadedTextureInfo>const& GetTextureMap() const noexcept = 0;
45+
4446

4547
Renderer(Renderer const&) = delete;
4648
Renderer(Renderer&&) = delete;

0 commit comments

Comments
 (0)