svm_checker.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. /* Checker */
  18. ccl_device_noinline float svm_checker(float3 p)
  19. {
  20. /* avoid precision issues on unit coordinates */
  21. p.x = (p.x + 0.000001f) * 0.999999f;
  22. p.y = (p.y + 0.000001f) * 0.999999f;
  23. p.z = (p.z + 0.000001f) * 0.999999f;
  24. int xi = abs(float_to_int(floorf(p.x)));
  25. int yi = abs(float_to_int(floorf(p.y)));
  26. int zi = abs(float_to_int(floorf(p.z)));
  27. return ((xi % 2 == yi % 2) == (zi % 2)) ? 1.0f : 0.0f;
  28. }
  29. ccl_device void svm_node_tex_checker(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
  30. {
  31. uint co_offset, color1_offset, color2_offset, scale_offset;
  32. uint color_offset, fac_offset;
  33. decode_node_uchar4(node.y, &co_offset, &color1_offset, &color2_offset, &scale_offset);
  34. decode_node_uchar4(node.z, &color_offset, &fac_offset, NULL, NULL);
  35. float3 co = stack_load_float3(stack, co_offset);
  36. float3 color1 = stack_load_float3(stack, color1_offset);
  37. float3 color2 = stack_load_float3(stack, color2_offset);
  38. float scale = stack_load_float_default(stack, scale_offset, node.w);
  39. float f = svm_checker(co * scale);
  40. if (stack_valid(color_offset))
  41. stack_store_float3(stack, color_offset, (f == 1.0f) ? color1 : color2);
  42. if (stack_valid(fac_offset))
  43. stack_store_float(stack, fac_offset, f);
  44. }
  45. CCL_NAMESPACE_END