kernel_emission.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /* Direction Emission */
  18. ccl_device_noinline float3 direct_emissive_eval(KernelGlobals *kg,
  19. ShaderData *emission_sd,
  20. LightSample *ls,
  21. ccl_addr_space PathState *state,
  22. float3 I,
  23. differential3 dI,
  24. float t,
  25. float time)
  26. {
  27. /* setup shading at emitter */
  28. float3 eval;
  29. if (shader_constant_emission_eval(kg, ls->shader, &eval)) {
  30. if ((ls->prim != PRIM_NONE) && dot(ls->Ng, I) < 0.0f) {
  31. ls->Ng = -ls->Ng;
  32. }
  33. }
  34. else {
  35. /* Setup shader data and call shader_eval_surface once, better
  36. * for GPU coherence and compile times. */
  37. #ifdef __BACKGROUND_MIS__
  38. if (ls->type == LIGHT_BACKGROUND) {
  39. Ray ray;
  40. ray.D = ls->D;
  41. ray.P = ls->P;
  42. ray.t = 1.0f;
  43. ray.time = time;
  44. ray.dP = differential3_zero();
  45. ray.dD = dI;
  46. shader_setup_from_background(kg, emission_sd, &ray);
  47. }
  48. else
  49. #endif
  50. {
  51. shader_setup_from_sample(kg,
  52. emission_sd,
  53. ls->P,
  54. ls->Ng,
  55. I,
  56. ls->shader,
  57. ls->object,
  58. ls->prim,
  59. ls->u,
  60. ls->v,
  61. t,
  62. time,
  63. false,
  64. ls->lamp);
  65. ls->Ng = emission_sd->Ng;
  66. }
  67. /* No proper path flag, we're evaluating this for all closures. that's
  68. * weak but we'd have to do multiple evaluations otherwise. */
  69. path_state_modify_bounce(state, true);
  70. shader_eval_surface(kg, emission_sd, state, PATH_RAY_EMISSION);
  71. path_state_modify_bounce(state, false);
  72. /* Evaluate closures. */
  73. #ifdef __BACKGROUND_MIS__
  74. if (ls->type == LIGHT_BACKGROUND) {
  75. eval = shader_background_eval(emission_sd);
  76. }
  77. else
  78. #endif
  79. {
  80. eval = shader_emissive_eval(emission_sd);
  81. }
  82. }
  83. eval *= ls->eval_fac;
  84. if (ls->lamp != LAMP_NONE) {
  85. const ccl_global KernelLight *klight = &kernel_tex_fetch(__lights, ls->lamp);
  86. eval *= make_float3(klight->strength[0], klight->strength[1], klight->strength[2]);
  87. }
  88. return eval;
  89. }
  90. ccl_device_noinline bool direct_emission(KernelGlobals *kg,
  91. ShaderData *sd,
  92. ShaderData *emission_sd,
  93. LightSample *ls,
  94. ccl_addr_space PathState *state,
  95. Ray *ray,
  96. BsdfEval *eval,
  97. bool *is_lamp,
  98. float rand_terminate)
  99. {
  100. if (ls->pdf == 0.0f)
  101. return false;
  102. /* todo: implement */
  103. differential3 dD = differential3_zero();
  104. /* evaluate closure */
  105. float3 light_eval = direct_emissive_eval(
  106. kg, emission_sd, ls, state, -ls->D, dD, ls->t, sd->time);
  107. if (is_zero(light_eval))
  108. return false;
  109. /* evaluate BSDF at shading point */
  110. #ifdef __VOLUME__
  111. if (sd->prim != PRIM_NONE)
  112. shader_bsdf_eval(kg, sd, ls->D, eval, ls->pdf, ls->shader & SHADER_USE_MIS);
  113. else {
  114. float bsdf_pdf;
  115. shader_volume_phase_eval(kg, sd, ls->D, eval, &bsdf_pdf);
  116. if (ls->shader & SHADER_USE_MIS) {
  117. /* Multiple importance sampling. */
  118. float mis_weight = power_heuristic(ls->pdf, bsdf_pdf);
  119. light_eval *= mis_weight;
  120. }
  121. }
  122. #else
  123. shader_bsdf_eval(kg, sd, ls->D, eval, ls->pdf, ls->shader & SHADER_USE_MIS);
  124. #endif
  125. bsdf_eval_mul3(eval, light_eval / ls->pdf);
  126. #ifdef __PASSES__
  127. /* use visibility flag to skip lights */
  128. if (ls->shader & SHADER_EXCLUDE_ANY) {
  129. if (ls->shader & SHADER_EXCLUDE_DIFFUSE) {
  130. eval->diffuse = make_float3(0.0f, 0.0f, 0.0f);
  131. eval->subsurface = make_float3(0.0f, 0.0f, 0.0f);
  132. }
  133. if (ls->shader & SHADER_EXCLUDE_GLOSSY)
  134. eval->glossy = make_float3(0.0f, 0.0f, 0.0f);
  135. if (ls->shader & SHADER_EXCLUDE_TRANSMIT)
  136. eval->transmission = make_float3(0.0f, 0.0f, 0.0f);
  137. if (ls->shader & SHADER_EXCLUDE_SCATTER)
  138. eval->scatter = make_float3(0.0f, 0.0f, 0.0f);
  139. }
  140. #endif
  141. if (bsdf_eval_is_zero(eval))
  142. return false;
  143. if (kernel_data.integrator.light_inv_rr_threshold > 0.0f
  144. #ifdef __SHADOW_TRICKS__
  145. && (state->flag & PATH_RAY_SHADOW_CATCHER) == 0
  146. #endif
  147. ) {
  148. float probability = max3(fabs(bsdf_eval_sum(eval))) *
  149. kernel_data.integrator.light_inv_rr_threshold;
  150. if (probability < 1.0f) {
  151. if (rand_terminate >= probability) {
  152. return false;
  153. }
  154. bsdf_eval_mul(eval, 1.0f / probability);
  155. }
  156. }
  157. if (ls->shader & SHADER_CAST_SHADOW) {
  158. /* setup ray */
  159. bool transmit = (dot(sd->Ng, ls->D) < 0.0f);
  160. ray->P = ray_offset(sd->P, (transmit) ? -sd->Ng : sd->Ng);
  161. if (ls->t == FLT_MAX) {
  162. /* distant light */
  163. ray->D = ls->D;
  164. ray->t = ls->t;
  165. }
  166. else {
  167. /* other lights, avoid self-intersection */
  168. ray->D = ray_offset(ls->P, ls->Ng) - ray->P;
  169. ray->D = normalize_len(ray->D, &ray->t);
  170. }
  171. ray->dP = sd->dP;
  172. ray->dD = differential3_zero();
  173. }
  174. else {
  175. /* signal to not cast shadow ray */
  176. ray->t = 0.0f;
  177. }
  178. /* return if it's a lamp for shadow pass */
  179. *is_lamp = (ls->prim == PRIM_NONE && ls->type != LIGHT_BACKGROUND);
  180. return true;
  181. }
  182. /* Indirect Primitive Emission */
  183. ccl_device_noinline float3 indirect_primitive_emission(
  184. KernelGlobals *kg, ShaderData *sd, float t, int path_flag, float bsdf_pdf)
  185. {
  186. /* evaluate emissive closure */
  187. float3 L = shader_emissive_eval(sd);
  188. #ifdef __HAIR__
  189. if (!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_USE_MIS) &&
  190. (sd->type & PRIMITIVE_ALL_TRIANGLE))
  191. #else
  192. if (!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_USE_MIS))
  193. #endif
  194. {
  195. /* multiple importance sampling, get triangle light pdf,
  196. * and compute weight with respect to BSDF pdf */
  197. float pdf = triangle_light_pdf(kg, sd, t);
  198. float mis_weight = power_heuristic(bsdf_pdf, pdf);
  199. return L * mis_weight;
  200. }
  201. return L;
  202. }
  203. /* Indirect Lamp Emission */
  204. ccl_device_noinline bool indirect_lamp_emission(KernelGlobals *kg,
  205. ShaderData *emission_sd,
  206. ccl_addr_space PathState *state,
  207. Ray *ray,
  208. float3 *emission)
  209. {
  210. bool hit_lamp = false;
  211. *emission = make_float3(0.0f, 0.0f, 0.0f);
  212. for (int lamp = 0; lamp < kernel_data.integrator.num_all_lights; lamp++) {
  213. LightSample ls;
  214. if (!lamp_light_eval(kg, lamp, ray->P, ray->D, ray->t, &ls))
  215. continue;
  216. #ifdef __PASSES__
  217. /* use visibility flag to skip lights */
  218. if (ls.shader & SHADER_EXCLUDE_ANY) {
  219. if (((ls.shader & SHADER_EXCLUDE_DIFFUSE) && (state->flag & PATH_RAY_DIFFUSE)) ||
  220. ((ls.shader & SHADER_EXCLUDE_GLOSSY) &&
  221. ((state->flag & (PATH_RAY_GLOSSY | PATH_RAY_REFLECT)) ==
  222. (PATH_RAY_GLOSSY | PATH_RAY_REFLECT))) ||
  223. ((ls.shader & SHADER_EXCLUDE_TRANSMIT) && (state->flag & PATH_RAY_TRANSMIT)) ||
  224. ((ls.shader & SHADER_EXCLUDE_SCATTER) && (state->flag & PATH_RAY_VOLUME_SCATTER)))
  225. continue;
  226. }
  227. #endif
  228. float3 L = direct_emissive_eval(
  229. kg, emission_sd, &ls, state, -ray->D, ray->dD, ls.t, ray->time);
  230. #ifdef __VOLUME__
  231. if (state->volume_stack[0].shader != SHADER_NONE) {
  232. /* shadow attenuation */
  233. Ray volume_ray = *ray;
  234. volume_ray.t = ls.t;
  235. float3 volume_tp = make_float3(1.0f, 1.0f, 1.0f);
  236. kernel_volume_shadow(kg, emission_sd, state, &volume_ray, &volume_tp);
  237. L *= volume_tp;
  238. }
  239. #endif
  240. if (!(state->flag & PATH_RAY_MIS_SKIP)) {
  241. /* multiple importance sampling, get regular light pdf,
  242. * and compute weight with respect to BSDF pdf */
  243. float mis_weight = power_heuristic(state->ray_pdf, ls.pdf);
  244. L *= mis_weight;
  245. }
  246. *emission += L;
  247. hit_lamp = true;
  248. }
  249. return hit_lamp;
  250. }
  251. /* Indirect Background */
  252. ccl_device_noinline float3 indirect_background(KernelGlobals *kg,
  253. ShaderData *emission_sd,
  254. ccl_addr_space PathState *state,
  255. ccl_addr_space Ray *ray)
  256. {
  257. #ifdef __BACKGROUND__
  258. int shader = kernel_data.background.surface_shader;
  259. /* Use visibility flag to skip lights. */
  260. if (shader & SHADER_EXCLUDE_ANY) {
  261. if (((shader & SHADER_EXCLUDE_DIFFUSE) && (state->flag & PATH_RAY_DIFFUSE)) ||
  262. ((shader & SHADER_EXCLUDE_GLOSSY) &&
  263. ((state->flag & (PATH_RAY_GLOSSY | PATH_RAY_REFLECT)) ==
  264. (PATH_RAY_GLOSSY | PATH_RAY_REFLECT))) ||
  265. ((shader & SHADER_EXCLUDE_TRANSMIT) && (state->flag & PATH_RAY_TRANSMIT)) ||
  266. ((shader & SHADER_EXCLUDE_CAMERA) && (state->flag & PATH_RAY_CAMERA)) ||
  267. ((shader & SHADER_EXCLUDE_SCATTER) && (state->flag & PATH_RAY_VOLUME_SCATTER)))
  268. return make_float3(0.0f, 0.0f, 0.0f);
  269. }
  270. /* Evaluate background shader. */
  271. float3 L;
  272. if (!shader_constant_emission_eval(kg, shader, &L)) {
  273. # ifdef __SPLIT_KERNEL__
  274. Ray priv_ray = *ray;
  275. shader_setup_from_background(kg, emission_sd, &priv_ray);
  276. # else
  277. shader_setup_from_background(kg, emission_sd, ray);
  278. # endif
  279. path_state_modify_bounce(state, true);
  280. shader_eval_surface(kg, emission_sd, state, state->flag | PATH_RAY_EMISSION);
  281. path_state_modify_bounce(state, false);
  282. L = shader_background_eval(emission_sd);
  283. }
  284. /* Background MIS weights. */
  285. # ifdef __BACKGROUND_MIS__
  286. /* Check if background light exists or if we should skip pdf. */
  287. int res_x = kernel_data.integrator.pdf_background_res_x;
  288. if (!(state->flag & PATH_RAY_MIS_SKIP) && res_x) {
  289. /* multiple importance sampling, get background light pdf for ray
  290. * direction, and compute weight with respect to BSDF pdf */
  291. float pdf = background_light_pdf(kg, ray->P, ray->D);
  292. float mis_weight = power_heuristic(state->ray_pdf, pdf);
  293. return L * mis_weight;
  294. }
  295. # endif
  296. return L;
  297. #else
  298. return make_float3(0.8f, 0.8f, 0.8f);
  299. #endif
  300. }
  301. CCL_NAMESPACE_END