SkinnedMeshFeatureProcessor.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <SkinnedMesh/SkinnedMeshRenderProxy.h>
  10. #include <SkinnedMesh/SkinnedMeshStatsCollector.h>
  11. #include <Atom/Feature/SkinnedMesh/SkinnedMeshFeatureProcessorInterface.h>
  12. #include <Atom/RHI/FrameGraphInterface.h>
  13. #include <Atom/RHI/FrameGraphExecuteContext.h>
  14. #include <Atom/RPI.Public/Culling.h>
  15. #include <Atom/RPI.Public/FeatureProcessor.h>
  16. #include <Atom/Utils/StableDynamicArray.h>
  17. #include <AzCore/base.h>
  18. #include <AtomCore/std/parallel/concurrency_checker.h>
  19. namespace AZ
  20. {
  21. namespace Render
  22. {
  23. class SkinnedMeshComputePass;
  24. class MorphTargetComputePass;
  25. class MeshFeatureProcessor;
  26. class SkinnedMeshFeatureProcessor final
  27. : public SkinnedMeshFeatureProcessorInterface
  28. {
  29. friend class SkinnedMeshStatsCollector;
  30. public:
  31. AZ_CLASS_ALLOCATOR(SkinnedMeshFeatureProcessor, AZ::SystemAllocator)
  32. AZ_RTTI(AZ::Render::SkinnedMeshFeatureProcessor, "{D1F44963-913F-4210-92E1-945FA306BED4}", AZ::Render::SkinnedMeshFeatureProcessorInterface);
  33. AZ_FEATURE_PROCESSOR(SkinnedMeshFeatureProcessor);
  34. static void Reflect(AZ::ReflectContext* context);
  35. SkinnedMeshFeatureProcessor() = default;
  36. virtual ~SkinnedMeshFeatureProcessor() = default;
  37. // FeatureProcessor overrides ...
  38. void Activate() override;
  39. void Deactivate() override;
  40. void Render(const FeatureProcessor::RenderPacket& packet) override;
  41. void OnRenderEnd() override;
  42. // RPI::SceneNotificationBus overrides ...
  43. void OnRenderPipelineChanged(RPI::RenderPipeline* renderPipeline, RPI::SceneNotification::RenderPipelineChangeType changeType) override;
  44. void OnBeginPrepareRender() override;
  45. // SkinnedMeshFeatureProcessorInterface overrides ...
  46. SkinnedMeshHandle AcquireSkinnedMesh(const SkinnedMeshHandleDescriptor& desc) override;
  47. bool ReleaseSkinnedMesh(SkinnedMeshHandle& handle) override;
  48. void SetSkinningMatrices(const SkinnedMeshHandle& handle, const AZStd::vector<float>& data) override;
  49. void SetMorphTargetWeights(const SkinnedMeshHandle& handle, uint32_t lodIndex, const AZStd::vector<float>& weights) override;
  50. void EnableSkinning(const SkinnedMeshHandle& handle, uint32_t lodIndex, uint32_t meshIndex) override;
  51. void DisableSkinning(const SkinnedMeshHandle& handle, uint32_t lodIndex, uint32_t meshIndex) override;
  52. Data::Instance<RPI::Shader> GetSkinningShader() const;
  53. RPI::ShaderOptionGroup CreateSkinningShaderOptionGroup(const SkinnedMeshShaderOptions shaderOptions, SkinnedMeshShaderOptionNotificationBus::Handler& shaderReinitializedHandler);
  54. void OnSkinningShaderReinitialized(const Data::Instance<RPI::Shader> skinningShader);
  55. void SubmitSkinningDispatchItems(const RHI::FrameGraphExecuteContext& context, uint32_t startIndex, uint32_t endIndex);
  56. void SetupSkinningScope(RHI::FrameGraphInterface frameGraph);
  57. Data::Instance<RPI::Shader> GetMorphTargetShader() const;
  58. void SubmitMorphTargetDispatchItems(const RHI::FrameGraphExecuteContext& context, uint32_t startIndex, uint32_t endIndex);
  59. void SetupMorphTargetScope(RHI::FrameGraphInterface frameGraph);
  60. private:
  61. AZ_DISABLE_COPY_MOVE(SkinnedMeshFeatureProcessor);
  62. void InitSkinningAndMorphPass(RPI::RenderPipeline* renderPipeline);
  63. static const char* s_featureProcessorName;
  64. Data::Instance<RPI::Shader> m_skinningShader;
  65. CachedSkinnedMeshShaderOptions m_cachedSkinningShaderOptions;
  66. Data::Instance<RPI::Shader> m_morphTargetShader;
  67. AZStd::concurrency_checker m_renderProxiesChecker;
  68. StableDynamicArray<SkinnedMeshRenderProxy> m_renderProxies;
  69. AZStd::unique_ptr<SkinnedMeshStatsCollector> m_statsCollector;
  70. AZStd::unordered_set<const RHI::DispatchItem*> m_skinningDispatches;
  71. bool m_alreadyCreatedSkinningScopeThisFrame = false;
  72. AZStd::unordered_set<const RHI::DispatchItem*> m_morphTargetDispatches;
  73. bool m_alreadyCreatedMorphTargetScopeThisFrame = false;
  74. AZStd::mutex m_dispatchItemMutex;
  75. };
  76. } // namespace Render
  77. } // namespace AZ