blob: 2ce8de0ccdea7b3fa7f68ac729c379fbd25743d6 [file] [log] [blame]
Marek Olšák65f2e332017-10-07 22:54:31 +02001/*
2 * Copyright 2013 Advanced Micro Devices, Inc.
Marek Olšák4c5efc42018-04-01 16:49:48 -04003 * All Rights Reserved.
Marek Olšák65f2e332017-10-07 22:54:31 +02004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
Marek Olšák65f2e332017-10-07 22:54:31 +020023 */
24
Marek Olšák57774882018-04-01 18:42:33 -040025#include "si_build_pm4.h"
Marek Olšák65f2e332017-10-07 22:54:31 +020026#include "util/u_memory.h"
Marek Olšáka8abbbb2018-04-08 20:20:39 -040027#include "util/u_suballoc.h"
Marek Olšák65f2e332017-10-07 22:54:31 +020028
29static void si_set_streamout_enable(struct si_context *sctx, bool enable);
30
Marek Olšáka86c9322017-10-07 23:04:25 +020031static inline void si_so_target_reference(struct si_streamout_target **dst,
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010032 struct pipe_stream_output_target *src)
Marek Olšáka86c9322017-10-07 23:04:25 +020033{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010034 pipe_so_target_reference((struct pipe_stream_output_target **)dst, src);
Marek Olšáka86c9322017-10-07 23:04:25 +020035}
36
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010037static struct pipe_stream_output_target *si_create_so_target(struct pipe_context *ctx,
38 struct pipe_resource *buffer,
39 unsigned buffer_offset,
40 unsigned buffer_size)
Marek Olšák65f2e332017-10-07 22:54:31 +020041{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010042 struct si_context *sctx = (struct si_context *)ctx;
43 struct si_streamout_target *t;
44 struct si_resource *buf = si_resource(buffer);
Marek Olšák65f2e332017-10-07 22:54:31 +020045
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010046 t = CALLOC_STRUCT(si_streamout_target);
47 if (!t) {
48 return NULL;
49 }
Marek Olšák65f2e332017-10-07 22:54:31 +020050
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010051 unsigned buf_filled_size_size = sctx->screen->use_ngg_streamout ? 8 : 4;
52 u_suballocator_alloc(sctx->allocator_zeroed_memory, buf_filled_size_size, 4,
53 &t->buf_filled_size_offset, (struct pipe_resource **)&t->buf_filled_size);
54 if (!t->buf_filled_size) {
55 FREE(t);
56 return NULL;
57 }
Marek Olšák65f2e332017-10-07 22:54:31 +020058
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010059 t->b.reference.count = 1;
60 t->b.context = ctx;
61 pipe_resource_reference(&t->b.buffer, buffer);
62 t->b.buffer_offset = buffer_offset;
63 t->b.buffer_size = buffer_size;
Marek Olšák65f2e332017-10-07 22:54:31 +020064
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010065 util_range_add(&buf->b.b, &buf->valid_buffer_range, buffer_offset, buffer_offset + buffer_size);
66 return &t->b;
Marek Olšák65f2e332017-10-07 22:54:31 +020067}
68
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010069static void si_so_target_destroy(struct pipe_context *ctx, struct pipe_stream_output_target *target)
Marek Olšák65f2e332017-10-07 22:54:31 +020070{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010071 struct si_streamout_target *t = (struct si_streamout_target *)target;
72 pipe_resource_reference(&t->b.buffer, NULL);
73 si_resource_reference(&t->buf_filled_size, NULL);
74 FREE(t);
Marek Olšák65f2e332017-10-07 22:54:31 +020075}
76
77void si_streamout_buffers_dirty(struct si_context *sctx)
78{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010079 if (!sctx->streamout.enabled_mask)
80 return;
Marek Olšák65f2e332017-10-07 22:54:31 +020081
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010082 si_mark_atom_dirty(sctx, &sctx->atoms.s.streamout_begin);
83 si_set_streamout_enable(sctx, true);
Marek Olšák65f2e332017-10-07 22:54:31 +020084}
85
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010086static void si_set_streamout_targets(struct pipe_context *ctx, unsigned num_targets,
87 struct pipe_stream_output_target **targets,
88 const unsigned *offsets)
Marek Olšák65f2e332017-10-07 22:54:31 +020089{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010090 struct si_context *sctx = (struct si_context *)ctx;
91 unsigned old_num_targets = sctx->streamout.num_targets;
92 unsigned i;
93 bool wait_now = false;
Marek Olšák65f2e332017-10-07 22:54:31 +020094
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +010095 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
96 if (sctx->streamout.num_targets && sctx->streamout.begin_emitted) {
97 /* Since streamout uses vector writes which go through TC L2
98 * and most other clients can use TC L2 as well, we don't need
99 * to flush it.
100 *
101 * The only cases which requires flushing it is VGT DMA index
102 * fetching (on <= GFX7) and indirect draw data, which are rare
103 * cases. Thus, flag the TC L2 dirtiness in the resource and
104 * handle it at draw call time.
105 */
106 for (i = 0; i < sctx->streamout.num_targets; i++)
107 if (sctx->streamout.targets[i])
108 si_resource(sctx->streamout.targets[i]->b.buffer)->TC_L2_dirty = true;
Marek Olšákda619462017-10-07 23:06:11 +0200109
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100110 /* Invalidate the scalar cache in case a streamout buffer is
111 * going to be used as a constant buffer.
112 *
113 * Invalidate vL1, because streamout bypasses it (done by
114 * setting GLC=1 in the store instruction), but vL1 in other
115 * CUs can contain outdated data of streamout buffers.
116 *
117 * VS_PARTIAL_FLUSH is required if the buffers are going to be
118 * used as an input immediately.
119 */
120 sctx->flags |= SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200121
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100122 /* The BUFFER_FILLED_SIZE is written using a PS_DONE event. */
123 if (sctx->screen->use_ngg_streamout) {
124 sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH;
Marek Olšák39518592019-06-04 22:02:25 -0400125
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100126 /* Wait now. This is needed to make sure that GDS is not
127 * busy at the end of IBs.
128 *
129 * Also, the next streamout operation will overwrite GDS,
130 * so we need to make sure that it's idle.
131 */
132 wait_now = true;
133 } else {
134 sctx->flags |= SI_CONTEXT_VS_PARTIAL_FLUSH;
135 }
136 }
Marek Olšák65f2e332017-10-07 22:54:31 +0200137
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100138 /* All readers of the streamout targets need to be finished before we can
139 * start writing to the targets.
140 */
141 if (num_targets) {
142 if (sctx->screen->use_ngg_streamout)
143 si_allocate_gds(sctx);
Marek Olšák6944f992019-06-04 22:08:41 -0400144
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100145 sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH | SI_CONTEXT_CS_PARTIAL_FLUSH;
146 }
Marek Olšákda619462017-10-07 23:06:11 +0200147
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100148 /* Streamout buffers must be bound in 2 places:
149 * 1) in VGT by setting the VGT_STRMOUT registers
150 * 2) as shader resources
151 */
Marek Olšákda619462017-10-07 23:06:11 +0200152
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100153 /* Stop streamout. */
154 if (sctx->streamout.num_targets && sctx->streamout.begin_emitted)
155 si_emit_streamout_end(sctx);
Marek Olšákda619462017-10-07 23:06:11 +0200156
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100157 /* Set the new targets. */
158 unsigned enabled_mask = 0, append_bitmask = 0;
159 for (i = 0; i < num_targets; i++) {
160 si_so_target_reference(&sctx->streamout.targets[i], targets[i]);
161 if (!targets[i])
162 continue;
Marek Olšák65f2e332017-10-07 22:54:31 +0200163
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100164 si_context_add_resource_size(sctx, targets[i]->buffer);
165 enabled_mask |= 1 << i;
Marek Olšákda619462017-10-07 23:06:11 +0200166
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100167 if (offsets[i] == ((unsigned)-1))
168 append_bitmask |= 1 << i;
169 }
Marek Olšákda619462017-10-07 23:06:11 +0200170
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100171 for (; i < sctx->streamout.num_targets; i++)
172 si_so_target_reference(&sctx->streamout.targets[i], NULL);
Marek Olšák65f2e332017-10-07 22:54:31 +0200173
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100174 sctx->streamout.enabled_mask = enabled_mask;
175 sctx->streamout.num_targets = num_targets;
176 sctx->streamout.append_bitmask = append_bitmask;
Marek Olšák65f2e332017-10-07 22:54:31 +0200177
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100178 /* Update dirty state bits. */
179 if (num_targets) {
180 si_streamout_buffers_dirty(sctx);
181 } else {
182 si_set_atom_dirty(sctx, &sctx->atoms.s.streamout_begin, false);
183 si_set_streamout_enable(sctx, false);
184 }
Marek Olšákda619462017-10-07 23:06:11 +0200185
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100186 /* Set the shader resources.*/
187 for (i = 0; i < num_targets; i++) {
188 if (targets[i]) {
189 struct pipe_shader_buffer sbuf;
190 sbuf.buffer = targets[i]->buffer;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200191
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100192 if (sctx->screen->use_ngg_streamout) {
193 sbuf.buffer_offset = targets[i]->buffer_offset;
194 sbuf.buffer_size = targets[i]->buffer_size;
195 } else {
196 sbuf.buffer_offset = 0;
197 sbuf.buffer_size = targets[i]->buffer_offset + targets[i]->buffer_size;
198 }
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200199
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100200 si_set_rw_shader_buffer(sctx, SI_VS_STREAMOUT_BUF0 + i, &sbuf);
201 si_resource(targets[i]->buffer)->bind_history |= PIPE_BIND_STREAM_OUTPUT;
202 } else {
203 si_set_rw_shader_buffer(sctx, SI_VS_STREAMOUT_BUF0 + i, NULL);
204 }
205 }
206 for (; i < old_num_targets; i++)
207 si_set_rw_shader_buffer(sctx, SI_VS_STREAMOUT_BUF0 + i, NULL);
Marek Olšák39518592019-06-04 22:02:25 -0400208
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100209 if (wait_now)
210 sctx->emit_cache_flush(sctx);
Marek Olšák65f2e332017-10-07 22:54:31 +0200211}
212
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200213static void gfx10_emit_streamout_begin(struct si_context *sctx)
214{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100215 struct si_streamout_target **t = sctx->streamout.targets;
216 struct radeon_cmdbuf *cs = sctx->gfx_cs;
217 unsigned last_target = 0;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200218
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100219 for (unsigned i = 0; i < sctx->streamout.num_targets; i++) {
220 if (t[i])
221 last_target = i;
222 }
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200223
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100224 for (unsigned i = 0; i < sctx->streamout.num_targets; i++) {
225 if (!t[i])
226 continue;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200227
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100228 t[i]->stride_in_dw = sctx->streamout.stride_in_dw[i];
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200229
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100230 bool append = sctx->streamout.append_bitmask & (1 << i);
231 uint64_t va = 0;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200232
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100233 if (append) {
234 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, t[i]->buf_filled_size, RADEON_USAGE_READ,
235 RADEON_PRIO_SO_FILLED_SIZE);
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200236
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100237 va = t[i]->buf_filled_size->gpu_address + t[i]->buf_filled_size_offset;
238 }
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200239
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100240 radeon_emit(cs, PKT3(PKT3_DMA_DATA, 5, 0));
241 radeon_emit(cs, S_411_SRC_SEL(append ? V_411_SRC_ADDR_TC_L2 : V_411_DATA) |
242 S_411_DST_SEL(V_411_GDS) | S_411_CP_SYNC(i == last_target));
243 radeon_emit(cs, va);
244 radeon_emit(cs, va >> 32);
245 radeon_emit(cs, 4 * i); /* destination in GDS */
246 radeon_emit(cs, 0);
247 radeon_emit(cs, S_414_BYTE_COUNT_GFX9(4) | S_414_DISABLE_WR_CONFIRM_GFX9(i != last_target));
248 }
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200249
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100250 sctx->streamout.begin_emitted = true;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200251}
252
253static void gfx10_emit_streamout_end(struct si_context *sctx)
254{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100255 struct si_streamout_target **t = sctx->streamout.targets;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200256
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100257 for (unsigned i = 0; i < sctx->streamout.num_targets; i++) {
258 if (!t[i])
259 continue;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200260
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100261 uint64_t va = t[i]->buf_filled_size->gpu_address + t[i]->buf_filled_size_offset;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200262
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100263 si_cp_release_mem(sctx, sctx->gfx_cs, V_028A90_PS_DONE, 0, EOP_DST_SEL_TC_L2,
264 EOP_INT_SEL_SEND_DATA_AFTER_WR_CONFIRM, EOP_DATA_SEL_GDS,
265 t[i]->buf_filled_size, va, EOP_DATA_GDS(i, 1), 0);
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200266
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100267 t[i]->buf_filled_size_valid = true;
268 }
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200269
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100270 sctx->streamout.begin_emitted = false;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200271}
272
Marek Olšák65f2e332017-10-07 22:54:31 +0200273static void si_flush_vgt_streamout(struct si_context *sctx)
274{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100275 struct radeon_cmdbuf *cs = sctx->gfx_cs;
276 unsigned reg_strmout_cntl;
Marek Olšák65f2e332017-10-07 22:54:31 +0200277
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100278 /* The register is at different places on different ASICs. */
279 if (sctx->chip_class >= GFX7) {
280 reg_strmout_cntl = R_0300FC_CP_STRMOUT_CNTL;
281 radeon_set_uconfig_reg(cs, reg_strmout_cntl, 0);
282 } else {
283 reg_strmout_cntl = R_0084FC_CP_STRMOUT_CNTL;
284 radeon_set_config_reg(cs, reg_strmout_cntl, 0);
285 }
Marek Olšák65f2e332017-10-07 22:54:31 +0200286
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100287 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
288 radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_SO_VGTSTREAMOUT_FLUSH) | EVENT_INDEX(0));
Marek Olšák65f2e332017-10-07 22:54:31 +0200289
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100290 radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
291 radeon_emit(cs,
292 WAIT_REG_MEM_EQUAL); /* wait until the register is equal to the reference value */
293 radeon_emit(cs, reg_strmout_cntl >> 2); /* register */
294 radeon_emit(cs, 0);
295 radeon_emit(cs, S_0084FC_OFFSET_UPDATE_DONE(1)); /* reference value */
296 radeon_emit(cs, S_0084FC_OFFSET_UPDATE_DONE(1)); /* mask */
297 radeon_emit(cs, 4); /* poll interval */
Marek Olšák65f2e332017-10-07 22:54:31 +0200298}
299
Marek Olšák3160ee82018-04-08 21:20:53 -0400300static void si_emit_streamout_begin(struct si_context *sctx)
Marek Olšák65f2e332017-10-07 22:54:31 +0200301{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100302 struct radeon_cmdbuf *cs = sctx->gfx_cs;
303 struct si_streamout_target **t = sctx->streamout.targets;
304 uint16_t *stride_in_dw = sctx->streamout.stride_in_dw;
305 unsigned i;
Marek Olšák65f2e332017-10-07 22:54:31 +0200306
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100307 si_flush_vgt_streamout(sctx);
Marek Olšák65f2e332017-10-07 22:54:31 +0200308
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100309 for (i = 0; i < sctx->streamout.num_targets; i++) {
310 if (!t[i])
311 continue;
Marek Olšák65f2e332017-10-07 22:54:31 +0200312
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100313 t[i]->stride_in_dw = stride_in_dw[i];
Marek Olšák65f2e332017-10-07 22:54:31 +0200314
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100315 /* AMD GCN binds streamout buffers as shader resources.
316 * VGT only counts primitives and tells the shader
317 * through SGPRs what to do. */
318 radeon_set_context_reg_seq(cs, R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0 + 16 * i, 2);
319 radeon_emit(cs, (t[i]->b.buffer_offset + t[i]->b.buffer_size) >> 2); /* BUFFER_SIZE (in DW) */
320 radeon_emit(cs, stride_in_dw[i]); /* VTX_STRIDE (in DW) */
Marek Olšák65f2e332017-10-07 22:54:31 +0200321
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100322 if (sctx->streamout.append_bitmask & (1 << i) && t[i]->buf_filled_size_valid) {
323 uint64_t va = t[i]->buf_filled_size->gpu_address + t[i]->buf_filled_size_offset;
Marek Olšák65f2e332017-10-07 22:54:31 +0200324
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100325 /* Append. */
326 radeon_emit(cs, PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0));
327 radeon_emit(cs, STRMOUT_SELECT_BUFFER(i) |
328 STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_MEM)); /* control */
329 radeon_emit(cs, 0); /* unused */
330 radeon_emit(cs, 0); /* unused */
331 radeon_emit(cs, va); /* src address lo */
332 radeon_emit(cs, va >> 32); /* src address hi */
Marek Olšák65f2e332017-10-07 22:54:31 +0200333
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100334 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, t[i]->buf_filled_size, RADEON_USAGE_READ,
335 RADEON_PRIO_SO_FILLED_SIZE);
336 } else {
337 /* Start from the beginning. */
338 radeon_emit(cs, PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0));
339 radeon_emit(cs, STRMOUT_SELECT_BUFFER(i) |
340 STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_PACKET)); /* control */
341 radeon_emit(cs, 0); /* unused */
342 radeon_emit(cs, 0); /* unused */
343 radeon_emit(cs, t[i]->b.buffer_offset >> 2); /* buffer offset in DW */
344 radeon_emit(cs, 0); /* unused */
345 }
346 }
Marek Olšák65f2e332017-10-07 22:54:31 +0200347
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100348 sctx->streamout.begin_emitted = true;
Marek Olšák65f2e332017-10-07 22:54:31 +0200349}
350
351void si_emit_streamout_end(struct si_context *sctx)
352{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100353 if (sctx->screen->use_ngg_streamout) {
354 gfx10_emit_streamout_end(sctx);
355 return;
356 }
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200357
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100358 struct radeon_cmdbuf *cs = sctx->gfx_cs;
359 struct si_streamout_target **t = sctx->streamout.targets;
360 unsigned i;
361 uint64_t va;
Marek Olšák65f2e332017-10-07 22:54:31 +0200362
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100363 si_flush_vgt_streamout(sctx);
Marek Olšák65f2e332017-10-07 22:54:31 +0200364
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100365 for (i = 0; i < sctx->streamout.num_targets; i++) {
366 if (!t[i])
367 continue;
Marek Olšák65f2e332017-10-07 22:54:31 +0200368
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100369 va = t[i]->buf_filled_size->gpu_address + t[i]->buf_filled_size_offset;
370 radeon_emit(cs, PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0));
371 radeon_emit(cs, STRMOUT_SELECT_BUFFER(i) | STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_NONE) |
372 STRMOUT_STORE_BUFFER_FILLED_SIZE); /* control */
373 radeon_emit(cs, va); /* dst address lo */
374 radeon_emit(cs, va >> 32); /* dst address hi */
375 radeon_emit(cs, 0); /* unused */
376 radeon_emit(cs, 0); /* unused */
Marek Olšák65f2e332017-10-07 22:54:31 +0200377
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100378 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, t[i]->buf_filled_size, RADEON_USAGE_WRITE,
379 RADEON_PRIO_SO_FILLED_SIZE);
Marek Olšák65f2e332017-10-07 22:54:31 +0200380
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100381 /* Zero the buffer size. The counters (primitives generated,
382 * primitives emitted) may be enabled even if there is not
383 * buffer bound. This ensures that the primitives-emitted query
384 * won't increment. */
385 radeon_set_context_reg(cs, R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0 + 16 * i, 0);
386 sctx->context_roll = true;
Marek Olšák65f2e332017-10-07 22:54:31 +0200387
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100388 t[i]->buf_filled_size_valid = true;
389 }
Marek Olšák65f2e332017-10-07 22:54:31 +0200390
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100391 sctx->streamout.begin_emitted = false;
Marek Olšák65f2e332017-10-07 22:54:31 +0200392}
393
394/* STREAMOUT CONFIG DERIVED STATE
395 *
396 * Streamout must be enabled for the PRIMITIVES_GENERATED query to work.
397 * The buffer mask is an independent state, so no writes occur if there
398 * are no buffers bound.
399 */
400
Marek Olšák3160ee82018-04-08 21:20:53 -0400401static void si_emit_streamout_enable(struct si_context *sctx)
Marek Olšák65f2e332017-10-07 22:54:31 +0200402{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100403 assert(!sctx->screen->use_ngg_streamout);
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200404
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100405 radeon_set_context_reg_seq(sctx->gfx_cs, R_028B94_VGT_STRMOUT_CONFIG, 2);
406 radeon_emit(sctx->gfx_cs, S_028B94_STREAMOUT_0_EN(si_get_strmout_en(sctx)) |
407 S_028B94_RAST_STREAM(0) |
408 S_028B94_STREAMOUT_1_EN(si_get_strmout_en(sctx)) |
409 S_028B94_STREAMOUT_2_EN(si_get_strmout_en(sctx)) |
410 S_028B94_STREAMOUT_3_EN(si_get_strmout_en(sctx)));
411 radeon_emit(sctx->gfx_cs,
412 sctx->streamout.hw_enabled_mask & sctx->streamout.enabled_stream_buffers_mask);
Marek Olšák65f2e332017-10-07 22:54:31 +0200413}
414
415static void si_set_streamout_enable(struct si_context *sctx, bool enable)
416{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100417 bool old_strmout_en = si_get_strmout_en(sctx);
418 unsigned old_hw_enabled_mask = sctx->streamout.hw_enabled_mask;
Marek Olšák65f2e332017-10-07 22:54:31 +0200419
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100420 sctx->streamout.streamout_enabled = enable;
Marek Olšák65f2e332017-10-07 22:54:31 +0200421
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100422 sctx->streamout.hw_enabled_mask =
423 sctx->streamout.enabled_mask | (sctx->streamout.enabled_mask << 4) |
424 (sctx->streamout.enabled_mask << 8) | (sctx->streamout.enabled_mask << 12);
Marek Olšák65f2e332017-10-07 22:54:31 +0200425
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100426 if (!sctx->screen->use_ngg_streamout &&
427 ((old_strmout_en != si_get_strmout_en(sctx)) ||
428 (old_hw_enabled_mask != sctx->streamout.hw_enabled_mask)))
429 si_mark_atom_dirty(sctx, &sctx->atoms.s.streamout_enable);
Marek Olšák65f2e332017-10-07 22:54:31 +0200430}
431
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100432void si_update_prims_generated_query_state(struct si_context *sctx, unsigned type, int diff)
Marek Olšák65f2e332017-10-07 22:54:31 +0200433{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100434 if (!sctx->screen->use_ngg_streamout && type == PIPE_QUERY_PRIMITIVES_GENERATED) {
435 bool old_strmout_en = si_get_strmout_en(sctx);
Marek Olšák65f2e332017-10-07 22:54:31 +0200436
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100437 sctx->streamout.num_prims_gen_queries += diff;
438 assert(sctx->streamout.num_prims_gen_queries >= 0);
Marek Olšák65f2e332017-10-07 22:54:31 +0200439
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100440 sctx->streamout.prims_gen_query_enabled = sctx->streamout.num_prims_gen_queries != 0;
Marek Olšák65f2e332017-10-07 22:54:31 +0200441
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100442 if (old_strmout_en != si_get_strmout_en(sctx))
443 si_mark_atom_dirty(sctx, &sctx->atoms.s.streamout_enable);
Marek Olšák776f05a2019-08-20 20:07:26 -0400444
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100445 if (si_update_ngg(sctx)) {
446 si_shader_change_notify(sctx);
447 sctx->do_update_shaders = true;
448 }
449 }
Marek Olšák65f2e332017-10-07 22:54:31 +0200450}
451
452void si_init_streamout_functions(struct si_context *sctx)
453{
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100454 sctx->b.create_stream_output_target = si_create_so_target;
455 sctx->b.stream_output_target_destroy = si_so_target_destroy;
456 sctx->b.set_stream_output_targets = si_set_streamout_targets;
Nicolai Hähnle5ff3aff2018-09-21 22:07:01 +0200457
Pierre-Eric Pelloux-Prayerd7008fe2020-03-27 19:32:38 +0100458 if (sctx->screen->use_ngg_streamout) {
459 sctx->atoms.s.streamout_begin.emit = gfx10_emit_streamout_begin;
460 } else {
461 sctx->atoms.s.streamout_begin.emit = si_emit_streamout_begin;
462 sctx->atoms.s.streamout_enable.emit = si_emit_streamout_enable;
463 }
Marek Olšák65f2e332017-10-07 22:54:31 +0200464}