r300g: always set VF_MIN_VTX_INDX to 0
[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
43 #include <limits.h>
44
45 #define IMMD_DWORDS 32
46
47 static uint32_t r300_translate_primitive(unsigned prim)
48 {
49 static const int prim_conv[] = {
50 R300_VAP_VF_CNTL__PRIM_POINTS,
51 R300_VAP_VF_CNTL__PRIM_LINES,
52 R300_VAP_VF_CNTL__PRIM_LINE_LOOP,
53 R300_VAP_VF_CNTL__PRIM_LINE_STRIP,
54 R300_VAP_VF_CNTL__PRIM_TRIANGLES,
55 R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP,
56 R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN,
57 R300_VAP_VF_CNTL__PRIM_QUADS,
58 R300_VAP_VF_CNTL__PRIM_QUAD_STRIP,
59 R300_VAP_VF_CNTL__PRIM_POLYGON,
60 -1,
61 -1,
62 -1,
63 -1
64 };
65 unsigned hwprim = prim_conv[prim];
66
67 assert(hwprim != -1);
68 return hwprim;
69 }
70
71 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
72 unsigned mode)
73 {
74 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
75 uint32_t color_control = rs->color_control;
76
77 /* By default (see r300_state.c:r300_create_rs_state) color_control is
78 * initialized to provoking the first vertex.
79 *
80 * Triangle fans must be reduced to the second vertex, not the first, in
81 * Gallium flatshade-first mode, as per the GL spec.
82 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
83 *
84 * Quads never provoke correctly in flatshade-first mode. The first
85 * vertex is never considered as provoking, so only the second, third,
86 * and fourth vertices can be selected, and both "third" and "last" modes
87 * select the fourth vertex. This is probably due to D3D lacking quads.
88 *
89 * Similarly, polygons reduce to the first, not the last, vertex, when in
90 * "last" mode, and all other modes start from the second vertex.
91 *
92 * ~ C.
93 */
94
95 if (rs->rs.flatshade_first) {
96 switch (mode) {
97 case PIPE_PRIM_TRIANGLE_FAN:
98 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
99 break;
100 case PIPE_PRIM_QUADS:
101 case PIPE_PRIM_QUAD_STRIP:
102 case PIPE_PRIM_POLYGON:
103 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
104 break;
105 default:
106 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
107 break;
108 }
109 } else {
110 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
111 }
112
113 return color_control;
114 }
115
116 void r500_emit_index_bias(struct r300_context *r300, int index_bias)
117 {
118 CS_LOCALS(r300);
119
120 BEGIN_CS(2);
121 OUT_CS_REG(R500_VAP_INDEX_OFFSET,
122 (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
123 END_CS;
124 }
125
126 static void r300_emit_draw_init(struct r300_context *r300, unsigned mode,
127 unsigned max_index)
128 {
129 CS_LOCALS(r300);
130
131 BEGIN_CS(5);
132 OUT_CS_REG(R300_GA_COLOR_CONTROL,
133 r300_provoking_vertex_fixes(r300, mode));
134 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
135 OUT_CS(max_index);
136 OUT_CS(0);
137 END_CS;
138 }
139
140 /* This function splits the index bias value into two parts:
141 * - buffer_offset: the value that can be safely added to buffer offsets
142 * in r300_emit_vertex_arrays (it must yield a positive offset when added to
143 * a vertex buffer offset)
144 * - index_offset: the value that must be manually subtracted from indices
145 * in an index buffer to achieve negative offsets. */
146 static void r300_split_index_bias(struct r300_context *r300, int index_bias,
147 int *buffer_offset, int *index_offset)
148 {
149 struct pipe_vertex_buffer *vb, *vbufs = r300->vbuf_mgr->real_vertex_buffer;
150 struct pipe_vertex_element *velem = r300->velems->velem;
151 unsigned i, size;
152 int max_neg_bias;
153
154 if (index_bias < 0) {
155 /* See how large index bias we may subtract. We must be careful
156 * here because negative buffer offsets are not allowed
157 * by the DRM API. */
158 max_neg_bias = INT_MAX;
159 for (i = 0; i < r300->velems->count; i++) {
160 vb = &vbufs[velem[i].vertex_buffer_index];
161 size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
162 max_neg_bias = MIN2(max_neg_bias, size);
163 }
164
165 /* Now set the minimum allowed value. */
166 *buffer_offset = MAX2(-max_neg_bias, index_bias);
167 } else {
168 /* A positive index bias is OK. */
169 *buffer_offset = index_bias;
170 }
171
172 *index_offset = index_bias - *buffer_offset;
173 }
174
175 enum r300_prepare_flags {
176 PREP_EMIT_STATES = (1 << 0), /* call emit_dirty_state and friends? */
177 PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */
178 PREP_EMIT_VARRAYS = (1 << 2), /* call emit_vertex_arrays? */
179 PREP_EMIT_VARRAYS_SWTCL = (1 << 3), /* call emit_vertex_arrays_swtcl? */
180 PREP_INDEXED = (1 << 4) /* is this draw_elements? */
181 };
182
183 /**
184 * Check if the requested number of dwords is available in the CS and
185 * if not, flush.
186 * \param r300 The context.
187 * \param flags See r300_prepare_flags.
188 * \param cs_dwords The number of dwords to reserve in CS.
189 * \return TRUE if the CS was flushed
190 */
191 static boolean r300_reserve_cs_dwords(struct r300_context *r300,
192 enum r300_prepare_flags flags,
193 unsigned cs_dwords)
194 {
195 boolean flushed = FALSE;
196 boolean emit_states = flags & PREP_EMIT_STATES;
197 boolean emit_vertex_arrays = flags & PREP_EMIT_VARRAYS;
198 boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;
199
200 /* Add dirty state, index offset, and AOS. */
201 if (emit_states)
202 cs_dwords += r300_get_num_dirty_dwords(r300);
203
204 if (r300->screen->caps.is_r500)
205 cs_dwords += 2; /* emit_index_offset */
206
207 if (emit_vertex_arrays)
208 cs_dwords += 55; /* emit_vertex_arrays */
209
210 if (emit_vertex_arrays_swtcl)
211 cs_dwords += 7; /* emit_vertex_arrays_swtcl */
212
213 cs_dwords += r300_get_num_cs_end_dwords(r300);
214
215 /* Reserve requested CS space. */
216 if (cs_dwords > (RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
217 r300_flush(&r300->context, RADEON_FLUSH_ASYNC, NULL);
218 flushed = TRUE;
219 }
220
221 return flushed;
222 }
223
224 /**
225 * Validate buffers and emit dirty state.
226 * \param r300 The context.
227 * \param flags See r300_prepare_flags.
228 * \param index_buffer The index buffer to validate. The parameter may be NULL.
229 * \param buffer_offset The offset passed to emit_vertex_arrays.
230 * \param index_bias The index bias to emit.
231 * \param instance_id Index of instance to render
232 * \return TRUE if rendering should be skipped
233 */
234 static boolean r300_emit_states(struct r300_context *r300,
235 enum r300_prepare_flags flags,
236 struct pipe_resource *index_buffer,
237 int buffer_offset,
238 int index_bias, int instance_id)
239 {
240 boolean emit_states = flags & PREP_EMIT_STATES;
241 boolean emit_vertex_arrays = flags & PREP_EMIT_VARRAYS;
242 boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;
243 boolean indexed = flags & PREP_INDEXED;
244 boolean validate_vbos = flags & PREP_VALIDATE_VBOS;
245
246 /* Validate buffers and emit dirty state if needed. */
247 if (emit_states || (emit_vertex_arrays && validate_vbos)) {
248 if (!r300_emit_buffer_validate(r300, validate_vbos,
249 index_buffer)) {
250 fprintf(stderr, "r300: CS space validation failed. "
251 "(not enough memory?) Skipping rendering.\n");
252 return FALSE;
253 }
254 }
255
256 if (emit_states)
257 r300_emit_dirty_state(r300);
258
259 if (r300->screen->caps.is_r500) {
260 if (r300->screen->caps.has_tcl)
261 r500_emit_index_bias(r300, index_bias);
262 else
263 r500_emit_index_bias(r300, 0);
264 }
265
266 if (emit_vertex_arrays &&
267 (r300->vertex_arrays_dirty ||
268 r300->vertex_arrays_indexed != indexed ||
269 r300->vertex_arrays_offset != buffer_offset ||
270 r300->vertex_arrays_instance_id != instance_id)) {
271 r300_emit_vertex_arrays(r300, buffer_offset, indexed, instance_id);
272
273 r300->vertex_arrays_dirty = FALSE;
274 r300->vertex_arrays_indexed = indexed;
275 r300->vertex_arrays_offset = buffer_offset;
276 r300->vertex_arrays_instance_id = instance_id;
277 }
278
279 if (emit_vertex_arrays_swtcl)
280 r300_emit_vertex_arrays_swtcl(r300, indexed);
281
282 return TRUE;
283 }
284
285 /**
286 * Check if the requested number of dwords is available in the CS and
287 * if not, flush. Then validate buffers and emit dirty state.
288 * \param r300 The context.
289 * \param flags See r300_prepare_flags.
290 * \param index_buffer The index buffer to validate. The parameter may be NULL.
291 * \param cs_dwords The number of dwords to reserve in CS.
292 * \param buffer_offset The offset passed to emit_vertex_arrays.
293 * \param index_bias The index bias to emit.
294 * \param instance_id The instance to render.
295 * \return TRUE if rendering should be skipped
296 */
297 static boolean r300_prepare_for_rendering(struct r300_context *r300,
298 enum r300_prepare_flags flags,
299 struct pipe_resource *index_buffer,
300 unsigned cs_dwords,
301 int buffer_offset,
302 int index_bias,
303 int instance_id)
304 {
305 /* Make sure there is enough space in the command stream and emit states. */
306 if (r300_reserve_cs_dwords(r300, flags, cs_dwords))
307 flags |= PREP_EMIT_STATES;
308
309 return r300_emit_states(r300, flags, index_buffer, buffer_offset,
310 index_bias, instance_id);
311 }
312
313 static boolean immd_is_good_idea(struct r300_context *r300,
314 unsigned count)
315 {
316 if (DBG_ON(r300, DBG_NO_IMMD)) {
317 return FALSE;
318 }
319
320 if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
321 return FALSE;
322 }
323
324 /* Buffers can only be used for read by r300 (except query buffers, but
325 * those can't be bound by a state tracker as vertex buffers). */
326 return TRUE;
327 }
328
329 /*****************************************************************************
330 * The HWTCL draw functions. *
331 ****************************************************************************/
332
333 static void r300_draw_arrays_immediate(struct r300_context *r300,
334 const struct pipe_draw_info *info)
335 {
336 struct pipe_vertex_element* velem;
337 struct pipe_vertex_buffer* vbuf;
338 unsigned vertex_element_count = r300->velems->count;
339 unsigned i, v, vbi;
340
341 /* Size of the vertex, in dwords. */
342 unsigned vertex_size = r300->velems->vertex_size_dwords;
343
344 /* The number of dwords for this draw operation. */
345 unsigned dwords = 4 + info->count * vertex_size;
346
347 /* Size of the vertex element, in dwords. */
348 unsigned size[PIPE_MAX_ATTRIBS];
349
350 /* Stride to the same attrib in the next vertex in the vertex buffer,
351 * in dwords. */
352 unsigned stride[PIPE_MAX_ATTRIBS];
353
354 /* Mapped vertex buffers. */
355 uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
356 uint32_t* mapelem[PIPE_MAX_ATTRIBS];
357
358 CS_LOCALS(r300);
359
360 if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
361 return;
362
363 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
364 for (i = 0; i < vertex_element_count; i++) {
365 velem = &r300->velems->velem[i];
366 size[i] = r300->velems->format_size[i] / 4;
367 vbi = velem->vertex_buffer_index;
368 vbuf = &r300->vbuf_mgr->real_vertex_buffer[vbi];
369 stride[i] = vbuf->stride / 4;
370
371 /* Map the buffer. */
372 if (!map[vbi]) {
373 map[vbi] = (uint32_t*)r300->rws->buffer_map(
374 r300_resource(vbuf->buffer)->buf,
375 r300->cs, PIPE_TRANSFER_READ | PIPE_TRANSFER_UNSYNCHRONIZED);
376 map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * info->start;
377 }
378 mapelem[i] = map[vbi] + (velem->src_offset / 4);
379 }
380
381 r300_emit_draw_init(r300, info->mode, info->count-1);
382
383 BEGIN_CS(dwords);
384 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
385 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, info->count * vertex_size);
386 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (info->count << 16) |
387 r300_translate_primitive(info->mode));
388
389 /* Emit vertices. */
390 for (v = 0; v < info->count; v++) {
391 for (i = 0; i < vertex_element_count; i++) {
392 OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
393 }
394 }
395 END_CS;
396
397 /* Unmap buffers. */
398 for (i = 0; i < vertex_element_count; i++) {
399 vbi = r300->velems->velem[i].vertex_buffer_index;
400
401 if (map[vbi]) {
402 r300->rws->buffer_unmap(r300_resource(r300->vbuf_mgr->real_vertex_buffer[vbi].buffer)->buf);
403 map[vbi] = NULL;
404 }
405 }
406 }
407
408 static void r300_emit_draw_arrays(struct r300_context *r300,
409 unsigned mode,
410 unsigned count)
411 {
412 boolean alt_num_verts = count > 65535;
413 CS_LOCALS(r300);
414
415 if (count >= (1 << 24)) {
416 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
417 "refusing to render.\n", count);
418 return;
419 }
420
421 r300_emit_draw_init(r300, mode, count-1);
422
423 BEGIN_CS(2 + (alt_num_verts ? 2 : 0));
424 if (alt_num_verts) {
425 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
426 }
427 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
428 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
429 r300_translate_primitive(mode) |
430 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
431 END_CS;
432 }
433
434 static void r300_emit_draw_elements(struct r300_context *r300,
435 struct pipe_resource* indexBuffer,
436 unsigned indexSize,
437 unsigned max_index,
438 unsigned mode,
439 unsigned start,
440 unsigned count,
441 uint16_t *imm_indices3)
442 {
443 uint32_t count_dwords, offset_dwords;
444 boolean alt_num_verts = count > 65535;
445 CS_LOCALS(r300);
446
447 if (count >= (1 << 24) || max_index >= (1 << 24)) {
448 fprintf(stderr, "r300: Got a huge number of vertices: %i, "
449 "refusing to render (max_index: %i).\n", count, max_index);
450 return;
451 }
452
453 DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, max %u\n",
454 count, max_index);
455
456 r300_emit_draw_init(r300, mode, max_index);
457
458 /* If start is odd, render the first triangle with indices embedded
459 * in the command stream. This will increase start by 3 and make it
460 * even. We can then proceed without a fallback. */
461 if (indexSize == 2 && (start & 1) &&
462 mode == PIPE_PRIM_TRIANGLES) {
463 BEGIN_CS(4);
464 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 2);
465 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (3 << 16) |
466 R300_VAP_VF_CNTL__PRIM_TRIANGLES);
467 OUT_CS(imm_indices3[1] << 16 | imm_indices3[0]);
468 OUT_CS(imm_indices3[2]);
469 END_CS;
470
471 start += 3;
472 count -= 3;
473 if (!count)
474 return;
475 }
476
477 offset_dwords = indexSize * start / sizeof(uint32_t);
478
479 BEGIN_CS(8 + (alt_num_verts ? 2 : 0));
480 if (alt_num_verts) {
481 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
482 }
483 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
484 if (indexSize == 4) {
485 count_dwords = count;
486 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
487 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
488 r300_translate_primitive(mode) |
489 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
490 } else {
491 count_dwords = (count + 1) / 2;
492 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
493 r300_translate_primitive(mode) |
494 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
495 }
496
497 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
498 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
499 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
500 OUT_CS(offset_dwords << 2);
501 OUT_CS(count_dwords);
502 OUT_CS_RELOC(r300_resource(indexBuffer));
503 END_CS;
504 }
505
506 static void r300_draw_elements_immediate(struct r300_context *r300,
507 const struct pipe_draw_info *info)
508 {
509 uint8_t *ptr1;
510 uint16_t *ptr2;
511 uint32_t *ptr4;
512 unsigned index_size = r300->index_buffer.index_size;
513 unsigned i, count_dwords = index_size == 4 ? info->count :
514 (info->count + 1) / 2;
515 CS_LOCALS(r300);
516
517 /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
518 if (!r300_prepare_for_rendering(r300,
519 PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |
520 PREP_INDEXED, NULL, 2+count_dwords, 0, info->index_bias, -1))
521 return;
522
523 r300_emit_draw_init(r300, info->mode, info->max_index);
524
525 BEGIN_CS(2 + count_dwords);
526 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);
527
528 switch (index_size) {
529 case 1:
530 ptr1 = r300_resource(r300->index_buffer.buffer)->b.user_ptr;
531 ptr1 += info->start;
532
533 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
534 r300_translate_primitive(info->mode));
535
536 if (info->index_bias && !r300->screen->caps.is_r500) {
537 for (i = 0; i < info->count-1; i += 2)
538 OUT_CS(((ptr1[i+1] + info->index_bias) << 16) |
539 (ptr1[i] + info->index_bias));
540
541 if (info->count & 1)
542 OUT_CS(ptr1[i] + info->index_bias);
543 } else {
544 for (i = 0; i < info->count-1; i += 2)
545 OUT_CS(((ptr1[i+1]) << 16) |
546 (ptr1[i] ));
547
548 if (info->count & 1)
549 OUT_CS(ptr1[i]);
550 }
551 break;
552
553 case 2:
554 ptr2 = (uint16_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
555 ptr2 += info->start;
556
557 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
558 r300_translate_primitive(info->mode));
559
560 if (info->index_bias && !r300->screen->caps.is_r500) {
561 for (i = 0; i < info->count-1; i += 2)
562 OUT_CS(((ptr2[i+1] + info->index_bias) << 16) |
563 (ptr2[i] + info->index_bias));
564
565 if (info->count & 1)
566 OUT_CS(ptr2[i] + info->index_bias);
567 } else {
568 OUT_CS_TABLE(ptr2, count_dwords);
569 }
570 break;
571
572 case 4:
573 ptr4 = (uint32_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
574 ptr4 += info->start;
575
576 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
577 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
578 r300_translate_primitive(info->mode));
579
580 if (info->index_bias && !r300->screen->caps.is_r500) {
581 for (i = 0; i < info->count; i++)
582 OUT_CS(ptr4[i] + info->index_bias);
583 } else {
584 OUT_CS_TABLE(ptr4, count_dwords);
585 }
586 break;
587 }
588 END_CS;
589 }
590
591 static void r300_draw_elements(struct r300_context *r300,
592 const struct pipe_draw_info *info,
593 int instance_id)
594 {
595 struct pipe_resource *indexBuffer = r300->index_buffer.buffer;
596 unsigned indexSize = r300->index_buffer.index_size;
597 struct pipe_resource* orgIndexBuffer = indexBuffer;
598 unsigned start = info->start;
599 unsigned count = info->count;
600 boolean alt_num_verts = r300->screen->caps.is_r500 &&
601 count > 65536;
602 unsigned short_count;
603 int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
604 uint16_t indices3[3];
605
606 if (info->index_bias && !r300->screen->caps.is_r500) {
607 r300_split_index_bias(r300, info->index_bias, &buffer_offset, &index_offset);
608 }
609
610 r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
611 &start, count);
612
613 /* Fallback for misaligned ushort indices. */
614 if (indexSize == 2 && (start & 1) &&
615 !r300_resource(indexBuffer)->b.user_ptr) {
616 /* If we got here, then orgIndexBuffer == indexBuffer. */
617 uint16_t *ptr = r300->rws->buffer_map(r300_resource(orgIndexBuffer)->buf,
618 r300->cs,
619 PIPE_TRANSFER_READ |
620 PIPE_TRANSFER_UNSYNCHRONIZED);
621
622 if (info->mode == PIPE_PRIM_TRIANGLES) {
623 memcpy(indices3, ptr + start, 6);
624 } else {
625 /* Copy the mapped index buffer directly to the upload buffer.
626 * The start index will be aligned simply from the fact that
627 * every sub-buffer in the upload buffer is aligned. */
628 r300_upload_index_buffer(r300, &indexBuffer, indexSize, &start,
629 count, (uint8_t*)ptr);
630 }
631 r300->rws->buffer_unmap(r300_resource(orgIndexBuffer)->buf);
632 } else {
633 if (r300_resource(indexBuffer)->b.user_ptr)
634 r300_upload_index_buffer(r300, &indexBuffer, indexSize,
635 &start, count,
636 r300_resource(indexBuffer)->b.user_ptr);
637 }
638
639 /* 19 dwords for emit_draw_elements. Give up if the function fails. */
640 if (!r300_prepare_for_rendering(r300,
641 PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |
642 PREP_INDEXED, indexBuffer, 19, buffer_offset, info->index_bias,
643 instance_id))
644 goto done;
645
646 if (alt_num_verts || count <= 65535) {
647 r300_emit_draw_elements(r300, indexBuffer, indexSize,
648 info->max_index, info->mode, start, count,
649 indices3);
650 } else {
651 do {
652 /* The maximum must be divisible by 4 and 3,
653 * so that quad and triangle lists are split correctly.
654 *
655 * Strips, loops, and fans won't work. */
656 short_count = MIN2(count, 65532);
657
658 r300_emit_draw_elements(r300, indexBuffer, indexSize,
659 info->max_index,
660 info->mode, start, short_count, indices3);
661
662 start += short_count;
663 count -= short_count;
664
665 /* 15 dwords for emit_draw_elements */
666 if (count) {
667 if (!r300_prepare_for_rendering(r300,
668 PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS | PREP_INDEXED,
669 indexBuffer, 19, buffer_offset, info->index_bias,
670 instance_id))
671 goto done;
672 }
673 } while (count);
674 }
675
676 done:
677 if (indexBuffer != orgIndexBuffer) {
678 pipe_resource_reference( &indexBuffer, NULL );
679 }
680 }
681
682 static void r300_draw_arrays(struct r300_context *r300,
683 const struct pipe_draw_info *info,
684 int instance_id)
685 {
686 boolean alt_num_verts = r300->screen->caps.is_r500 &&
687 info->count > 65536;
688 unsigned start = info->start;
689 unsigned count = info->count;
690 unsigned short_count;
691
692 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
693 if (!r300_prepare_for_rendering(r300,
694 PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS,
695 NULL, 9, start, 0, instance_id))
696 return;
697
698 if (alt_num_verts || count <= 65535) {
699 r300_emit_draw_arrays(r300, info->mode, count);
700 } else {
701 do {
702 /* The maximum must be divisible by 4 and 3,
703 * so that quad and triangle lists are split correctly.
704 *
705 * Strips, loops, and fans won't work. */
706 short_count = MIN2(count, 65532);
707 r300_emit_draw_arrays(r300, info->mode, short_count);
708
709 start += short_count;
710 count -= short_count;
711
712 /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
713 if (count) {
714 if (!r300_prepare_for_rendering(r300,
715 PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS, NULL, 9,
716 start, 0, instance_id))
717 return;
718 }
719 } while (count);
720 }
721 }
722
723 static void r300_draw_arrays_instanced(struct r300_context *r300,
724 const struct pipe_draw_info *info)
725 {
726 int i;
727
728 for (i = 0; i < info->instance_count; i++)
729 r300_draw_arrays(r300, info, i);
730 }
731
732 static void r300_draw_elements_instanced(struct r300_context *r300,
733 const struct pipe_draw_info *info)
734 {
735 int i;
736
737 for (i = 0; i < info->instance_count; i++)
738 r300_draw_elements(r300, info, i);
739 }
740
741 static void r300_draw_vbo(struct pipe_context* pipe,
742 const struct pipe_draw_info *dinfo)
743 {
744 struct r300_context* r300 = r300_context(pipe);
745 struct pipe_draw_info info = *dinfo;
746
747 info.indexed = info.indexed && r300->index_buffer.buffer;
748
749 if (r300->skip_rendering ||
750 !u_trim_pipe_prim(info.mode, &info.count)) {
751 return;
752 }
753
754 r300_update_derived_state(r300);
755
756 /* Start the vbuf manager and update buffers if needed. */
757 if (u_vbuf_draw_begin(r300->vbuf_mgr, &info) & U_VBUF_BUFFERS_UPDATED) {
758 r300->vertex_arrays_dirty = TRUE;
759 }
760
761 /* Draw. */
762 if (info.indexed) {
763 info.start += r300->index_buffer.offset;
764 info.max_index = MIN2(r300->vbuf_mgr->max_index, info.max_index);
765
766 if (info.instance_count <= 1) {
767 if (info.count <= 8 &&
768 r300_resource(r300->index_buffer.buffer)->b.user_ptr) {
769 r300_draw_elements_immediate(r300, &info);
770 } else {
771 r300_draw_elements(r300, &info, -1);
772 }
773 } else {
774 r300_draw_elements_instanced(r300, &info);
775 }
776 } else {
777 if (info.instance_count <= 1) {
778 if (immd_is_good_idea(r300, info.count)) {
779 r300_draw_arrays_immediate(r300, &info);
780 } else {
781 r300_draw_arrays(r300, &info, -1);
782 }
783 } else {
784 r300_draw_arrays_instanced(r300, &info);
785 }
786 }
787
788 u_vbuf_draw_end(r300->vbuf_mgr);
789 }
790
791 /****************************************************************************
792 * The rest of this file is for SW TCL rendering only. Please be polite and *
793 * keep these functions separated so that they are easier to locate. ~C. *
794 ***************************************************************************/
795
796 /* SW TCL elements, using Draw. */
797 static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
798 const struct pipe_draw_info *info)
799 {
800 struct r300_context* r300 = r300_context(pipe);
801 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
802 struct pipe_transfer *ib_transfer = NULL;
803 int i;
804 void *indices = NULL;
805 boolean indexed = info->indexed && r300->index_buffer.buffer;
806
807 if (r300->skip_rendering) {
808 return;
809 }
810
811 r300_update_derived_state(r300);
812
813 r300_reserve_cs_dwords(r300,
814 PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL |
815 (indexed ? PREP_INDEXED : 0),
816 indexed ? 256 : 6);
817
818 for (i = 0; i < r300->vbuf_mgr->nr_vertex_buffers; i++) {
819 if (r300->vbuf_mgr->vertex_buffer[i].buffer) {
820 void *buf = pipe_buffer_map(pipe,
821 r300->vbuf_mgr->vertex_buffer[i].buffer,
822 PIPE_TRANSFER_READ |
823 PIPE_TRANSFER_UNSYNCHRONIZED,
824 &vb_transfer[i]);
825 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
826 }
827 }
828
829 if (indexed) {
830 indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
831 PIPE_TRANSFER_READ |
832 PIPE_TRANSFER_UNSYNCHRONIZED, &ib_transfer);
833 }
834
835 draw_set_mapped_index_buffer(r300->draw, indices);
836
837 r300->draw_vbo_locked = TRUE;
838 r300->draw_first_emitted = FALSE;
839 draw_vbo(r300->draw, info);
840 draw_flush(r300->draw);
841 r300->draw_vbo_locked = FALSE;
842
843 for (i = 0; i < r300->vbuf_mgr->nr_vertex_buffers; i++) {
844 if (r300->vbuf_mgr->vertex_buffer[i].buffer) {
845 pipe_buffer_unmap(pipe, vb_transfer[i]);
846 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
847 }
848 }
849
850 if (indexed) {
851 pipe_buffer_unmap(pipe, ib_transfer);
852 draw_set_mapped_index_buffer(r300->draw, NULL);
853 }
854 }
855
856 /* Object for rendering using Draw. */
857 struct r300_render {
858 /* Parent class */
859 struct vbuf_render base;
860
861 /* Pipe context */
862 struct r300_context* r300;
863
864 /* Vertex information */
865 size_t vertex_size;
866 unsigned prim;
867 unsigned hwprim;
868
869 /* VBO */
870 size_t vbo_max_used;
871 void * vbo_ptr;
872
873 struct pipe_transfer *vbo_transfer;
874 };
875
876 static INLINE struct r300_render*
877 r300_render(struct vbuf_render* render)
878 {
879 return (struct r300_render*)render;
880 }
881
882 static const struct vertex_info*
883 r300_render_get_vertex_info(struct vbuf_render* render)
884 {
885 struct r300_render* r300render = r300_render(render);
886 struct r300_context* r300 = r300render->r300;
887
888 return &r300->vertex_info;
889 }
890
891 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
892 ushort vertex_size,
893 ushort count)
894 {
895 struct r300_render* r300render = r300_render(render);
896 struct r300_context* r300 = r300render->r300;
897 struct pipe_screen* screen = r300->context.screen;
898 size_t size = (size_t)vertex_size * (size_t)count;
899
900 DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);
901
902 if (size + r300->draw_vbo_offset > r300->draw_vbo_size)
903 {
904 pipe_resource_reference(&r300->vbo, NULL);
905 r300->vbo = pipe_buffer_create(screen,
906 PIPE_BIND_VERTEX_BUFFER,
907 PIPE_USAGE_STREAM,
908 R300_MAX_DRAW_VBO_SIZE);
909 r300->draw_vbo_offset = 0;
910 r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
911 }
912
913 r300render->vertex_size = vertex_size;
914
915 return (r300->vbo) ? TRUE : FALSE;
916 }
917
918 static void* r300_render_map_vertices(struct vbuf_render* render)
919 {
920 struct r300_render* r300render = r300_render(render);
921 struct r300_context* r300 = r300render->r300;
922
923 assert(!r300render->vbo_transfer);
924
925 DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");
926
927 r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
928 r300->vbo,
929 PIPE_TRANSFER_WRITE |
930 PIPE_TRANSFER_UNSYNCHRONIZED,
931 &r300render->vbo_transfer);
932
933 assert(r300render->vbo_ptr);
934
935 return ((uint8_t*)r300render->vbo_ptr + r300->draw_vbo_offset);
936 }
937
938 static void r300_render_unmap_vertices(struct vbuf_render* render,
939 ushort min,
940 ushort max)
941 {
942 struct r300_render* r300render = r300_render(render);
943 struct pipe_context* context = &r300render->r300->context;
944 struct r300_context* r300 = r300render->r300;
945
946 assert(r300render->vbo_transfer);
947
948 DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");
949
950 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
951 r300render->vertex_size * (max + 1));
952 pipe_buffer_unmap(context, r300render->vbo_transfer);
953
954 r300render->vbo_transfer = NULL;
955 }
956
957 static void r300_render_release_vertices(struct vbuf_render* render)
958 {
959 struct r300_render* r300render = r300_render(render);
960 struct r300_context* r300 = r300render->r300;
961
962 DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");
963
964 r300->draw_vbo_offset += r300render->vbo_max_used;
965 r300render->vbo_max_used = 0;
966 }
967
968 static boolean r300_render_set_primitive(struct vbuf_render* render,
969 unsigned prim)
970 {
971 struct r300_render* r300render = r300_render(render);
972
973 r300render->prim = prim;
974 r300render->hwprim = r300_translate_primitive(prim);
975
976 return TRUE;
977 }
978
979 static void r300_render_draw_arrays(struct vbuf_render* render,
980 unsigned start,
981 unsigned count)
982 {
983 struct r300_render* r300render = r300_render(render);
984 struct r300_context* r300 = r300render->r300;
985 uint8_t* ptr;
986 unsigned i;
987 unsigned dwords = 6;
988
989 CS_LOCALS(r300);
990 (void) i; (void) ptr;
991
992 DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
993
994 if (r300->draw_first_emitted) {
995 if (!r300_prepare_for_rendering(r300,
996 PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,
997 NULL, dwords, 0, 0, -1))
998 return;
999 } else {
1000 if (!r300_emit_states(r300,
1001 PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,
1002 NULL, 0, 0, -1))
1003 return;
1004 }
1005
1006 BEGIN_CS(dwords);
1007 OUT_CS_REG(R300_GA_COLOR_CONTROL,
1008 r300_provoking_vertex_fixes(r300, r300render->prim));
1009 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
1010 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
1011 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
1012 r300render->hwprim);
1013 END_CS;
1014
1015 r300->draw_first_emitted = TRUE;
1016 }
1017
1018 static void r300_render_draw_elements(struct vbuf_render* render,
1019 const ushort* indices,
1020 uint count)
1021 {
1022 struct r300_render* r300render = r300_render(render);
1023 struct r300_context* r300 = r300render->r300;
1024 int i;
1025 unsigned end_cs_dwords;
1026 unsigned max_index = (r300->draw_vbo_size - r300->draw_vbo_offset) /
1027 (r300render->r300->vertex_info.size * 4) - 1;
1028 unsigned short_count;
1029 unsigned free_dwords;
1030
1031 CS_LOCALS(r300);
1032 DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
1033
1034 if (r300->draw_first_emitted) {
1035 if (!r300_prepare_for_rendering(r300,
1036 PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1037 NULL, 256, 0, 0, -1))
1038 return;
1039 } else {
1040 if (!r300_emit_states(r300,
1041 PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1042 NULL, 0, 0, -1))
1043 return;
1044 }
1045
1046 /* Below we manage the CS space manually because there may be more
1047 * indices than it can fit in CS. */
1048
1049 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1050
1051 while (count) {
1052 free_dwords = RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw;
1053
1054 short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
1055
1056 BEGIN_CS(6 + (short_count+1)/2);
1057 OUT_CS_REG(R300_GA_COLOR_CONTROL,
1058 r300_provoking_vertex_fixes(r300, r300render->prim));
1059 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
1060 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
1061 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
1062 r300render->hwprim);
1063 for (i = 0; i < short_count-1; i += 2) {
1064 OUT_CS(indices[i+1] << 16 | indices[i]);
1065 }
1066 if (short_count % 2) {
1067 OUT_CS(indices[short_count-1]);
1068 }
1069 END_CS;
1070
1071 /* OK now subtract the emitted indices and see if we need to emit
1072 * another draw packet. */
1073 indices += short_count;
1074 count -= short_count;
1075
1076 if (count) {
1077 if (!r300_prepare_for_rendering(r300,
1078 PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1079 NULL, 256, 0, 0, -1))
1080 return;
1081
1082 end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1083 }
1084 }
1085
1086 r300->draw_first_emitted = TRUE;
1087 }
1088
1089 static void r300_render_destroy(struct vbuf_render* render)
1090 {
1091 FREE(render);
1092 }
1093
1094 static struct vbuf_render* r300_render_create(struct r300_context* r300)
1095 {
1096 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
1097
1098 r300render->r300 = r300;
1099
1100 r300render->base.max_vertex_buffer_bytes = 1024 * 1024;
1101 r300render->base.max_indices = 16 * 1024;
1102
1103 r300render->base.get_vertex_info = r300_render_get_vertex_info;
1104 r300render->base.allocate_vertices = r300_render_allocate_vertices;
1105 r300render->base.map_vertices = r300_render_map_vertices;
1106 r300render->base.unmap_vertices = r300_render_unmap_vertices;
1107 r300render->base.set_primitive = r300_render_set_primitive;
1108 r300render->base.draw_elements = r300_render_draw_elements;
1109 r300render->base.draw_arrays = r300_render_draw_arrays;
1110 r300render->base.release_vertices = r300_render_release_vertices;
1111 r300render->base.destroy = r300_render_destroy;
1112
1113 return &r300render->base;
1114 }
1115
1116 struct draw_stage* r300_draw_stage(struct r300_context* r300)
1117 {
1118 struct vbuf_render* render;
1119 struct draw_stage* stage;
1120
1121 render = r300_render_create(r300);
1122
1123 if (!render) {
1124 return NULL;
1125 }
1126
1127 stage = draw_vbuf_stage(r300->draw, render);
1128
1129 if (!stage) {
1130 render->destroy(render);
1131 return NULL;
1132 }
1133
1134 draw_set_render(r300->draw, render);
1135
1136 return stage;
1137 }
1138
1139 void r300_draw_flush_vbuf(struct r300_context *r300)
1140 {
1141 pipe_resource_reference(&r300->vbo, NULL);
1142 r300->draw_vbo_size = 0;
1143 }
1144
1145 /****************************************************************************
1146 * End of SW TCL functions *
1147 ***************************************************************************/
1148
1149 /* This functions is used to draw a rectangle for the blitter module.
1150 *
1151 * If we rendered a quad, the pixels on the main diagonal
1152 * would be computed and stored twice, which makes the clear/copy codepaths
1153 * somewhat inefficient. Instead we use a rectangular point sprite. */
1154 static void r300_blitter_draw_rectangle(struct blitter_context *blitter,
1155 unsigned x1, unsigned y1,
1156 unsigned x2, unsigned y2,
1157 float depth,
1158 enum blitter_attrib_type type,
1159 const union pipe_color_union *attrib)
1160 {
1161 struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
1162 unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1163 unsigned width = x2 - x1;
1164 unsigned height = y2 - y1;
1165 unsigned vertex_size =
1166 type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1167 unsigned dwords = 13 + vertex_size +
1168 (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
1169 static const union pipe_color_union zeros;
1170 CS_LOCALS(r300);
1171
1172 if (r300->skip_rendering)
1173 return;
1174
1175 r300->context.set_vertex_buffers(&r300->context, 0, NULL);
1176
1177 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
1178 r300->sprite_coord_enable = 1;
1179
1180 r300_update_derived_state(r300);
1181
1182 /* Mark some states we don't care about as non-dirty. */
1183 r300->clip_state.dirty = FALSE;
1184 r300->viewport_state.dirty = FALSE;
1185
1186 if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
1187 goto done;
1188
1189 DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1190
1191 BEGIN_CS(dwords);
1192 /* Set up GA. */
1193 OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1194
1195 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
1196 /* Set up the GA to generate texcoords. */
1197 OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1198 (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1199 OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1200 OUT_CS_32F(attrib->f[0]);
1201 OUT_CS_32F(attrib->f[3]);
1202 OUT_CS_32F(attrib->f[2]);
1203 OUT_CS_32F(attrib->f[1]);
1204 }
1205
1206 /* Set up VAP controls. */
1207 OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1208 OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1209 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1210 OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1211 OUT_CS(1);
1212 OUT_CS(0);
1213
1214 /* Draw. */
1215 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1216 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1217 R300_VAP_VF_CNTL__PRIM_POINTS);
1218
1219 OUT_CS_32F(x1 + width * 0.5f);
1220 OUT_CS_32F(y1 + height * 0.5f);
1221 OUT_CS_32F(depth);
1222 OUT_CS_32F(1);
1223
1224 if (vertex_size == 8) {
1225 if (!attrib)
1226 attrib = &zeros;
1227 OUT_CS_TABLE(attrib->f, 4);
1228 }
1229 END_CS;
1230
1231 done:
1232 /* Restore the state. */
1233 r300_mark_atom_dirty(r300, &r300->clip_state);
1234 r300_mark_atom_dirty(r300, &r300->rs_state);
1235 r300_mark_atom_dirty(r300, &r300->viewport_state);
1236
1237 r300->sprite_coord_enable = last_sprite_coord_enable;
1238 }
1239
1240 static void r300_resource_resolve(struct pipe_context *pipe,
1241 const struct pipe_resolve_info *info)
1242 {
1243 struct r300_context *r300 = r300_context(pipe);
1244 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
1245 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1246 static const union pipe_color_union color;
1247
1248 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1249 surf_tmpl.format = info->src.res->format;
1250 surf_tmpl.u.tex.first_layer =
1251 surf_tmpl.u.tex.last_layer = info->src.layer;
1252 srcsurf = pipe->create_surface(pipe, info->src.res, &surf_tmpl);
1253 /* XXX Offset both surfaces by x0,y1. */
1254
1255 surf_tmpl.format = info->dst.res->format;
1256 surf_tmpl.u.tex.level = info->dst.level;
1257 surf_tmpl.u.tex.first_layer =
1258 surf_tmpl.u.tex.last_layer = info->dst.layer;
1259 dstsurf = pipe->create_surface(pipe, info->dst.res, &surf_tmpl);
1260
1261 DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
1262
1263 /* Enable AA resolve. */
1264 aa->dest = r300_surface(dstsurf);
1265 aa->aaresolve_ctl =
1266 R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
1267 R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
1268 r300->aa_state.size = 10;
1269 r300_mark_atom_dirty(r300, &r300->aa_state);
1270
1271 /* Resolve the surface. */
1272 /* XXX: y1 < 0 ==> Y flip */
1273 r300->context.clear_render_target(pipe,
1274 srcsurf, &color, 0, 0,
1275 info->dst.x1 - info->dst.x0,
1276 info->dst.y1 - info->dst.y0);
1277
1278 /* Disable AA resolve. */
1279 aa->aaresolve_ctl = 0;
1280 r300->aa_state.size = 4;
1281 r300_mark_atom_dirty(r300, &r300->aa_state);
1282
1283 pipe_surface_reference(&srcsurf, NULL);
1284 pipe_surface_reference(&dstsurf, NULL);
1285 }
1286
1287 void r300_init_render_functions(struct r300_context *r300)
1288 {
1289 /* Set draw functions based on presence of HW TCL. */
1290 if (r300->screen->caps.has_tcl) {
1291 r300->context.draw_vbo = r300_draw_vbo;
1292 } else {
1293 r300->context.draw_vbo = r300_swtcl_draw_vbo;
1294 }
1295
1296 r300->context.resource_resolve = r300_resource_resolve;
1297 r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
1298
1299 /* Plug in the two-sided stencil reference value fallback if needed. */
1300 if (!r300->screen->caps.is_r500)
1301 r300_plug_in_stencil_ref_fallback(r300);
1302 }