bokeh_dof.cpp 29 KB

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