Showing the changes to libraries/render-utils/src/RenderShadowTask.cpp (1 of 3 files). Show all
Unified · Split
| @@ -213,7 +213,7 @@ void RenderShadowTask::build(JobModel& task, const render::Varying& input, rende · 212 unmodified lines | ||
| 213 | 213 | initZPassPipelines(*shapePlumber, state); |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | − task.addJob<RenderShadowSetup>("ShadowSetup"); | |
| 216 | + const auto coarseFrustum = task.addJob<RenderShadowSetup>("ShadowSetup"); | |
| 217 | 217 | |
| 218 | 218 | for (auto i = 0; i < SHADOW_CASCADE_MAX_COUNT; i++) { |
| 219 | 219 | char jobName[64]; |
| @@ -243,7 +243,31 @@ void RenderShadowTask::configure(const Config& configuration) { · 23 unmodified lines | ||
| 243 | 243 | // Task::configure(configuration); |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | −void RenderShadowSetup::run(const render::RenderContextPointer& renderContext) { | |
| 246 | +RenderShadowSetup::RenderShadowSetup() : | |
| 247 | + _coarseShadowFrustum{ std::make_shared<ViewFrustum>() } { | |
| 248 | + | |
| 249 | +} | |
| 250 | + | |
| 251 | +void RenderShadowSetup::configure(const Config& configuration) { | |
| 252 | + setConstantBias(0, configuration.constantBias0); | |
| 253 | + setConstantBias(1, configuration.constantBias1); | |
| 254 | + setConstantBias(2, configuration.constantBias2); | |
| 255 | + setConstantBias(3, configuration.constantBias3); | |
| 256 | + setSlopeBias(0, configuration.slopeBias0); | |
| 257 | + setSlopeBias(1, configuration.slopeBias1); | |
| 258 | + setSlopeBias(2, configuration.slopeBias2); | |
| 259 | + setSlopeBias(3, configuration.slopeBias3); | |
| 260 | +} | |
| 261 | + | |
| 262 | +void RenderShadowSetup::setConstantBias(int cascadeIndex, float value) { | |
| 263 | + _bias[cascadeIndex]._constant = value * value * value * 0.004f; | |
| 264 | +} | |
| 265 | + | |
| 266 | +void RenderShadowSetup::setSlopeBias(int cascadeIndex, float value) { | |
| 267 | + _bias[cascadeIndex]._slope = value * value * value * 0.01f; | |
| 268 | +} | |
| 269 | + | |
| 270 | +void RenderShadowSetup::run(const render::RenderContextPointer& renderContext, Output& output) { | |
| 247 | 271 | auto lightStage = renderContext->_scene->getStage<LightStage>(); |
| 248 | 272 | assert(lightStage); |
| 249 | 273 | // Cache old render args |
| @@ -252,12 +276,46 @@ void RenderShadowSetup::run(const render::RenderContextPointer& renderContext) { · 2 unmodified lines | ||
| 252 | 276 | const auto globalShadow = lightStage->getCurrentKeyShadow(); |
| 253 | 277 | if (globalShadow) { |
| 254 | 278 | globalShadow->setKeylightFrustum(args->getViewFrustum(), SHADOW_FRUSTUM_NEAR, SHADOW_FRUSTUM_FAR); |
| 255 | − } | |
| 256 | −} | |
| 279 | + auto firstCascadeFrustum = globalShadow->getCascade(0).getFrustum(); | |
| 280 | + unsigned int cascadeIndex; | |
| 281 | + _coarseShadowFrustum->setPosition(firstCascadeFrustum->getPosition()); | |
| 282 | + _coarseShadowFrustum->setOrientation(firstCascadeFrustum->getOrientation()); | |
| 283 | + | |
| 284 | + // Adjust each cascade frustum | |
| 285 | + for (cascadeIndex = 0; cascadeIndex < globalShadow->getCascadeCount(); ++cascadeIndex) { | |
| 286 | + auto& bias = _bias[cascadeIndex]; | |
| 287 | + globalShadow->setKeylightCascadeFrustum(cascadeIndex, args->getViewFrustum(), | |
| 288 | + SHADOW_FRUSTUM_NEAR, SHADOW_FRUSTUM_FAR, | |
| 289 | + bias._constant, bias._slope); | |
| 290 | + } | |
| 291 | + // Now adjust coarse frustum bounds | |
| 292 | + auto left = glm::dot(firstCascadeFrustum->getFarTopLeft(), firstCascadeFrustum->getRight()); | |
| 293 | + auto right = glm::dot(firstCascadeFrustum->getFarTopRight(), firstCascadeFrustum->getRight()); | |
| 294 | + auto top = glm::dot(firstCascadeFrustum->getFarTopLeft(), firstCascadeFrustum->getUp()); | |
| 295 | + auto bottom = glm::dot(firstCascadeFrustum->getFarBottomRight(), firstCascadeFrustum->getUp()); | |
| 296 | + auto near = firstCascadeFrustum->getNearClip(); | |
| 297 | + auto far = firstCascadeFrustum->getFarClip(); | |
| 298 | + for (cascadeIndex = 1; cascadeIndex < globalShadow->getCascadeCount(); ++cascadeIndex) { | |
| 299 | + auto cascadeLeft = glm::dot(firstCascadeFrustum->getFarTopLeft(), firstCascadeFrustum->getRight()); | |
| 300 | + auto cascadeRight = glm::dot(firstCascadeFrustum->getFarTopRight(), firstCascadeFrustum->getRight()); | |
| 301 | + auto cascadeTop = glm::dot(firstCascadeFrustum->getFarTopLeft(), firstCascadeFrustum->getUp()); | |
| 302 | + auto cascadeBottom = glm::dot(firstCascadeFrustum->getFarBottomRight(), firstCascadeFrustum->getUp()); | |
| 303 | + auto cascadeNear = firstCascadeFrustum->getNearClip(); | |
| 304 | + auto cascadeFar = firstCascadeFrustum->getFarClip(); | |
| 305 | + left = glm::min(left, cascadeLeft); | |
| 306 | + right = glm::max(right, cascadeRight); | |
| 307 | + bottom = glm::min(bottom, cascadeBottom); | |
| 308 | + top = glm::max(top, cascadeTop); | |
| 309 | + near = glm::min(near, cascadeNear); | |
| 310 | + far = glm::max(far, cascadeFar); | |
| 311 | + } | |
| 312 | + _coarseShadowFrustum->setProjection(glm::ortho<float>(left, right, bottom, top, near, far)); | |
| 313 | + _coarseShadowFrustum->calculate(); | |
| 257 | 314 | |
| 258 | −void RenderShadowCascadeSetup::configure(const Config& configuration) { | |
| 259 | − _fixedBias = configuration.fixedBias * configuration.fixedBias * configuration.fixedBias * 0.004f; | |
| 260 | − _slopeBias = configuration.slopeBias * configuration.slopeBias * configuration.slopeBias * 0.01f; | |
| 315 | + output = _coarseShadowFrustum; | |
| 316 | + } else { | |
| 317 | + output = nullptr; | |
| 318 | + } | |
| 261 | 319 | } |
| 262 | 320 | |
| 263 | 321 | void RenderShadowCascadeSetup::run(const render::RenderContextPointer& renderContext, Outputs& output) { |
| @@ -273,9 +331,6 @@ void RenderShadowCascadeSetup::run(const render::RenderContextPointer& renderCon · 9 unmodified lines | ||
| 273 | 331 | if (globalShadow && _cascadeIndex<globalShadow->getCascadeCount()) { |
| 274 | 332 | output.edit1() = ItemFilter::Builder::visibleWorldItems().withTypeShape().withOpaque().withoutLayered(); |
| 275 | 333 | |
| 276 | − globalShadow->setKeylightCascadeFrustum(_cascadeIndex, args->getViewFrustum(), SHADOW_FRUSTUM_NEAR, SHADOW_FRUSTUM_FAR, | |
| 277 | − _fixedBias, _slopeBias); | |
| 278 | − | |
| 279 | 334 | // Set the keylight render args |
| 280 | 335 | args->pushViewFrustum(*(globalShadow->getCascade(_cascadeIndex).getFrustum())); |
| 281 | 336 | args->_renderMode = RenderArgs::SHADOW_RENDER_MODE; |