radeonsi: implement accelerated buffer copying
[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 "util/u_memory.h"
28 #include "util/u_framebuffer.h"
29 #include "util/u_blitter.h"
30 #include "tgsi/tgsi_parse.h"
31 #include "radeonsi_pipe.h"
32 #include "radeonsi_shader.h"
33 #include "si_state.h"
34 #include "../radeon/r600_cs.h"
35 #include "sid.h"
36
37 /*
38 * Shaders
39 */
40
41 static void si_pipe_shader_vs(struct pipe_context *ctx, struct si_pipe_shader *shader)
42 {
43 struct r600_context *rctx = (struct r600_context *)ctx;
44 struct si_pm4_state *pm4;
45 unsigned num_sgprs, num_user_sgprs;
46 unsigned nparams, i, vgpr_comp_cnt;
47 uint64_t va;
48
49 si_pm4_delete_state(rctx, vs, shader->pm4);
50 pm4 = shader->pm4 = si_pm4_alloc_state(rctx);
51
52 if (pm4 == NULL)
53 return;
54
55 /* Certain attributes (position, psize, etc.) don't count as params.
56 * VS is required to export at least one param and r600_shader_from_tgsi()
57 * takes care of adding a dummy export.
58 */
59 for (nparams = 0, i = 0 ; i < shader->shader.noutput; i++) {
60 switch (shader->shader.output[i].name) {
61 case TGSI_SEMANTIC_CLIPVERTEX:
62 case TGSI_SEMANTIC_POSITION:
63 case TGSI_SEMANTIC_PSIZE:
64 break;
65 default:
66 nparams++;
67 }
68 }
69 if (nparams < 1)
70 nparams = 1;
71
72 si_pm4_set_reg(pm4, R_0286C4_SPI_VS_OUT_CONFIG,
73 S_0286C4_VS_EXPORT_COUNT(nparams - 1));
74
75 si_pm4_set_reg(pm4, R_02870C_SPI_SHADER_POS_FORMAT,
76 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
77 S_02870C_POS1_EXPORT_FORMAT(shader->shader.nr_pos_exports > 1 ?
78 V_02870C_SPI_SHADER_4COMP :
79 V_02870C_SPI_SHADER_NONE) |
80 S_02870C_POS2_EXPORT_FORMAT(shader->shader.nr_pos_exports > 2 ?
81 V_02870C_SPI_SHADER_4COMP :
82 V_02870C_SPI_SHADER_NONE) |
83 S_02870C_POS3_EXPORT_FORMAT(shader->shader.nr_pos_exports > 3 ?
84 V_02870C_SPI_SHADER_4COMP :
85 V_02870C_SPI_SHADER_NONE));
86
87 va = r600_resource_va(ctx->screen, (void *)shader->bo);
88 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ);
89 si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8);
90 si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, va >> 40);
91
92 num_user_sgprs = SI_VS_NUM_USER_SGPR;
93 num_sgprs = shader->num_sgprs;
94 if (num_user_sgprs > num_sgprs) {
95 /* Last 2 reserved SGPRs are used for VCC */
96 num_sgprs = num_user_sgprs + 2;
97 }
98 assert(num_sgprs <= 104);
99
100 vgpr_comp_cnt = shader->shader.uses_instanceid ? 3 : 0;
101
102 si_pm4_set_reg(pm4, R_00B128_SPI_SHADER_PGM_RSRC1_VS,
103 S_00B128_VGPRS((shader->num_vgprs - 1) / 4) |
104 S_00B128_SGPRS((num_sgprs - 1) / 8) |
105 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt));
106 si_pm4_set_reg(pm4, R_00B12C_SPI_SHADER_PGM_RSRC2_VS,
107 S_00B12C_USER_SGPR(num_user_sgprs) |
108 S_00B12C_SO_BASE0_EN(!!shader->selector->so.stride[0]) |
109 S_00B12C_SO_BASE1_EN(!!shader->selector->so.stride[1]) |
110 S_00B12C_SO_BASE2_EN(!!shader->selector->so.stride[2]) |
111 S_00B12C_SO_BASE3_EN(!!shader->selector->so.stride[3]) |
112 S_00B12C_SO_EN(!!shader->selector->so.num_outputs));
113
114 if (rctx->b.chip_class >= CIK) {
115 si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS,
116 S_00B118_CU_EN(0xffff));
117 si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS,
118 S_00B11C_LIMIT(0));
119 }
120
121 si_pm4_bind_state(rctx, vs, shader->pm4);
122 rctx->b.flags |= R600_CONTEXT_INV_SHADER_CACHE;
123 }
124
125 static void si_pipe_shader_ps(struct pipe_context *ctx, struct si_pipe_shader *shader)
126 {
127 struct r600_context *rctx = (struct r600_context *)ctx;
128 struct si_pm4_state *pm4;
129 unsigned i, exports_ps, num_cout, spi_ps_in_control, db_shader_control;
130 unsigned num_sgprs, num_user_sgprs;
131 unsigned spi_baryc_cntl = 0, spi_ps_input_ena, spi_shader_z_format;
132 uint64_t va;
133
134 si_pm4_delete_state(rctx, ps, shader->pm4);
135 pm4 = shader->pm4 = si_pm4_alloc_state(rctx);
136
137 if (pm4 == NULL)
138 return;
139
140 db_shader_control = S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z) |
141 S_02880C_ALPHA_TO_MASK_DISABLE(rctx->fb_cb0_is_integer);
142
143 for (i = 0; i < shader->shader.ninput; i++) {
144 switch (shader->shader.input[i].name) {
145 case TGSI_SEMANTIC_POSITION:
146 if (shader->shader.input[i].centroid) {
147 /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION
148 * Possible vaules:
149 * 0 -> Position = pixel center (default)
150 * 1 -> Position = pixel centroid
151 * 2 -> Position = iterated sample number XXX:
152 * What does this mean?
153 */
154 spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(1);
155 }
156 /* Fall through */
157 case TGSI_SEMANTIC_FACE:
158 continue;
159 }
160 }
161
162 for (i = 0; i < shader->shader.noutput; i++) {
163 if (shader->shader.output[i].name == TGSI_SEMANTIC_POSITION)
164 db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
165 if (shader->shader.output[i].name == TGSI_SEMANTIC_STENCIL)
166 db_shader_control |= S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
167 }
168 if (shader->shader.uses_kill || shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
169 db_shader_control |= S_02880C_KILL_ENABLE(1);
170
171 exports_ps = 0;
172 num_cout = 0;
173 for (i = 0; i < shader->shader.noutput; i++) {
174 if (shader->shader.output[i].name == TGSI_SEMANTIC_POSITION ||
175 shader->shader.output[i].name == TGSI_SEMANTIC_STENCIL)
176 exports_ps |= 1;
177 else if (shader->shader.output[i].name == TGSI_SEMANTIC_COLOR) {
178 if (shader->shader.fs_write_all)
179 num_cout = shader->shader.nr_cbufs;
180 else
181 num_cout++;
182 }
183 }
184 if (!exports_ps) {
185 /* always at least export 1 component per pixel */
186 exports_ps = 2;
187 }
188
189 spi_ps_in_control = S_0286D8_NUM_INTERP(shader->shader.ninterp) |
190 S_0286D8_BC_OPTIMIZE_DISABLE(1);
191
192 si_pm4_set_reg(pm4, R_0286E0_SPI_BARYC_CNTL, spi_baryc_cntl);
193 spi_ps_input_ena = shader->spi_ps_input_ena;
194 /* we need to enable at least one of them, otherwise we hang the GPU */
195 assert(G_0286CC_PERSP_SAMPLE_ENA(spi_ps_input_ena) ||
196 G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) ||
197 G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) ||
198 G_0286CC_PERSP_PULL_MODEL_ENA(spi_ps_input_ena) ||
199 G_0286CC_LINEAR_SAMPLE_ENA(spi_ps_input_ena) ||
200 G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena) ||
201 G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena) ||
202 G_0286CC_LINE_STIPPLE_TEX_ENA(spi_ps_input_ena));
203
204 si_pm4_set_reg(pm4, R_0286CC_SPI_PS_INPUT_ENA, spi_ps_input_ena);
205 si_pm4_set_reg(pm4, R_0286D0_SPI_PS_INPUT_ADDR, spi_ps_input_ena);
206 si_pm4_set_reg(pm4, R_0286D8_SPI_PS_IN_CONTROL, spi_ps_in_control);
207
208 if (G_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(db_shader_control))
209 spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
210 else if (G_02880C_Z_EXPORT_ENABLE(db_shader_control))
211 spi_shader_z_format = V_028710_SPI_SHADER_32_R;
212 else
213 spi_shader_z_format = 0;
214 si_pm4_set_reg(pm4, R_028710_SPI_SHADER_Z_FORMAT, spi_shader_z_format);
215 si_pm4_set_reg(pm4, R_028714_SPI_SHADER_COL_FORMAT,
216 shader->spi_shader_col_format);
217 si_pm4_set_reg(pm4, R_02823C_CB_SHADER_MASK, shader->cb_shader_mask);
218
219 va = r600_resource_va(ctx->screen, (void *)shader->bo);
220 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ);
221 si_pm4_set_reg(pm4, R_00B020_SPI_SHADER_PGM_LO_PS, va >> 8);
222 si_pm4_set_reg(pm4, R_00B024_SPI_SHADER_PGM_HI_PS, va >> 40);
223
224 num_user_sgprs = SI_PS_NUM_USER_SGPR;
225 num_sgprs = shader->num_sgprs;
226 /* One SGPR after user SGPRs is pre-loaded with {prim_mask, lds_offset} */
227 if ((num_user_sgprs + 1) > num_sgprs) {
228 /* Last 2 reserved SGPRs are used for VCC */
229 num_sgprs = num_user_sgprs + 1 + 2;
230 }
231 assert(num_sgprs <= 104);
232
233 si_pm4_set_reg(pm4, R_00B028_SPI_SHADER_PGM_RSRC1_PS,
234 S_00B028_VGPRS((shader->num_vgprs - 1) / 4) |
235 S_00B028_SGPRS((num_sgprs - 1) / 8));
236 si_pm4_set_reg(pm4, R_00B02C_SPI_SHADER_PGM_RSRC2_PS,
237 S_00B02C_EXTRA_LDS_SIZE(shader->lds_size) |
238 S_00B02C_USER_SGPR(num_user_sgprs));
239 if (rctx->b.chip_class >= CIK) {
240 si_pm4_set_reg(pm4, R_00B01C_SPI_SHADER_PGM_RSRC3_PS,
241 S_00B01C_CU_EN(0xffff));
242 }
243
244 si_pm4_set_reg(pm4, R_02880C_DB_SHADER_CONTROL, db_shader_control);
245
246 shader->cb0_is_integer = rctx->fb_cb0_is_integer;
247 shader->sprite_coord_enable = rctx->sprite_coord_enable;
248 si_pm4_bind_state(rctx, ps, shader->pm4);
249 rctx->b.flags |= R600_CONTEXT_INV_SHADER_CACHE;
250 }
251
252 /*
253 * Drawing
254 */
255
256 static unsigned si_conv_pipe_prim(unsigned pprim)
257 {
258 static const unsigned prim_conv[] = {
259 [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
260 [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
261 [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
262 [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
263 [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
264 [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
265 [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
266 [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
267 [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
268 [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
269 [PIPE_PRIM_LINES_ADJACENCY] = ~0,
270 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = ~0,
271 [PIPE_PRIM_TRIANGLES_ADJACENCY] = ~0,
272 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = ~0
273 };
274 unsigned result = prim_conv[pprim];
275 if (result == ~0) {
276 R600_ERR("unsupported primitive type %d\n", pprim);
277 }
278 return result;
279 }
280
281 static unsigned r600_conv_prim_to_gs_out(unsigned mode)
282 {
283 static const int prim_conv[] = {
284 [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
285 [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
286 [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
287 [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
288 [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
289 [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
290 [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
291 [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
292 [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
293 [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
294 [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
295 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
296 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
297 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP
298 };
299 assert(mode < Elements(prim_conv));
300
301 return prim_conv[mode];
302 }
303
304 static bool si_update_draw_info_state(struct r600_context *rctx,
305 const struct pipe_draw_info *info,
306 const struct pipe_index_buffer *ib)
307 {
308 struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
309 struct si_shader *vs = &rctx->vs_shader->current->shader;
310 unsigned prim = si_conv_pipe_prim(info->mode);
311 unsigned gs_out_prim = r600_conv_prim_to_gs_out(info->mode);
312 unsigned ls_mask = 0;
313
314 if (pm4 == NULL)
315 return false;
316
317 if (prim == ~0) {
318 FREE(pm4);
319 return false;
320 }
321
322 if (rctx->b.chip_class >= CIK) {
323 struct si_state_rasterizer *rs = rctx->queued.named.rasterizer;
324 bool wd_switch_on_eop = prim == V_008958_DI_PT_POLYGON ||
325 prim == V_008958_DI_PT_LINELOOP ||
326 prim == V_008958_DI_PT_TRIFAN ||
327 prim == V_008958_DI_PT_TRISTRIP_ADJ ||
328 info->primitive_restart ||
329 (rs ? rs->line_stipple_enable : false);
330 /* If the WD switch is false, the IA switch must be false too. */
331 bool ia_switch_on_eop = wd_switch_on_eop;
332
333 si_pm4_set_reg(pm4, R_028AA8_IA_MULTI_VGT_PARAM,
334 S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) |
335 S_028AA8_PARTIAL_VS_WAVE_ON(1) |
336 S_028AA8_PRIMGROUP_SIZE(63) |
337 S_028AA8_WD_SWITCH_ON_EOP(wd_switch_on_eop));
338 si_pm4_set_reg(pm4, R_028B74_VGT_DISPATCH_DRAW_INDEX,
339 ib->index_size == 4 ? 0xFC000000 : 0xFC00);
340
341 si_pm4_set_reg(pm4, R_030908_VGT_PRIMITIVE_TYPE, prim);
342 } else {
343 si_pm4_set_reg(pm4, R_008958_VGT_PRIMITIVE_TYPE, prim);
344 }
345
346 si_pm4_set_reg(pm4, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out_prim);
347 si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
348 si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
349 si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET,
350 info->indexed ? info->index_bias : info->start);
351 si_pm4_set_reg(pm4, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX, info->restart_index);
352 si_pm4_set_reg(pm4, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, info->primitive_restart);
353 si_pm4_set_reg(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0 + SI_SGPR_START_INSTANCE * 4,
354 info->start_instance);
355
356 if (prim == V_008958_DI_PT_LINELIST)
357 ls_mask = 1;
358 else if (prim == V_008958_DI_PT_LINESTRIP)
359 ls_mask = 2;
360 si_pm4_set_reg(pm4, R_028A0C_PA_SC_LINE_STIPPLE,
361 S_028A0C_AUTO_RESET_CNTL(ls_mask) |
362 rctx->pa_sc_line_stipple);
363
364 if (info->mode == PIPE_PRIM_QUADS || info->mode == PIPE_PRIM_QUAD_STRIP || info->mode == PIPE_PRIM_POLYGON) {
365 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
366 S_028814_PROVOKING_VTX_LAST(1) | rctx->pa_su_sc_mode_cntl);
367 } else {
368 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL, rctx->pa_su_sc_mode_cntl);
369 }
370 si_pm4_set_reg(pm4, R_02881C_PA_CL_VS_OUT_CNTL,
371 S_02881C_USE_VTX_POINT_SIZE(vs->vs_out_point_size) |
372 S_02881C_USE_VTX_EDGE_FLAG(vs->vs_out_edgeflag) |
373 S_02881C_USE_VTX_RENDER_TARGET_INDX(vs->vs_out_layer) |
374 S_02881C_VS_OUT_CCDIST0_VEC_ENA((vs->clip_dist_write & 0x0F) != 0) |
375 S_02881C_VS_OUT_CCDIST1_VEC_ENA((vs->clip_dist_write & 0xF0) != 0) |
376 S_02881C_VS_OUT_MISC_VEC_ENA(vs->vs_out_misc_write) |
377 (rctx->queued.named.rasterizer->clip_plane_enable &
378 vs->clip_dist_write));
379 si_pm4_set_reg(pm4, R_028810_PA_CL_CLIP_CNTL,
380 rctx->queued.named.rasterizer->pa_cl_clip_cntl |
381 (vs->clip_dist_write ? 0 :
382 rctx->queued.named.rasterizer->clip_plane_enable & 0x3F));
383
384 si_pm4_set_state(rctx, draw_info, pm4);
385 return true;
386 }
387
388 static void si_update_spi_map(struct r600_context *rctx)
389 {
390 struct si_shader *ps = &rctx->ps_shader->current->shader;
391 struct si_shader *vs = &rctx->vs_shader->current->shader;
392 struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
393 unsigned i, j, tmp;
394
395 for (i = 0; i < ps->ninput; i++) {
396 unsigned name = ps->input[i].name;
397 unsigned param_offset = ps->input[i].param_offset;
398
399 if (name == TGSI_SEMANTIC_POSITION)
400 /* Read from preloaded VGPRs, not parameters */
401 continue;
402
403 bcolor:
404 tmp = 0;
405
406 if (ps->input[i].interpolate == TGSI_INTERPOLATE_CONSTANT ||
407 (ps->input[i].interpolate == TGSI_INTERPOLATE_COLOR &&
408 rctx->ps_shader->current->key.ps.flatshade)) {
409 tmp |= S_028644_FLAT_SHADE(1);
410 }
411
412 if (name == TGSI_SEMANTIC_GENERIC &&
413 rctx->sprite_coord_enable & (1 << ps->input[i].sid)) {
414 tmp |= S_028644_PT_SPRITE_TEX(1);
415 }
416
417 for (j = 0; j < vs->noutput; j++) {
418 if (name == vs->output[j].name &&
419 ps->input[i].sid == vs->output[j].sid) {
420 tmp |= S_028644_OFFSET(vs->output[j].param_offset);
421 break;
422 }
423 }
424
425 if (j == vs->noutput) {
426 /* No corresponding output found, load defaults into input */
427 tmp |= S_028644_OFFSET(0x20);
428 }
429
430 si_pm4_set_reg(pm4,
431 R_028644_SPI_PS_INPUT_CNTL_0 + param_offset * 4,
432 tmp);
433
434 if (name == TGSI_SEMANTIC_COLOR &&
435 rctx->ps_shader->current->key.ps.color_two_side) {
436 name = TGSI_SEMANTIC_BCOLOR;
437 param_offset++;
438 goto bcolor;
439 }
440 }
441
442 si_pm4_set_state(rctx, spi, pm4);
443 }
444
445 static void si_update_derived_state(struct r600_context *rctx)
446 {
447 struct pipe_context * ctx = (struct pipe_context*)rctx;
448 unsigned vs_dirty = 0, ps_dirty = 0;
449
450 if (!rctx->blitter->running) {
451 /* Flush depth textures which need to be flushed. */
452 for (int i = 0; i < SI_NUM_SHADERS; i++) {
453 if (rctx->samplers[i].depth_texture_mask) {
454 si_flush_depth_textures(rctx, &rctx->samplers[i]);
455 }
456 if (rctx->samplers[i].compressed_colortex_mask) {
457 r600_decompress_color_textures(rctx, &rctx->samplers[i]);
458 }
459 }
460 }
461
462 si_shader_select(ctx, rctx->vs_shader, &vs_dirty);
463
464 if (!rctx->vs_shader->current->pm4) {
465 si_pipe_shader_vs(ctx, rctx->vs_shader->current);
466 vs_dirty = 0;
467 }
468
469 if (vs_dirty) {
470 si_pm4_bind_state(rctx, vs, rctx->vs_shader->current->pm4);
471 }
472
473
474 si_shader_select(ctx, rctx->ps_shader, &ps_dirty);
475
476 if (!rctx->ps_shader->current->pm4) {
477 si_pipe_shader_ps(ctx, rctx->ps_shader->current);
478 ps_dirty = 0;
479 }
480 if (!rctx->ps_shader->current->bo) {
481 if (!rctx->dummy_pixel_shader->pm4)
482 si_pipe_shader_ps(ctx, rctx->dummy_pixel_shader);
483 else
484 si_pm4_bind_state(rctx, vs, rctx->dummy_pixel_shader->pm4);
485
486 ps_dirty = 0;
487 }
488 if (rctx->ps_shader->current->cb0_is_integer != rctx->fb_cb0_is_integer) {
489 si_pipe_shader_ps(ctx, rctx->ps_shader->current);
490 ps_dirty = 1;
491 }
492
493 if (ps_dirty) {
494 si_pm4_bind_state(rctx, ps, rctx->ps_shader->current->pm4);
495 }
496
497 if (si_pm4_state_changed(rctx, ps) || si_pm4_state_changed(rctx, vs)) {
498 /* XXX: Emitting the PS state even when only the VS changed
499 * fixes random failures with piglit glsl-max-varyings.
500 * Not sure why...
501 */
502 rctx->emitted.named.ps = NULL;
503 si_update_spi_map(rctx);
504 }
505 }
506
507 static void si_vertex_buffer_update(struct r600_context *rctx)
508 {
509 struct pipe_context *ctx = &rctx->b.b;
510 struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
511 bool bound[PIPE_MAX_ATTRIBS] = {};
512 unsigned i, count;
513 uint64_t va;
514
515 rctx->b.flags |= R600_CONTEXT_INV_TEX_CACHE;
516
517 count = rctx->vertex_elements->count;
518 assert(count <= 256 / 4);
519
520 si_pm4_sh_data_begin(pm4);
521 for (i = 0 ; i < count; i++) {
522 struct pipe_vertex_element *ve = &rctx->vertex_elements->elements[i];
523 struct pipe_vertex_buffer *vb;
524 struct r600_resource *rbuffer;
525 unsigned offset;
526
527 if (ve->vertex_buffer_index >= rctx->nr_vertex_buffers)
528 continue;
529
530 vb = &rctx->vertex_buffer[ve->vertex_buffer_index];
531 rbuffer = (struct r600_resource*)vb->buffer;
532 if (rbuffer == NULL)
533 continue;
534
535 offset = 0;
536 offset += vb->buffer_offset;
537 offset += ve->src_offset;
538
539 va = r600_resource_va(ctx->screen, (void*)rbuffer);
540 va += offset;
541
542 /* Fill in T# buffer resource description */
543 si_pm4_sh_data_add(pm4, va & 0xFFFFFFFF);
544 si_pm4_sh_data_add(pm4, (S_008F04_BASE_ADDRESS_HI(va >> 32) |
545 S_008F04_STRIDE(vb->stride)));
546 if (vb->stride)
547 /* Round up by rounding down and adding 1 */
548 si_pm4_sh_data_add(pm4,
549 (vb->buffer->width0 - offset -
550 util_format_get_blocksize(ve->src_format)) /
551 vb->stride + 1);
552 else
553 si_pm4_sh_data_add(pm4, vb->buffer->width0 - offset);
554 si_pm4_sh_data_add(pm4, rctx->vertex_elements->rsrc_word3[i]);
555
556 if (!bound[ve->vertex_buffer_index]) {
557 si_pm4_add_bo(pm4, rbuffer, RADEON_USAGE_READ);
558 bound[ve->vertex_buffer_index] = true;
559 }
560 }
561 si_pm4_sh_data_end(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0, SI_SGPR_VERTEX_BUFFER);
562 si_pm4_set_state(rctx, vertex_buffers, pm4);
563 }
564
565 static void si_state_draw(struct r600_context *rctx,
566 const struct pipe_draw_info *info,
567 const struct pipe_index_buffer *ib)
568 {
569 struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
570
571 if (pm4 == NULL)
572 return;
573
574 /* queries need some special values
575 * (this is non-zero if any query is active) */
576 if (rctx->num_cs_dw_nontimer_queries_suspend) {
577 struct si_state_dsa *dsa = rctx->queued.named.dsa;
578
579 if (rctx->b.chip_class >= CIK) {
580 si_pm4_set_reg(pm4, R_028004_DB_COUNT_CONTROL,
581 S_028004_PERFECT_ZPASS_COUNTS(1) |
582 S_028004_SAMPLE_RATE(rctx->fb_log_samples) |
583 S_028004_ZPASS_ENABLE(1) |
584 S_028004_SLICE_EVEN_ENABLE(1) |
585 S_028004_SLICE_ODD_ENABLE(1));
586 } else {
587 si_pm4_set_reg(pm4, R_028004_DB_COUNT_CONTROL,
588 S_028004_PERFECT_ZPASS_COUNTS(1) |
589 S_028004_SAMPLE_RATE(rctx->fb_log_samples));
590 }
591 si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE,
592 dsa->db_render_override |
593 S_02800C_NOOP_CULL_DISABLE(1));
594 }
595
596 if (info->count_from_stream_output) {
597 struct r600_so_target *t =
598 (struct r600_so_target*)info->count_from_stream_output;
599 uint64_t va = r600_resource_va(&rctx->screen->b.b,
600 &t->buf_filled_size->b.b);
601 va += t->buf_filled_size_offset;
602
603 si_pm4_set_reg(pm4, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE,
604 t->stride_in_dw);
605
606 si_pm4_cmd_begin(pm4, PKT3_COPY_DATA);
607 si_pm4_cmd_add(pm4,
608 COPY_DATA_SRC_SEL(COPY_DATA_MEM) |
609 COPY_DATA_DST_SEL(COPY_DATA_REG) |
610 COPY_DATA_WR_CONFIRM);
611 si_pm4_cmd_add(pm4, va); /* src address lo */
612 si_pm4_cmd_add(pm4, va >> 32UL); /* src address hi */
613 si_pm4_cmd_add(pm4, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2);
614 si_pm4_cmd_add(pm4, 0); /* unused */
615 si_pm4_add_bo(pm4, t->buf_filled_size, RADEON_USAGE_READ);
616 si_pm4_cmd_end(pm4, true);
617 }
618
619 /* draw packet */
620 si_pm4_cmd_begin(pm4, PKT3_INDEX_TYPE);
621 if (ib->index_size == 4) {
622 si_pm4_cmd_add(pm4, V_028A7C_VGT_INDEX_32 | (R600_BIG_ENDIAN ?
623 V_028A7C_VGT_DMA_SWAP_32_BIT : 0));
624 } else {
625 si_pm4_cmd_add(pm4, V_028A7C_VGT_INDEX_16 | (R600_BIG_ENDIAN ?
626 V_028A7C_VGT_DMA_SWAP_16_BIT : 0));
627 }
628 si_pm4_cmd_end(pm4, rctx->predicate_drawing);
629
630 si_pm4_cmd_begin(pm4, PKT3_NUM_INSTANCES);
631 si_pm4_cmd_add(pm4, info->instance_count);
632 si_pm4_cmd_end(pm4, rctx->predicate_drawing);
633
634 if (info->indexed) {
635 uint32_t max_size = (ib->buffer->width0 - ib->offset) /
636 rctx->index_buffer.index_size;
637 uint64_t va;
638 va = r600_resource_va(&rctx->screen->b.b, ib->buffer);
639 va += ib->offset;
640
641 si_pm4_add_bo(pm4, (struct r600_resource *)ib->buffer, RADEON_USAGE_READ);
642 si_cmd_draw_index_2(pm4, max_size, va, info->count,
643 V_0287F0_DI_SRC_SEL_DMA,
644 rctx->predicate_drawing);
645 } else {
646 uint32_t initiator = V_0287F0_DI_SRC_SEL_AUTO_INDEX;
647 initiator |= S_0287F0_USE_OPAQUE(!!info->count_from_stream_output);
648 si_cmd_draw_index_auto(pm4, info->count, initiator, rctx->predicate_drawing);
649 }
650 si_pm4_set_state(rctx, draw, pm4);
651 }
652
653 void si_emit_cache_flush(struct r600_common_context *rctx, struct r600_atom *atom)
654 {
655 struct radeon_winsys_cs *cs = rctx->rings.gfx.cs;
656 uint32_t cp_coher_cntl = 0;
657
658 /* XXX SI flushes both ICACHE and KCACHE if either flag is set.
659 * XXX CIK shouldn't have this issue. Test CIK before separating the flags
660 * XXX to ensure there is no regression. Also find out if there is another
661 * XXX way to flush either ICACHE or KCACHE but not both for SI. */
662 if (rctx->flags & (R600_CONTEXT_INV_SHADER_CACHE |
663 R600_CONTEXT_INV_CONST_CACHE)) {
664 cp_coher_cntl |= S_0085F0_SH_ICACHE_ACTION_ENA(1) |
665 S_0085F0_SH_KCACHE_ACTION_ENA(1);
666 }
667 if (rctx->flags & (R600_CONTEXT_INV_TEX_CACHE |
668 R600_CONTEXT_STREAMOUT_FLUSH)) {
669 cp_coher_cntl |= S_0085F0_TC_ACTION_ENA(1) |
670 S_0085F0_TCL1_ACTION_ENA(1);
671 }
672 if (rctx->flags & R600_CONTEXT_FLUSH_AND_INV_CB) {
673 cp_coher_cntl |= S_0085F0_CB_ACTION_ENA(1) |
674 S_0085F0_CB0_DEST_BASE_ENA(1) |
675 S_0085F0_CB1_DEST_BASE_ENA(1) |
676 S_0085F0_CB2_DEST_BASE_ENA(1) |
677 S_0085F0_CB3_DEST_BASE_ENA(1) |
678 S_0085F0_CB4_DEST_BASE_ENA(1) |
679 S_0085F0_CB5_DEST_BASE_ENA(1) |
680 S_0085F0_CB6_DEST_BASE_ENA(1) |
681 S_0085F0_CB7_DEST_BASE_ENA(1);
682 }
683 if (rctx->flags & R600_CONTEXT_FLUSH_AND_INV_DB) {
684 cp_coher_cntl |= S_0085F0_DB_ACTION_ENA(1) |
685 S_0085F0_DB_DEST_BASE_ENA(1);
686 }
687
688 if (cp_coher_cntl) {
689 if (rctx->chip_class >= CIK) {
690 radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 5, 0));
691 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
692 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
693 radeon_emit(cs, 0xff); /* CP_COHER_SIZE_HI */
694 radeon_emit(cs, 0); /* CP_COHER_BASE */
695 radeon_emit(cs, 0); /* CP_COHER_BASE_HI */
696 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
697 } else {
698 radeon_emit(cs, PKT3(PKT3_SURFACE_SYNC, 3, 0));
699 radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
700 radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
701 radeon_emit(cs, 0); /* CP_COHER_BASE */
702 radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
703 }
704 }
705
706 if (rctx->flags & R600_CONTEXT_FLUSH_AND_INV_CB_META) {
707 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
708 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_CB_META) | EVENT_INDEX(0));
709 }
710
711 if (rctx->flags & R600_CONTEXT_WAIT_3D_IDLE) {
712 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
713 radeon_emit(cs, EVENT_TYPE(V_028A90_PS_PARTIAL_FLUSH) | EVENT_INDEX(4));
714 } else if (rctx->flags & R600_CONTEXT_STREAMOUT_FLUSH) {
715 /* Needed if streamout buffers are going to be used as a source. */
716 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
717 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
718 }
719
720 rctx->flags = 0;
721 }
722
723 const struct r600_atom si_atom_cache_flush = { si_emit_cache_flush, 11 }; /* number of CS dwords */
724
725 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
726 {
727 struct r600_context *rctx = (struct r600_context *)ctx;
728 struct pipe_index_buffer ib = {};
729 uint32_t i;
730
731 if (!info->count && (info->indexed || !info->count_from_stream_output))
732 return;
733
734 if (!rctx->ps_shader || !rctx->vs_shader)
735 return;
736
737 si_update_derived_state(rctx);
738 si_vertex_buffer_update(rctx);
739
740 if (info->indexed) {
741 /* Initialize the index buffer struct. */
742 pipe_resource_reference(&ib.buffer, rctx->index_buffer.buffer);
743 ib.user_buffer = rctx->index_buffer.user_buffer;
744 ib.index_size = rctx->index_buffer.index_size;
745 ib.offset = rctx->index_buffer.offset + info->start * ib.index_size;
746
747 /* Translate or upload, if needed. */
748 r600_translate_index_buffer(rctx, &ib, info->count);
749
750 if (ib.user_buffer && !ib.buffer) {
751 r600_upload_index_buffer(rctx, &ib, info->count);
752 }
753 }
754
755 if (!si_update_draw_info_state(rctx, info, &ib))
756 return;
757
758 si_state_draw(rctx, info, &ib);
759
760 rctx->pm4_dirty_cdwords += si_pm4_dirty_dw(rctx);
761
762 /* Check flush flags. */
763 if (rctx->b.flags)
764 rctx->atoms.cache_flush->dirty = true;
765
766 si_need_cs_space(rctx, 0, TRUE);
767
768 /* Emit states. */
769 for (i = 0; i < SI_NUM_ATOMS(rctx); i++) {
770 if (rctx->atoms.array[i]->dirty) {
771 rctx->atoms.array[i]->emit(&rctx->b, rctx->atoms.array[i]);
772 rctx->atoms.array[i]->dirty = false;
773 }
774 }
775
776 si_pm4_emit_dirty(rctx);
777 rctx->pm4_dirty_cdwords = 0;
778
779 #if R600_TRACE_CS
780 if (rctx->screen->trace_bo) {
781 r600_trace_emit(rctx);
782 }
783 #endif
784
785 /* Set the depth buffer as dirty. */
786 if (rctx->framebuffer.zsbuf) {
787 struct pipe_surface *surf = rctx->framebuffer.zsbuf;
788 struct r600_texture *rtex = (struct r600_texture *)surf->texture;
789
790 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
791 }
792 if (rctx->fb_compressed_cb_mask) {
793 struct pipe_surface *surf;
794 struct r600_texture *rtex;
795 unsigned mask = rctx->fb_compressed_cb_mask;
796
797 do {
798 unsigned i = u_bit_scan(&mask);
799 surf = rctx->framebuffer.cbufs[i];
800 rtex = (struct r600_texture*)surf->texture;
801
802 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
803 } while (mask);
804 }
805
806 pipe_resource_reference(&ib.buffer, NULL);
807 }