• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "nir_builder.h"
25 
26 #include "util/format_rgb9e5.h"
27 #include "util/macros.h"
28 
29 static inline nir_def *
nir_shift_imm(nir_builder * b,nir_def * value,int left_shift)30 nir_shift_imm(nir_builder *b, nir_def *value, int left_shift)
31 {
32    if (left_shift > 0)
33       return nir_ishl_imm(b, value, left_shift);
34    else if (left_shift < 0)
35       return nir_ushr_imm(b, value, -left_shift);
36    else
37       return value;
38 }
39 
40 static inline nir_def *
nir_shift(nir_builder * b,nir_def * value,nir_def * left_shift)41 nir_shift(nir_builder *b, nir_def *value, nir_def *left_shift)
42 {
43    return nir_bcsel(b,
44                     nir_ige_imm(b, left_shift, 0),
45                     nir_ishl(b, value, left_shift),
46                     nir_ushr(b, value, nir_ineg(b, left_shift)));
47 }
48 
49 static inline nir_def *
nir_mask_shift(struct nir_builder * b,nir_def * src,uint32_t mask,int left_shift)50 nir_mask_shift(struct nir_builder *b, nir_def *src,
51                uint32_t mask, int left_shift)
52 {
53    return nir_shift_imm(b, nir_iand_imm(b, src, mask), left_shift);
54 }
55 
56 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)57 nir_mask_shift_or(struct nir_builder *b, nir_def *dst, nir_def *src,
58                   uint32_t src_mask, int src_left_shift)
59 {
60    return nir_ior(b, nir_mask_shift(b, src, src_mask, src_left_shift), dst);
61 }
62 
63 static inline nir_def *
nir_format_mask_uvec(nir_builder * b,nir_def * src,const unsigned * bits)64 nir_format_mask_uvec(nir_builder *b, nir_def *src, const unsigned *bits)
65 {
66    nir_const_value mask[NIR_MAX_VEC_COMPONENTS];
67    memset(mask, 0, sizeof(mask));
68    for (unsigned i = 0; i < src->num_components; i++) {
69       assert(bits[i] < 32);
70       mask[i].u32 = (1u << bits[i]) - 1;
71    }
72    return nir_iand(b, src, nir_build_imm(b, src->num_components, 32, mask));
73 }
74 
75 static inline nir_def *
nir_format_sign_extend_ivec(nir_builder * b,nir_def * src,const unsigned * bits)76 nir_format_sign_extend_ivec(nir_builder *b, nir_def *src,
77                             const unsigned *bits)
78 {
79    assert(src->num_components <= 4);
80    nir_def *comps[4];
81    for (unsigned i = 0; i < src->num_components; i++) {
82       unsigned shift = src->bit_size - bits[i];
83       comps[i] = nir_ishr_imm(b, nir_ishl_imm(b, nir_channel(b, src, i), shift),
84                               shift);
85    }
86    return nir_vec(b, comps, src->num_components);
87 }
88 
89 static inline nir_def *
nir_format_unpack_int(nir_builder * b,nir_def * packed,const unsigned * bits,unsigned num_components,bool sign_extend)90 nir_format_unpack_int(nir_builder *b, nir_def *packed,
91                       const unsigned *bits, unsigned num_components,
92                       bool sign_extend)
93 {
94    assert(num_components >= 1 && num_components <= 4);
95    const unsigned bit_size = packed->bit_size;
96    nir_def *comps[4];
97 
98    if (bits[0] >= bit_size) {
99       assert(bits[0] == bit_size);
100       assert(num_components == 1);
101       return packed;
102    }
103 
104    unsigned next_chan = 0;
105    unsigned offset = 0;
106    for (unsigned i = 0; i < num_components; i++) {
107       assert(bits[i] < bit_size);
108       assert(offset + bits[i] <= bit_size);
109       nir_def *chan = nir_channel(b, packed, next_chan);
110       unsigned lshift = bit_size - (offset + bits[i]);
111       unsigned rshift = bit_size - bits[i];
112       if (sign_extend)
113          comps[i] = nir_ishr_imm(b, nir_ishl_imm(b, chan, lshift), rshift);
114       else
115          comps[i] = nir_ushr_imm(b, nir_ishl_imm(b, chan, lshift), rshift);
116       offset += bits[i];
117       if (offset >= bit_size) {
118          next_chan++;
119          offset -= bit_size;
120       }
121    }
122 
123    return nir_vec(b, comps, num_components);
124 }
125 
126 static inline nir_def *
nir_format_unpack_uint(nir_builder * b,nir_def * packed,const unsigned * bits,unsigned num_components)127 nir_format_unpack_uint(nir_builder *b, nir_def *packed,
128                        const unsigned *bits, unsigned num_components)
129 {
130    return nir_format_unpack_int(b, packed, bits, num_components, false);
131 }
132 
133 static inline nir_def *
nir_format_unpack_sint(nir_builder * b,nir_def * packed,const unsigned * bits,unsigned num_components)134 nir_format_unpack_sint(nir_builder *b, nir_def *packed,
135                        const unsigned *bits, unsigned num_components)
136 {
137    return nir_format_unpack_int(b, packed, bits, num_components, true);
138 }
139 
140 static inline nir_def *
nir_format_pack_uint_unmasked(nir_builder * b,nir_def * color,const unsigned * bits,unsigned num_components)141 nir_format_pack_uint_unmasked(nir_builder *b, nir_def *color,
142                               const unsigned *bits, unsigned num_components)
143 {
144    assert(num_components >= 1 && num_components <= 4);
145    nir_def *packed = nir_imm_int(b, 0);
146    unsigned offset = 0;
147    for (unsigned i = 0; i < num_components; i++) {
148       packed = nir_ior(b, packed, nir_shift_imm(b, nir_channel(b, color, i), offset));
149       offset += bits[i];
150    }
151    assert(offset <= packed->bit_size);
152 
153    return packed;
154 }
155 
156 static inline nir_def *
nir_format_pack_uint_unmasked_ssa(nir_builder * b,nir_def * color,nir_def * bits)157 nir_format_pack_uint_unmasked_ssa(nir_builder *b, nir_def *color,
158                                   nir_def *bits)
159 {
160    nir_def *packed = nir_imm_int(b, 0);
161    nir_def *offset = nir_imm_int(b, 0);
162    for (unsigned i = 0; i < bits->num_components; i++) {
163       packed = nir_ior(b, packed, nir_ishl(b, nir_channel(b, color, i), offset));
164       offset = nir_iadd(b, offset, nir_channel(b, bits, i));
165    }
166    return packed;
167 }
168 
169 static inline nir_def *
nir_format_pack_uint(nir_builder * b,nir_def * color,const unsigned * bits,unsigned num_components)170 nir_format_pack_uint(nir_builder *b, nir_def *color,
171                      const unsigned *bits, unsigned num_components)
172 {
173    return nir_format_pack_uint_unmasked(b, nir_format_mask_uvec(b, color, bits),
174                                         bits, num_components);
175 }
176 
177 static inline nir_def *
nir_format_bitcast_uvec_unmasked(nir_builder * b,nir_def * src,unsigned src_bits,unsigned dst_bits)178 nir_format_bitcast_uvec_unmasked(nir_builder *b, nir_def *src,
179                                  unsigned src_bits, unsigned dst_bits)
180 {
181    assert(src->bit_size >= src_bits && src->bit_size >= dst_bits);
182    assert(src_bits == 8 || src_bits == 16 || src_bits == 32);
183    assert(dst_bits == 8 || dst_bits == 16 || dst_bits == 32);
184 
185    if (src_bits == dst_bits)
186       return src;
187 
188    const unsigned dst_components =
189       DIV_ROUND_UP(src->num_components * src_bits, dst_bits);
190    assert(dst_components <= 4);
191 
192    nir_def *dst_chan[4] = { 0 };
193    if (dst_bits > src_bits) {
194       unsigned shift = 0;
195       unsigned dst_idx = 0;
196       for (unsigned i = 0; i < src->num_components; i++) {
197          nir_def *shifted = nir_ishl_imm(b, nir_channel(b, src, i),
198                                          shift);
199          if (shift == 0) {
200             dst_chan[dst_idx] = shifted;
201          } else {
202             dst_chan[dst_idx] = nir_ior(b, dst_chan[dst_idx], shifted);
203          }
204 
205          shift += src_bits;
206          if (shift >= dst_bits) {
207             dst_idx++;
208             shift = 0;
209          }
210       }
211    } else {
212       unsigned mask = ~0u >> (32 - dst_bits);
213 
214       unsigned src_idx = 0;
215       unsigned shift = 0;
216       for (unsigned i = 0; i < dst_components; i++) {
217          dst_chan[i] = nir_iand_imm(b,
218                                     nir_ushr_imm(b,
219                                                  nir_channel(b, src, src_idx),
220                                                  shift),
221                                     mask);
222          shift += dst_bits;
223          if (shift >= src_bits) {
224             src_idx++;
225             shift = 0;
226          }
227       }
228    }
229 
230    return nir_vec(b, dst_chan, dst_components);
231 }
232 
233 static inline nir_def *
_nir_format_norm_factor(nir_builder * b,const unsigned * bits,unsigned num_components,bool is_signed)234 _nir_format_norm_factor(nir_builder *b, const unsigned *bits,
235                         unsigned num_components,
236                         bool is_signed)
237 {
238    nir_const_value factor[NIR_MAX_VEC_COMPONENTS];
239    memset(factor, 0, sizeof(factor));
240    for (unsigned i = 0; i < num_components; i++) {
241       assert(bits[i] <= 32);
242       factor[i].f32 = (1ull << (bits[i] - is_signed)) - 1;
243    }
244    return nir_build_imm(b, num_components, 32, factor);
245 }
246 
247 static inline nir_def *
nir_format_unorm_to_float(nir_builder * b,nir_def * u,const unsigned * bits)248 nir_format_unorm_to_float(nir_builder *b, nir_def *u, const unsigned *bits)
249 {
250    nir_def *factor =
251       _nir_format_norm_factor(b, bits, u->num_components, false);
252 
253    return nir_fdiv(b, nir_u2f32(b, u), factor);
254 }
255 
256 static inline nir_def *
nir_format_snorm_to_float(nir_builder * b,nir_def * s,const unsigned * bits)257 nir_format_snorm_to_float(nir_builder *b, nir_def *s, const unsigned *bits)
258 {
259    nir_def *factor =
260       _nir_format_norm_factor(b, bits, s->num_components, true);
261 
262    return nir_fmax(b, nir_fdiv(b, nir_i2f32(b, s), factor),
263                    nir_imm_float(b, -1.0f));
264 }
265 
266 static inline nir_def *
nir_format_float_to_unorm(nir_builder * b,nir_def * f,const unsigned * bits)267 nir_format_float_to_unorm(nir_builder *b, nir_def *f, const unsigned *bits)
268 {
269    nir_def *factor =
270       _nir_format_norm_factor(b, bits, f->num_components, false);
271 
272    /* Clamp to the range [0, 1] */
273    f = nir_fsat(b, f);
274 
275    return nir_f2u32(b, nir_fround_even(b, nir_fmul(b, f, factor)));
276 }
277 
278 static inline nir_def *
nir_format_float_to_snorm(nir_builder * b,nir_def * f,const unsigned * bits)279 nir_format_float_to_snorm(nir_builder *b, nir_def *f, const unsigned *bits)
280 {
281    nir_def *factor =
282       _nir_format_norm_factor(b, bits, f->num_components, true);
283 
284    /* Clamp to the range [-1, 1] */
285    f = nir_fmin(b, nir_fmax(b, f, nir_imm_float(b, -1)), nir_imm_float(b, 1));
286 
287    return nir_f2i32(b, nir_fround_even(b, nir_fmul(b, f, factor)));
288 }
289 
290 /* Converts a vector of floats to a vector of half-floats packed in the low 16
291  * bits.
292  */
293 static inline nir_def *
nir_format_float_to_half(nir_builder * b,nir_def * f)294 nir_format_float_to_half(nir_builder *b, nir_def *f)
295 {
296    nir_def *zero = nir_imm_float(b, 0);
297    nir_def *f16comps[4];
298    for (unsigned i = 0; i < f->num_components; i++)
299       f16comps[i] = nir_pack_half_2x16_split(b, nir_channel(b, f, i), zero);
300    return nir_vec(b, f16comps, f->num_components);
301 }
302 
303 static inline nir_def *
nir_format_linear_to_srgb(nir_builder * b,nir_def * c)304 nir_format_linear_to_srgb(nir_builder *b, nir_def *c)
305 {
306    nir_def *linear = nir_fmul_imm(b, c, 12.92f);
307    nir_def *curved =
308       nir_fadd_imm(b, nir_fmul_imm(b, nir_fpow(b, c, nir_imm_float(b, 1.0 / 2.4)), 1.055f),
309                    -0.055f);
310 
311    return nir_fsat(b, nir_bcsel(b, nir_flt_imm(b, c, 0.0031308f),
312                                 linear, curved));
313 }
314 
315 static inline nir_def *
nir_format_srgb_to_linear(nir_builder * b,nir_def * c)316 nir_format_srgb_to_linear(nir_builder *b, nir_def *c)
317 {
318    nir_def *linear = nir_fdiv_imm(b, c, 12.92f);
319    nir_def *curved =
320       nir_fpow(b, nir_fmul_imm(b, nir_fadd_imm(b, c, 0.055f), 1.0 / 1.055f),
321                nir_imm_float(b, 2.4f));
322 
323    return nir_fsat(b, nir_bcsel(b, nir_fle_imm(b, c, 0.04045f),
324                                 linear, curved));
325 }
326 
327 /* Clamps a vector of uints so they don't extend beyond the given number of
328  * bits per channel.
329  */
330 static inline nir_def *
nir_format_clamp_uint(nir_builder * b,nir_def * f,const unsigned * bits)331 nir_format_clamp_uint(nir_builder *b, nir_def *f, const unsigned *bits)
332 {
333    if (bits[0] == 32)
334       return f;
335 
336    nir_const_value max[NIR_MAX_VEC_COMPONENTS];
337    memset(max, 0, sizeof(max));
338    for (unsigned i = 0; i < f->num_components; i++) {
339       assert(bits[i] < 32 && bits[i] <= f->bit_size);
340       max[i].u32 = u_uintN_max(bits[i]);
341    }
342    return nir_umin(b, f, nir_u2uN(b, nir_build_imm(b, f->num_components, 32, max), f->bit_size));
343 }
344 
345 /* Clamps a vector of sints so they don't extend beyond the given number of
346  * bits per channel.
347  */
348 static inline nir_def *
nir_format_clamp_sint(nir_builder * b,nir_def * f,const unsigned * bits)349 nir_format_clamp_sint(nir_builder *b, nir_def *f, const unsigned *bits)
350 {
351    if (bits[0] == 32)
352       return f;
353 
354    nir_const_value min[NIR_MAX_VEC_COMPONENTS], max[NIR_MAX_VEC_COMPONENTS];
355    memset(min, 0, sizeof(min));
356    memset(max, 0, sizeof(max));
357    for (unsigned i = 0; i < f->num_components; i++) {
358       assert(bits[i] < 32 && bits[i] <= f->bit_size);
359       max[i].i32 = u_intN_max(bits[i]);
360       min[i].i32 = u_intN_min(bits[i]);
361    }
362    f = nir_imin(b, f, nir_i2iN(b, nir_build_imm(b, f->num_components, 32, max), f->bit_size));
363    f = nir_imax(b, f, nir_i2iN(b, nir_build_imm(b, f->num_components, 32, min), f->bit_size));
364 
365    return f;
366 }
367 
368 static inline nir_def *
nir_format_unpack_11f11f10f(nir_builder * b,nir_def * packed)369 nir_format_unpack_11f11f10f(nir_builder *b, nir_def *packed)
370 {
371    nir_def *chans[3];
372    chans[0] = nir_mask_shift(b, packed, 0x000007ff, 4);
373    chans[1] = nir_mask_shift(b, packed, 0x003ff800, -7);
374    chans[2] = nir_mask_shift(b, packed, 0xffc00000, -17);
375 
376    for (unsigned i = 0; i < 3; i++)
377       chans[i] = nir_unpack_half_2x16_split_x(b, chans[i]);
378 
379    return nir_vec(b, chans, 3);
380 }
381 
382 static inline nir_def *
nir_format_pack_11f11f10f(nir_builder * b,nir_def * color)383 nir_format_pack_11f11f10f(nir_builder *b, nir_def *color)
384 {
385    /* 10 and 11-bit floats are unsigned.  Clamp to non-negative */
386    nir_def *clamped = nir_fmax(b, color, nir_imm_float(b, 0));
387 
388    nir_def *undef = nir_undef(b, 1, color->bit_size);
389    nir_def *p1 = nir_pack_half_2x16_split(b, nir_channel(b, clamped, 0),
390                                           nir_channel(b, clamped, 1));
391    nir_def *p2 = nir_pack_half_2x16_split(b, nir_channel(b, clamped, 2),
392                                           undef);
393 
394    /* A 10 or 11-bit float has the same exponent as a 16-bit float but with
395     * fewer mantissa bits and no sign bit.  All we have to do is throw away
396     * the sign bit and the bottom mantissa bits and shift it into place.
397     */
398    nir_def *packed = nir_imm_int(b, 0);
399    packed = nir_mask_shift_or(b, packed, p1, 0x00007ff0, -4);
400    packed = nir_mask_shift_or(b, packed, p1, 0x7ff00000, -9);
401    packed = nir_mask_shift_or(b, packed, p2, 0x00007fe0, 17);
402 
403    return packed;
404 }
405 
406 static inline nir_def *
nir_format_pack_r9g9b9e5(nir_builder * b,nir_def * color)407 nir_format_pack_r9g9b9e5(nir_builder *b, nir_def *color)
408 {
409    /* See also float3_to_rgb9e5 */
410 
411    /* First, we need to clamp it to range. */
412    nir_def *clamped = nir_fmin(b, color, nir_imm_float(b, MAX_RGB9E5));
413 
414    /* Get rid of negatives and NaN */
415    clamped = nir_bcsel(b, nir_ugt_imm(b, color, 0x7f800000),
416                        nir_imm_float(b, 0), clamped);
417 
418    /* maxrgb.u = MAX3(rc.u, gc.u, bc.u); */
419    nir_def *maxu = nir_umax(b, nir_channel(b, clamped, 0),
420                             nir_umax(b, nir_channel(b, clamped, 1),
421                                      nir_channel(b, clamped, 2)));
422 
423    /* maxrgb.u += maxrgb.u & (1 << (23-9)); */
424    maxu = nir_iadd(b, maxu, nir_iand_imm(b, maxu, 1 << 14));
425 
426    /* exp_shared = MAX2((maxrgb.u >> 23), -RGB9E5_EXP_BIAS - 1 + 127) +
427     *              1 + RGB9E5_EXP_BIAS - 127;
428     */
429    nir_def *exp_shared =
430       nir_iadd_imm(b, nir_umax(b, nir_ushr_imm(b, maxu, 23), nir_imm_int(b, -RGB9E5_EXP_BIAS - 1 + 127)),
431                    1 + RGB9E5_EXP_BIAS - 127);
432 
433    /* revdenom_biasedexp = 127 - (exp_shared - RGB9E5_EXP_BIAS -
434     *                             RGB9E5_MANTISSA_BITS) + 1;
435     */
436    nir_def *revdenom_biasedexp =
437       nir_isub_imm(b, 127 + RGB9E5_EXP_BIAS + RGB9E5_MANTISSA_BITS + 1,
438                    exp_shared);
439 
440    /* revdenom.u = revdenom_biasedexp << 23; */
441    nir_def *revdenom =
442       nir_ishl_imm(b, revdenom_biasedexp, 23);
443 
444    /* rm = (int) (rc.f * revdenom.f);
445     * gm = (int) (gc.f * revdenom.f);
446     * bm = (int) (bc.f * revdenom.f);
447     */
448    nir_def *mantissa =
449       nir_f2i32(b, nir_fmul(b, clamped, revdenom));
450 
451    /* rm = (rm & 1) + (rm >> 1);
452     * gm = (gm & 1) + (gm >> 1);
453     * bm = (bm & 1) + (bm >> 1);
454     */
455    mantissa = nir_iadd(b, nir_iand_imm(b, mantissa, 1),
456                        nir_ushr_imm(b, mantissa, 1));
457 
458    nir_def *packed = nir_channel(b, mantissa, 0);
459    packed = nir_mask_shift_or(b, packed, nir_channel(b, mantissa, 1), ~0, 9);
460    packed = nir_mask_shift_or(b, packed, nir_channel(b, mantissa, 2), ~0, 18);
461    packed = nir_mask_shift_or(b, packed, exp_shared, ~0, 27);
462 
463    return packed;
464 }
465