ImageBasedLightFeatureProcessor.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <ImageBasedLights/ImageBasedLightFeatureProcessor.h>
  9. #include <Atom/RPI.Public/Scene.h>
  10. #include <Atom/RPI.Public/RPIUtils.h>
  11. #include <Atom/RPI.Public/Image/StreamingImage.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. void ImageBasedLightFeatureProcessor::Reflect(ReflectContext* context)
  17. {
  18. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  19. {
  20. serializeContext
  21. ->Class<ImageBasedLightFeatureProcessor, FeatureProcessor>()
  22. ->Version(0);
  23. }
  24. }
  25. void ImageBasedLightFeatureProcessor::Activate()
  26. {
  27. m_sceneSrg = GetParentScene()->GetShaderResourceGroup();
  28. // Load default specular and diffuse cubemaps
  29. // These are assigned when Global IBL is disabled or removed from the scene to prevent a Vulkan TDR.
  30. // [GFX-TODO][ATOM-4181] This can be removed after Vulkan is changed to automatically handle this issue.
  31. LoadDefaultCubeMaps();
  32. }
  33. void ImageBasedLightFeatureProcessor::Deactivate()
  34. {
  35. m_iblOrientationConstantIndex.Reset();
  36. m_iblExposureConstantIndex.Reset();
  37. m_diffuseEnvMapIndex.Reset();
  38. m_specularEnvMapIndex.Reset();
  39. m_sceneSrg = {};
  40. }
  41. void ImageBasedLightFeatureProcessor::Simulate(const FeatureProcessor::SimulatePacket& packet)
  42. {
  43. AZ_PROFILE_SCOPE(RPI, "ImageBasedLightFeatureProcessor: Simulate");
  44. AZ_UNUSED(packet);
  45. m_sceneSrg->SetImage(m_specularEnvMapIndex, m_specular);
  46. m_sceneSrg->SetImage(m_diffuseEnvMapIndex, m_diffuse);
  47. m_sceneSrg->SetConstant(m_iblExposureConstantIndex, m_exposure);
  48. m_sceneSrg->SetConstant(m_iblOrientationConstantIndex, m_orientation);
  49. }
  50. void ImageBasedLightFeatureProcessor::SetSpecularImage(const Data::Asset<RPI::StreamingImageAsset>& imageAsset)
  51. {
  52. m_specular = GetInstanceForImage(imageAsset, m_defaultSpecularImage);
  53. }
  54. void ImageBasedLightFeatureProcessor::SetDiffuseImage(const Data::Asset<RPI::StreamingImageAsset>& imageAsset)
  55. {
  56. m_diffuse = GetInstanceForImage(imageAsset, m_defaultDiffuseImage);
  57. }
  58. void ImageBasedLightFeatureProcessor::SetExposure(float exposure)
  59. {
  60. m_exposure = exposure;
  61. }
  62. void ImageBasedLightFeatureProcessor::SetOrientation(const Quaternion& orientation)
  63. {
  64. m_orientation = orientation;
  65. }
  66. void ImageBasedLightFeatureProcessor::Reset()
  67. {
  68. m_specular = m_defaultSpecularImage;
  69. m_diffuse = m_defaultDiffuseImage;
  70. m_exposure = 0;
  71. }
  72. void ImageBasedLightFeatureProcessor::LoadDefaultCubeMaps()
  73. {
  74. const constexpr char* DefaultSpecularCubeMapPath = "textures/default/default_iblglobalcm_iblspecular.dds.streamingimage";
  75. const constexpr char* DefaultDiffuseCubeMapPath = "textures/default/default_iblglobalcm_ibldiffuse.dds.streamingimage";
  76. m_defaultSpecularImage = RPI::LoadStreamingTexture(DefaultSpecularCubeMapPath);
  77. AZ_Assert(m_defaultSpecularImage, "Failed to load default specular cubemap");
  78. m_defaultDiffuseImage = RPI::LoadStreamingTexture(DefaultDiffuseCubeMapPath);
  79. AZ_Assert(m_defaultDiffuseImage, "Failed to load default diffuse cubemap");
  80. }
  81. Data::Instance<RPI::Image> ImageBasedLightFeatureProcessor::GetInstanceForImage(const Data::Asset<RPI::StreamingImageAsset>& imageAsset, const Data::Instance<RPI::Image>& defaultImage)
  82. {
  83. Data::Instance<RPI::Image> image;
  84. if (imageAsset.GetId().IsValid())
  85. {
  86. image = RPI::StreamingImage::FindOrCreate(imageAsset);
  87. if (image && !ValidateIsCubemap(image))
  88. {
  89. image = defaultImage;
  90. }
  91. }
  92. return image;
  93. }
  94. bool ImageBasedLightFeatureProcessor::ValidateIsCubemap(Data::Instance<RPI::Image> image)
  95. {
  96. const RHI::ImageDescriptor& desc = image->GetRHIImage()->GetDescriptor();
  97. return (desc.m_isCubemap || desc.m_arraySize == 6);
  98. }
  99. } // namespace Render
  100. } // namespace AZ