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/nir.h"
27#include "nir_builder.h"
28
29static void
30lower_tex_src_to_offset(nir_builder *b,
31                        nir_tex_instr *instr, unsigned src_idx)
32{
33   nir_ssa_def *index = NULL;
34   unsigned base_index = 0;
35   unsigned array_elements = 1;
36   nir_tex_src *src = &instr->src[src_idx];
37   bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
38
39   /* We compute first the offsets */
40   nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
41   while (deref->deref_type != nir_deref_type_var) {
42      assert(deref->parent.is_ssa);
43      nir_deref_instr *parent =
44         nir_instr_as_deref(deref->parent.ssa->parent_instr);
45
46      assert(deref->deref_type == nir_deref_type_array);
47
48      if (nir_src_is_const(deref->arr.index) && index == NULL) {
49         /* We're still building a direct index */
50         unsigned index_in_array = nir_src_as_uint(deref->arr.index);
51
52         /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
53          *
54          *    In the subsections described above for array, vector, matrix and
55          *    structure accesses, any out-of-bounds access produced undefined
56          *    behavior.... Out-of-bounds reads return undefined values, which
57          *    include values from other variables of the active program or zero.
58          *
59          * Robustness extensions suggest to return zero on out-of-bounds
60          * accesses, however it's not applicable to the arrays of samplers,
61          * so just clamp the index.
62          *
63          * Otherwise instr->sampler_index or instr->texture_index would be out
64          * of bounds, and they are used as an index to arrays of driver state.
65          */
66         if (index_in_array < glsl_array_size(parent->type)) {
67            base_index += index_in_array * array_elements;
68         } else {
69            base_index = glsl_array_size(parent->type) - 1;
70         }
71      } else {
72         if (index == NULL) {
73            /* We used to be direct but not anymore */
74            index = nir_imm_int(b, base_index);
75            base_index = 0;
76         }
77
78         index = nir_iadd(b, index,
79                          nir_imul(b, nir_imm_int(b, array_elements),
80                                   nir_ssa_for_src(b, deref->arr.index, 1)));
81      }
82
83      array_elements *= glsl_get_length(parent->type);
84
85      deref = parent;
86   }
87
88   if (index)
89      index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
90
91   /* We hit the deref_var.  This is the end of the line */
92   assert(deref->deref_type == nir_deref_type_var);
93
94   base_index += deref->var->data.binding;
95
96   /* We have the offsets, we apply them, rewriting the source or removing
97    * instr if needed
98    */
99   if (index) {
100      nir_instr_rewrite_src(&instr->instr, &src->src,
101                            nir_src_for_ssa(index));
102
103      src->src_type = is_sampler ?
104         nir_tex_src_sampler_offset :
105         nir_tex_src_texture_offset;
106   } else {
107      nir_tex_instr_remove_src(instr, src_idx);
108   }
109
110   if (is_sampler) {
111      instr->sampler_index = base_index;
112   } else {
113      instr->texture_index = base_index;
114   }
115}
116
117static bool
118lower_sampler(nir_builder *b, nir_tex_instr *instr)
119{
120   int texture_idx =
121      nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
122
123   if (texture_idx >= 0) {
124      b->cursor = nir_before_instr(&instr->instr);
125
126      lower_tex_src_to_offset(b, instr, texture_idx);
127   }
128
129   int sampler_idx =
130      nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
131
132   if (sampler_idx >= 0) {
133      lower_tex_src_to_offset(b, instr, sampler_idx);
134   }
135
136   if (texture_idx < 0 && sampler_idx < 0)
137      return false;
138
139   return true;
140}
141
142static bool
143lower_impl(nir_function_impl *impl)
144{
145   nir_builder b;
146   nir_builder_init(&b, impl);
147   bool progress = false;
148
149   nir_foreach_block(block, impl) {
150      nir_foreach_instr(instr, block) {
151         if (instr->type == nir_instr_type_tex)
152            progress |= lower_sampler(&b, nir_instr_as_tex(instr));
153      }
154   }
155
156   if (progress) {
157      nir_metadata_preserve(impl, nir_metadata_block_index |
158                                  nir_metadata_dominance);
159   } else {
160      nir_metadata_preserve(impl, nir_metadata_all);
161   }
162
163   return progress;
164}
165
166bool
167nir_lower_samplers(nir_shader *shader)
168{
169   bool progress = false;
170
171   /* Next, lower derefs to offsets. */
172   nir_foreach_function(function, shader) {
173      if (function->impl)
174         progress |= lower_impl(function->impl);
175   }
176
177   return progress;
178}
179