• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 /**
28  * @file
29  *
30  * This pass combines separate clip and cull distance arrays into a
31  * single array that contains both.  Clip distances come first, then
32  * cull distances.  It also populates nir_shader_info with the size
33  * of the original arrays so the driver knows which are which.
34  */
35 
36 /**
37  * Get the length of the clip/cull distance array, looking past
38  * any interface block arrays.
39  */
40 static unsigned
get_unwrapped_array_length(nir_shader * nir,nir_variable * var)41 get_unwrapped_array_length(nir_shader *nir, nir_variable *var)
42 {
43    if (!var)
44       return 0;
45 
46    /* Unwrap GS input and TCS input/output interfaces.  We want the
47     * underlying clip/cull distance array length, not the per-vertex
48     * array length.
49     */
50    const struct glsl_type *type = var->type;
51    if (nir_is_per_vertex_io(var, nir->info.stage))
52       type = glsl_get_array_element(type);
53 
54    assert(glsl_type_is_array(type));
55 
56    return glsl_get_length(type);
57 }
58 
59 /**
60  * Update the type of the combined array (including interface block nesting).
61  */
62 static void
update_type(nir_variable * var,gl_shader_stage stage,unsigned length)63 update_type(nir_variable *var, gl_shader_stage stage, unsigned length)
64 {
65    const struct glsl_type *type = glsl_array_type(glsl_float_type(), length);
66 
67    if (nir_is_per_vertex_io(var, stage))
68       type = glsl_array_type(type, glsl_get_length(var->type));
69 
70    var->type = type;
71 }
72 
73 /**
74  * Rewrite any clip/cull distances to refer to the new combined array.
75  */
76 static void
rewrite_references(nir_instr * instr,nir_variable * combined,unsigned cull_offset)77 rewrite_references(nir_instr *instr,
78                    nir_variable *combined,
79                    unsigned cull_offset)
80 {
81    if (instr->type != nir_instr_type_intrinsic)
82       return;
83 
84    nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
85 
86    /* copy_var needs to be lowered to load/store before calling this pass */
87    assert(intrin->intrinsic != nir_intrinsic_copy_var);
88 
89    if (intrin->intrinsic != nir_intrinsic_load_var &&
90        intrin->intrinsic != nir_intrinsic_store_var)
91       return;
92 
93    nir_deref_var *var_ref = intrin->variables[0];
94    if (var_ref->var->data.mode != combined->data.mode)
95       return;
96 
97    if (var_ref->var->data.location != VARYING_SLOT_CLIP_DIST0 &&
98        var_ref->var->data.location != VARYING_SLOT_CULL_DIST0)
99       return;
100 
101    /* Update types along the deref chain */
102    const struct glsl_type *type = combined->type;
103    nir_deref *deref = &var_ref->deref;
104    while (deref) {
105       deref->type = type;
106       deref = deref->child;
107       type = glsl_get_array_element(type);
108    }
109 
110    /* For cull distances, add an offset to the array index */
111    if (var_ref->var->data.location == VARYING_SLOT_CULL_DIST0) {
112       nir_deref *tail = nir_deref_tail(&intrin->variables[0]->deref);
113       nir_deref_array *array_ref = nir_deref_as_array(tail);
114 
115       array_ref->base_offset += cull_offset;
116    }
117 
118    /* Point the deref at the combined array */
119    var_ref->var = combined;
120 
121    /* There's no need to update writemasks; it's a scalar array. */
122 }
123 
124 static bool
combine_clip_cull(nir_shader * nir,struct exec_list * vars,bool store_info)125 combine_clip_cull(nir_shader *nir,
126                   struct exec_list *vars,
127                   bool store_info)
128 {
129    nir_variable *cull = NULL;
130    nir_variable *clip = NULL;
131    bool progress = false;
132 
133    nir_foreach_variable(var, vars) {
134       if (var->data.location == VARYING_SLOT_CLIP_DIST0)
135          clip = var;
136 
137       if (var->data.location == VARYING_SLOT_CULL_DIST0)
138          cull = var;
139    }
140 
141    const unsigned clip_array_size = get_unwrapped_array_length(nir, clip);
142    const unsigned cull_array_size = get_unwrapped_array_length(nir, cull);
143 
144    if (store_info) {
145       nir->info.clip_distance_array_size = clip_array_size;
146       nir->info.cull_distance_array_size = cull_array_size;
147    }
148 
149    if (clip)
150       clip->data.compact = true;
151 
152    if (cull)
153       cull->data.compact = true;
154 
155    if (cull_array_size > 0) {
156       if (clip_array_size == 0) {
157          /* No clip distances, just change the cull distance location */
158          cull->data.location = VARYING_SLOT_CLIP_DIST0;
159       } else {
160          /* Turn the ClipDistance array into a combined one */
161          update_type(clip, nir->info.stage, clip_array_size + cull_array_size);
162 
163          /* Rewrite CullDistance to reference the combined array */
164          nir_foreach_function(function, nir) {
165             if (function->impl) {
166                nir_foreach_block(block, function->impl) {
167                   nir_foreach_instr(instr, block) {
168                      rewrite_references(instr, clip, clip_array_size);
169                   }
170                }
171             }
172          }
173 
174          /* Delete the old CullDistance variable */
175          exec_node_remove(&cull->node);
176          ralloc_free(cull);
177       }
178 
179       nir_foreach_function(function, nir) {
180          if (function->impl) {
181             nir_metadata_preserve(function->impl,
182                                   nir_metadata_block_index |
183                                   nir_metadata_dominance);
184          }
185       }
186       progress = true;
187    }
188 
189    return progress;
190 }
191 
192 bool
nir_lower_clip_cull_distance_arrays(nir_shader * nir)193 nir_lower_clip_cull_distance_arrays(nir_shader *nir)
194 {
195    bool progress = false;
196 
197    if (nir->info.stage <= MESA_SHADER_GEOMETRY)
198       progress |= combine_clip_cull(nir, &nir->outputs, true);
199 
200    if (nir->info.stage > MESA_SHADER_VERTEX)
201       progress |= combine_clip_cull(nir, &nir->inputs, false);
202 
203    return progress;
204 }
205