Skip to content

Commit 0d09278

Browse files
authored
Unify shadow path on sampler2DShadow + hardware PCF (#2573)
The ES version was not working on some drivers when we don't set ``` glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); ``` , unify the two paths.
1 parent 3ddc36f commit 0d09278

4 files changed

Lines changed: 7 additions & 49 deletions

File tree

src/celengine/rendcontext.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "lightenv.h"
2222
#include "rendcontext.h"
2323
#include "render.h"
24-
#include "shadowmap.h" // GL_ONLY_SHADOWS definition
2524
#include "texmanager.h"
2625
#include "texture.h"
2726

@@ -410,9 +409,7 @@ GLSL_RenderContext::makeCurrent(const cmod::Material& m)
410409
{
411410
glActiveTexture(GL_TEXTURE0 + nTextures);
412411
glBindTexture(GL_TEXTURE_2D, shadowMap);
413-
#if GL_ONLY_SHADOWS
414-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
415-
#endif
412+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
416413
Eigen::Matrix4f shadowBias(Eigen::Matrix4f::Zero());
417414
shadowBias.diagonal() = Eigen::Vector4f(0.5f, 0.5f, 0.5f, 1.0f);
418415
shadowBias.col(3) = Eigen::Vector4f(0.5f, 0.5f, 0.5f, 1.0f);

src/celengine/renderglsl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "renderglsl.h"
3939
#include "renderinfo.h"
4040
#include "shadermanager.h"
41-
#include "shadowmap.h" // GL_ONLY_SHADOWS definition
4241
#include "texture.h"
4342

4443
using namespace celestia;
@@ -413,9 +412,7 @@ void renderGeometry_GLSL(RenderGeometry* geometry,
413412
glActiveTexture(GL_TEXTURE0);
414413
glEnable(GL_TEXTURE_2D);
415414
glBindTexture(GL_TEXTURE_2D, shadowBuffer->depthTexture());
416-
#if GL_ONLY_SHADOWS
417415
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
418-
#endif
419416

420417
glBegin(GL_QUADS);
421418
float side = 300.0f;

src/celengine/shadermanager.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ namespace util = celestia::util;
4646

4747
namespace
4848
{
49-
#if GL_ONLY_SHADOWS
5049
constexpr const int ShadowSampleKernelWidth = 2;
51-
#endif
5250

5351
const std::filesystem::path ShaderDirectory{ "shaders" };
5452

@@ -116,7 +114,7 @@ void main(void)
116114
#ifdef GL_ES
117115
constexpr std::string_view VersionHeader = "#version 300 es\n"sv;
118116
constexpr std::string_view VersionHeaderGeom = "#version 320 es\n"sv;
119-
constexpr std::string_view CommonHeader = "precision highp float;\n"sv;
117+
constexpr std::string_view CommonHeader = "precision highp float;\nprecision highp sampler2DShadow;\n"sv;
120118
#else
121119
constexpr std::string_view VersionHeader = "#version 330\n"sv;
122120
constexpr std::string_view VersionHeaderGeom = "#version 330\n"sv;
@@ -924,11 +922,7 @@ TextureSamplerDeclarations(const ShaderProperties& props)
924922

925923
if (util::is_set(props.texUsage, TexUsage::ShadowMapTexture))
926924
{
927-
#if GL_ONLY_SHADOWS
928925
source += DeclareUniform("shadowMapTex0", Shader_Sampler2DShadow);
929-
#else
930-
source += DeclareUniform("shadowMapTex0", Shader_Sampler2D);
931-
#endif
932926
}
933927

934928
return source;
@@ -1037,42 +1031,24 @@ std::string
10371031
CalculateShadow()
10381032
{
10391033
std::string source;
1040-
#if GL_ONLY_SHADOWS
10411034
source += R"glsl(
10421035
float calculateShadow()
10431036
{
10441037
float texelSize = 1.0 / shadowMapSize;
10451038
float s = 0.0;
10461039
float bias = max(0.005 * (1.0 - cosNormalLightDir), 0.0005);
10471040
)glsl"sv;
1048-
float boxFilterWidth = (float) ShadowSampleKernelWidth - 1.0f;
1041+
float boxFilterWidth = static_cast<float>(ShadowSampleKernelWidth) - 1.0f;
10491042
float firstSample = -boxFilterWidth / 2.0f;
10501043
float lastSample = firstSample + boxFilterWidth;
1051-
float sampleWeight = 1.0f / (float) (ShadowSampleKernelWidth * ShadowSampleKernelWidth);
1044+
float sampleWeight = 1.0f / static_cast<float>(ShadowSampleKernelWidth * ShadowSampleKernelWidth);
10521045
source += fmt::format(" for (float y = {:f}; y <= {:f}; y += 1.0)\n", firstSample, lastSample);
10531046
source += fmt::format(" for (float x = {:f}; x <= {:f}; x += 1.0)\n", firstSample, lastSample);
1054-
source += " s += shadow2D(shadowMapTex0, shadowTexCoord0.xyz + vec3(x * texelSize, y * texelSize, bias)).z;\n";
1047+
// Modern GLSL hardware compare: returns single float (PCF result when
1048+
// GL_LINEAR filter + GL_COMPARE_REF_TO_TEXTURE).
1049+
source += " s += texture(shadowMapTex0, shadowTexCoord0.xyz + vec3(x * texelSize, y * texelSize, bias));\n";
10551050
source += fmt::format(" return s * {:f};\n", sampleWeight);
10561051
source += "}\n";
1057-
#else
1058-
source += R"glsl(
1059-
float calculateShadow()
1060-
{
1061-
float texelSize = 1.0 / shadowMapSize;
1062-
float s = 0.0;
1063-
float bias = max(0.005 * (1.0 - cosNormalLightDir), 0.0005);
1064-
for(float x = -1.0; x <= 1.0; x += 1.0)
1065-
{
1066-
for(float y = -1.0; y <= 1.0; y += 1.0)
1067-
{
1068-
float pcfDepth = texture(shadowMapTex0, shadowTexCoord0.xy + vec2(x * texelSize, y * texelSize)).r;
1069-
s += shadowTexCoord0.z - bias > pcfDepth ? 1.0 : 0.0;
1070-
}
1071-
}
1072-
return 1.0 - s / 9.0;
1073-
}
1074-
)glsl"sv;
1075-
#endif
10761052
return source;
10771053
}
10781054

src/celengine/shadowmap.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)