From 7ff7b88cf66131a559ea0ee931bddce4785507a7 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 28 Jun 2021 20:55:59 +0500 Subject: Implemented vertex data iterleaving --- src/RendererSection.cpp | 76 ++++++++++++++------------------------------- src/RendererSection.hpp | 11 +------ src/RendererSectionData.cpp | 38 +++++++++++------------ src/RendererSectionData.hpp | 16 ++++++---- 4 files changed, 52 insertions(+), 89 deletions(-) diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index df8d3c7..397a660 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -1,5 +1,7 @@ #include "RendererSection.hpp" +#include + #include #include @@ -12,85 +14,74 @@ RendererSection::RendererSection(const RendererSectionData &data) { glGenVertexArrays(1, &Vao); - glGenBuffers(VBOCOUNT, Vbo); + glGenBuffers(1, &Vbo); + glBindBuffer(GL_ARRAY_BUFFER, Vbo); glBindVertexArray(Vao); { //Cube vertices GLuint VertAttribPos = 0; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); - glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[0])); glEnableVertexAttribArray(VertAttribPos); glVertexAttribDivisor(VertAttribPos, 1); - glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(1 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[1])); glEnableVertexAttribArray(VertAttribPos + 1); glVertexAttribDivisor(VertAttribPos + 1, 1); - glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(2 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[2])); glEnableVertexAttribArray(VertAttribPos + 2); glVertexAttribDivisor(VertAttribPos + 2, 1); - glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(3 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[3])); glEnableVertexAttribArray(VertAttribPos + 3); glVertexAttribDivisor(VertAttribPos + 3, 1); glCheckError(); //Cube uvs GLuint UvAttribPos = 4; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVS]); - glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[0])); glEnableVertexAttribArray(UvAttribPos); glVertexAttribDivisor(UvAttribPos, 1); - glVertexAttribPointer(UvAttribPos + 1, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(1 * 2 * sizeof(GLfloat))); + glVertexAttribPointer(UvAttribPos + 1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[1])); glEnableVertexAttribArray(UvAttribPos + 1); glVertexAttribDivisor(UvAttribPos + 1, 1); - glVertexAttribPointer(UvAttribPos + 2, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(2 * 2 * sizeof(GLfloat))); + glVertexAttribPointer(UvAttribPos + 2, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[2])); glEnableVertexAttribArray(UvAttribPos + 2); glVertexAttribDivisor(UvAttribPos + 2, 1); - glVertexAttribPointer(UvAttribPos + 3, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(3 * 2 * sizeof(GLfloat))); + glVertexAttribPointer(UvAttribPos + 3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[3])); glEnableVertexAttribArray(UvAttribPos + 3); glVertexAttribDivisor(UvAttribPos + 3, 1); - glCheckError(); //Uv Layer GLuint uvLayerAttribPos = 8; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVLAYERS]); - glVertexAttribPointer(uvLayerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), nullptr); + glVertexAttribPointer(uvLayerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvLayers)); glEnableVertexAttribArray(uvLayerAttribPos); glVertexAttribDivisor(uvLayerAttribPos, 1); - glCheckError(); //Animation GLuint animationAttribPos = 9; - glCheckError(); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[ANIMATIONS]); - glCheckError(); - glVertexAttribPointer(animationAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); - glCheckError(); + glVertexAttribPointer(animationAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, animations)); glEnableVertexAttribArray(animationAttribPos); - glCheckError(); glVertexAttribDivisor(animationAttribPos, 1); - glCheckError(); //Color GLuint colorAttribPos = 10; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); - glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, colors)); glEnableVertexAttribArray(colorAttribPos); glVertexAttribDivisor(colorAttribPos, 1); - glCheckError(); + + size_t m = sizeof(VertexData); + size_t d = offsetof(VertexData, lights); //Light GLuint lightAttribPos = 11; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); - glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, lights)); glEnableVertexAttribArray(lightAttribPos); glVertexAttribDivisor(lightAttribPos, 1); - glCheckError(); glBindBuffer(GL_ARRAY_BUFFER, 0); } @@ -109,13 +100,7 @@ RendererSection::~RendererSection() { if (Vao != 0) glDeleteVertexArrays(1, &Vao); - for (int i = 0; i < VBOCOUNT; i++) - if (Vbo[i] != 0) { - glBindBuffer(GL_ARRAY_BUFFER, Vbo[i]); - glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_STATIC_DRAW); - } - - glDeleteBuffers(VBOCOUNT, Vbo); + glDeleteBuffers(1, &Vbo); } void swap(RendererSection & lhs, RendererSection & rhs) { @@ -144,28 +129,13 @@ size_t RendererSection::GetHash() { void RendererSection::UpdateData(const RendererSectionData & data) { OPTICK_EVENT(); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); - glBufferData(GL_ARRAY_BUFFER, data.positions.size() * sizeof(glm::vec4), data.positions.data(), GL_DYNAMIC_DRAW); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVS]); - glBufferData(GL_ARRAY_BUFFER, data.uvs.size() * sizeof(glm::vec2), data.uvs.data(), GL_DYNAMIC_DRAW); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVLAYERS]); - glBufferData(GL_ARRAY_BUFFER, data.uvLayers.size() * sizeof(GLfloat), data.uvLayers.data(), GL_DYNAMIC_DRAW); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo[ANIMATIONS]); - glBufferData(GL_ARRAY_BUFFER, data.animations.size() * sizeof(glm::vec2), data.animations.data(), GL_DYNAMIC_DRAW); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); - glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); - glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo); + glBufferData(GL_ARRAY_BUFFER, data.vertices.size() * sizeof(VertexData), data.vertices.data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glCheckError(); - numOfFaces = data.animations.size(); + numOfFaces = data.vertices.size(); sectionPos = data.sectionPos; hash = data.hash; } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 9bb55e3..3ea1fec 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -9,17 +9,8 @@ class RenderState; class RendererSectionData; class RendererSection { - enum Vbos { - POSITIONS = 0, - UVS, - UVLAYERS, - ANIMATIONS, - COLORS, - LIGHTS, - VBOCOUNT, - }; GLuint Vao = { 0 }; - GLuint Vbo[VBOCOUNT] = { 0 }; + GLuint Vbo = { 0 }; size_t hash; Vector sectionPos; diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 78e2239..e74590a 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -47,21 +47,24 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co lightness = glm::vec2(light.face[faceDirection], skyLight.face[faceDirection]); } + data.vertices.emplace_back(); + VertexData& vertexData = data.vertices.back(); + glm::mat4 transformed = transform * model.transform * face.transform; - data.positions.push_back(transformed * glm::vec4(0, 0, 0, 1)); - data.positions.push_back(transformed * glm::vec4(0, 0, 1, 1)); - data.positions.push_back(transformed * glm::vec4(1, 0, 1, 1)); - data.positions.push_back(transformed * glm::vec4(1, 0, 0, 1)); - - data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames)); - data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames)); - data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(1, 1), face.frames)); - data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(0, 1), face.frames)); - - data.uvLayers.push_back(face.layer); - data.animations.push_back(glm::vec2(face.texture.w / face.frames, face.frames)); - data.colors.push_back(face.color); - data.lights.push_back(lightness); + vertexData.positions[0] = transformed * glm::vec4(0, 0, 0, 1); + vertexData.positions[1] = transformed * glm::vec4(0, 0, 1, 1); + vertexData.positions[2] = transformed * glm::vec4(1, 0, 1, 1); + vertexData.positions[3] = transformed * glm::vec4(1, 0, 0, 1); + + vertexData.uvs[0] = TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames); + vertexData.uvs[1] = TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames); + vertexData.uvs[2] = TransformTextureCoord(face.texture, glm::vec2(1, 1), face.frames); + vertexData.uvs[3] = TransformTextureCoord(face.texture, glm::vec2(0, 1), face.frames); + + vertexData.uvLayers = face.layer; + vertexData.animations = glm::vec2(face.texture.w / face.frames, face.frames); + vertexData.colors = face.color; + vertexData.lights = lightness; } } @@ -154,12 +157,7 @@ RendererSectionData ParseSection(const SectionsData §ions) { } } } - data.positions.shrink_to_fit(); - data.uvs.shrink_to_fit(); - data.uvLayers.shrink_to_fit(); - data.animations.shrink_to_fit(); - data.colors.shrink_to_fit(); - data.lights.shrink_to_fit(); + data.vertices.shrink_to_fit(); return data; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 7b30e44..e2fd190 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -30,13 +30,17 @@ struct SectionsData { BlockLightness GetSkyLight(const Vector &pos) const; }; +struct VertexData { + glm::vec4 positions[4]; + glm::vec2 uvs[4]; + float uvLayers; + glm::vec2 animations; + glm::vec3 colors; + glm::vec2 lights; +}; + struct RendererSectionData { - std::vector positions; //4 per instance - std::vector uvs; //4 per instance - std::vector uvLayers; - std::vector animations; - std::vector colors; - std::vector lights; + std::vector vertices; size_t hash = 0; Vector sectionPos; bool forced = false; -- cgit v1.2.3