rasterizer_effects_rd.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_array) {
  32. p_array[0] = p_basis.elements[0][0];
  33. p_array[1] = p_basis.elements[1][0];
  34. p_array[2] = p_basis.elements[2][0];
  35. p_array[3] = 0;
  36. p_array[4] = p_basis.elements[0][1];
  37. p_array[5] = p_basis.elements[1][1];
  38. p_array[6] = p_basis.elements[2][1];
  39. p_array[7] = 0;
  40. p_array[8] = p_basis.elements[0][2];
  41. p_array[9] = p_basis.elements[1][2];
  42. p_array[10] = p_basis.elements[2][2];
  43. p_array[11] = 0;
  44. }
  45. RID RasterizerEffectsRD::_get_uniform_set_from_image(RID p_image) {
  46. if (image_to_uniform_set_cache.has(p_image)) {
  47. RID uniform_set = image_to_uniform_set_cache[p_image];
  48. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  49. return uniform_set;
  50. }
  51. }
  52. Vector<RD::Uniform> uniforms;
  53. RD::Uniform u;
  54. u.type = RD::UNIFORM_TYPE_IMAGE;
  55. u.binding = 0;
  56. u.ids.push_back(p_image);
  57. uniforms.push_back(u);
  58. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  59. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1);
  60. image_to_uniform_set_cache[p_image] = uniform_set;
  61. return uniform_set;
  62. }
  63. RID RasterizerEffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  64. if (texture_to_uniform_set_cache.has(p_texture)) {
  65. RID uniform_set = texture_to_uniform_set_cache[p_texture];
  66. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  67. return uniform_set;
  68. }
  69. }
  70. Vector<RD::Uniform> uniforms;
  71. RD::Uniform u;
  72. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  73. u.binding = 0;
  74. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  75. u.ids.push_back(p_texture);
  76. uniforms.push_back(u);
  77. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  78. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, blur.shader.version_get_shader(blur.shader_version, 0), 0);
  79. texture_to_uniform_set_cache[p_texture] = uniform_set;
  80. return uniform_set;
  81. }
  82. RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) {
  83. if (texture_to_compute_uniform_set_cache.has(p_texture)) {
  84. RID uniform_set = texture_to_compute_uniform_set_cache[p_texture];
  85. if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  86. return uniform_set;
  87. }
  88. }
  89. Vector<RD::Uniform> uniforms;
  90. RD::Uniform u;
  91. u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  92. u.binding = 0;
  93. u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler);
  94. u.ids.push_back(p_texture);
  95. uniforms.push_back(u);
  96. //any thing with the same configuration (one texture in binding 0 for set 0), is good
  97. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0);
  98. texture_to_compute_uniform_set_cache[p_texture] = uniform_set;
  99. return uniform_set;
  100. }
  101. void RasterizerEffectsRD::copy_to_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_rect, bool p_flip_y) {
  102. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  103. if (p_flip_y) {
  104. blur.push_constant.flags |= BLUR_FLAG_FLIP_Y;
  105. }
  106. 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);
  107. 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)));
  108. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  109. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  110. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  111. RD::get_singleton()->draw_list_draw(draw_list, true);
  112. RD::get_singleton()->draw_list_end();
  113. }
  114. void RasterizerEffectsRD::region_copy(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_region) {
  115. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  116. if (p_region != Rect2()) {
  117. blur.push_constant.flags = BLUR_FLAG_USE_BLUR_SECTION;
  118. blur.push_constant.section[0] = p_region.position.x;
  119. blur.push_constant.section[1] = p_region.position.y;
  120. blur.push_constant.section[2] = p_region.size.width;
  121. blur.push_constant.section[3] = p_region.size.height;
  122. }
  123. 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);
  124. 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)));
  125. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  126. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  127. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  128. RD::get_singleton()->draw_list_draw(draw_list, true);
  129. RD::get_singleton()->draw_list_end();
  130. }
  131. 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) {
  132. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  133. uint32_t base_flags = 0;
  134. if (p_region != Rect2()) {
  135. base_flags = BLUR_FLAG_USE_BLUR_SECTION;
  136. blur.push_constant.section[0] = p_region.position.x;
  137. blur.push_constant.section[1] = p_region.position.y;
  138. blur.push_constant.section[2] = p_region.size.width;
  139. blur.push_constant.section[3] = p_region.size.height;
  140. }
  141. blur.push_constant.pixel_size[0] = p_pixel_size.x;
  142. blur.push_constant.pixel_size[1] = p_pixel_size.y;
  143. //HORIZONTAL
  144. 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);
  145. 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)));
  146. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  147. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  148. blur.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL;
  149. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  150. RD::get_singleton()->draw_list_draw(draw_list, true);
  151. RD::get_singleton()->draw_list_end();
  152. //VERTICAL
  153. 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);
  154. 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)));
  155. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_rd_texture_half), 0);
  156. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  157. blur.push_constant.flags = base_flags;
  158. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  159. RD::get_singleton()->draw_list_draw(draw_list, true);
  160. RD::get_singleton()->draw_list_end();
  161. }
  162. 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) {
  163. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  164. BlurMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
  165. uint32_t base_flags = 0;
  166. blur.push_constant.pixel_size[0] = p_pixel_size.x;
  167. blur.push_constant.pixel_size[1] = p_pixel_size.y;
  168. blur.push_constant.glow_strength = p_strength;
  169. blur.push_constant.glow_bloom = p_bloom;
  170. blur.push_constant.glow_hdr_threshold = p_hdr_bleed_treshold;
  171. blur.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
  172. blur.push_constant.glow_exposure = p_exposure;
  173. blur.push_constant.glow_white = 0; //actually unused
  174. blur.push_constant.glow_luminance_cap = p_luminance_cap;
  175. blur.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also
  176. //HORIZONTAL
  177. 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);
  178. 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)));
  179. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  180. if (p_auto_exposure.is_valid() && p_first_pass) {
  181. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_auto_exposure), 1);
  182. }
  183. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  184. blur.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
  185. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  186. RD::get_singleton()->draw_list_draw(draw_list, true);
  187. RD::get_singleton()->draw_list_end();
  188. blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
  189. //VERTICAL
  190. 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);
  191. 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)));
  192. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_rd_texture_half), 0);
  193. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  194. blur.push_constant.flags = base_flags;
  195. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  196. RD::get_singleton()->draw_list_draw(draw_list, true);
  197. RD::get_singleton()->draw_list_end();
  198. }
  199. 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) {
  200. zeromem(&roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  201. roughness.push_constant.face_id = p_face_id;
  202. roughness.push_constant.roughness = p_roughness;
  203. roughness.push_constant.sample_count = p_sample_count;
  204. roughness.push_constant.use_direct_write = p_roughness == 0.0;
  205. //RUN
  206. 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);
  207. 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)));
  208. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  209. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  210. RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
  211. RD::get_singleton()->draw_list_draw(draw_list, true);
  212. RD::get_singleton()->draw_list_end();
  213. }
  214. 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) {
  215. zeromem(&sky.push_constant, sizeof(SkyPushConstant));
  216. sky.push_constant.proj[0] = p_camera.matrix[2][0];
  217. sky.push_constant.proj[1] = p_camera.matrix[0][0];
  218. sky.push_constant.proj[2] = p_camera.matrix[2][1];
  219. sky.push_constant.proj[3] = p_camera.matrix[1][1];
  220. sky.push_constant.alpha = p_alpha;
  221. sky.push_constant.depth = 1.0;
  222. sky.push_constant.multiplier = p_multipler;
  223. store_transform_3x3(p_orientation, sky.push_constant.orientation);
  224. RD::DrawListID draw_list = p_list;
  225. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, sky.pipeline.get_render_pipeline(RD::INVALID_ID, p_fb_format));
  226. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_panorama), 0);
  227. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  228. RD::get_singleton()->draw_list_set_push_constant(draw_list, &sky.push_constant, sizeof(SkyPushConstant));
  229. RD::get_singleton()->draw_list_draw(draw_list, true);
  230. }
  231. void RasterizerEffectsRD::make_mipmap(RID p_source_rd_texture, RID p_dest_framebuffer, const Vector2 &p_pixel_size) {
  232. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  233. blur.push_constant.pixel_size[0] = p_pixel_size.x;
  234. blur.push_constant.pixel_size[1] = p_pixel_size.y;
  235. 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);
  236. 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)));
  237. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  238. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  239. RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur.push_constant, sizeof(BlurPushConstant));
  240. RD::get_singleton()->draw_list_draw(draw_list, true);
  241. RD::get_singleton()->draw_list_end();
  242. }
  243. 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) {
  244. CopyToDPPushConstant push_constant;
  245. push_constant.bias = p_bias;
  246. push_constant.z_far = p_z_far;
  247. push_constant.z_near = p_z_near;
  248. push_constant.z_flip = p_dp_flip;
  249. 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);
  250. 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)));
  251. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_rd_texture), 0);
  252. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  253. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
  254. RD::get_singleton()->draw_list_draw(draw_list, true);
  255. RD::get_singleton()->draw_list_end();
  256. }
  257. void RasterizerEffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
  258. zeromem(&tonemap.push_constant, sizeof(TonemapPushConstant));
  259. tonemap.push_constant.use_bcs = p_settings.use_bcs;
  260. tonemap.push_constant.bcs[0] = p_settings.brightness;
  261. tonemap.push_constant.bcs[1] = p_settings.contrast;
  262. tonemap.push_constant.bcs[2] = p_settings.saturation;
  263. tonemap.push_constant.use_glow = p_settings.use_glow;
  264. tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
  265. tonemap.push_constant.glow_level_flags = p_settings.glow_level_flags;
  266. tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
  267. tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
  268. tonemap.push_constant.glow_mode = p_settings.glow_mode;
  269. TonemapMode mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
  270. tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
  271. tonemap.push_constant.use_auto_exposure = p_settings.use_auto_exposure;
  272. tonemap.push_constant.exposure = p_settings.exposure;
  273. tonemap.push_constant.white = p_settings.white;
  274. tonemap.push_constant.auto_exposure_grey = p_settings.auto_exposure_grey;
  275. tonemap.push_constant.use_color_correction = p_settings.use_color_correction;
  276. 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);
  277. 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)));
  278. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_source_color), 0);
  279. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.exposure_texture), 1);
  280. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.glow_texture, true), 2);
  281. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, _get_uniform_set_from_texture(p_settings.color_correction_texture), 3);
  282. RD::get_singleton()->draw_list_bind_index_array(draw_list, index_array);
  283. RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
  284. RD::get_singleton()->draw_list_draw(draw_list, true);
  285. RD::get_singleton()->draw_list_end();
  286. }
  287. 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) {
  288. luminance_reduce.push_constant.source_size[0] = p_source_size.x;
  289. luminance_reduce.push_constant.source_size[1] = p_source_size.y;
  290. luminance_reduce.push_constant.max_luminance = p_max_luminance;
  291. luminance_reduce.push_constant.min_luminance = p_min_luminance;
  292. luminance_reduce.push_constant.exposure_adjust = p_adjust;
  293. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  294. for (int i = 0; i < p_reduce.size(); i++) {
  295. if (i == 0) {
  296. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]);
  297. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0);
  298. } else {
  299. RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done
  300. if (i == p_reduce.size() - 1 && !p_set) {
  301. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]);
  302. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2);
  303. } else {
  304. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]);
  305. }
  306. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0);
  307. }
  308. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1);
  309. RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant));
  310. int32_t x_groups = (luminance_reduce.push_constant.source_size[0] - 1) / 8 + 1;
  311. int32_t y_groups = (luminance_reduce.push_constant.source_size[1] - 1) / 8 + 1;
  312. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  313. luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1);
  314. luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1);
  315. }
  316. RD::get_singleton()->compute_list_end();
  317. }
  318. void RasterizerEffectsRD::bokeh_dof(RID p_base_texture, RID p_depth_texture, const Size2i &p_base_texture_size, RID p_bokeh_texture, 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, VS::DOFBlurQuality p_quality, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
  319. bokeh.push_constant.blur_far_active = p_dof_far;
  320. bokeh.push_constant.blur_far_begin = p_dof_far_begin;
  321. bokeh.push_constant.blur_far_end = p_dof_far_begin + p_dof_far_size;
  322. bokeh.push_constant.blur_near_active = p_dof_near;
  323. bokeh.push_constant.blur_near_begin = p_dof_near_begin;
  324. bokeh.push_constant.blur_near_end = MAX(0, p_dof_near_begin - p_dof_near_size);
  325. bokeh.push_constant.size[0] = p_base_texture_size.x;
  326. bokeh.push_constant.size[1] = p_base_texture_size.y;
  327. bokeh.push_constant.z_near = p_cam_znear;
  328. bokeh.push_constant.z_far = p_cam_zfar;
  329. bokeh.push_constant.orthogonal = p_cam_orthogonal;
  330. bokeh.push_constant.blur_size = p_bokeh_size;
  331. bokeh.push_constant.blur_scale = 0.5;
  332. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  333. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BLUR_SIZE]);
  334. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  335. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  336. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  337. int32_t x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  338. int32_t y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  339. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  340. RD::get_singleton()->compute_list_add_barrier(compute_list);
  341. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_GEN_BOKEH]);
  342. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_bokeh_texture), 0);
  343. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_depth_texture), 1);
  344. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_base_texture), 2);
  345. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  346. x_groups = ((p_base_texture_size.x >> 1) - 1) / 8 + 1;
  347. y_groups = ((p_base_texture_size.y >> 1) - 1) / 8 + 1;
  348. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  349. RD::get_singleton()->compute_list_add_barrier(compute_list);
  350. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.pipelines[BOKEH_COMPOSITE]);
  351. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_base_texture), 0);
  352. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_bokeh_texture), 1);
  353. RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
  354. x_groups = (p_base_texture_size.x - 1) / 8 + 1;
  355. y_groups = (p_base_texture_size.y - 1) / 8 + 1;
  356. RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1);
  357. RD::get_singleton()->compute_list_add_barrier(compute_list);
  358. RD::get_singleton()->compute_list_end();
  359. }
  360. RasterizerEffectsRD::RasterizerEffectsRD() {
  361. {
  362. // Initialize blur
  363. Vector<String> blur_modes;
  364. blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
  365. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n");
  366. blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
  367. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_LOW\n");
  368. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_MEDIUM\n");
  369. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_HIGH\n");
  370. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_LOW\n#define DOF_NEAR_BLUR_MERGE\n");
  371. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_MEDIUM\n#define DOF_NEAR_BLUR_MERGE\n");
  372. blur_modes.push_back("\n#define MODE_DOF_NEAR_BLUR\n#define DOF_QUALITY_HIGH\n#define DOF_NEAR_BLUR_MERGE\n");
  373. blur_modes.push_back("\n#define MODE_DOF_FAR_BLUR\n#define DOF_QUALITY_LOW\n");
  374. blur_modes.push_back("\n#define MODE_DOF_FAR_BLUR\n#define DOF_QUALITY_MEDIUM\n");
  375. blur_modes.push_back("\n#define MODE_DOF_FAR_BLUR\n#define DOF_QUALITY_HIGH\n");
  376. blur_modes.push_back("\n#define MODE_SSAO_MERGE\n");
  377. blur_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
  378. blur_modes.push_back("\n#define MODE_MIPMAP\n");
  379. blur.shader.initialize(blur_modes);
  380. zeromem(&blur.push_constant, sizeof(BlurPushConstant));
  381. blur.shader_version = blur.shader.version_create();
  382. for (int i = 0; i < BLUR_MODE_MAX; i++) {
  383. 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);
  384. }
  385. }
  386. {
  387. // Initialize roughness
  388. Vector<String> cubemap_roughness_modes;
  389. cubemap_roughness_modes.push_back("\n#define MODE_SOURCE_PANORAMA\n");
  390. cubemap_roughness_modes.push_back("\n#define MODE_SOURCE_CUBEMAP\n");
  391. roughness.shader.initialize(cubemap_roughness_modes);
  392. roughness.shader_version = roughness.shader.version_create();
  393. for (int i = 0; i < CUBEMAP_ROUGHNESS_SOURCE_MAX; i++) {
  394. 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);
  395. }
  396. }
  397. {
  398. // Initialize sky
  399. Vector<String> sky_modes;
  400. sky_modes.push_back("");
  401. sky.shader.initialize(sky_modes);
  402. sky.shader_version = sky.shader.version_create();
  403. RD::PipelineDepthStencilState depth_stencil_state;
  404. depth_stencil_state.enable_depth_test = true;
  405. depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  406. 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);
  407. }
  408. {
  409. // Initialize tonemapper
  410. Vector<String> tonemap_modes;
  411. tonemap_modes.push_back("\n");
  412. tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
  413. tonemap.shader.initialize(tonemap_modes);
  414. tonemap.shader_version = tonemap.shader.version_create();
  415. for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
  416. 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);
  417. }
  418. }
  419. {
  420. // Initialize luminance_reduce
  421. Vector<String> luminance_reduce_modes;
  422. luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n");
  423. luminance_reduce_modes.push_back("\n");
  424. luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n");
  425. luminance_reduce.shader.initialize(luminance_reduce_modes);
  426. luminance_reduce.shader_version = luminance_reduce.shader.version_create();
  427. for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) {
  428. luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i));
  429. }
  430. }
  431. {
  432. // Initialize copier
  433. Vector<String> copy_modes;
  434. copy_modes.push_back("\n#define MODE_CUBE_TO_DP\n");
  435. copy.shader.initialize(copy_modes);
  436. copy.shader_version = copy.shader.version_create();
  437. for (int i = 0; i < COPY_MODE_MAX; i++) {
  438. 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);
  439. }
  440. }
  441. {
  442. // Initialize bokeh
  443. Vector<String> bokeh_modes;
  444. bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
  445. bokeh_modes.push_back("\n#define MODE_GEN_BOKEH\n");
  446. bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
  447. bokeh.shader.initialize(bokeh_modes);
  448. bokeh.shader_version = bokeh.shader.version_create();
  449. for (int i = 0; i < BOKEH_MAX; i++) {
  450. bokeh.pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.shader.version_get_shader(bokeh.shader_version, i));
  451. }
  452. }
  453. RD::SamplerState sampler;
  454. sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  455. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  456. sampler.max_lod = 0;
  457. default_sampler = RD::get_singleton()->sampler_create(sampler);
  458. sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
  459. sampler.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  460. sampler.max_lod = 1e20;
  461. default_mipmap_sampler = RD::get_singleton()->sampler_create(sampler);
  462. { //create index array for copy shaders
  463. PoolVector<uint8_t> pv;
  464. pv.resize(6 * 4);
  465. {
  466. PoolVector<uint8_t>::Write w = pv.write();
  467. int *p32 = (int *)w.ptr();
  468. p32[0] = 0;
  469. p32[1] = 1;
  470. p32[2] = 2;
  471. p32[3] = 0;
  472. p32[4] = 2;
  473. p32[5] = 3;
  474. }
  475. index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  476. index_array = RD::get_singleton()->index_array_create(index_buffer, 0, 6);
  477. }
  478. }
  479. RasterizerEffectsRD::~RasterizerEffectsRD() {
  480. RD::get_singleton()->free(default_sampler);
  481. blur.shader.version_free(blur.shader_version);
  482. RD::get_singleton()->free(index_buffer); //array gets freed as dependency
  483. }