xref: /aosp_15_r20/external/mesa3d/src/panfrost/midgard/midgard_nir_lower_image_bitsize.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
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_intrinsic_instr * intr,UNUSED void * data)32 nir_lower_image_bitsize(nir_builder *b, nir_intrinsic_instr *intr,
33                         UNUSED void *data)
34 {
35    switch (intr->intrinsic) {
36    case nir_intrinsic_image_load:
37    case nir_intrinsic_image_store:
38    case nir_intrinsic_image_texel_address:
39       break;
40    default:
41       return false;
42    }
43 
44    if (nir_src_bit_size(intr->src[1]) == 16)
45       return false;
46 
47    b->cursor = nir_before_instr(&intr->instr);
48 
49    nir_def *coord = intr->src[1].ssa;
50 
51    nir_def *coord16 = nir_u2u16(b, coord);
52 
53    nir_src_rewrite(&intr->src[1], coord16);
54 
55    return true;
56 }
57 
58 bool
midgard_nir_lower_image_bitsize(nir_shader * shader)59 midgard_nir_lower_image_bitsize(nir_shader *shader)
60 {
61    return nir_shader_intrinsics_pass(
62       shader, nir_lower_image_bitsize,
63       nir_metadata_control_flow, NULL);
64 }
65