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