Skip to content

Commit 1ae2518

Browse files
committed
Make loadAllShaders possible to reload shaders
1 parent d24fe7c commit 1ae2518

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

lib/graphics_engine/src/ge_vulkan_driver.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,8 +2576,7 @@ void GEVulkanDriver::updateDriver(bool scale_changed, bool pbr_changed,
25762576
destroySwapChainRelated(false/*handle_surface*/);
25772577
if (pbr_changed)
25782578
{
2579-
GEVulkanShaderManager::destroy();
2580-
GEVulkanShaderManager::init(this);
2579+
GEVulkanShaderManager::loadAllShaders();
25812580
GEVulkanSampler sampler = m_mesh_texture_descriptor->getSamplerUse();
25822581
delete m_mesh_texture_descriptor;
25832582
m_mesh_texture_descriptor = new GEVulkanTextureDescriptor(
@@ -2628,8 +2627,7 @@ void GEVulkanDriver::reloadShaders()
26282627
waitIdle();
26292628
setDisableWaitIdle(true);
26302629
clearDrawCallsCache();
2631-
GEVulkanShaderManager::destroy();
2632-
GEVulkanShaderManager::init(this);
2630+
GEVulkanShaderManager::loadAllShaders();
26332631
for (auto& dc : static_cast<GEVulkanSceneManager*>(
26342632
m_irrlicht_device->getSceneManager())->getDrawCalls())
26352633
dc.second = std::unique_ptr<GEVulkanDrawCall>(new GEVulkanDrawCall);

lib/graphics_engine/src/ge_vulkan_shader_manager.cpp

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <algorithm>
1010
#include <map>
11+
#include <memory>
1112
#include <sstream>
1213
#include <string>
1314
#include <stdexcept>
@@ -29,7 +30,21 @@ uint32_t g_mesh_texture_layer = 2;
2930

3031
uint32_t g_sampler_size = 512;
3132

32-
std::map<std::string, std::pair<GESpinLock, VkShaderModule>* > g_shaders;
33+
struct ShaderHolder
34+
{
35+
GESpinLock m_lock;
36+
VkShaderModule m_shader_module;
37+
ShaderHolder() : m_shader_module(VK_NULL_HANDLE) {}
38+
~ShaderHolder()
39+
{
40+
m_lock.lock();
41+
m_lock.unlock();
42+
if (g_vk && m_shader_module != VK_NULL_HANDLE)
43+
vkDestroyShaderModule(g_vk->getDevice(), m_shader_module, NULL);
44+
}
45+
};
46+
47+
std::map<std::string, std::unique_ptr<ShaderHolder> > g_shaders;
3348
} // GEVulkanShaderManager
3449

3550
// ============================================================================
@@ -51,7 +66,21 @@ void GEVulkanShaderManager::init(GEVulkanDriver* vk)
5166
{
5267
g_vk = vk;
5368
g_file_system = vk->getFileSystem();
69+
loadAllShaders();
70+
} // init
5471

72+
// ----------------------------------------------------------------------------
73+
void GEVulkanShaderManager::destroy()
74+
{
75+
g_shaders.clear();
76+
g_vk = NULL;
77+
g_file_system = NULL;
78+
} // destroy
79+
80+
// ----------------------------------------------------------------------------
81+
void GEVulkanShaderManager::loadAllShaders(const std::string& match_filename)
82+
{
83+
#ifndef DISABLE_SHADERC
5584
std::ostringstream oss;
5685
oss << "#version 450\n";
5786
if (getGEConfig()->m_pbr)
@@ -79,35 +108,16 @@ void GEVulkanShaderManager::init(GEVulkanDriver* vk)
79108
oss << "#define SHADER_STORAGE_IMAGE_EXTENDED_FORMATS\n";
80109
g_predefines = oss.str();
81110

82-
loadAllShaders();
83-
} // init
84-
85-
// ----------------------------------------------------------------------------
86-
void GEVulkanShaderManager::destroy()
87-
{
88-
if (!g_vk)
89-
return;
90-
for (auto& p : g_shaders)
91-
{
92-
p.second->first.lock();
93-
p.second->first.unlock();
94-
vkDestroyShaderModule(g_vk->getDevice(), p.second->second, NULL);
95-
delete p.second;
96-
}
97-
g_shaders.clear();
98-
} // destroy
99-
100-
// ----------------------------------------------------------------------------
101-
void GEVulkanShaderManager::loadAllShaders()
102-
{
103-
#ifndef DISABLE_SHADERC
104111
irr::io::IFileList* files = g_file_system->createFileList(
105112
getShaderFolder().c_str());
106113
for (unsigned i = 0; i < files->getFileCount(); i++)
107114
{
108115
if (files->isDirectory(i))
109116
continue;
110117
std::string filename = files->getFileName(i).c_str();
118+
if (!match_filename.empty() &&
119+
filename.find(match_filename) == std::string::npos)
120+
continue;
111121
std::string ext = filename.substr(filename.find_last_of(".") + 1);
112122
shaderc_shader_kind kind;
113123
if (ext == "vert")
@@ -122,21 +132,21 @@ void GEVulkanShaderManager::loadAllShaders()
122132
kind = shaderc_tess_evaluation_shader;
123133
else
124134
continue;
125-
auto pair = new std::pair<GESpinLock, VkShaderModule>();
126-
g_shaders[filename] = pair;
127-
pair->first.lock();
135+
g_shaders[filename] = std::unique_ptr<ShaderHolder>(new ShaderHolder);
136+
auto holder = g_shaders.at(filename).get();
137+
holder->m_lock.lock();
128138
GEVulkanCommandLoader::addMultiThreadingCommand(
129-
[pair, kind, filename]()
139+
[holder, kind, filename]()
130140
{
131141
try
132142
{
133-
pair->second = loadShader(kind, filename);
143+
holder->m_shader_module = loadShader(kind, filename);
134144
}
135145
catch (std::exception& e)
136146
{
137147
printf("%s", e.what());
138148
}
139-
pair->first.unlock();
149+
holder->m_lock.unlock();
140150
});
141151
}
142152
files->drop();
@@ -278,12 +288,12 @@ unsigned GEVulkanShaderManager::getMeshTextureLayer()
278288
// ----------------------------------------------------------------------------
279289
VkShaderModule GEVulkanShaderManager::getShader(const std::string& filename)
280290
{
281-
auto it = g_shaders.at(filename);
282-
it->first.lock();
283-
it->first.unlock();
284-
if (it->second == VK_NULL_HANDLE)
291+
auto& it = g_shaders.at(filename);
292+
it->m_lock.lock();
293+
it->m_lock.unlock();
294+
if (it->m_shader_module == VK_NULL_HANDLE)
285295
throw std::runtime_error(std::string("Missing shader ") + filename);
286-
return it->second;
296+
return it->m_shader_module;
287297
} // getShader
288298

289299
}

lib/graphics_engine/src/ge_vulkan_shader_manager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void init(GEVulkanDriver*);
1919
// ----------------------------------------------------------------------------
2020
void destroy();
2121
// ----------------------------------------------------------------------------
22-
void loadAllShaders();
22+
void loadAllShaders(const std::string& match_filename = "");
2323
// ----------------------------------------------------------------------------
2424
VkShaderModule getShader(const std::string& filename);
2525
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)