Skip to content

Commit

Permalink
Reduce framebuffer switches
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Jan 15, 2025
1 parent 1b190e3 commit 8f70e1c
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 57 deletions.
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/background.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PassBackground: RenderPass
{
if (group && gbuffer)
{
gbuffer.bind();
bindFramebuffer(gbuffer.framebuffer);

glScissor(0, 0, gbuffer.width, gbuffer.height);
glViewport(0, 0, gbuffer.width, gbuffer.height);
Expand Down Expand Up @@ -119,8 +119,6 @@ class PassBackground: RenderPass
}

glClear(GL_DEPTH_BUFFER_BIT);

gbuffer.unbind();
}
}
}
5 changes: 1 addition & 4 deletions src/dagon/render/deferred/passes/debugoutput.d
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PassDebugOutput: RenderPass
if (view && gbuffer)
{
if (outputBuffer)
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.colorTexture = gbuffer.colorTexture;
state.depthTexture = gbuffer.depthTexture;
Expand Down Expand Up @@ -105,9 +105,6 @@ class PassDebugOutput: RenderPass
screenSurface.render(&state);
debugOutputShader.unbindParameters(&state);
debugOutputShader.unbind();

if (outputBuffer)
outputBuffer.unbind();
}
}
}
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/decal.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PassDecal: RenderPass
{
if (group)
{
gbuffer.bind();
bindFramebuffer(gbuffer.framebuffer);

glScissor(0, 0, gbuffer.width, gbuffer.height);
glViewport(0, 0, gbuffer.width, gbuffer.height);
Expand All @@ -78,8 +78,6 @@ class PassDecal: RenderPass
decalShader.unbind();

glEnable(GL_DEPTH_TEST);

gbuffer.unbind();
}
}
}
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/emission.d
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PassEmission: RenderPass
{
if (outputBuffer && gbuffer)
{
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.colorTexture = gbuffer.colorTexture;
state.depthTexture = gbuffer.depthTexture;
Expand All @@ -88,8 +88,6 @@ class PassEmission: RenderPass
emissionShader.unbind();

glDisable(GL_BLEND);

outputBuffer.unbind();
}
}
}
10 changes: 1 addition & 9 deletions src/dagon/render/deferred/passes/environment.d
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PassEnvironment: RenderPass
{
if (outputBuffer && gbuffer)
{
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.colorTexture = gbuffer.colorTexture;
state.depthTexture = gbuffer.depthTexture;
Expand All @@ -77,19 +77,11 @@ class PassEnvironment: RenderPass
glScissor(0, 0, outputBuffer.width, outputBuffer.height);
glViewport(0, 0, outputBuffer.width, outputBuffer.height);

// Additive blending
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE);

environmentShader.bind();
environmentShader.bindParameters(&state);
screenSurface.render(&state);
environmentShader.unbindParameters(&state);
environmentShader.unbind();

//glDisable(GL_BLEND);

outputBuffer.unbind();
}
}
}
5 changes: 1 addition & 4 deletions src/dagon/render/deferred/passes/forward.d
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ class PassForward: RenderPass

prepareFramebuffer();

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
bindFramebuffer(framebuffer);

// TODO: move depth blit to separate stage
glBindFramebuffer(GL_READ_FRAMEBUFFER, gbuffer.framebuffer);
glBlitFramebuffer(0, 0, gbuffer.width, gbuffer.height, 0, 0, gbuffer.width, gbuffer.height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
Expand Down Expand Up @@ -139,8 +138,6 @@ class PassForward: RenderPass

glDisablei(GL_BLEND, 0);
glDisablei(GL_BLEND, 1);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
}
}
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/geometry.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PassGeometry: RenderPass

if (group && gbuffer)
{
gbuffer.bind();
bindFramebuffer(gbuffer.framebuffer);

glScissor(0, 0, gbuffer.width, gbuffer.height);
glViewport(0, 0, gbuffer.width, gbuffer.height);
Expand All @@ -90,8 +90,6 @@ class PassGeometry: RenderPass
}
}
geometryShader.unbind();

gbuffer.unbind();
}
}
}
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/light.d
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PassLight: RenderPass
{
if (groupSunLights && groupAreaLights && outputBuffer && gbuffer)
{
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.colorTexture = gbuffer.colorTexture;
state.depthTexture = gbuffer.depthTexture;
Expand Down Expand Up @@ -147,8 +147,6 @@ class PassLight: RenderPass
glEnable(GL_DEPTH_TEST);

glDisable(GL_BLEND);

outputBuffer.unbind();
}
}
}
8 changes: 2 additions & 6 deletions src/dagon/render/deferred/passes/occlusion.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ class PassOcclusion: RenderPass

override void render()
{
if (view && gbuffer)
if (view && gbuffer && outputBuffer)
{
if (outputBuffer)
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.colorTexture = gbuffer.colorTexture;
state.depthTexture = gbuffer.depthTexture;
Expand All @@ -77,9 +76,6 @@ class PassOcclusion: RenderPass
screenSurface.render(&state);
ssaoShader.unbindParameters(&state);
ssaoShader.unbind();

if (outputBuffer)
outputBuffer.unbind();
}
}
}
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/particles.d
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PassParticles: RenderPass
{
if (group && outputBuffer && gbuffer)
{
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.depthTexture = gbuffer.depthTexture;
state.environment = pipeline.environment;
Expand All @@ -95,8 +95,6 @@ class PassParticles: RenderPass
particleShader.unbind();

glDisable(GL_BLEND);

outputBuffer.unbind();
}
}
}
4 changes: 1 addition & 3 deletions src/dagon/render/deferred/passes/probe.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PassEnvironmentProbe: RenderPass
{
if (outputBuffer && gbuffer)
{
outputBuffer.bind();
bindFramebuffer(outputBuffer.framebuffer);

state.colorTexture = gbuffer.colorTexture;
state.depthTexture = gbuffer.depthTexture;
Expand Down Expand Up @@ -94,8 +94,6 @@ class PassEnvironmentProbe: RenderPass

glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

outputBuffer.unbind();
}
}
}
12 changes: 6 additions & 6 deletions src/dagon/render/deferred/passes/shadow.d
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,28 @@ class PassShadow: RenderPass
state.invViewMatrix = csm.area[0].invViewMatrix;
state.projectionMatrix = csm.area[0].projectionMatrix;
state.invProjectionMatrix = csm.area[0].projectionMatrix.inverse;
glBindFramebuffer(GL_FRAMEBUFFER, csm.framebuffer1);
//glBindFramebuffer(GL_FRAMEBUFFER, csm.framebuffer1);
bindFramebuffer(csm.framebuffer1);
glClear(GL_DEPTH_BUFFER_BIT);
renderEntities(csmShader);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

state.viewMatrix = csm.area[1].viewMatrix;
state.invViewMatrix = csm.area[1].invViewMatrix;
state.projectionMatrix = csm.area[1].projectionMatrix;
state.invProjectionMatrix = csm.area[1].projectionMatrix.inverse;
glBindFramebuffer(GL_FRAMEBUFFER, csm.framebuffer2);
//glBindFramebuffer(GL_FRAMEBUFFER, csm.framebuffer2);
bindFramebuffer(csm.framebuffer2);
glClear(GL_DEPTH_BUFFER_BIT);
renderEntities(csmShader);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

state.viewMatrix = csm.area[2].viewMatrix;
state.invViewMatrix = csm.area[2].invViewMatrix;
state.projectionMatrix = csm.area[2].projectionMatrix;
state.invProjectionMatrix = csm.area[2].projectionMatrix.inverse;
glBindFramebuffer(GL_FRAMEBUFFER, csm.framebuffer3);
//glBindFramebuffer(GL_FRAMEBUFFER, csm.framebuffer3);
bindFramebuffer(csm.framebuffer3);
glClear(GL_DEPTH_BUFFER_BIT);
renderEntities(csmShader);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glEnable(GL_CULL_FACE);
glPolygonOffset(0.0, 0.0);
Expand Down
9 changes: 2 additions & 7 deletions src/dagon/render/deferred/passes/terrain.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ class PassTerrain: RenderPass
uint terrains = 0;

prepareFramebuffer();

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
bindFramebuffer(framebuffer);

glScissor(0, 0, gbuffer.width, gbuffer.height);
glViewport(0, 0, gbuffer.width, gbuffer.height);
Expand All @@ -137,16 +136,14 @@ class PassTerrain: RenderPass
}
terrainShader.unbind();

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

// Texturing passes
// TODO: do it for each terrain entity (need to manage state properly)
TerrainMaterial terrainMaterial = pipeline.environment.terrainMaterial;
if (terrainMaterial && terrains > 0)
{
updateState();

gbuffer.bind();
bindFramebuffer(gbuffer.framebuffer);

state.normalTexture = normalBuffer.colorTexture;
state.texcoordTexture = texcoordBuffer.colorTexture;
Expand Down Expand Up @@ -185,8 +182,6 @@ class PassTerrain: RenderPass
glDisablei(GL_BLEND, 1);
glDisablei(GL_BLEND, 2);
glDisablei(GL_BLEND, 3);

gbuffer.unbind();
}
}
}
12 changes: 12 additions & 0 deletions src/dagon/render/pipeline.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ import dagon.graphics.entity;
import dagon.graphics.environment;
import dagon.render.pass;

__gshared GLuint currentFramebuffer = 0;

// Does nothing if the buffer already bound
void bindFramebuffer(GLuint fb)
{
if (fb != currentFramebuffer)
{
currentFramebuffer = fb;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
}
}

class RenderPipeline: EventListener
{
Array!RenderPass passes;
Expand Down

0 comments on commit 8f70e1c

Please sign in to comment.