svm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #ifndef __SVM_H__
  17. #define __SVM_H__
  18. /* Shader Virtual Machine
  19. *
  20. * A shader is a list of nodes to be executed. These are simply read one after
  21. * the other and executed, using an node counter. Each node and it's associated
  22. * data is encoded as one or more uint4's in a 1D texture. If the data is larger
  23. * than an uint4, the node can increase the node counter to compensate for this.
  24. * Floats are encoded as int and then converted to float again.
  25. *
  26. * Nodes write their output into a stack. All stack data in the stack is
  27. * floats, since it's all factors, colors and vectors. The stack will be stored
  28. * in local memory on the GPU, as it would take too many register and indexes in
  29. * ways not known at compile time. This seems the only solution even though it
  30. * may be slow, with two positive factors. If the same shader is being executed,
  31. * memory access will be coalesced and cached.
  32. *
  33. * The result of shader execution will be a single closure. This means the
  34. * closure type, associated label, data and weight. Sampling from multiple
  35. * closures is supported through the mix closure node, the logic for that is
  36. * mostly taken care of in the SVM compiler.
  37. */
  38. #include "kernel/svm/svm_types.h"
  39. CCL_NAMESPACE_BEGIN
  40. /* Stack */
  41. ccl_device_inline float3 stack_load_float3(float *stack, uint a)
  42. {
  43. kernel_assert(a + 2 < SVM_STACK_SIZE);
  44. return make_float3(stack[a + 0], stack[a + 1], stack[a + 2]);
  45. }
  46. ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
  47. {
  48. kernel_assert(a + 2 < SVM_STACK_SIZE);
  49. stack[a + 0] = f.x;
  50. stack[a + 1] = f.y;
  51. stack[a + 2] = f.z;
  52. }
  53. ccl_device_inline float stack_load_float(float *stack, uint a)
  54. {
  55. kernel_assert(a < SVM_STACK_SIZE);
  56. return stack[a];
  57. }
  58. ccl_device_inline float stack_load_float_default(float *stack, uint a, uint value)
  59. {
  60. return (a == (uint)SVM_STACK_INVALID) ? __uint_as_float(value) : stack_load_float(stack, a);
  61. }
  62. ccl_device_inline void stack_store_float(float *stack, uint a, float f)
  63. {
  64. kernel_assert(a < SVM_STACK_SIZE);
  65. stack[a] = f;
  66. }
  67. ccl_device_inline int stack_load_int(float *stack, uint a)
  68. {
  69. kernel_assert(a < SVM_STACK_SIZE);
  70. return __float_as_int(stack[a]);
  71. }
  72. ccl_device_inline int stack_load_int_default(float *stack, uint a, uint value)
  73. {
  74. return (a == (uint)SVM_STACK_INVALID) ? (int)value : stack_load_int(stack, a);
  75. }
  76. ccl_device_inline void stack_store_int(float *stack, uint a, int i)
  77. {
  78. kernel_assert(a < SVM_STACK_SIZE);
  79. stack[a] = __int_as_float(i);
  80. }
  81. ccl_device_inline bool stack_valid(uint a)
  82. {
  83. return a != (uint)SVM_STACK_INVALID;
  84. }
  85. /* Reading Nodes */
  86. ccl_device_inline uint4 read_node(KernelGlobals *kg, int *offset)
  87. {
  88. uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
  89. (*offset)++;
  90. return node;
  91. }
  92. ccl_device_inline float4 read_node_float(KernelGlobals *kg, int *offset)
  93. {
  94. uint4 node = kernel_tex_fetch(__svm_nodes, *offset);
  95. float4 f = make_float4(__uint_as_float(node.x),
  96. __uint_as_float(node.y),
  97. __uint_as_float(node.z),
  98. __uint_as_float(node.w));
  99. (*offset)++;
  100. return f;
  101. }
  102. ccl_device_inline float4 fetch_node_float(KernelGlobals *kg, int offset)
  103. {
  104. uint4 node = kernel_tex_fetch(__svm_nodes, offset);
  105. return make_float4(__uint_as_float(node.x),
  106. __uint_as_float(node.y),
  107. __uint_as_float(node.z),
  108. __uint_as_float(node.w));
  109. }
  110. ccl_device_inline void decode_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
  111. {
  112. if (x)
  113. *x = (i & 0xFF);
  114. if (y)
  115. *y = ((i >> 8) & 0xFF);
  116. if (z)
  117. *z = ((i >> 16) & 0xFF);
  118. if (w)
  119. *w = ((i >> 24) & 0xFF);
  120. }
  121. CCL_NAMESPACE_END
  122. /* Nodes */
  123. #include "kernel/svm/svm_noise.h"
  124. #include "svm_texture.h"
  125. #include "kernel/svm/svm_color_util.h"
  126. #include "kernel/svm/svm_math_util.h"
  127. #include "kernel/svm/svm_attribute.h"
  128. #include "kernel/svm/svm_gradient.h"
  129. #include "kernel/svm/svm_blackbody.h"
  130. #include "kernel/svm/svm_closure.h"
  131. #include "kernel/svm/svm_noisetex.h"
  132. #include "kernel/svm/svm_convert.h"
  133. #include "kernel/svm/svm_displace.h"
  134. #include "kernel/svm/svm_fresnel.h"
  135. #include "kernel/svm/svm_wireframe.h"
  136. #include "kernel/svm/svm_wavelength.h"
  137. #include "kernel/svm/svm_camera.h"
  138. #include "kernel/svm/svm_geometry.h"
  139. #include "kernel/svm/svm_hsv.h"
  140. #include "kernel/svm/svm_ies.h"
  141. #include "kernel/svm/svm_image.h"
  142. #include "kernel/svm/svm_gamma.h"
  143. #include "kernel/svm/svm_brightness.h"
  144. #include "kernel/svm/svm_invert.h"
  145. #include "kernel/svm/svm_light_path.h"
  146. #include "kernel/svm/svm_magic.h"
  147. #include "kernel/svm/svm_mapping.h"
  148. #include "kernel/svm/svm_normal.h"
  149. #include "kernel/svm/svm_wave.h"
  150. #include "kernel/svm/svm_math.h"
  151. #include "kernel/svm/svm_mix.h"
  152. #include "kernel/svm/svm_ramp.h"
  153. #include "kernel/svm/svm_sepcomb_hsv.h"
  154. #include "kernel/svm/svm_sepcomb_vector.h"
  155. #include "kernel/svm/svm_musgrave.h"
  156. #include "kernel/svm/svm_sky.h"
  157. #include "kernel/svm/svm_tex_coord.h"
  158. #include "kernel/svm/svm_value.h"
  159. #include "kernel/svm/svm_voronoi.h"
  160. #include "kernel/svm/svm_checker.h"
  161. #include "kernel/svm/svm_brick.h"
  162. #include "kernel/svm/svm_vector_transform.h"
  163. #include "kernel/svm/svm_voxel.h"
  164. #include "kernel/svm/svm_bump.h"
  165. #ifdef __SHADER_RAYTRACE__
  166. # include "kernel/svm/svm_ao.h"
  167. # include "kernel/svm/svm_bevel.h"
  168. #endif
  169. CCL_NAMESPACE_BEGIN
  170. #define NODES_GROUP(group) ((group) <= __NODES_MAX_GROUP__)
  171. #define NODES_FEATURE(feature) ((__NODES_FEATURES__ & (feature)) != 0)
  172. /* Main Interpreter Loop */
  173. ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg,
  174. ShaderData *sd,
  175. ccl_addr_space PathState *state,
  176. ShaderType type,
  177. int path_flag)
  178. {
  179. float stack[SVM_STACK_SIZE];
  180. int offset = sd->shader & SHADER_MASK;
  181. while (1) {
  182. uint4 node = read_node(kg, &offset);
  183. switch (node.x) {
  184. #if NODES_GROUP(NODE_GROUP_LEVEL_0)
  185. case NODE_SHADER_JUMP: {
  186. if (type == SHADER_TYPE_SURFACE)
  187. offset = node.y;
  188. else if (type == SHADER_TYPE_VOLUME)
  189. offset = node.z;
  190. else if (type == SHADER_TYPE_DISPLACEMENT)
  191. offset = node.w;
  192. else
  193. return;
  194. break;
  195. }
  196. case NODE_CLOSURE_BSDF:
  197. svm_node_closure_bsdf(kg, sd, stack, node, type, path_flag, &offset);
  198. break;
  199. case NODE_CLOSURE_EMISSION:
  200. svm_node_closure_emission(sd, stack, node);
  201. break;
  202. case NODE_CLOSURE_BACKGROUND:
  203. svm_node_closure_background(sd, stack, node);
  204. break;
  205. case NODE_CLOSURE_SET_WEIGHT:
  206. svm_node_closure_set_weight(sd, node.y, node.z, node.w);
  207. break;
  208. case NODE_CLOSURE_WEIGHT:
  209. svm_node_closure_weight(sd, stack, node.y);
  210. break;
  211. case NODE_EMISSION_WEIGHT:
  212. svm_node_emission_weight(kg, sd, stack, node);
  213. break;
  214. case NODE_MIX_CLOSURE:
  215. svm_node_mix_closure(sd, stack, node);
  216. break;
  217. case NODE_JUMP_IF_ZERO:
  218. if (stack_load_float(stack, node.z) == 0.0f)
  219. offset += node.y;
  220. break;
  221. case NODE_JUMP_IF_ONE:
  222. if (stack_load_float(stack, node.z) == 1.0f)
  223. offset += node.y;
  224. break;
  225. case NODE_GEOMETRY:
  226. svm_node_geometry(kg, sd, stack, node.y, node.z);
  227. break;
  228. case NODE_CONVERT:
  229. svm_node_convert(kg, sd, stack, node.y, node.z, node.w);
  230. break;
  231. case NODE_TEX_COORD:
  232. svm_node_tex_coord(kg, sd, path_flag, stack, node, &offset);
  233. break;
  234. case NODE_VALUE_F:
  235. svm_node_value_f(kg, sd, stack, node.y, node.z);
  236. break;
  237. case NODE_VALUE_V:
  238. svm_node_value_v(kg, sd, stack, node.y, &offset);
  239. break;
  240. case NODE_ATTR:
  241. svm_node_attr(kg, sd, stack, node);
  242. break;
  243. # if NODES_FEATURE(NODE_FEATURE_BUMP)
  244. case NODE_GEOMETRY_BUMP_DX:
  245. svm_node_geometry_bump_dx(kg, sd, stack, node.y, node.z);
  246. break;
  247. case NODE_GEOMETRY_BUMP_DY:
  248. svm_node_geometry_bump_dy(kg, sd, stack, node.y, node.z);
  249. break;
  250. case NODE_SET_DISPLACEMENT:
  251. svm_node_set_displacement(kg, sd, stack, node.y);
  252. break;
  253. case NODE_DISPLACEMENT:
  254. svm_node_displacement(kg, sd, stack, node);
  255. break;
  256. case NODE_VECTOR_DISPLACEMENT:
  257. svm_node_vector_displacement(kg, sd, stack, node, &offset);
  258. break;
  259. # endif /* NODES_FEATURE(NODE_FEATURE_BUMP) */
  260. # ifdef __TEXTURES__
  261. case NODE_TEX_IMAGE:
  262. svm_node_tex_image(kg, sd, stack, node);
  263. break;
  264. case NODE_TEX_IMAGE_BOX:
  265. svm_node_tex_image_box(kg, sd, stack, node);
  266. break;
  267. case NODE_TEX_NOISE:
  268. svm_node_tex_noise(kg, sd, stack, node, &offset);
  269. break;
  270. # endif /* __TEXTURES__ */
  271. # ifdef __EXTRA_NODES__
  272. # if NODES_FEATURE(NODE_FEATURE_BUMP)
  273. case NODE_SET_BUMP:
  274. svm_node_set_bump(kg, sd, stack, node);
  275. break;
  276. case NODE_ATTR_BUMP_DX:
  277. svm_node_attr_bump_dx(kg, sd, stack, node);
  278. break;
  279. case NODE_ATTR_BUMP_DY:
  280. svm_node_attr_bump_dy(kg, sd, stack, node);
  281. break;
  282. case NODE_TEX_COORD_BUMP_DX:
  283. svm_node_tex_coord_bump_dx(kg, sd, path_flag, stack, node, &offset);
  284. break;
  285. case NODE_TEX_COORD_BUMP_DY:
  286. svm_node_tex_coord_bump_dy(kg, sd, path_flag, stack, node, &offset);
  287. break;
  288. case NODE_CLOSURE_SET_NORMAL:
  289. svm_node_set_normal(kg, sd, stack, node.y, node.z);
  290. break;
  291. # if NODES_FEATURE(NODE_FEATURE_BUMP_STATE)
  292. case NODE_ENTER_BUMP_EVAL:
  293. svm_node_enter_bump_eval(kg, sd, stack, node.y);
  294. break;
  295. case NODE_LEAVE_BUMP_EVAL:
  296. svm_node_leave_bump_eval(kg, sd, stack, node.y);
  297. break;
  298. # endif /* NODES_FEATURE(NODE_FEATURE_BUMP_STATE) */
  299. # endif /* NODES_FEATURE(NODE_FEATURE_BUMP) */
  300. case NODE_HSV:
  301. svm_node_hsv(kg, sd, stack, node, &offset);
  302. break;
  303. # endif /* __EXTRA_NODES__ */
  304. #endif /* NODES_GROUP(NODE_GROUP_LEVEL_0) */
  305. #if NODES_GROUP(NODE_GROUP_LEVEL_1)
  306. case NODE_CLOSURE_HOLDOUT:
  307. svm_node_closure_holdout(sd, stack, node);
  308. break;
  309. case NODE_FRESNEL:
  310. svm_node_fresnel(sd, stack, node.y, node.z, node.w);
  311. break;
  312. case NODE_LAYER_WEIGHT:
  313. svm_node_layer_weight(sd, stack, node);
  314. break;
  315. # if NODES_FEATURE(NODE_FEATURE_VOLUME)
  316. case NODE_CLOSURE_VOLUME:
  317. svm_node_closure_volume(kg, sd, stack, node, type);
  318. break;
  319. case NODE_PRINCIPLED_VOLUME:
  320. svm_node_principled_volume(kg, sd, stack, node, type, path_flag, &offset);
  321. break;
  322. # endif /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
  323. # ifdef __EXTRA_NODES__
  324. case NODE_MATH:
  325. svm_node_math(kg, sd, stack, node.y, node.z, node.w, &offset);
  326. break;
  327. case NODE_VECTOR_MATH:
  328. svm_node_vector_math(kg, sd, stack, node.y, node.z, node.w, &offset);
  329. break;
  330. case NODE_RGB_RAMP:
  331. svm_node_rgb_ramp(kg, sd, stack, node, &offset);
  332. break;
  333. case NODE_GAMMA:
  334. svm_node_gamma(sd, stack, node.y, node.z, node.w);
  335. break;
  336. case NODE_BRIGHTCONTRAST:
  337. svm_node_brightness(sd, stack, node.y, node.z, node.w);
  338. break;
  339. case NODE_LIGHT_PATH:
  340. svm_node_light_path(sd, state, stack, node.y, node.z, path_flag);
  341. break;
  342. case NODE_OBJECT_INFO:
  343. svm_node_object_info(kg, sd, stack, node.y, node.z);
  344. break;
  345. case NODE_PARTICLE_INFO:
  346. svm_node_particle_info(kg, sd, stack, node.y, node.z);
  347. break;
  348. # ifdef __HAIR__
  349. # if NODES_FEATURE(NODE_FEATURE_HAIR)
  350. case NODE_HAIR_INFO:
  351. svm_node_hair_info(kg, sd, stack, node.y, node.z);
  352. break;
  353. # endif /* NODES_FEATURE(NODE_FEATURE_HAIR) */
  354. # endif /* __HAIR__ */
  355. # endif /* __EXTRA_NODES__ */
  356. #endif /* NODES_GROUP(NODE_GROUP_LEVEL_1) */
  357. #if NODES_GROUP(NODE_GROUP_LEVEL_2)
  358. case NODE_MAPPING:
  359. svm_node_mapping(kg, sd, stack, node.y, node.z, &offset);
  360. break;
  361. case NODE_MIN_MAX:
  362. svm_node_min_max(kg, sd, stack, node.y, node.z, &offset);
  363. break;
  364. case NODE_CAMERA:
  365. svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
  366. break;
  367. # ifdef __TEXTURES__
  368. case NODE_TEX_ENVIRONMENT:
  369. svm_node_tex_environment(kg, sd, stack, node);
  370. break;
  371. case NODE_TEX_SKY:
  372. svm_node_tex_sky(kg, sd, stack, node, &offset);
  373. break;
  374. case NODE_TEX_GRADIENT:
  375. svm_node_tex_gradient(sd, stack, node);
  376. break;
  377. case NODE_TEX_VORONOI:
  378. svm_node_tex_voronoi(kg, sd, stack, node, &offset);
  379. break;
  380. case NODE_TEX_MUSGRAVE:
  381. svm_node_tex_musgrave(kg, sd, stack, node, &offset);
  382. break;
  383. case NODE_TEX_WAVE:
  384. svm_node_tex_wave(kg, sd, stack, node, &offset);
  385. break;
  386. case NODE_TEX_MAGIC:
  387. svm_node_tex_magic(kg, sd, stack, node, &offset);
  388. break;
  389. case NODE_TEX_CHECKER:
  390. svm_node_tex_checker(kg, sd, stack, node);
  391. break;
  392. case NODE_TEX_BRICK:
  393. svm_node_tex_brick(kg, sd, stack, node, &offset);
  394. break;
  395. # endif /* __TEXTURES__ */
  396. # ifdef __EXTRA_NODES__
  397. case NODE_NORMAL:
  398. svm_node_normal(kg, sd, stack, node.y, node.z, node.w, &offset);
  399. break;
  400. case NODE_LIGHT_FALLOFF:
  401. svm_node_light_falloff(sd, stack, node);
  402. break;
  403. case NODE_IES:
  404. svm_node_ies(kg, sd, stack, node, &offset);
  405. break;
  406. # endif /* __EXTRA_NODES__ */
  407. #endif /* NODES_GROUP(NODE_GROUP_LEVEL_2) */
  408. #if NODES_GROUP(NODE_GROUP_LEVEL_3)
  409. case NODE_RGB_CURVES:
  410. case NODE_VECTOR_CURVES:
  411. svm_node_curves(kg, sd, stack, node, &offset);
  412. break;
  413. case NODE_TANGENT:
  414. svm_node_tangent(kg, sd, stack, node);
  415. break;
  416. case NODE_NORMAL_MAP:
  417. svm_node_normal_map(kg, sd, stack, node);
  418. break;
  419. # ifdef __EXTRA_NODES__
  420. case NODE_INVERT:
  421. svm_node_invert(sd, stack, node.y, node.z, node.w);
  422. break;
  423. case NODE_MIX:
  424. svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
  425. break;
  426. case NODE_SEPARATE_VECTOR:
  427. svm_node_separate_vector(sd, stack, node.y, node.z, node.w);
  428. break;
  429. case NODE_COMBINE_VECTOR:
  430. svm_node_combine_vector(sd, stack, node.y, node.z, node.w);
  431. break;
  432. case NODE_SEPARATE_HSV:
  433. svm_node_separate_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
  434. break;
  435. case NODE_COMBINE_HSV:
  436. svm_node_combine_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
  437. break;
  438. case NODE_VECTOR_TRANSFORM:
  439. svm_node_vector_transform(kg, sd, stack, node);
  440. break;
  441. case NODE_WIREFRAME:
  442. svm_node_wireframe(kg, sd, stack, node);
  443. break;
  444. case NODE_WAVELENGTH:
  445. svm_node_wavelength(kg, sd, stack, node.y, node.z);
  446. break;
  447. case NODE_BLACKBODY:
  448. svm_node_blackbody(kg, sd, stack, node.y, node.z);
  449. break;
  450. # endif /* __EXTRA_NODES__ */
  451. # if NODES_FEATURE(NODE_FEATURE_VOLUME)
  452. case NODE_TEX_VOXEL:
  453. svm_node_tex_voxel(kg, sd, stack, node, &offset);
  454. break;
  455. # endif /* NODES_FEATURE(NODE_FEATURE_VOLUME) */
  456. # ifdef __SHADER_RAYTRACE__
  457. case NODE_BEVEL:
  458. svm_node_bevel(kg, sd, state, stack, node);
  459. break;
  460. case NODE_AMBIENT_OCCLUSION:
  461. svm_node_ao(kg, sd, state, stack, node);
  462. break;
  463. # endif /* __SHADER_RAYTRACE__ */
  464. #endif /* NODES_GROUP(NODE_GROUP_LEVEL_3) */
  465. case NODE_END:
  466. return;
  467. default:
  468. kernel_assert(!"Unknown node type was passed to the SVM machine");
  469. return;
  470. }
  471. }
  472. }
  473. #undef NODES_GROUP
  474. #undef NODES_FEATURE
  475. CCL_NAMESPACE_END
  476. #endif /* __SVM_H__ */