tone_mapper.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**************************************************************************/
  2. /* tone_mapper.cpp */
  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. #include "tone_mapper.h"
  31. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  33. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  34. using namespace RendererRD;
  35. ToneMapper::ToneMapper() {
  36. {
  37. // Initialize tonemapper
  38. Vector<String> tonemap_modes;
  39. tonemap_modes.push_back("\n");
  40. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  41. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  42. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  43. tonemap_modes.push_back("\n#define SUBPASS\n");
  44. tonemap_modes.push_back("\n#define SUBPASS\n#define USE_1D_LUT\n");
  45. // multiview versions of our shaders
  46. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n");
  47. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n");
  48. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n");
  49. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  50. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n");
  51. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n#define USE_1D_LUT\n");
  52. tonemap.shader.initialize(tonemap_modes);
  53. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  54. tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false);
  55. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false);
  56. tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false);
  57. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false);
  58. tonemap.shader.set_variant_enabled(TONEMAP_MODE_SUBPASS_MULTIVIEW, false);
  59. tonemap.shader.set_variant_enabled(TONEMAP_MODE_SUBPASS_1D_LUT_MULTIVIEW, false);
  60. }
  61. tonemap.shader_version = tonemap.shader.version_create();
  62. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  63. if (tonemap.shader.is_variant_enabled(i)) {
  64. tonemap.pipelines[i].setup(tonemap.shader.version_get_shader(tonemap.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  65. } else {
  66. tonemap.pipelines[i].clear();
  67. }
  68. }
  69. }
  70. }
  71. ToneMapper::~ToneMapper() {
  72. tonemap.shader.version_free(tonemap.shader_version);
  73. }
  74. void ToneMapper::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  75. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  76. ERR_FAIL_NULL(uniform_set_cache);
  77. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  78. ERR_FAIL_NULL(material_storage);
  79. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  80. tonemap.push_constant.flags |= p_settings.use_bcs ? TONEMAP_FLAG_USE_BCS : 0;
  81. tonemap.push_constant.bcs[0] = p_settings.brightness;
  82. tonemap.push_constant.bcs[1] = p_settings.contrast;
  83. tonemap.push_constant.bcs[2] = p_settings.saturation;
  84. tonemap.push_constant.flags |= p_settings.use_glow ? TONEMAP_FLAG_USE_GLOW : 0;
  85. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  86. tonemap.push_constant.glow_map_strength = p_settings.glow_map_strength;
  87. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  88. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  89. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  90. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  91. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  92. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  93. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  94. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  95. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  96. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  97. int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  98. if (p_settings.use_1d_color_correction) {
  99. mode += 2;
  100. }
  101. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  102. tonemap.push_constant.flags |= p_settings.use_auto_exposure ? TONEMAP_FLAG_USE_AUTO_EXPOSURE : 0;
  103. tonemap.push_constant.exposure = p_settings.exposure;
  104. tonemap.push_constant.white = p_settings.white;
  105. tonemap.push_constant.auto_exposure_scale = p_settings.auto_exposure_scale;
  106. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  107. tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0;
  108. tonemap.push_constant.flags |= p_settings.use_fxaa ? TONEMAP_FLAG_USE_FXAA : 0;
  109. tonemap.push_constant.flags |= p_settings.use_debanding ? TONEMAP_FLAG_USE_DEBANDING : 0;
  110. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  111. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  112. tonemap.push_constant.flags |= p_settings.convert_to_srgb ? TONEMAP_FLAG_CONVERT_TO_SRGB : 0;
  113. if (p_settings.view_count > 1) {
  114. // Use USE_MULTIVIEW versions
  115. mode += 6;
  116. }
  117. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  118. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  119. RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color }));
  120. RD::Uniform u_exposure_texture;
  121. u_exposure_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  122. u_exposure_texture.binding = 0;
  123. u_exposure_texture.append_id(default_sampler);
  124. u_exposure_texture.append_id(p_settings.exposure_texture);
  125. RD::Uniform u_glow_texture;
  126. u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  127. u_glow_texture.binding = 0;
  128. u_glow_texture.append_id(default_mipmap_sampler);
  129. u_glow_texture.append_id(p_settings.glow_texture);
  130. RD::Uniform u_glow_map;
  131. u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  132. u_glow_map.binding = 1;
  133. u_glow_map.append_id(default_mipmap_sampler);
  134. u_glow_map.append_id(p_settings.glow_map);
  135. RD::Uniform u_color_correction_texture;
  136. u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  137. u_color_correction_texture.binding = 0;
  138. u_color_correction_texture.append_id(default_sampler);
  139. u_color_correction_texture.append_id(p_settings.color_correction_texture);
  140. RID shader = tonemap.shader.version_get_shader(tonemap.shader_version, mode);
  141. ERR_FAIL_COND(shader.is_null());
  142. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer);
  143. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer), false, RD::get_singleton()->draw_list_get_current_pass()));
  144. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color), 0);
  145. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_exposure_texture), 1);
  146. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_glow_texture, u_glow_map), 2);
  147. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 3, u_color_correction_texture), 3);
  148. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  149. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  150. RD::get_singleton()->draw_list_end();
  151. }
  152. void ToneMapper::tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings) {
  153. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  154. ERR_FAIL_NULL(uniform_set_cache);
  155. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  156. ERR_FAIL_NULL(material_storage);
  157. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  158. tonemap.push_constant.flags |= p_settings.use_bcs ? TONEMAP_FLAG_USE_BCS : 0;
  159. tonemap.push_constant.bcs[0] = p_settings.brightness;
  160. tonemap.push_constant.bcs[1] = p_settings.contrast;
  161. tonemap.push_constant.bcs[2] = p_settings.saturation;
  162. ERR_FAIL_COND_MSG(p_settings.use_glow, "Glow is not supported when using subpasses.");
  163. tonemap.push_constant.flags |= p_settings.use_glow ? TONEMAP_FLAG_USE_GLOW : 0;
  164. int mode = p_settings.use_1d_color_correction ? TONEMAP_MODE_SUBPASS_1D_LUT : TONEMAP_MODE_SUBPASS;
  165. if (p_settings.view_count > 1) {
  166. // Use USE_MULTIVIEW versions
  167. mode += 6;
  168. }
  169. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  170. tonemap.push_constant.flags |= p_settings.use_auto_exposure ? TONEMAP_FLAG_USE_AUTO_EXPOSURE : 0;
  171. tonemap.push_constant.exposure = p_settings.exposure;
  172. tonemap.push_constant.white = p_settings.white;
  173. tonemap.push_constant.auto_exposure_scale = p_settings.auto_exposure_scale;
  174. tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0;
  175. tonemap.push_constant.flags |= p_settings.use_debanding ? TONEMAP_FLAG_USE_DEBANDING : 0;
  176. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  177. tonemap.push_constant.flags |= p_settings.convert_to_srgb ? TONEMAP_FLAG_CONVERT_TO_SRGB : 0;
  178. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  179. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  180. RD::Uniform u_source_color;
  181. u_source_color.uniform_type = RD::UNIFORM_TYPE_INPUT_ATTACHMENT;
  182. u_source_color.binding = 0;
  183. u_source_color.append_id(p_source_color);
  184. RD::Uniform u_exposure_texture;
  185. u_exposure_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  186. u_exposure_texture.binding = 0;
  187. u_exposure_texture.append_id(default_sampler);
  188. u_exposure_texture.append_id(p_settings.exposure_texture);
  189. RD::Uniform u_glow_texture;
  190. u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  191. u_glow_texture.binding = 0;
  192. u_glow_texture.append_id(default_mipmap_sampler);
  193. u_glow_texture.append_id(p_settings.glow_texture);
  194. RD::Uniform u_glow_map;
  195. u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  196. u_glow_map.binding = 1;
  197. u_glow_map.append_id(default_mipmap_sampler);
  198. u_glow_map.append_id(p_settings.glow_map);
  199. RD::Uniform u_color_correction_texture;
  200. u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  201. u_color_correction_texture.binding = 0;
  202. u_color_correction_texture.append_id(default_sampler);
  203. u_color_correction_texture.append_id(p_settings.color_correction_texture);
  204. RID shader = tonemap.shader.version_get_shader(tonemap.shader_version, mode);
  205. ERR_FAIL_COND(shader.is_null());
  206. RD::get_singleton()->draw_list_bind_render_pipeline(p_subpass_draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_dst_format_id, false, RD::get_singleton()->draw_list_get_current_pass()));
  207. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color), 0);
  208. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 1, u_exposure_texture), 1); // should be set to a default texture, it's ignored
  209. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 2, u_glow_texture, u_glow_map), 2); // should be set to a default texture, it's ignored
  210. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 3, u_color_correction_texture), 3);
  211. RD::get_singleton()->draw_list_set_push_constant(p_subpass_draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  212. RD::get_singleton()->draw_list_draw(p_subpass_draw_list, false, 1u, 3u);
  213. }