nir: Fix printing of individual instructions with io semantics.
[mesa.git] / src / compiler / nir / nir_lower_clip_disable.c
1 /*
2 * Copyright © 2020 Mike Blumenkrantz
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 * Authors:
24 * Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30
31 /**
32 * This pass uses the enabled clip planes from the rasterizer state to rewrite
33 * vertex shader store operations and store an undef to the corresponding gl_ClipDistance[n]
34 * value if the plane is disabled
35 */
36
37 /* recursively nest if/else blocks until we get to an array index,
38 * then overwrite it if that plane isn't enabled
39 */
40 static void
41 recursive_if_chain(nir_builder *b, nir_deref_instr *deref, nir_ssa_def *value, unsigned clip_plane_enable, nir_ssa_def *index, unsigned start, unsigned end)
42 {
43 if (start == end - 1) {
44 /* store the original value again if the clip plane is enabled */
45 if (clip_plane_enable & (1 << start))
46 nir_store_deref(b, deref, value, 1 << start);
47 else
48 nir_store_deref(b, deref, nir_ssa_undef(b, 1, 32), 1 << start);
49 return;
50 }
51
52 unsigned mid = start + (end - start) / 2;
53 nir_push_if(b, nir_ilt(b, index, nir_imm_int(b, mid)));
54 recursive_if_chain(b, deref, value, clip_plane_enable, index, start, mid);
55 nir_push_else(b, NULL);
56 recursive_if_chain(b, deref, value, clip_plane_enable, index, mid, end);
57 nir_pop_if(b, NULL);
58 }
59
60 /* vulkan (and some drivers) provides no concept of enabling clip planes through api,
61 * so we rewrite disabled clip planes to an undefined value in order to disable them
62 */
63 static bool
64 lower_clip_plane_store(nir_intrinsic_instr *instr, unsigned clip_plane_enable, nir_builder *b)
65 {
66 nir_variable *out;
67 unsigned plane;
68
69 if (instr->intrinsic != nir_intrinsic_store_deref)
70 return false;
71
72 nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
73
74 out = nir_deref_instr_get_variable(deref);
75 if ((out->data.location != VARYING_SLOT_CLIP_DIST0) ||
76 out->data.mode != nir_var_shader_out)
77 return false;
78
79 b->cursor = nir_after_instr(&instr->instr);
80 if (nir_src_is_const(deref->arr.index)) {
81 /* storing using a constant index */
82 plane = nir_src_as_uint(deref->arr.index);
83 /* no need to make changes if the clip plane is enabled */
84 if (clip_plane_enable & (1 << plane))
85 return false;
86
87 nir_store_deref(b, deref, nir_ssa_undef(b, 1, 32), 1 << plane);
88 } else {
89 /* storing using a variable index */
90 nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1);
91 unsigned length = glsl_get_length(nir_deref_instr_parent(deref)->type);
92
93 recursive_if_chain(b, deref, instr->src[1].ssa, clip_plane_enable, index, 0, length);
94 }
95 nir_instr_remove(&instr->instr);
96 return true;
97 }
98
99 bool
100 nir_lower_clip_disable(nir_shader *shader, unsigned clip_plane_enable)
101 {
102 bool progress = false;
103
104 nir_foreach_shader_out_variable(var, shader) {
105 if (var->data.location == VARYING_SLOT_CLIP_DIST0) {
106 unsigned size = glsl_get_length(var->type);
107 /* if currently-enabled planes match used planes then no-op */
108 if (clip_plane_enable == (1u << size) - 1)
109 return false;
110 }
111 }
112
113 nir_foreach_function(function, shader) {
114 if (function->impl) {
115 nir_builder builder;
116 nir_builder_init(&builder, function->impl);
117 nir_foreach_block(block, function->impl) {
118 nir_foreach_instr_safe(instr, block) {
119 if (instr->type == nir_instr_type_intrinsic)
120 progress |= lower_clip_plane_store(nir_instr_as_intrinsic(instr),
121 clip_plane_enable,
122 &builder);
123 }
124 }
125
126 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
127 }
128 }
129
130 return progress;
131 }