• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2022 Google, 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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 #include "ir3/ir3_descriptor.h"
28 
29 static bool
lower_intrinsic(nir_builder * b,nir_intrinsic_instr * intr)30 lower_intrinsic(nir_builder *b, nir_intrinsic_instr *intr)
31 {
32    unsigned desc_offset;
33 
34    switch (intr->intrinsic) {
35    case nir_intrinsic_load_ssbo:
36    case nir_intrinsic_store_ssbo:
37    case nir_intrinsic_ssbo_atomic:
38    case nir_intrinsic_ssbo_atomic_swap:
39    case nir_intrinsic_get_ssbo_size:
40       desc_offset = IR3_BINDLESS_SSBO_OFFSET;
41       break;
42    case nir_intrinsic_image_load:
43    case nir_intrinsic_image_store:
44    case nir_intrinsic_image_atomic:
45    case nir_intrinsic_image_atomic_swap:
46    case nir_intrinsic_image_size:
47    case nir_intrinsic_image_samples:
48       desc_offset = IR3_BINDLESS_IMAGE_OFFSET;
49       break;
50    default:
51       return false;
52    }
53 
54    unsigned buffer_src;
55    if (intr->intrinsic == nir_intrinsic_store_ssbo) {
56       /* store_ssbo has the value first, and ssbo src as 2nd src: */
57       buffer_src = 1;
58    } else {
59       /* the rest have ssbo src as 1st src: */
60       buffer_src = 0;
61    }
62 
63    unsigned set = ir3_shader_descriptor_set(b->shader->info.stage);
64    nir_def *src = intr->src[buffer_src].ssa;
65    src = nir_iadd_imm(b, src, desc_offset);
66    /* An out-of-bounds index into an SSBO/image array can cause a GPU fault
67     * on access to the descriptor (I don't see any hw mechanism to bound the
68     * access).  We could just allow the resulting iova fault (it is a read
69     * fault, so shouldn't corrupt anything), but at the cost of one extra
70     * instruction (as long as IR3_BINDLESS_DESC_COUNT is a power-of-two) we
71     * can avoid the dmesg spam and users thinking this is a driver bug:
72     */
73    src = nir_umod_imm(b, src, IR3_BINDLESS_DESC_COUNT);
74    nir_def *bindless = nir_bindless_resource_ir3(b, 32, src, set);
75    nir_src_rewrite(&intr->src[buffer_src], bindless);
76 
77    return true;
78 }
79 
80 static bool
lower_instr(nir_builder * b,nir_instr * instr,void * cb_data)81 lower_instr(nir_builder *b, nir_instr *instr, void *cb_data)
82 {
83    b->cursor = nir_before_instr(instr);
84    switch (instr->type) {
85    case nir_instr_type_intrinsic:
86       return lower_intrinsic(b, nir_instr_as_intrinsic(instr));
87    default:
88       return false;
89    }
90 }
91 
92 /**
93  * Lower bindful image/SSBO to bindless
94  */
95 bool
ir3_nir_lower_io_to_bindless(nir_shader * shader)96 ir3_nir_lower_io_to_bindless(nir_shader *shader)
97 {
98    /* Note: We don't currently support API level bindless, as we assume we
99     * can remap bindful images/SSBOs to bindless while controlling the entire
100     * descriptor set space.
101     *
102     * If we needed to support API level bindless, we could probably just remap
103     * bindful ops to a range of the descriptor set space that does not conflict
104     * with what we advertise for bindless descriptors?  But I'm not sure that
105     * ARB_bindless_texture is of too much value to care about, especially for
106     * GLES
107     */
108    assert(!shader->info.uses_bindless);
109 
110    return nir_shader_instructions_pass(shader, lower_instr, nir_metadata_none, NULL);
111 }
112