OSDN Git Service

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