nir/lower_samplers: don't assume a deref for both texture and sampler srcs
[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 const struct gl_shader_program *shader_program)
38 {
39 nir_ssa_def *index = NULL;
40 unsigned base_index = 0;
41 unsigned array_elements = 1;
42 nir_tex_src *src = &instr->src[src_idx];
43 bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
44 unsigned location = 0;
45
46 /* We compute first the offsets */
47 nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
48 while (deref->deref_type != nir_deref_type_var) {
49 assert(deref->parent.is_ssa);
50 nir_deref_instr *parent =
51 nir_instr_as_deref(deref->parent.ssa->parent_instr);
52
53 switch (deref->deref_type) {
54 case nir_deref_type_struct:
55 location += glsl_get_record_location_offset(parent->type,
56 deref->strct.index);
57 break;
58
59 case nir_deref_type_array: {
60 nir_const_value *const_deref_index =
61 nir_src_as_const_value(deref->arr.index);
62
63 if (const_deref_index && index == NULL) {
64 /* We're still building a direct index */
65 base_index += const_deref_index->u32[0] * array_elements;
66 } else {
67 if (index == NULL) {
68 /* We used to be direct but not anymore */
69 index = nir_imm_int(b, base_index);
70 base_index = 0;
71 }
72
73 index = nir_iadd(b, index,
74 nir_imul(b, nir_imm_int(b, array_elements),
75 nir_ssa_for_src(b, deref->arr.index, 1)));
76 }
77
78 array_elements *= glsl_get_length(parent->type);
79 break;
80 }
81
82 default:
83 unreachable("Invalid sampler deref type");
84 }
85
86 deref = parent;
87 }
88
89 if (index)
90 index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
91
92 /* We hit the deref_var. This is the end of the line */
93 assert(deref->deref_type == nir_deref_type_var);
94
95 location += deref->var->data.location;
96
97 gl_shader_stage stage = b->shader->info.stage;
98 assert(location < shader_program->data->NumUniformStorage &&
99 shader_program->data->UniformStorage[location].opaque[stage].active);
100
101 base_index +=
102 shader_program->data->UniformStorage[location].opaque[stage].index;
103
104 /* We have the offsets, we apply them, rewriting the source or removing
105 * instr if needed
106 */
107 if (index) {
108 nir_instr_rewrite_src(&instr->instr, &src->src,
109 nir_src_for_ssa(index));
110
111 src->src_type = is_sampler ?
112 nir_tex_src_sampler_offset :
113 nir_tex_src_texture_offset;
114
115 instr->texture_array_size = array_elements;
116 } else {
117 nir_tex_instr_remove_src(instr, src_idx);
118 }
119
120 if (is_sampler) {
121 instr->sampler_index = base_index;
122 } else {
123 instr->texture_index = base_index;
124 instr->texture_array_size = array_elements;
125 }
126 }
127
128 static bool
129 lower_sampler(nir_builder *b, nir_tex_instr *instr,
130 const struct gl_shader_program *shader_program)
131 {
132 int texture_idx =
133 nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
134
135 if (texture_idx >= 0) {
136 b->cursor = nir_before_instr(&instr->instr);
137
138 lower_tex_src_to_offset(b, instr, texture_idx,
139 shader_program);
140 }
141
142 int sampler_idx =
143 nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
144
145 if (sampler_idx >= 0) {
146 lower_tex_src_to_offset(b, instr, sampler_idx,
147 shader_program);
148 }
149
150 if (texture_idx < 0 && sampler_idx < 0)
151 return false;
152
153 return true;
154 }
155
156 static bool
157 lower_impl(nir_function_impl *impl,
158 const struct gl_shader_program *shader_program)
159 {
160 nir_builder b;
161 nir_builder_init(&b, impl);
162 bool progress = false;
163
164 nir_foreach_block(block, impl) {
165 nir_foreach_instr(instr, block) {
166 if (instr->type == nir_instr_type_tex)
167 progress |= lower_sampler(&b, nir_instr_as_tex(instr),
168 shader_program);
169 }
170 }
171
172 return progress;
173 }
174
175 bool
176 gl_nir_lower_samplers(nir_shader *shader,
177 const struct gl_shader_program *shader_program)
178 {
179 bool progress = false;
180
181 nir_foreach_function(function, shader) {
182 if (function->impl)
183 progress |= lower_impl(function->impl, shader_program);
184 }
185
186 return progress;
187 }