Skip to content

Commit aa24698

Browse files
committed
Merge pull request godotengine#103934 from LiveTrower/dfg-lut
Forward+: Replace the current BRDF approximation with a DFG LUT and add multiscattering energy compensation
2 parents 0de813a + e3f79b0 commit aa24698

8 files changed

Lines changed: 250 additions & 24 deletions

File tree

doc/classes/BaseMaterial3D.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@
729729
</constant>
730730
<constant name="SPECULAR_SCHLICK_GGX" value="0" enum="SpecularMode">
731731
Default specular blob.
732+
[b]Note:[/b] Forward+ uses multiscattering for more accurate reflections, although the impact of multiscattering is more noticeable on rough metallic surfaces than on smooth, non-metallic surfaces.
733+
[b]Note:[/b] Mobile and Compatibility don't perform multiscattering for performance reasons. Instead, they perform single scattering, which means rough metallic surfaces may look slightly darker than intended.
732734
</constant>
733735
<constant name="SPECULAR_TOON" value="1" enum="SpecularMode">
734736
Toon blob which changes size based on roughness.

servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,14 @@ void RenderForwardClustered::_update_render_base_uniform_set() {
32053205
uniforms.push_back(u);
32063206
}
32073207

3208+
{
3209+
RD::Uniform u;
3210+
u.binding = 16;
3211+
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
3212+
u.append_id(dfg_lut.texture);
3213+
uniforms.push_back(u);
3214+
}
3215+
32083216
render_base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, scene_shader.default_shader_rd, SCENE_UNIFORM_SET);
32093217
}
32103218
}
@@ -4951,6 +4959,45 @@ RenderForwardClustered::RenderForwardClustered() {
49514959
best_fit_normal.shader.version_free(best_fit_normal.shader_version);
49524960
}
49534961

4962+
/* DFG LUT */
4963+
{
4964+
Vector<String> modes;
4965+
modes.push_back("\n");
4966+
dfg_lut.shader.initialize(modes);
4967+
dfg_lut.shader_version = dfg_lut.shader.version_create();
4968+
dfg_lut.pipeline = RD::get_singleton()->compute_pipeline_create(dfg_lut.shader.version_get_shader(dfg_lut.shader_version, 0));
4969+
4970+
RD::TextureFormat tformat;
4971+
tformat.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
4972+
tformat.width = 128;
4973+
tformat.height = 128;
4974+
tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
4975+
tformat.texture_type = RD::TEXTURE_TYPE_2D;
4976+
dfg_lut.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
4977+
4978+
RID shader = dfg_lut.shader.version_get_shader(dfg_lut.shader_version, 0);
4979+
ERR_FAIL_COND(shader.is_null());
4980+
4981+
Vector<RD::Uniform> uniforms;
4982+
4983+
{
4984+
RD::Uniform u;
4985+
u.binding = 0;
4986+
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
4987+
u.append_id(dfg_lut.texture);
4988+
uniforms.push_back(u);
4989+
}
4990+
RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader, 0);
4991+
4992+
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
4993+
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, dfg_lut.pipeline);
4994+
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
4995+
RD::get_singleton()->compute_list_dispatch_threads(compute_list, tformat.width, tformat.height, 1);
4996+
RD::get_singleton()->compute_list_end();
4997+
4998+
dfg_lut.shader.version_free(dfg_lut.shader_version);
4999+
}
5000+
49545001
_update_shader_quality_settings();
49555002
_update_global_pipeline_data_requirements_from_project();
49565003

@@ -5000,6 +5047,7 @@ RenderForwardClustered::~RenderForwardClustered() {
50005047
RD::get_singleton()->free(shadow_sampler);
50015048
RSG::light_storage->directional_shadow_atlas_set_size(0);
50025049
RD::get_singleton()->free(best_fit_normal.texture);
5050+
RD::get_singleton()->free(dfg_lut.texture);
50035051

50045052
{
50055053
for (const RID &rid : scene_state.uniform_buffers) {

servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.h"
4444
#include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
4545
#include "servers/rendering/renderer_rd/shaders/forward_clustered/best_fit_normal.glsl.gen.h"
46+
#include "servers/rendering/renderer_rd/shaders/forward_clustered/integrate_dfg.glsl.gen.h"
4647

4748
#define RB_SCOPE_FORWARD_CLUSTERED SNAME("forward_clustered")
4849

@@ -180,6 +181,13 @@ class RenderForwardClustered : public RendererSceneRenderRD {
180181
RID texture;
181182
} best_fit_normal;
182183

184+
struct IntegrateDFG {
185+
IntegrateDfgShaderRD shader;
186+
RID shader_version;
187+
RID pipeline;
188+
RID texture;
189+
} dfg_lut;
190+
183191
enum PassMode {
184192
PASS_MODE_COLOR,
185193
PASS_MODE_SHADOW,
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#[compute]
2+
#version 450
3+
4+
// References:
5+
// https://www.gamedevs.org/uploads/real-shading-in-unreal-engine-4.pdf
6+
// https://google.github.io/filament/Filament.html
7+
// https://learnopengl.com/PBR/IBL/Specular-IBL
8+
9+
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
10+
11+
layout(rgba16f, set = 0, binding = 0) uniform restrict writeonly image2D current_image;
12+
13+
#define M_PI 3.14159265359
14+
#define SAMPLE_COUNT 1024
15+
#define SIZE 128
16+
17+
#define saturate(x) clamp(x, 0, 1)
18+
19+
// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
20+
// efficient VanDerCorpus calculation
21+
float radical_inverse_vdc(uint bits) {
22+
bits = (bits << 16u) | (bits >> 16u);
23+
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
24+
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
25+
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
26+
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
27+
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
28+
}
29+
30+
vec2 hammersley(uint i, float n) {
31+
return vec2(float(i) / n, radical_inverse_vdc(i));
32+
}
33+
34+
vec3 importance_sample_ggx(vec2 Xi, vec3 N, float roughness) {
35+
float a = roughness * roughness;
36+
37+
float phi = 2.0 * M_PI * Xi.x;
38+
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a * a - 1.0) * Xi.y));
39+
float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
40+
41+
// from spherical coordinates to cartesian coordinates - halfway vector
42+
vec3 H;
43+
H.x = cos(phi) * sinTheta;
44+
H.y = sin(phi) * sinTheta;
45+
H.z = cosTheta;
46+
47+
// from tangent-space H vector to world-space sample vector
48+
vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
49+
vec3 tangent = normalize(cross(up, N));
50+
vec3 bitangent = cross(N, tangent);
51+
52+
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
53+
return normalize(sampleVec);
54+
}
55+
56+
float geometry_schlick_ggx(float NdotV, float roughness) {
57+
// note that we use a different k for IBL
58+
float a = roughness;
59+
float k = (a * a) / 2.0;
60+
61+
float nom = NdotV;
62+
float denom = NdotV * (1.0 - k) + k;
63+
64+
return nom / denom;
65+
}
66+
67+
float geometry_smith(vec3 N, vec3 V, vec3 L, float roughness) {
68+
float NdotV = saturate(dot(N, V));
69+
float NdotL = saturate(dot(N, L));
70+
float ggx2 = geometry_schlick_ggx(NdotV, roughness);
71+
float ggx1 = geometry_schlick_ggx(NdotL, roughness);
72+
73+
return ggx1 * ggx2;
74+
}
75+
76+
vec3 importance_uniform_sample(vec2 u) {
77+
float phi = 2.0f * M_PI * u.x;
78+
float cosTheta = 1 - u.y;
79+
float sinTheta = sqrt(1 - cosTheta * cosTheta);
80+
return vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
81+
}
82+
83+
float distribution_charlie(float NoH, float roughness) {
84+
// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF"
85+
float a = roughness * roughness;
86+
float invAlpha = 1 / a;
87+
float cos2h = NoH * NoH;
88+
float sin2h = 1 - cos2h;
89+
return (2.0f + invAlpha) * pow(sin2h, invAlpha * 0.5f) / (2.0f * M_PI);
90+
}
91+
92+
float visibility_ashikhmin(float NoV, float NoL) {
93+
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
94+
return 1 / (4 * (NoL + NoV - NoL * NoV));
95+
}
96+
97+
void integrate_brdfs(float n_dot_v, float roughness, out vec2 brdf, out float cloth_brdf) {
98+
vec3 v = vec3(sqrt(1.0 - n_dot_v * n_dot_v), 0, n_dot_v);
99+
vec3 n = vec3(0.0f, 0.0f, 1.0f);
100+
float A = 0.0f;
101+
float B = 0.0f;
102+
float C = 0.0f;
103+
104+
for (uint i = 0; i < SAMPLE_COUNT; ++i) {
105+
vec2 Xi = hammersley(i, SAMPLE_COUNT);
106+
vec3 h = importance_sample_ggx(Xi, n, roughness);
107+
vec3 l = normalize(2.0 * dot(v, h) * h - v);
108+
109+
float n_dot_l = saturate(l.z);
110+
float n_dot_h = saturate(h.z);
111+
float v_dot_h = saturate(dot(v, h));
112+
113+
if (n_dot_l > 0.0) {
114+
float G = geometry_smith(n, v, l, roughness);
115+
float G_Vis = (G * v_dot_h) / (n_dot_h * n_dot_v);
116+
float Fc = pow(1.0 - v_dot_h, 5.0);
117+
118+
// LDFG term for multiscattering
119+
// https://google.github.io/filament/Filament.html#toc5.3.4.7
120+
A += Fc * G_Vis;
121+
B += G_Vis;
122+
}
123+
124+
// Cloth BRDF calculations
125+
// https://github.com/google/filament/blob/main/libs/ibl/src/CubemapIBL.cpp#L856-L874
126+
vec3 h_cloth = importance_uniform_sample(Xi);
127+
vec3 l_cloth = normalize(2.0 * dot(v, h_cloth) * h_cloth - v);
128+
float n_dot_l_cloth = saturate(l_cloth.z);
129+
float n_dot_h_cloth = saturate(h_cloth.z);
130+
float v_dot_h_cloth = saturate(dot(v, h_cloth));
131+
132+
if (n_dot_l_cloth > 0.0) {
133+
float v_cloth = visibility_ashikhmin(n_dot_v, n_dot_l_cloth);
134+
float d_cloth = distribution_charlie(n_dot_h_cloth, roughness);
135+
C += v_cloth * d_cloth * n_dot_l_cloth * v_dot_h_cloth;
136+
}
137+
}
138+
139+
A /= float(SAMPLE_COUNT);
140+
B /= float(SAMPLE_COUNT);
141+
C *= (4.0 * 2.0 * M_PI / SAMPLE_COUNT);
142+
143+
brdf = vec2(A, B);
144+
cloth_brdf = C;
145+
}
146+
147+
void main() {
148+
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
149+
float roughness = float(pos.y + 0.5f) / SIZE;
150+
float NdotV = float(pos.x + 0.5f) / SIZE;
151+
vec2 brdf;
152+
float cloth_brdf;
153+
integrate_brdfs(NdotV, roughness, brdf, cloth_brdf);
154+
ivec2 out_pos = ivec2(pos.x, (SIZE - 1) - pos.y);
155+
imageStore(current_image, out_pos, vec4(brdf, cloth_brdf, 1.0));
156+
}

servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ void fragment_shader(in SceneData scene_data) {
11511151
float clearcoat_roughness = 0.0;
11521152
float anisotropy = 0.0;
11531153
vec2 anisotropy_flow = vec2(1.0, 0.0);
1154+
vec3 energy_compensation = vec3(1.0);
11541155
#ifndef FOG_DISABLED
11551156
vec4 fog = vec4(0.0);
11561157
#endif // !FOG_DISABLED
@@ -2004,19 +2005,15 @@ void fragment_shader(in SceneData scene_data) {
20042005
//simplify for toon, as
20052006
specular_light *= specular * metallic * albedo * 2.0;
20062007
#else
2008+
// Base Layer
2009+
float NdotV = clamp(dot(normal, view), 0.0001, 1.0);
2010+
vec2 envBRDF = prefiltered_dfg(roughness, NdotV).xy;
2011+
// Multiscattering
2012+
energy_compensation = get_energy_compensation(f0, envBRDF.y);
20072013

2008-
// scales the specular reflections, needs to be computed before lighting happens,
2009-
// but after environment, GI, and reflection probes are added
2010-
// Environment brdf approximation (Lazarov 2013)
2011-
// see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
2012-
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
2013-
const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
2014-
vec4 r = roughness * c0 + c1;
2015-
float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2016-
float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
2017-
vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
2018-
2019-
specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0);
2014+
// cheap luminance approximation
2015+
float f90 = clamp(50.0 * f0.g, metallic, 1.0);
2016+
specular_light *= energy_compensation * (f90 * envBRDF.x + f0 * envBRDF.y);
20202017
#endif
20212018
}
20222019

@@ -2415,7 +2412,7 @@ void fragment_shader(in SceneData scene_data) {
24152412
#else
24162413
directional_lights.data[i].color * directional_lights.data[i].energy * tint,
24172414
#endif
2418-
true, shadow, f0, orms, directional_lights.data[i].specular, albedo, alpha, screen_uv,
2415+
true, shadow, f0, orms, directional_lights.data[i].specular, albedo, alpha, screen_uv, energy_compensation,
24192416
#ifdef LIGHT_BACKLIGHT_USED
24202417
backlight,
24212418
#endif
@@ -2485,7 +2482,7 @@ void fragment_shader(in SceneData scene_data) {
24852482
continue; // Statically baked light and object uses lightmap, skip
24862483
}
24872484

2488-
light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv,
2485+
light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv, energy_compensation,
24892486
#ifdef LIGHT_BACKLIGHT_USED
24902487
backlight,
24912488
#endif
@@ -2553,7 +2550,7 @@ void fragment_shader(in SceneData scene_data) {
25532550
continue; // Statically baked light and object uses lightmap, skip
25542551
}
25552552

2556-
light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv,
2553+
light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv, energy_compensation,
25572554
#ifdef LIGHT_BACKLIGHT_USED
25582555
backlight,
25592556
#endif

servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered_inc.glsl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ layout(set = 0, binding = 14) uniform sampler DEFAULT_SAMPLER_LINEAR_WITH_MIPMAP
280280

281281
layout(set = 0, binding = 15) uniform texture2D best_fit_normal_texture;
282282

283+
layout(set = 0, binding = 16) uniform texture2D dfg;
284+
283285
/* Set 1: Render Pass (changes per render pass) */
284286

285287
layout(set = 1, binding = 0, std140) uniform SceneDataBlock {
@@ -454,6 +456,18 @@ vec4 normal_roughness_compatibility(vec4 p_normal_roughness) {
454456
return vec4(normalize(p_normal_roughness.xyz * 2.0 - 1.0) * 0.5 + 0.5, roughness);
455457
}
456458

459+
// https://google.github.io/filament/Filament.html#toc5.3.4.7
460+
// Note: The roughness value is inverted
461+
vec3 prefiltered_dfg(float lod, float NoV) {
462+
return textureLod(sampler2D(dfg, SAMPLER_LINEAR_CLAMP), vec2(NoV, 1.0 - lod), 0.0).rgb;
463+
}
464+
465+
// Compute multiscatter compensation
466+
// https://google.github.io/filament/Filament.html#listing_energycompensationimpl
467+
vec3 get_energy_compensation(vec3 f0, float env) {
468+
return 1.0 + f0 * (1.0 / env - 1.0);
469+
}
470+
457471
/* Set 2 Skeleton & Instancing (can change per item) */
458472

459473
layout(set = 2, binding = 0, std430) restrict readonly buffer Transforms {

servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,8 @@ void main() {
17141714

17151715
light_compute(normal, directional_lights.data[i].direction, view, size_A,
17161716
directional_lights.data[i].color * directional_lights.data[i].energy * tint,
1717-
true, shadow, f0, orms, directional_lights.data[i].specular, albedo, alpha, screen_uv,
1717+
true, shadow, f0, orms, directional_lights.data[i].specular, albedo, alpha,
1718+
screen_uv, vec3(1.0),
17181719
#ifdef LIGHT_BACKLIGHT_USED
17191720
backlight,
17201721
#endif
@@ -1745,7 +1746,7 @@ void main() {
17451746
uvec2 omni_indices = instances.data[draw_call.instance_index].omni_lights;
17461747
for (uint i = 0; i < sc_omni_lights(); i++) {
17471748
uint light_index = (i > 3) ? ((omni_indices.y >> ((i - 4) * 8)) & 0xFF) : ((omni_indices.x >> (i * 8)) & 0xFF);
1748-
light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv,
1749+
light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv, vec3(1.0),
17491750
#ifdef LIGHT_BACKLIGHT_USED
17501751
backlight,
17511752
#endif
@@ -1773,7 +1774,7 @@ void main() {
17731774
uvec2 spot_indices = instances.data[draw_call.instance_index].spot_lights;
17741775
for (uint i = 0; i < sc_spot_lights(); i++) {
17751776
uint light_index = (i > 3) ? ((spot_indices.y >> ((i - 4) * 8)) & 0xFF) : ((spot_indices.x >> (i * 8)) & 0xFF);
1776-
light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv,
1777+
light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv, vec3(1.0),
17771778
#ifdef LIGHT_BACKLIGHT_USED
17781779
backlight,
17791780
#endif

0 commit comments

Comments
 (0)