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