xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_format_convert.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2017 Intel Corporation
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_FORMAT_CONVERT_H
25 #define NIR_FORMAT_CONVERT_H
26 
27 #include "nir_builder.h"
28 #include "util/format/u_formats.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 static inline nir_def *
nir_shift_imm(nir_builder * b,nir_def * value,int left_shift)35 nir_shift_imm(nir_builder *b, nir_def *value, int left_shift)
36 {
37    if (left_shift > 0)
38       return nir_ishl_imm(b, value, left_shift);
39    else if (left_shift < 0)
40       return nir_ushr_imm(b, value, -left_shift);
41    else
42       return value;
43 }
44 
45 static inline nir_def *
nir_shift(nir_builder * b,nir_def * value,nir_def * left_shift)46 nir_shift(nir_builder *b, nir_def *value, nir_def *left_shift)
47 {
48    return nir_bcsel(b,
49                     nir_ige_imm(b, left_shift, 0),
50                     nir_ishl(b, value, left_shift),
51                     nir_ushr(b, value, nir_ineg(b, left_shift)));
52 }
53 
54 static inline nir_def *
nir_mask_shift(struct nir_builder * b,nir_def * src,uint32_t mask,int left_shift)55 nir_mask_shift(struct nir_builder *b, nir_def *src,
56                uint32_t mask, int left_shift)
57 {
58    return nir_shift_imm(b, nir_iand_imm(b, src, mask), left_shift);
59 }
60 
61 static inline nir_def *
nir_mask_shift_or(struct nir_builder * b,nir_def * dst,nir_def * src,uint32_t src_mask,int src_left_shift)62 nir_mask_shift_or(struct nir_builder *b, nir_def *dst, nir_def *src,
63                   uint32_t src_mask, int src_left_shift)
64 {
65    return nir_ior(b, nir_mask_shift(b, src, src_mask, src_left_shift), dst);
66 }
67 
68 nir_def *nir_format_mask_uvec(nir_builder *b, nir_def *src,
69                               const unsigned *bits);
70 nir_def *nir_format_sign_extend_ivec(nir_builder *b, nir_def *src,
71                                      const unsigned *bits);
72 nir_def *nir_format_unpack_int(nir_builder *b, nir_def *packed,
73                                const unsigned *bits,
74                                unsigned num_components,
75                                bool sign_extend);
76 
77 static inline nir_def *
nir_format_unpack_uint(nir_builder * b,nir_def * packed,const unsigned * bits,unsigned num_components)78 nir_format_unpack_uint(nir_builder *b, nir_def *packed,
79                        const unsigned *bits, unsigned num_components)
80 {
81    return nir_format_unpack_int(b, packed, bits, num_components, false);
82 }
83 
84 static inline nir_def *
nir_format_unpack_sint(nir_builder * b,nir_def * packed,const unsigned * bits,unsigned num_components)85 nir_format_unpack_sint(nir_builder *b, nir_def *packed,
86                        const unsigned *bits, unsigned num_components)
87 {
88    return nir_format_unpack_int(b, packed, bits, num_components, true);
89 }
90 
91 nir_def *nir_format_pack_uint_unmasked(nir_builder *b, nir_def *color,
92                                        const unsigned *bits,
93                                        unsigned num_components);
94 nir_def *nir_format_pack_uint_unmasked_ssa(nir_builder *b, nir_def *color,
95                                            nir_def *bits);
96 nir_def *nir_format_pack_uint(nir_builder *b, nir_def *color,
97                               const unsigned *bits,
98                               unsigned num_components);
99 
100 nir_def *nir_format_bitcast_uvec_unmasked(nir_builder *b, nir_def *src,
101                                           unsigned src_bits,
102                                           unsigned dst_bits);
103 
104 nir_def *nir_format_unorm_to_float(nir_builder *b, nir_def *u,
105                                    const unsigned *bits);
106 nir_def *nir_format_snorm_to_float(nir_builder *b, nir_def *s,
107                                    const unsigned *bits);
108 nir_def *nir_format_float_to_unorm(nir_builder *b, nir_def *f,
109                                    const unsigned *bits);
110 nir_def *nir_format_float_to_snorm(nir_builder *b, nir_def *f,
111                                    const unsigned *bits);
112 
113 nir_def *nir_format_float_to_half(nir_builder *b, nir_def *f);
114 nir_def *nir_format_linear_to_srgb(nir_builder *b, nir_def *c);
115 nir_def *nir_format_srgb_to_linear(nir_builder *b, nir_def *c);
116 
117 nir_def *nir_format_clamp_uint(nir_builder *b, nir_def *f,
118                                const unsigned *bits);
119 nir_def *nir_format_clamp_sint(nir_builder *b, nir_def *f,
120                                const unsigned *bits);
121 
122 nir_def *nir_format_unpack_11f11f10f(nir_builder *b, nir_def *packed);
123 nir_def *nir_format_pack_11f11f10f(nir_builder *b, nir_def *color);
124 nir_def *nir_format_unpack_r9g9b9e5(nir_builder *b, nir_def *packed);
125 nir_def *nir_format_pack_r9g9b9e5(nir_builder *b, nir_def *color);
126 
127 nir_def *nir_format_unpack_rgba(nir_builder *b, nir_def *packed,
128                                 enum pipe_format format);
129 nir_def *nir_format_pack_rgba(nir_builder *b, enum pipe_format format,
130                               nir_def *rgba);
131 
132 #ifdef __cplusplus
133 } /* extern "C" */
134 #endif
135 
136 #endif /* NIR_FORMAT_CONVERT_H */
137