Skip to content

Commit

Permalink
Simplify IdTracker to IdGenerator
Browse files Browse the repository at this point in the history
* Do not bother caching previously used Ids, just a simple increment will do
  • Loading branch information
SirBob01 committed Oct 24, 2024
1 parent 8f4e760 commit 1835a3e
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 279 deletions.
2 changes: 1 addition & 1 deletion src/Dynamo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <Sound/Source.hpp>
#include <Utils/Allocator.hpp>
#include <Utils/Bits.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>
#include <Utils/Log.hpp>
#include <Utils/Random.hpp>
#include <Utils/RingBuffer.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Material.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Graphics/Shader.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <vector>

#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Graphics/Material.hpp>
#include <Graphics/Mesh.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
11 changes: 5 additions & 6 deletions src/Graphics/Vulkan/MeshSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,21 @@ namespace Dynamo::Graphics::Vulkan {
}

// Register the allocation
Mesh mesh = _ids.generate();
_allocations.insert(mesh, allocation);
Mesh mesh = IdGenerator<Mesh>::generate();
_allocations.emplace(mesh, allocation);
return mesh;
}

MeshAllocation &MeshSet::get(Mesh mesh) { return _allocations.get(mesh); }
MeshAllocation &MeshSet::get(Mesh mesh) { return _allocations.at(mesh); }

void MeshSet::free(Mesh mesh, Buffer &vertex_buffer, Buffer &index_buffer) {
MeshAllocation &allocation = _allocations.get(mesh);
MeshAllocation &allocation = _allocations.at(mesh);
for (unsigned offset : allocation.attribute_offsets) {
vertex_buffer.free(offset);
}
if (allocation.index_type != VK_INDEX_TYPE_NONE_KHR) {
index_buffer.free(allocation.index_offset);
}
_allocations.remove(mesh);
_ids.discard(mesh);
_allocations.erase(mesh);
}
} // namespace Dynamo::Graphics::Vulkan
7 changes: 3 additions & 4 deletions src/Graphics/Vulkan/MeshSet.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <unordered_map>
#include <vector>

#include <vulkan/vulkan_core.h>

#include <Graphics/Mesh.hpp>
#include <Graphics/Vulkan/Buffer.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/SparseSet.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics::Vulkan {
/**
Expand All @@ -29,8 +29,7 @@ namespace Dynamo::Graphics::Vulkan {
*
*/
class MeshSet {
IdTracker<Mesh> _ids;
SparseSet<Mesh, MeshAllocation> _allocations;
std::unordered_map<Mesh, MeshAllocation> _allocations;

public:
/**
Expand Down
14 changes: 6 additions & 8 deletions src/Graphics/Vulkan/ShaderSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,17 @@ namespace Dynamo::Graphics::Vulkan {
Log::info("");

// Register the new shader
Shader shader = _ids.generate();
_modules.insert(shader, module);
Shader shader = IdGenerator<Shader>::generate();
_modules.emplace(shader, module);
return shader;
}

const ShaderModule &ShaderSet::get(Shader shader) const { return _modules.get(shader); }
const ShaderModule &ShaderSet::get(Shader shader) const { return _modules.at(shader); }

void ShaderSet::destroy(Shader shader) {
ShaderModule &module = _modules.get(shader);
ShaderModule &module = _modules.at(shader);
vkDestroyShaderModule(_device, module.handle, nullptr);
_modules.remove(shader);
_ids.discard(shader);
_modules.erase(shader);
}

void ShaderSet::destroy() {
Expand All @@ -236,10 +235,9 @@ namespace Dynamo::Graphics::Vulkan {
}
_descriptor_layouts.clear();

for (ShaderModule &module : _modules) {
for (const auto &[key, module] : _modules) {
vkDestroyShaderModule(_device, module.handle, nullptr);
}
_modules.clear();
_ids.clear();
}
} // namespace Dynamo::Graphics::Vulkan
5 changes: 2 additions & 3 deletions src/Graphics/Vulkan/ShaderSet.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <string>
#include <unordered_map>
#include <vector>

#include <spirv_reflect.h>
#include <vulkan/vulkan_core.h>

#include <Graphics/Shader.hpp>
#include <Utils/SparseSet.hpp>

namespace Dynamo::Graphics::Vulkan {
/**
Expand Down Expand Up @@ -71,8 +71,7 @@ namespace Dynamo::Graphics::Vulkan {
*/
class ShaderSet {
VkDevice _device;
IdTracker<Shader> _ids;
SparseSet<Shader, ShaderModule> _modules;
std::unordered_map<Shader, ShaderModule> _modules;
std::unordered_map<DescriptorLayoutKey, VkDescriptorSetLayout, DescriptorLayoutKey::Hash> _descriptor_layouts;

/**
Expand Down
1 change: 0 additions & 1 deletion src/Sound/Jukebox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <portaudio.h>

#include <Utils/IdTracker.hpp>
#include <Utils/RingBuffer.hpp>

#include <Sound/Buffer.hpp>
Expand Down
2 changes: 0 additions & 2 deletions src/Sound/Listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include <Math/Quaternion.hpp>
#include <Math/Vec3.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/SparseSet.hpp>

namespace Dynamo::Sound {
/**
Expand Down
36 changes: 36 additions & 0 deletions src/Utils/IdGenerator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <cstdint>
#include <type_traits>

#define DYN_DEFINE_ID_TYPE(T) using T = struct T##_t *

namespace Dynamo {
/**
* @brief Generate typesafe handles.
*
* @tparam Id
*/
template <typename Id>
class IdGenerator {
static_assert(std::is_pointer<Id>::value, "Id must be a valid handle type (opaque pointer).");
static_assert(sizeof(Id) == sizeof(uintptr_t), "Id and its integer mode must be the same size.");
static inline uintptr_t _counter = 0;

public:
/**
* @brief Generate an id.
*
* @return Id
*/
static inline Id generate() { return reinterpret_cast<Id>(_counter++); }

/**
* @brief Get the key of the id.
*
* @param id
* @return uintptr_t
*/
static inline uintptr_t key(Id id) { return reinterpret_cast<uintptr_t>(id); }
};
} // namespace Dynamo
119 changes: 0 additions & 119 deletions src/Utils/IdTracker.hpp

This file was deleted.

Loading

0 comments on commit 1835a3e

Please sign in to comment.