Add renderer API validation tests
This commit is contained in:
105
src/renderer_api/renderer_api.cpp
Normal file
105
src/renderer_api/renderer_api.cpp
Normal 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";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user