Make shadows work with one cascade

This commit is contained in:
Sam 2018-07-02 10:46:53 +02:00
parent 0cb8b4dfa1
commit c1856a5e36
7 changed files with 34 additions and 13 deletions

View file

@ -182,7 +182,7 @@ void GL41Texture::syncSampler() const {
glTexParameteri(_target, GL_TEXTURE_MAG_FILTER, fm.magFilter); glTexParameteri(_target, GL_TEXTURE_MAG_FILTER, fm.magFilter);
if (sampler.doComparison()) { if (sampler.doComparison()) {
glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE_ARB); glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(_target, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]); glTexParameteri(_target, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]);
} else { } else {
glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_NONE); glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
@ -197,7 +197,7 @@ void GL41Texture::syncSampler() const {
glTexParameterf(_target, GL_TEXTURE_MIN_LOD, (float)sampler.getMinMip()); glTexParameterf(_target, GL_TEXTURE_MIN_LOD, (float)sampler.getMinMip());
glTexParameterf(_target, GL_TEXTURE_MAX_LOD, (sampler.getMaxMip() == Sampler::MAX_MIP_LEVEL ? 1000.f : sampler.getMaxMip())); glTexParameterf(_target, GL_TEXTURE_MAX_LOD, (sampler.getMaxMip() == Sampler::MAX_MIP_LEVEL ? 1000.f : sampler.getMaxMip()));
glTexParameterf(_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, sampler.getMaxAnisotropy()); glTexParameterf(_target, GL_TEXTURE_MAX_ANISOTROPY, sampler.getMaxAnisotropy());
} }
using GL41FixedAllocationTexture = GL41Backend::GL41FixedAllocationTexture; using GL41FixedAllocationTexture = GL41Backend::GL41FixedAllocationTexture;

View file

@ -152,7 +152,7 @@ public:
glSamplerParameteri(result, GL_TEXTURE_MIN_FILTER, fm.minFilter); glSamplerParameteri(result, GL_TEXTURE_MIN_FILTER, fm.minFilter);
glSamplerParameteri(result, GL_TEXTURE_MAG_FILTER, fm.magFilter); glSamplerParameteri(result, GL_TEXTURE_MAG_FILTER, fm.magFilter);
if (sampler.doComparison()) { if (sampler.doComparison()) {
glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE_ARB); glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glSamplerParameteri(result, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]); glSamplerParameteri(result, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]);
} else { } else {
glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_NONE); glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_NONE);
@ -341,7 +341,7 @@ void GL45Texture::syncSampler() const {
glTextureParameteri(_id, GL_TEXTURE_MAG_FILTER, fm.magFilter); glTextureParameteri(_id, GL_TEXTURE_MAG_FILTER, fm.magFilter);
if (sampler.doComparison()) { if (sampler.doComparison()) {
glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE_ARB); glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTextureParameteri(_id, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]); glTextureParameteri(_id, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]);
} else { } else {
glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_NONE); glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_NONE);

View file

@ -61,7 +61,7 @@ Framebuffer* Framebuffer::createShadowmap(uint16 width) {
samplerDesc._borderColor = glm::vec4(1.0f); samplerDesc._borderColor = glm::vec4(1.0f);
samplerDesc._wrapModeU = Sampler::WRAP_BORDER; samplerDesc._wrapModeU = Sampler::WRAP_BORDER;
samplerDesc._wrapModeV = Sampler::WRAP_BORDER; samplerDesc._wrapModeV = Sampler::WRAP_BORDER;
samplerDesc._filter = Sampler::FILTER_MIN_MAG_LINEAR; samplerDesc._filter = Sampler::FILTER_MIN_MAG_POINT;
samplerDesc._comparisonFunc = LESS; samplerDesc._comparisonFunc = LESS;
depthTexture->setSampler(Sampler(samplerDesc)); depthTexture->setSampler(Sampler(samplerDesc));

View file

@ -227,7 +227,7 @@ void RenderShadowTask::build(JobModel& task, const render::Varying& input, rende
} }
const auto setupOutput = task.addJob<RenderShadowSetup>("ShadowSetup"); const auto setupOutput = task.addJob<RenderShadowSetup>("ShadowSetup");
const auto queryResolution = setupOutput.getN<RenderShadowSetup::Outputs>(2); const auto queryResolution = setupOutput.getN<RenderShadowSetup::Outputs>(1);
// Fetch and cull the items from the scene // Fetch and cull the items from the scene
static const auto shadowCasterReceiverFilter = ItemFilter::Builder::visibleWorldItems().withTypeShape().withOpaque().withoutLayered().withTagBits(tagBits, tagMask); static const auto shadowCasterReceiverFilter = ItemFilter::Builder::visibleWorldItems().withTypeShape().withOpaque().withoutLayered().withTagBits(tagBits, tagMask);
@ -248,10 +248,12 @@ void RenderShadowTask::build(JobModel& task, const render::Varying& input, rende
const auto sortedShapes = task.addJob<DepthSortShapes>("DepthSortShadow", sortedPipelines, true); const auto sortedShapes = task.addJob<DepthSortShapes>("DepthSortShadow", sortedPipelines, true);
render::Varying cascadeFrustums[SHADOW_CASCADE_MAX_COUNT] = { render::Varying cascadeFrustums[SHADOW_CASCADE_MAX_COUNT] = {
ViewFrustumPointer(), ViewFrustumPointer()
ViewFrustumPointer(), #if SHADOW_CASCADE_MAX_COUNT>1
,ViewFrustumPointer(),
ViewFrustumPointer(), ViewFrustumPointer(),
ViewFrustumPointer() ViewFrustumPointer()
#endif
}; };
for (auto i = 0; i < SHADOW_CASCADE_MAX_COUNT; i++) { for (auto i = 0; i < SHADOW_CASCADE_MAX_COUNT; i++) {
@ -293,13 +295,15 @@ RenderShadowSetup::RenderShadowSetup() :
void RenderShadowSetup::configure(const Config& configuration) { void RenderShadowSetup::configure(const Config& configuration) {
setConstantBias(0, configuration.constantBias0); setConstantBias(0, configuration.constantBias0);
setConstantBias(1, configuration.constantBias1);
setConstantBias(2, configuration.constantBias2);
setConstantBias(3, configuration.constantBias3);
setSlopeBias(0, configuration.slopeBias0); setSlopeBias(0, configuration.slopeBias0);
#if SHADOW_CASCADE_MAX_COUNT>1
setConstantBias(1, configuration.constantBias1);
setSlopeBias(1, configuration.slopeBias1); setSlopeBias(1, configuration.slopeBias1);
setConstantBias(2, configuration.constantBias2);
setSlopeBias(2, configuration.slopeBias2); setSlopeBias(2, configuration.slopeBias2);
setConstantBias(3, configuration.constantBias3);
setSlopeBias(3, configuration.slopeBias3); setSlopeBias(3, configuration.slopeBias3);
#endif
} }
void RenderShadowSetup::setConstantBias(int cascadeIndex, float value) { void RenderShadowSetup::setConstantBias(int cascadeIndex, float value) {

View file

@ -17,11 +17,19 @@
#define SHADOW_SCREEN_SPACE_DITHER 1 #define SHADOW_SCREEN_SPACE_DITHER 1
// the shadow texture // the shadow texture
#if SHADOW_CASCADE_MAX_COUNT>1
uniform sampler2DShadow shadowMaps[SHADOW_CASCADE_MAX_COUNT]; uniform sampler2DShadow shadowMaps[SHADOW_CASCADE_MAX_COUNT];
#else
uniform sampler2DShadow shadowMaps;
#endif
// Sample the shadowMap with PCF (built-in) // Sample the shadowMap with PCF (built-in)
float fetchShadow(int cascadeIndex, vec3 shadowTexcoord) { float fetchShadow(int cascadeIndex, vec3 shadowTexcoord) {
#if SHADOW_CASCADE_MAX_COUNT>1
return texture(shadowMaps[cascadeIndex], shadowTexcoord); return texture(shadowMaps[cascadeIndex], shadowTexcoord);
#else
return texture(shadowMaps, shadowTexcoord);
#endif
} }
vec2 PCFkernel[4] = vec2[4]( vec2 PCFkernel[4] = vec2[4](
@ -89,6 +97,7 @@ float evalShadowCascadeAttenuation(int cascadeIndex, ShadowSampleOffsets offsets
float evalShadowAttenuation(vec3 worldLightDir, vec4 worldPosition, float viewDepth, vec3 worldNormal) { float evalShadowAttenuation(vec3 worldLightDir, vec4 worldPosition, float viewDepth, vec3 worldNormal) {
ShadowSampleOffsets offsets = evalShadowFilterOffsets(worldPosition); ShadowSampleOffsets offsets = evalShadowFilterOffsets(worldPosition);
#if 0
vec4 cascadeShadowCoords[2]; vec4 cascadeShadowCoords[2];
cascadeShadowCoords[0] = vec4(0); cascadeShadowCoords[0] = vec4(0);
cascadeShadowCoords[1] = vec4(0); cascadeShadowCoords[1] = vec4(0);
@ -105,6 +114,11 @@ float evalShadowAttenuation(vec3 worldLightDir, vec4 worldPosition, float viewDe
float attenuation = mix(cascadeAttenuations.x, cascadeAttenuations.y, cascadeMix); float attenuation = mix(cascadeAttenuations.x, cascadeAttenuations.y, cascadeMix);
// Falloff to max distance // Falloff to max distance
return mix(1.0, attenuation, evalShadowFalloff(viewDepth)); return mix(1.0, attenuation, evalShadowFalloff(viewDepth));
#else
vec4 shadowTexcoord = evalShadowTexcoord(0, worldPosition);
float attenuation = fetchShadow(0, shadowTexcoord.xyz);
return attenuation;
#endif
} }
<@endif@> <@endif@>

View file

@ -5,7 +5,7 @@
# define MAT4 mat4 # define MAT4 mat4
#endif #endif
#define SHADOW_CASCADE_MAX_COUNT 4 #define SHADOW_CASCADE_MAX_COUNT 1
struct ShadowTransform { struct ShadowTransform {
MAT4 reprojection; MAT4 reprojection;

View file

@ -370,10 +370,13 @@ void CullShapeBounds::run(const RenderContextPointer& renderContext, const Input
const auto& inShapes = inputs.get0(); const auto& inShapes = inputs.get0();
const auto& cullFilter = inputs.get1(); const auto& cullFilter = inputs.get1();
const auto& boundsFilter = inputs.get2(); const auto& boundsFilter = inputs.get2();
const auto& antiFrustum = inputs.get3(); ViewFrustumPointer antiFrustum;
auto& outShapes = outputs.edit0(); auto& outShapes = outputs.edit0();
auto& outBounds = outputs.edit1(); auto& outBounds = outputs.edit1();
if (!inputs[3].isNull()) {
antiFrustum = inputs.get3();
}
outShapes.clear(); outShapes.clear();
outBounds = AABox(); outBounds = AABox();