kernel_passes.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #if defined(__SPLIT_KERNEL__) || defined(__KERNEL_CUDA__)
  17. # define __ATOMIC_PASS_WRITE__
  18. #endif
  19. #include "kernel/kernel_id_passes.h"
  20. CCL_NAMESPACE_BEGIN
  21. ccl_device_inline void kernel_write_pass_float(ccl_global float *buffer, float value)
  22. {
  23. ccl_global float *buf = buffer;
  24. #ifdef __ATOMIC_PASS_WRITE__
  25. atomic_add_and_fetch_float(buf, value);
  26. #else
  27. *buf += value;
  28. #endif
  29. }
  30. ccl_device_inline void kernel_write_pass_float3(ccl_global float *buffer, float3 value)
  31. {
  32. #ifdef __ATOMIC_PASS_WRITE__
  33. ccl_global float *buf_x = buffer + 0;
  34. ccl_global float *buf_y = buffer + 1;
  35. ccl_global float *buf_z = buffer + 2;
  36. atomic_add_and_fetch_float(buf_x, value.x);
  37. atomic_add_and_fetch_float(buf_y, value.y);
  38. atomic_add_and_fetch_float(buf_z, value.z);
  39. #else
  40. ccl_global float3 *buf = (ccl_global float3 *)buffer;
  41. *buf += value;
  42. #endif
  43. }
  44. ccl_device_inline void kernel_write_pass_float4(ccl_global float *buffer, float4 value)
  45. {
  46. #ifdef __ATOMIC_PASS_WRITE__
  47. ccl_global float *buf_x = buffer + 0;
  48. ccl_global float *buf_y = buffer + 1;
  49. ccl_global float *buf_z = buffer + 2;
  50. ccl_global float *buf_w = buffer + 3;
  51. atomic_add_and_fetch_float(buf_x, value.x);
  52. atomic_add_and_fetch_float(buf_y, value.y);
  53. atomic_add_and_fetch_float(buf_z, value.z);
  54. atomic_add_and_fetch_float(buf_w, value.w);
  55. #else
  56. ccl_global float4 *buf = (ccl_global float4 *)buffer;
  57. *buf += value;
  58. #endif
  59. }
  60. #ifdef __DENOISING_FEATURES__
  61. ccl_device_inline void kernel_write_pass_float_variance(ccl_global float *buffer, float value)
  62. {
  63. kernel_write_pass_float(buffer, value);
  64. /* The online one-pass variance update that's used for the mega-kernel can't easily be
  65. * implemented with atomics,
  66. * so for the split kernel the E[x^2] - 1/N * (E[x])^2 fallback is used. */
  67. kernel_write_pass_float(buffer + 1, value * value);
  68. }
  69. # ifdef __ATOMIC_PASS_WRITE__
  70. # define kernel_write_pass_float3_unaligned kernel_write_pass_float3
  71. # else
  72. ccl_device_inline void kernel_write_pass_float3_unaligned(ccl_global float *buffer, float3 value)
  73. {
  74. buffer[0] += value.x;
  75. buffer[1] += value.y;
  76. buffer[2] += value.z;
  77. }
  78. # endif
  79. ccl_device_inline void kernel_write_pass_float3_variance(ccl_global float *buffer, float3 value)
  80. {
  81. kernel_write_pass_float3_unaligned(buffer, value);
  82. kernel_write_pass_float3_unaligned(buffer + 3, value * value);
  83. }
  84. ccl_device_inline void kernel_write_denoising_shadow(KernelGlobals *kg,
  85. ccl_global float *buffer,
  86. int sample,
  87. float path_total,
  88. float path_total_shaded)
  89. {
  90. if (kernel_data.film.pass_denoising_data == 0)
  91. return;
  92. buffer += (sample & 1) ? DENOISING_PASS_SHADOW_B : DENOISING_PASS_SHADOW_A;
  93. path_total = ensure_finite(path_total);
  94. path_total_shaded = ensure_finite(path_total_shaded);
  95. kernel_write_pass_float(buffer, path_total);
  96. kernel_write_pass_float(buffer + 1, path_total_shaded);
  97. float value = path_total_shaded / max(path_total, 1e-7f);
  98. kernel_write_pass_float(buffer + 2, value * value);
  99. }
  100. #endif /* __DENOISING_FEATURES__ */
  101. ccl_device_inline void kernel_update_denoising_features(KernelGlobals *kg,
  102. ShaderData *sd,
  103. ccl_addr_space PathState *state,
  104. PathRadiance *L)
  105. {
  106. #ifdef __DENOISING_FEATURES__
  107. if (state->denoising_feature_weight == 0.0f) {
  108. return;
  109. }
  110. L->denoising_depth += ensure_finite(state->denoising_feature_weight * sd->ray_length);
  111. /* Skip implicitly transparent surfaces. */
  112. if (sd->flag & SD_HAS_ONLY_VOLUME) {
  113. return;
  114. }
  115. float3 normal = make_float3(0.0f, 0.0f, 0.0f);
  116. float3 albedo = make_float3(0.0f, 0.0f, 0.0f);
  117. float sum_weight = 0.0f, sum_nonspecular_weight = 0.0f;
  118. for (int i = 0; i < sd->num_closure; i++) {
  119. ShaderClosure *sc = &sd->closure[i];
  120. if (!CLOSURE_IS_BSDF_OR_BSSRDF(sc->type))
  121. continue;
  122. /* All closures contribute to the normal feature, but only diffuse-like ones to the albedo. */
  123. normal += sc->N * sc->sample_weight;
  124. sum_weight += sc->sample_weight;
  125. if (bsdf_get_specular_roughness_squared(sc) > sqr(0.075f)) {
  126. albedo += sc->weight;
  127. sum_nonspecular_weight += sc->sample_weight;
  128. }
  129. }
  130. /* Wait for next bounce if 75% or more sample weight belongs to specular-like closures. */
  131. if ((sum_weight == 0.0f) || (sum_nonspecular_weight * 4.0f > sum_weight)) {
  132. if (sum_weight != 0.0f) {
  133. normal /= sum_weight;
  134. }
  135. L->denoising_normal += ensure_finite3(state->denoising_feature_weight * normal);
  136. L->denoising_albedo += ensure_finite3(state->denoising_feature_weight * albedo);
  137. state->denoising_feature_weight = 0.0f;
  138. }
  139. #else
  140. (void)kg;
  141. (void)sd;
  142. (void)state;
  143. (void)L;
  144. #endif /* __DENOISING_FEATURES__ */
  145. }
  146. #ifdef __KERNEL_DEBUG__
  147. ccl_device_inline void kernel_write_debug_passes(KernelGlobals *kg,
  148. ccl_global float *buffer,
  149. PathRadiance *L)
  150. {
  151. int flag = kernel_data.film.pass_flag;
  152. if (flag & PASSMASK(BVH_TRAVERSED_NODES)) {
  153. kernel_write_pass_float(buffer + kernel_data.film.pass_bvh_traversed_nodes,
  154. L->debug_data.num_bvh_traversed_nodes);
  155. }
  156. if (flag & PASSMASK(BVH_TRAVERSED_INSTANCES)) {
  157. kernel_write_pass_float(buffer + kernel_data.film.pass_bvh_traversed_instances,
  158. L->debug_data.num_bvh_traversed_instances);
  159. }
  160. if (flag & PASSMASK(BVH_INTERSECTIONS)) {
  161. kernel_write_pass_float(buffer + kernel_data.film.pass_bvh_intersections,
  162. L->debug_data.num_bvh_intersections);
  163. }
  164. if (flag & PASSMASK(RAY_BOUNCES)) {
  165. kernel_write_pass_float(buffer + kernel_data.film.pass_ray_bounces,
  166. L->debug_data.num_ray_bounces);
  167. }
  168. }
  169. #endif /* __KERNEL_DEBUG__ */
  170. #ifdef __KERNEL_CPU__
  171. # define WRITE_ID_SLOT(buffer, depth, id, matte_weight, name) \
  172. kernel_write_id_pass_cpu(buffer, depth * 2, id, matte_weight, kg->coverage_##name)
  173. ccl_device_inline size_t kernel_write_id_pass_cpu(
  174. float *buffer, size_t depth, float id, float matte_weight, CoverageMap *map)
  175. {
  176. if (map) {
  177. (*map)[id] += matte_weight;
  178. return 0;
  179. }
  180. #else /* __KERNEL_CPU__ */
  181. # define WRITE_ID_SLOT(buffer, depth, id, matte_weight, name) \
  182. kernel_write_id_slots_gpu(buffer, depth * 2, id, matte_weight)
  183. ccl_device_inline size_t kernel_write_id_slots_gpu(ccl_global float *buffer,
  184. size_t depth,
  185. float id,
  186. float matte_weight)
  187. {
  188. #endif /* __KERNEL_CPU__ */
  189. kernel_write_id_slots(buffer, depth, id, matte_weight);
  190. return depth * 2;
  191. }
  192. ccl_device_inline void kernel_write_data_passes(KernelGlobals *kg,
  193. ccl_global float *buffer,
  194. PathRadiance *L,
  195. ShaderData *sd,
  196. ccl_addr_space PathState *state,
  197. float3 throughput)
  198. {
  199. #ifdef __PASSES__
  200. int path_flag = state->flag;
  201. if (!(path_flag & PATH_RAY_CAMERA))
  202. return;
  203. int flag = kernel_data.film.pass_flag;
  204. int light_flag = kernel_data.film.light_pass_flag;
  205. if (!((flag | light_flag) & PASS_ANY))
  206. return;
  207. if (!(path_flag & PATH_RAY_SINGLE_PASS_DONE)) {
  208. if (!(sd->flag & SD_TRANSPARENT) || kernel_data.film.pass_alpha_threshold == 0.0f ||
  209. average(shader_bsdf_alpha(kg, sd)) >= kernel_data.film.pass_alpha_threshold) {
  210. if (state->sample == 0) {
  211. if (flag & PASSMASK(DEPTH)) {
  212. float depth = camera_distance(kg, sd->P);
  213. kernel_write_pass_float(buffer + kernel_data.film.pass_depth, depth);
  214. }
  215. if (flag & PASSMASK(OBJECT_ID)) {
  216. float id = object_pass_id(kg, sd->object);
  217. kernel_write_pass_float(buffer + kernel_data.film.pass_object_id, id);
  218. }
  219. if (flag & PASSMASK(MATERIAL_ID)) {
  220. float id = shader_pass_id(kg, sd);
  221. kernel_write_pass_float(buffer + kernel_data.film.pass_material_id, id);
  222. }
  223. }
  224. if (flag & PASSMASK(NORMAL)) {
  225. float3 normal = shader_bsdf_average_normal(kg, sd);
  226. kernel_write_pass_float3(buffer + kernel_data.film.pass_normal, normal);
  227. }
  228. if (flag & PASSMASK(UV)) {
  229. float3 uv = primitive_uv(kg, sd);
  230. kernel_write_pass_float3(buffer + kernel_data.film.pass_uv, uv);
  231. }
  232. if (flag & PASSMASK(MOTION)) {
  233. float4 speed = primitive_motion_vector(kg, sd);
  234. kernel_write_pass_float4(buffer + kernel_data.film.pass_motion, speed);
  235. kernel_write_pass_float(buffer + kernel_data.film.pass_motion_weight, 1.0f);
  236. }
  237. state->flag |= PATH_RAY_SINGLE_PASS_DONE;
  238. }
  239. }
  240. if (kernel_data.film.cryptomatte_passes) {
  241. const float matte_weight = average(throughput) *
  242. (1.0f - average(shader_bsdf_transparency(kg, sd)));
  243. if (matte_weight > 0.0f) {
  244. ccl_global float *cryptomatte_buffer = buffer + kernel_data.film.pass_cryptomatte;
  245. if (kernel_data.film.cryptomatte_passes & CRYPT_OBJECT) {
  246. float id = object_cryptomatte_id(kg, sd->object);
  247. cryptomatte_buffer += WRITE_ID_SLOT(
  248. cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight, object);
  249. }
  250. if (kernel_data.film.cryptomatte_passes & CRYPT_MATERIAL) {
  251. float id = shader_cryptomatte_id(kg, sd->shader);
  252. cryptomatte_buffer += WRITE_ID_SLOT(
  253. cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight, material);
  254. }
  255. if (kernel_data.film.cryptomatte_passes & CRYPT_ASSET) {
  256. float id = object_cryptomatte_asset_id(kg, sd->object);
  257. cryptomatte_buffer += WRITE_ID_SLOT(
  258. cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight, asset);
  259. }
  260. }
  261. }
  262. if (light_flag & PASSMASK_COMPONENT(DIFFUSE))
  263. L->color_diffuse += shader_bsdf_diffuse(kg, sd) * throughput;
  264. if (light_flag & PASSMASK_COMPONENT(GLOSSY))
  265. L->color_glossy += shader_bsdf_glossy(kg, sd) * throughput;
  266. if (light_flag & PASSMASK_COMPONENT(TRANSMISSION))
  267. L->color_transmission += shader_bsdf_transmission(kg, sd) * throughput;
  268. if (light_flag & PASSMASK_COMPONENT(SUBSURFACE))
  269. L->color_subsurface += shader_bsdf_subsurface(kg, sd) * throughput;
  270. if (light_flag & PASSMASK(MIST)) {
  271. /* bring depth into 0..1 range */
  272. float mist_start = kernel_data.film.mist_start;
  273. float mist_inv_depth = kernel_data.film.mist_inv_depth;
  274. float depth = camera_distance(kg, sd->P);
  275. float mist = saturate((depth - mist_start) * mist_inv_depth);
  276. /* falloff */
  277. float mist_falloff = kernel_data.film.mist_falloff;
  278. if (mist_falloff == 1.0f)
  279. ;
  280. else if (mist_falloff == 2.0f)
  281. mist = mist * mist;
  282. else if (mist_falloff == 0.5f)
  283. mist = sqrtf(mist);
  284. else
  285. mist = powf(mist, mist_falloff);
  286. /* modulate by transparency */
  287. float3 alpha = shader_bsdf_alpha(kg, sd);
  288. L->mist += (1.0f - mist) * average(throughput * alpha);
  289. }
  290. #endif
  291. }
  292. ccl_device_inline void kernel_write_light_passes(KernelGlobals *kg,
  293. ccl_global float *buffer,
  294. PathRadiance *L)
  295. {
  296. #ifdef __PASSES__
  297. int light_flag = kernel_data.film.light_pass_flag;
  298. if (!kernel_data.film.use_light_pass)
  299. return;
  300. if (light_flag & PASSMASK(DIFFUSE_INDIRECT))
  301. kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_indirect, L->indirect_diffuse);
  302. if (light_flag & PASSMASK(GLOSSY_INDIRECT))
  303. kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_indirect, L->indirect_glossy);
  304. if (light_flag & PASSMASK(TRANSMISSION_INDIRECT))
  305. kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_indirect,
  306. L->indirect_transmission);
  307. if (light_flag & PASSMASK(SUBSURFACE_INDIRECT))
  308. kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_indirect,
  309. L->indirect_subsurface);
  310. if (light_flag & PASSMASK(VOLUME_INDIRECT))
  311. kernel_write_pass_float3(buffer + kernel_data.film.pass_volume_indirect, L->indirect_scatter);
  312. if (light_flag & PASSMASK(DIFFUSE_DIRECT))
  313. kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_direct, L->direct_diffuse);
  314. if (light_flag & PASSMASK(GLOSSY_DIRECT))
  315. kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_direct, L->direct_glossy);
  316. if (light_flag & PASSMASK(TRANSMISSION_DIRECT))
  317. kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_direct,
  318. L->direct_transmission);
  319. if (light_flag & PASSMASK(SUBSURFACE_DIRECT))
  320. kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_direct,
  321. L->direct_subsurface);
  322. if (light_flag & PASSMASK(VOLUME_DIRECT))
  323. kernel_write_pass_float3(buffer + kernel_data.film.pass_volume_direct, L->direct_scatter);
  324. if (light_flag & PASSMASK(EMISSION))
  325. kernel_write_pass_float3(buffer + kernel_data.film.pass_emission, L->emission);
  326. if (light_flag & PASSMASK(BACKGROUND))
  327. kernel_write_pass_float3(buffer + kernel_data.film.pass_background, L->background);
  328. if (light_flag & PASSMASK(AO))
  329. kernel_write_pass_float3(buffer + kernel_data.film.pass_ao, L->ao);
  330. if (light_flag & PASSMASK(DIFFUSE_COLOR))
  331. kernel_write_pass_float3(buffer + kernel_data.film.pass_diffuse_color, L->color_diffuse);
  332. if (light_flag & PASSMASK(GLOSSY_COLOR))
  333. kernel_write_pass_float3(buffer + kernel_data.film.pass_glossy_color, L->color_glossy);
  334. if (light_flag & PASSMASK(TRANSMISSION_COLOR))
  335. kernel_write_pass_float3(buffer + kernel_data.film.pass_transmission_color,
  336. L->color_transmission);
  337. if (light_flag & PASSMASK(SUBSURFACE_COLOR))
  338. kernel_write_pass_float3(buffer + kernel_data.film.pass_subsurface_color, L->color_subsurface);
  339. if (light_flag & PASSMASK(SHADOW)) {
  340. float4 shadow = L->shadow;
  341. shadow.w = kernel_data.film.pass_shadow_scale;
  342. kernel_write_pass_float4(buffer + kernel_data.film.pass_shadow, shadow);
  343. }
  344. if (light_flag & PASSMASK(MIST))
  345. kernel_write_pass_float(buffer + kernel_data.film.pass_mist, 1.0f - L->mist);
  346. #endif
  347. }
  348. ccl_device_inline void kernel_write_result(KernelGlobals *kg,
  349. ccl_global float *buffer,
  350. int sample,
  351. PathRadiance *L)
  352. {
  353. PROFILING_INIT(kg, PROFILING_WRITE_RESULT);
  354. PROFILING_OBJECT(PRIM_NONE);
  355. float alpha;
  356. float3 L_sum = path_radiance_clamp_and_sum(kg, L, &alpha);
  357. kernel_write_pass_float4(buffer, make_float4(L_sum.x, L_sum.y, L_sum.z, alpha));
  358. kernel_write_light_passes(kg, buffer, L);
  359. #ifdef __DENOISING_FEATURES__
  360. if (kernel_data.film.pass_denoising_data) {
  361. # ifdef __SHADOW_TRICKS__
  362. kernel_write_denoising_shadow(kg,
  363. buffer + kernel_data.film.pass_denoising_data,
  364. sample,
  365. average(L->path_total),
  366. average(L->path_total_shaded));
  367. # else
  368. kernel_write_denoising_shadow(
  369. kg, buffer + kernel_data.film.pass_denoising_data, sample, 0.0f, 0.0f);
  370. # endif
  371. if (kernel_data.film.pass_denoising_clean) {
  372. float3 noisy, clean;
  373. path_radiance_split_denoising(kg, L, &noisy, &clean);
  374. kernel_write_pass_float3_variance(
  375. buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_COLOR, noisy);
  376. kernel_write_pass_float3_unaligned(buffer + kernel_data.film.pass_denoising_clean, clean);
  377. }
  378. else {
  379. kernel_write_pass_float3_variance(buffer + kernel_data.film.pass_denoising_data +
  380. DENOISING_PASS_COLOR,
  381. ensure_finite3(L_sum));
  382. }
  383. kernel_write_pass_float3_variance(buffer + kernel_data.film.pass_denoising_data +
  384. DENOISING_PASS_NORMAL,
  385. L->denoising_normal);
  386. kernel_write_pass_float3_variance(buffer + kernel_data.film.pass_denoising_data +
  387. DENOISING_PASS_ALBEDO,
  388. L->denoising_albedo);
  389. kernel_write_pass_float_variance(
  390. buffer + kernel_data.film.pass_denoising_data + DENOISING_PASS_DEPTH, L->denoising_depth);
  391. }
  392. #endif /* __DENOISING_FEATURES__ */
  393. #ifdef __KERNEL_DEBUG__
  394. kernel_write_debug_passes(kg, buffer, L);
  395. #endif
  396. }
  397. CCL_NAMESPACE_END