diff options
Diffstat (limited to 'servers/rendering/renderer_rd/shaders')
7 files changed, 99 insertions, 58 deletions
diff --git a/servers/rendering/renderer_rd/shaders/canvas.glsl b/servers/rendering/renderer_rd/shaders/canvas.glsl index 65a621b203..fbc641ee9e 100644 --- a/servers/rendering/renderer_rd/shaders/canvas.glsl +++ b/servers/rendering/renderer_rd/shaders/canvas.glsl @@ -82,7 +82,7 @@ void main() { #endif - mat4 world_matrix = mat4(vec4(draw_data.world_x, 0.0, 0.0), vec4(draw_data.world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(draw_data.world_ofs, 0.0, 1.0)); + mat4 model_matrix = mat4(vec4(draw_data.world_x, 0.0, 0.0), vec4(draw_data.world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(draw_data.world_ofs, 0.0, 1.0)); #define FLAGS_INSTANCING_MASK 0x7F #define FLAGS_INSTANCING_HAS_COLORS (1 << 7) @@ -156,7 +156,7 @@ void main() { } matrix = transpose(matrix); - world_matrix = world_matrix * matrix; + model_matrix = model_matrix * matrix; } } @@ -179,7 +179,7 @@ void main() { #endif #if !defined(SKIP_TRANSFORM_USED) - vertex = (world_matrix * vec4(vertex, 0.0, 1.0)).xy; + vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy; #endif color_interp = color; diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl index a8648fc96a..33b0ee5fae 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_clustered.glsl @@ -99,6 +99,18 @@ layout(location = 8) out float dp_clip; layout(location = 9) out flat uint instance_index_interp; +#ifdef USE_MULTIVIEW +#ifdef has_VK_KHR_multiview +#define ViewIndex gl_ViewIndex +#else // has_VK_KHR_multiview +// !BAS! This needs to become an input once we implement our fallback! +#define ViewIndex 0 +#endif // has_VK_KHR_multiview +#else // USE_MULTIVIEW +// Set to zero, not supported in non stereo +#define ViewIndex 0 +#endif //USE_MULTIVIEW + invariant gl_Position; #GLOBALS @@ -118,13 +130,13 @@ void main() { instance_index_interp = instance_index; - mat4 world_matrix = instances.data[instance_index].transform; + mat4 model_matrix = instances.data[instance_index].transform; - mat3 world_normal_matrix; + mat3 model_normal_matrix; if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) { - world_normal_matrix = transpose(inverse(mat3(world_matrix))); + model_normal_matrix = transpose(inverse(mat3(model_matrix))); } else { - world_normal_matrix = mat3(world_matrix); + model_normal_matrix = mat3(model_matrix); } if (is_multimesh) { @@ -217,8 +229,8 @@ void main() { #endif //transpose matrix = transpose(matrix); - world_matrix = world_matrix * matrix; - world_normal_matrix = world_normal_matrix * mat3(matrix); + model_matrix = model_matrix * matrix; + model_normal_matrix = model_normal_matrix * mat3(matrix); } vec3 vertex = vertex_attrib; @@ -244,29 +256,35 @@ void main() { vec4 position; #endif +#ifdef USE_MULTIVIEW + mat4 projection_matrix = scene_data.projection_matrix_view[ViewIndex]; + mat4 inv_projection_matrix = scene_data.inv_projection_matrix_view[ViewIndex]; +#else mat4 projection_matrix = scene_data.projection_matrix; + mat4 inv_projection_matrix = scene_data.inv_projection_matrix; +#endif //USE_MULTIVIEW //using world coordinates #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - vertex = (world_matrix * vec4(vertex, 1.0)).xyz; + vertex = (model_matrix * vec4(vertex, 1.0)).xyz; #ifdef NORMAL_USED - normal = world_normal_matrix * normal; + normal = model_normal_matrix * normal; #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) - tangent = world_normal_matrix * tangent; - binormal = world_normal_matrix * binormal; + tangent = model_normal_matrix * tangent; + binormal = model_normal_matrix * binormal; #endif #endif float roughness = 1.0; - mat4 modelview = scene_data.inv_camera_matrix * world_matrix; - mat3 modelview_normal = mat3(scene_data.inv_camera_matrix) * world_normal_matrix; + mat4 modelview = scene_data.view_matrix * model_matrix; + mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix; { #CODE : VERTEX @@ -291,14 +309,14 @@ void main() { //using world coordinates #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - vertex = (scene_data.inv_camera_matrix * vec4(vertex, 1.0)).xyz; + vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz; #ifdef NORMAL_USED - normal = (scene_data.inv_camera_matrix * vec4(normal, 0.0)).xyz; + normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz; #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) - binormal = (scene_data.inv_camera_matrix * vec4(binormal, 0.0)).xyz; - tangent = (scene_data.inv_camera_matrix * vec4(tangent, 0.0)).xyz; + binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz; + tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz; #endif #endif @@ -421,10 +439,28 @@ layout(location = 8) in float dp_clip; layout(location = 9) in flat uint instance_index_interp; +#ifdef USE_MULTIVIEW +#ifdef has_VK_KHR_multiview +#define ViewIndex gl_ViewIndex +#else // has_VK_KHR_multiview +// !BAS! This needs to become an input once we implement our fallback! +#define ViewIndex 0 +#endif // has_VK_KHR_multiview +#else // USE_MULTIVIEW +// Set to zero, not supported in non stereo +#define ViewIndex 0 +#endif //USE_MULTIVIEW + //defines to keep compatibility with vertex -#define world_matrix instances.data[instance_index].transform +#define model_matrix instances.data[instance_index].transform +#ifdef USE_MULTIVIEW +#define projection_matrix scene_data.projection_matrix_view[ViewIndex] +#define inv_projection_matrix scene_data.inv_projection_matrix_view[ViewIndex] +#else #define projection_matrix scene_data.projection_matrix +#define inv_projection_matrix scene_data.inv_projection_matrix +#endif #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE) //both required for transmittance to be enabled @@ -536,7 +572,7 @@ vec4 fog_process(vec3 vertex) { float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { - float y = (scene_data.camera_matrix * vec4(vertex, 1.0)).y; + float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; @@ -1007,7 +1043,7 @@ void main() { if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture uint index = instances.data[instance_index].gi_offset; - vec3 wnormal = mat3(scene_data.camera_matrix) * normal; + vec3 wnormal = mat3(scene_data.inv_view_matrix) * normal; const float c1 = 0.429043; const float c2 = 0.511664; const float c3 = 0.743125; @@ -1061,9 +1097,9 @@ void main() { if (sc_use_forward_gi && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SDFGI)) { //has lightmap capture //make vertex orientation the world one, but still align to camera - vec3 cam_pos = mat3(scene_data.camera_matrix) * vertex; - vec3 cam_normal = mat3(scene_data.camera_matrix) * normal; - vec3 cam_reflection = mat3(scene_data.camera_matrix) * reflect(-view, normal); + vec3 cam_pos = mat3(scene_data.inv_view_matrix) * vertex; + vec3 cam_normal = mat3(scene_data.inv_view_matrix) * normal; + vec3 cam_reflection = mat3(scene_data.inv_view_matrix) * reflect(-view, normal); //apply y-mult cam_pos.y *= sdfgi.y_mult; @@ -1803,7 +1839,7 @@ void main() { vec3(0, -1, 0), vec3(0, 0, -1)); - vec3 cam_normal = mat3(scene_data.camera_matrix) * normalize(normal_interp); + vec3 cam_normal = mat3(scene_data.inv_view_matrix) * normalize(normal_interp); float closest_dist = -1e20; 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 3b110aded2..f2672f10e7 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_clustered_inc.glsl @@ -2,6 +2,7 @@ #define ROUGHNESS_MAX_LOD 5 #define MAX_VOXEL_GI_INSTANCES 8 +#define MAX_VIEWS 2 #if defined(has_GL_KHR_shader_subgroup_ballot) && defined(has_GL_KHR_shader_subgroup_arithmetic) @@ -12,6 +13,10 @@ #endif +#if defined(USE_MULTIVIEW) && defined(has_VK_KHR_multiview) +#extension GL_EXT_multiview : enable +#endif + #include "cluster_data_inc.glsl" #include "decal_data_inc.glsl" @@ -169,9 +174,12 @@ sdfgi; layout(set = 1, binding = 0, std140) uniform SceneData { mat4 projection_matrix; mat4 inv_projection_matrix; + mat4 inv_view_matrix; + mat4 view_matrix; - mat4 camera_matrix; - mat4 inv_camera_matrix; + // only used for multiview + mat4 projection_matrix_view[MAX_VIEWS]; + mat4 inv_projection_matrix_view[MAX_VIEWS]; vec2 viewport_size; vec2 screen_pixel_size; 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 1c9b08b6d3..bd1c2b5758 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl @@ -1,9 +1,9 @@ // Functions related to lighting float D_GGX(float cos_theta_m, float alpha) { - float alpha2 = alpha * alpha; - float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m; - return alpha2 / (M_PI * d * d); + float a = cos_theta_m * alpha; + float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); + return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl index 0fcf449659..6911cab27b 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_mobile.glsl @@ -122,13 +122,13 @@ void main() { bool is_multimesh = bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH); - mat4 world_matrix = draw_call.transform; + mat4 model_matrix = draw_call.transform; - mat3 world_normal_matrix; + mat3 model_normal_matrix; if (bool(draw_call.flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) { - world_normal_matrix = transpose(inverse(mat3(world_matrix))); + model_normal_matrix = transpose(inverse(mat3(model_matrix))); } else { - world_normal_matrix = mat3(world_matrix); + model_normal_matrix = mat3(model_matrix); } if (is_multimesh) { @@ -221,8 +221,8 @@ void main() { #endif //transpose matrix = transpose(matrix); - world_matrix = world_matrix * matrix; - world_normal_matrix = world_normal_matrix * mat3(matrix); + model_matrix = model_matrix * matrix; + model_normal_matrix = model_normal_matrix * mat3(matrix); } vec3 vertex = vertex_attrib; @@ -259,24 +259,24 @@ void main() { //using world coordinates #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - vertex = (world_matrix * vec4(vertex, 1.0)).xyz; + vertex = (model_matrix * vec4(vertex, 1.0)).xyz; #ifdef NORMAL_USED - normal = world_normal_matrix * normal; + normal = model_normal_matrix * normal; #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) - tangent = world_normal_matrix * tangent; - binormal = world_normal_matrix * binormal; + tangent = model_normal_matrix * tangent; + binormal = model_normal_matrix * binormal; #endif #endif float roughness = 1.0; - mat4 modelview = scene_data.inv_camera_matrix * world_matrix; - mat3 modelview_normal = mat3(scene_data.inv_camera_matrix) * world_normal_matrix; + mat4 modelview = scene_data.view_matrix * model_matrix; + mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix; { #CODE : VERTEX @@ -303,14 +303,14 @@ void main() { //using world coordinates #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - vertex = (scene_data.inv_camera_matrix * vec4(vertex, 1.0)).xyz; + vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz; #ifdef NORMAL_USED - normal = (scene_data.inv_camera_matrix * vec4(normal, 0.0)).xyz; + normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz; #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) - binormal = (scene_data.inv_camera_matrix * vec4(binormal, 0.0)).xyz; - tangent = (scene_data.inv_camera_matrix * vec4(tangent, 0.0)).xyz; + binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz; + tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz; #endif #endif @@ -458,7 +458,7 @@ layout(location = 8) highp in float dp_clip; //defines to keep compatibility with vertex -#define world_matrix draw_call.transform +#define model_matrix draw_call.transform #ifdef USE_MULTIVIEW #define projection_matrix scene_data.projection_matrix_view[ViewIndex] #else @@ -560,7 +560,7 @@ vec4 fog_process(vec3 vertex) { float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { - float y = (scene_data.camera_matrix * vec4(vertex, 1.0)).y; + float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; @@ -967,7 +967,7 @@ void main() { if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture uint index = draw_call.gi_offset; - vec3 wnormal = mat3(scene_data.camera_matrix) * normal; + vec3 wnormal = mat3(scene_data.inv_view_matrix) * normal; const float c1 = 0.429043; const float c2 = 0.511664; const float c3 = 0.743125; 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 7a624c3b95..91ef19ab67 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_mobile_inc.glsl @@ -128,8 +128,8 @@ global_variables; layout(set = 1, binding = 0, std140) uniform SceneData { highp mat4 projection_matrix; highp mat4 inv_projection_matrix; - highp mat4 camera_matrix; - highp mat4 inv_camera_matrix; + highp mat4 inv_view_matrix; + highp mat4 view_matrix; // only used for multiview highp mat4 projection_matrix_view[MAX_VIEWS]; diff --git a/servers/rendering/renderer_rd/shaders/sky.glsl b/servers/rendering/renderer_rd/shaders/sky.glsl index b258e89c66..5b4594da99 100644 --- a/servers/rendering/renderer_rd/shaders/sky.glsl +++ b/servers/rendering/renderer_rd/shaders/sky.glsl @@ -180,12 +180,11 @@ void main() { cube_normal.x = (cube_normal.z * (-uv_interp.x - params.projections[ViewIndex].x)) / params.projections[ViewIndex].y; cube_normal.y = -(cube_normal.z * (-uv_interp.y - params.projections[ViewIndex].z)) / params.projections[ViewIndex].w; cube_normal = mat3(params.orientation) * cube_normal; - cube_normal.z = -cube_normal.z; cube_normal = normalize(cube_normal); vec2 uv = uv_interp * 0.5 + 0.5; - vec2 panorama_coords = vec2(atan(cube_normal.x, cube_normal.z), acos(cube_normal.y)); + vec2 panorama_coords = vec2(atan(cube_normal.x, -cube_normal.z), acos(cube_normal.y)); if (panorama_coords.x < 0.0) { panorama_coords.x += M_PI * 2.0; @@ -200,13 +199,11 @@ void main() { vec4 custom_fog = vec4(0.0); #ifdef USE_CUBEMAP_PASS - vec3 inverted_cube_normal = cube_normal; - inverted_cube_normal.z *= -1.0; #ifdef USES_HALF_RES_COLOR - half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal) * params.luminance_multiplier; + half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_normal) * params.luminance_multiplier; #endif #ifdef USES_QUARTER_RES_COLOR - quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal) * params.luminance_multiplier; + quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_normal) * params.luminance_multiplier; #endif #else #ifdef USES_HALF_RES_COLOR |