Add renderer scissor state contract

This commit is contained in:
2026-06-02 15:46:03 +02:00
parent 5226746c1a
commit 9a7e1c4def
9 changed files with 215 additions and 65 deletions

View File

@@ -34,6 +34,7 @@ using pp::renderer::RecordingRenderDevice;
using pp::renderer::RecordingRenderTarget;
using pp::renderer::RecordingShaderProgram;
using pp::renderer::RecordingTexture2D;
using pp::renderer::ScissorRect;
using pp::renderer::ShaderProgramDesc;
using pp::renderer::ShaderStageSource;
using pp::renderer::TextureDesc;
@@ -57,6 +58,7 @@ using pp::renderer::validate_blend_op;
using pp::renderer::validate_blend_state;
using pp::renderer::validate_mesh_desc;
using pp::renderer::validate_readback_region;
using pp::renderer::validate_scissor;
using pp::renderer::validate_shader_catalog;
using pp::renderer::validate_shader_program_desc;
using pp::renderer::validate_texture_slot;
@@ -150,6 +152,19 @@ public:
return validate_viewport(viewport, Extent2D { .width = 64, .height = 32 });
}
[[nodiscard]] pp::foundation::Status set_scissor(ScissorRect scissor) noexcept override
{
if (!in_render_pass) {
return pp::foundation::Status::invalid_argument("render pass has not begun");
}
const auto status = validate_scissor(scissor, Extent2D { .width = 64, .height = 32 });
if (!status.ok()) {
return status;
}
last_scissor = scissor;
return pp::foundation::Status::success();
}
[[nodiscard]] pp::foundation::Status bind_shader(IShaderProgram& shader) noexcept override
{
shader_name = shader.debug_name();
@@ -286,6 +301,7 @@ public:
bool in_render_pass = false;
const char* shader_name = nullptr;
ScissorRect last_scissor {};
BlendState last_blend_state {};
std::uint32_t last_texture_slot = 0;
std::uint64_t last_texture_bytes = 0;
@@ -502,6 +518,11 @@ void validates_viewports_and_mesh_descriptors(pp::tests::Harness& h)
Viewport { .x = 0, .y = 0, .width = 64, .height = 32, .min_depth = 0.0F, .max_depth = 1.0F },
target)
.ok());
PP_EXPECT(h, validate_scissor(ScissorRect {}, target).ok());
PP_EXPECT(h, validate_scissor(ScissorRect { .enabled = true, .x = 0, .y = 0, .width = 64, .height = 32 }, target)
.ok());
PP_EXPECT(h, validate_scissor(ScissorRect { .enabled = true, .x = 63, .y = 31, .width = 1, .height = 1 }, target)
.ok());
PP_EXPECT(h, validate_mesh_desc(MeshDesc { .vertex_count = 3, .topology = PrimitiveTopology::triangles }).ok());
PP_EXPECT(h, primitive_topology_name(PrimitiveTopology::lines) == std::string_view("lines"));
@@ -511,6 +532,9 @@ void validates_viewports_and_mesh_descriptors(pp::tests::Harness& h)
Viewport { .x = 0, .y = 0, .width = 1, .height = 1, .min_depth = 0.75F, .max_depth = 0.25F },
target);
const auto empty_mesh = validate_mesh_desc(MeshDesc {});
const auto negative_scissor = validate_scissor(ScissorRect { .enabled = true, .x = -1, .y = 0, .width = 1, .height = 1 }, target);
const auto empty_scissor = validate_scissor(ScissorRect { .enabled = true, .x = 0, .y = 0, .width = 0, .height = 1 }, target);
const auto scissor_overrun = validate_scissor(ScissorRect { .enabled = true, .x = 63, .y = 0, .width = 2, .height = 1 }, target);
PP_EXPECT(h, !negative_origin.ok());
PP_EXPECT(h, negative_origin.code == StatusCode::invalid_argument);
@@ -520,6 +544,12 @@ void validates_viewports_and_mesh_descriptors(pp::tests::Harness& h)
PP_EXPECT(h, bad_depth.code == StatusCode::out_of_range);
PP_EXPECT(h, !empty_mesh.ok());
PP_EXPECT(h, empty_mesh.code == StatusCode::invalid_argument);
PP_EXPECT(h, !negative_scissor.ok());
PP_EXPECT(h, negative_scissor.code == StatusCode::invalid_argument);
PP_EXPECT(h, !empty_scissor.ok());
PP_EXPECT(h, empty_scissor.code == StatusCode::invalid_argument);
PP_EXPECT(h, !scissor_overrun.ok());
PP_EXPECT(h, scissor_overrun.code == StatusCode::out_of_range);
PP_EXPECT(h, validate_texture_slot(0).ok());
PP_EXPECT(h, validate_texture_slot(max_texture_slots - 1U).ok());
@@ -655,6 +685,7 @@ void renderer_interfaces_support_backend_neutral_dispatch(pp::tests::Harness& h)
auto& context = device.immediate_context();
PP_EXPECT(h, context.begin_render_pass(target, ClearColor { .r = 0.1F, .g = 0.2F, .b = 0.3F, .a = 1.0F }).ok());
PP_EXPECT(h, context.set_viewport(Viewport { .x = 0, .y = 0, .width = 64, .height = 32 }).ok());
PP_EXPECT(h, context.set_scissor(ScissorRect { .enabled = true, .x = 4, .y = 5, .width = 16, .height = 8 }).ok());
PP_EXPECT(h, context.set_blend_state(BlendState {
.enabled = true,
.source_color = BlendFactor::source_alpha,
@@ -689,6 +720,9 @@ void renderer_interfaces_support_backend_neutral_dispatch(pp::tests::Harness& h)
BlitFilter::linear)
.ok());
PP_EXPECT(h, device.context.shader_name == std::string_view("fake-shader"));
PP_EXPECT(h, device.context.last_scissor.enabled);
PP_EXPECT(h, device.context.last_scissor.x == 4);
PP_EXPECT(h, device.context.last_scissor.height == 8U);
PP_EXPECT(h, device.context.last_blend_state.enabled);
PP_EXPECT(h, device.context.last_blend_state.source_color == BlendFactor::source_alpha);
PP_EXPECT(h, device.context.last_blend_state.destination_color == BlendFactor::one_minus_source_alpha);
@@ -731,6 +765,7 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
auto& context = device.immediate_context();
PP_EXPECT(h, context.begin_render_pass(target, ClearColor { .r = 0.2F, .g = 0.3F, .b = 0.4F, .a = 1.0F }).ok());
PP_EXPECT(h, context.set_viewport(Viewport { .x = 0, .y = 0, .width = 64, .height = 32 }).ok());
PP_EXPECT(h, context.set_scissor(ScissorRect { .enabled = true, .x = 4, .y = 6, .width = 16, .height = 8 }).ok());
PP_EXPECT(h, context.set_blend_state(BlendState {
.enabled = true,
.source_color = BlendFactor::source_alpha,
@@ -746,7 +781,7 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
context.end_render_pass();
const auto commands = device.commands();
PP_EXPECT(h, commands.size() == 9U);
PP_EXPECT(h, commands.size() == 10U);
PP_EXPECT(h, commands[0].kind == RecordedRenderCommandKind::trace_marker);
PP_EXPECT(h, commands[0].component == std::string_view("renderer"));
PP_EXPECT(h, commands[0].name == std::string_view("frame"));
@@ -755,21 +790,26 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
PP_EXPECT(h, commands[1].clear_color.a == 1.0F);
PP_EXPECT(h, commands[2].kind == RecordedRenderCommandKind::set_viewport);
PP_EXPECT(h, commands[2].viewport.height == 32U);
PP_EXPECT(h, commands[3].kind == RecordedRenderCommandKind::set_blend_state);
PP_EXPECT(h, commands[3].blend_state.enabled);
PP_EXPECT(h, commands[3].blend_state.destination_color == BlendFactor::one_minus_source_alpha);
PP_EXPECT(h, recorded_render_command_kind_name(commands[3].kind) == std::string_view("set_blend_state"));
PP_EXPECT(h, commands[4].kind == RecordedRenderCommandKind::bind_shader);
PP_EXPECT(h, commands[4].name == std::string_view("recorded-shader"));
PP_EXPECT(h, commands[5].kind == RecordedRenderCommandKind::bind_texture);
PP_EXPECT(h, commands[5].texture_slot == 1U);
PP_EXPECT(h, commands[5].texture_desc.extent.height == 32U);
PP_EXPECT(h, recorded_render_command_kind_name(commands[5].kind) == std::string_view("bind_texture"));
PP_EXPECT(h, commands[6].kind == RecordedRenderCommandKind::bind_mesh);
PP_EXPECT(h, commands[6].mesh_desc.vertex_count == 3U);
PP_EXPECT(h, commands[7].kind == RecordedRenderCommandKind::draw);
PP_EXPECT(h, commands[8].kind == RecordedRenderCommandKind::end_render_pass);
PP_EXPECT(h, recorded_render_command_kind_name(commands[7].kind) == std::string_view("draw"));
PP_EXPECT(h, commands[3].kind == RecordedRenderCommandKind::set_scissor);
PP_EXPECT(h, commands[3].scissor.enabled);
PP_EXPECT(h, commands[3].scissor.x == 4);
PP_EXPECT(h, commands[3].scissor.height == 8U);
PP_EXPECT(h, recorded_render_command_kind_name(commands[3].kind) == std::string_view("set_scissor"));
PP_EXPECT(h, commands[4].kind == RecordedRenderCommandKind::set_blend_state);
PP_EXPECT(h, commands[4].blend_state.enabled);
PP_EXPECT(h, commands[4].blend_state.destination_color == BlendFactor::one_minus_source_alpha);
PP_EXPECT(h, recorded_render_command_kind_name(commands[4].kind) == std::string_view("set_blend_state"));
PP_EXPECT(h, commands[5].kind == RecordedRenderCommandKind::bind_shader);
PP_EXPECT(h, commands[5].name == std::string_view("recorded-shader"));
PP_EXPECT(h, commands[6].kind == RecordedRenderCommandKind::bind_texture);
PP_EXPECT(h, commands[6].texture_slot == 1U);
PP_EXPECT(h, commands[6].texture_desc.extent.height == 32U);
PP_EXPECT(h, recorded_render_command_kind_name(commands[6].kind) == std::string_view("bind_texture"));
PP_EXPECT(h, commands[7].kind == RecordedRenderCommandKind::bind_mesh);
PP_EXPECT(h, commands[7].mesh_desc.vertex_count == 3U);
PP_EXPECT(h, commands[8].kind == RecordedRenderCommandKind::draw);
PP_EXPECT(h, commands[9].kind == RecordedRenderCommandKind::end_render_pass);
PP_EXPECT(h, recorded_render_command_kind_name(commands[8].kind) == std::string_view("draw"));
PP_EXPECT(h, context.upload_texture(
texture,
@@ -777,12 +817,12 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
upload_bytes)
.ok());
const auto commands_after_upload = device.commands();
PP_EXPECT(h, commands_after_upload.size() == 10U);
PP_EXPECT(h, commands_after_upload[9].kind == RecordedRenderCommandKind::upload_texture);
PP_EXPECT(h, commands_after_upload[9].texture_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_upload[9].readback_region.x == 4U);
PP_EXPECT(h, commands_after_upload[9].upload_bytes == 96U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_upload[9].kind) == std::string_view("upload_texture"));
PP_EXPECT(h, commands_after_upload.size() == 11U);
PP_EXPECT(h, commands_after_upload[10].kind == RecordedRenderCommandKind::upload_texture);
PP_EXPECT(h, commands_after_upload[10].texture_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_upload[10].readback_region.x == 4U);
PP_EXPECT(h, commands_after_upload[10].upload_bytes == 96U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_upload[10].kind) == std::string_view("upload_texture"));
PP_EXPECT(h, context.read_texture(
texture,
@@ -790,22 +830,22 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
readback_buffer)
.ok());
const auto commands_after_readback = device.commands();
PP_EXPECT(h, commands_after_readback.size() == 11U);
PP_EXPECT(h, commands_after_readback[10].kind == RecordedRenderCommandKind::read_texture);
PP_EXPECT(h, commands_after_readback[10].texture_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_readback[10].readback_region.x == 4U);
PP_EXPECT(h, commands_after_readback[10].readback_region.height == 3U);
PP_EXPECT(h, commands_after_readback[10].readback_bytes == 96U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_readback[10].kind) == std::string_view("read_texture"));
PP_EXPECT(h, commands_after_readback.size() == 12U);
PP_EXPECT(h, commands_after_readback[11].kind == RecordedRenderCommandKind::read_texture);
PP_EXPECT(h, commands_after_readback[11].texture_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_readback[11].readback_region.x == 4U);
PP_EXPECT(h, commands_after_readback[11].readback_region.height == 3U);
PP_EXPECT(h, commands_after_readback[11].readback_bytes == 96U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_readback[11].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() == 12U);
PP_EXPECT(h, commands_after_capture[11].kind == RecordedRenderCommandKind::capture_frame);
PP_EXPECT(h, commands_after_capture[11].target_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_capture[11].target_desc.extent.height == 32U);
PP_EXPECT(h, commands_after_capture[11].capture_bytes == 8192U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_capture[11].kind) == std::string_view("capture_frame"));
PP_EXPECT(h, commands_after_capture.size() == 13U);
PP_EXPECT(h, commands_after_capture[12].kind == RecordedRenderCommandKind::capture_frame);
PP_EXPECT(h, commands_after_capture[12].target_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_capture[12].target_desc.extent.height == 32U);
PP_EXPECT(h, commands_after_capture[12].capture_bytes == 8192U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_capture[12].kind) == std::string_view("capture_frame"));
PP_EXPECT(h, context.blit_render_target(
target,
@@ -815,16 +855,16 @@ void recording_renderer_records_valid_command_sequences(pp::tests::Harness& h)
BlitFilter::linear)
.ok());
const auto commands_after_blit = device.commands();
PP_EXPECT(h, commands_after_blit.size() == 13U);
PP_EXPECT(h, commands_after_blit[12].kind == RecordedRenderCommandKind::blit_render_target);
PP_EXPECT(h, commands_after_blit[12].source_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_blit[12].destination_desc.extent.height == 32U);
PP_EXPECT(h, commands_after_blit[12].source_region.width == 16U);
PP_EXPECT(h, commands_after_blit[12].destination_region.x == 2U);
PP_EXPECT(h, commands_after_blit[12].blit_filter == BlitFilter::linear);
PP_EXPECT(h, commands_after_blit[12].blit_source_bytes == 512U);
PP_EXPECT(h, commands_after_blit[12].blit_destination_bytes == 128U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_blit[12].kind) == std::string_view("blit_render_target"));
PP_EXPECT(h, commands_after_blit.size() == 14U);
PP_EXPECT(h, commands_after_blit[13].kind == RecordedRenderCommandKind::blit_render_target);
PP_EXPECT(h, commands_after_blit[13].source_desc.extent.width == 64U);
PP_EXPECT(h, commands_after_blit[13].destination_desc.extent.height == 32U);
PP_EXPECT(h, commands_after_blit[13].source_region.width == 16U);
PP_EXPECT(h, commands_after_blit[13].destination_region.x == 2U);
PP_EXPECT(h, commands_after_blit[13].blit_filter == BlitFilter::linear);
PP_EXPECT(h, commands_after_blit[13].blit_source_bytes == 512U);
PP_EXPECT(h, commands_after_blit[13].blit_destination_bytes == 128U);
PP_EXPECT(h, recorded_render_command_kind_name(commands_after_blit[13].kind) == std::string_view("blit_render_target"));
device.clear();
PP_EXPECT(h, device.commands().empty());
@@ -870,6 +910,10 @@ void recording_renderer_rejects_invalid_command_order_and_targets(pp::tests::Har
PP_EXPECT(h, !blend_before_begin.ok());
PP_EXPECT(h, blend_before_begin.code == StatusCode::invalid_argument);
const auto scissor_before_begin = context.set_scissor(ScissorRect {});
PP_EXPECT(h, !scissor_before_begin.ok());
PP_EXPECT(h, scissor_before_begin.code == StatusCode::invalid_argument);
const auto invalid_target = context.begin_render_pass(non_render_target, ClearColor {});
PP_EXPECT(h, !invalid_target.ok());
PP_EXPECT(h, invalid_target.code == StatusCode::invalid_argument);
@@ -919,6 +963,24 @@ void recording_renderer_rejects_invalid_command_order_and_targets(pp::tests::Har
.destination_color = BlendFactor::one_minus_source_alpha,
})
.ok());
PP_EXPECT(h, context.set_scissor(ScissorRect {
.enabled = true,
.x = 0,
.y = 0,
.width = 16,
.height = 8,
})
.ok());
const auto invalid_scissor = context.set_scissor(ScissorRect {
.enabled = true,
.x = 31,
.y = 15,
.width = 2,
.height = 1,
});
PP_EXPECT(h, !invalid_scissor.ok());
PP_EXPECT(h, invalid_scissor.code == StatusCode::out_of_range);
const auto draw_without_bindings = context.draw();
PP_EXPECT(h, !draw_without_bindings.ok());
@@ -950,6 +1012,10 @@ void recording_renderer_rejects_invalid_command_order_and_targets(pp::tests::Har
PP_EXPECT(h, !blend_after_end.ok());
PP_EXPECT(h, blend_after_end.code == StatusCode::invalid_argument);
const auto scissor_after_end = context.set_scissor(ScissorRect {});
PP_EXPECT(h, !scissor_after_end.ok());
PP_EXPECT(h, scissor_after_end.code == StatusCode::invalid_argument);
const auto bind_texture_after_end = context.bind_texture(0, texture);
PP_EXPECT(h, !bind_texture_after_end.ok());
PP_EXPECT(h, bind_texture_after_end.code == StatusCode::invalid_argument);