3
0

EditorStateMeshDrawPacket.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/RPI.Public/Shader/Shader.h>
  10. #include <Atom/RPI.Public/Material/Material.h>
  11. #include <Atom/RPI.Public/Model/ModelLod.h>
  12. #include <Atom/RHI/DrawPacket.h>
  13. #include <Atom/RHI/DrawPacketBuilder.h>
  14. #include <AzCore/Math/Obb.h>
  15. namespace AZ::RPI
  16. {
  17. class Scene;
  18. }
  19. namespace AZ::Render
  20. {
  21. //! Holds and manages an RHI DrawPacket for a specific mesh, and the resources that are needed to build and maintain it.
  22. //! @note This class is based on the meshDrawPacket class and could be paired down even further to leave only the pertinent
  23. //! parts of the interface and implementation.
  24. class EditorStateMeshDrawPacket
  25. {
  26. public:
  27. using ShaderList = AZStd::vector<Data::Instance<RPI::Shader>>;
  28. EditorStateMeshDrawPacket() = default;
  29. EditorStateMeshDrawPacket(
  30. RPI::ModelLod& modelLod,
  31. size_t modelLodMeshIndex,
  32. Data::Instance<RPI::Material> materialOverride,
  33. AZ::Name drawList,
  34. Data::Instance<RPI::ShaderResourceGroup> objectSrg,
  35. const RPI::MaterialModelUvOverrideMap& materialModelUvMap = {});
  36. AZ_DEFAULT_COPY(EditorStateMeshDrawPacket);
  37. AZ_DEFAULT_MOVE(EditorStateMeshDrawPacket);
  38. bool Update(const RPI::Scene& parentScene, bool forceUpdate = false);
  39. const RHI::DrawPacket* GetRHIDrawPacket() const;
  40. void SetStencilRef(uint8_t stencilRef) { m_stencilRef = stencilRef; }
  41. void SetSortKey(RHI::DrawItemSortKey sortKey) { m_sortKey = sortKey; };
  42. bool SetShaderOption(const Name& shaderOptionName, RPI::ShaderOptionValue value);
  43. Data::Instance<RPI::Material> GetMaterial();
  44. private:
  45. bool DoUpdate(const RPI::Scene& parentScene);
  46. RPI::ConstPtr<RHI::DrawPacket> m_drawPacket;
  47. // Note, many of the following items are held locally in the EditorStateMeshDrawPacket solely to keep them resident in memory as long as they are needed
  48. // for the m_drawPacket. RHI::DrawPacket uses raw pointers only, but we use smart pointers here to hold on to the data.
  49. // Maintains references to the shader instances to keep their PSO caches resident (see Shader::Shutdown())
  50. ShaderList m_activeShaders;
  51. // The model that contains the mesh being represented by the DrawPacket
  52. Data::Instance<RPI::ModelLod> m_modelLod;
  53. // The index of the mesh within m_modelLod that is represented by the DrawPacket
  54. size_t m_modelLodMeshIndex;
  55. // The per-object shader resource group
  56. Data::Instance<RPI::ShaderResourceGroup> m_objectSrg;
  57. // We hold ConstPtr<RHI::ShaderResourceGroup> instead of Instance<RPI::ShaderResourceGroup> because the Material class
  58. // does not allow public access to its Instance<RPI::ShaderResourceGroup>.
  59. RPI::ConstPtr<RHI::ShaderResourceGroup> m_materialSrg;
  60. AZStd::fixed_vector<Data::Instance<RPI::ShaderResourceGroup>, RHI::DrawPacketBuilder::DrawItemCountMax> m_perDrawSrgs;
  61. // A reference to the material, used to rebuild the DrawPacket if needed
  62. Data::Instance<RPI::Material> m_material;
  63. // Tracks whether the Material has change since the DrawPacket was last built
  64. RPI::Material::ChangeId m_materialChangeId = RPI::Material::DEFAULT_CHANGE_ID;
  65. // Set the sort key for the draw packet
  66. RHI::DrawItemSortKey m_sortKey = 0;
  67. // Set the stencil value for this draw packet
  68. uint8_t m_stencilRef = 0;
  69. //! A map matches the index of UV names of this material to the custom names from the model.
  70. RPI::MaterialModelUvOverrideMap m_materialModelUvMap;
  71. //! List of shader options set for this specific draw packet
  72. typedef AZStd::pair<Name, RPI::ShaderOptionValue> ShaderOptionPair;
  73. typedef AZStd::vector<ShaderOptionPair> ShaderOptionVector;
  74. ShaderOptionVector m_shaderOptions;
  75. RHI::DrawListTag m_drawListTag;
  76. };
  77. } // namespace AZ::Render