Explorar o código

enum for shader types and buffer usage, fixed wrong alignment cast in
typed buffer, swapped arguments for more consistency

Kajetan Johannes Hammerle hai 1 ano
pai
achega
fb5fe257a2
Modificáronse 8 ficheiros con 59 adicións e 47 borrados
  1. 18 17
      rendering/GL.cpp
  2. 9 11
      rendering/GL.h
  3. 6 6
      rendering/Shader.cpp
  4. 1 1
      rendering/Shader.h
  5. 1 1
      rendering/VertexBuffer.h
  6. 16 2
      tests/TypedBufferTests.cpp
  7. 1 1
      utils/Buffer.h
  8. 7 8
      utils/TypedBuffer.h

+ 18 - 17
rendering/GL.cpp

@@ -12,8 +12,6 @@ static_assert(std::is_same<GL::Program, GLuint>::value, "p has invalid type");
 static_assert(std::is_same<char, GLchar>::value, "char has invalid type");
 static_assert(std::is_same<int, GLint>::value, "int has invalid type");
 static_assert(std::is_same<float, GLfloat>::value, "float has invalid type");
-static_assert(std::is_same<GL::ShaderType, GLenum>::value,
-              "shader type has invalid type");
 static_assert(std::is_same<GL::Texture, GLuint>::value,
               "texture has invalid type");
 static_assert(std::is_same<GL::Framebuffer, GLuint>::value,
@@ -24,19 +22,6 @@ static_assert(std::is_same<GL::VertexArray, GLuint>::value,
               "vertex array has invalid type");
 static_assert(std::is_same<GL::Buffer, GLuint>::value,
               "buffer has invalid type");
-static_assert(std::is_same<GL::BufferUsage, GLenum>::value,
-              "buffer usage has invalid type");
-
-GL::ShaderType GL::NO_SHADER = 0;
-GL::ShaderType GL::VERTEX_SHADER = GL_VERTEX_SHADER;
-GL::ShaderType GL::FRAGMENT_SHADER = GL_FRAGMENT_SHADER;
-GL::ShaderType GL::GEOMETRY_SHADER = GL_GEOMETRY_SHADER;
-GL::ShaderType GL::TESSELATION_CONTROL_SHADER = GL_TESS_CONTROL_SHADER;
-GL::ShaderType GL::TESSELATION_EVALUATION_SHADER = GL_TESS_EVALUATION_SHADER;
-
-GL::BufferUsage GL::STATIC_DRAW = GL_STATIC_DRAW;
-GL::BufferUsage GL::STREAM_DRAW = GL_STREAM_DRAW;
-GL::BufferUsage GL::DYNAMIC_DRAW = GL_DYNAMIC_DRAW;
 
 GL::Attribute::Attribute(int count_, int size_, int type_, bool normalized_)
     : count(count_), size(size_), type(type_), normalized(normalized_) {
@@ -210,7 +195,17 @@ void GL::deleteProgram(Program p) {
 }
 
 GL::Shader GL::createShader(ShaderType type) {
-    return glCreateShader(type);
+    static constexpr GLenum MAP[] = {0,
+                                     GL_VERTEX_SHADER,
+                                     GL_FRAGMENT_SHADER,
+                                     GL_GEOMETRY_SHADER,
+                                     GL_TESS_CONTROL_SHADER,
+                                     GL_TESS_EVALUATION_SHADER};
+    static constexpr int MAP_SIZE =
+        static_cast<int>(sizeof(MAP) / sizeof(GLenum));
+    int index = static_cast<int>(type);
+    GLenum realTyp = index < 0 || index >= MAP_SIZE ? MAP[0] : MAP[index];
+    return glCreateShader(realTyp);
 }
 
 void GL::compileShader(Shader s, const char* code) {
@@ -398,7 +393,13 @@ void GL::bindBuffer(Buffer b) {
 }
 
 void GL::bufferData(int size, const void* data, BufferUsage usage) {
-    glBufferData(GL_ARRAY_BUFFER, size, data, usage);
+    static constexpr GLenum MAP[] = {0, GL_STATIC_DRAW, GL_STREAM_DRAW,
+                                     GL_DYNAMIC_DRAW};
+    static constexpr int MAP_SIZE =
+        static_cast<int>(sizeof(MAP) / sizeof(GLenum));
+    int index = static_cast<int>(usage);
+    GLenum realUsage = index < 0 || index >= MAP_SIZE ? MAP[0] : MAP[index];
+    glBufferData(GL_ARRAY_BUFFER, size, data, realUsage);
 }
 
 void GL::bufferSubData(int offset, int size, const void* data) {

+ 9 - 11
rendering/GL.h

@@ -6,24 +6,22 @@
 namespace GL {
     typedef unsigned int Shader;
     typedef unsigned int Program;
-    typedef unsigned int ShaderType;
     typedef unsigned int Texture;
     typedef unsigned int Framebuffer;
     typedef unsigned int ColorAttachment;
     typedef unsigned int VertexArray;
     typedef unsigned int Buffer;
-    typedef unsigned int BufferUsage;
 
-    extern ShaderType NO_SHADER;
-    extern ShaderType VERTEX_SHADER;
-    extern ShaderType FRAGMENT_SHADER;
-    extern ShaderType GEOMETRY_SHADER;
-    extern ShaderType TESSELATION_CONTROL_SHADER;
-    extern ShaderType TESSELATION_EVALUATION_SHADER;
+    enum class ShaderType {
+        INVALID,
+        VERTEX,
+        FRAGMENT,
+        GEOMETRY,
+        TESSELATION_CONTROL,
+        TESSELATION_EVALUATION
+    };
 
-    extern BufferUsage STATIC_DRAW;
-    extern BufferUsage STREAM_DRAW;
-    extern BufferUsage DYNAMIC_DRAW;
+    enum class BufferUsage { INVALID, STATIC, STREAM, DYNAMIC };
 
     class Attribute final {
         int count;

+ 6 - 6
rendering/Shader.cpp

@@ -25,17 +25,17 @@ static bool endsWith(const char* path, int length, const char* ending) {
 GL::ShaderType Shader::getShaderType(const char* path) const {
     int length = static_cast<int>(strlen(path));
     if(endsWith(path, length, ".vs")) {
-        return GL::VERTEX_SHADER;
+        return GL::ShaderType::VERTEX;
     } else if(endsWith(path, length, ".fs")) {
-        return GL::FRAGMENT_SHADER;
+        return GL::ShaderType::FRAGMENT;
     } else if(endsWith(path, length, ".gs")) {
-        return GL::GEOMETRY_SHADER;
+        return GL::ShaderType::GEOMETRY;
     } else if(endsWith(path, length, ".tcs")) {
-        return GL::TESSELATION_CONTROL_SHADER;
+        return GL::ShaderType::TESSELATION_CONTROL;
     } else if(endsWith(path, length, ".tes")) {
-        return GL::TESSELATION_EVALUATION_SHADER;
+        return GL::ShaderType::TESSELATION_EVALUATION;
     }
-    return GL::NO_SHADER;
+    return GL::ShaderType::INVALID;
 }
 
 Error Shader::readAndCompile(const char* path, GL::Shader& s,

+ 1 - 1
rendering/Shader.h

@@ -28,7 +28,7 @@ public:
 
         for(int i = 0; i < size; i++) {
             GL::ShaderType type = getShaderType(paths[i]);
-            if(type == GL::NO_SHADER) {
+            if(type == GL::ShaderType::INVALID) {
                 Error error = {"'"};
                 error.message.append(paths[i]).append(
                     "' is not a valid shader type");

+ 1 - 1
rendering/VertexBuffer.h

@@ -38,7 +38,7 @@ public:
     void setData(int size, const void* data, GL::BufferUsage usage);
     void setData(const Buffer& buffer, GL::BufferUsage usage);
     template<typename T>
-    void setData(GL::BufferUsage usage, const TypedBuffer<T>& buffer) {
+    void setData(const TypedBuffer<T>& buffer, GL::BufferUsage usage) {
         setData(buffer.getByteLength(), buffer, usage);
     }
 

+ 16 - 2
tests/TypedBufferTests.cpp

@@ -1,9 +1,11 @@
 #include "tests/TypedBufferTests.h"
+
+#include "math/Vector.h"
 #include "tests/Test.h"
 #include "utils/TypedBuffer.h"
 
 static void testAdd(Test& test) {
-    TypedBuffer<int> buffer(10);
+    TypedBuffer<int> buffer;
     for(int i = 0; i < 100000; i++) {
         buffer.add(5);
         buffer.add(5L);
@@ -11,11 +13,23 @@ static void testAdd(Test& test) {
         buffer.add(5.0);
     }
     test.checkEqual(400000, buffer.getLength(), "add increments length");
-    test.checkEqual(static_cast<int> ((sizeof (int)) * 400000), buffer.getByteLength(), "add increments byte length");
+    test.checkEqual(static_cast<int>((sizeof(int)) * 400000),
+                    buffer.getByteLength(), "add increments byte length");
+}
+
+static void testCast(Test& test) {
+    TypedBuffer<Vector3> buffer;
+    buffer.add(Vector3(0.0f, 1.0f, 2.0f));
+
+    const Vector3* v = buffer;
+    test.checkEqual((*v)[0], 0.0f, "buffer contains element 1");
+    test.checkEqual((*v)[1], 1.0f, "buffer contains element 2");
+    test.checkEqual((*v)[2], 2.0f, "buffer contains element 3");
 }
 
 void TypedBufferTests::test() {
     Test test("TypedBuffer");
     testAdd(test);
+    testCast(test);
     test.finalize();
 }

+ 1 - 1
utils/Buffer.h

@@ -7,7 +7,7 @@ class Buffer final {
     char* buffer;
 
 public:
-    Buffer(int initialSize);
+    Buffer(int initialSize = 32);
     Buffer(const Buffer& other);
     Buffer(Buffer&& other);
     ~Buffer();

+ 7 - 8
utils/TypedBuffer.h

@@ -1,31 +1,30 @@
 #ifndef TYPEBUFFER_H
 #define TYPEBUFFER_H
 
-#include "utils/Buffer.h"
+#include <iostream>
+
+#include "data/List.h"
 
 template<typename T>
 class TypedBuffer {
-    Buffer data;
+    List<T> data;
 
 public:
-    TypedBuffer(int n) : data(static_cast<int>(sizeof(T)) * n) {
-    }
-
     TypedBuffer& add(const T& t) {
         data.add(t);
         return *this;
     }
 
     int getLength() const {
-        return data.getLength() / static_cast<int>(sizeof(T));
+        return data.getLength();
     }
 
     int getByteLength() const {
-        return data.getLength();
+        return data.getLength() * static_cast<int>(sizeof(T));
     }
 
     operator const T*() const {
-        return reinterpret_cast<const T*>(static_cast<const char*>(data));
+        return data.begin();
     }
 
     void clear() {