summaryrefslogtreecommitdiff
path: root/servers/rendering/renderer_rd/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'servers/rendering/renderer_rd/shaders')
-rw-r--r--servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl102
-rw-r--r--servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl12
-rw-r--r--servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl26
-rw-r--r--servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl29
-rw-r--r--servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl8
-rw-r--r--servers/rendering/renderer_rd/shaders/taa_resolve.glsl393
6 files changed, 509 insertions, 61 deletions
diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl
index 4f49c186a6..9f27cea843 100644
--- a/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl
+++ b/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl
@@ -83,6 +83,11 @@ layout(location = 5) out vec3 tangent_interp;
layout(location = 6) out vec3 binormal_interp;
#endif
+#ifdef MOTION_VECTORS
+layout(location = 7) out vec4 screen_position;
+layout(location = 8) out vec4 prev_screen_position;
+#endif
+
#ifdef MATERIAL_UNIFORMS_USED
layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
@@ -93,11 +98,11 @@ layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms
#ifdef MODE_DUAL_PARABOLOID
-layout(location = 8) out float dp_clip;
+layout(location = 9) out float dp_clip;
#endif
-layout(location = 9) out flat uint instance_index_interp;
+layout(location = 10) out flat uint instance_index_interp;
#ifdef USE_MULTIVIEW
#ifdef has_VK_KHR_multiview
@@ -115,23 +120,12 @@ invariant gl_Position;
#GLOBALS
-void main() {
+void vertex_shader(in uint instance_index, in bool is_multimesh, in SceneData scene_data, in mat4 model_matrix, out vec4 screen_pos) {
vec4 instance_custom = vec4(0.0);
#if defined(COLOR_USED)
color_interp = color_attrib;
#endif
- uint instance_index = draw_call.instance_index;
-
- bool is_multimesh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH);
- if (!is_multimesh) {
- instance_index += gl_InstanceIndex;
- }
-
- instance_index_interp = instance_index;
-
- mat4 model_matrix = instances.data[instance_index].transform;
-
mat3 model_normal_matrix;
if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
model_normal_matrix = transpose(inverse(mat3(model_matrix)));
@@ -321,6 +315,11 @@ void main() {
#endif
vertex_interp = vertex;
+
+#ifdef MOTION_VECTORS
+ screen_pos = projection_matrix * vec4(vertex_interp, 1.0);
+#endif
+
#ifdef NORMAL_USED
normal_interp = normal;
#endif
@@ -375,6 +374,27 @@ void main() {
#endif
}
+void main() {
+ uint instance_index = draw_call.instance_index;
+
+ bool is_multimesh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH);
+ if (!is_multimesh) {
+ instance_index += gl_InstanceIndex;
+ }
+
+ instance_index_interp = instance_index;
+
+ SceneData scene_data = scene_data_block.data;
+ mat4 model_matrix = instances.data[instance_index].transform;
+#if defined(MOTION_VECTORS)
+ vertex_shader(instance_index, is_multimesh, scene_data_block.prev_data, instances.data[instance_index].prev_transform, prev_screen_position);
+ vertex_shader(instance_index, is_multimesh, scene_data, model_matrix, screen_position);
+#else
+ vec4 screen_position;
+ vertex_shader(instance_index, is_multimesh, scene_data, model_matrix, screen_position);
+#endif
+}
+
#[fragment]
#version 450
@@ -431,13 +451,18 @@ layout(location = 5) in vec3 tangent_interp;
layout(location = 6) in vec3 binormal_interp;
#endif
+#ifdef MOTION_VECTORS
+layout(location = 7) in vec4 screen_position;
+layout(location = 8) in vec4 prev_screen_position;
+#endif
+
#ifdef MODE_DUAL_PARABOLOID
-layout(location = 8) in float dp_clip;
+layout(location = 9) in float dp_clip;
#endif
-layout(location = 9) in flat uint instance_index_interp;
+layout(location = 10) in flat uint instance_index_interp;
#ifdef USE_MULTIVIEW
#ifdef has_VK_KHR_multiview
@@ -510,6 +535,10 @@ layout(location = 0) out vec4 frag_color;
#endif // RENDER DEPTH
+#ifdef MOTION_VECTORS
+layout(location = 2) out vec2 motion_vector;
+#endif
+
#include "scene_forward_aa_inc.glsl"
#if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
@@ -528,24 +557,24 @@ layout(location = 0) out vec4 frag_color;
#ifndef MODE_RENDER_DEPTH
vec4 volumetric_fog_process(vec2 screen_uv, float z) {
- vec3 fog_pos = vec3(screen_uv, z * scene_data.volumetric_fog_inv_length);
+ vec3 fog_pos = vec3(screen_uv, z * scene_data_block.data.volumetric_fog_inv_length);
if (fog_pos.z < 0.0) {
return vec4(0.0);
} else if (fog_pos.z < 1.0) {
- fog_pos.z = pow(fog_pos.z, scene_data.volumetric_fog_detail_spread);
+ fog_pos.z = pow(fog_pos.z, scene_data_block.data.volumetric_fog_detail_spread);
}
return texture(sampler3D(volumetric_fog_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), fog_pos);
}
vec4 fog_process(vec3 vertex) {
- vec3 fog_color = scene_data.fog_light_color;
+ vec3 fog_color = scene_data_block.data.fog_light_color;
- if (scene_data.fog_aerial_perspective > 0.0) {
+ if (scene_data_block.data.fog_aerial_perspective > 0.0) {
vec3 sky_fog_color = vec3(0.0);
- vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
+ vec3 cube_view = scene_data_block.data.radiance_inverse_xform * vertex;
// mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
- float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near));
+ float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data_block.data.z_near) / (scene_data_block.data.z_far - scene_data_block.data.z_near));
#ifdef USE_RADIANCE_CUBEMAP_ARRAY
float lod, blend;
blend = modf(mip_level * MAX_ROUGHNESS_LOD, lod);
@@ -554,29 +583,29 @@ vec4 fog_process(vec3 vertex) {
#else
sky_fog_color = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_view, mip_level * MAX_ROUGHNESS_LOD).rgb;
#endif //USE_RADIANCE_CUBEMAP_ARRAY
- fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
+ fog_color = mix(fog_color, sky_fog_color, scene_data_block.data.fog_aerial_perspective);
}
- if (scene_data.fog_sun_scatter > 0.001) {
+ if (scene_data_block.data.fog_sun_scatter > 0.001) {
vec4 sun_scatter = vec4(0.0);
float sun_total = 0.0;
vec3 view = normalize(vertex);
- for (uint i = 0; i < scene_data.directional_light_count; i++) {
+ for (uint i = 0; i < scene_data_block.data.directional_light_count; i++) {
vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
- fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
+ fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
}
}
- float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
+ float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data_block.data.fog_density));
- if (abs(scene_data.fog_height_density) >= 0.0001) {
- float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y;
+ if (abs(scene_data_block.data.fog_height_density) >= 0.0001) {
+ float y = (scene_data_block.data.inv_view_matrix * vec4(vertex, 1.0)).y;
- float y_dist = y - scene_data.fog_height;
+ float y_dist = y - scene_data_block.data.fog_height;
- float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
+ float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data_block.data.fog_height_density));
fog_amount = max(vfog_amount, fog_amount);
}
@@ -608,6 +637,8 @@ void main() {
discard;
#endif
+ SceneData scene_data = scene_data_block.data;
+ SceneData prev_scene_data = scene_data_block.prev_data;
uint instance_index = instance_index_interp;
//lay out everything, whatever is unused is optimized away anyway
@@ -2015,4 +2046,13 @@ void main() {
#endif //MODE_SEPARATE_SPECULAR
#endif //MODE_RENDER_DEPTH
+#ifdef MOTION_VECTORS
+ vec2 position_clip = (screen_position.xy / screen_position.w) - scene_data.taa_jitter;
+ vec2 prev_position_clip = (prev_screen_position.xy / prev_screen_position.w) - prev_scene_data.taa_jitter;
+
+ vec2 position_uv = position_clip * vec2(0.5, 0.5);
+ vec2 prev_position_uv = prev_position_clip * vec2(0.5, 0.5);
+
+ motion_vector = position_uv - prev_position_uv;
+#endif
}
diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl
index f2672f10e7..b700e21543 100644
--- a/servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl
+++ b/servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl
@@ -171,7 +171,7 @@ sdfgi;
/* Set 1: Render Pass (changes per render pass) */
-layout(set = 1, binding = 0, std140) uniform SceneData {
+struct SceneData {
mat4 projection_matrix;
mat4 inv_projection_matrix;
mat4 inv_view_matrix;
@@ -249,11 +249,19 @@ layout(set = 1, binding = 0, std140) uniform SceneData {
float reflection_multiplier; // one normally, zero when rendering reflections
bool pancake_shadows;
+ vec2 taa_jitter;
+ uvec2 pad;
+};
+
+layout(set = 1, binding = 0, std140) uniform SceneDataBlock {
+ SceneData data;
+ SceneData prev_data;
}
-scene_data;
+scene_data_block;
struct InstanceData {
mat4 transform;
+ mat4 prev_transform;
uint flags;
uint instance_uniforms_ofs; //base offset in global buffer for instance variables
uint gi_offset; //GI information when using lightmapping (VCT or lightmap index)
diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl
index bd1c2b5758..5a308bbd02 100644
--- a/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl
+++ b/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl
@@ -262,7 +262,7 @@ float sample_directional_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, ve
float avg = 0.0;
for (uint i = 0; i < sc_directional_soft_shadow_samples; i++) {
- avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data.directional_soft_shadow_kernel[i].xy), depth, 1.0));
+ avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data_block.data.directional_soft_shadow_kernel[i].xy), depth, 1.0));
}
return avg * (1.0 / float(sc_directional_soft_shadow_samples));
@@ -288,7 +288,7 @@ float sample_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec3 coord) {
float avg = 0.0;
for (uint i = 0; i < sc_soft_shadow_samples; i++) {
- avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data.soft_shadow_kernel[i].xy), depth, 1.0));
+ avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data_block.data.soft_shadow_kernel[i].xy), depth, 1.0));
}
return avg * (1.0 / float(sc_soft_shadow_samples));
@@ -311,10 +311,10 @@ float sample_omni_pcf_shadow(texture2D shadow, float blur_scale, vec2 coord, vec
}
float avg = 0.0;
- vec2 offset_scale = blur_scale * 2.0 * scene_data.shadow_atlas_pixel_size / uv_rect.zw;
+ vec2 offset_scale = blur_scale * 2.0 * scene_data_block.data.shadow_atlas_pixel_size / uv_rect.zw;
for (uint i = 0; i < sc_soft_shadow_samples; i++) {
- vec2 offset = offset_scale * (disk_rotation * scene_data.soft_shadow_kernel[i].xy);
+ vec2 offset = offset_scale * (disk_rotation * scene_data_block.data.soft_shadow_kernel[i].xy);
vec2 sample_coord = coord + offset;
float sample_coord_length_sqaured = dot(sample_coord, sample_coord);
@@ -351,7 +351,7 @@ float sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex
}
for (uint i = 0; i < sc_directional_penumbra_shadow_samples; i++) {
- vec2 suv = pssm_coord.xy + (disk_rotation * scene_data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
+ vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
float d = textureLod(sampler2D(shadow, material_samplers[SAMPLER_LINEAR_CLAMP]), suv, 0.0).r;
if (d < pssm_coord.z) {
blocker_average += d;
@@ -367,7 +367,7 @@ float sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex
float s = 0.0;
for (uint i = 0; i < sc_directional_penumbra_shadow_samples; i++) {
- vec2 suv = pssm_coord.xy + (disk_rotation * scene_data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
+ vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
s += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(suv, pssm_coord.z, 1.0));
}
@@ -394,7 +394,7 @@ float light_process_omni_shadow(uint idx, vec3 vertex, vec3 normal) {
#ifndef SHADOWS_DISABLED
if (omni_lights.data[idx].shadow_enabled) {
// there is a shadowmap
- vec2 texel_size = scene_data.shadow_atlas_pixel_size;
+ vec2 texel_size = scene_data_block.data.shadow_atlas_pixel_size;
vec4 base_uv_rect = omni_lights.data[idx].atlas_rect;
base_uv_rect.xy += texel_size;
base_uv_rect.zw -= texel_size * 2.0;
@@ -438,7 +438,7 @@ float light_process_omni_shadow(uint idx, vec3 vertex, vec3 normal) {
bitangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
- vec2 disk = disk_rotation * scene_data.penumbra_shadow_kernel[i].xy;
+ vec2 disk = disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy;
vec3 pos = local_vert + tangent * disk.x + bitangent * disk.y;
@@ -474,7 +474,7 @@ float light_process_omni_shadow(uint idx, vec3 vertex, vec3 normal) {
shadow = 0.0;
for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
- vec2 disk = disk_rotation * scene_data.penumbra_shadow_kernel[i].xy;
+ vec2 disk = disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy;
vec3 pos = local_vert + tangent * disk.x + bitangent * disk.y;
pos = normalize(pos);
@@ -579,7 +579,7 @@ void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 v
splane.xy = splane.xy * 0.5 + 0.5;
splane.z = shadow_len * omni_lights.data[idx].inv_radius;
splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
- // splane.xy = clamp(splane.xy,clamp_rect.xy + scene_data.shadow_atlas_pixel_size,clamp_rect.xy + clamp_rect.zw - scene_data.shadow_atlas_pixel_size );
+ // splane.xy = clamp(splane.xy,clamp_rect.xy + scene_data_block.data.shadow_atlas_pixel_size,clamp_rect.xy + clamp_rect.zw - scene_data_block.data.shadow_atlas_pixel_size );
splane.w = 1.0; //needed? i think it should be 1 already
float shadow_z = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), splane.xy, 0.0).r;
@@ -709,7 +709,7 @@ float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
float uv_size = spot_lights.data[idx].soft_shadow_size * z_norm * spot_lights.data[idx].soft_shadow_scale;
vec2 clamp_max = spot_lights.data[idx].atlas_rect.xy + spot_lights.data[idx].atlas_rect.zw;
for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
- vec2 suv = shadow_uv + (disk_rotation * scene_data.penumbra_shadow_kernel[i].xy) * uv_size;
+ vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
float d = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), suv, 0.0).r;
if (d < splane.z) {
@@ -726,7 +726,7 @@ float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
shadow = 0.0;
for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
- vec2 suv = shadow_uv + (disk_rotation * scene_data.penumbra_shadow_kernel[i].xy) * uv_size;
+ vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
shadow += textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(suv, splane.z, 1.0));
}
@@ -740,7 +740,7 @@ float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
} else {
//hard shadow
vec3 shadow_uv = vec3(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z);
- shadow = sample_pcf_shadow(shadow_atlas, spot_lights.data[idx].soft_shadow_scale * scene_data.shadow_atlas_pixel_size, shadow_uv);
+ shadow = sample_pcf_shadow(shadow_atlas, spot_lights.data[idx].soft_shadow_scale * scene_data_block.data.shadow_atlas_pixel_size, shadow_uv);
}
return shadow;
diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl
index fd0fefc5fd..e15ebbfc91 100644
--- a/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl
+++ b/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl
@@ -115,6 +115,8 @@ invariant gl_Position;
#GLOBALS
void main() {
+ SceneData scene_data = scene_data_block.data;
+
vec4 instance_custom = vec4(0.0);
#if defined(COLOR_USED)
color_interp = color_attrib;
@@ -527,13 +529,13 @@ layout(location = 0) out mediump vec4 frag_color;
*/
vec4 fog_process(vec3 vertex) {
- vec3 fog_color = scene_data.fog_light_color;
+ vec3 fog_color = scene_data_block.data.fog_light_color;
- if (scene_data.fog_aerial_perspective > 0.0) {
+ if (scene_data_block.data.fog_aerial_perspective > 0.0) {
vec3 sky_fog_color = vec3(0.0);
- vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
+ vec3 cube_view = scene_data_block.data.radiance_inverse_xform * vertex;
// mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
- float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near));
+ float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data_block.data.z_near) / (scene_data_block.data.z_far - scene_data_block.data.z_near));
#ifdef USE_RADIANCE_CUBEMAP_ARRAY
float lod, blend;
blend = modf(mip_level * MAX_ROUGHNESS_LOD, lod);
@@ -542,29 +544,29 @@ vec4 fog_process(vec3 vertex) {
#else
sky_fog_color = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_view, mip_level * MAX_ROUGHNESS_LOD).rgb;
#endif //USE_RADIANCE_CUBEMAP_ARRAY
- fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
+ fog_color = mix(fog_color, sky_fog_color, scene_data_block.data.fog_aerial_perspective);
}
- if (scene_data.fog_sun_scatter > 0.001) {
+ if (scene_data_block.data.fog_sun_scatter > 0.001) {
vec4 sun_scatter = vec4(0.0);
float sun_total = 0.0;
vec3 view = normalize(vertex);
- for (uint i = 0; i < scene_data.directional_light_count; i++) {
+ for (uint i = 0; i < scene_data_block.data.directional_light_count; i++) {
vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
- fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
+ fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
}
}
- float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
+ float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data_block.data.fog_density));
- if (abs(scene_data.fog_height_density) >= 0.0001) {
- float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y;
+ if (abs(scene_data_block.data.fog_height_density) >= 0.0001) {
+ float y = (scene_data_block.data.inv_view_matrix * vec4(vertex, 1.0)).y;
- float y_dist = y - scene_data.fog_height;
+ float y_dist = y - scene_data_block.data.fog_height;
- float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
+ float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data_block.data.fog_height_density));
fog_amount = max(vfog_amount, fog_amount);
}
@@ -580,6 +582,7 @@ void main() {
if (dp_clip > 0.0)
discard;
#endif
+ SceneData scene_data = scene_data_block.data;
//lay out everything, whatever is unused is optimized away anyway
vec3 vertex = vertex_interp;
diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl
index 91ef19ab67..dd14a15837 100644
--- a/servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl
+++ b/servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl
@@ -125,7 +125,7 @@ global_variables;
/* Set 1: Render Pass (changes per render pass) */
-layout(set = 1, binding = 0, std140) uniform SceneData {
+struct SceneData {
highp mat4 projection_matrix;
highp mat4 inv_projection_matrix;
highp mat4 inv_view_matrix;
@@ -189,8 +189,12 @@ layout(set = 1, binding = 0, std140) uniform SceneData {
uint pad1;
uint pad2;
uint pad3;
+};
+
+layout(set = 1, binding = 0, std140) uniform SceneDataBlock {
+ SceneData data;
}
-scene_data;
+scene_data_block;
#ifdef USE_RADIANCE_CUBEMAP_ARRAY
diff --git a/servers/rendering/renderer_rd/shaders/taa_resolve.glsl b/servers/rendering/renderer_rd/shaders/taa_resolve.glsl
new file mode 100644
index 0000000000..a1a77b95aa
--- /dev/null
+++ b/servers/rendering/renderer_rd/shaders/taa_resolve.glsl
@@ -0,0 +1,393 @@
+///////////////////////////////////////////////////////////////////////////////////
+// Copyright(c) 2016-2022 Panos Karabelas
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
+// copies of the Software, and to permit persons to whom the Software is furnished
+// to do so, subject to the following conditions :
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+///////////////////////////////////////////////////////////////////////////////////
+// File changes (yyyy-mm-dd)
+// 2022-05-06: Panos Karabelas: first commit
+// 2020-12-05: Joan Fons: convert to Vulkan and Godot
+///////////////////////////////////////////////////////////////////////////////////
+
+#[compute]
+
+#version 450
+
+#VERSION_DEFINES
+
+// Based on Spartan Engine's TAA implementation https://github.com/PanosK92/SpartanEngine/blob/master/Data/shaders/temporal_antialiasing.hlsl
+
+#define USE_SUBGROUPS
+
+#define GROUP_SIZE 8
+#define FLT_MIN 0.00000001
+#define FLT_MAX 32767.0
+#define RPC_9 0.11111111111
+#define RPC_16 0.0625
+
+#ifdef USE_SUBGROUPS
+layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE, local_size_z = 1) in;
+#endif
+
+layout(rgba16f, set = 0, binding = 0) uniform restrict readonly image2D color_buffer;
+layout(set = 0, binding = 1) uniform sampler2D depth_buffer;
+layout(rg16f, set = 0, binding = 2) uniform restrict readonly image2D velocity_buffer;
+layout(rg16f, set = 0, binding = 3) uniform restrict readonly image2D last_velocity_buffer;
+layout(set = 0, binding = 4) uniform sampler2D history_buffer;
+layout(rgba16f, set = 0, binding = 5) uniform restrict writeonly image2D output_buffer;
+
+layout(push_constant, std430) uniform Params {
+ vec2 resolution;
+ float disocclusion_threshold; // 0.1 / max(params.resolution.x, params.resolution.y
+ float disocclusion_scale;
+}
+params;
+
+const ivec2 kOffsets3x3[9] = {
+ ivec2(-1, -1),
+ ivec2(0, -1),
+ ivec2(1, -1),
+ ivec2(-1, 0),
+ ivec2(0, 0),
+ ivec2(1, 0),
+ ivec2(-1, 1),
+ ivec2(0, 1),
+ ivec2(1, 1),
+};
+
+/*------------------------------------------------------------------------------
+ THREAD GROUP SHARED MEMORY (LDS)
+------------------------------------------------------------------------------*/
+
+const int kBorderSize = 1;
+const int kGroupSize = GROUP_SIZE;
+const int kTileDimension = kGroupSize + kBorderSize * 2;
+const int kTileDimension2 = kTileDimension * kTileDimension;
+
+vec3 reinhard(vec3 hdr) {
+ return hdr / (hdr + 1.0);
+}
+vec3 reinhard_inverse(vec3 sdr) {
+ return sdr / (1.0 - sdr);
+}
+
+float get_depth(ivec2 thread_id) {
+ return texelFetch(depth_buffer, thread_id, 0).r;
+}
+
+#ifdef USE_SUBGROUPS
+shared vec3 tile_color[kTileDimension][kTileDimension];
+shared float tile_depth[kTileDimension][kTileDimension];
+
+vec3 load_color(uvec2 group_thread_id) {
+ group_thread_id += kBorderSize;
+ return tile_color[group_thread_id.x][group_thread_id.y];
+}
+
+void store_color(uvec2 group_thread_id, vec3 color) {
+ tile_color[group_thread_id.x][group_thread_id.y] = color;
+}
+
+float load_depth(uvec2 group_thread_id) {
+ group_thread_id += kBorderSize;
+ return tile_depth[group_thread_id.x][group_thread_id.y];
+}
+
+void store_depth(uvec2 group_thread_id, float depth) {
+ tile_depth[group_thread_id.x][group_thread_id.y] = depth;
+}
+
+void store_color_depth(uvec2 group_thread_id, ivec2 thread_id) {
+ // out of bounds clamp
+ thread_id = clamp(thread_id, ivec2(0, 0), ivec2(params.resolution) - ivec2(1, 1));
+
+ store_color(group_thread_id, imageLoad(color_buffer, thread_id).rgb);
+ store_depth(group_thread_id, get_depth(thread_id));
+}
+
+void populate_group_shared_memory(uvec2 group_id, uint group_index) {
+ // Populate group shared memory
+ ivec2 group_top_left = ivec2(group_id) * kGroupSize - kBorderSize;
+ if (group_index < (kTileDimension2 >> 2)) {
+ ivec2 group_thread_id_1 = ivec2(group_index % kTileDimension, group_index / kTileDimension);
+ ivec2 group_thread_id_2 = ivec2((group_index + (kTileDimension2 >> 2)) % kTileDimension, (group_index + (kTileDimension2 >> 2)) / kTileDimension);
+ ivec2 group_thread_id_3 = ivec2((group_index + (kTileDimension2 >> 1)) % kTileDimension, (group_index + (kTileDimension2 >> 1)) / kTileDimension);
+ ivec2 group_thread_id_4 = ivec2((group_index + kTileDimension2 * 3 / 4) % kTileDimension, (group_index + kTileDimension2 * 3 / 4) / kTileDimension);
+
+ store_color_depth(group_thread_id_1, group_top_left + group_thread_id_1);
+ store_color_depth(group_thread_id_2, group_top_left + group_thread_id_2);
+ store_color_depth(group_thread_id_3, group_top_left + group_thread_id_3);
+ store_color_depth(group_thread_id_4, group_top_left + group_thread_id_4);
+ }
+
+ // Wait for group threads to load store data.
+ groupMemoryBarrier();
+ barrier();
+}
+#else
+vec3 load_color(uvec2 screen_pos) {
+ return imageLoad(color_buffer, ivec2(screen_pos)).rgb;
+}
+
+float load_depth(uvec2 screen_pos) {
+ return get_depth(ivec2(screen_pos));
+}
+#endif
+
+/*------------------------------------------------------------------------------
+ VELOCITY
+------------------------------------------------------------------------------*/
+
+void depth_test_min(uvec2 pos, inout float min_depth, inout uvec2 min_pos) {
+ float depth = load_depth(pos);
+
+ if (depth < min_depth) {
+ min_depth = depth;
+ min_pos = pos;
+ }
+}
+
+// Returns velocity with closest depth (3x3 neighborhood)
+void get_closest_pixel_velocity_3x3(in uvec2 group_pos, uvec2 group_top_left, out vec2 velocity) {
+ float min_depth = 1.0;
+ uvec2 min_pos = group_pos;
+
+ depth_test_min(group_pos + kOffsets3x3[0], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[1], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[2], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[3], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[4], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[5], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[6], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[7], min_depth, min_pos);
+ depth_test_min(group_pos + kOffsets3x3[8], min_depth, min_pos);
+
+ // Velocity out
+ velocity = imageLoad(velocity_buffer, ivec2(group_top_left + min_pos)).xy;
+}
+
+/*------------------------------------------------------------------------------
+ HISTORY SAMPLING
+------------------------------------------------------------------------------*/
+
+vec3 sample_catmull_rom_9(sampler2D stex, vec2 uv, vec2 resolution) {
+ // Source: https://gist.github.com/TheRealMJP/c83b8c0f46b63f3a88a5986f4fa982b1
+ // License: https://gist.github.com/TheRealMJP/bc503b0b87b643d3505d41eab8b332ae
+
+ // We're going to sample a a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding
+ // down the sample location to get the exact center of our "starting" texel. The starting texel will be at
+ // location [1, 1] in the grid, where [0, 0] is the top left corner.
+ vec2 sample_pos = uv * resolution;
+ vec2 texPos1 = floor(sample_pos - 0.5f) + 0.5f;
+
+ // Compute the fractional offset from our starting texel to our original sample location, which we'll
+ // feed into the Catmull-Rom spline function to get our filter weights.
+ vec2 f = sample_pos - texPos1;
+
+ // Compute the Catmull-Rom weights using the fractional offset that we calculated earlier.
+ // These equations are pre-expanded based on our knowledge of where the texels will be located,
+ // which lets us avoid having to evaluate a piece-wise function.
+ vec2 w0 = f * (-0.5f + f * (1.0f - 0.5f * f));
+ vec2 w1 = 1.0f + f * f * (-2.5f + 1.5f * f);
+ vec2 w2 = f * (0.5f + f * (2.0f - 1.5f * f));
+ vec2 w3 = f * f * (-0.5f + 0.5f * f);
+
+ // Work out weighting factors and sampling offsets that will let us use bilinear filtering to
+ // simultaneously evaluate the middle 2 samples from the 4x4 grid.
+ vec2 w12 = w1 + w2;
+ vec2 offset12 = w2 / (w1 + w2);
+
+ // Compute the final UV coordinates we'll use for sampling the texture
+ vec2 texPos0 = texPos1 - 1.0f;
+ vec2 texPos3 = texPos1 + 2.0f;
+ vec2 texPos12 = texPos1 + offset12;
+
+ texPos0 /= resolution;
+ texPos3 /= resolution;
+ texPos12 /= resolution;
+
+ vec3 result = vec3(0.0f, 0.0f, 0.0f);
+
+ result += textureLod(stex, vec2(texPos0.x, texPos0.y), 0.0).xyz * w0.x * w0.y;
+ result += textureLod(stex, vec2(texPos12.x, texPos0.y), 0.0).xyz * w12.x * w0.y;
+ result += textureLod(stex, vec2(texPos3.x, texPos0.y), 0.0).xyz * w3.x * w0.y;
+
+ result += textureLod(stex, vec2(texPos0.x, texPos12.y), 0.0).xyz * w0.x * w12.y;
+ result += textureLod(stex, vec2(texPos12.x, texPos12.y), 0.0).xyz * w12.x * w12.y;
+ result += textureLod(stex, vec2(texPos3.x, texPos12.y), 0.0).xyz * w3.x * w12.y;
+
+ result += textureLod(stex, vec2(texPos0.x, texPos3.y), 0.0).xyz * w0.x * w3.y;
+ result += textureLod(stex, vec2(texPos12.x, texPos3.y), 0.0).xyz * w12.x * w3.y;
+ result += textureLod(stex, vec2(texPos3.x, texPos3.y), 0.0).xyz * w3.x * w3.y;
+
+ return max(result, 0.0f);
+}
+
+/*------------------------------------------------------------------------------
+ HISTORY CLIPPING
+------------------------------------------------------------------------------*/
+
+// Based on "Temporal Reprojection Anti-Aliasing" - https://github.com/playdeadgames/temporal
+vec3 clip_aabb(vec3 aabb_min, vec3 aabb_max, vec3 p, vec3 q) {
+ vec3 r = q - p;
+ vec3 rmax = (aabb_max - p.xyz);
+ vec3 rmin = (aabb_min - p.xyz);
+
+ if (r.x > rmax.x + FLT_MIN)
+ r *= (rmax.x / r.x);
+ if (r.y > rmax.y + FLT_MIN)
+ r *= (rmax.y / r.y);
+ if (r.z > rmax.z + FLT_MIN)
+ r *= (rmax.z / r.z);
+
+ if (r.x < rmin.x - FLT_MIN)
+ r *= (rmin.x / r.x);
+ if (r.y < rmin.y - FLT_MIN)
+ r *= (rmin.y / r.y);
+ if (r.z < rmin.z - FLT_MIN)
+ r *= (rmin.z / r.z);
+
+ return p + r;
+}
+
+// Clip history to the neighbourhood of the current sample
+vec3 clip_history_3x3(uvec2 group_pos, vec3 color_history, vec2 velocity_closest) {
+ // Sample a 3x3 neighbourhood
+ vec3 s1 = load_color(group_pos + kOffsets3x3[0]);
+ vec3 s2 = load_color(group_pos + kOffsets3x3[1]);
+ vec3 s3 = load_color(group_pos + kOffsets3x3[2]);
+ vec3 s4 = load_color(group_pos + kOffsets3x3[3]);
+ vec3 s5 = load_color(group_pos + kOffsets3x3[4]);
+ vec3 s6 = load_color(group_pos + kOffsets3x3[5]);
+ vec3 s7 = load_color(group_pos + kOffsets3x3[6]);
+ vec3 s8 = load_color(group_pos + kOffsets3x3[7]);
+ vec3 s9 = load_color(group_pos + kOffsets3x3[8]);
+
+ // Compute min and max (with an adaptive box size, which greatly reduces ghosting)
+ vec3 color_avg = (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9) * RPC_9;
+ vec3 color_avg2 = ((s1 * s1) + (s2 * s2) + (s3 * s3) + (s4 * s4) + (s5 * s5) + (s6 * s6) + (s7 * s7) + (s8 * s8) + (s9 * s9)) * RPC_9;
+ float box_size = mix(0.0f, 2.5f, smoothstep(0.02f, 0.0f, length(velocity_closest)));
+ vec3 dev = sqrt(abs(color_avg2 - (color_avg * color_avg))) * box_size;
+ vec3 color_min = color_avg - dev;
+ vec3 color_max = color_avg + dev;
+
+ // Variance clipping
+ vec3 color = clip_aabb(color_min, color_max, clamp(color_avg, color_min, color_max), color_history);
+
+ // Clamp to prevent NaNs
+ color = clamp(color, FLT_MIN, FLT_MAX);
+
+ return color;
+}
+
+/*------------------------------------------------------------------------------
+ TAA
+------------------------------------------------------------------------------*/
+
+const vec3 lumCoeff = vec3(0.299f, 0.587f, 0.114f);
+
+float luminance(vec3 color) {
+ return max(dot(color, lumCoeff), 0.0001f);
+}
+
+float get_factor_disocclusion(vec2 uv_reprojected, vec2 velocity) {
+ vec2 velocity_previous = imageLoad(last_velocity_buffer, ivec2(uv_reprojected * params.resolution)).xy;
+ vec2 velocity_texels = velocity * params.resolution;
+ vec2 prev_velocity_texels = velocity_previous * params.resolution;
+ float disocclusion = length(prev_velocity_texels - velocity_texels) - params.disocclusion_threshold;
+ return clamp(disocclusion * params.disocclusion_scale, 0.0, 1.0);
+}
+
+vec3 temporal_antialiasing(uvec2 pos_group_top_left, uvec2 pos_group, uvec2 pos_screen, vec2 uv, sampler2D tex_history) {
+ // Get the velocity of the current pixel
+ vec2 velocity = imageLoad(velocity_buffer, ivec2(pos_screen)).xy;
+
+ // Get reprojected uv
+ vec2 uv_reprojected = uv - velocity;
+
+ // Get input color
+ vec3 color_input = load_color(pos_group);
+
+ // Get history color (catmull-rom reduces a lot of the blurring that you get under motion)
+ vec3 color_history = sample_catmull_rom_9(tex_history, uv_reprojected, params.resolution).rgb;
+
+ // Clip history to the neighbourhood of the current sample (fixes a lot of the ghosting).
+ vec2 velocity_closest = vec2(0.0); // This is best done by using the velocity with the closest depth.
+ get_closest_pixel_velocity_3x3(pos_group, pos_group_top_left, velocity_closest);
+ color_history = clip_history_3x3(pos_group, color_history, velocity_closest);
+
+ // Compute blend factor
+ float blend_factor = RPC_16; // We want to be able to accumulate as many jitter samples as we generated, that is, 16.
+ {
+ // If re-projected UV is out of screen, converge to current color immediatel
+ float factor_screen = any(lessThan(uv_reprojected, vec2(0.0))) || any(greaterThan(uv_reprojected, vec2(1.0))) ? 1.0 : 0.0;
+
+ // Increase blend factor when there is disocclusion (fixes a lot of the remaining ghosting).
+ float factor_disocclusion = get_factor_disocclusion(uv_reprojected, velocity);
+
+ // Add to the blend factor
+ blend_factor = clamp(blend_factor + factor_screen + factor_disocclusion, 0.0, 1.0);
+ }
+
+ // Resolve
+ vec3 color_resolved = vec3(0.0);
+ {
+ // Tonemap
+ color_history = reinhard(color_history);
+ color_input = reinhard(color_input);
+
+ // Reduce flickering
+ float lum_color = luminance(color_input);
+ float lum_history = luminance(color_history);
+ float diff = abs(lum_color - lum_history) / max(lum_color, max(lum_history, 1.001));
+ diff = 1.0 - diff;
+ diff = diff * diff;
+ blend_factor = mix(0.0, blend_factor, diff);
+
+ // Lerp/blend
+ color_resolved = mix(color_history, color_input, blend_factor);
+
+ // Inverse tonemap
+ color_resolved = reinhard_inverse(color_resolved);
+ }
+
+ return color_resolved;
+}
+
+void main() {
+#ifdef USE_SUBGROUPS
+ populate_group_shared_memory(gl_WorkGroupID.xy, gl_LocalInvocationIndex);
+#endif
+
+ // Out of bounds check
+ if (any(greaterThanEqual(vec2(gl_GlobalInvocationID.xy), params.resolution))) {
+ return;
+ }
+
+#ifdef USE_SUBGROUPS
+ const uvec2 pos_group = gl_LocalInvocationID.xy;
+ const uvec2 pos_group_top_left = gl_WorkGroupID.xy * kGroupSize - kBorderSize;
+#else
+ const uvec2 pos_group = gl_GlobalInvocationID.xy;
+ const uvec2 pos_group_top_left = uvec2(0, 0);
+#endif
+ const uvec2 pos_screen = gl_GlobalInvocationID.xy;
+ const vec2 uv = (gl_GlobalInvocationID.xy + 0.5f) / params.resolution;
+
+ vec3 result = temporal_antialiasing(pos_group_top_left, pos_group, pos_screen, uv, history_buffer);
+ imageStore(output_buffer, ivec2(gl_GlobalInvocationID.xy), vec4(result, 1.0));
+}