kernel_bake.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. #ifdef __BAKING__
  18. ccl_device_inline void compute_light_pass(
  19. KernelGlobals *kg, ShaderData *sd, PathRadiance *L, uint rng_hash, int pass_filter, int sample)
  20. {
  21. kernel_assert(kernel_data.film.use_light_pass);
  22. PathRadiance L_sample;
  23. PathState state;
  24. Ray ray;
  25. float3 throughput = make_float3(1.0f, 1.0f, 1.0f);
  26. /* emission and indirect shader data memory used by various functions */
  27. ShaderData emission_sd, indirect_sd;
  28. ray.P = sd->P + sd->Ng;
  29. ray.D = -sd->Ng;
  30. ray.t = FLT_MAX;
  31. # ifdef __CAMERA_MOTION__
  32. ray.time = 0.5f;
  33. # endif
  34. /* init radiance */
  35. path_radiance_init(&L_sample, kernel_data.film.use_light_pass);
  36. /* init path state */
  37. path_state_init(kg, &emission_sd, &state, rng_hash, sample, NULL);
  38. /* evaluate surface shader */
  39. shader_eval_surface(kg, sd, &state, state.flag);
  40. /* TODO, disable more closures we don't need besides transparent */
  41. shader_bsdf_disable_transparency(kg, sd);
  42. # ifdef __BRANCHED_PATH__
  43. if (!kernel_data.integrator.branched) {
  44. /* regular path tracer */
  45. # endif
  46. /* sample ambient occlusion */
  47. if (pass_filter & BAKE_FILTER_AO) {
  48. kernel_path_ao(
  49. kg, sd, &emission_sd, &L_sample, &state, throughput, shader_bsdf_alpha(kg, sd));
  50. }
  51. /* sample emission */
  52. if ((pass_filter & BAKE_FILTER_EMISSION) && (sd->flag & SD_EMISSION)) {
  53. float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
  54. path_radiance_accum_emission(&L_sample, &state, throughput, emission);
  55. }
  56. bool is_sss_sample = false;
  57. # ifdef __SUBSURFACE__
  58. /* sample subsurface scattering */
  59. if ((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
  60. /* When mixing BSSRDF and BSDF closures we should skip BSDF lighting
  61. * if scattering was successful. */
  62. SubsurfaceIndirectRays ss_indirect;
  63. kernel_path_subsurface_init_indirect(&ss_indirect);
  64. if (kernel_path_subsurface_scatter(
  65. kg, sd, &emission_sd, &L_sample, &state, &ray, &throughput, &ss_indirect)) {
  66. while (ss_indirect.num_rays) {
  67. kernel_path_subsurface_setup_indirect(
  68. kg, &ss_indirect, &state, &ray, &L_sample, &throughput);
  69. kernel_path_indirect(
  70. kg, &indirect_sd, &emission_sd, &ray, throughput, &state, &L_sample);
  71. }
  72. is_sss_sample = true;
  73. }
  74. }
  75. # endif
  76. /* sample light and BSDF */
  77. if (!is_sss_sample && (pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT))) {
  78. kernel_path_surface_connect_light(kg, sd, &emission_sd, throughput, &state, &L_sample);
  79. if (kernel_path_surface_bounce(kg, sd, &throughput, &state, &L_sample.state, &ray)) {
  80. # ifdef __LAMP_MIS__
  81. state.ray_t = 0.0f;
  82. # endif
  83. /* compute indirect light */
  84. kernel_path_indirect(kg, &indirect_sd, &emission_sd, &ray, throughput, &state, &L_sample);
  85. /* sum and reset indirect light pass variables for the next samples */
  86. path_radiance_sum_indirect(&L_sample);
  87. path_radiance_reset_indirect(&L_sample);
  88. }
  89. }
  90. # ifdef __BRANCHED_PATH__
  91. }
  92. else {
  93. /* branched path tracer */
  94. /* sample ambient occlusion */
  95. if (pass_filter & BAKE_FILTER_AO) {
  96. kernel_branched_path_ao(kg, sd, &emission_sd, &L_sample, &state, throughput);
  97. }
  98. /* sample emission */
  99. if ((pass_filter & BAKE_FILTER_EMISSION) && (sd->flag & SD_EMISSION)) {
  100. float3 emission = indirect_primitive_emission(kg, sd, 0.0f, state.flag, state.ray_pdf);
  101. path_radiance_accum_emission(&L_sample, &state, throughput, emission);
  102. }
  103. # ifdef __SUBSURFACE__
  104. /* sample subsurface scattering */
  105. if ((pass_filter & BAKE_FILTER_SUBSURFACE) && (sd->flag & SD_BSSRDF)) {
  106. /* When mixing BSSRDF and BSDF closures we should skip BSDF lighting
  107. * if scattering was successful. */
  108. kernel_branched_path_subsurface_scatter(
  109. kg, sd, &indirect_sd, &emission_sd, &L_sample, &state, &ray, throughput);
  110. }
  111. # endif
  112. /* sample light and BSDF */
  113. if (pass_filter & (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT)) {
  114. # if defined(__EMISSION__)
  115. /* direct light */
  116. if (kernel_data.integrator.use_direct_light) {
  117. int all = kernel_data.integrator.sample_all_lights_direct;
  118. kernel_branched_path_surface_connect_light(
  119. kg, sd, &emission_sd, &state, throughput, 1.0f, &L_sample, all);
  120. }
  121. # endif
  122. /* indirect light */
  123. kernel_branched_path_surface_indirect_light(
  124. kg, sd, &indirect_sd, &emission_sd, throughput, 1.0f, &state, &L_sample);
  125. }
  126. }
  127. # endif
  128. /* accumulate into master L */
  129. path_radiance_accum_sample(L, &L_sample);
  130. }
  131. /* this helps with AA but it's not the real solution as it does not AA the geometry
  132. * but it's better than nothing, thus committed */
  133. ccl_device_inline float bake_clamp_mirror_repeat(float u, float max)
  134. {
  135. /* use mirror repeat (like opengl texture) so that if the barycentric
  136. * coordinate goes past the end of the triangle it is not always clamped
  137. * to the same value, gives ugly patterns */
  138. u /= max;
  139. float fu = floorf(u);
  140. u = u - fu;
  141. return ((((int)fu) & 1) ? 1.0f - u : u) * max;
  142. }
  143. ccl_device_inline float3 kernel_bake_shader_bsdf(KernelGlobals *kg,
  144. ShaderData *sd,
  145. const ShaderEvalType type)
  146. {
  147. switch (type) {
  148. case SHADER_EVAL_DIFFUSE:
  149. return shader_bsdf_diffuse(kg, sd);
  150. case SHADER_EVAL_GLOSSY:
  151. return shader_bsdf_glossy(kg, sd);
  152. case SHADER_EVAL_TRANSMISSION:
  153. return shader_bsdf_transmission(kg, sd);
  154. # ifdef __SUBSURFACE__
  155. case SHADER_EVAL_SUBSURFACE:
  156. return shader_bsdf_subsurface(kg, sd);
  157. # endif
  158. default:
  159. kernel_assert(!"Unknown bake type passed to BSDF evaluate");
  160. return make_float3(0.0f, 0.0f, 0.0f);
  161. }
  162. }
  163. ccl_device float3 kernel_bake_evaluate_direct_indirect(KernelGlobals *kg,
  164. ShaderData *sd,
  165. PathState *state,
  166. float3 direct,
  167. float3 indirect,
  168. const ShaderEvalType type,
  169. const int pass_filter)
  170. {
  171. float3 color;
  172. const bool is_color = (pass_filter & BAKE_FILTER_COLOR) != 0;
  173. const bool is_direct = (pass_filter & BAKE_FILTER_DIRECT) != 0;
  174. const bool is_indirect = (pass_filter & BAKE_FILTER_INDIRECT) != 0;
  175. float3 out = make_float3(0.0f, 0.0f, 0.0f);
  176. if (is_color) {
  177. if (is_direct || is_indirect) {
  178. /* Leave direct and diffuse channel colored. */
  179. color = make_float3(1.0f, 1.0f, 1.0f);
  180. }
  181. else {
  182. /* surface color of the pass only */
  183. shader_eval_surface(kg, sd, state, 0);
  184. return kernel_bake_shader_bsdf(kg, sd, type);
  185. }
  186. }
  187. else {
  188. shader_eval_surface(kg, sd, state, 0);
  189. color = kernel_bake_shader_bsdf(kg, sd, type);
  190. }
  191. if (is_direct) {
  192. out += safe_divide_even_color(direct, color);
  193. }
  194. if (is_indirect) {
  195. out += safe_divide_even_color(indirect, color);
  196. }
  197. return out;
  198. }
  199. ccl_device void kernel_bake_evaluate(KernelGlobals *kg,
  200. ccl_global uint4 *input,
  201. ccl_global float4 *output,
  202. ShaderEvalType type,
  203. int pass_filter,
  204. int i,
  205. int offset,
  206. int sample)
  207. {
  208. ShaderData sd;
  209. PathState state = {0};
  210. uint4 in = input[i * 2];
  211. uint4 diff = input[i * 2 + 1];
  212. float3 out = make_float3(0.0f, 0.0f, 0.0f);
  213. int object = in.x;
  214. int prim = in.y;
  215. if (prim == -1)
  216. return;
  217. float u = __uint_as_float(in.z);
  218. float v = __uint_as_float(in.w);
  219. float dudx = __uint_as_float(diff.x);
  220. float dudy = __uint_as_float(diff.y);
  221. float dvdx = __uint_as_float(diff.z);
  222. float dvdy = __uint_as_float(diff.w);
  223. int num_samples = kernel_data.integrator.aa_samples;
  224. /* random number generator */
  225. uint rng_hash = cmj_hash(offset + i, kernel_data.integrator.seed);
  226. float filter_x, filter_y;
  227. if (sample == 0) {
  228. filter_x = filter_y = 0.5f;
  229. }
  230. else {
  231. path_rng_2D(kg, rng_hash, sample, num_samples, PRNG_FILTER_U, &filter_x, &filter_y);
  232. }
  233. /* subpixel u/v offset */
  234. if (sample > 0) {
  235. u = bake_clamp_mirror_repeat(u + dudx * (filter_x - 0.5f) + dudy * (filter_y - 0.5f), 1.0f);
  236. v = bake_clamp_mirror_repeat(v + dvdx * (filter_x - 0.5f) + dvdy * (filter_y - 0.5f),
  237. 1.0f - u);
  238. }
  239. /* triangle */
  240. int shader;
  241. float3 P, Ng;
  242. triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
  243. /* light passes */
  244. PathRadiance L;
  245. path_radiance_init(&L, kernel_data.film.use_light_pass);
  246. shader_setup_from_sample(
  247. kg,
  248. &sd,
  249. P,
  250. Ng,
  251. Ng,
  252. shader,
  253. object,
  254. prim,
  255. u,
  256. v,
  257. 1.0f,
  258. 0.5f,
  259. !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
  260. LAMP_NONE);
  261. sd.I = sd.N;
  262. /* update differentials */
  263. sd.dP.dx = sd.dPdu * dudx + sd.dPdv * dvdx;
  264. sd.dP.dy = sd.dPdu * dudy + sd.dPdv * dvdy;
  265. sd.du.dx = dudx;
  266. sd.du.dy = dudy;
  267. sd.dv.dx = dvdx;
  268. sd.dv.dy = dvdy;
  269. /* set RNG state for shaders that use sampling */
  270. state.rng_hash = rng_hash;
  271. state.rng_offset = 0;
  272. state.sample = sample;
  273. state.num_samples = num_samples;
  274. state.min_ray_pdf = FLT_MAX;
  275. /* light passes if we need more than color */
  276. if (pass_filter & ~BAKE_FILTER_COLOR)
  277. compute_light_pass(kg, &sd, &L, rng_hash, pass_filter, sample);
  278. switch (type) {
  279. /* data passes */
  280. case SHADER_EVAL_NORMAL:
  281. case SHADER_EVAL_ROUGHNESS:
  282. case SHADER_EVAL_EMISSION: {
  283. if (type != SHADER_EVAL_NORMAL || (sd.flag & SD_HAS_BUMP)) {
  284. int path_flag = (type == SHADER_EVAL_EMISSION) ? PATH_RAY_EMISSION : 0;
  285. shader_eval_surface(kg, &sd, &state, path_flag);
  286. }
  287. if (type == SHADER_EVAL_NORMAL) {
  288. float3 N = sd.N;
  289. if (sd.flag & SD_HAS_BUMP) {
  290. N = shader_bsdf_average_normal(kg, &sd);
  291. }
  292. /* encoding: normal = (2 * color) - 1 */
  293. out = N * 0.5f + make_float3(0.5f, 0.5f, 0.5f);
  294. }
  295. else if (type == SHADER_EVAL_ROUGHNESS) {
  296. float roughness = shader_bsdf_average_roughness(&sd);
  297. out = make_float3(roughness, roughness, roughness);
  298. }
  299. else {
  300. out = shader_emissive_eval(&sd);
  301. }
  302. break;
  303. }
  304. case SHADER_EVAL_UV: {
  305. out = primitive_uv(kg, &sd);
  306. break;
  307. }
  308. # ifdef __PASSES__
  309. /* light passes */
  310. case SHADER_EVAL_AO: {
  311. out = L.ao;
  312. break;
  313. }
  314. case SHADER_EVAL_COMBINED: {
  315. if ((pass_filter & BAKE_FILTER_COMBINED) == BAKE_FILTER_COMBINED) {
  316. float alpha;
  317. out = path_radiance_clamp_and_sum(kg, &L, &alpha);
  318. break;
  319. }
  320. if ((pass_filter & BAKE_FILTER_DIFFUSE_DIRECT) == BAKE_FILTER_DIFFUSE_DIRECT)
  321. out += L.direct_diffuse;
  322. if ((pass_filter & BAKE_FILTER_DIFFUSE_INDIRECT) == BAKE_FILTER_DIFFUSE_INDIRECT)
  323. out += L.indirect_diffuse;
  324. if ((pass_filter & BAKE_FILTER_GLOSSY_DIRECT) == BAKE_FILTER_GLOSSY_DIRECT)
  325. out += L.direct_glossy;
  326. if ((pass_filter & BAKE_FILTER_GLOSSY_INDIRECT) == BAKE_FILTER_GLOSSY_INDIRECT)
  327. out += L.indirect_glossy;
  328. if ((pass_filter & BAKE_FILTER_TRANSMISSION_DIRECT) == BAKE_FILTER_TRANSMISSION_DIRECT)
  329. out += L.direct_transmission;
  330. if ((pass_filter & BAKE_FILTER_TRANSMISSION_INDIRECT) == BAKE_FILTER_TRANSMISSION_INDIRECT)
  331. out += L.indirect_transmission;
  332. if ((pass_filter & BAKE_FILTER_SUBSURFACE_DIRECT) == BAKE_FILTER_SUBSURFACE_DIRECT)
  333. out += L.direct_subsurface;
  334. if ((pass_filter & BAKE_FILTER_SUBSURFACE_INDIRECT) == BAKE_FILTER_SUBSURFACE_INDIRECT)
  335. out += L.indirect_subsurface;
  336. if ((pass_filter & BAKE_FILTER_EMISSION) != 0)
  337. out += L.emission;
  338. break;
  339. }
  340. case SHADER_EVAL_SHADOW: {
  341. out = make_float3(L.shadow.x, L.shadow.y, L.shadow.z);
  342. break;
  343. }
  344. case SHADER_EVAL_DIFFUSE: {
  345. out = kernel_bake_evaluate_direct_indirect(
  346. kg, &sd, &state, L.direct_diffuse, L.indirect_diffuse, type, pass_filter);
  347. break;
  348. }
  349. case SHADER_EVAL_GLOSSY: {
  350. out = kernel_bake_evaluate_direct_indirect(
  351. kg, &sd, &state, L.direct_glossy, L.indirect_glossy, type, pass_filter);
  352. break;
  353. }
  354. case SHADER_EVAL_TRANSMISSION: {
  355. out = kernel_bake_evaluate_direct_indirect(
  356. kg, &sd, &state, L.direct_transmission, L.indirect_transmission, type, pass_filter);
  357. break;
  358. }
  359. case SHADER_EVAL_SUBSURFACE: {
  360. # ifdef __SUBSURFACE__
  361. out = kernel_bake_evaluate_direct_indirect(
  362. kg, &sd, &state, L.direct_subsurface, L.indirect_subsurface, type, pass_filter);
  363. # endif
  364. break;
  365. }
  366. # endif
  367. /* extra */
  368. case SHADER_EVAL_ENVIRONMENT: {
  369. /* setup ray */
  370. Ray ray;
  371. ray.P = make_float3(0.0f, 0.0f, 0.0f);
  372. ray.D = normalize(P);
  373. ray.t = 0.0f;
  374. # ifdef __CAMERA_MOTION__
  375. ray.time = 0.5f;
  376. # endif
  377. # ifdef __RAY_DIFFERENTIALS__
  378. ray.dD = differential3_zero();
  379. ray.dP = differential3_zero();
  380. # endif
  381. /* setup shader data */
  382. shader_setup_from_background(kg, &sd, &ray);
  383. /* evaluate */
  384. int path_flag = 0; /* we can't know which type of BSDF this is for */
  385. shader_eval_surface(kg, &sd, &state, path_flag | PATH_RAY_EMISSION);
  386. out = shader_background_eval(&sd);
  387. break;
  388. }
  389. default: {
  390. /* no real shader, returning the position of the verts for debugging */
  391. out = normalize(P);
  392. break;
  393. }
  394. }
  395. /* write output */
  396. const float output_fac = 1.0f / num_samples;
  397. const float4 scaled_result = make_float4(out.x, out.y, out.z, 1.0f) * output_fac;
  398. output[i] = (sample == 0) ? scaled_result : output[i] + scaled_result;
  399. }
  400. #endif /* __BAKING__ */
  401. ccl_device void kernel_displace_evaluate(KernelGlobals *kg,
  402. ccl_global uint4 *input,
  403. ccl_global float4 *output,
  404. int i)
  405. {
  406. ShaderData sd;
  407. PathState state = {0};
  408. uint4 in = input[i];
  409. /* setup shader data */
  410. int object = in.x;
  411. int prim = in.y;
  412. float u = __uint_as_float(in.z);
  413. float v = __uint_as_float(in.w);
  414. shader_setup_from_displace(kg, &sd, object, prim, u, v);
  415. /* evaluate */
  416. float3 P = sd.P;
  417. shader_eval_displacement(kg, &sd, &state);
  418. float3 D = sd.P - P;
  419. object_inverse_dir_transform(kg, &sd, &D);
  420. /* write output */
  421. output[i] += make_float4(D.x, D.y, D.z, 0.0f);
  422. }
  423. ccl_device void kernel_background_evaluate(KernelGlobals *kg,
  424. ccl_global uint4 *input,
  425. ccl_global float4 *output,
  426. int i)
  427. {
  428. ShaderData sd;
  429. PathState state = {0};
  430. uint4 in = input[i];
  431. /* setup ray */
  432. Ray ray;
  433. float u = __uint_as_float(in.x);
  434. float v = __uint_as_float(in.y);
  435. ray.P = make_float3(0.0f, 0.0f, 0.0f);
  436. ray.D = equirectangular_to_direction(u, v);
  437. ray.t = 0.0f;
  438. #ifdef __CAMERA_MOTION__
  439. ray.time = 0.5f;
  440. #endif
  441. #ifdef __RAY_DIFFERENTIALS__
  442. ray.dD = differential3_zero();
  443. ray.dP = differential3_zero();
  444. #endif
  445. /* setup shader data */
  446. shader_setup_from_background(kg, &sd, &ray);
  447. /* evaluate */
  448. int path_flag = 0; /* we can't know which type of BSDF this is for */
  449. shader_eval_surface(kg, &sd, &state, path_flag | PATH_RAY_EMISSION);
  450. float3 color = shader_background_eval(&sd);
  451. /* write output */
  452. output[i] += make_float4(color.x, color.y, color.z, 0.0f);
  453. }
  454. CCL_NAMESPACE_END