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