Add renderer texture upload contract
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "test_harness.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <string_view>
|
||||
|
||||
using pp::foundation::StatusCode;
|
||||
@@ -168,6 +169,22 @@ public:
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
[[nodiscard]] pp::foundation::Status upload_texture(
|
||||
pp::renderer::ITexture2D& texture,
|
||||
ReadbackRegion region,
|
||||
std::span<const std::byte> rgba_or_channel_bytes) noexcept override
|
||||
{
|
||||
const auto bytes = readback_byte_size(texture.desc(), region);
|
||||
if (!bytes) {
|
||||
return bytes.status();
|
||||
}
|
||||
if (rgba_or_channel_bytes.size() != bytes.value()) {
|
||||
return pp::foundation::Status::invalid_argument("texture upload byte size does not match the region");
|
||||
}
|
||||
last_upload_bytes = bytes.value();
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
[[nodiscard]] pp::foundation::Status capture_frame(
|
||||
IRenderTarget& target,
|
||||
pp::renderer::IReadbackBuffer& destination) noexcept override
|
||||
@@ -190,6 +207,7 @@ public:
|
||||
|
||||
bool in_render_pass = false;
|
||||
const char* shader_name = nullptr;
|
||||
std::uint64_t last_upload_bytes = 0;
|
||||
std::uint64_t last_readback_bytes = 0;
|
||||
std::uint64_t last_capture_bytes = 0;
|
||||
};
|
||||
@@ -463,6 +481,7 @@ void renderer_interfaces_support_backend_neutral_dispatch(pp::tests::Harness& h)
|
||||
FakeRenderTarget target;
|
||||
FakeTexture texture;
|
||||
FakeReadbackBuffer readback_buffer(64U * 32U * 4U);
|
||||
const std::array<std::byte, 80> upload_bytes {};
|
||||
FakeShaderProgram shader;
|
||||
FakeMesh mesh;
|
||||
|
||||
@@ -482,6 +501,11 @@ void renderer_interfaces_support_backend_neutral_dispatch(pp::tests::Harness& h)
|
||||
const auto draw_after_end = context.draw();
|
||||
PP_EXPECT(h, !draw_after_end.ok());
|
||||
PP_EXPECT(h, draw_after_end.code == StatusCode::invalid_argument);
|
||||
PP_EXPECT(h, context.upload_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 2, .y = 3, .width = 4, .height = 5 },
|
||||
upload_bytes)
|
||||
.ok());
|
||||
PP_EXPECT(h, context.read_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 2, .y = 3, .width = 4, .height = 5 },
|
||||
@@ -489,6 +513,7 @@ void renderer_interfaces_support_backend_neutral_dispatch(pp::tests::Harness& h)
|
||||
.ok());
|
||||
PP_EXPECT(h, context.capture_frame(target, readback_buffer).ok());
|
||||
PP_EXPECT(h, device.context.shader_name == std::string_view("fake-shader"));
|
||||
PP_EXPECT(h, device.context.last_upload_bytes == 80U);
|
||||
PP_EXPECT(h, device.context.last_readback_bytes == 80U);
|
||||
PP_EXPECT(h, device.context.last_capture_bytes == 8192U);
|
||||
}
|
||||
@@ -502,6 +527,7 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
|
||||
.render_target = true,
|
||||
});
|
||||
RecordingReadbackBuffer readback_buffer(64U * 32U * 4U);
|
||||
const std::array<std::byte, 96> upload_bytes {};
|
||||
RecordingRenderTarget target(TextureDesc {
|
||||
.extent = Extent2D { .width = 64, .height = 32 },
|
||||
.format = TextureFormat::rgba8,
|
||||
@@ -539,28 +565,41 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
|
||||
PP_EXPECT(h, commands[6].kind == RecordedRenderCommandKind::end_render_pass);
|
||||
PP_EXPECT(h, recorded_render_command_kind_name(commands[5].kind) == std::string_view("draw"));
|
||||
|
||||
PP_EXPECT(h, context.upload_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 4, .y = 5, .width = 8, .height = 3 },
|
||||
upload_bytes)
|
||||
.ok());
|
||||
const auto commands_after_upload = device.commands();
|
||||
PP_EXPECT(h, commands_after_upload.size() == 8U);
|
||||
PP_EXPECT(h, commands_after_upload[7].kind == RecordedRenderCommandKind::upload_texture);
|
||||
PP_EXPECT(h, commands_after_upload[7].texture_desc.extent.width == 64U);
|
||||
PP_EXPECT(h, commands_after_upload[7].readback_region.x == 4U);
|
||||
PP_EXPECT(h, commands_after_upload[7].upload_bytes == 96U);
|
||||
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_upload[7].kind) == std::string_view("upload_texture"));
|
||||
|
||||
PP_EXPECT(h, context.read_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 4, .y = 5, .width = 8, .height = 3 },
|
||||
readback_buffer)
|
||||
.ok());
|
||||
const auto commands_after_readback = device.commands();
|
||||
PP_EXPECT(h, commands_after_readback.size() == 8U);
|
||||
PP_EXPECT(h, commands_after_readback[7].kind == RecordedRenderCommandKind::read_texture);
|
||||
PP_EXPECT(h, commands_after_readback[7].texture_desc.extent.width == 64U);
|
||||
PP_EXPECT(h, commands_after_readback[7].readback_region.x == 4U);
|
||||
PP_EXPECT(h, commands_after_readback[7].readback_region.height == 3U);
|
||||
PP_EXPECT(h, commands_after_readback[7].readback_bytes == 96U);
|
||||
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_readback[7].kind) == std::string_view("read_texture"));
|
||||
PP_EXPECT(h, commands_after_readback.size() == 9U);
|
||||
PP_EXPECT(h, commands_after_readback[8].kind == RecordedRenderCommandKind::read_texture);
|
||||
PP_EXPECT(h, commands_after_readback[8].texture_desc.extent.width == 64U);
|
||||
PP_EXPECT(h, commands_after_readback[8].readback_region.x == 4U);
|
||||
PP_EXPECT(h, commands_after_readback[8].readback_region.height == 3U);
|
||||
PP_EXPECT(h, commands_after_readback[8].readback_bytes == 96U);
|
||||
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_readback[8].kind) == std::string_view("read_texture"));
|
||||
|
||||
PP_EXPECT(h, context.capture_frame(target, readback_buffer).ok());
|
||||
const auto commands_after_capture = device.commands();
|
||||
PP_EXPECT(h, commands_after_capture.size() == 9U);
|
||||
PP_EXPECT(h, commands_after_capture[8].kind == RecordedRenderCommandKind::capture_frame);
|
||||
PP_EXPECT(h, commands_after_capture[8].target_desc.extent.width == 64U);
|
||||
PP_EXPECT(h, commands_after_capture[8].target_desc.extent.height == 32U);
|
||||
PP_EXPECT(h, commands_after_capture[8].capture_bytes == 8192U);
|
||||
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_capture[8].kind) == std::string_view("capture_frame"));
|
||||
PP_EXPECT(h, commands_after_capture.size() == 10U);
|
||||
PP_EXPECT(h, commands_after_capture[9].kind == RecordedRenderCommandKind::capture_frame);
|
||||
PP_EXPECT(h, commands_after_capture[9].target_desc.extent.width == 64U);
|
||||
PP_EXPECT(h, commands_after_capture[9].target_desc.extent.height == 32U);
|
||||
PP_EXPECT(h, commands_after_capture[9].capture_bytes == 8192U);
|
||||
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_capture[9].kind) == std::string_view("capture_frame"));
|
||||
|
||||
device.clear();
|
||||
PP_EXPECT(h, device.commands().empty());
|
||||
@@ -586,6 +625,8 @@ void recording_renderer_rejects_invalid_command_order_and_targets(pp::tests::Har
|
||||
});
|
||||
RecordingReadbackBuffer small_readback_buffer(3U);
|
||||
RecordingReadbackBuffer full_readback_buffer(32U * 16U * 4U);
|
||||
const std::array<std::byte, 4> one_pixel_upload {};
|
||||
const std::array<std::byte, 3> undersized_upload {};
|
||||
RecordingShaderProgram shader("strict-shader");
|
||||
RecordingMesh mesh(MeshDesc { .vertex_count = 3, .topology = PrimitiveTopology::triangles });
|
||||
RecordingMesh empty_mesh(MeshDesc {});
|
||||
@@ -601,6 +642,13 @@ void recording_renderer_rejects_invalid_command_order_and_targets(pp::tests::Har
|
||||
PP_EXPECT(h, device.commands().empty());
|
||||
|
||||
PP_EXPECT(h, context.begin_render_pass(target, ClearColor {}).ok());
|
||||
const auto upload_during_render_pass = context.upload_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 0, .y = 0, .width = 1, .height = 1 },
|
||||
one_pixel_upload);
|
||||
PP_EXPECT(h, !upload_during_render_pass.ok());
|
||||
PP_EXPECT(h, upload_during_render_pass.code == StatusCode::invalid_argument);
|
||||
|
||||
const auto read_during_render_pass = context.read_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 0, .y = 0, .width = 1, .height = 1 },
|
||||
@@ -644,6 +692,20 @@ void recording_renderer_rejects_invalid_command_order_and_targets(pp::tests::Har
|
||||
PP_EXPECT(h, !read_outside_bounds.ok());
|
||||
PP_EXPECT(h, read_outside_bounds.code == StatusCode::out_of_range);
|
||||
|
||||
const auto upload_outside_bounds = context.upload_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 31, .y = 15, .width = 2, .height = 1 },
|
||||
one_pixel_upload);
|
||||
PP_EXPECT(h, !upload_outside_bounds.ok());
|
||||
PP_EXPECT(h, upload_outside_bounds.code == StatusCode::out_of_range);
|
||||
|
||||
const auto upload_with_wrong_size = context.upload_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 0, .y = 0, .width = 1, .height = 1 },
|
||||
undersized_upload);
|
||||
PP_EXPECT(h, !upload_with_wrong_size.ok());
|
||||
PP_EXPECT(h, upload_with_wrong_size.code == StatusCode::invalid_argument);
|
||||
|
||||
const auto read_into_small_buffer = context.read_texture(
|
||||
texture,
|
||||
ReadbackRegion { .x = 0, .y = 0, .width = 1, .height = 1 },
|
||||
|
||||
Reference in New Issue
Block a user