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 57struct state { 58 nir_builder *builder; 59 nir_variable *vertex_count_vars[NIR_MAX_XFB_STREAMS]; 60 nir_variable *vtxcnt_per_prim_vars[NIR_MAX_XFB_STREAMS]; 61 nir_variable *primitive_count_vars[NIR_MAX_XFB_STREAMS]; 62 bool per_stream; 63 bool count_prims; 64 bool count_vtx_per_prim; 65 bool overwrite_incomplete; 66 bool progress; 67}; 68 69/** 70 * Replace emit_vertex intrinsics with: 71 * 72 * if (vertex_count < max_vertices) { 73 * emit_vertex_with_counter vertex_count, vertex_count_per_primitive (optional) ... 74 * vertex_count += 1 75 * vertex_count_per_primitive += 1 76 * } 77 */ 78static void 79rewrite_emit_vertex(nir_intrinsic_instr *intrin, struct state *state) 80{ 81 nir_builder *b = state->builder; 82 unsigned stream = nir_intrinsic_stream_id(intrin); 83 84 /* Load the vertex count */ 85 b->cursor = nir_before_instr(&intrin->instr); 86 assert(state->vertex_count_vars[stream] != NULL); 87 nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[stream]); 88 nir_ssa_def *count_per_primitive; 89 90 if (state->count_vtx_per_prim) 91 count_per_primitive = nir_load_var(b, state->vtxcnt_per_prim_vars[stream]); 92 else 93 count_per_primitive = nir_ssa_undef(b, 1, 32); 94 95 nir_ssa_def *max_vertices = 96 nir_imm_int(b, b->shader->info.gs.vertices_out); 97 98 /* Create: if (vertex_count < max_vertices) and insert it. 99 * 100 * The new if statement needs to be hooked up to the control flow graph 101 * before we start inserting instructions into it. 102 */ 103 nir_push_if(b, nir_ilt(b, count, max_vertices)); 104 105 nir_emit_vertex_with_counter(b, count, count_per_primitive, stream); 106 107 /* Increment the vertex count by 1 */ 108 nir_store_var(b, state->vertex_count_vars[stream], 109 nir_iadd_imm(b, count, 1), 110 0x1); /* .x */ 111 112 if (state->count_vtx_per_prim) { 113 /* Increment the per-primitive vertex count by 1 */ 114 nir_variable *var = state->vtxcnt_per_prim_vars[stream]; 115 nir_ssa_def *vtx_per_prim_cnt = nir_load_var(b, var); 116 nir_store_var(b, var, 117 nir_iadd_imm(b, vtx_per_prim_cnt, 1), 118 0x1); /* .x */ 119 } 120 121 nir_pop_if(b, NULL); 122 123 nir_instr_remove(&intrin->instr); 124 125 state->progress = true; 126} 127 128/** 129 * Emits code that overwrites incomplete primitives and their vertices. 130 * 131 * A primitive is considered incomplete when it doesn't have enough vertices. 132 * For example, a triangle strip that has 2 or fewer vertices, or a line strip 133 * with 1 vertex are considered incomplete. 134 * 135 * After each end_primitive and at the end of the shader before emitting 136 * set_vertex_and_primitive_count, we check if the primitive that is being 137 * emitted has enough vertices or not, and we adjust the vertex and primitive 138 * counters accordingly. 139 * 140 * This means that the following emit_vertex can reuse the vertex index of 141 * a previous vertex, if the previous primitive was incomplete, so the compiler 142 * backend is expected to simply overwrite any data that belonged to those. 143 */ 144static void 145overwrite_incomplete_primitives(struct state *state, unsigned stream) 146{ 147 assert(state->count_vtx_per_prim); 148 149 nir_builder *b = state->builder; 150 unsigned outprim = b->shader->info.gs.output_primitive; 151 unsigned outprim_min_vertices; 152 153 if (outprim == GL_POINTS) 154 outprim_min_vertices = 1; 155 else if (outprim == GL_LINE_STRIP) 156 outprim_min_vertices = 2; 157 else if (outprim == GL_TRIANGLE_STRIP) 158 outprim_min_vertices = 3; 159 else 160 unreachable("Invalid GS output primitive type."); 161 162 /* Total count of vertices emitted so far. */ 163 nir_ssa_def *vtxcnt_total = 164 nir_load_var(b, state->vertex_count_vars[stream]); 165 166 /* Number of vertices emitted for the last primitive */ 167 nir_ssa_def *vtxcnt_per_primitive = 168 nir_load_var(b, state->vtxcnt_per_prim_vars[stream]); 169 170 /* See if the current primitive is a incomplete */ 171 nir_ssa_def *is_inc_prim = 172 nir_ilt(b, vtxcnt_per_primitive, nir_imm_int(b, outprim_min_vertices)); 173 174 /* Number of vertices in the incomplete primitive */ 175 nir_ssa_def *num_inc_vtx = 176 nir_bcsel(b, is_inc_prim, vtxcnt_per_primitive, nir_imm_int(b, 0)); 177 178 /* Store corrected total vertex count */ 179 nir_store_var(b, state->vertex_count_vars[stream], 180 nir_isub(b, vtxcnt_total, num_inc_vtx), 181 0x1); /* .x */ 182 183 if (state->count_prims) { 184 /* Number of incomplete primitives (0 or 1) */ 185 nir_ssa_def *num_inc_prim = nir_b2i32(b, is_inc_prim); 186 187 /* Store corrected primitive count */ 188 nir_ssa_def *prim_cnt = nir_load_var(b, state->primitive_count_vars[stream]); 189 nir_store_var(b, state->primitive_count_vars[stream], 190 nir_isub(b, prim_cnt, num_inc_prim), 191 0x1); /* .x */ 192 } 193} 194 195/** 196 * Replace end_primitive with end_primitive_with_counter. 197 */ 198static void 199rewrite_end_primitive(nir_intrinsic_instr *intrin, struct state *state) 200{ 201 nir_builder *b = state->builder; 202 unsigned stream = nir_intrinsic_stream_id(intrin); 203 204 b->cursor = nir_before_instr(&intrin->instr); 205 assert(state->vertex_count_vars[stream] != NULL); 206 nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[stream]); 207 nir_ssa_def *count_per_primitive; 208 209 if (state->count_vtx_per_prim) 210 count_per_primitive = nir_load_var(b, state->vtxcnt_per_prim_vars[stream]); 211 else 212 count_per_primitive = nir_ssa_undef(b, count->num_components, count->bit_size); 213 214 nir_end_primitive_with_counter(b, count, count_per_primitive, stream); 215 216 if (state->count_prims) { 217 /* Increment the primitive count by 1 */ 218 nir_ssa_def *prim_cnt = nir_load_var(b, state->primitive_count_vars[stream]); 219 nir_store_var(b, state->primitive_count_vars[stream], 220 nir_iadd_imm(b, prim_cnt, 1), 221 0x1); /* .x */ 222 } 223 224 if (state->count_vtx_per_prim) { 225 if (state->overwrite_incomplete) 226 overwrite_incomplete_primitives(state, stream); 227 228 /* Store 0 to per-primitive vertex count */ 229 nir_store_var(b, state->vtxcnt_per_prim_vars[stream], 230 nir_imm_int(b, 0), 231 0x1); /* .x */ 232 } 233 234 nir_instr_remove(&intrin->instr); 235 236 state->progress = true; 237} 238 239static bool 240rewrite_intrinsics(nir_block *block, struct state *state) 241{ 242 nir_foreach_instr_safe(instr, block) { 243 if (instr->type != nir_instr_type_intrinsic) 244 continue; 245 246 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 247 switch (intrin->intrinsic) { 248 case nir_intrinsic_emit_vertex: 249 case nir_intrinsic_emit_vertex_with_counter: 250 rewrite_emit_vertex(intrin, state); 251 break; 252 case nir_intrinsic_end_primitive: 253 case nir_intrinsic_end_primitive_with_counter: 254 rewrite_end_primitive(intrin, state); 255 break; 256 default: 257 /* not interesting; skip this */ 258 break; 259 } 260 } 261 262 return true; 263} 264 265/** 266 * Add a set_vertex_and_primitive_count intrinsic at the end of the program 267 * (representing the final total vertex and primitive count). 268 */ 269static void 270append_set_vertex_and_primitive_count(nir_block *end_block, struct state *state) 271{ 272 nir_builder *b = state->builder; 273 nir_shader *shader = state->builder->shader; 274 275 /* Insert the new intrinsic in all of the predecessors of the end block, 276 * but before any jump instructions (return). 277 */ 278 set_foreach(end_block->predecessors, entry) { 279 nir_block *pred = (nir_block *) entry->key; 280 b->cursor = nir_after_block_before_jump(pred); 281 282 for (unsigned stream = 0; stream < NIR_MAX_XFB_STREAMS; ++stream) { 283 /* When it's not per-stream, we only need to write one variable. */ 284 if (!state->per_stream && stream != 0) 285 continue; 286 287 nir_ssa_def *vtx_cnt; 288 nir_ssa_def *prim_cnt; 289 290 if (state->per_stream && !(shader->info.gs.active_stream_mask & (1 << stream))) { 291 /* Inactive stream: vertex count is 0, primitive count is 0 or undef. */ 292 vtx_cnt = nir_imm_int(b, 0); 293 prim_cnt = state->count_prims 294 ? nir_imm_int(b, 0) 295 : nir_ssa_undef(b, 1, 32); 296 } else { 297 if (state->overwrite_incomplete) 298 overwrite_incomplete_primitives(state, stream); 299 300 vtx_cnt = nir_load_var(b, state->vertex_count_vars[stream]); 301 prim_cnt = state->count_prims 302 ? nir_load_var(b, state->primitive_count_vars[stream]) 303 : nir_ssa_undef(b, 1, 32); 304 } 305 306 nir_set_vertex_and_primitive_count(b, vtx_cnt, prim_cnt, stream); 307 state->progress = true; 308 } 309 } 310} 311 312/** 313 * Check to see if there are any blocks that need set_vertex_and_primitive_count 314 * 315 * If every block that could need the set_vertex_and_primitive_count intrinsic 316 * already has one, there is nothing for this pass to do. 317 */ 318static bool 319a_block_needs_set_vertex_and_primitive_count(nir_block *end_block, bool per_stream) 320{ 321 set_foreach(end_block->predecessors, entry) { 322 nir_block *pred = (nir_block *) entry->key; 323 324 325 for (unsigned stream = 0; stream < NIR_MAX_XFB_STREAMS; ++stream) { 326 /* When it's not per-stream, we only need to write one variable. */ 327 if (!per_stream && stream != 0) 328 continue; 329 330 bool found = false; 331 332 nir_foreach_instr_reverse(instr, pred) { 333 if (instr->type != nir_instr_type_intrinsic) 334 continue; 335 336 const nir_intrinsic_instr *const intrin = 337 nir_instr_as_intrinsic(instr); 338 339 if (intrin->intrinsic == nir_intrinsic_set_vertex_and_primitive_count && 340 intrin->const_index[0] == stream) { 341 found = true; 342 break; 343 } 344 } 345 346 if (!found) 347 return true; 348 } 349 } 350 351 return false; 352} 353 354bool 355nir_lower_gs_intrinsics(nir_shader *shader, nir_lower_gs_intrinsics_flags options) 356{ 357 bool per_stream = options & nir_lower_gs_intrinsics_per_stream; 358 bool count_primitives = options & nir_lower_gs_intrinsics_count_primitives; 359 bool overwrite_incomplete = options & nir_lower_gs_intrinsics_overwrite_incomplete; 360 bool count_vtx_per_prim = 361 overwrite_incomplete || 362 (options & nir_lower_gs_intrinsics_count_vertices_per_primitive); 363 364 struct state state; 365 state.progress = false; 366 state.count_prims = count_primitives; 367 state.count_vtx_per_prim = count_vtx_per_prim; 368 state.overwrite_incomplete = overwrite_incomplete; 369 state.per_stream = per_stream; 370 371 nir_function_impl *impl = nir_shader_get_entrypoint(shader); 372 assert(impl); 373 374 if (!a_block_needs_set_vertex_and_primitive_count(impl->end_block, per_stream)) 375 return false; 376 377 nir_builder b; 378 nir_builder_init(&b, impl); 379 state.builder = &b; 380 381 b.cursor = nir_before_cf_list(&impl->body); 382 383 for (unsigned i = 0; i < NIR_MAX_XFB_STREAMS; i++) { 384 if (per_stream && !(shader->info.gs.active_stream_mask & (1 << i))) 385 continue; 386 387 if (i == 0 || per_stream) { 388 state.vertex_count_vars[i] = 389 nir_local_variable_create(impl, glsl_uint_type(), "vertex_count"); 390 /* initialize to 0 */ 391 nir_store_var(&b, state.vertex_count_vars[i], nir_imm_int(&b, 0), 0x1); 392 393 if (count_primitives) { 394 state.primitive_count_vars[i] = 395 nir_local_variable_create(impl, glsl_uint_type(), "primitive_count"); 396 /* initialize to 1 */ 397 nir_store_var(&b, state.primitive_count_vars[i], nir_imm_int(&b, 1), 0x1); 398 } 399 if (count_vtx_per_prim) { 400 state.vtxcnt_per_prim_vars[i] = 401 nir_local_variable_create(impl, glsl_uint_type(), "vertices_per_primitive"); 402 /* initialize to 0 */ 403 nir_store_var(&b, state.vtxcnt_per_prim_vars[i], nir_imm_int(&b, 0), 0x1); 404 } 405 } else { 406 /* If per_stream is false, we only have one counter of each kind which we 407 * want to use for all streams. Duplicate the counter pointers so all 408 * streams use the same counters. 409 */ 410 state.vertex_count_vars[i] = state.vertex_count_vars[0]; 411 412 if (count_primitives) 413 state.primitive_count_vars[i] = state.primitive_count_vars[0]; 414 if (count_vtx_per_prim) 415 state.vtxcnt_per_prim_vars[i] = state.vtxcnt_per_prim_vars[0]; 416 } 417 } 418 419 nir_foreach_block_safe(block, impl) 420 rewrite_intrinsics(block, &state); 421 422 /* This only works because we have a single main() function. */ 423 append_set_vertex_and_primitive_count(impl->end_block, &state); 424 425 nir_metadata_preserve(impl, 0); 426 427 return state.progress; 428} 429