Showing the changes to libraries/render-utils/src/DeferredLightingEffect.cpp (1 of 5 files). Show all
Unified · Split
| @@ -50,6 +50,26 @@ · 49 unmodified lines | ||
| 50 | 50 | |
| 51 | 51 | static const std::string glowIntensityShaderHandle = "glowIntensity"; |
| 52 | 52 | |
| 53 | +struct LightLocations { | |
| 54 | + int shadowDistances; | |
| 55 | + int shadowScale; | |
| 56 | + int nearLocation; | |
| 57 | + int depthScale; | |
| 58 | + int depthTexCoordOffset; | |
| 59 | + int depthTexCoordScale; | |
| 60 | + int radius; | |
| 61 | + int ambientSphere; | |
| 62 | + int lightBufferUnit; | |
| 63 | + int atmosphereBufferUnit; | |
| 64 | + int invViewMat; | |
| 65 | + int texcoordMat; | |
| 66 | + int coneParam; | |
| 67 | + int stereo; | |
| 68 | +}; | |
| 69 | + | |
| 70 | +static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocationsPtr& locations); | |
| 71 | + | |
| 72 | + | |
| 53 | 73 | gpu::PipelinePointer DeferredLightingEffect::getPipeline(SimpleProgramKey config) { |
| 54 | 74 | auto it = _simplePrograms.find(config); |
| 55 | 75 | if (it != _simplePrograms.end()) { |
| @@ -91,6 +111,18 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { · 35 unmodified lines | ||
| 91 | 111 | gpu::Shader::makeProgram(*_emissiveShader, slotBindings); |
| 92 | 112 | |
| 93 | 113 | _viewState = viewState; |
| 114 | + _directionalLightLocations = std::make_shared<LightLocations>(); | |
| 115 | + _directionalLightShadowMapLocations = std::make_shared<LightLocations>(); | |
| 116 | + _directionalLightCascadedShadowMapLocations = std::make_shared<LightLocations>(); | |
| 117 | + _directionalAmbientSphereLightLocations = std::make_shared<LightLocations>(); | |
| 118 | + _directionalAmbientSphereLightShadowMapLocations = std::make_shared<LightLocations>(); | |
| 119 | + _directionalAmbientSphereLightCascadedShadowMapLocations = std::make_shared<LightLocations>(); | |
| 120 | + _directionalSkyboxLightLocations = std::make_shared<LightLocations>(); | |
| 121 | + _directionalSkyboxLightShadowMapLocations = std::make_shared<LightLocations>(); | |
| 122 | + _directionalSkyboxLightCascadedShadowMapLocations = std::make_shared<LightLocations>(); | |
| 123 | + _pointLightLocations = std::make_shared<LightLocations>(); | |
| 124 | + _spotLightLocations = std::make_shared<LightLocations>(); | |
| 125 | + | |
| 94 | 126 | loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations); |
| 95 | 127 | loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap, |
| 96 | 128 | _directionalLightShadowMapLocations); |
| @@ -282,7 +314,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 185 unmodified lines | ||
| 282 | 314 | invViewMat = args->_viewFrustum->getView(); |
| 283 | 315 | |
| 284 | 316 | auto& program = _directionalLight; |
| 285 | − const LightLocations* locations = &_directionalLightLocations; | |
| 317 | + LightLocationsPtr locations = _directionalLightLocations; | |
| 286 | 318 | |
| 287 | 319 | // FIXME: Note: we've removed the menu items to enable shadows, so this will always be false for now. |
| 288 | 320 | // When we add back shadow support, this old approach may likely be removed and completely replaced |
| @@ -294,16 +326,16 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 5 unmodified lines | ||
| 294 | 326 | batch.setResourceTexture(4, framebufferCache->getShadowFramebuffer()->getDepthStencilBuffer()); |
| 295 | 327 | |
| 296 | 328 | program = _directionalLightShadowMap; |
| 297 | − locations = &_directionalLightShadowMapLocations; | |
| 329 | + locations = _directionalLightShadowMapLocations; | |
| 298 | 330 | if (cascadeShadowsEnabled) { |
| 299 | 331 | program = _directionalLightCascadedShadowMap; |
| 300 | − locations = &_directionalLightCascadedShadowMapLocations; | |
| 332 | + locations = _directionalLightCascadedShadowMapLocations; | |
| 301 | 333 | if (useSkyboxCubemap) { |
| 302 | 334 | program = _directionalSkyboxLightCascadedShadowMap; |
| 303 | − locations = &_directionalSkyboxLightCascadedShadowMapLocations; | |
| 335 | + locations = _directionalSkyboxLightCascadedShadowMapLocations; | |
| 304 | 336 | } else if (_ambientLightMode > -1) { |
| 305 | 337 | program = _directionalAmbientSphereLightCascadedShadowMap; |
| 306 | − locations = &_directionalAmbientSphereLightCascadedShadowMapLocations; | |
| 338 | + locations = _directionalAmbientSphereLightCascadedShadowMapLocations; | |
| 307 | 339 | } |
| 308 | 340 | batch.setPipeline(program); |
| 309 | 341 | batch._glUniform3fv(locations->shadowDistances, 1, (const float*) &_viewState->getShadowDistances()); |
| @@ -311,10 +343,10 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 1 unmodified line | ||
| 311 | 343 | } else { |
| 312 | 344 | if (useSkyboxCubemap) { |
| 313 | 345 | program = _directionalSkyboxLightShadowMap; |
| 314 | − locations = &_directionalSkyboxLightShadowMapLocations; | |
| 346 | + locations = _directionalSkyboxLightShadowMapLocations; | |
| 315 | 347 | } else if (_ambientLightMode > -1) { |
| 316 | 348 | program = _directionalAmbientSphereLightShadowMap; |
| 317 | − locations = &_directionalAmbientSphereLightShadowMapLocations; | |
| 349 | + locations = _directionalAmbientSphereLightShadowMapLocations; | |
| 318 | 350 | } |
| 319 | 351 | batch.setPipeline(program); |
| 320 | 352 | } |
| @@ -323,10 +355,10 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 2 unmodified lines | ||
| 323 | 355 | } else { |
| 324 | 356 | if (useSkyboxCubemap) { |
| 325 | 357 | program = _directionalSkyboxLight; |
| 326 | − locations = &_directionalSkyboxLightLocations; | |
| 358 | + locations = _directionalSkyboxLightLocations; | |
| 327 | 359 | } else if (_ambientLightMode > -1) { |
| 328 | 360 | program = _directionalAmbientSphereLight; |
| 329 | − locations = &_directionalAmbientSphereLightLocations; | |
| 361 | + locations = _directionalAmbientSphereLightLocations; | |
| 330 | 362 | } |
| 331 | 363 | batch.setPipeline(program); |
| 332 | 364 | } |
| @@ -374,7 +406,9 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 41 unmodified lines | ||
| 374 | 406 | float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; |
| 375 | 407 | batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); |
| 376 | 408 | batch._glUniform2f(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); |
| 377 | − | |
| 409 | + bool stereo = args->_context->isStereo(); | |
| 410 | + batch._glUniform1f(locations->stereo, stereo ? 1 : 0); | |
| 411 | + | |
| 378 | 412 | { |
| 379 | 413 | Transform model; |
| 380 | 414 | model.setTranslation(glm::vec3(sMin, tMin, 0.0)); |
| @@ -427,20 +461,20 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 46 unmodified lines | ||
| 427 | 461 | |
| 428 | 462 | if (!_pointLights.empty()) { |
| 429 | 463 | batch.setPipeline(_pointLight); |
| 430 | − batch._glUniform1f(_pointLightLocations.nearLocation, nearVal); | |
| 431 | − batch._glUniform1f(_pointLightLocations.depthScale, depthScale); | |
| 432 | − batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | |
| 433 | − batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | |
| 464 | + batch._glUniform1f(_pointLightLocations->nearLocation, nearVal); | |
| 465 | + batch._glUniform1f(_pointLightLocations->depthScale, depthScale); | |
| 466 | + batch._glUniform2f(_pointLightLocations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | |
| 467 | + batch._glUniform2f(_pointLightLocations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | |
| 434 | 468 | |
| 435 | − batch._glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const float* >(&invViewMat)); | |
| 469 | + batch._glUniformMatrix4fv(_pointLightLocations->invViewMat, 1, false, reinterpret_cast< const float* >(&invViewMat)); | |
| 436 | 470 | |
| 437 | − batch._glUniformMatrix4fv(_pointLightLocations.texcoordMat, 1, false, reinterpret_cast< const float* >(&texcoordMat)); | |
| 471 | + batch._glUniformMatrix4fv(_pointLightLocations->texcoordMat, 1, false, reinterpret_cast< const float* >(&texcoordMat)); | |
| 438 | 472 | |
| 439 | 473 | for (auto lightID : _pointLights) { |
| 440 | 474 | auto& light = _allocatedLights[lightID]; |
| 441 | 475 | // IN DEBUG: light->setShowContour(true); |
| 442 | − if (_pointLightLocations.lightBufferUnit >= 0) { | |
| 443 | − batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); | |
| 476 | + if (_pointLightLocations->lightBufferUnit >= 0) { | |
| 477 | + batch.setUniformBuffer(_pointLightLocations->lightBufferUnit, light->getSchemaBuffer()); | |
| 444 | 478 | } |
| 445 | 479 | |
| 446 | 480 | float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); |
| @@ -475,20 +509,20 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 28 unmodified lines | ||
| 475 | 509 | |
| 476 | 510 | if (!_spotLights.empty()) { |
| 477 | 511 | batch.setPipeline(_spotLight); |
| 478 | − batch._glUniform1f(_spotLightLocations.nearLocation, nearVal); | |
| 479 | − batch._glUniform1f(_spotLightLocations.depthScale, depthScale); | |
| 480 | − batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | |
| 481 | − batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | |
| 512 | + batch._glUniform1f(_spotLightLocations->nearLocation, nearVal); | |
| 513 | + batch._glUniform1f(_spotLightLocations->depthScale, depthScale); | |
| 514 | + batch._glUniform2f(_spotLightLocations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | |
| 515 | + batch._glUniform2f(_spotLightLocations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | |
| 482 | 516 | |
| 483 | − batch._glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const float* >(&invViewMat)); | |
| 517 | + batch._glUniformMatrix4fv(_spotLightLocations->invViewMat, 1, false, reinterpret_cast< const float* >(&invViewMat)); | |
| 484 | 518 | |
| 485 | − batch._glUniformMatrix4fv(_spotLightLocations.texcoordMat, 1, false, reinterpret_cast< const float* >(&texcoordMat)); | |
| 519 | + batch._glUniformMatrix4fv(_spotLightLocations->texcoordMat, 1, false, reinterpret_cast< const float* >(&texcoordMat)); | |
| 486 | 520 | |
| 487 | 521 | for (auto lightID : _spotLights) { |
| 488 | 522 | auto light = _allocatedLights[lightID]; |
| 489 | 523 | // IN DEBUG: light->setShowContour(true); |
| 490 | 524 | |
| 491 | − batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); | |
| 525 | + batch.setUniformBuffer(_spotLightLocations->lightBufferUnit, light->getSchemaBuffer()); | |
| 492 | 526 | |
| 493 | 527 | auto eyeLightPos = eyePoint - light->getPosition(); |
| 494 | 528 | auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection()); |
| @@ -502,7 +536,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 7 unmodified lines | ||
| 502 | 536 | if ((eyeHalfPlaneDistance > -nearRadius) && |
| 503 | 537 | (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius)) { |
| 504 | 538 | coneParam.w = 0.0f; |
| 505 | − batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const float* >(&coneParam)); | |
| 539 | + batch._glUniform4fv(_spotLightLocations->coneParam, 1, reinterpret_cast< const float* >(&coneParam)); | |
| 506 | 540 | |
| 507 | 541 | Transform model; |
| 508 | 542 | model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); |
| @@ -522,7 +556,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 13 unmodified lines | ||
| 522 | 556 | batch.setViewTransform(viewMat); |
| 523 | 557 | } else { |
| 524 | 558 | coneParam.w = 1.0f; |
| 525 | − batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const float* >(&coneParam)); | |
| 559 | + batch._glUniform4fv(_spotLightLocations->coneParam, 1, reinterpret_cast< const float* >(&coneParam)); | |
| 526 | 560 | |
| 527 | 561 | Transform model; |
| 528 | 562 | model.setTranslation(light->getPosition()); |
| @@ -596,7 +630,7 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU · 67 unmodified lines | ||
| 596 | 630 | args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); |
| 597 | 631 | } |
| 598 | 632 | |
| 599 | −void DeferredLightingEffect::loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) { | |
| 633 | +static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocationsPtr& locations) { | |
| 600 | 634 | auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(vertSource))); |
| 601 | 635 | auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource))); |
| 602 | 636 | |
| @@ -616,20 +650,21 @@ void DeferredLightingEffect::loadLightProgram(const char* vertSource, const char · 13 unmodified lines | ||
| 616 | 650 | |
| 617 | 651 | gpu::Shader::makeProgram(*program, slotBindings); |
| 618 | 652 | |
| 619 | − locations.shadowDistances = program->getUniforms().findLocation("shadowDistances"); | |
| 620 | − locations.shadowScale = program->getUniforms().findLocation("shadowScale"); | |
| 621 | − locations.nearLocation = program->getUniforms().findLocation("near"); | |
| 622 | − locations.depthScale = program->getUniforms().findLocation("depthScale"); | |
| 623 | − locations.depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset"); | |
| 624 | − locations.depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale"); | |
| 625 | − locations.radius = program->getUniforms().findLocation("radius"); | |
| 626 | − locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); | |
| 627 | − locations.invViewMat = program->getUniforms().findLocation("invViewMat"); | |
| 628 | − locations.texcoordMat = program->getUniforms().findLocation("texcoordMat"); | |
| 629 | − locations.coneParam = program->getUniforms().findLocation("coneParam"); | |
| 630 | − | |
| 631 | − locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); | |
| 632 | − locations.atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit"); | |
| 653 | + locations->stereo = program->getUniforms().findLocation("stereoMode"); | |
| 654 | + locations->shadowDistances = program->getUniforms().findLocation("shadowDistances"); | |
| 655 | + locations->shadowScale = program->getUniforms().findLocation("shadowScale"); | |
| 656 | + locations->nearLocation = program->getUniforms().findLocation("near"); | |
| 657 | + locations->depthScale = program->getUniforms().findLocation("depthScale"); | |
| 658 | + locations->depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset"); | |
| 659 | + locations->depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale"); | |
| 660 | + locations->radius = program->getUniforms().findLocation("radius"); | |
| 661 | + locations->ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); | |
| 662 | + locations->invViewMat = program->getUniforms().findLocation("invViewMat"); | |
| 663 | + locations->texcoordMat = program->getUniforms().findLocation("texcoordMat"); | |
| 664 | + locations->coneParam = program->getUniforms().findLocation("coneParam"); | |
| 665 | + | |
| 666 | + locations->lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); | |
| 667 | + locations->atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit"); | |
| 633 | 668 | |
| 634 | 669 | auto state = std::make_shared<gpu::State>(); |
| 635 | 670 | if (lightVolume) { |