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;
|
||||
|
||||
386
src/shape.cpp
386
src/shape.cpp
@@ -4,38 +4,166 @@
|
||||
#include "app.h"
|
||||
#include "renderer_gl/opengl_capabilities.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] GLenum array_buffer_target() noexcept
|
||||
void gen_buffers_adapter(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::array_buffer_target());
|
||||
glGenBuffers(static_cast<GLsizei>(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<GLenum>(pp::renderer::gl::element_array_buffer_target());
|
||||
glDeleteBuffers(static_cast<GLsizei>(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<GLenum>(pp::renderer::gl::static_draw_buffer_usage());
|
||||
glBindBuffer(static_cast<GLenum>(target), static_cast<GLuint>(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<GLenum>(pp::renderer::gl::vertex_attribute_float_component_type());
|
||||
glBufferData(static_cast<GLenum>(target), static_cast<GLsizeiptr>(byte_count), data, static_cast<GLenum>(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<GLboolean>(pp::renderer::gl::vertex_attribute_not_normalized());
|
||||
glGenVertexArrays(static_cast<GLsizei>(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<GLenum>(pp::renderer::gl::index_type_for_index_size(sizeof(GLushort)));
|
||||
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 {
|
||||
pp::renderer::gl::OpenGlVertexAttribute {
|
||||
.index = 0U,
|
||||
.component_count = 4,
|
||||
.component_type = pp::renderer::gl::vertex_attribute_float_component_type(),
|
||||
.normalized = static_cast<std::uint8_t>(pp::renderer::gl::vertex_attribute_not_normalized()),
|
||||
.stride = static_cast<std::int32_t>(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<std::uint8_t>(pp::renderer::gl::vertex_attribute_not_normalized()),
|
||||
.stride = static_cast<std::int32_t>(sizeof(vertex_t)),
|
||||
.offset = static_cast<std::uintptr_t>(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<std::uint8_t>(pp::renderer::gl::vertex_attribute_not_normalized()),
|
||||
.stride = static_cast<std::int32_t>(sizeof(vertex_t)),
|
||||
.offset = static_cast<std::uintptr_t>(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<std::uint8_t>(pp::renderer::gl::vertex_attribute_not_normalized()),
|
||||
.stride = static_cast<std::int32_t>(sizeof(vertex_t)),
|
||||
.offset = static_cast<std::uintptr_t>(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<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]);
|
||||
});
|
||||
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<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]);
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
@@ -159,31 +247,16 @@ 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([=]
|
||||
{
|
||||
#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<std::int32_t>(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<GLenum>(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<std::int32_t>(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<std::intptr_t>(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<std::intptr_t>(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<std::intptr_t>(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<std::intptr_t>(sizeof(vertex_t) * vcount),
|
||||
.usage = pp::renderer::gl::static_draw_buffer_usage(),
|
||||
},
|
||||
buffer_upload_dispatch());
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user