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