Debian Codegraph @

src/asahi/libagx/geometry.cl

raw
1 /*
2 * Copyright 2023 Alyssa Rosenzweig
3 * Copyright 2023 Valve Corporation
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "compiler/libcl/libcl_vk.h"
8 #include "poly/cl/restart.h"
9 #include "poly/geometry.h"
10 #include "poly/prim.h"
11 #include "poly/tessellator.h"
12 #include "util/macros.h"
13 #include "util/u_math.h"
14
15 KERNEL(1)
16 libagx_increment_ia(global uint32_t *ia_vertices,
17 global uint32_t *ia_primitives,
18 global uint32_t *vs_invocations, global uint32_t *c_prims,
19 global uint32_t *c_invs, constant uint32_t *draw,
20 enum mesa_prim prim, unsigned verts_per_patch)
21 {
22 poly_increment_ia(ia_vertices, ia_primitives, vs_invocations, c_prims,
23 c_invs, draw, prim, verts_per_patch);
24 }
25
26 KERNEL(1024)
27 libagx_increment_ia_restart(global uint32_t *ia_vertices,
28 global uint32_t *ia_primitives,
29 global uint32_t *vs_invocations,
30 global uint32_t *c_prims, global uint32_t *c_invs,
31 constant uint32_t *draw, uint64_t index_buffer,
32 uint32_t index_buffer_range_el,
33 uint32_t restart_index, uint32_t index_size_B,
34 enum mesa_prim prim, unsigned verts_per_patch)
35 {
36 uint tid = cl_global_id.x;
37 unsigned count = draw[0];
38 local uint scratch;
39
40 uint start = draw[2];
41 uint partial = 0;
42
43 /* Count non-restart indices */
44 for (uint i = tid; i < count; i += 1024) {
45 uint index = poly_load_index(index_buffer, index_buffer_range_el,
46 start + i, index_size_B);
47
48 if (index != restart_index)
49 partial++;
50 }
51
52 /* Accumulate the partials across the workgroup */
53 scratch = 0;
54 barrier(CLK_LOCAL_MEM_FENCE);
55 atomic_add(&scratch, partial);
56 barrier(CLK_LOCAL_MEM_FENCE);
57
58 /* Elect a single thread from the workgroup to increment the counters */
59 if (tid == 0) {
60 poly_increment_counters(ia_vertices, vs_invocations, NULL,
61 scratch * draw[1]);
62 }
63
64 /* TODO: We should vectorize this */
65 if ((ia_primitives || c_prims || c_invs) && tid == 0) {
66 uint accum = 0;
67 int last_restart = -1;
68 for (uint i = 0; i < count; ++i) {
69 uint index = poly_load_index(index_buffer, index_buffer_range_el,
70 start + i, index_size_B);
71
72 if (index == restart_index) {
73 accum += poly_decomposed_prims_for_vertices_with_tess(
74 prim, i - last_restart - 1, verts_per_patch);
75 last_restart = i;
76 }
77 }
78
79 {
80 accum += poly_decomposed_prims_for_vertices_with_tess(
81 prim, count - last_restart - 1, verts_per_patch);
82 }
83
84 poly_increment_counters(ia_primitives, c_prims, c_invs, accum * draw[1]);
85 }
86 }
87
88 KERNEL(1024)
89 libagx_unroll_restart(global struct poly_heap *heap, uint64_t index_buffer,
90 constant uint *in_draw, global uint32_t *out_draw,
91 uint32_t max_draws, uint32_t restart_index,
92 uint32_t index_buffer_size_el, uint32_t index_size_log2,
93 uint32_t flatshade_first, uint mode__11)
94 {
95 uint32_t index_size_B = 1 << index_size_log2;
96 enum mesa_prim mode = poly_uncompact_prim(mode__11);
97
98 POLY_DECL_UNROLL_RESTART_SCRATCH(scratch, 1024);
99 poly_unroll_restart(out_draw, heap, in_draw, index_buffer,
100 index_buffer_size_el, index_size_B, restart_index,
101 flatshade_first, mode, scratch);
102 }
103
104 KERNEL(1)
105 libagx_gs_setup_indirect(
106 uint64_t index_buffer, constant uint *draw,
107 global struct poly_vertex_params *vp /* output */,
108 global struct poly_geometry_params *p /* output */,
109 global struct poly_heap *heap,
110 uint64_t vs_outputs /* Vertex (TES) output mask */,
111 uint32_t index_size_B /* 0 if no index bffer */,
112 uint32_t index_buffer_range_el,
113 uint32_t prim /* Input primitive type, enum mesa_prim */,
114 int is_prefix_summing, uint max_indices, enum poly_gs_shape shape)
115 {
116 poly_gs_setup_indirect(index_buffer, draw, vp, p, heap,
117 vs_outputs, index_size_B, index_buffer_range_el, prim,
118 is_prefix_summing, max_indices, shape);
119 }
120
121 KERNEL(1024)
122 libagx_prefix_sum_geom(constant struct poly_geometry_params *p)
123 {
124 local uint scratch[32];
125 poly_prefix_sum(scratch, p->count_buffer, p->input_primitives,
126 p->count_buffer_stride / 4, cl_group_id.x, 1024);
127 }
128
129 KERNEL(1024)
130 libagx_prefix_sum_tess(global struct poly_tess_params *p, global uint *c_prims,
131 global uint *c_invs, uint increment_stats__2)
132 {
133 local uint scratch[32];
134 poly_prefix_sum(scratch, p->counts, p->nr_patches, 1 /* words */,
135 0 /* word */, 1024);
136
137 /* After prefix summing, we know the total # of indices, so allocate the
138 * index buffer now. Elect a thread for the allocation.
139 */
140 barrier(CLK_LOCAL_MEM_FENCE);
141 if (cl_local_id.x != 0)
142 return;
143
144 /* The last element of an inclusive prefix sum is the total sum */
145 uint total = p->nr_patches > 0 ? p->counts[p->nr_patches - 1] : 0;
146
147 /* Allocate 4-byte indices */
148 uint32_t elsize_B = sizeof(uint32_t);
149 uint32_t size_B = total * elsize_B;
150 uint alloc_B = poly_heap_alloc_offs(p->heap, size_B);
151 p->index_buffer = (global uint32_t *)(((uintptr_t)p->heap->base) + alloc_B);
152
153 /* ...and now we can generate the API indexed draw */
154 global uint32_t *desc = p->out_draws;
155
156 desc[0] = total; /* count */
157 desc[1] = 1; /* instance_count */
158 desc[2] = alloc_B / elsize_B; /* start */
159 desc[3] = 0; /* index_bias */
160 desc[4] = 0; /* start_instance */
161
162 /* If necessary, increment clipper statistics too. This is only used when
163 * there's no geometry shader following us. See poly_nir_lower_gs.c for more
164 * info on the emulation. We just need to calculate the # of primitives
165 * tessellated.
166 */
167 if (increment_stats__2) {
168 uint prims = p->points_mode ? total
169 : p->isolines ? (total / 2)
170 : (total / 3);
171
172 poly_increment_counters(c_prims, c_invs, NULL, prims);
173 }
174 }