bokeh_dof.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*************************************************************************/
  2. /* bokeh_dof.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "bokeh_dof.h"
  31. #include "copy_effects.h"
  32. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  33. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  34. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  35. using namespace RendererRD;
  36. BokehDOF::BokehDOF(bool p_prefer_raster_effects) {
  37. prefer_raster_effects = p_prefer_raster_effects;
  38. // Initialize bokeh
  39. Vector<String> bokeh_modes;
  40. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  41. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n#define OUTPUT_WEIGHT\n");
  42. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  43. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n#define OUTPUT_WEIGHT\n");
  44. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  45. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n#define OUTPUT_WEIGHT\n");
  46. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  47. if (prefer_raster_effects) {
  48. bokeh.raster_shader.initialize(bokeh_modes);
  49. bokeh.shader_version = bokeh.raster_shader.version_create();
  50. const int att_count[BOKEH_MAX] = { 1, 2, 1, 2, 1, 2, 1 };
  51. for (int i = 0; i < BOKEH_MAX; i++) {
  52. RD::PipelineColorBlendState blend_state = (i == BOKEH_COMPOSITE) ? RD::PipelineColorBlendState::create_blend(att_count[i]) : RD::PipelineColorBlendState::create_disabled(att_count[i]);
  53. bokeh.raster_pipelines[i].setup(bokeh.raster_shader.version_get_shader(bokeh.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
  54. }
  55. } else {
  56. bokeh.compute_shader.initialize(bokeh_modes);
  57. bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_BOX_NOWEIGHT, false);
  58. bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT, false);
  59. bokeh.shader_version = bokeh.compute_shader.version_create();
  60. for (int i = 0; i < BOKEH_MAX; i++) {
  61. if (bokeh.compute_shader.is_variant_enabled(i)) {
  62. bokeh.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.compute_shader.version_get_shader(bokeh.shader_version, i));
  63. }
  64. }
  65. for (int i = 0; i < BOKEH_MAX; i++) {
  66. bokeh.raster_pipelines[i].clear();
  67. }
  68. }
  69. }
  70. BokehDOF::~BokehDOF() {
  71. if (prefer_raster_effects) {
  72. bokeh.raster_shader.version_free(bokeh.shader_version);
  73. } else {
  74. bokeh.compute_shader.version_free(bokeh.shader_version);
  75. }
  76. }
  77. void BokehDOF::bokeh_dof_compute(const BokehBuffers &p_buffers, bool p_dof_far, float p_dof_far_begin, float p_dof_far_size, bool p_dof_near, float p_dof_near_begin, float p_dof_near_size, float p_bokeh_size, RenderingServer::DOFBokehShape p_bokeh_shape, RS::DOFBlurQuality p_quality, bool p_use_jitter, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  78. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of bokeh depth of field with the mobile renderer.");
  79. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  80. ERR_FAIL_NULL(uniform_set_cache);
  81. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  82. ERR_FAIL_NULL(material_storage);
  83. // setup our push constant
  84. memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
  85. bokeh.push_constant.blur_far_active = p_dof_far;
  86. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  87. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  88. bokeh.push_constant.blur_near_active = p_dof_near;
  89. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  90. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  91. bokeh.push_constant.use_jitter = p_use_jitter;
  92. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  93. bokeh.push_constant.z_near = p_cam_znear;
  94. bokeh.push_constant.z_far = p_cam_zfar;
  95. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  96. bokeh.push_constant.blur_size = p_bokeh_size;
  97. bokeh.push_constant.second_pass = false;
  98. bokeh.push_constant.half_size = false;
  99. bokeh.push_constant.blur_scale = 0.5;
  100. // setup our uniforms
  101. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  102. RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));
  103. RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));
  104. RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));
  105. RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));
  106. RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));
  107. RD::Uniform u_base_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.base_texture);
  108. RD::Uniform u_secondary_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.secondary_texture);
  109. RD::Uniform u_half_image0(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[0]);
  110. RD::Uniform u_half_image1(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[1]);
  111. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  112. /* FIRST PASS */
  113. // The alpha channel of the source color texture is filled with the expected circle size
  114. // If used for DOF far, the size is positive, if used for near, its negative.
  115. RID shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);
  116. ERR_FAIL_COND(shader.is_null());
  117. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE]);
  118. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  119. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_texture), 1);
  120. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  121. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  122. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  123. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  124. RD::get_singleton()->compute_list_add_barrier(compute_list);
  125. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  126. //second pass
  127. BokehMode mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
  128. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, mode);
  129. ERR_FAIL_COND(shader.is_null());
  130. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[mode]);
  131. static const int quality_samples[4] = { 6, 12, 12, 24 };
  132. bokeh.push_constant.steps = quality_samples[p_quality];
  133. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  134. //box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
  135. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);
  136. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
  137. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  138. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  139. bokeh.push_constant.half_size = true;
  140. bokeh.push_constant.blur_size *= 0.5;
  141. } else {
  142. //medium and high quality use full size
  143. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_secondary_image), 0);
  144. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
  145. }
  146. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  147. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  148. RD::get_singleton()->compute_list_add_barrier(compute_list);
  149. //third pass
  150. bokeh.push_constant.second_pass = true;
  151. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  152. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image1), 0);
  153. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);
  154. } else {
  155. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  156. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_secondary_texture), 1);
  157. }
  158. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  159. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  160. RD::get_singleton()->compute_list_add_barrier(compute_list);
  161. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  162. //forth pass, upscale for low quality
  163. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);
  164. ERR_FAIL_COND(shader.is_null());
  165. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
  166. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  167. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture1), 1);
  168. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  169. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  170. bokeh.push_constant.half_size = false;
  171. bokeh.push_constant.second_pass = false;
  172. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  173. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  174. }
  175. } else {
  176. //circle
  177. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BOKEH_CIRCULAR);
  178. ERR_FAIL_COND(shader.is_null());
  179. //second pass
  180. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  181. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  182. bokeh.push_constant.steps = 0;
  183. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  184. //circle always runs in half size, otherwise too expensive
  185. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);
  186. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
  187. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  188. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  189. bokeh.push_constant.half_size = true;
  190. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  191. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  192. RD::get_singleton()->compute_list_add_barrier(compute_list);
  193. //circle is just one pass, then upscale
  194. // upscale
  195. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);
  196. ERR_FAIL_COND(shader.is_null());
  197. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
  198. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  199. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);
  200. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  201. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  202. bokeh.push_constant.half_size = false;
  203. bokeh.push_constant.second_pass = false;
  204. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  205. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  206. }
  207. RD::get_singleton()->compute_list_end();
  208. }
  209. void BokehDOF::bokeh_dof_raster(const BokehBuffers &p_buffers, bool p_dof_far, float p_dof_far_begin, float p_dof_far_size, bool p_dof_near, float p_dof_near_begin, float p_dof_near_size, float p_dof_blur_amount, RenderingServer::DOFBokehShape p_bokeh_shape, RS::DOFBlurQuality p_quality, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  210. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't blur-based depth of field with the clustered renderer.");
  211. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  212. ERR_FAIL_NULL(uniform_set_cache);
  213. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  214. ERR_FAIL_NULL(material_storage);
  215. // setup our base push constant
  216. memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
  217. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  218. bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;
  219. bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;
  220. bokeh.push_constant.z_far = p_cam_zfar;
  221. bokeh.push_constant.z_near = p_cam_znear;
  222. bokeh.push_constant.second_pass = false;
  223. bokeh.push_constant.half_size = false;
  224. bokeh.push_constant.blur_size = p_dof_blur_amount;
  225. // setup our uniforms
  226. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  227. RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));
  228. RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));
  229. RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));
  230. RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));
  231. RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));
  232. RD::Uniform u_weight_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[0] }));
  233. RD::Uniform u_weight_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[1] }));
  234. RD::Uniform u_weight_texture2(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[2] }));
  235. RD::Uniform u_weight_texture3(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[3] }));
  236. if (p_dof_far || p_dof_near) {
  237. if (p_dof_far) {
  238. bokeh.push_constant.blur_far_active = true;
  239. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  240. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  241. }
  242. if (p_dof_near) {
  243. bokeh.push_constant.blur_near_active = true;
  244. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  245. bokeh.push_constant.blur_near_end = p_dof_near_begin - p_dof_near_size;
  246. }
  247. {
  248. // generate our depth data
  249. RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);
  250. ERR_FAIL_COND(shader.is_null());
  251. RID framebuffer = p_buffers.base_weight_fb;
  252. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  253. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[BOKEH_GEN_BLUR_SIZE].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  254. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_depth_texture), 0);
  255. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  256. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  257. RD::get_singleton()->draw_list_draw(draw_list, true);
  258. RD::get_singleton()->draw_list_end();
  259. }
  260. if (p_bokeh_shape == RS::DOF_BOKEH_BOX || p_bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  261. // double pass approach
  262. BokehMode mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
  263. RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  264. ERR_FAIL_COND(shader.is_null());
  265. if (p_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == RS::DOF_BLUR_QUALITY_LOW) {
  266. //box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
  267. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  268. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  269. bokeh.push_constant.half_size = true;
  270. bokeh.push_constant.blur_size *= 0.5;
  271. }
  272. static const int quality_samples[4] = { 6, 12, 12, 24 };
  273. bokeh.push_constant.blur_scale = 0.5;
  274. bokeh.push_constant.steps = quality_samples[p_quality];
  275. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  276. // Pass 1
  277. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  278. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  279. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);
  280. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);
  281. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  282. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  283. RD::get_singleton()->draw_list_draw(draw_list, true);
  284. RD::get_singleton()->draw_list_end();
  285. // Pass 2
  286. if (!bokeh.push_constant.half_size) {
  287. // do not output weight, we're writing back into our base buffer
  288. mode = p_bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;
  289. shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  290. ERR_FAIL_COND(shader.is_null());
  291. }
  292. bokeh.push_constant.second_pass = true;
  293. framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;
  294. RD::Uniform texture = bokeh.push_constant.half_size ? u_half_texture0 : u_secondary_texture;
  295. RD::Uniform weight = bokeh.push_constant.half_size ? u_weight_texture2 : u_weight_texture1;
  296. draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  297. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  298. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, texture), 0);
  299. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, weight), 1);
  300. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  301. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  302. RD::get_singleton()->draw_list_draw(draw_list, true);
  303. RD::get_singleton()->draw_list_end();
  304. if (bokeh.push_constant.half_size) {
  305. // Compose pass
  306. mode = BOKEH_COMPOSITE;
  307. shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  308. ERR_FAIL_COND(shader.is_null());
  309. framebuffer = p_buffers.base_fb;
  310. draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  311. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  312. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture1), 0);
  313. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture3), 1);
  314. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);
  315. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  316. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  317. RD::get_singleton()->draw_list_draw(draw_list, true);
  318. RD::get_singleton()->draw_list_end();
  319. }
  320. } else {
  321. // circular is a single pass approach
  322. BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;
  323. RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  324. ERR_FAIL_COND(shader.is_null());
  325. {
  326. // circle always runs in half size, otherwise too expensive (though the code below does support making this optional)
  327. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  328. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  329. bokeh.push_constant.half_size = true;
  330. // bokeh.push_constant.blur_size *= 0.5;
  331. }
  332. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  333. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  334. bokeh.push_constant.steps = 0.0;
  335. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  336. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  337. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  338. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);
  339. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);
  340. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  341. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  342. RD::get_singleton()->draw_list_draw(draw_list, true);
  343. RD::get_singleton()->draw_list_end();
  344. if (bokeh.push_constant.half_size) {
  345. // Compose
  346. mode = BOKEH_COMPOSITE;
  347. shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  348. ERR_FAIL_COND(shader.is_null());
  349. framebuffer = p_buffers.base_fb;
  350. draw_list = RD::get_singleton()->draw_list_begin(framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  351. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
  352. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture0), 0);
  353. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture2), 1);
  354. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);
  355. RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
  356. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  357. RD::get_singleton()->draw_list_draw(draw_list, true);
  358. RD::get_singleton()->draw_list_end();
  359. } else {
  360. CopyEffects::get_singleton()->copy_raster(p_buffers.secondary_texture, p_buffers.base_fb);
  361. }
  362. }
  363. }
  364. }