3
0

LuminanceHistogramGeneratorPass.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <PostProcessing/LuminanceHistogramGeneratorPass.h>
  9. #include <Atom/RHI/Factory.h>
  10. #include <Atom/RHI/FrameGraphAttachmentInterface.h>
  11. #include <Atom/RHI/FrameGraphInterface.h>
  12. #include <Atom/RHI/DevicePipelineState.h>
  13. #include <Atom/RPI.Public/Base.h>
  14. #include <Atom/RPI.Public/Pass/PassUtils.h>
  15. #include <Atom/RPI.Public/RenderPipeline.h>
  16. #include <Atom/RHI/RHISystemInterface.h>
  17. #include <Atom/RPI.Public/RPIUtils.h>
  18. #include <Atom/RPI.Public/Scene.h>
  19. #include <Atom/RPI.Public/View.h>
  20. #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
  21. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  22. #include <PostProcess/PostProcessFeatureProcessor.h>
  23. #include <PostProcess/ExposureControl/ExposureControlSettings.h>
  24. namespace AZ
  25. {
  26. namespace Render
  27. {
  28. // This must match the value in LuminanceHistogramCommon.azsli
  29. static const size_t NumHistogramBins = 128;
  30. RPI::Ptr<LuminanceHistogramGeneratorPass> LuminanceHistogramGeneratorPass::Create(const RPI::PassDescriptor& descriptor)
  31. {
  32. RPI::Ptr<LuminanceHistogramGeneratorPass> pass = aznew LuminanceHistogramGeneratorPass(descriptor);
  33. return AZStd::move(pass);
  34. }
  35. LuminanceHistogramGeneratorPass::LuminanceHistogramGeneratorPass(const RPI::PassDescriptor& descriptor)
  36. : RPI::ComputePass(descriptor)
  37. {
  38. }
  39. void LuminanceHistogramGeneratorPass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)
  40. {
  41. RHI::CommandList* commandList = context.GetCommandList();
  42. SetSrgsForDispatch(context);
  43. const RHI::Size resolution = GetColorBufferResolution();
  44. SetTargetThreadCounts(resolution.m_width, resolution.m_height, 1);
  45. commandList->Submit(m_dispatchItem.GetDeviceDispatchItem(context.GetDeviceIndex()));
  46. }
  47. void LuminanceHistogramGeneratorPass::CreateHistogramBuffer()
  48. {
  49. RPI::CommonBufferDescriptor desc;
  50. desc.m_poolType = RPI::CommonBufferPoolType::ReadWrite;
  51. desc.m_bufferName = "LuminanceHistogramBuffer";
  52. desc.m_elementSize = sizeof(uint32_t);
  53. desc.m_byteCount = NumHistogramBins * sizeof(uint32_t);
  54. desc.m_elementFormat = RHI::Format::Unknown;
  55. m_histogram = RPI::BufferSystemInterface::Get()->CreateBufferFromCommonPool(desc);
  56. AZ_Assert(m_histogram != nullptr, "Unable to allocate buffer");
  57. }
  58. AZ::RHI::Size LuminanceHistogramGeneratorPass::GetColorBufferResolution()
  59. {
  60. const auto& binding = GetInputBinding(0);
  61. AZ_Assert(binding.m_name == AZ::Name("ColorInput"), "ColorInput was expected to be the first input");
  62. const RPI::PassAttachment* colorBuffer = binding.GetAttachment().get();
  63. return colorBuffer->m_descriptor.m_image.m_size;
  64. }
  65. void LuminanceHistogramGeneratorPass::BuildInternal()
  66. {
  67. CreateHistogramBuffer();
  68. AttachHistogramBuffer();
  69. }
  70. void LuminanceHistogramGeneratorPass::AttachHistogramBuffer()
  71. {
  72. if (m_histogram != nullptr)
  73. {
  74. AttachBufferToSlot(Name("Output"), m_histogram);
  75. }
  76. }
  77. } // namespace Render
  78. } // namespace AZ