4c76977195b993ced67c8a229ae7508066c72886
[mesa.git] / src / compiler / glsl / gl_nir_lower_bindless_images.c
1 /*
2 * Copyright © 2019 Red Hat 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file
26 *
27 * Lower bindless image operations by turning the image_deref_* into a
28 * bindless_image_* intrinsic and adding a load_deref on the previous deref
29 * source. All applicable indicies are also set so that fetching the variable
30 * in the backend wouldn't be needed anymore.
31 */
32
33 #include "compiler/nir/nir.h"
34 #include "compiler/nir/nir_builder.h"
35 #include "compiler/nir/nir_deref.h"
36
37 #include "compiler/glsl/gl_nir.h"
38
39 static bool
40 lower_impl(nir_builder *b, nir_instr *instr) {
41 if (instr->type != nir_instr_type_intrinsic)
42 return false;
43
44 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
45
46 nir_deref_instr *deref;
47 nir_variable *var;
48
49 switch (intrinsic->intrinsic) {
50 case nir_intrinsic_image_deref_atomic_add:
51 case nir_intrinsic_image_deref_atomic_min:
52 case nir_intrinsic_image_deref_atomic_max:
53 case nir_intrinsic_image_deref_atomic_and:
54 case nir_intrinsic_image_deref_atomic_or:
55 case nir_intrinsic_image_deref_atomic_xor:
56 case nir_intrinsic_image_deref_atomic_exchange:
57 case nir_intrinsic_image_deref_atomic_comp_swap:
58 case nir_intrinsic_image_deref_atomic_fadd:
59 case nir_intrinsic_image_deref_load:
60 case nir_intrinsic_image_deref_samples:
61 case nir_intrinsic_image_deref_size:
62 case nir_intrinsic_image_deref_store: {
63 deref = nir_src_as_deref(intrinsic->src[0]);
64 var = nir_deref_instr_get_variable(deref);
65 break;
66 }
67 default:
68 return false;
69 }
70
71 if (deref->mode == nir_var_uniform && !var->data.bindless)
72 return false;
73
74 b->cursor = nir_before_instr(instr);
75 nir_ssa_def *handle = nir_load_deref(b, deref);
76 nir_rewrite_image_intrinsic(intrinsic, handle, true);
77 return true;
78 }
79
80 bool
81 gl_nir_lower_bindless_images(nir_shader *shader)
82 {
83 bool progress = false;
84
85 nir_foreach_function(function, shader) {
86 if (function->impl) {
87 nir_builder b;
88 nir_builder_init(&b, function->impl);
89
90 nir_foreach_block(block, function->impl)
91 nir_foreach_instr(instr, block)
92 progress |= lower_impl(&b, instr);
93 }
94 }
95
96 return progress;
97 }