Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/sgl/device/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ ref<SlangModule> SlangSession::load_module_from_source(
module->load(build);
module->store_built_data(build);

module = deduplicate_module(std::move(module));

// Update cache of loaded modules.
update_module_cache_and_dependencies();

Expand Down Expand Up @@ -636,6 +638,11 @@ void SlangSession::_unregister_module(SlangModule* module)
auto existing = std::find(m_registered_modules.begin(), m_registered_modules.end(), module);
SGL_ASSERT(existing != m_registered_modules.end());
m_registered_modules.erase(existing);

if (auto it = m_sgl_to_cache_location_map.find(module); it != m_sgl_to_cache_location_map.end()) {
m_slang_to_sgl_module_map.erase(it->second);
m_sgl_to_cache_location_map.erase(it);
}
}

std::string SlangSession::to_string() const
Expand Down Expand Up @@ -738,6 +745,18 @@ bool SlangSession::write_module_to_cache(slang::IModule* module)
return true;
}

ref<SlangModule> SlangSession::deduplicate_module(ref<SlangModule>&& module)
{
auto it = m_slang_to_sgl_module_map.find(module->slang_module());
if (it != m_slang_to_sgl_module_map.end())
return ref<SlangModule>(it->second);

it = m_slang_to_sgl_module_map.insert(std::make_pair(module->slang_module(), module.get())).first;
m_sgl_to_cache_location_map[module.get()] = it;

return module;
}

std::string SlangSessionData::resolve_module_name(std::string_view module_name) const
{
// Return if module name is an absolute file path.
Expand Down
9 changes: 9 additions & 0 deletions src/sgl/device/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,21 @@ class SGL_API SlangSession : public Object {
/// Note: this is a vector, as order of creation matters.
std::vector<SlangModule*> m_registered_modules;

/// Map to deduplicate sgl::SlangModule that reference same slang::IModule
std::map<slang::IModule*, SlangModule*> m_slang_to_sgl_module_map;

/// Caches the entry in the m_slang_to_sgl_module_map for faster deletion.
std::map<SlangModule*, std::map<slang::IModule*, SlangModule*>::iterator> m_sgl_to_cache_location_map;

/// All created sgl programs (via link_program)
std::set<ShaderProgram*> m_registered_programs;

void update_module_cache_and_dependencies();
bool write_module_to_cache(slang::IModule* module);
void create_session(SlangSessionBuild& build);

// Returns either the same module, or a different `SlangModule` that references identical `slang::IModule`
ref<SlangModule> deduplicate_module(ref<SlangModule>&& module);
};

struct SlangModuleDesc {
Expand Down