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