kernel_light.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  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. CCL_NAMESPACE_BEGIN
  17. /* Light Sample result */
  18. typedef struct LightSample {
  19. float3 P; /* position on light, or direction for distant light */
  20. float3 Ng; /* normal on light */
  21. float3 D; /* direction from shading point to light */
  22. float t; /* distance to light (FLT_MAX for distant light) */
  23. float u, v; /* parametric coordinate on primitive */
  24. float pdf; /* light sampling probability density function */
  25. float eval_fac; /* intensity multiplier */
  26. int object; /* object id for triangle/curve lights */
  27. int prim; /* primitive id for triangle/curve lights */
  28. int shader; /* shader id */
  29. int lamp; /* lamp id */
  30. LightType type; /* type of light */
  31. } LightSample;
  32. /* Area light sampling */
  33. /* Uses the following paper:
  34. *
  35. * Carlos Urena et al.
  36. * An Area-Preserving Parametrization for Spherical Rectangles.
  37. *
  38. * https://www.solidangle.com/research/egsr2013_spherical_rectangle.pdf
  39. *
  40. * Note: light_p is modified when sample_coord is true.
  41. */
  42. ccl_device_inline float rect_light_sample(float3 P,
  43. float3 *light_p,
  44. float3 axisu,
  45. float3 axisv,
  46. float randu,
  47. float randv,
  48. bool sample_coord)
  49. {
  50. /* In our name system we're using P for the center,
  51. * which is o in the paper.
  52. */
  53. float3 corner = *light_p - axisu * 0.5f - axisv * 0.5f;
  54. float axisu_len, axisv_len;
  55. /* Compute local reference system R. */
  56. float3 x = normalize_len(axisu, &axisu_len);
  57. float3 y = normalize_len(axisv, &axisv_len);
  58. float3 z = cross(x, y);
  59. /* Compute rectangle coords in local reference system. */
  60. float3 dir = corner - P;
  61. float z0 = dot(dir, z);
  62. /* Flip 'z' to make it point against Q. */
  63. if (z0 > 0.0f) {
  64. z *= -1.0f;
  65. z0 *= -1.0f;
  66. }
  67. float x0 = dot(dir, x);
  68. float y0 = dot(dir, y);
  69. float x1 = x0 + axisu_len;
  70. float y1 = y0 + axisv_len;
  71. /* Compute internal angles (gamma_i). */
  72. float4 diff = make_float4(x0, y1, x1, y0) - make_float4(x1, y0, x0, y1);
  73. float4 nz = make_float4(y0, x1, y1, x0) * diff;
  74. nz = nz / sqrt(z0 * z0 * diff * diff + nz * nz);
  75. float g0 = safe_acosf(-nz.x * nz.y);
  76. float g1 = safe_acosf(-nz.y * nz.z);
  77. float g2 = safe_acosf(-nz.z * nz.w);
  78. float g3 = safe_acosf(-nz.w * nz.x);
  79. /* Compute predefined constants. */
  80. float b0 = nz.x;
  81. float b1 = nz.z;
  82. float b0sq = b0 * b0;
  83. float k = M_2PI_F - g2 - g3;
  84. /* Compute solid angle from internal angles. */
  85. float S = g0 + g1 - k;
  86. if (sample_coord) {
  87. /* Compute cu. */
  88. float au = randu * S + k;
  89. float fu = (cosf(au) * b0 - b1) / sinf(au);
  90. float cu = 1.0f / sqrtf(fu * fu + b0sq) * (fu > 0.0f ? 1.0f : -1.0f);
  91. cu = clamp(cu, -1.0f, 1.0f);
  92. /* Compute xu. */
  93. float xu = -(cu * z0) / max(sqrtf(1.0f - cu * cu), 1e-7f);
  94. xu = clamp(xu, x0, x1);
  95. /* Compute yv. */
  96. float z0sq = z0 * z0;
  97. float y0sq = y0 * y0;
  98. float y1sq = y1 * y1;
  99. float d = sqrtf(xu * xu + z0sq);
  100. float h0 = y0 / sqrtf(d * d + y0sq);
  101. float h1 = y1 / sqrtf(d * d + y1sq);
  102. float hv = h0 + randv * (h1 - h0), hv2 = hv * hv;
  103. float yv = (hv2 < 1.0f - 1e-6f) ? (hv * d) / sqrtf(1.0f - hv2) : y1;
  104. /* Transform (xu, yv, z0) to world coords. */
  105. *light_p = P + xu * x + yv * y + z0 * z;
  106. }
  107. /* return pdf */
  108. if (S != 0.0f)
  109. return 1.0f / S;
  110. else
  111. return 0.0f;
  112. }
  113. ccl_device_inline float3 ellipse_sample(float3 ru, float3 rv, float randu, float randv)
  114. {
  115. to_unit_disk(&randu, &randv);
  116. return ru * randu + rv * randv;
  117. }
  118. ccl_device float3 disk_light_sample(float3 v, float randu, float randv)
  119. {
  120. float3 ru, rv;
  121. make_orthonormals(v, &ru, &rv);
  122. return ellipse_sample(ru, rv, randu, randv);
  123. }
  124. ccl_device float3 distant_light_sample(float3 D, float radius, float randu, float randv)
  125. {
  126. return normalize(D + disk_light_sample(D, randu, randv) * radius);
  127. }
  128. ccl_device float3
  129. sphere_light_sample(float3 P, float3 center, float radius, float randu, float randv)
  130. {
  131. return disk_light_sample(normalize(P - center), randu, randv) * radius;
  132. }
  133. ccl_device float spot_light_attenuation(float3 dir,
  134. float spot_angle,
  135. float spot_smooth,
  136. LightSample *ls)
  137. {
  138. float3 I = ls->Ng;
  139. float attenuation = dot(dir, I);
  140. if (attenuation <= spot_angle) {
  141. attenuation = 0.0f;
  142. }
  143. else {
  144. float t = attenuation - spot_angle;
  145. if (t < spot_smooth && spot_smooth != 0.0f)
  146. attenuation *= smoothstepf(t / spot_smooth);
  147. }
  148. return attenuation;
  149. }
  150. ccl_device float lamp_light_pdf(KernelGlobals *kg, const float3 Ng, const float3 I, float t)
  151. {
  152. float cos_pi = dot(Ng, I);
  153. if (cos_pi <= 0.0f)
  154. return 0.0f;
  155. return t * t / cos_pi;
  156. }
  157. /* Background Light */
  158. #ifdef __BACKGROUND_MIS__
  159. /* TODO(sergey): In theory it should be all fine to use noinline for all
  160. * devices, but we're so close to the release so better not screw things
  161. * up for CPU at least.
  162. */
  163. # ifdef __KERNEL_GPU__
  164. ccl_device_noinline
  165. # else
  166. ccl_device
  167. # endif
  168. float3
  169. background_map_sample(KernelGlobals *kg, float randu, float randv, float *pdf)
  170. {
  171. /* for the following, the CDF values are actually a pair of floats, with the
  172. * function value as X and the actual CDF as Y. The last entry's function
  173. * value is the CDF total. */
  174. int res_x = kernel_data.integrator.pdf_background_res_x;
  175. int res_y = kernel_data.integrator.pdf_background_res_y;
  176. int cdf_width = res_x + 1;
  177. /* this is basically std::lower_bound as used by pbrt */
  178. int first = 0;
  179. int count = res_y;
  180. while (count > 0) {
  181. int step = count >> 1;
  182. int middle = first + step;
  183. if (kernel_tex_fetch(__light_background_marginal_cdf, middle).y < randv) {
  184. first = middle + 1;
  185. count -= step + 1;
  186. }
  187. else
  188. count = step;
  189. }
  190. int index_v = max(0, first - 1);
  191. kernel_assert(index_v >= 0 && index_v < res_y);
  192. float2 cdf_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v);
  193. float2 cdf_next_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v + 1);
  194. float2 cdf_last_v = kernel_tex_fetch(__light_background_marginal_cdf, res_y);
  195. /* importance-sampled V direction */
  196. float dv = inverse_lerp(cdf_v.y, cdf_next_v.y, randv);
  197. float v = (index_v + dv) / res_y;
  198. /* this is basically std::lower_bound as used by pbrt */
  199. first = 0;
  200. count = res_x;
  201. while (count > 0) {
  202. int step = count >> 1;
  203. int middle = first + step;
  204. if (kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_width + middle).y <
  205. randu) {
  206. first = middle + 1;
  207. count -= step + 1;
  208. }
  209. else
  210. count = step;
  211. }
  212. int index_u = max(0, first - 1);
  213. kernel_assert(index_u >= 0 && index_u < res_x);
  214. float2 cdf_u = kernel_tex_fetch(__light_background_conditional_cdf,
  215. index_v * cdf_width + index_u);
  216. float2 cdf_next_u = kernel_tex_fetch(__light_background_conditional_cdf,
  217. index_v * cdf_width + index_u + 1);
  218. float2 cdf_last_u = kernel_tex_fetch(__light_background_conditional_cdf,
  219. index_v * cdf_width + res_x);
  220. /* importance-sampled U direction */
  221. float du = inverse_lerp(cdf_u.y, cdf_next_u.y, randu);
  222. float u = (index_u + du) / res_x;
  223. /* compute pdf */
  224. float denom = cdf_last_u.x * cdf_last_v.x;
  225. float sin_theta = sinf(M_PI_F * v);
  226. if (sin_theta == 0.0f || denom == 0.0f)
  227. *pdf = 0.0f;
  228. else
  229. *pdf = (cdf_u.x * cdf_v.x) / (M_2PI_F * M_PI_F * sin_theta * denom);
  230. /* compute direction */
  231. return equirectangular_to_direction(u, v);
  232. }
  233. /* TODO(sergey): Same as above, after the release we should consider using
  234. * 'noinline' for all devices.
  235. */
  236. # ifdef __KERNEL_GPU__
  237. ccl_device_noinline
  238. # else
  239. ccl_device
  240. # endif
  241. float
  242. background_map_pdf(KernelGlobals *kg, float3 direction)
  243. {
  244. float2 uv = direction_to_equirectangular(direction);
  245. int res_x = kernel_data.integrator.pdf_background_res_x;
  246. int res_y = kernel_data.integrator.pdf_background_res_y;
  247. int cdf_width = res_x + 1;
  248. float sin_theta = sinf(uv.y * M_PI_F);
  249. if (sin_theta == 0.0f)
  250. return 0.0f;
  251. int index_u = clamp(float_to_int(uv.x * res_x), 0, res_x - 1);
  252. int index_v = clamp(float_to_int(uv.y * res_y), 0, res_y - 1);
  253. /* pdfs in V direction */
  254. float2 cdf_last_u = kernel_tex_fetch(__light_background_conditional_cdf,
  255. index_v * cdf_width + res_x);
  256. float2 cdf_last_v = kernel_tex_fetch(__light_background_marginal_cdf, res_y);
  257. float denom = cdf_last_u.x * cdf_last_v.x;
  258. if (denom == 0.0f)
  259. return 0.0f;
  260. /* pdfs in U direction */
  261. float2 cdf_u = kernel_tex_fetch(__light_background_conditional_cdf,
  262. index_v * cdf_width + index_u);
  263. float2 cdf_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v);
  264. return (cdf_u.x * cdf_v.x) / (M_2PI_F * M_PI_F * sin_theta * denom);
  265. }
  266. ccl_device_inline bool background_portal_data_fetch_and_check_side(
  267. KernelGlobals *kg, float3 P, int index, float3 *lightpos, float3 *dir)
  268. {
  269. int portal = kernel_data.integrator.portal_offset + index;
  270. const ccl_global KernelLight *klight = &kernel_tex_fetch(__lights, portal);
  271. *lightpos = make_float3(klight->co[0], klight->co[1], klight->co[2]);
  272. *dir = make_float3(klight->area.dir[0], klight->area.dir[1], klight->area.dir[2]);
  273. /* Check whether portal is on the right side. */
  274. if (dot(*dir, P - *lightpos) > 1e-4f)
  275. return true;
  276. return false;
  277. }
  278. ccl_device_inline float background_portal_pdf(
  279. KernelGlobals *kg, float3 P, float3 direction, int ignore_portal, bool *is_possible)
  280. {
  281. float portal_pdf = 0.0f;
  282. int num_possible = 0;
  283. for (int p = 0; p < kernel_data.integrator.num_portals; p++) {
  284. if (p == ignore_portal)
  285. continue;
  286. float3 lightpos, dir;
  287. if (!background_portal_data_fetch_and_check_side(kg, P, p, &lightpos, &dir))
  288. continue;
  289. /* There's a portal that could be sampled from this position. */
  290. if (is_possible) {
  291. *is_possible = true;
  292. }
  293. num_possible++;
  294. int portal = kernel_data.integrator.portal_offset + p;
  295. const ccl_global KernelLight *klight = &kernel_tex_fetch(__lights, portal);
  296. float3 axisu = make_float3(
  297. klight->area.axisu[0], klight->area.axisu[1], klight->area.axisu[2]);
  298. float3 axisv = make_float3(
  299. klight->area.axisv[0], klight->area.axisv[1], klight->area.axisv[2]);
  300. bool is_round = (klight->area.invarea < 0.0f);
  301. if (!ray_quad_intersect(P,
  302. direction,
  303. 1e-4f,
  304. FLT_MAX,
  305. lightpos,
  306. axisu,
  307. axisv,
  308. dir,
  309. NULL,
  310. NULL,
  311. NULL,
  312. NULL,
  313. is_round))
  314. continue;
  315. if (is_round) {
  316. float t;
  317. float3 D = normalize_len(lightpos - P, &t);
  318. portal_pdf += fabsf(klight->area.invarea) * lamp_light_pdf(kg, dir, -D, t);
  319. }
  320. else {
  321. portal_pdf += rect_light_sample(P, &lightpos, axisu, axisv, 0.0f, 0.0f, false);
  322. }
  323. }
  324. if (ignore_portal >= 0) {
  325. /* We have skipped a portal that could be sampled as well. */
  326. num_possible++;
  327. }
  328. return (num_possible > 0) ? portal_pdf / num_possible : 0.0f;
  329. }
  330. ccl_device int background_num_possible_portals(KernelGlobals *kg, float3 P)
  331. {
  332. int num_possible_portals = 0;
  333. for (int p = 0; p < kernel_data.integrator.num_portals; p++) {
  334. float3 lightpos, dir;
  335. if (background_portal_data_fetch_and_check_side(kg, P, p, &lightpos, &dir))
  336. num_possible_portals++;
  337. }
  338. return num_possible_portals;
  339. }
  340. ccl_device float3 background_portal_sample(KernelGlobals *kg,
  341. float3 P,
  342. float randu,
  343. float randv,
  344. int num_possible,
  345. int *sampled_portal,
  346. float *pdf)
  347. {
  348. /* Pick a portal, then re-normalize randv. */
  349. randv *= num_possible;
  350. int portal = (int)randv;
  351. randv -= portal;
  352. /* TODO(sergey): Some smarter way of finding portal to sample
  353. * is welcome.
  354. */
  355. for (int p = 0; p < kernel_data.integrator.num_portals; p++) {
  356. /* Search for the sampled portal. */
  357. float3 lightpos, dir;
  358. if (!background_portal_data_fetch_and_check_side(kg, P, p, &lightpos, &dir))
  359. continue;
  360. if (portal == 0) {
  361. /* p is the portal to be sampled. */
  362. int portal = kernel_data.integrator.portal_offset + p;
  363. const ccl_global KernelLight *klight = &kernel_tex_fetch(__lights, portal);
  364. float3 axisu = make_float3(
  365. klight->area.axisu[0], klight->area.axisu[1], klight->area.axisu[2]);
  366. float3 axisv = make_float3(
  367. klight->area.axisv[0], klight->area.axisv[1], klight->area.axisv[2]);
  368. bool is_round = (klight->area.invarea < 0.0f);
  369. float3 D;
  370. if (is_round) {
  371. lightpos += ellipse_sample(axisu * 0.5f, axisv * 0.5f, randu, randv);
  372. float t;
  373. D = normalize_len(lightpos - P, &t);
  374. *pdf = fabsf(klight->area.invarea) * lamp_light_pdf(kg, dir, -D, t);
  375. }
  376. else {
  377. *pdf = rect_light_sample(P, &lightpos, axisu, axisv, randu, randv, true);
  378. D = normalize(lightpos - P);
  379. }
  380. *pdf /= num_possible;
  381. *sampled_portal = p;
  382. return D;
  383. }
  384. portal--;
  385. }
  386. return make_float3(0.0f, 0.0f, 0.0f);
  387. }
  388. ccl_device_inline float3
  389. background_light_sample(KernelGlobals *kg, float3 P, float randu, float randv, float *pdf)
  390. {
  391. /* Probability of sampling portals instead of the map. */
  392. float portal_sampling_pdf = kernel_data.integrator.portal_pdf;
  393. /* Check if there are portals in the scene which we can sample. */
  394. if (portal_sampling_pdf > 0.0f) {
  395. int num_portals = background_num_possible_portals(kg, P);
  396. if (num_portals > 0) {
  397. if (portal_sampling_pdf == 1.0f || randu < portal_sampling_pdf) {
  398. if (portal_sampling_pdf < 1.0f) {
  399. randu /= portal_sampling_pdf;
  400. }
  401. int portal;
  402. float3 D = background_portal_sample(kg, P, randu, randv, num_portals, &portal, pdf);
  403. if (num_portals > 1) {
  404. /* Ignore the chosen portal, its pdf is already included. */
  405. *pdf += background_portal_pdf(kg, P, D, portal, NULL);
  406. }
  407. /* We could also have sampled the map, so combine with MIS. */
  408. if (portal_sampling_pdf < 1.0f) {
  409. float cdf_pdf = background_map_pdf(kg, D);
  410. *pdf = (portal_sampling_pdf * (*pdf) + (1.0f - portal_sampling_pdf) * cdf_pdf);
  411. }
  412. return D;
  413. }
  414. else {
  415. /* Sample map, but with nonzero portal_sampling_pdf for MIS. */
  416. randu = (randu - portal_sampling_pdf) / (1.0f - portal_sampling_pdf);
  417. }
  418. }
  419. else {
  420. /* We can't sample a portal.
  421. * Check if we can sample the map instead.
  422. */
  423. if (portal_sampling_pdf == 1.0f) {
  424. /* Use uniform as a fallback if we can't sample the map. */
  425. *pdf = 1.0f / M_4PI_F;
  426. return sample_uniform_sphere(randu, randv);
  427. }
  428. else {
  429. portal_sampling_pdf = 0.0f;
  430. }
  431. }
  432. }
  433. float3 D = background_map_sample(kg, randu, randv, pdf);
  434. /* Use MIS if portals could be sampled as well. */
  435. if (portal_sampling_pdf > 0.0f) {
  436. float portal_pdf = background_portal_pdf(kg, P, D, -1, NULL);
  437. *pdf = (portal_sampling_pdf * portal_pdf + (1.0f - portal_sampling_pdf) * (*pdf));
  438. }
  439. return D;
  440. }
  441. ccl_device float background_light_pdf(KernelGlobals *kg, float3 P, float3 direction)
  442. {
  443. /* Probability of sampling portals instead of the map. */
  444. float portal_sampling_pdf = kernel_data.integrator.portal_pdf;
  445. float portal_pdf = 0.0f, map_pdf = 0.0f;
  446. if (portal_sampling_pdf > 0.0f) {
  447. /* Evaluate PDF of sampling this direction by portal sampling. */
  448. bool is_possible = false;
  449. portal_pdf = background_portal_pdf(kg, P, direction, -1, &is_possible) * portal_sampling_pdf;
  450. if (!is_possible) {
  451. /* Portal sampling is not possible here because all portals point to the wrong side.
  452. * If map sampling is possible, it would be used instead,
  453. * otherwise fallback sampling is used. */
  454. if (portal_sampling_pdf == 1.0f) {
  455. return kernel_data.integrator.pdf_lights / M_4PI_F;
  456. }
  457. else {
  458. /* Force map sampling. */
  459. portal_sampling_pdf = 0.0f;
  460. }
  461. }
  462. }
  463. if (portal_sampling_pdf < 1.0f) {
  464. /* Evaluate PDF of sampling this direction by map sampling. */
  465. map_pdf = background_map_pdf(kg, direction) * (1.0f - portal_sampling_pdf);
  466. }
  467. return (portal_pdf + map_pdf) * kernel_data.integrator.pdf_lights;
  468. }
  469. #endif
  470. /* Regular Light */
  471. ccl_device_inline bool lamp_light_sample(
  472. KernelGlobals *kg, int lamp, float randu, float randv, float3 P, LightSample *ls)
  473. {
  474. const ccl_global KernelLight *klight = &kernel_tex_fetch(__lights, lamp);
  475. LightType type = (LightType)klight->type;
  476. ls->type = type;
  477. ls->shader = klight->shader_id;
  478. ls->object = PRIM_NONE;
  479. ls->prim = PRIM_NONE;
  480. ls->lamp = lamp;
  481. ls->u = randu;
  482. ls->v = randv;
  483. if (type == LIGHT_DISTANT) {
  484. /* distant light */
  485. float3 lightD = make_float3(klight->co[0], klight->co[1], klight->co[2]);
  486. float3 D = lightD;
  487. float radius = klight->distant.radius;
  488. float invarea = klight->distant.invarea;
  489. if (radius > 0.0f)
  490. D = distant_light_sample(D, radius, randu, randv);
  491. ls->P = D;
  492. ls->Ng = D;
  493. ls->D = -D;
  494. ls->t = FLT_MAX;
  495. float costheta = dot(lightD, D);
  496. ls->pdf = invarea / (costheta * costheta * costheta);
  497. ls->eval_fac = ls->pdf;
  498. }
  499. #ifdef __BACKGROUND_MIS__
  500. else if (type == LIGHT_BACKGROUND) {
  501. /* infinite area light (e.g. light dome or env light) */
  502. float3 D = -background_light_sample(kg, P, randu, randv, &ls->pdf);
  503. ls->P = D;
  504. ls->Ng = D;
  505. ls->D = -D;
  506. ls->t = FLT_MAX;
  507. ls->eval_fac = 1.0f;
  508. }
  509. #endif
  510. else {
  511. ls->P = make_float3(klight->co[0], klight->co[1], klight->co[2]);
  512. if (type == LIGHT_POINT || type == LIGHT_SPOT) {
  513. float radius = klight->spot.radius;
  514. if (radius > 0.0f)
  515. /* sphere light */
  516. ls->P += sphere_light_sample(P, ls->P, radius, randu, randv);
  517. ls->D = normalize_len(ls->P - P, &ls->t);
  518. ls->Ng = -ls->D;
  519. float invarea = klight->spot.invarea;
  520. ls->eval_fac = (0.25f * M_1_PI_F) * invarea;
  521. ls->pdf = invarea;
  522. if (type == LIGHT_SPOT) {
  523. /* spot light attenuation */
  524. float3 dir = make_float3(klight->spot.dir[0], klight->spot.dir[1], klight->spot.dir[2]);
  525. ls->eval_fac *= spot_light_attenuation(
  526. dir, klight->spot.spot_angle, klight->spot.spot_smooth, ls);
  527. if (ls->eval_fac == 0.0f) {
  528. return false;
  529. }
  530. }
  531. float2 uv = map_to_sphere(ls->Ng);
  532. ls->u = uv.x;
  533. ls->v = uv.y;
  534. ls->pdf *= lamp_light_pdf(kg, ls->Ng, -ls->D, ls->t);
  535. }
  536. else {
  537. /* area light */
  538. float3 axisu = make_float3(
  539. klight->area.axisu[0], klight->area.axisu[1], klight->area.axisu[2]);
  540. float3 axisv = make_float3(
  541. klight->area.axisv[0], klight->area.axisv[1], klight->area.axisv[2]);
  542. float3 D = make_float3(klight->area.dir[0], klight->area.dir[1], klight->area.dir[2]);
  543. float invarea = fabsf(klight->area.invarea);
  544. bool is_round = (klight->area.invarea < 0.0f);
  545. if (dot(ls->P - P, D) > 0.0f) {
  546. return false;
  547. }
  548. float3 inplane;
  549. if (is_round) {
  550. inplane = ellipse_sample(axisu * 0.5f, axisv * 0.5f, randu, randv);
  551. ls->P += inplane;
  552. ls->pdf = invarea;
  553. }
  554. else {
  555. inplane = ls->P;
  556. ls->pdf = rect_light_sample(P, &ls->P, axisu, axisv, randu, randv, true);
  557. inplane = ls->P - inplane;
  558. }
  559. ls->u = dot(inplane, axisu) * (1.0f / dot(axisu, axisu)) + 0.5f;
  560. ls->v = dot(inplane, axisv) * (1.0f / dot(axisv, axisv)) + 0.5f;
  561. ls->Ng = D;
  562. ls->D = normalize_len(ls->P - P, &ls->t);
  563. ls->eval_fac = 0.25f * invarea;
  564. if (is_round) {
  565. ls->pdf *= lamp_light_pdf(kg, D, -ls->D, ls->t);
  566. }
  567. }
  568. }
  569. ls->pdf *= kernel_data.integrator.pdf_lights;
  570. return (ls->pdf > 0.0f);
  571. }
  572. ccl_device bool lamp_light_eval(
  573. KernelGlobals *kg, int lamp, float3 P, float3 D, float t, LightSample *ls)
  574. {
  575. const ccl_global KernelLight *klight = &kernel_tex_fetch(__lights, lamp);
  576. LightType type = (LightType)klight->type;
  577. ls->type = type;
  578. ls->shader = klight->shader_id;
  579. ls->object = PRIM_NONE;
  580. ls->prim = PRIM_NONE;
  581. ls->lamp = lamp;
  582. /* todo: missing texture coordinates */
  583. ls->u = 0.0f;
  584. ls->v = 0.0f;
  585. if (!(ls->shader & SHADER_USE_MIS))
  586. return false;
  587. if (type == LIGHT_DISTANT) {
  588. /* distant light */
  589. float radius = klight->distant.radius;
  590. if (radius == 0.0f)
  591. return false;
  592. if (t != FLT_MAX)
  593. return false;
  594. /* a distant light is infinitely far away, but equivalent to a disk
  595. * shaped light exactly 1 unit away from the current shading point.
  596. *
  597. * radius t^2/cos(theta)
  598. * <----------> t = sqrt(1^2 + tan(theta)^2)
  599. * tan(th) area = radius*radius*pi
  600. * <----->
  601. * \ | (1 + tan(theta)^2)/cos(theta)
  602. * \ | (1 + tan(acos(cos(theta)))^2)/cos(theta)
  603. * t \th| 1 simplifies to
  604. * \-| 1/(cos(theta)^3)
  605. * \| magic!
  606. * P
  607. */
  608. float3 lightD = make_float3(klight->co[0], klight->co[1], klight->co[2]);
  609. float costheta = dot(-lightD, D);
  610. float cosangle = klight->distant.cosangle;
  611. if (costheta < cosangle)
  612. return false;
  613. ls->P = -D;
  614. ls->Ng = -D;
  615. ls->D = D;
  616. ls->t = FLT_MAX;
  617. /* compute pdf */
  618. float invarea = klight->distant.invarea;
  619. ls->pdf = invarea / (costheta * costheta * costheta);
  620. ls->eval_fac = ls->pdf;
  621. }
  622. else if (type == LIGHT_POINT || type == LIGHT_SPOT) {
  623. float3 lightP = make_float3(klight->co[0], klight->co[1], klight->co[2]);
  624. float radius = klight->spot.radius;
  625. /* sphere light */
  626. if (radius == 0.0f)
  627. return false;
  628. if (!ray_aligned_disk_intersect(P, D, t, lightP, radius, &ls->P, &ls->t)) {
  629. return false;
  630. }
  631. ls->Ng = -D;
  632. ls->D = D;
  633. float invarea = klight->spot.invarea;
  634. ls->eval_fac = (0.25f * M_1_PI_F) * invarea;
  635. ls->pdf = invarea;
  636. if (type == LIGHT_SPOT) {
  637. /* spot light attenuation */
  638. float3 dir = make_float3(klight->spot.dir[0], klight->spot.dir[1], klight->spot.dir[2]);
  639. ls->eval_fac *= spot_light_attenuation(
  640. dir, klight->spot.spot_angle, klight->spot.spot_smooth, ls);
  641. if (ls->eval_fac == 0.0f)
  642. return false;
  643. }
  644. float2 uv = map_to_sphere(ls->Ng);
  645. ls->u = uv.x;
  646. ls->v = uv.y;
  647. /* compute pdf */
  648. if (ls->t != FLT_MAX)
  649. ls->pdf *= lamp_light_pdf(kg, ls->Ng, -ls->D, ls->t);
  650. }
  651. else if (type == LIGHT_AREA) {
  652. /* area light */
  653. float invarea = fabsf(klight->area.invarea);
  654. bool is_round = (klight->area.invarea < 0.0f);
  655. if (invarea == 0.0f)
  656. return false;
  657. float3 axisu = make_float3(
  658. klight->area.axisu[0], klight->area.axisu[1], klight->area.axisu[2]);
  659. float3 axisv = make_float3(
  660. klight->area.axisv[0], klight->area.axisv[1], klight->area.axisv[2]);
  661. float3 Ng = make_float3(klight->area.dir[0], klight->area.dir[1], klight->area.dir[2]);
  662. /* one sided */
  663. if (dot(D, Ng) >= 0.0f)
  664. return false;
  665. float3 light_P = make_float3(klight->co[0], klight->co[1], klight->co[2]);
  666. if (!ray_quad_intersect(
  667. P, D, 0.0f, t, light_P, axisu, axisv, Ng, &ls->P, &ls->t, &ls->u, &ls->v, is_round)) {
  668. return false;
  669. }
  670. ls->D = D;
  671. ls->Ng = Ng;
  672. if (is_round) {
  673. ls->pdf = invarea * lamp_light_pdf(kg, Ng, -D, ls->t);
  674. }
  675. else {
  676. ls->pdf = rect_light_sample(P, &light_P, axisu, axisv, 0, 0, false);
  677. }
  678. ls->eval_fac = 0.25f * invarea;
  679. }
  680. else {
  681. return false;
  682. }
  683. ls->pdf *= kernel_data.integrator.pdf_lights;
  684. return true;
  685. }
  686. /* Triangle Light */
  687. /* returns true if the triangle is has motion blur or an instancing transform applied */
  688. ccl_device_inline bool triangle_world_space_vertices(
  689. KernelGlobals *kg, int object, int prim, float time, float3 V[3])
  690. {
  691. bool has_motion = false;
  692. const int object_flag = kernel_tex_fetch(__object_flag, object);
  693. if (object_flag & SD_OBJECT_HAS_VERTEX_MOTION && time >= 0.0f) {
  694. motion_triangle_vertices(kg, object, prim, time, V);
  695. has_motion = true;
  696. }
  697. else {
  698. triangle_vertices(kg, prim, V);
  699. }
  700. #ifdef __INSTANCING__
  701. if (!(object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
  702. # ifdef __OBJECT_MOTION__
  703. float object_time = (time >= 0.0f) ? time : 0.5f;
  704. Transform tfm = object_fetch_transform_motion_test(kg, object, object_time, NULL);
  705. # else
  706. Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
  707. # endif
  708. V[0] = transform_point(&tfm, V[0]);
  709. V[1] = transform_point(&tfm, V[1]);
  710. V[2] = transform_point(&tfm, V[2]);
  711. has_motion = true;
  712. }
  713. #endif
  714. return has_motion;
  715. }
  716. ccl_device_inline float triangle_light_pdf_area(KernelGlobals *kg,
  717. const float3 Ng,
  718. const float3 I,
  719. float t)
  720. {
  721. float pdf = kernel_data.integrator.pdf_triangles;
  722. float cos_pi = fabsf(dot(Ng, I));
  723. if (cos_pi == 0.0f)
  724. return 0.0f;
  725. return t * t * pdf / cos_pi;
  726. }
  727. ccl_device_forceinline float triangle_light_pdf(KernelGlobals *kg, ShaderData *sd, float t)
  728. {
  729. /* A naive heuristic to decide between costly solid angle sampling
  730. * and simple area sampling, comparing the distance to the triangle plane
  731. * to the length of the edges of the triangle. */
  732. float3 V[3];
  733. bool has_motion = triangle_world_space_vertices(kg, sd->object, sd->prim, sd->time, V);
  734. const float3 e0 = V[1] - V[0];
  735. const float3 e1 = V[2] - V[0];
  736. const float3 e2 = V[2] - V[1];
  737. const float longest_edge_squared = max(len_squared(e0), max(len_squared(e1), len_squared(e2)));
  738. const float3 N = cross(e0, e1);
  739. const float distance_to_plane = fabsf(dot(N, sd->I * t)) / dot(N, N);
  740. if (longest_edge_squared > distance_to_plane * distance_to_plane) {
  741. /* sd contains the point on the light source
  742. * calculate Px, the point that we're shading */
  743. const float3 Px = sd->P + sd->I * t;
  744. const float3 v0_p = V[0] - Px;
  745. const float3 v1_p = V[1] - Px;
  746. const float3 v2_p = V[2] - Px;
  747. const float3 u01 = safe_normalize(cross(v0_p, v1_p));
  748. const float3 u02 = safe_normalize(cross(v0_p, v2_p));
  749. const float3 u12 = safe_normalize(cross(v1_p, v2_p));
  750. const float alpha = fast_acosf(dot(u02, u01));
  751. const float beta = fast_acosf(-dot(u01, u12));
  752. const float gamma = fast_acosf(dot(u02, u12));
  753. const float solid_angle = alpha + beta + gamma - M_PI_F;
  754. /* pdf_triangles is calculated over triangle area, but we're not sampling over its area */
  755. if (UNLIKELY(solid_angle == 0.0f)) {
  756. return 0.0f;
  757. }
  758. else {
  759. float area = 1.0f;
  760. if (has_motion) {
  761. /* get the center frame vertices, this is what the PDF was calculated from */
  762. triangle_world_space_vertices(kg, sd->object, sd->prim, -1.0f, V);
  763. area = triangle_area(V[0], V[1], V[2]);
  764. }
  765. else {
  766. area = 0.5f * len(N);
  767. }
  768. const float pdf = area * kernel_data.integrator.pdf_triangles;
  769. return pdf / solid_angle;
  770. }
  771. }
  772. else {
  773. float pdf = triangle_light_pdf_area(kg, sd->Ng, sd->I, t);
  774. if (has_motion) {
  775. const float area = 0.5f * len(N);
  776. if (UNLIKELY(area == 0.0f)) {
  777. return 0.0f;
  778. }
  779. /* scale the PDF.
  780. * area = the area the sample was taken from
  781. * area_pre = the are from which pdf_triangles was calculated from */
  782. triangle_world_space_vertices(kg, sd->object, sd->prim, -1.0f, V);
  783. const float area_pre = triangle_area(V[0], V[1], V[2]);
  784. pdf = pdf * area_pre / area;
  785. }
  786. return pdf;
  787. }
  788. }
  789. ccl_device_forceinline void triangle_light_sample(KernelGlobals *kg,
  790. int prim,
  791. int object,
  792. float randu,
  793. float randv,
  794. float time,
  795. LightSample *ls,
  796. const float3 P)
  797. {
  798. /* A naive heuristic to decide between costly solid angle sampling
  799. * and simple area sampling, comparing the distance to the triangle plane
  800. * to the length of the edges of the triangle. */
  801. float3 V[3];
  802. bool has_motion = triangle_world_space_vertices(kg, object, prim, time, V);
  803. const float3 e0 = V[1] - V[0];
  804. const float3 e1 = V[2] - V[0];
  805. const float3 e2 = V[2] - V[1];
  806. const float longest_edge_squared = max(len_squared(e0), max(len_squared(e1), len_squared(e2)));
  807. const float3 N0 = cross(e0, e1);
  808. float Nl = 0.0f;
  809. ls->Ng = safe_normalize_len(N0, &Nl);
  810. float area = 0.5f * Nl;
  811. /* flip normal if necessary */
  812. const int object_flag = kernel_tex_fetch(__object_flag, object);
  813. if (object_flag & SD_OBJECT_NEGATIVE_SCALE_APPLIED) {
  814. ls->Ng = -ls->Ng;
  815. }
  816. ls->eval_fac = 1.0f;
  817. ls->shader = kernel_tex_fetch(__tri_shader, prim);
  818. ls->object = object;
  819. ls->prim = prim;
  820. ls->lamp = LAMP_NONE;
  821. ls->shader |= SHADER_USE_MIS;
  822. ls->type = LIGHT_TRIANGLE;
  823. float distance_to_plane = fabsf(dot(N0, V[0] - P) / dot(N0, N0));
  824. if (longest_edge_squared > distance_to_plane * distance_to_plane) {
  825. /* see James Arvo, "Stratified Sampling of Spherical Triangles"
  826. * http://www.graphics.cornell.edu/pubs/1995/Arv95c.pdf */
  827. /* project the triangle to the unit sphere
  828. * and calculate its edges and angles */
  829. const float3 v0_p = V[0] - P;
  830. const float3 v1_p = V[1] - P;
  831. const float3 v2_p = V[2] - P;
  832. const float3 u01 = safe_normalize(cross(v0_p, v1_p));
  833. const float3 u02 = safe_normalize(cross(v0_p, v2_p));
  834. const float3 u12 = safe_normalize(cross(v1_p, v2_p));
  835. const float3 A = safe_normalize(v0_p);
  836. const float3 B = safe_normalize(v1_p);
  837. const float3 C = safe_normalize(v2_p);
  838. const float cos_alpha = dot(u02, u01);
  839. const float cos_beta = -dot(u01, u12);
  840. const float cos_gamma = dot(u02, u12);
  841. /* calculate dihedral angles */
  842. const float alpha = fast_acosf(cos_alpha);
  843. const float beta = fast_acosf(cos_beta);
  844. const float gamma = fast_acosf(cos_gamma);
  845. /* the area of the unit spherical triangle = solid angle */
  846. const float solid_angle = alpha + beta + gamma - M_PI_F;
  847. /* precompute a few things
  848. * these could be re-used to take several samples
  849. * as they are independent of randu/randv */
  850. const float cos_c = dot(A, B);
  851. const float sin_alpha = fast_sinf(alpha);
  852. const float product = sin_alpha * cos_c;
  853. /* Select a random sub-area of the spherical triangle
  854. * and calculate the third vertex C_ of that new triangle */
  855. const float phi = randu * solid_angle - alpha;
  856. float s, t;
  857. fast_sincosf(phi, &s, &t);
  858. const float u = t - cos_alpha;
  859. const float v = s + product;
  860. const float3 U = safe_normalize(C - dot(C, A) * A);
  861. float q = 1.0f;
  862. const float det = ((v * s + u * t) * sin_alpha);
  863. if (det != 0.0f) {
  864. q = ((v * t - u * s) * cos_alpha - v) / det;
  865. }
  866. const float temp = max(1.0f - q * q, 0.0f);
  867. const float3 C_ = safe_normalize(q * A + sqrtf(temp) * U);
  868. /* Finally, select a random point along the edge of the new triangle
  869. * That point on the spherical triangle is the sampled ray direction */
  870. const float z = 1.0f - randv * (1.0f - dot(C_, B));
  871. ls->D = z * B + safe_sqrtf(1.0f - z * z) * safe_normalize(C_ - dot(C_, B) * B);
  872. /* calculate intersection with the planar triangle */
  873. if (!ray_triangle_intersect(P,
  874. ls->D,
  875. FLT_MAX,
  876. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  877. (ssef *)V,
  878. #else
  879. V[0],
  880. V[1],
  881. V[2],
  882. #endif
  883. &ls->u,
  884. &ls->v,
  885. &ls->t)) {
  886. ls->pdf = 0.0f;
  887. return;
  888. }
  889. ls->P = P + ls->D * ls->t;
  890. /* pdf_triangles is calculated over triangle area, but we're sampling over solid angle */
  891. if (UNLIKELY(solid_angle == 0.0f)) {
  892. ls->pdf = 0.0f;
  893. return;
  894. }
  895. else {
  896. if (has_motion) {
  897. /* get the center frame vertices, this is what the PDF was calculated from */
  898. triangle_world_space_vertices(kg, object, prim, -1.0f, V);
  899. area = triangle_area(V[0], V[1], V[2]);
  900. }
  901. const float pdf = area * kernel_data.integrator.pdf_triangles;
  902. ls->pdf = pdf / solid_angle;
  903. }
  904. }
  905. else {
  906. /* compute random point in triangle */
  907. randu = sqrtf(randu);
  908. const float u = 1.0f - randu;
  909. const float v = randv * randu;
  910. const float t = 1.0f - u - v;
  911. ls->P = u * V[0] + v * V[1] + t * V[2];
  912. /* compute incoming direction, distance and pdf */
  913. ls->D = normalize_len(ls->P - P, &ls->t);
  914. ls->pdf = triangle_light_pdf_area(kg, ls->Ng, -ls->D, ls->t);
  915. if (has_motion && area != 0.0f) {
  916. /* scale the PDF.
  917. * area = the area the sample was taken from
  918. * area_pre = the are from which pdf_triangles was calculated from */
  919. triangle_world_space_vertices(kg, object, prim, -1.0f, V);
  920. const float area_pre = triangle_area(V[0], V[1], V[2]);
  921. ls->pdf = ls->pdf * area_pre / area;
  922. }
  923. ls->u = u;
  924. ls->v = v;
  925. }
  926. }
  927. /* Light Distribution */
  928. ccl_device int light_distribution_sample(KernelGlobals *kg, float *randu)
  929. {
  930. /* This is basically std::upper_bound as used by pbrt, to find a point light or
  931. * triangle to emit from, proportional to area. a good improvement would be to
  932. * also sample proportional to power, though it's not so well defined with
  933. * arbitrary shaders. */
  934. int first = 0;
  935. int len = kernel_data.integrator.num_distribution + 1;
  936. float r = *randu;
  937. while (len > 0) {
  938. int half_len = len >> 1;
  939. int middle = first + half_len;
  940. if (r < kernel_tex_fetch(__light_distribution, middle).totarea) {
  941. len = half_len;
  942. }
  943. else {
  944. first = middle + 1;
  945. len = len - half_len - 1;
  946. }
  947. }
  948. /* Clamping should not be needed but float rounding errors seem to
  949. * make this fail on rare occasions. */
  950. int index = clamp(first - 1, 0, kernel_data.integrator.num_distribution - 1);
  951. /* Rescale to reuse random number. this helps the 2D samples within
  952. * each area light be stratified as well. */
  953. float distr_min = kernel_tex_fetch(__light_distribution, index).totarea;
  954. float distr_max = kernel_tex_fetch(__light_distribution, index + 1).totarea;
  955. *randu = (r - distr_min) / (distr_max - distr_min);
  956. return index;
  957. }
  958. /* Generic Light */
  959. ccl_device bool light_select_reached_max_bounces(KernelGlobals *kg, int index, int bounce)
  960. {
  961. return (bounce > kernel_tex_fetch(__lights, index).max_bounces);
  962. }
  963. ccl_device_noinline bool light_sample(
  964. KernelGlobals *kg, float randu, float randv, float time, float3 P, int bounce, LightSample *ls)
  965. {
  966. /* sample index */
  967. int index = light_distribution_sample(kg, &randu);
  968. /* fetch light data */
  969. const ccl_global KernelLightDistribution *kdistribution = &kernel_tex_fetch(__light_distribution,
  970. index);
  971. int prim = kdistribution->prim;
  972. if (prim >= 0) {
  973. int object = kdistribution->mesh_light.object_id;
  974. int shader_flag = kdistribution->mesh_light.shader_flag;
  975. triangle_light_sample(kg, prim, object, randu, randv, time, ls, P);
  976. ls->shader |= shader_flag;
  977. return (ls->pdf > 0.0f);
  978. }
  979. else {
  980. int lamp = -prim - 1;
  981. if (UNLIKELY(light_select_reached_max_bounces(kg, lamp, bounce))) {
  982. return false;
  983. }
  984. return lamp_light_sample(kg, lamp, randu, randv, P, ls);
  985. }
  986. }
  987. ccl_device int light_select_num_samples(KernelGlobals *kg, int index)
  988. {
  989. return kernel_tex_fetch(__lights, index).samples;
  990. }
  991. CCL_NAMESPACE_END