Number of shadow samples are now a compiling constant.

This commit is contained in:
Jonathas Costa
2020-01-30 15:10:15 -05:00
parent 199ddb6292
commit b6b1817160
4 changed files with 46 additions and 30 deletions

View File

@@ -25,6 +25,8 @@
#include "PowerScaling/powerScaling_fs.hglsl"
#include "fragment.glsl"
#define NSSamples #{nShadowSamples}
in vec2 vs_st;
in float vs_screenSpaceDepth;
in vec4 shadowCoords;
@@ -36,7 +38,7 @@ uniform float transparency;
uniform vec3 sunPosition;
uniform float _nightFactor;
uniform int nShadowSamples;
//uniform int nShadowSamples;
uniform float zFightingPercentage;
// temp
@@ -83,18 +85,18 @@ Fragment getFragment() {
normalizedShadowCoords.w = 1.0;
float sum = 0;
for (int i = 0; i < nShadowSamples; ++i) {
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-nShadowSamples + i, -nShadowSamples + i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-nShadowSamples + i, 0));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-nShadowSamples + i, nShadowSamples - i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( 0 , -nShadowSamples + i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( 0 , nShadowSamples - i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( nShadowSamples - i, -nShadowSamples + i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( nShadowSamples - i, 0));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( nShadowSamples - i, nShadowSamples - i));
for (int i = 0; i < NSSamples; ++i) {
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-NSSamples + i, -NSSamples + i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-NSSamples + i, 0));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-NSSamples + i, NSSamples - i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( 0 , -NSSamples + i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( 0 , NSSamples - i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( NSSamples - i, -NSSamples + i));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( NSSamples - i, 0));
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( NSSamples - i, NSSamples - i));
}
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(0, 0));
shadow = sum / (8.0 * nShadowSamples + 1.f);
shadow = sum / (8.0 * NSSamples + 1.f);
}
// The normal for the one plane depends on whether we are dealing

View File

@@ -49,10 +49,12 @@
#include <locale>
namespace {
constexpr const std::array<const char*, 10> UniformNames = {
constexpr const char* _loggerCat = "RingsComponent";
constexpr const std::array<const char*, 9> UniformNames = {
"modelViewProjectionMatrix", "textureOffset", "transparency", "_nightFactor",
"sunPosition", "ringTexture", "shadowMatrix", "shadowMapTexture",
"nShadowSamples", "zFightingPercentage"
"zFightingPercentage"
};
constexpr const std::array<const char*, 3> GeomUniformNames = {
@@ -243,6 +245,7 @@ void RingsComponent::initialize() {
if (_ringsDictionary.hasKey(NumberShadowSamplesInfo.identifier)) {
_nShadowSamples = _ringsDictionary.value<int>(NumberShadowSamplesInfo.identifier);
}
_nShadowSamples.onChange([this]() { compileShadowShader(); });
addProperty(_nShadowSamples);
addProperty(_transparency);
@@ -253,11 +256,7 @@ bool RingsComponent::isReady() const {
}
void RingsComponent::initializeGL() {
_shader = global::renderEngine.buildRenderProgram(
"RingsProgram",
absPath("${MODULE_GLOBEBROWSING}/shaders/rings_vs.glsl"),
absPath("${MODULE_GLOBEBROWSING}/shaders/rings_fs.glsl")
);
compileShadowShader();
_geometryOnlyShader = global::renderEngine.buildRenderProgram(
"RingsGeomOnlyProgram",
@@ -326,7 +325,7 @@ void RingsComponent::draw(const RenderData& data,
_shader->setUniform(_uniformCache.transparency, _transparency);
_shader->setUniform(_uniformCache.nightFactor, _nightFactor);
_shader->setUniform(_uniformCache.sunPosition, _sunPosition);
_shader->setUniform(_uniformCache.nShadowSamples, _nShadowSamples);
//_shader->setUniform(_uniformCache.nShadowSamples, _nShadowSamples);
_shader->setUniform(_uniformCache.zFightingPercentage, _zFightingPercentage);
ringTextureUnit.activate();
@@ -377,8 +376,8 @@ void RingsComponent::draw(const RenderData& data,
void RingsComponent::update(const UpdateData& data) {
if (_shader->isDirty()) {
_shader->rebuildFromFile();
ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames);
//_shader->rebuildFromFile();
compileShadowShader();
}
if (_geometryOnlyShader->isDirty()) {
@@ -474,6 +473,24 @@ void RingsComponent::createPlane() {
);
}
void RingsComponent::compileShadowShader() {
ghoul::Dictionary dict;
dict.setValue("nShadowSamples", std::to_string(_nShadowSamples.value()));
try {
_shader = global::renderEngine.buildRenderProgram(
"RingsProgram",
absPath("${MODULE_GLOBEBROWSING}/shaders/rings_vs.glsl"),
absPath("${MODULE_GLOBEBROWSING}/shaders/rings_fs.glsl"),
dict
);
}
catch (const ghoul::RuntimeError& e) {
LERROR(e.message);
}
ghoul::opengl::updateUniformLocations(*_shader, _uniformCache, UniformNames);
}
bool RingsComponent::isEnabled() const {
return _enabled;
}

View File

@@ -77,6 +77,7 @@ public:
private:
void loadTexture();
void createPlane();
void compileShadowShader();
properties::StringProperty _texturePath;
properties::FloatProperty _size;
@@ -90,8 +91,7 @@ private:
std::unique_ptr<ghoul::opengl::ProgramObject> _shader;
std::unique_ptr<ghoul::opengl::ProgramObject> _geometryOnlyShader;
UniformCache(modelViewProjectionMatrix, textureOffset, transparency, nightFactor,
sunPosition, ringTexture, shadowMatrix, shadowMapTexture, nShadowSamples,
zFightingPercentage
sunPosition, ringTexture, shadowMatrix, shadowMapTexture, zFightingPercentage
) _uniformCache;
UniformCache(modelViewProjectionMatrix, textureOffset, ringTexture)
_geomUniformCache;

View File

@@ -108,13 +108,10 @@ private:
int _shadowDepthTextureWidth = 4096;
bool _dynamicDepthTextureRes = true;
// All of these initializations should probably be 0 since they are GLuints?
GLuint _shadowDepthTexture = -1;
GLuint _positionInLightSpaceTexture = -1;
GLuint _shadowFBO = -1;
GLuint _firstPassSubroutine = -1;
GLuint _secondPassSubroutine = 1;
GLint _defaultFBO = -1;
GLuint _shadowDepthTexture = 0;
GLuint _positionInLightSpaceTexture = 0;
GLuint _shadowFBO = 0;
GLint _defaultFBO = 0;
GLint _mViewport[4];
GLboolean _faceCulling;