svm_hsv.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_HSV_H__
  17. #define __SVM_HSV_H__
  18. CCL_NAMESPACE_BEGIN
  19. ccl_device void svm_node_hsv(
  20. KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
  21. {
  22. uint in_color_offset, fac_offset, out_color_offset;
  23. uint hue_offset, sat_offset, val_offset;
  24. decode_node_uchar4(node.y, &in_color_offset, &fac_offset, &out_color_offset, NULL);
  25. decode_node_uchar4(node.z, &hue_offset, &sat_offset, &val_offset, NULL);
  26. float fac = stack_load_float(stack, fac_offset);
  27. float3 in_color = stack_load_float3(stack, in_color_offset);
  28. float3 color = in_color;
  29. float hue = stack_load_float(stack, hue_offset);
  30. float sat = stack_load_float(stack, sat_offset);
  31. float val = stack_load_float(stack, val_offset);
  32. color = rgb_to_hsv(color);
  33. /* remember: fmod doesn't work for negative numbers here */
  34. color.x = fmodf(color.x + hue + 0.5f, 1.0f);
  35. color.y = saturate(color.y * sat);
  36. color.z *= val;
  37. color = hsv_to_rgb(color);
  38. color.x = fac * color.x + (1.0f - fac) * in_color.x;
  39. color.y = fac * color.y + (1.0f - fac) * in_color.y;
  40. color.z = fac * color.z + (1.0f - fac) * in_color.z;
  41. /* Clamp color to prevent negative values caused by oversaturation. */
  42. color.x = max(color.x, 0.0f);
  43. color.y = max(color.y, 0.0f);
  44. color.z = max(color.z, 0.0f);
  45. if (stack_valid(out_color_offset))
  46. stack_store_float3(stack, out_color_offset, color);
  47. }
  48. CCL_NAMESPACE_END
  49. #endif /* __SVM_HSV_H__ */