Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FOG, RADIANCE, and IRRADIANCE shader overrides #41415

Merged
merged 1 commit into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,9 @@ RasterizerSceneHighEndRD::RasterizerSceneHighEndRD(RasterizerStorageRD *p_storag
actions.renames["NORMAL_ROUGHNESS_TEXTURE"] = "normal_roughness_buffer";
actions.renames["DEPTH"] = "gl_FragDepth";
actions.renames["OUTPUT_IS_SRGB"] = "true";
actions.renames["FOG"] = "custom_fog";
actions.renames["RADIANCE"] = "custom_radiance";
actions.renames["IRRADIANCE"] = "custom_irradiance";

//for light
actions.renames["VIEW"] = "view";
Expand Down Expand Up @@ -2726,6 +2729,10 @@ RasterizerSceneHighEndRD::RasterizerSceneHighEndRD(RasterizerStorageRD *p_storag
actions.usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions.usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";

actions.usage_defines["FOG"] = "#define CUSTOM_FOG_USED\n";
actions.usage_defines["RADIANCE"] = "#define CUSTOM_RADIANCE_USED\n";
actions.usage_defines["IRRADIANCE"] = "#define CUSTOM_IRRADIANCE_USED\n";

actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
actions.render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
actions.render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
Expand Down
1 change: 1 addition & 0 deletions servers/rendering/rasterizer_rd/rasterizer_scene_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7971,6 +7971,7 @@ RasterizerSceneRD::RasterizerSceneRD(RasterizerStorageRD *p_storage) {
actions.renames["HALF_RES_COLOR"] = "half_res_color";
actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
actions.renames["RADIANCE"] = "radiance";
actions.renames["FOG"] = "custom_fog";
actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
Expand Down
26 changes: 25 additions & 1 deletion servers/rendering/rasterizer_rd/shaders/scene_high_end.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,15 @@ void main() {
float clearcoat_gloss = 0.0;
float anisotropy = 0.0;
vec2 anisotropy_flow = vec2(1.0, 0.0);
#if defined(CUSTOM_FOG_USED)
vec4 custom_fog = vec4(0.0);
#endif
#if defined(CUSTOM_RADIANCE_USED)
vec4 custom_radiance = vec4(0.0);
#endif
#if defined(CUSTOM_IRRADIANCE_USED)
vec4 custom_irradiance = vec4(0.0);
#endif

#if defined(AO_USED)
float ao = 1.0;
Expand Down Expand Up @@ -1889,6 +1898,10 @@ FRAGMENT_SHADER_CODE
specular_light *= scene_data.ambient_light_color_energy.a;
}

#if defined(CUSTOM_RADIANCE_USED)
specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
#endif

#ifndef USE_LIGHTMAP
//lightmap overrides everything
if (scene_data.use_ambient_light) {
Expand All @@ -1906,7 +1919,9 @@ FRAGMENT_SHADER_CODE
}
}
#endif // USE_LIGHTMAP

#if defined(CUSTOM_IRRADIANCE_USED)
ambient_light = mix(specular_light, custom_irradiance.rgb, custom_irradiance.a);
#endif
#endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)

//radiance
Expand Down Expand Up @@ -2734,6 +2749,11 @@ FRAGMENT_SHADER_CODE
specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
}

#if defined(CUSTOM_FOG_USED)
diffuse_buffer.rgb = mix(diffuse_buffer.rgb, custom_fog.rgb, custom_fog.a);
specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), custom_fog.a);
#endif //CUSTOM_FOG_USED

#else //MODE_MULTIPLE_RENDER_TARGETS

#ifdef MODE_UNSHADED
Expand All @@ -2753,6 +2773,10 @@ FRAGMENT_SHADER_CODE
frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
}

#if defined(CUSTOM_FOG_USED)
frag_color.rgb = mix(frag_color.rgb, custom_fog.rgb, custom_fog.a);
#endif //CUSTOM_FOG_USED

#endif //MODE_MULTIPLE_RENDER_TARGETS

#endif //MODE_RENDER_DEPTH
Expand Down
5 changes: 5 additions & 0 deletions servers/rendering/rasterizer_rd/shaders/sky.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ void main() {
float alpha = 1.0; // Only available to subpasses
vec4 half_res_color = vec4(1.0);
vec4 quarter_res_color = vec4(1.0);
vec4 custom_fog = vec4(0.0);

#ifdef USE_CUBEMAP_PASS
vec3 inverted_cube_normal = cube_normal;
Expand Down Expand Up @@ -233,6 +234,10 @@ FRAGMENT_SHADER_CODE
frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
}

if (custom_fog.a > 0.0) {
frag_color.rgb = mix(frag_color.rgb, custom_fog.rgb, custom_fog.a);
}

#endif // DISABLE_FOG

// Blending is disabled for Sky, so alpha doesn't blend
Expand Down
4 changes: 4 additions & 0 deletions servers/rendering/shader_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ ShaderTypes::ShaderTypes() {
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["PROJECTION_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["INV_PROJECTION_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["VIEWPORT_SIZE"] = constt(ShaderLanguage::TYPE_VEC2);
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["FOG"] = ShaderLanguage::TYPE_VEC4; // TODO consider adding to light shader
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["RADIANCE"] = ShaderLanguage::TYPE_VEC4;
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].built_ins["IRRADIANCE"] = ShaderLanguage::TYPE_VEC4;
shader_modes[RS::SHADER_SPATIAL].functions["fragment"].can_discard = true;

shader_modes[RS::SHADER_SPATIAL].functions["light"].built_ins["WORLD_MATRIX"] = constt(ShaderLanguage::TYPE_MAT4);
Expand Down Expand Up @@ -325,6 +328,7 @@ ShaderTypes::ShaderTypes() {
shader_modes[RS::SHADER_SKY].functions["fragment"].built_ins["SKY_COORDS"] = constt(ShaderLanguage::TYPE_VEC2);
shader_modes[RS::SHADER_SKY].functions["fragment"].built_ins["HALF_RES_COLOR"] = constt(ShaderLanguage::TYPE_VEC4);
shader_modes[RS::SHADER_SKY].functions["fragment"].built_ins["QUARTER_RES_COLOR"] = constt(ShaderLanguage::TYPE_VEC4);
shader_modes[RS::SHADER_SKY].functions["fragment"].built_ins["FOG"] = ShaderLanguage::TYPE_VEC4;

shader_modes[RS::SHADER_SKY].modes.push_back("use_half_res_pass");
shader_modes[RS::SHADER_SKY].modes.push_back("use_quarter_res_pass");
Expand Down