1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_cistatic nir_intrinsic_instr *
28bf215546Sopenharmony_cias_intrinsic(nir_instr *instr, nir_intrinsic_op op)
29bf215546Sopenharmony_ci{
30bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_intrinsic)
31bf215546Sopenharmony_ci      return NULL;
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
34bf215546Sopenharmony_ci   if (intrin->intrinsic != op)
35bf215546Sopenharmony_ci      return NULL;
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci   return intrin;
38bf215546Sopenharmony_ci}
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic nir_intrinsic_instr *
41bf215546Sopenharmony_cias_set_vertex_and_primitive_count(nir_instr *instr)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   return as_intrinsic(instr, nir_intrinsic_set_vertex_and_primitive_count);
44bf215546Sopenharmony_ci}
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci/**
47bf215546Sopenharmony_ci * Count the number of vertices/primitives emitted by a geometry shader per stream.
48bf215546Sopenharmony_ci * If a constant number of vertices is emitted, the output is set to
49bf215546Sopenharmony_ci * that number, otherwise it is unknown at compile time and the
50bf215546Sopenharmony_ci * result will be -1.
51bf215546Sopenharmony_ci *
52bf215546Sopenharmony_ci * This only works if you've used nir_lower_gs_intrinsics() to do vertex
53bf215546Sopenharmony_ci * counting at the NIR level.
54bf215546Sopenharmony_ci */
55bf215546Sopenharmony_civoid
56bf215546Sopenharmony_cinir_gs_count_vertices_and_primitives(const nir_shader *shader,
57bf215546Sopenharmony_ci                                     int *out_vtxcnt,
58bf215546Sopenharmony_ci                                     int *out_prmcnt,
59bf215546Sopenharmony_ci                                     unsigned num_streams)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   assert(num_streams);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   int vtxcnt_arr[4] = {-1, -1, -1, -1};
64bf215546Sopenharmony_ci   int prmcnt_arr[4] = {-1, -1, -1, -1};
65bf215546Sopenharmony_ci   bool cnt_found[4] = {false, false, false, false};
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
68bf215546Sopenharmony_ci      if (!function->impl)
69bf215546Sopenharmony_ci         continue;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci      /* set_vertex_and_primitive_count intrinsics only appear in predecessors of the
72bf215546Sopenharmony_ci       * end block.  So we don't need to walk all of them.
73bf215546Sopenharmony_ci       */
74bf215546Sopenharmony_ci      set_foreach(function->impl->end_block->predecessors, entry) {
75bf215546Sopenharmony_ci         nir_block *block = (nir_block *) entry->key;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci         nir_foreach_instr_reverse(instr, block) {
78bf215546Sopenharmony_ci            nir_intrinsic_instr *intrin = as_set_vertex_and_primitive_count(instr);
79bf215546Sopenharmony_ci            if (!intrin)
80bf215546Sopenharmony_ci               continue;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci            unsigned stream = nir_intrinsic_stream_id(intrin);
83bf215546Sopenharmony_ci            if (stream >= num_streams)
84bf215546Sopenharmony_ci               continue;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci            int vtxcnt = -1;
87bf215546Sopenharmony_ci            int prmcnt = -1;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci            /* If the number of vertices/primitives is compile-time known, we use that,
90bf215546Sopenharmony_ci             * otherwise we leave it at -1 which means that it's unknown.
91bf215546Sopenharmony_ci             */
92bf215546Sopenharmony_ci            if (nir_src_is_const(intrin->src[0]))
93bf215546Sopenharmony_ci               vtxcnt = nir_src_as_int(intrin->src[0]);
94bf215546Sopenharmony_ci            if (nir_src_is_const(intrin->src[1]))
95bf215546Sopenharmony_ci               prmcnt = nir_src_as_int(intrin->src[1]);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci            /* We've found contradictory set_vertex_and_primitive_count intrinsics.
98bf215546Sopenharmony_ci             * This can happen if there are early-returns in main() and
99bf215546Sopenharmony_ci             * different paths emit different numbers of vertices.
100bf215546Sopenharmony_ci             */
101bf215546Sopenharmony_ci            if (cnt_found[stream] && vtxcnt != vtxcnt_arr[stream])
102bf215546Sopenharmony_ci               vtxcnt = -1;
103bf215546Sopenharmony_ci            if (cnt_found[stream] && prmcnt != prmcnt_arr[stream])
104bf215546Sopenharmony_ci               prmcnt = -1;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci            vtxcnt_arr[stream] = vtxcnt;
107bf215546Sopenharmony_ci            prmcnt_arr[stream] = prmcnt;
108bf215546Sopenharmony_ci            cnt_found[stream] = true;
109bf215546Sopenharmony_ci         }
110bf215546Sopenharmony_ci      }
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   if (out_vtxcnt)
114bf215546Sopenharmony_ci      memcpy(out_vtxcnt, vtxcnt_arr, num_streams * sizeof(int));
115bf215546Sopenharmony_ci   if (out_prmcnt)
116bf215546Sopenharmony_ci      memcpy(out_prmcnt, prmcnt_arr, num_streams * sizeof(int));
117bf215546Sopenharmony_ci}
118