nir_builtin_builder.h revision 01e04c3f
1/*
2 * Copyright © 2018 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#ifndef NIR_BUILTIN_BUILDER_H
25#define NIR_BUILTIN_BUILDER_H
26
27#include "nir/nir_builder.h"
28
29/*
30 * Functions are sorted alphabetically with removed type and "fast" prefix.
31 * Definitions for functions in the C file come first.
32 */
33
34nir_ssa_def* nir_cross(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y);
35nir_ssa_def* nir_fast_length(nir_builder *b, nir_ssa_def *vec);
36nir_ssa_def* nir_smoothstep(nir_builder *b, nir_ssa_def *edge0,
37                            nir_ssa_def *edge1, nir_ssa_def *x);
38
39static inline nir_ssa_def *
40nir_fclamp(nir_builder *b,
41           nir_ssa_def *x, nir_ssa_def *min_val, nir_ssa_def *max_val)
42{
43   return nir_fmin(b, nir_fmax(b, x, min_val), max_val);
44}
45
46static inline nir_ssa_def *
47nir_iclamp(nir_builder *b,
48           nir_ssa_def *x, nir_ssa_def *min_val, nir_ssa_def *max_val)
49{
50   return nir_imin(b, nir_imax(b, x, min_val), max_val);
51}
52
53static inline nir_ssa_def *
54nir_uclamp(nir_builder *b,
55           nir_ssa_def *x, nir_ssa_def *min_val, nir_ssa_def *max_val)
56{
57   return nir_umin(b, nir_umax(b, x, min_val), max_val);
58}
59
60static inline nir_ssa_def *
61nir_degrees(nir_builder *b, nir_ssa_def *val)
62{
63   return nir_fmul(b, val, nir_imm_float(b, 57.2957795131));
64}
65
66static inline nir_ssa_def *
67nir_fast_distance(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
68{
69   return nir_fast_length(b, nir_fsub(b, x, y));
70}
71
72static inline nir_ssa_def*
73nir_fast_normalize(nir_builder *b, nir_ssa_def *vec)
74{
75   return nir_fdiv(b, vec, nir_fast_length(b, vec));
76}
77
78static inline nir_ssa_def *
79nir_radians(nir_builder *b, nir_ssa_def *val)
80{
81   return nir_fmul(b, val, nir_imm_float(b, 0.01745329251));
82}
83
84#endif /* NIR_BUILTIN_BUILDER_H */
85