r300_render.c revision 2d03d4f4a365d7af5f4dac20700009152eba1682
13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * Copyright 2010 Marek Olšák <maraeo@gmail.com>
43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy *
53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * Permission is hereby granted, free of charge, to any person obtaining a
63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * copy of this software and associated documentation files (the "Software"),
73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * to deal in the Software without restriction, including without limitation
83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * on the rights to use, copy, modify, merge, publish, distribute, sub
93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * license, and/or sell copies of the Software, and to permit persons to whom
103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * the Software is furnished to do so, subject to the following conditions:
113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy *
123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * The above copyright notice and this permission notice (including the next
133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * paragraph) shall be included in all copies or substantial portions of the
143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * Software.
153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy *
163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
2045ef08fd6a09813e4a8f5ddadf85ba9e0ec2cdc7cristy * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * USE OR OTHER DEALINGS IN THE SOFTWARE. */
233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* r300_render: Vertex and index buffer primitive emission. Contains both
253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "draw/draw_context.h"
283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "draw/draw_vbuf.h"
293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "util/u_inlines.h"
313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "util/u_format.h"
333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "util/u_memory.h"
343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "util/u_upload_mgr.h"
353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "util/u_prim.h"
363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "r300_cs.h"
383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "r300_context.h"
393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "r300_screen_buffer.h"
403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "r300_emit.h"
413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "r300_reg.h"
424c08aed51c5899665ade97263692328eea4af106cristy
434c08aed51c5899665ade97263692328eea4af106cristy#include <limits.h>
4476ce6e193e5a305875173a744bbd59d27ddb3148cristy
454c08aed51c5899665ade97263692328eea4af106cristy#define IMMD_DWORDS 32
464c08aed51c5899665ade97263692328eea4af106cristy
478a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristystatic uint32_t r300_translate_primitive(unsigned prim)
484c08aed51c5899665ade97263692328eea4af106cristy{
498a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy    static const int prim_conv[] = {
504c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_POINTS,
514c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_LINES,
524c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_LINE_LOOP,
534c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_LINE_STRIP,
544c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_TRIANGLES,
554c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP,
564c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN,
578a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy        R300_VAP_VF_CNTL__PRIM_QUADS,
584c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_QUAD_STRIP,
594c08aed51c5899665ade97263692328eea4af106cristy        R300_VAP_VF_CNTL__PRIM_POLYGON,
604c08aed51c5899665ade97263692328eea4af106cristy        -1,
614c08aed51c5899665ade97263692328eea4af106cristy        -1,
624c08aed51c5899665ade97263692328eea4af106cristy        -1,
638a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy        -1
648a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy    };
653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned hwprim = prim_conv[prim];
663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    assert(hwprim != -1);
683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return hwprim;
693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
713ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                            unsigned mode)
733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    uint32_t color_control = rs->color_control;
763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* By default (see r300_state.c:r300_create_rs_state) color_control is
783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * initialized to provoking the first vertex.
793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     *
803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * Triangle fans must be reduced to the second vertex, not the first, in
813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * Gallium flatshade-first mode, as per the GL spec.
823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     *
843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * Quads never provoke correctly in flatshade-first mode. The first
853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * vertex is never considered as provoking, so only the second, third,
863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * and fourth vertices can be selected, and both "third" and "last" modes
873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * select the fourth vertex. This is probably due to D3D lacking quads.
883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     *
893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * Similarly, polygons reduce to the first, not the last, vertex, when in
903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * "last" mode, and all other modes start from the second vertex.
913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     *
923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * ~ C.
933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     */
943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (rs->rs.flatshade_first) {
963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        switch (mode) {
973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case PIPE_PRIM_TRIANGLE_FAN:
983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
9954ec42737fdec7527afa06ca95be82c8ca0bd866cristy                break;
10054ec42737fdec7527afa06ca95be82c8ca0bd866cristy            case PIPE_PRIM_QUADS:
1013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case PIPE_PRIM_QUAD_STRIP:
1023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case PIPE_PRIM_POLYGON:
103ab272ac4f115daf25c032de9dbb49a399eebe49bcristy                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
104ab272ac4f115daf25c032de9dbb49a399eebe49bcristy                break;
1053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            default:
1063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
1073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                break;
1083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
1093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    } else {
1103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
1113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return color_control;
1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
115bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy
1163ed852eea50f9d4cd633efb8c2b054b8e33c253cristyvoid r500_emit_index_bias(struct r300_context *r300, int index_bias)
1173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
118bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    CS_LOCALS(r300);
1193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BEGIN_CS(2);
1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    OUT_CS_REG(R500_VAP_INDEX_OFFSET,
1224e82e51d7ebce7b4ef0f808d906124dd6f812248cristy               (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
1234e82e51d7ebce7b4ef0f808d906124dd6f812248cristy    END_CS;
1244e82e51d7ebce7b4ef0f808d906124dd6f812248cristy}
1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1263ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic void r300_emit_draw_init(struct r300_context *r300, unsigned mode,
1273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                unsigned max_index)
1283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
1293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    CS_LOCALS(r300);
1303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    assert(max_index < (1 << 24));
1323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BEGIN_CS(5);
1343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    OUT_CS_REG(R300_GA_COLOR_CONTROL,
1359950d57e1124b73f684fb5946e206994cefda628cristy            r300_provoking_vertex_fixes(r300, mode));
1363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    OUT_CS(max_index);
1383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    OUT_CS(0);
1393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    END_CS;
1408b206bac411969018dc3a6d395f525f1664af412cristy}
141ab272ac4f115daf25c032de9dbb49a399eebe49bcristy
142ab272ac4f115daf25c032de9dbb49a399eebe49bcristy/* This function splits the index bias value into two parts:
143ab272ac4f115daf25c032de9dbb49a399eebe49bcristy * - buffer_offset: the value that can be safely added to buffer offsets
144ab272ac4f115daf25c032de9dbb49a399eebe49bcristy *   in r300_emit_vertex_arrays (it must yield a positive offset when added to
145ab272ac4f115daf25c032de9dbb49a399eebe49bcristy *   a vertex buffer offset)
146ab272ac4f115daf25c032de9dbb49a399eebe49bcristy * - index_offset: the value that must be manually subtracted from indices
147ab272ac4f115daf25c032de9dbb49a399eebe49bcristy *   in an index buffer to achieve negative offsets. */
148ab272ac4f115daf25c032de9dbb49a399eebe49bcristystatic void r300_split_index_bias(struct r300_context *r300, int index_bias,
149d15e65928aec551b7388c2863de3e3e628e2e0ddcristy                                  int *buffer_offset, int *index_offset)
1503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
151d15e65928aec551b7388c2863de3e3e628e2e0ddcristy    struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
1523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    struct pipe_vertex_element *velem = r300->velems->velem;
1536fc392931fb03ddff4853716af6d51e329f59696cristy    unsigned i, size;
1548b206bac411969018dc3a6d395f525f1664af412cristy    int max_neg_bias;
1553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
156042ee78fa9004bf1ac6a95f09d9d1faca631dda1cristy    if (index_bias < 0) {
1573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        /* See how large index bias we may subtract. We must be careful
1588db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy         * here because negative buffer offsets are not allowed
1598db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy         * by the DRM API. */
1608db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        max_neg_bias = INT_MAX;
1618db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        for (i = 0; i < r300->velems->count; i++) {
16254ec42737fdec7527afa06ca95be82c8ca0bd866cristy            vb = &vbufs[velem[i].vertex_buffer_index];
1639f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy            size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
16454ec42737fdec7527afa06ca95be82c8ca0bd866cristy            max_neg_bias = MIN2(max_neg_bias, size);
16554ec42737fdec7527afa06ca95be82c8ca0bd866cristy        }
16654ec42737fdec7527afa06ca95be82c8ca0bd866cristy
1678db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        /* Now set the minimum allowed value. */
1688db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        *buffer_offset = MAX2(-max_neg_bias, index_bias);
1698db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    } else {
1708db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        /* A positive index bias is OK. */
1718db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        *buffer_offset = index_bias;
1728db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    }
173a492593efccb08b72392d64966b5c7728e56f16acristy
1748db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    *index_offset = index_bias - *buffer_offset;
1758db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy}
176a492593efccb08b72392d64966b5c7728e56f16acristy
177b712c6e549e8dbd76a125beb14182d41cc8027eecristyenum r300_prepare_flags {
178a492593efccb08b72392d64966b5c7728e56f16acristy    PREP_EMIT_STATES    = (1 << 0), /* call emit_dirty_state and friends? */
1798db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    PREP_VALIDATE_VBOS  = (1 << 1), /* validate VBOs? */
1808db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    PREP_EMIT_VARRAYS       = (1 << 2), /* call emit_vertex_arrays? */
1818db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    PREP_EMIT_VARRAYS_SWTCL = (1 << 3), /* call emit_vertex_arrays_swtcl? */
1828db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    PREP_INDEXED        = (1 << 4)  /* is this draw_elements? */
1838db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy};
1848db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy
18554ec42737fdec7527afa06ca95be82c8ca0bd866cristy/**
1869f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy * Check if the requested number of dwords is available in the CS and
18754ec42737fdec7527afa06ca95be82c8ca0bd866cristy * if not, flush.
18854ec42737fdec7527afa06ca95be82c8ca0bd866cristy * \param r300          The context.
18954ec42737fdec7527afa06ca95be82c8ca0bd866cristy * \param flags         See r300_prepare_flags.
1908db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy * \param cs_dwords     The number of dwords to reserve in CS.
1918db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy * \return TRUE if the CS was flushed
1928db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy */
1938db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristystatic boolean r300_reserve_cs_dwords(struct r300_context *r300,
1948db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy                                      enum r300_prepare_flags flags,
1958db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy                                      unsigned cs_dwords)
1968db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy{
19754ec42737fdec7527afa06ca95be82c8ca0bd866cristy    boolean flushed        = FALSE;
1988db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    boolean emit_states    = flags & PREP_EMIT_STATES;
1998db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy    boolean emit_vertex_arrays       = flags & PREP_EMIT_VARRAYS;
200a492593efccb08b72392d64966b5c7728e56f16acristy    boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;
201a492593efccb08b72392d64966b5c7728e56f16acristy
20254ec42737fdec7527afa06ca95be82c8ca0bd866cristy    /* Add dirty state, index offset, and AOS. */
20354ec42737fdec7527afa06ca95be82c8ca0bd866cristy    if (emit_states)
2048db9ca45d3b9861f5b18cdc760c8e8f0f157b270cristy        cs_dwords += r300_get_num_dirty_dwords(r300);
205a492593efccb08b72392d64966b5c7728e56f16acristy
2063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (r300->screen->caps.is_r500)
207bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        cs_dwords += 2; /* emit_index_offset */
208bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy
209bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy    if (emit_vertex_arrays)
210bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        cs_dwords += 55; /* emit_vertex_arrays */
2113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
212bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy    if (emit_vertex_arrays_swtcl)
2133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        cs_dwords += 7; /* emit_vertex_arrays_swtcl */
2148592b059b2b4f03c0280b0e1146b225207e624f8cristy
2153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    cs_dwords += r300_get_num_cs_end_dwords(r300);
2163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2179f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy    /* Reserve requested CS space. */
2185cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy    if (cs_dwords > (RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
2193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        r300_flush(&r300->context, RADEON_FLUSH_ASYNC, NULL);
2203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        flushed = TRUE;
221b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy    }
2223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return flushed;
2243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
2255cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy
226bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy/**
2274e82e51d7ebce7b4ef0f808d906124dd6f812248cristy * Validate buffers and emit dirty state.
22854ec42737fdec7527afa06ca95be82c8ca0bd866cristy * \param r300          The context.
2293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * \param flags         See r300_prepare_flags.
23054ec42737fdec7527afa06ca95be82c8ca0bd866cristy * \param index_buffer  The index buffer to validate. The parameter may be NULL.
2318b235b2d0ea1bce8f9743d2d25bd6ddc9f72ff1dcristy * \param buffer_offset The offset passed to emit_vertex_arrays.
2323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * \param index_bias    The index bias to emit.
233bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy * \param instance_id   Index of instance to render
2348592b059b2b4f03c0280b0e1146b225207e624f8cristy * \return TRUE if rendering should be skipped
2353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy */
236bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristystatic boolean r300_emit_states(struct r300_context *r300,
2373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                enum r300_prepare_flags flags,
2389f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy                                struct pipe_resource *index_buffer,
2395cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy                                int buffer_offset,
2403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                int index_bias, int instance_id)
2413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
242b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy    boolean emit_states    = flags & PREP_EMIT_STATES;
2433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    boolean emit_vertex_arrays       = flags & PREP_EMIT_VARRAYS;
2443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    boolean emit_vertex_arrays_swtcl = flags & PREP_EMIT_VARRAYS_SWTCL;
2453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    boolean indexed        = flags & PREP_INDEXED;
2465cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy    boolean validate_vbos  = flags & PREP_VALIDATE_VBOS;
247bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy
2484e82e51d7ebce7b4ef0f808d906124dd6f812248cristy    /* Validate buffers and emit dirty state if needed. */
24942f5a6a8af8b1e4fd5f341e709c4d06778b8d85fcristy    if (emit_states || (emit_vertex_arrays && validate_vbos)) {
250bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy        if (!r300_emit_buffer_validate(r300, validate_vbos,
251bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy                                       index_buffer)) {
252bdf24b20c5b02c773580a7b8cbf4ae8bb11ef994cristy           fprintf(stderr, "r300: CS space validation failed. "
2533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                   "(not enough memory?) Skipping rendering.\n");
2546cd29479f50f920eb9e65e97ba682037204f7708cristy           return FALSE;
25517386ff059907b275aeb762297179b2d26d95a33cristy        }
25617386ff059907b275aeb762297179b2d26d95a33cristy    }
2579f92e3cee3ca2d153ccb77ee02fd27eed563021ecristy
25817386ff059907b275aeb762297179b2d26d95a33cristy    if (emit_states)
25917386ff059907b275aeb762297179b2d26d95a33cristy        r300_emit_dirty_state(r300);
26017386ff059907b275aeb762297179b2d26d95a33cristy
26117386ff059907b275aeb762297179b2d26d95a33cristy    if (r300->screen->caps.is_r500) {
26217386ff059907b275aeb762297179b2d26d95a33cristy        if (r300->screen->caps.has_tcl)
26317386ff059907b275aeb762297179b2d26d95a33cristy            r500_emit_index_bias(r300, index_bias);
26417386ff059907b275aeb762297179b2d26d95a33cristy        else
26517386ff059907b275aeb762297179b2d26d95a33cristy            r500_emit_index_bias(r300, 0);
26617386ff059907b275aeb762297179b2d26d95a33cristy    }
26717386ff059907b275aeb762297179b2d26d95a33cristy
26817386ff059907b275aeb762297179b2d26d95a33cristy    if (emit_vertex_arrays &&
26917386ff059907b275aeb762297179b2d26d95a33cristy        (r300->vertex_arrays_dirty ||
2706cd29479f50f920eb9e65e97ba682037204f7708cristy         r300->vertex_arrays_indexed != indexed ||
27117386ff059907b275aeb762297179b2d26d95a33cristy         r300->vertex_arrays_offset != buffer_offset ||
272ec0bf4bde1f4ae360eaad31b98fb9ffe06d3b35dcristy         r300->vertex_arrays_instance_id != instance_id)) {
2733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        r300_emit_vertex_arrays(r300, buffer_offset, indexed, instance_id);
274a492593efccb08b72392d64966b5c7728e56f16acristy
275a492593efccb08b72392d64966b5c7728e56f16acristy        r300->vertex_arrays_dirty = FALSE;
276a492593efccb08b72392d64966b5c7728e56f16acristy        r300->vertex_arrays_indexed = indexed;
277ea1a8aa04a9fe1500104284407c1cc06d20da699cristy        r300->vertex_arrays_offset = buffer_offset;
27895524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy        r300->vertex_arrays_instance_id = instance_id;
27995524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy    }
28095524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy
28195524f92fc346f548f202d36ffc6a17fdbd1b1cbcristy    if (emit_vertex_arrays_swtcl)
2823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        r300_emit_vertex_arrays_swtcl(r300, indexed);
2833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return TRUE;
2853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
2865cbc016effaa2d7ee617f46ca0a2371533d4ae17cristy
287024843f38984cd26602f0b3b28a0768dfa056bb5cristy/**
288024843f38984cd26602f0b3b28a0768dfa056bb5cristy * Check if the requested number of dwords is available in the CS and
289bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy * if not, flush. Then validate buffers and emit dirty state.
2903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * \param r300          The context.
2913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * \param flags         See r300_prepare_flags.
292b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy * \param index_buffer  The index buffer to validate. The parameter may be NULL.
2933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * \param cs_dwords     The number of dwords to reserve in CS.
2943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy * \param buffer_offset The offset passed to emit_vertex_arrays.
295024843f38984cd26602f0b3b28a0768dfa056bb5cristy * \param index_bias    The index bias to emit.
296024843f38984cd26602f0b3b28a0768dfa056bb5cristy * \param instance_id The instance to render.
297024843f38984cd26602f0b3b28a0768dfa056bb5cristy * \return TRUE if rendering should be skipped
298024843f38984cd26602f0b3b28a0768dfa056bb5cristy */
299024843f38984cd26602f0b3b28a0768dfa056bb5cristystatic boolean r300_prepare_for_rendering(struct r300_context *r300,
3003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                          enum r300_prepare_flags flags,
301ab272ac4f115daf25c032de9dbb49a399eebe49bcristy                                          struct pipe_resource *index_buffer,
3023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                          unsigned cs_dwords,
3033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                          int buffer_offset,
304ab272ac4f115daf25c032de9dbb49a399eebe49bcristy                                          int index_bias,
305ab272ac4f115daf25c032de9dbb49a399eebe49bcristy                                          int instance_id)
306ab272ac4f115daf25c032de9dbb49a399eebe49bcristy{
307ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    /* Make sure there is enough space in the command stream and emit states. */
308ab272ac4f115daf25c032de9dbb49a399eebe49bcristy    if (r300_reserve_cs_dwords(r300, flags, cs_dwords))
3093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        flags |= PREP_EMIT_STATES;
3103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return r300_emit_states(r300, flags, index_buffer, buffer_offset,
3123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                            index_bias, instance_id);
3133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
3143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3153ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic boolean immd_is_good_idea(struct r300_context *r300,
3163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                 unsigned count)
3173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
3183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (DBG_ON(r300, DBG_NO_IMMD)) {
3193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        return FALSE;
3203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
3213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (count * r300->velems->vertex_size_dwords > IMMD_DWORDS) {
3233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        return FALSE;
3243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
3253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Buffers can only be used for read by r300 (except query buffers, but
3273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * those can't be bound by a state tracker as vertex buffers). */
3283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return TRUE;
3293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
3303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*****************************************************************************
332bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy * The HWTCL draw functions.                                                 *
3333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy ****************************************************************************/
3343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
335bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristystatic void r300_draw_arrays_immediate(struct r300_context *r300,
3363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                       const struct pipe_draw_info *info)
3373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
3383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    struct pipe_vertex_element* velem;
3393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    struct pipe_vertex_buffer* vbuf;
3403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned vertex_element_count = r300->velems->count;
3413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned i, v, vbi;
3428a3ce7f37eeb9deabe9134cb75cd69e7dae75301cristy
3433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Size of the vertex, in dwords. */
3443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned vertex_size = r300->velems->vertex_size_dwords;
3453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* The number of dwords for this draw operation. */
3473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned dwords = 4 + info->count * vertex_size;
3483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Size of the vertex element, in dwords. */
3503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned size[PIPE_MAX_ATTRIBS];
3513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Stride to the same attrib in the next vertex in the vertex buffer,
3533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy     * in dwords. */
3543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned stride[PIPE_MAX_ATTRIBS];
3553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Mapped vertex buffers. */
3573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
3583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    uint32_t* mapelem[PIPE_MAX_ATTRIBS];
3593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    CS_LOCALS(r300);
3613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
3633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        return;
3643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
3663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    for (i = 0; i < vertex_element_count; i++) {
3673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        velem = &r300->velems->velem[i];
3683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        size[i] = r300->velems->format_size[i] / 4;
3693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        vbi = velem->vertex_buffer_index;
3703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        vbuf = &r300->vertex_buffer[vbi];
3713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        stride[i] = vbuf->stride / 4;
372
373        /* Map the buffer. */
374        if (!map[vbi]) {
375            map[vbi] = (uint32_t*)r300->rws->buffer_map(
376                r300_resource(vbuf->buffer)->buf,
377                r300->cs, PIPE_TRANSFER_READ | PIPE_TRANSFER_UNSYNCHRONIZED);
378            map[vbi] += (vbuf->buffer_offset / 4) + stride[i] * info->start;
379        }
380        mapelem[i] = map[vbi] + (velem->src_offset / 4);
381    }
382
383    r300_emit_draw_init(r300, info->mode, info->count-1);
384
385    BEGIN_CS(dwords);
386    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
387    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, info->count * vertex_size);
388    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (info->count << 16) |
389            r300_translate_primitive(info->mode));
390
391    /* Emit vertices. */
392    for (v = 0; v < info->count; v++) {
393        for (i = 0; i < vertex_element_count; i++) {
394            OUT_CS_TABLE(&mapelem[i][stride[i] * v], size[i]);
395        }
396    }
397    END_CS;
398
399    /* Unmap buffers. */
400    for (i = 0; i < vertex_element_count; i++) {
401        vbi = r300->velems->velem[i].vertex_buffer_index;
402
403        if (map[vbi]) {
404            r300->rws->buffer_unmap(r300_resource(r300->vertex_buffer[vbi].buffer)->buf);
405            map[vbi] = NULL;
406        }
407    }
408}
409
410static void r300_emit_draw_arrays(struct r300_context *r300,
411                                  unsigned mode,
412                                  unsigned count)
413{
414    boolean alt_num_verts = count > 65535;
415    CS_LOCALS(r300);
416
417    if (count >= (1 << 24)) {
418        fprintf(stderr, "r300: Got a huge number of vertices: %i, "
419                "refusing to render.\n", count);
420        return;
421    }
422
423    r300_emit_draw_init(r300, mode, count-1);
424
425    BEGIN_CS(2 + (alt_num_verts ? 2 : 0));
426    if (alt_num_verts) {
427        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
428    }
429    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
430    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
431           r300_translate_primitive(mode) |
432           (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
433    END_CS;
434}
435
436static void r300_emit_draw_elements(struct r300_context *r300,
437                                    struct pipe_resource* indexBuffer,
438                                    unsigned indexSize,
439                                    unsigned max_index,
440                                    unsigned mode,
441                                    unsigned start,
442                                    unsigned count,
443                                    uint16_t *imm_indices3)
444{
445    uint32_t count_dwords, offset_dwords;
446    boolean alt_num_verts = count > 65535;
447    CS_LOCALS(r300);
448
449    if (count >= (1 << 24)) {
450        fprintf(stderr, "r300: Got a huge number of vertices: %i, "
451                "refusing to render (max_index: %i).\n", count, max_index);
452        return;
453    }
454
455    DBG(r300, DBG_DRAW, "r300: Indexbuf of %u indices, max %u\n",
456        count, max_index);
457
458    r300_emit_draw_init(r300, mode, max_index);
459
460    /* If start is odd, render the first triangle with indices embedded
461     * in the command stream. This will increase start by 3 and make it
462     * even. We can then proceed without a fallback. */
463    if (indexSize == 2 && (start & 1) &&
464        mode == PIPE_PRIM_TRIANGLES) {
465        BEGIN_CS(4);
466        OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 2);
467        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (3 << 16) |
468               R300_VAP_VF_CNTL__PRIM_TRIANGLES);
469        OUT_CS(imm_indices3[1] << 16 | imm_indices3[0]);
470        OUT_CS(imm_indices3[2]);
471        END_CS;
472
473        start += 3;
474        count -= 3;
475        if (!count)
476           return;
477    }
478
479    offset_dwords = indexSize * start / sizeof(uint32_t);
480
481    BEGIN_CS(8 + (alt_num_verts ? 2 : 0));
482    if (alt_num_verts) {
483        OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
484    }
485    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
486    if (indexSize == 4) {
487        count_dwords = count;
488        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
489               R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
490               r300_translate_primitive(mode) |
491               (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
492    } else {
493        count_dwords = (count + 1) / 2;
494        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
495               r300_translate_primitive(mode) |
496               (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
497    }
498
499    OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
500    OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
501           (0 << R300_INDX_BUFFER_SKIP_SHIFT));
502    OUT_CS(offset_dwords << 2);
503    OUT_CS(count_dwords);
504    OUT_CS_RELOC(r300_resource(indexBuffer));
505    END_CS;
506}
507
508static void r300_draw_elements_immediate(struct r300_context *r300,
509                                         const struct pipe_draw_info *info)
510{
511    uint8_t *ptr1;
512    uint16_t *ptr2;
513    uint32_t *ptr4;
514    unsigned index_size = r300->index_buffer.index_size;
515    unsigned i, count_dwords = index_size == 4 ? info->count :
516                                                 (info->count + 1) / 2;
517    CS_LOCALS(r300);
518
519    /* 19 dwords for r300_draw_elements_immediate. Give up if the function fails. */
520    if (!r300_prepare_for_rendering(r300,
521            PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |
522            PREP_INDEXED, NULL, 2+count_dwords, 0, info->index_bias, -1))
523        return;
524
525    r300_emit_draw_init(r300, info->mode, info->max_index);
526
527    BEGIN_CS(2 + count_dwords);
528    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, count_dwords);
529
530    switch (index_size) {
531    case 1:
532        ptr1 = r300_resource(r300->index_buffer.buffer)->b.user_ptr;
533        ptr1 += info->start;
534
535        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
536               r300_translate_primitive(info->mode));
537
538        if (info->index_bias && !r300->screen->caps.is_r500) {
539            for (i = 0; i < info->count-1; i += 2)
540                OUT_CS(((ptr1[i+1] + info->index_bias) << 16) |
541                        (ptr1[i]   + info->index_bias));
542
543            if (info->count & 1)
544                OUT_CS(ptr1[i] + info->index_bias);
545        } else {
546            for (i = 0; i < info->count-1; i += 2)
547                OUT_CS(((ptr1[i+1]) << 16) |
548                        (ptr1[i]  ));
549
550            if (info->count & 1)
551                OUT_CS(ptr1[i]);
552        }
553        break;
554
555    case 2:
556        ptr2 = (uint16_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
557        ptr2 += info->start;
558
559        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
560               r300_translate_primitive(info->mode));
561
562        if (info->index_bias && !r300->screen->caps.is_r500) {
563            for (i = 0; i < info->count-1; i += 2)
564                OUT_CS(((ptr2[i+1] + info->index_bias) << 16) |
565                        (ptr2[i]   + info->index_bias));
566
567            if (info->count & 1)
568                OUT_CS(ptr2[i] + info->index_bias);
569        } else {
570            OUT_CS_TABLE(ptr2, count_dwords);
571        }
572        break;
573
574    case 4:
575        ptr4 = (uint32_t*)r300_resource(r300->index_buffer.buffer)->b.user_ptr;
576        ptr4 += info->start;
577
578        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (info->count << 16) |
579               R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
580               r300_translate_primitive(info->mode));
581
582        if (info->index_bias && !r300->screen->caps.is_r500) {
583            for (i = 0; i < info->count; i++)
584                OUT_CS(ptr4[i] + info->index_bias);
585        } else {
586            OUT_CS_TABLE(ptr4, count_dwords);
587        }
588        break;
589    }
590    END_CS;
591}
592
593static void r300_draw_elements(struct r300_context *r300,
594                               const struct pipe_draw_info *info,
595                               int instance_id)
596{
597    struct pipe_resource *indexBuffer = r300->index_buffer.buffer;
598    unsigned indexSize = r300->index_buffer.index_size;
599    struct pipe_resource* orgIndexBuffer = indexBuffer;
600    unsigned start = info->start;
601    unsigned count = info->count;
602    boolean alt_num_verts = r300->screen->caps.is_r500 &&
603                            count > 65536;
604    unsigned short_count;
605    int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
606    uint16_t indices3[3];
607
608    if (info->index_bias && !r300->screen->caps.is_r500) {
609        r300_split_index_bias(r300, info->index_bias, &buffer_offset, &index_offset);
610    }
611
612    r300_translate_index_buffer(r300, &indexBuffer, &indexSize, index_offset,
613                                &start, count);
614
615    /* Fallback for misaligned ushort indices. */
616    if (indexSize == 2 && (start & 1) &&
617        !r300_resource(indexBuffer)->b.user_ptr) {
618        /* If we got here, then orgIndexBuffer == indexBuffer. */
619        uint16_t *ptr = r300->rws->buffer_map(r300_resource(orgIndexBuffer)->buf,
620                                              r300->cs,
621                                              PIPE_TRANSFER_READ |
622                                              PIPE_TRANSFER_UNSYNCHRONIZED);
623
624        if (info->mode == PIPE_PRIM_TRIANGLES) {
625           memcpy(indices3, ptr + start, 6);
626        } else {
627            /* Copy the mapped index buffer directly to the upload buffer.
628             * The start index will be aligned simply from the fact that
629             * every sub-buffer in the upload buffer is aligned. */
630            r300_upload_index_buffer(r300, &indexBuffer, indexSize, &start,
631                                     count, (uint8_t*)ptr);
632        }
633        r300->rws->buffer_unmap(r300_resource(orgIndexBuffer)->buf);
634    } else {
635        if (r300_resource(indexBuffer)->b.user_ptr)
636            r300_upload_index_buffer(r300, &indexBuffer, indexSize,
637                                     &start, count,
638                                     r300_resource(indexBuffer)->b.user_ptr);
639    }
640
641    /* 19 dwords for emit_draw_elements. Give up if the function fails. */
642    if (!r300_prepare_for_rendering(r300,
643            PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS |
644            PREP_INDEXED, indexBuffer, 19, buffer_offset, info->index_bias,
645            instance_id))
646        goto done;
647
648    if (alt_num_verts || count <= 65535) {
649        r300_emit_draw_elements(r300, indexBuffer, indexSize,
650                                info->max_index, info->mode, start, count,
651                                indices3);
652    } else {
653        do {
654            /* The maximum must be divisible by 4 and 3,
655             * so that quad and triangle lists are split correctly.
656             *
657             * Strips, loops, and fans won't work. */
658            short_count = MIN2(count, 65532);
659
660            r300_emit_draw_elements(r300, indexBuffer, indexSize,
661                                     info->max_index,
662                                     info->mode, start, short_count, indices3);
663
664            start += short_count;
665            count -= short_count;
666
667            /* 15 dwords for emit_draw_elements */
668            if (count) {
669                if (!r300_prepare_for_rendering(r300,
670                        PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS | PREP_INDEXED,
671                        indexBuffer, 19, buffer_offset, info->index_bias,
672                        instance_id))
673                    goto done;
674            }
675        } while (count);
676    }
677
678done:
679    if (indexBuffer != orgIndexBuffer) {
680        pipe_resource_reference( &indexBuffer, NULL );
681    }
682}
683
684static void r300_draw_arrays(struct r300_context *r300,
685                             const struct pipe_draw_info *info,
686                             int instance_id)
687{
688    boolean alt_num_verts = r300->screen->caps.is_r500 &&
689                            info->count > 65536;
690    unsigned start = info->start;
691    unsigned count = info->count;
692    unsigned short_count;
693
694    /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
695    if (!r300_prepare_for_rendering(r300,
696                                    PREP_EMIT_STATES | PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS,
697                                    NULL, 9, start, 0, instance_id))
698        return;
699
700    if (alt_num_verts || count <= 65535) {
701        r300_emit_draw_arrays(r300, info->mode, count);
702    } else {
703        do {
704            /* The maximum must be divisible by 4 and 3,
705             * so that quad and triangle lists are split correctly.
706             *
707             * Strips, loops, and fans won't work. */
708            short_count = MIN2(count, 65532);
709            r300_emit_draw_arrays(r300, info->mode, short_count);
710
711            start += short_count;
712            count -= short_count;
713
714            /* 9 spare dwords for emit_draw_arrays. Give up if the function fails. */
715            if (count) {
716                if (!r300_prepare_for_rendering(r300,
717                                                PREP_VALIDATE_VBOS | PREP_EMIT_VARRAYS, NULL, 9,
718                                                start, 0, instance_id))
719                    return;
720            }
721        } while (count);
722    }
723}
724
725static void r300_draw_arrays_instanced(struct r300_context *r300,
726                                       const struct pipe_draw_info *info)
727{
728    int i;
729
730    for (i = 0; i < info->instance_count; i++)
731        r300_draw_arrays(r300, info, i);
732}
733
734static void r300_draw_elements_instanced(struct r300_context *r300,
735                                         const struct pipe_draw_info *info)
736{
737    int i;
738
739    for (i = 0; i < info->instance_count; i++)
740        r300_draw_elements(r300, info, i);
741}
742
743static void r300_draw_vbo(struct pipe_context* pipe,
744                          const struct pipe_draw_info *dinfo)
745{
746    struct r300_context* r300 = r300_context(pipe);
747    struct pipe_draw_info info = *dinfo;
748
749    info.indexed = info.indexed && r300->index_buffer.buffer;
750
751    if (r300->skip_rendering ||
752        !u_trim_pipe_prim(info.mode, &info.count)) {
753        return;
754    }
755
756    r300_update_derived_state(r300);
757    u_vbuf_draw_begin(r300->vbuf_mgr, &info);
758
759    /* Draw. */
760    if (info.indexed) {
761        unsigned max_count = u_vbuf_draw_max_vertex_count(r300->vbuf_mgr);
762
763        if (!max_count) {
764           fprintf(stderr, "r300: Skipping a draw command. There is a buffer "
765                   " which is too small to be used for rendering.\n");
766           goto done;
767        }
768
769        if (max_count == ~0) {
770           /* There are no per-vertex vertex elements. Use the hardware maximum. */
771           max_count = 0xffffff;
772        }
773
774        info.max_index = max_count - 1;
775        info.start += r300->index_buffer.offset / r300->index_buffer.index_size;
776
777        if (info.instance_count <= 1) {
778            if (info.count <= 8 &&
779                r300_resource(r300->index_buffer.buffer)->b.user_ptr) {
780                r300_draw_elements_immediate(r300, &info);
781            } else {
782                r300_draw_elements(r300, &info, -1);
783            }
784        } else {
785            r300_draw_elements_instanced(r300, &info);
786        }
787    } else {
788        if (info.instance_count <= 1) {
789            if (immd_is_good_idea(r300, info.count)) {
790                r300_draw_arrays_immediate(r300, &info);
791            } else {
792                r300_draw_arrays(r300, &info, -1);
793            }
794        } else {
795            r300_draw_arrays_instanced(r300, &info);
796        }
797    }
798
799done:
800    u_vbuf_draw_end(r300->vbuf_mgr);
801}
802
803/****************************************************************************
804 * The rest of this file is for SW TCL rendering only. Please be polite and *
805 * keep these functions separated so that they are easier to locate. ~C.    *
806 ***************************************************************************/
807
808/* SW TCL elements, using Draw. */
809static void r300_swtcl_draw_vbo(struct pipe_context* pipe,
810                                const struct pipe_draw_info *info)
811{
812    struct r300_context* r300 = r300_context(pipe);
813    struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
814    struct pipe_transfer *ib_transfer = NULL;
815    int i;
816    void *indices = NULL;
817    boolean indexed = info->indexed && r300->index_buffer.buffer;
818
819    if (r300->skip_rendering) {
820        return;
821    }
822
823    r300_update_derived_state(r300);
824
825    r300_reserve_cs_dwords(r300,
826            PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL |
827            (indexed ? PREP_INDEXED : 0),
828            indexed ? 256 : 6);
829
830    for (i = 0; i < r300->nr_vertex_buffers; i++) {
831        if (r300->vertex_buffer[i].buffer) {
832            void *buf = pipe_buffer_map(pipe,
833                                  r300->vertex_buffer[i].buffer,
834                                  PIPE_TRANSFER_READ |
835                                  PIPE_TRANSFER_UNSYNCHRONIZED,
836                                  &vb_transfer[i]);
837            draw_set_mapped_vertex_buffer(r300->draw, i, buf);
838        }
839    }
840
841    if (indexed) {
842        indices = pipe_buffer_map(pipe, r300->index_buffer.buffer,
843                                  PIPE_TRANSFER_READ |
844                                  PIPE_TRANSFER_UNSYNCHRONIZED, &ib_transfer);
845    }
846
847    draw_set_mapped_index_buffer(r300->draw, indices);
848
849    r300->draw_vbo_locked = TRUE;
850    r300->draw_first_emitted = FALSE;
851    draw_vbo(r300->draw, info);
852    draw_flush(r300->draw);
853    r300->draw_vbo_locked = FALSE;
854
855    for (i = 0; i < r300->nr_vertex_buffers; i++) {
856        if (r300->vertex_buffer[i].buffer) {
857            pipe_buffer_unmap(pipe, vb_transfer[i]);
858            draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
859        }
860    }
861
862    if (indexed) {
863        pipe_buffer_unmap(pipe, ib_transfer);
864        draw_set_mapped_index_buffer(r300->draw, NULL);
865    }
866}
867
868/* Object for rendering using Draw. */
869struct r300_render {
870    /* Parent class */
871    struct vbuf_render base;
872
873    /* Pipe context */
874    struct r300_context* r300;
875
876    /* Vertex information */
877    size_t vertex_size;
878    unsigned prim;
879    unsigned hwprim;
880
881    /* VBO */
882    size_t vbo_max_used;
883    void * vbo_ptr;
884
885    struct pipe_transfer *vbo_transfer;
886};
887
888static INLINE struct r300_render*
889r300_render(struct vbuf_render* render)
890{
891    return (struct r300_render*)render;
892}
893
894static const struct vertex_info*
895r300_render_get_vertex_info(struct vbuf_render* render)
896{
897    struct r300_render* r300render = r300_render(render);
898    struct r300_context* r300 = r300render->r300;
899
900    return &r300->vertex_info;
901}
902
903static boolean r300_render_allocate_vertices(struct vbuf_render* render,
904                                                   ushort vertex_size,
905                                                   ushort count)
906{
907    struct r300_render* r300render = r300_render(render);
908    struct r300_context* r300 = r300render->r300;
909    struct pipe_screen* screen = r300->context.screen;
910    size_t size = (size_t)vertex_size * (size_t)count;
911
912    DBG(r300, DBG_DRAW, "r300: render_allocate_vertices (size: %d)\n", size);
913
914    if (size + r300->draw_vbo_offset > r300->draw_vbo_size)
915    {
916	pipe_resource_reference(&r300->vbo, NULL);
917        r300->vbo = pipe_buffer_create(screen,
918				       PIPE_BIND_VERTEX_BUFFER,
919				       PIPE_USAGE_STREAM,
920				       R300_MAX_DRAW_VBO_SIZE);
921        r300->draw_vbo_offset = 0;
922        r300->draw_vbo_size = R300_MAX_DRAW_VBO_SIZE;
923    }
924
925    r300render->vertex_size = vertex_size;
926
927    return (r300->vbo) ? TRUE : FALSE;
928}
929
930static void* r300_render_map_vertices(struct vbuf_render* render)
931{
932    struct r300_render* r300render = r300_render(render);
933    struct r300_context* r300 = r300render->r300;
934
935    assert(!r300render->vbo_transfer);
936
937    DBG(r300, DBG_DRAW, "r300: render_map_vertices\n");
938
939    r300render->vbo_ptr = pipe_buffer_map(&r300render->r300->context,
940					  r300->vbo,
941                                          PIPE_TRANSFER_WRITE |
942                                          PIPE_TRANSFER_UNSYNCHRONIZED,
943					  &r300render->vbo_transfer);
944
945    assert(r300render->vbo_ptr);
946
947    return ((uint8_t*)r300render->vbo_ptr + r300->draw_vbo_offset);
948}
949
950static void r300_render_unmap_vertices(struct vbuf_render* render,
951                                             ushort min,
952                                             ushort max)
953{
954    struct r300_render* r300render = r300_render(render);
955    struct pipe_context* context = &r300render->r300->context;
956    struct r300_context* r300 = r300render->r300;
957
958    assert(r300render->vbo_transfer);
959
960    DBG(r300, DBG_DRAW, "r300: render_unmap_vertices\n");
961
962    r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
963                                    r300render->vertex_size * (max + 1));
964    pipe_buffer_unmap(context, r300render->vbo_transfer);
965
966    r300render->vbo_transfer = NULL;
967}
968
969static void r300_render_release_vertices(struct vbuf_render* render)
970{
971    struct r300_render* r300render = r300_render(render);
972    struct r300_context* r300 = r300render->r300;
973
974    DBG(r300, DBG_DRAW, "r300: render_release_vertices\n");
975
976    r300->draw_vbo_offset += r300render->vbo_max_used;
977    r300render->vbo_max_used = 0;
978}
979
980static void r300_render_set_primitive(struct vbuf_render* render,
981                                      unsigned prim)
982{
983    struct r300_render* r300render = r300_render(render);
984
985    r300render->prim = prim;
986    r300render->hwprim = r300_translate_primitive(prim);
987}
988
989static void r300_render_draw_arrays(struct vbuf_render* render,
990                                    unsigned start,
991                                    unsigned count)
992{
993    struct r300_render* r300render = r300_render(render);
994    struct r300_context* r300 = r300render->r300;
995    uint8_t* ptr;
996    unsigned i;
997    unsigned dwords = 6;
998
999    CS_LOCALS(r300);
1000    (void) i; (void) ptr;
1001
1002    DBG(r300, DBG_DRAW, "r300: render_draw_arrays (count: %d)\n", count);
1003
1004    if (r300->draw_first_emitted) {
1005        if (!r300_prepare_for_rendering(r300,
1006                PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,
1007                NULL, dwords, 0, 0, -1))
1008            return;
1009    } else {
1010        if (!r300_emit_states(r300,
1011                PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL,
1012                NULL, 0, 0, -1))
1013            return;
1014    }
1015
1016    BEGIN_CS(dwords);
1017    OUT_CS_REG(R300_GA_COLOR_CONTROL,
1018            r300_provoking_vertex_fixes(r300, r300render->prim));
1019    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
1020    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
1021    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
1022           r300render->hwprim);
1023    END_CS;
1024
1025    r300->draw_first_emitted = TRUE;
1026}
1027
1028static void r300_render_draw_elements(struct vbuf_render* render,
1029                                      const ushort* indices,
1030                                      uint count)
1031{
1032    struct r300_render* r300render = r300_render(render);
1033    struct r300_context* r300 = r300render->r300;
1034    int i;
1035    unsigned end_cs_dwords;
1036    unsigned max_index = (r300->draw_vbo_size - r300->draw_vbo_offset) /
1037                         (r300render->r300->vertex_info.size * 4) - 1;
1038    unsigned short_count;
1039    unsigned free_dwords;
1040
1041    CS_LOCALS(r300);
1042    DBG(r300, DBG_DRAW, "r300: render_draw_elements (count: %d)\n", count);
1043
1044    if (r300->draw_first_emitted) {
1045        if (!r300_prepare_for_rendering(r300,
1046                PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1047                NULL, 256, 0, 0, -1))
1048            return;
1049    } else {
1050        if (!r300_emit_states(r300,
1051                PREP_EMIT_STATES | PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1052                NULL, 0, 0, -1))
1053            return;
1054    }
1055
1056    /* Below we manage the CS space manually because there may be more
1057     * indices than it can fit in CS. */
1058
1059    end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1060
1061    while (count) {
1062        free_dwords = RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw;
1063
1064        short_count = MIN2(count, (free_dwords - end_cs_dwords - 6) * 2);
1065
1066        BEGIN_CS(6 + (short_count+1)/2);
1067        OUT_CS_REG(R300_GA_COLOR_CONTROL,
1068                r300_provoking_vertex_fixes(r300, r300render->prim));
1069        OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max_index);
1070        OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (short_count+1)/2);
1071        OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (short_count << 16) |
1072               r300render->hwprim);
1073        for (i = 0; i < short_count-1; i += 2) {
1074            OUT_CS(indices[i+1] << 16 | indices[i]);
1075        }
1076        if (short_count % 2) {
1077            OUT_CS(indices[short_count-1]);
1078        }
1079        END_CS;
1080
1081        /* OK now subtract the emitted indices and see if we need to emit
1082         * another draw packet. */
1083        indices += short_count;
1084        count -= short_count;
1085
1086        if (count) {
1087            if (!r300_prepare_for_rendering(r300,
1088                    PREP_EMIT_VARRAYS_SWTCL | PREP_INDEXED,
1089                    NULL, 256, 0, 0, -1))
1090                return;
1091
1092            end_cs_dwords = r300_get_num_cs_end_dwords(r300);
1093        }
1094    }
1095
1096    r300->draw_first_emitted = TRUE;
1097}
1098
1099static void r300_render_destroy(struct vbuf_render* render)
1100{
1101    FREE(render);
1102}
1103
1104static struct vbuf_render* r300_render_create(struct r300_context* r300)
1105{
1106    struct r300_render* r300render = CALLOC_STRUCT(r300_render);
1107
1108    r300render->r300 = r300;
1109
1110    r300render->base.max_vertex_buffer_bytes = 1024 * 1024;
1111    r300render->base.max_indices = 16 * 1024;
1112
1113    r300render->base.get_vertex_info = r300_render_get_vertex_info;
1114    r300render->base.allocate_vertices = r300_render_allocate_vertices;
1115    r300render->base.map_vertices = r300_render_map_vertices;
1116    r300render->base.unmap_vertices = r300_render_unmap_vertices;
1117    r300render->base.set_primitive = r300_render_set_primitive;
1118    r300render->base.draw_elements = r300_render_draw_elements;
1119    r300render->base.draw_arrays = r300_render_draw_arrays;
1120    r300render->base.release_vertices = r300_render_release_vertices;
1121    r300render->base.destroy = r300_render_destroy;
1122
1123    return &r300render->base;
1124}
1125
1126struct draw_stage* r300_draw_stage(struct r300_context* r300)
1127{
1128    struct vbuf_render* render;
1129    struct draw_stage* stage;
1130
1131    render = r300_render_create(r300);
1132
1133    if (!render) {
1134        return NULL;
1135    }
1136
1137    stage = draw_vbuf_stage(r300->draw, render);
1138
1139    if (!stage) {
1140        render->destroy(render);
1141        return NULL;
1142    }
1143
1144    draw_set_render(r300->draw, render);
1145
1146    return stage;
1147}
1148
1149void r300_draw_flush_vbuf(struct r300_context *r300)
1150{
1151    pipe_resource_reference(&r300->vbo, NULL);
1152    r300->draw_vbo_size = 0;
1153}
1154
1155/****************************************************************************
1156 *                         End of SW TCL functions                          *
1157 ***************************************************************************/
1158
1159/* This functions is used to draw a rectangle for the blitter module.
1160 *
1161 * If we rendered a quad, the pixels on the main diagonal
1162 * would be computed and stored twice, which makes the clear/copy codepaths
1163 * somewhat inefficient. Instead we use a rectangular point sprite. */
1164void r300_blitter_draw_rectangle(struct blitter_context *blitter,
1165                                 unsigned x1, unsigned y1,
1166                                 unsigned x2, unsigned y2,
1167                                 float depth,
1168                                 enum blitter_attrib_type type,
1169                                 const union pipe_color_union *attrib)
1170{
1171    struct r300_context *r300 = r300_context(util_blitter_get_pipe(blitter));
1172    unsigned last_sprite_coord_enable = r300->sprite_coord_enable;
1173    unsigned width = x2 - x1;
1174    unsigned height = y2 - y1;
1175    unsigned vertex_size =
1176            type == UTIL_BLITTER_ATTRIB_COLOR || !r300->draw ? 8 : 4;
1177    unsigned dwords = 13 + vertex_size +
1178                      (type == UTIL_BLITTER_ATTRIB_TEXCOORD ? 7 : 0);
1179    static const union pipe_color_union zeros;
1180    CS_LOCALS(r300);
1181
1182    if (r300->skip_rendering)
1183        return;
1184
1185    r300->context.set_vertex_buffers(&r300->context, 0, NULL);
1186
1187    if (type == UTIL_BLITTER_ATTRIB_TEXCOORD)
1188        r300->sprite_coord_enable = 1;
1189
1190    r300_update_derived_state(r300);
1191
1192    /* Mark some states we don't care about as non-dirty. */
1193    r300->viewport_state.dirty = FALSE;
1194
1195    if (!r300_prepare_for_rendering(r300, PREP_EMIT_STATES, NULL, dwords, 0, 0, -1))
1196        goto done;
1197
1198    DBG(r300, DBG_DRAW, "r300: draw_rectangle\n");
1199
1200    BEGIN_CS(dwords);
1201    /* Set up GA. */
1202    OUT_CS_REG(R300_GA_POINT_SIZE, (height * 6) | ((width * 6) << 16));
1203
1204    if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
1205        /* Set up the GA to generate texcoords. */
1206        OUT_CS_REG(R300_GB_ENABLE, R300_GB_POINT_STUFF_ENABLE |
1207                   (R300_GB_TEX_STR << R300_GB_TEX0_SOURCE_SHIFT));
1208        OUT_CS_REG_SEQ(R300_GA_POINT_S0, 4);
1209        OUT_CS_32F(attrib->f[0]);
1210        OUT_CS_32F(attrib->f[3]);
1211        OUT_CS_32F(attrib->f[2]);
1212        OUT_CS_32F(attrib->f[1]);
1213    }
1214
1215    /* Set up VAP controls. */
1216    OUT_CS_REG(R300_VAP_CLIP_CNTL, R300_CLIP_DISABLE);
1217    OUT_CS_REG(R300_VAP_VTE_CNTL, R300_VTX_XY_FMT | R300_VTX_Z_FMT);
1218    OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
1219    OUT_CS_REG_SEQ(R300_VAP_VF_MAX_VTX_INDX, 2);
1220    OUT_CS(1);
1221    OUT_CS(0);
1222
1223    /* Draw. */
1224    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, vertex_size);
1225    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (1 << 16) |
1226           R300_VAP_VF_CNTL__PRIM_POINTS);
1227
1228    OUT_CS_32F(x1 + width * 0.5f);
1229    OUT_CS_32F(y1 + height * 0.5f);
1230    OUT_CS_32F(depth);
1231    OUT_CS_32F(1);
1232
1233    if (vertex_size == 8) {
1234        if (!attrib)
1235            attrib = &zeros;
1236        OUT_CS_TABLE(attrib->f, 4);
1237    }
1238    END_CS;
1239
1240done:
1241    /* Restore the state. */
1242    r300_mark_atom_dirty(r300, &r300->rs_state);
1243    r300_mark_atom_dirty(r300, &r300->viewport_state);
1244
1245    r300->sprite_coord_enable = last_sprite_coord_enable;
1246}
1247
1248static void r300_resource_resolve(struct pipe_context *pipe,
1249                                  const struct pipe_resolve_info *info)
1250{
1251    struct r300_context *r300 = r300_context(pipe);
1252    struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
1253    struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1254    static const union pipe_color_union color;
1255
1256    assert(0 && "Resource resolve is unsupported, invalid call.");
1257
1258    memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1259    surf_tmpl.format = info->src.res->format;
1260    surf_tmpl.u.tex.first_layer =
1261    surf_tmpl.u.tex.last_layer = info->src.layer;
1262    srcsurf = pipe->create_surface(pipe, info->src.res, &surf_tmpl);
1263    /* XXX Offset both surfaces by x0,y1. */
1264
1265    surf_tmpl.format = info->dst.res->format;
1266    surf_tmpl.u.tex.level = info->dst.level;
1267    surf_tmpl.u.tex.first_layer =
1268    surf_tmpl.u.tex.last_layer = info->dst.layer;
1269    dstsurf = pipe->create_surface(pipe, info->dst.res, &surf_tmpl);
1270
1271    DBG(r300, DBG_DRAW, "r300: Resolving resource...\n");
1272
1273    /* Enable AA resolve. */
1274    aa->dest = r300_surface(dstsurf);
1275    aa->aaresolve_ctl =
1276        R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
1277        R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE;
1278    r300->aa_state.size = 10;
1279    r300_mark_atom_dirty(r300, &r300->aa_state);
1280
1281    /* Resolve the surface. */
1282    /* XXX: y1 < 0 ==> Y flip */
1283    r300->context.clear_render_target(pipe,
1284                                      srcsurf, &color, 0, 0,
1285                                      info->dst.x1 - info->dst.x0,
1286                                      info->dst.y1 - info->dst.y0);
1287
1288    /* Disable AA resolve. */
1289    aa->dest = NULL;
1290    aa->aaresolve_ctl = 0;
1291    r300->aa_state.size = 4;
1292    r300_mark_atom_dirty(r300, &r300->aa_state);
1293
1294    pipe_surface_reference(&srcsurf, NULL);
1295    pipe_surface_reference(&dstsurf, NULL);
1296}
1297
1298void r300_init_render_functions(struct r300_context *r300)
1299{
1300    /* Set draw functions based on presence of HW TCL. */
1301    if (r300->screen->caps.has_tcl) {
1302        r300->context.draw_vbo = r300_draw_vbo;
1303    } else {
1304        r300->context.draw_vbo = r300_swtcl_draw_vbo;
1305    }
1306
1307    r300->context.resource_resolve = r300_resource_resolve;
1308
1309    /* Plug in the two-sided stencil reference value fallback if needed. */
1310    if (!r300->screen->caps.is_r500)
1311        r300_plug_in_stencil_ref_fallback(r300);
1312}
1313