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