ss_effects.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /**************************************************************************/
  2. /* ss_effects.h */
  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. #ifndef SS_EFFECTS_RD_H
  31. #define SS_EFFECTS_RD_H
  32. #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection.glsl.gen.h"
  33. #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_filter.glsl.gen.h"
  34. #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_scale.glsl.gen.h"
  35. #include "servers/rendering/renderer_rd/shaders/effects/ss_effects_downsample.glsl.gen.h"
  36. #include "servers/rendering/renderer_rd/shaders/effects/ssao.glsl.gen.h"
  37. #include "servers/rendering/renderer_rd/shaders/effects/ssao_blur.glsl.gen.h"
  38. #include "servers/rendering/renderer_rd/shaders/effects/ssao_importance_map.glsl.gen.h"
  39. #include "servers/rendering/renderer_rd/shaders/effects/ssao_interleave.glsl.gen.h"
  40. #include "servers/rendering/renderer_rd/shaders/effects/ssil.glsl.gen.h"
  41. #include "servers/rendering/renderer_rd/shaders/effects/ssil_blur.glsl.gen.h"
  42. #include "servers/rendering/renderer_rd/shaders/effects/ssil_importance_map.glsl.gen.h"
  43. #include "servers/rendering/renderer_rd/shaders/effects/ssil_interleave.glsl.gen.h"
  44. #include "servers/rendering/renderer_rd/shaders/effects/subsurface_scattering.glsl.gen.h"
  45. #include "servers/rendering_server.h"
  46. #define RB_SCOPE_SSDS SNAME("rb_ssds")
  47. #define RB_SCOPE_SSIL SNAME("rb_ssil")
  48. #define RB_SCOPE_SSAO SNAME("rb_ssao")
  49. #define RB_SCOPE_SSR SNAME("rb_ssr")
  50. #define RB_LINEAR_DEPTH SNAME("linear_depth")
  51. #define RB_FINAL SNAME("final")
  52. #define RB_LAST_FRAME SNAME("last_frame")
  53. #define RB_DEINTERLEAVED SNAME("deinterleaved")
  54. #define RB_DEINTERLEAVED_PONG SNAME("deinterleaved_pong")
  55. #define RB_EDGES SNAME("edges")
  56. #define RB_IMPORTANCE_MAP SNAME("importance_map")
  57. #define RB_IMPORTANCE_PONG SNAME("importance_pong")
  58. #define RB_DEPTH_SCALED SNAME("depth_scaled")
  59. #define RB_NORMAL_SCALED SNAME("normal_scaled")
  60. #define RB_BLUR_RADIUS SNAME("blur_radius")
  61. #define RB_INTERMEDIATE SNAME("intermediate")
  62. #define RB_OUTPUT SNAME("output")
  63. class RenderSceneBuffersRD;
  64. namespace RendererRD {
  65. class SSEffects {
  66. private:
  67. static SSEffects *singleton;
  68. public:
  69. static SSEffects *get_singleton() { return singleton; }
  70. SSEffects();
  71. ~SSEffects();
  72. /* SS Downsampler */
  73. void downsample_depth(Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_view, const Projection &p_projection);
  74. /* SSIL */
  75. void ssil_set_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to);
  76. struct SSILRenderBuffers {
  77. bool half_size = false;
  78. int buffer_width;
  79. int buffer_height;
  80. int half_buffer_width;
  81. int half_buffer_height;
  82. };
  83. struct SSILSettings {
  84. float radius = 1.0;
  85. float intensity = 2.0;
  86. float sharpness = 0.98;
  87. float normal_rejection = 1.0;
  88. Size2i full_screen_size;
  89. };
  90. void ssil_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, const SSILSettings &p_settings);
  91. void screen_space_indirect_lighting(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, uint32_t p_view, RID p_normal_buffer, const Projection &p_projection, const Projection &p_last_projection, const SSILSettings &p_settings);
  92. /* SSAO */
  93. void ssao_set_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to);
  94. struct SSAORenderBuffers {
  95. bool half_size = false;
  96. int buffer_width;
  97. int buffer_height;
  98. int half_buffer_width;
  99. int half_buffer_height;
  100. };
  101. struct SSAOSettings {
  102. float radius = 1.0;
  103. float intensity = 2.0;
  104. float power = 1.5;
  105. float detail = 0.5;
  106. float horizon = 0.06;
  107. float sharpness = 0.98;
  108. Size2i full_screen_size;
  109. };
  110. void ssao_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, const SSAOSettings &p_settings);
  111. void generate_ssao(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, uint32_t p_view, RID p_normal_buffer, const Projection &p_projection, const SSAOSettings &p_settings);
  112. /* Screen Space Reflection */
  113. void ssr_set_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality);
  114. struct SSRRenderBuffers {
  115. Size2i size;
  116. RenderingServer::EnvironmentSSRRoughnessQuality roughness_quality = RenderingServer::ENV_SSR_ROUGHNESS_QUALITY_DISABLED;
  117. };
  118. void ssr_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RenderingDevice::DataFormat p_color_format);
  119. void screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RID *p_normal_roughness_slices, const RID *p_metallic_slices, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const Projection *p_projections, const Vector3 *p_eye_offsets);
  120. /* subsurface scattering */
  121. void sss_set_quality(RS::SubSurfaceScatteringQuality p_quality);
  122. RS::SubSurfaceScatteringQuality sss_get_quality() const;
  123. void sss_set_scale(float p_scale, float p_depth_scale);
  124. void sub_surface_scattering(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_diffuse, RID p_depth, const Projection &p_camera, const Size2i &p_screen_size);
  125. private:
  126. /* Settings */
  127. RS::EnvironmentSSAOQuality ssao_quality = RS::ENV_SSAO_QUALITY_MEDIUM;
  128. bool ssao_half_size = false;
  129. float ssao_adaptive_target = 0.5;
  130. int ssao_blur_passes = 2;
  131. float ssao_fadeout_from = 50.0;
  132. float ssao_fadeout_to = 300.0;
  133. RS::EnvironmentSSILQuality ssil_quality = RS::ENV_SSIL_QUALITY_MEDIUM;
  134. bool ssil_half_size = false;
  135. float ssil_adaptive_target = 0.5;
  136. int ssil_blur_passes = 4;
  137. float ssil_fadeout_from = 50.0;
  138. float ssil_fadeout_to = 300.0;
  139. RS::EnvironmentSSRRoughnessQuality ssr_roughness_quality = RS::ENV_SSR_ROUGHNESS_QUALITY_LOW;
  140. RS::SubSurfaceScatteringQuality sss_quality = RS::SUB_SURFACE_SCATTERING_QUALITY_MEDIUM;
  141. float sss_scale = 0.05;
  142. float sss_depth_scale = 0.01;
  143. /* SS Downsampler */
  144. struct SSEffectsDownsamplePushConstant {
  145. float pixel_size[2];
  146. float z_far;
  147. float z_near;
  148. uint32_t orthogonal;
  149. float radius_sq;
  150. uint32_t pad[2];
  151. };
  152. enum SSEffectsMode {
  153. SS_EFFECTS_DOWNSAMPLE,
  154. SS_EFFECTS_DOWNSAMPLE_HALF_RES,
  155. SS_EFFECTS_DOWNSAMPLE_MIPMAP,
  156. SS_EFFECTS_DOWNSAMPLE_MIPMAP_HALF_RES,
  157. SS_EFFECTS_DOWNSAMPLE_HALF,
  158. SS_EFFECTS_DOWNSAMPLE_HALF_RES_HALF,
  159. SS_EFFECTS_DOWNSAMPLE_FULL_MIPS,
  160. SS_EFFECTS_MAX
  161. };
  162. struct SSEffectsGatherConstants {
  163. float rotation_matrices[80]; //5 vec4s * 4
  164. };
  165. struct SSEffectsShader {
  166. SSEffectsDownsamplePushConstant downsample_push_constant;
  167. SsEffectsDownsampleShaderRD downsample_shader;
  168. RID downsample_shader_version;
  169. bool used_half_size_last_frame = false;
  170. bool used_mips_last_frame = false;
  171. bool used_full_mips_last_frame = false;
  172. RID gather_constants_buffer;
  173. RID mirror_sampler;
  174. RID pipelines[SS_EFFECTS_MAX];
  175. } ss_effects;
  176. /* SSIL */
  177. enum SSILMode {
  178. SSIL_GATHER,
  179. SSIL_GATHER_BASE,
  180. SSIL_GATHER_ADAPTIVE,
  181. SSIL_GENERATE_IMPORTANCE_MAP,
  182. SSIL_PROCESS_IMPORTANCE_MAPA,
  183. SSIL_PROCESS_IMPORTANCE_MAPB,
  184. SSIL_BLUR_PASS,
  185. SSIL_BLUR_PASS_SMART,
  186. SSIL_BLUR_PASS_WIDE,
  187. SSIL_INTERLEAVE,
  188. SSIL_INTERLEAVE_SMART,
  189. SSIL_INTERLEAVE_HALF,
  190. SSIL_MAX
  191. };
  192. struct SSILGatherPushConstant {
  193. int32_t screen_size[2];
  194. int pass;
  195. int quality;
  196. float half_screen_pixel_size[2];
  197. float half_screen_pixel_size_x025[2];
  198. float NDC_to_view_mul[2];
  199. float NDC_to_view_add[2];
  200. float pad2[2];
  201. float z_near;
  202. float z_far;
  203. float radius;
  204. float intensity;
  205. int size_multiplier;
  206. int pad;
  207. float fade_out_mul;
  208. float fade_out_add;
  209. float normal_rejection_amount;
  210. float inv_radius_near_limit;
  211. uint32_t is_orthogonal;
  212. float neg_inv_radius;
  213. float load_counter_avg_div;
  214. float adaptive_sample_limit;
  215. int32_t pass_coord_offset[2];
  216. float pass_uv_offset[2];
  217. };
  218. struct SSILImportanceMapPushConstant {
  219. float half_screen_pixel_size[2];
  220. float intensity;
  221. float pad;
  222. };
  223. struct SSILBlurPushConstant {
  224. float edge_sharpness;
  225. float pad;
  226. float half_screen_pixel_size[2];
  227. };
  228. struct SSILInterleavePushConstant {
  229. float inv_sharpness;
  230. uint32_t size_modifier;
  231. float pixel_size[2];
  232. };
  233. struct SSILProjectionUniforms {
  234. float inv_last_frame_projection_matrix[16];
  235. };
  236. struct SSIL {
  237. SSILGatherPushConstant gather_push_constant;
  238. SsilShaderRD gather_shader;
  239. RID gather_shader_version;
  240. RID projection_uniform_buffer;
  241. SSILImportanceMapPushConstant importance_map_push_constant;
  242. SsilImportanceMapShaderRD importance_map_shader;
  243. RID importance_map_shader_version;
  244. RID importance_map_load_counter;
  245. RID counter_uniform_set;
  246. SSILBlurPushConstant blur_push_constant;
  247. SsilBlurShaderRD blur_shader;
  248. RID blur_shader_version;
  249. SSILInterleavePushConstant interleave_push_constant;
  250. SsilInterleaveShaderRD interleave_shader;
  251. RID interleave_shader_version;
  252. RID pipelines[SSIL_MAX];
  253. } ssil;
  254. void gather_ssil(RD::ComputeListID p_compute_list, const RID *p_ssil_slices, const RID *p_edges_slices, const SSILSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set, RID p_projection_uniform_set);
  255. /* SSAO */
  256. enum SSAOMode {
  257. SSAO_GATHER,
  258. SSAO_GATHER_BASE,
  259. SSAO_GATHER_ADAPTIVE,
  260. SSAO_GENERATE_IMPORTANCE_MAP,
  261. SSAO_PROCESS_IMPORTANCE_MAPA,
  262. SSAO_PROCESS_IMPORTANCE_MAPB,
  263. SSAO_BLUR_PASS,
  264. SSAO_BLUR_PASS_SMART,
  265. SSAO_BLUR_PASS_WIDE,
  266. SSAO_INTERLEAVE,
  267. SSAO_INTERLEAVE_SMART,
  268. SSAO_INTERLEAVE_HALF,
  269. SSAO_MAX
  270. };
  271. struct SSAOGatherPushConstant {
  272. int32_t screen_size[2];
  273. int pass;
  274. int quality;
  275. float half_screen_pixel_size[2];
  276. int size_multiplier;
  277. float detail_intensity;
  278. float NDC_to_view_mul[2];
  279. float NDC_to_view_add[2];
  280. float pad[2];
  281. float half_screen_pixel_size_x025[2];
  282. float radius;
  283. float intensity;
  284. float shadow_power;
  285. float shadow_clamp;
  286. float fade_out_mul;
  287. float fade_out_add;
  288. float horizon_angle_threshold;
  289. float inv_radius_near_limit;
  290. uint32_t is_orthogonal;
  291. float neg_inv_radius;
  292. float load_counter_avg_div;
  293. float adaptive_sample_limit;
  294. int32_t pass_coord_offset[2];
  295. float pass_uv_offset[2];
  296. };
  297. struct SSAOImportanceMapPushConstant {
  298. float half_screen_pixel_size[2];
  299. float intensity;
  300. float power;
  301. };
  302. struct SSAOBlurPushConstant {
  303. float edge_sharpness;
  304. float pad;
  305. float half_screen_pixel_size[2];
  306. };
  307. struct SSAOInterleavePushConstant {
  308. float inv_sharpness;
  309. uint32_t size_modifier;
  310. float pixel_size[2];
  311. };
  312. struct SSAO {
  313. SSAOGatherPushConstant gather_push_constant;
  314. SsaoShaderRD gather_shader;
  315. RID gather_shader_version;
  316. SSAOImportanceMapPushConstant importance_map_push_constant;
  317. SsaoImportanceMapShaderRD importance_map_shader;
  318. RID importance_map_shader_version;
  319. RID importance_map_load_counter;
  320. RID counter_uniform_set;
  321. SSAOBlurPushConstant blur_push_constant;
  322. SsaoBlurShaderRD blur_shader;
  323. RID blur_shader_version;
  324. SSAOInterleavePushConstant interleave_push_constant;
  325. SsaoInterleaveShaderRD interleave_shader;
  326. RID interleave_shader_version;
  327. RID pipelines[SSAO_MAX];
  328. } ssao;
  329. void gather_ssao(RD::ComputeListID p_compute_list, const RID *p_ao_slices, const SSAOSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set);
  330. /* Screen Space Reflection */
  331. enum SSRShaderSpecializations {
  332. SSR_MULTIVIEW = 1 << 0,
  333. SSR_VARIATIONS = 2,
  334. };
  335. struct ScreenSpaceReflectionSceneData {
  336. float projection[2][16];
  337. float inv_projection[2][16];
  338. float eye_offset[2][4];
  339. };
  340. // SSR Scale
  341. struct ScreenSpaceReflectionScalePushConstant {
  342. int32_t screen_size[2];
  343. float camera_z_near;
  344. float camera_z_far;
  345. uint32_t orthogonal;
  346. uint32_t filter;
  347. uint32_t view_index;
  348. uint32_t pad1;
  349. };
  350. struct ScreenSpaceReflectionScale {
  351. ScreenSpaceReflectionScaleShaderRD shader;
  352. RID shader_version;
  353. RID pipelines[SSR_VARIATIONS];
  354. } ssr_scale;
  355. // SSR main
  356. enum ScreenSpaceReflectionMode {
  357. SCREEN_SPACE_REFLECTION_NORMAL,
  358. SCREEN_SPACE_REFLECTION_ROUGH,
  359. SCREEN_SPACE_REFLECTION_MAX,
  360. };
  361. struct ScreenSpaceReflectionPushConstant {
  362. float proj_info[4]; // 16 - 16
  363. int32_t screen_size[2]; // 8 - 24
  364. float camera_z_near; // 4 - 28
  365. float camera_z_far; // 4 - 32
  366. int32_t num_steps; // 4 - 36
  367. float depth_tolerance; // 4 - 40
  368. float distance_fade; // 4 - 44
  369. float curve_fade_in; // 4 - 48
  370. uint32_t orthogonal; // 4 - 52
  371. float filter_mipmap_levels; // 4 - 56
  372. uint32_t use_half_res; // 4 - 60
  373. uint32_t view_index; // 4 - 64
  374. // float projection[16]; // this is in our ScreenSpaceReflectionSceneData now
  375. };
  376. struct ScreenSpaceReflection {
  377. ScreenSpaceReflectionShaderRD shader;
  378. RID shader_version;
  379. RID pipelines[SSR_VARIATIONS][SCREEN_SPACE_REFLECTION_MAX];
  380. RID ubo;
  381. } ssr;
  382. // SSR Filter
  383. struct ScreenSpaceReflectionFilterPushConstant {
  384. float proj_info[4]; // 16 - 16
  385. uint32_t orthogonal; // 4 - 20
  386. float edge_tolerance; // 4 - 24
  387. int32_t increment; // 4 - 28
  388. uint32_t view_index; // 4 - 32
  389. int32_t screen_size[2]; // 8 - 40
  390. uint32_t vertical; // 4 - 44
  391. uint32_t steps; // 4 - 48
  392. };
  393. enum SSRReflectionMode {
  394. SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL,
  395. SCREEN_SPACE_REFLECTION_FILTER_VERTICAL,
  396. SCREEN_SPACE_REFLECTION_FILTER_MAX,
  397. };
  398. struct ScreenSpaceReflectionFilter {
  399. ScreenSpaceReflectionFilterShaderRD shader;
  400. RID shader_version;
  401. RID pipelines[SSR_VARIATIONS][SCREEN_SPACE_REFLECTION_FILTER_MAX];
  402. } ssr_filter;
  403. /* Subsurface scattering */
  404. struct SubSurfaceScatteringPushConstant {
  405. int32_t screen_size[2];
  406. float camera_z_far;
  407. float camera_z_near;
  408. uint32_t vertical;
  409. uint32_t orthogonal;
  410. float unit_size;
  411. float scale;
  412. float depth_scale;
  413. uint32_t pad[3];
  414. };
  415. struct SubSurfaceScattering {
  416. SubSurfaceScatteringPushConstant push_constant;
  417. SubsurfaceScatteringShaderRD shader;
  418. RID shader_version;
  419. RID pipelines[3]; //3 quality levels
  420. } sss;
  421. };
  422. } // namespace RendererRD
  423. #endif // SS_EFFECTS_RD_H