bokeh_dof.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**************************************************************************/
  2. /* bokeh_dof.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 "bokeh_dof.h"
  31. #include "copy_effects.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  33. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  34. #include "servers/rendering/storage/camera_attributes_storage.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].create_compute_pipeline(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. for (int i = 0; i < BOKEH_MAX; i++) {
  72. bokeh.compute_pipelines[i].free();
  73. }
  74. if (prefer_raster_effects) {
  75. bokeh.raster_shader.version_free(bokeh.shader_version);
  76. } else {
  77. bokeh.compute_shader.version_free(bokeh.shader_version);
  78. }
  79. }
  80. void BokehDOF::bokeh_dof_compute(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  81. ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of bokeh depth of field with the mobile renderer.");
  82. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  83. ERR_FAIL_NULL(uniform_set_cache);
  84. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  85. ERR_FAIL_NULL(material_storage);
  86. bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);
  87. float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);
  88. float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);
  89. bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);
  90. float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);
  91. float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);
  92. float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.
  93. bool use_jitter = RSG::camera_attributes->camera_attributes_get_dof_blur_use_jitter();
  94. RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();
  95. RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();
  96. // setup our push constant
  97. memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
  98. bokeh.push_constant.blur_far_active = dof_far;
  99. bokeh.push_constant.blur_far_begin = dof_far_begin;
  100. bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size; // Only used with non-physically-based.
  101. bokeh.push_constant.use_physical_far = dof_far_size < 0.0;
  102. bokeh.push_constant.blur_size_far = bokeh_size; // Only used with physically-based.
  103. bokeh.push_constant.blur_near_active = dof_near;
  104. bokeh.push_constant.blur_near_begin = dof_near_begin;
  105. bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size; // Only used with non-physically-based.
  106. bokeh.push_constant.use_physical_near = dof_near_size < 0.0;
  107. bokeh.push_constant.blur_size_near = bokeh_size; // Only used with physically-based.
  108. bokeh.push_constant.use_jitter = use_jitter;
  109. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  110. bokeh.push_constant.z_near = p_cam_znear;
  111. bokeh.push_constant.z_far = p_cam_zfar;
  112. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  113. bokeh.push_constant.blur_size = (dof_near_size < 0.0 && dof_far_size < 0.0) ? 32 : bokeh_size; // Cap with physically-based to keep performance reasonable.
  114. bokeh.push_constant.second_pass = false;
  115. bokeh.push_constant.half_size = false;
  116. bokeh.push_constant.blur_scale = 0.5;
  117. // setup our uniforms
  118. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  119. RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));
  120. RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));
  121. RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));
  122. RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));
  123. RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));
  124. RD::Uniform u_base_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.base_texture);
  125. RD::Uniform u_secondary_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.secondary_texture);
  126. RD::Uniform u_half_image0(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[0]);
  127. RD::Uniform u_half_image1(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[1]);
  128. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  129. /* FIRST PASS */
  130. // The alpha channel of the source color texture is filled with the expected circle size
  131. // If used for DOF far, the size is positive, if used for near, its negative.
  132. RID shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);
  133. ERR_FAIL_COND(shader.is_null());
  134. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE].get_rid());
  135. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  136. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_texture), 1);
  137. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  138. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  139. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  140. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  141. RD::get_singleton()->compute_list_add_barrier(compute_list);
  142. if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  143. //second pass
  144. BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
  145. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, mode);
  146. ERR_FAIL_COND(shader.is_null());
  147. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[mode].get_rid());
  148. static const int quality_samples[4] = { 6, 12, 12, 24 };
  149. bokeh.push_constant.steps = quality_samples[blur_quality];
  150. if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
  151. //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)
  152. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);
  153. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
  154. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  155. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  156. bokeh.push_constant.half_size = true;
  157. bokeh.push_constant.blur_size *= 0.5;
  158. } else {
  159. //medium and high quality use full size
  160. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_secondary_image), 0);
  161. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
  162. }
  163. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  164. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  165. RD::get_singleton()->compute_list_add_barrier(compute_list);
  166. //third pass
  167. bokeh.push_constant.second_pass = true;
  168. if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
  169. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image1), 0);
  170. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);
  171. } else {
  172. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  173. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_secondary_texture), 1);
  174. }
  175. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  176. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  177. RD::get_singleton()->compute_list_add_barrier(compute_list);
  178. if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
  179. //forth pass, upscale for low quality
  180. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);
  181. ERR_FAIL_COND(shader.is_null());
  182. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE].get_rid());
  183. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  184. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture1), 1);
  185. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  186. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  187. bokeh.push_constant.half_size = false;
  188. bokeh.push_constant.second_pass = false;
  189. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  190. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  191. }
  192. } else {
  193. //circle
  194. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BOKEH_CIRCULAR);
  195. ERR_FAIL_COND(shader.is_null());
  196. //second pass
  197. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR].get_rid());
  198. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  199. bokeh.push_constant.steps = 0;
  200. bokeh.push_constant.blur_scale = quality_scale[blur_quality];
  201. //circle always runs in half size, otherwise too expensive
  202. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);
  203. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
  204. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  205. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  206. bokeh.push_constant.half_size = true;
  207. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  208. RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
  209. RD::get_singleton()->compute_list_add_barrier(compute_list);
  210. //circle is just one pass, then upscale
  211. // upscale
  212. shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);
  213. ERR_FAIL_COND(shader.is_null());
  214. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE].get_rid());
  215. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
  216. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);
  217. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
  218. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
  219. bokeh.push_constant.half_size = false;
  220. bokeh.push_constant.second_pass = false;
  221. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  222. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
  223. }
  224. RD::get_singleton()->compute_list_end();
  225. }
  226. void BokehDOF::bokeh_dof_raster(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  227. ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't blur-based depth of field with the clustered renderer.");
  228. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  229. ERR_FAIL_NULL(uniform_set_cache);
  230. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  231. ERR_FAIL_NULL(material_storage);
  232. bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);
  233. float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);
  234. float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);
  235. bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);
  236. float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);
  237. float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);
  238. float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.
  239. RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();
  240. RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();
  241. // setup our base push constant
  242. memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
  243. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  244. bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;
  245. bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;
  246. bokeh.push_constant.z_far = p_cam_zfar;
  247. bokeh.push_constant.z_near = p_cam_znear;
  248. bokeh.push_constant.second_pass = false;
  249. bokeh.push_constant.half_size = false;
  250. bokeh.push_constant.blur_size = bokeh_size;
  251. // setup our uniforms
  252. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  253. RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));
  254. RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));
  255. RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));
  256. RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));
  257. RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));
  258. RD::Uniform u_weight_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[0] }));
  259. RD::Uniform u_weight_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[1] }));
  260. RD::Uniform u_weight_texture2(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[2] }));
  261. RD::Uniform u_weight_texture3(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[3] }));
  262. if (dof_far || dof_near) {
  263. if (dof_far) {
  264. bokeh.push_constant.blur_far_active = true;
  265. bokeh.push_constant.blur_far_begin = dof_far_begin;
  266. bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size;
  267. }
  268. if (dof_near) {
  269. bokeh.push_constant.blur_near_active = true;
  270. bokeh.push_constant.blur_near_begin = dof_near_begin;
  271. bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size;
  272. }
  273. {
  274. // generate our depth data
  275. RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);
  276. ERR_FAIL_COND(shader.is_null());
  277. RID framebuffer = p_buffers.base_weight_fb;
  278. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  279. 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)));
  280. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_depth_texture), 0);
  281. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  282. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  283. RD::get_singleton()->draw_list_end();
  284. }
  285. if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
  286. // double pass approach
  287. BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
  288. RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  289. ERR_FAIL_COND(shader.is_null());
  290. if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
  291. //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)
  292. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  293. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  294. bokeh.push_constant.half_size = true;
  295. bokeh.push_constant.blur_size *= 0.5;
  296. }
  297. static const int quality_samples[4] = { 6, 12, 12, 24 };
  298. bokeh.push_constant.blur_scale = 0.5;
  299. bokeh.push_constant.steps = quality_samples[blur_quality];
  300. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  301. // Pass 1
  302. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  303. 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)));
  304. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);
  305. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);
  306. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  307. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  308. RD::get_singleton()->draw_list_end();
  309. // Pass 2
  310. if (!bokeh.push_constant.half_size) {
  311. // do not output weight, we're writing back into our base buffer
  312. mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;
  313. shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  314. ERR_FAIL_COND(shader.is_null());
  315. }
  316. bokeh.push_constant.second_pass = true;
  317. framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;
  318. RD::Uniform texture = bokeh.push_constant.half_size ? u_half_texture0 : u_secondary_texture;
  319. RD::Uniform weight = bokeh.push_constant.half_size ? u_weight_texture2 : u_weight_texture1;
  320. draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  321. 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)));
  322. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, texture), 0);
  323. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, weight), 1);
  324. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  325. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  326. RD::get_singleton()->draw_list_end();
  327. if (bokeh.push_constant.half_size) {
  328. // Compose pass
  329. mode = BOKEH_COMPOSITE;
  330. shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  331. ERR_FAIL_COND(shader.is_null());
  332. framebuffer = p_buffers.base_fb;
  333. draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  334. 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)));
  335. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture1), 0);
  336. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture3), 1);
  337. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);
  338. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  339. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  340. RD::get_singleton()->draw_list_end();
  341. }
  342. } else {
  343. // circular is a single pass approach
  344. BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;
  345. RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  346. ERR_FAIL_COND(shader.is_null());
  347. {
  348. // circle always runs in half size, otherwise too expensive (though the code below does support making this optional)
  349. bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
  350. bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
  351. bokeh.push_constant.half_size = true;
  352. // bokeh.push_constant.blur_size *= 0.5;
  353. }
  354. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  355. bokeh.push_constant.blur_scale = quality_scale[blur_quality];
  356. bokeh.push_constant.steps = 0.0;
  357. RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
  358. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  359. 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)));
  360. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);
  361. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);
  362. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  363. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  364. RD::get_singleton()->draw_list_end();
  365. if (bokeh.push_constant.half_size) {
  366. // Compose
  367. mode = BOKEH_COMPOSITE;
  368. shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
  369. ERR_FAIL_COND(shader.is_null());
  370. framebuffer = p_buffers.base_fb;
  371. draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
  372. 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)));
  373. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture0), 0);
  374. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture2), 1);
  375. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);
  376. RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  377. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  378. RD::get_singleton()->draw_list_end();
  379. } else {
  380. CopyEffects::get_singleton()->copy_raster(p_buffers.secondary_texture, p_buffers.base_fb);
  381. }
  382. }
  383. }
  384. }