1 /*
2 * Copyright (C) 2020 Collabora, Ltd.
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 * Authors (Collabora):
24 * Italo Nicola <italonicola@collabora.com>
25 */
26
27 #include "compiler/nir/nir.h"
28 #include "compiler/nir/nir_builder.h"
29 #include "midgard_nir.h"
30
31 static bool
nir_lower_image_bitsize(nir_builder * b,nir_instr * instr,UNUSED void * data)32 nir_lower_image_bitsize(nir_builder *b, nir_instr *instr, UNUSED void *data)
33 {
34 if (instr->type != nir_instr_type_intrinsic)
35 return false;
36
37 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
38
39 switch (intr->intrinsic) {
40 case nir_intrinsic_image_load:
41 case nir_intrinsic_image_store:
42 case nir_intrinsic_image_atomic_add:
43 case nir_intrinsic_image_atomic_and:
44 case nir_intrinsic_image_atomic_comp_swap:
45 case nir_intrinsic_image_atomic_exchange:
46 case nir_intrinsic_image_atomic_imax:
47 case nir_intrinsic_image_atomic_imin:
48 case nir_intrinsic_image_atomic_or:
49 case nir_intrinsic_image_atomic_umax:
50 case nir_intrinsic_image_atomic_umin:
51 case nir_intrinsic_image_atomic_xor:
52 break;
53 default:
54 return false;
55 }
56
57 if (nir_src_bit_size(intr->src[1]) == 16)
58 return false;
59
60 b->cursor = nir_before_instr(instr);
61
62 nir_ssa_def *coord =
63 nir_ssa_for_src(b, intr->src[1],
64 nir_src_num_components(intr->src[1]));
65
66 nir_ssa_def *coord16 = nir_u2u16(b, coord);
67
68 nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(coord16));
69
70 return true;
71 }
72
73 bool
midgard_nir_lower_image_bitsize(nir_shader * shader)74 midgard_nir_lower_image_bitsize(nir_shader *shader)
75 {
76 return nir_shader_instructions_pass(shader,
77 nir_lower_image_bitsize,
78 nir_metadata_block_index | nir_metadata_dominance,
79 NULL);
80 }
81