ss_effects.h 16 KB

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