Skip to content

Commit

Permalink
Use insert_replace in shader manager.
Browse files Browse the repository at this point in the history
We want to see updates.
  • Loading branch information
Hans-Kristian Arntzen committed Oct 5, 2018
1 parent 0b08941 commit ae31fae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions util/thread_safe_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class Cache
return ret;
}

T *insert_replace(Hash hash, std::unique_ptr<T> value)
{
auto &cache = hashmap[hash];
cache = std::move(value);

auto *ret = cache.get();
return ret;
}

HashMap<std::unique_ptr<T>> &get_hashmap()
{
return hashmap;
Expand Down Expand Up @@ -84,6 +93,14 @@ class ThreadSafeCache
return ret;
}

T *insert_replace(Hash hash, std::unique_ptr<T> value)
{
lock.lock_write();
auto *ret = cache.insert_replace(hash, std::move(value));
lock.unlock_write();
return ret;
}

HashMap<std::unique_ptr<T>> &get_hashmap()
{
return cache.get_hashmap();
Expand Down
6 changes: 3 additions & 3 deletions vulkan/managers/shader_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Vulkan::Program *ShaderProgram::get_program(unsigned variant)
{
var.program = device->request_program(comp->spirv.data(), comp->spirv.size() * sizeof(uint32_t));
auto spirv_hash = var.program->get_shader(ShaderStage::Compute)->get_hash();
cache.insert(comp->hash, make_unique<Hash>(spirv_hash));
cache.insert_replace(comp->hash, make_unique<Hash>(spirv_hash));
}
}
auto ret = var.program;
Expand Down Expand Up @@ -223,15 +223,15 @@ Vulkan::Program *ShaderProgram::get_program(unsigned variant)
else
{
vert_shader = device->request_shader(vert->spirv.data(), vert->spirv.size() * sizeof(uint32_t));
cache.insert(vert->hash, make_unique<Hash>(vert_shader->get_hash()));
cache.insert_replace(vert->hash, make_unique<Hash>(vert_shader->get_hash()));
}

if (frag->spirv.empty())
frag_shader = device->request_shader_by_hash(frag->spirv_hash);
else
{
frag_shader = device->request_shader(frag->spirv.data(), frag->spirv.size() * sizeof(uint32_t));
cache.insert(frag->hash, make_unique<Hash>(frag_shader->get_hash()));
cache.insert_replace(frag->hash, make_unique<Hash>(frag_shader->get_hash()));
}

var.program = device->request_program(vert_shader, frag_shader);
Expand Down

0 comments on commit ae31fae

Please sign in to comment.