Skip to content

Commit cdd508b

Browse files
committed
ggx and beckmann cos_remainder_and_pdf() changes
1 parent 830f46c commit cdd508b

File tree

3 files changed

+57
-75
lines changed

3 files changed

+57
-75
lines changed

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

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ float irr_glsl_beckmann_pdf_wo_clamps(in float NdotH2, in float maxNdotV, in flo
112112
float dummy;
113113
return irr_glsl_beckmann_pdf_wo_clamps(ndf, maxNdotV,NdotV2, a2, dummy);
114114
}
115-
float irr_glsl_beckmann_pdf(in irr_glsl_IsotropicViewSurfaceInteraction i, irr_glsl_IsotropicMicrofacetCache h, in float a2)
116-
{
117-
return irr_glsl_beckmann_pdf_wo_clamps(h.NdotH2, max(i.NdotV, 0.0), i.NdotV_squared, a2);
118-
}
119115

120116
// anisotropic PDF
121117
float irr_glsl_beckmann_pdf_wo_clamps(in float ndf, in float maxNdotV, in float TdotV2, in float BdotV2, in float NdotV2, in float ax2, in float ay2, out float onePlusLambda_V)
@@ -134,18 +130,6 @@ float irr_glsl_beckmann_pdf_wo_clamps(in float NdotH2, in float TdotH2, in float
134130
return irr_glsl_beckmann_pdf_wo_clamps(ndf, maxNdotV, TdotV2, BdotV2, NdotV2, ax2, ay2, dummy);
135131
}
136132

137-
float irr_glsl_beckmann_pdf(in irr_glsl_AnisotropicViewSurfaceInteraction i, irr_glsl_AnisotropicMicrofacetCache h, in float ax, in float ax2, in float ay, in float ay2)
138-
{
139-
float TdotH2 = h.TdotH * h.TdotH;
140-
float BdotH2 = h.BdotH * h.BdotH;
141-
float maxNdotV = max(0.0, i.isotropic.NdotV);
142-
float NdotV2 = i.isotropic.NdotV_squared;
143-
float TdotV2 = i.TdotV * i.TdotV;
144-
float BdotV2 = i.BdotV * i.BdotV;
145-
return irr_glsl_beckmann_pdf_wo_clamps(h.isotropic.NdotH2, TdotH2, BdotH2, maxNdotV, TdotV2, BdotV2, NdotV2, ax, ax2, ay, ay2);
146-
}
147-
148-
149133
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)
150134
{
151135
float onePlusLambda_V;
@@ -156,18 +140,19 @@ vec3 irr_glsl_beckmann_cos_remainder_and_pdf_wo_clamps(out float pdf, in float n
156140
}
157141
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)
158142
{
143+
const float ndf = irr_glsl_beckmann(a2, _cache.NdotH2);
144+
float onePlusLambda_V;
145+
pdf = irr_glsl_beckmann_pdf_wo_clamps(ndf, interaction.NdotV, interaction.NdotV_squared, a2, onePlusLambda_V);
146+
vec3 rem = vec3(0.0);
159147
if (_sample.NdotL>FLT_MIN && interaction.NdotV>FLT_MIN)
160148
{
161-
const float ndf = irr_glsl_beckmann(a2, _cache.NdotH2);
162149
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.VdotH);
163150

164-
return irr_glsl_beckmann_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL2, interaction.NdotV, interaction.NdotV_squared, reflectance, a2);
165-
}
166-
else
167-
{
168-
pdf = irr_glsl_beckmann_pdf(interaction, _cache, a2);
169-
return vec3(0.0);
151+
float G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(onePlusLambda_V, _sample.NdotL2, a2);
152+
rem = reflectance * G2_over_G1;
170153
}
154+
155+
return rem;
171156
}
172157

173158

@@ -182,29 +167,31 @@ vec3 irr_glsl_beckmann_aniso_cos_remainder_and_pdf_wo_clamps(out float pdf, in f
182167
}
183168
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)
184169
{
170+
const float ax2 = ax * ax;
171+
const float ay2 = ay * ay;
172+
173+
const float TdotH2 = _cache.TdotH * _cache.TdotH;
174+
const float BdotH2 = _cache.BdotH * _cache.BdotH;
175+
const float TdotV2 = interaction.TdotV * interaction.TdotV;
176+
const float BdotV2 = interaction.BdotV * interaction.BdotV;
177+
178+
const float NdotV2 = interaction.isotropic.NdotV_squared;
179+
180+
const float ndf = irr_glsl_beckmann(ax, ay, ax2, ay2, TdotH2, BdotH2, _cache.isotropic.NdotH2);
181+
float onePlusLambda_V;
182+
pdf = irr_glsl_beckmann_pdf_wo_clamps(ndf, interaction.isotropic.NdotV, TdotV2, BdotV2, NdotV2, ax2, ay2, onePlusLambda_V);
183+
vec3 rem = vec3(0.0);
185184
if (_sample.NdotL>FLT_MIN && interaction.isotropic.NdotV>FLT_MIN)
186185
{
187-
const float TdotH2 = _cache.TdotH*_cache.TdotH;
188-
const float BdotH2 = _cache.BdotH*_cache.BdotH;
189-
190186
const float TdotL2 = _sample.TdotL*_sample.TdotL;
191187
const float BdotL2 = _sample.BdotL*_sample.BdotL;
192188

193-
const float TdotV2 = interaction.TdotV*interaction.TdotV;
194-
const float BdotV2 = interaction.BdotV*interaction.BdotV;
195-
196-
const float ax2 = ax*ax;
197-
const float ay2 = ay*ay;
198-
const float ndf = irr_glsl_beckmann(ax, ay, ax2, ay2, TdotH2, BdotH2, _cache.isotropic.NdotH2);
199189
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.isotropic.VdotH);
200190

201-
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);
202-
}
203-
else
204-
{
205-
pdf = irr_glsl_beckmann_pdf(interaction, _cache, ax, ax*ax, ay, ay*ay);
206-
return vec3(0.0);
191+
rem = irr_glsl_beckmann_aniso_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL2, TdotL2, BdotL2, interaction.isotropic.NdotV, TdotV2, BdotV2, NdotV2, reflectance, ax2, ay2);
207192
}
193+
194+
return rem;
208195
}
209196

210197

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

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ float irr_glsl_ggx_pdf_wo_clamps(in float NdotH2, in float maxNdotV, in float Nd
142142

143143
return irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, maxNdotV);
144144
}
145-
float irr_glsl_ggx_pdf(in irr_glsl_IsotropicViewSurfaceInteraction i, irr_glsl_IsotropicMicrofacetCache h, in float a2)
146-
{
147-
return irr_glsl_ggx_pdf_wo_clamps(h.NdotH2, max(i.NdotV, 0.0), i.NdotV_squared, a2);
148-
}
149145

150146
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)
151147
{
@@ -154,16 +150,6 @@ float irr_glsl_ggx_pdf_wo_clamps(in float NdotH2, in float TdotH2, in float Bdot
154150

155151
return irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, maxNdotV);
156152
}
157-
float irr_glsl_ggx_pdf(in irr_glsl_AnisotropicViewSurfaceInteraction i, irr_glsl_AnisotropicMicrofacetCache h, in float ax, in float ay, in float ax2, in float ay2)
158-
{
159-
float TdotH2 = h.TdotH*h.TdotH;
160-
float BdotH2 = h.BdotH*h.BdotH;
161-
float maxNdotV = max(0.0,i.isotropic.NdotV);
162-
float NdotV2 = i.isotropic.NdotV_squared;
163-
float TdotV2 = i.TdotV*i.TdotV;
164-
float BdotV2 = i.BdotV*i.BdotV;
165-
return irr_glsl_ggx_pdf_wo_clamps(h.isotropic.NdotH2, TdotH2, BdotH2, maxNdotV, NdotV2, TdotV2, BdotV2, ax, ay, ax2, ay2);
166-
}
167153

168154

169155
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 vec3 reflectance, in float a2)
@@ -179,18 +165,20 @@ vec3 irr_glsl_ggx_cos_remainder_and_pdf_wo_clamps(out float pdf, in float ndf, i
179165

180166
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)
181167
{
168+
const float one_minus_a2 = 1.0 - a2;
169+
const float ndf = irr_glsl_ggx_trowbridge_reitz(a2, _cache.NdotH2);
170+
const float devsh_v = irr_glsl_smith_ggx_devsh_part(interaction.NdotV_squared, a2, one_minus_a2);
171+
pdf = irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, interaction.NdotV);
172+
vec3 rem = vec3(0.0);
182173
if (_sample.NdotL>FLT_MIN && interaction.NdotV>FLT_MIN)
183174
{
184-
const float ndf = irr_glsl_ggx_trowbridge_reitz(a2, _cache.NdotH2);
185175
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.VdotH);
176+
const float G2_over_G1 = irr_glsl_ggx_smith_G2_over_G1_devsh(_sample.NdotL, _sample.NdotL2, interaction.NdotV, devsh_v, a2, one_minus_a2);
186177

187-
return irr_glsl_ggx_cos_remainder_and_pdf_wo_clamps(pdf, ndf, _sample.NdotL, _sample.NdotL2, interaction.NdotV, interaction.NdotV_squared, reflectance, a2);
188-
}
189-
else
190-
{
191-
pdf = irr_glsl_ggx_pdf(interaction,_cache,a2);
192-
return vec3(0.0);
178+
rem = reflectance * G2_over_G1;
193179
}
180+
181+
return rem;
194182
}
195183

196184

@@ -210,29 +198,36 @@ vec3 irr_glsl_ggx_aniso_cos_remainder_and_pdf_wo_clamps(out float pdf, in float
210198

211199
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)
212200
{
201+
const float ax2 = ax * ax;
202+
const float ay2 = ay * ay;
203+
204+
const float TdotV2 = interaction.TdotV * interaction.TdotV;
205+
const float BdotV2 = interaction.BdotV * interaction.BdotV;
206+
const float NdotV2 = interaction.isotropic.NdotV_squared;
207+
208+
const float TdotH2 = _cache.TdotH * _cache.TdotH;
209+
const float BdotH2 = _cache.BdotH * _cache.BdotH;
210+
211+
const float devsh_v = irr_glsl_smith_ggx_devsh_part(TdotV2, BdotV2, NdotV2, ax2, ay2);
212+
const float ndf = irr_glsl_ggx_aniso(TdotH2, BdotH2, _cache.isotropic.NdotH2, ax, ay, ax2, ay2);
213+
pdf = irr_glsl_ggx_pdf_wo_clamps(ndf, devsh_v, interaction.isotropic.NdotV);
214+
vec3 rem = vec3(0.0);
213215
if (_sample.NdotL>FLT_MIN && interaction.isotropic.NdotV>FLT_MIN)
214216
{
215-
const float TdotH2 = _cache.TdotH*_cache.TdotH;
216-
const float BdotH2 = _cache.BdotH*_cache.BdotH;
217-
218217
const float TdotL2 = _sample.TdotL*_sample.TdotL;
219218
const float BdotL2 = _sample.BdotL*_sample.BdotL;
220219

221-
const float TdotV2 = interaction.TdotV*interaction.TdotV;
222-
const float BdotV2 = interaction.BdotV*interaction.BdotV;
223-
224-
const float ax2 = ax*ax;
225-
const float ay2 = ay*ay;
226-
const float ndf = irr_glsl_ggx_aniso(TdotH2,BdotH2,_cache.isotropic.NdotH2, ax, ay, ax2, ay2);
227220
const vec3 reflectance = irr_glsl_fresnel_conductor(ior[0], ior[1], _cache.isotropic.VdotH);
221+
const float G2_over_G1 = irr_glsl_ggx_smith_G2_over_G1_devsh(
222+
_sample.NdotL, TdotL2, BdotL2, _sample.NdotL2,
223+
interaction.isotropic.NdotV, devsh_v,
224+
ax2, ay2
225+
);
228226

229-
return irr_glsl_ggx_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);
230-
}
231-
else
232-
{
233-
pdf = irr_glsl_ggx_pdf(interaction, _cache, ax, ay, ax*ax, ay*ay);
234-
return vec3(0.0);
227+
rem = reflectance * G2_over_G1;
235228
}
229+
230+
return rem;
236231
}
237232

238233
#endif

include/irr/builtin/glsl/sampling/cos_weighted.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ float irr_glsl_projected_sphere_remainder_and_pdf(out float pdf, in vec3 L)
4747

4848
float irr_glsl_projected_sphere_pdf(in float L_z)
4949
{
50-
return 0.5*L_z*irr_glsl_RECIPROCAL_PI;
50+
return 0.5*irr_glsl_projected_hemisphere_pdf(L_z);
5151
}
5252

5353
#endif

0 commit comments

Comments
 (0)