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
type_size_align_1(const struct glsl_type * type,unsigned * size,unsigned * align)40 type_size_align_1(const struct glsl_type *type, unsigned *size, unsigned *align)
41 {
42 unsigned s;
43
44 if (glsl_type_is_array(type))
45 s = glsl_get_aoa_size(type);
46 else
47 s = 1;
48
49 *size = s;
50 *align = s;
51 }
52
53 static bool
lower_instr(nir_builder * b,nir_instr * instr,void * cb_data)54 lower_instr(nir_builder *b, nir_instr *instr, void *cb_data)
55 {
56 if (instr->type != nir_instr_type_intrinsic)
57 return false;
58
59 bool bindless_only = *(bool *)cb_data;
60
61 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
62
63 nir_deref_instr *deref;
64 nir_variable *var;
65
66 switch (intrinsic->intrinsic) {
67 case nir_intrinsic_image_deref_atomic:
68 case nir_intrinsic_image_deref_atomic_swap:
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_samples_identical:
73 case nir_intrinsic_image_deref_descriptor_amd:
74 case nir_intrinsic_image_deref_store: {
75 deref = nir_src_as_deref(intrinsic->src[0]);
76 var = nir_deref_instr_get_variable(deref);
77 break;
78 }
79 default:
80 return false;
81 }
82
83 bool bindless = var->data.mode != nir_var_image || var->data.bindless;
84 if (bindless_only && !bindless)
85 return false;
86
87 b->cursor = nir_before_instr(instr);
88
89 nir_def *src;
90 int range_base = 0;
91 if (bindless) {
92 src = nir_load_deref(b, deref);
93 } else if (b->shader->options->lower_image_offset_to_range_base) {
94 src = nir_build_deref_offset(b, deref, type_size_align_1);
95 range_base = var->data.driver_location;
96 } else {
97 src = nir_iadd_imm(b,
98 nir_build_deref_offset(b, deref, type_size_align_1),
99 var->data.driver_location);
100 }
101 nir_rewrite_image_intrinsic(intrinsic, src, bindless);
102 if (!bindless)
103 nir_intrinsic_set_range_base(intrinsic, range_base);
104
105 return true;
106 }
107
108 bool
gl_nir_lower_images(nir_shader * shader,bool bindless_only)109 gl_nir_lower_images(nir_shader *shader, bool bindless_only)
110 {
111 return nir_shader_instructions_pass(shader, lower_instr,
112 nir_metadata_control_flow,
113 &bindless_only);
114 }
115