Welcome to mirror list, hosted at ThFree Co, Russian Federation.

map_range.h « svm « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea85bc43b745db7cc4db69716cf336347ab5b6e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#pragma once

CCL_NAMESPACE_BEGIN

/* Map Range Node */

ccl_device_inline float smootherstep(float edge0, float edge1, float x)
{
  x = clamp(safe_divide((x - edge0), (edge1 - edge0)), 0.0f, 1.0f);
  return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f);
}

ccl_device_noinline int svm_node_map_range(KernelGlobals kg,
                                           ccl_private ShaderData *sd,
                                           ccl_private float *stack,
                                           uint value_stack_offset,
                                           uint parameters_stack_offsets,
                                           uint results_stack_offsets,
                                           int offset)
{
  uint from_min_stack_offset, from_max_stack_offset, to_min_stack_offset, to_max_stack_offset;
  uint type_stack_offset, steps_stack_offset, result_stack_offset;
  svm_unpack_node_uchar4(parameters_stack_offsets,
                         &from_min_stack_offset,
                         &from_max_stack_offset,
                         &to_min_stack_offset,
                         &to_max_stack_offset);
  svm_unpack_node_uchar3(
      results_stack_offsets, &type_stack_offset, &steps_stack_offset, &result_stack_offset);

  uint4 defaults = read_node(kg, &offset);
  uint4 defaults2 = read_node(kg, &offset);

  float value = stack_load_float(stack, value_stack_offset);
  float from_min = stack_load_float_default(stack, from_min_stack_offset, defaults.x);
  float from_max = stack_load_float_default(stack, from_max_stack_offset, defaults.y);
  float to_min = stack_load_float_default(stack, to_min_stack_offset, defaults.z);
  float to_max = stack_load_float_default(stack, to_max_stack_offset, defaults.w);
  float steps = stack_load_float_default(stack, steps_stack_offset, defaults2.x);

  float result;

  if (from_max != from_min) {
    float factor = value;
    switch (type_stack_offset) {
      default:
      case NODE_MAP_RANGE_LINEAR:
        factor = (value - from_min) / (from_max - from_min);
        break;
      case NODE_MAP_RANGE_STEPPED: {
        factor = (value - from_min) / (from_max - from_min);
        factor = (steps > 0.0f) ? floorf(factor * (steps + 1.0f)) / steps : 0.0f;
        break;
      }
      case NODE_MAP_RANGE_SMOOTHSTEP: {
        factor = (from_min > from_max) ? 1.0f - smoothstep(from_max, from_min, factor) :
                                         smoothstep(from_min, from_max, factor);
        break;
      }
      case NODE_MAP_RANGE_SMOOTHERSTEP: {
        factor = (from_min > from_max) ? 1.0f - smootherstep(from_max, from_min, factor) :
                                         smootherstep(from_min, from_max, factor);
        break;
      }
    }
    result = to_min + factor * (to_max - to_min);
  }
  else {
    result = 0.0f;
  }
  stack_store_float(stack, result_stack_offset, result);
  return offset;
}

ccl_device_noinline int svm_node_vector_map_range(KernelGlobals kg,
                                                  ccl_private ShaderData *sd,
                                                  ccl_private float *stack,
                                                  uint value_stack_offset,
                                                  uint parameters_stack_offsets,
                                                  uint results_stack_offsets,
                                                  int offset)
{
  uint from_min_stack_offset, from_max_stack_offset, to_min_stack_offset, to_max_stack_offset;
  uint steps_stack_offset, clamp_stack_offset, range_type_stack_offset, result_stack_offset;
  svm_unpack_node_uchar4(parameters_stack_offsets,
                         &from_min_stack_offset,
                         &from_max_stack_offset,
                         &to_min_stack_offset,
                         &to_max_stack_offset);
  svm_unpack_node_uchar4(results_stack_offsets,
                         &steps_stack_offset,
                         &clamp_stack_offset,
                         &range_type_stack_offset,
                         &result_stack_offset);

  float3 value = stack_load_float3(stack, value_stack_offset);
  float3 from_min = stack_load_float3(stack, from_min_stack_offset);
  float3 from_max = stack_load_float3(stack, from_max_stack_offset);
  float3 to_min = stack_load_float3(stack, to_min_stack_offset);
  float3 to_max = stack_load_float3(stack, to_max_stack_offset);
  float3 steps = stack_load_float3(stack, steps_stack_offset);

  int type = range_type_stack_offset;
  int use_clamp = (type == NODE_MAP_RANGE_SMOOTHSTEP || type == NODE_MAP_RANGE_SMOOTHERSTEP) ?
                      0 :
                      clamp_stack_offset;
  float3 result;
  float3 factor = value;
  switch (range_type_stack_offset) {
    default:
    case NODE_MAP_RANGE_LINEAR:
      factor = safe_divide((value - from_min), (from_max - from_min));
      break;
    case NODE_MAP_RANGE_STEPPED: {
      factor = safe_divide((value - from_min), (from_max - from_min));
      factor = make_float3((steps.x > 0.0f) ? floorf(factor.x * (steps.x + 1.0f)) / steps.x : 0.0f,
                           (steps.y > 0.0f) ? floorf(factor.y * (steps.y + 1.0f)) / steps.y : 0.0f,
                           (steps.z > 0.0f) ? floorf(factor.z * (steps.z + 1.0f)) / steps.z :
                                              0.0f);
      break;
    }
    case NODE_MAP_RANGE_SMOOTHSTEP: {
      factor = safe_divide((value - from_min), (from_max - from_min));
      factor = clamp(factor, zero_float3(), one_float3());
      factor = (make_float3(3.0f, 3.0f, 3.0f) - 2.0f * factor) * (factor * factor);
      break;
    }
    case NODE_MAP_RANGE_SMOOTHERSTEP: {
      factor = safe_divide((value - from_min), (from_max - from_min));
      factor = clamp(factor, zero_float3(), one_float3());
      factor = factor * factor * factor * (factor * (factor * 6.0f - 15.0f) + 10.0f);
      break;
    }
  }
  result = to_min + factor * (to_max - to_min);
  if (use_clamp > 0) {
    result.x = (to_min.x > to_max.x) ? clamp(result.x, to_max.x, to_min.x) :
                                       clamp(result.x, to_min.x, to_max.x);
    result.y = (to_min.y > to_max.y) ? clamp(result.y, to_max.y, to_min.y) :
                                       clamp(result.y, to_min.y, to_max.y);
    result.z = (to_min.z > to_max.z) ? clamp(result.z, to_max.z, to_min.z) :
                                       clamp(result.z, to_min.z, to_max.z);
  }

  stack_store_float3(stack, result_stack_offset, result);
  return offset;
}

CCL_NAMESPACE_END