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

geom_motion_curve.h « geom « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7380c506bf431550daa526ab1ea768981ad786ba (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

CCL_NAMESPACE_BEGIN

/* Motion Curve Primitive
 *
 * These are stored as regular curves, plus extra positions and radii at times
 * other than the frame center. Computing the curve keys at a given ray time is
 * a matter of interpolation of the two steps between which the ray time lies.
 *
 * The extra curve keys are stored as ATTR_STD_MOTION_VERTEX_POSITION.
 */

#ifdef __HAIR__

ccl_device_inline int find_attribute_curve_motion(KernelGlobals *kg,
                                                  int object,
                                                  uint id,
                                                  AttributeElement *elem)
{
  /* todo: find a better (faster) solution for this, maybe store offset per object.
   *
   * NOTE: currently it's not a bottleneck because in test scenes the loop below runs
   * zero iterations and rendering is really slow with motion curves. For until other
   * areas are speed up it's probably not so crucial to optimize this out.
   */
  uint attr_offset = object_attribute_map_offset(kg, object) + ATTR_PRIM_CURVE;
  uint4 attr_map = kernel_tex_fetch(__attributes_map, attr_offset);

  while (attr_map.x != id) {
    attr_offset += ATTR_PRIM_TYPES;
    attr_map = kernel_tex_fetch(__attributes_map, attr_offset);
  }

  *elem = (AttributeElement)attr_map.y;

  /* return result */
  return (attr_map.y == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : (int)attr_map.z;
}

ccl_device_inline void motion_curve_keys_for_step(KernelGlobals *kg,
                                                  int offset,
                                                  int numkeys,
                                                  int numsteps,
                                                  int step,
                                                  int k0,
                                                  int k1,
                                                  float4 keys[2])
{
  if (step == numsteps) {
    /* center step: regular key location */
    keys[0] = kernel_tex_fetch(__curve_keys, k0);
    keys[1] = kernel_tex_fetch(__curve_keys, k1);
  }
  else {
    /* center step is not stored in this array */
    if (step > numsteps)
      step--;

    offset += step * numkeys;

    keys[0] = kernel_tex_fetch(__attributes_float3, offset + k0);
    keys[1] = kernel_tex_fetch(__attributes_float3, offset + k1);
  }
}

/* return 2 curve key locations */
ccl_device_inline void motion_curve_keys(
    KernelGlobals *kg, int object, int prim, float time, int k0, int k1, float4 keys[2])
{
  /* get motion info */
  int numsteps, numkeys;
  object_motion_info(kg, object, &numsteps, NULL, &numkeys);

  /* figure out which steps we need to fetch and their interpolation factor */
  int maxstep = numsteps * 2;
  int step = min((int)(time * maxstep), maxstep - 1);
  float t = time * maxstep - step;

  /* find attribute */
  AttributeElement elem;
  int offset = find_attribute_curve_motion(kg, object, ATTR_STD_MOTION_VERTEX_POSITION, &elem);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* fetch key coordinates */
  float4 next_keys[2];

  motion_curve_keys_for_step(kg, offset, numkeys, numsteps, step, k0, k1, keys);
  motion_curve_keys_for_step(kg, offset, numkeys, numsteps, step + 1, k0, k1, next_keys);

  /* interpolate between steps */
  keys[0] = (1.0f - t) * keys[0] + t * next_keys[0];
  keys[1] = (1.0f - t) * keys[1] + t * next_keys[1];
}

ccl_device_inline void motion_cardinal_curve_keys_for_step(KernelGlobals *kg,
                                                           int offset,
                                                           int numkeys,
                                                           int numsteps,
                                                           int step,
                                                           int k0,
                                                           int k1,
                                                           int k2,
                                                           int k3,
                                                           float4 keys[4])
{
  if (step == numsteps) {
    /* center step: regular key location */
    keys[0] = kernel_tex_fetch(__curve_keys, k0);
    keys[1] = kernel_tex_fetch(__curve_keys, k1);
    keys[2] = kernel_tex_fetch(__curve_keys, k2);
    keys[3] = kernel_tex_fetch(__curve_keys, k3);
  }
  else {
    /* center step is not stored in this array */
    if (step > numsteps)
      step--;

    offset += step * numkeys;

    keys[0] = kernel_tex_fetch(__attributes_float3, offset + k0);
    keys[1] = kernel_tex_fetch(__attributes_float3, offset + k1);
    keys[2] = kernel_tex_fetch(__attributes_float3, offset + k2);
    keys[3] = kernel_tex_fetch(__attributes_float3, offset + k3);
  }
}

/* return 2 curve key locations */
ccl_device_inline void motion_cardinal_curve_keys(KernelGlobals *kg,
                                                  int object,
                                                  int prim,
                                                  float time,
                                                  int k0,
                                                  int k1,
                                                  int k2,
                                                  int k3,
                                                  float4 keys[4])
{
  /* get motion info */
  int numsteps, numkeys;
  object_motion_info(kg, object, &numsteps, NULL, &numkeys);

  /* figure out which steps we need to fetch and their interpolation factor */
  int maxstep = numsteps * 2;
  int step = min((int)(time * maxstep), maxstep - 1);
  float t = time * maxstep - step;

  /* find attribute */
  AttributeElement elem;
  int offset = find_attribute_curve_motion(kg, object, ATTR_STD_MOTION_VERTEX_POSITION, &elem);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* fetch key coordinates */
  float4 next_keys[4];

  motion_cardinal_curve_keys_for_step(kg, offset, numkeys, numsteps, step, k0, k1, k2, k3, keys);
  motion_cardinal_curve_keys_for_step(
      kg, offset, numkeys, numsteps, step + 1, k0, k1, k2, k3, next_keys);

  /* interpolate between steps */
  keys[0] = (1.0f - t) * keys[0] + t * next_keys[0];
  keys[1] = (1.0f - t) * keys[1] + t * next_keys[1];
  keys[2] = (1.0f - t) * keys[2] + t * next_keys[2];
  keys[3] = (1.0f - t) * keys[3] + t * next_keys[3];
}

#  if defined(__KERNEL_AVX2__) && defined(__KERNEL_SSE__)
/* Similar to above, but returns keys as pair of two AVX registers with each
 * holding two float4.
 */
ccl_device_inline void motion_cardinal_curve_keys_avx(KernelGlobals *kg,
                                                      int object,
                                                      int prim,
                                                      float time,
                                                      int k0,
                                                      int k1,
                                                      int k2,
                                                      int k3,
                                                      avxf *out_keys_0_1,
                                                      avxf *out_keys_2_3)
{
  /* Get motion info. */
  int numsteps, numkeys;
  object_motion_info(kg, object, &numsteps, NULL, &numkeys);

  /* Figure out which steps we need to fetch and their interpolation factor. */
  int maxstep = numsteps * 2;
  int step = min((int)(time * maxstep), maxstep - 1);
  float t = time * maxstep - step;

  /* Find attribute. */
  AttributeElement elem;
  int offset = find_attribute_curve_motion(kg, object, ATTR_STD_MOTION_VERTEX_POSITION, &elem);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* Fetch key coordinates. */
  float4 next_keys[4];
  float4 keys[4];
  motion_cardinal_curve_keys_for_step(kg, offset, numkeys, numsteps, step, k0, k1, k2, k3, keys);
  motion_cardinal_curve_keys_for_step(
      kg, offset, numkeys, numsteps, step + 1, k0, k1, k2, k3, next_keys);

  const avxf keys_0_1 = avxf(keys[0].m128, keys[1].m128);
  const avxf keys_2_3 = avxf(keys[2].m128, keys[3].m128);
  const avxf next_keys_0_1 = avxf(next_keys[0].m128, next_keys[1].m128);
  const avxf next_keys_2_3 = avxf(next_keys[2].m128, next_keys[3].m128);

  /* Interpolate between steps. */
  *out_keys_0_1 = (1.0f - t) * keys_0_1 + t * next_keys_0_1;
  *out_keys_2_3 = (1.0f - t) * keys_2_3 + t * next_keys_2_3;
}
#  endif

#endif

CCL_NAMESPACE_END