Add renderer texture usage contract

This commit is contained in:
2026-06-02 16:42:53 +02:00
parent 75dd5cfdc9
commit 901aff1051
7 changed files with 345 additions and 68 deletions

View File

@@ -47,6 +47,13 @@ std::uint32_t bytes_per_pixel(TextureFormat format) noexcept
return 0;
}
bool has_texture_usage(TextureUsage usage, TextureUsage required) noexcept
{
const auto usage_bits = static_cast<std::uint32_t>(usage);
const auto required_bits = static_cast<std::uint32_t>(required);
return required_bits != 0U && (usage_bits & required_bits) == required_bits;
}
pp::foundation::Status validate_extent(Extent2D extent) noexcept
{
if (extent.width == 0 || extent.height == 0) {
@@ -60,19 +67,50 @@ pp::foundation::Status validate_extent(Extent2D extent) noexcept
return pp::foundation::Status::success();
}
pp::foundation::Result<std::uint64_t> texture_byte_size(TextureDesc desc) noexcept
pp::foundation::Status validate_texture_usage(TextureUsage usage) noexcept
{
constexpr auto allowed_usage = TextureUsage::sampled
| TextureUsage::render_target
| TextureUsage::upload_destination
| TextureUsage::readback_source
| TextureUsage::copy_source
| TextureUsage::copy_destination;
const auto usage_bits = static_cast<std::uint32_t>(usage);
const auto allowed_bits = static_cast<std::uint32_t>(allowed_usage);
if (usage_bits == 0U) {
return pp::foundation::Status::invalid_argument("texture usage must not be empty");
}
if ((usage_bits & ~allowed_bits) != 0U) {
return pp::foundation::Status::invalid_argument("texture usage contains unsupported flags");
}
return pp::foundation::Status::success();
}
pp::foundation::Status validate_texture_desc(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);
return extent_status;
}
if (bytes_per_pixel(desc.format) == 0U) {
return pp::foundation::Status::invalid_argument("texture format is not supported");
}
return validate_texture_usage(desc.usage);
}
pp::foundation::Result<std::uint64_t> texture_byte_size(TextureDesc desc) noexcept
{
const auto desc_status = validate_texture_desc(desc);
if (!desc_status.ok()) {
return pp::foundation::Result<std::uint64_t>::failure(desc_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) {
@@ -481,11 +519,16 @@ pp::foundation::Result<std::uint64_t> readback_byte_size(TextureDesc desc, Readb
pp::foundation::Result<std::uint64_t> frame_capture_byte_size(TextureDesc desc) noexcept
{
if (!desc.render_target) {
if (!has_texture_usage(desc.usage, TextureUsage::render_target)) {
return pp::foundation::Result<std::uint64_t>::failure(
pp::foundation::Status::invalid_argument("frame capture source must be a render target"));
}
if (!has_texture_usage(desc.usage, TextureUsage::readback_source)) {
return pp::foundation::Result<std::uint64_t>::failure(
pp::foundation::Status::invalid_argument("frame capture source must allow readback"));
}
return texture_byte_size(desc);
}
@@ -495,6 +538,14 @@ pp::foundation::Status validate_texture_copy_descs(
TextureDesc destination,
ReadbackRegion destination_region) noexcept
{
if (!has_texture_usage(source.usage, TextureUsage::copy_source)) {
return pp::foundation::Status::invalid_argument("texture copy source must allow copy_source usage");
}
if (!has_texture_usage(destination.usage, TextureUsage::copy_destination)) {
return pp::foundation::Status::invalid_argument("texture copy destination must allow copy_destination usage");
}
if (source.format != destination.format) {
return pp::foundation::Status::invalid_argument("texture copy endpoints must use matching formats");
}
@@ -524,10 +575,19 @@ pp::foundation::Status validate_blit_filter(BlitFilter filter) noexcept
pp::foundation::Status validate_blit_descs(TextureDesc source, TextureDesc destination) noexcept
{
if (!source.render_target || !destination.render_target) {
if (!has_texture_usage(source.usage, TextureUsage::render_target)
|| !has_texture_usage(destination.usage, TextureUsage::render_target)) {
return pp::foundation::Status::invalid_argument("blit endpoints must be render targets");
}
if (!has_texture_usage(source.usage, TextureUsage::copy_source)) {
return pp::foundation::Status::invalid_argument("blit source must allow copy_source usage");
}
if (!has_texture_usage(destination.usage, TextureUsage::copy_destination)) {
return pp::foundation::Status::invalid_argument("blit destination must allow copy_destination usage");
}
if (source.format != destination.format) {
return pp::foundation::Status::invalid_argument("blit endpoints must use matching texture formats");
}