From e1cce05bd625a15c637ed001b1655f9f22b7ac2a Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 3 Jun 2026 07:19:14 +0200 Subject: [PATCH] Route Shape mesh operations through renderer GL --- docs/modernization/build-inventory.md | 5 +- docs/modernization/roadmap.md | 10 +- src/renderer_gl/opengl_capabilities.cpp | 150 +++++++ src/renderer_gl/opengl_capabilities.h | 108 +++++ src/shape.cpp | 386 ++++++++++------- tests/renderer_gl/capabilities_tests.cpp | 506 +++++++++++++++++++++++ 6 files changed, 1007 insertions(+), 158 deletions(-) diff --git a/docs/modernization/build-inventory.md b/docs/modernization/build-inventory.md index 17061bf..b1039ec 100644 --- a/docs/modernization/build-inventory.md +++ b/docs/modernization/build-inventory.md @@ -206,7 +206,10 @@ Known local toolchain state: validates renderer API primitive-topology to OpenGL draw-mode mapping, Shape index-type, fill/stroke primitive-mode, buffer target, static upload usage, and vertex attribute component/normalization mapping used by - the legacy mesh draw path, plus the PanoPainter cube-face to OpenGL + the legacy mesh draw path. Legacy `Shape` mesh buffer/VAO creation, zero-byte + dynamic-buffer creation, dynamic vertex/index uploads, fill/stroke draw + calls, and buffer/VAO deletion now consume tested dispatch contracts here, + plus the PanoPainter cube-face to OpenGL texture-target mapping used by `TextureCube`. It also owns and validates sampler wrap S/T/R, min/mag filter, and desktop border-color parameter mapping used by legacy `Sampler`, plus renderer API diff --git a/docs/modernization/roadmap.md b/docs/modernization/roadmap.md index 0d4891f..d918da5 100644 --- a/docs/modernization/roadmap.md +++ b/docs/modernization/roadmap.md @@ -657,7 +657,11 @@ live in `pp_renderer_gl`. `RTT` no longer spells GL enum names directly. Renderer API primitive-topology to OpenGL draw-mode mapping, mesh index-type and primitive-mode decisions used by legacy `Shape` drawing, plus Shape buffer targets, static upload usage, and vertex attribute component/normalization -tokens, also live in `pp_renderer_gl`. The PanoPainter cube-face to +tokens, also live in `pp_renderer_gl`. Legacy `Shape` mesh buffer/VAO +creation, dynamic vertex/index uploads, fill/stroke draws, and buffer/VAO +deletion now execute through tested `pp_renderer_gl` dispatch contracts, +leaving the retained shape utility with thin GL adapter functions. The +PanoPainter cube-face to OpenGL texture-target mapping used by `TextureCube` also lives in `pp_renderer_gl`. The legacy app delegates extension, upload-format, framebuffer diagnostic, framebuffer blit, render-target setup, clear-state, @@ -1125,6 +1129,10 @@ Results: attribute rebinding, active-uniform count/enumeration, and uniform-location discovery also execute through tested `pp_renderer_gl` dispatch contracts, leaving only thin GL adapter functions in the retained `Shader` utility. + Legacy `Shape` mesh buffer/VAO creation, zero-byte dynamic-buffer creation, + dynamic buffer uploads, indexed and non-indexed draws, and resource deletion + now execute through tested `pp_renderer_gl` dispatch contracts, leaving only + thin GL adapter functions in the retained shape utility. - `pp_renderer_gl_command_plan_tests` covers the headless OpenGL command planner for recorded render-pass clear masks/values, viewport/scissor state, blend/depth/sampler state, texture format mapping, mesh/draw primitive modes, diff --git a/src/renderer_gl/opengl_capabilities.cpp b/src/renderer_gl/opengl_capabilities.cpp index 70b7c71..aae42a4 100644 --- a/src/renderer_gl/opengl_capabilities.cpp +++ b/src/renderer_gl/opengl_capabilities.cpp @@ -1215,6 +1215,156 @@ pp::foundation::Result get_opengl_uniform_location( dispatch.get_uniform_location(program_id, uniform_name)); } +pp::foundation::Result 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::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::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::failure( + pp::foundation::Status::invalid_argument("OpenGL mesh vertex attribute parameters are invalid")); + } + } + + std::array 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::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 vertex_arrays {}; + dispatch.gen_vertex_arrays(static_cast(vertex_arrays.size()), vertex_arrays.data()); + if (vertex_arrays[0] == 0U || vertex_arrays[1] == 0U) { + return pp::foundation::Result::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(attribute.offset)); + } + } + dispatch.bind_vertex_array(0U); + + return pp::foundation::Result::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; diff --git a/src/renderer_gl/opengl_capabilities.h b/src/renderer_gl/opengl_capabilities.h index 458cdee..18f9416 100644 --- a/src/renderer_gl/opengl_capabilities.h +++ b/src/renderer_gl/opengl_capabilities.h @@ -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 attributes; +}; + +struct OpenGlMeshObjects { + std::uint32_t vertex_buffer = 0; + std::uint32_t index_buffer = 0; + std::array 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 buffers {}; + std::array 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 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 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; diff --git a/src/shape.cpp b/src/shape.cpp index bbaa4f2..324a478 100644 --- a/src/shape.cpp +++ b/src/shape.cpp @@ -4,38 +4,166 @@ #include "app.h" #include "renderer_gl/opengl_capabilities.h" +#include +#include #include namespace { -[[nodiscard]] GLenum array_buffer_target() noexcept +void gen_buffers_adapter(std::uint32_t count, std::uint32_t* ids) noexcept { - return static_cast(pp::renderer::gl::array_buffer_target()); + glGenBuffers(static_cast(count), ids); } -[[nodiscard]] GLenum element_array_buffer_target() noexcept +void delete_buffers_adapter(std::uint32_t count, const std::uint32_t* ids) noexcept { - return static_cast(pp::renderer::gl::element_array_buffer_target()); + glDeleteBuffers(static_cast(count), ids); } -[[nodiscard]] GLenum static_draw_buffer_usage() noexcept +void bind_buffer_adapter(std::uint32_t target, std::uint32_t buffer) noexcept { - return static_cast(pp::renderer::gl::static_draw_buffer_usage()); + glBindBuffer(static_cast(target), static_cast(buffer)); } -[[nodiscard]] GLenum vertex_attribute_float_component_type() noexcept +void buffer_data_adapter( + std::uint32_t target, + std::intptr_t byte_count, + const void* data, + std::uint32_t usage) noexcept { - return static_cast(pp::renderer::gl::vertex_attribute_float_component_type()); + glBufferData(static_cast(target), static_cast(byte_count), data, static_cast(usage)); } -[[nodiscard]] GLboolean vertex_attribute_not_normalized() noexcept +void gen_vertex_arrays_adapter(std::uint32_t count, std::uint32_t* ids) noexcept { - return static_cast(pp::renderer::gl::vertex_attribute_not_normalized()); + glGenVertexArrays(static_cast(count), ids); } -[[nodiscard]] GLenum uint16_index_type() noexcept +void delete_vertex_arrays_adapter(std::uint32_t count, const std::uint32_t* ids) noexcept { - return static_cast(pp::renderer::gl::index_type_for_index_size(sizeof(GLushort))); + glDeleteVertexArrays(static_cast(count), ids); +} + +void bind_vertex_array_adapter(std::uint32_t vertex_array) noexcept +{ + glBindVertexArray(static_cast(vertex_array)); +} + +void enable_vertex_attrib_array_adapter(std::uint32_t index) noexcept +{ + glEnableVertexAttribArray(static_cast(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(index), + static_cast(component_count), + static_cast(component_type), + static_cast(normalized), + static_cast(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(mode), + static_cast(count), + static_cast(index_type), + index_offset); +} + +void draw_arrays_adapter(std::uint32_t mode, std::int32_t first, std::int32_t count) noexcept +{ + glDrawArrays(static_cast(mode), static_cast(first), static_cast(count)); +} + +[[nodiscard]] std::span shape_vertex_attributes() noexcept +{ + static const std::array attributes { + pp::renderer::gl::OpenGlVertexAttribute { + .index = 0U, + .component_count = 4, + .component_type = pp::renderer::gl::vertex_attribute_float_component_type(), + .normalized = static_cast(pp::renderer::gl::vertex_attribute_not_normalized()), + .stride = static_cast(sizeof(vertex_t)), + .offset = 0U, + }, + pp::renderer::gl::OpenGlVertexAttribute { + .index = 1U, + .component_count = 2, + .component_type = pp::renderer::gl::vertex_attribute_float_component_type(), + .normalized = static_cast(pp::renderer::gl::vertex_attribute_not_normalized()), + .stride = static_cast(sizeof(vertex_t)), + .offset = static_cast(offsetof(vertex_t, uvs)), + }, + pp::renderer::gl::OpenGlVertexAttribute { + .index = 2U, + .component_count = 2, + .component_type = pp::renderer::gl::vertex_attribute_float_component_type(), + .normalized = static_cast(pp::renderer::gl::vertex_attribute_not_normalized()), + .stride = static_cast(sizeof(vertex_t)), + .offset = static_cast(offsetof(vertex_t, uvs2)), + }, + pp::renderer::gl::OpenGlVertexAttribute { + .index = 3U, + .component_count = 3, + .component_type = pp::renderer::gl::vertex_attribute_float_component_type(), + .normalized = static_cast(pp::renderer::gl::vertex_attribute_not_normalized()), + .stride = static_cast(sizeof(vertex_t)), + .offset = static_cast(offsetof(vertex_t, nor)), + }, + }; + 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, + }; } } @@ -63,43 +191,24 @@ bool Shape::create_buffers_imp(GLvoid* idx, GLvoid* vertices, int isize, int vsi { destroy(); - glGenBuffers(2, buffers); - if (!(buffers[0] && buffers[1])) - { + const auto mesh = pp::renderer::gl::create_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshUpload { + .vertex_data = vertices, + .vertex_byte_count = vsize, + .index_data = idx, + .index_byte_count = isize, + .indexed = true, + .attributes = shape_vertex_attributes(), + }, + mesh_create_dispatch()); + if (!mesh.ok()) { ret = false; return; } - - glBindBuffer(element_array_buffer_target(), buffers[1]); - glBufferData(element_array_buffer_target(), isize, idx, static_draw_buffer_usage()); - glBindBuffer(array_buffer_target(), buffers[0]); - glBufferData(array_buffer_target(), vsize, vertices, static_draw_buffer_usage()); - glBindBuffer(element_array_buffer_target(), 0); - glBindBuffer(array_buffer_target(), 0); - -#if USE_VBO - glGenVertexArrays(2, arrays); - if (!(arrays[0] && arrays[1])) - { - ret = false; - return; - } - for (int i = 0; i < 2; i++) - { - glBindVertexArray(arrays[i]); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glBindBuffer(element_array_buffer_target(), buffers[1]); - glBindBuffer(array_buffer_target(), buffers[0]); - glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0); - glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); - glVertexAttribPointer(2, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs2)); - glVertexAttribPointer(3, 3, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, nor)); - } - glBindVertexArray(0); -#endif + buffers[0] = static_cast(mesh.value().vertex_buffer); + buffers[1] = static_cast(mesh.value().index_buffer); + arrays[0] = static_cast(mesh.value().vertex_arrays[0]); + arrays[1] = static_cast(mesh.value().vertex_arrays[1]); }); return ret; } @@ -112,43 +221,22 @@ bool Shape::create_buffers(GLvoid* vertices, int vsize) { destroy(); - glGenBuffers(1, buffers); - if (!buffers[0]) - { + const auto mesh = pp::renderer::gl::create_opengl_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()) { ret = false; return; } - - if (vsize) - { - glBindBuffer(array_buffer_target(), buffers[0]); - glBufferData(array_buffer_target(), vsize, vertices, static_draw_buffer_usage()); - glBindBuffer(element_array_buffer_target(), 0); - glBindBuffer(array_buffer_target(), 0); - } - -#if USE_VBO - glGenVertexArrays(2, arrays); - if (!(arrays[0] && arrays[1])) - { - ret = false; - return; - } - for (int i = 0; i < 2; i++) - { - glBindVertexArray(arrays[i]); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glBindBuffer(array_buffer_target(), buffers[0]); - glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0); - glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); - glVertexAttribPointer(2, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs2)); - glVertexAttribPointer(3, 3, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, nor)); - } - glBindVertexArray(0); -#endif + buffers[0] = static_cast(mesh.value().vertex_buffer); + buffers[1] = static_cast(mesh.value().index_buffer); + arrays[0] = static_cast(mesh.value().vertex_arrays[0]); + arrays[1] = static_cast(mesh.value().vertex_arrays[1]); }); return ret; } @@ -159,31 +247,16 @@ void Shape::draw_fill() const const auto type = static_cast(pp::renderer::gl::primitive_mode_for_fill_count(count[0])); App::I->render_task([=] { -#if USE_VBO - glBindVertexArray(arrays[0]); - if (use_idx) - glDrawElements(type, count[0], index_type, ioff[0]); - else - glDrawArrays(type, 0, count[0]); - glBindVertexArray(0); -#else - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glBindBuffer(array_buffer_target(), buffers[0]); - glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0); - glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); - if (use_idx) - { - glBindBuffer(element_array_buffer_target(), buffers[1]); - glDrawElements(type, count[0], uint16_index_type(), ioff[0]); - } - else - glDrawArrays(type, 0, count[0]); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glBindBuffer(array_buffer_target(), 0); - glBindBuffer(element_array_buffer_target(), 0); -#endif // USE_VBO + (void)pp::renderer::gl::draw_opengl_mesh( + pp::renderer::gl::OpenGlMeshDraw { + .vertex_array = arrays[0], + .mode = type, + .count = static_cast(count[0]), + .indexed = use_idx, + .index_type = index_type, + .index_offset = ioff[0], + }, + mesh_draw_dispatch()); }); } void Shape::draw_stroke() const @@ -193,32 +266,16 @@ void Shape::draw_stroke() const const auto type = static_cast(pp::renderer::gl::primitive_mode_for_stroke_count(count[1])); App::I->render_task([=] { -#if USE_VBO - glBindVertexArray(arrays[1]); - if (use_idx) - glDrawElements(type, count[1], index_type, ioff[1]); - else - glDrawArrays(type, 0, count[1]); - glBindVertexArray(0); -#else - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glBindBuffer(element_array_buffer_target(), buffers[1]); - glBindBuffer(array_buffer_target(), buffers[0]); - glVertexAttribPointer(0, 4, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)0); - glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); - if (use_idx) - { - glBindBuffer(element_array_buffer_target(), buffers[1]); - glDrawElements(type, count[1], uint16_index_type(), ioff[1]); - } - else - glDrawArrays(type, 0, count[1]); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glBindBuffer(array_buffer_target(), 0); - glBindBuffer(element_array_buffer_target(), 0); -#endif // USE_VBO + (void)pp::renderer::gl::draw_opengl_mesh( + pp::renderer::gl::OpenGlMeshDraw { + .vertex_array = arrays[1], + .mode = type, + .count = static_cast(count[1]), + .indexed = use_idx, + .index_type = index_type, + .index_offset = ioff[1], + }, + mesh_draw_dispatch()); }); } @@ -226,18 +283,12 @@ void Shape::destroy() { if (App::I) App::I->render_task_async([b1=buffers[0],b2=buffers[1],a1=arrays[0],a2=arrays[1]] { - if (b1 || b2) - { - glDeleteBuffers(1, &b1); - glDeleteBuffers(1, &b2); - } -#if USE_VBO - if (a1 || a2) - { - glDeleteVertexArrays(1, &a1); - glDeleteVertexArrays(1, &a2); - } -#endif // USE_VBO + (void)pp::renderer::gl::delete_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshDelete { + .buffers = { b1, b2 }, + .vertex_arrays = { a1, a2 }, + }, + mesh_delete_dispatch()); }); buffers[0] = buffers[1] = 0; arrays[0] = arrays[1] = 0; @@ -523,8 +574,15 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g App::I->render_task([this] { - glBindBuffer(array_buffer_target(), buffers[0]); - glBufferData(array_buffer_target(), sizeof(vertices), vertices, static_draw_buffer_usage()); + (void)pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = pp::renderer::gl::array_buffer_target(), + .buffer_id = buffers[0], + .data = vertices, + .byte_count = static_cast(sizeof(vertices)), + .usage = pp::renderer::gl::static_draw_buffer_usage(), + }, + buffer_upload_dispatch()); static GLushort idx[6 + 8]{ 0, 1, 2, 0, 2, 3, @@ -533,11 +591,15 @@ void Plane::update_vertices(const glm::vec4* data, const glm::vec2* uvs, const g 2, 3, 3, 0, }; - glBindBuffer(element_array_buffer_target(), buffers[1]); - glBufferData(element_array_buffer_target(), sizeof(idx), idx, static_draw_buffer_usage()); - - glBindBuffer(array_buffer_target(), 0); - glBindBuffer(element_array_buffer_target(), 0); + (void)pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = pp::renderer::gl::element_array_buffer_target(), + .buffer_id = buffers[1], + .data = idx, + .byte_count = static_cast(sizeof(idx)), + .usage = pp::renderer::gl::static_draw_buffer_usage(), + }, + buffer_upload_dispatch()); }); } void Circle::create_impl(float radius, int div, GLushort* idx, vertex_t* vertices) @@ -796,9 +858,15 @@ 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 - glBindBuffer(array_buffer_target(), buffers[0]); - glBufferData(array_buffer_target(), sizeof(vertices), vertices, static_draw_buffer_usage()); - glBindBuffer(array_buffer_target(), 0); + (void)pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = pp::renderer::gl::array_buffer_target(), + .buffer_id = buffers[0], + .data = vertices, + .byte_count = static_cast(sizeof(vertices)), + .usage = pp::renderer::gl::static_draw_buffer_usage(), + }, + buffer_upload_dispatch()); }); } void DynamicShape::update_vertices(vertex_t* vertices, int vcount) @@ -807,8 +875,14 @@ void DynamicShape::update_vertices(vertex_t* vertices, int vcount) { count[0] = vcount; count[1] = vcount; - glBindBuffer(array_buffer_target(), buffers[0]); - glBufferData(array_buffer_target(), sizeof(vertex_t) * vcount, vertices, static_draw_buffer_usage()); - glBindBuffer(array_buffer_target(), 0); + (void)pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = pp::renderer::gl::array_buffer_target(), + .buffer_id = buffers[0], + .data = vertices, + .byte_count = static_cast(sizeof(vertex_t) * vcount), + .usage = pp::renderer::gl::static_draw_buffer_usage(), + }, + buffer_upload_dispatch()); }); } diff --git a/tests/renderer_gl/capabilities_tests.cpp b/tests/renderer_gl/capabilities_tests.cpp index 529b034..39f250e 100644 --- a/tests/renderer_gl/capabilities_tests.cpp +++ b/tests/renderer_gl/capabilities_tests.cpp @@ -135,6 +135,36 @@ struct RecordedOpenGlActiveUniformCall { std::int32_t capacity = 0; }; +struct RecordedOpenGlBufferBindCall { + std::uint32_t target = 0; + std::uint32_t buffer = 0; +}; + +struct RecordedOpenGlBufferDataCall { + std::uint32_t target = 0; + std::intptr_t byte_count = 0; + const void* data = nullptr; + std::uint32_t usage = 0; +}; + +struct RecordedOpenGlVertexAttribPointerCall { + 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; + const void* offset = nullptr; +}; + +struct RecordedOpenGlMeshDrawCall { + bool indexed = false; + std::uint32_t mode = 0; + std::int32_t first = 0; + std::int32_t count = 0; + std::uint32_t index_type = 0; + const void* index_offset = nullptr; +}; + std::vector recorded_state_calls; std::vector recorded_string_queries; std::vector recorded_clear_calls; @@ -175,11 +205,23 @@ std::vector recorded_program_integer_queries; std::vector recorded_active_uniform_calls; std::vector recorded_uniform_location_programs; std::vector recorded_uniform_location_names; +std::vector recorded_generated_buffer_counts; +std::vector recorded_deleted_buffers; +std::vector recorded_generated_vertex_array_counts; +std::vector recorded_deleted_vertex_arrays; +std::vector recorded_buffer_bind_calls; +std::vector recorded_buffer_data_calls; +std::vector recorded_vertex_array_bind_calls; +std::vector recorded_enabled_vertex_attributes; +std::vector recorded_vertex_attrib_pointer_calls; +std::vector recorded_mesh_draw_calls; std::uint32_t next_texture_id = 91U; std::uint32_t next_framebuffer_id = 44U; std::uint32_t next_sampler_id = 71U; std::uint32_t next_shader_id = 301U; std::uint32_t next_program_id = 401U; +std::uint32_t next_buffer_id = 501U; +std::uint32_t next_vertex_array_id = 601U; std::int32_t configured_shader_compile_status = 1; std::int32_t configured_program_link_status = 1; std::int32_t configured_active_uniform_count = 2; @@ -792,6 +834,120 @@ std::int32_t record_get_uniform_location(std::uint32_t program, const char* name return -1; } +void record_gen_buffers(std::uint32_t count, std::uint32_t* ids) noexcept +{ + recorded_generated_buffer_counts.push_back(count); + if (ids == nullptr) { + return; + } + for (std::uint32_t i = 0; i < count; ++i) { + ids[i] = next_buffer_id++; + } +} + +void record_delete_buffers(std::uint32_t count, const std::uint32_t* ids) noexcept +{ + for (std::uint32_t i = 0; i < count; ++i) { + recorded_deleted_buffers.push_back(ids == nullptr ? 0U : ids[i]); + } +} + +void record_bind_buffer(std::uint32_t target, std::uint32_t buffer) noexcept +{ + recorded_buffer_bind_calls.push_back(RecordedOpenGlBufferBindCall { + .target = target, + .buffer = buffer, + }); +} + +void record_buffer_data( + std::uint32_t target, + std::intptr_t byte_count, + const void* data, + std::uint32_t usage) noexcept +{ + recorded_buffer_data_calls.push_back(RecordedOpenGlBufferDataCall { + .target = target, + .byte_count = byte_count, + .data = data, + .usage = usage, + }); +} + +void record_gen_vertex_arrays(std::uint32_t count, std::uint32_t* ids) noexcept +{ + recorded_generated_vertex_array_counts.push_back(count); + if (ids == nullptr) { + return; + } + for (std::uint32_t i = 0; i < count; ++i) { + ids[i] = next_vertex_array_id++; + } +} + +void record_delete_vertex_arrays(std::uint32_t count, const std::uint32_t* ids) noexcept +{ + for (std::uint32_t i = 0; i < count; ++i) { + recorded_deleted_vertex_arrays.push_back(ids == nullptr ? 0U : ids[i]); + } +} + +void record_bind_vertex_array(std::uint32_t vertex_array) noexcept +{ + recorded_vertex_array_bind_calls.push_back(vertex_array); +} + +void record_enable_vertex_attrib_array(std::uint32_t index) noexcept +{ + recorded_enabled_vertex_attributes.push_back(index); +} + +void record_vertex_attrib_pointer( + 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 +{ + recorded_vertex_attrib_pointer_calls.push_back(RecordedOpenGlVertexAttribPointerCall { + .index = index, + .component_count = component_count, + .component_type = component_type, + .normalized = normalized, + .stride = stride, + .offset = offset, + }); +} + +void record_draw_elements( + std::uint32_t mode, + std::int32_t count, + std::uint32_t index_type, + const void* index_offset) noexcept +{ + recorded_mesh_draw_calls.push_back(RecordedOpenGlMeshDrawCall { + .indexed = true, + .mode = mode, + .count = count, + .index_type = index_type, + .index_offset = index_offset, + }); +} + +void record_draw_arrays( + std::uint32_t mode, + std::int32_t first, + std::int32_t count) noexcept +{ + recorded_mesh_draw_calls.push_back(RecordedOpenGlMeshDrawCall { + .indexed = false, + .mode = mode, + .first = first, + .count = count, + }); +} + void detects_common_extension_capabilities(pp::tests::Harness& h) { constexpr std::array extensions { @@ -2771,6 +2927,352 @@ void rejects_invalid_uniform_discovery_dispatch(pp::tests::Harness& h) PP_EXPECT(h, invalid_uniform_name.status().code == pp::foundation::StatusCode::invalid_argument); } +void creates_indexed_mesh_objects_through_dispatch(pp::tests::Harness& h) +{ + recorded_generated_buffer_counts.clear(); + recorded_generated_vertex_array_counts.clear(); + recorded_buffer_bind_calls.clear(); + recorded_buffer_data_calls.clear(); + recorded_vertex_array_bind_calls.clear(); + recorded_enabled_vertex_attributes.clear(); + recorded_vertex_attrib_pointer_calls.clear(); + next_buffer_id = 501U; + next_vertex_array_id = 601U; + + constexpr std::array vertices {}; + constexpr std::array indices { 0, 1, 2, 0, 2, 3 }; + constexpr std::array attributes { + pp::renderer::gl::OpenGlVertexAttribute { + .index = 0U, + .component_count = 4, + .component_type = 0x1406U, + .normalized = 0U, + .stride = 32, + .offset = 0U, + }, + pp::renderer::gl::OpenGlVertexAttribute { + .index = 1U, + .component_count = 2, + .component_type = 0x1406U, + .normalized = 0U, + .stride = 32, + .offset = 16U, + }, + }; + + const auto mesh = pp::renderer::gl::create_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshUpload { + .vertex_data = vertices.data(), + .vertex_byte_count = static_cast(sizeof(float) * vertices.size()), + .index_data = indices.data(), + .index_byte_count = static_cast(sizeof(std::uint16_t) * indices.size()), + .indexed = true, + .attributes = attributes, + }, + pp::renderer::gl::OpenGlMeshCreateDispatch { + .gen_buffers = record_gen_buffers, + .bind_buffer = record_bind_buffer, + .buffer_data = record_buffer_data, + .gen_vertex_arrays = record_gen_vertex_arrays, + .bind_vertex_array = record_bind_vertex_array, + .enable_vertex_attrib_array = record_enable_vertex_attrib_array, + .vertex_attrib_pointer = record_vertex_attrib_pointer, + }); + + PP_EXPECT(h, mesh.ok()); + PP_EXPECT(h, mesh.value().vertex_buffer == 501U); + PP_EXPECT(h, mesh.value().index_buffer == 502U); + PP_EXPECT(h, mesh.value().vertex_arrays[0] == 601U); + PP_EXPECT(h, mesh.value().vertex_arrays[1] == 602U); + PP_EXPECT(h, recorded_generated_buffer_counts.size() == 1U); + PP_EXPECT(h, recorded_generated_buffer_counts[0] == 2U); + PP_EXPECT(h, recorded_generated_vertex_array_counts.size() == 1U); + PP_EXPECT(h, recorded_generated_vertex_array_counts[0] == 2U); + PP_EXPECT(h, recorded_buffer_data_calls.size() == 2U); + PP_EXPECT(h, recorded_buffer_data_calls[0].target == 0x8893U); + PP_EXPECT(h, recorded_buffer_data_calls[0].byte_count == static_cast(sizeof(std::uint16_t) * indices.size())); + PP_EXPECT(h, recorded_buffer_data_calls[0].data == indices.data()); + PP_EXPECT(h, recorded_buffer_data_calls[0].usage == 0x88E4U); + PP_EXPECT(h, recorded_buffer_data_calls[1].target == 0x8892U); + PP_EXPECT(h, recorded_buffer_data_calls[1].byte_count == static_cast(sizeof(float) * vertices.size())); + PP_EXPECT(h, recorded_buffer_data_calls[1].data == vertices.data()); + PP_EXPECT(h, recorded_vertex_array_bind_calls.size() == 3U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[0] == 601U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[1] == 602U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[2] == 0U); + PP_EXPECT(h, recorded_enabled_vertex_attributes.size() == 4U); + PP_EXPECT(h, recorded_enabled_vertex_attributes[0] == 0U); + PP_EXPECT(h, recorded_enabled_vertex_attributes[1] == 1U); + PP_EXPECT(h, recorded_enabled_vertex_attributes[2] == 0U); + PP_EXPECT(h, recorded_enabled_vertex_attributes[3] == 1U); + PP_EXPECT(h, recorded_vertex_attrib_pointer_calls.size() == 4U); + PP_EXPECT(h, recorded_vertex_attrib_pointer_calls[0].index == 0U); + PP_EXPECT(h, recorded_vertex_attrib_pointer_calls[0].component_count == 4); + PP_EXPECT(h, recorded_vertex_attrib_pointer_calls[0].component_type == 0x1406U); + PP_EXPECT(h, recorded_vertex_attrib_pointer_calls[1].offset == reinterpret_cast(16U)); +} + +void creates_dynamic_mesh_without_initial_vertex_upload(pp::tests::Harness& h) +{ + recorded_generated_buffer_counts.clear(); + recorded_buffer_data_calls.clear(); + recorded_vertex_array_bind_calls.clear(); + next_buffer_id = 701U; + next_vertex_array_id = 801U; + + constexpr std::array attributes { + pp::renderer::gl::OpenGlVertexAttribute { + .index = 0U, + .component_count = 4, + .component_type = 0x1406U, + .normalized = 0U, + .stride = 32, + .offset = 0U, + }, + }; + + const auto mesh = pp::renderer::gl::create_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshUpload { + .vertex_data = nullptr, + .vertex_byte_count = 0, + .index_data = nullptr, + .index_byte_count = 0, + .indexed = false, + .attributes = attributes, + }, + pp::renderer::gl::OpenGlMeshCreateDispatch { + .gen_buffers = record_gen_buffers, + .bind_buffer = record_bind_buffer, + .buffer_data = record_buffer_data, + .gen_vertex_arrays = record_gen_vertex_arrays, + .bind_vertex_array = record_bind_vertex_array, + .enable_vertex_attrib_array = record_enable_vertex_attrib_array, + .vertex_attrib_pointer = record_vertex_attrib_pointer, + }); + + PP_EXPECT(h, mesh.ok()); + PP_EXPECT(h, mesh.value().vertex_buffer == 701U); + PP_EXPECT(h, mesh.value().index_buffer == 0U); + PP_EXPECT(h, recorded_generated_buffer_counts.size() == 1U); + PP_EXPECT(h, recorded_generated_buffer_counts[0] == 1U); + PP_EXPECT(h, recorded_buffer_data_calls.empty()); + PP_EXPECT(h, recorded_vertex_array_bind_calls.size() == 3U); +} + +void updates_draws_and_deletes_mesh_through_dispatch(pp::tests::Harness& h) +{ + recorded_buffer_bind_calls.clear(); + recorded_buffer_data_calls.clear(); + recorded_vertex_array_bind_calls.clear(); + recorded_mesh_draw_calls.clear(); + recorded_deleted_buffers.clear(); + recorded_deleted_vertex_arrays.clear(); + + constexpr std::array vertices {}; + const auto upload_status = pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = 0x8892U, + .buffer_id = 501U, + .data = vertices.data(), + .byte_count = static_cast(sizeof(float) * vertices.size()), + .usage = 0x88E4U, + }, + pp::renderer::gl::OpenGlBufferUploadDispatch { + .bind_buffer = record_bind_buffer, + .buffer_data = record_buffer_data, + }); + const auto indexed_draw = pp::renderer::gl::draw_opengl_mesh( + pp::renderer::gl::OpenGlMeshDraw { + .vertex_array = 601U, + .mode = 0x0004U, + .count = 6, + .indexed = true, + .index_type = 0x1403U, + .index_offset = reinterpret_cast(12U), + }, + pp::renderer::gl::OpenGlMeshDrawDispatch { + .bind_vertex_array = record_bind_vertex_array, + .draw_elements = record_draw_elements, + .draw_arrays = record_draw_arrays, + }); + const auto array_draw = pp::renderer::gl::draw_opengl_mesh( + pp::renderer::gl::OpenGlMeshDraw { + .vertex_array = 602U, + .mode = 0x0001U, + .count = 2, + .indexed = false, + }, + pp::renderer::gl::OpenGlMeshDrawDispatch { + .bind_vertex_array = record_bind_vertex_array, + .draw_elements = record_draw_elements, + .draw_arrays = record_draw_arrays, + }); + const auto delete_status = pp::renderer::gl::delete_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshDelete { + .buffers = { 501U, 502U }, + .vertex_arrays = { 601U, 602U }, + }, + pp::renderer::gl::OpenGlMeshDeleteDispatch { + .delete_buffers = record_delete_buffers, + .delete_vertex_arrays = record_delete_vertex_arrays, + }); + + PP_EXPECT(h, upload_status.ok()); + PP_EXPECT(h, indexed_draw.ok()); + PP_EXPECT(h, array_draw.ok()); + PP_EXPECT(h, delete_status.ok()); + PP_EXPECT(h, recorded_buffer_bind_calls.size() >= 2U); + PP_EXPECT(h, recorded_buffer_bind_calls[0].target == 0x8892U); + PP_EXPECT(h, recorded_buffer_bind_calls[0].buffer == 501U); + PP_EXPECT(h, recorded_buffer_bind_calls[1].target == 0x8892U); + PP_EXPECT(h, recorded_buffer_bind_calls[1].buffer == 0U); + PP_EXPECT(h, recorded_buffer_data_calls.size() == 1U); + PP_EXPECT(h, recorded_buffer_data_calls[0].data == vertices.data()); + PP_EXPECT(h, recorded_vertex_array_bind_calls.size() == 4U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[0] == 601U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[1] == 0U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[2] == 602U); + PP_EXPECT(h, recorded_vertex_array_bind_calls[3] == 0U); + PP_EXPECT(h, recorded_mesh_draw_calls.size() == 2U); + PP_EXPECT(h, recorded_mesh_draw_calls[0].indexed); + PP_EXPECT(h, recorded_mesh_draw_calls[0].mode == 0x0004U); + PP_EXPECT(h, recorded_mesh_draw_calls[0].count == 6); + PP_EXPECT(h, recorded_mesh_draw_calls[0].index_type == 0x1403U); + PP_EXPECT(h, recorded_mesh_draw_calls[0].index_offset == reinterpret_cast(12U)); + PP_EXPECT(h, !recorded_mesh_draw_calls[1].indexed); + PP_EXPECT(h, recorded_mesh_draw_calls[1].first == 0); + PP_EXPECT(h, recorded_deleted_buffers.size() == 2U); + PP_EXPECT(h, recorded_deleted_buffers[0] == 501U); + PP_EXPECT(h, recorded_deleted_buffers[1] == 502U); + PP_EXPECT(h, recorded_deleted_vertex_arrays.size() == 2U); + PP_EXPECT(h, recorded_deleted_vertex_arrays[0] == 601U); + PP_EXPECT(h, recorded_deleted_vertex_arrays[1] == 602U); +} + +void rejects_invalid_mesh_dispatch(pp::tests::Harness& h) +{ + constexpr std::array vertices {}; + constexpr std::array attributes { + pp::renderer::gl::OpenGlVertexAttribute { + .index = 0U, + .component_count = 4, + .component_type = 0x1406U, + .normalized = 0U, + .stride = 16, + .offset = 0U, + }, + }; + constexpr std::array bad_attributes { + pp::renderer::gl::OpenGlVertexAttribute { + .index = 0U, + .component_count = 0, + .component_type = 0x1406U, + .normalized = 0U, + .stride = 16, + .offset = 0U, + }, + }; + + const auto missing_create_dispatch = pp::renderer::gl::create_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshUpload { + .vertex_data = vertices.data(), + .vertex_byte_count = static_cast(sizeof(float) * vertices.size()), + .attributes = attributes, + }, + pp::renderer::gl::OpenGlMeshCreateDispatch { + .gen_buffers = record_gen_buffers, + }); + const auto invalid_create_data = pp::renderer::gl::create_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshUpload { + .vertex_data = nullptr, + .vertex_byte_count = 4, + .attributes = attributes, + }, + pp::renderer::gl::OpenGlMeshCreateDispatch { + .gen_buffers = record_gen_buffers, + .bind_buffer = record_bind_buffer, + .buffer_data = record_buffer_data, + .gen_vertex_arrays = record_gen_vertex_arrays, + .bind_vertex_array = record_bind_vertex_array, + .enable_vertex_attrib_array = record_enable_vertex_attrib_array, + .vertex_attrib_pointer = record_vertex_attrib_pointer, + }); + const auto invalid_attribute = pp::renderer::gl::create_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshUpload { + .vertex_data = vertices.data(), + .vertex_byte_count = static_cast(sizeof(float) * vertices.size()), + .attributes = bad_attributes, + }, + pp::renderer::gl::OpenGlMeshCreateDispatch { + .gen_buffers = record_gen_buffers, + .bind_buffer = record_bind_buffer, + .buffer_data = record_buffer_data, + .gen_vertex_arrays = record_gen_vertex_arrays, + .bind_vertex_array = record_bind_vertex_array, + .enable_vertex_attrib_array = record_enable_vertex_attrib_array, + .vertex_attrib_pointer = record_vertex_attrib_pointer, + }); + const auto missing_upload_dispatch = pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = 0x8892U, + .buffer_id = 1U, + .data = vertices.data(), + .byte_count = static_cast(sizeof(float) * vertices.size()), + .usage = 0x88E4U, + }, + pp::renderer::gl::OpenGlBufferUploadDispatch {}); + const auto invalid_upload = pp::renderer::gl::upload_opengl_buffer_data( + pp::renderer::gl::OpenGlBufferUpload { + .target = 0x8892U, + .buffer_id = 0U, + .data = vertices.data(), + .byte_count = static_cast(sizeof(float) * vertices.size()), + .usage = 0x88E4U, + }, + pp::renderer::gl::OpenGlBufferUploadDispatch { + .bind_buffer = record_bind_buffer, + .buffer_data = record_buffer_data, + }); + const auto missing_draw_dispatch = pp::renderer::gl::draw_opengl_mesh( + pp::renderer::gl::OpenGlMeshDraw { + .vertex_array = 1U, + .mode = 0x0004U, + .count = 6, + }, + pp::renderer::gl::OpenGlMeshDrawDispatch {}); + const auto invalid_draw = pp::renderer::gl::draw_opengl_mesh( + pp::renderer::gl::OpenGlMeshDraw { + .vertex_array = 0U, + .mode = 0x0004U, + .count = 6, + }, + pp::renderer::gl::OpenGlMeshDrawDispatch { + .bind_vertex_array = record_bind_vertex_array, + .draw_elements = record_draw_elements, + .draw_arrays = record_draw_arrays, + }); + const auto missing_delete_dispatch = pp::renderer::gl::delete_opengl_mesh_objects( + pp::renderer::gl::OpenGlMeshDelete {}, + pp::renderer::gl::OpenGlMeshDeleteDispatch {}); + + PP_EXPECT(h, !missing_create_dispatch.ok()); + PP_EXPECT(h, missing_create_dispatch.status().code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !invalid_create_data.ok()); + PP_EXPECT(h, invalid_create_data.status().code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !invalid_attribute.ok()); + PP_EXPECT(h, invalid_attribute.status().code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !missing_upload_dispatch.ok()); + PP_EXPECT(h, missing_upload_dispatch.code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !invalid_upload.ok()); + PP_EXPECT(h, invalid_upload.code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !missing_draw_dispatch.ok()); + PP_EXPECT(h, missing_draw_dispatch.code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !invalid_draw.ok()); + PP_EXPECT(h, invalid_draw.code == pp::foundation::StatusCode::invalid_argument); + PP_EXPECT(h, !missing_delete_dispatch.ok()); + PP_EXPECT(h, missing_delete_dispatch.code == pp::foundation::StatusCode::invalid_argument); +} + void updates_texture_2d_through_dispatch(pp::tests::Harness& h) { recorded_binding_calls.clear(); @@ -3565,6 +4067,10 @@ int main() harness.run("rejects_invalid_shader_program_link_dispatch", rejects_invalid_shader_program_link_dispatch); harness.run("discovers_program_uniforms_through_dispatch", discovers_program_uniforms_through_dispatch); harness.run("rejects_invalid_uniform_discovery_dispatch", rejects_invalid_uniform_discovery_dispatch); + harness.run("creates_indexed_mesh_objects_through_dispatch", creates_indexed_mesh_objects_through_dispatch); + harness.run("creates_dynamic_mesh_without_initial_vertex_upload", creates_dynamic_mesh_without_initial_vertex_upload); + harness.run("updates_draws_and_deletes_mesh_through_dispatch", updates_draws_and_deletes_mesh_through_dispatch); + harness.run("rejects_invalid_mesh_dispatch", rejects_invalid_mesh_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);