Path: blob/master/servers/rendering/renderer_rd/effects/ss_effects.h
10279 views
/**************************************************************************/1/* ss_effects.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#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"4647#define RB_SCOPE_SSDS SNAME("rb_ssds")48#define RB_SCOPE_SSIL SNAME("rb_ssil")49#define RB_SCOPE_SSAO SNAME("rb_ssao")50#define RB_SCOPE_SSR SNAME("rb_ssr")5152#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")6061#define RB_DEPTH_SCALED SNAME("depth_scaled")62#define RB_NORMAL_SCALED SNAME("normal_scaled")63#define RB_BLUR_RADIUS SNAME("blur_radius")64#define RB_INTERMEDIATE SNAME("intermediate")65#define RB_OUTPUT SNAME("output")6667class RenderSceneBuffersRD;6869namespace RendererRD {7071class SSEffects {72private:73static SSEffects *singleton;7475public:76static SSEffects *get_singleton() { return singleton; }7778SSEffects();79~SSEffects();8081/* SS Downsampler */8283void downsample_depth(Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_view, const Projection &p_projection);8485/* SSIL */86void 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);8788struct SSILRenderBuffers {89bool half_size = false;90int buffer_width;91int buffer_height;92int half_buffer_width;93int half_buffer_height;94};9596struct SSILSettings {97float radius = 1.0;98float intensity = 2.0;99float sharpness = 0.98;100float normal_rejection = 1.0;101102Size2i full_screen_size;103};104105void ssil_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, const SSILSettings &p_settings);106void 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);107108/* SSAO */109void 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);110111struct SSAORenderBuffers {112bool half_size = false;113int buffer_width;114int buffer_height;115int half_buffer_width;116int half_buffer_height;117};118119struct SSAOSettings {120float radius = 1.0;121float intensity = 2.0;122float power = 1.5;123float detail = 0.5;124float horizon = 0.06;125float sharpness = 0.98;126127Size2i full_screen_size;128};129130void ssao_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, const SSAOSettings &p_settings);131void 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);132133/* Screen Space Reflection */134void ssr_set_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality);135136struct SSRRenderBuffers {137Size2i size;138RenderingServer::EnvironmentSSRRoughnessQuality roughness_quality = RenderingServer::ENV_SSR_ROUGHNESS_QUALITY_DISABLED;139};140141void ssr_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RenderingDevice::DataFormat p_color_format);142void 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);143144/* subsurface scattering */145void sss_set_quality(RS::SubSurfaceScatteringQuality p_quality);146RS::SubSurfaceScatteringQuality sss_get_quality() const;147void sss_set_scale(float p_scale, float p_depth_scale);148149void sub_surface_scattering(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_diffuse, RID p_depth, const Projection &p_camera, const Size2i &p_screen_size);150151private:152/* Settings */153154RS::EnvironmentSSAOQuality ssao_quality = RS::ENV_SSAO_QUALITY_MEDIUM;155bool ssao_half_size = false;156float ssao_adaptive_target = 0.5;157int ssao_blur_passes = 2;158float ssao_fadeout_from = 50.0;159float ssao_fadeout_to = 300.0;160161RS::EnvironmentSSILQuality ssil_quality = RS::ENV_SSIL_QUALITY_MEDIUM;162bool ssil_half_size = false;163float ssil_adaptive_target = 0.5;164int ssil_blur_passes = 4;165float ssil_fadeout_from = 50.0;166float ssil_fadeout_to = 300.0;167168RS::EnvironmentSSRRoughnessQuality ssr_roughness_quality = RS::ENV_SSR_ROUGHNESS_QUALITY_LOW;169170RS::SubSurfaceScatteringQuality sss_quality = RS::SUB_SURFACE_SCATTERING_QUALITY_MEDIUM;171float sss_scale = 0.05;172float sss_depth_scale = 0.01;173174/* SS Downsampler */175176struct SSEffectsDownsamplePushConstant {177float pixel_size[2];178float z_far;179float z_near;180uint32_t orthogonal;181float radius_sq;182uint32_t pad[2];183};184185enum SSEffectsMode {186SS_EFFECTS_DOWNSAMPLE,187SS_EFFECTS_DOWNSAMPLE_HALF_RES,188SS_EFFECTS_DOWNSAMPLE_MIPMAP,189SS_EFFECTS_DOWNSAMPLE_MIPMAP_HALF_RES,190SS_EFFECTS_DOWNSAMPLE_HALF,191SS_EFFECTS_DOWNSAMPLE_HALF_RES_HALF,192SS_EFFECTS_DOWNSAMPLE_FULL_MIPS,193SS_EFFECTS_MAX194};195196struct SSEffectsGatherConstants {197float rotation_matrices[80]; //5 vec4s * 4198};199200struct SSEffectsShader {201SSEffectsDownsamplePushConstant downsample_push_constant;202SsEffectsDownsampleShaderRD downsample_shader;203RID downsample_shader_version;204bool used_half_size_last_frame = false;205bool used_mips_last_frame = false;206bool used_full_mips_last_frame = false;207208RID gather_constants_buffer;209210RID mirror_sampler;211212RID pipelines[SS_EFFECTS_MAX];213} ss_effects;214215/* SSIL */216217enum SSILMode {218SSIL_GATHER,219SSIL_GATHER_BASE,220SSIL_GATHER_ADAPTIVE,221SSIL_GENERATE_IMPORTANCE_MAP,222SSIL_PROCESS_IMPORTANCE_MAPA,223SSIL_PROCESS_IMPORTANCE_MAPB,224SSIL_BLUR_PASS,225SSIL_BLUR_PASS_SMART,226SSIL_BLUR_PASS_WIDE,227SSIL_INTERLEAVE,228SSIL_INTERLEAVE_SMART,229SSIL_INTERLEAVE_HALF,230SSIL_MAX231};232233struct SSILGatherPushConstant {234int32_t screen_size[2];235int pass;236int quality;237238float half_screen_pixel_size[2];239float half_screen_pixel_size_x025[2];240241float NDC_to_view_mul[2];242float NDC_to_view_add[2];243244float pad2[2];245float z_near;246float z_far;247248float radius;249float intensity;250int size_multiplier;251int pad;252253float fade_out_mul;254float fade_out_add;255float normal_rejection_amount;256float inv_radius_near_limit;257258uint32_t is_orthogonal;259float neg_inv_radius;260float load_counter_avg_div;261float adaptive_sample_limit;262263int32_t pass_coord_offset[2];264float pass_uv_offset[2];265};266267struct SSILImportanceMapPushConstant {268float half_screen_pixel_size[2];269float intensity;270float pad;271};272273struct SSILBlurPushConstant {274float edge_sharpness;275float pad;276float half_screen_pixel_size[2];277};278279struct SSILInterleavePushConstant {280float inv_sharpness;281uint32_t size_modifier;282float pixel_size[2];283};284285struct SSILProjectionUniforms {286float inv_last_frame_projection_matrix[16];287};288289struct SSIL {290SSILGatherPushConstant gather_push_constant;291SsilShaderRD gather_shader;292RID gather_shader_version;293RID projection_uniform_buffer;294295SSILImportanceMapPushConstant importance_map_push_constant;296SsilImportanceMapShaderRD importance_map_shader;297RID importance_map_shader_version;298RID importance_map_load_counter;299RID counter_uniform_set;300301SSILBlurPushConstant blur_push_constant;302SsilBlurShaderRD blur_shader;303RID blur_shader_version;304305SSILInterleavePushConstant interleave_push_constant;306SsilInterleaveShaderRD interleave_shader;307RID interleave_shader_version;308309RID pipelines[SSIL_MAX];310} ssil;311312void 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);313314/* SSAO */315316enum SSAOMode {317SSAO_GATHER,318SSAO_GATHER_BASE,319SSAO_GATHER_ADAPTIVE,320SSAO_GENERATE_IMPORTANCE_MAP,321SSAO_PROCESS_IMPORTANCE_MAPA,322SSAO_PROCESS_IMPORTANCE_MAPB,323SSAO_BLUR_PASS,324SSAO_BLUR_PASS_SMART,325SSAO_BLUR_PASS_WIDE,326SSAO_INTERLEAVE,327SSAO_INTERLEAVE_SMART,328SSAO_INTERLEAVE_HALF,329SSAO_MAX330};331332struct SSAOGatherPushConstant {333int32_t screen_size[2];334int pass;335int quality;336337float half_screen_pixel_size[2];338int size_multiplier;339float detail_intensity;340341float NDC_to_view_mul[2];342float NDC_to_view_add[2];343344float pad[2];345float half_screen_pixel_size_x025[2];346347float radius;348float intensity;349float shadow_power;350float shadow_clamp;351352float fade_out_mul;353float fade_out_add;354float horizon_angle_threshold;355float inv_radius_near_limit;356357uint32_t is_orthogonal;358float neg_inv_radius;359float load_counter_avg_div;360float adaptive_sample_limit;361362int32_t pass_coord_offset[2];363float pass_uv_offset[2];364};365366struct SSAOImportanceMapPushConstant {367float half_screen_pixel_size[2];368float intensity;369float power;370};371372struct SSAOBlurPushConstant {373float edge_sharpness;374float pad;375float half_screen_pixel_size[2];376};377378struct SSAOInterleavePushConstant {379float inv_sharpness;380uint32_t size_modifier;381float pixel_size[2];382};383384struct SSAO {385SSAOGatherPushConstant gather_push_constant;386SsaoShaderRD gather_shader;387RID gather_shader_version;388389SSAOImportanceMapPushConstant importance_map_push_constant;390SsaoImportanceMapShaderRD importance_map_shader;391RID importance_map_shader_version;392RID importance_map_load_counter;393RID counter_uniform_set;394395SSAOBlurPushConstant blur_push_constant;396SsaoBlurShaderRD blur_shader;397RID blur_shader_version;398399SSAOInterleavePushConstant interleave_push_constant;400SsaoInterleaveShaderRD interleave_shader;401RID interleave_shader_version;402403RID pipelines[SSAO_MAX];404} ssao;405406void 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);407408/* Screen Space Reflection */409410enum SSRShaderSpecializations {411SSR_MULTIVIEW = 1 << 0,412SSR_VARIATIONS = 2,413};414415struct ScreenSpaceReflectionSceneData {416float projection[2][16];417float inv_projection[2][16];418float eye_offset[2][4];419};420421// SSR Scale422423struct ScreenSpaceReflectionScalePushConstant {424int32_t screen_size[2];425float camera_z_near;426float camera_z_far;427428uint32_t orthogonal;429uint32_t filter;430uint32_t view_index;431uint32_t pad1;432};433434struct ScreenSpaceReflectionScale {435ScreenSpaceReflectionScaleShaderRD shader;436RID shader_version;437RID pipelines[SSR_VARIATIONS];438} ssr_scale;439440// SSR main441442enum ScreenSpaceReflectionMode {443SCREEN_SPACE_REFLECTION_NORMAL,444SCREEN_SPACE_REFLECTION_ROUGH,445SCREEN_SPACE_REFLECTION_MAX,446};447448struct ScreenSpaceReflectionPushConstant {449float proj_info[4]; // 16 - 16450451int32_t screen_size[2]; // 8 - 24452float camera_z_near; // 4 - 28453float camera_z_far; // 4 - 32454455int32_t num_steps; // 4 - 36456float depth_tolerance; // 4 - 40457float distance_fade; // 4 - 44458float curve_fade_in; // 4 - 48459460uint32_t orthogonal; // 4 - 52461float filter_mipmap_levels; // 4 - 56462uint32_t use_half_res; // 4 - 60463uint32_t view_index; // 4 - 64464465// float projection[16]; // this is in our ScreenSpaceReflectionSceneData now466};467468struct ScreenSpaceReflection {469ScreenSpaceReflectionShaderRD shader;470RID shader_version;471RID pipelines[SSR_VARIATIONS][SCREEN_SPACE_REFLECTION_MAX];472473RID ubo;474} ssr;475476// SSR Filter477478struct ScreenSpaceReflectionFilterPushConstant {479float proj_info[4]; // 16 - 16480481uint32_t orthogonal; // 4 - 20482float edge_tolerance; // 4 - 24483int32_t increment; // 4 - 28484uint32_t view_index; // 4 - 32485486int32_t screen_size[2]; // 8 - 40487uint32_t vertical; // 4 - 44488uint32_t steps; // 4 - 48489};490491enum SSRReflectionMode {492SCREEN_SPACE_REFLECTION_FILTER_HORIZONTAL,493SCREEN_SPACE_REFLECTION_FILTER_VERTICAL,494SCREEN_SPACE_REFLECTION_FILTER_MAX,495};496497struct ScreenSpaceReflectionFilter {498ScreenSpaceReflectionFilterShaderRD shader;499RID shader_version;500RID pipelines[SSR_VARIATIONS][SCREEN_SPACE_REFLECTION_FILTER_MAX];501} ssr_filter;502503/* Subsurface scattering */504505struct SubSurfaceScatteringPushConstant {506int32_t screen_size[2];507float camera_z_far;508float camera_z_near;509510uint32_t vertical;511uint32_t orthogonal;512float unit_size;513float scale;514515float depth_scale;516uint32_t pad[3];517};518519struct SubSurfaceScattering {520SubSurfaceScatteringPushConstant push_constant;521SubsurfaceScatteringShaderRD shader;522RID shader_version;523RID pipelines[3]; //3 quality levels524} sss;525};526527} // namespace RendererRD528529530