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 | static const std::string glowIntensityShaderHandle = "glowIntensity"; | 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 | gpu::PipelinePointer DeferredLightingEffect::getPipeline(SimpleProgramKey config) { | 73 | gpu::PipelinePointer DeferredLightingEffect::getPipeline(SimpleProgramKey config) { |
| 54 | auto it = _simplePrograms.find(config); | 74 | auto it = _simplePrograms.find(config); |
| 55 | if (it != _simplePrograms.end()) { | 75 | if (it != _simplePrograms.end()) { |
| @@ -91,6 +111,18 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { · 35 unmodified lines | |||
| 91 | gpu::Shader::makeProgram(*_emissiveShader, slotBindings); | 111 | gpu::Shader::makeProgram(*_emissiveShader, slotBindings); |
| 92 | 112 | ||
| 93 | _viewState = viewState; | 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 | loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations); | 126 | loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations); |
| 95 | loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap, | 127 | loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap, |
| 96 | _directionalLightShadowMapLocations); | 128 | _directionalLightShadowMapLocations); |
| @@ -282,7 +314,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 185 unmodified lines | |||
| 282 | invViewMat = args->_viewFrustum->getView(); | 314 | invViewMat = args->_viewFrustum->getView(); |
| 283 | 315 | ||
| 284 | auto& program = _directionalLight; | 316 | auto& program = _directionalLight; |
| 285 | const LightLocations* locations = &_directionalLightLocations; | 317 | LightLocationsPtr locations = _directionalLightLocations; |
| 286 | 318 | ||
| 287 | // FIXME: Note: we've removed the menu items to enable shadows, so this will always be false for now. | 319 | // FIXME: Note: we've removed the menu items to enable shadows, so this will always be false for now. |
| 288 | // When we add back shadow support, this old approach may likely be removed and completely replaced | 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 | batch.setResourceTexture(4, framebufferCache->getShadowFramebuffer()->getDepthStencilBuffer()); | 326 | batch.setResourceTexture(4, framebufferCache->getShadowFramebuffer()->getDepthStencilBuffer()); |
| 295 | 327 | ||
| 296 | program = _directionalLightShadowMap; | 328 | program = _directionalLightShadowMap; |
| 297 | locations = &_directionalLightShadowMapLocations; | 329 | locations = _directionalLightShadowMapLocations; |
| 298 | if (cascadeShadowsEnabled) { | 330 | if (cascadeShadowsEnabled) { |
| 299 | program = _directionalLightCascadedShadowMap; | 331 | program = _directionalLightCascadedShadowMap; |
| 300 | locations = &_directionalLightCascadedShadowMapLocations; | 332 | locations = _directionalLightCascadedShadowMapLocations; |
| 301 | if (useSkyboxCubemap) { | 333 | if (useSkyboxCubemap) { |
| 302 | program = _directionalSkyboxLightCascadedShadowMap; | 334 | program = _directionalSkyboxLightCascadedShadowMap; |
| 303 | locations = &_directionalSkyboxLightCascadedShadowMapLocations; | 335 | locations = _directionalSkyboxLightCascadedShadowMapLocations; |
| 304 | } else if (_ambientLightMode > -1) { | 336 | } else if (_ambientLightMode > -1) { |
| 305 | program = _directionalAmbientSphereLightCascadedShadowMap; | 337 | program = _directionalAmbientSphereLightCascadedShadowMap; |
| 306 | locations = &_directionalAmbientSphereLightCascadedShadowMapLocations; | 338 | locations = _directionalAmbientSphereLightCascadedShadowMapLocations; |
| 307 | } | 339 | } |
| 308 | batch.setPipeline(program); | 340 | batch.setPipeline(program); |
| 309 | batch._glUniform3fv(locations->shadowDistances, 1, (const float*) &_viewState->getShadowDistances()); | 341 | batch._glUniform3fv(locations->shadowDistances, 1, (const float*) &_viewState->getShadowDistances()); |
| @@ -311,10 +343,10 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 1 unmodified line | |||
| 311 | } else { | 343 | } else { |
| 312 | if (useSkyboxCubemap) { | 344 | if (useSkyboxCubemap) { |
| 313 | program = _directionalSkyboxLightShadowMap; | 345 | program = _directionalSkyboxLightShadowMap; |
| 314 | locations = &_directionalSkyboxLightShadowMapLocations; | 346 | locations = _directionalSkyboxLightShadowMapLocations; |
| 315 | } else if (_ambientLightMode > -1) { | 347 | } else if (_ambientLightMode > -1) { |
| 316 | program = _directionalAmbientSphereLightShadowMap; | 348 | program = _directionalAmbientSphereLightShadowMap; |
| 317 | locations = &_directionalAmbientSphereLightShadowMapLocations; | 349 | locations = _directionalAmbientSphereLightShadowMapLocations; |
| 318 | } | 350 | } |
| 319 | batch.setPipeline(program); | 351 | batch.setPipeline(program); |
| 320 | } | 352 | } |
| @@ -323,10 +355,10 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 2 unmodified lines | |||
| 323 | } else { | 355 | } else { |
| 324 | if (useSkyboxCubemap) { | 356 | if (useSkyboxCubemap) { |
| 325 | program = _directionalSkyboxLight; | 357 | program = _directionalSkyboxLight; |
| 326 | locations = &_directionalSkyboxLightLocations; | 358 | locations = _directionalSkyboxLightLocations; |
| 327 | } else if (_ambientLightMode > -1) { | 359 | } else if (_ambientLightMode > -1) { |
| 328 | program = _directionalAmbientSphereLight; | 360 | program = _directionalAmbientSphereLight; |
| 329 | locations = &_directionalAmbientSphereLightLocations; | 361 | locations = _directionalAmbientSphereLightLocations; |
| 330 | } | 362 | } |
| 331 | batch.setPipeline(program); | 363 | batch.setPipeline(program); |
| 332 | } | 364 | } |
| @@ -374,7 +406,9 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 41 unmodified lines | |||
| 374 | float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; | 406 | float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; |
| 375 | batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | 407 | batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); |
| 376 | batch._glUniform2f(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | 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 | Transform model; | 413 | Transform model; |
| 380 | model.setTranslation(glm::vec3(sMin, tMin, 0.0)); | 414 | model.setTranslation(glm::vec3(sMin, tMin, 0.0)); |
| @@ -427,20 +461,20 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 46 unmodified lines | |||
| 427 | 461 | ||
| 428 | if (!_pointLights.empty()) { | 462 | if (!_pointLights.empty()) { |
| 429 | batch.setPipeline(_pointLight); | 463 | batch.setPipeline(_pointLight); |
| 430 | batch._glUniform1f(_pointLightLocations.nearLocation, nearVal); | 464 | batch._glUniform1f(_pointLightLocations->nearLocation, nearVal); |
| 431 | batch._glUniform1f(_pointLightLocations.depthScale, depthScale); | 465 | batch._glUniform1f(_pointLightLocations->depthScale, depthScale); |
| 432 | batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | 466 | batch._glUniform2f(_pointLightLocations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); |
| 433 | batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | 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 | for (auto lightID : _pointLights) { | 473 | for (auto lightID : _pointLights) { |
| 440 | auto& light = _allocatedLights[lightID]; | 474 | auto& light = _allocatedLights[lightID]; |
| 441 | // IN DEBUG: light->setShowContour(true); | 475 | // IN DEBUG: light->setShowContour(true); |
| 442 | if (_pointLightLocations.lightBufferUnit >= 0) { | 476 | if (_pointLightLocations->lightBufferUnit >= 0) { |
| 443 | batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); | 477 | batch.setUniformBuffer(_pointLightLocations->lightBufferUnit, light->getSchemaBuffer()); |
| 444 | } | 478 | } |
| 445 | 479 | ||
| 446 | float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); | 480 | float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); |
| @@ -475,20 +509,20 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 28 unmodified lines | |||
| 475 | 509 | ||
| 476 | if (!_spotLights.empty()) { | 510 | if (!_spotLights.empty()) { |
| 477 | batch.setPipeline(_spotLight); | 511 | batch.setPipeline(_spotLight); |
| 478 | batch._glUniform1f(_spotLightLocations.nearLocation, nearVal); | 512 | batch._glUniform1f(_spotLightLocations->nearLocation, nearVal); |
| 479 | batch._glUniform1f(_spotLightLocations.depthScale, depthScale); | 513 | batch._glUniform1f(_spotLightLocations->depthScale, depthScale); |
| 480 | batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); | 514 | batch._glUniform2f(_spotLightLocations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); |
| 481 | batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); | 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 | for (auto lightID : _spotLights) { | 521 | for (auto lightID : _spotLights) { |
| 488 | auto light = _allocatedLights[lightID]; | 522 | auto light = _allocatedLights[lightID]; |
| 489 | // IN DEBUG: light->setShowContour(true); | 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 | auto eyeLightPos = eyePoint - light->getPosition(); | 527 | auto eyeLightPos = eyePoint - light->getPosition(); |
| 494 | auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection()); | 528 | auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection()); |
| @@ -502,7 +536,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 7 unmodified lines | |||
| 502 | if ((eyeHalfPlaneDistance > -nearRadius) && | 536 | if ((eyeHalfPlaneDistance > -nearRadius) && |
| 503 | (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius)) { | 537 | (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius)) { |
| 504 | coneParam.w = 0.0f; | 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 | Transform model; | 541 | Transform model; |
| 508 | model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); | 542 | model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); |
| @@ -522,7 +556,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { · 13 unmodified lines | |||
| 522 | batch.setViewTransform(viewMat); | 556 | batch.setViewTransform(viewMat); |
| 523 | } else { | 557 | } else { |
| 524 | coneParam.w = 1.0f; | 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 | Transform model; | 561 | Transform model; |
| 528 | model.setTranslation(light->getPosition()); | 562 | model.setTranslation(light->getPosition()); |
| @@ -596,7 +630,7 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU · 67 unmodified lines | |||
| 596 | args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); | 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 | auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(vertSource))); | 634 | auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(vertSource))); |
| 601 | auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource))); | 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 | gpu::Shader::makeProgram(*program, slotBindings); | 651 | gpu::Shader::makeProgram(*program, slotBindings); |
| 618 | 652 | ||
| 619 | locations.shadowDistances = program->getUniforms().findLocation("shadowDistances"); | 653 | locations->stereo = program->getUniforms().findLocation("stereoMode"); |
| 620 | locations.shadowScale = program->getUniforms().findLocation("shadowScale"); | 654 | locations->shadowDistances = program->getUniforms().findLocation("shadowDistances"); |
| 621 | locations.nearLocation = program->getUniforms().findLocation("near"); | 655 | locations->shadowScale = program->getUniforms().findLocation("shadowScale"); |
| 622 | locations.depthScale = program->getUniforms().findLocation("depthScale"); | 656 | locations->nearLocation = program->getUniforms().findLocation("near"); |
| 623 | locations.depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset"); | 657 | locations->depthScale = program->getUniforms().findLocation("depthScale"); |
| 624 | locations.depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale"); | 658 | locations->depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset"); |
| 625 | locations.radius = program->getUniforms().findLocation("radius"); | 659 | locations->depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale"); |
| 626 | locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); | 660 | locations->radius = program->getUniforms().findLocation("radius"); |
| 627 | locations.invViewMat = program->getUniforms().findLocation("invViewMat"); | 661 | locations->ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); |
| 628 | locations.texcoordMat = program->getUniforms().findLocation("texcoordMat"); | 662 | locations->invViewMat = program->getUniforms().findLocation("invViewMat"); |
| 629 | locations.coneParam = program->getUniforms().findLocation("coneParam"); | 663 | locations->texcoordMat = program->getUniforms().findLocation("texcoordMat"); |
| 630 | 664 | locations->coneParam = program->getUniforms().findLocation("coneParam"); | |
| 631 | locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); | 665 | |
| 632 | locations.atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit"); | 666 | locations->lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); |
| 667 | locations->atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit"); | ||
| 633 | 668 | ||
| 634 | auto state = std::make_shared<gpu::State>(); | 669 | auto state = std::make_shared<gpu::State>(); |
| 635 | if (lightVolume) { | 670 | if (lightVolume) { |