split render pipelines

This commit is contained in:
MihailRis
2025-05-09 15:31:04 +03:00
parent fc46b7c434
commit c1608fe8e2
13 changed files with 176 additions and 191 deletions
+2 -40
View File
@@ -42,22 +42,8 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
// Setup color attachment (texture)
texture = create_texture(width, height, format);
glGenTextures(1, &positions);
glBindTexture(GL_TEXTURE_2D, positions);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, positions, 0);
glGenTextures(1, &normals);
glBindTexture(GL_TEXTURE_2D, normals);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, normals, 0);
unsigned int attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, attachments);
unsigned int attachments[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, attachments);
// Setup depth attachment
glGenRenderbuffers(1, &depth);
@@ -74,7 +60,6 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
Framebuffer::~Framebuffer() {
glDeleteFramebuffers(1, &fbo);
glDeleteTextures(1, &normals);
glDeleteTextures(1, &depth);
}
@@ -86,19 +71,6 @@ void Framebuffer::unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::bindBuffers() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->getId());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, positions);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, normals);
glActiveTexture(GL_TEXTURE0);
}
void Framebuffer::resize(uint width, uint height) {
if (this->width == width && this->height == height) {
return;
@@ -112,16 +84,6 @@ void Framebuffer::resize(uint width, uint height) {
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, positions);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, positions, 0);
glBindTexture(GL_TEXTURE_2D, normals);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, normals, 0);
glBindTexture(GL_TEXTURE_2D, 0);
texture = create_texture(width, height, format);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
-4
View File
@@ -10,8 +10,6 @@ class Texture;
class Framebuffer : public Bindable {
uint fbo;
uint depth;
uint positions;
uint normals;
uint width;
uint height;
uint format;
@@ -27,8 +25,6 @@ public:
/// @brief Stop using framebuffer
void unbind() override;
void bindBuffers();
/// @brief Update framebuffer texture size
/// @param width new width
/// @param height new height
+11 -1
View File
@@ -117,7 +117,7 @@ void GBuffer::unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void GBuffer::bindBuffers() {
void GBuffer::bindBuffers() const {
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, normalsBuffer);
@@ -177,6 +177,16 @@ void GBuffer::resize(uint width, uint height) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
std::unique_ptr<ImageData> GBuffer::toImage() const {
auto data = std::make_unique<ubyte[]>(width * height * 3);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, data.get());
glBindTexture(GL_TEXTURE_2D, 0);
return std::make_unique<ImageData>(
ImageFormat::rgb888, width, height, std::move(data)
);
}
uint GBuffer::getWidth() const {
return width;
}
+6 -3
View File
@@ -2,21 +2,24 @@
#include "typedefs.hpp"
#include "commons.hpp"
#include "ImageData.hpp"
class GBuffer : public Bindable{
class GBuffer : public Bindable {
public:
GBuffer(uint width, uint height);
~GBuffer();
~GBuffer() override;
void bind() override;
void unbind() override;
void bindBuffers();
void bindBuffers() const;
void resize(uint width, uint height);
uint getWidth() const;
uint getHeight() const;
std::unique_ptr<ImageData> toImage() const;
private:
uint width;
uint height;
+68 -49
View File
@@ -1,6 +1,7 @@
#include "PostProcessing.hpp"
#include "Mesh.hpp"
#include "Shader.hpp"
#include "GBuffer.hpp"
#include "Texture.hpp"
#include "Framebuffer.hpp"
#include "DrawContext.hpp"
@@ -60,22 +61,65 @@ PostProcessing::PostProcessing(size_t effectSlotsCount)
PostProcessing::~PostProcessing() = default;
void PostProcessing::use(DrawContext& context) {
void PostProcessing::use(DrawContext& context, bool gbufferPipeline) {
const auto& vp = context.getViewport();
if (fbo) {
fbo->resize(vp.x, vp.y);
fboSecond->resize(vp.x, vp.y);
if (gbufferPipeline) {
if (gbuffer == nullptr) {
gbuffer = std::make_unique<GBuffer>(vp.x, vp.y);
} else {
gbuffer->resize(vp.x, vp.y);
}
context.setFramebuffer(gbuffer.get());
} else {
fbo = std::make_unique<Framebuffer>(vp.x, vp.y);
fboSecond = std::make_unique<Framebuffer>(vp.x, vp.y);
if (fbo) {
fbo->resize(vp.x, vp.y);
fboSecond->resize(vp.x, vp.y);
} else {
fbo = std::make_unique<Framebuffer>(vp.x, vp.y);
fboSecond = std::make_unique<Framebuffer>(vp.x, vp.y);
}
context.setFramebuffer(fbo.get());
}
context.setFramebuffer(fbo.get());
}
void PostProcessing::configureEffect(
const DrawContext& context,
Shader& shader,
float timer,
const Camera& camera,
uint shadowMap
) {
const auto& viewport = context.getViewport();
if (!ssaoConfigured) {
for (unsigned int i = 0; i < 64; ++i) {
auto name = "u_ssaoSamples["+ std::to_string(i) + "]";
shader.uniform3f(name, ssaoKernel[i]);
}
ssaoConfigured = true;
}
shader.uniform1i("u_screen", 0);
if (gbuffer) {
shader.uniform1i("u_position", 1);
shader.uniform1i("u_normal", 2);
}
shader.uniform1i("u_noise", 3);
shader.uniform1i("u_shadows", 4);
shader.uniform2i("u_screenSize", viewport);
shader.uniform1f("u_timer", timer);
shader.uniform1i("u_enableShadows", shadowMap != 0);
shader.uniformMatrix("u_projection", camera.getProjection());
}
void PostProcessing::render(
const DrawContext& context, const Assets& assets, float timer, const Camera& camera, uint depthMap
const DrawContext& context,
const Assets& assets,
float timer,
const Camera& camera,
uint shadowMap
) {
if (fbo == nullptr) {
if (fbo == nullptr && gbuffer == nullptr) {
throw std::runtime_error("'use(...)' was never called");
}
int totalPasses = 0;
@@ -83,40 +127,24 @@ void PostProcessing::render(
totalPasses += (effect != nullptr && effect->isActive());
}
if (totalPasses == 0) {
auto& effect = assets.require<PostEffect>("default");
fbo->getTexture()->bind();
fbo->bindBuffers();
if (gbuffer) {
gbuffer->bindBuffers();
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, noiseTexture);
glActiveTexture(GL_TEXTURE0);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, depthMap);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shadowMap);
} else {
fbo->getTexture()->bind();
}
const auto& viewport = context.getViewport();
glActiveTexture(GL_TEXTURE0);
if (totalPasses == 0) {
auto& effect = assets.require<PostEffect>("default");
auto& shader = effect.use();
if (!ssaoConfigured) {
for (unsigned int i = 0; i < 64; ++i) {
auto name = "samples["+ std::to_string(i) + "]";
shader.uniform3f(name, ssaoKernel[i]);
}
ssaoConfigured = true;
}
shader.uniform1i("u_screen", 0);
shader.uniform1i("u_position", 1);
shader.uniform1i("u_normal", 2);
shader.uniform1i("u_noise", 3);
shader.uniform1i("u_shadows", 4);
shader.uniform2i("u_screenSize", viewport);
shader.uniform1f("u_timer", timer);
shader.uniform1f("near_plane", camera.near);
shader.uniform1f("far_plane", camera.far);
shader.uniformMatrix("u_projection", camera.getProjection());
configureEffect(context, shader, timer, camera, shadowMap);
quadMesh->draw();
return;
}
@@ -127,21 +155,9 @@ void PostProcessing::render(
continue;
}
auto& shader = effect->use();
configureEffect(context, shader, timer, camera, shadowMap);
for (unsigned int i = 0; i < 64; ++i) {
shader.uniform3f("samples["+ std::to_string(i) + "]", ssaoKernel[i]);
}
const auto& viewport = context.getViewport();
shader.uniform1i("u_screen", 0);
shader.uniform1i("u_position", 1);
shader.uniform1i("u_normal", 2);
shader.uniform1i("u_noise", 3);
shader.uniform2i("u_screenSize", viewport);
shader.uniform1f("u_timer", timer);
shader.uniformMatrix("u_projection", camera.getProjView(false));
fbo->getTexture()->bind();
fbo->bindBuffers();
if (currentPass < totalPasses) {
fboSecond->bind();
@@ -165,6 +181,9 @@ PostEffect* PostProcessing::getEffect(size_t slot) {
}
std::unique_ptr<ImageData> PostProcessing::toImage() {
if (gbuffer) {
return gbuffer->toImage();
}
return fbo->getTexture()->readData();
}
+30 -12
View File
@@ -12,6 +12,8 @@ class DrawContext;
class ImageData;
class PostEffect;
class Camera;
class GBuffer;
class Shader;
struct PostProcessingVertex {
glm::vec2 position;
@@ -25,29 +27,25 @@ struct PostProcessingVertex {
/// @attention Current implementation does not support multiple render passes
/// for multiple effects. Will be implemented in v0.21
class PostProcessing {
/// @brief Main framebuffer (lasy field)
std::unique_ptr<Framebuffer> fbo;
std::unique_ptr<Framebuffer> fboSecond;
/// @brief Fullscreen quad mesh as the post-processing canvas
std::unique_ptr<Mesh<PostProcessingVertex>> quadMesh;
std::vector<std::shared_ptr<PostEffect>> effectSlots;
std::vector<glm::vec3> ssaoKernel;
uint noiseTexture;
bool ssaoConfigured = false;
public:
PostProcessing(size_t effectSlotsCount);
~PostProcessing();
/// @brief Prepare and bind framebuffer
/// @param context graphics context will be modified
void use(DrawContext& context);
void use(DrawContext& context, bool gbufferPipeline);
/// @brief Render fullscreen quad using the passed shader
/// with framebuffer texture bound
/// @param context graphics context
/// @throws std::runtime_error if use(...) wasn't called before
void render(const DrawContext& context, const Assets& assets, float timer, const Camera& camera, uint depthMap);
void render(
const DrawContext& context,
const Assets& assets,
float timer,
const Camera& camera,
uint shadowMap
);
void setEffect(size_t slot, std::shared_ptr<PostEffect> effect);
@@ -57,4 +55,24 @@ public:
std::unique_ptr<ImageData> toImage();
Framebuffer* getFramebuffer() const;
private:
void configureEffect(
const DrawContext& context,
Shader& shader,
float timer,
const Camera& camera,
uint shadowMap
);
/// @brief Main framebuffer (lasy field)
std::unique_ptr<Framebuffer> fbo;
std::unique_ptr<Framebuffer> fboSecond;
/// @brief Fullscreen quad mesh as the post-processing canvas
std::unique_ptr<Mesh<PostProcessingVertex>> quadMesh;
std::vector<std::shared_ptr<PostEffect>> effectSlots;
std::unique_ptr<GBuffer> gbuffer;
std::vector<glm::vec3> ssaoKernel;
uint noiseTexture;
bool ssaoConfigured = false;
};
+14 -17
View File
@@ -103,7 +103,7 @@ WorldRenderer::WorldRenderer(
assets->require<Shader>("skybox_gen")
);
shadowMap = std::make_unique<ShadowMap>(1024);
shadowMap = std::make_unique<ShadowMap>(1024 * 4);
shadowCamera = std::make_unique<Camera>();
}
@@ -373,18 +373,18 @@ void WorldRenderer::draw(
auto& shadowsShader = assets.require<Shader>("shadows");
if (gbufferPipeline) {
float shadowMapScale = 0.05f;
float shadowMapScale = 0.05f * 2;
float shadowMapSize = shadowMap->getResolution() * shadowMapScale;
*shadowCamera = Camera(camera.position, shadowMapSize);
shadowCamera->near = 0.5f;
shadowCamera->far = 600.0f;
shadowCamera->perspective = false;
shadowCamera->setAspectRatio(1.0f);
shadowCamera->rotate(glm::radians(-65.0f), glm::radians(-35.0f), glm::radians(-35.0f));
shadowCamera->rotate(glm::radians(-64.0f), glm::radians(-35.0f), glm::radians(-35.0f));
//shadowCamera.perspective = false;
/*shadowCamera.rotation = //glm::inverse(
glm::lookAt({}, glm::normalize(shadowCamera.position-camera.position), glm::vec3(0, 1, 0));
//);*/
// shadowCamera->rotation = glm::inverse(
// glm::lookAt({}, glm::normalize(camera.position-shadowCamera->position), glm::vec3(0, 1, 0))
// );
shadowCamera->updateVectors();
//shadowCamera->position += camera.dir * shadowMapSize * 0.5f;
shadowCamera->position -= shadowCamera->front * 100.0f;
@@ -406,16 +406,7 @@ void WorldRenderer::draw(
/* World render scope with diegetic HUD included */ {
DrawContext wctx = pctx.sub();
if (gbufferPipeline) {
if (gbuffer == nullptr) {
gbuffer = std::make_unique<GBuffer>(vp.x, vp.y);
} else {
gbuffer->resize(vp.x, vp.y);
}
wctx.setFramebuffer(gbuffer.get());
} else {
postProcessing.use(wctx);
}
postProcessing.use(wctx, gbufferPipeline);
display::clearDepth();
@@ -446,7 +437,13 @@ void WorldRenderer::draw(
//renderBlockOverlay(wctx);
}
postProcessing.render(pctx, assets, timer, camera, shadowMap->getDepthMap());
postProcessing.render(
pctx,
assets,
timer,
camera,
gbufferPipeline ? shadowMap->getDepthMap() : 0
);
}
void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
+1 -3
View File
@@ -54,9 +54,7 @@ class WorldRenderer {
float timer = 0.0f;
bool debug = false;
bool lightsDebug = false;
std::unique_ptr<GBuffer> gbuffer;
bool gbufferPipeline = false;
bool gbufferPipeline = true;
/// @brief Render block selection lines
void renderBlockSelection();