95 lines
2.1 KiB
C++
95 lines
2.1 KiB
C++
#include <../../include/destrum/Graphics/Caches/ImageCache.h>
|
|
|
|
#include <destrum/Graphics/GfxDevice.h>
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
ImageCache::ImageCache(GfxDevice& gfxDevice) : gfxDevice(gfxDevice) {
|
|
}
|
|
|
|
ImageID ImageCache::loadImageFromFile(
|
|
const std::filesystem::path& path,
|
|
VkImageUsageFlags usage,
|
|
bool mipMap,
|
|
TextureIntent intent)
|
|
{
|
|
for (const auto& [id, info] : loadedImagesInfo) {
|
|
if (info.path == path &&
|
|
info.intent == intent &&
|
|
info.usage == usage &&
|
|
info.mipMap == mipMap)
|
|
{
|
|
return id;
|
|
}
|
|
}
|
|
|
|
auto imageOpt = gfxDevice.loadImageFromFileRaw(path, usage, mipMap, intent);
|
|
|
|
if (!imageOpt.has_value()) {
|
|
spdlog::warn(
|
|
"Using error texture for failed image load: '{}'",
|
|
path.string()
|
|
);
|
|
|
|
return errorImageId;
|
|
}
|
|
|
|
auto image = std::move(imageOpt.value());
|
|
|
|
const auto id = getFreeImageId();
|
|
|
|
addImage(id, std::move(image));
|
|
|
|
loadedImagesInfo.emplace(
|
|
id,
|
|
LoadedImageInfo{
|
|
.path = path,
|
|
.intent = intent,
|
|
.usage = usage,
|
|
.mipMap = mipMap,
|
|
});
|
|
|
|
return id;
|
|
}
|
|
|
|
ImageID ImageCache::addImage(GPUImage image) {
|
|
return addImage(getFreeImageId(), std::move(image));
|
|
}
|
|
|
|
ImageID ImageCache::addImage(ImageID id, GPUImage image) {
|
|
image.setBindlessId(static_cast<std::uint32_t>(id));
|
|
|
|
if (id < images.size()) {
|
|
gfxDevice.destroyImage(images[id]);
|
|
images[id] = std::move(image);
|
|
} else {
|
|
assert(id == images.size());
|
|
images.push_back(std::move(image));
|
|
}
|
|
|
|
bindlessSetManager.addImage(
|
|
gfxDevice.getDevice(),
|
|
id,
|
|
images[id].imageView
|
|
);
|
|
|
|
return id;
|
|
}
|
|
|
|
const GPUImage& ImageCache::getImage(ImageID id) const {
|
|
assert(id != NULL_IMAGE_ID && id < images.size());
|
|
return images.at(id);
|
|
}
|
|
|
|
ImageID ImageCache::getFreeImageId() const {
|
|
return images.size();
|
|
}
|
|
|
|
void ImageCache::destroyImages() {
|
|
for (const auto& image: images) {
|
|
gfxDevice.destroyImage(image);
|
|
}
|
|
images.clear();
|
|
loadedImagesInfo.clear();
|
|
}
|