Route font mesh operations through renderer GL
This commit is contained in:
240
src/font.cpp
240
src/font.cpp
@@ -7,6 +7,10 @@
|
||||
#include "app.h"
|
||||
#include "renderer_gl/opengl_capabilities.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace {
|
||||
|
||||
[[nodiscard]] GLint font_atlas_internal_format() noexcept
|
||||
@@ -19,46 +23,133 @@ namespace {
|
||||
return static_cast<GLint>(pp::renderer::gl::texture_format_for_channel_count(1U).pixel_format);
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum element_array_buffer_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::element_array_buffer_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum array_buffer_target() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::array_buffer_target());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum static_draw_buffer_usage() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::static_draw_buffer_usage());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum vertex_attribute_float_component_type() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::vertex_attribute_float_component_type());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLboolean vertex_attribute_not_normalized() noexcept
|
||||
{
|
||||
return static_cast<GLboolean>(pp::renderer::gl::vertex_attribute_not_normalized());
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum text_mesh_primitive_mode() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::primitive_mode_for_fill_count(3U));
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum text_mesh_index_type() noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::index_type_for_index_size(sizeof(GLushort)));
|
||||
}
|
||||
|
||||
[[nodiscard]] GLenum texture_unit(std::uint32_t unit_index) noexcept
|
||||
{
|
||||
return static_cast<GLenum>(pp::renderer::gl::active_texture_unit(unit_index));
|
||||
}
|
||||
|
||||
void gen_buffers_adapter(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
glGenBuffers(static_cast<GLsizei>(count), ids);
|
||||
}
|
||||
|
||||
void bind_buffer_adapter(std::uint32_t target, std::uint32_t buffer) noexcept
|
||||
{
|
||||
glBindBuffer(static_cast<GLenum>(target), static_cast<GLuint>(buffer));
|
||||
}
|
||||
|
||||
void buffer_data_adapter(
|
||||
std::uint32_t target,
|
||||
std::intptr_t byte_count,
|
||||
const void* data,
|
||||
std::uint32_t usage) noexcept
|
||||
{
|
||||
glBufferData(static_cast<GLenum>(target), static_cast<GLsizeiptr>(byte_count), data, static_cast<GLenum>(usage));
|
||||
}
|
||||
|
||||
void gen_vertex_arrays_adapter(std::uint32_t count, std::uint32_t* ids) noexcept
|
||||
{
|
||||
glGenVertexArrays(static_cast<GLsizei>(count), ids);
|
||||
}
|
||||
|
||||
void 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> text_mesh_vertex_attributes() noexcept
|
||||
{
|
||||
static const std::array<pp::renderer::gl::OpenGlVertexAttribute, 2> attributes {
|
||||
pp::renderer::gl::OpenGlVertexAttribute {
|
||||
.index = 0U,
|
||||
.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(glm::vec4)),
|
||||
.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(glm::vec4)),
|
||||
.offset = static_cast<std::uintptr_t>(sizeof(float) * 2),
|
||||
},
|
||||
};
|
||||
return attributes;
|
||||
}
|
||||
|
||||
[[nodiscard]] pp::renderer::gl::OpenGlMeshCreateDispatch text_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 text_buffer_upload_dispatch() noexcept
|
||||
{
|
||||
return pp::renderer::gl::OpenGlBufferUploadDispatch {
|
||||
.bind_buffer = bind_buffer_adapter,
|
||||
.buffer_data = buffer_data_adapter,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] pp::renderer::gl::OpenGlMeshDrawDispatch text_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,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::map<std::string, Font> FontManager::m_fonts;
|
||||
@@ -210,18 +301,22 @@ bool TextMesh::create()
|
||||
{
|
||||
App::I->render_task([this]
|
||||
{
|
||||
glGenBuffers(2, font_buffers);
|
||||
#if USE_VBO
|
||||
glGenVertexArrays(1, &font_array);
|
||||
glBindVertexArray(font_array);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(element_array_buffer_target(), font_buffers[1]);
|
||||
glBindBuffer(array_buffer_target(), font_buffers[0]);
|
||||
glVertexAttribPointer(0, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(glm::vec4), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(glm::vec4), (GLvoid*)(sizeof(float) * 2));
|
||||
glBindVertexArray(0);
|
||||
#endif // USE_VBO
|
||||
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 = true,
|
||||
.vertex_array_count = 1U,
|
||||
.attributes = text_mesh_vertex_attributes(),
|
||||
},
|
||||
text_mesh_create_dispatch());
|
||||
if (mesh.ok()) {
|
||||
font_buffers[0] = static_cast<GLuint>(mesh.value().vertex_buffer);
|
||||
font_buffers[1] = static_cast<GLuint>(mesh.value().index_buffer);
|
||||
font_array = static_cast<GLuint>(mesh.value().vertex_arrays[0]);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -309,12 +404,24 @@ void TextMesh::update(const std::string& text, const std::string& font, int size
|
||||
font_array_count = (int)idx.size();
|
||||
App::I->render_task([&]
|
||||
{
|
||||
glBindBuffer(element_array_buffer_target(), font_buffers[1]);
|
||||
glBufferData(element_array_buffer_target(), idx.size() * sizeof(GLushort), idx.data(), static_draw_buffer_usage());
|
||||
glBindBuffer(array_buffer_target(), font_buffers[0]);
|
||||
glBufferData(array_buffer_target(), v.size() * sizeof(glm::vec4), v.data(), 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 = font_buffers[1],
|
||||
.data = idx.data(),
|
||||
.byte_count = static_cast<std::intptr_t>(idx.size() * sizeof(GLushort)),
|
||||
.usage = pp::renderer::gl::static_draw_buffer_usage(),
|
||||
},
|
||||
text_buffer_upload_dispatch());
|
||||
(void)pp::renderer::gl::upload_opengl_buffer_data(
|
||||
pp::renderer::gl::OpenGlBufferUpload {
|
||||
.target = pp::renderer::gl::array_buffer_target(),
|
||||
.buffer_id = font_buffers[0],
|
||||
.data = v.data(),
|
||||
.byte_count = static_cast<std::intptr_t>(v.size() * sizeof(glm::vec4)),
|
||||
.usage = pp::renderer::gl::static_draw_buffer_usage(),
|
||||
},
|
||||
text_buffer_upload_dispatch());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -328,23 +435,16 @@ void TextMesh::draw()
|
||||
f.font_tex.bind();
|
||||
FontManager::m_sampler.bind(0);
|
||||
|
||||
#if USE_VBO
|
||||
glBindVertexArray(font_array);
|
||||
glDrawElements(text_mesh_primitive_mode(), font_array_count, text_mesh_index_type(), 0);
|
||||
glBindVertexArray(0);
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(element_array_buffer_target(), font_buffers[1]);
|
||||
glBindBuffer(array_buffer_target(), font_buffers[0]);
|
||||
glVertexAttribPointer(0, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(glm::vec4), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, vertex_attribute_float_component_type(), vertex_attribute_not_normalized(), sizeof(glm::vec4), (GLvoid*)(sizeof(float) * 2));
|
||||
glDrawElements(text_mesh_primitive_mode(), font_array_count, text_mesh_index_type(), 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 = font_array,
|
||||
.mode = pp::renderer::gl::primitive_mode_for_fill_count(3U),
|
||||
.count = font_array_count,
|
||||
.indexed = true,
|
||||
.index_type = pp::renderer::gl::index_type_for_index_size(sizeof(GLushort)),
|
||||
.index_offset = nullptr,
|
||||
},
|
||||
text_mesh_draw_dispatch());
|
||||
|
||||
f.font_tex.unbind();
|
||||
FontManager::m_sampler.unbind();
|
||||
|
||||
Reference in New Issue
Block a user