Path: blob/21.2-virgl/src/compiler/nir/nir_gs_count_vertices.c
4547 views
/*1* Copyright © 2015 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "nir.h"24#include "nir_builder.h"2526static nir_intrinsic_instr *27as_intrinsic(nir_instr *instr, nir_intrinsic_op op)28{29if (instr->type != nir_instr_type_intrinsic)30return NULL;3132nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);33if (intrin->intrinsic != op)34return NULL;3536return intrin;37}3839static nir_intrinsic_instr *40as_set_vertex_and_primitive_count(nir_instr *instr)41{42return as_intrinsic(instr, nir_intrinsic_set_vertex_and_primitive_count);43}4445/**46* Count the number of vertices/primitives emitted by a geometry shader per stream.47* If a constant number of vertices is emitted, the output is set to48* that number, otherwise it is unknown at compile time and the49* result will be -1.50*51* This only works if you've used nir_lower_gs_intrinsics() to do vertex52* counting at the NIR level.53*/54void55nir_gs_count_vertices_and_primitives(const nir_shader *shader,56int *out_vtxcnt,57int *out_prmcnt,58unsigned num_streams)59{60assert(num_streams);6162int vtxcnt_arr[4] = {-1, -1, -1, -1};63int prmcnt_arr[4] = {-1, -1, -1, -1};64bool cnt_found[4] = {false, false, false, false};6566nir_foreach_function(function, shader) {67if (!function->impl)68continue;6970/* set_vertex_and_primitive_count intrinsics only appear in predecessors of the71* end block. So we don't need to walk all of them.72*/73set_foreach(function->impl->end_block->predecessors, entry) {74nir_block *block = (nir_block *) entry->key;7576nir_foreach_instr_reverse(instr, block) {77nir_intrinsic_instr *intrin = as_set_vertex_and_primitive_count(instr);78if (!intrin)79continue;8081unsigned stream = nir_intrinsic_stream_id(intrin);82if (stream >= num_streams)83continue;8485int vtxcnt = -1;86int prmcnt = -1;8788/* If the number of vertices/primitives is compile-time known, we use that,89* otherwise we leave it at -1 which means that it's unknown.90*/91if (nir_src_is_const(intrin->src[0]))92vtxcnt = nir_src_as_int(intrin->src[0]);93if (nir_src_is_const(intrin->src[1]))94prmcnt = nir_src_as_int(intrin->src[1]);9596/* We've found contradictory set_vertex_and_primitive_count intrinsics.97* This can happen if there are early-returns in main() and98* different paths emit different numbers of vertices.99*/100if (cnt_found[stream] && vtxcnt != vtxcnt_arr[stream])101vtxcnt = -1;102if (cnt_found[stream] && prmcnt != prmcnt_arr[stream])103prmcnt = -1;104105vtxcnt_arr[stream] = vtxcnt;106prmcnt_arr[stream] = prmcnt;107cnt_found[stream] = true;108}109}110}111112if (out_vtxcnt)113memcpy(out_vtxcnt, vtxcnt_arr, num_streams * sizeof(int));114if (out_prmcnt)115memcpy(out_prmcnt, prmcnt_arr, num_streams * sizeof(int));116}117118119