BsPostProcessing.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsRendererMaterial.h"
  6. #include "BsParamBlocks.h"
  7. #include "BsRenderTexturePool.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. /** Settings that control the post-process operation. */
  14. struct PostProcessSettings
  15. {
  16. /**
  17. * Determines minimum luminance value in the eye adaptation histogram. In log2 units (-8 = 1/256). In the range
  18. * [-16, 0].
  19. */
  20. float histogramLog2Min = -8.0f;
  21. /**
  22. * Determines maximum luminance value in the eye adaptation histogram. In log2 units (4 = 16). In the range
  23. * [0, 16].
  24. */
  25. float histogramLog2Max = 4.0f;
  26. };
  27. /** Contains per-camera data used by post process effects. */
  28. struct PostProcessInfo
  29. {
  30. PostProcessSettings settings;
  31. SPtr<PooledRenderTexture> downsampledSceneTex;
  32. SPtr<PooledRenderTexture> histogramTex;
  33. SPtr<PooledRenderTexture> eyeAdaptationTex;
  34. };
  35. BS_PARAM_BLOCK_BEGIN(DownsampleParams)
  36. BS_PARAM_BLOCK_ENTRY(Vector2, gInvTexSize)
  37. BS_PARAM_BLOCK_END
  38. /** Shader that downsamples a texture to half its size. */
  39. class DownsampleMat : public RendererMaterial<DownsampleMat>
  40. {
  41. RMAT_DEF("PPDownsample.bsl");
  42. public:
  43. DownsampleMat();
  44. /** Renders the post-process effect with the provided parameters. */
  45. void execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
  46. /** Releases the output render target. */
  47. void release(PostProcessInfo& ppInfo);
  48. /** Returns the render texture where the output will be written. */
  49. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  50. private:
  51. DownsampleParams mParams;
  52. MaterialParamVec2Core mInvTexSize;
  53. MaterialParamTextureCore mInputTexture;
  54. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  55. SPtr<RenderTextureCore> mOutput;
  56. };
  57. BS_PARAM_BLOCK_BEGIN(EyeAdaptHistogramParams)
  58. BS_PARAM_BLOCK_ENTRY(Vector4I, gPixelOffsetAndSize)
  59. BS_PARAM_BLOCK_ENTRY(Vector2, gHistogramParams)
  60. BS_PARAM_BLOCK_ENTRY(Vector2I, gThreadGroupCount)
  61. BS_PARAM_BLOCK_END
  62. /** Shader that creates a luminance histogram used for eye adaptation. */
  63. class EyeAdaptHistogramMat : public RendererMaterial<EyeAdaptHistogramMat>
  64. {
  65. RMAT_DEF("PPEyeAdaptHistogram.bsl");
  66. public:
  67. EyeAdaptHistogramMat();
  68. /** Executes the post-process effect with the provided parameters. */
  69. void execute(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
  70. /** Releases the output render target. */
  71. void release(PostProcessInfo& ppInfo);
  72. /** Returns the render texture where the output was written. */
  73. SPtr<RenderTextureCore> getOutput() const { return mOutput; }
  74. private:
  75. EyeAdaptHistogramParams mParams;
  76. MaterialParamTextureCore mSceneColor;
  77. MaterialParamLoadStoreTextureCore mOutputTex;
  78. POOLED_RENDER_TEXTURE_DESC mOutputDesc;
  79. SPtr<RenderTextureCore> mOutput;
  80. static const INT32 THREAD_GROUP_SIZE_X = 4;
  81. static const INT32 THREAD_GROUP_SIZE_Y = 4;
  82. static const INT32 LOOP_COUNT_X = 8;
  83. static const INT32 LOOP_COUNT_Y = 8;
  84. };
  85. /**
  86. * Renders post-processing effects for the provided render target.
  87. *
  88. * @note Core thread only.
  89. */
  90. class BS_BSRND_EXPORT PostProcessing : public Module<PostProcessing>
  91. {
  92. public:
  93. /** Renders post-processing effects for the provided render target. */
  94. void postProcess(const SPtr<RenderTextureCore>& target, PostProcessInfo& ppInfo);
  95. private:
  96. DownsampleMat mDownsample;
  97. EyeAdaptHistogramMat mEyeAdaptHistogram;
  98. };
  99. /** @} */
  100. }