xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_gs_count_vertices.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 
27 static nir_intrinsic_instr *
as_intrinsic(nir_instr * instr,nir_intrinsic_op op)28 as_intrinsic(nir_instr *instr, nir_intrinsic_op op)
29 {
30    if (instr->type != nir_instr_type_intrinsic)
31       return NULL;
32 
33    nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
34    if (intrin->intrinsic != op)
35       return NULL;
36 
37    return intrin;
38 }
39 
40 static nir_intrinsic_instr *
as_set_vertex_and_primitive_count(nir_instr * instr)41 as_set_vertex_and_primitive_count(nir_instr *instr)
42 {
43    return as_intrinsic(instr, nir_intrinsic_set_vertex_and_primitive_count);
44 }
45 
46 /**
47  * Count the number of vertices/primitives emitted by a geometry shader per stream.
48  * If a constant number of vertices is emitted, the output is set to
49  * that number, otherwise it is unknown at compile time and the
50  * result will be -1.
51  *
52  * This only works if you've used nir_lower_gs_intrinsics() to do vertex
53  * counting at the NIR level.
54  */
55 void
nir_gs_count_vertices_and_primitives(const nir_shader * shader,int * out_vtxcnt,int * out_prmcnt,int * out_decomposed_prmcnt,unsigned num_streams)56 nir_gs_count_vertices_and_primitives(const nir_shader *shader,
57                                      int *out_vtxcnt,
58                                      int *out_prmcnt,
59                                      int *out_decomposed_prmcnt,
60                                      unsigned num_streams)
61 {
62    assert(num_streams);
63 
64    int vtxcnt_arr[4] = { -1, -1, -1, -1 };
65    int prmcnt_arr[4] = { -1, -1, -1, -1 };
66    int decomposed_prmcnt_arr[4] = { -1, -1, -1, -1 };
67    bool cnt_found[4] = { false, false, false, false };
68 
69    nir_foreach_function_impl(impl, shader) {
70       /* set_vertex_and_primitive_count intrinsics only appear in predecessors of the
71        * end block.  So we don't need to walk all of them.
72        */
73       set_foreach(impl->end_block->predecessors, entry) {
74          nir_block *block = (nir_block *)entry->key;
75 
76          nir_foreach_instr_reverse(instr, block) {
77             nir_intrinsic_instr *intrin = as_set_vertex_and_primitive_count(instr);
78             if (!intrin)
79                continue;
80 
81             unsigned stream = nir_intrinsic_stream_id(intrin);
82             if (stream >= num_streams)
83                continue;
84 
85             int vtxcnt = -1;
86             int prmcnt = -1;
87             int decomposed_prmcnt = -1;
88 
89             /* If the number of vertices/primitives is compile-time known, we use that,
90              * otherwise we leave it at -1 which means that it's unknown.
91              */
92             if (nir_src_is_const(intrin->src[0]))
93                vtxcnt = nir_src_as_int(intrin->src[0]);
94             if (nir_src_is_const(intrin->src[1]))
95                prmcnt = nir_src_as_int(intrin->src[1]);
96             if (nir_src_is_const(intrin->src[2]))
97                decomposed_prmcnt = nir_src_as_int(intrin->src[2]);
98 
99             /* We've found contradictory set_vertex_and_primitive_count intrinsics.
100              * This can happen if there are early-returns in main() and
101              * different paths emit different numbers of vertices.
102              */
103             if (cnt_found[stream] && vtxcnt != vtxcnt_arr[stream])
104                vtxcnt = -1;
105             if (cnt_found[stream] && prmcnt != prmcnt_arr[stream])
106                prmcnt = -1;
107             if (cnt_found[stream] && decomposed_prmcnt != decomposed_prmcnt_arr[stream])
108                decomposed_prmcnt = -1;
109 
110             vtxcnt_arr[stream] = vtxcnt;
111             prmcnt_arr[stream] = prmcnt;
112             decomposed_prmcnt_arr[stream] = decomposed_prmcnt;
113             cnt_found[stream] = true;
114          }
115       }
116    }
117 
118    if (out_vtxcnt)
119       memcpy(out_vtxcnt, vtxcnt_arr, num_streams * sizeof(int));
120    if (out_prmcnt)
121       memcpy(out_prmcnt, prmcnt_arr, num_streams * sizeof(int));
122    if (out_decomposed_prmcnt)
123       memcpy(out_decomposed_prmcnt, decomposed_prmcnt_arr, num_streams * sizeof(int));
124 }
125