From d9cbc97ec07909e6ddb6687e0c22da07cc6bbdb6 Mon Sep 17 00:00:00 2001 From: Qing Tao <55564570+VickyAtAZ@users.noreply.github.com> Date: Fri, 10 Sep 2021 10:38:16 -0700 Subject: [PATCH] ATOM-16063 Remove SetShaderResourceGroupCallback in scene and update scene srg handling (#3969) ATOM-16273 Compiling SceneSRG before updating it can cause a gpu crash Changes include: 1. Removed Scene::SetShaderResourceGroupCallback() function and clean up code which use this function. 2. Moved SceneTimeSrg.azsli to RPI's DefaultSceneSrg folder and setup the constants in RPI::Scene 3. Add AZ::Event for Scene's update srg event which features and update scene srg at proper place 4. UpdateTransformServcie FP to use PrepareSceneSrg event handler. 5. Clean up shaders and srgs used in project templates. Signed-off-by: Qing Tao --- AutomatedTesting/Shaders/CommonVS.azsli | 51 ------------------ .../Code/Source/BootstrapSystemComponent.cpp | 33 +----------- .../Code/Source/BootstrapSystemComponent.h | 3 -- .../ShaderResourceGroups/SceneSrgAll.azsli | 2 +- .../Shaders/PostProcessing/EyeAdaptation.azsl | 4 +- .../PostProcessing/EyeAdaptationUtil.azsli | 3 +- .../atom_feature_common_asset_files.cmake | 1 - .../TransformServiceFeatureProcessor.h | 15 +++--- .../ExposureControlRenderProxy.cpp | 12 ++--- .../Source/PostProcessing/EyeAdaptationPass.h | 1 + .../TransformServiceFeatureProcessor.cpp | 25 ++++----- .../DefaultSceneSrg.azsli} | 1 - .../RPI/Code/Include/Atom/RPI.Public/Scene.h | 23 +++++--- .../Code/Include/Atom/RPI.Public/SceneBus.h | 1 - .../RPI.Public/Pass/AttachmentReadback.cpp | 6 +-- .../RPI/Code/Source/RPI.Public/RPISystem.cpp | 2 +- .../Atom/RPI/Code/Source/RPI.Public/Scene.cpp | 47 ++++++++++------- .../Viewport/MaterialViewportRenderer.cpp | 20 +------ .../Viewport/MaterialViewportRenderer.h | 2 - .../Rendering/ThumbnailRendererData.h | 2 - .../ThumbnailRendererSteps/CaptureStep.cpp | 5 +- .../ThumbnailRendererSteps/InitializeStep.cpp | 28 ---------- .../Template/ShaderLib/scenesrg.srgi | 3 +- .../Template/Shaders/CommonVS.azsli | 52 ------------------- .../ShaderResourceGroups/SceneSrg.azsli | 20 ------- Templates/DefaultProject/template.json | 12 ----- .../Template/ShaderLib/scenesrg.srgi | 3 +- .../Template/Shaders/CommonVS.azsli | 52 ------------------- .../ShaderResourceGroups/SceneSrg.azsli | 20 ------- Templates/MinimalProject/template.json | 12 ----- 30 files changed, 80 insertions(+), 381 deletions(-) delete mode 100644 AutomatedTesting/Shaders/CommonVS.azsli rename Gems/Atom/{Feature/Common/Assets/ShaderResourceGroups/SceneTimeSrg.azsli => RPI/Assets/ShaderLib/Atom/RPI/ShaderResourceGroups/DefaultSceneSrg.azsli} (94%) delete mode 100644 Templates/DefaultProject/Template/Shaders/CommonVS.azsli delete mode 100644 Templates/DefaultProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli delete mode 100644 Templates/MinimalProject/Template/Shaders/CommonVS.azsli delete mode 100644 Templates/MinimalProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli diff --git a/AutomatedTesting/Shaders/CommonVS.azsli b/AutomatedTesting/Shaders/CommonVS.azsli deleted file mode 100644 index 3d46871b59..0000000000 --- a/AutomatedTesting/Shaders/CommonVS.azsli +++ /dev/null @@ -1,51 +0,0 @@ - -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ - -#pragma once - -#include -#include -#include - -struct VertexInput -{ - float3 m_position : POSITION; - float3 m_normal : NORMAL; - float4 m_tangent : TANGENT; - float3 m_bitangent : BITANGENT; - float2 m_uv : UV0; -}; - -struct VertexOutput -{ - float4 m_position : SV_Position; - float3 m_normal : NORMAL; - float3 m_tangent : TANGENT; - float3 m_bitangent : BITANGENT; - float2 m_uv : UV0; - float3 m_view : VIEW; -}; - -VertexOutput CommonVS(VertexInput input) -{ - float4x4 objectToWorld = ObjectSrg::GetWorldMatrix(); - float3x3 objectToWorldIT = ObjectSrg::GetWorldMatrixInverseTranspose(); - - VertexOutput output; - float3 worldPosition = mul(objectToWorld, float4(input.m_position, 1)).xyz; - output.m_position = mul(ViewSrg::m_viewProjectionMatrix, float4(worldPosition, 1.0)); - - output.m_uv = input.m_uv; - - output.m_view = worldPosition - ViewSrg::m_worldPosition; - - ConstructTBN(input.m_normal, input.m_tangent, input.m_bitangent, objectToWorld, objectToWorldIT, output.m_normal, output.m_tangent, output.m_bitangent); - - return output; -} diff --git a/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.cpp b/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.cpp index 11758138e0..d837988cfb 100644 --- a/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.cpp +++ b/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.cpp @@ -253,34 +253,6 @@ namespace AZ RPI::SceneDescriptor sceneDesc; AZ::RPI::ScenePtr atomScene = RPI::Scene::CreateScene(sceneDesc); atomScene->EnableAllFeatureProcessors(); - - // Setup scene srg modification callback. - RPI::ShaderResourceGroupCallback callback = [this](RPI::ShaderResourceGroup* srg) - { - if (srg == nullptr) - { - return; - } - bool needCompile = false; - RHI::ShaderInputConstantIndex timeIndex = srg->FindShaderInputConstantIndex(Name{ "m_time" }); - if (timeIndex.IsValid()) - { - srg->SetConstant(timeIndex, m_simulateTime); - needCompile = true; - } - RHI::ShaderInputConstantIndex deltaTimeIndex = srg->FindShaderInputConstantIndex(Name{ "m_deltaTime" }); - if (deltaTimeIndex.IsValid()) - { - srg->SetConstant(deltaTimeIndex, m_deltaTime); - needCompile = true; - } - - if (needCompile) - { - srg->Compile(); - } - }; - atomScene->SetShaderResourceGroupCallback(callback); atomScene->Activate(); // Register scene to RPI system so it will be processed/rendered per tick @@ -408,11 +380,8 @@ namespace AZ m_renderPipelineId = ""; } - void BootstrapSystemComponent::OnTick(float deltaTime, [[maybe_unused]] ScriptTimePoint time) + void BootstrapSystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] ScriptTimePoint time) { - m_simulateTime += deltaTime; - m_deltaTime = deltaTime; - // Temp: When running in the launcher without the legacy renderer // we need to call RenderTick on the viewport context each frame. if (m_viewportContext) diff --git a/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.h b/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.h index bd5d417b8f..566d19b1a4 100644 --- a/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.h +++ b/Gems/Atom/Bootstrap/Code/Source/BootstrapSystemComponent.h @@ -105,9 +105,6 @@ namespace AZ RPI::ScenePtr m_defaultScene = nullptr; AZStd::shared_ptr m_defaultFrameworkScene = nullptr; - float m_simulateTime = 0; - float m_deltaTime = 0.016f; - bool m_isAssetCatalogLoaded = false; // The id of the render pipeline created by this component diff --git a/Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneSrgAll.azsli b/Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneSrgAll.azsli index 96ed740f98..421178bb01 100644 --- a/Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneSrgAll.azsli +++ b/Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneSrgAll.azsli @@ -11,6 +11,6 @@ // Please review README.md to understand how this file is used in SceneSrg.azsrg generation #ifdef AZ_COLLECTING_PARTIAL_SRGS -#include +#include #include #endif diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptation.azsl b/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptation.azsl index ce10eb9b91..1844144401 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptation.azsl +++ b/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptation.azsl @@ -78,10 +78,12 @@ void MainCS(uint3 dispatch_id : SV_DispatchThreadID) const float speed = exposureDifference > 0.0 ? ViewSrg::m_exposureControl.m_speedUp : ViewSrg::m_exposureControl.m_speedDown; // Update the adjustment for this frame based on the frame deltaTime and speed - float exposureAdjustment = exposureDifference * SceneSrg::m_deltaTime * speed; + float deltaTime = clamp(SceneSrg::m_time - PassSrg::m_eyeAdaptationData[0].m_setValueTime, 0.0f, 1.0f); + float exposureAdjustment = exposureDifference * deltaTime * speed; float newExposureLog2 = previousFrameExposureLog2 + exposureAdjustment; // Store the linear exposure so it can be used by the look modification transform later. // newExposureLog2 is negated because m_exposureValue is used to correct for a given exposure. PassSrg::m_eyeAdaptationData[0].m_exposureValue = pow(2.0f, -newExposureLog2); + PassSrg::m_eyeAdaptationData[0].m_setValueTime = SceneSrg::m_time; } diff --git a/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptationUtil.azsli b/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptationUtil.azsli index 54e96122ac..fb75e1079c 100644 --- a/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptationUtil.azsli +++ b/Gems/Atom/Feature/Common/Assets/Shaders/PostProcessing/EyeAdaptationUtil.azsli @@ -8,5 +8,6 @@ struct EyeAdaptation { - float m_exposureValue; // current frame's exposure value in stops (logarithmic space) + float m_exposureValue; // current frame's exposure value in stops (logarithmic space) + float m_setValueTime; // the time when the m_exposureValue was set }; diff --git a/Gems/Atom/Feature/Common/Assets/atom_feature_common_asset_files.cmake b/Gems/Atom/Feature/Common/Assets/atom_feature_common_asset_files.cmake index 75181517a4..e43498c55d 100644 --- a/Gems/Atom/Feature/Common/Assets/atom_feature_common_asset_files.cmake +++ b/Gems/Atom/Feature/Common/Assets/atom_feature_common_asset_files.cmake @@ -289,7 +289,6 @@ set(FILES ShaderLib/Atom/Features/Vertex/VertexHelper.azsli ShaderResourceGroups/SceneSrg.azsli ShaderResourceGroups/SceneSrgAll.azsli - ShaderResourceGroups/SceneTimeSrg.azsli ShaderResourceGroups/ViewSrg.azsli ShaderResourceGroups/ViewSrgAll.azsli ShaderResourceGroups/CoreLights/SceneSrg.azsli diff --git a/Gems/Atom/Feature/Common/Code/Include/Atom/Feature/TransformService/TransformServiceFeatureProcessor.h b/Gems/Atom/Feature/Common/Code/Include/Atom/Feature/TransformService/TransformServiceFeatureProcessor.h index 6482871234..936fc88fa6 100644 --- a/Gems/Atom/Feature/Common/Code/Include/Atom/Feature/TransformService/TransformServiceFeatureProcessor.h +++ b/Gems/Atom/Feature/Common/Code/Include/Atom/Feature/TransformService/TransformServiceFeatureProcessor.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace AZ @@ -36,8 +37,6 @@ namespace AZ void Activate() override; //! Releases GPU resources. void Deactivate() override; - //! Binds buffers - void Render(const FeatureProcessor::RenderPacket& packet) override; // RPI::SceneNotificationBus overrides ... void OnBeginPrepareRender() override; @@ -68,11 +67,13 @@ namespace AZ // Prepare GPU buffers for object transformation matrices // Create the buffers if they don't exist. Otherwise, resize them if they are not large enough for the matrices void PrepareBuffers(); - - Data::Instance m_sceneSrg; - RHI::ShaderInputBufferIndex m_objectToWorldBufferIndex; - RHI::ShaderInputBufferIndex m_objectToWorldInverseTransposeBufferIndex; - RHI::ShaderInputBufferIndex m_objectToWorldHistoryBufferIndex; + + void UpdateSceneSrg(RPI::ShaderResourceGroup *sceneSrg); + + RPI::Scene::PrepareSceneSrgEvent::Handler m_updateSceneSrgHandler; + RHI::ShaderInputNameIndex m_objectToWorldBufferIndex = "m_objectToWorldBuffer"; + RHI::ShaderInputNameIndex m_objectToWorldInverseTransposeBufferIndex = "m_objectToWorldInverseTransposeBuffer"; + RHI::ShaderInputNameIndex m_objectToWorldHistoryBufferIndex = "m_objectToWorldHistoryBuffer"; // Stores transforms that are uploaded to a GPU buffer. Used slots have float12(matrix3x4) values, empty slots // have a uint32_t that points to the next empty slot like a linked list. m_firstAvailableMeshTransformIndex stores the first diff --git a/Gems/Atom/Feature/Common/Code/Source/PostProcessing/ExposureControlRenderProxy.cpp b/Gems/Atom/Feature/Common/Code/Source/PostProcessing/ExposureControlRenderProxy.cpp index 1d345a2432..924af4ab98 100644 --- a/Gems/Atom/Feature/Common/Code/Source/PostProcessing/ExposureControlRenderProxy.cpp +++ b/Gems/Atom/Feature/Common/Code/Source/PostProcessing/ExposureControlRenderProxy.cpp @@ -36,8 +36,6 @@ namespace AZ m_viewPtr = view; m_viewSrg = view->GetShaderResourceGroup(); - m_exposureControlBufferInputIndex = m_viewSrg->FindShaderInputBufferIndex(Name("m_exposureControl")); - m_eyeAdaptationBuffer.Init(m_viewSrg, idNumber); } @@ -109,14 +107,10 @@ namespace AZ m_eyeAdaptationBuffer.UpdateSrg(); - if (m_exposureControlBufferInputIndex.IsValid()) + m_viewSrg->SetBufferView(m_exposureControlBufferInputIndex, m_buffer->GetBufferView()); + if (m_viewPtr) { - m_viewSrg->SetBufferView(m_exposureControlBufferInputIndex, m_buffer->GetBufferView()); - - if (m_viewPtr) - { - m_viewPtr->InvalidateSrg(); - } + m_viewPtr->InvalidateSrg(); } } diff --git a/Gems/Atom/Feature/Common/Code/Source/PostProcessing/EyeAdaptationPass.h b/Gems/Atom/Feature/Common/Code/Source/PostProcessing/EyeAdaptationPass.h index 0d045a0f78..bec3af2934 100644 --- a/Gems/Atom/Feature/Common/Code/Source/PostProcessing/EyeAdaptationPass.h +++ b/Gems/Atom/Feature/Common/Code/Source/PostProcessing/EyeAdaptationPass.h @@ -52,6 +52,7 @@ namespace AZ struct ExposureCalculationData { float m_exposureValue = 1.0f; + float m_setValueTime = 0; }; void BuildInternal() override; diff --git a/Gems/Atom/Feature/Common/Code/Source/TransformService/TransformServiceFeatureProcessor.cpp b/Gems/Atom/Feature/Common/Code/Source/TransformService/TransformServiceFeatureProcessor.cpp index 7a76e1b7e0..6f9c0dc73a 100644 --- a/Gems/Atom/Feature/Common/Code/Source/TransformService/TransformServiceFeatureProcessor.cpp +++ b/Gems/Atom/Feature/Common/Code/Source/TransformService/TransformServiceFeatureProcessor.cpp @@ -36,11 +36,8 @@ namespace AZ void TransformServiceFeatureProcessor::Activate() { - m_sceneSrg = GetParentScene()->GetShaderResourceGroup(); - - m_objectToWorldBufferIndex = m_sceneSrg->FindShaderInputBufferIndex(Name{"m_objectToWorldBuffer"}); - m_objectToWorldInverseTransposeBufferIndex = m_sceneSrg->FindShaderInputBufferIndex(Name{"m_objectToWorldInverseTransposeBuffer"}); - m_objectToWorldHistoryBufferIndex = m_sceneSrg->FindShaderInputBufferIndex(Name{"m_objectToWorldHistoryBuffer"}); + m_updateSceneSrgHandler = RPI::Scene::PrepareSceneSrgEvent::Handler([this](RPI::ShaderResourceGroup *sceneSrg) { this->UpdateSceneSrg(sceneSrg); }); + GetParentScene()->ConnectEvent(m_updateSceneSrgHandler); m_deviceBufferNeedsUpdate = true; m_objectToWorldTransforms.reserve(BufferReserveCount); @@ -62,9 +59,14 @@ namespace AZ m_firstAvailableTransformIndex = NoAvailableTransformIndices; + m_objectToWorldBufferIndex.Reset(); + m_objectToWorldInverseTransposeBufferIndex.Reset(); + m_objectToWorldHistoryBufferIndex.Reset(); + m_isWriteable = false; RPI::SceneNotificationBus::Handler::BusDisconnect(); + m_updateSceneSrgHandler.Disconnect(); } void TransformServiceFeatureProcessor::PrepareBuffers() @@ -131,16 +133,11 @@ namespace AZ } } - void TransformServiceFeatureProcessor::Render([[maybe_unused]] const FeatureProcessor::RenderPacket& packet) + void TransformServiceFeatureProcessor::UpdateSceneSrg(RPI::ShaderResourceGroup *sceneSrg) { - AZ_ATOM_PROFILE_FUNCTION("RPI", "TransformServiceFeatureProcessor: Render"); - AZ_UNUSED(packet); - - AZ_Assert(!m_isWriteable, "Must be called between OnBeginPrepareRender() and OnEndPrepareRender()"); - - m_sceneSrg->SetBufferView(m_objectToWorldBufferIndex, m_objectToWorldBuffer->GetBufferView()); - m_sceneSrg->SetBufferView(m_objectToWorldInverseTransposeBufferIndex, m_objectToWorldInverseTransposeBuffer->GetBufferView()); - m_sceneSrg->SetBufferView(m_objectToWorldHistoryBufferIndex, m_objectToWorldHistoryBuffer->GetBufferView()); + sceneSrg->SetBufferView(m_objectToWorldBufferIndex, m_objectToWorldBuffer->GetBufferView()); + sceneSrg->SetBufferView(m_objectToWorldInverseTransposeBufferIndex, m_objectToWorldInverseTransposeBuffer->GetBufferView()); + sceneSrg->SetBufferView(m_objectToWorldHistoryBufferIndex, m_objectToWorldHistoryBuffer->GetBufferView()); } void TransformServiceFeatureProcessor::OnBeginPrepareRender() diff --git a/Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneTimeSrg.azsli b/Gems/Atom/RPI/Assets/ShaderLib/Atom/RPI/ShaderResourceGroups/DefaultSceneSrg.azsli similarity index 94% rename from Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneTimeSrg.azsli rename to Gems/Atom/RPI/Assets/ShaderLib/Atom/RPI/ShaderResourceGroups/DefaultSceneSrg.azsli index c89fc6b4c4..f9428b3125 100644 --- a/Gems/Atom/Feature/Common/Assets/ShaderResourceGroups/SceneTimeSrg.azsli +++ b/Gems/Atom/RPI/Assets/ShaderLib/Atom/RPI/ShaderResourceGroups/DefaultSceneSrg.azsli @@ -13,6 +13,5 @@ partial ShaderResourceGroup SceneSrg { float m_time; - float m_deltaTime; } diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Scene.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Scene.h index fa918ae033..b1c6aac92d 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Scene.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Scene.h @@ -129,10 +129,6 @@ namespace AZ void RemoveRenderPipeline(const RenderPipelineId& pipelineId); - //! Set a callback function to set values for scene's srg. - //! The callback function is usually defined by the one who create the scene since it knows how the layout look like. - void SetShaderResourceGroupCallback(ShaderResourceGroupCallback callback); - const RHI::ShaderResourceGroup* GetRHIShaderResourceGroup() const; Data::Instance GetShaderResourceGroup() const; @@ -166,9 +162,13 @@ namespace AZ RenderPipelinePtr FindRenderPipelineForWindow(AzFramework::NativeWindowHandle windowHandle); + using PrepareSceneSrgEvent = AZ::Event; + //! Connect a handler to listen to the event that the Scene is ready to update and compile its scene srg + //! User should use this event to update the part scene srg they know of + void ConnectEvent(PrepareSceneSrgEvent::Handler& handler); + protected: // SceneFinder overrides... - Scene* FindSelf(); void OnSceneNotifictaionHandlerConnected(SceneNotification* handler); // Cpu simulation which runs all active FeatureProcessor Simulate() functions. @@ -183,7 +183,7 @@ namespace AZ // Function called when the current frame is finished rendering. void OnFrameEnd(); - // Update and compile view srgs + // Update and compile scene and view srgs // This is called after PassSystem's FramePrepare so passes can still modify view srgs in its FramePrepareIntenal function before they are submitted to command list void UpdateSrgs(); @@ -200,6 +200,10 @@ namespace AZ // Add a created feature processor to this scene void AddFeatureProcessor(FeatureProcessorPtr fp); + // Send out event to PrepareSceneSrgEvent::Handlers so they can update scene srg as needed + // This happens in UpdateSrgs() + void PrepareSceneSrg(); + // List of feature processors that are active for this scene AZStd::vector m_featureProcessors; @@ -215,9 +219,10 @@ namespace AZ AZ::RPI::FeatureProcessor::SimulatePacket m_simulatePacket; AZ::RPI::FeatureProcessor::RenderPacket m_renderPacket; - // Scene's srg and its set function + // Scene's srg Data::Instance m_srg; - ShaderResourceGroupCallback m_srgCallback; + // Event to for prepare scene srg + PrepareSceneSrgEvent m_prepareSrgEvent; // The uuid to identify this scene. SceneId m_id; @@ -234,6 +239,8 @@ namespace AZ // Registry which allocates draw filter tag for RenderPipeline RHI::Ptr m_drawFilterTagRegistry; + + float m_simulationTime; }; // --- Template functions --- diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/SceneBus.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/SceneBus.h index 4222f634cc..ca531d2de6 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/SceneBus.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/SceneBus.h @@ -80,7 +80,6 @@ namespace AZ static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById; using BusIdType = SceneId; - virtual Scene* FindSelf() = 0; virtual void OnSceneNotifictaionHandlerConnected(SceneNotification* handler) = 0; }; diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/AttachmentReadback.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/AttachmentReadback.cpp index 146c37fe1b..c19ae565d0 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/AttachmentReadback.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Pass/AttachmentReadback.cpp @@ -308,10 +308,10 @@ namespace AZ // The fix is to clear the buffer outside of the callback. for (int32_t i = 0; i < RHI::Limits::Device::FrameCountMax; i++) { - if (m_isReadbackComplete[m_readbackBufferCurrentIndex]) + if (m_isReadbackComplete[i]) { - m_isReadbackComplete[m_readbackBufferCurrentIndex] = false; - m_readbackBufferArray[m_readbackBufferCurrentIndex] = nullptr; + m_isReadbackComplete[i] = false; + m_readbackBufferArray[i] = nullptr; } } // Loop the triple buffer index and cache the current index to the callback. diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/RPISystem.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/RPISystem.cpp index 66c4f6d20a..500bf21628 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/RPISystem.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/RPISystem.cpp @@ -293,7 +293,7 @@ namespace AZ // scope producers only can be added to the frame when frame started which cleans up previous scope producers. m_passSystem.FrameUpdate(frameGraphBuilder); - // Update View Srgs + // Update Scene and View Srgs for (auto& scenePtr : m_scenes) { scenePtr->UpdateSrgs(); diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp index 761ce20ee8..0b0481b960 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Scene.cpp @@ -352,6 +352,8 @@ namespace AZ { AZ_ATOM_PROFILE_FUNCTION("RPI", "Scene: Simulate"); + m_simulationTime = tickInfo.m_currentGameTime; + // If previous simulation job wasn't done, wait for it to finish. WaitAndCleanCompletionJob(m_simulationCompletion); @@ -395,6 +397,29 @@ namespace AZ } } + void Scene::ConnectEvent(PrepareSceneSrgEvent::Handler& handler) + { + handler.Connect(m_prepareSrgEvent); + } + + void Scene::PrepareSceneSrg() + { + if (m_srg) + { + // Set value for constants defined in SceneTimeSrg.azsli + RHI::ShaderInputConstantIndex timeIndex = m_srg->FindShaderInputConstantIndex(Name{ "m_time" }); + if (timeIndex.IsValid()) + { + m_srg->SetConstant(timeIndex, m_simulationTime); + } + + // signal any handlers to update values for their partial scene srg + m_prepareSrgEvent.Signal(m_srg.get()); + + m_srg->Compile(); + } + } + void Scene::PrepareRender(const TickTimeInfo& tickInfo, RHI::JobPolicy jobPolicy) { AZ_ATOM_PROFILE_FUNCTION("RPI", "Scene: PrepareRender"); @@ -407,16 +432,6 @@ namespace AZ SceneNotificationBus::Event(GetId(), &SceneNotification::OnBeginPrepareRender); - { - AZ_PROFILE_SCOPE(RPI, "m_srgCallback"); - AZ_ATOM_PROFILE_TIME_GROUP_REGION("RPI", "ShaderResourceGroupCallback: SrgCallback"); - // Set values for scene srg - if (m_srg && m_srgCallback) - { - m_srgCallback(m_srg.get()); - } - } - // Get active pipelines which need to be rendered and notify them frame started AZStd::vector activePipelines; { @@ -587,17 +602,14 @@ namespace AZ void Scene::UpdateSrgs() { + PrepareSceneSrg(); + for (auto& view : m_renderPacket.m_views) { view->UpdateSrg(); } } - void Scene::SetShaderResourceGroupCallback(ShaderResourceGroupCallback callback) - { - m_srgCallback = callback; - } - const RHI::ShaderResourceGroup* Scene::GetRHIShaderResourceGroup() const { if (m_srg.get()) @@ -641,11 +653,6 @@ namespace AZ return m_pipelines; } - Scene* Scene::FindSelf() - { - return this; - } - void Scene::OnSceneNotifictaionHandlerConnected(SceneNotification* handler) { for (auto renderPipeline : m_pipelines) diff --git a/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.cpp b/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.cpp index f98cdce91b..36498d6d08 100644 --- a/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.cpp +++ b/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.cpp @@ -70,22 +70,6 @@ namespace MaterialEditor m_scene = AZ::RPI::Scene::CreateScene(sceneDesc); m_scene->EnableAllFeatureProcessors(); - // Setup scene srg modification callback. - AZ::RPI::ShaderResourceGroupCallback callback = [this](AZ::RPI::ShaderResourceGroup* srg) - { - if (srg == nullptr) - { - return; - } - AZ::RHI::ShaderInputConstantIndex timeIndex = srg->FindShaderInputConstantIndex(AZ::Name{ "m_time" }); - if (timeIndex.IsValid()) - { - srg->SetConstant(timeIndex, m_simulateTime); - srg->Compile(); - } - }; - m_scene->SetShaderResourceGroupCallback(callback); - // Bind m_defaultScene to the GameEntityContext's AzFramework::Scene auto sceneSystem = AzFramework::SceneSystemInterface::Get(); AZ_Assert(sceneSystem, "MaterialViewportRenderer was unable to get the scene system during construction."); @@ -448,14 +432,12 @@ namespace MaterialEditor } } - void MaterialViewportRenderer::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) + void MaterialViewportRenderer::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) { m_renderPipeline->AddToRenderTickOnce(); PerformanceMonitorRequestBus::Broadcast(&PerformanceMonitorRequestBus::Handler::GatherMetrics); - m_simulateTime += deltaTime; - if (m_shadowCatcherMaterial) { // Compile the m_shadowCatcherMaterial in OnTick because changes can only be compiled once per frame. diff --git a/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.h b/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.h index 240d66fd43..8a3e0ca4ff 100644 --- a/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.h +++ b/Gems/Atom/Tools/MaterialEditor/Code/Source/Viewport/MaterialViewportRenderer.h @@ -114,8 +114,6 @@ namespace MaterialEditor AZ::Entity* m_iblEntity = nullptr; AZ::Render::SkyBoxFeatureProcessorInterface* m_skyboxFeatureProcessor = nullptr; - float m_simulateTime = 0; - AZStd::shared_ptr m_viewportController; }; } // namespace MaterialEditor diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererData.h b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererData.h index 4ced050fc1..c471cf90d7 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererData.h +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererData.h @@ -46,8 +46,6 @@ namespace AZ RPI::ViewPtr m_view = nullptr; Entity* m_modelEntity = nullptr; - double m_simulateTime = 0.0f; - float m_deltaTime = 0.0f; int m_thumbnailSize = 512; //! Incoming thumbnail requests are appended to this queue and processed one at a time in OnTick function. diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/CaptureStep.cpp b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/CaptureStep.cpp index 9a425ddeb4..f728d56bbc 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/CaptureStep.cpp +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/CaptureStep.cpp @@ -81,11 +81,8 @@ namespace AZ m_context->GetData()->m_view->SetCameraTransform(Matrix3x4::CreateFromTransform(cameraTransform)); } - void CaptureStep::OnTick(float deltaTime, ScriptTimePoint time) + void CaptureStep::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] ScriptTimePoint time) { - m_context->GetData()->m_deltaTime = deltaTime; - m_context->GetData()->m_simulateTime = time.GetSeconds(); - if (m_readyToCapture && m_ticksToCapture-- <= 0) { m_context->GetData()->m_renderPipeline->AddToRenderTickOnce(); diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/InitializeStep.cpp b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/InitializeStep.cpp index 4851d8f792..46eb0937bd 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/InitializeStep.cpp +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Thumbnails/Rendering/ThumbnailRendererSteps/InitializeStep.cpp @@ -72,34 +72,6 @@ namespace AZ data->m_scene = RPI::Scene::CreateScene(sceneDesc); - // Setup scene srg modification callback (to push per-frame values to the shaders) - RPI::ShaderResourceGroupCallback callback = [data](RPI::ShaderResourceGroup* srg) - { - if (srg == nullptr) - { - return; - } - bool needCompile = false; - RHI::ShaderInputConstantIndex timeIndex = srg->FindShaderInputConstantIndex(Name{ "m_time" }); - if (timeIndex.IsValid()) - { - srg->SetConstant(timeIndex, aznumeric_cast(data->m_simulateTime)); - needCompile = true; - } - RHI::ShaderInputConstantIndex deltaTimeIndex = srg->FindShaderInputConstantIndex(Name{ "m_deltaTime" }); - if (deltaTimeIndex.IsValid()) - { - srg->SetConstant(deltaTimeIndex, data->m_deltaTime); - needCompile = true; - } - - if (needCompile) - { - srg->Compile(); - } - }; - data->m_scene->SetShaderResourceGroupCallback(callback); - // Bind m_defaultScene to the GameEntityContext's AzFramework::Scene auto* sceneSystem = AzFramework::SceneSystemInterface::Get(); AZ_Assert(sceneSystem, "Thumbnail system failed to get scene system implementation."); diff --git a/Templates/DefaultProject/Template/ShaderLib/scenesrg.srgi b/Templates/DefaultProject/Template/ShaderLib/scenesrg.srgi index 38335bfc26..c5024b86ad 100644 --- a/Templates/DefaultProject/Template/ShaderLib/scenesrg.srgi +++ b/Templates/DefaultProject/Template/ShaderLib/scenesrg.srgi @@ -22,6 +22,5 @@ partial ShaderResourceGroup SceneSrg : SRG_PerScene }; #define AZ_COLLECTING_PARTIAL_SRGS -#include -#include +#include #undef AZ_COLLECTING_PARTIAL_SRGS diff --git a/Templates/DefaultProject/Template/Shaders/CommonVS.azsli b/Templates/DefaultProject/Template/Shaders/CommonVS.azsli deleted file mode 100644 index 4c20d85b88..0000000000 --- a/Templates/DefaultProject/Template/Shaders/CommonVS.azsli +++ /dev/null @@ -1,52 +0,0 @@ -// {BEGIN_LICENSE} -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ -// {END_LICENSE} - -#pragma once - -#include -#include -#include - -struct VertexInput -{ - float3 m_position : POSITION; - float3 m_normal : NORMAL; - float4 m_tangent : TANGENT; - float3 m_bitangent : BITANGENT; - float2 m_uv : UV0; -}; - -struct VertexOutput -{ - float4 m_position : SV_Position; - float3 m_normal : NORMAL; - float3 m_tangent : TANGENT; - float3 m_bitangent : BITANGENT; - float2 m_uv : UV0; - float3 m_view : VIEW; -}; - -VertexOutput CommonVS(VertexInput input) -{ - float4x4 objectToWorld = ObjectSrg::GetWorldMatrix(); - float3x3 objectToWorldIT = ObjectSrg::GetWorldMatrixInverseTranspose(); - - VertexOutput output; - float3 worldPosition = mul(objectToWorld, float4(input.m_position, 1)).xyz; - output.m_position = mul(ViewSrg::m_viewProjectionMatrix, float4(worldPosition, 1.0)); - - output.m_uv = input.m_uv; - - output.m_view = worldPosition - ViewSrg::m_worldPosition; - - ConstructTBN(input.m_normal, input.m_tangent, input.m_bitangent, objectToWorld, objectToWorldIT, output.m_normal, output.m_tangent, output.m_bitangent); - - return output; -} diff --git a/Templates/DefaultProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli b/Templates/DefaultProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli deleted file mode 100644 index f6422f8b2f..0000000000 --- a/Templates/DefaultProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli +++ /dev/null @@ -1,20 +0,0 @@ -// {BEGIN_LICENSE} -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ -// {END_LICENSE} - -#ifndef AZ_COLLECTING_PARTIAL_SRGS -#error Do not include this file directly. Include the main .srgi file instead. -#endif - -partial ShaderResourceGroup SceneSrg -{ - float m_time; - float m_deltaTime; -} - diff --git a/Templates/DefaultProject/template.json b/Templates/DefaultProject/template.json index 6cf861a2ef..1e84ea8424 100644 --- a/Templates/DefaultProject/template.json +++ b/Templates/DefaultProject/template.json @@ -504,18 +504,6 @@ "isTemplated": true, "isOptional": false }, - { - "file": "Shaders/CommonVS.azsli", - "origin": "Shaders/CommonVS.azsli", - "isTemplated": true, - "isOptional": false - }, - { - "file": "Shaders/ShaderResourceGroups/SceneSrg.azsli", - "origin": "Shaders/ShaderResourceGroups/SceneSrg.azsli", - "isTemplated": true, - "isOptional": false - }, { "file": "autoexec.cfg", "origin": "autoexec.cfg", diff --git a/Templates/MinimalProject/Template/ShaderLib/scenesrg.srgi b/Templates/MinimalProject/Template/ShaderLib/scenesrg.srgi index 38335bfc26..c5024b86ad 100644 --- a/Templates/MinimalProject/Template/ShaderLib/scenesrg.srgi +++ b/Templates/MinimalProject/Template/ShaderLib/scenesrg.srgi @@ -22,6 +22,5 @@ partial ShaderResourceGroup SceneSrg : SRG_PerScene }; #define AZ_COLLECTING_PARTIAL_SRGS -#include -#include +#include #undef AZ_COLLECTING_PARTIAL_SRGS diff --git a/Templates/MinimalProject/Template/Shaders/CommonVS.azsli b/Templates/MinimalProject/Template/Shaders/CommonVS.azsli deleted file mode 100644 index 4c20d85b88..0000000000 --- a/Templates/MinimalProject/Template/Shaders/CommonVS.azsli +++ /dev/null @@ -1,52 +0,0 @@ -// {BEGIN_LICENSE} -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ -// {END_LICENSE} - -#pragma once - -#include -#include -#include - -struct VertexInput -{ - float3 m_position : POSITION; - float3 m_normal : NORMAL; - float4 m_tangent : TANGENT; - float3 m_bitangent : BITANGENT; - float2 m_uv : UV0; -}; - -struct VertexOutput -{ - float4 m_position : SV_Position; - float3 m_normal : NORMAL; - float3 m_tangent : TANGENT; - float3 m_bitangent : BITANGENT; - float2 m_uv : UV0; - float3 m_view : VIEW; -}; - -VertexOutput CommonVS(VertexInput input) -{ - float4x4 objectToWorld = ObjectSrg::GetWorldMatrix(); - float3x3 objectToWorldIT = ObjectSrg::GetWorldMatrixInverseTranspose(); - - VertexOutput output; - float3 worldPosition = mul(objectToWorld, float4(input.m_position, 1)).xyz; - output.m_position = mul(ViewSrg::m_viewProjectionMatrix, float4(worldPosition, 1.0)); - - output.m_uv = input.m_uv; - - output.m_view = worldPosition - ViewSrg::m_worldPosition; - - ConstructTBN(input.m_normal, input.m_tangent, input.m_bitangent, objectToWorld, objectToWorldIT, output.m_normal, output.m_tangent, output.m_bitangent); - - return output; -} diff --git a/Templates/MinimalProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli b/Templates/MinimalProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli deleted file mode 100644 index f6422f8b2f..0000000000 --- a/Templates/MinimalProject/Template/Shaders/ShaderResourceGroups/SceneSrg.azsli +++ /dev/null @@ -1,20 +0,0 @@ -// {BEGIN_LICENSE} -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ -// {END_LICENSE} - -#ifndef AZ_COLLECTING_PARTIAL_SRGS -#error Do not include this file directly. Include the main .srgi file instead. -#endif - -partial ShaderResourceGroup SceneSrg -{ - float m_time; - float m_deltaTime; -} - diff --git a/Templates/MinimalProject/template.json b/Templates/MinimalProject/template.json index 9d9ef730c1..21608e9204 100644 --- a/Templates/MinimalProject/template.json +++ b/Templates/MinimalProject/template.json @@ -490,18 +490,6 @@ "isTemplated": true, "isOptional": false }, - { - "file": "Shaders/CommonVS.azsli", - "origin": "Shaders/CommonVS.azsli", - "isTemplated": true, - "isOptional": false - }, - { - "file": "Shaders/ShaderResourceGroups/SceneSrg.azsli", - "origin": "Shaders/ShaderResourceGroups/SceneSrg.azsli", - "isTemplated": true, - "isOptional": false - }, { "file": "autoexec.cfg", "origin": "autoexec.cfg",