nir: Switch the arguments to nir_foreach_instr
[mesa.git] / src / compiler / nir / nir_lower_gs_intrinsics.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 nir_lower_gs_intrinsics.c
29 *
30 * Geometry Shaders can call EmitVertex()/EmitStreamVertex() to output an
31 * arbitrary number of vertices. However, the shader must declare the maximum
32 * number of vertices that it will ever output - further attempts to emit
33 * vertices result in undefined behavior according to the GLSL specification.
34 *
35 * Drivers might use this maximum number of vertices to allocate enough space
36 * to hold the geometry shader's output. Some drivers (such as i965) need to
37 * implement "safety checks" which ensure that the shader hasn't emitted too
38 * many vertices, to avoid overflowing that space and trashing other memory.
39 *
40 * The count of emitted vertices can also be useful in buffer offset
41 * calculations, so drivers know where to write the GS output.
42 *
43 * However, for simple geometry shaders that emit a statically determinable
44 * number of vertices, this extra bookkeeping is unnecessary and inefficient.
45 * By tracking the vertex count in NIR, we allow constant folding/propagation
46 * and dead control flow optimizations to eliminate most of it where possible.
47 *
48 * This pass introduces a new global variable which stores the current vertex
49 * count (initialized to 0), and converts emit_vertex/end_primitive intrinsics
50 * to their *_with_counter variants. emit_vertex is also wrapped in a safety
51 * check to avoid buffer overflows. Finally, it adds a set_vertex_count
52 * intrinsic at the end of the program, informing the driver of the final
53 * vertex count.
54 */
55
56 struct state {
57 nir_builder *builder;
58 nir_variable *vertex_count_var;
59 bool progress;
60 };
61
62 /**
63 * Replace emit_vertex intrinsics with:
64 *
65 * if (vertex_count < max_vertices) {
66 * emit_vertex_with_counter vertex_count ...
67 * vertex_count += 1
68 * }
69 */
70 static void
71 rewrite_emit_vertex(nir_intrinsic_instr *intrin, struct state *state)
72 {
73 nir_builder *b = state->builder;
74
75 /* Load the vertex count */
76 b->cursor = nir_before_instr(&intrin->instr);
77 nir_ssa_def *count = nir_load_var(b, state->vertex_count_var);
78
79 nir_ssa_def *max_vertices = nir_imm_int(b, b->shader->info.gs.vertices_out);
80
81 /* Create: if (vertex_count < max_vertices) and insert it.
82 *
83 * The new if statement needs to be hooked up to the control flow graph
84 * before we start inserting instructions into it.
85 */
86 nir_if *if_stmt = nir_if_create(b->shader);
87 if_stmt->condition = nir_src_for_ssa(nir_ilt(b, count, max_vertices));
88 nir_builder_cf_insert(b, &if_stmt->cf_node);
89
90 /* Fill out the new then-block */
91 b->cursor = nir_after_cf_list(&if_stmt->then_list);
92
93 nir_intrinsic_instr *lowered =
94 nir_intrinsic_instr_create(b->shader,
95 nir_intrinsic_emit_vertex_with_counter);
96 nir_intrinsic_set_stream_id(lowered, nir_intrinsic_stream_id(intrin));
97 lowered->src[0] = nir_src_for_ssa(count);
98 nir_builder_instr_insert(b, &lowered->instr);
99
100 /* Increment the vertex count by 1 */
101 nir_store_var(b, state->vertex_count_var,
102 nir_iadd(b, count, nir_imm_int(b, 1)),
103 0x1); /* .x */
104
105 nir_instr_remove(&intrin->instr);
106
107 state->progress = true;
108 }
109
110 /**
111 * Replace end_primitive with end_primitive_with_counter.
112 */
113 static void
114 rewrite_end_primitive(nir_intrinsic_instr *intrin, struct state *state)
115 {
116 nir_builder *b = state->builder;
117
118 b->cursor = nir_before_instr(&intrin->instr);
119 nir_ssa_def *count = nir_load_var(b, state->vertex_count_var);
120
121 nir_intrinsic_instr *lowered =
122 nir_intrinsic_instr_create(b->shader,
123 nir_intrinsic_end_primitive_with_counter);
124 nir_intrinsic_set_stream_id(lowered, nir_intrinsic_stream_id(intrin));
125 lowered->src[0] = nir_src_for_ssa(count);
126 nir_builder_instr_insert(b, &lowered->instr);
127
128 nir_instr_remove(&intrin->instr);
129
130 state->progress = true;
131 }
132
133 static bool
134 rewrite_intrinsics(nir_block *block, struct state *state)
135 {
136 nir_foreach_instr_safe(instr, block) {
137 if (instr->type != nir_instr_type_intrinsic)
138 continue;
139
140 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
141 switch (intrin->intrinsic) {
142 case nir_intrinsic_emit_vertex:
143 rewrite_emit_vertex(intrin, state);
144 break;
145 case nir_intrinsic_end_primitive:
146 rewrite_end_primitive(intrin, state);
147 break;
148 default:
149 /* not interesting; skip this */
150 break;
151 }
152 }
153
154 return true;
155 }
156
157 /**
158 * Add a set_vertex_count intrinsic at the end of the program
159 * (representing the final vertex count).
160 */
161 static void
162 append_set_vertex_count(nir_block *end_block, struct state *state)
163 {
164 nir_builder *b = state->builder;
165 nir_shader *shader = state->builder->shader;
166
167 /* Insert the new intrinsic in all of the predecessors of the end block,
168 * but before any jump instructions (return).
169 */
170 struct set_entry *entry;
171 set_foreach(end_block->predecessors, entry) {
172 nir_block *pred = (nir_block *) entry->key;
173 b->cursor = nir_after_block_before_jump(pred);
174
175 nir_ssa_def *count = nir_load_var(b, state->vertex_count_var);
176
177 nir_intrinsic_instr *set_vertex_count =
178 nir_intrinsic_instr_create(shader, nir_intrinsic_set_vertex_count);
179 set_vertex_count->src[0] = nir_src_for_ssa(count);
180
181 nir_builder_instr_insert(b, &set_vertex_count->instr);
182 }
183 }
184
185 bool
186 nir_lower_gs_intrinsics(nir_shader *shader)
187 {
188 struct state state;
189 state.progress = false;
190
191 /* Create the counter variable */
192 nir_variable *var = rzalloc(shader, nir_variable);
193 var->data.mode = nir_var_global;
194 var->type = glsl_uint_type();
195 var->name = "vertex_count";
196 var->constant_initializer = rzalloc(shader, nir_constant); /* initialize to 0 */
197
198 exec_list_push_tail(&shader->globals, &var->node);
199 state.vertex_count_var = var;
200
201 nir_foreach_function(shader, function) {
202 if (function->impl) {
203 nir_builder b;
204 nir_builder_init(&b, function->impl);
205 state.builder = &b;
206
207 nir_foreach_block_safe(block, function->impl) {
208 rewrite_intrinsics(block, &state);
209 }
210
211 /* This only works because we have a single main() function. */
212 append_set_vertex_count(function->impl->end_block, &state);
213
214 nir_metadata_preserve(function->impl, 0);
215 }
216 }
217
218 return state.progress;
219 }