Route Texture2D through renderer GL

This commit is contained in:
2026-06-03 06:24:56 +02:00
parent 9971b2b7f2
commit ae69f7437f
6 changed files with 959 additions and 69 deletions

View File

@@ -38,6 +38,39 @@ struct RecordedOpenGlBindingCall {
std::uint32_t second = 0;
};
struct RecordedOpenGlTextureImageCall {
std::uint32_t target = 0;
std::int32_t level = 0;
std::int32_t internal_format = 0;
std::int32_t x = 0;
std::int32_t y = 0;
std::int32_t width = 0;
std::int32_t height = 0;
std::int32_t border = 0;
std::uint32_t pixel_format = 0;
std::uint32_t component_type = 0;
const void* data = nullptr;
bool sub_image = false;
};
struct RecordedOpenGlFramebufferAttachmentCall {
std::uint32_t target = 0;
std::uint32_t attachment = 0;
std::uint32_t texture_target = 0;
std::uint32_t texture = 0;
std::int32_t level = 0;
};
struct RecordedOpenGlReadPixelsCall {
std::int32_t x = 0;
std::int32_t y = 0;
std::int32_t width = 0;
std::int32_t height = 0;
std::uint32_t pixel_format = 0;
std::uint32_t component_type = 0;
void* pixels = nullptr;
};
std::vector<RecordedOpenGlStateCall> recorded_state_calls;
std::vector<std::uint32_t> recorded_string_queries;
std::vector<pp::renderer::gl::OpenGlDefaultClear> recorded_clear_calls;
@@ -47,6 +80,18 @@ std::vector<std::uint32_t> recorded_integer_queries;
std::vector<std::uint32_t> recorded_float_queries;
std::vector<std::uint32_t> recorded_active_texture_calls;
std::vector<RecordedOpenGlBindingCall> recorded_binding_calls;
std::vector<std::uint32_t> recorded_generated_texture_counts;
std::vector<std::uint32_t> recorded_deleted_textures;
std::vector<RecordedOpenGlTextureImageCall> recorded_texture_image_calls;
std::vector<std::uint32_t> recorded_mipmap_targets;
std::vector<std::uint32_t> recorded_generated_framebuffer_counts;
std::vector<std::uint32_t> recorded_deleted_framebuffers;
std::vector<RecordedOpenGlFramebufferAttachmentCall> recorded_framebuffer_attachment_calls;
std::vector<std::uint32_t> recorded_framebuffer_status_queries;
std::vector<RecordedOpenGlReadPixelsCall> recorded_read_pixels_calls;
std::uint32_t next_texture_id = 91U;
std::uint32_t next_framebuffer_id = 44U;
std::uint32_t configured_framebuffer_status = 0x8CD5U;
void record_enable(std::uint32_t state) noexcept
{
@@ -236,6 +281,132 @@ void record_bind_sampler(std::uint32_t unit, std::uint32_t sampler) noexcept
});
}
void record_gen_textures(std::uint32_t count, std::uint32_t* ids) noexcept
{
recorded_generated_texture_counts.push_back(count);
for (std::uint32_t i = 0U; i < count; ++i) {
ids[i] = next_texture_id + i;
}
}
void record_delete_textures(std::uint32_t count, const std::uint32_t* ids) noexcept
{
for (std::uint32_t i = 0U; i < count; ++i) {
recorded_deleted_textures.push_back(ids[i]);
}
}
void record_tex_image_2d(
std::uint32_t target,
std::int32_t level,
std::int32_t internal_format,
std::int32_t width,
std::int32_t height,
std::int32_t border,
std::uint32_t pixel_format,
std::uint32_t component_type,
const void* data) noexcept
{
recorded_texture_image_calls.push_back(RecordedOpenGlTextureImageCall {
.target = target,
.level = level,
.internal_format = internal_format,
.width = width,
.height = height,
.border = border,
.pixel_format = pixel_format,
.component_type = component_type,
.data = data,
});
}
void record_tex_sub_image_2d(
std::uint32_t target,
std::int32_t level,
std::int32_t x,
std::int32_t y,
std::int32_t width,
std::int32_t height,
std::uint32_t pixel_format,
std::uint32_t component_type,
const void* data) noexcept
{
recorded_texture_image_calls.push_back(RecordedOpenGlTextureImageCall {
.target = target,
.level = level,
.x = x,
.y = y,
.width = width,
.height = height,
.pixel_format = pixel_format,
.component_type = component_type,
.data = data,
.sub_image = true,
});
}
void record_generate_mipmap(std::uint32_t target) noexcept
{
recorded_mipmap_targets.push_back(target);
}
void record_gen_framebuffers(std::uint32_t count, std::uint32_t* ids) noexcept
{
recorded_generated_framebuffer_counts.push_back(count);
for (std::uint32_t i = 0U; i < count; ++i) {
ids[i] = next_framebuffer_id + i;
}
}
void record_delete_framebuffers(std::uint32_t count, const std::uint32_t* ids) noexcept
{
for (std::uint32_t i = 0U; i < count; ++i) {
recorded_deleted_framebuffers.push_back(ids[i]);
}
}
void record_framebuffer_texture_2d(
std::uint32_t target,
std::uint32_t attachment,
std::uint32_t texture_target,
std::uint32_t texture,
std::int32_t level) noexcept
{
recorded_framebuffer_attachment_calls.push_back(RecordedOpenGlFramebufferAttachmentCall {
.target = target,
.attachment = attachment,
.texture_target = texture_target,
.texture = texture,
.level = level,
});
}
std::uint32_t record_check_framebuffer_status(std::uint32_t target) noexcept
{
recorded_framebuffer_status_queries.push_back(target);
return configured_framebuffer_status;
}
void record_read_pixels(
std::int32_t x,
std::int32_t y,
std::int32_t width,
std::int32_t height,
std::uint32_t pixel_format,
std::uint32_t component_type,
void* pixels) noexcept
{
recorded_read_pixels_calls.push_back(RecordedOpenGlReadPixelsCall {
.x = x,
.y = y,
.width = width,
.height = height,
.pixel_format = pixel_format,
.component_type = component_type,
.pixels = pixels,
});
}
void detects_common_extension_capabilities(pp::tests::Harness& h)
{
constexpr std::array<std::string_view, 2> extensions {
@@ -1317,6 +1488,261 @@ void rejects_incomplete_buffer_clear_dispatch(pp::tests::Harness& h)
PP_EXPECT(h, status.code == pp::foundation::StatusCode::invalid_argument);
}
void allocates_texture_2d_through_dispatch(pp::tests::Harness& h)
{
recorded_generated_texture_counts.clear();
recorded_binding_calls.clear();
recorded_texture_image_calls.clear();
next_texture_id = 91U;
const std::array<std::uint8_t, 4> pixels { 1U, 2U, 3U, 4U };
const auto texture = pp::renderer::gl::allocate_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DAllocation {
.width = 8,
.height = 4,
.internal_format = 0x8058,
.pixel_format = 0x1908U,
.component_type = 0x1401U,
.data = pixels.data(),
},
pp::renderer::gl::OpenGlTexture2DAllocationDispatch {
.gen_textures = record_gen_textures,
.bind_texture = record_bind_texture,
.tex_image_2d = record_tex_image_2d,
});
PP_EXPECT(h, texture.ok());
PP_EXPECT(h, texture.value() == 91U);
PP_EXPECT(h, recorded_generated_texture_counts.size() == 1U);
PP_EXPECT(h, recorded_generated_texture_counts[0] == 1U);
PP_EXPECT(h, recorded_binding_calls.size() == 2U);
PP_EXPECT(h, recorded_binding_calls[0].kind == RecordedOpenGlBindingCall::Kind::bind_texture);
PP_EXPECT(h, recorded_binding_calls[0].first == 0x0DE1U);
PP_EXPECT(h, recorded_binding_calls[0].second == 91U);
PP_EXPECT(h, recorded_binding_calls[1].second == 0U);
PP_EXPECT(h, recorded_texture_image_calls.size() == 1U);
PP_EXPECT(h, recorded_texture_image_calls[0].target == 0x0DE1U);
PP_EXPECT(h, recorded_texture_image_calls[0].width == 8);
PP_EXPECT(h, recorded_texture_image_calls[0].height == 4);
PP_EXPECT(h, recorded_texture_image_calls[0].data == pixels.data());
}
void rejects_invalid_texture_2d_allocation(pp::tests::Harness& h)
{
const auto missing_dispatch = pp::renderer::gl::allocate_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DAllocation {
.width = 1,
.height = 1,
.internal_format = 0x8058,
.pixel_format = 0x1908U,
.component_type = 0x1401U,
},
pp::renderer::gl::OpenGlTexture2DAllocationDispatch {
.gen_textures = record_gen_textures,
.bind_texture = record_bind_texture,
});
const auto invalid_dimensions = pp::renderer::gl::allocate_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DAllocation {
.width = 0,
.height = 1,
.internal_format = 0x8058,
.pixel_format = 0x1908U,
.component_type = 0x1401U,
},
pp::renderer::gl::OpenGlTexture2DAllocationDispatch {
.gen_textures = record_gen_textures,
.bind_texture = record_bind_texture,
.tex_image_2d = record_tex_image_2d,
});
PP_EXPECT(h, !missing_dispatch.ok());
PP_EXPECT(h, missing_dispatch.status().code == pp::foundation::StatusCode::invalid_argument);
PP_EXPECT(h, !invalid_dimensions.ok());
PP_EXPECT(h, invalid_dimensions.status().code == pp::foundation::StatusCode::invalid_argument);
}
void deletes_and_binds_texture_2d_through_dispatch(pp::tests::Harness& h)
{
recorded_deleted_textures.clear();
recorded_binding_calls.clear();
const auto delete_status = pp::renderer::gl::delete_opengl_texture_2d(
27U,
pp::renderer::gl::OpenGlTexture2DDeleteDispatch {
.delete_textures = record_delete_textures,
});
const auto bind_status = pp::renderer::gl::bind_opengl_texture_2d(
27U,
pp::renderer::gl::OpenGlTexture2DBindDispatch {
.bind_texture = record_bind_texture,
});
PP_EXPECT(h, delete_status.ok());
PP_EXPECT(h, recorded_deleted_textures.size() == 1U);
PP_EXPECT(h, recorded_deleted_textures[0] == 27U);
PP_EXPECT(h, bind_status.ok());
PP_EXPECT(h, recorded_binding_calls.size() == 1U);
PP_EXPECT(h, recorded_binding_calls[0].first == 0x0DE1U);
PP_EXPECT(h, recorded_binding_calls[0].second == 27U);
}
void updates_texture_2d_through_dispatch(pp::tests::Harness& h)
{
recorded_binding_calls.clear();
recorded_texture_image_calls.clear();
const std::array<std::uint8_t, 4> pixels { 9U, 8U, 7U, 6U };
const auto status = pp::renderer::gl::update_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DUpdate {
.texture_id = 31U,
.width = 2,
.height = 2,
.pixel_format = 0x1908U,
.component_type = 0x1401U,
.data = pixels.data(),
},
pp::renderer::gl::OpenGlTexture2DUpdateDispatch {
.bind_texture = record_bind_texture,
.tex_sub_image_2d = record_tex_sub_image_2d,
});
PP_EXPECT(h, status.ok());
PP_EXPECT(h, recorded_binding_calls.size() == 1U);
PP_EXPECT(h, recorded_binding_calls[0].second == 31U);
PP_EXPECT(h, recorded_texture_image_calls.size() == 1U);
PP_EXPECT(h, recorded_texture_image_calls[0].sub_image);
PP_EXPECT(h, recorded_texture_image_calls[0].width == 2);
PP_EXPECT(h, recorded_texture_image_calls[0].height == 2);
PP_EXPECT(h, recorded_texture_image_calls[0].data == pixels.data());
}
void generates_texture_2d_mipmaps_through_dispatch(pp::tests::Harness& h)
{
recorded_binding_calls.clear();
recorded_mipmap_targets.clear();
const auto status = pp::renderer::gl::generate_opengl_texture_2d_mipmaps(
41U,
pp::renderer::gl::OpenGlTexture2DMipmapDispatch {
.bind_texture = record_bind_texture,
.generate_mipmap = record_generate_mipmap,
});
PP_EXPECT(h, status.ok());
PP_EXPECT(h, recorded_binding_calls.size() == 2U);
PP_EXPECT(h, recorded_binding_calls[0].second == 41U);
PP_EXPECT(h, recorded_binding_calls[1].second == 0U);
PP_EXPECT(h, recorded_mipmap_targets.size() == 1U);
PP_EXPECT(h, recorded_mipmap_targets[0] == 0x0DE1U);
}
void reads_back_texture_2d_through_framebuffer_dispatch(pp::tests::Harness& h)
{
recorded_generated_framebuffer_counts.clear();
recorded_deleted_framebuffers.clear();
recorded_binding_calls.clear();
recorded_framebuffer_attachment_calls.clear();
recorded_framebuffer_status_queries.clear();
recorded_read_pixels_calls.clear();
next_framebuffer_id = 44U;
configured_framebuffer_status = 0x8CD5U;
std::array<std::uint8_t, 16> pixels {};
const auto result = pp::renderer::gl::readback_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DReadback {
.texture_id = 56U,
.width = 2,
.height = 2,
.format = pp::renderer::gl::rgba8_readback_format(),
.pixels = pixels.data(),
},
pp::renderer::gl::OpenGlTexture2DReadbackDispatch {
.bind_texture = record_bind_texture,
.gen_framebuffers = record_gen_framebuffers,
.get_integer = record_get_integer,
.bind_framebuffer = record_bind_framebuffer,
.framebuffer_texture_2d = record_framebuffer_texture_2d,
.check_framebuffer_status = record_check_framebuffer_status,
.read_pixels = record_read_pixels,
.delete_framebuffers = record_delete_framebuffers,
});
PP_EXPECT(h, result.ok());
PP_EXPECT(h, result.value().pixels_read);
PP_EXPECT(h, result.value().framebuffer_status == 0x8CD5U);
PP_EXPECT(h, recorded_generated_framebuffer_counts.size() == 1U);
PP_EXPECT(h, recorded_generated_framebuffer_counts[0] == 1U);
PP_EXPECT(h, recorded_binding_calls.size() == 3U);
PP_EXPECT(h, recorded_binding_calls[0].kind == RecordedOpenGlBindingCall::Kind::bind_texture);
PP_EXPECT(h, recorded_binding_calls[0].second == 56U);
PP_EXPECT(h, recorded_binding_calls[1].kind == RecordedOpenGlBindingCall::Kind::bind_framebuffer);
PP_EXPECT(h, recorded_binding_calls[1].first == 0x8D40U);
PP_EXPECT(h, recorded_binding_calls[1].second == 44U);
PP_EXPECT(h, recorded_binding_calls[2].kind == RecordedOpenGlBindingCall::Kind::bind_framebuffer);
PP_EXPECT(h, recorded_binding_calls[2].second == 7U);
PP_EXPECT(h, recorded_framebuffer_attachment_calls.size() == 1U);
PP_EXPECT(h, recorded_framebuffer_attachment_calls[0].attachment == 0x8CE0U);
PP_EXPECT(h, recorded_framebuffer_attachment_calls[0].texture == 56U);
PP_EXPECT(h, recorded_read_pixels_calls.size() == 1U);
PP_EXPECT(h, recorded_read_pixels_calls[0].pixels == pixels.data());
PP_EXPECT(h, recorded_deleted_framebuffers.size() == 1U);
PP_EXPECT(h, recorded_deleted_framebuffers[0] == 44U);
}
void reports_incomplete_texture_2d_readback_framebuffer(pp::tests::Harness& h)
{
recorded_deleted_framebuffers.clear();
recorded_read_pixels_calls.clear();
next_framebuffer_id = 45U;
configured_framebuffer_status = 0x8CD6U;
std::array<std::uint8_t, 16> pixels {};
const auto result = pp::renderer::gl::readback_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DReadback {
.texture_id = 57U,
.width = 2,
.height = 2,
.format = pp::renderer::gl::rgba8_readback_format(),
.pixels = pixels.data(),
},
pp::renderer::gl::OpenGlTexture2DReadbackDispatch {
.bind_texture = record_bind_texture,
.gen_framebuffers = record_gen_framebuffers,
.get_integer = record_get_integer,
.bind_framebuffer = record_bind_framebuffer,
.framebuffer_texture_2d = record_framebuffer_texture_2d,
.check_framebuffer_status = record_check_framebuffer_status,
.read_pixels = record_read_pixels,
.delete_framebuffers = record_delete_framebuffers,
});
PP_EXPECT(h, result.ok());
PP_EXPECT(h, !result.value().pixels_read);
PP_EXPECT(h, result.value().framebuffer_status == 0x8CD6U);
PP_EXPECT(h, recorded_read_pixels_calls.empty());
PP_EXPECT(h, recorded_deleted_framebuffers.back() == 45U);
}
void rejects_incomplete_texture_2d_readback_dispatch(pp::tests::Harness& h)
{
std::array<std::uint8_t, 4> pixels {};
const auto result = pp::renderer::gl::readback_opengl_texture_2d(
pp::renderer::gl::OpenGlTexture2DReadback {
.texture_id = 1U,
.width = 1,
.height = 1,
.format = pp::renderer::gl::rgba8_readback_format(),
.pixels = pixels.data(),
},
pp::renderer::gl::OpenGlTexture2DReadbackDispatch {
.bind_texture = record_bind_texture,
.gen_framebuffers = record_gen_framebuffers,
});
PP_EXPECT(h, !result.ok());
PP_EXPECT(h, result.status().code == pp::foundation::StatusCode::invalid_argument);
}
void maps_renderer_viewports_and_scissors(pp::tests::Harness& h)
{
const auto viewport = pp::renderer::gl::viewport_for_renderer_viewport(
@@ -1711,6 +2137,14 @@ int main()
harness.run("rejects_incomplete_generic_capability_dispatch", rejects_incomplete_generic_capability_dispatch);
harness.run("applies_buffer_clear_dispatch", applies_buffer_clear_dispatch);
harness.run("rejects_incomplete_buffer_clear_dispatch", rejects_incomplete_buffer_clear_dispatch);
harness.run("allocates_texture_2d_through_dispatch", allocates_texture_2d_through_dispatch);
harness.run("rejects_invalid_texture_2d_allocation", rejects_invalid_texture_2d_allocation);
harness.run("deletes_and_binds_texture_2d_through_dispatch", deletes_and_binds_texture_2d_through_dispatch);
harness.run("updates_texture_2d_through_dispatch", updates_texture_2d_through_dispatch);
harness.run("generates_texture_2d_mipmaps_through_dispatch", generates_texture_2d_mipmaps_through_dispatch);
harness.run("reads_back_texture_2d_through_framebuffer_dispatch", reads_back_texture_2d_through_framebuffer_dispatch);
harness.run("reports_incomplete_texture_2d_readback_framebuffer", reports_incomplete_texture_2d_readback_framebuffer);
harness.run("rejects_incomplete_texture_2d_readback_dispatch", rejects_incomplete_texture_2d_readback_dispatch);
harness.run("maps_renderer_viewports_and_scissors", maps_renderer_viewports_and_scissors);
harness.run("maps_renderer_blend_state_tokens", maps_renderer_blend_state_tokens);
harness.run("maps_renderer_color_write_masks", maps_renderer_color_write_masks);