tone_mapper.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**************************************************************************/
  2. /* tone_mapper.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
  32. #include "servers/rendering/renderer_rd/shaders/effects/tonemap.glsl.gen.h"
  33. #include "servers/rendering_server.h"
  34. namespace RendererRD {
  35. class ToneMapper {
  36. private:
  37. enum TonemapMode {
  38. TONEMAP_MODE_NORMAL,
  39. TONEMAP_MODE_BICUBIC_GLOW_FILTER,
  40. TONEMAP_MODE_1D_LUT,
  41. TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT,
  42. TONEMAP_MODE_SUBPASS,
  43. TONEMAP_MODE_SUBPASS_1D_LUT,
  44. TONEMAP_MODE_NORMAL_MULTIVIEW,
  45. TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW,
  46. TONEMAP_MODE_1D_LUT_MULTIVIEW,
  47. TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW,
  48. TONEMAP_MODE_SUBPASS_MULTIVIEW,
  49. TONEMAP_MODE_SUBPASS_1D_LUT_MULTIVIEW,
  50. TONEMAP_MODE_MAX
  51. };
  52. enum {
  53. TONEMAP_FLAG_USE_BCS = (1 << 0),
  54. TONEMAP_FLAG_USE_GLOW = (1 << 1),
  55. TONEMAP_FLAG_USE_AUTO_EXPOSURE = (1 << 2),
  56. TONEMAP_FLAG_USE_COLOR_CORRECTION = (1 << 3),
  57. TONEMAP_FLAG_USE_FXAA = (1 << 4),
  58. TONEMAP_FLAG_USE_8_BIT_DEBANDING = (1 << 5),
  59. TONEMAP_FLAG_USE_10_BIT_DEBANDING = (1 << 6),
  60. TONEMAP_FLAG_CONVERT_TO_SRGB = (1 << 7),
  61. };
  62. struct TonemapPushConstant {
  63. float bcs[3]; // 12 - 12
  64. uint32_t flags; // 4 - 16
  65. float pixel_size[2]; // 8 - 24
  66. uint32_t tonemapper; // 4 - 28
  67. uint32_t pad; // 4 - 32
  68. uint32_t glow_texture_size[2]; // 8 - 40
  69. float glow_intensity; // 4 - 44
  70. float glow_map_strength; // 4 - 48
  71. uint32_t glow_mode; // 4 - 52
  72. float glow_levels[7]; // 28 - 80
  73. float exposure; // 4 - 84
  74. float white; // 4 - 88
  75. float auto_exposure_scale; // 4 - 92
  76. float luminance_multiplier; // 4 - 96
  77. };
  78. /* tonemap actually writes to a framebuffer, which is
  79. * better to do using the raster pipeline rather than
  80. * compute, as that framebuffer might be in different formats
  81. */
  82. struct Tonemap {
  83. TonemapPushConstant push_constant;
  84. TonemapShaderRD shader;
  85. RID shader_version;
  86. PipelineCacheRD pipelines[TONEMAP_MODE_MAX];
  87. } tonemap;
  88. public:
  89. ToneMapper();
  90. ~ToneMapper();
  91. struct TonemapSettings {
  92. bool use_glow = false;
  93. enum GlowMode {
  94. GLOW_MODE_ADD,
  95. GLOW_MODE_SCREEN,
  96. GLOW_MODE_SOFTLIGHT,
  97. GLOW_MODE_REPLACE,
  98. GLOW_MODE_MIX
  99. };
  100. GlowMode glow_mode = GLOW_MODE_ADD;
  101. float glow_intensity = 1.0;
  102. float glow_map_strength = 0.0f;
  103. float glow_levels[7] = { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0 };
  104. Vector2i glow_texture_size;
  105. bool glow_use_bicubic_upscale = false;
  106. RID glow_texture;
  107. RID glow_map;
  108. RS::EnvironmentToneMapper tonemap_mode = RS::ENV_TONE_MAPPER_LINEAR;
  109. float exposure = 1.0;
  110. float white = 1.0;
  111. bool use_auto_exposure = false;
  112. float auto_exposure_scale = 0.5;
  113. RID exposure_texture;
  114. float luminance_multiplier = 1.0;
  115. bool use_bcs = false;
  116. float brightness = 1.0;
  117. float contrast = 1.0;
  118. float saturation = 1.0;
  119. bool use_color_correction = false;
  120. bool use_1d_color_correction = false;
  121. RID color_correction_texture;
  122. bool use_fxaa = false;
  123. enum DebandingMode {
  124. DEBANDING_MODE_DISABLED,
  125. DEBANDING_MODE_8_BIT,
  126. DEBANDING_MODE_10_BIT,
  127. };
  128. DebandingMode debanding_mode = DEBANDING_MODE_DISABLED;
  129. Vector2i texture_size;
  130. uint32_t view_count = 1;
  131. bool convert_to_srgb = false;
  132. };
  133. void tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings);
  134. void tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings);
  135. };
  136. } // namespace RendererRD