svm_ramp.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_RAMP_H__
  17. #define __SVM_RAMP_H__
  18. CCL_NAMESPACE_BEGIN
  19. /* NOTE: svm_ramp.h, svm_ramp_util.h and node_ramp_util.h must stay consistent */
  20. ccl_device_inline float4 rgb_ramp_lookup(
  21. KernelGlobals *kg, int offset, float f, bool interpolate, bool extrapolate, int table_size)
  22. {
  23. if ((f < 0.0f || f > 1.0f) && extrapolate) {
  24. float4 t0, dy;
  25. if (f < 0.0f) {
  26. t0 = fetch_node_float(kg, offset);
  27. dy = t0 - fetch_node_float(kg, offset + 1);
  28. f = -f;
  29. }
  30. else {
  31. t0 = fetch_node_float(kg, offset + table_size - 1);
  32. dy = t0 - fetch_node_float(kg, offset + table_size - 2);
  33. f = f - 1.0f;
  34. }
  35. return t0 + dy * f * (table_size - 1);
  36. }
  37. f = saturate(f) * (table_size - 1);
  38. /* clamp int as well in case of NaN */
  39. int i = clamp(float_to_int(f), 0, table_size - 1);
  40. float t = f - (float)i;
  41. float4 a = fetch_node_float(kg, offset + i);
  42. if (interpolate && t > 0.0f)
  43. a = (1.0f - t) * a + t * fetch_node_float(kg, offset + i + 1);
  44. return a;
  45. }
  46. ccl_device void svm_node_rgb_ramp(
  47. KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
  48. {
  49. uint fac_offset, color_offset, alpha_offset;
  50. uint interpolate = node.z;
  51. decode_node_uchar4(node.y, &fac_offset, &color_offset, &alpha_offset, NULL);
  52. uint table_size = read_node(kg, offset).x;
  53. float fac = stack_load_float(stack, fac_offset);
  54. float4 color = rgb_ramp_lookup(kg, *offset, fac, interpolate, false, table_size);
  55. if (stack_valid(color_offset))
  56. stack_store_float3(stack, color_offset, float4_to_float3(color));
  57. if (stack_valid(alpha_offset))
  58. stack_store_float(stack, alpha_offset, color.w);
  59. *offset += table_size;
  60. }
  61. ccl_device void svm_node_curves(
  62. KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
  63. {
  64. uint fac_offset, color_offset, out_offset;
  65. decode_node_uchar4(node.y, &fac_offset, &color_offset, &out_offset, NULL);
  66. uint table_size = read_node(kg, offset).x;
  67. float fac = stack_load_float(stack, fac_offset);
  68. float3 color = stack_load_float3(stack, color_offset);
  69. const float min_x = __int_as_float(node.z), max_x = __int_as_float(node.w);
  70. const float range_x = max_x - min_x;
  71. const float3 relpos = (color - make_float3(min_x, min_x, min_x)) / range_x;
  72. float r = rgb_ramp_lookup(kg, *offset, relpos.x, true, true, table_size).x;
  73. float g = rgb_ramp_lookup(kg, *offset, relpos.y, true, true, table_size).y;
  74. float b = rgb_ramp_lookup(kg, *offset, relpos.z, true, true, table_size).z;
  75. color = (1.0f - fac) * color + fac * make_float3(r, g, b);
  76. stack_store_float3(stack, out_offset, color);
  77. *offset += table_size;
  78. }
  79. CCL_NAMESPACE_END
  80. #endif /* __SVM_RAMP_H__ */