Route Shape mesh operations through renderer GL
This commit is contained in:
@@ -1215,6 +1215,156 @@ pp::foundation::Result<std::int32_t> get_opengl_uniform_location(
|
||||
dispatch.get_uniform_location(program_id, uniform_name));
|
||||
}
|
||||
|
||||
pp::foundation::Result<OpenGlMeshObjects> create_opengl_mesh_objects(
|
||||
OpenGlMeshUpload upload,
|
||||
OpenGlMeshCreateDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.gen_buffers == nullptr
|
||||
|| dispatch.bind_buffer == nullptr
|
||||
|| dispatch.buffer_data == nullptr
|
||||
|| dispatch.gen_vertex_arrays == nullptr
|
||||
|| dispatch.bind_vertex_array == nullptr
|
||||
|| dispatch.enable_vertex_attrib_array == nullptr
|
||||
|| dispatch.vertex_attrib_pointer == nullptr) {
|
||||
return pp::foundation::Result<OpenGlMeshObjects>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL mesh create dispatch callbacks must not be null"));
|
||||
}
|
||||
|
||||
if (upload.vertex_byte_count < 0
|
||||
|| (upload.vertex_byte_count > 0 && upload.vertex_data == nullptr)
|
||||
|| (upload.indexed && (upload.index_byte_count <= 0 || upload.index_data == nullptr))
|
||||
|| (!upload.indexed && upload.index_byte_count != 0)
|
||||
|| upload.attributes.empty()) {
|
||||
return pp::foundation::Result<OpenGlMeshObjects>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL mesh upload parameters are invalid"));
|
||||
}
|
||||
|
||||
for (const auto& attribute : upload.attributes) {
|
||||
if (attribute.component_count <= 0 || attribute.component_type == 0U || attribute.stride <= 0) {
|
||||
return pp::foundation::Result<OpenGlMeshObjects>::failure(
|
||||
pp::foundation::Status::invalid_argument("OpenGL mesh vertex attribute parameters are invalid"));
|
||||
}
|
||||
}
|
||||
|
||||
std::array<std::uint32_t, 2> buffers {};
|
||||
const auto buffer_count = upload.indexed ? 2U : 1U;
|
||||
dispatch.gen_buffers(buffer_count, buffers.data());
|
||||
if (buffers[0] == 0U || (upload.indexed && buffers[1] == 0U)) {
|
||||
return pp::foundation::Result<OpenGlMeshObjects>::failure(
|
||||
pp::foundation::Status::out_of_range("OpenGL mesh buffer allocation returned id 0"));
|
||||
}
|
||||
|
||||
if (upload.indexed) {
|
||||
dispatch.bind_buffer(element_array_buffer_target(), buffers[1]);
|
||||
dispatch.buffer_data(element_array_buffer_target(), upload.index_byte_count, upload.index_data, static_draw_buffer_usage());
|
||||
}
|
||||
if (upload.vertex_byte_count > 0) {
|
||||
dispatch.bind_buffer(array_buffer_target(), buffers[0]);
|
||||
dispatch.buffer_data(array_buffer_target(), upload.vertex_byte_count, upload.vertex_data, static_draw_buffer_usage());
|
||||
}
|
||||
dispatch.bind_buffer(element_array_buffer_target(), 0U);
|
||||
dispatch.bind_buffer(array_buffer_target(), 0U);
|
||||
|
||||
std::array<std::uint32_t, 2> vertex_arrays {};
|
||||
dispatch.gen_vertex_arrays(static_cast<std::uint32_t>(vertex_arrays.size()), vertex_arrays.data());
|
||||
if (vertex_arrays[0] == 0U || vertex_arrays[1] == 0U) {
|
||||
return pp::foundation::Result<OpenGlMeshObjects>::failure(
|
||||
pp::foundation::Status::out_of_range("OpenGL mesh vertex array allocation returned id 0"));
|
||||
}
|
||||
|
||||
for (const auto vertex_array : vertex_arrays) {
|
||||
dispatch.bind_vertex_array(vertex_array);
|
||||
for (const auto& attribute : upload.attributes) {
|
||||
dispatch.enable_vertex_attrib_array(attribute.index);
|
||||
}
|
||||
if (upload.indexed) {
|
||||
dispatch.bind_buffer(element_array_buffer_target(), buffers[1]);
|
||||
}
|
||||
dispatch.bind_buffer(array_buffer_target(), buffers[0]);
|
||||
for (const auto& attribute : upload.attributes) {
|
||||
dispatch.vertex_attrib_pointer(
|
||||
attribute.index,
|
||||
attribute.component_count,
|
||||
attribute.component_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
reinterpret_cast<const void*>(attribute.offset));
|
||||
}
|
||||
}
|
||||
dispatch.bind_vertex_array(0U);
|
||||
|
||||
return pp::foundation::Result<OpenGlMeshObjects>::success(OpenGlMeshObjects {
|
||||
.vertex_buffer = buffers[0],
|
||||
.index_buffer = buffers[1],
|
||||
.vertex_arrays = vertex_arrays,
|
||||
});
|
||||
}
|
||||
|
||||
pp::foundation::Status upload_opengl_buffer_data(
|
||||
OpenGlBufferUpload upload,
|
||||
OpenGlBufferUploadDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_buffer == nullptr || dispatch.buffer_data == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL buffer upload dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (upload.target == 0U
|
||||
|| upload.buffer_id == 0U
|
||||
|| upload.byte_count < 0
|
||||
|| (upload.byte_count > 0 && upload.data == nullptr)
|
||||
|| upload.usage == 0U) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL buffer upload parameters are invalid");
|
||||
}
|
||||
|
||||
dispatch.bind_buffer(upload.target, upload.buffer_id);
|
||||
dispatch.buffer_data(upload.target, upload.byte_count, upload.data, upload.usage);
|
||||
dispatch.bind_buffer(upload.target, 0U);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status draw_opengl_mesh(
|
||||
OpenGlMeshDraw draw,
|
||||
OpenGlMeshDrawDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.bind_vertex_array == nullptr
|
||||
|| dispatch.draw_elements == nullptr
|
||||
|| dispatch.draw_arrays == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL mesh draw dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (draw.vertex_array == 0U || draw.count < 0 || (draw.indexed && draw.index_type == 0U)) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL mesh draw parameters are invalid");
|
||||
}
|
||||
|
||||
dispatch.bind_vertex_array(draw.vertex_array);
|
||||
if (draw.indexed) {
|
||||
dispatch.draw_elements(draw.mode, draw.count, draw.index_type, draw.index_offset);
|
||||
} else {
|
||||
dispatch.draw_arrays(draw.mode, 0, draw.count);
|
||||
}
|
||||
dispatch.bind_vertex_array(0U);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status delete_opengl_mesh_objects(
|
||||
OpenGlMeshDelete objects,
|
||||
OpenGlMeshDeleteDispatch dispatch) noexcept
|
||||
{
|
||||
if (dispatch.delete_buffers == nullptr || dispatch.delete_vertex_arrays == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("OpenGL mesh delete dispatch callbacks must not be null");
|
||||
}
|
||||
|
||||
if (objects.buffers[0] != 0U || objects.buffers[1] != 0U) {
|
||||
dispatch.delete_buffers(1U, &objects.buffers[0]);
|
||||
dispatch.delete_buffers(1U, &objects.buffers[1]);
|
||||
}
|
||||
if (objects.vertex_arrays[0] != 0U || objects.vertex_arrays[1] != 0U) {
|
||||
dispatch.delete_vertex_arrays(1U, &objects.vertex_arrays[0]);
|
||||
dispatch.delete_vertex_arrays(1U, &objects.vertex_arrays[1]);
|
||||
}
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
std::uint32_t extension_count_query() noexcept
|
||||
{
|
||||
return gl_num_extensions;
|
||||
|
||||
@@ -168,6 +168,52 @@ struct OpenGlActiveUniformInfo {
|
||||
std::uint32_t type = 0;
|
||||
};
|
||||
|
||||
struct OpenGlVertexAttribute {
|
||||
std::uint32_t index = 0;
|
||||
std::int32_t component_count = 0;
|
||||
std::uint32_t component_type = 0;
|
||||
std::uint8_t normalized = 0;
|
||||
std::int32_t stride = 0;
|
||||
std::uintptr_t offset = 0;
|
||||
};
|
||||
|
||||
struct OpenGlMeshUpload {
|
||||
const void* vertex_data = nullptr;
|
||||
std::intptr_t vertex_byte_count = 0;
|
||||
const void* index_data = nullptr;
|
||||
std::intptr_t index_byte_count = 0;
|
||||
bool indexed = false;
|
||||
std::span<const OpenGlVertexAttribute> attributes;
|
||||
};
|
||||
|
||||
struct OpenGlMeshObjects {
|
||||
std::uint32_t vertex_buffer = 0;
|
||||
std::uint32_t index_buffer = 0;
|
||||
std::array<std::uint32_t, 2> vertex_arrays {};
|
||||
};
|
||||
|
||||
struct OpenGlBufferUpload {
|
||||
std::uint32_t target = 0;
|
||||
std::uint32_t buffer_id = 0;
|
||||
const void* data = nullptr;
|
||||
std::intptr_t byte_count = 0;
|
||||
std::uint32_t usage = 0;
|
||||
};
|
||||
|
||||
struct OpenGlMeshDraw {
|
||||
std::uint32_t vertex_array = 0;
|
||||
std::uint32_t mode = 0;
|
||||
std::int32_t count = 0;
|
||||
bool indexed = false;
|
||||
std::uint32_t index_type = 0;
|
||||
const void* index_offset = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlMeshDelete {
|
||||
std::array<std::uint32_t, 2> buffers {};
|
||||
std::array<std::uint32_t, 2> vertex_arrays {};
|
||||
};
|
||||
|
||||
struct OpenGlFramebufferRect {
|
||||
std::int32_t x0 = 0;
|
||||
std::int32_t y0 = 0;
|
||||
@@ -306,6 +352,30 @@ using OpenGlSamplerParameterfvFn = void (*)(
|
||||
const float* values) noexcept;
|
||||
using OpenGlGenObjectsFn = void (*)(std::uint32_t count, std::uint32_t* ids) noexcept;
|
||||
using OpenGlDeleteObjectsFn = void (*)(std::uint32_t count, const std::uint32_t* ids) noexcept;
|
||||
using OpenGlBindBufferFn = void (*)(std::uint32_t target, std::uint32_t buffer) noexcept;
|
||||
using OpenGlBufferDataFn = void (*)(
|
||||
std::uint32_t target,
|
||||
std::intptr_t byte_count,
|
||||
const void* data,
|
||||
std::uint32_t usage) noexcept;
|
||||
using OpenGlBindVertexArrayFn = void (*)(std::uint32_t vertex_array) noexcept;
|
||||
using OpenGlEnableVertexAttribArrayFn = void (*)(std::uint32_t index) noexcept;
|
||||
using OpenGlVertexAttribPointerFn = void (*)(
|
||||
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;
|
||||
using OpenGlDrawElementsFn = void (*)(
|
||||
std::uint32_t mode,
|
||||
std::int32_t count,
|
||||
std::uint32_t index_type,
|
||||
const void* index_offset) noexcept;
|
||||
using OpenGlDrawArraysFn = void (*)(
|
||||
std::uint32_t mode,
|
||||
std::int32_t first,
|
||||
std::int32_t count) noexcept;
|
||||
using OpenGlTexImage2DFn = void (*)(
|
||||
std::uint32_t target,
|
||||
std::int32_t level,
|
||||
@@ -578,6 +648,32 @@ struct OpenGlUniformLocationDispatch {
|
||||
OpenGlGetUniformLocationFn get_uniform_location = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlMeshCreateDispatch {
|
||||
OpenGlGenObjectsFn gen_buffers = nullptr;
|
||||
OpenGlBindBufferFn bind_buffer = nullptr;
|
||||
OpenGlBufferDataFn buffer_data = nullptr;
|
||||
OpenGlGenObjectsFn gen_vertex_arrays = nullptr;
|
||||
OpenGlBindVertexArrayFn bind_vertex_array = nullptr;
|
||||
OpenGlEnableVertexAttribArrayFn enable_vertex_attrib_array = nullptr;
|
||||
OpenGlVertexAttribPointerFn vertex_attrib_pointer = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlBufferUploadDispatch {
|
||||
OpenGlBindBufferFn bind_buffer = nullptr;
|
||||
OpenGlBufferDataFn buffer_data = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlMeshDrawDispatch {
|
||||
OpenGlBindVertexArrayFn bind_vertex_array = nullptr;
|
||||
OpenGlDrawElementsFn draw_elements = nullptr;
|
||||
OpenGlDrawArraysFn draw_arrays = nullptr;
|
||||
};
|
||||
|
||||
struct OpenGlMeshDeleteDispatch {
|
||||
OpenGlDeleteObjectsFn delete_buffers = nullptr;
|
||||
OpenGlDeleteObjectsFn delete_vertex_arrays = nullptr;
|
||||
};
|
||||
|
||||
[[nodiscard]] OpenGlCapabilities detect_opengl_capabilities(
|
||||
std::span<const std::string_view> extensions,
|
||||
OpenGlRuntime runtime) noexcept;
|
||||
@@ -729,6 +825,18 @@ struct OpenGlUniformLocationDispatch {
|
||||
std::uint32_t program_id,
|
||||
const char* uniform_name,
|
||||
OpenGlUniformLocationDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Result<OpenGlMeshObjects> create_opengl_mesh_objects(
|
||||
OpenGlMeshUpload upload,
|
||||
OpenGlMeshCreateDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status upload_opengl_buffer_data(
|
||||
OpenGlBufferUpload upload,
|
||||
OpenGlBufferUploadDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status draw_opengl_mesh(
|
||||
OpenGlMeshDraw draw,
|
||||
OpenGlMeshDrawDispatch dispatch) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status delete_opengl_mesh_objects(
|
||||
OpenGlMeshDelete objects,
|
||||
OpenGlMeshDeleteDispatch dispatch) noexcept;
|
||||
|
||||
[[nodiscard]] std::uint32_t extension_count_query() noexcept;
|
||||
[[nodiscard]] std::uint32_t extension_string_name() noexcept;
|
||||
|
||||
Reference in New Issue
Block a user