mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-28 20:27:28 +01:00
Ordering them before the imports in the same file was not enough: a project header on line 1 can import a module several levels down whose global module fragment already pulled in the same standard header, and that include guard does not cross the module boundary. Putting them first means the guard is set before anything can import. Skips vendored single-header libraries and anything inside an #if.
111 lines
4.0 KiB
C++
111 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <nvrhi/nvrhi.h>
|
|
|
|
#include <entt/entt.hpp>
|
|
|
|
import eu07.glm;
|
|
class NvTextureManager;
|
|
class MaResourceRegistry;
|
|
|
|
struct MaResourceMapping {
|
|
int m_slot;
|
|
int m_size;
|
|
entt::hashed_string m_key;
|
|
nvrhi::ResourceType m_type;
|
|
|
|
#define MA_RESOURCE_MAPPING_INITIALIZER(type) \
|
|
static MaResourceMapping type(int slot, const char* key) { \
|
|
MaResourceMapping mapping{}; \
|
|
mapping.m_slot = slot; \
|
|
mapping.m_key = static_cast<entt::hashed_string>(key); \
|
|
mapping.m_type = nvrhi::ResourceType::type; \
|
|
return mapping; \
|
|
}
|
|
|
|
static MaResourceMapping Texture_SRV(int slot, const char* key) {
|
|
MaResourceMapping mapping{};
|
|
mapping.m_slot = slot;
|
|
mapping.m_key = static_cast<entt::hashed_string>(key);
|
|
mapping.m_type = nvrhi::ResourceType::Texture_SRV;
|
|
return mapping;
|
|
}
|
|
MA_RESOURCE_MAPPING_INITIALIZER(Texture_UAV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(TypedBuffer_SRV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(TypedBuffer_UAV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(StructuredBuffer_SRV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(StructuredBuffer_UAV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(RawBuffer_SRV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(RawBuffer_UAV)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(ConstantBuffer)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(VolatileConstantBuffer)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(Sampler)
|
|
MA_RESOURCE_MAPPING_INITIALIZER(RayTracingAccelStruct)
|
|
|
|
#undef MA_RESOURCE_MAPPING_INITIALIZER
|
|
|
|
static MaResourceMapping PushConstants(const int slot, const int size) {
|
|
MaResourceMapping mapping{};
|
|
mapping.m_slot = slot;
|
|
mapping.m_type = nvrhi::ResourceType::PushConstants;
|
|
mapping.m_size = size;
|
|
return mapping;
|
|
}
|
|
|
|
template <typename PushType>
|
|
static MaResourceMapping PushConstants(const int slot) {
|
|
MaResourceMapping mapping{};
|
|
mapping.m_slot = slot;
|
|
mapping.m_type = nvrhi::ResourceType::PushConstants;
|
|
mapping.m_size = sizeof(PushType);
|
|
return mapping;
|
|
}
|
|
};
|
|
|
|
struct MaResourceMappingSet {
|
|
nvrhi::static_vector<MaResourceMapping, 128> m_mappings;
|
|
MaResourceMappingSet& Add(const MaResourceMapping& mapping);
|
|
void ToBindingLayout(nvrhi::BindingLayoutDesc& desc) const;
|
|
bool ToBindingSet(nvrhi::BindingSetDesc& desc,
|
|
const MaResourceRegistry* registry,
|
|
const nvrhi::BindingLayoutDesc* reference_layout = nullptr) const;
|
|
};
|
|
|
|
struct MaResourceView {
|
|
nvrhi::IResource* m_resource = nullptr;
|
|
nvrhi::ResourceType m_type = nvrhi::ResourceType::None;
|
|
[[nodiscard]] bool IsValid() const {
|
|
return m_resource && m_type != nvrhi::ResourceType::None;
|
|
}
|
|
};
|
|
|
|
class MaResourceRegistry {
|
|
public:
|
|
[[nodiscard]] MaResourceView GetResource(
|
|
const entt::hashed_string& key, nvrhi::ResourceType requested_type) const;
|
|
[[nodiscard]] NvTextureManager* GetTextureManager() const;
|
|
[[nodiscard]] uint64_t GetLastStreamingTextureUpdate() const;
|
|
void UpdateLastStreamingTextureUse(const glm::dvec3& location) const;
|
|
void RegisterResource(bool global, const entt::hashed_string& key,
|
|
nvrhi::IResource* resource, nvrhi::ResourceType type);
|
|
void RegisterTexture(const entt::hashed_string& key, size_t handle);
|
|
void RegisterResource(bool global, const char* key,
|
|
nvrhi::IResource* resource, nvrhi::ResourceType type);
|
|
void RegisterTexture(const char* key, size_t handle);
|
|
virtual ~MaResourceRegistry() = default;
|
|
|
|
protected:
|
|
explicit MaResourceRegistry(MaResourceRegistry* parent);
|
|
void InitResourceRegistry();
|
|
|
|
private:
|
|
[[nodiscard]] MaResourceView GetResourceLocal(
|
|
const entt::hashed_string& key, nvrhi::ResourceType requested_type) const;
|
|
MaResourceRegistry* m_parent = nullptr;
|
|
std::shared_ptr<NvTextureManager> m_texture_manager = nullptr;
|
|
entt::dense_map<entt::id_type, MaResourceView> m_resource_views{};
|
|
entt::dense_map<entt::id_type, size_t> m_streaming_textures{};
|
|
}; |