Skip to content

Commit ae9c586

Browse files
fixed a bug in beckmann remainder function
1 parent 4f38e0b commit ae9c586

File tree

7 files changed

+93
-95
lines changed

7 files changed

+93
-95
lines changed

examples_tests/46.SamplingValidation/fullscreen.frag

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <irr/builtin/glsl/bxdf/brdf/specular/ggx.glsl>
77
#include <irr/builtin/glsl/bxdf/brdf/specular/beckmann.glsl>
88
#include <irr/builtin/glsl/bxdf/bsdf/diffuse/lambert.glsl>
9-
#include <irr/builtin/glsl/bxdf/bsdf/specular/ggx.glsl>
9+
//#include <irr/builtin/glsl/bxdf/bsdf/specular/ggx.glsl>
1010
//#include <irr/builtin/glsl/bxdf/bsdf/specular/beckmann.glsl>
1111

1212
layout (location = 0) out vec4 Color;
@@ -15,7 +15,7 @@ layout (push_constant) uniform PC {
1515
vec2 a;
1616
uint test;
1717
} pc;
18-
18+
1919
#define ETC_LAMBERT 0u
2020
#define ETC_GGX 1u
2121
#define ETC_BECKMANN 2u
@@ -36,7 +36,6 @@ void main()
3636
float pdf = 0.0;
3737
vec3 rem = vec3(0.0);
3838
vec3 brdf = vec3(0.0);
39-
float multiplier = 0.5;
4039
if (pc.test==ETC_LAMBERT)
4140
{
4241
s = irr_glsl_lambertian_cos_generate(inter,u.xy);
@@ -56,28 +55,33 @@ void main()
5655
s = irr_glsl_beckmann_cos_generate(inter, u.xy, ax, ay, _cache);
5756
rem = irr_glsl_beckmann_aniso_cos_remainder_and_pdf(pdf, s, inter, _cache, ior, ax, ay);
5857
brdf = irr_glsl_beckmann_aniso_height_correlated_cos_eval(s, inter, _cache, ior, ax, ay);
58+
/*
59+
s = irr_glsl_beckmann_cos_generate(inter,u.xy,ax,ay,_cache);
60+
rem = vec3(1.0);//4.0/inter.isotropic.NdotV);
61+
pdf = irr_glsl_beckmann_pdf_wo_clamps(_cache.isotropic.NdotH2,max(inter.isotropic.NdotV,0.0),inter.isotropic.NdotV_squared,ax*ay);
62+
brdf = vec3(pdf);
63+
*/
5964
}
6065
else if (pc.test==ETC_LAMBERT)
6166
{
6267
s = irr_glsl_lambertian_transmitter_cos_generate(inter,u);
6368
rem = vec3(irr_glsl_lambertian_cos_remainder_and_pdf(pdf, s));
6469
brdf = vec3(irr_glsl_lambertian_cos_eval(s));
65-
multiplier = 0.25;
66-
}
70+
}/* cant do the tests properly :(, would need a 3D derivative and determinant
6771
else if (pc.test==ETC_GGX)
6872
{
6973
irr_glsl_AnisotropicMicrofacetCache _cache;
7074
s = irr_glsl_ggx_transmitter_cos_generate(inter, u.xy, ax, ay, _cache);
71-
rem = irr_glsl_ggx_aniso_cos_remainder_and_pdf(pdf, s, inter, _cache, ior, ax, ay);
72-
brdf = irr_glsl_ggx_height_correlated_aniso_cos_eval(s, inter, _cache, ior, ax, ay);
75+
rem = irr_glsl_ggx_transmitter_aniso_cos_remainder_and_pdf(pdf, s, inter, _cache, ior[0].g, ax, ay);
76+
brdf = irr_glsl_ggx_transmitter_height_correlated_aniso_cos_eval(s, inter, _cache, ior[0].g, ax, ay);
7377
multiplier = 0.25;
74-
}/*
78+
}
7579
else if (pc.test==ETC_BECKMANN)
7680
{
7781
irr_glsl_AnisotropicMicrofacetCache _cache;
7882
s = irr_glsl_beckmann_transmitter_cos_generate(inter, u.xy, ax, ay, _cache);
79-
rem = irr_glsl_beckmann_aniso_cos_remainder_and_pdf(pdf, s, inter, _cache, ior, ax, ay);
80-
brdf = irr_glsl_beckmann_aniso_height_correlated_cos_eval(s, inter, _cache, ior, ax, ay);
83+
rem = irr_glsl_beckmann_aniso_cos_remainder_and_pdf(pdf, s, inter, _cache, ior[0].g, ax, ay);
84+
brdf = irr_glsl_beckmann_aniso_height_correlated_cos_eval(s, inter, _cache, ior[0].g, ax, ay);
8185
multiplier = 0.25;
8286
}*/
8387

@@ -90,6 +94,12 @@ void main()
9094
);
9195
float det = determinant(m);
9296

93-
Color = vec4(abs(rem*pdf-brdf),multiplier*abs(det*pdf/s.NdotL)); // preferred version of the test
94-
//Color = vec4(multiplier*abs(det*pdf/s.NdotL),abs(rem*pdf-brdf));
97+
const vec4 validColor = vec4(0.0,0.0,0.0,0.5);
98+
if (s.NdotL>0.0)
99+
{
100+
Color = vec4(abs(rem*pdf-brdf),abs(det*pdf/s.NdotL)*0.5); // preferred version of the test
101+
//Color = vec4(abs(det*pdf/s.NdotL)*0.5,abs(rem*pdf-brdf));
102+
}
103+
else
104+
Color = validColor;
95105
}

include/irr/builtin/glsl/bxdf/brdf/specular/beckmann.glsl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ float irr_glsl_beckmann_pdf_wo_clamps(in float NdotH2, in float TdotH2, in float
130130

131131

132132

133-
vec3 irr_glsl_beckmann_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float maxNdotL, in float NdotL2, in float maxNdotV, in float NdotV2, in vec3 reflectance, in float a2)
133+
vec3 irr_glsl_beckmann_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float NdotL2, in float maxNdotV, in float NdotV2, in vec3 reflectance, in float a2)
134134
{
135135
float onePlusLambda_V;
136136
pdf = irr_glsl_beckmann_pdf_wo_clamps(ndf,maxNdotV,NdotV2,a2,onePlusLambda_V);
137137

138-
float G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(onePlusLambda_V, maxNdotL, NdotL2, a2);
138+
float G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(onePlusLambda_V, NdotL2, a2);
139139
return reflectance*G2_over_G1;
140140
}
141141
vec3 irr_glsl_beckmann_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSample _sample, in irr_glsl_IsotropicViewSurfaceInteraction interaction, in irr_glsl_IsotropicMicrofacetCache _cache, in mat2x3 ior, in float a2)
@@ -145,7 +145,7 @@ vec3 irr_glsl_beckmann_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSam
145145
const float ndf = irr_glsl_beckmann(a2, _cache.NdotH2);
146146
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.VdotH);
147147

148-
return irr_glsl_beckmann_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL, _sample.NdotL2, interaction.NdotV, interaction.NdotV_squared, reflectance, a2);
148+
return irr_glsl_beckmann_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL2, interaction.NdotV, interaction.NdotV_squared, reflectance, a2);
149149
}
150150
else
151151
{
@@ -156,12 +156,12 @@ vec3 irr_glsl_beckmann_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSam
156156

157157

158158

159-
vec3 irr_glsl_beckmann_aniso_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float maxNdotL, in float NdotL2, in float TdotL2, in float BdotL2, in float maxNdotV, in float TdotV2, in float BdotV2, in float NdotV2, in vec3 reflectance, in float ax2, in float ay2)
159+
vec3 irr_glsl_beckmann_aniso_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float NdotL2, in float TdotL2, in float BdotL2, in float maxNdotV, in float TdotV2, in float BdotV2, in float NdotV2, in vec3 reflectance, in float ax2, in float ay2)
160160
{
161161
float onePlusLambda_V;
162162
pdf = irr_glsl_beckmann_pdf_wo_clamps(ndf,maxNdotV,TdotV2,BdotV2,NdotV2,ax2,ay2,onePlusLambda_V);
163163

164-
float G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(onePlusLambda_V, maxNdotL, TdotL2, BdotL2, NdotL2, ax2, ay2);
164+
float G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(onePlusLambda_V, TdotL2, BdotL2, NdotL2, ax2, ay2);
165165
return reflectance * G2_over_G1;
166166
}
167167
vec3 irr_glsl_beckmann_aniso_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSample _sample, in irr_glsl_AnisotropicViewSurfaceInteraction interaction, in irr_glsl_AnisotropicMicrofacetCache _cache, in mat2x3 ior, in float ax, in float ay)
@@ -182,7 +182,7 @@ vec3 irr_glsl_beckmann_aniso_cos_remainder_and_pdf(out float pdf, in irr_glsl_Li
182182
const float ndf = irr_glsl_beckmann(ax, ay, ax2, ay2, TdotH2, BdotH2, _cache.isotropic.NdotH2);
183183
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.isotropic.VdotH);
184184

185-
return irr_glsl_beckmann_aniso_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL, _sample.NdotL2,TdotL2,BdotL2, interaction.isotropic.NdotV,TdotV2,BdotV2, interaction.isotropic.NdotV_squared, reflectance, ax2, ay2);
185+
return irr_glsl_beckmann_aniso_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL2,TdotL2,BdotL2, interaction.isotropic.NdotV,TdotV2,BdotV2, interaction.isotropic.NdotV_squared, reflectance, ax2, ay2);
186186
}
187187
else
188188
{

include/irr/builtin/glsl/bxdf/brdf/specular/ggx.glsl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,41 +141,41 @@ float irr_glsl_ggx_pdf_wo_clamps(in float ndf, in float devsh_v, in float maxNdo
141141
}
142142
float irr_glsl_ggx_pdf_wo_clamps(in float NdotH2, in float maxNdotV, in float NdotV2, in float a2)
143143
{
144-
float ndf = irr_glsl_ggx_trowbridge_reitz(a2, NdotH2);
145-
float devsh_v = irr_glsl_smith_ggx_devsh_part(NdotV2, a2, 1.0-a2);
144+
const float ndf = irr_glsl_ggx_trowbridge_reitz(a2, NdotH2);
145+
const float devsh_v = irr_glsl_smith_ggx_devsh_part(NdotV2, a2, 1.0-a2);
146146

147147
return irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, maxNdotV);
148148
}
149149

150150
float irr_glsl_ggx_pdf_wo_clamps(in float NdotH2, in float TdotH2, in float BdotH2, in float maxNdotV, in float NdotV2, in float TdotV2, in float BdotV2, in float ax, in float ay, in float ax2, in float ay2)
151151
{
152-
float ndf = irr_glsl_ggx_aniso(TdotH2,BdotH2,NdotH2, ax, ay, ax2, ay2);
153-
float devsh_v = irr_glsl_smith_ggx_devsh_part(TdotV2, BdotV2, NdotV2, ax2, ay2);
152+
const float ndf = irr_glsl_ggx_aniso(TdotH2,BdotH2,NdotH2, ax, ay, ax2, ay2);
153+
const float devsh_v = irr_glsl_smith_ggx_devsh_part(TdotV2, BdotV2, NdotV2, ax2, ay2);
154154

155155
return irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, maxNdotV);
156156
}
157157

158158

159159

160-
vec3 irr_glsl_ggx_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float maxNdotL, in float NdotL2, in float maxNdotV, in float NdotV2, in float VdotH, in mat2x3 ior, in float a2)
160+
vec3 irr_glsl_ggx_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float maxNdotL, in float NdotL2, in float maxNdotV, in float NdotV2, in float VdotH, in vec3 reflectance, in float a2)
161161
{
162162
float one_minus_a2 = 1.0 - a2;
163163
float devsh_v = irr_glsl_smith_ggx_devsh_part(NdotV2, a2, one_minus_a2);
164164
pdf = irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, maxNdotV);
165165

166166
float G2_over_G1 = irr_glsl_ggx_smith_G2_over_G1_devsh(maxNdotL, NdotL2, maxNdotV, devsh_v, a2, one_minus_a2);
167167

168-
vec3 fr = irr_glsl_fresnel_conductor(ior[0], ior[1], VdotH);
169-
return fr * G2_over_G1;
168+
return reflectance * G2_over_G1;
170169
}
171170

172171
vec3 irr_glsl_ggx_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSample _sample, in irr_glsl_IsotropicViewSurfaceInteraction interaction, in irr_glsl_IsotropicMicrofacetCache _cache, in mat2x3 ior, in float a2)
173172
{
174173
if (_sample.NdotL>FLT_MIN && interaction.NdotV>FLT_MIN)
175174
{
176175
const float ndf = irr_glsl_ggx_trowbridge_reitz(a2, _cache.NdotH2);
176+
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.VdotH);
177177

178-
return irr_glsl_ggx_cos_remainder_and_pdf_wo_clamps(pdf, ndf, max(_sample.NdotL,0.0), _sample.NdotL2, max(interaction.NdotV,0.0), interaction.NdotV_squared, _cache.VdotH, ior, a2);
178+
return irr_glsl_ggx_cos_remainder_and_pdf_wo_clamps(pdf, ndf, max(_sample.NdotL,0.0), _sample.NdotL2, max(interaction.NdotV,0.0), interaction.NdotV_squared, _cache.VdotH, reflectance, a2);
179179
}
180180
else
181181
{
@@ -185,19 +185,18 @@ vec3 irr_glsl_ggx_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSample _
185185
}
186186

187187

188-
vec3 irr_glsl_ggx_aniso_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float maxNdotL, in float NdotL2, in float TdotL2, in float BdotL2, in float maxNdotV, in float TdotV2, in float BdotV2, in float NdotV2, in float VdotH, in mat2x3 ior, in float ax2,in float ay2)
188+
vec3 irr_glsl_ggx_aniso_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, in float maxNdotL, in float NdotL2, in float TdotL2, in float BdotL2, in float maxNdotV, in float TdotV2, in float BdotV2, in float NdotV2, in float VdotH, in vec3 reflectance, in float ax2,in float ay2)
189189
{
190-
float devsh_v = irr_glsl_smith_ggx_devsh_part(TdotV2, BdotV2, NdotV2, ax2, ay2);
190+
const float devsh_v = irr_glsl_smith_ggx_devsh_part(TdotV2, BdotV2, NdotV2, ax2, ay2);
191191
pdf = irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, maxNdotV);
192192

193-
float G2_over_G1 = irr_glsl_ggx_smith_G2_over_G1_devsh(
193+
const float G2_over_G1 = irr_glsl_ggx_smith_G2_over_G1_devsh(
194194
maxNdotL, TdotL2,BdotL2,NdotL2,
195195
maxNdotV, devsh_v,
196196
ax2, ay2
197197
);
198198

199-
vec3 fr = irr_glsl_fresnel_conductor(ior[0], ior[1], VdotH);
200-
return fr * G2_over_G1;
199+
return reflectance * G2_over_G1;
201200
}
202201

203202
vec3 irr_glsl_ggx_aniso_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSample _sample, in irr_glsl_AnisotropicViewSurfaceInteraction interaction, in irr_glsl_AnisotropicMicrofacetCache _cache, in mat2x3 ior, in float ax, in float ay)
@@ -213,12 +212,12 @@ vec3 irr_glsl_ggx_aniso_cos_remainder_and_pdf(out float pdf, in irr_glsl_LightSa
213212
const float TdotV2 = interaction.TdotV*interaction.TdotV;
214213
const float BdotV2 = interaction.BdotV*interaction.BdotV;
215214

216-
float ax2 = ax*ax;
217-
float ay2 = ay*ay;
218-
float ndf = irr_glsl_ggx_aniso(TdotH2,BdotH2,_cache.isotropic.NdotH2, ax, ay, ax2, ay2);
219-
220-
// ggx doesn't need the special `if` guards because
221-
return irr_glsl_ggx_aniso_cos_remainder_and_pdf_wo_clamps(pdf, ndf, max(_sample.NdotL, 0.0), _sample.NdotL2, TdotL2, BdotL2, max(interaction.isotropic.NdotV, 0.0), TdotV2, BdotV2, interaction.isotropic.NdotV_squared, _cache.isotropic.VdotH, ior, ax2, ay2);
215+
const float ax2 = ax*ax;
216+
const float ay2 = ay*ay;
217+
const float ndf = irr_glsl_ggx_aniso(TdotH2,BdotH2,_cache.isotropic.NdotH2, ax, ay, ax2, ay2);
218+
219+
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.isotropic.VdotH);
220+
return irr_glsl_ggx_aniso_cos_remainder_and_pdf_wo_clamps(pdf, ndf, max(_sample.NdotL, 0.0), _sample.NdotL2, TdotL2, BdotL2, max(interaction.isotropic.NdotV, 0.0), TdotV2, BdotV2, interaction.isotropic.NdotV_squared, _cache.isotropic.VdotH, reflectance, ax2, ay2);
222221
}
223222
else
224223
{

include/irr/builtin/glsl/bxdf/bsdf/specular/dielectric.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ irr_glsl_LightSample irr_glsl_thin_smooth_dielectric_cos_generate_wo_clamps(in v
3535
return smpl;
3636
}
3737

38+
39+
3840
irr_glsl_LightSample irr_glsl_thin_smooth_dielectric_cos_generate_wo_clamps(in vec3 V, in vec3 N, in float NdotV, in float absNdotV, vec3 u, in vec3 eta2, in vec3 luminosityContributionHint)
3941
{
4042
vec3 dummy;
@@ -49,6 +51,7 @@ irr_glsl_LightSample irr_glsl_thin_smooth_dielectric_cos_generate(in irr_glsl_An
4951
}
5052

5153

54+
5255
vec3 irr_glsl_thin_smooth_dielectric_cos_remainder_and_pdf_wo_clamps(out float pdf, in vec3 remainderMetadata)
5356
{
5457
pdf = 1.0 / 0.0; // should be reciprocal probability of the fresnel choice divided by 0.0, but would still be an INF.
@@ -74,6 +77,7 @@ vec3 irr_glsl_thin_smooth_dielectric_cos_remainder_and_pdf(out float pdf, in irr
7477

7578

7679

80+
7781
irr_glsl_LightSample irr_glsl_smooth_dielectric_cos_generate_wo_clamps(in vec3 V, in vec3 N, in bool backside, in float NdotV, in float absNdotV, in float NdotV2, vec3 u, in float rcpOrientedEta, in float orientedEta2, in float rcpOrientedEta2, out bool transmitted)
7882
{
7983
const float reflectance = irr_glsl_fresnel_dielectric_common(orientedEta2,absNdotV);

0 commit comments

Comments
 (0)