Browse Source

framebuffer moved to gaming core

Kajetan Johannes Hammerle 3 years ago
parent
commit
04c83eda84

+ 2 - 2
client/Main.cpp

@@ -38,8 +38,8 @@ int main() {
         return 0;
     }
 
-    Framebuffers framebuffers(size);
-    if(framebuffers.hasError()) {
+    Framebuffers framebuffers;
+    if(framebuffers.init(size)) {
         return 0;
     }
 

+ 13 - 15
client/rendering/Engine.cpp

@@ -31,7 +31,7 @@ void Engine::renderTick(float lag, Game& game) {
 }
 
 void Engine::renderShadow(float lag, Game& game) {
-    framebuffers.shadow.bind();
+    framebuffers.shadow.bindAndClear();
     GL::enableDepthTesting();
     shaders.shadow.use();
     worldShadowProjView = worldShadowProj;
@@ -44,7 +44,7 @@ void Engine::renderShadow(float lag, Game& game) {
 }
 
 void Engine::renderWorld(float lag, Game& game) {
-    framebuffers.world.bind();
+    framebuffers.world.bindAndClear();
     GL::enableDepthTesting();
     shaders.world.use();
 
@@ -58,7 +58,7 @@ void Engine::renderWorld(float lag, Game& game) {
     shaders.world.setMatrix("view", worldView.getValues());
     model.clear();
     shaders.world.setMatrix("model", model.peek().getValues());
-    framebuffers.shadow.bindDepthTexture(1);
+    framebuffers.shadow.bindTextureTo(0, 1);
     shaders.world.setInt("shadows", renderSettings.shadows);
     shaders.world.setFloat("radius", renderSettings.testRadius);
     shaders.world.setFloat("zbias", renderSettings.testBias);
@@ -76,17 +76,15 @@ void Engine::renderSSAO() {
     shaders.ssao.setMatrix("proj", rProj.getValues());
     shaders.ssao.setInt("width", size.width);
     shaders.ssao.setInt("height", size.height);
-    framebuffers.world.bindPositionTexture(0);
-    framebuffers.world.bindNormalTexture(1);
-    framebuffers.world.bindColorTexture(2);
-    framebuffers.world.bindDepthTexture(3);
-    ssaoNoise.bind(4);
-    framebuffers.ssao.bind();
+    framebuffers.world.bindTextureTo(0, 0);
+    framebuffers.world.bindTextureTo(4, 1);
+    ssaoNoise.bindTo(2);
+    framebuffers.ssao.bindAndClear();
     rectangle.draw();
 
     shaders.ssaoBlur.use();
-    framebuffers.ssao.bindRedTexture(0);
-    framebuffers.ssaoBlur.bind();
+    framebuffers.ssao.bindTextureTo(0, 0);
+    framebuffers.ssaoBlur.bindAndClear();
     rectangle.draw();
 }
 
@@ -94,10 +92,10 @@ void Engine::renderPostWorld() {
     GL::bindMainFramebuffer();
     GL::clearFramebuffer();
     shaders.postWorld.use();
-    framebuffers.world.bindColorTexture(0);
-    framebuffers.ssaoBlur.bindRedTexture(1);
-    framebuffers.world.bindRedTexture(2);
-    framebuffers.world.bindNormalTexture(3);
+    framebuffers.world.bindTextureTo(2, 0);
+    framebuffers.ssaoBlur.bindTextureTo(0, 1);
+    framebuffers.world.bindTextureTo(3, 2);
+    framebuffers.world.bindTextureTo(1, 3);
     shaders.postWorld.setInt("ssao", renderSettings.ssao);
     shaders.postWorld.setInt("shadows", renderSettings.shadows);
     rectangle.draw();

+ 1 - 1
client/rendering/FontRenderer.cpp

@@ -68,7 +68,7 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         index++;
     }
 
-    tex.bind(0);
+    tex.bindTo(0);
     vertexBuffer.updateData(0, buffer.getLength() * sizeof(float), buffer.begin());
     vertexBuffer.drawStrip(buffer.getLength() / 7);
 }

+ 1 - 1
client/rendering/FontRenderer.h

@@ -6,7 +6,7 @@
 
 class FontRenderer final {
     VertexBuffer vertexBuffer;
-    FileTexture tex;
+    FileTexture<4> tex;
     
 public:
     FontRenderer();

+ 8 - 7
client/rendering/Framebuffers.cpp

@@ -1,10 +1,11 @@
 #include "client/rendering/Framebuffers.h"
 
-Framebuffers::Framebuffers(const Size& size) :
-world(size, Framebuffer::POSITION | Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::RED | Framebuffer::DEPTH),
-ssao(size, Framebuffer::RED),
-ssaoBlur(size, Framebuffer::RED),
-shadow(size, Framebuffer::DEPTH) {
+Framebuffers::Framebuffers() :
+world(TextureFormat::float32(3), TextureFormat::float32(3), TextureFormat::color8(4), TextureFormat::float32(1),
+TextureFormat::depth32()),
+ssao(TextureFormat::float32(1)),
+ssaoBlur(TextureFormat::float32(1)),
+shadow(TextureFormat::depth32()) {
 }
 
 void Framebuffers::resize(const Size& size) {
@@ -14,6 +15,6 @@ void Framebuffers::resize(const Size& size) {
     shadow.resize(size);
 }
 
-bool Framebuffers::hasError() const {
-    return world.hasError() || ssao.hasError() || ssaoBlur.hasError() || shadow.hasError();
+bool Framebuffers::init(const Size& size) {
+    return world.init(size) || ssao.init(size) || ssaoBlur.init(size) || shadow.init(size);
 }

+ 7 - 7
client/rendering/Framebuffers.h

@@ -2,17 +2,17 @@
 #define FRAMEBUFFERS_H
 
 #include "gaming-core/utils/Size.h"
-#include "client/rendering/wrapper/Framebuffer.h"
+#include "gaming-core/wrapper/Framebuffer.h"
 
 struct Framebuffers final {
-    Framebuffers(const Size& size);
+    Framebuffers();
     void resize(const Size& size);
-    bool hasError() const;
+    bool init(const Size& size);
 
-    Framebuffer world;
-    Framebuffer ssao;
-    Framebuffer ssaoBlur;
-    Framebuffer shadow;
+    Framebuffer<5> world;
+    Framebuffer<1> ssao;
+    Framebuffer<1> ssaoBlur;
+    Framebuffer<1> shadow;
 };
 
 #endif

+ 4 - 4
client/rendering/NoiseTexture.cpp

@@ -2,15 +2,15 @@
 #include "gaming-core/utils/Random.h"
 #include "gaming-core/utils/List.h"
 
-NoiseTexture::NoiseTexture() {
+NoiseTexture::NoiseTexture() : texture(TextureFormat::float32(3)) {
     Random r(1);
     List<float, 48> data;
     for(int i = 0; i < 48; i++) {
         data.add(r.nextFloat() * 2.0f - 1.0f);
     }
-    texture.setRGBFloatData(4, 4, &(data[0]));
+    texture.setData(4, 4, &(data[0]));
 }
 
-void NoiseTexture::bind(int index) const {
-    texture.bind(index);
+void NoiseTexture::bindTo(int index) const {
+    texture.bindTo(index);
 }

+ 1 - 1
client/rendering/NoiseTexture.h

@@ -9,7 +9,7 @@ class NoiseTexture final {
 public:
     NoiseTexture();
 
-    void bind(int index) const;
+    void bindTo(int index) const;
 };
 
 #endif

+ 1 - 1
client/rendering/renderer/WorldRenderer.cpp

@@ -19,7 +19,7 @@ WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resour
 void WorldRenderer::render(float lag, Renderer& renderer) {
     (void) lag;
     (void) renderer;
-    texture.bind(0);
+    texture.bindTo(0);
     mesh.draw();
 }
 

+ 1 - 1
client/rendering/renderer/WorldRenderer.h

@@ -17,7 +17,7 @@ public:
 private:
     const World& world;
     Mesh mesh;
-    FileTexture texture;
+    FileTexture<4> texture;
 };
 
 #endif

+ 0 - 122
client/rendering/wrapper/Framebuffer.cpp

@@ -1,122 +0,0 @@
-#include <iostream>
-
-#include "client/rendering/wrapper/Framebuffer.h"
-
-Framebuffer::Framebuffer(const Size& size, int mode, bool texCompare) : size(size), mode(mode), textures(0),
-buffer(0), error(false) {
-    glGenFramebuffers(1, &buffer);
-    glBindFramebuffer(GL_FRAMEBUFFER, buffer);
-
-    data[0] = {POSITION, GL_RGB16F, GL_RGB, GL_FLOAT};
-    data[1] = {NORMAL, GL_RGB16F, GL_RGB, GL_FLOAT};
-    data[2] = {COLOR, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE};
-    data[3] = {RED, GL_R32F, GL_RGB, GL_FLOAT};
-    data[4] = {DEPTH, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT};
-
-    GLuint attachments[4];
-    int counter = 0;
-    for(int i = 0; i < 4; i++) {
-        if(mode & data[i].mask) {
-            setupTexture(i, size, attachments, counter);
-        }
-    }
-    if(mode & DEPTH) {
-        genTexture(4, size);
-        if(texCompare) {
-            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
-            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
-        }
-        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textures[4], 0);
-    }
-    glDrawBuffers(counter, attachments);
-
-    GLenum glError = glCheckFramebufferStatus(GL_FRAMEBUFFER);
-    if(glError != GL_FRAMEBUFFER_COMPLETE) {
-        std::cout << "frame buffer error: " << getErrorString(error) << "\n";
-        error = true;
-    }
-}
-
-Framebuffer::~Framebuffer() {
-    glDeleteFramebuffers(1, &buffer);
-    for(GLuint& texture : textures) {
-        glDeleteTextures(1, &texture);
-    }
-}
-
-bool Framebuffer::hasError() const {
-    return error;
-}
-
-void Framebuffer::bind() const {
-    glViewport(0, 0, size.width, size.height);
-    glBindFramebuffer(GL_FRAMEBUFFER, buffer);
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-}
-
-void Framebuffer::resize(const Size& size) const {
-    for(int i = 0; i < 5; i++) {
-        if(mode & data[i].mask) {
-            glBindTexture(GL_TEXTURE_2D, textures[i]);
-            glTexImage2D(GL_TEXTURE_2D, 0, data[i].internalFormat, 
-                    size.width, size.height, 0, data[i].format, data[i].type, nullptr);
-        }
-    }
-}
-
-void Framebuffer::bindTexture(int textureUnit, GLuint texture) const {
-    glActiveTexture(GL_TEXTURE0 + textureUnit);
-    glBindTexture(GL_TEXTURE_2D, texture);
-}
-
-void Framebuffer::bindPositionTexture(int textureUnit) const {
-    bindTexture(textureUnit, textures[0]);
-}
-
-void Framebuffer::bindNormalTexture(int textureUnit) const {
-    bindTexture(textureUnit, textures[1]);
-}
-
-void Framebuffer::bindColorTexture(int textureUnit) const {
-    bindTexture(textureUnit, textures[2]);
-}
-
-void Framebuffer::bindRedTexture(int textureUnit) const {
-    bindTexture(textureUnit, textures[3]);
-}
-
-void Framebuffer::bindDepthTexture(int textureUnit) const {
-    bindTexture(textureUnit, textures[4]);
-}
-
-void Framebuffer::genTexture(int index, const Size& size) {
-    glGenTextures(1, &(textures[index]));
-    glBindTexture(GL_TEXTURE_2D, textures[index]);
-    glTexImage2D(GL_TEXTURE_2D, 0, data[index].internalFormat, 
-            size.width, size.height, 0, data[index].format, data[index].type, nullptr);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-}
-
-void Framebuffer::setupTexture(int index, const Size& size, GLuint* attachments, int& counter) {
-    genTexture(index, size);
-    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + counter, GL_TEXTURE_2D, textures[index], 0);
-    attachments[counter] = GL_COLOR_ATTACHMENT0 + counter;
-    counter++;
-}
-
-const char* Framebuffer::getErrorString(GLenum error) const {
-    switch(error) {
-        case GL_FRAMEBUFFER_UNDEFINED: return "GL_FRAMEBUFFER_UNDEFINED";
-        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
-        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
-        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER";
-        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER";
-        case GL_FRAMEBUFFER_UNSUPPORTED: return "GL_FRAMEBUFFER_UNSUPPORTED";
-        case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: return "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE";
-        case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: return "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS";
-    }
-    return "unknown error";
-}

+ 0 - 56
client/rendering/wrapper/Framebuffer.h

@@ -1,56 +0,0 @@
-#ifndef FRAMEBUFFER_H
-#define FRAMEBUFFER_H
-
-#include <GL/glew.h>
-
-#include "gaming-core/utils/Size.h"
-#include "gaming-core/utils/Array.h"
-
-class Framebuffer final {
-public:
-    static const int POSITION = 1;
-    static const int NORMAL = 2;
-    static const int COLOR = 4;
-    static const int RED = 8;
-    static const int DEPTH = 16;
-
-    Framebuffer(const Size& size, int mode, bool texCompare = false);
-    ~Framebuffer();
-    Framebuffer(const Framebuffer& other) = delete;
-    Framebuffer(Framebuffer&& other) = delete;
-    Framebuffer& operator=(const Framebuffer& other) = delete;
-    Framebuffer& operator=(Framebuffer&& other) = delete;
-
-    bool hasError() const;
-    void bind() const;
-
-    void resize(const Size& size) const;
-
-    void bindPositionTexture(int textureUnit) const;
-    void bindNormalTexture(int textureUnit) const;
-    void bindColorTexture(int textureUnit) const;
-    void bindRedTexture(int textureUnit) const;
-    void bindDepthTexture(int textureUnit) const;
-
-private:
-    void genTexture(int index, const Size& size);
-    void setupTexture(int index, const Size& size, GLuint* attachments, int& counter);
-    void bindTexture(int textureUnit, GLuint texture) const;
-    const char* getErrorString(GLenum error) const;
-
-    const Size& size;
-    int mode;
-    Array<GLuint, 5> textures;
-    GLuint buffer;
-    bool error;
-
-    struct Data final {
-        int mask;
-        GLint internalFormat;
-        GLenum format;
-        GLenum type;
-    };
-    Data data[5];
-};
-
-#endif

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit b071869bd3f254d2c1d619ff0dc2b47d7bd9899c
+Subproject commit 33d0fc4551fd47d5d1b607df6f2a807b6107c7a1

+ 1 - 2
meson.build

@@ -29,9 +29,9 @@ sourcesClient = ['client/Main.cpp',
     'gaming-core/wrapper/GLEW.cpp',
     'gaming-core/wrapper/Shader.cpp',
     'gaming-core/wrapper/Texture.cpp',
+    'gaming-core/wrapper/TextureFormat.cpp',
     'gaming-core/wrapper/VertexBuffer.cpp',
     'gaming-core/wrapper/Attributes.cpp',
-    'gaming-core/rendering/FileTexture.cpp',
     'gaming-core/images/PNGReader.cpp',
     'gaming-core/wrapper/Window.cpp',
     'gaming-core/wrapper/WindowOptions.cpp',
@@ -43,7 +43,6 @@ sourcesClient = ['client/Main.cpp',
     'client/rendering/Mesh.cpp',
     'client/Game.cpp',
     'client/rendering/FontRenderer.cpp',
-    'client/rendering/wrapper/Framebuffer.cpp',
     'client/rendering/NoiseTexture.cpp',
     'client/input/Controller.cpp',
     'client/rendering/RenderSettings.cpp',

+ 2 - 4
resources/shader/ssaoFragment.fs

@@ -1,10 +1,8 @@
 #version 430
 
 layout (binding = 0) uniform sampler2D worldPositionSamp;
-layout (binding = 1) uniform sampler2D worldNormalSamp;
-layout (binding = 2) uniform sampler2D worldColorSamp;
-layout (binding = 3) uniform sampler2D worldDepthSamp;
-layout (binding = 4) uniform sampler2D ssaoNoise;
+layout (binding = 1) uniform sampler2D worldDepthSamp;
+layout (binding = 2) uniform sampler2D ssaoNoise;
 
 const int numberOfSamples = 64;
 const vec3 ssaoKernel[64] = {