Misc renderer improvements:

- Reuse renderer and gltf loader between scenes
- Use puish descriptors to simplify the management of descriptor sets in the scene renderer
- Don't keep the imgui swapchain in the scene classes
- Fix alpha blending, add alpha masking
- Fix mipmaps not being used in renderer
- Add cache when loading images and models
- Only premultiply the colours if required
- Add support for gltTF animations

Allow removing an object and its descendants from the scene, allow loading from a memory buffer
This commit is contained in:
Guillaume Meunier committed 2025-10-23 21:39:12 +02:00
1 parent 1f6ea0d4ca
commit aad1bb6a1b
21 files changed
+915 -372

No files matched your search

+59 -45
View File
@@ -318,7 +318,52 @@ image_loader::~image_loader()
ktxVulkanDeviceInfo_Destruct(&vdi);
}
loaded_image image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::Format format, const std::string & name)
template <typename T>
float alpha_scale;
template <>
float alpha_scale<uint8_t> = 1. / 255.;
template <>
float alpha_scale<uint16_t> = 1. / 65535.;
template <>
float alpha_scale<float> = 1.;
template <typename T>
void premultiply_alpha_aux(T * destination, const T * source, int n)
{
for (int i = 0; i < n; i++)
{
float alpha = source[4 * i + 3] * alpha_scale<T>;
destination[4 * i + 0] = source[4 * i + 0] * alpha;
destination[4 * i + 1] = source[4 * i + 1] * alpha;
destination[4 * i + 2] = source[4 * i + 2] * alpha;
destination[4 * i + 3] = source[4 * i + 3];
}
}
static void premultiply_alpha(void * destination, const void * source, vk::Extent3D extent, vk::Format format)
{
switch (format)
{
case vk::Format::eR8G8B8A8Srgb: // TODO: sRGB
case vk::Format::eR8G8B8A8Unorm:
premultiply_alpha_aux<uint8_t>((uint8_t *)destination, (const uint8_t *)source, extent.width * extent.height * extent.depth);
break;
case vk::Format::eR16G16B16A16Unorm:
premultiply_alpha_aux<uint16_t>((uint16_t *)destination, (const uint16_t *)source, extent.width * extent.height * extent.depth);
break;
case vk::Format::eR32G32B32A32Sfloat:
premultiply_alpha_aux<float>((float *)destination, (const float *)source, extent.width * extent.height * extent.depth);
break;
default:
break;
}
}
loaded_image image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::Format format, const std::string & name, bool premultiply)
{
auto cb = std::move(device.allocateCommandBuffers({
.commandPool = *cb_pool,
@@ -346,7 +391,11 @@ loaded_image image_loader::do_load_raw(const void * pixels, vk::Extent3D extent,
},
name + " (staging)"};
memcpy(staging_buffer.map(), pixels, byte_size);
if (premultiply)
premultiply_alpha(staging_buffer.map(), pixels, extent, format);
else
memcpy(staging_buffer.map(), pixels, byte_size);
staging_buffer.unmap();
// Allocate image
@@ -544,6 +593,7 @@ loaded_image image_loader::do_load_raw(const void * pixels, vk::Extent3D extent,
.extent = extent,
.num_mipmaps = num_mipmaps,
.image_view_type = image_view_type,
.is_alpha_premultiplied = premultiply,
};
}
@@ -609,6 +659,7 @@ loaded_image image_loader::do_load_ktx(std::span<const std::byte> bytes, bool sr
.extent{vk::Extent3D(texture->baseWidth, texture->baseHeight, texture->baseDepth)},
.num_mipmaps = texture->numLevels,
.image_view_type = vk::ImageViewType(vk_texture.viewType),
.is_alpha_premultiplied = ktxTexture2_GetPremultipliedAlpha(texture),
};
}
@@ -617,42 +668,7 @@ static constexpr bool starts_with(std::span<const std::byte> data, std::span<con
return data.size() >= prefix.size() && !memcmp(data.data(), prefix.data(), prefix.size());
}
template <typename T>
void premultiply_alpha_aux(T * pixels, float alpha_scale, int width, int height)
{
for (int i = 0, n = width * height; i < n; i++)
{
float alpha = pixels[4 * i + 3] * alpha_scale;
pixels[4 * i + 0] = pixels[4 * i + 0] * alpha;
pixels[4 * i + 1] = pixels[4 * i + 1] * alpha;
pixels[4 * i + 2] = pixels[4 * i + 2] * alpha;
}
}
static void premultiply_alpha(void * pixels, vk::Extent3D extent, vk::Format format)
{
switch (format)
{
case vk::Format::eR8G8B8A8Srgb: // TODO: sRGB
case vk::Format::eR8G8B8A8Unorm:
premultiply_alpha_aux<uint8_t>((uint8_t *)pixels, 1. / 255., extent.width, extent.height);
break;
case vk::Format::eR16G16B16A16Unorm:
premultiply_alpha_aux<uint16_t>((uint16_t *)pixels, 1. / 65535., extent.width, extent.height);
break;
case vk::Format::eR32G32B32A32Sfloat:
premultiply_alpha_aux<float>((float *)pixels, 1, extent.width, extent.height);
break;
default:
break;
}
}
loaded_image image_loader::do_load_image(std::span<const std::byte> bytes, bool srgb, const std::string & name)
loaded_image image_loader::do_load_image(std::span<const std::byte> bytes, bool srgb, const std::string & name, bool premultiply)
{
const stbi_uc * image_data = (const stbi_uc *)bytes.data();
size_t image_size = bytes.size();
@@ -691,13 +707,11 @@ loaded_image image_loader::do_load_image(std::span<const std::byte> bytes, bool
.depth = 1,
};
premultiply_alpha(pixels.get(), extent, format);
return do_load_raw(pixels.get(), extent, format, name);
return do_load_raw(pixels.get(), extent, format, name, premultiply);
}
// Load a PNG/JPEG/KTX2 file
loaded_image image_loader::load(std::span<const std::byte> bytes, bool srgb, const std::string & name)
loaded_image image_loader::load(std::span<const std::byte> bytes, bool srgb, const std::string & name, bool premultiply)
{
const uint8_t ktx1_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
const uint8_t ktx2_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
@@ -705,14 +719,14 @@ loaded_image image_loader::load(std::span<const std::byte> bytes, bool srgb, con
if (starts_with(bytes, ktx1_magic) || starts_with(bytes, ktx2_magic))
return do_load_ktx(bytes, srgb, name);
else
return do_load_image(bytes, srgb, name);
return do_load_image(bytes, srgb, name, premultiply);
}
// Load raw pixel data
loaded_image image_loader::load(const void * pixels, size_t size, vk::Extent3D extent, vk::Format format, const std::string & name)
loaded_image image_loader::load(const void * pixels, size_t size, vk::Extent3D extent, vk::Format format, const std::string & name, bool premultiply)
{
if (size < extent.width * extent.height * extent.depth * bytes_per_pixel(format))
throw std::invalid_argument("size");
return do_load_raw(pixels, extent, format, name);
return do_load_raw(pixels, extent, format, name, premultiply);
}