Add renderer API validation tests

This commit is contained in:
2026-06-01 00:05:41 +02:00
parent 23eba07901
commit 31322bbd83
9 changed files with 288 additions and 8 deletions

View File

@@ -0,0 +1,105 @@
#include "renderer_api/renderer_api.h"
#include <limits>
namespace pp::renderer {
std::uint32_t bytes_per_pixel(TextureFormat format) noexcept
{
switch (format) {
case TextureFormat::rgba8:
return 4;
case TextureFormat::r8:
return 1;
case TextureFormat::depth24_stencil8:
return 4;
}
return 0;
}
pp::foundation::Status validate_extent(Extent2D extent) noexcept
{
if (extent.width == 0 || extent.height == 0) {
return pp::foundation::Status::invalid_argument("texture extent must be greater than zero");
}
if (extent.width > max_texture_dimension || extent.height > max_texture_dimension) {
return pp::foundation::Status::out_of_range("texture extent exceeds the configured limit");
}
return pp::foundation::Status::success();
}
pp::foundation::Result<std::uint64_t> texture_byte_size(TextureDesc desc) noexcept
{
const auto extent_status = validate_extent(desc.extent);
if (!extent_status.ok()) {
return pp::foundation::Result<std::uint64_t>::failure(extent_status);
}
const auto bpp = static_cast<std::uint64_t>(bytes_per_pixel(desc.format));
if (bpp == 0) {
return pp::foundation::Result<std::uint64_t>::failure(
pp::foundation::Status::invalid_argument("texture format is not supported"));
}
const auto width = static_cast<std::uint64_t>(desc.extent.width);
const auto height = static_cast<std::uint64_t>(desc.extent.height);
if (width > std::numeric_limits<std::uint64_t>::max() / height) {
return pp::foundation::Result<std::uint64_t>::failure(
pp::foundation::Status::out_of_range("texture size overflows uint64"));
}
const auto pixels = width * height;
if (pixels > std::numeric_limits<std::uint64_t>::max() / bpp) {
return pp::foundation::Result<std::uint64_t>::failure(
pp::foundation::Status::out_of_range("texture byte size overflows uint64"));
}
const auto bytes = pixels * bpp;
if (bytes > max_texture_bytes) {
return pp::foundation::Result<std::uint64_t>::failure(
pp::foundation::Status::out_of_range("texture byte size exceeds the configured limit"));
}
return pp::foundation::Result<std::uint64_t>::success(bytes);
}
pp::foundation::Status validate_readback_region(TextureDesc desc, ReadbackRegion region) noexcept
{
const auto extent_status = validate_extent(desc.extent);
if (!extent_status.ok()) {
return extent_status;
}
if (region.width == 0 || region.height == 0) {
return pp::foundation::Status::invalid_argument("readback region must be greater than zero");
}
if (region.x > desc.extent.width || region.y > desc.extent.height) {
return pp::foundation::Status::out_of_range("readback origin is outside the texture");
}
if (region.width > desc.extent.width - region.x || region.height > desc.extent.height - region.y) {
return pp::foundation::Status::out_of_range("readback region exceeds texture bounds");
}
return pp::foundation::Status::success();
}
const char* texture_format_name(TextureFormat format) noexcept
{
switch (format) {
case TextureFormat::rgba8:
return "rgba8";
case TextureFormat::r8:
return "r8";
case TextureFormat::depth24_stencil8:
return "depth24_stencil8";
}
return "unknown";
}
}

View File

@@ -0,0 +1,60 @@
#pragma once
#include "foundation/result.h"
#include <cstdint>
namespace pp::renderer {
constexpr std::uint32_t max_texture_dimension = 32768;
constexpr std::uint64_t max_texture_bytes = 1024ULL * 1024ULL * 1024ULL;
enum class TextureFormat : std::uint8_t {
rgba8,
r8,
depth24_stencil8,
};
struct Extent2D {
std::uint32_t width = 0;
std::uint32_t height = 0;
};
struct TextureDesc {
Extent2D extent;
TextureFormat format = TextureFormat::rgba8;
bool render_target = false;
};
struct ReadbackRegion {
std::uint32_t x = 0;
std::uint32_t y = 0;
std::uint32_t width = 0;
std::uint32_t height = 0;
};
class ITexture2D {
public:
virtual ~ITexture2D() = default;
[[nodiscard]] virtual TextureDesc desc() const noexcept = 0;
};
class IReadbackBuffer {
public:
virtual ~IReadbackBuffer() = default;
[[nodiscard]] virtual std::uint64_t size_bytes() const noexcept = 0;
};
class IRenderTrace {
public:
virtual ~IRenderTrace() = default;
virtual void marker(const char* component, const char* name) noexcept = 0;
};
[[nodiscard]] std::uint32_t bytes_per_pixel(TextureFormat format) noexcept;
[[nodiscard]] pp::foundation::Status validate_extent(Extent2D extent) noexcept;
[[nodiscard]] pp::foundation::Result<std::uint64_t> texture_byte_size(TextureDesc desc) noexcept;
[[nodiscard]] pp::foundation::Status validate_readback_region(TextureDesc desc, ReadbackRegion region) noexcept;
[[nodiscard]] const char* texture_format_name(TextureFormat format) noexcept;
}