2 * Copyright © 2015 Intel Corporation
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:
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
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
25 #include "nir_builder.h"
28 * \file nir_lower_gs_intrinsics.c
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.
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.
40 * The count of emitted vertices can also be useful in buffer offset
41 * calculations, so drivers know where to write the GS output.
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.
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
58 nir_variable
*vertex_count_var
;
63 * Replace emit_vertex intrinsics with:
65 * if (vertex_count < max_vertices) {
66 * emit_vertex_with_counter vertex_count ...
71 rewrite_emit_vertex(nir_intrinsic_instr
*intrin
, struct state
*state
)
73 nir_builder
*b
= state
->builder
;
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
);
79 nir_ssa_def
*max_vertices
=
80 nir_imm_int(b
, b
->shader
->info
.gs
.vertices_out
);
82 /* Create: if (vertex_count < max_vertices) and insert it.
84 * The new if statement needs to be hooked up to the control flow graph
85 * before we start inserting instructions into it.
87 nir_push_if(b
, nir_ilt(b
, count
, max_vertices
));
89 nir_intrinsic_instr
*lowered
=
90 nir_intrinsic_instr_create(b
->shader
,
91 nir_intrinsic_emit_vertex_with_counter
);
92 nir_intrinsic_set_stream_id(lowered
, nir_intrinsic_stream_id(intrin
));
93 lowered
->src
[0] = nir_src_for_ssa(count
);
94 nir_builder_instr_insert(b
, &lowered
->instr
);
96 /* Increment the vertex count by 1 */
97 nir_store_var(b
, state
->vertex_count_var
,
98 nir_iadd(b
, count
, nir_imm_int(b
, 1)),
103 nir_instr_remove(&intrin
->instr
);
105 state
->progress
= true;
109 * Replace end_primitive with end_primitive_with_counter.
112 rewrite_end_primitive(nir_intrinsic_instr
*intrin
, struct state
*state
)
114 nir_builder
*b
= state
->builder
;
116 b
->cursor
= nir_before_instr(&intrin
->instr
);
117 nir_ssa_def
*count
= nir_load_var(b
, state
->vertex_count_var
);
119 nir_intrinsic_instr
*lowered
=
120 nir_intrinsic_instr_create(b
->shader
,
121 nir_intrinsic_end_primitive_with_counter
);
122 nir_intrinsic_set_stream_id(lowered
, nir_intrinsic_stream_id(intrin
));
123 lowered
->src
[0] = nir_src_for_ssa(count
);
124 nir_builder_instr_insert(b
, &lowered
->instr
);
126 nir_instr_remove(&intrin
->instr
);
128 state
->progress
= true;
132 rewrite_intrinsics(nir_block
*block
, struct state
*state
)
134 nir_foreach_instr_safe(instr
, block
) {
135 if (instr
->type
!= nir_instr_type_intrinsic
)
138 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
139 switch (intrin
->intrinsic
) {
140 case nir_intrinsic_emit_vertex
:
141 rewrite_emit_vertex(intrin
, state
);
143 case nir_intrinsic_end_primitive
:
144 rewrite_end_primitive(intrin
, state
);
147 /* not interesting; skip this */
156 * Add a set_vertex_count intrinsic at the end of the program
157 * (representing the final vertex count).
160 append_set_vertex_count(nir_block
*end_block
, struct state
*state
)
162 nir_builder
*b
= state
->builder
;
163 nir_shader
*shader
= state
->builder
->shader
;
165 /* Insert the new intrinsic in all of the predecessors of the end block,
166 * but before any jump instructions (return).
168 set_foreach(end_block
->predecessors
, entry
) {
169 nir_block
*pred
= (nir_block
*) entry
->key
;
170 b
->cursor
= nir_after_block_before_jump(pred
);
172 nir_ssa_def
*count
= nir_load_var(b
, state
->vertex_count_var
);
174 nir_intrinsic_instr
*set_vertex_count
=
175 nir_intrinsic_instr_create(shader
, nir_intrinsic_set_vertex_count
);
176 set_vertex_count
->src
[0] = nir_src_for_ssa(count
);
178 nir_builder_instr_insert(b
, &set_vertex_count
->instr
);
183 nir_lower_gs_intrinsics(nir_shader
*shader
)
186 state
.progress
= false;
188 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
192 nir_builder_init(&b
, impl
);
195 /* Create the counter variable */
196 state
.vertex_count_var
=
197 nir_local_variable_create(impl
, glsl_uint_type(), "vertex_count");
198 /* initialize to 0 */
199 b
.cursor
= nir_before_cf_list(&impl
->body
);
200 nir_store_var(&b
, state
.vertex_count_var
, nir_imm_int(&b
, 0), 0x1);
202 nir_foreach_block_safe(block
, impl
)
203 rewrite_intrinsics(block
, &state
);
205 /* This only works because we have a single main() function. */
206 append_set_vertex_count(impl
->end_block
, &state
);
208 nir_metadata_preserve(impl
, 0);
210 return state
.progress
;