| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
|
| 18 |
#include <ibl/CubemapIBL.h> |
| 19 |
|
| 20 |
#include <ibl/Cubemap.h> |
| 21 |
#include <ibl/CubemapUtils.h> |
| 22 |
#include <ibl/utilities.h> |
| 23 |
|
| 24 |
#include "CubemapUtilsImpl.h" |
| 25 |
|
| 26 |
#include <utils/JobSystem.h> |
| 27 |
|
| 28 |
#include <math/mat3.h> |
| 29 |
#include <math/scalar.h> |
| 30 |
|
| 31 |
#include <random> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
using namespace filament::math; |
| 35 |
using namespace utils; |
| 36 |
|
| 37 |
namespace filament { |
| 38 |
namespace ibl { |
| 39 |
|
| 40 |
static float pow5(float x) { |
| 41 |
const float x2 = x * x; |
| 42 |
return x2 * x2 * x; |
| 43 |
} |
| 44 |
|
| 45 |
static float pow6(float x) { |
| 46 |
const float x2 = x * x; |
| 47 |
return x2 * x2 * x2; |
| 48 |
} |
| 49 |
|
| 50 |
static float3 hemisphereImportanceSampleDggx(float2 u, float a) { // pdf = D(a) * cosTheta |
| 51 |
const float phi = 2.0f * (float) F_PI * u.x; |
| 52 |
// NOTE: (aa-1) == (a-1)(a+1) produces better fp accuracy |
| 53 |
const float cosTheta2 = (1 - u.y) / (1 + (a + 1) * ((a - 1) * u.y)); |
| 54 |
const float cosTheta = std::sqrt(cosTheta2); |
| 55 |
const float sinTheta = std::sqrt(1 - cosTheta2); |
| 56 |
return { sinTheta * std::cos(phi), sinTheta * std::sin(phi), cosTheta }; |
| 57 |
} |
| 58 |
|
| 59 |
static float3 UTILS_UNUSED hemisphereCosSample(float2 u) { // pdf = cosTheta / F_PI; |
| 60 |
const float phi = 2.0f * (float) F_PI * u.x; |
| 61 |
const float cosTheta2 = 1 - u.y; |
| 62 |
const float cosTheta = std::sqrt(cosTheta2); |
| 63 |
const float sinTheta = std::sqrt(1 - cosTheta2); |
| 64 |
return { sinTheta * std::cos(phi), sinTheta * std::sin(phi), cosTheta }; |
| 65 |
} |
| 66 |
|
| 67 |
static float3 UTILS_UNUSED hemisphereUniformSample(float2 u) { // pdf = 1.0 / (2.0 * F_PI); |
| 68 |
const float phi = 2.0f * (float) F_PI * u.x; |
| 69 |
const float cosTheta = 1 - u.y; |
| 70 |
const float sinTheta = std::sqrt(1 - cosTheta * cosTheta); |
| 71 |
return { sinTheta * std::cos(phi), sinTheta * std::sin(phi), cosTheta }; |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* |
| 76 |
* Importance sampling Charlie |
| 77 |
* --------------------------- |
| 78 |
* |
| 79 |
* In order to pick the most significative samples and increase the convergence rate, we chose to |
| 80 |
* rely on Charlie's distribution function for the pdf as we do in hemisphereImportanceSampleDggx. |
| 81 |
* |
| 82 |
* To determine the direction we then need to resolve the cdf associated to the chosen pdf for random inputs. |
| 83 |
* |
| 84 |
* Knowing pdf() = DCharlie(h) <n•h> |
| 85 |
* |
| 86 |
* We need to find the cdf: |
| 87 |
* |
| 88 |
* / 2pi / pi/2 |
| 89 |
* | | (2 + (1 / a)) * sin(theta) ^ (1 / a) * cos(theta) * sin(theta) |
| 90 |
* / phi=0 / theta=0 |
| 91 |
* |
| 92 |
* We sample theta and phi independently. |
| 93 |
* |
| 94 |
* 1. as in all the other isotropic cases phi = 2 * pi * epsilon |
| 95 |
* (https://www.tobias-franke.eu/log/2014/03/30/notes_on_importance_sampling.html) |
| 96 |
* |
| 97 |
* 2. we need to solve the integral on theta: |
| 98 |
* |
| 99 |
* / sTheta |
| 100 |
* P(sTheta) = | (2 + (1 / a)) * sin(theta) ^ (1 / a + 1) * cos(theta) * dtheta |
| 101 |
* / theta=0 |
| 102 |
* |
| 103 |
* By subsitution of u = sin(theta) and du = cos(theta) * dtheta |
| 104 |
* |
| 105 |
* / |
| 106 |
* | (2 + (1 / a)) * u ^ (1 / a + 1) * du |
| 107 |
* / |
| 108 |
* |
| 109 |
* = (2 + (1 / a)) * u ^ (1 / a + 2) / (1 / a + 2) |
| 110 |
* |
| 111 |
* = u ^ (1 / a + 2) |
| 112 |
* |
| 113 |
* = sin(theta) ^ (1 / a + 2) |
| 114 |
* |
| 115 |
* +- -+ sTheta |
| 116 |
* P(sTheta) = | sin(theta) ^ (1 / a + 2) | |
| 117 |
* +- -+ 0 |
| 118 |
* |
| 119 |
* P(sTheta) = sin(sTheta) ^ (1 / a + 2) |
| 120 |
* |
| 121 |
* We now need to resolve the cdf for an epsilon value: |
| 122 |
* |
| 123 |
* epsilon = sin(theta) ^ (a / ( 2 * a + 1)) |
| 124 |
* |
| 125 |
* +--------------------------------------------+ |
| 126 |
* | | |
| 127 |
* | sin(theta) = epsilon ^ (a / ( 2 * a + 1)) | |
| 128 |
* | | |
| 129 |
* +--------------------------------------------+ |
| 130 |
*/ |
| 131 |
static float3 UTILS_UNUSED hemisphereImportanceSampleDCharlie(float2 u, float a) { // pdf = DistributionCharlie() * cosTheta |
| 132 |
const float phi = 2.0f * (float) F_PI * u.x; |
| 133 |
|
| 134 |
const float sinTheta = std::pow(u.y, a / (2 * a + 1)); |
| 135 |
const float cosTheta = std::sqrt(1 - sinTheta * sinTheta); |
| 136 |
|
| 137 |
return { sinTheta * std::cos(phi), sinTheta * std::sin(phi), cosTheta }; |
| 138 |
} |
| 139 |
|
| 140 |
static float DistributionGGX(float NoH, float linearRoughness) { |
| 141 |
// NOTE: (aa-1) == (a-1)(a+1) produces better fp accuracy |
| 142 |
float a = linearRoughness; |
| 143 |
float f = (a - 1) * ((a + 1) * (NoH * NoH)) + 1; |
| 144 |
return (a * a) / ((float) F_PI * f * f); |
| 145 |
} |
| 146 |
|
| 147 |
static float UTILS_UNUSED DistributionAshikhmin(float NoH, float linearRoughness) { |
| 148 |
float a = linearRoughness; |
| 149 |
float a2 = a * a; |
| 150 |
float cos2h = NoH * NoH; |
| 151 |
float sin2h = 1 - cos2h; |
| 152 |
float sin4h = sin2h * sin2h; |
| 153 |
return 1.0f / ((float) F_PI * (1 + 4 * a2)) * (sin4h + 4 * std::exp(-cos2h / (a2 * sin2h))); |
| 154 |
} |
| 155 |
|
| 156 |
static float UTILS_UNUSED DistributionCharlie(float NoH, float linearRoughness) { |
| 157 |
// Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF" |
| 158 |
float a = linearRoughness; |
| 159 |
float invAlpha = 1 / a; |
| 160 |
float cos2h = NoH * NoH; |
| 161 |
float sin2h = 1 - cos2h; |
| 162 |
return (2.0f + invAlpha) * std::pow(sin2h, invAlpha * 0.5f) / (2.0f * (float) F_PI); |
| 163 |
} |
| 164 |
|
| 165 |
static float Fresnel(float f0, float f90, float LoH) { |
| 166 |
const float Fc = pow5(1 - LoH); |
| 167 |
return f0 * (1 - Fc) + f90 * Fc; |
| 168 |
} |
| 169 |
|
| 170 |
static float Visibility(float NoV, float NoL, float a) { |
| 171 |
// Heitz 2014, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs" |
| 172 |
// Height-correlated GGX |
| 173 |
const float a2 = a * a; |
| 174 |
const float GGXL = NoV * std::sqrt((NoL - NoL * a2) * NoL + a2); |
| 175 |
const float GGXV = NoL * std::sqrt((NoV - NoV * a2) * NoV + a2); |
| 176 |
return 0.5f / (GGXV + GGXL); |
| 177 |
} |
| 178 |
|
| 179 |
static float UTILS_UNUSED VisibilityAshikhmin(float NoV, float NoL, float /*a*/) { |
| 180 |
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886" |
| 181 |
return 1 / (4 * (NoL + NoV - NoL * NoV)); |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* |
| 186 |
* Importance sampling GGX - Trowbridge-Reitz |
| 187 |
* ------------------------------------------ |
| 188 |
* |
| 189 |
* Important samples are chosen to integrate Dggx() * cos(theta) over the hemisphere. |
| 190 |
* |
| 191 |
* All calculations are made in tangent space, with n = [0 0 1] |
| 192 |
* |
| 193 |
* l h (important sample) |
| 194 |
* .\ /. |
| 195 |
* . \ / . |
| 196 |
* . \ / . |
| 197 |
* . \/ . |
| 198 |
* ----+---o----+-------> n [0 0 1] |
| 199 |
* cos(2*theta) cos(theta) |
| 200 |
* = n•l = n•h |
| 201 |
* |
| 202 |
* v = n |
| 203 |
* f0 = f90 = 1 |
| 204 |
* V = 1 |
| 205 |
* |
| 206 |
* h is micro facet's normal |
| 207 |
* |
| 208 |
* l is the reflection of v (i.e.: n) around h ==> n•h = l•h = v•h |
| 209 |
* |
| 210 |
* h = important_sample_ggx() |
| 211 |
* |
| 212 |
* n•h = [0 0 1]•h = h.z |
| 213 |
* |
| 214 |
* l = reflect(-n, h) |
| 215 |
* = 2 * (n•h) * h - n; |
| 216 |
* |
| 217 |
* n•l = cos(2 * theta) |
| 218 |
* = cos(theta)^2 - sin(theta)^2 |
| 219 |
* = (n•h)^2 - (1 - (n•h)^2) |
| 220 |
* = 2(n•h)^2 - 1 |
| 221 |
* |
| 222 |
* |
| 223 |
* pdf() = D(h) <n•h> |J(h)| |
| 224 |
* |
| 225 |
* 1 |
| 226 |
* |J(h)| = ---------- |
| 227 |
* 4 <v•h> |
| 228 |
* |
| 229 |
* |
| 230 |
* Pre-filtered importance sampling |
| 231 |
* -------------------------------- |
| 232 |
* |
| 233 |
* see: "Real-time Shading with Filtered Importance Sampling", Jaroslav Krivanek |
| 234 |
* see: "GPU-Based Importance Sampling, GPU Gems 3", Mark Colbert |
| 235 |
* |
| 236 |
* |
| 237 |
* Ωs |
| 238 |
* lod = log4(K ----) |
| 239 |
* Ωp |
| 240 |
* |
| 241 |
* log4(K) = 1, works well for box filters |
| 242 |
* K = 4 |
| 243 |
* |
| 244 |
* 1 |
| 245 |
* Ωs = ---------, solid-angle of an important sample |
| 246 |
* N * pdf |
| 247 |
* |
| 248 |
* 4 PI |
| 249 |
* Ωp ~ --------------, solid-angle of a sample in the base cubemap |
| 250 |
* texel_count |
| 251 |
* |
| 252 |
* |
| 253 |
* Evaluating the integral |
| 254 |
* ----------------------- |
| 255 |
* |
| 256 |
* K fr(h) |
| 257 |
* Er() = --- ∑ ------- L(h) <n•l> |
| 258 |
* N h pdf |
| 259 |
* |
| 260 |
* with: |
| 261 |
* |
| 262 |
* fr() = D(h) |
| 263 |
* |
| 264 |
* N |
| 265 |
* K = ----------------- |
| 266 |
* fr(h) |
| 267 |
* ∑ ------- <n•l> |
| 268 |
* h pdf |
| 269 |
* |
| 270 |
* |
| 271 |
* It results that: |
| 272 |
* |
| 273 |
* K 4 <v•h> |
| 274 |
* Er() = --- ∑ D(h) ------------ L(h) <n•l> |
| 275 |
* N h D(h) <n•h> |
| 276 |
* |
| 277 |
* |
| 278 |
* K |
| 279 |
* Er() = 4 --- ∑ L(h) <n•l> |
| 280 |
* N h |
| 281 |
* |
| 282 |
* N 4 |
| 283 |
* Er() = ------------- --- ∑ V(v) <n•l> |
| 284 |
* 4 ∑ <n•l> N |
| 285 |
* |
| 286 |
* |
| 287 |
* +------------------------------+ |
| 288 |
* | ∑ <n•l> L(h) | |
| 289 |
* | Er() = -------------- | |
| 290 |
* | ∑ <n•l> | |
| 291 |
* +------------------------------+ |
| 292 |
* |
| 293 |
*/ |
| 294 |
|
| 295 |
void CubemapIBL::roughnessFilter( |
| 296 |
utils::JobSystem& js, Cubemap& dst, const std::vector<Cubemap>& levels, |
| 297 |
float linearRoughness, size_t maxNumSamples, math::float3 mirror, bool prefilter, |
| 298 |
Progress updater, void* userdata) |
| 299 |
{ |
| 300 |
const float numSamples = maxNumSamples; |
| 301 |
const float inumSamples = 1.0f / numSamples; |
| 302 |
const size_t maxLevel = levels.size()-1; |
| 303 |
const float maxLevelf = maxLevel; |
| 304 |
const Cubemap& base(levels[0]); |
| 305 |
const size_t dim0 = base.getDimensions(); |
| 306 |
const float omegaP = (4.0f * (float) F_PI) / float(6 * dim0 * dim0); |
| 307 |
std::atomic_uint progress = {0}; |
| 308 |
|
| 309 |
if (linearRoughness == 0) { |
| 310 |
auto scanline = [&] |
| 311 |
(CubemapUtils::EmptyState&, size_t y, Cubemap::Face f, Cubemap::Texel* data, size_t dim) { |
| 312 |
if (UTILS_UNLIKELY(updater)) { |
| 313 |
size_t p = progress.fetch_add(1, std::memory_order_relaxed) + 1; |
| 314 |
updater(0, (float)p / ((float) dim * 6.0f), userdata); |
| 315 |
} |
| 316 |
const Cubemap& cm = levels[0]; |
| 317 |
for (size_t x = 0; x < dim; ++x, ++data) { |
| 318 |
const float2 p(Cubemap::center(x, y)); |
| 319 |
const float3 N(dst.getDirectionFor(f, p.x, p.y) * mirror); |
| 320 |
// FIXME: we should pick the proper LOD here and do trilinear filtering |
| 321 |
Cubemap::writeAt(data, cm.sampleAt(N)); |
| 322 |
} |
| 323 |
}; |
| 324 |
// at least 256 pixel cubemap before we use multithreading -- the overhead of launching |
| 325 |
// jobs is too large compared to the work above. |
| 326 |
if (dst.getDimensions() <= 256) { |
| 327 |
CubemapUtils::processSingleThreaded<CubemapUtils::EmptyState>( |
| 328 |
dst, js, std::ref(scanline)); |
| 329 |
} else { |
| 330 |
CubemapUtils::process<CubemapUtils::EmptyState>(dst, js, std::ref(scanline)); |
| 331 |
} |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
// be careful w/ the size of this structure, the smaller the better |
| 336 |
struct CacheEntry { |
| 337 |
float3 L; |
| 338 |
float brdf_NoL; |
| 339 |
float lerp; |
| 340 |
uint8_t l0; |
| 341 |
uint8_t l1; |
| 342 |
}; |
| 343 |
|
| 344 |
std::vector<CacheEntry> cache; |
| 345 |
cache.reserve(maxNumSamples); |
| 346 |
|
| 347 |
// precompute everything that only depends on the sample # |
| 348 |
float weight = 0; |
| 349 |
// index of the sample to use |
| 350 |
// our goal is to use maxNumSamples for which NoL is > 0 |
| 351 |
// to achieve this, we might have to try more samples than |
| 352 |
// maxNumSamples |
| 353 |
for (size_t sampleIndex = 0 ; sampleIndex < maxNumSamples; sampleIndex++) { |
| 354 |
|
| 355 |
// get Hammersley distribution for the half-sphere |
| 356 |
const float2 u = hammersley(uint32_t(sampleIndex), inumSamples); |
| 357 |
|
| 358 |
// Importance sampling GGX - Trowbridge-Reitz |
| 359 |
const float3 H = hemisphereImportanceSampleDggx(u, linearRoughness); |
| 360 |
|
| 361 |
#if 0 |
| 362 |
// This produces the same result that the code below using the the non-simplified |
| 363 |
// equation. This let's us see that N == V and that L = -reflect(V, H) |
| 364 |
// Keep this for reference. |
| 365 |
const float3 N = {0, 0, 1}; |
| 366 |
const float3 V = N; |
| 367 |
const float3 L = 2 * dot(H, V) * H - V; |
| 368 |
const float NoL = dot(N, L); |
| 369 |
const float NoH = dot(N, H); |
| 370 |
const float NoH2 = NoH * NoH; |
| 371 |
const float NoV = dot(N, V); |
| 372 |
#else |
| 373 |
const float NoH = H.z; |
| 374 |
const float NoH2 = H.z * H.z; |
| 375 |
const float NoL = 2 * NoH2 - 1; |
| 376 |
const float3 L(2 * NoH * H.x, 2 * NoH * H.y, NoL); |
| 377 |
#endif |
| 378 |
|
| 379 |
if (NoL > 0) { |
| 380 |
const float pdf = DistributionGGX(NoH, linearRoughness) / 4; |
| 381 |
|
| 382 |
// K is a LOD bias that allows a bit of overlapping between samples |
| 383 |
constexpr float K = 4; |
| 384 |
const float omegaS = 1 / (numSamples * pdf); |
| 385 |
const float l = float(log4(omegaS) - log4(omegaP) + log4(K)); |
| 386 |
const float mipLevel = prefilter ? clamp(float(l), 0.0f, maxLevelf) : 0.0f; |
| 387 |
|
| 388 |
const float brdf_NoL = float(NoL); |
| 389 |
|
| 390 |
weight += brdf_NoL; |
| 391 |
|
| 392 |
uint8_t l0 = uint8_t(mipLevel); |
| 393 |
uint8_t l1 = uint8_t(std::min(maxLevel, size_t(l0 + 1))); |
| 394 |
float lerp = mipLevel - (float) l0; |
| 395 |
|
| 396 |
cache.push_back({ L, brdf_NoL, lerp, l0, l1 }); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
for (auto& entry : cache) { |
| 401 |
entry.brdf_NoL *= 1.0f / weight; |
| 402 |
} |
| 403 |
|
| 404 |
// we can sample the cubemap in any order, sort by the weight, it could improve fp precision |
| 405 |
std::sort(cache.begin(), cache.end(), [](CacheEntry const& lhs, CacheEntry const& rhs) { |
| 406 |
return lhs.brdf_NoL < rhs.brdf_NoL; |
| 407 |
}); |
| 408 |
|
| 409 |
|
| 410 |
struct State { |
| 411 |
// maybe blue-noise instead would look even better |
| 412 |
std::default_random_engine gen; |
| 413 |
std::uniform_real_distribution<float> distribution{ -F_PI, F_PI }; |
| 414 |
}; |
| 415 |
|
| 416 |
auto scanline = [&](State& state, size_t y, |
| 417 |
Cubemap::Face f, Cubemap::Texel* data, size_t dim) { |
| 418 |
if (UTILS_UNLIKELY(updater)) { |
| 419 |
size_t p = progress.fetch_add(1, std::memory_order_relaxed) + 1; |
| 420 |
updater(0, (float) p / ((float) dim * 6.0f), userdata); |
| 421 |
} |
| 422 |
mat3 R; |
| 423 |
const size_t numSamples = cache.size(); |
| 424 |
for (size_t x = 0; x < dim; ++x, ++data) { |
| 425 |
const float2 p(Cubemap::center(x, y)); |
| 426 |
const float3 N(dst.getDirectionFor(f, p.x, p.y) * mirror); |
| 427 |
|
| 428 |
// center the cone around the normal (handle case of normal close to up) |
| 429 |
const float3 up = std::abs(N.z) < 0.999 ? float3(0, 0, 1) : float3(1, 0, 0); |
| 430 |
R[0] = normalize(cross(up, N)); |
| 431 |
R[1] = cross(N, R[0]); |
| 432 |
R[2] = N; |
| 433 |
|
| 434 |
R *= mat3f::rotation(state.distribution(state.gen), float3{0,0,1}); |
| 435 |
|
| 436 |
float3 Li = 0; |
| 437 |
for (size_t sample = 0; sample < numSamples; sample++) { |
| 438 |
const CacheEntry& e = cache[sample]; |
| 439 |
const float3 L(R * e.L); |
| 440 |
const Cubemap& cmBase = levels[e.l0]; |
| 441 |
const Cubemap& next = levels[e.l1]; |
| 442 |
const float3 c0 = Cubemap::trilinearFilterAt(cmBase, next, e.lerp, L); |
| 443 |
Li += c0 * e.brdf_NoL; |
| 444 |
} |
| 445 |
Cubemap::writeAt(data, Cubemap::Texel(Li)); |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
// don't use the jobsystem unless we have enough work per scanline -- or the overhead of |
| 450 |
// launching jobs will prevail. |
| 451 |
if (dst.getDimensions() * maxNumSamples <= 256) { |
| 452 |
CubemapUtils::processSingleThreaded<State>(dst, js, std::ref(scanline)); |
| 453 |
} else { |
| 454 |
CubemapUtils::process<State>(dst, js, std::ref(scanline)); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* |
| 460 |
* Importance sampling |
| 461 |
* ------------------- |
| 462 |
* |
| 463 |
* Important samples are chosen to integrate cos(theta) over the hemisphere. |
| 464 |
* |
| 465 |
* All calculations are made in tangent space, with n = [0 0 1] |
| 466 |
* |
| 467 |
* l (important sample) |
| 468 |
* /. |
| 469 |
* / . |
| 470 |
* / . |
| 471 |
* / . |
| 472 |
* --------o----+-------> n (direction) |
| 473 |
* cos(theta) |
| 474 |
* = n•l |
| 475 |
* |
| 476 |
* |
| 477 |
* 'direction' is given as an input parameter, and serves as tge z direction of the tangent space. |
| 478 |
* |
| 479 |
* l = important_sample_cos() |
| 480 |
* |
| 481 |
* n•l = [0 0 1] • l = l.z |
| 482 |
* |
| 483 |
* n•l |
| 484 |
* pdf() = ----- |
| 485 |
* PI |
| 486 |
* |
| 487 |
* |
| 488 |
* Pre-filtered importance sampling |
| 489 |
* -------------------------------- |
| 490 |
* |
| 491 |
* see: "Real-time Shading with Filtered Importance Sampling", Jaroslav Krivanek |
| 492 |
* see: "GPU-Based Importance Sampling, GPU Gems 3", Mark Colbert |
| 493 |
* |
| 494 |
* |
| 495 |
* Ωs |
| 496 |
* lod = log4(K ----) |
| 497 |
* Ωp |
| 498 |
* |
| 499 |
* log4(K) = 1, works well for box filters |
| 500 |
* K = 4 |
| 501 |
* |
| 502 |
* 1 |
| 503 |
* Ωs = ---------, solid-angle of an important sample |
| 504 |
* N * pdf |
| 505 |
* |
| 506 |
* 4 PI |
| 507 |
* Ωp ~ --------------, solid-angle of a sample in the base cubemap |
| 508 |
* texel_count |
| 509 |
* |
| 510 |
* |
| 511 |
* Evaluating the integral |
| 512 |
* ----------------------- |
| 513 |
* |
| 514 |
* We are trying to evaluate the following integral: |
| 515 |
* |
| 516 |
* / |
| 517 |
* Ed() = | L(s) <n•l> ds |
| 518 |
* / |
| 519 |
* Ω |
| 520 |
* |
| 521 |
* For this, we're using importance sampling: |
| 522 |
* |
| 523 |
* 1 L(l) |
| 524 |
* Ed() = --- ∑ ------- <n•l> |
| 525 |
* N l pdf |
| 526 |
* |
| 527 |
* |
| 528 |
* It results that: |
| 529 |
* |
| 530 |
* 1 PI |
| 531 |
* Ed() = ---- ∑ L(l) ------ <n•l> |
| 532 |
* N l n•l |
| 533 |
* |
| 534 |
* |
| 535 |
* To avoid to multiply by 1/PI in the shader, we do it here, which simplifies to: |
| 536 |
* |
| 537 |
* +----------------------+ |
| 538 |
* | 1 | |
| 539 |
* | Ed() = ---- ∑ L(l) | |
| 540 |
* | N l | |
| 541 |
* +----------------------+ |
| 542 |
* |
| 543 |
*/ |
| 544 |
|
| 545 |
void CubemapIBL::diffuseIrradiance(JobSystem& js, Cubemap& dst, const std::vector<Cubemap>& levels, |
| 546 |
size_t maxNumSamples, CubemapIBL::Progress updater, void* userdata) |
| 547 |
{ |
| 548 |
const float numSamples = maxNumSamples; |
| 549 |
const float inumSamples = 1.0f / numSamples; |
| 550 |
const size_t maxLevel = levels.size()-1; |
| 551 |
const float maxLevelf = maxLevel; |
| 552 |
const Cubemap& base(levels[0]); |
| 553 |
const size_t dim0 = base.getDimensions(); |
| 554 |
const float omegaP = (4.0f * (float) F_PI) / float(6 * dim0 * dim0); |
| 555 |
|
| 556 |
std::atomic_uint progress = {0}; |
| 557 |
|
| 558 |
struct CacheEntry { |
| 559 |
float3 L; |
| 560 |
float lerp; |
| 561 |
uint8_t l0; |
| 562 |
uint8_t l1; |
| 563 |
}; |
| 564 |
|
| 565 |
std::vector<CacheEntry> cache; |
| 566 |
cache.reserve(maxNumSamples); |
| 567 |
|
| 568 |
// precompute everything that only depends on the sample # |
| 569 |
for (size_t sampleIndex = 0; sampleIndex < maxNumSamples; sampleIndex++) { |
| 570 |
// get Hammersley distribution for the half-sphere |
| 571 |
const float2 u = hammersley(uint32_t(sampleIndex), inumSamples); |
| 572 |
const float3 L = hemisphereCosSample(u); |
| 573 |
const float3 N = { 0, 0, 1 }; |
| 574 |
const float NoL = dot(N, L); |
| 575 |
|
| 576 |
if (NoL > 0) { |
| 577 |
float pdf = NoL * (float) F_1_PI; |
| 578 |
|
| 579 |
constexpr float K = 4; |
| 580 |
const float omegaS = 1.0f / (numSamples * pdf); |
| 581 |
const float l = float(log4(omegaS) - log4(omegaP) + log4(K)); |
| 582 |
const float mipLevel = clamp(float(l), 0.0f, maxLevelf); |
| 583 |
|
| 584 |
uint8_t l0 = uint8_t(mipLevel); |
| 585 |
uint8_t l1 = uint8_t(std::min(maxLevel, size_t(l0 + 1))); |
| 586 |
float lerp = mipLevel - (float) l0; |
| 587 |
|
| 588 |
cache.push_back({ L, lerp, l0, l1 }); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
CubemapUtils::process<CubemapUtils::EmptyState>(dst, js, |
| 593 |
[&](CubemapUtils::EmptyState&, size_t y, |
| 594 |
Cubemap::Face f, Cubemap::Texel* data, size_t dim) { |
| 595 |
|
| 596 |
if (updater) { |
| 597 |
size_t p = progress.fetch_add(1, std::memory_order_relaxed) + 1; |
| 598 |
updater(0, (float)p / ((float) dim * 6.0f), userdata); |
| 599 |
} |
| 600 |
|
| 601 |
mat3 R; |
| 602 |
const size_t numSamples = cache.size(); |
| 603 |
for (size_t x = 0; x < dim; ++x, ++data) { |
| 604 |
const float2 p(Cubemap::center(x, y)); |
| 605 |
const float3 N(dst.getDirectionFor(f, p.x, p.y)); |
| 606 |
|
| 607 |
// center the cone around the normal (handle case of normal close to up) |
| 608 |
const float3 up = std::abs(N.z) < 0.999 ? float3(0, 0, 1) : float3(1, 0, 0); |
| 609 |
R[0] = normalize(cross(up, N)); |
| 610 |
R[1] = cross(N, R[0]); |
| 611 |
R[2] = N; |
| 612 |
|
| 613 |
float3 Li = 0; |
| 614 |
for (size_t sample = 0; sample < numSamples; sample++) { |
| 615 |
const CacheEntry& e = cache[sample]; |
| 616 |
const float3 L(R * e.L); |
| 617 |
const Cubemap& cmBase = levels[e.l0]; |
| 618 |
const Cubemap& next = levels[e.l1]; |
| 619 |
const float3 c0 = Cubemap::trilinearFilterAt(cmBase, next, e.lerp, L); |
| 620 |
Li += c0; |
| 621 |
} |
| 622 |
Cubemap::writeAt(data, Cubemap::Texel(Li * inumSamples)); |
| 623 |
} |
| 624 |
}); |
| 625 |
} |
| 626 |
|
| 627 |
// Not importance-sampled |
| 628 |
static float2 UTILS_UNUSED DFV_NoIS(float NoV, float roughness, size_t numSamples) { |
| 629 |
float2 r = 0; |
| 630 |
const float linearRoughness = roughness * roughness; |
| 631 |
const float3 V(std::sqrt(1 - NoV * NoV), 0, NoV); |
| 632 |
for (size_t i = 0; i < numSamples; i++) { |
| 633 |
const float2 u = hammersley(uint32_t(i), 1.0f / numSamples); |
| 634 |
const float3 H = hemisphereCosSample(u); |
| 635 |
const float3 L = 2 * dot(V, H) * H - V; |
| 636 |
const float VoH = saturate(dot(V, H)); |
| 637 |
const float NoL = saturate(L.z); |
| 638 |
const float NoH = saturate(H.z); |
| 639 |
if (NoL > 0) { |
| 640 |
// Note: remember VoH == LoH (H is half vector) |
| 641 |
const float J = 1.0f / (4.0f * VoH); |
| 642 |
const float pdf = NoH / (float) F_PI; |
| 643 |
const float d = DistributionGGX(NoH, linearRoughness) * NoL / (pdf * J); |
| 644 |
const float Fc = pow5(1 - VoH); |
| 645 |
const float v = Visibility(NoV, NoL, linearRoughness); |
| 646 |
r.x += d * v * (1.0f - Fc); |
| 647 |
r.y += d * v * Fc; |
| 648 |
} |
| 649 |
} |
| 650 |
return r / numSamples; |
| 651 |
} |
| 652 |
|
| 653 |
/* |
| 654 |
* |
| 655 |
* Importance sampling GGX - Trowbridge-Reitz |
| 656 |
* ------------------------------------------ |
| 657 |
* |
| 658 |
* Important samples are chosen to integrate Dggx() * cos(theta) over the hemisphere. |
| 659 |
* |
| 660 |
* All calculations are made in tangent space, with n = [0 0 1] |
| 661 |
* |
| 662 |
* h (important sample) |
| 663 |
* /. |
| 664 |
* / . |
| 665 |
* / . |
| 666 |
* / . |
| 667 |
* --------o----+-------> n |
| 668 |
* cos(theta) |
| 669 |
* = n•h |
| 670 |
* |
| 671 |
* h is micro facet's normal |
| 672 |
* l is the reflection of v around h, l = reflect(-v, h) ==> v•h = l•h |
| 673 |
* |
| 674 |
* n•v is given as an input parameter at runtime |
| 675 |
* |
| 676 |
* Since n = [0 0 1], we also have v.z = n•v |
| 677 |
* |
| 678 |
* Since we need to compute v•h, we chose v as below. This choice only affects the |
| 679 |
* computation of v•h (and therefore the fresnel term too), but doesn't affect |
| 680 |
* n•l, which only relies on l.z (which itself only relies on v.z, i.e.: n•v) |
| 681 |
* |
| 682 |
* | sqrt(1 - (n•v)^2) (sin) |
| 683 |
* v = | 0 |
| 684 |
* | n•v (cos) |
| 685 |
* |
| 686 |
* |
| 687 |
* h = important_sample_ggx() |
| 688 |
* |
| 689 |
* l = reflect(-v, h) = 2 * v•h * h - v; |
| 690 |
* |
| 691 |
* n•l = [0 0 1] • l = l.z |
| 692 |
* |
| 693 |
* n•h = [0 0 1] • l = h.z |
| 694 |
* |
| 695 |
* |
| 696 |
* pdf() = D(h) <n•h> |J(h)| |
| 697 |
* |
| 698 |
* 1 |
| 699 |
* |J(h)| = ---------- |
| 700 |
* 4 <v•h> |
| 701 |
* |
| 702 |
* |
| 703 |
* Evaluating the integral |
| 704 |
* ----------------------- |
| 705 |
* |
| 706 |
* We are trying to evaluate the following integral: |
| 707 |
* |
| 708 |
* / |
| 709 |
* Er() = | fr(s) <n•l> ds |
| 710 |
* / |
| 711 |
* Ω |
| 712 |
* |
| 713 |
* For this, we're using importance sampling: |
| 714 |
* |
| 715 |
* 1 fr(h) |
| 716 |
* Er() = --- ∑ ------- <n•l> |
| 717 |
* N h pdf |
| 718 |
* |
| 719 |
* with: |
| 720 |
* |
| 721 |
* fr() = D(h) F(h) V(v, l) |
| 722 |
* |
| 723 |
* |
| 724 |
* It results that: |
| 725 |
* |
| 726 |
* 1 4 <v•h> |
| 727 |
* Er() = --- ∑ D(h) F(h) V(v, l) ------------ <n•l> |
| 728 |
* N h D(h) <n•h> |
| 729 |
* |
| 730 |
* |
| 731 |
* +-------------------------------------------+ |
| 732 |
* | 4 <v•h> | |
| 733 |
* | Er() = --- ∑ F(h) V(v, l) ------- <n•l> | |
| 734 |
* | N h <n•h> | |
| 735 |
* +-------------------------------------------+ |
| 736 |
* |
| 737 |
*/ |
| 738 |
|
| 739 |
static float2 DFV(float NoV, float linearRoughness, size_t numSamples) { |
| 740 |
float2 r = 0; |
| 741 |
const float3 V(std::sqrt(1 - NoV * NoV), 0, NoV); |
| 742 |
for (size_t i = 0; i < numSamples; i++) { |
| 743 |
const float2 u = hammersley(uint32_t(i), 1.0f / numSamples); |
| 744 |
const float3 H = hemisphereImportanceSampleDggx(u, linearRoughness); |
| 745 |
const float3 L = 2 * dot(V, H) * H - V; |
| 746 |
const float VoH = saturate(dot(V, H)); |
| 747 |
const float NoL = saturate(L.z); |
| 748 |
const float NoH = saturate(H.z); |
| 749 |
if (NoL > 0) { |
| 750 |
/* |
| 751 |
* Fc = (1 - V•H)^5 |
| 752 |
* F(h) = f0*(1 - Fc) + f90*Fc |
| 753 |
* |
| 754 |
* f0 and f90 are known at runtime, but thankfully can be factored out, allowing us |
| 755 |
* to split the integral in two terms and store both terms separately in a LUT. |
| 756 |
* |
| 757 |
* At runtime, we can reconstruct Er() exactly as below: |
| 758 |
* |
| 759 |
* 4 <v•h> |
| 760 |
* DFV.x = --- ∑ (1 - Fc) V(v, l) ------- <n•l> |
| 761 |
* N h <n•h> |
| 762 |
* |
| 763 |
* |
| 764 |
* 4 <v•h> |
| 765 |
* DFV.y = --- ∑ ( Fc) V(v, l) ------- <n•l> |
| 766 |
* N h <n•h> |
| 767 |
* |
| 768 |
* |
| 769 |
* Er() = f0 * DFV.x + f90 * DFV.y |
| 770 |
* |
| 771 |
*/ |
| 772 |
const float v = Visibility(NoV, NoL, linearRoughness) * NoL * (VoH / NoH); |
| 773 |
const float Fc = pow5(1 - VoH); |
| 774 |
r.x += v * (1.0f - Fc); |
| 775 |
r.y += v * Fc; |
| 776 |
} |
| 777 |
} |
| 778 |
return r * (4.0f / numSamples); |
| 779 |
} |
| 780 |
|
| 781 |
static float2 DFV_Multiscatter(float NoV, float linearRoughness, size_t numSamples) { |
| 782 |
float2 r = 0; |
| 783 |
const float3 V(std::sqrt(1 - NoV * NoV), 0, NoV); |
| 784 |
for (size_t i = 0; i < numSamples; i++) { |
| 785 |
const float2 u = hammersley(uint32_t(i), 1.0f / numSamples); |
| 786 |
const float3 H = hemisphereImportanceSampleDggx(u, linearRoughness); |
| 787 |
const float3 L = 2 * dot(V, H) * H - V; |
| 788 |
const float VoH = saturate(dot(V, H)); |
| 789 |
const float NoL = saturate(L.z); |
| 790 |
const float NoH = saturate(H.z); |
| 791 |
if (NoL > 0) { |
| 792 |
const float v = Visibility(NoV, NoL, linearRoughness) * NoL * (VoH / NoH); |
| 793 |
const float Fc = pow5(1 - VoH); |
| 794 |
/* |
| 795 |
* Assuming f90 = 1 |
| 796 |
* Fc = (1 - V•H)^5 |
| 797 |
* F(h) = f0*(1 - Fc) + Fc |
| 798 |
* |
| 799 |
* f0 and f90 are known at runtime, but thankfully can be factored out, allowing us |
| 800 |
* to split the integral in two terms and store both terms separately in a LUT. |
| 801 |
* |
| 802 |
* At runtime, we can reconstruct Er() exactly as below: |
| 803 |
* |
| 804 |
* 4 <v•h> |
| 805 |
* DFV.x = --- ∑ Fc V(v, l) ------- <n•l> |
| 806 |
* N h <n•h> |
| 807 |
* |
| 808 |
* |
| 809 |
* 4 <v•h> |
| 810 |
* DFV.y = --- ∑ V(v, l) ------- <n•l> |
| 811 |
* N h <n•h> |
| 812 |
* |
| 813 |
* |
| 814 |
* Er() = (1 - f0) * DFV.x + f0 * DFV.y |
| 815 |
* |
| 816 |
* = mix(DFV.xxx, DFV.yyy, f0) |
| 817 |
* |
| 818 |
*/ |
| 819 |
r.x += v * Fc; |
| 820 |
r.y += v; |
| 821 |
} |
| 822 |
} |
| 823 |
return r * (4.0f / numSamples); |
| 824 |
} |
| 825 |
|
| 826 |
static float UTILS_UNUSED DFV_LazanyiTerm(float NoV, float linearRoughness, size_t numSamples) { |
| 827 |
float r = 0; |
| 828 |
const float cosThetaMax = (float) std::cos(81.7 * F_PI / 180.0); |
| 829 |
const float q = 1.0f / (cosThetaMax * pow6(1.0f - cosThetaMax)); |
| 830 |
const float3 V(std::sqrt(1 - NoV * NoV), 0, NoV); |
| 831 |
for (size_t i = 0; i < numSamples; i++) { |
| 832 |
const float2 u = hammersley(uint32_t(i), 1.0f / numSamples); |
| 833 |
const float3 H = hemisphereImportanceSampleDggx(u, linearRoughness); |
| 834 |
const float3 L = 2 * dot(V, H) * H - V; |
| 835 |
const float VoH = saturate(dot(V, H)); |
| 836 |
const float NoL = saturate(L.z); |
| 837 |
const float NoH = saturate(H.z); |
| 838 |
if (NoL > 0) { |
| 839 |
const float v = Visibility(NoV, NoL, linearRoughness) * NoL * (VoH / NoH); |
| 840 |
const float Fc = pow6(1 - VoH); |
| 841 |
r += v * Fc * VoH * q; |
| 842 |
} |
| 843 |
} |
| 844 |
return r * (4.0f / numSamples); |
| 845 |
} |
| 846 |
|
| 847 |
static float DFV_Charlie_Uniform(float NoV, float linearRoughness, size_t numSamples) { |
| 848 |
float r = 0.0; |
| 849 |
const float3 V(std::sqrt(1 - NoV * NoV), 0, NoV); |
| 850 |
for (size_t i = 0; i < numSamples; i++) { |
| 851 |
const float2 u = hammersley(uint32_t(i), 1.0f / numSamples); |
| 852 |
const float3 H = hemisphereUniformSample(u); |
| 853 |
const float3 L = 2 * dot(V, H) * H - V; |
| 854 |
const float VoH = saturate(dot(V, H)); |
| 855 |
const float NoL = saturate(L.z); |
| 856 |
const float NoH = saturate(H.z); |
| 857 |
if (NoL > 0) { |
| 858 |
const float v = VisibilityAshikhmin(NoV, NoL, linearRoughness); |
| 859 |
const float d = DistributionCharlie(NoH, linearRoughness); |
| 860 |
r += v * d * NoL * VoH; // VoH comes from the Jacobian, 1/(4*VoH) |
| 861 |
} |
| 862 |
} |
| 863 |
// uniform sampling, the PDF is 1/2pi, 4 comes from the Jacobian |
| 864 |
return r * (4.0f * 2.0f * (float) F_PI / numSamples); |
| 865 |
} |
| 866 |
|
| 867 |
/* |
| 868 |
* |
| 869 |
* Importance sampling Charlie |
| 870 |
* --------------------------- |
| 871 |
* |
| 872 |
* Important samples are chosen to integrate DCharlie() * cos(theta) over the hemisphere. |
| 873 |
* |
| 874 |
* All calculations are made in tangent space, with n = [0 0 1] |
| 875 |
* |
| 876 |
* h (important sample) |
| 877 |
* /. |
| 878 |
* / . |
| 879 |
* / . |
| 880 |
* / . |
| 881 |
* --------o----+-------> n |
| 882 |
* cos(theta) |
| 883 |
* = n•h |
| 884 |
* |
| 885 |
* h is micro facet's normal |
| 886 |
* l is the reflection of v around h, l = reflect(-v, h) ==> v•h = l•h |
| 887 |
* |
| 888 |
* n•v is given as an input parameter at runtime |
| 889 |
* |
| 890 |
* Since n = [0 0 1], we also have v.z = n•v |
| 891 |
* |
| 892 |
* Since we need to compute v•h, we chose v as below. This choice only affects the |
| 893 |
* computation of v•h (and therefore the fresnel term too), but doesn't affect |
| 894 |
* n•l, which only relies on l.z (which itself only relies on v.z, i.e.: n•v) |
| 895 |
* |
| 896 |
* | sqrt(1 - (n•v)^2) (sin) |
| 897 |
* v = | 0 |
| 898 |
* | n•v (cos) |
| 899 |
* |
| 900 |
* |
| 901 |
* h = hemisphereImportanceSampleDCharlie() |
| 902 |
* |
| 903 |
* l = reflect(-v, h) = 2 * v•h * h - v; |
| 904 |
* |
| 905 |
* n•l = [0 0 1] • l = l.z |
| 906 |
* |
| 907 |
* n•h = [0 0 1] • l = h.z |
| 908 |
* |
| 909 |
* |
| 910 |
* pdf() = DCharlie(h) <n•h> |J(h)| |
| 911 |
* |
| 912 |
* 1 |
| 913 |
* |J(h)| = ---------- |
| 914 |
* 4 <v•h> |
| 915 |
* |
| 916 |
* |
| 917 |
* Evaluating the integral |
| 918 |
* ----------------------- |
| 919 |
* |
| 920 |
* We are trying to evaluate the following integral: |
| 921 |
* |
| 922 |
* / |
| 923 |
* Er() = | fr(s) <n•l> ds |
| 924 |
* / |
| 925 |
* Ω |
| 926 |
* |
| 927 |
* For this, we're using importance sampling: |
| 928 |
* |
| 929 |
* 1 fr(h) |
| 930 |
* Er() = --- ∑ ------- <n•l> |
| 931 |
* N h pdf |
| 932 |
* |
| 933 |
* with: |
| 934 |
* |
| 935 |
* fr() = DCharlie(h) V(v, l) |
| 936 |
* |
| 937 |
* |
| 938 |
* It results that: |
| 939 |
* |
| 940 |
* 1 4 <v•h> |
| 941 |
* Er() = --- ∑ DCharlie(h) V(v, l) ------------ <n•l> |
| 942 |
* N h DCharlie(h) <n•h> |
| 943 |
* |
| 944 |
* |
| 945 |
* +---------------------------------------+ |
| 946 |
* | 4 <v•h> | |
| 947 |
* | Er() = --- ∑ V(v, l) ------- <n•l> | |
| 948 |
* | N h <n•h> | |
| 949 |
* +---------------------------------------+ |
| 950 |
* |
| 951 |
*/ |
| 952 |
static float UTILS_UNUSED DFV_Charlie_IS(float NoV, float linearRoughness, size_t numSamples) { |
| 953 |
float r = 0.0; |
| 954 |
const float3 V(std::sqrt(1 - NoV * NoV), 0, NoV); |
| 955 |
for (size_t i = 0; i < numSamples; i++) { |
| 956 |
const float2 u = hammersley(uint32_t(i), 1.0f / numSamples); |
| 957 |
const float3 H = hemisphereImportanceSampleDCharlie(u, linearRoughness); |
| 958 |
const float3 L = 2 * dot(V, H) * H - V; |
| 959 |
const float VoH = saturate(dot(V, H)); |
| 960 |
const float NoL = saturate(L.z); |
| 961 |
const float NoH = saturate(H.z); |
| 962 |
if (NoL > 0) { |
| 963 |
const float J = 1.0f / (4.0f * VoH); |
| 964 |
const float pdf = NoH; // D has been removed as it cancels out in the previous equation |
| 965 |
const float v = VisibilityAshikhmin(NoV, NoL, linearRoughness); |
| 966 |
|
| 967 |
r += v * NoL / (pdf * J); |
| 968 |
} |
| 969 |
} |
| 970 |
return r / numSamples; |
| 971 |
} |
| 972 |
|
| 973 |
void CubemapIBL::brdf(utils::JobSystem& js, Cubemap& dst, float linearRoughness) { |
| 974 |
CubemapUtils::process<CubemapUtils::EmptyState>(dst, js, |
| 975 |
[ & ](CubemapUtils::EmptyState&, size_t y, |
| 976 |
Cubemap::Face f, Cubemap::Texel* data, size_t dim) { |
| 977 |
for (size_t x=0 ; x<dim ; ++x, ++data) { |
| 978 |
const float2 p(Cubemap::center(x, y)); |
| 979 |
const float3 H(dst.getDirectionFor(f, p.x, p.y)); |
| 980 |
const float3 N = { 0, 0, 1 }; |
| 981 |
const float3 V = N; |
| 982 |
const float3 L = 2 * dot(H, V) * H - V; |
| 983 |
const float NoL = dot(N, L); |
| 984 |
const float NoH = dot(N, H); |
| 985 |
const float NoV = dot(N, V); |
| 986 |
const float LoH = dot(L, H); |
| 987 |
float brdf_NoL = 0; |
| 988 |
if (NoL > 0 && LoH > 0) { |
| 989 |
const float D = DistributionGGX(NoH, linearRoughness); |
| 990 |
const float F = Fresnel(0.04f, 1.0f, LoH); |
| 991 |
const float V = Visibility(NoV, NoL, linearRoughness); |
| 992 |
brdf_NoL = float(D * F * V * NoL); |
| 993 |
} |
| 994 |
Cubemap::writeAt(data, Cubemap::Texel{ brdf_NoL }); |
| 995 |
} |
| 996 |
}); |
| 997 |
} |
| 998 |
|
| 999 |
void CubemapIBL::DFG(JobSystem& js, Image& dst, bool multiscatter, bool cloth) { |
| 1000 |
auto dfvFunction = multiscatter ? DFV_Multiscatter : DFV; |
| 1001 |
auto job = jobs::parallel_for<char>(js, nullptr, nullptr, uint32_t(dst.getHeight()), |
| 1002 |
[&dst, dfvFunction, cloth](char const* d, size_t c) { |
| 1003 |
const size_t width = dst.getWidth(); |
| 1004 |
const size_t height = dst.getHeight(); |
| 1005 |
size_t y0 = size_t(d); |
| 1006 |
for (size_t y = y0; y < y0 + c; y++) { |
| 1007 |
Cubemap::Texel* UTILS_RESTRICT data = |
| 1008 |
static_cast<Cubemap::Texel*>(dst.getPixelRef(0, y)); |
| 1009 |
|
| 1010 |
const float h = (float) height; |
| 1011 |
const float coord = saturate((h - y + 0.5f) / h); |
| 1012 |
// map the coordinate in the texture to a linear_roughness, |
| 1013 |
// here we're using ^2, but other mappings are possible. |
| 1014 |
// ==> coord = sqrt(linear_roughness) |
| 1015 |
const float linear_roughness = coord * coord; |
| 1016 |
for (size_t x = 0; x < height; x++, data++) { |
| 1017 |
// const float NoV = float(x) / (width-1); |
| 1018 |
const float NoV = saturate((x + 0.5f) / width); |
| 1019 |
float3 r = { dfvFunction(NoV, linear_roughness, 1024), 0 }; |
| 1020 |
if (cloth) { |
| 1021 |
r.b = float(DFV_Charlie_Uniform(NoV, linear_roughness, 4096)); |
| 1022 |
} |
| 1023 |
*data = r; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
}, jobs::CountSplitter<1, 8>()); |
| 1027 |
js.runAndWait(job); |
| 1028 |
} |
| 1029 |
|
| 1030 |
} // namespace ibl |
| 1031 |
} // namespace filament |