svm_magic.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /* Magic */
  18. ccl_device_noinline float3 svm_magic(float3 p, int n, float distortion)
  19. {
  20. float x = sinf((p.x + p.y + p.z) * 5.0f);
  21. float y = cosf((-p.x + p.y - p.z) * 5.0f);
  22. float z = -cosf((-p.x - p.y + p.z) * 5.0f);
  23. if (n > 0) {
  24. x *= distortion;
  25. y *= distortion;
  26. z *= distortion;
  27. y = -cosf(x - y + z);
  28. y *= distortion;
  29. if (n > 1) {
  30. x = cosf(x - y - z);
  31. x *= distortion;
  32. if (n > 2) {
  33. z = sinf(-x - y - z);
  34. z *= distortion;
  35. if (n > 3) {
  36. x = -cosf(-x + y - z);
  37. x *= distortion;
  38. if (n > 4) {
  39. y = -sinf(-x + y + z);
  40. y *= distortion;
  41. if (n > 5) {
  42. y = -cosf(-x + y + z);
  43. y *= distortion;
  44. if (n > 6) {
  45. x = cosf(x + y + z);
  46. x *= distortion;
  47. if (n > 7) {
  48. z = sinf(x + y - z);
  49. z *= distortion;
  50. if (n > 8) {
  51. x = -cosf(-x - y + z);
  52. x *= distortion;
  53. if (n > 9) {
  54. y = -sinf(x - y + z);
  55. y *= distortion;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. if (distortion != 0.0f) {
  67. distortion *= 2.0f;
  68. x /= distortion;
  69. y /= distortion;
  70. z /= distortion;
  71. }
  72. return make_float3(0.5f - x, 0.5f - y, 0.5f - z);
  73. }
  74. ccl_device void svm_node_tex_magic(
  75. KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
  76. {
  77. uint depth;
  78. uint scale_offset, distortion_offset, co_offset, fac_offset, color_offset;
  79. decode_node_uchar4(node.y, &depth, &color_offset, &fac_offset, NULL);
  80. decode_node_uchar4(node.z, &co_offset, &scale_offset, &distortion_offset, NULL);
  81. uint4 node2 = read_node(kg, offset);
  82. float3 co = stack_load_float3(stack, co_offset);
  83. float scale = stack_load_float_default(stack, scale_offset, node2.x);
  84. float distortion = stack_load_float_default(stack, distortion_offset, node2.y);
  85. float3 color = svm_magic(co * scale, depth, distortion);
  86. if (stack_valid(fac_offset))
  87. stack_store_float(stack, fac_offset, average(color));
  88. if (stack_valid(color_offset))
  89. stack_store_float3(stack, color_offset, color);
  90. }
  91. CCL_NAMESPACE_END