radeonsi: only emit line stippling and provoking vertex state when it changes
[mesa.git] / src / gallium / drivers / radeonsi / si_state_draw.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Christian König <christian.koenig@amd.com>
25 */
26
27 #include "si_pipe.h"
28 #include "si_shader.h"
29 #include "radeon/r600_cs.h"
30 #include "sid.h"
31
32 #include "util/u_index_modify.h"
33 #include "util/u_upload_mgr.h"
34
35 static void si_decompress_textures(struct si_context *sctx)
36 {
37 if (!sctx->blitter->running) {
38 /* Flush depth textures which need to be flushed. */
39 for (int i = 0; i < SI_NUM_SHADERS; i++) {
40 if (sctx->samplers[i].depth_texture_mask) {
41 si_flush_depth_textures(sctx, &sctx->samplers[i]);
42 }
43 if (sctx->samplers[i].compressed_colortex_mask) {
44 si_decompress_color_textures(sctx, &sctx->samplers[i]);
45 }
46 }
47 }
48 }
49
50 static unsigned si_conv_pipe_prim(unsigned mode)
51 {
52 static const unsigned prim_conv[] = {
53 [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
54 [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
55 [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
56 [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
57 [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
58 [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
59 [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
60 [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
61 [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
62 [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
63 [PIPE_PRIM_LINES_ADJACENCY] = V_008958_DI_PT_LINELIST_ADJ,
64 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_008958_DI_PT_LINESTRIP_ADJ,
65 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_008958_DI_PT_TRILIST_ADJ,
66 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_008958_DI_PT_TRISTRIP_ADJ,
67 [R600_PRIM_RECTANGLE_LIST] = V_008958_DI_PT_RECTLIST
68 };
69 assert(mode < Elements(prim_conv));
70 return prim_conv[mode];
71 }
72
73 static unsigned si_conv_prim_to_gs_out(unsigned mode)
74 {
75 static const int prim_conv[] = {
76 [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
77 [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
78 [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
79 [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
80 [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
81 [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
82 [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
83 [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
84 [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
85 [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
86 [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
87 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
88 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
89 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
90 [R600_PRIM_RECTANGLE_LIST] = V_028A6C_OUTPRIM_TYPE_TRISTRIP
91 };
92 assert(mode < Elements(prim_conv));
93
94 return prim_conv[mode];
95 }
96
97 static unsigned si_get_ia_multi_vgt_param(struct si_context *sctx,
98 const struct pipe_draw_info *info)
99 {
100 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
101 unsigned prim = info->mode;
102 unsigned primgroup_size = 128; /* recommended without a GS */
103
104 /* SWITCH_ON_EOP(0) is always preferable. */
105 bool wd_switch_on_eop = false;
106 bool ia_switch_on_eop = false;
107 bool partial_vs_wave = false;
108
109 if (sctx->gs_shader)
110 primgroup_size = 64; /* recommended with a GS */
111
112 /* This is a hardware requirement. */
113 if ((rs && rs->line_stipple_enable) ||
114 (sctx->b.screen->debug_flags & DBG_SWITCH_ON_EOP)) {
115 ia_switch_on_eop = true;
116 wd_switch_on_eop = true;
117 }
118
119 if (sctx->b.streamout.streamout_enabled ||
120 sctx->b.streamout.prims_gen_query_enabled)
121 partial_vs_wave = true;
122
123 if (sctx->b.chip_class >= CIK) {
124 /* WD_SWITCH_ON_EOP has no effect on GPUs with less than
125 * 4 shader engines. Set 1 to pass the assertion below.
126 * The other cases are hardware requirements. */
127 if (sctx->b.screen->info.max_se < 4 ||
128 prim == PIPE_PRIM_POLYGON ||
129 prim == PIPE_PRIM_LINE_LOOP ||
130 prim == PIPE_PRIM_TRIANGLE_FAN ||
131 prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY ||
132 info->primitive_restart)
133 wd_switch_on_eop = true;
134
135 /* Hawaii hangs if instancing is enabled and WD_SWITCH_ON_EOP is 0.
136 * We don't know that for indirect drawing, so treat it as
137 * always problematic. */
138 if (sctx->b.family == CHIP_HAWAII &&
139 (info->indirect || info->instance_count > 1))
140 wd_switch_on_eop = true;
141
142 /* If the WD switch is false, the IA switch must be false too. */
143 assert(wd_switch_on_eop || !ia_switch_on_eop);
144 }
145
146 return S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) |
147 S_028AA8_PARTIAL_VS_WAVE_ON(partial_vs_wave) |
148 S_028AA8_PRIMGROUP_SIZE(primgroup_size - 1) |
149 S_028AA8_WD_SWITCH_ON_EOP(sctx->b.chip_class >= CIK ? wd_switch_on_eop : 0);
150 }
151
152 static void si_emit_rasterizer_prim_state(struct si_context *sctx, unsigned mode)
153 {
154 struct radeon_winsys_cs *cs = sctx->b.rings.gfx.cs;
155
156 if (sctx->gs_shader)
157 mode = sctx->gs_shader->gs_output_prim;
158
159 if (mode == sctx->last_rast_prim)
160 return;
161
162 r600_write_context_reg(cs, R_028A0C_PA_SC_LINE_STIPPLE,
163 sctx->pa_sc_line_stipple |
164 S_028A0C_AUTO_RESET_CNTL(mode == PIPE_PRIM_LINES ? 1 :
165 mode == PIPE_PRIM_LINE_STRIP ? 2 : 0));
166
167 r600_write_context_reg(cs, R_028814_PA_SU_SC_MODE_CNTL,
168 sctx->pa_su_sc_mode_cntl |
169 S_028814_PROVOKING_VTX_LAST(mode == PIPE_PRIM_QUADS ||
170 mode == PIPE_PRIM_QUAD_STRIP ||
171 mode == PIPE_PRIM_POLYGON));
172
173 sctx->last_rast_prim = mode;
174 }
175
176 static void si_emit_draw_registers(struct si_context *sctx,
177 const struct pipe_draw_info *info,
178 const struct pipe_index_buffer *ib)
179 {
180 struct radeon_winsys_cs *cs = sctx->b.rings.gfx.cs;
181 unsigned prim = si_conv_pipe_prim(info->mode);
182 unsigned gs_out_prim =
183 si_conv_prim_to_gs_out(sctx->gs_shader ?
184 sctx->gs_shader->gs_output_prim :
185 info->mode);
186 unsigned ia_multi_vgt_param = si_get_ia_multi_vgt_param(sctx, info);
187
188 /* Draw state. */
189 if (prim != sctx->last_prim ||
190 ia_multi_vgt_param != sctx->last_multi_vgt_param) {
191 if (sctx->b.chip_class >= CIK) {
192 radeon_emit(cs, PKT3(PKT3_DRAW_PREAMBLE, 2, 0));
193 radeon_emit(cs, prim); /* VGT_PRIMITIVE_TYPE */
194 radeon_emit(cs, ia_multi_vgt_param); /* IA_MULTI_VGT_PARAM */
195 radeon_emit(cs, 0); /* VGT_LS_HS_CONFIG */
196 } else {
197 r600_write_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE, prim);
198 r600_write_context_reg(cs, R_028AA8_IA_MULTI_VGT_PARAM, ia_multi_vgt_param);
199 }
200 sctx->last_prim = prim;
201 sctx->last_multi_vgt_param = ia_multi_vgt_param;
202 }
203
204 if (gs_out_prim != sctx->last_gs_out_prim) {
205 r600_write_context_reg(cs, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out_prim);
206 sctx->last_gs_out_prim = gs_out_prim;
207 }
208
209 /* Primitive restart. */
210 if (info->primitive_restart != sctx->last_primitive_restart_en) {
211 r600_write_context_reg(cs, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, info->primitive_restart);
212 sctx->last_primitive_restart_en = info->primitive_restart;
213
214 if (info->primitive_restart &&
215 (info->restart_index != sctx->last_restart_index ||
216 sctx->last_restart_index == SI_RESTART_INDEX_UNKNOWN)) {
217 r600_write_context_reg(cs, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX,
218 info->restart_index);
219 sctx->last_restart_index = info->restart_index;
220 }
221 }
222 }
223
224 static void si_emit_draw_packets(struct si_context *sctx,
225 const struct pipe_draw_info *info,
226 const struct pipe_index_buffer *ib)
227 {
228 struct radeon_winsys_cs *cs = sctx->b.rings.gfx.cs;
229 unsigned sh_base_reg = (sctx->gs_shader ? R_00B330_SPI_SHADER_USER_DATA_ES_0 :
230 R_00B130_SPI_SHADER_USER_DATA_VS_0);
231
232 if (info->count_from_stream_output) {
233 struct r600_so_target *t =
234 (struct r600_so_target*)info->count_from_stream_output;
235 uint64_t va = t->buf_filled_size->gpu_address +
236 t->buf_filled_size_offset;
237
238 r600_write_context_reg(cs, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE,
239 t->stride_in_dw);
240
241 radeon_emit(cs, PKT3(PKT3_COPY_DATA, 4, 0));
242 radeon_emit(cs, COPY_DATA_SRC_SEL(COPY_DATA_MEM) |
243 COPY_DATA_DST_SEL(COPY_DATA_REG) |
244 COPY_DATA_WR_CONFIRM);
245 radeon_emit(cs, va); /* src address lo */
246 radeon_emit(cs, va >> 32); /* src address hi */
247 radeon_emit(cs, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2);
248 radeon_emit(cs, 0); /* unused */
249
250 r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
251 t->buf_filled_size, RADEON_USAGE_READ,
252 RADEON_PRIO_MIN);
253 }
254
255 /* draw packet */
256 if (info->indexed) {
257 radeon_emit(cs, PKT3(PKT3_INDEX_TYPE, 0, 0));
258
259 if (ib->index_size == 4) {
260 radeon_emit(cs, V_028A7C_VGT_INDEX_32 | (SI_BIG_ENDIAN ?
261 V_028A7C_VGT_DMA_SWAP_32_BIT : 0));
262 } else {
263 radeon_emit(cs, V_028A7C_VGT_INDEX_16 | (SI_BIG_ENDIAN ?
264 V_028A7C_VGT_DMA_SWAP_16_BIT : 0));
265 }
266 }
267
268 if (!info->indirect) {
269 int base_vertex;
270
271 radeon_emit(cs, PKT3(PKT3_NUM_INSTANCES, 0, 0));
272 radeon_emit(cs, info->instance_count);
273
274 /* Base vertex and start instance. */
275 base_vertex = info->indexed ? info->index_bias : info->start;
276
277 if (base_vertex != sctx->last_base_vertex ||
278 sctx->last_base_vertex == SI_BASE_VERTEX_UNKNOWN ||
279 info->start_instance != sctx->last_start_instance ||
280 sh_base_reg != sctx->last_sh_base_reg) {
281 si_write_sh_reg_seq(cs, sh_base_reg + SI_SGPR_BASE_VERTEX * 4, 2);
282 radeon_emit(cs, base_vertex);
283 radeon_emit(cs, info->start_instance);
284
285 sctx->last_base_vertex = base_vertex;
286 sctx->last_start_instance = info->start_instance;
287 sctx->last_sh_base_reg = sh_base_reg;
288 }
289 } else {
290 si_invalidate_draw_sh_constants(sctx);
291
292 r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
293 (struct r600_resource *)info->indirect,
294 RADEON_USAGE_READ, RADEON_PRIO_MIN);
295 }
296
297 if (info->indexed) {
298 uint32_t index_max_size = (ib->buffer->width0 - ib->offset) /
299 ib->index_size;
300 uint64_t index_va = r600_resource(ib->buffer)->gpu_address + ib->offset;
301
302 r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
303 (struct r600_resource *)ib->buffer,
304 RADEON_USAGE_READ, RADEON_PRIO_MIN);
305
306 if (info->indirect) {
307 uint64_t indirect_va = r600_resource(info->indirect)->gpu_address;
308
309 assert(indirect_va % 8 == 0);
310 assert(index_va % 2 == 0);
311 assert(info->indirect_offset % 4 == 0);
312
313 radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0));
314 radeon_emit(cs, 1);
315 radeon_emit(cs, indirect_va);
316 radeon_emit(cs, indirect_va >> 32);
317
318 radeon_emit(cs, PKT3(PKT3_INDEX_BASE, 1, 0));
319 radeon_emit(cs, index_va);
320 radeon_emit(cs, index_va >> 32);
321
322 radeon_emit(cs, PKT3(PKT3_INDEX_BUFFER_SIZE, 0, 0));
323 radeon_emit(cs, index_max_size);
324
325 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_INDIRECT, 3, sctx->b.predicate_drawing));
326 radeon_emit(cs, info->indirect_offset);
327 radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
328 radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
329 radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
330 } else {
331 index_va += info->start * ib->index_size;
332
333 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, sctx->b.predicate_drawing));
334 radeon_emit(cs, index_max_size);
335 radeon_emit(cs, index_va);
336 radeon_emit(cs, (index_va >> 32UL) & 0xFF);
337 radeon_emit(cs, info->count);
338 radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
339 }
340 } else {
341 if (info->indirect) {
342 uint64_t indirect_va = r600_resource(info->indirect)->gpu_address;
343
344 assert(indirect_va % 8 == 0);
345 assert(info->indirect_offset % 4 == 0);
346
347 radeon_emit(cs, PKT3(PKT3_SET_BASE, 2, 0));
348 radeon_emit(cs, 1);
349 radeon_emit(cs, indirect_va);
350 radeon_emit(cs, indirect_va >> 32);
351
352 radeon_emit(cs, PKT3(PKT3_DRAW_INDIRECT, 3, sctx->b.predicate_drawing));
353 radeon_emit(cs, info->indirect_offset);
354 radeon_emit(cs, (sh_base_reg + SI_SGPR_BASE_VERTEX * 4 - SI_SH_REG_OFFSET) >> 2);
355 radeon_emit(cs, (sh_base_reg + SI_SGPR_START_INSTANCE * 4 - SI_SH_REG_OFFSET) >> 2);
356 radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX);
357 } else {
358 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, sctx->b.predicate_drawing));
359 radeon_emit(cs, info->count);
360 radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX |
361 S_0287F0_USE_OPAQUE(!!info->count_from_stream_output));
362 }
363 }
364 }
365
366 void si_emit_cache_flush(struct r600_common_context *sctx, struct r600_atom *atom)
367 {
368 struct radeon_winsys_cs *cs = sctx->rings.gfx.cs;
369 uint32_t cp_coher_cntl = 0;
370 uint32_t compute =
371 PKT3_SHADER_TYPE_S(!!(sctx->flags & R600_CONTEXT_FLAG_COMPUTE));
372
373 /* XXX SI flushes both ICACHE and KCACHE if either flag is set.
374 * XXX CIK shouldn't have this issue. Test CIK before separating the flags
375 * XXX to ensure there is no regression. Also find out if there is another
376 * XXX way to flush either ICACHE or KCACHE but not both for SI. */
377 if (sctx->flags & (R600_CONTEXT_INV_SHADER_CACHE |
378 R600_CONTEXT_INV_CONST_CACHE)) {
379 cp_coher_cntl |= S_0085F0_SH_ICACHE_ACTION_ENA(1) |
380 S_0085F0_SH_KCACHE_ACTION_ENA(1);
381 }
382 if (sctx->flags & (R600_CONTEXT_INV_TEX_CACHE |
383 R600_CONTEXT_STREAMOUT_FLUSH)) {
384 cp_coher_cntl |= S_0085F0_TC_ACTION_ENA(1) |
385 S_0085F0_TCL1_ACTION_ENA(1);
386 }
387 if (sctx->flags & R600_CONTEXT_FLUSH_AND_INV_CB) {
388 cp_coher_cntl |= S_0085F0_CB_ACTION_ENA(1) |
389 S_0085F0_CB0_DEST_BASE_ENA(1) |
390 S_0085F0_CB1_DEST_BASE_ENA(1) |
391 S_0085F0_CB2_DEST_BASE_ENA(1) |
392 S_0085F0_CB3_DEST_BASE_ENA(1) |
393 S_0085F0_CB4_DEST_BASE_ENA(1) |
394 S_0085F0_CB5_DEST_BASE_ENA(1) |
395 S_0085F0_CB6_DEST_BASE_ENA(1) |
396 S_0085F0_CB7_DEST_BASE_ENA(1);
397 }
398 if (sctx->flags & R600_CONTEXT_FLUSH_AND_INV_DB) {
399 cp_coher_cntl |= S_0085F0_DB_ACTION_ENA(1) |
400 S_0085F0_DB_DEST_BASE_ENA(1);
401 }
402
403 if (cp_coher_cntl) {
404 if (sctx->chip_class >= CIK) {
405 radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 5, 0) | compute);
406 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
407 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
408 radeon_emit(cs, 0xff); /* CP_COHER_SIZE_HI */
409 radeon_emit(cs, 0); /* CP_COHER_BASE */
410 radeon_emit(cs, 0); /* CP_COHER_BASE_HI */
411 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
412 } else {
413 radeon_emit(cs, PKT3(PKT3_SURFACE_SYNC, 3, 0) | compute);
414 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
415 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
416 radeon_emit(cs, 0); /* CP_COHER_BASE */
417 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
418 }
419 }
420
421 if (sctx->flags & R600_CONTEXT_FLUSH_AND_INV_CB_META) {
422 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
423 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_CB_META) | EVENT_INDEX(0));
424 }
425 if (sctx->flags & R600_CONTEXT_FLUSH_AND_INV_DB_META) {
426 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
427 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_DB_META) | EVENT_INDEX(0));
428 }
429 if (sctx->flags & R600_CONTEXT_FLUSH_WITH_INV_L2) {
430 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
431 radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_CACHE_FLUSH) | EVENT_INDEX(7) |
432 EVENT_WRITE_INV_L2);
433 }
434
435 if (sctx->flags & (R600_CONTEXT_WAIT_3D_IDLE |
436 R600_CONTEXT_PS_PARTIAL_FLUSH)) {
437 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
438 radeon_emit(cs, EVENT_TYPE(V_028A90_PS_PARTIAL_FLUSH) | EVENT_INDEX(4));
439 } else if (sctx->flags & R600_CONTEXT_STREAMOUT_FLUSH) {
440 /* Needed if streamout buffers are going to be used as a source. */
441 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
442 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
443 }
444
445 if (sctx->flags & R600_CONTEXT_CS_PARTIAL_FLUSH) {
446 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
447 radeon_emit(cs, EVENT_TYPE(V_028A90_CS_PARTIAL_FLUSH | EVENT_INDEX(4)));
448 }
449
450 if (sctx->flags & R600_CONTEXT_VGT_FLUSH) {
451 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
452 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
453 }
454 if (sctx->flags & R600_CONTEXT_VGT_STREAMOUT_SYNC) {
455 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0) | compute);
456 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_STREAMOUT_SYNC) | EVENT_INDEX(0));
457 }
458
459 sctx->flags = 0;
460 }
461
462 const struct r600_atom si_atom_cache_flush = { si_emit_cache_flush, 21 }; /* number of CS dwords */
463
464 static void si_get_draw_start_count(struct si_context *sctx,
465 const struct pipe_draw_info *info,
466 unsigned *start, unsigned *count)
467 {
468 if (info->indirect) {
469 struct r600_resource *indirect =
470 (struct r600_resource*)info->indirect;
471 int *data = r600_buffer_map_sync_with_rings(&sctx->b,
472 indirect, PIPE_TRANSFER_READ);
473 data += info->indirect_offset/sizeof(int);
474 *start = data[2];
475 *count = data[0];
476 } else {
477 *start = info->start;
478 *count = info->count;
479 }
480 }
481
482 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
483 {
484 struct si_context *sctx = (struct si_context *)ctx;
485 struct pipe_index_buffer ib = {};
486 uint32_t i;
487
488 if (!info->count && !info->indirect &&
489 (info->indexed || !info->count_from_stream_output))
490 return;
491
492 if (!sctx->ps_shader || !sctx->vs_shader)
493 return;
494
495 si_decompress_textures(sctx);
496 si_update_shaders(sctx);
497
498 if (sctx->vertex_buffers_dirty) {
499 si_update_vertex_buffers(sctx);
500 sctx->vertex_buffers_dirty = false;
501 }
502
503 if (info->indexed) {
504 /* Initialize the index buffer struct. */
505 pipe_resource_reference(&ib.buffer, sctx->index_buffer.buffer);
506 ib.user_buffer = sctx->index_buffer.user_buffer;
507 ib.index_size = sctx->index_buffer.index_size;
508 ib.offset = sctx->index_buffer.offset;
509
510 /* Translate or upload, if needed. */
511 if (ib.index_size == 1) {
512 struct pipe_resource *out_buffer = NULL;
513 unsigned out_offset, start, count, start_offset;
514 void *ptr;
515
516 si_get_draw_start_count(sctx, info, &start, &count);
517 start_offset = start * ib.index_size;
518
519 u_upload_alloc(sctx->b.uploader, start_offset, count * 2,
520 &out_offset, &out_buffer, &ptr);
521
522 util_shorten_ubyte_elts_to_userptr(&sctx->b.b, &ib, 0,
523 ib.offset + start_offset,
524 count, ptr);
525
526 pipe_resource_reference(&ib.buffer, NULL);
527 ib.user_buffer = NULL;
528 ib.buffer = out_buffer;
529 /* info->start will be added by the drawing code */
530 ib.offset = out_offset - start_offset;
531 ib.index_size = 2;
532 } else if (ib.user_buffer && !ib.buffer) {
533 unsigned start, count, start_offset;
534
535 si_get_draw_start_count(sctx, info, &start, &count);
536 start_offset = start * ib.index_size;
537
538 u_upload_data(sctx->b.uploader, start_offset, count * ib.index_size,
539 (char*)ib.user_buffer + start_offset,
540 &ib.offset, &ib.buffer);
541 /* info->start will be added by the drawing code */
542 ib.offset -= start_offset;
543 }
544 }
545
546 /* Check flush flags. */
547 if (sctx->b.flags)
548 sctx->atoms.s.cache_flush->dirty = true;
549
550 si_need_cs_space(sctx, 0, TRUE);
551
552 /* Emit states. */
553 for (i = 0; i < SI_NUM_ATOMS(sctx); i++) {
554 if (sctx->atoms.array[i]->dirty) {
555 sctx->atoms.array[i]->emit(&sctx->b, sctx->atoms.array[i]);
556 sctx->atoms.array[i]->dirty = false;
557 }
558 }
559
560 si_pm4_emit_dirty(sctx);
561 si_emit_rasterizer_prim_state(sctx, info->mode);
562 si_emit_draw_registers(sctx, info, &ib);
563 si_emit_draw_packets(sctx, info, &ib);
564
565 #if SI_TRACE_CS
566 if (sctx->screen->b.trace_bo) {
567 si_trace_emit(sctx);
568 }
569 #endif
570
571 /* Workaround for a VGT hang when streamout is enabled.
572 * It must be done after drawing. */
573 if (sctx->b.family == CHIP_HAWAII &&
574 (sctx->b.streamout.streamout_enabled ||
575 sctx->b.streamout.prims_gen_query_enabled)) {
576 sctx->b.flags |= R600_CONTEXT_VGT_STREAMOUT_SYNC;
577 }
578
579 /* Set the depth buffer as dirty. */
580 if (sctx->framebuffer.state.zsbuf) {
581 struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
582 struct r600_texture *rtex = (struct r600_texture *)surf->texture;
583
584 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
585 }
586 if (sctx->framebuffer.compressed_cb_mask) {
587 struct pipe_surface *surf;
588 struct r600_texture *rtex;
589 unsigned mask = sctx->framebuffer.compressed_cb_mask;
590
591 do {
592 unsigned i = u_bit_scan(&mask);
593 surf = sctx->framebuffer.state.cbufs[i];
594 rtex = (struct r600_texture*)surf->texture;
595
596 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
597 } while (mask);
598 }
599
600 pipe_resource_reference(&ib.buffer, NULL);
601 sctx->b.num_draw_calls++;
602 }
603
604 #if SI_TRACE_CS
605 void si_trace_emit(struct si_context *sctx)
606 {
607 struct si_screen *sscreen = sctx->screen;
608 struct radeon_winsys_cs *cs = sctx->b.rings.gfx.cs;
609 uint64_t va;
610
611 va = sscreen->b.trace_bo->gpu_address;
612 r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx, sscreen->b.trace_bo,
613 RADEON_USAGE_READWRITE, RADEON_PRIO_MIN);
614 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 4, 0));
615 radeon_emit(cs, PKT3_WRITE_DATA_DST_SEL(PKT3_WRITE_DATA_DST_SEL_MEM_SYNC) |
616 PKT3_WRITE_DATA_WR_CONFIRM |
617 PKT3_WRITE_DATA_ENGINE_SEL(PKT3_WRITE_DATA_ENGINE_SEL_ME));
618 radeon_emit(cs, va & 0xFFFFFFFFUL);
619 radeon_emit(cs, (va >> 32UL) & 0xFFFFFFFFUL);
620 radeon_emit(cs, cs->cdw);
621 radeon_emit(cs, sscreen->b.cs_count);
622 }
623 #endif