Make index buffers use 32 bit ints

This commit is contained in:
Bailey Harrison 2022-10-24 09:34:09 +01:00
parent 6542b7039b
commit e811349e5b
7 changed files with 38 additions and 83 deletions

View File

@ -6,6 +6,7 @@
#include "resources/font.hpp"
#include "resources/mesh.hpp"
#include "resources/shader.hpp"
#include <glm/mat4x4.hpp>

View File

@ -4,9 +4,7 @@
#include "resource.hpp"
#include "resources/shader.hpp"
#include <glad/glad.h>
#include "gfx.hpp"
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
@ -20,34 +18,30 @@ struct Vertex {
glm::vec2 uv;
};
namespace engine {
class GFXDevice;
}
namespace engine::resources {
class ENGINE_API Mesh : public Resource {
public:
Mesh(const std::vector<Vertex>& vertices);
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices);
Mesh(const std::filesystem::path& resPath);
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices);
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
Mesh(GFXDevice* gfx, const std::filesystem::path& resPath);
~Mesh() override;
void drawMesh(const Shader& shader);
static void invalidate()
{
s_active_vao = -1;
}
void drawMesh(const gfx::Pipeline* pipeline);
std::vector<Vertex> m_vertices;
std::vector<unsigned int> m_indices;
std::vector<uint32_t> m_indices;
private:
static int s_active_vao;
const gfx::Buffer* vb;
const gfx::Buffer* ib;
GLuint m_vao;
GLuint m_vbo;
GLuint m_ebo;
void bindVAO() const;
GFXDevice* gfx;
void initMesh();

View File

@ -33,13 +33,13 @@ void Renderer::render(glm::mat4 transform)
m_shader->setUniform_v3("baseColor", m_color );
m_shader->setUniform_v3("emission", m_emission );
if (m_mesh)
m_mesh->drawMesh(*m_shader);
// if (m_mesh)
// m_mesh->drawMesh(*m_shader);
}
void Renderer::setMesh(const std::string& name)
{
m_mesh = parent.res.get<resources::Mesh>(name);
// m_mesh = parent.res.get<resources::Mesh>(name);
}
void Renderer::setTexture(const std::string& name)

View File

@ -57,6 +57,7 @@ void UI::render(glm::mat4 transform)
float w = glyph.size.x * scale;
float h = glyph.size.y * scale;
/*
resources::Mesh mesh({
{{xpos, ypos + h, 0.0f}, {}, {0.0f, 0.0f}},
{{xpos, ypos , 0.0f}, {}, {0.0f, 1.0f}},
@ -64,11 +65,11 @@ void UI::render(glm::mat4 transform)
{{xpos, ypos + h, 0.0f}, {}, {0.0f, 0.0f}},
{{xpos + w, ypos, 0.0f}, {}, {1.0f, 1.0f}},
{{xpos + w, ypos + h, 0.0f}, {}, {1.0f, 0.0f}},
});
});*/
glBindTexture(GL_TEXTURE_2D, glyph.textureID);
mesh.drawMesh(*m_shader);
// mesh.drawMesh(*m_shader);
x += (glyph.advance >> 6) * scale;

View File

@ -37,10 +37,10 @@ namespace engine {
{ {-0.5f, 0.5f}, {0.0f, 1.0f, 1.0f} },
};
vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, sizeof(Vertex) * vertices.size(), vertices.data());
const std::vector<uint16_t> indices = {
const std::vector<uint32_t> indices = {
0, 1, 2, 2, 1, 3,
};
ib = m_gfx->createBuffer(gfx::BufferType::INDEX, sizeof(uint16_t) * indices.size(), indices.data());
ib = m_gfx->createBuffer(gfx::BufferType::INDEX, sizeof(uint32_t) * indices.size(), indices.data());
}

View File

@ -1134,7 +1134,7 @@ namespace engine {
// do a simple draw call
vkCmdDraw(pimpl->commandBuffer, call.count, 1, 0, 0);
} else {
vkCmdBindIndexBuffer(pimpl->commandBuffer, call.indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT16);
vkCmdBindIndexBuffer(pimpl->commandBuffer, call.indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(pimpl->commandBuffer, call.count, 1, 0, 0, 0);
}
queue.pop();

View File

@ -1,5 +1,7 @@
#include "resources/mesh.hpp"
#include "gfx_device.hpp"
namespace engine::resources {
struct MeshFileHeader {
@ -8,7 +10,7 @@ struct MeshFileHeader {
int material;
};
static void loadMeshFromFile(const std::filesystem::path& path, std::vector<Vertex>* vertices, std::vector<unsigned int>* indices)
static void loadMeshFromFile(const std::filesystem::path& path, std::vector<Vertex>* vertices, std::vector<uint32_t>* indices)
{
// TODO
@ -26,73 +28,34 @@ static void loadMeshFromFile(const std::filesystem::path& path, std::vector<Vert
indices->resize(header.index_count);
vertices->resize(header.vertex_count);
fread(&(*indices)[0], sizeof(unsigned int) * header.index_count, 1, fp);
fread(&((*vertices)[0].pos[0]), sizeof(float) * 8 * header.vertex_count, 1, fp);
fread(indices->data(), sizeof(unsigned int) * header.index_count, 1, fp);
fread(vertices->data(), sizeof(float) * 8 * header.vertex_count, 1, fp);
fclose(fp);
}
static void loadObjFromFile(const std::filesystem::path& path, std::vector<Vertex>* vertices, std::vector<unsigned int>* indices)
{
}
// -1 means invalidated
int Mesh::s_active_vao = -1;
void Mesh::bindVAO() const
{
if (s_active_vao != m_vao) {
glBindVertexArray(m_vao);
s_active_vao = m_vao;
}
}
void Mesh::initMesh()
{
glGenVertexArrays(1, &m_vao);
bindVAO();
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ebo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertices.size()*sizeof(Vertex), &m_vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned int), &(m_indices[0]), GL_STATIC_DRAW);
// position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)offsetof(Vertex, pos));
// normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)offsetof(Vertex, norm));
// uv
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)offsetof(Vertex, uv));
vb = gfx->createBuffer(gfx::BufferType::VERTEX, m_vertices.size() * sizeof(Vertex), m_vertices.data());
ib = gfx->createBuffer(gfx::BufferType::INDEX, m_indices.size() * sizeof(uint32_t), m_indices.data());
}
void Mesh::drawMesh(const Shader& shader)
void Mesh::drawMesh(const gfx::Pipeline* pipeline)
{
bindVAO();
shader.makeActive();
#ifndef SDLTEST_NOGFX
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()), GL_UNSIGNED_INT, 0);
#endif
gfx->drawIndexed(pipeline, vb, ib, m_indices.size());
}
Mesh::Mesh(const std::vector<Vertex>& vertices) : Resource("", "mesh")
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices) : Resource("", "mesh"), gfx(gfx)
{
// constructor for custom meshes without an index array
m_vertices = vertices; // COPY over vertices
for (int i = 0; i < m_vertices.size(); i++) {
for (uint32_t i = 0; i < m_vertices.size(); i++) {
m_indices.push_back(i);
}
initMesh();
}
Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices) : Resource("", "mesh")
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices) : Resource("", "mesh"), gfx(gfx)
{
m_vertices = vertices; // COPY over vertices
m_indices = indices; // COPY over indices;
@ -100,7 +63,7 @@ Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>&
}
// To be used with the resource manager
Mesh::Mesh(const std::filesystem::path& resPath) : Resource(resPath, "mesh")
Mesh::Mesh(GFXDevice* gfx, const std::filesystem::path& resPath) : Resource(resPath, "mesh"), gfx(gfx)
{
loadMeshFromFile(resPath, &m_vertices, &m_indices);
initMesh();
@ -108,12 +71,8 @@ Mesh::Mesh(const std::filesystem::path& resPath) : Resource(resPath, "mesh")
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
glDeleteBuffers(1, &m_ebo);
if (s_active_vao == m_vao) {
s_active_vao = -1;
}
gfx->destroyBuffer(ib);
gfx->destroyBuffer(vb);
}
}