svm_ramp_util.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_UTIL_H__
  17. #define __SVM_RAMP_UTIL_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 float3
  21. rgb_ramp_lookup(const float3 *ramp, float f, bool interpolate, bool extrapolate, int table_size)
  22. {
  23. if ((f < 0.0f || f > 1.0f) && extrapolate) {
  24. float3 t0, dy;
  25. if (f < 0.0f) {
  26. t0 = ramp[0];
  27. dy = t0 - ramp[1], f = -f;
  28. }
  29. else {
  30. t0 = ramp[table_size - 1];
  31. dy = t0 - ramp[table_size - 2];
  32. f = f - 1.0f;
  33. }
  34. return t0 + dy * f * (table_size - 1);
  35. }
  36. f = clamp(f, 0.0f, 1.0f) * (table_size - 1);
  37. /* clamp int as well in case of NaN */
  38. int i = clamp(float_to_int(f), 0, table_size - 1);
  39. float t = f - (float)i;
  40. float3 result = ramp[i];
  41. if (interpolate && t > 0.0f) {
  42. result = (1.0f - t) * result + t * ramp[i + 1];
  43. }
  44. return result;
  45. }
  46. ccl_device float float_ramp_lookup(
  47. const float *ramp, float f, bool interpolate, bool extrapolate, int table_size)
  48. {
  49. if ((f < 0.0f || f > 1.0f) && extrapolate) {
  50. float t0, dy;
  51. if (f < 0.0f) {
  52. t0 = ramp[0];
  53. dy = t0 - ramp[1], f = -f;
  54. }
  55. else {
  56. t0 = ramp[table_size - 1];
  57. dy = t0 - ramp[table_size - 2];
  58. f = f - 1.0f;
  59. }
  60. return t0 + dy * f * (table_size - 1);
  61. }
  62. f = clamp(f, 0.0f, 1.0f) * (table_size - 1);
  63. /* clamp int as well in case of NaN */
  64. int i = clamp(float_to_int(f), 0, table_size - 1);
  65. float t = f - (float)i;
  66. float result = ramp[i];
  67. if (interpolate && t > 0.0f) {
  68. result = (1.0f - t) * result + t * ramp[i + 1];
  69. }
  70. return result;
  71. }
  72. CCL_NAMESPACE_END
  73. #endif /* __SVM_RAMP_UTIL_H__ */