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