nir: Properly preserve metadata in more cases
[mesa.git] / src / compiler / glsl / gl_nir_lower_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 image operations by turning the image_deref_* into a image_* on an
28 * index number or bindless_image_* intrinsic on a load_deref of the previous
29 * deref source. All applicable indicies are also set so that fetching the
30 * variable 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 void
40 type_size_align_1(const struct glsl_type *type, unsigned *size, unsigned *align)
41 {
42 *size = 1;
43 *align = 1;
44 }
45
46 static bool
47 lower_impl(nir_builder *b, nir_instr *instr, bool bindless_only)
48 {
49 if (instr->type != nir_instr_type_intrinsic)
50 return false;
51
52 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
53
54 nir_deref_instr *deref;
55 nir_variable *var;
56
57 switch (intrinsic->intrinsic) {
58 case nir_intrinsic_image_deref_atomic_add:
59 case nir_intrinsic_image_deref_atomic_imin:
60 case nir_intrinsic_image_deref_atomic_umin:
61 case nir_intrinsic_image_deref_atomic_imax:
62 case nir_intrinsic_image_deref_atomic_umax:
63 case nir_intrinsic_image_deref_atomic_and:
64 case nir_intrinsic_image_deref_atomic_or:
65 case nir_intrinsic_image_deref_atomic_xor:
66 case nir_intrinsic_image_deref_atomic_exchange:
67 case nir_intrinsic_image_deref_atomic_comp_swap:
68 case nir_intrinsic_image_deref_atomic_fadd:
69 case nir_intrinsic_image_deref_load:
70 case nir_intrinsic_image_deref_samples:
71 case nir_intrinsic_image_deref_size:
72 case nir_intrinsic_image_deref_store: {
73 deref = nir_src_as_deref(intrinsic->src[0]);
74 var = nir_deref_instr_get_variable(deref);
75 break;
76 }
77 default:
78 return false;
79 }
80
81 bool bindless = deref->mode != nir_var_uniform || var->data.bindless;
82 if (bindless_only && !bindless)
83 return false;
84
85 b->cursor = nir_before_instr(instr);
86
87 nir_ssa_def *src;
88 if (bindless) {
89 src = nir_load_deref(b, deref);
90 } else {
91 src = nir_iadd_imm(b,
92 nir_build_deref_offset(b, deref, type_size_align_1),
93 var->data.driver_location);
94 }
95 nir_rewrite_image_intrinsic(intrinsic, src, bindless);
96
97 return true;
98 }
99
100 bool
101 gl_nir_lower_images(nir_shader *shader, bool bindless_only)
102 {
103 bool progress = false;
104
105 nir_foreach_function(function, shader) {
106 if (function->impl) {
107 nir_builder b;
108 nir_builder_init(&b, function->impl);
109
110 bool impl_progress = false;
111 nir_foreach_block(block, function->impl)
112 nir_foreach_instr(instr, block)
113 impl_progress |= lower_impl(&b, instr, bindless_only);
114
115 if (impl_progress) {
116 nir_metadata_preserve(function->impl,
117 nir_metadata_block_index |
118 nir_metadata_dominance);
119 progress = true;
120 } else {
121 nir_metadata_preserve(function->impl, nir_metadata_all);
122 }
123 }
124 }
125
126 return progress;
127 }