Skip to content

Commit

Permalink
piedraw.cpp: Support configurable shadow cascades count
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Nov 2, 2023
1 parent 4721812 commit 3da3648
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/ivis_opengl/piedef.h
Expand Up @@ -71,6 +71,9 @@ enum class ShadowMode
};
bool pie_setShadowMode(ShadowMode mode);
ShadowMode pie_getShadowMode();
bool pie_setShadowCascades(uint32_t newValue);
uint32_t pie_getShadowCascades();

optional<bool> pie_supportsShadowMapping();
bool pie_setShadowMapResolution(uint32_t resolution);
uint32_t pie_getShadowMapResolution();
Expand Down
31 changes: 28 additions & 3 deletions lib/ivis_opengl/piedraw.cpp
Expand Up @@ -68,6 +68,7 @@ static size_t drawCallsCount = 0;
static bool shadows = false;
static bool shadowsHasBeenInit = false;
static ShadowMode shadowMode = ShadowMode::Shadow_Mapping;
static uint32_t numShadowCascades = WZ_MAX_SHADOW_CASCADES;
static gfx_api::gfxFloat lighting0[LIGHT_MAX][4];
static gfx_api::gfxFloat lightingDefault[LIGHT_MAX][4];

Expand Down Expand Up @@ -114,19 +115,23 @@ static void refreshShadowShaders()
{
auto shadowConstants = gfx_api::context::get().getShadowConstants();
bool bShadowMappingEnabled = isShadowMappingEnabled();
uint32_t actualNumCascadesAndPasses = (bShadowMappingEnabled) ? numShadowCascades : 0;
if (bShadowMappingEnabled)
{
shadowConstants.shadowMode = 1; // Possible future TODO: Could get this from the config file to allow testing alternative filter methods
shadowConstants.shadowCascadesCount = actualNumCascadesAndPasses;
}
else
{
shadowConstants.shadowMode = 0; // Disable shader-drawn / shadow-mapping shadows
}
// Preserve existing shadowFilterSize and shadowCascadesCount

// Preserve existing shadowFilterSize

gfx_api::context::get().setShadowConstants(shadowConstants);

// Trigger depth pass buffer free / rebuild if needed
gfx_api::context::get().setDepthPassProperties((bShadowMappingEnabled) ? WZ_MAX_SHADOW_CASCADES : 0, gfx_api::context::get().getDepthPassDimensions(0));
gfx_api::context::get().setDepthPassProperties(actualNumCascadesAndPasses, gfx_api::context::get().getDepthPassDimensions(0));
}
}

Expand All @@ -152,7 +157,7 @@ bool pie_setShadowMapResolution(uint32_t resolution)
{
ASSERT_OR_RETURN(false, resolution && !(resolution & (resolution - 1)), "Expecting power-of-2 resolution, received: %" PRIu32, resolution);
bool bShadowMappingEnabled = isShadowMappingEnabled();
return gfx_api::context::get().setDepthPassProperties((bShadowMappingEnabled) ? WZ_MAX_SHADOW_CASCADES : 0, resolution);
return gfx_api::context::get().setDepthPassProperties((bShadowMappingEnabled) ? numShadowCascades : 0, resolution);
}

uint32_t pie_getShadowMapResolution()
Expand Down Expand Up @@ -189,6 +194,26 @@ ShadowMode pie_getShadowMode()
return shadowMode;
}

bool pie_setShadowCascades(uint32_t newValue)
{
if (newValue == 0 || newValue > WZ_MAX_SHADOW_CASCADES)
{
return false;
}
if (numShadowCascades == newValue)
{
return true;
}
numShadowCascades = newValue;
refreshShadowShaders();
return true;
}

uint32_t pie_getShadowCascades()
{
return numShadowCascades;
}

static Vector3f currentSunPosition(0.f, 0.f, 0.f);

void pie_BeginLighting(const Vector3f &light)
Expand Down

0 comments on commit 3da3648

Please sign in to comment.