Browse Source

Convert some shared to unique pointers

master
Riyyi 4 years ago
parent
commit
3dc21abe17
  1. 1
      CMakeLists.txt
  2. 17
      inferno/src/inferno/render/renderer.cpp
  3. 11
      inferno/src/inferno/render/renderer.h

1
CMakeLists.txt

@ -107,6 +107,7 @@ target_precompile_headers(${ENGINE} PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:<string$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<string_view$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<unordered_map$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<utility$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<vector$<ANGLE-R>>"
)

17
inferno/src/inferno/render/renderer.cpp

@ -1,4 +1,5 @@
#include <algorithm> // std::min
#include <utility> // std::move
#include <glad/glad.h>
@ -47,10 +48,10 @@ namespace Inferno {
// -----------------------------------------
// Create array for storing quads vertices
s_quadBatch->vertexBufferBase = std::shared_ptr<QuadVertex[]>(new QuadVertex[QuadBatch::vertexCount]);
s_quadBatch->vertexBufferBase = std::unique_ptr<QuadVertex[]>(new QuadVertex[QuadBatch::vertexCount]);
s_quadBatch->vertexBufferPtr = s_quadBatch->vertexBufferBase.get();
// Set basic quad vertex positions
// Set default quad vertex positions
s_quadBatch->vertexPositions[0] = { -0.5f, -0.5f, 0.0f, 1.0f };
s_quadBatch->vertexPositions[1] = { 0.5f, -0.5f, 0.0f, 1.0f };
s_quadBatch->vertexPositions[2] = { 0.5f, 0.5f, 0.0f, 1.0f };
@ -74,7 +75,7 @@ namespace Inferno {
// -----------------------------------------
// Create shader
s_quadBatch->shader = std::make_shared<Shader>("assets/glsl/batch-quad");
s_quadBatch->shader = std::make_unique<Shader>("assets/glsl/batch-quad");
s_quadBatch->shader->bind();
s_quadBatch->shader->setInt("u_textures", samplers, s_quadBatch->textureUnitPerBatch);
s_quadBatch->shader->unbind();
@ -83,14 +84,14 @@ namespace Inferno {
s_quadBatch->vertexArray = std::make_shared<VertexArray>();
// Create vertex buffer
s_quadBatch->vertexBuffer = std::make_shared<VertexBuffer>(sizeof(QuadVertex) * QuadBatch::vertexCount);
s_quadBatch->vertexBuffer->setLayout({
auto vertexBuffer = std::make_shared<VertexBuffer>(sizeof(QuadVertex) * QuadBatch::vertexCount);
vertexBuffer->setLayout({
{ BufferElementType::Vec3, "a_position" },
{ BufferElementType::Vec4, "a_color" },
{ BufferElementType::Vec2, "a_textureCoordinates" },
{ BufferElementType::Float, "a_textureIndex" },
});
s_quadBatch->vertexArray->addVertexBuffer(s_quadBatch->vertexBuffer);
s_quadBatch->vertexArray->addVertexBuffer(std::move(vertexBuffer));
// Generate indices
@ -109,7 +110,7 @@ namespace Inferno {
}
// Create index buffer
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(indices, sizeof(uint32_t) * QuadBatch::indexCount);
auto indexBuffer = std::make_shared<IndexBuffer>(indices, sizeof(uint32_t) * QuadBatch::indexCount);
s_quadBatch->vertexArray->setIndexBuffer(indexBuffer);
delete[] indices;
@ -226,7 +227,7 @@ namespace Inferno {
}
// Upload vertex data to GPU
s_quadBatch->vertexBuffer->uploadData(
s_quadBatch->vertexArray->getVertexBuffers().at(0)->uploadData(
s_quadBatch->vertexBufferBase.get(),
s_quadBatch->quadCount * QuadBatch::vertexPerQuad * sizeof(QuadVertex));

11
inferno/src/inferno/render/renderer.h

@ -2,7 +2,7 @@
#define RENDERER_H
#include <cstdint> // std::uint32_t
#include <memory> // std::shared_ptr
#include <memory> // std::shared_ptr, std::unique_ptr
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
#include "glm/ext/vector_float2.hpp" // glm::vec2
@ -33,18 +33,21 @@ namespace Inferno {
static const uint32_t indexCount = quads * indexPerQuad;
static const uint32_t textureUnitPerBatch = 32;
// CPU quad vertices
uint32_t quadCount = 0;
std::shared_ptr<QuadVertex[]> vertexBufferBase = nullptr;
std::unique_ptr<QuadVertex[]> vertexBufferBase = nullptr;
QuadVertex* vertexBufferPtr = nullptr;
// Default quad vertex positions
glm::vec4 vertexPositions[vertexPerQuad];
// Texture units
uint32_t supportedTextureUnitPerBatch = 0;
uint32_t textureUnitIndex = 1;
std::array<std::shared_ptr<Texture>, textureUnitPerBatch> textureUnits;
std::shared_ptr<Shader> shader = nullptr;
std::shared_ptr<VertexBuffer> vertexBuffer = nullptr;
// GPU objects
std::unique_ptr<Shader> shader = nullptr;
std::shared_ptr<VertexArray> vertexArray = nullptr;
};

Loading…
Cancel
Save