Files
Destrum/destrum/include/destrum/Graphics/RenderResources.h
T

111 lines
3.7 KiB
C++

#ifndef RENDER_RESOURCES_H
#define RENDER_RESOURCES_H
#include <destrum/Graphics/Caches/ImageCache.h>
#include <destrum/Graphics/Caches/MeshCache.h>
#include <destrum/Graphics/Caches/MaterialCache.h>
#include <destrum/Graphics/ids.h>
#include <destrum/Graphics/Util.h>
#include <filesystem>
#include <memory>
#include <string_view>
#include <unordered_set>
namespace ModelDoc { struct MaterialInfo; }
class GfxDevice;
class RenderResources {
public:
RenderResources();
RenderResources(const RenderResources&) = delete;
RenderResources& operator=(const RenderResources&) = delete;
void init(GfxDevice& gfxDevice);
void cleanup(GfxDevice& gfxDevice);
ImageID createImage(
GfxDevice& gfxDevice,
const vkutil::CreateImageInfo& createInfo,
const std::string& debugName = "",
void* pixelData = nullptr,
ImageID imageId = NULL_IMAGE_ID);
ImageID createDrawImage(
GfxDevice& gfxDevice,
VkFormat format,
glm::ivec2 size,
const std::string& debugName = "",
ImageID imageId = NULL_IMAGE_ID);
ImageID loadImageFromFile(
GfxDevice& gfxDevice,
const std::filesystem::path& path,
VkImageUsageFlags usage = VK_IMAGE_USAGE_SAMPLED_BIT,
bool mipMap = true,
TextureIntent intent = TextureIntent::ColorSrgb);
ImageID loadMaterialTexture(GfxDevice& gfxDevice, const std::filesystem::path& path, TextureIntent intent);
struct ReleaseStats {
std::size_t meshes{0};
std::size_t materials{0};
std::size_t images{0};
};
ReleaseStats releaseUnused(
GfxDevice& gfxDevice,
const std::unordered_set<MeshID>& usedMeshes,
const std::unordered_set<MaterialID>& usedMaterials);
ImageID addImageToCache(GPUImage image);
void destroyImage(ImageID id);
// Imports a file-backed model and uploads its mesh/material resources.
// References are keyed as "source#subresource" in the resource caches.
void loadModelAsset(GfxDevice& gfxDevice, std::string_view sourceReference);
// Creates a PBR material from an imported model material, loading its texture maps.
MaterialID addModelMaterial(GfxDevice& gfxDevice, const ModelDoc::MaterialInfo& sourceMaterial);
[[nodiscard]] const GPUImage& getImage(ImageID id) const;
[[nodiscard]] ImageID getWhiteTextureID() const { return whiteImageId; }
[[nodiscard]] ImageID getErrorTextureID() const { return errorImageId; }
[[nodiscard]] ImageID getDefaultNormalTextureID() const { return defaultNormalImageId; }
BindlessSetManager& getBindlessSetManager();
VkDescriptorSetLayout getBindlessDescSetLayout() const;
const VkDescriptorSet& getBindlessDescSet() const;
void bindBindlessDescSet(
VkCommandBuffer cmd,
VkPipelineLayout layout,
VkPipelineBindPoint bindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS) const;
[[nodiscard]] bool isInitialized() const { return imageCache != nullptr && materialCache != nullptr; }
const ImageCache& images() const { return *imageCache; }
MeshCache& meshes() { return *meshCache; }
const MeshCache& meshes() const { return *meshCache; }
MaterialCache& materials() { return *materialCache; }
const MaterialCache& materials() const { return *materialCache; }
private:
static uint32_t BytesPerTexel(VkFormat fmt);
std::unique_ptr<ImageCache> imageCache;
std::unique_ptr<MeshCache> meshCache;
std::unique_ptr<MaterialCache> materialCache;
std::unordered_set<ImageID> materialTextures;
std::unordered_set<ImageID> pinnedImages;
ImageID whiteImageId{NULL_IMAGE_ID};
ImageID errorImageId{NULL_IMAGE_ID};
ImageID defaultNormalImageId{NULL_IMAGE_ID};
};
#endif