@@ -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 ) };
0 commit comments