rs_math.rsh revision eb0dfedaf861049858a365d56d67cc85bfcad3c3
1#ifndef __RS_MATH_RSH__
2#define __RS_MATH_RSH__
3
4/**
5 * Copy reference to the specified object.
6 *
7 * @param dst
8 * @param src
9 */
10extern void __attribute__((overloadable))
11    rsSetObject(rs_element *dst, rs_element src);
12extern void __attribute__((overloadable))
13    rsSetObject(rs_type *dst, rs_type src);
14extern void __attribute__((overloadable))
15    rsSetObject(rs_allocation *dst, rs_allocation src);
16extern void __attribute__((overloadable))
17    rsSetObject(rs_sampler *dst, rs_sampler src);
18extern void __attribute__((overloadable))
19    rsSetObject(rs_script *dst, rs_script src);
20extern void __attribute__((overloadable))
21    rsSetObject(rs_mesh *dst, rs_mesh src);
22extern void __attribute__((overloadable))
23    rsSetObject(rs_program_fragment *dst, rs_program_fragment src);
24extern void __attribute__((overloadable))
25    rsSetObject(rs_program_vertex *dst, rs_program_vertex src);
26extern void __attribute__((overloadable))
27    rsSetObject(rs_program_raster *dst, rs_program_raster src);
28extern void __attribute__((overloadable))
29    rsSetObject(rs_program_store *dst, rs_program_store src);
30extern void __attribute__((overloadable))
31    rsSetObject(rs_font *dst, rs_font src);
32
33/**
34 * Sets the object to NULL.
35 *
36 * @return bool
37 */
38extern void __attribute__((overloadable))
39    rsClearObject(rs_element *dst);
40extern void __attribute__((overloadable))
41    rsClearObject(rs_type *dst);
42extern void __attribute__((overloadable))
43    rsClearObject(rs_allocation *dst);
44extern void __attribute__((overloadable))
45    rsClearObject(rs_sampler *dst);
46extern void __attribute__((overloadable))
47    rsClearObject(rs_script *dst);
48extern void __attribute__((overloadable))
49    rsClearObject(rs_mesh *dst);
50extern void __attribute__((overloadable))
51    rsClearObject(rs_program_fragment *dst);
52extern void __attribute__((overloadable))
53    rsClearObject(rs_program_vertex *dst);
54extern void __attribute__((overloadable))
55    rsClearObject(rs_program_raster *dst);
56extern void __attribute__((overloadable))
57    rsClearObject(rs_program_store *dst);
58extern void __attribute__((overloadable))
59    rsClearObject(rs_font *dst);
60
61/**
62 * Tests if the object is valid.  Returns true if the object is valid, false if
63 * it is NULL.
64 *
65 * @return bool
66 */
67extern bool __attribute__((overloadable))
68    rsIsObject(rs_element);
69extern bool __attribute__((overloadable))
70    rsIsObject(rs_type);
71extern bool __attribute__((overloadable))
72    rsIsObject(rs_allocation);
73extern bool __attribute__((overloadable))
74    rsIsObject(rs_sampler);
75extern bool __attribute__((overloadable))
76    rsIsObject(rs_script);
77extern bool __attribute__((overloadable))
78    rsIsObject(rs_mesh);
79extern bool __attribute__((overloadable))
80    rsIsObject(rs_program_fragment);
81extern bool __attribute__((overloadable))
82    rsIsObject(rs_program_vertex);
83extern bool __attribute__((overloadable))
84    rsIsObject(rs_program_raster);
85extern bool __attribute__((overloadable))
86    rsIsObject(rs_program_store);
87extern bool __attribute__((overloadable))
88    rsIsObject(rs_font);
89
90
91/**
92 * Returns the Allocation for a given pointer.  The pointer should point within
93 * a valid allocation.  The results are undefined if the pointer is not from a
94 * valid allocation.
95 */
96extern rs_allocation __attribute__((overloadable))
97    rsGetAllocation(const void *);
98
99/**
100 * Query the dimension of an allocation.
101 *
102 * @return uint32_t The X dimension of the allocation.
103 */
104extern uint32_t __attribute__((overloadable))
105    rsAllocationGetDimX(rs_allocation);
106
107/**
108 * Query the dimension of an allocation.
109 *
110 * @return uint32_t The Y dimension of the allocation.
111 */
112extern uint32_t __attribute__((overloadable))
113    rsAllocationGetDimY(rs_allocation);
114
115/**
116 * Query the dimension of an allocation.
117 *
118 * @return uint32_t The Z dimension of the allocation.
119 */
120extern uint32_t __attribute__((overloadable))
121    rsAllocationGetDimZ(rs_allocation);
122
123/**
124 * Query an allocation for the presence of more than one LOD.
125 *
126 * @return uint32_t Returns 1 if more than one LOD is present, 0 otherwise.
127 */
128extern uint32_t __attribute__((overloadable))
129    rsAllocationGetDimLOD(rs_allocation);
130
131/**
132 * Query an allocation for the presence of more than one face.
133 *
134 * @return uint32_t Returns 1 if more than one face is present, 0 otherwise.
135 */
136extern uint32_t __attribute__((overloadable))
137    rsAllocationGetDimFaces(rs_allocation);
138
139/**
140 * Copy part of an allocation from another allocation.
141 *
142 * @param dstAlloc Allocation to copy data into.
143 * @param dstOff The offset of the first element to be copied in
144 *               the destination allocation.
145 * @param dstMip Mip level in the destination allocation.
146 * @param count The number of elements to be copied.
147 * @param srcAlloc The source data allocation.
148 * @param srcOff The offset of the first element in data to be
149 *               copied in the source allocation.
150 * @param srcMip Mip level in the source allocation.
151 */
152extern void __attribute__((overloadable))
153    rsAllocationCopy1DRange(rs_allocation dstAlloc,
154                            uint32_t dstOff, uint32_t dstMip,
155                            uint32_t count,
156                            rs_allocation srcAlloc,
157                            uint32_t srcOff, uint32_t srcMip);
158
159/**
160 * Copy a rectangular region into the allocation from another
161 * allocation.
162 *
163 * @param dstAlloc allocation to copy data into.
164 * @param dstXoff X offset of the region to update in the
165 *                destination allocation.
166 * @param dstYoff Y offset of the region to update in the
167 *                destination allocation.
168 * @param dstMip Mip level in the destination allocation.
169 * @param dstFace Cubemap face of the destination allocation,
170 *                ignored for allocations that aren't cubemaps.
171 * @param width Width of the incoming region to update.
172 * @param height Height of the incoming region to update.
173 * @param srcAlloc The source data allocation.
174 * @param srcXoff X offset in data of the source allocation.
175 * @param srcYoff Y offset in data of the source allocation.
176 * @param srcMip Mip level in the source allocation.
177 * @param srcFace Cubemap face of the source allocation,
178 *                ignored for allocations that aren't cubemaps.
179 */
180extern void __attribute__((overloadable))
181    rsAllocationCopy2DRange(rs_allocation dstAlloc,
182                            uint32_t dstXoff, uint32_t dstYoff,
183                            uint32_t dstMip,
184                            rs_allocation_cubemap_face dstFace,
185                            uint32_t width, uint32_t height,
186                            rs_allocation srcAlloc,
187                            uint32_t srcXoff, uint32_t srcYoff,
188                            uint32_t srcMip,
189                            rs_allocation_cubemap_face srcFace);
190
191// Extract a single element from an allocation.
192extern const void * __attribute__((overloadable))
193    rsGetElementAt(rs_allocation, uint32_t x);
194extern const void * __attribute__((overloadable))
195    rsGetElementAt(rs_allocation, uint32_t x, uint32_t y);
196extern const void * __attribute__((overloadable))
197    rsGetElementAt(rs_allocation, uint32_t x, uint32_t y, uint32_t z);
198
199// Return a random value between 0 (or min_value) and max_malue.
200extern int __attribute__((overloadable))
201    rsRand(int max_value);
202extern int __attribute__((overloadable))
203    rsRand(int min_value, int max_value);
204extern float __attribute__((overloadable))
205    rsRand(float max_value);
206extern float __attribute__((overloadable))
207    rsRand(float min_value, float max_value);
208
209// return the fractional part of a float
210// min(v - ((int)floor(v)), 0x1.fffffep-1f);
211extern float __attribute__((overloadable))
212    rsFrac(float);
213
214// Send a message back to the client.  Will not block and returns true
215// if the message was sendable and false if the fifo was full.
216// A message ID is required.  Data payload is optional.
217extern bool __attribute__((overloadable))
218    rsSendToClient(int cmdID);
219extern bool __attribute__((overloadable))
220    rsSendToClient(int cmdID, const void *data, uint len);
221
222// Send a message back to the client, blocking until the message is queued.
223// A message ID is required.  Data payload is optional.
224extern void __attribute__((overloadable))
225    rsSendToClientBlocking(int cmdID);
226extern void __attribute__((overloadable))
227    rsSendToClientBlocking(int cmdID, const void *data, uint len);
228
229
230// Script to Script
231enum rs_for_each_strategy {
232    RS_FOR_EACH_STRATEGY_SERIAL,
233    RS_FOR_EACH_STRATEGY_DONT_CARE,
234    RS_FOR_EACH_STRATEGY_DST_LINEAR,
235    RS_FOR_EACH_STRATEGY_TILE_SMALL,
236    RS_FOR_EACH_STRATEGY_TILE_MEDIUM,
237    RS_FOR_EACH_STRATEGY_TILE_LARGE
238};
239
240typedef struct rs_script_call {
241    enum rs_for_each_strategy strategy;
242    uint32_t xStart;
243    uint32_t xEnd;
244    uint32_t yStart;
245    uint32_t yEnd;
246    uint32_t zStart;
247    uint32_t zEnd;
248    uint32_t arrayStart;
249    uint32_t arrayEnd;
250} rs_script_call_t;
251
252#if !defined(RS_VERSION) || (RS_VERSION < 14)
253extern void __attribute__((overloadable))
254    rsForEach(rs_script script, rs_allocation input,
255              rs_allocation output, const void * usrData,
256              const rs_script_call_t *);
257
258extern void __attribute__((overloadable))
259    rsForEach(rs_script script, rs_allocation input,
260              rs_allocation output, const void * usrData);
261#else
262extern void __attribute__((overloadable))
263    rsForEach(rs_script script, rs_allocation input, rs_allocation output);
264
265extern void __attribute__((overloadable))
266    rsForEach(rs_script script, rs_allocation input, rs_allocation output,
267              const void * usrData, size_t usrDataLen);
268
269extern void __attribute__((overloadable))
270    rsForEach(rs_script script, rs_allocation input, rs_allocation output,
271              const void * usrData, size_t usrDataLen, const rs_script_call_t *);
272#endif
273
274
275/**
276 * Atomic add one to the value at addr.
277 * Equal to rsAtomicAdd(addr, 1)
278 *
279 * @param addr Address of value to increment
280 *
281 * @return old value
282 */
283extern int32_t __attribute__((overloadable))
284    rsAtomicInc(volatile int32_t* addr);
285/**
286 * Atomic add one to the value at addr.
287 * Equal to rsAtomicAdd(addr, 1)
288 *
289 * @param addr Address of value to increment
290 *
291 * @return old value
292 */
293extern uint32_t __attribute__((overloadable))
294    rsAtomicInc(volatile uint32_t* addr);
295
296/**
297 * Atomic subtract one from the value at addr. Equal to rsAtomicSub(addr, 1)
298 *
299 * @param addr Address of value to decrement
300 *
301 * @return old value
302 */
303extern int32_t __attribute__((overloadable))
304    rsAtomicDec(volatile int32_t* addr);
305/**
306 * Atomic subtract one from the value at addr. Equal to rsAtomicSub(addr, 1)
307 *
308 * @param addr Address of value to decrement
309 *
310 * @return old value
311 */
312extern uint32_t __attribute__((overloadable))
313    rsAtomicDec(volatile uint32_t* addr);
314
315/**
316 * Atomic add a value to the value at addr.  addr[0] += value
317 *
318 * @param addr Address of value to modify
319 * @param value Amount to add to the value at addr
320 *
321 * @return old value
322 */
323extern int32_t __attribute__((overloadable))
324    rsAtomicAdd(volatile int32_t* addr, int32_t value);
325/**
326 * Atomic add a value to the value at addr.  addr[0] += value
327 *
328 * @param addr Address of value to modify
329 * @param value Amount to add to the value at addr
330 *
331 * @return old value
332 */
333extern uint32_t __attribute__((overloadable))
334    rsAtomicAdd(volatile uint32_t* addr, uint32_t value);
335
336/**
337 * Atomic Subtract a value from the value at addr.  addr[0] -= value
338 *
339 * @param addr Address of value to modify
340 * @param value Amount to subtract from the value at addr
341 *
342 * @return old value
343 */
344extern int32_t __attribute__((overloadable))
345    rsAtomicSub(volatile int32_t* addr, int32_t value);
346/**
347 * Atomic Subtract a value from the value at addr.  addr[0] -= value
348 *
349 * @param addr Address of value to modify
350 * @param value Amount to subtract from the value at addr
351 *
352 * @return old value
353 */
354extern uint32_t __attribute__((overloadable))
355    rsAtomicSub(volatile uint32_t* addr, uint32_t value);
356
357/**
358 * Atomic Bitwise and a value from the value at addr.  addr[0] &= value
359 *
360 * @param addr Address of value to modify
361 * @param value Amount to and with the value at addr
362 *
363 * @return old value
364 */
365extern int32_t __attribute__((overloadable))
366    rsAtomicAnd(volatile int32_t* addr, int32_t value);
367/**
368 * Atomic Bitwise and a value from the value at addr.  addr[0] &= value
369 *
370 * @param addr Address of value to modify
371 * @param value Amount to and with the value at addr
372 *
373 * @return old value
374 */
375extern uint32_t __attribute__((overloadable))
376    rsAtomicAnd(volatile uint32_t* addr, uint32_t value);
377
378/**
379 * Atomic Bitwise or a value from the value at addr.  addr[0] |= value
380 *
381 * @param addr Address of value to modify
382 * @param value Amount to or with the value at addr
383 *
384 * @return old value
385 */
386extern int32_t __attribute__((overloadable))
387    rsAtomicOr(volatile int32_t* addr, int32_t value);
388/**
389 * Atomic Bitwise or a value from the value at addr.  addr[0] |= value
390 *
391 * @param addr Address of value to modify
392 * @param value Amount to or with the value at addr
393 *
394 * @return old value
395 */
396extern uint32_t __attribute__((overloadable))
397    rsAtomicOr(volatile uint32_t* addr, uint32_t value);
398
399/**
400 * Atomic Bitwise xor a value from the value at addr.  addr[0] ^= value
401 *
402 * @param addr Address of value to modify
403 * @param value Amount to xor with the value at addr
404 *
405 * @return old value
406 */
407extern uint32_t __attribute__((overloadable))
408    rsAtomicXor(volatile uint32_t* addr, uint32_t value);
409/**
410 * Atomic Bitwise xor a value from the value at addr.  addr[0] ^= value
411 *
412 * @param addr Address of value to modify
413 * @param value Amount to xor with the value at addr
414 *
415 * @return old value
416 */
417extern int32_t __attribute__((overloadable))
418    rsAtomicXor(volatile int32_t* addr, int32_t value);
419
420/**
421 * Atomic Set the value at addr to the min of addr and value
422 * addr[0] = rsMin(addr[0], value)
423 *
424 * @param addr Address of value to modify
425 * @param value comparison value
426 *
427 * @return old value
428 */
429extern uint32_t __attribute__((overloadable))
430    rsAtomicMin(volatile uint32_t* addr, uint32_t value);
431/**
432 * Atomic Set the value at addr to the min of addr and value
433 * addr[0] = rsMin(addr[0], value)
434 *
435 * @param addr Address of value to modify
436 * @param value comparison value
437 *
438 * @return old value
439 */
440extern int32_t __attribute__((overloadable))
441    rsAtomicMin(volatile int32_t* addr, int32_t value);
442
443/**
444 * Atomic Set the value at addr to the max of addr and value
445 * addr[0] = rsMax(addr[0], value)
446 *
447 * @param addr Address of value to modify
448 * @param value comparison value
449 *
450 * @return old value
451 */
452extern uint32_t __attribute__((overloadable))
453    rsAtomicMax(volatile uint32_t* addr, uint32_t value);
454/**
455 * Atomic Set the value at addr to the max of addr and value
456 * addr[0] = rsMin(addr[0], value)
457 *
458 * @param addr Address of value to modify
459 * @param value comparison value
460 *
461 * @return old value
462 */
463extern int32_t __attribute__((overloadable))
464    rsAtomicMax(volatile int32_t* addr, int32_t value);
465
466/**
467 * Compare-and-set operation with a full memory barrier.
468 *
469 * If the value at addr matches compareValue then newValue is written.
470 *
471 * @param addr The address to compare and replace if the compare passes.
472 * @param compareValue The value to test addr[0] against.
473 * @param newValue The value to write if the test passes.
474 *
475 * @return old value
476 */
477extern int32_t __attribute__((overloadable))
478    rsAtomicCas(volatile int32_t* addr, int32_t compareValue, int32_t newValue);
479
480/**
481 * Compare-and-set operation with a full memory barrier.
482 *
483 * If the value at addr matches compareValue then newValue is written.
484 *
485 * @param addr The address to compare and replace if the compare passes.
486 * @param compareValue The value to test addr[0] against.
487 * @param newValue The value to write if the test passes.
488 *
489 * @return old value
490 */
491extern uint32_t __attribute__((overloadable))
492    rsAtomicCas(volatile uint32_t* addr, int32_t compareValue, int32_t newValue);
493
494
495#endif
496