#include "nir.h"
#include "nir_builder.h"
+#include "nir_xfb_info.h"
/**
* \file nir_lower_gs_intrinsics.c
struct state {
nir_builder *builder;
- nir_variable *vertex_count_var;
+ nir_variable *vertex_count_vars[NIR_MAX_XFB_STREAMS];
bool progress;
};
rewrite_emit_vertex(nir_intrinsic_instr *intrin, struct state *state)
{
nir_builder *b = state->builder;
+ unsigned stream = nir_intrinsic_stream_id(intrin);
/* Load the vertex count */
b->cursor = nir_before_instr(&intrin->instr);
- nir_ssa_def *count = nir_load_var(b, state->vertex_count_var);
+ assert(state->vertex_count_vars[stream] != NULL);
+ nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[stream]);
- nir_ssa_def *max_vertices = nir_imm_int(b, b->shader->info.gs.vertices_out);
+ nir_ssa_def *max_vertices =
+ nir_imm_int(b, b->shader->info.gs.vertices_out);
/* Create: if (vertex_count < max_vertices) and insert it.
*
* The new if statement needs to be hooked up to the control flow graph
* before we start inserting instructions into it.
*/
- nir_if *if_stmt = nir_if_create(b->shader);
- if_stmt->condition = nir_src_for_ssa(nir_ilt(b, count, max_vertices));
- nir_builder_cf_insert(b, &if_stmt->cf_node);
-
- /* Fill out the new then-block */
- b->cursor = nir_after_cf_list(&if_stmt->then_list);
+ nir_push_if(b, nir_ilt(b, count, max_vertices));
nir_intrinsic_instr *lowered =
nir_intrinsic_instr_create(b->shader,
nir_intrinsic_emit_vertex_with_counter);
- nir_intrinsic_set_stream_id(lowered, nir_intrinsic_stream_id(intrin));
+ nir_intrinsic_set_stream_id(lowered, stream);
lowered->src[0] = nir_src_for_ssa(count);
nir_builder_instr_insert(b, &lowered->instr);
/* Increment the vertex count by 1 */
- nir_store_var(b, state->vertex_count_var,
+ nir_store_var(b, state->vertex_count_vars[stream],
nir_iadd(b, count, nir_imm_int(b, 1)),
0x1); /* .x */
+ nir_pop_if(b, NULL);
+
nir_instr_remove(&intrin->instr);
state->progress = true;
rewrite_end_primitive(nir_intrinsic_instr *intrin, struct state *state)
{
nir_builder *b = state->builder;
+ unsigned stream = nir_intrinsic_stream_id(intrin);
b->cursor = nir_before_instr(&intrin->instr);
- nir_ssa_def *count = nir_load_var(b, state->vertex_count_var);
+ assert(state->vertex_count_vars[stream] != NULL);
+ nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[stream]);
nir_intrinsic_instr *lowered =
nir_intrinsic_instr_create(b->shader,
nir_intrinsic_end_primitive_with_counter);
- nir_intrinsic_set_stream_id(lowered, nir_intrinsic_stream_id(intrin));
+ nir_intrinsic_set_stream_id(lowered, stream);
lowered->src[0] = nir_src_for_ssa(count);
nir_builder_instr_insert(b, &lowered->instr);
/* Insert the new intrinsic in all of the predecessors of the end block,
* but before any jump instructions (return).
*/
- struct set_entry *entry;
set_foreach(end_block->predecessors, entry) {
nir_block *pred = (nir_block *) entry->key;
b->cursor = nir_after_block_before_jump(pred);
- nir_ssa_def *count = nir_load_var(b, state->vertex_count_var);
+ nir_ssa_def *count = nir_load_var(b, state->vertex_count_vars[0]);
nir_intrinsic_instr *set_vertex_count =
nir_intrinsic_instr_create(shader, nir_intrinsic_set_vertex_count);
}
bool
-nir_lower_gs_intrinsics(nir_shader *shader)
+nir_lower_gs_intrinsics(nir_shader *shader, bool per_stream)
{
struct state state;
state.progress = false;
- /* Create the counter variable */
- nir_variable *var = rzalloc(shader, nir_variable);
- var->data.mode = nir_var_global;
- var->type = glsl_uint_type();
- var->name = "vertex_count";
- var->constant_initializer = rzalloc(shader, nir_constant); /* initialize to 0 */
-
- exec_list_push_tail(&shader->globals, &var->node);
- state.vertex_count_var = var;
-
- nir_foreach_function(function, shader) {
- if (function->impl) {
- nir_builder b;
- nir_builder_init(&b, function->impl);
- state.builder = &b;
+ nir_function_impl *impl = nir_shader_get_entrypoint(shader);
+ assert(impl);
- nir_foreach_block_safe(block, function->impl) {
- rewrite_intrinsics(block, &state);
- }
+ nir_builder b;
+ nir_builder_init(&b, impl);
+ state.builder = &b;
- /* This only works because we have a single main() function. */
- append_set_vertex_count(function->impl->end_block, &state);
+ /* Create the counter variables */
+ b.cursor = nir_before_cf_list(&impl->body);
+ for (unsigned i = 0; i < NIR_MAX_XFB_STREAMS; i++) {
+ if (per_stream && !(shader->info.gs.active_stream_mask & (1 << i)))
+ continue;
- nir_metadata_preserve(function->impl, 0);
+ if (i == 0 || per_stream) {
+ state.vertex_count_vars[i] =
+ nir_local_variable_create(impl, glsl_uint_type(), "vertex_count");
+ /* initialize to 0 */
+ nir_store_var(&b, state.vertex_count_vars[i], nir_imm_int(&b, 0), 0x1);
+ } else {
+ /* If per_stream is false, we only have one counter which we want to use
+ * for all streams. Duplicate the counter pointer so all streams use the
+ * same counter.
+ */
+ state.vertex_count_vars[i] = state.vertex_count_vars[0];
}
}
+ nir_foreach_block_safe(block, impl)
+ rewrite_intrinsics(block, &state);
+
+ /* This only works because we have a single main() function. */
+ if (!per_stream)
+ append_set_vertex_count(impl->end_block, &state);
+
+ nir_metadata_preserve(impl, 0);
+
return state.progress;
}