r300g: fix potentially uninitialized variables in create_rs_state
[mesa.git] / src / gallium / drivers / r300 / r300_render.c
1 /*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
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 /* r300_render: Vertex and index buffer primitive emission. Contains both
25 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
26
27 #include "draw/draw_context.h"
28 #include "draw/draw_vbuf.h"
29
30 #include "util/u_inlines.h"
31
32 #include "util/u_format.h"
33 #include "util/u_memory.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/u_prim.h"
36
37 #include "r300_cs.h"
38 #include "r300_context.h"
39 #include "r300_screen_buffer.h"
40 #include "r300_emit.h"
41 #include "r300_reg.h"
42 #include "r300_state_derived.h"
43
44 #include <limits.h>
45
46 #define IMMD_DWORDS 32
47
48 static uint32_t r300_translate_primitive(unsigned prim)
49 {
50 switch (prim) {
51 case PIPE_PRIM_POINTS:
52 return R300_VAP_VF_CNTL__PRIM_POINTS;
53 case PIPE_PRIM_LINES:
54 return R300_VAP_VF_CNTL__PRIM_LINES;
55 case PIPE_PRIM_LINE_LOOP:
56 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
57 case PIPE_PRIM_LINE_STRIP:
58 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
59 case PIPE_PRIM_TRIANGLES:
60 return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
61 case PIPE_PRIM_TRIANGLE_STRIP:
62 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
63 case PIPE_PRIM_TRIANGLE_FAN:
64 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
65 case PIPE_PRIM_QUADS:
66 return R300_VAP_VF_CNTL__PRIM_QUADS;
67 case PIPE_PRIM_QUAD_STRIP:
68 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
69 case PIPE_PRIM_POLYGON:
70 return R300_VAP_VF_CNTL__PRIM_POLYGON;
71 default:
72 return 0;
73 }
74 }
75
76 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
77 unsigned mode)
78 {
79 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
80 uint32_t color_control = rs->color_control;
81
82 /* By default (see r300_state.c:r300_create_rs_state) color_control is
83 * initialized to provoking the first vertex.
84 *
85 * Triangle fans must be reduced to the second vertex, not the first, in
86 * Gallium flatshade-first mode, as per the GL spec.
87 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
88 *
89 * Quads never provoke correctly in flatshade-first mode. The first
90 * vertex is never considered as provoking, so only the second, third,
91 * and fourth vertices can be selected, and both "third" and "last" modes
92 * select the fourth vertex. This is probably due to D3D lacking quads.
93 *
94 * Similarly, polygons reduce to the first, not the last, vertex, when in
95 * "last" mode, and all other modes start from the second vertex.
96 *
97 * ~ C.
98 */
99
100 if (rs->rs.flatshade_first) {
101 switch (mode) {
102 case PIPE_PRIM_TRIANGLE_FAN:
103 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
104 break;
105 case PIPE_PRIM_QUADS:
106 case PIPE_PRIM_QUAD_STRIP:
107 case PIPE_PRIM_POLYGON:
108 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
109 break;
110 default:
111 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
112 break;
113 }
114 } else {
115 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
116 }
117
118 return color_control;
119 }
120
121 boolean r500_index_bias_supported(struct r300_context *r300)
122 {
123 return r300->screen->caps.is_r500 &&
124 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
125 }
126
127 void r500_emit_index_bias(struct r300_context *r300, int index_bias)
128 {
129 CS_LOCALS(r300);
130
131 BEGIN_CS(2);
132 OUT_CS_REG(R500_VAP_INDEX_OFFSET,
133 (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
134 END_CS;
135 }
136
137 /* This function splits the index bias value into two parts:
138 * - buffer_offset: the value that can be safely added to buffer offsets
139 * in r300_emit_aos (it must yield a positive offset when added to
140 * a vertex buffer offset)
141 * - index_offset: the value that must be manually subtracted from indices
142 * in an index buffer to achieve negative offsets. */
143 static void r300_split_index_bias(struct r300_context *r300, int index_bias,
144 int *buffer_offset, int *index_offset)
145 {
146 struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
147 struct pipe_vertex_element *velem = r300->velems->velem;
148 unsigned i, size;
149 int max_neg_bias;
150
151 if (index_bias < 0) {
152 /* See how large index bias we may subtract. We must be careful
153 * here because negative buffer offsets are not allowed
154 * by the DRM API. */
155 max_neg_bias = INT_MAX;
156 for (i = 0; i < r300->velems->count; i++) {
157 vb = &vbufs[velem[i].vertex_buffer_index];
158 size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
159 max_neg_bias = MIN2(max_neg_bias, size);
160 }
161
162 /* Now set the minimum allowed value. */
163 *buffer_offset = MAX2(-max_neg_bias, index_bias);
164 } else {
165 /* A positive index bias is OK. */
166 *buffer_offset = index_bias;
167 }
168
169 *index_offset = index_bias - *buffer_offset;
170 }
171
172 enum r300_prepare_flags {
173 PREP_FIRST_DRAW = (1 << 0), /* call emit_dirty_state and friends? */
174 PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */
175 PREP_EMIT_AOS = (1 << 2), /* call emit_aos? */
176 PREP_EMIT_AOS_SWTCL = (1 << 3), /* call emit_aos_swtcl? */
177 PREP_INDEXED = (1 << 4) /* is this draw_elements? */
178 };
179
180 /**
181 * Check if the requested number of dwords is available in the CS and
182 * if not, flush. Then validate buffers and emit dirty state.
183 * \param r300 The context.
184 * \param flags See r300_prepare_flags.
185 * \param index_buffer The index buffer to validate. The parameter may be NULL.
186 * \param cs_dwords The number of dwords to reserve in CS.
187 * \param aos_offset The offset passed to emit_aos.
188 * \param index_bias The index bias to emit.
189 */
190 static void r300_prepare_for_rendering(struct r300_context *r300,
191 enum r300_prepare_flags flags,
192 struct pipe_resource *index_buffer,
193 unsigned cs_dwords,
194 int aos_offset,
195 int index_bias)
196 {
197 boolean flushed = FALSE;
198 boolean first_draw = flags & PREP_FIRST_DRAW;
199 boolean emit_aos = flags & PREP_EMIT_AOS;
200 boolean emit_aos_swtcl = flags & PREP_EMIT_AOS_SWTCL;
201 boolean indexed = flags & PREP_INDEXED;
202 boolean hw_index_bias = r500_index_bias_supported(r300);
203
204 /* Add dirty state, index offset, and AOS. */
205 if (first_draw) {
206 cs_dwords += r300_get_num_dirty_dwords(r300);
207
208 if (hw_index_bias)
209 cs_dwords += 2; /* emit_index_offset */
210
211 if (emit_aos)
212 cs_dwords += 55; /* emit_aos */
213
214 if (emit_aos_swtcl)
215 cs_dwords += 7; /* emit_aos_swtcl */
216 }
217
218 cs_dwords += r300_get_num_cs_end_dwords(r300);
219
220 /* Reserve requested CS space. */
221 if (cs_dwords > (r300->cs->ndw - r300->cs->cdw)) {
222 r300->context.flush(&r300->context, 0, NULL);
223 flushed = TRUE;
224 }
225
226 /* Validate buffers and emit dirty state if needed. */
227 if (first_draw || flushed) {
228 r300_emit_buffer_validate(r300, flags & PREP_VALIDATE_VBOS, index_buffer);
229 r300_emit_dirty_state(r300);
230 if (hw_index_bias) {
231 if (r300->screen->caps.has_tcl)
232 r500_emit_index_bias(r300, index_bias);
233 else
234 r500_emit_index_bias(r300, 0);
235 }
236
237 if (emit_aos)
238 r300_emit_aos(r300, aos_offset, indexed);
239
240 if (emit_aos_swtcl)
241 r300_emit_aos_swtcl(r300, indexed);
242 }
243 }
244
245 static boolean immd_is_good_idea(struct r300_context *r300,
246 unsigned count)
247 {
248 struct pipe_vertex_element* velem;
249 struct pipe_vertex_buffer* vbuf;
250 boolean checked[PIPE_MAX_ATTRIBS] = {0};
251 unsigned vertex_element_count = r300->velems->count;
252 unsigned i, vbi;
253
254 if (DBG_ON(r300, DBG_NO_IMMD)) {
255 return FALSE;
256 }
257
258 if (r300->draw) {
259 return FALSE;
260 }
261
262 if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
263 return FALSE;
264 }
265
266 /* We shouldn't map buffers referenced by CS, busy buffers,
267 * and ones placed in VRAM. */
268 for (i = 0; i < vertex_element_count; i++) {
269 velem = &r300->velems->velem[i];
270 vbi = velem->vertex_buffer_index;
271
272 if (!checked[vbi]) {
273 vbuf = &r300->vertex_buffer[vbi];
274
275 if (!(r300_buffer(vbuf->buffer)->domain & R300_DOMAIN_GTT)) {
276 return FALSE;
277 }
278
279 if (r300_buffer_is_referenced(&r300->context,
280 vbuf->buffer,
281 R300_REF_CS | R300_REF_HW)) {
282 /* It's a very bad idea to map it... */
283 return FALSE;
284 }
285 checked[vbi] = TRUE;
286 }
287 }
288 return TRUE;
289 }
290
291 /*****************************************************************************
292 * The HWTCL draw functions. *
293 ****************************************************************************/
294
295 static void r300_emit_draw_arrays_immediate(struct r300_context *r300,
296 unsigned mode,
297 unsigned start,
298 unsigned count)
299 {
300 struct pipe_vertex_element* velem;
301 struct pipe_vertex_buffer* vbuf;
302 unsigned vertex_element_count = r300->velems->count;
303 unsigned i, v, vbi, dwords;
304
305 /* Size of the vertex, in dwords. */
306 unsigned vertex_size = r300->velems->vertex_size_dwords;
307
308 /* Size of the vertex element, in dwords. */
309 unsigned size[PIPE_MAX_ATTRIBS];
310
311 /* Stride to the same attrib in the next vertex in the vertex buffer,
312 * in dwords. */
313 unsigned stride[PIPE_MAX_ATTRIBS];
314
315 /* Mapped vertex buffers. */
316 uint32_t* map[PIPE_MAX_ATTRIBS];
317 uint32_t* mapelem[PIPE_MAX_ATTRIBS];
318 struct pipe_transfer* transfer[PIPE_MAX_ATTRIBS] = {0};
319
320 CS_LOCALS(r300);
321
322 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
323 for (i = 0; i < vertex_element_count; i++) {
324 velem = &r300->velems->velem[i];
325 size[i] = r300->velems->hw_format_size[i] / 4;
326 vbi = velem->vertex_buffer_index;
327 vbuf = &r300->vertex_buffer[vbi];
328 stride[i] = vbuf->stride / 4;
329
330 /* Map the buffer. */
331 if (!transfer[vbi]) {
332 map[vbi] = (uint32_t*)pipe_buffer_map(&r300->context,
333 vbuf->buffer,
334 PIPE_TRANSFER_READ,
335 &transfer[vbi]);
336 map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * start;
337 }
338 mapelem[i] = map[vbi] + (velem->src_offset / 4);
339 }
340
341 dwords = 9 + count * vertex_size;
342
343 r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0);
344
345 BEGIN_CS(dwords);
346 OUT_CS_REG(R300_GA_COLOR_CONTROL,
347 r300_provoking_vertex_fixes(r300, mode));
348 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
349 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
350 OUT_CS(count - 1);
351 OUT_CS(0);
352 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
353 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
354 r300_translate_primitive(mode));
355
356 /* Emit vertices. */
357 for (v = 0; v < count; v++) {
358 for (i = 0; i < vertex_element_count; i++) {
359 OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
360 }
361 }
362 END_CS;
363
364 /* Unmap buffers. */
365 for (i = 0; i < vertex_element_count; i++) {
366 vbi = r300->velems->velem[i].vertex_buffer_index;
367
368 if (transfer[vbi]) {
369 vbuf = &r300->vertex_buffer[vbi];
370 pipe_buffer_unmap(&r300->context, vbuf->buffer, transfer[vbi]);
371 transfer[vbi] = NULL;
372 }
373 }
374 }
375
376 static void r300_emit_draw_arrays(struct r300_context *r300,
377 unsigned mode,
378 unsigned count)
379 {
380 boolean alt_num_verts = count > 65535;
381 CS_LOCALS(r300);
382
383 if (count >= (1 << 24)) {
384 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
385 "refusing to render.\n", count);
386 return;
387 }
388
389 BEGIN_CS(7 + (alt_num_verts ? 2 : 0));
390 if (alt_num_verts) {
391 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
392 }
393 OUT_CS_REG(R300_GA_COLOR_CONTROL,
394 r300_provoking_vertex_fixes(r300, mode));
395 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
396 OUT_CS(count - 1);
397 OUT_CS(0);
398 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
399 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
400 r300_translate_primitive(mode) |
401 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
402 END_CS;
403 }
404
405 static void r300_emit_draw_elements(struct r300_context *r300,
406 struct pipe_resource* indexBuffer,
407 unsigned indexSize,
408 unsigned minIndex,
409 unsigned maxIndex,
410 unsigned mode,
411 unsigned start,
412 unsigned count)
413 {
414 uint32_t count_dwords;
415 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
416 boolean alt_num_verts = count > 65535;
417 CS_LOCALS(r300);
418
419 if (count >= (1 << 24)) {
420 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
421 "refusing to render.\n", count);
422 return;
423 }
424
425 maxIndex = MIN2(maxIndex, r300->vertex_buffer_max_index);
426
427 DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, min %u max %u\n",
428 count, minIndex, maxIndex);
429
430 BEGIN_CS(13 + (alt_num_verts ? 2 : 0));
431 if (alt_num_verts) {
432 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
433 }
434 OUT_CS_REG(R300_GA_COLOR_CONTROL,
435 r300_provoking_vertex_fixes(r300, mode));
436 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
437 OUT_CS(maxIndex);
438 OUT_CS(minIndex);
439 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
440 if (indexSize == 4) {
441 count_dwords = count;
442 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
443 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
444 r300_translate_primitive(mode) |
445 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
446 } else {
447 count_dwords = (count + 1) / 2;
448 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
449 r300_translate_primitive(mode) |
450 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
451 }
452
453 /* INDX_BUFFER is a truly special packet3.
454 * Unlike most other packet3, where the offset is after the count,
455 * the order is reversed, so the relocation ends up carrying the
456 * size of the indexbuf instead of the offset.
457 */
458 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
459 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
460 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
461 OUT_CS(offset_dwords << 2);
462 OUT_CS_BUF_RELOC(indexBuffer, count_dwords,
463 r300_buffer(indexBuffer)->domain, 0);
464
465 END_CS;
466 }
467
468 /* This is the fast-path drawing & emission for HW TCL. */
469 static void r300_draw_range_elements(struct pipe_context* pipe,
470 struct pipe_resource* indexBuffer,
471 unsigned indexSize,
472 int indexBias,
473 unsigned minIndex,
474 unsigned maxIndex,
475 unsigned mode,
476 unsigned start,
477 unsigned count)
478 {
479 struct r300_context* r300 = r300_context(pipe);
480 struct pipe_resource* orgIndexBuffer = indexBuffer;
481 boolean alt_num_verts = r300->screen->caps.is_r500 &&
482 count > 65536 &&
483 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
484 unsigned short_count;
485 int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
486 boolean translate = FALSE;
487 unsigned new_offset;
488
489 if (r300->skip_rendering) {
490 return;
491 }
492
493 if (!u_trim_pipe_prim(mode, &count)) {
494 return;
495 }
496
497 /* Index buffer range checking. */
498 if ((start + count) * indexSize > indexBuffer->width0) {
499 fprintf(stderr, "r300: Invalid index buffer range. Skipping rendering.\n");
500 return;
501 }
502
503 /* Set up fallback for incompatible vertex layout if needed. */
504 if (r300->incompatible_vb_layout || r300->velems->incompatible_layout) {
505 r300_begin_vertex_translate(r300);
506 translate = TRUE;
507 }
508
509 if (indexBias && !r500_index_bias_supported(r300)) {
510 r300_split_index_bias(r300, indexBias, &buffer_offset, &index_offset);
511 }
512
513 r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
514 &start, count);
515
516 r300_update_derived_state(r300);
517 r300_upload_index_buffer(r300, &indexBuffer, indexSize, start, count, &new_offset);
518
519 start = new_offset;
520 /* 15 dwords for emit_draw_elements */
521 r300_prepare_for_rendering(r300,
522 PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
523 indexBuffer, 15, buffer_offset, indexBias);
524
525 if (alt_num_verts || count <= 65535) {
526 r300_emit_draw_elements(r300, indexBuffer, indexSize,
527 minIndex, maxIndex, mode, start, count);
528 } else {
529 do {
530 short_count = MIN2(count, 65534);
531 r300_emit_draw_elements(r300, indexBuffer, indexSize,
532 minIndex, maxIndex,
533 mode, start, short_count);
534
535 start += short_count;
536 count -= short_count;
537
538 /* 15 dwords for emit_draw_elements */
539 if (count) {
540 r300_prepare_for_rendering(r300,
541 PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
542 indexBuffer, 15, buffer_offset, indexBias);
543 }
544 } while (count);
545 }
546
547 if (indexBuffer != orgIndexBuffer) {
548 pipe_resource_reference( &indexBuffer, NULL );
549 }
550
551 if (translate) {
552 r300_end_vertex_translate(r300);
553 }
554 }
555
556 static void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
557 unsigned start, unsigned count)
558 {
559 struct r300_context* r300 = r300_context(pipe);
560 boolean alt_num_verts = r300->screen->caps.is_r500 &&
561 count > 65536 &&
562 r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
563 unsigned short_count;
564 boolean translate = FALSE;
565
566 if (r300->skip_rendering) {
567 return;
568 }
569
570 if (!u_trim_pipe_prim(mode, &count)) {
571 return;
572 }
573
574 /* Set up fallback for incompatible vertex layout if needed. */
575 if (r300->incompatible_vb_layout || r300->velems->incompatible_layout) {
576 r300_begin_vertex_translate(r300);
577 translate = TRUE;
578 }
579
580 r300_update_derived_state(r300);
581
582 if (immd_is_good_idea(r300, count)) {
583 r300_emit_draw_arrays_immediate(r300, mode, start, count);
584 } else {
585 /* 9 spare dwords for emit_draw_arrays. */
586 r300_prepare_for_rendering(r300, PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS,
587 NULL, 9, start, 0);
588
589 if (alt_num_verts || count <= 65535) {
590 r300_emit_draw_arrays(r300, mode, count);
591 } else {
592 do {
593 short_count = MIN2(count, 65535);
594 r300_emit_draw_arrays(r300, mode, short_count);
595
596 start += short_count;
597 count -= short_count;
598
599 /* 9 spare dwords for emit_draw_arrays. */
600 if (count) {
601 r300_prepare_for_rendering(r300,
602 PREP_VALIDATE_VBOS | PREP_EMIT_AOS, NULL, 9,
603 start, 0);
604 }
605 } while (count);
606 }
607 u_upload_flush(r300->upload_vb);
608 }
609
610 if (translate) {
611 r300_end_vertex_translate(r300);
612 }
613 }
614
615 static void r300_draw_vbo(struct pipe_context* pipe,
616 const struct pipe_draw_info *info)
617 {
618 struct r300_context* r300 = r300_context(pipe);
619
620 if (info->indexed && r300->index_buffer.buffer) {
621 unsigned offset;
622
623 assert(r300->index_buffer.offset % r300->index_buffer.index_size == 0);
624 offset = r300->index_buffer.offset / r300->index_buffer.index_size;
625
626 r300_draw_range_elements(pipe,
627 r300->index_buffer.buffer,
628 r300->index_buffer.index_size,
629 info->index_bias,
630 info->min_index,
631 info->max_index,
632 info->mode,
633 info->start + offset,
634 info->count);
635 }
636 else {
637 r300_draw_arrays(pipe,
638 info->mode,
639 info->start,
640 info->count);
641 }
642 }
643
644 /****************************************************************************
645 * The rest of this file is for SW TCL rendering only. Please be polite and *
646 * keep these functions separated so that they are easier to locate. ~C. *
647 ***************************************************************************/
648
649 /* SW TCL elements, using Draw. */
650 static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
651 const struct pipe_draw_info *info)
652 {
653 struct r300_context* r300 = r300_context(pipe);
654 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
655 struct pipe_transfer *ib_transfer = NULL;
656 unsigned count = info->count;
657 int i;
658 void* indices = NULL;
659
660 if (r300->skip_rendering) {
661 return;
662 }
663
664 if (!u_trim_pipe_prim(info->mode, &count)) {
665 return;
666 }
667
668 r300_update_derived_state(r300);
669
670 for (i = 0; i < r300->vertex_buffer_count; i++) {
671 if (r300->vertex_buffer[i].buffer) {
672 void *buf = pipe_buffer_map(pipe,
673 r300->vertex_buffer[i].buffer,
674 PIPE_TRANSFER_READ,
675 &vb_transfer[i]);
676 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
677 }
678 }
679
680 if (info->indexed && r300->index_buffer.buffer) {
681 indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
682 PIPE_TRANSFER_READ, &ib_transfer);
683 }
684
685 draw_set_mapped_index_buffer(r300->draw, indices);
686
687 draw_vbo(r300->draw, info);
688
689 /* XXX Not sure whether this is the best fix.
690 * It prevents CS from being rejected and weird assertion failures. */
691 draw_flush(r300->draw);
692
693 for (i = 0; i < r300->vertex_buffer_count; i++) {
694 if (r300->vertex_buffer[i].buffer) {
695 pipe_buffer_unmap(pipe, r300->vertex_buffer[i].buffer,
696 vb_transfer[i]);
697 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
698 }
699 }
700
701 if (ib_transfer) {
702 pipe_buffer_unmap(pipe, r300->index_buffer.buffer, ib_transfer);
703 draw_set_mapped_index_buffer(r300->draw, NULL);
704 }
705 }
706
707 /* Object for rendering using Draw. */
708 struct r300_render {
709 /* Parent class */
710 struct vbuf_render base;
711
712 /* Pipe context */
713 struct r300_context* r300;
714
715 /* Vertex information */
716 size_t vertex_size;
717 unsigned prim;
718 unsigned hwprim;
719
720 /* VBO */
721 size_t vbo_offset;
722 size_t vbo_max_used;
723 void * vbo_ptr;
724
725 struct pipe_transfer *vbo_transfer;
726 };
727
728 static INLINE struct r300_render*
729 r300_render(struct vbuf_render* render)
730 {
731 return (struct r300_render*)render;
732 }
733
734 static const struct vertex_info*
735 r300_render_get_vertex_info(struct vbuf_render* render)
736 {
737 struct r300_render* r300render = r300_render(render);
738 struct r300_context* r300 = r300render->r300;
739
740 return &r300->vertex_info;
741 }
742
743 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
744 ushort vertex_size,
745 ushort count)
746 {
747 struct r300_render* r300render = r300_render(render);
748 struct r300_context* r300 = r300render->r300;
749 struct pipe_screen* screen = r300->context.screen;
750 size_t size = (size_t)vertex_size * (size_t)count;
751
752 if (size + r300render->vbo_offset > r300->draw_vbo_size)
753 {
754 pipe_resource_reference(&r300->vbo, NULL);
755 r300->vbo = pipe_buffer_create(screen,
756 PIPE_BIND_VERTEX_BUFFER,
757 R300_MAX_DRAW_VBO_SIZE);
758 r300render->vbo_offset = 0;
759 r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
760 }
761
762 r300render->vertex_size = vertex_size;
763 r300->vbo_offset = r300render->vbo_offset;
764
765 return (r300->vbo) ? TRUE : FALSE;
766 }
767
768 static void* r300_render_map_vertices(struct vbuf_render* render)
769 {
770 struct r300_render* r300render = r300_render(render);
771 struct r300_context* r300 = r300render->r300;
772
773 assert(!r300render->vbo_transfer);
774
775 r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
776 r300->vbo,
777 PIPE_TRANSFER_WRITE,
778 &r300render->vbo_transfer);
779
780 assert(r300render->vbo_ptr);
781
782 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
783 }
784
785 static void r300_render_unmap_vertices(struct vbuf_render* render,
786 ushort min,
787 ushort max)
788 {
789 struct r300_render* r300render = r300_render(render);
790 struct pipe_context* context = &r300render->r300->context;
791 struct r300_context* r300 = r300render->r300;
792
793 assert(r300render->vbo_transfer);
794
795 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
796 r300render->vertex_size * (max + 1));
797 pipe_buffer_unmap(context, r300->vbo, r300render->vbo_transfer);
798
799 r300render->vbo_transfer = NULL;
800 }
801
802 static void r300_render_release_vertices(struct vbuf_render* render)
803 {
804 struct r300_render* r300render = r300_render(render);
805
806 r300render->vbo_offset += r300render->vbo_max_used;
807 r300render->vbo_max_used = 0;
808 }
809
810 static boolean r300_render_set_primitive(struct vbuf_render* render,
811 unsigned prim)
812 {
813 struct r300_render* r300render = r300_render(render);
814
815 r300render->prim = prim;
816 r300render->hwprim = r300_translate_primitive(prim);
817
818 return TRUE;
819 }
820
821 static void r300_render_draw_arrays(struct vbuf_render* render,
822 unsigned start,
823 unsigned count)
824 {
825 struct r300_render* r300render = r300_render(render);
826 struct r300_context* r300 = r300render->r300;
827 uint8_t* ptr;
828 unsigned i;
829 unsigned dwords = 6;
830
831 CS_LOCALS(r300);
832 (void) i; (void) ptr;
833
834 r300_prepare_for_rendering(r300, PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL,
835 NULL, dwords, 0, 0);
836
837 DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
838
839 /* Uncomment to dump all VBOs rendered through this interface.
840 * Slow and noisy!
841 ptr = pipe_buffer_map(&r300render->r300->context,
842 r300render->vbo, PIPE_TRANSFER_READ,
843 &r300render->vbo_transfer);
844
845 for (i = 0; i < count; i++) {
846 printf("r300: Vertex %d\n", i);
847 draw_dump_emitted_vertex(&r300->vertex_info, ptr);
848 ptr += r300->vertex_info.size * 4;
849 printf("\n");
850 }
851
852 pipe_buffer_unmap(&r300render->r300->context, r300render->vbo,
853 r300render->vbo_transfer);
854 */
855
856 BEGIN_CS(dwords);
857 OUT_CS_REG(R300_GA_COLOR_CONTROL,
858 r300_provoking_vertex_fixes(r300, r300render->prim));
859 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
860 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
861 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
862 r300render->hwprim);
863 END_CS;
864 }
865
866 static void r300_render_draw_elements(struct vbuf_render* render,
867 const ushort* indices,
868 uint count)
869 {
870 struct r300_render* r300render = r300_render(render);
871 struct r300_context* r300 = r300render->r300;
872 int i;
873 unsigned end_cs_dwords;
874 unsigned max_index = (r300->draw_vbo_size - r300render->vbo_offset) /
875 (r300render->r300->vertex_info.size * 4) - 1;
876 unsigned short_count;
877 unsigned free_dwords;
878
879 CS_LOCALS(r300);
880 DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
881
882 /* Reserve at least 256 dwords.
883 *
884 * Below we manage the CS space manually because there may be more
885 * indices than it can fit in CS. */
886 r300_prepare_for_rendering(r300,
887 PREP_FIRST_DRAW | PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
888 NULL, 256, 0, 0);
889 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
890
891 while (count) {
892 free_dwords = r300->cs->ndw - r300->cs->cdw;
893
894 short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
895
896 BEGIN_CS(6 + (short_count+1)/2);
897 OUT_CS_REG(R300_GA_COLOR_CONTROL,
898 r300_provoking_vertex_fixes(r300, r300render->prim));
899 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
900 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
901 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
902 r300render->hwprim);
903 for (i = 0; i < short_count-1; i += 2) {
904 OUT_CS(indices[i+1] << 16 | indices[i]);
905 }
906 if (short_count % 2) {
907 OUT_CS(indices[short_count-1]);
908 }
909 END_CS;
910
911 /* OK now subtract the emitted indices and see if we need to emit
912 * another draw packet. */
913 indices += short_count;
914 count -= short_count;
915
916 if (count) {
917 r300_prepare_for_rendering(r300,
918 PREP_EMIT_AOS_SWTCL | PREP_INDEXED,
919 NULL, 256, 0, 0);
920 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
921 }
922 }
923 }
924
925 static void r300_render_destroy(struct vbuf_render* render)
926 {
927 FREE(render);
928 }
929
930 static struct vbuf_render* r300_render_create(struct r300_context* r300)
931 {
932 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
933
934 r300render->r300 = r300;
935
936 /* XXX find real numbers plz */
937 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
938 r300render->base.max_indices = 16 * 1024;
939
940 r300render->base.get_vertex_info = r300_render_get_vertex_info;
941 r300render->base.allocate_vertices = r300_render_allocate_vertices;
942 r300render->base.map_vertices = r300_render_map_vertices;
943 r300render->base.unmap_vertices = r300_render_unmap_vertices;
944 r300render->base.set_primitive = r300_render_set_primitive;
945 r300render->base.draw_elements = r300_render_draw_elements;
946 r300render->base.draw_arrays = r300_render_draw_arrays;
947 r300render->base.release_vertices = r300_render_release_vertices;
948 r300render->base.destroy = r300_render_destroy;
949
950 r300render->vbo_offset = 0;
951
952 return &r300render->base;
953 }
954
955 struct draw_stage* r300_draw_stage(struct r300_context* r300)
956 {
957 struct vbuf_render* render;
958 struct draw_stage* stage;
959
960 render = r300_render_create(r300);
961
962 if (!render) {
963 return NULL;
964 }
965
966 stage = draw_vbuf_stage(r300->draw, render);
967
968 if (!stage) {
969 render->destroy(render);
970 return NULL;
971 }
972
973 draw_set_render(r300->draw, render);
974
975 return stage;
976 }
977
978 void r300_draw_flush_vbuf(struct r300_context *r300)
979 {
980 pipe_resource_reference(&r300->vbo, NULL);
981 r300->draw_vbo_size = 0;
982 }
983
984 /****************************************************************************
985 * End of SW TCL functions *
986 ***************************************************************************/
987
988 /* If we used a quad to draw a rectangle, the pixels on the main diagonal
989 * would be computed and stored twice, which makes the clear/copy codepaths
990 * somewhat inefficient. Instead we use a rectangular point sprite. */
991 static void r300_blitter_draw_rectangle(struct blitter_context *blitter,
992 unsigned x1, unsigned y1,
993 unsigned x2, unsigned y2,
994 float depth,
995 enum blitter_attrib_type type,
996 const float attrib[4])
997 {
998 struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
999 unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1000 unsigned width = x2 - x1;
1001 unsigned height = y2 - y1;
1002 unsigned vertex_size =
1003 type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1004 unsigned dwords = 13 + vertex_size +
1005 (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
1006 const float zeros[4] = {0, 0, 0, 0};
1007 CS_LOCALS(r300);
1008
1009 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
1010 r300->sprite_coord_enable = 1;
1011
1012 r300_update_derived_state(r300);
1013
1014 /* Mark some states we don't care about as non-dirty. */
1015 r300->clip_state.dirty = FALSE;
1016 r300->viewport_state.dirty = FALSE;
1017
1018 r300_prepare_for_rendering(r300, PREP_FIRST_DRAW, NULL, dwords, 0, 0);
1019
1020 DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1021
1022 BEGIN_CS(dwords);
1023 /* Set up GA. */
1024 OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1025
1026 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
1027 /* Set up the GA to generate texcoords. */
1028 OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1029 (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1030 OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1031 OUT_CS_32F(attrib[0]);
1032 OUT_CS_32F(attrib[3]);
1033 OUT_CS_32F(attrib[2]);
1034 OUT_CS_32F(attrib[1]);
1035 }
1036
1037 /* Set up VAP controls. */
1038 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1039 OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1040 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1041 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1042 OUT_CS(1);
1043 OUT_CS(0);
1044
1045 /* Draw. */
1046 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1047 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1048 R300_VAP_VF_CNTL__PRIM_POINTS);
1049
1050 OUT_CS_32F(x1 + width * 0.5f);
1051 OUT_CS_32F(y1 + height * 0.5f);
1052 OUT_CS_32F(depth);
1053 OUT_CS_32F(1);
1054
1055 if (vertex_size == 8) {
1056 if (!attrib)
1057 attrib = zeros;
1058 OUT_CS_TABLE(attrib, 4);
1059 }
1060 END_CS;
1061
1062 /* Restore the state. */
1063 r300->clip_state.dirty = TRUE;
1064 r300->rs_state.dirty = TRUE;
1065 r300->viewport_state.dirty = TRUE;
1066
1067 r300->sprite_coord_enable = last_sprite_coord_enable;
1068 }
1069
1070 static void r300_resource_resolve(struct pipe_context* pipe,
1071 struct pipe_resource* dest,
1072 struct pipe_subresource subdest,
1073 struct pipe_resource* src,
1074 struct pipe_subresource subsrc)
1075 {
1076 struct r300_context* r300 = r300_context(pipe);
1077 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1078 struct pipe_surface* srcsurf = src->screen->get_tex_surface(src->screen,
1079 src, subsrc.face, subsrc.level, 0, 0);
1080 float color[] = {0, 0, 0, 0};
1081
1082 DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
1083
1084 /* Enable AA resolve. */
1085 aa->dest = r300_surface(
1086 dest->screen->get_tex_surface(dest->screen, dest, subdest.face,
1087 subdest.level, 0, 0));
1088
1089 aa->aaresolve_ctl =
1090 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
1091 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
1092 r300->aa_state.size = 12;
1093 r300->aa_state.dirty = TRUE;
1094
1095 /* Resolve the surface. */
1096 r300->context.clear_render_target(pipe,
1097 srcsurf, color, 0, 0, src->width0, src->height0);
1098
1099 /* Disable AA resolve. */
1100 aa->aaresolve_ctl = 0;
1101 r300->aa_state.size = 4;
1102 r300->aa_state.dirty = TRUE;
1103
1104 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
1105 pipe_surface_reference((struct pipe_surface**)&aa->dest, NULL);
1106 }
1107
1108 void r300_init_render_functions(struct r300_context *r300)
1109 {
1110 /* Set draw functions based on presence of HW TCL. */
1111 if (r300->screen->caps.has_tcl) {
1112 r300->context.draw_vbo = r300_draw_vbo;
1113 } else {
1114 r300->context.draw_vbo = r300_swtcl_draw_vbo;
1115 }
1116
1117 r300->context.resource_resolve = r300_resource_resolve;
1118 r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
1119
1120 /* Plug in the two-sided stencil reference value fallback if needed. */
1121 if (!r300->screen->caps.is_r500)
1122 r300_plug_in_stencil_ref_fallback(r300);
1123 }