Skip to content

Commit

Permalink
seperating shaders for speed
Browse files Browse the repository at this point in the history
Signed-off-by: Perminder <[email protected]>
  • Loading branch information
perminder-17 committed Sep 25, 2024
1 parent dab63d4 commit 0c27224
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 42 deletions.
1 change: 1 addition & 0 deletions avogadro/rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ set(shader_files
"mesh_vs.glsl"
"solid_vs.glsl"
"solid_first_fs.glsl"
"solid_fog_fs.glsl"
"spheres_fs.glsl"
"spheres_vs.glsl"
"sphere_ao_depth_vs.glsl"
Expand Down
36 changes: 6 additions & 30 deletions avogadro/rendering/solid_first_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,21 @@ varying vec2 UV;
// RGB rendered texture
uniform sampler2D inRGBTex;

// RGB color for applying fog
uniform float fogR;
uniform float fogG;
uniform float fogB;
uniform float offset;

// Depth rendered texture
uniform sampler2D inDepthTex;
// 1.0 if enabled, 0.0 if disabled
uniform float inAoEnabled;
// 1.0 if enabled, 0.0 if disabled
uniform float inDofEnabled;
// 0.0 if disabled
uniform float inFogStrength;
// Shadow strength for SSAO
uniform float inAoStrength;
// 1.0 if enabled, 0.0 if disabled
uniform float inEdStrength;
// amount of offset when zoom-in or zoom-out.
uniform float uoffset;
// Dof strength
// Dof strength 0.0 if dof is disabled.
uniform float inDofStrength;
// Dof position
uniform float inDofPosition;
// position for other molecules.
uniform float inFogPosition;
// Rendering surface dimensions, in pixels
uniform float width, height;

Expand Down Expand Up @@ -127,14 +116,6 @@ vec4 applyBlur(vec2 texCoord) {
}
return color / total;
}
vec4 applyFog(vec2 texCoord) {
vec4 finalColor = mix(
texture2D(inRGBTex, texCoord),
vec4(vec3(fogR, fogG, fogB), 1.),
pow(texture2D(inDepthTex, texCoord.xy).r, uoffset * inFogPosition/10.0)
) + inFogStrength / 100.0;
return finalColor;
}

const vec2 SSAOkernel[16] = vec2[16](
vec2(0.072170, 0.081556),
Expand Down Expand Up @@ -184,25 +165,20 @@ float computeEdgeLuminosity(vec3 normal)
}

void main() {
// Some cleanups required.

float luminosity = 1.0;
luminosity *= max(1.2 * (1.0 - inAoEnabled), computeSSAOLuminosity(getNormalNear(UV)));
luminosity *= max(1.0 - inEdStrength, computeEdgeLuminosity(getNormalAt(UV)));
vec4 color = texture2D(inRGBTex, UV);
if (inFogStrength == 0.0 && inDofEnabled == 0.0) {

if (inDofStrength == 0.0) {
gl_FragColor = vec4(color.xyz * luminosity, color.w);
}
else if (inFogStrength != 0.0) { // apply fog since it's fast.
// Apply fog to the color texture
vec4 foggedColor = applyFog(UV);
vec4 fogColor = vec4(luminosity * foggedColor.xyz, foggedColor.w);
gl_FragColor = fogColor;
}
else {
} else {
// Apply blur to the color texture
vec4 blurredColor = applyBlur(UV);
vec4 blurColor = vec4(luminosity * blurredColor.xyz, blurredColor.w);
gl_FragColor = blurColor;
}

gl_FragDepth = texture2D(inDepthTex, UV).x;
}
34 changes: 34 additions & 0 deletions avogadro/rendering/solid_fog_fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 120

varying vec2 UV;

uniform sampler2D inRGBTex;

uniform float fogR;
uniform float fogG;
uniform float fogB;

uniform sampler2D inDepthTex;
uniform float inFogStrength;
uniform float uoffset;
uniform float inFogPosition;

vec4 applyFog(vec2 texCoord) {
vec4 finalColor = mix(
texture2D(inRGBTex, texCoord),
vec4(vec3(fogR, fogG, fogB), 1.),
pow(texture2D(inDepthTex, texCoord.xy).r, uoffset * inFogPosition/10.0)
) + inFogStrength / 100.0;

return finalColor;
}


void main() {

vec4 foggedColor = applyFog(UV);

gl_FragColor = foggedColor;
gl_FragDepth = texture2D(inDepthTex, UV).x;

}
48 changes: 38 additions & 10 deletions avogadro/rendering/solidpipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
#include "shader.h"
#include "camera.h"
#include "shaderprogram.h"
#include "camera.h"

#include "solid_vs.h"

#include "solid_first_fs.h"
#include "solid_fog_fs.h"

#include <iostream>

Expand Down Expand Up @@ -53,8 +52,10 @@ class SolidPipeline::Private
GLuint depthTexture;
GLuint screenVBO;
ShaderProgram firstStageShaders;
ShaderProgram fogStageShaders;
Shader screenVertexShader;
Shader firstFragmentShader;
Shader fogFragmentShader;
};

static const GLfloat s_fullscreenQuad[] = {
Expand Down Expand Up @@ -115,15 +116,30 @@ void SolidPipeline::initialize()
if (!d->screenVertexShader.compile())
std::cout << d->screenVertexShader.error() << std::endl;

// shader program for fog.
d->fogFragmentShader.setType(Shader::Fragment);
d->fogFragmentShader.setSource(solid_fog_fs);
if(!d->fogFragmentShader.compile())
std::cout << d->fogFragmentShader.error() << std::endl;

// shader for ao and blur.
d->firstFragmentShader.setType(Shader::Fragment);
d->firstFragmentShader.setSource(solid_first_fs);
if (!d->firstFragmentShader.compile())
std::cout << d->firstFragmentShader.error() << std::endl;


d->firstStageShaders.attachShader(d->screenVertexShader);
d->firstStageShaders.attachShader(d->firstFragmentShader);
if (!d->firstStageShaders.link())
std::cout << d->firstStageShaders.error() << std::endl;

// shaderProgram for fog.
d->fogStageShaders.attachShader(d->screenVertexShader);
d->fogStageShaders.attachShader(d->fogFragmentShader);
if (!d->fogStageShaders.link())
std::cout << d->fogStageShaders.error() << std::endl;

}

void SolidPipeline::begin()
Expand Down Expand Up @@ -158,20 +174,27 @@ void SolidPipeline::end()
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDrawBuffer(GL_BACK);
}

if(m_fogEnabled){
// shader for fog.
d->attachStage(d->fogStageShaders, "inRGBTex", d->renderTexture, "inDepthTex",
d->depthTexture, m_width, m_height);
d->fogStageShaders.setUniformValue("inFogStrength", m_fogStrength);
d->fogStageShaders.setUniformValue("inFogPosition", m_fogPosition);
d->fogStageShaders.setUniformValue("fogR", (m_backgroundColor[0])/255.0f);
d->fogStageShaders.setUniformValue("fogG", (m_backgroundColor[1])/255.0f);
d->fogStageShaders.setUniformValue("fogB", (m_backgroundColor[2])/255.0f);
} else {
d->attachStage(d->firstStageShaders, "inRGBTex", d->renderTexture, "inDepthTex",
d->depthTexture, m_width, m_height);
d->firstStageShaders.setUniformValue("inAoEnabled", m_aoEnabled ? 1.0f : 0.0f);
d->firstStageShaders.setUniformValue("inDofEnabled", m_dofEnabled ? 1.0f : 0.0f);
d->firstStageShaders.setUniformValue("inDofEnabled", m_dofEnabled ? m_dofStrength : 0.0f);
d->firstStageShaders.setUniformValue("inDofStrength", ((m_dofStrength) * 100.0f));
d->firstStageShaders.setUniformValue("inDofPosition", ((m_dofPosition) /10.0f));
d->firstStageShaders.setUniformValue("inAoStrength", m_aoStrength);
d->firstStageShaders.setUniformValue("inEdStrength", m_edStrength);
d->firstStageShaders.setUniformValue("inFogEnabled", m_fogEnabled ? 1.0f : 0.0f);
d->firstStageShaders.setUniformValue("inFogStrength", m_fogEnabled ? m_fogStrength : 0.0f);
d->firstStageShaders.setUniformValue("inFogPosition", m_fogPosition);
d->firstStageShaders.setUniformValue("fogR", (m_backgroundColor[0])/255.0f);
d->firstStageShaders.setUniformValue("fogG", (m_backgroundColor[1])/255.0f);
d->firstStageShaders.setUniformValue("fogB", (m_backgroundColor[2])/255.0f);
}

glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
}
Expand All @@ -193,7 +216,12 @@ void SolidPipeline::adjustOffset(const Camera& cam) {
else if(project >= 21595.588 ){
offSet = 9.952 * project - 212865;
}
// setting uniforms.
if (m_fogEnabled){
d->fogStageShaders.setUniformValue("uoffset", offSet);
} else {
d->firstStageShaders.setUniformValue("uoffset", offSet);
}

}

Expand All @@ -216,4 +244,4 @@ void SolidPipeline::setPixelRatio(float ratio)
m_pixelRatio = ratio;
}

} // End namespace Avogadro::Rendering
} // End namespace Avogadro::Rendering
4 changes: 2 additions & 2 deletions avogadro/rendering/solidpipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ class SolidPipeline
private:
float m_pixelRatio;
bool m_aoEnabled;
Eigen::Affine3f modelView;
float m_dofStrength;
float m_dofPosition;
bool m_dofEnabled;
float m_fogPosition;
Vector4ub m_backgroundColor;
Eigen::Affine3f modelView;
bool m_fogEnabled;
float m_aoStrength;
float m_fogStrength;
Expand All @@ -143,4 +143,4 @@ class SolidPipeline
} // End namespace Rendering
} // End namespace Avogadro

#endif
#endif

0 comments on commit 0c27224

Please sign in to comment.