3
0

MaterialAssetTestUtils.cpp 5.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <Material/MaterialAssetTestUtils.h>
  9. #include <Atom/RPI.Reflect/Shader/ShaderAssetCreator.h>
  10. #include <Atom/RPI.Edit/Shader/ShaderVariantAssetCreator.h>
  11. #include <Atom/RHI/Factory.h>
  12. #include <Common/RHI/Stubs.h>
  13. namespace UnitTest
  14. {
  15. void AddMaterialPropertyForSrg(
  16. AZ::RPI::MaterialTypeAssetCreator& materialTypeCreator,
  17. const AZ::Name& propertyName,
  18. AZ::RPI::MaterialPropertyDataType dataType,
  19. const AZ::Name& shaderInputName)
  20. {
  21. using namespace AZ::RPI;
  22. materialTypeCreator.BeginMaterialProperty(propertyName, dataType);
  23. materialTypeCreator.ConnectMaterialPropertyToShaderInput(shaderInputName);
  24. if (dataType == MaterialPropertyDataType::Enum)
  25. {
  26. materialTypeCreator.SetMaterialPropertyEnumNames(AZStd::vector<AZStd::string>({ "Enum0", "Enum1", "Enum2" }));
  27. }
  28. materialTypeCreator.EndMaterialProperty();
  29. }
  30. void AddCommonTestMaterialProperties(AZ::RPI::MaterialTypeAssetCreator& materialTypeCreator, AZStd::string propertyGroupPrefix)
  31. {
  32. using namespace AZ;
  33. using namespace RPI;
  34. using namespace RHI;
  35. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyBool" }, MaterialPropertyDataType::Bool, Name{ "m_bool" });
  36. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyInt" }, MaterialPropertyDataType::Int, Name{ "m_int" });
  37. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyUInt" }, MaterialPropertyDataType::UInt, Name{ "m_uint" });
  38. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyFloat" }, MaterialPropertyDataType::Float, Name{ "m_float" });
  39. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyFloat2" }, MaterialPropertyDataType::Vector2, Name{ "m_float2" });
  40. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyFloat3" }, MaterialPropertyDataType::Vector3, Name{ "m_float3" });
  41. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyFloat4" }, MaterialPropertyDataType::Vector4, Name{ "m_float4" });
  42. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyColor" }, MaterialPropertyDataType::Color, Name{ "m_color" });
  43. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyImage" }, MaterialPropertyDataType::Image, Name{ "m_image" });
  44. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyEnum" }, MaterialPropertyDataType::Enum, Name{ "m_enum" });
  45. AddMaterialPropertyForSrg(materialTypeCreator, Name{ propertyGroupPrefix + "MyAttachmentImage" }, MaterialPropertyDataType::Image, Name{ "m_attachmentImage" });
  46. }
  47. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout> CreateCommonTestMaterialSrgLayout()
  48. {
  49. using namespace AZ;
  50. using namespace RPI;
  51. using namespace RHI;
  52. // Note we specify the shader inputs and material properties in a different order so the indexes don't align
  53. // We also include a couple unused inputs to further make sure shader and material indexes don't align
  54. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout> srgLayout = RHI::ShaderResourceGroupLayout::Create();
  55. srgLayout->SetName(Name("MaterialSrg"));
  56. srgLayout->SetUniqueId(Uuid::CreateRandom().ToString<AZStd::string>()); // Any random string will suffice.
  57. srgLayout->SetBindingSlot(SrgBindingSlot::Material);
  58. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_unused" }, 0, 4, 0, 0});
  59. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_color" }, 4, 16, 0, 0});
  60. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_float" }, 20, 4, 0, 0});
  61. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_int" }, 24, 4, 0, 0});
  62. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_uint" }, 28, 4, 0, 0});
  63. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_float2" }, 32, 8, 0, 0});
  64. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_float3" }, 40, 12, 0, 0});
  65. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_float4" }, 52, 16, 0, 0});
  66. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_enum" }, 68, 4, 0, 0});
  67. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_bool" }, 72, 4, 0, 0});
  68. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_float3x3" }, 76, 44, 0, 0}); // See ConstantsData::SetConstant<Matrix3x3> for packing rules
  69. srgLayout->AddShaderInput(RHI::ShaderInputConstantDescriptor{Name{ "m_float4x4" }, 120, 64, 0, 0});
  70. srgLayout->AddShaderInput(RHI::ShaderInputImageDescriptor{Name{ "m_unusedImage" }, RHI::ShaderInputImageAccess::Read, RHI::ShaderInputImageType::Image2D, 1, 1, 1});
  71. srgLayout->AddShaderInput(RHI::ShaderInputImageDescriptor{Name{ "m_image" }, RHI::ShaderInputImageAccess::Read, RHI::ShaderInputImageType::Image2D, 1, 2, 2});
  72. srgLayout->AddShaderInput(RHI::ShaderInputImageDescriptor{Name{ "m_attachmentImage" }, RHI::ShaderInputImageAccess::Read, RHI::ShaderInputImageType::Image2D, 1, 2, 2});
  73. EXPECT_TRUE(srgLayout->Finalize());
  74. return srgLayout;
  75. }
  76. } // namespace UnitTest