u_vbuf: implement another upload codepath which unrolls indices
[mesa.git] / src / gallium / drivers / r600 / r600_state_common.c
1 /*
2 * Copyright 2010 Red Hat Inc.
3 * 2010 Jerome Glisse
4 *
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie <airlied@redhat.com>
25 * Jerome Glisse <jglisse@redhat.com>
26 */
27 #include "util/u_blitter.h"
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
30 #include "pipebuffer/pb_buffer.h"
31 #include "pipe/p_shader_tokens.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "r600_formats.h"
34 #include "r600_pipe.h"
35 #include "r600d.h"
36
37 static bool r600_conv_pipe_prim(unsigned pprim, unsigned *prim)
38 {
39 static const int prim_conv[] = {
40 V_008958_DI_PT_POINTLIST,
41 V_008958_DI_PT_LINELIST,
42 V_008958_DI_PT_LINELOOP,
43 V_008958_DI_PT_LINESTRIP,
44 V_008958_DI_PT_TRILIST,
45 V_008958_DI_PT_TRISTRIP,
46 V_008958_DI_PT_TRIFAN,
47 V_008958_DI_PT_QUADLIST,
48 V_008958_DI_PT_QUADSTRIP,
49 V_008958_DI_PT_POLYGON,
50 -1,
51 -1,
52 -1,
53 -1
54 };
55
56 *prim = prim_conv[pprim];
57 if (*prim == -1) {
58 fprintf(stderr, "%s:%d unsupported %d\n", __func__, __LINE__, pprim);
59 return false;
60 }
61 return true;
62 }
63
64 /* common state between evergreen and r600 */
65 void r600_bind_blend_state(struct pipe_context *ctx, void *state)
66 {
67 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
68 struct r600_pipe_blend *blend = (struct r600_pipe_blend *)state;
69 struct r600_pipe_state *rstate;
70
71 if (state == NULL)
72 return;
73 rstate = &blend->rstate;
74 rctx->states[rstate->id] = rstate;
75 rctx->cb_target_mask = blend->cb_target_mask;
76 r600_context_pipe_state_set(&rctx->ctx, rstate);
77 }
78
79 void r600_bind_dsa_state(struct pipe_context *ctx, void *state)
80 {
81 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
82 struct r600_pipe_dsa *dsa = state;
83 struct r600_pipe_state *rstate;
84
85 if (state == NULL)
86 return;
87 rstate = &dsa->rstate;
88 rctx->states[rstate->id] = rstate;
89 rctx->alpha_ref = dsa->alpha_ref;
90 rctx->alpha_ref_dirty = true;
91 r600_context_pipe_state_set(&rctx->ctx, rstate);
92 }
93
94 void r600_bind_rs_state(struct pipe_context *ctx, void *state)
95 {
96 struct r600_pipe_rasterizer *rs = (struct r600_pipe_rasterizer *)state;
97 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
98
99 if (state == NULL)
100 return;
101
102 rctx->clamp_vertex_color = rs->clamp_vertex_color;
103 rctx->clamp_fragment_color = rs->clamp_fragment_color;
104
105 rctx->sprite_coord_enable = rs->sprite_coord_enable;
106
107 rctx->rasterizer = rs;
108
109 rctx->states[rs->rstate.id] = &rs->rstate;
110 r600_context_pipe_state_set(&rctx->ctx, &rs->rstate);
111
112 if (rctx->chip_class >= EVERGREEN) {
113 evergreen_polygon_offset_update(rctx);
114 } else {
115 r600_polygon_offset_update(rctx);
116 }
117 }
118
119 void r600_delete_rs_state(struct pipe_context *ctx, void *state)
120 {
121 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
122 struct r600_pipe_rasterizer *rs = (struct r600_pipe_rasterizer *)state;
123
124 if (rctx->rasterizer == rs) {
125 rctx->rasterizer = NULL;
126 }
127 if (rctx->states[rs->rstate.id] == &rs->rstate) {
128 rctx->states[rs->rstate.id] = NULL;
129 }
130 free(rs);
131 }
132
133 void r600_sampler_view_destroy(struct pipe_context *ctx,
134 struct pipe_sampler_view *state)
135 {
136 struct r600_pipe_sampler_view *resource = (struct r600_pipe_sampler_view *)state;
137
138 pipe_resource_reference(&state->texture, NULL);
139 FREE(resource);
140 }
141
142 void r600_delete_state(struct pipe_context *ctx, void *state)
143 {
144 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
145 struct r600_pipe_state *rstate = (struct r600_pipe_state *)state;
146
147 if (rctx->states[rstate->id] == rstate) {
148 rctx->states[rstate->id] = NULL;
149 }
150 for (int i = 0; i < rstate->nregs; i++) {
151 pipe_resource_reference((struct pipe_resource**)&rstate->regs[i].bo, NULL);
152 }
153 free(rstate);
154 }
155
156 void r600_bind_vertex_elements(struct pipe_context *ctx, void *state)
157 {
158 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
159 struct r600_vertex_element *v = (struct r600_vertex_element*)state;
160
161 rctx->vertex_elements = v;
162 if (v) {
163 u_vbuf_bind_vertex_elements(rctx->vbuf_mgr, state,
164 v->vmgr_elements);
165
166 rctx->states[v->rstate.id] = &v->rstate;
167 r600_context_pipe_state_set(&rctx->ctx, &v->rstate);
168 }
169 }
170
171 void r600_delete_vertex_element(struct pipe_context *ctx, void *state)
172 {
173 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
174 struct r600_vertex_element *v = (struct r600_vertex_element*)state;
175
176 if (rctx->states[v->rstate.id] == &v->rstate) {
177 rctx->states[v->rstate.id] = NULL;
178 }
179 if (rctx->vertex_elements == state)
180 rctx->vertex_elements = NULL;
181
182 pipe_resource_reference((struct pipe_resource**)&v->fetch_shader, NULL);
183 u_vbuf_destroy_vertex_elements(rctx->vbuf_mgr, v->vmgr_elements);
184 FREE(state);
185 }
186
187
188 void r600_set_index_buffer(struct pipe_context *ctx,
189 const struct pipe_index_buffer *ib)
190 {
191 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
192
193 u_vbuf_set_index_buffer(rctx->vbuf_mgr, ib);
194 }
195
196 void r600_set_vertex_buffers(struct pipe_context *ctx, unsigned count,
197 const struct pipe_vertex_buffer *buffers)
198 {
199 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
200 int i;
201
202 /* Zero states. */
203 for (i = 0; i < count; i++) {
204 if (!buffers[i].buffer) {
205 if (rctx->chip_class >= EVERGREEN) {
206 evergreen_context_pipe_state_set_fs_resource(&rctx->ctx, NULL, i);
207 } else {
208 r600_context_pipe_state_set_fs_resource(&rctx->ctx, NULL, i);
209 }
210 }
211 }
212 for (; i < rctx->vbuf_mgr->nr_real_vertex_buffers; i++) {
213 if (rctx->chip_class >= EVERGREEN) {
214 evergreen_context_pipe_state_set_fs_resource(&rctx->ctx, NULL, i);
215 } else {
216 r600_context_pipe_state_set_fs_resource(&rctx->ctx, NULL, i);
217 }
218 }
219
220 u_vbuf_set_vertex_buffers(rctx->vbuf_mgr, count, buffers);
221 }
222
223 void *r600_create_vertex_elements(struct pipe_context *ctx,
224 unsigned count,
225 const struct pipe_vertex_element *elements)
226 {
227 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
228 struct r600_vertex_element *v = CALLOC_STRUCT(r600_vertex_element);
229
230 assert(count < 32);
231 if (!v)
232 return NULL;
233
234 v->count = count;
235 v->vmgr_elements =
236 u_vbuf_create_vertex_elements(rctx->vbuf_mgr, count,
237 elements, v->elements);
238
239 if (r600_vertex_elements_build_fetch_shader(rctx, v)) {
240 FREE(v);
241 return NULL;
242 }
243
244 return v;
245 }
246
247 void *r600_create_shader_state(struct pipe_context *ctx,
248 const struct pipe_shader_state *state)
249 {
250 struct r600_pipe_shader *shader = CALLOC_STRUCT(r600_pipe_shader);
251 int r;
252
253 shader->tokens = tgsi_dup_tokens(state->tokens);
254 shader->so = state->stream_output;
255
256 r = r600_pipe_shader_create(ctx, shader);
257 if (r) {
258 return NULL;
259 }
260 return shader;
261 }
262
263 void r600_bind_ps_shader(struct pipe_context *ctx, void *state)
264 {
265 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
266
267 /* TODO delete old shader */
268 rctx->ps_shader = (struct r600_pipe_shader *)state;
269 if (state) {
270 r600_context_pipe_state_set(&rctx->ctx, &rctx->ps_shader->rstate);
271 }
272 if (rctx->ps_shader && rctx->vs_shader) {
273 r600_adjust_gprs(rctx);
274 }
275 }
276
277 void r600_bind_vs_shader(struct pipe_context *ctx, void *state)
278 {
279 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
280
281 /* TODO delete old shader */
282 rctx->vs_shader = (struct r600_pipe_shader *)state;
283 if (state) {
284 r600_context_pipe_state_set(&rctx->ctx, &rctx->vs_shader->rstate);
285 }
286 if (rctx->ps_shader && rctx->vs_shader) {
287 r600_adjust_gprs(rctx);
288 }
289 }
290
291 void r600_delete_ps_shader(struct pipe_context *ctx, void *state)
292 {
293 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
294 struct r600_pipe_shader *shader = (struct r600_pipe_shader *)state;
295
296 if (rctx->ps_shader == shader) {
297 rctx->ps_shader = NULL;
298 }
299
300 free(shader->tokens);
301 r600_pipe_shader_destroy(ctx, shader);
302 free(shader);
303 }
304
305 void r600_delete_vs_shader(struct pipe_context *ctx, void *state)
306 {
307 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
308 struct r600_pipe_shader *shader = (struct r600_pipe_shader *)state;
309
310 if (rctx->vs_shader == shader) {
311 rctx->vs_shader = NULL;
312 }
313
314 free(shader->tokens);
315 r600_pipe_shader_destroy(ctx, shader);
316 free(shader);
317 }
318
319 static void r600_update_alpha_ref(struct r600_pipe_context *rctx)
320 {
321 unsigned alpha_ref;
322 struct r600_pipe_state rstate;
323
324 alpha_ref = rctx->alpha_ref;
325 rstate.nregs = 0;
326 if (rctx->export_16bpc)
327 alpha_ref &= ~0x1FFF;
328 r600_pipe_state_add_reg(&rstate, R_028438_SX_ALPHA_REF, alpha_ref, 0xFFFFFFFF, NULL, 0);
329
330 r600_context_pipe_state_set(&rctx->ctx, &rstate);
331 rctx->alpha_ref_dirty = false;
332 }
333
334 void r600_set_constant_buffer(struct pipe_context *ctx, uint shader, uint index,
335 struct pipe_resource *buffer)
336 {
337 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
338 struct r600_resource *rbuffer = r600_resource(buffer);
339 struct r600_pipe_resource_state *rstate;
340 uint32_t offset;
341
342 /* Note that the state tracker can unbind constant buffers by
343 * passing NULL here.
344 */
345 if (buffer == NULL) {
346 return;
347 }
348
349 r600_upload_const_buffer(rctx, &rbuffer, &offset);
350
351 switch (shader) {
352 case PIPE_SHADER_VERTEX:
353 rctx->vs_const_buffer.nregs = 0;
354 r600_pipe_state_add_reg(&rctx->vs_const_buffer,
355 R_028180_ALU_CONST_BUFFER_SIZE_VS_0,
356 ALIGN_DIVUP(buffer->width0 >> 4, 16),
357 0xFFFFFFFF, NULL, 0);
358 r600_pipe_state_add_reg(&rctx->vs_const_buffer,
359 R_028980_ALU_CONST_CACHE_VS_0,
360 offset >> 8, 0xFFFFFFFF, rbuffer, RADEON_USAGE_READ);
361 r600_context_pipe_state_set(&rctx->ctx, &rctx->vs_const_buffer);
362
363 rstate = &rctx->vs_const_buffer_resource[index];
364 if (!rstate->id) {
365 if (rctx->chip_class >= EVERGREEN) {
366 evergreen_pipe_init_buffer_resource(rctx, rstate);
367 } else {
368 r600_pipe_init_buffer_resource(rctx, rstate);
369 }
370 }
371
372 if (rctx->chip_class >= EVERGREEN) {
373 evergreen_pipe_mod_buffer_resource(rstate, rbuffer, offset, 16, RADEON_USAGE_READ);
374 evergreen_context_pipe_state_set_vs_resource(&rctx->ctx, rstate, index);
375 } else {
376 r600_pipe_mod_buffer_resource(rstate, rbuffer, offset, 16, RADEON_USAGE_READ);
377 r600_context_pipe_state_set_vs_resource(&rctx->ctx, rstate, index);
378 }
379 break;
380 case PIPE_SHADER_FRAGMENT:
381 rctx->ps_const_buffer.nregs = 0;
382 r600_pipe_state_add_reg(&rctx->ps_const_buffer,
383 R_028140_ALU_CONST_BUFFER_SIZE_PS_0,
384 ALIGN_DIVUP(buffer->width0 >> 4, 16),
385 0xFFFFFFFF, NULL, 0);
386 r600_pipe_state_add_reg(&rctx->ps_const_buffer,
387 R_028940_ALU_CONST_CACHE_PS_0,
388 offset >> 8, 0xFFFFFFFF, rbuffer, RADEON_USAGE_READ);
389 r600_context_pipe_state_set(&rctx->ctx, &rctx->ps_const_buffer);
390
391 rstate = &rctx->ps_const_buffer_resource[index];
392 if (!rstate->id) {
393 if (rctx->chip_class >= EVERGREEN) {
394 evergreen_pipe_init_buffer_resource(rctx, rstate);
395 } else {
396 r600_pipe_init_buffer_resource(rctx, rstate);
397 }
398 }
399 if (rctx->chip_class >= EVERGREEN) {
400 evergreen_pipe_mod_buffer_resource(rstate, rbuffer, offset, 16, RADEON_USAGE_READ);
401 evergreen_context_pipe_state_set_ps_resource(&rctx->ctx, rstate, index);
402 } else {
403 r600_pipe_mod_buffer_resource(rstate, rbuffer, offset, 16, RADEON_USAGE_READ);
404 r600_context_pipe_state_set_ps_resource(&rctx->ctx, rstate, index);
405 }
406 break;
407 default:
408 R600_ERR("unsupported %d\n", shader);
409 return;
410 }
411
412 if (buffer != &rbuffer->b.b.b)
413 pipe_resource_reference((struct pipe_resource**)&rbuffer, NULL);
414 }
415
416 struct pipe_stream_output_target *
417 r600_create_so_target(struct pipe_context *ctx,
418 struct pipe_resource *buffer,
419 unsigned buffer_offset,
420 unsigned buffer_size)
421 {
422 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
423 struct r600_so_target *t;
424 void *ptr;
425
426 t = CALLOC_STRUCT(r600_so_target);
427 if (!t) {
428 return NULL;
429 }
430
431 t->b.reference.count = 1;
432 t->b.context = ctx;
433 pipe_resource_reference(&t->b.buffer, buffer);
434 t->b.buffer_offset = buffer_offset;
435 t->b.buffer_size = buffer_size;
436
437 t->filled_size = (struct r600_resource*)
438 pipe_buffer_create(ctx->screen, PIPE_BIND_CUSTOM, PIPE_USAGE_STATIC, 4);
439 ptr = rctx->ws->buffer_map(t->filled_size->buf, rctx->ctx.cs, PIPE_TRANSFER_WRITE);
440 memset(ptr, 0, t->filled_size->buf->size);
441 rctx->ws->buffer_unmap(t->filled_size->buf);
442
443 return &t->b;
444 }
445
446 void r600_so_target_destroy(struct pipe_context *ctx,
447 struct pipe_stream_output_target *target)
448 {
449 struct r600_so_target *t = (struct r600_so_target*)target;
450 pipe_resource_reference(&t->b.buffer, NULL);
451 pipe_resource_reference((struct pipe_resource**)&t->filled_size, NULL);
452 FREE(t);
453 }
454
455 void r600_set_so_targets(struct pipe_context *ctx,
456 unsigned num_targets,
457 struct pipe_stream_output_target **targets,
458 unsigned append_bitmask)
459 {
460 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
461 unsigned i;
462
463 /* Stop streamout. */
464 if (rctx->ctx.num_so_targets) {
465 r600_context_streamout_end(&rctx->ctx);
466 }
467
468 /* Set the new targets. */
469 for (i = 0; i < num_targets; i++) {
470 pipe_so_target_reference((struct pipe_stream_output_target**)&rctx->ctx.so_targets[i], targets[i]);
471 }
472 for (; i < rctx->ctx.num_so_targets; i++) {
473 pipe_so_target_reference((struct pipe_stream_output_target**)&rctx->ctx.so_targets[i], NULL);
474 }
475
476 rctx->ctx.num_so_targets = num_targets;
477 rctx->ctx.streamout_start = num_targets != 0;
478 rctx->ctx.streamout_append_bitmask = append_bitmask;
479 }
480
481 static void r600_vertex_buffer_update(struct r600_pipe_context *rctx)
482 {
483 struct r600_pipe_resource_state *rstate;
484 struct r600_resource *rbuffer;
485 struct pipe_vertex_buffer *vertex_buffer;
486 unsigned i, count, offset;
487
488 if (rctx->vertex_elements->vbuffer_need_offset) {
489 /* one resource per vertex elements */
490 count = rctx->vertex_elements->count;
491 } else {
492 /* bind vertex buffer once */
493 count = rctx->vbuf_mgr->nr_real_vertex_buffers;
494 }
495
496 for (i = 0 ; i < count; i++) {
497 rstate = &rctx->fs_resource[i];
498
499 if (rctx->vertex_elements->vbuffer_need_offset) {
500 /* one resource per vertex elements */
501 unsigned vbuffer_index;
502 vbuffer_index = rctx->vertex_elements->elements[i].vertex_buffer_index;
503 vertex_buffer = &rctx->vbuf_mgr->real_vertex_buffer[vbuffer_index];
504 rbuffer = (struct r600_resource*)vertex_buffer->buffer;
505 offset = rctx->vertex_elements->vbuffer_offset[i];
506 } else {
507 /* bind vertex buffer once */
508 vertex_buffer = &rctx->vbuf_mgr->real_vertex_buffer[i];
509 rbuffer = (struct r600_resource*)vertex_buffer->buffer;
510 offset = 0;
511 }
512 if (vertex_buffer == NULL || rbuffer == NULL)
513 continue;
514 offset += vertex_buffer->buffer_offset;
515
516 if (!rstate->id) {
517 if (rctx->chip_class >= EVERGREEN) {
518 evergreen_pipe_init_buffer_resource(rctx, rstate);
519 } else {
520 r600_pipe_init_buffer_resource(rctx, rstate);
521 }
522 }
523
524 if (rctx->chip_class >= EVERGREEN) {
525 evergreen_pipe_mod_buffer_resource(rstate, rbuffer, offset, vertex_buffer->stride, RADEON_USAGE_READ);
526 evergreen_context_pipe_state_set_fs_resource(&rctx->ctx, rstate, i);
527 } else {
528 r600_pipe_mod_buffer_resource(rstate, rbuffer, offset, vertex_buffer->stride, RADEON_USAGE_READ);
529 r600_context_pipe_state_set_fs_resource(&rctx->ctx, rstate, i);
530 }
531 }
532 }
533
534 static int r600_shader_rebuild(struct pipe_context * ctx, struct r600_pipe_shader * shader)
535 {
536 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
537 int r;
538
539 r600_pipe_shader_destroy(ctx, shader);
540 r = r600_pipe_shader_create(ctx, shader);
541 if (r) {
542 return r;
543 }
544 r600_context_pipe_state_set(&rctx->ctx, &shader->rstate);
545
546 return 0;
547 }
548
549 static void r600_update_derived_state(struct r600_pipe_context *rctx)
550 {
551 struct pipe_context * ctx = (struct pipe_context*)rctx;
552
553 if (!rctx->blitter->running) {
554 if (rctx->have_depth_fb || rctx->have_depth_texture)
555 r600_flush_depth_textures(rctx);
556 }
557
558 if (rctx->chip_class < EVERGREEN) {
559 r600_update_sampler_states(rctx);
560 }
561
562 if (rctx->vs_shader->shader.clamp_color != rctx->clamp_vertex_color) {
563 r600_shader_rebuild(&rctx->context, rctx->vs_shader);
564 }
565
566 if ((rctx->ps_shader->shader.clamp_color != rctx->clamp_fragment_color) ||
567 ((rctx->chip_class >= EVERGREEN) && rctx->ps_shader->shader.fs_write_all &&
568 (rctx->ps_shader->shader.nr_cbufs != rctx->nr_cbufs))) {
569 r600_shader_rebuild(&rctx->context, rctx->ps_shader);
570 }
571
572 if (rctx->alpha_ref_dirty) {
573 r600_update_alpha_ref(rctx);
574 }
575
576 if (rctx->ps_shader && rctx->sprite_coord_enable &&
577 (rctx->ps_shader->sprite_coord_enable != rctx->sprite_coord_enable)) {
578
579 if (rctx->chip_class >= EVERGREEN)
580 evergreen_pipe_shader_ps(ctx, rctx->ps_shader);
581 else
582 r600_pipe_shader_ps(ctx, rctx->ps_shader);
583
584 r600_context_pipe_state_set(&rctx->ctx, &rctx->ps_shader->rstate);
585 }
586
587 }
588
589 void r600_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *dinfo)
590 {
591 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
592 struct pipe_draw_info info = *dinfo;
593 struct r600_draw rdraw = {};
594 struct pipe_index_buffer ib = {};
595 unsigned prim, mask, ls_mask = 0;
596
597 if ((!info.count && (info.indexed || !info.count_from_stream_output)) ||
598 (info.indexed && !rctx->vbuf_mgr->index_buffer.buffer) ||
599 !r600_conv_pipe_prim(info.mode, &prim)) {
600 return;
601 }
602
603 if (!rctx->ps_shader || !rctx->vs_shader)
604 return;
605
606 r600_update_derived_state(rctx);
607
608 u_vbuf_draw_begin(rctx->vbuf_mgr, &info);
609 r600_vertex_buffer_update(rctx);
610
611 rdraw.vgt_num_indices = info.count;
612 rdraw.vgt_num_instances = info.instance_count;
613
614 if (info.indexed) {
615 /* Initialize the index buffer struct. */
616 pipe_resource_reference(&ib.buffer, rctx->vbuf_mgr->index_buffer.buffer);
617 ib.index_size = rctx->vbuf_mgr->index_buffer.index_size;
618 ib.offset = rctx->vbuf_mgr->index_buffer.offset + info.start * ib.index_size;
619
620 /* Translate or upload, if needed. */
621 r600_translate_index_buffer(rctx, &ib, info.count);
622
623 if (u_vbuf_resource(ib.buffer)->user_ptr) {
624 r600_upload_index_buffer(rctx, &ib, info.count);
625 }
626
627 /* Initialize the r600_draw struct with index buffer info. */
628 if (ib.index_size == 4) {
629 rdraw.vgt_index_type = VGT_INDEX_32 |
630 (R600_BIG_ENDIAN ? VGT_DMA_SWAP_32_BIT : 0);
631 } else {
632 rdraw.vgt_index_type = VGT_INDEX_16 |
633 (R600_BIG_ENDIAN ? VGT_DMA_SWAP_16_BIT : 0);
634 }
635 rdraw.indices = (struct r600_resource*)ib.buffer;
636 rdraw.indices_bo_offset = ib.offset;
637 rdraw.vgt_draw_initiator = V_0287F0_DI_SRC_SEL_DMA;
638 } else {
639 info.index_bias = info.start;
640 rdraw.vgt_draw_initiator = V_0287F0_DI_SRC_SEL_AUTO_INDEX;
641 if (info.count_from_stream_output) {
642 rdraw.vgt_draw_initiator |= S_0287F0_USE_OPAQUE(1);
643
644 r600_context_draw_opaque_count(&rctx->ctx, (struct r600_so_target*)info.count_from_stream_output);
645 }
646 }
647
648 rctx->ctx.vs_shader_so_strides = rctx->vs_shader->so_strides;
649
650 mask = (1ULL << ((unsigned)rctx->framebuffer.nr_cbufs * 4)) - 1;
651
652 if (rctx->vgt.id != R600_PIPE_STATE_VGT) {
653 rctx->vgt.id = R600_PIPE_STATE_VGT;
654 rctx->vgt.nregs = 0;
655 r600_pipe_state_add_reg(&rctx->vgt, R_008958_VGT_PRIMITIVE_TYPE, prim, 0xFFFFFFFF, NULL, 0);
656 r600_pipe_state_add_reg(&rctx->vgt, R_028238_CB_TARGET_MASK, rctx->cb_target_mask & mask, 0xFFFFFFFF, NULL, 0);
657 r600_pipe_state_add_reg(&rctx->vgt, R_028400_VGT_MAX_VTX_INDX, ~0, 0xFFFFFFFF, NULL, 0);
658 r600_pipe_state_add_reg(&rctx->vgt, R_028404_VGT_MIN_VTX_INDX, 0, 0xFFFFFFFF, NULL, 0);
659 r600_pipe_state_add_reg(&rctx->vgt, R_028408_VGT_INDX_OFFSET, info.index_bias, 0xFFFFFFFF, NULL, 0);
660 r600_pipe_state_add_reg(&rctx->vgt, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX, info.restart_index, 0xFFFFFFFF, NULL, 0);
661 r600_pipe_state_add_reg(&rctx->vgt, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, info.primitive_restart, 0xFFFFFFFF, NULL, 0);
662 r600_pipe_state_add_reg(&rctx->vgt, R_03CFF0_SQ_VTX_BASE_VTX_LOC, 0, 0xFFFFFFFF, NULL, 0);
663 r600_pipe_state_add_reg(&rctx->vgt, R_03CFF4_SQ_VTX_START_INST_LOC, info.start_instance, 0xFFFFFFFF, NULL, 0);
664 r600_pipe_state_add_reg(&rctx->vgt, R_028A0C_PA_SC_LINE_STIPPLE,
665 0,
666 S_028A0C_AUTO_RESET_CNTL(3), NULL, 0);
667 r600_pipe_state_add_reg(&rctx->vgt, R_028814_PA_SU_SC_MODE_CNTL,
668 0,
669 S_028814_PROVOKING_VTX_LAST(1), NULL, 0);
670 }
671
672 rctx->vgt.nregs = 0;
673 r600_pipe_state_mod_reg(&rctx->vgt, prim);
674 r600_pipe_state_mod_reg(&rctx->vgt, rctx->cb_target_mask & mask);
675 r600_pipe_state_mod_reg(&rctx->vgt, ~0);
676 r600_pipe_state_mod_reg(&rctx->vgt, 0);
677 r600_pipe_state_mod_reg(&rctx->vgt, info.index_bias);
678 r600_pipe_state_mod_reg(&rctx->vgt, info.restart_index);
679 r600_pipe_state_mod_reg(&rctx->vgt, info.primitive_restart);
680 r600_pipe_state_mod_reg(&rctx->vgt, 0);
681 r600_pipe_state_mod_reg(&rctx->vgt, info.start_instance);
682
683 if (prim == V_008958_DI_PT_LINELIST)
684 ls_mask = 1;
685 else if (prim == V_008958_DI_PT_LINESTRIP)
686 ls_mask = 2;
687 r600_pipe_state_mod_reg(&rctx->vgt, S_028A0C_AUTO_RESET_CNTL(ls_mask));
688
689 if (info.mode == PIPE_PRIM_QUADS || info.mode == PIPE_PRIM_QUAD_STRIP || info.mode == PIPE_PRIM_POLYGON) {
690 r600_pipe_state_mod_reg(&rctx->vgt, S_028814_PROVOKING_VTX_LAST(1));
691 }
692
693 r600_context_pipe_state_set(&rctx->ctx, &rctx->vgt);
694
695 if (rctx->chip_class >= EVERGREEN) {
696 evergreen_context_draw(&rctx->ctx, &rdraw);
697 } else {
698 r600_context_draw(&rctx->ctx, &rdraw);
699 }
700
701 if (rctx->framebuffer.zsbuf)
702 {
703 struct pipe_resource *tex = rctx->framebuffer.zsbuf->texture;
704 ((struct r600_resource_texture *)tex)->dirty_db = TRUE;
705 }
706
707 pipe_resource_reference(&ib.buffer, NULL);
708 u_vbuf_draw_end(rctx->vbuf_mgr);
709 }
710
711 void _r600_pipe_state_add_reg(struct r600_context *ctx,
712 struct r600_pipe_state *state,
713 u32 offset, u32 value, u32 mask,
714 u32 range_id, u32 block_id,
715 struct r600_resource *bo,
716 enum radeon_bo_usage usage)
717 {
718 struct r600_range *range;
719 struct r600_block *block;
720
721 if (bo) assert(usage);
722
723 range = &ctx->range[range_id];
724 block = range->blocks[block_id];
725 state->regs[state->nregs].block = block;
726 state->regs[state->nregs].id = (offset - block->start_offset) >> 2;
727
728 state->regs[state->nregs].value = value;
729 state->regs[state->nregs].mask = mask;
730 state->regs[state->nregs].bo = bo;
731 state->regs[state->nregs].bo_usage = usage;
732
733 state->nregs++;
734 assert(state->nregs < R600_BLOCK_MAX_REG);
735 }
736
737 void r600_pipe_state_add_reg_noblock(struct r600_pipe_state *state,
738 u32 offset, u32 value, u32 mask,
739 struct r600_resource *bo,
740 enum radeon_bo_usage usage)
741 {
742 if (bo) assert(usage);
743
744 state->regs[state->nregs].id = offset;
745 state->regs[state->nregs].block = NULL;
746 state->regs[state->nregs].value = value;
747 state->regs[state->nregs].mask = mask;
748 state->regs[state->nregs].bo = bo;
749 state->regs[state->nregs].bo_usage = usage;
750
751 state->nregs++;
752 assert(state->nregs < R600_BLOCK_MAX_REG);
753 }