radeonsi: emit TA_BC_BASE_ADDR_HI for border color on CIK
[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 "sid.h"
35
36 /*
37 * Shaders
38 */
39
40 static void si_pipe_shader_vs(struct pipe_context *ctx, struct si_pipe_shader *shader)
41 {
42 struct r600_context *rctx = (struct r600_context *)ctx;
43 struct si_pm4_state *pm4;
44 unsigned num_sgprs, num_user_sgprs;
45 unsigned nparams, i, vgpr_comp_cnt;
46 uint64_t va;
47
48 si_pm4_delete_state(rctx, vs, shader->pm4);
49 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
50
51 si_pm4_inval_shader_cache(pm4);
52
53 /* Certain attributes (position, psize, etc.) don't count as params.
54 * VS is required to export at least one param and r600_shader_from_tgsi()
55 * takes care of adding a dummy export.
56 */
57 for (nparams = 0, i = 0 ; i < shader->shader.noutput; i++) {
58 switch (shader->shader.output[i].name) {
59 case TGSI_SEMANTIC_POSITION:
60 case TGSI_SEMANTIC_PSIZE:
61 break;
62 default:
63 nparams++;
64 }
65 }
66 if (nparams < 1)
67 nparams = 1;
68
69 si_pm4_set_reg(pm4, R_0286C4_SPI_VS_OUT_CONFIG,
70 S_0286C4_VS_EXPORT_COUNT(nparams - 1));
71
72 si_pm4_set_reg(pm4, R_02870C_SPI_SHADER_POS_FORMAT,
73 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
74 S_02870C_POS1_EXPORT_FORMAT(shader->shader.vs_out_misc_write ?
75 V_02870C_SPI_SHADER_4COMP :
76 V_02870C_SPI_SHADER_NONE) |
77 S_02870C_POS2_EXPORT_FORMAT((shader->shader.clip_dist_write & 0x0F) ?
78 V_02870C_SPI_SHADER_4COMP :
79 V_02870C_SPI_SHADER_NONE) |
80 S_02870C_POS3_EXPORT_FORMAT((shader->shader.clip_dist_write & 0xF0) ?
81 V_02870C_SPI_SHADER_4COMP :
82 V_02870C_SPI_SHADER_NONE));
83
84 va = r600_resource_va(ctx->screen, (void *)shader->bo);
85 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ);
86 si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8);
87 si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, va >> 40);
88
89 num_user_sgprs = SI_VS_NUM_USER_SGPR;
90 num_sgprs = shader->num_sgprs;
91 if (num_user_sgprs > num_sgprs)
92 num_sgprs = num_user_sgprs;
93 /* Last 2 reserved SGPRs are used for VCC */
94 num_sgprs += 2;
95 assert(num_sgprs <= 104);
96
97 vgpr_comp_cnt = shader->shader.uses_instanceid ? 3 : 0;
98
99 si_pm4_set_reg(pm4, R_00B128_SPI_SHADER_PGM_RSRC1_VS,
100 S_00B128_VGPRS((shader->num_vgprs - 1) / 4) |
101 S_00B128_SGPRS((num_sgprs - 1) / 8) |
102 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt));
103 si_pm4_set_reg(pm4, R_00B12C_SPI_SHADER_PGM_RSRC2_VS,
104 S_00B12C_USER_SGPR(num_user_sgprs));
105
106 si_pm4_bind_state(rctx, vs, shader->pm4);
107 }
108
109 static void si_pipe_shader_ps(struct pipe_context *ctx, struct si_pipe_shader *shader)
110 {
111 struct r600_context *rctx = (struct r600_context *)ctx;
112 struct si_pm4_state *pm4;
113 unsigned i, exports_ps, num_cout, spi_ps_in_control, db_shader_control;
114 unsigned num_sgprs, num_user_sgprs;
115 boolean have_linear = FALSE, have_centroid = FALSE, have_perspective = FALSE;
116 unsigned fragcoord_interp_mode = 0;
117 unsigned spi_baryc_cntl, spi_ps_input_ena, spi_shader_z_format;
118 uint64_t va;
119
120 si_pm4_delete_state(rctx, ps, shader->pm4);
121 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
122
123 si_pm4_inval_shader_cache(pm4);
124
125 db_shader_control = S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z);
126 for (i = 0; i < shader->shader.ninput; i++) {
127 switch (shader->shader.input[i].name) {
128 case TGSI_SEMANTIC_POSITION:
129 if (shader->shader.input[i].centroid) {
130 /* fragcoord_interp_mode will be written to
131 * SPI_BARYC_CNTL.POS_FLOAT_LOCATION
132 * Possible vaules:
133 * 0 -> Position = pixel center (default)
134 * 1 -> Position = pixel centroid
135 * 2 -> Position = iterated sample number XXX:
136 * What does this mean?
137 */
138 fragcoord_interp_mode = 1;
139 }
140 /* Fall through */
141 case TGSI_SEMANTIC_FACE:
142 continue;
143 }
144
145 if (shader->shader.input[i].interpolate == TGSI_INTERPOLATE_LINEAR)
146 have_linear = TRUE;
147 if (shader->shader.input[i].interpolate == TGSI_INTERPOLATE_PERSPECTIVE)
148 have_perspective = TRUE;
149 if (shader->shader.input[i].centroid)
150 have_centroid = TRUE;
151 }
152
153 for (i = 0; i < shader->shader.noutput; i++) {
154 if (shader->shader.output[i].name == TGSI_SEMANTIC_POSITION)
155 db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
156 if (shader->shader.output[i].name == TGSI_SEMANTIC_STENCIL)
157 db_shader_control |= S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
158 }
159 if (shader->shader.uses_kill || shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
160 db_shader_control |= S_02880C_KILL_ENABLE(1);
161
162 exports_ps = 0;
163 num_cout = 0;
164 for (i = 0; i < shader->shader.noutput; i++) {
165 if (shader->shader.output[i].name == TGSI_SEMANTIC_POSITION ||
166 shader->shader.output[i].name == TGSI_SEMANTIC_STENCIL)
167 exports_ps |= 1;
168 else if (shader->shader.output[i].name == TGSI_SEMANTIC_COLOR) {
169 if (shader->shader.fs_write_all)
170 num_cout = shader->shader.nr_cbufs;
171 else
172 num_cout++;
173 }
174 }
175 if (!exports_ps) {
176 /* always at least export 1 component per pixel */
177 exports_ps = 2;
178 }
179
180 spi_ps_in_control = S_0286D8_NUM_INTERP(shader->shader.ninterp);
181
182 spi_baryc_cntl = 0;
183 if (have_perspective)
184 spi_baryc_cntl |= have_centroid ?
185 S_0286E0_PERSP_CENTROID_CNTL(1) : S_0286E0_PERSP_CENTER_CNTL(1);
186 if (have_linear)
187 spi_baryc_cntl |= have_centroid ?
188 S_0286E0_LINEAR_CENTROID_CNTL(1) : S_0286E0_LINEAR_CENTER_CNTL(1);
189 spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(fragcoord_interp_mode);
190
191 si_pm4_set_reg(pm4, R_0286E0_SPI_BARYC_CNTL, spi_baryc_cntl);
192 spi_ps_input_ena = shader->spi_ps_input_ena;
193 /* we need to enable at least one of them, otherwise we hang the GPU */
194 assert(G_0286CC_PERSP_SAMPLE_ENA(spi_ps_input_ena) ||
195 G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) ||
196 G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) ||
197 G_0286CC_PERSP_PULL_MODEL_ENA(spi_ps_input_ena) ||
198 G_0286CC_LINEAR_SAMPLE_ENA(spi_ps_input_ena) ||
199 G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena) ||
200 G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena) ||
201 G_0286CC_LINE_STIPPLE_TEX_ENA(spi_ps_input_ena));
202
203 si_pm4_set_reg(pm4, R_0286CC_SPI_PS_INPUT_ENA, spi_ps_input_ena);
204 si_pm4_set_reg(pm4, R_0286D0_SPI_PS_INPUT_ADDR, spi_ps_input_ena);
205 si_pm4_set_reg(pm4, R_0286D8_SPI_PS_IN_CONTROL, spi_ps_in_control);
206
207 if (G_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(db_shader_control))
208 spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
209 else if (G_02880C_Z_EXPORT_ENABLE(db_shader_control))
210 spi_shader_z_format = V_028710_SPI_SHADER_32_R;
211 else
212 spi_shader_z_format = 0;
213 si_pm4_set_reg(pm4, R_028710_SPI_SHADER_Z_FORMAT, spi_shader_z_format);
214 si_pm4_set_reg(pm4, R_028714_SPI_SHADER_COL_FORMAT,
215 shader->spi_shader_col_format);
216 si_pm4_set_reg(pm4, R_02823C_CB_SHADER_MASK, shader->cb_shader_mask);
217
218 va = r600_resource_va(ctx->screen, (void *)shader->bo);
219 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ);
220 si_pm4_set_reg(pm4, R_00B020_SPI_SHADER_PGM_LO_PS, va >> 8);
221 si_pm4_set_reg(pm4, R_00B024_SPI_SHADER_PGM_HI_PS, va >> 40);
222
223 num_user_sgprs = SI_PS_NUM_USER_SGPR;
224 num_sgprs = shader->num_sgprs;
225 if (num_user_sgprs > num_sgprs)
226 num_sgprs = num_user_sgprs;
227 /* Last 2 reserved SGPRs are used for VCC */
228 num_sgprs += 2;
229 assert(num_sgprs <= 104);
230
231 si_pm4_set_reg(pm4, R_00B028_SPI_SHADER_PGM_RSRC1_PS,
232 S_00B028_VGPRS((shader->num_vgprs - 1) / 4) |
233 S_00B028_SGPRS((num_sgprs - 1) / 8));
234 si_pm4_set_reg(pm4, R_00B02C_SPI_SHADER_PGM_RSRC2_PS,
235 S_00B02C_USER_SGPR(num_user_sgprs));
236
237 si_pm4_set_reg(pm4, R_02880C_DB_SHADER_CONTROL, db_shader_control);
238
239 shader->sprite_coord_enable = rctx->sprite_coord_enable;
240 si_pm4_bind_state(rctx, ps, shader->pm4);
241 }
242
243 /*
244 * Drawing
245 */
246
247 static unsigned si_conv_pipe_prim(unsigned pprim)
248 {
249 static const unsigned prim_conv[] = {
250 [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
251 [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
252 [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
253 [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
254 [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
255 [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
256 [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
257 [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
258 [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
259 [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
260 [PIPE_PRIM_LINES_ADJACENCY] = ~0,
261 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = ~0,
262 [PIPE_PRIM_TRIANGLES_ADJACENCY] = ~0,
263 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = ~0
264 };
265 unsigned result = prim_conv[pprim];
266 if (result == ~0) {
267 R600_ERR("unsupported primitive type %d\n", pprim);
268 }
269 return result;
270 }
271
272 static bool si_update_draw_info_state(struct r600_context *rctx,
273 const struct pipe_draw_info *info)
274 {
275 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
276 struct si_shader *vs = &rctx->vs_shader->current->shader;
277 unsigned prim = si_conv_pipe_prim(info->mode);
278 unsigned ls_mask = 0;
279
280 if (pm4 == NULL)
281 return false;
282
283 if (prim == ~0) {
284 FREE(pm4);
285 return false;
286 }
287
288 if (rctx->chip_class >= CIK)
289 si_pm4_set_reg(pm4, R_030908_VGT_PRIMITIVE_TYPE, prim);
290 else
291 si_pm4_set_reg(pm4, R_008958_VGT_PRIMITIVE_TYPE, prim);
292 si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
293 si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
294 si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET,
295 info->indexed ? info->index_bias : info->start);
296 si_pm4_set_reg(pm4, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX, info->restart_index);
297 si_pm4_set_reg(pm4, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, info->primitive_restart);
298 si_pm4_set_reg(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0 + SI_SGPR_START_INSTANCE * 4,
299 info->start_instance);
300
301 if (prim == V_008958_DI_PT_LINELIST)
302 ls_mask = 1;
303 else if (prim == V_008958_DI_PT_LINESTRIP)
304 ls_mask = 2;
305 si_pm4_set_reg(pm4, R_028A0C_PA_SC_LINE_STIPPLE,
306 S_028A0C_AUTO_RESET_CNTL(ls_mask) |
307 rctx->pa_sc_line_stipple);
308
309 if (info->mode == PIPE_PRIM_QUADS || info->mode == PIPE_PRIM_QUAD_STRIP || info->mode == PIPE_PRIM_POLYGON) {
310 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
311 S_028814_PROVOKING_VTX_LAST(1) | rctx->pa_su_sc_mode_cntl);
312 } else {
313 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL, rctx->pa_su_sc_mode_cntl);
314 }
315 si_pm4_set_reg(pm4, R_02881C_PA_CL_VS_OUT_CNTL,
316 S_02881C_USE_VTX_POINT_SIZE(vs->vs_out_point_size) |
317 S_02881C_VS_OUT_CCDIST0_VEC_ENA((vs->clip_dist_write & 0x0F) != 0) |
318 S_02881C_VS_OUT_CCDIST1_VEC_ENA((vs->clip_dist_write & 0xF0) != 0) |
319 S_02881C_VS_OUT_MISC_VEC_ENA(vs->vs_out_misc_write) |
320 (rctx->queued.named.rasterizer->clip_plane_enable &
321 vs->clip_dist_write));
322 si_pm4_set_reg(pm4, R_028810_PA_CL_CLIP_CNTL,
323 rctx->queued.named.rasterizer->pa_cl_clip_cntl |
324 (vs->clip_dist_write ? 0 :
325 rctx->queued.named.rasterizer->clip_plane_enable & 0x3F));
326
327 si_pm4_set_state(rctx, draw_info, pm4);
328 return true;
329 }
330
331 static void si_update_spi_map(struct r600_context *rctx)
332 {
333 struct si_shader *ps = &rctx->ps_shader->current->shader;
334 struct si_shader *vs = &rctx->vs_shader->current->shader;
335 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
336 unsigned i, j, tmp;
337
338 for (i = 0; i < ps->ninput; i++) {
339 unsigned name = ps->input[i].name;
340 unsigned param_offset = ps->input[i].param_offset;
341
342 if (name == TGSI_SEMANTIC_POSITION)
343 /* Read from preloaded VGPRs, not parameters */
344 continue;
345
346 bcolor:
347 tmp = 0;
348
349 if (ps->input[i].interpolate == TGSI_INTERPOLATE_CONSTANT ||
350 (ps->input[i].interpolate == TGSI_INTERPOLATE_COLOR &&
351 rctx->ps_shader->current->key.ps.flatshade)) {
352 tmp |= S_028644_FLAT_SHADE(1);
353 }
354
355 if (name == TGSI_SEMANTIC_GENERIC &&
356 rctx->sprite_coord_enable & (1 << ps->input[i].sid)) {
357 tmp |= S_028644_PT_SPRITE_TEX(1);
358 }
359
360 for (j = 0; j < vs->noutput; j++) {
361 if (name == vs->output[j].name &&
362 ps->input[i].sid == vs->output[j].sid) {
363 tmp |= S_028644_OFFSET(vs->output[j].param_offset);
364 break;
365 }
366 }
367
368 if (j == vs->noutput) {
369 /* No corresponding output found, load defaults into input */
370 tmp |= S_028644_OFFSET(0x20);
371 }
372
373 si_pm4_set_reg(pm4,
374 R_028644_SPI_PS_INPUT_CNTL_0 + param_offset * 4,
375 tmp);
376
377 if (name == TGSI_SEMANTIC_COLOR &&
378 rctx->ps_shader->current->key.ps.color_two_side) {
379 name = TGSI_SEMANTIC_BCOLOR;
380 param_offset++;
381 goto bcolor;
382 }
383 }
384
385 si_pm4_set_state(rctx, spi, pm4);
386 }
387
388 static void si_update_derived_state(struct r600_context *rctx)
389 {
390 struct pipe_context * ctx = (struct pipe_context*)rctx;
391 unsigned vs_dirty = 0, ps_dirty = 0;
392
393 if (!rctx->blitter->running) {
394 /* Flush depth textures which need to be flushed. */
395 if (rctx->vs_samplers.depth_texture_mask) {
396 si_flush_depth_textures(rctx, &rctx->vs_samplers);
397 }
398 if (rctx->ps_samplers.depth_texture_mask) {
399 si_flush_depth_textures(rctx, &rctx->ps_samplers);
400 }
401 }
402
403 si_shader_select(ctx, rctx->vs_shader, &vs_dirty);
404
405 if (!rctx->vs_shader->current->pm4) {
406 si_pipe_shader_vs(ctx, rctx->vs_shader->current);
407 vs_dirty = 0;
408 }
409
410 if (vs_dirty) {
411 si_pm4_bind_state(rctx, vs, rctx->vs_shader->current->pm4);
412 }
413
414
415 si_shader_select(ctx, rctx->ps_shader, &ps_dirty);
416
417 if (!rctx->ps_shader->current->pm4) {
418 si_pipe_shader_ps(ctx, rctx->ps_shader->current);
419 ps_dirty = 0;
420 }
421 if (!rctx->ps_shader->current->bo) {
422 if (!rctx->dummy_pixel_shader->pm4)
423 si_pipe_shader_ps(ctx, rctx->dummy_pixel_shader);
424 else
425 si_pm4_bind_state(rctx, vs, rctx->dummy_pixel_shader->pm4);
426
427 ps_dirty = 0;
428 }
429
430 if (ps_dirty) {
431 si_pm4_bind_state(rctx, ps, rctx->ps_shader->current->pm4);
432 }
433
434 if (si_pm4_state_changed(rctx, ps) || si_pm4_state_changed(rctx, vs)) {
435 /* XXX: Emitting the PS state even when only the VS changed
436 * fixes random failures with piglit glsl-max-varyings.
437 * Not sure why...
438 */
439 rctx->emitted.named.ps = NULL;
440 si_update_spi_map(rctx);
441 }
442 }
443
444 static void si_constant_buffer_update(struct r600_context *rctx)
445 {
446 struct pipe_context *ctx = &rctx->context;
447 struct si_pm4_state *pm4;
448 unsigned shader, i;
449 uint64_t va;
450
451 if (!rctx->constbuf_state[PIPE_SHADER_VERTEX].dirty_mask &&
452 !rctx->constbuf_state[PIPE_SHADER_FRAGMENT].dirty_mask)
453 return;
454
455 for (shader = PIPE_SHADER_VERTEX ; shader <= PIPE_SHADER_FRAGMENT; shader++) {
456 struct r600_constbuf_state *state = &rctx->constbuf_state[shader];
457
458 pm4 = CALLOC_STRUCT(si_pm4_state);
459 if (!pm4)
460 continue;
461
462 si_pm4_inval_shader_cache(pm4);
463 si_pm4_sh_data_begin(pm4);
464
465 for (i = 0; i < 2; i++) {
466 if (state->enabled_mask & (1 << i)) {
467 struct pipe_constant_buffer *cb = &state->cb[i];
468 struct si_resource *rbuffer = si_resource(cb->buffer);
469
470 va = r600_resource_va(ctx->screen, (void*)rbuffer);
471 va += cb->buffer_offset;
472
473 si_pm4_add_bo(pm4, rbuffer, RADEON_USAGE_READ);
474
475 /* Fill in a T# buffer resource description */
476 si_pm4_sh_data_add(pm4, va);
477 si_pm4_sh_data_add(pm4, (S_008F04_BASE_ADDRESS_HI(va >> 32) |
478 S_008F04_STRIDE(0)));
479 si_pm4_sh_data_add(pm4, cb->buffer_size);
480 si_pm4_sh_data_add(pm4, S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
481 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
482 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
483 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
484 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
485 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32));
486 } else {
487 /* Fill in an empty T# buffer resource description */
488 si_pm4_sh_data_add(pm4, 0);
489 si_pm4_sh_data_add(pm4, 0);
490 si_pm4_sh_data_add(pm4, 0);
491 si_pm4_sh_data_add(pm4, 0);
492 }
493 }
494
495 switch (shader) {
496 case PIPE_SHADER_VERTEX:
497 si_pm4_sh_data_end(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0, SI_SGPR_CONST);
498 si_pm4_set_state(rctx, vs_const, pm4);
499 break;
500
501 case PIPE_SHADER_FRAGMENT:
502 si_pm4_sh_data_end(pm4, R_00B030_SPI_SHADER_USER_DATA_PS_0, SI_SGPR_CONST);
503 si_pm4_set_state(rctx, ps_const, pm4);
504 break;
505
506 default:
507 R600_ERR("unsupported %d\n", shader);
508 FREE(pm4);
509 return;
510 }
511
512 state->dirty_mask = 0;
513 }
514 }
515
516 static void si_vertex_buffer_update(struct r600_context *rctx)
517 {
518 struct pipe_context *ctx = &rctx->context;
519 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
520 bool bound[PIPE_MAX_ATTRIBS] = {};
521 unsigned i, count;
522 uint64_t va;
523
524 si_pm4_inval_texture_cache(pm4);
525
526 /* bind vertex buffer once */
527 count = rctx->vertex_elements->count;
528 assert(count <= 256 / 4);
529
530 si_pm4_sh_data_begin(pm4);
531 for (i = 0 ; i < count; i++) {
532 struct pipe_vertex_element *ve = &rctx->vertex_elements->elements[i];
533 struct pipe_vertex_buffer *vb;
534 struct si_resource *rbuffer;
535 unsigned offset;
536
537 if (ve->vertex_buffer_index >= rctx->nr_vertex_buffers)
538 continue;
539
540 vb = &rctx->vertex_buffer[ve->vertex_buffer_index];
541 rbuffer = (struct si_resource*)vb->buffer;
542 if (rbuffer == NULL)
543 continue;
544
545 offset = 0;
546 offset += vb->buffer_offset;
547 offset += ve->src_offset;
548
549 va = r600_resource_va(ctx->screen, (void*)rbuffer);
550 va += offset;
551
552 /* Fill in T# buffer resource description */
553 si_pm4_sh_data_add(pm4, va & 0xFFFFFFFF);
554 si_pm4_sh_data_add(pm4, (S_008F04_BASE_ADDRESS_HI(va >> 32) |
555 S_008F04_STRIDE(vb->stride)));
556 if (vb->stride)
557 /* Round up by rounding down and adding 1 */
558 si_pm4_sh_data_add(pm4,
559 (vb->buffer->width0 - offset -
560 util_format_get_blocksize(ve->src_format)) /
561 vb->stride + 1);
562 else
563 si_pm4_sh_data_add(pm4, vb->buffer->width0 - offset);
564 si_pm4_sh_data_add(pm4, rctx->vertex_elements->rsrc_word3[i]);
565
566 if (!bound[ve->vertex_buffer_index]) {
567 si_pm4_add_bo(pm4, rbuffer, RADEON_USAGE_READ);
568 bound[ve->vertex_buffer_index] = true;
569 }
570 }
571 si_pm4_sh_data_end(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0, SI_SGPR_VERTEX_BUFFER);
572 si_pm4_set_state(rctx, vertex_buffers, pm4);
573 }
574
575 static void si_state_draw(struct r600_context *rctx,
576 const struct pipe_draw_info *info,
577 const struct pipe_index_buffer *ib)
578 {
579 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
580
581 /* queries need some special values
582 * (this is non-zero if any query is active) */
583 if (rctx->num_cs_dw_queries_suspend) {
584 struct si_state_dsa *dsa = rctx->queued.named.dsa;
585
586 si_pm4_set_reg(pm4, R_028004_DB_COUNT_CONTROL,
587 S_028004_PERFECT_ZPASS_COUNTS(1));
588 si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE,
589 dsa->db_render_override |
590 S_02800C_NOOP_CULL_DISABLE(1));
591 }
592
593 /* draw packet */
594 si_pm4_cmd_begin(pm4, PKT3_INDEX_TYPE);
595 if (ib->index_size == 4) {
596 si_pm4_cmd_add(pm4, V_028A7C_VGT_INDEX_32 | (R600_BIG_ENDIAN ?
597 V_028A7C_VGT_DMA_SWAP_32_BIT : 0));
598 } else {
599 si_pm4_cmd_add(pm4, V_028A7C_VGT_INDEX_16 | (R600_BIG_ENDIAN ?
600 V_028A7C_VGT_DMA_SWAP_16_BIT : 0));
601 }
602 si_pm4_cmd_end(pm4, rctx->predicate_drawing);
603
604 si_pm4_cmd_begin(pm4, PKT3_NUM_INSTANCES);
605 si_pm4_cmd_add(pm4, info->instance_count);
606 si_pm4_cmd_end(pm4, rctx->predicate_drawing);
607
608 if (info->indexed) {
609 uint32_t max_size = (ib->buffer->width0 - ib->offset) /
610 rctx->index_buffer.index_size;
611 uint64_t va;
612 va = r600_resource_va(&rctx->screen->screen, ib->buffer);
613 va += ib->offset;
614
615 si_pm4_add_bo(pm4, (struct si_resource *)ib->buffer, RADEON_USAGE_READ);
616 si_cmd_draw_index_2(pm4, max_size, va, info->count,
617 V_0287F0_DI_SRC_SEL_DMA,
618 rctx->predicate_drawing);
619 } else {
620 uint32_t initiator = V_0287F0_DI_SRC_SEL_AUTO_INDEX;
621 initiator |= S_0287F0_USE_OPAQUE(!!info->count_from_stream_output);
622 si_cmd_draw_index_auto(pm4, info->count, initiator, rctx->predicate_drawing);
623 }
624 si_pm4_set_state(rctx, draw, pm4);
625 }
626
627 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
628 {
629 struct r600_context *rctx = (struct r600_context *)ctx;
630 struct pipe_index_buffer ib = {};
631 uint32_t cp_coher_cntl;
632
633 if (!info->count && (info->indexed || !info->count_from_stream_output))
634 return;
635
636 if (!rctx->ps_shader || !rctx->vs_shader)
637 return;
638
639 si_update_derived_state(rctx);
640 si_constant_buffer_update(rctx);
641 si_vertex_buffer_update(rctx);
642
643 if (info->indexed) {
644 /* Initialize the index buffer struct. */
645 pipe_resource_reference(&ib.buffer, rctx->index_buffer.buffer);
646 ib.user_buffer = rctx->index_buffer.user_buffer;
647 ib.index_size = rctx->index_buffer.index_size;
648 ib.offset = rctx->index_buffer.offset + info->start * ib.index_size;
649
650 /* Translate or upload, if needed. */
651 r600_translate_index_buffer(rctx, &ib, info->count);
652
653 if (ib.user_buffer && !ib.buffer) {
654 r600_upload_index_buffer(rctx, &ib, info->count);
655 }
656
657 } else if (info->count_from_stream_output) {
658 r600_context_draw_opaque_count(rctx, (struct r600_so_target*)info->count_from_stream_output);
659 }
660
661 rctx->vs_shader_so_strides = rctx->vs_shader->current->so_strides;
662
663 if (!si_update_draw_info_state(rctx, info))
664 return;
665
666 si_state_draw(rctx, info, &ib);
667
668 cp_coher_cntl = si_pm4_sync_flags(rctx);
669 if (cp_coher_cntl) {
670 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
671 si_cmd_surface_sync(pm4, cp_coher_cntl);
672 si_pm4_set_state(rctx, sync, pm4);
673 }
674
675 /* Emit states. */
676 rctx->pm4_dirty_cdwords += si_pm4_dirty_dw(rctx);
677
678 si_need_cs_space(rctx, 0, TRUE);
679
680 si_pm4_emit_dirty(rctx);
681 rctx->pm4_dirty_cdwords = 0;
682
683 #if R600_TRACE_CS
684 if (rctx->screen->trace_bo) {
685 r600_trace_emit(rctx);
686 }
687 #endif
688
689 #if 0
690 /* Enable stream out if needed. */
691 if (rctx->streamout_start) {
692 r600_context_streamout_begin(rctx);
693 rctx->streamout_start = FALSE;
694 }
695 #endif
696
697 rctx->flags |= R600_CONTEXT_DST_CACHES_DIRTY;
698
699 /* Set the depth buffer as dirty. */
700 if (rctx->framebuffer.zsbuf) {
701 struct pipe_surface *surf = rctx->framebuffer.zsbuf;
702 struct r600_resource_texture *rtex = (struct r600_resource_texture *)surf->texture;
703
704 rtex->dirty_db_mask |= 1 << surf->u.tex.level;
705 }
706
707 pipe_resource_reference(&ib.buffer, NULL);
708 }