Skip to content

Commit

Permalink
Implement PCSS & Revert graphics level 8
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingJellyfish committed Feb 3, 2025
1 parent 017b7bc commit d64b156
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 63 deletions.
54 changes: 31 additions & 23 deletions data/gui/dialogs/custom_video_settings.stkgui
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div layout="horizontal-row" proportion="1" height="fit">
<label text="Shadows" I18N="Video settings"/>
<spacer width="10" height="10"/>
<gauge id="shadows" min_value="0" max_value="4" proportion="1"/>
<gauge id="shadows" min_value="0" max_value="3" proportion="1"/>
</div>
</div>

Expand All @@ -39,17 +39,17 @@
<spacer width="70" height="10"/>

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="mlaa"/>
<checkbox id="bloom"/>
<spacer width="10" height="10"/>
<label text="Anti-aliasing" I18N="Video settings"/>
<label text="Bloom" I18N="Video settings"/>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="lightscattering"/>
<checkbox id="lightshaft"/>
<spacer width="10" height="10"/>
<label text="Light Scattering" I18N="Video settings"/>
<label text="Light shaft (God rays)" I18N="Video settings"/>
</div>
</div>

Expand All @@ -59,69 +59,77 @@
<spacer width="70" height="10"/>

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="glow"/>
<checkbox id="ssao"/>
<spacer width="10" height="10"/>
<label text="Glow (Outlines)" I18N="Video settings"/>
<label text="Ambient occlusion" I18N="Video settings"/>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="lightshaft"/>
<checkbox id="dof"/>
<spacer width="10" height="10"/>
<label text="Light shaft (God rays)" I18N="Video settings"/>
<label text="Depth of field" I18N="Video settings"/>
</div>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" width="100%" proportion="1">
<spacer width="70" height="10"/>

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="ibl"/>
<checkbox id="glow"/>
<spacer width="10" height="10"/>
<label text="Image-based lighting" I18N="Video settings"/>
<label text="Glow (Outlines)" I18N="Video settings"/>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="bloom"/>
<checkbox id="mlaa"/>
<spacer width="10" height="10"/>
<label text="Bloom" I18N="Video settings"/>
<label text="Anti-aliasing" I18N="Video settings"/>
</div>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" width="100%" proportion="1">
<spacer width="70" height="10"/>

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="motionblur"/>
<spacer width="10" height="10"/>
<label text="Motion blur" I18N="Video settings"/>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="ssao"/>
<checkbox id="ibl"/>
<spacer width="10" height="10"/>
<label text="Ambient occlusion" I18N="Video settings"/>
<label text="Image-based lighting" I18N="Video settings"/>
</div>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" width="100%" proportion="1">
<spacer width="70" height="10"/>

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="motionblur"/>
<checkbox id="lightscattering"/>
<spacer width="10" height="10"/>
<label text="Motion blur" I18N="Video settings"/>
<label text="Light Scattering" I18N="Video settings"/>
</div>

<spacer height="4" width="10" />

<div layout="horizontal-row" proportion="1" height="fit">
<checkbox id="dof"/>
<checkbox id="pcss"/>
<spacer width="10" height="10"/>
<label text="Depth of field" I18N="Video settings"/>
<label text="Soft shadows" I18N="Video settings"/>
</div>
</div>

Expand Down Expand Up @@ -167,7 +175,7 @@
<gauge id="geometry_detail" min_value="0" max_value="5" width="50%" />
</div>

<spacer height="10" width="10"/>
<spacer height="10" width="10" />

<buttonbar id="buttons" height="15%" width="30%" align="center">
<icon-button id="cancel" width="128" height="128" icon="gui/icons/red_x.png"
Expand All @@ -176,4 +184,4 @@
I18N="Video settings" text="Apply" align="center"/>
</buttonbar>
</div>
</stkgui>
</stkgui>
4 changes: 4 additions & 0 deletions src/config/user_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,10 @@ namespace UserConfigParams
PARAM_DEFAULT(BoolUserConfigParam(true,
"light_scatter", &m_graphics_quality,
"Enable light scattering shaders") );
PARAM_PREFIX BoolUserConfigParam m_pcss
PARAM_DEFAULT(BoolUserConfigParam(false,
"pcss", &m_graphics_quality,
"Enable Percentage Filtered Soft Shadows (Very heavy, needs high shadow resolution)") );
PARAM_PREFIX IntUserConfigParam m_shadows_resolution
PARAM_DEFAULT( IntUserConfigParam(0,
"shadows_resolution", &m_graphics_quality,
Expand Down
72 changes: 66 additions & 6 deletions src/graphics/lighting_passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,53 @@ class ShadowedSunLightShaderPCF : public TextureShader<ShadowedSunLightShaderPCF
} // render
}; // ShadowedSunLightShaderPCF

// ============================================================================
class ShadowedSunLightShaderPCSS : public TextureShader<ShadowedSunLightShaderPCSS,
4, float, float, float,
float, float, float,
core::vector3df, core::vector3df,
core::vector3df, core::vector3df,
core::vector3df, video::SColorf>
{
public:
ShadowedSunLightShaderPCSS()
{
loadProgram(OBJECT, GL_VERTEX_SHADER, "screenquad.vert",
GL_FRAGMENT_SHADER, "sunlightshadowpcss.frag");

// Use 8 to circumvent a catalyst bug when binding sampler
assignSamplerNames(0, "ntex", ST_NEAREST_FILTERED,
1, "dtex", ST_NEAREST_FILTERED,
8, "shadowtexdepth", ST_NEAREST_FILTERED_ARRAY2D,
16, "shadowtex", ST_SHADOW_SAMPLER);
assignUniforms("split0", "split1", "split2", "splitmax", "shadow_res",
"overlap_proportion", "box0", "box1", "box2", "box3", "sundirection", "sun_color");
} // ShadowedSunLightShaderPCF
// ------------------------------------------------------------------------
void render(GLuint normal_depth_texture,
GLuint depth_stencil_texture,
const FrameBuffer* shadow_framebuffer,
const core::vector3df &direction,
const video::SColorf &col,
const core::vector3df* shadow_box_extents)
{
setTextureUnits(normal_depth_texture,
depth_stencil_texture,
shadow_framebuffer->getDepthTexture(),
shadow_framebuffer->getDepthTexture() );
drawFullScreenEffect(ShadowMatrices::m_shadow_split[1],
ShadowMatrices::m_shadow_split[2],
ShadowMatrices::m_shadow_split[3],
ShadowMatrices::m_shadow_split[4],
float(UserConfigParams::m_shadows_resolution),
ShadowMatrices::m_shadow_overlap_proportion,
shadow_box_extents[0], shadow_box_extents[1],
shadow_box_extents[2], shadow_box_extents[3],
direction, col);

} // render
}; // ShadowedSunLightShaderPCSS

// ============================================================================
class SunLightShader : public TextureShader<SunLightShader, 2,
core::vector3df, video::SColorf>
Expand Down Expand Up @@ -443,7 +490,8 @@ void LightingPasses::renderLights( bool has_shadow,
const FrameBuffer* shadow_framebuffer,
GLuint ssao_texture,
GLuint diffuse_color_texture,
GLuint specular_probe)
GLuint specular_probe,
const core::vector3df* shadow_box_extents)
{
{
ScopedGPUTimer timer(irr_driver->getGPUTimer(Q_ENVMAP));
Expand All @@ -466,11 +514,23 @@ void LightingPasses::renderLights( bool has_shadow,
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_ONE, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
ShadowedSunLightShaderPCF::getInstance()->render(normal_depth_texture,
depth_stencil_texture,
shadow_framebuffer,
irr_driver->getSunDirection(),
irr_driver->getSunColor());
if (UserConfigParams::m_pcss)
{
ShadowedSunLightShaderPCSS::getInstance()->render(normal_depth_texture,
depth_stencil_texture,
shadow_framebuffer,
irr_driver->getSunDirection(),
irr_driver->getSunColor(),
shadow_box_extents);
}
else
{
ShadowedSunLightShaderPCF::getInstance()->render(normal_depth_texture,
depth_stencil_texture,
shadow_framebuffer,
irr_driver->getSunDirection(),
irr_driver->getSunColor());
}
}
else
renderSunlight(irr_driver->getSunDirection(),
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/lighting_passes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class LightingPasses
const FrameBuffer* shadow_framebuffer,
GLuint ssao_texture,
GLuint diffuse_color_texture,
GLuint specular_probe);
GLuint specular_probe,
const core::vector3df* shadow_box_extents);
void renderLightsScatter(GLuint depth_stencil_texture,
const FrameBuffer& half1_framebuffer,
const FrameBuffer& half2_framebuffer,
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/shader_based_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca
m_rtts->getShadowFrameBuffer(),
m_rtts->getRenderTarget(RTT_HALF1_R),
m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR),
specular_probe);
specular_probe,
m_shadow_matrices.getShadowFrustumBoxExtent());
PROFILER_POP_CPU_MARKER();
}

Expand Down
8 changes: 6 additions & 2 deletions src/graphics/shadow_matrices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ static std::vector<vector3df> getFrustrumVertex(const scene::SViewFrustum &frust
* \param pointsInside a vector of point in 3d space.
*/
core::matrix4 ShadowMatrices::getTightestFitOrthoProj(const core::matrix4 &transform,
const std::vector<vector3df> &pointsInside)
const std::vector<vector3df> &pointsInside,
core::vector3df& bounding_box_extent)
{
float xmin = std::numeric_limits<float>::infinity();
float xmax = -std::numeric_limits<float>::infinity();
Expand Down Expand Up @@ -178,6 +179,9 @@ core::matrix4 ShadowMatrices::getTightestFitOrthoProj(const core::matrix4 &trans
tmp_matrix.buildProjectionMatrixOrthoLH(left, right,
down, up,
zmin - 100, zmax);
bounding_box_extent.X = right - left;
bounding_box_extent.Y = down - up;
bounding_box_extent.Z = zmax - zmin + 100;
return tmp_matrix;
} // getTightestFitOrthoProj

Expand Down Expand Up @@ -275,7 +279,7 @@ void ShadowMatrices::computeMatrixesAndCameras(scene::ICameraSceneNode *const ca
memcpy(m_shadows_cam[i], tmp, 24 * sizeof(float));

std::vector<vector3df> vectors = getFrustrumVertex(*frustrum);
tmp_matrix = getTightestFitOrthoProj(sun_cam_view_matrix, vectors);
tmp_matrix = getTightestFitOrthoProj(sun_cam_view_matrix, vectors, m_frustum_box_extent[i]);


m_shadow_cam_nodes[i]->setProjectionMatrix(tmp_matrix, true);
Expand Down
9 changes: 8 additions & 1 deletion src/graphics/shadow_matrices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ class ShadowMatrices
float m_shadows_cam[4][24];
bool m_rsm_map_available;
float m_mat_ubo[16 * 9 + 2];
core::vector3df m_frustum_box_extent[4];

core::matrix4 getTightestFitOrthoProj(const core::matrix4 &transform,
const std::vector<core::vector3df> &pointsInside);
const std::vector<core::vector3df> &pointsInside,
core::vector3df& bounding_box_extent);
void renderWireFrameFrustrum(float *tmp, unsigned i);
public:

Expand All @@ -75,6 +77,11 @@ class ShadowMatrices
return m_shadow_cam_nodes;
} // getShadowCamNodes
// ------------------------------------------------------------------------
core::vector3df* getShadowFrustumBoxExtent()
{
return m_frustum_box_extent;
} // getShadowCamNodes
// ------------------------------------------------------------------------
scene::ICameraSceneNode* getSunCam() { return m_sun_cam; }
// ------------------------------------------------------------------------
std::vector<core::matrix4>& getSunOrthoMatrices()
Expand Down
13 changes: 13 additions & 0 deletions src/graphics/texture_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TextureShaderBase::BindFunction TextureShaderBase::m_all_bind_functions[] =
/* ST_TRILINEAR_ANISOTROPIC_FILTERED */ &TextureShaderBase::bindTextureTrilinearAnisotropic,
/* ST_TRILINEAR_CUBEMAP */ &TextureShaderBase::bindCubemapTrilinear,
/* ST_BILINEAR_FILTERED */ &TextureShaderBase::bindTextureBilinear,
/* ST_NEAREST_FILTERED_ARRAY2D */ &TextureShaderBase::bindTextureNearestArrayTexture,
/* ST_SHADOW_SAMPLER */ &TextureShaderBase::bindTextureShadow,
/* ST_TRILINEAR_CLAMPED_ARRAY2D */ &TextureShaderBase::bindTrilinearClampedArrayTexture,
/* ST_VOLUME_LINEAR_FILTERED */ &TextureShaderBase::bindTextureVolume,
Expand All @@ -42,6 +43,7 @@ GLuint TextureShaderBase::m_all_texture_types[] =
/* ST_TRILINEAR_ANISOTROPIC_FILTERED */ GL_TEXTURE_2D,
/* ST_TRILINEAR_CUBEMAP */ GL_TEXTURE_CUBE_MAP,
/* ST_BILINEAR_FILTERED */ GL_TEXTURE_2D ,
/* ST_NEAREST_FILTERED_ARRAY2D */ GL_TEXTURE_2D_ARRAY,
/* ST_SHADOW_SAMPLER */ GL_TEXTURE_2D_ARRAY,
/* ST_TRILINEAR_CLAMPED_ARRAY2D */ GL_TEXTURE_2D_ARRAY,
/* ST_VOLUME_LINEAR_FILTERED */ GL_TEXTURE_3D,
Expand Down Expand Up @@ -168,6 +170,17 @@ void TextureShaderBase::bindTextureSemiTrilinear(GLuint tex_unit, GLuint tex_id)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.);
} // bindTextureSemiTrilinear

// ----------------------------------------------------------------------------
void TextureShaderBase::bindTextureNearestArrayTexture(GLuint tex_unit, GLuint tex_id)
{
glActiveTexture(GL_TEXTURE0 + tex_unit);
glBindTexture(GL_TEXTURE_2D_ARRAY, tex_id);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
} // bindTextureShadow

// ----------------------------------------------------------------------------
void TextureShaderBase::bindTextureShadow(GLuint tex_unit, GLuint tex_id)
{
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/texture_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum SamplerTypeNew
ST_TRILINEAR_ANISOTROPIC_FILTERED,
ST_TRILINEAR_CUBEMAP,
ST_BILINEAR_FILTERED,
ST_NEAREST_FILTERED_ARRAY2D,
ST_SHADOW_SAMPLER,
ST_TRILINEAR_CLAMPED_ARRAY2D,
ST_VOLUME_LINEAR_FILTERED,
Expand Down Expand Up @@ -70,6 +71,7 @@ class TextureShaderBase
static void bindTextureTrilinearAnisotropic(GLuint tex_unit, GLuint tex_id);
static void bindTextureSemiTrilinear(GLuint tex_unit, GLuint tex_id);
static void bindCubemapTrilinear(GLuint tex_unit, GLuint tex_id);
static void bindTextureNearestArrayTexture(GLuint tex_unit, GLuint tex_id);
static void bindTextureShadow(GLuint tex_unit, GLuint tex_id);
static void bindTrilinearClampedArrayTexture(GLuint tex_unit, GLuint tex_id);
static void bindTextureVolume(GLuint tex_unit, GLuint tex_id);
Expand Down
Loading

0 comments on commit d64b156

Please sign in to comment.