Route Texture2D through renderer GL
This commit is contained in:
@@ -422,6 +422,192 @@ pp::foundation::Status clear_opengl_buffers(
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::uint32_t> allocate_opengl_texture_2d(
|
||||
OpenGlTexture2DAllocation allocation,
|
||||
OpenGlTexture2DAllocationDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.gen_textures == nullptr
|
||||
|| dispatch.bind_texture == nullptr
|
||||
|| dispatch.tex_image_2d == nullptr) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL texture allocation dispatch callbacks must not be null"));
|
||||
}
|
||||
|
||||
if (allocation.width <= 0
|
||||
|| allocation.height <= 0
|
||||
|| allocation.internal_format == 0
|
||||
|| allocation.pixel_format == 0U
|
||||
|| allocation.component_type == 0U) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL texture allocation parameters are invalid"));
|
||||
}
|
||||
|
||||
std::uint32_t texture_id = 0U;
|
||||
dispatch.gen_textures(1U, &texture_id);
|
||||
if (texture_id == 0U) {
|
||||
return pp::foundation::Result<std::uint32_t>::failure(
|
||||
pp::foundation::Status::out_of_range("OpenGL texture allocation returned id 0"));
|
||||
}
|
||||
|
||||
dispatch.bind_texture(texture_2d_target(), texture_id);
|
||||
dispatch.tex_image_2d(
|
||||
texture_2d_target(),
|
||||
0,
|
||||
allocation.internal_format,
|
||||
allocation.width,
|
||||
allocation.height,
|
||||
0,
|
||||
allocation.pixel_format,
|
||||
allocation.component_type,
|
||||
allocation.data);
|
||||
dispatch.bind_texture(texture_2d_target(), default_framebuffer_id());
|
||||
return pp::foundation::Result<std::uint32_t>::success(texture_id);
|
||||
}
|
||||
|
||||
pp::foundation::Status delete_opengl_texture_2d(
|
||||
std::uint32_t texture_id,
|
||||
OpenGlTexture2DDeleteDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.delete_textures == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL texture delete dispatch callback must not be null");
|
||||
}
|
||||
|
||||
if (texture_id == 0U) {
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
dispatch.delete_textures(1U, &texture_id);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status bind_opengl_texture_2d(
|
||||
std::uint32_t texture_id,
|
||||
OpenGlTexture2DBindDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_texture == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL texture bind dispatch callback must not be null");
|
||||
}
|
||||
|
||||
dispatch.bind_texture(texture_2d_target(), texture_id);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status update_opengl_texture_2d(
|
||||
OpenGlTexture2DUpdate update,
|
||||
OpenGlTexture2DUpdateDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_texture == nullptr || dispatch.tex_sub_image_2d == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL texture update dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (update.texture_id == 0U
|
||||
|| update.width <= 0
|
||||
|| update.height <= 0
|
||||
|| update.pixel_format == 0U
|
||||
|| update.component_type == 0U
|
||||
|| update.data == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL texture update parameters are invalid");
|
||||
}
|
||||
|
||||
dispatch.bind_texture(texture_2d_target(), update.texture_id);
|
||||
dispatch.tex_sub_image_2d(
|
||||
texture_2d_target(),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
update.width,
|
||||
update.height,
|
||||
update.pixel_format,
|
||||
update.component_type,
|
||||
update.data);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status generate_opengl_texture_2d_mipmaps(
|
||||
std::uint32_t texture_id,
|
||||
OpenGlTexture2DMipmapDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_texture == nullptr || dispatch.generate_mipmap == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL texture mipmap dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (texture_id == 0U) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL texture mipmap target is invalid");
|
||||
}
|
||||
|
||||
dispatch.bind_texture(texture_2d_target(), texture_id);
|
||||
dispatch.generate_mipmap(texture_2d_target());
|
||||
dispatch.bind_texture(texture_2d_target(), default_framebuffer_id());
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Result<OpenGlTexture2DReadbackResult> readback_opengl_texture_2d(
|
||||
OpenGlTexture2DReadback readback,
|
||||
OpenGlTexture2DReadbackDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_texture == nullptr
|
||||
|| dispatch.gen_framebuffers == nullptr
|
||||
|| dispatch.get_integer == nullptr
|
||||
|| dispatch.bind_framebuffer == nullptr
|
||||
|| dispatch.framebuffer_texture_2d == nullptr
|
||||
|| dispatch.check_framebuffer_status == nullptr
|
||||
|| dispatch.read_pixels == nullptr
|
||||
|| dispatch.delete_framebuffers == nullptr) {
|
||||
return pp::foundation::Result<OpenGlTexture2DReadbackResult>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL texture readback dispatch callbacks must not be null"));
|
||||
}
|
||||
|
||||
if (readback.texture_id == 0U
|
||||
|| readback.width <= 0
|
||||
|| readback.height <= 0
|
||||
|| readback.format.pixel_format == 0U
|
||||
|| readback.format.component_type == 0U
|
||||
|| readback.format.bytes_per_pixel == 0U
|
||||
|| readback.pixels == nullptr) {
|
||||
return pp::foundation::Result<OpenGlTexture2DReadbackResult>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL texture readback parameters are invalid"));
|
||||
}
|
||||
|
||||
std::uint32_t framebuffer_id = 0U;
|
||||
dispatch.gen_framebuffers(1U, &framebuffer_id);
|
||||
if (framebuffer_id == 0U) {
|
||||
return pp::foundation::Result<OpenGlTexture2DReadbackResult>::failure(
|
||||
pp::foundation::Status::out_of_range("OpenGL framebuffer allocation returned id 0"));
|
||||
}
|
||||
|
||||
std::int32_t previous_framebuffer = 0;
|
||||
dispatch.get_integer(draw_framebuffer_binding_query(), &previous_framebuffer);
|
||||
dispatch.bind_texture(texture_2d_target(), readback.texture_id);
|
||||
dispatch.bind_framebuffer(framebuffer_target(), framebuffer_id);
|
||||
dispatch.framebuffer_texture_2d(
|
||||
framebuffer_target(),
|
||||
framebuffer_color_attachment(),
|
||||
texture_2d_target(),
|
||||
readback.texture_id,
|
||||
0);
|
||||
|
||||
const auto framebuffer_status = dispatch.check_framebuffer_status(framebuffer_target());
|
||||
OpenGlTexture2DReadbackResult result {
|
||||
.framebuffer_status = framebuffer_status,
|
||||
.pixels_read = false,
|
||||
};
|
||||
if (framebuffer_status == framebuffer_complete_status()) {
|
||||
dispatch.read_pixels(
|
||||
0,
|
||||
0,
|
||||
readback.width,
|
||||
readback.height,
|
||||
readback.format.pixel_format,
|
||||
readback.format.component_type,
|
||||
readback.pixels);
|
||||
result.pixels_read = true;
|
||||
}
|
||||
|
||||
dispatch.bind_framebuffer(framebuffer_target(), static_cast<std::uint32_t>(previous_framebuffer));
|
||||
dispatch.delete_framebuffers(1U, &framebuffer_id);
|
||||
return pp::foundation::Result<OpenGlTexture2DReadbackResult>::success(result);
|
||||
}
|
||||
|
||||
std::uint32_t extension_count_query() noexcept
|
||||
{
|
||||
return gl_num_extensions;
|
||||
|
||||
Reference in New Issue
Block a user