3
0

AlphaCoverage.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <Processing/ImageObjectImpl.h>
  9. #include <Processing/ImageConvert.h>
  10. #include <Processing/PixelFormatInfo.h>
  11. #include <Converters/PixelOperation.h>
  12. ///////////////////////////////////////////////////////////////////////////////////
  13. //functions for maintaining alpha coverage.
  14. namespace ImageProcessingAtom
  15. {
  16. void CImageObject::TransferAlphaCoverage(const TextureSettings* textureSetting, const IImageObjectPtr srcImg)
  17. {
  18. EPixelFormat srcFmt = srcImg->GetPixelFormat();
  19. //both this image and src image need to be uncompressed
  20. if (!CPixelFormats::GetInstance().IsPixelFormatUncompressed(m_pixelFormat)
  21. || !CPixelFormats::GetInstance().IsPixelFormatUncompressed(srcFmt))
  22. {
  23. AZ_Assert(false, "Both source image and dest image need to be uncompressed");
  24. return;
  25. }
  26. const float fAlphaRef = 0.5f; // Seems to give good overall results
  27. const float fDesiredAlphaCoverage = srcImg->ComputeAlphaCoverage(0, fAlphaRef);
  28. //create pixel operation function
  29. IPixelOperationPtr pixelOp = CreatePixelOperation(m_pixelFormat);
  30. //get count of bytes per pixel
  31. AZ::u32 pixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(m_pixelFormat)->bitsPerBlock / 8;
  32. for (uint32 mip = 0; mip < GetMipCount(); mip++)
  33. {
  34. const float fAlphaOffset = textureSetting->ComputeMIPAlphaOffset(mip);
  35. const float fAlphaScale = ComputeAlphaCoverageScaleFactor(mip, fDesiredAlphaCoverage, fAlphaRef);
  36. AZ::u8* pixelBuf = m_mips[mip]->m_pData;
  37. const AZ::u32 pixelCount = GetPixelCount(mip);
  38. for (AZ::u32 i = 0; i < pixelCount; ++i, pixelBuf += pixelBytes)
  39. {
  40. float r, g, b, a;
  41. pixelOp->GetRGBA(pixelBuf, r, g, b, a);
  42. a = AZ::GetMin(a * fAlphaScale + fAlphaOffset, 1.0f);
  43. pixelOp->SetRGBA(pixelBuf, r, g, b, a);
  44. }
  45. }
  46. }
  47. float CImageObject::ComputeAlphaCoverageScaleFactor(AZ::u32 mip, float fDesiredCoverage, float fAlphaRef) const
  48. {
  49. float minAlphaRef = 0.0f;
  50. float maxAlphaRef = 1.0f;
  51. float midAlphaRef = 0.5f;
  52. // Find best alpha test reference value using a binary search
  53. for (int i = 0; i < 10; i++)
  54. {
  55. const float currentCoverage = ComputeAlphaCoverage(mip, midAlphaRef);
  56. if (currentCoverage > fDesiredCoverage)
  57. {
  58. minAlphaRef = midAlphaRef;
  59. }
  60. else if (currentCoverage < fDesiredCoverage)
  61. {
  62. maxAlphaRef = midAlphaRef;
  63. }
  64. else
  65. {
  66. break;
  67. }
  68. midAlphaRef = (minAlphaRef + maxAlphaRef) * 0.5f;
  69. }
  70. return fAlphaRef / midAlphaRef;
  71. }
  72. float CImageObject::ComputeAlphaCoverage(AZ::u32 mip, float fAlphaRef) const
  73. {
  74. //This function only works with uncompressed image
  75. if (!CPixelFormats::GetInstance().IsPixelFormatUncompressed(m_pixelFormat))
  76. {
  77. AZ_Assert(false, "This image need to be uncompressed");
  78. return 0;
  79. }
  80. uint32 coverage = 0;
  81. //create pixel operation function
  82. IPixelOperationPtr pixelOp = CreatePixelOperation(m_pixelFormat);
  83. //get count of bytes per pixel
  84. AZ::u32 pixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(m_pixelFormat)->bitsPerBlock / 8;
  85. AZ::u8* pixelBuf = m_mips[mip]->m_pData;
  86. const AZ::u32 pixelCount = GetPixelCount(mip);
  87. for (AZ::u32 i = 0; i < pixelCount; ++i, pixelBuf += pixelBytes)
  88. {
  89. float r, g, b, a;
  90. pixelOp->GetRGBA(pixelBuf, r, g, b, a);
  91. coverage += a > fAlphaRef;
  92. }
  93. return (float)coverage / (float)(pixelCount);
  94. }
  95. } // namespace ImageProcessingAtom