rasterizer_effects_rd.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*************************************************************************/
  2. /* rasterizer_effects_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_effects_rd.h"
  31. #include "core/os/os.h"
  32. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  33. p_array[0] = p_basis.elements[0][0];
  34. p_array[1] = p_basis.elements[1][0];
  35. p_array[2] = p_basis.elements[2][0];
  36. p_array[3] = 0;
  37. p_array[4] = p_basis.elements[0][1];
  38. p_array[5] = p_basis.elements[1][1];
  39. p_array[6] = p_basis.elements[2][1];
  40. p_array[7] = 0;
  41. p_array[8] = p_basis.elements[0][2];
  42. p_array[9] = p_basis.elements[1][2];
  43. p_array[10] = p_basis.elements[2][2];
  44. p_array[11] = 0;
  45. }
  46. RID RasterizerEffectsRD::_get_uniform_set_from_image(RID p_image) {
  47. if (image_to_uniform_set_cache.has(p_image)) {
  48. RID uniform_set = image_to_uniform_set_cache[p_image];
  49. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  50. return uniform_set;
  51. }
  52. }
  53. Vector<RD::Uniform> uniforms;
  54. RD::Uniform u;
  55. u.type = RD::UNIFORM_TYPE_IMAGE;
  56. u.binding = 0;
  57. u.ids.push_back(p_image);
  58. uniforms.push_back(u);
  59. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  60. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  61. image_to_uniform_set_cache[p_image] = uniform_set;
  62. return uniform_set;
  63. }
  64. RID RasterizerEffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  65. if (texture_to_uniform_set_cache.has(p_texture)) {
  66. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  67. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  68. return uniform_set;
  69. }
  70. }
  71. Vector<RD::Uniform> uniforms;
  72. RD::Uniform u;
  73. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  74. u.binding = 0;
  75. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  76. u.ids.push_back(p_texture);
  77. uniforms.push_back(u);
  78. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  79. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, blur.shader.version_get_shader(blur.shader_version, 0), 0);
  80. texture_to_uniform_set_cache[p_texture] = uniform_set;
  81. return uniform_set;
  82. }
  83. RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  84. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  85. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  86. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  87. return uniform_set;
  88. }
  89. }
  90. Vector<RD::Uniform> uniforms;
  91. RD::Uniform u;
  92. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  93. u.binding = 0;
  94. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  95. u.ids.push_back(p_texture);
  96. uniforms.push_back(u);
  97. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  98. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  99. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  100. return uniform_set;
  101. }
  102. void RasterizerEffectsRD::copy_to_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_rect, bool p_flip_y, bool p_force_luminance) {
  103. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  104. if (p_flip_y) {
  105. blur.push_constant.flags |= BLUR_FLAG_FLIP_Y;
  106. }
  107. if (p_force_luminance) {
  108. blur.push_constant.flags |= BLUR_COPY_FORCE_LUMINANCE;
  109. }
  110. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, p_rect);
  111. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[BLUR_MODE_SIMPLY_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  112. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  113. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  114. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  115. RD::get_singleton()->draw_list_draw(draw_list, true);
  116. RD::get_singleton()->draw_list_end();
  117. }
  118. void RasterizerEffectsRD::region_copy(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_region) {
  119. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  120. if (p_region != Rect2()) {
  121. blur.push_constant.flags = BLUR_FLAG_USE_BLUR_SECTION;
  122. blur.push_constant.section[0] = p_region.position.x;
  123. blur.push_constant.section[1] = p_region.position.y;
  124. blur.push_constant.section[2] = p_region.size.width;
  125. blur.push_constant.section[3] = p_region.size.height;
  126. }
  127. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  128. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[BLUR_MODE_SIMPLY_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  129. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  130. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  131. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  132. RD::get_singleton()->draw_list_draw(draw_list, true);
  133. RD::get_singleton()->draw_list_end();
  134. }
  135. void RasterizerEffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, const Rect2 &p_region) {
  136. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  137. uint32_t base_flags = 0;
  138. if (p_region != Rect2()) {
  139. base_flags = BLUR_FLAG_USE_BLUR_SECTION;
  140. blur.push_constant.section[0] = p_region.position.x;
  141. blur.push_constant.section[1] = p_region.position.y;
  142. blur.push_constant.section[2] = p_region.size.width;
  143. blur.push_constant.section[3] = p_region.size.height;
  144. }
  145. blur.push_constant.pixel_size[0] = p_pixel_size.x;
  146. blur.push_constant.pixel_size[1] = p_pixel_size.y;
  147. //HORIZONTAL
  148. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer_half, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  149. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[BLUR_MODE_GAUSSIAN_BLUR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer_half)));
  150. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  151. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  152. blur.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL;
  153. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  154. RD::get_singleton()->draw_list_draw(draw_list, true);
  155. RD::get_singleton()->draw_list_end();
  156. //VERTICAL
  157. draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  158. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[BLUR_MODE_GAUSSIAN_BLUR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  159. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_rd_texture_half), 0);
  160. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  161. blur.push_constant.flags = base_flags;
  162. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  163. RD::get_singleton()->draw_list_draw(draw_list, true);
  164. RD::get_singleton()->draw_list_end();
  165. }
  166. void RasterizerEffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) {
  167. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  168. BlurMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  169. uint32_t base_flags = 0;
  170. blur.push_constant.pixel_size[0] = p_pixel_size.x;
  171. blur.push_constant.pixel_size[1] = p_pixel_size.y;
  172. blur.push_constant.glow_strength = p_strength;
  173. blur.push_constant.glow_bloom = p_bloom;
  174. blur.push_constant.glow_hdr_threshold = p_hdr_bleed_treshold;
  175. blur.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  176. blur.push_constant.glow_exposure = p_exposure;
  177. blur.push_constant.glow_white = 0; //actually unused
  178. blur.push_constant.glow_luminance_cap = p_luminance_cap;
  179. blur.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  180. //HORIZONTAL
  181. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer_half, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  182. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer_half)));
  183. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  184. if (p_auto_exposure.is_valid() && p_first_pass) {
  185. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_auto_exposure), 1);
  186. }
  187. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  188. blur.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  189. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  190. RD::get_singleton()->draw_list_draw(draw_list, true);
  191. RD::get_singleton()->draw_list_end();
  192. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  193. //VERTICAL
  194. draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  195. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  196. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_rd_texture_half), 0);
  197. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  198. blur.push_constant.flags = base_flags;
  199. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  200. RD::get_singleton()->draw_list_draw(draw_list, true);
  201. RD::get_singleton()->draw_list_end();
  202. }
  203. void RasterizerEffectsRD::cubemap_roughness(RID p_source_rd_texture, bool p_source_is_panorama, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness) {
  204. zeromem(&roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  205. roughness.push_constant.face_id = p_face_id;
  206. roughness.push_constant.roughness = p_roughness;
  207. roughness.push_constant.sample_count = p_sample_count;
  208. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  209. //RUN
  210. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  211. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, roughness.pipelines[p_source_is_panorama ? CUBEMAP_ROUGHNESS_SOURCE_PANORAMA : CUBEMAP_ROUGHNESS_SOURCE_CUBEMAP].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  212. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  213. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  214. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  215. RD::get_singleton()->draw_list_draw(draw_list, true);
  216. RD::get_singleton()->draw_list_end();
  217. }
  218. void RasterizerEffectsRD::render_panorama(RD::DrawListID p_list, RenderingDevice::FramebufferFormatID p_fb_format, RID p_panorama, const CameraMatrix &p_camera, const Basis &p_orientation, float p_alpha, float p_multipler) {
  219. zeromem(&sky.push_constant, sizeof(SkyPushConstant));
  220. sky.push_constant.proj[0] = p_camera.matrix[2][0];
  221. sky.push_constant.proj[1] = p_camera.matrix[0][0];
  222. sky.push_constant.proj[2] = p_camera.matrix[2][1];
  223. sky.push_constant.proj[3] = p_camera.matrix[1][1];
  224. sky.push_constant.alpha = p_alpha;
  225. sky.push_constant.depth = 1.0;
  226. sky.push_constant.multiplier = p_multipler;
  227. store_transform_3x3(p_orientation, sky.push_constant.orientation);
  228. RD::DrawListID draw_list = p_list;
  229. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, sky.pipeline.get_render_pipeline(RD::INVALID_ID, p_fb_format));
  230. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_panorama), 0);
  231. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  232. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky.push_constant, sizeof(SkyPushConstant));
  233. RD::get_singleton()->draw_list_draw(draw_list, true);
  234. }
  235. void RasterizerEffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_framebuffer, const Vector2 &p_pixel_size) {
  236. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  237. blur.push_constant.pixel_size[0] = p_pixel_size.x;
  238. blur.push_constant.pixel_size[1] = p_pixel_size.y;
  239. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  240. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur.pipelines[BLUR_MODE_MIPMAP].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  241. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  242. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  243. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  244. RD::get_singleton()->draw_list_draw(draw_list, true);
  245. RD::get_singleton()->draw_list_end();
  246. }
  247. void RasterizerEffectsRD::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_rect, float p_z_near, float p_z_far, float p_bias, bool p_dp_flip) {
  248. CopyToDPPushConstant push_constant;
  249. push_constant.bias = p_bias;
  250. push_constant.z_far = p_z_far;
  251. push_constant.z_near = p_z_near;
  252. push_constant.z_flip = p_dp_flip;
  253. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 1.0, 0, p_rect);
  254. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy.pipelines[COPY_MODE_CUBE_TO_DP].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
  255. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  256. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  257. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  258. RD::get_singleton()->draw_list_draw(draw_list, true);
  259. RD::get_singleton()->draw_list_end();
  260. }
  261. void RasterizerEffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  262. zeromem(&tonemap.push_constant, sizeof(TonemapPushConstant));
  263. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  264. tonemap.push_constant.bcs[0] = p_settings.brightness;
  265. tonemap.push_constant.bcs[1] = p_settings.contrast;
  266. tonemap.push_constant.bcs[2] = p_settings.saturation;
  267. tonemap.push_constant.use_glow = p_settings.use_glow;
  268. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  269. tonemap.push_constant.glow_level_flags = p_settings.glow_level_flags;
  270. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  271. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  272. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  273. TonemapMode mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  274. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  275. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  276. tonemap.push_constant.exposure = p_settings.exposure;
  277. tonemap.push_constant.white = p_settings.white;
  278. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  279. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  280. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  281. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
  282. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  283. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  284. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  285. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  286. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  287. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  288. RD::get_singleton()->draw_list_draw(draw_list, true);
  289. RD::get_singleton()->draw_list_end();
  290. }
  291. void RasterizerEffectsRD::luminance_reduction(RID p_source_texture, const Size2i p_source_size, const Vector<RID> p_reduce, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set) {
  292. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  293. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  294. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  295. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  296. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  297. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  298. for (int i = 0; i < p_reduce.size(); i++) {
  299. if (i == 0) {
  300. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  301. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  302. } else {
  303. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  304. if (i == p_reduce.size() - 1 && !p_set) {
  305. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  306. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  307. } else {
  308. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  309. }
  310. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  311. }
  312. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  313. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  314. int32_t x_groups = (luminance_reduce.push_constant.source_size[0] - 1) / 8 + 1;
  315. int32_t y_groups = (luminance_reduce.push_constant.source_size[1] - 1) / 8 + 1;
  316. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  317. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  318. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  319. }
  320. RD::get_singleton()->compute_list_end();
  321. }
  322. void RasterizerEffectsRD::bokeh_dof(RID p_base_texture, RID p_depth_texture, const Size2i &p_base_texture_size, RID p_secondary_texture, RID p_halfsize_texture1, RID p_halfsize_texture2, bool p_dof_far, float p_dof_far_begin, float p_dof_far_size, bool p_dof_near, float p_dof_near_begin, float p_dof_near_size, float p_bokeh_size, VisualServer::DOFBokehShape p_bokeh_shape, VS::DOFBlurQuality p_quality, bool p_use_jitter, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  323. bokeh.push_constant.blur_far_active = p_dof_far;
  324. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  325. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  326. bokeh.push_constant.blur_near_active = p_dof_near;
  327. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  328. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  329. bokeh.push_constant.use_jitter = p_use_jitter;
  330. bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
  331. bokeh.push_constant.z_near = p_cam_znear;
  332. bokeh.push_constant.z_far = p_cam_zfar;
  333. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  334. bokeh.push_constant.blur_size = p_bokeh_size;
  335. bokeh.push_constant.second_pass = false;
  336. bokeh.push_constant.half_size = false;
  337. bokeh.push_constant.blur_scale = 0.5;
  338. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  339. /* FIRST PASS */
  340. // The alpha channel of the source color texture is filled with the expected circle size
  341. // If used for DOF far, the size is positive, if used for near, its negative.
  342. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BLUR_SIZE]);
  343. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  344. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  345. int32_t x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  346. int32_t y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  347. bokeh.push_constant.size[0] = p_base_texture_size.x;
  348. bokeh.push_constant.size[1] = p_base_texture_size.y;
  349. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  350. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  351. RD::get_singleton()->compute_list_add_barrier(compute_list);
  352. if (p_bokeh_shape == VS::DOF_BOKEH_BOX || p_bokeh_shape == VS::DOF_BOKEH_HEXAGON) {
  353. //second pass
  354. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[p_bokeh_shape == VS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL]);
  355. static const int quality_samples[4] = { 6, 12, 12, 24 };
  356. bokeh.push_constant.steps = quality_samples[p_quality];
  357. if (p_quality == VS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == VS::DOF_BLUR_QUALITY_LOW) {
  358. //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)
  359. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  360. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  361. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  362. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  363. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  364. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  365. bokeh.push_constant.half_size = true;
  366. bokeh.push_constant.blur_size *= 0.5;
  367. } else {
  368. //medium and high quality use full size
  369. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_secondary_texture), 0);
  370. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  371. }
  372. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  373. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  374. RD::get_singleton()->compute_list_add_barrier(compute_list);
  375. //third pass
  376. bokeh.push_constant.second_pass = true;
  377. if (p_quality == VS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == VS::DOF_BLUR_QUALITY_LOW) {
  378. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture2), 0);
  379. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  380. } else {
  381. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  382. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_secondary_texture), 1);
  383. }
  384. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  385. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  386. RD::get_singleton()->compute_list_add_barrier(compute_list);
  387. if (p_quality == VS::DOF_BLUR_QUALITY_VERY_LOW || p_quality == VS::DOF_BLUR_QUALITY_LOW) {
  388. //forth pass, upscale for low quality
  389. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  390. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  391. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture2), 1);
  392. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  393. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  394. bokeh.push_constant.size[0] = p_base_texture_size.x;
  395. bokeh.push_constant.size[1] = p_base_texture_size.y;
  396. bokeh.push_constant.half_size = false;
  397. bokeh.push_constant.second_pass = false;
  398. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  399. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  400. }
  401. } else {
  402. //circle
  403. //second pass
  404. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
  405. static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
  406. bokeh.push_constant.steps = 0;
  407. bokeh.push_constant.blur_scale = quality_scale[p_quality];
  408. //circle always runs in half size, otherwise too expensive
  409. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_halfsize_texture1), 0);
  410. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 1);
  411. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  412. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  413. bokeh.push_constant.size[0] = p_base_texture_size.x >> 1;
  414. bokeh.push_constant.size[1] = p_base_texture_size.y >> 1;
  415. bokeh.push_constant.half_size = true;
  416. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  417. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  418. RD::get_singleton()->compute_list_add_barrier(compute_list);
  419. //circle is just one pass, then upscale
  420. // upscale
  421. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  422. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  423. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_halfsize_texture1), 1);
  424. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  425. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  426. bokeh.push_constant.size[0] = p_base_texture_size.x;
  427. bokeh.push_constant.size[1] = p_base_texture_size.y;
  428. bokeh.push_constant.half_size = false;
  429. bokeh.push_constant.second_pass = false;
  430. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  431. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  432. }
  433. RD::get_singleton()->compute_list_end();
  434. }
  435. void RasterizerEffectsRD::generate_ssao(RID p_depth_buffer, RID p_normal_buffer, const Size2i &p_depth_buffer_size, RID p_depth_mipmaps_texture, const Vector<RID> &depth_mipmaps, RID p_ao1, bool p_half_size, RID p_ao2, RID p_upscale_buffer, float p_intensity, float p_radius, float p_bias, const CameraMatrix &p_projection, VS::EnvironmentSSAOQuality p_quality, VS::EnvironmentSSAOBlur p_blur, float p_edge_sharpness) {
  436. //minify first
  437. ssao.minify_push_constant.orthogonal = p_projection.is_orthogonal();
  438. ssao.minify_push_constant.z_near = p_projection.get_z_near();
  439. ssao.minify_push_constant.z_far = p_projection.get_z_far();
  440. ssao.minify_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  441. ssao.minify_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  442. ssao.minify_push_constant.source_size[0] = p_depth_buffer_size.x;
  443. ssao.minify_push_constant.source_size[1] = p_depth_buffer_size.y;
  444. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  445. /* FIRST PASS */
  446. // Minify the depth buffer.
  447. for (int i = 0; i < depth_mipmaps.size(); i++) {
  448. if (i == 0) {
  449. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_FIRST]);
  450. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 0);
  451. } else {
  452. if (i == 1) {
  453. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_MINIFY_MIPMAP]);
  454. }
  455. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i - 1]), 0);
  456. }
  457. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(depth_mipmaps[i]), 1);
  458. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.minify_push_constant, sizeof(SSAOMinifyPushConstant));
  459. // shrink after set
  460. ssao.minify_push_constant.source_size[0] = MAX(1, ssao.minify_push_constant.source_size[0] >> 1);
  461. ssao.minify_push_constant.source_size[1] = MAX(1, ssao.minify_push_constant.source_size[1] >> 1);
  462. int x_groups = (ssao.minify_push_constant.source_size[0] - 1) / 8 + 1;
  463. int y_groups = (ssao.minify_push_constant.source_size[1] - 1) / 8 + 1;
  464. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  465. RD::get_singleton()->compute_list_add_barrier(compute_list);
  466. }
  467. /* SECOND PASS */
  468. // Gather samples
  469. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[(SSAO_GATHER_LOW + p_quality) + (p_half_size ? 4 : 0)]);
  470. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 0);
  471. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 1);
  472. if (!p_half_size) {
  473. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 2);
  474. }
  475. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_normal_buffer), 3);
  476. ssao.gather_push_constant.screen_size[0] = p_depth_buffer_size.x;
  477. ssao.gather_push_constant.screen_size[1] = p_depth_buffer_size.y;
  478. if (p_half_size) {
  479. ssao.gather_push_constant.screen_size[0] >>= 1;
  480. ssao.gather_push_constant.screen_size[1] >>= 1;
  481. }
  482. ssao.gather_push_constant.z_far = p_projection.get_z_far();
  483. ssao.gather_push_constant.z_near = p_projection.get_z_near();
  484. ssao.gather_push_constant.orthogonal = p_projection.is_orthogonal();
  485. ssao.gather_push_constant.proj_info[0] = -2.0f / (ssao.gather_push_constant.screen_size[0] * p_projection.matrix[0][0]);
  486. ssao.gather_push_constant.proj_info[1] = -2.0f / (ssao.gather_push_constant.screen_size[1] * p_projection.matrix[1][1]);
  487. ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  488. ssao.gather_push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  489. //ssao.gather_push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  490. //ssao.gather_push_constant.proj_info[3] = -(1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  491. ssao.gather_push_constant.radius = p_radius;
  492. ssao.gather_push_constant.proj_scale = float(p_projection.get_pixels_per_meter(ssao.gather_push_constant.screen_size[0]));
  493. ssao.gather_push_constant.bias = p_bias;
  494. ssao.gather_push_constant.intensity_div_r6 = p_intensity / pow(p_radius, 6.0f);
  495. ssao.gather_push_constant.pixel_size[0] = 1.0 / p_depth_buffer_size.x;
  496. ssao.gather_push_constant.pixel_size[1] = 1.0 / p_depth_buffer_size.y;
  497. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.gather_push_constant, sizeof(SSAOGatherPushConstant));
  498. int x_groups = (ssao.gather_push_constant.screen_size[0] - 1) / 8 + 1;
  499. int y_groups = (ssao.gather_push_constant.screen_size[1] - 1) / 8 + 1;
  500. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  501. RD::get_singleton()->compute_list_add_barrier(compute_list);
  502. /* THIRD PASS */
  503. // Blur horizontal
  504. ssao.blur_push_constant.edge_sharpness = p_edge_sharpness;
  505. ssao.blur_push_constant.filter_scale = p_blur + 1;
  506. ssao.blur_push_constant.screen_size[0] = ssao.gather_push_constant.screen_size[0];
  507. ssao.blur_push_constant.screen_size[1] = ssao.gather_push_constant.screen_size[1];
  508. ssao.blur_push_constant.z_far = p_projection.get_z_far();
  509. ssao.blur_push_constant.z_near = p_projection.get_z_near();
  510. ssao.blur_push_constant.orthogonal = p_projection.is_orthogonal();
  511. ssao.blur_push_constant.axis[0] = 1;
  512. ssao.blur_push_constant.axis[1] = 0;
  513. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[p_half_size ? SSAO_BLUR_PASS_HALF : SSAO_BLUR_PASS]);
  514. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  515. if (p_half_size) {
  516. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 1);
  517. } else {
  518. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  519. }
  520. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao2), 3);
  521. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  522. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  523. RD::get_singleton()->compute_list_add_barrier(compute_list);
  524. /* THIRD PASS */
  525. // Blur vertical
  526. ssao.blur_push_constant.axis[0] = 0;
  527. ssao.blur_push_constant.axis[1] = 1;
  528. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao2), 0);
  529. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_ao1), 3);
  530. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant));
  531. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  532. if (p_half_size) { //must upscale
  533. /* FOURTH PASS */
  534. // upscale if half size
  535. //back to full size
  536. ssao.blur_push_constant.screen_size[0] = p_depth_buffer_size.x;
  537. ssao.blur_push_constant.screen_size[1] = p_depth_buffer_size.y;
  538. RD::get_singleton()->compute_list_add_barrier(compute_list);
  539. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, ssao.pipelines[SSAO_BLUR_UPSCALE]);
  540. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_ao1), 0);
  541. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_upscale_buffer), 3);
  542. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_buffer), 1);
  543. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_mipmaps_texture), 2);
  544. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ssao.blur_push_constant, sizeof(SSAOBlurPushConstant)); //not used but set anyway
  545. x_groups = (p_depth_buffer_size.x - 1) / 8 + 1;
  546. y_groups = (p_depth_buffer_size.y - 1) / 8 + 1;
  547. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  548. }
  549. RD::get_singleton()->compute_list_end();
  550. }
  551. RasterizerEffectsRD::RasterizerEffectsRD() {
  552. {
  553. // Initialize blur
  554. Vector<String> blur_modes;
  555. blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  556. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  557. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  558. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_LOW\n");
  559. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_MEDIUM\n");
  560. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_HIGH\n");
  561. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_LOW\n#define DOF_NEAR_BLUR_MERGE\n");
  562. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_MEDIUM\n#define DOF_NEAR_BLUR_MERGE\n");
  563. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_HIGH\n#define DOF_NEAR_BLUR_MERGE\n");
  564. blur_modes.push_back("\n#define MODE_DOF_FAR_BLUR\n#define DOF_QUALITY_LOW\n");
  565. blur_modes.push_back("\n#define MODE_DOF_FAR_BLUR\n#define DOF_QUALITY_MEDIUM\n");
  566. blur_modes.push_back("\n#define MODE_DOF_FAR_BLUR\n#define DOF_QUALITY_HIGH\n");
  567. blur_modes.push_back("\n#define MODE_SSAO_MERGE\n");
  568. blur_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  569. blur_modes.push_back("\n#define MODE_MIPMAP\n");
  570. blur.shader.initialize(blur_modes);
  571. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  572. blur.shader_version = blur.shader.version_create();
  573. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  574. blur.pipelines[i].setup(blur.shader.version_get_shader(blur.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  575. }
  576. }
  577. {
  578. // Initialize roughness
  579. Vector<String> cubemap_roughness_modes;
  580. cubemap_roughness_modes.push_back("\n#define MODE_SOURCE_PANORAMA\n");
  581. cubemap_roughness_modes.push_back("\n#define MODE_SOURCE_CUBEMAP\n");
  582. roughness.shader.initialize(cubemap_roughness_modes);
  583. roughness.shader_version = roughness.shader.version_create();
  584. for (int i = 0; i < CUBEMAP_ROUGHNESS_SOURCE_MAX; i++) {
  585. roughness.pipelines[i].setup(roughness.shader.version_get_shader(roughness.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  586. }
  587. }
  588. {
  589. // Initialize sky
  590. Vector<String> sky_modes;
  591. sky_modes.push_back("");
  592. sky.shader.initialize(sky_modes);
  593. sky.shader_version = sky.shader.version_create();
  594. RD::PipelineDepthStencilState depth_stencil_state;
  595. depth_stencil_state.enable_depth_test = true;
  596. depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  597. sky.pipeline.setup(sky.shader.version_get_shader(sky.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), depth_stencil_state, RD::PipelineColorBlendState::create_disabled(), 0);
  598. }
  599. {
  600. // Initialize tonemapper
  601. Vector<String> tonemap_modes;
  602. tonemap_modes.push_back("\n");
  603. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  604. tonemap.shader.initialize(tonemap_modes);
  605. tonemap.shader_version = tonemap.shader.version_create();
  606. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  607. tonemap.pipelines[i].setup(tonemap.shader.version_get_shader(tonemap.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  608. }
  609. }
  610. {
  611. // Initialize luminance_reduce
  612. Vector<String> luminance_reduce_modes;
  613. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  614. luminance_reduce_modes.push_back("\n");
  615. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  616. luminance_reduce.shader.initialize(luminance_reduce_modes);
  617. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  618. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  619. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  620. }
  621. }
  622. {
  623. // Initialize copier
  624. Vector<String> copy_modes;
  625. copy_modes.push_back("\n#define MODE_CUBE_TO_DP\n");
  626. copy.shader.initialize(copy_modes);
  627. copy.shader_version = copy.shader.version_create();
  628. for (int i = 0; i < COPY_MODE_MAX; i++) {
  629. copy.pipelines[i].setup(copy.shader.version_get_shader(copy.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  630. }
  631. }
  632. {
  633. // Initialize bokeh
  634. Vector<String> bokeh_modes;
  635. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  636. bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
  637. bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
  638. bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n");
  639. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  640. bokeh.shader.initialize(bokeh_modes);
  641. bokeh.shader_version = bokeh.shader.version_create();
  642. for (int i = 0; i < BOKEH_MAX; i++) {
  643. bokeh.pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.shader.version_get_shader(bokeh.shader_version, i));
  644. }
  645. }
  646. {
  647. // Initialize ssao
  648. uint32_t pipeline = 0;
  649. {
  650. Vector<String> ssao_modes;
  651. ssao_modes.push_back("\n#define MINIFY_START\n");
  652. ssao_modes.push_back("\n");
  653. ssao.minify_shader.initialize(ssao_modes);
  654. ssao.minify_shader_version = ssao.minify_shader.version_create();
  655. for (int i = 0; i <= SSAO_MINIFY_MIPMAP; i++) {
  656. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.minify_shader.version_get_shader(ssao.minify_shader_version, i));
  657. pipeline++;
  658. }
  659. }
  660. {
  661. Vector<String> ssao_modes;
  662. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n");
  663. ssao_modes.push_back("\n");
  664. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n");
  665. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n");
  666. ssao_modes.push_back("\n#define SSAO_QUALITY_LOW\n#define USE_HALF_SIZE\n");
  667. ssao_modes.push_back("\n#define USE_HALF_SIZE\n");
  668. ssao_modes.push_back("\n#define SSAO_QUALITY_HIGH\n#define USE_HALF_SIZE\n");
  669. ssao_modes.push_back("\n#define SSAO_QUALITY_ULTRA\n#define USE_HALF_SIZE\n");
  670. ssao.gather_shader.initialize(ssao_modes);
  671. ssao.gather_shader_version = ssao.gather_shader.version_create();
  672. for (int i = SSAO_GATHER_LOW; i <= SSAO_GATHER_ULTRA_HALF; i++) {
  673. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.gather_shader.version_get_shader(ssao.gather_shader_version, i - SSAO_GATHER_LOW));
  674. pipeline++;
  675. }
  676. }
  677. {
  678. Vector<String> ssao_modes;
  679. ssao_modes.push_back("\n#define MODE_FULL_SIZE\n");
  680. ssao_modes.push_back("\n");
  681. ssao_modes.push_back("\n#define MODE_UPSCALE\n");
  682. ssao.blur_shader.initialize(ssao_modes);
  683. ssao.blur_shader_version = ssao.blur_shader.version_create();
  684. for (int i = SSAO_BLUR_PASS; i <= SSAO_BLUR_UPSCALE; i++) {
  685. ssao.pipelines[pipeline] = RD::get_singleton()->compute_pipeline_create(ssao.blur_shader.version_get_shader(ssao.blur_shader_version, i - SSAO_BLUR_PASS));
  686. pipeline++;
  687. }
  688. }
  689. ERR_FAIL_COND(pipeline != SSAO_MAX);
  690. }
  691. RD::SamplerState sampler;
  692. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  693. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  694. sampler.max_lod = 0;
  695. default_sampler = RD::get_singleton()->sampler_create(sampler);
  696. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  697. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  698. sampler.max_lod = 1e20;
  699. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  700. { //create index array for copy shaders
  701. PoolVector<uint8_t> pv;
  702. pv.resize(6 * 4);
  703. {
  704. PoolVector<uint8_t>::Write w = pv.write();
  705. int *p32 = (int *)w.ptr();
  706. p32[0] = 0;
  707. p32[1] = 1;
  708. p32[2] = 2;
  709. p32[3] = 0;
  710. p32[4] = 2;
  711. p32[5] = 3;
  712. }
  713. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  714. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  715. }
  716. }
  717. RasterizerEffectsRD::~RasterizerEffectsRD() {
  718. RD::get_singleton()->free(default_sampler);
  719. blur.shader.version_free(blur.shader_version);
  720. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  721. }