nir: support lowering clipdist to arrays
[mesa.git] / src / compiler / nir / nir_lower_clip_cull_distance_arrays.c
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 clip and cull distance arrays in separate locations and
31 * colocates them both in VARYING_SLOT_CLIP_DIST0. It does so by maintaining
32 * two arrays but making them compact and using location_frac to stack them on
33 * top of each other.
34 */
35
36 /**
37 * Get the length of the clip/cull distance array, looking past
38 * any interface block arrays.
39 */
40 static unsigned
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 static bool
60 combine_clip_cull(nir_shader *nir,
61 struct exec_list *vars,
62 bool store_info)
63 {
64 nir_variable *cull = NULL;
65 nir_variable *clip = NULL;
66
67 nir_foreach_variable(var, vars) {
68 if (var->data.location == VARYING_SLOT_CLIP_DIST0)
69 clip = var;
70
71 if (var->data.location == VARYING_SLOT_CULL_DIST0)
72 cull = var;
73 }
74
75 if (!cull && !clip)
76 return false;
77
78 if (!cull && clip) {
79 /* The GLSL IR lowering pass must have converted these to vectors */
80 if (!clip->data.compact)
81 return false;
82
83 /* If this pass has already run, don't repeat. We would think that
84 * the combined clip/cull distance array was clip-only and mess up.
85 */
86 if (clip->data.how_declared == nir_var_hidden)
87 return false;
88 }
89
90 const unsigned clip_array_size = get_unwrapped_array_length(nir, clip);
91 const unsigned cull_array_size = get_unwrapped_array_length(nir, cull);
92
93 if (store_info) {
94 nir->info.clip_distance_array_size = clip_array_size;
95 nir->info.cull_distance_array_size = cull_array_size;
96 }
97
98 if (clip) {
99 assert(clip->data.compact);
100 clip->data.how_declared = nir_var_hidden;
101 }
102
103 if (cull) {
104 assert(cull->data.compact);
105 cull->data.how_declared = nir_var_hidden;
106 cull->data.location = VARYING_SLOT_CLIP_DIST0 + clip_array_size / 4;
107 cull->data.location_frac = clip_array_size % 4;
108 }
109
110 nir_foreach_function(function, nir) {
111 if (function->impl) {
112 nir_metadata_preserve(function->impl,
113 nir_metadata_block_index |
114 nir_metadata_dominance |
115 nir_metadata_live_ssa_defs |
116 nir_metadata_loop_analysis);
117 }
118 }
119
120 return true;
121 }
122
123 bool
124 nir_lower_clip_cull_distance_arrays(nir_shader *nir)
125 {
126 bool progress = false;
127
128 if (nir->info.stage <= MESA_SHADER_GEOMETRY)
129 progress |= combine_clip_cull(nir, &nir->outputs, true);
130
131 if (nir->info.stage > MESA_SHADER_VERTEX)
132 progress |= combine_clip_cull(nir, &nir->inputs, false);
133
134 return progress;
135 }