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