From e2321c21dbf353b2ba1f22283054584109a6c1fa Mon Sep 17 00:00:00 2001 From: Manuel Dun Date: Thu, 11 May 2023 18:56:04 -0400 Subject: [PATCH] Enable the use of all builtins on the light shader When using the light process in spatial shader, the built-ins work as spected, now they work in forward+, mobile and compatibility renderer. --- drivers/gles3/shaders/scene.glsl | 4 ++++ .../scene_shader_forward_clustered.cpp | 4 ++-- .../scene_shader_forward_mobile.cpp | 4 ++-- .../scene_forward_clustered.glsl | 5 ++++- .../forward_mobile/scene_forward_mobile.glsl | 5 +++++ .../forward_mobile/scene_forward_mobile_inc.glsl | 1 + .../shaders/scene_forward_lights_inc.glsl | 15 +++++++++++++++ 7 files changed, 33 insertions(+), 5 deletions(-) diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 37976bb9a01..aa68febec8f 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -642,6 +642,10 @@ void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_di #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader + highp mat4 model_matrix = world_transform; + mat4 projection_matrix = scene_data.projection_matrix; + mat4 inv_projection_matrix = scene_data.inv_projection_matrix; + vec3 normal = N; vec3 light = L; vec3 view = V; diff --git a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp index 03f31f839e4..c07b11bd836 100644 --- a/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp +++ b/servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.cpp @@ -557,7 +557,7 @@ void SceneShaderForwardClustered::init(const String p_defines) { actions.renames["MODEL_MATRIX"] = "read_model_matrix"; actions.renames["MODEL_NORMAL_MATRIX"] = "model_normal_matrix"; - actions.renames["VIEW_MATRIX"] = "scene_data.view_matrix"; + actions.renames["VIEW_MATRIX"] = "read_view_matrix"; actions.renames["INV_VIEW_MATRIX"] = "inv_view_matrix"; actions.renames["PROJECTION_MATRIX"] = "projection_matrix"; actions.renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix"; @@ -588,7 +588,7 @@ void SceneShaderForwardClustered::init(const String p_defines) { actions.renames["PI"] = _MKSTR(Math_PI); actions.renames["TAU"] = _MKSTR(Math_TAU); actions.renames["E"] = _MKSTR(Math_E); - actions.renames["VIEWPORT_SIZE"] = "scene_data.viewport_size"; + actions.renames["VIEWPORT_SIZE"] = "read_viewport_size"; actions.renames["FRAGCOORD"] = "gl_FragCoord"; actions.renames["FRONT_FACING"] = "gl_FrontFacing"; diff --git a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp index f3bc8a6e55c..3552d47ceae 100644 --- a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp +++ b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp @@ -447,7 +447,7 @@ void SceneShaderForwardMobile::init(const String p_defines) { actions.renames["MODEL_MATRIX"] = "read_model_matrix"; actions.renames["MODEL_NORMAL_MATRIX"] = "model_normal_matrix"; - actions.renames["VIEW_MATRIX"] = "scene_data.view_matrix"; + actions.renames["VIEW_MATRIX"] = "read_view_matrix"; actions.renames["INV_VIEW_MATRIX"] = "inv_view_matrix"; actions.renames["PROJECTION_MATRIX"] = "projection_matrix"; actions.renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix"; @@ -478,7 +478,7 @@ void SceneShaderForwardMobile::init(const String p_defines) { actions.renames["PI"] = _MKSTR(Math_PI); actions.renames["TAU"] = _MKSTR(Math_TAU); actions.renames["E"] = _MKSTR(Math_E); - actions.renames["VIEWPORT_SIZE"] = "scene_data.viewport_size"; + actions.renames["VIEWPORT_SIZE"] = "read_viewport_size"; actions.renames["FRAGCOORD"] = "gl_FragCoord"; actions.renames["FRONT_FACING"] = "gl_FrontFacing"; diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl index 97501707240..cdeb882a625 100644 --- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl @@ -343,6 +343,8 @@ void vertex_shader(in uint instance_index, in bool is_multimesh, in uint multime mat4 modelview = scene_data.view_matrix * model_matrix; mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix; + mat4 read_view_matrix = scene_data.view_matrix; + vec2 read_viewport_size = scene_data.viewport_size; { #CODE : VERTEX @@ -823,7 +825,8 @@ void fragment_shader(in SceneData scene_data) { inv_view_matrix[1][3] = 0.0; inv_view_matrix[2][3] = 0.0; #endif - + mat4 read_view_matrix = scene_data.view_matrix; + vec2 read_viewport_size = scene_data.viewport_size; { #CODE : FRAGMENT } diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl index f87fa8d2ab5..e145fd8b521 100644 --- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl @@ -342,6 +342,8 @@ void main() { mat4 modelview = scene_data.view_matrix * model_matrix; mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix; + mat4 read_view_matrix = scene_data.view_matrix; + vec2 read_viewport_size = scene_data.viewport_size; { #CODE : VERTEX @@ -771,6 +773,9 @@ void main() { inv_view_matrix[2][3] = 0.0; #endif + mat4 read_view_matrix = scene_data.view_matrix; + vec2 read_viewport_size = scene_data.viewport_size; + { #CODE : FRAGMENT } diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl index 78b39a356d2..f16f80c6f65 100644 --- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl @@ -14,6 +14,7 @@ #endif #endif +#define USING_MOBILE_RENDERER /* don't exceed 128 bytes!! */ /* put instance data into our push content, not a array */ layout(push_constant, std430) uniform DrawCall { 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 41000f7b0e6..e159e5628ca 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl @@ -70,6 +70,21 @@ void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_di mat4 inv_view_matrix = scene_data_block.data.inv_view_matrix; +#ifdef USING_MOBILE_RENDERER + mat4 read_model_matrix = draw_call.transform; +#else + mat4 read_model_matrix = instances.data[instance_index_interp].transform; +#endif + + mat4 read_view_matrix = scene_data_block.data.view_matrix; + +#undef projection_matrix +#define projection_matrix scene_data_block.data.projection_matrix +#undef inv_projection_matrix +#define inv_projection_matrix scene_data_block.data.inv_projection_matrix + + vec2 read_viewport_size = scene_data_block.data.viewport_size; + vec3 normal = N; vec3 light = L; vec3 view = V;