Browse Source

vertex buffer moved to gaming core, vertex attributes are now set intelligent, stream buffer is removed and replaced by vertex buffer as there is no performance gain right now

Kajetan Johannes Hammerle 3 years ago
parent
commit
13c186db09

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 647e4f95de28a6300ed0d50762ff19a22b7be5f5
+Subproject commit 9398efdd34f486e25c52053ec2c95472a063e26f

+ 2 - 2
meson.build

@@ -14,13 +14,13 @@ sources = [
     'gaming-core/utils/Clock.cpp', 
     'gaming-core/rendering/FileTexture.cpp',
     'gaming-core/images/PNGReader.cpp',
+    'gaming-core/wrapper/VertexBuffer.cpp',
+    'gaming-core/wrapper/Attributes.cpp',
     'rendering/wrapper/Window.cpp', 
     'Game.cpp',
     'input/Controller.cpp',
     'input/Button.cpp',
     'rendering/Renderer.cpp',
-    'rendering/wrapper/StreamBuffer.cpp',
-    'rendering/wrapper/VertexBuffer.cpp',
     'rendering/ColorRenderer.cpp',
     'rendering/FontRenderer.cpp',
     'rendering/Mesh.cpp',

+ 18 - 11
rendering/ColorRenderer.cpp

@@ -1,17 +1,24 @@
 #include "rendering/ColorRenderer.h"
+#include "gaming-core/wrapper/Attributes.h"
+#include "gaming-core/utils/List.h"
 
-ColorRenderer::ColorRenderer() : buffer(8 * 1024 * 1024, 2 * sizeof(float) + 4) {
-    vertexBuffer.bind();
-    int step = 2 * sizeof(float) + 4;
-    vertexBuffer.setFloatAttribute(0, 2, 0, step);
-    vertexBuffer.setColorAttribute(2, 2 * sizeof(float), step);
+ColorRenderer::ColorRenderer() {
+    vertexBuffer.setAttributes(Attributes().addFloat(2).addSpacer(0, 0).addColor4());
+    vertexBuffer.setStreamData(3 * sizeof (float) * 6);
 }
 
 void ColorRenderer::draw(const Vertex& v1, const Vertex& v2, const Vertex& v3) {
-    vertexBuffer.bind();
-    buffer.reset(3 * sizeof (float) * 6);
-    buffer.add(v1.x).add(v1.y).add(v1.color);
-    buffer.add(v2.x).add(v2.y).add(v2.color);
-    buffer.add(v3.x).add(v3.y).add(v3.color);
-    buffer.draw();
+    List<float, 50> buffer;
+    buffer.add(v1.x);
+    buffer.add(v1.y);
+    buffer.add(*reinterpret_cast<const float*>(&(v1.color.data[0])));
+    buffer.add(v2.x);
+    buffer.add(v2.y);
+    buffer.add(*reinterpret_cast<const float*>(&(v2.color.data[0])));
+    buffer.add(v3.x);
+    buffer.add(v3.y);
+    buffer.add(*reinterpret_cast<const float*>(&(v3.color.data[0])));
+    
+    vertexBuffer.updateData(0, buffer.getLength() * sizeof(float), buffer.begin());
+    vertexBuffer.draw(3);
 }

+ 7 - 8
rendering/ColorRenderer.h

@@ -1,13 +1,10 @@
 #ifndef COLORRENDERER_H
 #define COLORRENDERER_H
 
-#include "rendering/wrapper/VertexBuffer.h"
-#include "rendering/wrapper/StreamBuffer.h"
+#include "gaming-core/wrapper/VertexBuffer.h"
 #include "gaming-core/utils/Color.h"
 
-class ColorRenderer final {
-public:
-    ColorRenderer();
+struct ColorRenderer final {
 
     struct Vertex {
         float x;
@@ -15,11 +12,13 @@ public:
         Color4 color;
     };
 
-    void draw(const Vertex& v1, const Vertex& v2, const Vertex& v3);
-
 private:
     VertexBuffer vertexBuffer;
-    StreamBuffer buffer;
+
+public:
+    ColorRenderer();
+
+    void draw(const Vertex& v1, const Vertex& v2, const Vertex& v3);
 };
 
 #endif

+ 43 - 16
rendering/FontRenderer.cpp

@@ -1,25 +1,23 @@
 #include "rendering/FontRenderer.h"
+#include "gaming-core/wrapper/Attributes.h"
+#include "gaming-core/utils/List.h"
 
-FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof (float)), tex1("resources/font8x8.png"),
-tex2("resources/font16x16.png"), tex3("resources/font24x24.png"), activeTex(&tex1), scale(1) {
-    vertexBuffer.bind();
-    int step = 8 * sizeof (float);
-    vertexBuffer.setFloatAttribute(0, 2, 0, step);
-    vertexBuffer.setFloatAttribute(1, 2, 2 * sizeof (float), step);
-    vertexBuffer.setFloatAttribute(2, 4, 4 * sizeof (float), step);
+FontRenderer::FontRenderer() : tex1("resources/font8x8.png"), tex2("resources/font16x16.png"),
+tex3("resources/font24x24.png"), activeTex(&tex1), scale(1) {
+    vertexBuffer.setAttributes(Attributes().addFloat(2).addFloat(2).addFloat(4));
+    vertexBuffer.setStreamData(256 * 4 * sizeof (float) * 8);
 }
 
 float FontRenderer::drawString(float x, float y, const char* text) {
-    vertexBuffer.bind();
-
     const int maxIndex = 256;
-    buffer.reset(maxIndex * 4 * sizeof (float) * 8);
+
+    List<float, maxIndex * 4 * 8> buffer;
 
     int index = 0;
     float r = 1.0f;
     float g = 1.0f;
     float b = 1.0f;
-    
+
     float addX = 6.0f * scale;
     float addY = 8.0f * scale;
 
@@ -45,17 +43,46 @@ float FontRenderer::drawString(float x, float y, const char* text) {
         float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
         float maxY = minY + (1.0f / 16.0f);
 
-        buffer.add(x).add(y).add(minX).add(minY).add(r).add(g).add(b).add(1.0f);
-        buffer.add(x).add(y + addY).add(minX).add(maxY).add(r).add(g).add(b).add(1.0f);
-        buffer.add(x + addX).add(y).add(maxX).add(minY).add(r).add(g).add(b).add(1.0f);
-        buffer.add(x + addX).add(y + addY).add(maxX).add(maxY).add(r).add(g).add(b).add(1.0f);
+        buffer.add(x);
+        buffer.add(y);
+        buffer.add(minX);
+        buffer.add(minY);
+        buffer.add(r);
+        buffer.add(g);
+        buffer.add(b);
+        buffer.add(1.0f);
+        buffer.add(x);
+        buffer.add(y + addY);
+        buffer.add(minX);
+        buffer.add(maxY);
+        buffer.add(r);
+        buffer.add(g);
+        buffer.add(b);
+        buffer.add(1.0f);
+        buffer.add(x + addX);
+        buffer.add(y);
+        buffer.add(maxX);
+        buffer.add(minY);
+        buffer.add(r);
+        buffer.add(g);
+        buffer.add(b);
+        buffer.add(1.0f);
+        buffer.add(x + addX);
+        buffer.add(y + addY);
+        buffer.add(maxX);
+        buffer.add(maxY);
+        buffer.add(r);
+        buffer.add(g);
+        buffer.add(b);
+        buffer.add(1.0f);
 
         x += addX;
         index++;
     }
 
     activeTex->bind();
-    buffer.draw();
+    vertexBuffer.updateData(0, buffer.getLength() * sizeof(float), buffer.begin());
+    vertexBuffer.drawStrip(buffer.getLength() / 8);
     return y + addY;
 }
 

+ 1 - 3
rendering/FontRenderer.h

@@ -1,14 +1,12 @@
 #ifndef FONTRENDERER_H
 #define FONTRENDERER_H
 
-#include "rendering/wrapper/VertexBuffer.h"
-#include "rendering/wrapper/StreamBuffer.h"
+#include "gaming-core/wrapper/VertexBuffer.h"
 #include "gaming-core/rendering/FileTexture.h"
 #include "utils/Array.h"
 
 class FontRenderer final {
     VertexBuffer vertexBuffer;
-    StreamBuffer buffer;
     FileTexture tex1;
     FileTexture tex2;
     FileTexture tex3;

+ 4 - 9
rendering/Mesh.cpp

@@ -1,11 +1,8 @@
 #include "rendering/Mesh.h"
+#include "gaming-core/wrapper/Attributes.h"
 
 Mesh::Mesh() {
-    vertexBuffer.bind();
-    vertexBuffer.bind();
-    vertexBuffer.setFloatAttribute(0, 3, 0, 8);
-    vertexBuffer.setFloatAttribute(1, 2, 3, 8);
-    vertexBuffer.setFloatAttribute(2, 3, 5, 8);
+    vertexBuffer.setAttributes(Attributes().addFloat(3).addFloat(2).addFloat(3));
 }
 
 void Mesh::add(const VertexData& data) {
@@ -17,11 +14,9 @@ void Mesh::clear() {
 }
 
 void Mesh::build() {
-    vertexBuffer.bind();
-    vertexBuffer.setData(sizeof (VertexData) * buffer.getLength(), &(buffer[0]));
+    vertexBuffer.setStaticData(sizeof (VertexData) * buffer.getLength(), buffer.begin());
 }
 
-void Mesh::draw() const {
-    vertexBuffer.bind();
+void Mesh::draw() {
     vertexBuffer.draw(buffer.getLength());
 }

+ 8 - 8
rendering/Mesh.h

@@ -1,11 +1,10 @@
 #ifndef MESH_H
 #define MESH_H
 
-#include "rendering/wrapper/VertexBuffer.h"
+#include "gaming-core/wrapper/VertexBuffer.h"
 #include "utils/List.h"
 
-class Mesh final {
-public:
+struct Mesh final {
 
     struct VertexData final {
         float x;
@@ -18,17 +17,18 @@ public:
         float nz;
     };
 
+private:
+    VertexBuffer vertexBuffer;
+    List<VertexData, 65536 * 2> buffer;
+
+public:
     Mesh();
 
     void add(const VertexData& data);
 
     void clear();
     void build();
-    void draw() const;
-
-private:
-    VertexBuffer vertexBuffer;
-    List<VertexData, 65536 * 2> buffer;
+    void draw();
 };
 
 #endif

+ 0 - 24
rendering/wrapper/StreamBuffer.cpp

@@ -1,24 +0,0 @@
-#include <GL/glew.h>
-#include <cassert>
-
-#include "rendering/wrapper/StreamBuffer.h"
-
-StreamBuffer::StreamBuffer(int size, int bytesPerVertex) :
-bufferSize(size), offset(size), bytesPerVertex(bytesPerVertex), index(0), buffer(nullptr) {
-}
-
-void StreamBuffer::reset(int size) {
-    if(offset + size >= bufferSize) {
-        offset = 0;
-        glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
-    }
-    buffer = static_cast<char*> (glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
-    assert(buffer != nullptr);
-    index = 0;
-}
-
-void StreamBuffer::draw() {
-    glUnmapBuffer(GL_ARRAY_BUFFER);
-    glDrawArrays(GL_TRIANGLE_STRIP, offset / bytesPerVertex, index / bytesPerVertex);
-    offset += index;
-}

+ 0 - 34
rendering/wrapper/StreamBuffer.h

@@ -1,34 +0,0 @@
-#ifndef STREAMBUFFER_H
-#define STREAMBUFFER_H
-
-#include <cstring>
-
-class StreamBuffer final {
-public:
-    StreamBuffer(int size, int bytesPerVertex);
-    StreamBuffer(const StreamBuffer& other) = delete;
-    StreamBuffer(StreamBuffer&& other) = delete;
-    StreamBuffer& operator=(const StreamBuffer& other) = delete;
-    StreamBuffer& operator=(StreamBuffer&& other) = delete;
-
-    void reset(int size);
-
-    template<typename T>
-    StreamBuffer& add(const T& t) {
-        std::memcpy(buffer + index, &t, sizeof (t));
-        index += sizeof (t);
-        return *this;
-    }
-    
-    void draw();
-
-private:
-    int bufferSize;
-    int offset;
-    int bytesPerVertex;
-
-    int index;
-    char* buffer;
-};
-
-#endif

+ 0 - 42
rendering/wrapper/VertexBuffer.cpp

@@ -1,42 +0,0 @@
-#include "rendering/wrapper/VertexBuffer.h"
-
-VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0) {
-    glGenVertexArrays(1, &vertexArray);
-    glGenBuffers(1, &vertexBuffer);
-}
-
-VertexBuffer::~VertexBuffer() {
-    glDeleteBuffers(1, &vertexBuffer);
-    glDeleteVertexArrays(1, &vertexArray);
-}
-
-void VertexBuffer::setFloatAttribute(int index, int length, int offset, int step) {
-    glVertexAttribPointer(index, length, GL_FLOAT, false, step, static_cast<char*> (0) + offset);
-    glEnableVertexAttribArray(index);
-}
-
-void VertexBuffer::setColorAttribute(int index, int offset, int step) {
-    glVertexAttribPointer(index, GL_BGRA, GL_UNSIGNED_BYTE, true, step, static_cast<char*> (0) + offset);
-    glEnableVertexAttribArray(index);
-}
-
-void VertexBuffer::setData(int size, const void* data) {
-    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
-}
-
-void VertexBuffer::bindArray() const {
-    glBindVertexArray(vertexArray);
-}
-
-void VertexBuffer::bindBuffer() const {
-    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
-}
-
-void VertexBuffer::bind() const {
-    bindArray();
-    bindBuffer();
-}
-
-void VertexBuffer::draw(int vertices) const {
-    glDrawArrays(GL_TRIANGLES, 0, vertices);
-}

+ 0 - 30
rendering/wrapper/VertexBuffer.h

@@ -1,30 +0,0 @@
-#ifndef VERTEXBUFFER_H
-#define VERTEXBUFFER_H
-
-#include <GL/glew.h>
-
-class VertexBuffer final {
-public:
-    VertexBuffer();
-    ~VertexBuffer();
-    VertexBuffer(const VertexBuffer& other) = delete;
-    VertexBuffer(VertexBuffer&& other) = delete;
-    VertexBuffer& operator=(const VertexBuffer& other) = delete;
-    VertexBuffer& operator=(VertexBuffer&& other) = delete;
-    
-    void setFloatAttribute(int index, int length, int offset, int step);
-    void setColorAttribute(int index, int offset, int step);
-    void setData(int size, const void* data);
-    
-    void bindArray() const;
-    void bindBuffer() const;
-    void bind() const;
-    
-    void draw(int vertices) const;
-
-private:
-    GLuint vertexArray;
-    GLuint vertexBuffer;
-};
-
-#endif