3
0

DynamicPrimitiveProcessor.h 7.1 KB

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