38 const int cdf_width = res_x + 1;
45 const int step = count >> 1;
46 const int middle = first +
step;
57 const int index_v =
max(0, first - 1);
66 const float v = (index_v + dv) / res_y;
72 const int step = count >> 1;
73 const int middle = first +
step;
75 if (
kernel_data_fetch(light_background_conditional_cdf, index_v * cdf_width + middle).
y <
86 const int index_u =
max(0, first - 1);
90 index_v * cdf_width + index_u);
92 index_v * cdf_width + index_u + 1);
94 index_v * cdf_width + res_x);
98 const float u = (index_u + du) / res_x;
104 if (
sin_theta == 0.0f || denom == 0.0f) {
108 *pdf = (cdf_u.
x * cdf_v.
x) / denom;
121 const int res_x =
kernel_data.background.map_res_x;
122 const int res_y =
kernel_data.background.map_res_y;
123 const int cdf_width = res_x + 1;
136 index_v * cdf_width + res_x);
147 index_v * cdf_width + index_u);
150 return (cdf_u.
x * cdf_v.
x) / denom;
162 *lightpos = klight->co;
163 *dir = klight->area.dir;
166 if (
dot(*dir,
P - *lightpos) > 1e-4f) {
176 const int ignore_portal,
179 float portal_pdf = 0.0f;
181 int num_possible = 0;
182 for (
int p = 0; p <
kernel_data.integrator.num_portals; p++) {
183 if (p == ignore_portal) {
199 const int portal =
kernel_data.integrator.portal_offset + p;
202 const float3 axis_u = klight->area.axis_u;
203 const float len_u = klight->area.len_u;
204 const float3 axis_v = klight->area.axis_v;
205 const float len_v = klight->area.len_v;
206 const float3 inv_extent_u = axis_u / len_u;
207 const float3 inv_extent_v = axis_v / len_v;
209 const bool is_round = (klight->area.invarea < 0.0f);
235 P, &lightpos, axis_u, len_u, axis_v, len_v,
zero_float2(),
false);
239 if (ignore_portal >= 0) {
244 return (num_possible > 0) ? portal_pdf / num_possible : 0.0f;
249 int num_possible_portals = 0;
250 for (
int p = 0; p <
kernel_data.integrator.num_portals; p++) {
254 num_possible_portals++;
257 return num_possible_portals;
263 const int num_possible,
268 rand.
y *= num_possible;
269 int portal = (int)rand.
y;
275 for (
int p = 0; p <
kernel_data.integrator.num_portals; p++) {
285 const int portal =
kernel_data.integrator.portal_offset + p;
287 const float3 axis_u = klight->area.axis_u;
288 const float3 axis_v = klight->area.axis_v;
289 const float len_u = klight->area.len_u;
290 const float len_v = klight->area.len_v;
291 const bool is_round = (klight->area.invarea < 0.0f);
295 lightpos +=
ellipse_sample(axis_u * len_u * 0.5f, axis_v * len_v * 0.5f, rand);
305 *pdf /= num_possible;
338 float portal_method_pdf =
kernel_data.background.portal_weight;
339 float sun_method_pdf =
kernel_data.background.sun_weight;
340 float map_method_pdf =
kernel_data.background.map_weight;
343 if (portal_method_pdf > 0.0f) {
346 if (num_portals == 0) {
347 portal_method_pdf = 0.0f;
351 float pdf_fac = (portal_method_pdf + sun_method_pdf + map_method_pdf);
352 if (pdf_fac == 0.0f) {
358 pdf_fac = 1.0f / pdf_fac;
359 portal_method_pdf *= pdf_fac;
360 sun_method_pdf *= pdf_fac;
361 map_method_pdf *= pdf_fac;
367 const float sun_method_cdf = portal_method_pdf + sun_method_pdf;
371 if (rand.
x < portal_method_pdf) {
374 if (portal_method_pdf != 1.0f) {
375 rand.
x /= portal_method_pdf;
381 if (num_portals > 1) {
387 if (portal_method_pdf == 1.0f) {
390 *pdf *= portal_method_pdf;
392 else if (rand.
x < sun_method_cdf) {
395 if (sun_method_pdf != 1.0f) {
396 rand.
x = (rand.
x - portal_method_pdf) / sun_method_pdf;
402 if (sun_method_pdf == 1.0f) {
405 *pdf *= sun_method_pdf;
410 if (map_method_pdf != 1.0f) {
411 rand.
x = (rand.
x - sun_method_cdf) / map_method_pdf;
417 if (map_method_pdf == 1.0f) {
420 *pdf *= map_method_pdf;
424 if (method != 0 && portal_method_pdf != 0.0f) {
427 if (method != 1 && sun_method_pdf != 0.0f) {
430 if (method != 2 && map_method_pdf != 0.0f) {
438 float portal_method_pdf =
kernel_data.background.portal_weight;
439 float sun_method_pdf =
kernel_data.background.sun_weight;
440 float map_method_pdf =
kernel_data.background.map_weight;
442 float portal_pdf = 0.0f;
445 if (portal_method_pdf > 0.0f) {
447 bool is_possible =
false;
453 portal_method_pdf = 0.0f;
457 float pdf_fac = (portal_method_pdf + sun_method_pdf + map_method_pdf);
458 if (pdf_fac == 0.0f) {
463 pdf_fac = 1.0f / pdf_fac;
464 portal_method_pdf *= pdf_fac;
465 sun_method_pdf *= pdf_fac;
466 map_method_pdf *= pdf_fac;
468 float pdf = portal_pdf * portal_method_pdf;
469 if (sun_method_pdf != 0.0f) {
472 if (map_method_pdf != 0.0f) {
479template<
bool in_volume_segment>
487 if (in_volume_segment) {
504 point_to_centroid = -centroid;
CCL_NAMESPACE_BEGIN ccl_device_inline float area_light_rect_sample(const float3 P, ccl_private float3 *light_p, const float3 axis_u, const float len_u, const float3 axis_v, const float len_v, const float2 rand, bool sample_coord)
ccl_device_inline float sin_theta(const float3 w)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
ccl_device float2 direction_to_equirectangular(const float3 dir)
ccl_device float3 equirectangular_to_direction(const float u, const float v)
ccl_device_inline float3 ellipse_sample(const float3 ru, const float3 rv, const float2 rand)
ccl_device float light_pdf_area_to_solid_angle(const float3 Ng, const float3 I, const float t)
#define kernel_assert(cond)
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_device_inline
#define CCL_NAMESPACE_END
float3 sample_uniform_cone(float2 rand, float cos_angle)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
float distance(float, float) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
ccl_device_inline float3 background_sun_sample(KernelGlobals kg, const float2 rand, ccl_private float *pdf)
ccl_device_inline float background_sun_pdf(KernelGlobals kg, const float3 D)
ccl_device_forceinline bool background_light_tree_parameters(const float3 centroid, const float t, ccl_private float &cos_theta_u, ccl_private float2 &distance, ccl_private float3 &point_to_centroid, ccl_private float &theta_d)
ccl_device int background_num_possible_portals(KernelGlobals kg, const float3 P)
ccl_device_inline float background_portal_pdf(KernelGlobals kg, const float3 P, float3 direction, const int ignore_portal, ccl_private bool *is_possible)
ccl_device float background_light_pdf(KernelGlobals kg, const float3 P, float3 direction)
ccl_device_inline bool background_portal_data_fetch_and_check_side(KernelGlobals kg, const float3 P, const int index, ccl_private float3 *lightpos, ccl_private float3 *dir)
CCL_NAMESPACE_BEGIN ccl_device_forceinline float background_light_clamp_dD(KernelGlobals kg, const float dD)
ccl_device float background_map_pdf(KernelGlobals kg, const float3 direction)
ccl_device float3 background_map_sample(KernelGlobals kg, const float2 rand, ccl_private float *pdf)
ccl_device float3 background_portal_sample(KernelGlobals kg, const float3 P, float2 rand, const int num_possible, ccl_private int *sampled_portal, ccl_private float *pdf)
ccl_device_inline float3 background_light_sample(KernelGlobals kg, const float3 P, float2 rand, ccl_private float *pdf)
ccl_device_inline float inverse_lerp(const float a, const float b, const float x)
ccl_device_inline int float_to_int(const float f)
ccl_device_inline float one_minus_cos(const float angle)
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
ccl_device_inline float2 normalize_len(const float2 a, ccl_private float *t)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
ccl_device bool ray_quad_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 quad_P, const float3 inv_quad_u, const float3 inv_quad_v, const float3 quad_n, ccl_private float3 *isect_P, ccl_private float *isect_t, ccl_private float *isect_u, ccl_private float *isect_v, bool ellipse)
ccl_device_inline float pdf_uniform_cone(const float3 N, const float3 D, const float angle)
ccl_device float3 sample_uniform_sphere(const float2 rand)