1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
3bf215546Sopenharmony_ci * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
4bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
14bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
15bf215546Sopenharmony_ci * Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "nir/nir.h"
27bf215546Sopenharmony_ci#include "nir_builder.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_cistatic void
30bf215546Sopenharmony_cilower_tex_src_to_offset(nir_builder *b,
31bf215546Sopenharmony_ci                        nir_tex_instr *instr, unsigned src_idx)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci   nir_ssa_def *index = NULL;
34bf215546Sopenharmony_ci   unsigned base_index = 0;
35bf215546Sopenharmony_ci   unsigned array_elements = 1;
36bf215546Sopenharmony_ci   nir_tex_src *src = &instr->src[src_idx];
37bf215546Sopenharmony_ci   bool is_sampler = src->src_type == nir_tex_src_sampler_deref;
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci   /* We compute first the offsets */
40bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_instr_as_deref(src->src.ssa->parent_instr);
41bf215546Sopenharmony_ci   while (deref->deref_type != nir_deref_type_var) {
42bf215546Sopenharmony_ci      assert(deref->parent.is_ssa);
43bf215546Sopenharmony_ci      nir_deref_instr *parent =
44bf215546Sopenharmony_ci         nir_instr_as_deref(deref->parent.ssa->parent_instr);
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci      assert(deref->deref_type == nir_deref_type_array);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci      if (nir_src_is_const(deref->arr.index) && index == NULL) {
49bf215546Sopenharmony_ci         /* We're still building a direct index */
50bf215546Sopenharmony_ci         unsigned index_in_array = nir_src_as_uint(deref->arr.index);
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci         /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
53bf215546Sopenharmony_ci          *
54bf215546Sopenharmony_ci          *    In the subsections described above for array, vector, matrix and
55bf215546Sopenharmony_ci          *    structure accesses, any out-of-bounds access produced undefined
56bf215546Sopenharmony_ci          *    behavior.... Out-of-bounds reads return undefined values, which
57bf215546Sopenharmony_ci          *    include values from other variables of the active program or zero.
58bf215546Sopenharmony_ci          *
59bf215546Sopenharmony_ci          * Robustness extensions suggest to return zero on out-of-bounds
60bf215546Sopenharmony_ci          * accesses, however it's not applicable to the arrays of samplers,
61bf215546Sopenharmony_ci          * so just clamp the index.
62bf215546Sopenharmony_ci          *
63bf215546Sopenharmony_ci          * Otherwise instr->sampler_index or instr->texture_index would be out
64bf215546Sopenharmony_ci          * of bounds, and they are used as an index to arrays of driver state.
65bf215546Sopenharmony_ci          */
66bf215546Sopenharmony_ci         if (index_in_array < glsl_array_size(parent->type)) {
67bf215546Sopenharmony_ci            base_index += index_in_array * array_elements;
68bf215546Sopenharmony_ci         } else {
69bf215546Sopenharmony_ci            base_index = glsl_array_size(parent->type) - 1;
70bf215546Sopenharmony_ci         }
71bf215546Sopenharmony_ci      } else {
72bf215546Sopenharmony_ci         if (index == NULL) {
73bf215546Sopenharmony_ci            /* We used to be direct but not anymore */
74bf215546Sopenharmony_ci            index = nir_imm_int(b, base_index);
75bf215546Sopenharmony_ci            base_index = 0;
76bf215546Sopenharmony_ci         }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci         index = nir_iadd(b, index,
79bf215546Sopenharmony_ci                          nir_imul(b, nir_imm_int(b, array_elements),
80bf215546Sopenharmony_ci                                   nir_ssa_for_src(b, deref->arr.index, 1)));
81bf215546Sopenharmony_ci      }
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci      array_elements *= glsl_get_length(parent->type);
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci      deref = parent;
86bf215546Sopenharmony_ci   }
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   if (index)
89bf215546Sopenharmony_ci      index = nir_umin(b, index, nir_imm_int(b, array_elements - 1));
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   /* We hit the deref_var.  This is the end of the line */
92bf215546Sopenharmony_ci   assert(deref->deref_type == nir_deref_type_var);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   base_index += deref->var->data.binding;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   /* We have the offsets, we apply them, rewriting the source or removing
97bf215546Sopenharmony_ci    * instr if needed
98bf215546Sopenharmony_ci    */
99bf215546Sopenharmony_ci   if (index) {
100bf215546Sopenharmony_ci      nir_instr_rewrite_src(&instr->instr, &src->src,
101bf215546Sopenharmony_ci                            nir_src_for_ssa(index));
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci      src->src_type = is_sampler ?
104bf215546Sopenharmony_ci         nir_tex_src_sampler_offset :
105bf215546Sopenharmony_ci         nir_tex_src_texture_offset;
106bf215546Sopenharmony_ci   } else {
107bf215546Sopenharmony_ci      nir_tex_instr_remove_src(instr, src_idx);
108bf215546Sopenharmony_ci   }
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   if (is_sampler) {
111bf215546Sopenharmony_ci      instr->sampler_index = base_index;
112bf215546Sopenharmony_ci   } else {
113bf215546Sopenharmony_ci      instr->texture_index = base_index;
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistatic bool
118bf215546Sopenharmony_cilower_sampler(nir_builder *b, nir_tex_instr *instr)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   int texture_idx =
121bf215546Sopenharmony_ci      nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   if (texture_idx >= 0) {
124bf215546Sopenharmony_ci      b->cursor = nir_before_instr(&instr->instr);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci      lower_tex_src_to_offset(b, instr, texture_idx);
127bf215546Sopenharmony_ci   }
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   int sampler_idx =
130bf215546Sopenharmony_ci      nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   if (sampler_idx >= 0) {
133bf215546Sopenharmony_ci      lower_tex_src_to_offset(b, instr, sampler_idx);
134bf215546Sopenharmony_ci   }
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   if (texture_idx < 0 && sampler_idx < 0)
137bf215546Sopenharmony_ci      return false;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   return true;
140bf215546Sopenharmony_ci}
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_cistatic bool
143bf215546Sopenharmony_cilower_impl(nir_function_impl *impl)
144bf215546Sopenharmony_ci{
145bf215546Sopenharmony_ci   nir_builder b;
146bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
147bf215546Sopenharmony_ci   bool progress = false;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
150bf215546Sopenharmony_ci      nir_foreach_instr(instr, block) {
151bf215546Sopenharmony_ci         if (instr->type == nir_instr_type_tex)
152bf215546Sopenharmony_ci            progress |= lower_sampler(&b, nir_instr_as_tex(instr));
153bf215546Sopenharmony_ci      }
154bf215546Sopenharmony_ci   }
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   if (progress) {
157bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
158bf215546Sopenharmony_ci                                  nir_metadata_dominance);
159bf215546Sopenharmony_ci   } else {
160bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_all);
161bf215546Sopenharmony_ci   }
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   return progress;
164bf215546Sopenharmony_ci}
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_cibool
167bf215546Sopenharmony_cinir_lower_samplers(nir_shader *shader)
168bf215546Sopenharmony_ci{
169bf215546Sopenharmony_ci   bool progress = false;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   /* Next, lower derefs to offsets. */
172bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
173bf215546Sopenharmony_ci      if (function->impl)
174bf215546Sopenharmony_ci         progress |= lower_impl(function->impl);
175bf215546Sopenharmony_ci   }
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci   return progress;
178bf215546Sopenharmony_ci}
179