nir: Add a pass to repair SSA form
[mesa.git] / src / compiler / nir / 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 "nir.h"
27 #include "nir_builder.h"
28 #include "program/hash_table.h"
29 #include "compiler/glsl/ir_uniform.h"
30
31 #include "main/compiler.h"
32 #include "main/mtypes.h"
33 #include "program/prog_parameter.h"
34 #include "program/program.h"
35
36 /* Calculate the sampler index based on array indicies and also
37 * calculate the base uniform location for struct members.
38 */
39 static void
40 calc_sampler_offsets(nir_deref *tail, nir_tex_instr *instr,
41 unsigned *array_elements, nir_ssa_def **indirect,
42 nir_builder *b, unsigned *location)
43 {
44 if (tail->child == NULL)
45 return;
46
47 switch (tail->child->deref_type) {
48 case nir_deref_type_array: {
49 nir_deref_array *deref_array = nir_deref_as_array(tail->child);
50
51 assert(deref_array->deref_array_type != nir_deref_array_type_wildcard);
52
53 calc_sampler_offsets(tail->child, instr, array_elements,
54 indirect, b, location);
55 instr->texture_index += deref_array->base_offset * *array_elements;
56
57 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
58 nir_ssa_def *mul =
59 nir_imul(b, nir_imm_int(b, *array_elements),
60 nir_ssa_for_src(b, deref_array->indirect, 1));
61
62 nir_instr_rewrite_src(&instr->instr, &deref_array->indirect,
63 NIR_SRC_INIT);
64
65 if (*indirect) {
66 *indirect = nir_iadd(b, *indirect, mul);
67 } else {
68 *indirect = mul;
69 }
70 }
71
72 *array_elements *= glsl_get_length(tail->type);
73 break;
74 }
75
76 case nir_deref_type_struct: {
77 nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);
78 *location += glsl_get_record_location_offset(tail->type, deref_struct->index);
79 calc_sampler_offsets(tail->child, instr, array_elements,
80 indirect, b, location);
81 break;
82 }
83
84 default:
85 unreachable("Invalid deref type");
86 break;
87 }
88 }
89
90 static void
91 lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_program,
92 gl_shader_stage stage, nir_builder *builder)
93 {
94 if (instr->texture == NULL)
95 return;
96
97 /* In GLSL, we only fill out the texture field. The sampler is inferred */
98 assert(instr->sampler == NULL);
99
100 instr->texture_index = 0;
101 unsigned location = instr->texture->var->data.location;
102 unsigned array_elements = 1;
103 nir_ssa_def *indirect = NULL;
104
105 builder->cursor = nir_before_instr(&instr->instr);
106 calc_sampler_offsets(&instr->texture->deref, instr, &array_elements,
107 &indirect, builder, &location);
108
109 if (indirect) {
110 /* First, we have to resize the array of texture sources */
111 nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
112 instr->num_srcs + 2);
113
114 for (unsigned i = 0; i < instr->num_srcs; i++) {
115 new_srcs[i].src_type = instr->src[i].src_type;
116 nir_instr_move_src(&instr->instr, &new_srcs[i].src,
117 &instr->src[i].src);
118 }
119
120 ralloc_free(instr->src);
121 instr->src = new_srcs;
122
123 /* Now we can go ahead and move the source over to being a
124 * first-class texture source.
125 */
126 instr->src[instr->num_srcs].src_type = nir_tex_src_texture_offset;
127 instr->num_srcs++;
128 nir_instr_rewrite_src(&instr->instr,
129 &instr->src[instr->num_srcs - 1].src,
130 nir_src_for_ssa(indirect));
131
132 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
133 instr->num_srcs++;
134 nir_instr_rewrite_src(&instr->instr,
135 &instr->src[instr->num_srcs - 1].src,
136 nir_src_for_ssa(indirect));
137
138 instr->texture_array_size = array_elements;
139 }
140
141 if (location > shader_program->NumUniformStorage - 1 ||
142 !shader_program->UniformStorage[location].opaque[stage].active) {
143 assert(!"cannot return a sampler");
144 return;
145 }
146
147 instr->texture_index +=
148 shader_program->UniformStorage[location].opaque[stage].index;
149
150 instr->sampler_index = instr->texture_index;
151
152 instr->texture = NULL;
153 }
154
155 typedef struct {
156 nir_builder builder;
157 const struct gl_shader_program *shader_program;
158 gl_shader_stage stage;
159 } lower_state;
160
161 static bool
162 lower_block_cb(nir_block *block, void *_state)
163 {
164 lower_state *state = (lower_state *) _state;
165
166 nir_foreach_instr(block, instr) {
167 if (instr->type == nir_instr_type_tex) {
168 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
169 lower_sampler(tex_instr, state->shader_program, state->stage,
170 &state->builder);
171 }
172 }
173
174 return true;
175 }
176
177 static void
178 lower_impl(nir_function_impl *impl, const struct gl_shader_program *shader_program,
179 gl_shader_stage stage)
180 {
181 lower_state state;
182
183 nir_builder_init(&state.builder, impl);
184 state.shader_program = shader_program;
185 state.stage = stage;
186
187 nir_foreach_block(impl, lower_block_cb, &state);
188 }
189
190 void
191 nir_lower_samplers(nir_shader *shader,
192 const struct gl_shader_program *shader_program)
193 {
194 nir_foreach_function(shader, function) {
195 if (function->impl)
196 lower_impl(function->impl, shader_program, shader->stage);
197 }
198 }