nir: Print array deref indices as decimal
[mesa.git] / src / compiler / glsl / gl_nir_lower_samplers.c
1 /*
2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2014 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "compiler/nir/nir.h"
27 #include "compiler/nir/nir_builder.h"
28 #include "gl_nir.h"
29 #include "ir_uniform.h"
30
31 #include "main/compiler.h"
32 #include "main/mtypes.h"
33
34 static void
35 lower_tex_src_to_offset(nir_builder *b,
36 nir_tex_instr *instr, unsigned src_idx)
37 {
38 nir_ssa_def *index = NULL;
39 unsigned base_index = 0;
40 unsigned array_elements = 1;
41 nir_tex_src *src = &instr->src[src_idx];
42 bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
43
44 /* We compute first the offsets */
45 nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
46 while (deref->deref_type != nir_deref_type_var) {
47 assert(deref->parent.is_ssa);
48 nir_deref_instr *parent =
49 nir_instr_as_deref(deref->parent.ssa->parent_instr);
50
51 assert(deref->deref_type == nir_deref_type_array);
52
53 if (nir_src_is_const(deref->arr.index) && index == NULL) {
54 /* We're still building a direct index */
55 base_index += nir_src_as_uint(deref->arr.index) * array_elements;
56 } else {
57 if (index == NULL) {
58 /* We used to be direct but not anymore */
59 index = nir_imm_int(b, base_index);
60 base_index = 0;
61 }
62
63 index = nir_iadd(b, index,
64 nir_imul(b, nir_imm_int(b, array_elements),
65 nir_ssa_for_src(b, deref->arr.index, 1)));
66 }
67
68 array_elements *= glsl_get_length(parent->type);
69
70 deref = parent;
71 }
72
73 if (index)
74 index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
75
76 /* We hit the deref_var. This is the end of the line */
77 assert(deref->deref_type == nir_deref_type_var);
78
79 base_index += deref->var->data.binding;
80
81 /* We have the offsets, we apply them, rewriting the source or removing
82 * instr if needed
83 */
84 if (index) {
85 nir_instr_rewrite_src(&instr->instr, &src->src,
86 nir_src_for_ssa(index));
87
88 src->src_type = is_sampler ?
89 nir_tex_src_sampler_offset :
90 nir_tex_src_texture_offset;
91
92 instr->texture_array_size = array_elements;
93 } else {
94 nir_tex_instr_remove_src(instr, src_idx);
95 }
96
97 if (is_sampler) {
98 instr->sampler_index = base_index;
99 } else {
100 instr->texture_index = base_index;
101 instr->texture_array_size = array_elements;
102 }
103 }
104
105 static bool
106 lower_sampler(nir_builder *b, nir_tex_instr *instr)
107 {
108 int texture_idx =
109 nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
110
111 if (texture_idx >= 0) {
112 b->cursor = nir_before_instr(&instr->instr);
113
114 lower_tex_src_to_offset(b, instr, texture_idx);
115 }
116
117 int sampler_idx =
118 nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
119
120 if (sampler_idx >= 0) {
121 lower_tex_src_to_offset(b, instr, sampler_idx);
122 }
123
124 if (texture_idx < 0 && sampler_idx < 0)
125 return false;
126
127 return true;
128 }
129
130 static bool
131 lower_impl(nir_function_impl *impl)
132 {
133 nir_builder b;
134 nir_builder_init(&b, impl);
135 bool progress = false;
136
137 nir_foreach_block(block, impl) {
138 nir_foreach_instr(instr, block) {
139 if (instr->type == nir_instr_type_tex)
140 progress |= lower_sampler(&b, nir_instr_as_tex(instr));
141 }
142 }
143
144 return progress;
145 }
146
147 bool
148 gl_nir_lower_samplers(nir_shader *shader,
149 const struct gl_shader_program *shader_program)
150 {
151 bool progress = false;
152
153 /* First, use gl_nir_lower_samplers_as_derefs to set var->data.binding
154 * based on the uniforms, and split structures to simplify derefs.
155 */
156 gl_nir_lower_samplers_as_deref(shader, shader_program);
157
158 /* Next, lower derefs to offsets. */
159 nir_foreach_function(function, shader) {
160 if (function->impl)
161 progress |= lower_impl(function->impl);
162 }
163
164 return progress;
165 }