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

@@ -101,8 +101,8 @@ pp::foundation::Status RecordingCommandContext::begin_render_pass(
}
active_target_ = target.color_desc();
if (!active_target_.render_target) {
return pp::foundation::Status::invalid_argument("render target texture must be flagged as render_target");
if (!has_texture_usage(active_target_.usage, TextureUsage::render_target)) {
return pp::foundation::Status::invalid_argument("render target texture must allow render_target usage");
}
const auto size_status = texture_byte_size(active_target_);
@@ -256,6 +256,10 @@ pp::foundation::Status RecordingCommandContext::bind_texture(
}
const auto desc = texture.desc();
if (!has_texture_usage(desc.usage, TextureUsage::sampled)) {
return pp::foundation::Status::invalid_argument("bound texture must allow sampled usage");
}
const auto size_status = texture_byte_size(desc);
if (!size_status.ok()) {
return size_status.status();
@@ -351,6 +355,10 @@ pp::foundation::Status RecordingCommandContext::read_texture(
}
const auto desc = texture.desc();
if (!has_texture_usage(desc.usage, TextureUsage::readback_source)) {
return pp::foundation::Status::invalid_argument("readback texture must allow readback_source usage");
}
const auto bytes = readback_byte_size(desc, region);
if (!bytes) {
return bytes.status();
@@ -379,6 +387,10 @@ pp::foundation::Status RecordingCommandContext::upload_texture(
}
const auto desc = texture.desc();
if (!has_texture_usage(desc.usage, TextureUsage::upload_destination)) {
return pp::foundation::Status::invalid_argument("texture upload destination must allow upload_destination usage");
}
const auto bytes = readback_byte_size(desc, region);
if (!bytes) {
return bytes.status();
@@ -560,6 +572,11 @@ const char* RecordingRenderDevice::backend_name() const noexcept
pp::foundation::Result<std::unique_ptr<ITexture2D>> RecordingRenderDevice::create_texture(
TextureDesc desc) noexcept
{
const auto desc_status = validate_texture_desc(desc);
if (!desc_status.ok()) {
return pp::foundation::Result<std::unique_ptr<ITexture2D>>::failure(desc_status);
}
const auto bytes = texture_byte_size(desc);
if (!bytes.ok()) {
return pp::foundation::Result<std::unique_ptr<ITexture2D>>::failure(bytes.status());
@@ -571,9 +588,14 @@ pp::foundation::Result<std::unique_ptr<ITexture2D>> RecordingRenderDevice::creat
pp::foundation::Result<std::unique_ptr<IRenderTarget>> RecordingRenderDevice::create_render_target(
TextureDesc color_desc) noexcept
{
if (!color_desc.render_target) {
if (!has_texture_usage(color_desc.usage, TextureUsage::render_target)) {
return pp::foundation::Result<std::unique_ptr<IRenderTarget>>::failure(
pp::foundation::Status::invalid_argument("render target texture must be flagged as render_target"));
pp::foundation::Status::invalid_argument("render target texture must allow render_target usage"));
}
const auto desc_status = validate_texture_desc(color_desc);
if (!desc_status.ok()) {
return pp::foundation::Result<std::unique_ptr<IRenderTarget>>::failure(desc_status);
}
const auto bytes = texture_byte_size(color_desc);

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");
}

View File

@@ -22,6 +22,34 @@ enum class TextureFormat : std::uint8_t {
depth24_stencil8,
};
enum class TextureUsage : std::uint32_t {
none = 0,
sampled = 1U << 0U,
render_target = 1U << 1U,
upload_destination = 1U << 2U,
readback_source = 1U << 3U,
copy_source = 1U << 4U,
copy_destination = 1U << 5U,
};
[[nodiscard]] constexpr TextureUsage operator|(TextureUsage lhs, TextureUsage rhs) noexcept
{
return static_cast<TextureUsage>(
static_cast<std::uint32_t>(lhs) | static_cast<std::uint32_t>(rhs));
}
[[nodiscard]] constexpr TextureUsage operator&(TextureUsage lhs, TextureUsage rhs) noexcept
{
return static_cast<TextureUsage>(
static_cast<std::uint32_t>(lhs) & static_cast<std::uint32_t>(rhs));
}
constexpr TextureUsage& operator|=(TextureUsage& lhs, TextureUsage rhs) noexcept
{
lhs = lhs | rhs;
return lhs;
}
struct Extent2D {
std::uint32_t width = 0;
std::uint32_t height = 0;
@@ -30,7 +58,11 @@ struct Extent2D {
struct TextureDesc {
Extent2D extent;
TextureFormat format = TextureFormat::rgba8;
bool render_target = false;
TextureUsage usage = TextureUsage::sampled
| TextureUsage::upload_destination
| TextureUsage::readback_source
| TextureUsage::copy_source
| TextureUsage::copy_destination;
};
struct ReadbackRegion {
@@ -279,7 +311,10 @@ public:
};
[[nodiscard]] std::uint32_t bytes_per_pixel(TextureFormat format) noexcept;
[[nodiscard]] bool has_texture_usage(TextureUsage usage, TextureUsage required) noexcept;
[[nodiscard]] pp::foundation::Status validate_extent(Extent2D extent) noexcept;
[[nodiscard]] pp::foundation::Status validate_texture_usage(TextureUsage usage) noexcept;
[[nodiscard]] pp::foundation::Status validate_texture_desc(TextureDesc desc) noexcept;
[[nodiscard]] pp::foundation::Status validate_viewport(Viewport viewport, Extent2D target_extent) noexcept;
[[nodiscard]] pp::foundation::Status validate_scissor(ScissorRect scissor, Extent2D target_extent) noexcept;
[[nodiscard]] pp::foundation::Status validate_render_pass_desc(RenderPassDesc desc) noexcept;