3
0

DynamicPrimitiveProcessor.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <Atom/RHI/IndexBufferView.h>
  10. #include <Atom/RHI/StreamBufferView.h>
  11. #include <Atom/RHI/DevicePipelineState.h>
  12. #include <Atom/RHI/DrawList.h>
  13. #include <Atom/RHI.Reflect/InputStreamLayout.h>
  14. #include <Atom/RHI.Reflect/Limits.h>
  15. #include <Atom/RPI.Public/FeatureProcessor.h>
  16. #include <Atom/RPI.Public/PipelineState.h>
  17. #include <AzCore/std/containers/fixed_vector.h>
  18. #include "AuxGeomBase.h"
  19. namespace AZ
  20. {
  21. namespace RHI
  22. {
  23. class DrawPacketBuilder;
  24. }
  25. namespace RPI
  26. {
  27. class Scene;
  28. class Shader;
  29. class ShaderVariant;
  30. class ShaderOptionGroup;
  31. class ShaderResourceGroup;
  32. }
  33. namespace Render
  34. {
  35. /**
  36. * DynamicPrimitiveProcessor does the feature processor work for dynamic primitives.
  37. * That is, primitives drawn using dynamic buffers for verts and indices.
  38. * This class, manages the dynamic RHI buffers, the stream layout, the shader asset
  39. * and the pipeline states.
  40. */
  41. class DynamicPrimitiveProcessor final
  42. {
  43. public:
  44. AZ_TYPE_INFO(DynamicPrimitiveProcessor, "{30391207-E4CB-4FCC-B407-05E361CF6815}");
  45. AZ_CLASS_ALLOCATOR(DynamicPrimitiveProcessor, AZ::SystemAllocator);
  46. DynamicPrimitiveProcessor() = default;
  47. ~DynamicPrimitiveProcessor() = default;
  48. //! Initialize the DynamicPrimitiveProcessor and all its buffers, shaders, stream layouts etc
  49. bool Initialize(const AZ::RPI::Scene* scene);
  50. //! Releases the DynamicPrimitiveProcessor and all primitive geometry buffers
  51. void Release();
  52. //! Process the list of primitives in the buffer data and add them to the views in the feature processor packet
  53. void ProcessDynamicPrimitives(const AuxGeomBufferData* bufferData, const RPI::FeatureProcessor::RenderPacket& fpPacket);
  54. //! Prepare frame.
  55. void PrepareFrame();
  56. //! Do any cleanup after current frame is rendered.
  57. void FrameEnd();
  58. //! Notify this DynamicPrimitiveProcessor to update its pipeline states
  59. void SetUpdatePipelineStates();
  60. private: // types
  61. using StreamBufferViewsForAllStreams = AZStd::fixed_vector<AZ::RHI::StreamBufferView, AZ::RHI::Limits::Pipeline::StreamCountMax>;
  62. struct DynamicBufferGroup
  63. {
  64. //! The view into the index buffer
  65. AZ::RHI::IndexBufferView m_indexBufferView;
  66. //! The stream views into the vertex buffer (we only have one in our case)
  67. StreamBufferViewsForAllStreams m_streamBufferViews;
  68. };
  69. using DrawPackets = AZStd::vector<AZ::RHI::ConstPtr<RHI::DrawPacket>>;
  70. struct ShaderData
  71. {
  72. RHI::Ptr<RHI::ShaderResourceGroupLayout> m_perDrawSrgLayout;
  73. Data::Instance<RPI::ShaderResourceGroup> m_defaultSRG; // default SRG for draws not overriding the view projection matrix
  74. AZ::RHI::DrawListTag m_drawListTag; // The draw list tag from our shader variant (determines which views primitives are in and which pass)
  75. AZ::RHI::ShaderInputNameIndex m_viewProjectionOverrideIndex = "m_viewProjectionOverride";
  76. AZ::RHI::ShaderInputNameIndex m_pointSizeIndex = "m_pointSize";
  77. };
  78. struct PipelineStateOptions
  79. {
  80. AuxGeomShapePerpectiveType m_perpectiveType = PerspectiveType_ViewProjection;
  81. AuxGeomBlendMode m_blendMode = BlendMode_Alpha;
  82. AuxGeomPrimitiveType m_primitiveType = PrimitiveType_TriangleList;
  83. AuxGeomDepthReadType m_depthReadType = DepthRead_On;
  84. AuxGeomDepthWriteType m_depthWriteType = DepthWrite_Off;
  85. AuxGeomFaceCullMode m_faceCullMode = FaceCull_Back;
  86. };
  87. private: // functions
  88. //!Uses the given drawPacketBuilder to build a draw packet with given data and returns it
  89. RHI::ConstPtr<RHI::DrawPacket> BuildDrawPacketForDynamicPrimitive(
  90. DynamicBufferGroup& group,
  91. const RPI::Ptr<RPI::PipelineStateForDraw>& pipelineState,
  92. Data::Instance<RPI::ShaderResourceGroup> srg,
  93. uint32_t indexCount,
  94. uint32_t indexOffset,
  95. RHI::DrawPacketBuilder& drawPacketBuilder,
  96. RHI::DrawItemSortKey sortKey = 0);
  97. // Update a dynamic index buffer, given the data from draw requests
  98. bool UpdateIndexBuffer(const IndexBuffer& indexSource, DynamicBufferGroup& group);
  99. // Update a dynamic vertex buffer, given the data from draw requests
  100. bool UpdateVertexBuffer(const VertexBuffer& source, DynamicBufferGroup& group);
  101. // Validate the given stream buffer views for the layout used for the given prim type (uses isValidated flags to see if necessary)
  102. void ValidateStreamBufferViews(StreamBufferViewsForAllStreams& streamBufferViews, bool* isValidated, int primitiveType);
  103. // Sets up stream layout used for dynamic primitive shader for the given toplogy
  104. void SetupInputStreamLayout(RHI::InputStreamLayout& inputStreamLayout, RHI::PrimitiveTopology topology);
  105. // Loads the shader used for dynamic primitives
  106. void InitShader();
  107. void InitPipelineState(const PipelineStateOptions& pipelineStateOptions);
  108. RPI::Ptr<RPI::PipelineStateForDraw>& GetPipelineState(const PipelineStateOptions& pipelineStateOptions);
  109. private: // data
  110. // We have a layout for each prim type because the layout contains the topology type
  111. RHI::InputStreamLayout m_inputStreamLayout[PrimitiveType_Count];
  112. // The pipeline state for processing opaque dynamic primitives
  113. RPI::Ptr<RPI::PipelineStateForDraw> m_pipelineStates[PerspectiveType_Count][BlendMode_Count][PrimitiveType_Count][DepthRead_Count][DepthWrite_Count][FaceCull_Count];
  114. AZStd::list<RPI::Ptr<RPI::PipelineStateForDraw>*> m_createdPipelineStates;
  115. ShaderData m_shaderData;
  116. // Buffers for all primitives
  117. DynamicBufferGroup m_primitiveBuffers;
  118. // Flags to see if stream buffer views have been validated for a prim type's layout
  119. bool m_streamBufferViewsValidatedForLayout[PrimitiveType_Count];
  120. // We keep all the draw packets around until the next time Process is called
  121. DrawPackets m_drawPackets;
  122. // We keep all the srg's around until the next time process is called
  123. AZStd::vector<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>> m_processSrgs;
  124. Data::Instance<AZ::RPI::Shader> m_shader;
  125. const AZ::RPI::Scene* m_scene = nullptr;
  126. bool m_needUpdatePipelineStates = false;
  127. };
  128. } // namespace Render
  129. } // namespace AZ