batch rendering
This commit is contained in:
@@ -25,6 +25,7 @@ namespace OpenEngine {
|
||||
// utility uniform functions
|
||||
void UploadBool(const std::string &name, bool value) const;
|
||||
void UploadInt(const std::string &name, int value) const;
|
||||
void UploadIntArray(const std::string &name, int* values, uint32_t count) const;
|
||||
void UploadFloat(const std::string &name, float value) const;
|
||||
void UploadMat4(const std::string &name, const glm::mat4& value) const;
|
||||
void UploadVec2(const std::string &name, const glm::vec2& value) const;
|
||||
@@ -33,6 +34,7 @@ namespace OpenEngine {
|
||||
|
||||
virtual void SetBool(const std::string &name, bool value) const override;
|
||||
virtual void SetInt(const std::string &name, int value) const override;
|
||||
virtual void SetIntArray(const std::string &name, int* values, uint32_t count) const override;
|
||||
virtual void SetFloat(const std::string &name, float value) const override;
|
||||
virtual void SetMat4(const std::string &name, const glm::mat4& value) const override;
|
||||
virtual void SetVec2(const std::string &name, const glm::vec2& value) const override;
|
||||
|
||||
@@ -15,10 +15,12 @@ namespace OpenEngine {
|
||||
|
||||
virtual uint32_t GetWidth() const override { return width; };
|
||||
virtual uint32_t GetHeight() const override { return height; };
|
||||
virtual uint32_t GetID() const override { return id; };
|
||||
|
||||
virtual void Bind(uint32_t slot = 0) const override;
|
||||
|
||||
virtual void SetData(void* data, uint32_t size) override;
|
||||
virtual bool operator==(const Texture& other) const override;
|
||||
|
||||
private:
|
||||
std::string path;
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace OpenEngine {
|
||||
|
||||
virtual void SetBool(const std::string &name, bool value) const = 0;
|
||||
virtual void SetInt(const std::string &name, int value) const = 0;
|
||||
virtual void SetIntArray(const std::string &name, int* values, uint32_t count) const = 0;
|
||||
virtual void SetFloat(const std::string &name, float value) const = 0;
|
||||
virtual void SetMat4(const std::string &name, const glm::mat4& value) const = 0;
|
||||
virtual void SetVec2(const std::string &name, const glm::vec2& value) const = 0;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace OpenEngine {
|
||||
class Texture
|
||||
{
|
||||
@@ -13,10 +14,12 @@ namespace OpenEngine {
|
||||
|
||||
virtual uint32_t GetWidth() const = 0;
|
||||
virtual uint32_t GetHeight() const = 0;
|
||||
virtual uint32_t GetID() const = 0;
|
||||
|
||||
virtual void Bind(uint32_t slot = 0) const = 0;
|
||||
|
||||
virtual void SetData(void* data, uint32_t size) = 0;
|
||||
virtual bool operator==(const Texture& other) const = 0;
|
||||
};
|
||||
|
||||
class Texture2D : public Texture
|
||||
@@ -25,6 +28,7 @@ namespace OpenEngine {
|
||||
static Ref<Texture2D> Create(uint32_t width, uint32_t height);
|
||||
static Ref<Texture2D> Create(const std::string& path);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // TEXTURE_HPP
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace OpenEngine {
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
Renderer::ShutDown();
|
||||
OpenEngine::Renderer2D::Shutdown();
|
||||
}
|
||||
|
||||
void Application::Run()
|
||||
|
||||
@@ -206,6 +206,12 @@ namespace OpenEngine {
|
||||
|
||||
UploadInt(name, value);
|
||||
}
|
||||
void OpenGLShader::SetIntArray(const std::string &name, int* values, uint32_t count) const
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
UploadIntArray(name, values, count);
|
||||
}
|
||||
void OpenGLShader::SetFloat(const std::string &name, float value) const
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
@@ -245,6 +251,10 @@ namespace OpenEngine {
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(id, name.c_str()), value);
|
||||
}
|
||||
void OpenGLShader::UploadIntArray(const std::string &name, int* values, uint32_t count) const
|
||||
{
|
||||
glUniform1iv(glGetUniformLocation(id, name.c_str()), count, values);
|
||||
}
|
||||
void OpenGLShader::UploadFloat(const std::string &name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
|
||||
|
||||
@@ -70,6 +70,7 @@ namespace OpenEngine {
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
OE_CORE_DEBUG("id {}", id);
|
||||
glDeleteTextures(1, &id);
|
||||
}
|
||||
|
||||
@@ -86,4 +87,9 @@ namespace OpenEngine {
|
||||
{
|
||||
glBindTextureUnit(slot, id);
|
||||
}
|
||||
|
||||
bool OpenGLTexture2D::operator==(const Texture& other) const
|
||||
{
|
||||
return id == other.GetID();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <renderer/texture.hpp>
|
||||
#include <renderer/buffer.hpp>
|
||||
#include <renderer/shader.hpp>
|
||||
#include "logging.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <glm/fwd.hpp>
|
||||
@@ -18,13 +19,16 @@ namespace OpenEngine {
|
||||
glm::vec3 position;
|
||||
glm::vec4 color;
|
||||
glm::vec2 tex_coord;
|
||||
float tex_index;
|
||||
float tiling_factor;
|
||||
};
|
||||
|
||||
struct Renderer2DData
|
||||
{
|
||||
const uint32_t MaxQuads = 10000;
|
||||
const uint32_t MaxVertices = MaxQuads * 4;
|
||||
const uint32_t MaxIndices = MaxQuads * 6;
|
||||
const uint32_t max_quads = 10000;
|
||||
const uint32_t max_vertices = max_quads * 4;
|
||||
const uint32_t max_indices = max_quads * 6;
|
||||
static const uint32_t max_texture_slots = 32;
|
||||
|
||||
Ref<VertexArray> vertex_array;
|
||||
Ref<VertexBuffer> vertex_buffer;
|
||||
@@ -34,6 +38,11 @@ namespace OpenEngine {
|
||||
uint32_t quad_index_count = 0;
|
||||
QuadVertex* quad_vertex_base = nullptr;
|
||||
QuadVertex* quad_vertex_ptr = nullptr;
|
||||
|
||||
std::array<Ref<Texture2D>, max_texture_slots> texture_slots;
|
||||
uint32_t texture_slot_index = 1; // 0 is white texture
|
||||
|
||||
glm::vec4 quad_vertex_positioning[4];
|
||||
};
|
||||
|
||||
static Renderer2DData renderer_data;
|
||||
@@ -43,23 +52,25 @@ namespace OpenEngine {
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
renderer_data.vertex_array = VertexArray::Create();
|
||||
renderer_data.vertex_buffer = VertexBuffer::Create(renderer_data.MaxVertices * sizeof(QuadVertex));
|
||||
renderer_data.vertex_buffer = VertexBuffer::Create(renderer_data.max_indices * sizeof(QuadVertex));
|
||||
|
||||
BufferLayout layout = {
|
||||
{ ShaderDataType::Float3, "a_Position" },
|
||||
{ ShaderDataType::Float4, "a_Color" },
|
||||
{ ShaderDataType::Float2, "a_TextCoord" }
|
||||
{ ShaderDataType::Float2, "a_TexCoords" },
|
||||
{ ShaderDataType::Float, "a_TexIndex" },
|
||||
{ ShaderDataType::Float, "a_TilingFactor" }
|
||||
};
|
||||
|
||||
renderer_data.vertex_buffer->SetLayout(layout);
|
||||
renderer_data.vertex_array->AddVertexBuffer(renderer_data.vertex_buffer);
|
||||
|
||||
renderer_data.quad_vertex_base = new QuadVertex[renderer_data.MaxIndices];
|
||||
renderer_data.quad_vertex_base = new QuadVertex[renderer_data.max_vertices];
|
||||
|
||||
uint32_t* quad_indices = new uint32_t[renderer_data.MaxIndices];
|
||||
uint32_t* quad_indices = new uint32_t[renderer_data.max_indices];
|
||||
|
||||
uint32_t offset = 0;
|
||||
for (uint32_t i = 0; i < renderer_data.MaxIndices; i += 6) {
|
||||
for (uint32_t i = 0; i < renderer_data.max_indices; i += 6) {
|
||||
quad_indices[i + 0] = offset + 0;
|
||||
quad_indices[i + 1] = offset + 1;
|
||||
quad_indices[i + 2] = offset + 2;
|
||||
@@ -71,7 +82,7 @@ namespace OpenEngine {
|
||||
offset += 4;
|
||||
}
|
||||
|
||||
Ref<IndexBuffer> index_buffer = IndexBuffer::Create(quad_indices, renderer_data.MaxIndices);
|
||||
Ref<IndexBuffer> index_buffer = IndexBuffer::Create(quad_indices, renderer_data.max_indices);
|
||||
renderer_data.vertex_array->SetIndexBuffer(index_buffer);
|
||||
delete[] quad_indices;
|
||||
|
||||
@@ -79,9 +90,20 @@ namespace OpenEngine {
|
||||
uint32_t white_texture_data = 0xffffffff;
|
||||
renderer_data.white_texture->SetData(&white_texture_data, sizeof(uint32_t));
|
||||
|
||||
int32_t samplers[renderer_data.max_texture_slots];
|
||||
for (uint32_t i = 0; i < renderer_data.max_texture_slots; i++)
|
||||
samplers[i] = i;
|
||||
|
||||
renderer_data.texture_shader = Shader::Create("assets/shaders/texture.glsl");
|
||||
renderer_data.texture_shader->Bind();
|
||||
renderer_data.texture_shader->SetInt("u_Texture", 0);
|
||||
renderer_data.texture_shader->SetIntArray("u_Texture", samplers, renderer_data.max_texture_slots);
|
||||
|
||||
renderer_data.texture_slots[0] = renderer_data.white_texture;
|
||||
|
||||
renderer_data.quad_vertex_positioning[0] = {-0.5, -0.5, 0.0f, 1.0f};
|
||||
renderer_data.quad_vertex_positioning[1] = {0.5, -0.5, 0.0f, 1.0f};
|
||||
renderer_data.quad_vertex_positioning[2] = {0.5, 0.5, 0.0f, 1.0f};
|
||||
renderer_data.quad_vertex_positioning[3] = {-0.5, 0.5, 0.0f, 1.0f};
|
||||
}
|
||||
|
||||
void Renderer2D::Shutdown()
|
||||
@@ -89,6 +111,12 @@ namespace OpenEngine {
|
||||
renderer_data.white_texture.reset();
|
||||
renderer_data.vertex_array.reset();
|
||||
renderer_data.vertex_buffer.reset();
|
||||
renderer_data.texture_shader.reset();
|
||||
|
||||
for (uint32_t i = 0; i < renderer_data.texture_slot_index; i++) {
|
||||
OE_CORE_DEBUG("force cleaning {}", renderer_data.texture_slots[i]->GetID());
|
||||
renderer_data.texture_slots[i].reset();
|
||||
}
|
||||
|
||||
OE_PROFILE_FUNCTION();
|
||||
}
|
||||
@@ -102,6 +130,8 @@ namespace OpenEngine {
|
||||
|
||||
renderer_data.quad_index_count = 0;
|
||||
renderer_data.quad_vertex_ptr = renderer_data.quad_vertex_base;
|
||||
|
||||
renderer_data.texture_slot_index = 1;
|
||||
}
|
||||
|
||||
void Renderer2D::EndScene()
|
||||
@@ -116,6 +146,14 @@ namespace OpenEngine {
|
||||
|
||||
void Renderer2D::Flush()
|
||||
{
|
||||
if (renderer_data.quad_index_count == 0)
|
||||
return;
|
||||
|
||||
renderer_data.vertex_array->Bind();
|
||||
|
||||
for (int i = 0; i < renderer_data.texture_slot_index; i++)
|
||||
renderer_data.texture_slots[i]->Bind(i);
|
||||
|
||||
RenderCommand::DrawIndexed(renderer_data.vertex_array, renderer_data.quad_index_count);
|
||||
}
|
||||
|
||||
@@ -123,19 +161,58 @@ namespace OpenEngine {
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
renderer_data.texture_shader->SetVec4("u_Color", glm::vec4(1.0f));
|
||||
renderer_data.texture_shader->SetFloat("u_TilingFactor", tiling_factor);
|
||||
texture->Bind();
|
||||
constexpr glm::vec4 color = glm::vec4(1.0f);
|
||||
|
||||
glm::vec3 position = transform_data.position;
|
||||
glm::vec3 size = transform_data.size;
|
||||
|
||||
float texture_index = 0;
|
||||
for (uint32_t i = 1; i < renderer_data.texture_slot_index; i++) {
|
||||
if (*renderer_data.texture_slots[i].get() == *texture.get()) {
|
||||
texture_index = (float)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (texture_index == 0) {
|
||||
texture_index = (float)renderer_data.texture_slot_index;
|
||||
renderer_data.texture_slots[renderer_data.texture_slot_index] = texture;
|
||||
renderer_data.texture_slot_index++;
|
||||
}
|
||||
|
||||
glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_data.position)
|
||||
* ((transform_data.rotation != 0) ?
|
||||
glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), {0.0f, 0.0f, 1.0f})
|
||||
: 1.0f)
|
||||
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
|
||||
* glm::scale(glm::mat4(1.0f), transform_data.size);
|
||||
renderer_data.texture_shader->SetMat4("u_Transform", transform);
|
||||
|
||||
renderer_data.vertex_array->Bind();
|
||||
RenderCommand::DrawIndexed(renderer_data.vertex_array);
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[0];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = { 0.0f, 0.0f };
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[1];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 0.0f};
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[2];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 1.0f};
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[3];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = {0.0f, 1.0f};
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_index_count += 6;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const Transform& transform_data, const glm::vec4& color)
|
||||
@@ -145,38 +222,38 @@ namespace OpenEngine {
|
||||
glm::vec3 position = transform_data.position;
|
||||
glm::vec3 size = transform_data.size;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = position;
|
||||
glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_data.position)
|
||||
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
|
||||
* glm::scale(glm::mat4(1.0f), transform_data.size);
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[0];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = { 0.0f, 0.0f };
|
||||
renderer_data.quad_vertex_ptr->tex_index = 0;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = 0;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = { position.x + size.x, position.y, position.z };
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[1];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 0.0f};
|
||||
renderer_data.quad_vertex_ptr->tex_index = 0;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = 0;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = { position.x + size.x, position.y + size.y, position.z };
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[2];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = {1.0f, 1.0f};
|
||||
renderer_data.quad_vertex_ptr->tex_index = 0;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = 0;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_vertex_ptr->position = { position.x, position.y + size.y, position.z };
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[3];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = {0.0f, 1.0f};
|
||||
renderer_data.quad_vertex_ptr->tex_index = 0;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = 0;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
|
||||
renderer_data.quad_index_count += 6;
|
||||
|
||||
/*
|
||||
glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_data.position)
|
||||
* ((transform_data.rotation != 0) ?
|
||||
glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), {0.0f, 0.0f, 1.0f})
|
||||
: 1.0f)
|
||||
* glm::scale(glm::mat4(1.0f), transform_data.size);
|
||||
renderer_data.texture_shader->SetMat4("u_Transform", transform);
|
||||
|
||||
renderer_data.vertex_array->Bind();
|
||||
RenderCommand::DrawIndexed(renderer_data.vertex_array);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +168,7 @@ namespace OpenEngine {
|
||||
|
||||
void LinuxWindow::Shutdown()
|
||||
{
|
||||
OE_CORE_DEBUG("context shutdown");
|
||||
glfwDestroyWindow(gl_window);
|
||||
glfwTerminate();
|
||||
gl_window = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user