bokeh_dof.cpp 27 KB

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