Initial claude dump
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <sophia/resource_registry_ref.hpp>
|
||||
#include <sophia/util/assert.hpp>
|
||||
#include <sophia/util/logger.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "resources/resource_registry.hpp"
|
||||
|
||||
@@ -14,6 +18,26 @@ MeshHandle ResourceRegistryRef::load<Mesh>(const std::string& path) {
|
||||
return MeshHandle{};
|
||||
}
|
||||
|
||||
template <>
|
||||
TextureHandle ResourceRegistryRef::load<Texture>(const std::string& path) {
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int channels = 0;
|
||||
// Always decode to RGBA8, whatever the file has.
|
||||
stbi_uc* pixels = stbi_load(path.c_str(), &width, &height, &channels, 4);
|
||||
if (pixels == nullptr) {
|
||||
log::fatal("failed to load texture", path, "-", stbi_failure_reason());
|
||||
throw std::runtime_error("failed to load texture: " + path);
|
||||
}
|
||||
|
||||
auto size = static_cast<size_t>(width) * static_cast<size_t>(height) * 4;
|
||||
TextureHandle handle = registry->texturePool.create(
|
||||
registry->device, static_cast<u32>(width), static_cast<u32>(height),
|
||||
std::span<const u8>{pixels, size});
|
||||
stbi_image_free(pixels);
|
||||
return handle;
|
||||
}
|
||||
|
||||
template <>
|
||||
void ResourceRegistryRef::release<Mesh>(MeshHandle handle) {
|
||||
registry->meshPool.release(handle);
|
||||
@@ -24,6 +48,15 @@ void ResourceRegistryRef::release<RenderTarget>(RenderTargetHandle handle) {
|
||||
registry->renderTargetPool.release(handle);
|
||||
}
|
||||
|
||||
template <>
|
||||
void ResourceRegistryRef::release<Texture>(TextureHandle handle) {
|
||||
SOPHIA_ASSERT(handle != registry->whiteTexture,
|
||||
"the built-in white texture cannot be released");
|
||||
// Frames in flight may still be sampling it.
|
||||
registry->device.waitIdle();
|
||||
registry->texturePool.release(handle);
|
||||
}
|
||||
|
||||
MeshHandle ResourceRegistryRef::createResource(
|
||||
std::type_identity<Mesh>, std::vector<CanvasVertex> vertices,
|
||||
std::vector<u32> indices) {
|
||||
@@ -42,6 +75,13 @@ RenderTargetHandle ResourceRegistryRef::createResource(
|
||||
return registry->renderTargetPool.create(width, height);
|
||||
}
|
||||
|
||||
TextureHandle ResourceRegistryRef::createResource(std::type_identity<Texture>,
|
||||
u32 width, u32 height,
|
||||
std::vector<u8> pixels) {
|
||||
return registry->texturePool.create(registry->device, width, height,
|
||||
std::span<const u8>{pixels});
|
||||
}
|
||||
|
||||
void ResourceRegistryRef::requestResize(RenderTargetHandle handle, u32 width,
|
||||
u32 height) {
|
||||
registry->renderTargetPool.get(handle).requestResize(width, height);
|
||||
@@ -56,4 +96,14 @@ bool ResourceRegistryRef::isAlive(RenderTargetHandle handle) const {
|
||||
return registry->renderTargetPool.isAlive(handle);
|
||||
}
|
||||
|
||||
glm::vec2 ResourceRegistryRef::getExtent(TextureHandle handle) const {
|
||||
const Texture& texture = registry->texturePool.get(handle);
|
||||
return {static_cast<float>(texture.getWidth()),
|
||||
static_cast<float>(texture.getHeight())};
|
||||
}
|
||||
|
||||
bool ResourceRegistryRef::isAlive(TextureHandle handle) const {
|
||||
return registry->texturePool.isAlive(handle);
|
||||
}
|
||||
|
||||
} // namespace sophia
|
||||
|
||||
Reference in New Issue
Block a user