slm: OpenCL code base  0.1
essentials.cl
Go to the documentation of this file.
1 
10 
11 #ifdef C_ORDER
12 static float2 speed_interpolator(float2 vec, __global const float2 *uv_array)
29 {
30  const uint x_lft = (uint)(max(0.0f, vec[0]+PAD_WIDTH_PP5));
31  const uint y_dwn = (uint)(max(0.0f, vec[1]+PAD_WIDTH_PP5));
32  const uint x_rgt = min( NX_PADDED-1, x_lft+1u );
33  const uint y_upp = min( NY_PADDED-1, y_dwn+1u );
34 
35  // Get the fractional displacement of the sample point from the down-left vertex
36  const float rx_weight = vec[0]-(float)x_lft+(float)PAD_WIDTH;
37  const float lx_weight = 1.0f-rx_weight;
38  const float uy_weight = vec[1]-(float)y_dwn+(float)PAD_WIDTH;
39  const float dy_weight = 1.0f-uy_weight;
40 
41  // Use to weight the four corner values...
42  const float2 uv_dwn = uv_array[NY_PADDED*x_lft+y_dwn]*lx_weight
43  + uv_array[NY_PADDED*x_rgt+y_dwn]*rx_weight;
44  const float2 uv_upp = uv_array[NY_PADDED*x_lft+y_upp]*lx_weight
45  + uv_array[NY_PADDED*x_rgt+y_upp]*rx_weight;
46  // Returns:
47  // interpolated 2d unit speed vector:
48  return fast_normalize(uv_dwn*dy_weight+uv_upp*uy_weight);
49 }
50 
62 static inline uint get_array_idx(float2 vec) {
63  return ( min( NY_PADDED-1, (uint)(max(0.0f, vec[1]+PAD_WIDTH_PP5)) )
64  +NY_PADDED*min( NX_PADDED-1, (uint)(max(0.0f, vec[0]+PAD_WIDTH_PP5)) ) );
65 }
66 #endif
67 
68 #ifdef F_ORDER
69 static float2 speed_interpolator(float2 vec, __global const float2 *uv_array)
82 {
83  const uint x_lft = (uint)(max(0.0f, vec[0]+PAD_WIDTH_PP5));
84  const uint y_dwn = (uint)(max(0.0f, vec[1]+PAD_WIDTH_PP5));
85  const uint x_rgt = min( NX_PADDED-1, x_lft+1u );
86  const uint y_upp = min( NY_PADDED-1, y_dwn+1u );
87 
88  // Get the fractional displacement of the sample point from the down-left vertex
89  const float rx_weight = vec[0]-(float)x_lft+(float)PAD_WIDTH;
90  const float lx_weight = 1.0f-rx_weight;
91  const float uy_weight = vec[1]-(float)y_dwn+(float)PAD_WIDTH;
92  const float dy_weight = 1.0f-uy_weight;
93 
94  // Use to weight the four corner values...
95  const float2 uv_dwn = uv_array[x_lft+NX_PADDED*y_dwn]*lx_weight
96  + uv_array[x_rgt+NX_PADDED*y_dwn]*rx_weight;
97  const float2 uv_upp = uv_array[x_lft+NX_PADDED*y_upp]*lx_weight
98  + uv_array[x_rgt+NX_PADDED*y_upp]*rx_weight;
99  // Returns:
100  // interpolated 2d unit speed vector:
101  return fast_normalize(uv_dwn*dy_weight+uv_upp*uy_weight);
102 }
103 
115 static inline uint get_array_idx(float2 vec) {
116  return ( min( NX_PADDED-1, (uint)(max(0.0f, vec[0]+PAD_WIDTH_PP5)) )
117  +NX_PADDED*min( NY_PADDED-1, (uint)(max(0.0f, vec[1]+PAD_WIDTH_PP5)) ) );
118 }
119 #endif
120 
131 static char2 compress(float2 raw_vector) {
132  return (char2)(raw_vector[0]*TRAJECTORY_RESOLUTION,
133  raw_vector[1]*TRAJECTORY_RESOLUTION);
134 }
135 
146 static float2 uncompress(char2 compressed_vector) {
147  return ((float2)(compressed_vector[0],
148  compressed_vector[1]))/TRAJECTORY_RESOLUTION;
149 }
150 
164 static float2 approximate(float2 raw_position) {
165  return ((float2)(
166  (char)(raw_position[0]*TRAJECTORY_RESOLUTION),
167  (char)(raw_position[1]*TRAJECTORY_RESOLUTION)
169 }
170 
181 static inline float dt_to_nearest_edge(float x,float u) {
182  float dt = 0.0f;
183  // going right?
184  dt = select(dt,((int)(x+1.5f)-(x+0.5f))/u, isgreater(u,0.0f));
185  // going left?
186  dt = select(dt,-((x+0.5f)-(int)(x+0.5f))/u, isless(u,0.0f));
187  // if dt=0 stuck
188  return dt;
189 }
static float2 speed_interpolator(float2 vec, __global const float2 *uv_array)
Bilinearly interpolate a velocity vector (choice of row-major or column-major arrays).
Definition: essentials.cl:28
#define NY_PADDED
Definition: info.h:70
#define NX_PADDED
Definition: info.h:69
static float2 uncompress(char2 compressed_vector)
Unsquish a byte vector back to a float vector.
Definition: essentials.cl:146
static char2 compress(float2 raw_vector)
Squish a float vector into a byte vector for O(<1 pixel) trajectory steps Achieved through scaling by...
Definition: essentials.cl:131
#define TRAJECTORY_RESOLUTION
Definition: info.h:50
static float dt_to_nearest_edge(float x, float u)
In Euler streamline integration (which is the last step), this function provides the delta time requi...
Definition: essentials.cl:181
static float2 approximate(float2 raw_position)
Approximate a float vector at the resolution provided by a scaled byte vector.
Definition: essentials.cl:164
#define PAD_WIDTH_PP5
Definition: info.h:64
static uint get_array_idx(float2 vec)
Compute the array index of the padded grid pixel pointed to by a float2 grid position vector (choice ...
Definition: essentials.cl:62
#define PAD_WIDTH
Definition: info.h:63