nir/lower_samplers: Clean up function arguments
[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_deref *tail, nir_tex_instr *instr,
39 unsigned *array_elements, nir_ssa_def **indirect,
40 nir_builder *b, unsigned *location)
41 {
42 if (tail->child == NULL)
43 return;
44
45 switch (tail->child->deref_type) {
46 case nir_deref_type_array: {
47 nir_deref_array *deref_array = nir_deref_as_array(tail->child);
48
49 assert(deref_array->deref_array_type != nir_deref_array_type_wildcard);
50
51 calc_sampler_offsets(tail->child, instr, array_elements,
52 indirect, b, location);
53 instr->texture_index += deref_array->base_offset * *array_elements;
54
55 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
56 nir_ssa_def *mul =
57 nir_imul(b, nir_imm_int(b, *array_elements),
58 nir_ssa_for_src(b, deref_array->indirect, 1));
59
60 nir_instr_rewrite_src(&instr->instr, &deref_array->indirect,
61 NIR_SRC_INIT);
62
63 if (*indirect) {
64 *indirect = nir_iadd(b, *indirect, mul);
65 } else {
66 *indirect = mul;
67 }
68 }
69
70 *array_elements *= glsl_get_length(tail->type);
71 break;
72 }
73
74 case nir_deref_type_struct: {
75 nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);
76 *location += glsl_get_record_location_offset(tail->type, deref_struct->index);
77 calc_sampler_offsets(tail->child, instr, array_elements,
78 indirect, b, location);
79 break;
80 }
81
82 default:
83 unreachable("Invalid deref type");
84 break;
85 }
86 }
87
88 static bool
89 lower_sampler(nir_builder *b, nir_tex_instr *instr,
90 const struct gl_shader_program *shader_program)
91 {
92 if (instr->texture == NULL)
93 return false;
94
95 /* In GLSL, we only fill out the texture field. The sampler is inferred */
96 assert(instr->sampler == NULL || shader_program->data->spirv);
97
98 instr->texture_index = 0;
99 unsigned location = instr->texture->var->data.location;
100 unsigned array_elements = 1;
101 nir_ssa_def *indirect = NULL;
102
103 b->cursor = nir_before_instr(&instr->instr);
104 calc_sampler_offsets(&instr->texture->deref, instr, &array_elements,
105 &indirect, b, &location);
106
107 if (indirect) {
108 assert(array_elements >= 1);
109 indirect = nir_umin(b, indirect, nir_imm_int(b, array_elements - 1));
110
111 nir_tex_instr_add_src(instr, nir_tex_src_texture_offset,
112 nir_src_for_ssa(indirect));
113 nir_tex_instr_add_src(instr, nir_tex_src_sampler_offset,
114 nir_src_for_ssa(indirect));
115
116 instr->texture_array_size = array_elements;
117 }
118
119 gl_shader_stage stage = b->shader->info.stage;
120 assert(location < shader_program->data->NumUniformStorage &&
121 shader_program->data->UniformStorage[location].opaque[stage].active);
122
123 instr->texture_index +=
124 shader_program->data->UniformStorage[location].opaque[stage].index;
125
126 instr->sampler_index = instr->texture_index;
127
128 instr->texture = NULL;
129 nir_instr_rewrite_deref(&instr->instr, &instr->sampler, NULL);
130
131 return true;
132 }
133
134 static bool
135 lower_impl(nir_function_impl *impl,
136 const struct gl_shader_program *shader_program)
137 {
138 nir_builder b;
139 nir_builder_init(&b, impl);
140 bool progress = false;
141
142 nir_foreach_block(block, impl) {
143 nir_foreach_instr(instr, block) {
144 if (instr->type == nir_instr_type_tex)
145 progress |= lower_sampler(&b, nir_instr_as_tex(instr),
146 shader_program);
147 }
148 }
149
150 return progress;
151 }
152
153 bool
154 gl_nir_lower_samplers(nir_shader *shader,
155 const struct gl_shader_program *shader_program)
156 {
157 bool progress = false;
158
159 nir_assert_lowered_derefs(shader, nir_lower_texture_derefs);
160
161 nir_foreach_function(function, shader) {
162 if (function->impl)
163 progress |= lower_impl(function->impl, shader_program);
164 }
165
166 return progress;
167 }