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