23 #ifdef __BACKGROUND_MIS__
25 ccl_device float3 background_map_sample(KernelGlobals *
kg,
float randu,
float randv,
float *pdf)
32 int cdf_width = res_x + 1;
39 int step =
count >> 1;
40 int middle = first + step;
50 int index_v =
max(0, first - 1);
59 float v = (index_v + dv) / res_y;
65 int step =
count >> 1;
66 int middle = first + step;
68 if (
kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_width + middle).y <
77 int index_u =
max(0, first - 1);
81 index_v * cdf_width + index_u);
83 index_v * cdf_width + index_u + 1);
85 index_v * cdf_width + res_x);
89 float u = (index_u + du) / res_x;
93 float denom = (
M_2PI_F *
M_PI_F * sin_theta) * cdf_last_u.
x * cdf_last_v.
x;
95 if (sin_theta == 0.0f || denom == 0.0f)
98 *pdf = (cdf_u.
x * cdf_v.
x) / denom;
112 int cdf_width = res_x + 1;
116 if (sin_theta == 0.0f)
124 index_v * cdf_width + res_x);
127 float denom = (
M_2PI_F *
M_PI_F * sin_theta) * cdf_last_u.
x * cdf_last_v.
x;
134 index_v * cdf_width + index_u);
137 return (cdf_u.
x * cdf_v.
x) / denom;
143 int portal =
kernel_data.background.portal_offset + index;
146 *lightpos =
make_float3(klight->co[0], klight->co[1], klight->co[2]);
147 *dir =
make_float3(klight->area.dir[0], klight->area.dir[1], klight->area.dir[2]);
150 if (
dot(*dir,
P - *lightpos) > 1e-4f)
157 KernelGlobals *
kg,
float3 P,
float3 direction,
int ignore_portal,
bool *is_possible)
159 float portal_pdf = 0.0f;
161 int num_possible = 0;
162 for (
int p = 0; p <
kernel_data.background.num_portals; p++) {
163 if (p == ignore_portal)
167 if (!background_portal_data_fetch_and_check_side(
kg,
P, p, &lightpos, &dir))
176 int portal =
kernel_data.background.portal_offset + p;
179 klight->area.axisu[0], klight->area.axisu[1], klight->area.axisu[2]);
181 klight->area.axisv[0], klight->area.axisv[1], klight->area.axisv[2]);
182 bool is_round = (klight->area.invarea < 0.0f);
209 if (ignore_portal >= 0) {
214 return (num_possible > 0) ? portal_pdf / num_possible : 0.0f;
219 int num_possible_portals = 0;
220 for (
int p = 0; p <
kernel_data.background.num_portals; p++) {
222 if (background_portal_data_fetch_and_check_side(
kg,
P, p, &lightpos, &dir))
223 num_possible_portals++;
225 return num_possible_portals;
237 randv *= num_possible;
238 int portal = (int)randv;
244 for (
int p = 0; p <
kernel_data.background.num_portals; p++) {
247 if (!background_portal_data_fetch_and_check_side(
kg,
P, p, &lightpos, &dir))
252 int portal =
kernel_data.background.portal_offset + p;
255 klight->area.axisu[0], klight->area.axisu[1], klight->area.axisu[2]);
257 klight->area.axisv[0], klight->area.axisv[1], klight->area.axisv[2]);
258 bool is_round = (klight->area.invarea < 0.0f);
262 lightpos +=
ellipse_sample(axisu * 0.5f, axisv * 0.5f, randu, randv);
272 *pdf /= num_possible;
303 background_light_sample(KernelGlobals *
kg,
float3 P,
float randu,
float randv,
float *pdf)
305 float portal_method_pdf =
kernel_data.background.portal_weight;
306 float sun_method_pdf =
kernel_data.background.sun_weight;
307 float map_method_pdf =
kernel_data.background.map_weight;
310 if (portal_method_pdf > 0.0f) {
312 num_portals = background_num_possible_portals(
kg,
P);
313 if (num_portals == 0) {
314 portal_method_pdf = 0.0f;
318 float pdf_fac = (portal_method_pdf + sun_method_pdf + map_method_pdf);
319 if (pdf_fac == 0.0f) {
325 pdf_fac = 1.0f / pdf_fac;
326 portal_method_pdf *= pdf_fac;
327 sun_method_pdf *= pdf_fac;
328 map_method_pdf *= pdf_fac;
334 float sun_method_cdf = portal_method_pdf + sun_method_pdf;
338 if (randu < portal_method_pdf) {
341 if (portal_method_pdf != 1.0f) {
342 randu /= portal_method_pdf;
347 D = background_portal_sample(
kg,
P, randu, randv, num_portals, &portal, pdf);
348 if (num_portals > 1) {
350 *pdf += background_portal_pdf(
kg,
P,
D, portal,
NULL);
354 if (portal_method_pdf == 1.0f) {
357 *pdf *= portal_method_pdf;
359 else if (randu < sun_method_cdf) {
362 if (sun_method_pdf != 1.0f) {
363 randu = (randu - portal_method_pdf) / sun_method_pdf;
366 D = background_sun_sample(
kg, randu, randv, pdf);
369 if (sun_method_pdf == 1.0f) {
372 *pdf *= sun_method_pdf;
377 if (map_method_pdf != 1.0f) {
378 randu = (randu - sun_method_cdf) / map_method_pdf;
381 D = background_map_sample(
kg, randu, randv, pdf);
384 if (map_method_pdf == 1.0f) {
387 *pdf *= map_method_pdf;
391 if (method != 0 && portal_method_pdf != 0.0f) {
392 *pdf += portal_method_pdf * background_portal_pdf(
kg,
P,
D, -1,
NULL);
394 if (method != 1 && sun_method_pdf != 0.0f) {
395 *pdf += sun_method_pdf * background_sun_pdf(
kg,
D);
397 if (method != 2 && map_method_pdf != 0.0f) {
398 *pdf += map_method_pdf * background_map_pdf(
kg,
D);
405 float portal_method_pdf =
kernel_data.background.portal_weight;
406 float sun_method_pdf =
kernel_data.background.sun_weight;
407 float map_method_pdf =
kernel_data.background.map_weight;
409 float portal_pdf = 0.0f;
412 if (portal_method_pdf > 0.0f) {
414 bool is_possible =
false;
415 portal_pdf = background_portal_pdf(
kg,
P, direction, -1, &is_possible);
420 portal_method_pdf = 0.0f;
424 float pdf_fac = (portal_method_pdf + sun_method_pdf + map_method_pdf);
425 if (pdf_fac == 0.0f) {
430 pdf_fac = 1.0f / pdf_fac;
431 portal_method_pdf *= pdf_fac;
432 sun_method_pdf *= pdf_fac;
433 map_method_pdf *= pdf_fac;
435 float pdf = portal_pdf * portal_method_pdf;
436 if (sun_method_pdf != 0.0f) {
437 pdf += background_sun_pdf(
kg, direction) * sun_method_pdf;
439 if (map_method_pdf != 0.0f) {
440 pdf += background_map_pdf(
kg, direction) * map_method_pdf;
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
#define kernel_assert(cond)
#define kernel_tex_fetch(tex, index)
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define make_float3(x, y, z)
CCL_NAMESPACE_BEGIN ccl_device_inline float rect_light_sample(float3 P, float3 *light_p, float3 axisu, float3 axisv, float randu, float randv, bool sample_coord)
ccl_device_inline float3 ellipse_sample(float3 ru, float3 rv, float randu, float randv)
ccl_device float lamp_light_pdf(KernelGlobals *kg, const float3 Ng, const float3 I, float t)
ccl_device float3 sample_uniform_sphere(float u1, float u2)
ccl_device_inline void sample_uniform_cone(const float3 N, float angle, float randu, float randv, float3 *omega_in, float *pdf)
ccl_device_inline float pdf_uniform_cone(const float3 N, float3 D, float angle)
ccl_device float3 equirectangular_to_direction(float u, float v)
ccl_device float2 direction_to_equirectangular(float3 dir)
ccl_device_inline float inverse_lerp(float a, float b, float x)
ccl_device_inline int float_to_int(float f)
ccl_device_inline float3 float4_to_float3(const float4 a)
ccl_device_inline int clamp(int a, int mn, int mx)
ccl_device_inline float2 normalize(const float2 &a)
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline float2 normalize_len(const float2 &a, float *t)
ccl_device_inline float3 zero_float3()
ccl_device bool ray_quad_intersect(float3 ray_P, float3 ray_D, float ray_mint, float ray_maxt, float3 quad_P, float3 quad_u, float3 quad_v, float3 quad_n, float3 *isect_P, float *isect_t, float *isect_u, float *isect_v, bool ellipse)
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)