Share retained mesh dispatch bridge

This commit is contained in:
2026-06-05 14:51:40 +02:00
parent d719a5a5e5
commit 96ff1c41e2
7 changed files with 298 additions and 325 deletions

View File

@@ -1,5 +1,6 @@
#include "pch.h"
#include "log.h"
#include "legacy_gl_mesh_dispatch.h"
#include "shape.h"
#include "app.h"
#include "renderer_gl/opengl_capabilities.h"
@@ -10,85 +11,6 @@
namespace {
void gen_buffers_adapter(std::uint32_t count, std::uint32_t* ids) noexcept
{
glGenBuffers(static_cast<GLsizei>(count), ids);
}
void delete_buffers_adapter(std::uint32_t count, const std::uint32_t* ids) noexcept
{
glDeleteBuffers(static_cast<GLsizei>(count), ids);
}
void bind_buffer_adapter(std::uint32_t target, std::uint32_t buffer) noexcept
{
glBindBuffer(static_cast<GLenum>(target), static_cast<GLuint>(buffer));
}
void buffer_data_adapter(
std::uint32_t target,
std::intptr_t byte_count,
const void* data,
std::uint32_t usage) noexcept
{
glBufferData(static_cast<GLenum>(target), static_cast<GLsizeiptr>(byte_count), data, static_cast<GLenum>(usage));
}
void gen_vertex_arrays_adapter(std::uint32_t count, std::uint32_t* ids) noexcept
{
glGenVertexArrays(static_cast<GLsizei>(count), ids);
}
void delete_vertex_arrays_adapter(std::uint32_t count, const std::uint32_t* ids) noexcept
{
glDeleteVertexArrays(static_cast<GLsizei>(count), ids);
}
void bind_vertex_array_adapter(std::uint32_t vertex_array) noexcept
{
glBindVertexArray(static_cast<GLuint>(vertex_array));
}
void enable_vertex_attrib_array_adapter(std::uint32_t index) noexcept
{
glEnableVertexAttribArray(static_cast<GLuint>(index));
}
void vertex_attrib_pointer_adapter(
std::uint32_t index,
std::int32_t component_count,
std::uint32_t component_type,
std::uint8_t normalized,
std::int32_t stride,
const void* offset) noexcept
{
glVertexAttribPointer(
static_cast<GLuint>(index),
static_cast<GLint>(component_count),
static_cast<GLenum>(component_type),
static_cast<GLboolean>(normalized),
static_cast<GLsizei>(stride),
offset);
}
void draw_elements_adapter(
std::uint32_t mode,
std::int32_t count,
std::uint32_t index_type,
const void* index_offset) noexcept
{
glDrawElements(
static_cast<GLenum>(mode),
static_cast<GLsizei>(count),
static_cast<GLenum>(index_type),
index_offset);
}
void draw_arrays_adapter(std::uint32_t mode, std::int32_t first, std::int32_t count) noexcept
{
glDrawArrays(static_cast<GLenum>(mode), static_cast<GLint>(first), static_cast<GLsizei>(count));
}
[[nodiscard]] std::span<const pp::renderer::gl::OpenGlVertexAttribute> shape_vertex_attributes() noexcept
{
static const std::array<pp::renderer::gl::OpenGlVertexAttribute, 4> attributes {
@@ -128,44 +50,6 @@ void draw_arrays_adapter(std::uint32_t mode, std::int32_t first, std::int32_t co
return attributes;
}
[[nodiscard]] pp::renderer::gl::OpenGlMeshCreateDispatch mesh_create_dispatch() noexcept
{
return pp::renderer::gl::OpenGlMeshCreateDispatch {
.gen_buffers = gen_buffers_adapter,
.bind_buffer = bind_buffer_adapter,
.buffer_data = buffer_data_adapter,
.gen_vertex_arrays = gen_vertex_arrays_adapter,
.bind_vertex_array = bind_vertex_array_adapter,
.enable_vertex_attrib_array = enable_vertex_attrib_array_adapter,
.vertex_attrib_pointer = vertex_attrib_pointer_adapter,
};
}
[[nodiscard]] pp::renderer::gl::OpenGlBufferUploadDispatch buffer_upload_dispatch() noexcept
{
return pp::renderer::gl::OpenGlBufferUploadDispatch {
.bind_buffer = bind_buffer_adapter,
.buffer_data = buffer_data_adapter,
};
}
[[nodiscard]] pp::renderer::gl::OpenGlMeshDrawDispatch mesh_draw_dispatch() noexcept
{
return pp::renderer::gl::OpenGlMeshDrawDispatch {
.bind_vertex_array = bind_vertex_array_adapter,
.draw_elements = draw_elements_adapter,
.draw_arrays = draw_arrays_adapter,
};
}
[[nodiscard]] pp::renderer::gl::OpenGlMeshDeleteDispatch mesh_delete_dispatch() noexcept
{
return pp::renderer::gl::OpenGlMeshDeleteDispatch {
.delete_buffers = delete_buffers_adapter,
.delete_vertex_arrays = delete_vertex_arrays_adapter,
};
}
}
bool Shape::create_buffers(GLushort * idx, GLvoid * vertices, int isize, int vsize)
@@ -191,7 +75,7 @@ bool Shape::create_buffers_imp(GLvoid* idx, GLvoid* vertices, int isize, int vsi
{
destroy();
const auto mesh = pp::renderer::gl::create_opengl_mesh_objects(
const auto mesh = pp::legacy::gl_mesh::create_mesh_objects(
pp::renderer::gl::OpenGlMeshUpload {
.vertex_data = vertices,
.vertex_byte_count = vsize,
@@ -200,15 +84,15 @@ bool Shape::create_buffers_imp(GLvoid* idx, GLvoid* vertices, int isize, int vsi
.indexed = true,
.attributes = shape_vertex_attributes(),
},
mesh_create_dispatch());
if (!mesh.ok()) {
"Shape::create_buffers indexed");
if (mesh.vertex_buffer == 0U) {
ret = false;
return;
}
buffers[0] = static_cast<GLuint>(mesh.value().vertex_buffer);
buffers[1] = static_cast<GLuint>(mesh.value().index_buffer);
arrays[0] = static_cast<GLuint>(mesh.value().vertex_arrays[0]);
arrays[1] = static_cast<GLuint>(mesh.value().vertex_arrays[1]);
buffers[0] = static_cast<GLuint>(mesh.vertex_buffer);
buffers[1] = static_cast<GLuint>(mesh.index_buffer);
arrays[0] = static_cast<GLuint>(mesh.vertex_arrays[0]);
arrays[1] = static_cast<GLuint>(mesh.vertex_arrays[1]);
});
return ret;
}
@@ -221,22 +105,22 @@ bool Shape::create_buffers(GLvoid* vertices, int vsize)
{
destroy();
const auto mesh = pp::renderer::gl::create_opengl_mesh_objects(
const auto mesh = pp::legacy::gl_mesh::create_mesh_objects(
pp::renderer::gl::OpenGlMeshUpload {
.vertex_data = vertices,
.vertex_byte_count = vsize,
.indexed = false,
.attributes = shape_vertex_attributes(),
},
mesh_create_dispatch());
if (!mesh.ok()) {
"Shape::create_buffers");
if (mesh.vertex_buffer == 0U) {
ret = false;
return;
}
buffers[0] = static_cast<GLuint>(mesh.value().vertex_buffer);
buffers[1] = static_cast<GLuint>(mesh.value().index_buffer);
arrays[0] = static_cast<GLuint>(mesh.value().vertex_arrays[0]);
arrays[1] = static_cast<GLuint>(mesh.value().vertex_arrays[1]);
buffers[0] = static_cast<GLuint>(mesh.vertex_buffer);
buffers[1] = static_cast<GLuint>(mesh.index_buffer);
arrays[0] = static_cast<GLuint>(mesh.vertex_arrays[0]);
arrays[1] = static_cast<GLuint>(mesh.vertex_arrays[1]);
});
return ret;
}
@@ -247,7 +131,7 @@ void Shape::draw_fill() const
const auto type = static_cast<GLenum>(pp::renderer::gl::primitive_mode_for_fill_count(count[0]));
App::I->render_task([=]
{
(void)pp::renderer::gl::draw_opengl_mesh(
(void)pp::legacy::gl_mesh::draw_mesh(
pp::renderer::gl::OpenGlMeshDraw {
.vertex_array = arrays[0],
.mode = type,
@@ -256,7 +140,7 @@ void Shape::draw_fill() const
.index_type = index_type,
.index_offset = ioff[0],
},
mesh_draw_dispatch());
"Shape::draw_fill");
});
}
void Shape::draw_stroke() const
@@ -266,7 +150,7 @@ void Shape::draw_stroke() const
const auto type = static_cast<GLenum>(pp::renderer::gl::primitive_mode_for_stroke_count(count[1]));
App::I->render_task([=]
{
(void)pp::renderer::gl::draw_opengl_mesh(
(void)pp::legacy::gl_mesh::draw_mesh(
pp::renderer::gl::OpenGlMeshDraw {
.vertex_array = arrays[1],
.mode = type,
@@ -275,7 +159,7 @@ void Shape::draw_stroke() const
.index_type = index_type,
.index_offset = ioff[1],
},
mesh_draw_dispatch());
"Shape::draw_stroke");
});
}
@@ -283,12 +167,7 @@ void Shape::destroy()
{
if (App::I) App::I->render_task_async([b1=buffers[0],b2=buffers[1],a1=arrays[0],a2=arrays[1]]
{
(void)pp::renderer::gl::delete_opengl_mesh_objects(
pp::renderer::gl::OpenGlMeshDelete {
.buffers = { b1, b2 },
.vertex_arrays = { a1, a2 },
},
mesh_delete_dispatch());
(void)pp::legacy::gl_mesh::delete_mesh_objects({ b1, b2 }, { a1, a2 }, "Shape::destroy");
});
buffers[0] = buffers[1] = 0;
arrays[0] = arrays[1] = 0;
@@ -574,7 +453,7 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g
App::I->render_task([this]
{
(void)pp::renderer::gl::upload_opengl_buffer_data(
(void)pp::legacy::gl_mesh::upload_buffer_data(
pp::renderer::gl::OpenGlBufferUpload {
.target = pp::renderer::gl::array_buffer_target(),
.buffer_id = buffers[0],
@@ -582,7 +461,7 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g
.byte_count = static_cast<std::intptr_t>(sizeof(vertices)),
.usage = pp::renderer::gl::static_draw_buffer_usage(),
},
buffer_upload_dispatch());
"Plane::update_vertices vertices");
static GLushort idx[6 + 8]{
0, 1, 2,
0, 2, 3,
@@ -591,7 +470,7 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g
2, 3,
3, 0,
};
(void)pp::renderer::gl::upload_opengl_buffer_data(
(void)pp::legacy::gl_mesh::upload_buffer_data(
pp::renderer::gl::OpenGlBufferUpload {
.target = pp::renderer::gl::element_array_buffer_target(),
.buffer_id = buffers[1],
@@ -599,7 +478,7 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g
.byte_count = static_cast<std::intptr_t>(sizeof(idx)),
.usage = pp::renderer::gl::static_draw_buffer_usage(),
},
buffer_upload_dispatch());
"Plane::update_vertices indices");
});
}
void Circle::create_impl(float radius, int div, GLushort* idx, vertex_t* vertices)
@@ -858,7 +737,7 @@ void LineSegment::update_vertices(const glm::vec4 data[2])
static vertex_t vertices[2];
vertices[0] = { data[0], { 0, 0 } }; // A
vertices[1] = { data[1], { 0, 1 } }; // B
(void)pp::renderer::gl::upload_opengl_buffer_data(
(void)pp::legacy::gl_mesh::upload_buffer_data(
pp::renderer::gl::OpenGlBufferUpload {
.target = pp::renderer::gl::array_buffer_target(),
.buffer_id = buffers[0],
@@ -866,7 +745,7 @@ void LineSegment::update_vertices(const glm::vec4 data[2])
.byte_count = static_cast<std::intptr_t>(sizeof(vertices)),
.usage = pp::renderer::gl::static_draw_buffer_usage(),
},
buffer_upload_dispatch());
"LineSegment::update_vertices");
});
}
void DynamicShape::update_vertices(vertex_t* vertices, int vcount)
@@ -875,7 +754,7 @@ void DynamicShape::update_vertices(vertex_t* vertices, int vcount)
{
count[0] = vcount;
count[1] = vcount;
(void)pp::renderer::gl::upload_opengl_buffer_data(
(void)pp::legacy::gl_mesh::upload_buffer_data(
pp::renderer::gl::OpenGlBufferUpload {
.target = pp::renderer::gl::array_buffer_target(),
.buffer_id = buffers[0],
@@ -883,6 +762,6 @@ void DynamicShape::update_vertices(vertex_t* vertices, int vcount)
.byte_count = static_cast<std::intptr_t>(sizeof(vertex_t) * vcount),
.usage = pp::renderer::gl::static_draw_buffer_usage(),
},
buffer_upload_dispatch());
"DynamicShape::update_vertices");
});
}