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