2
0

tone_mapper.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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(bool p_use_mobile_version) {
  36. using_mobile_version = p_use_mobile_version;
  37. if (using_mobile_version) {
  38. // Initialize tonemapper
  39. Vector<String> tonemap_modes;
  40. tonemap_modes.push_back("\n");
  41. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  42. tonemap_modes.push_back("\n#define SUBPASS\n");
  43. tonemap_modes.push_back("\n#define SUBPASS\n#define USE_1D_LUT\n");
  44. // multiview versions of our shaders
  45. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n");
  46. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n");
  47. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n");
  48. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n#define USE_1D_LUT\n");
  49. tonemap_mobile.shader.initialize(tonemap_modes);
  50. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  51. tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_NORMAL_MULTIVIEW, false);
  52. tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_1D_LUT_MULTIVIEW, false);
  53. tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_SUBPASS_MULTIVIEW, false);
  54. tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT_MULTIVIEW, false);
  55. }
  56. tonemap_mobile.shader_version = tonemap_mobile.shader.version_create();
  57. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  58. if (tonemap_mobile.shader.is_variant_enabled(i)) {
  59. tonemap_mobile.pipelines[i].setup(tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  60. } else {
  61. tonemap_mobile.pipelines[i].clear();
  62. }
  63. }
  64. } else {
  65. // Initialize tonemapper
  66. Vector<String> tonemap_modes;
  67. tonemap_modes.push_back("\n");
  68. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  69. tonemap_modes.push_back("\n#define USE_1D_LUT\n");
  70. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  71. // multiview versions of our shaders
  72. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n");
  73. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n");
  74. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n");
  75. tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
  76. tonemap.shader.initialize(tonemap_modes);
  77. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  78. tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false);
  79. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false);
  80. tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false);
  81. tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false);
  82. }
  83. tonemap.shader_version = tonemap.shader.version_create();
  84. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  85. if (tonemap.shader.is_variant_enabled(i)) {
  86. 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);
  87. } else {
  88. tonemap.pipelines[i].clear();
  89. }
  90. }
  91. }
  92. }
  93. ToneMapper::~ToneMapper() {
  94. if (using_mobile_version) {
  95. tonemap_mobile.shader.version_free(tonemap_mobile.shader_version);
  96. } else {
  97. tonemap.shader.version_free(tonemap.shader_version);
  98. }
  99. }
  100. void ToneMapper::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  101. ERR_FAIL_COND_MSG(using_mobile_version, "Can't use the non mobile version of the tonemapper with the Mobile renderer.");
  102. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  103. ERR_FAIL_NULL(uniform_set_cache);
  104. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  105. ERR_FAIL_NULL(material_storage);
  106. memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
  107. tonemap.push_constant.flags |= p_settings.use_bcs ? TONEMAP_FLAG_USE_BCS : 0;
  108. tonemap.push_constant.bcs[0] = p_settings.brightness;
  109. tonemap.push_constant.bcs[1] = p_settings.contrast;
  110. tonemap.push_constant.bcs[2] = p_settings.saturation;
  111. tonemap.push_constant.flags |= p_settings.use_glow ? TONEMAP_FLAG_USE_GLOW : 0;
  112. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  113. tonemap.push_constant.glow_map_strength = p_settings.glow_map_strength;
  114. tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
  115. tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
  116. tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
  117. tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
  118. tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
  119. tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
  120. tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
  121. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  122. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  123. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  124. int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  125. if (p_settings.use_1d_color_correction) {
  126. mode += 2;
  127. }
  128. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  129. tonemap.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0];
  130. tonemap.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1];
  131. tonemap.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2];
  132. tonemap.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3];
  133. tonemap.push_constant.flags |= p_settings.use_auto_exposure ? TONEMAP_FLAG_USE_AUTO_EXPOSURE : 0;
  134. tonemap.push_constant.exposure = p_settings.exposure;
  135. tonemap.push_constant.white = p_settings.white;
  136. tonemap.push_constant.auto_exposure_scale = p_settings.auto_exposure_scale;
  137. tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  138. tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0;
  139. tonemap.push_constant.flags |= p_settings.use_fxaa ? TONEMAP_FLAG_USE_FXAA : 0;
  140. if (p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT) {
  141. tonemap.push_constant.flags |= TONEMAP_FLAG_USE_8_BIT_DEBANDING;
  142. }
  143. tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
  144. tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
  145. tonemap.push_constant.flags |= p_settings.convert_to_srgb ? TONEMAP_FLAG_CONVERT_TO_SRGB : 0;
  146. if (p_settings.view_count > 1) {
  147. // Use USE_MULTIVIEW versions
  148. mode += 4;
  149. }
  150. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  151. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  152. RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color }));
  153. RD::Uniform u_exposure_texture;
  154. u_exposure_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  155. u_exposure_texture.binding = 0;
  156. u_exposure_texture.append_id(default_sampler);
  157. u_exposure_texture.append_id(p_settings.exposure_texture);
  158. RD::Uniform u_glow_texture;
  159. u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  160. u_glow_texture.binding = 0;
  161. u_glow_texture.append_id(default_mipmap_sampler);
  162. u_glow_texture.append_id(p_settings.glow_texture);
  163. RD::Uniform u_glow_map;
  164. u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  165. u_glow_map.binding = 1;
  166. u_glow_map.append_id(default_mipmap_sampler);
  167. u_glow_map.append_id(p_settings.glow_map);
  168. RD::Uniform u_color_correction_texture;
  169. u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  170. u_color_correction_texture.binding = 0;
  171. u_color_correction_texture.append_id(default_sampler);
  172. u_color_correction_texture.append_id(p_settings.color_correction_texture);
  173. RID shader = tonemap.shader.version_get_shader(tonemap.shader_version, mode);
  174. ERR_FAIL_COND(shader.is_null());
  175. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer);
  176. 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()));
  177. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color), 0);
  178. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_exposure_texture), 1);
  179. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_glow_texture, u_glow_map), 2);
  180. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 3, u_color_correction_texture), 3);
  181. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  182. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  183. RD::get_singleton()->draw_list_end();
  184. }
  185. void ToneMapper::tonemapper_mobile(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  186. ERR_FAIL_COND_MSG(!using_mobile_version, "Can't use the mobile version of the tonemapper with the clustered renderer.");
  187. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  188. ERR_FAIL_NULL(uniform_set_cache);
  189. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  190. ERR_FAIL_NULL(material_storage);
  191. memset(&tonemap_mobile.push_constant, 0, sizeof(TonemapPushConstantMobile));
  192. tonemap_mobile.push_constant.bcs[0] = p_settings.brightness;
  193. tonemap_mobile.push_constant.bcs[1] = p_settings.contrast;
  194. tonemap_mobile.push_constant.bcs[2] = p_settings.saturation;
  195. tonemap_mobile.push_constant.src_pixel_size[0] = 1.0 / p_settings.texture_size.x;
  196. tonemap_mobile.push_constant.src_pixel_size[1] = 1.0 / p_settings.texture_size.y;
  197. tonemap_mobile.push_constant.dest_pixel_size[0] = 1.0 / p_settings.dest_texture_size.x;
  198. tonemap_mobile.push_constant.dest_pixel_size[1] = 1.0 / p_settings.dest_texture_size.y;
  199. tonemap_mobile.push_constant.glow_intensity = p_settings.glow_intensity;
  200. tonemap_mobile.push_constant.glow_map_strength = p_settings.glow_map_strength;
  201. tonemap_mobile.push_constant.exposure = p_settings.exposure;
  202. tonemap_mobile.push_constant.white = p_settings.white;
  203. tonemap_mobile.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  204. tonemap_mobile.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0];
  205. tonemap_mobile.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1];
  206. tonemap_mobile.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2];
  207. tonemap_mobile.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3];
  208. uint32_t spec_constant = 0;
  209. spec_constant |= p_settings.use_bcs ? TONEMAP_MOBILE_FLAG_USE_BCS : 0;
  210. spec_constant |= p_settings.use_glow ? TONEMAP_MOBILE_FLAG_USE_GLOW : 0;
  211. spec_constant |= p_settings.glow_map_strength > 0.01 ? TONEMAP_MOBILE_FLAG_USE_GLOW_MAP : 0;
  212. spec_constant |= p_settings.use_color_correction ? TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION : 0;
  213. spec_constant |= p_settings.use_fxaa ? TONEMAP_MOBILE_FLAG_USE_FXAA : 0;
  214. spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT ? TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING : 0;
  215. spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_10_BIT ? TONEMAP_MOBILE_FLAG_USE_10_BIT_DEBANDING : 0;
  216. spec_constant |= p_settings.convert_to_srgb ? TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB : 0;
  217. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_LINEAR ? TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR : 0;
  218. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_REINHARD ? TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD : 0;
  219. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_FILMIC ? TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC : 0;
  220. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_ACES ? TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES : 0;
  221. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_AGX ? TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX : 0;
  222. spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_ADDITIVE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD : 0;
  223. spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN : 0;
  224. spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT : 0;
  225. spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE : 0;
  226. spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX : 0;
  227. int mode = p_settings.use_1d_color_correction ? TONEMAP_MOBILE_MODE_1D_LUT : TONEMAP_MOBILE_MODE_NORMAL;
  228. if (p_settings.view_count > 1) {
  229. // Use USE_MULTIVIEW versions
  230. mode += 4;
  231. }
  232. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  233. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  234. RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color }));
  235. RD::Uniform u_glow_texture;
  236. u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  237. u_glow_texture.binding = 1;
  238. u_glow_texture.append_id(default_mipmap_sampler);
  239. u_glow_texture.append_id(p_settings.glow_texture);
  240. RD::Uniform u_glow_map;
  241. u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  242. u_glow_map.binding = 2;
  243. u_glow_map.append_id(default_mipmap_sampler);
  244. u_glow_map.append_id(p_settings.glow_map);
  245. RD::Uniform u_color_correction_texture;
  246. u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  247. u_color_correction_texture.binding = 3;
  248. u_color_correction_texture.append_id(default_sampler);
  249. u_color_correction_texture.append_id(p_settings.color_correction_texture);
  250. RID shader = tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, mode);
  251. ERR_FAIL_COND(shader.is_null());
  252. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer);
  253. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap_mobile.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(), spec_constant));
  254. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color, u_glow_texture, u_glow_map, u_color_correction_texture), 0);
  255. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap_mobile.push_constant, sizeof(TonemapPushConstantMobile));
  256. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  257. RD::get_singleton()->draw_list_end();
  258. }
  259. void ToneMapper::tonemapper_subpass(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings) {
  260. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  261. ERR_FAIL_NULL(uniform_set_cache);
  262. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  263. ERR_FAIL_NULL(material_storage);
  264. ERR_FAIL_COND_MSG(p_settings.use_glow, "Glow is not supported when using subpasses.");
  265. memset(&tonemap_mobile.push_constant, 0, sizeof(TonemapPushConstantMobile));
  266. tonemap_mobile.push_constant.bcs[0] = p_settings.brightness;
  267. tonemap_mobile.push_constant.bcs[1] = p_settings.contrast;
  268. tonemap_mobile.push_constant.bcs[2] = p_settings.saturation;
  269. tonemap_mobile.push_constant.src_pixel_size[0] = 1.0 / p_settings.texture_size.x;
  270. tonemap_mobile.push_constant.src_pixel_size[1] = 1.0 / p_settings.texture_size.y;
  271. tonemap_mobile.push_constant.glow_intensity = p_settings.glow_intensity;
  272. tonemap_mobile.push_constant.glow_map_strength = p_settings.glow_map_strength;
  273. tonemap_mobile.push_constant.exposure = p_settings.exposure;
  274. tonemap_mobile.push_constant.white = p_settings.white;
  275. tonemap_mobile.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
  276. tonemap_mobile.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0];
  277. tonemap_mobile.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1];
  278. tonemap_mobile.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2];
  279. tonemap_mobile.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3];
  280. uint32_t spec_constant = TONEMAP_MOBILE_ADRENO_BUG;
  281. spec_constant |= p_settings.use_bcs ? TONEMAP_MOBILE_FLAG_USE_BCS : 0;
  282. //spec_constant |= p_settings.use_glow ? TONEMAP_MOBILE_FLAG_USE_GLOW : 0;
  283. //spec_constant |= p_settings.glow_map_strength > 0.01 ? TONEMAP_MOBILE_FLAG_USE_GLOW_MAP : 0;
  284. //spec_constant |= p_settings.use_color_correction ? TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION : 0;
  285. //spec_constant |= p_settings.use_fxaa ? TONEMAP_MOBILE_FLAG_USE_FXAA : 0;
  286. spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT ? TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING : 0;
  287. spec_constant |= p_settings.convert_to_srgb ? TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB : 0;
  288. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_LINEAR ? TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR : 0;
  289. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_REINHARD ? TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD : 0;
  290. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_FILMIC ? TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC : 0;
  291. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_ACES ? TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES : 0;
  292. spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_AGX ? TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX : 0;
  293. //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_ADDITIVE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD : 0;
  294. //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN : 0;
  295. //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT : 0;
  296. //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE : 0;
  297. //spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX : 0;
  298. int mode = p_settings.use_1d_color_correction ? TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT : TONEMAP_MOBILE_MODE_SUBPASS;
  299. if (p_settings.view_count > 1) {
  300. // Use USE_MULTIVIEW versions
  301. mode += 4;
  302. }
  303. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  304. RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  305. RD::Uniform u_source_color;
  306. u_source_color.uniform_type = RD::UNIFORM_TYPE_INPUT_ATTACHMENT;
  307. u_source_color.binding = 0;
  308. u_source_color.append_id(p_source_color);
  309. RD::Uniform u_glow_texture;
  310. u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  311. u_glow_texture.binding = 1;
  312. u_glow_texture.append_id(default_mipmap_sampler);
  313. u_glow_texture.append_id(p_settings.glow_texture);
  314. RD::Uniform u_glow_map;
  315. u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  316. u_glow_map.binding = 2;
  317. u_glow_map.append_id(default_mipmap_sampler);
  318. u_glow_map.append_id(p_settings.glow_map);
  319. RD::Uniform u_color_correction_texture;
  320. u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  321. u_color_correction_texture.binding = 3;
  322. u_color_correction_texture.append_id(default_sampler);
  323. u_color_correction_texture.append_id(p_settings.color_correction_texture);
  324. RID shader = tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, mode);
  325. ERR_FAIL_COND(shader.is_null());
  326. RD::get_singleton()->draw_list_bind_render_pipeline(p_subpass_draw_list, tonemap_mobile.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_dst_format_id, false, RD::get_singleton()->draw_list_get_current_pass(), spec_constant));
  327. RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color, u_glow_texture, u_glow_map, u_color_correction_texture), 0);
  328. RD::get_singleton()->draw_list_set_push_constant(p_subpass_draw_list, &tonemap_mobile.push_constant, sizeof(TonemapPushConstantMobile));
  329. RD::get_singleton()->draw_list_draw(p_subpass_draw_list, false, 1u, 3u);
  330. }