14a49301eSmrg/*
24a49301eSmrg * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3cdc920a0Smrg * Copyright 2009 Marek Olšák <maraeo@gmail.com>
44a49301eSmrg *
54a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a
64a49301eSmrg * copy of this software and associated documentation files (the "Software"),
74a49301eSmrg * to deal in the Software without restriction, including without limitation
84a49301eSmrg * on the rights to use, copy, modify, merge, publish, distribute, sub
94a49301eSmrg * license, and/or sell copies of the Software, and to permit persons to whom
104a49301eSmrg * the Software is furnished to do so, subject to the following conditions:
114a49301eSmrg *
124a49301eSmrg * The above copyright notice and this permission notice (including the next
134a49301eSmrg * paragraph) shall be included in all copies or substantial portions of the
144a49301eSmrg * Software.
154a49301eSmrg *
164a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
174a49301eSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184a49301eSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
194a49301eSmrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
204a49301eSmrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
214a49301eSmrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
224a49301eSmrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
234a49301eSmrg
244a49301eSmrg/* r300_emit: Functions for emitting state. */
254a49301eSmrg
267ec681f3Smrg#include "util/format/u_format.h"
274a49301eSmrg#include "util/u_math.h"
284a49301eSmrg
294a49301eSmrg#include "r300_context.h"
303464ebd5Sriastradh#include "r300_cb.h"
314a49301eSmrg#include "r300_cs.h"
324a49301eSmrg#include "r300_emit.h"
334a49301eSmrg#include "r300_fs.h"
344a49301eSmrg#include "r300_screen.h"
353464ebd5Sriastradh#include "r300_screen_buffer.h"
364a49301eSmrg#include "r300_vs.h"
374a49301eSmrg
384a49301eSmrgvoid r300_emit_blend_state(struct r300_context* r300,
39cdc920a0Smrg                           unsigned size, void* state)
404a49301eSmrg{
41cdc920a0Smrg    struct r300_blend_state* blend = (struct r300_blend_state*)state;
42cdc920a0Smrg    struct pipe_framebuffer_state* fb =
43cdc920a0Smrg        (struct pipe_framebuffer_state*)r300->fb_state.state;
44af69d88dSmrg    struct pipe_surface *cb;
454a49301eSmrg    CS_LOCALS(r300);
46cdc920a0Smrg
47af69d88dSmrg    cb = fb->nr_cbufs ? r300_get_nonnull_cb(fb, 0) : NULL;
48af69d88dSmrg
49af69d88dSmrg    if (cb) {
50af69d88dSmrg        if (cb->format == PIPE_FORMAT_R16G16B16A16_FLOAT) {
513464ebd5Sriastradh            WRITE_CS_TABLE(blend->cb_noclamp, size);
52af69d88dSmrg        } else if (cb->format == PIPE_FORMAT_R16G16B16X16_FLOAT) {
53af69d88dSmrg            WRITE_CS_TABLE(blend->cb_noclamp_noalpha, size);
54af69d88dSmrg        } else {
55af69d88dSmrg            unsigned swz = r300_surface(cb)->colormask_swizzle;
56af69d88dSmrg            WRITE_CS_TABLE(blend->cb_clamp[swz], size);
57af69d88dSmrg        }
58cdc920a0Smrg    } else {
593464ebd5Sriastradh        WRITE_CS_TABLE(blend->cb_no_readwrite, size);
60cdc920a0Smrg    }
614a49301eSmrg}
624a49301eSmrg
634a49301eSmrgvoid r300_emit_blend_color_state(struct r300_context* r300,
64cdc920a0Smrg                                 unsigned size, void* state)
654a49301eSmrg{
66cdc920a0Smrg    struct r300_blend_color_state* bc = (struct r300_blend_color_state*)state;
674a49301eSmrg    CS_LOCALS(r300);
684a49301eSmrg
693464ebd5Sriastradh    WRITE_CS_TABLE(bc->cb, size);
704a49301eSmrg}
714a49301eSmrg
724a49301eSmrgvoid r300_emit_clip_state(struct r300_context* r300,
73cdc920a0Smrg                          unsigned size, void* state)
744a49301eSmrg{
753464ebd5Sriastradh    struct r300_clip_state* clip = (struct r300_clip_state*)state;
764a49301eSmrg    CS_LOCALS(r300);
774a49301eSmrg
783464ebd5Sriastradh    WRITE_CS_TABLE(clip->cb, size);
794a49301eSmrg}
804a49301eSmrg
81cdc920a0Smrgvoid r300_emit_dsa_state(struct r300_context* r300, unsigned size, void* state)
824a49301eSmrg{
83cdc920a0Smrg    struct r300_dsa_state* dsa = (struct r300_dsa_state*)state;
84cdc920a0Smrg    struct pipe_framebuffer_state* fb =
85cdc920a0Smrg        (struct pipe_framebuffer_state*)r300->fb_state.state;
86af69d88dSmrg    boolean is_r500 = r300->screen->caps.is_r500;
874a49301eSmrg    CS_LOCALS(r300);
88af69d88dSmrg    uint32_t alpha_func = dsa->alpha_function;
894a49301eSmrg
90af69d88dSmrg    /* Choose the alpha ref value between 8-bit (FG_ALPHA_FUNC.AM_VAL) and
91af69d88dSmrg     * 16-bit (FG_ALPHA_VALUE). */
92af69d88dSmrg    if (is_r500 && (alpha_func & R300_FG_ALPHA_FUNC_ENABLE)) {
93af69d88dSmrg        struct pipe_surface *cb = fb->nr_cbufs ? r300_get_nonnull_cb(fb, 0) : NULL;
94af69d88dSmrg
95af69d88dSmrg        if (cb &&
96af69d88dSmrg            (cb->format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
97af69d88dSmrg             cb->format == PIPE_FORMAT_R16G16B16X16_FLOAT)) {
98af69d88dSmrg            alpha_func |= R500_FG_ALPHA_FUNC_FP16_ENABLE;
99af69d88dSmrg        } else {
100af69d88dSmrg            alpha_func |= R500_FG_ALPHA_FUNC_8BIT;
101af69d88dSmrg        }
1024a49301eSmrg    }
103af69d88dSmrg
104af69d88dSmrg    /* Setup alpha-to-coverage. */
105af69d88dSmrg    if (r300->alpha_to_coverage && r300->msaa_enable) {
106af69d88dSmrg        /* Always set 3/6, it improves precision even for 2x and 4x MSAA. */
107af69d88dSmrg        alpha_func |= R300_FG_ALPHA_FUNC_MASK_ENABLE |
108af69d88dSmrg                      R300_FG_ALPHA_FUNC_CFG_3_OF_6;
109af69d88dSmrg    }
110af69d88dSmrg
111af69d88dSmrg    BEGIN_CS(size);
112af69d88dSmrg    OUT_CS_REG(R300_FG_ALPHA_FUNC, alpha_func);
113af69d88dSmrg    OUT_CS_TABLE(fb->zsbuf ? &dsa->cb_begin : dsa->cb_zb_no_readwrite, size-2);
114af69d88dSmrg    END_CS;
1154a49301eSmrg}
1164a49301eSmrg
1173464ebd5Sriastradhstatic void get_rc_constant_state(
1183464ebd5Sriastradh    float vec[4],
1194a49301eSmrg    struct r300_context * r300,
1203464ebd5Sriastradh    struct rc_constant * constant)
1214a49301eSmrg{
1223464ebd5Sriastradh    struct r300_textures_state* texstate = r300->textures_state.state;
1233464ebd5Sriastradh    struct r300_resource *tex;
124cdc920a0Smrg
1253464ebd5Sriastradh    assert(constant->Type == RC_CONSTANT_STATE);
126cdc920a0Smrg
1273464ebd5Sriastradh    /* vec should either be (0, 0, 0, 1), which should be a relatively safe
128cdc920a0Smrg     * RGBA or STRQ value, or it could be one of the RC_CONSTANT_STATE
129cdc920a0Smrg     * state factors. */
1303464ebd5Sriastradh
1313464ebd5Sriastradh    switch (constant->u.State[0]) {
1323464ebd5Sriastradh        /* Factor for converting rectangle coords to
1333464ebd5Sriastradh         * normalized coords. Should only show up on non-r500. */
1343464ebd5Sriastradh        case RC_STATE_R300_TEXRECT_FACTOR:
1353464ebd5Sriastradh            tex = r300_resource(texstate->sampler_views[constant->u.State[1]]->base.texture);
1363464ebd5Sriastradh            vec[0] = 1.0 / tex->tex.width0;
1373464ebd5Sriastradh            vec[1] = 1.0 / tex->tex.height0;
1383464ebd5Sriastradh            vec[2] = 0;
1393464ebd5Sriastradh            vec[3] = 1;
1403464ebd5Sriastradh            break;
1413464ebd5Sriastradh
1423464ebd5Sriastradh        case RC_STATE_R300_TEXSCALE_FACTOR:
1433464ebd5Sriastradh            tex = r300_resource(texstate->sampler_views[constant->u.State[1]]->base.texture);
1443464ebd5Sriastradh            /* Add a small number to the texture size to work around rounding errors in hw. */
1457ec681f3Smrg            vec[0] = tex->b.width0  / (tex->tex.width0  + 0.001f);
1467ec681f3Smrg            vec[1] = tex->b.height0 / (tex->tex.height0 + 0.001f);
1477ec681f3Smrg            vec[2] = tex->b.depth0  / (tex->tex.depth0  + 0.001f);
1483464ebd5Sriastradh            vec[3] = 1;
1493464ebd5Sriastradh            break;
1503464ebd5Sriastradh
1513464ebd5Sriastradh        case RC_STATE_R300_VIEWPORT_SCALE:
1523464ebd5Sriastradh            vec[0] = r300->viewport.scale[0];
1533464ebd5Sriastradh            vec[1] = r300->viewport.scale[1];
1543464ebd5Sriastradh            vec[2] = r300->viewport.scale[2];
1553464ebd5Sriastradh            vec[3] = 1;
1563464ebd5Sriastradh            break;
1573464ebd5Sriastradh
1583464ebd5Sriastradh        case RC_STATE_R300_VIEWPORT_OFFSET:
1593464ebd5Sriastradh            vec[0] = r300->viewport.translate[0];
1603464ebd5Sriastradh            vec[1] = r300->viewport.translate[1];
1613464ebd5Sriastradh            vec[2] = r300->viewport.translate[2];
1623464ebd5Sriastradh            vec[3] = 1;
1633464ebd5Sriastradh            break;
1643464ebd5Sriastradh
1653464ebd5Sriastradh        default:
1663464ebd5Sriastradh            fprintf(stderr, "r300: Implementation error: "
1673464ebd5Sriastradh                "Unknown RC_CONSTANT type %d\n", constant->u.State[0]);
1683464ebd5Sriastradh            vec[0] = 0;
1693464ebd5Sriastradh            vec[1] = 0;
1703464ebd5Sriastradh            vec[2] = 0;
1713464ebd5Sriastradh            vec[3] = 1;
1723464ebd5Sriastradh    }
1734a49301eSmrg}
1744a49301eSmrg
1754a49301eSmrg/* Convert a normal single-precision float into the 7.16 format
1764a49301eSmrg * used by the R300 fragment shader.
1774a49301eSmrg */
1783464ebd5Sriastradhuint32_t pack_float24(float f)
1794a49301eSmrg{
1804a49301eSmrg    union {
1814a49301eSmrg        float fl;
1824a49301eSmrg        uint32_t u;
1834a49301eSmrg    } u;
1844a49301eSmrg    float mantissa;
1854a49301eSmrg    int exponent;
1864a49301eSmrg    uint32_t float24 = 0;
1874a49301eSmrg
1884a49301eSmrg    if (f == 0.0)
1894a49301eSmrg        return 0;
1904a49301eSmrg
1914a49301eSmrg    u.fl = f;
1924a49301eSmrg
1934a49301eSmrg    mantissa = frexpf(f, &exponent);
1944a49301eSmrg
1954a49301eSmrg    /* Handle -ve */
1964a49301eSmrg    if (mantissa < 0) {
1974a49301eSmrg        float24 |= (1 << 23);
1984a49301eSmrg        mantissa = mantissa * -1.0;
1994a49301eSmrg    }
2004a49301eSmrg    /* Handle exponent, bias of 63 */
2014a49301eSmrg    exponent += 62;
2024a49301eSmrg    float24 |= (exponent << 16);
2034a49301eSmrg    /* Kill 7 LSB of mantissa */
2044a49301eSmrg    float24 |= (u.u & 0x7FFFFF) >> 7;
2054a49301eSmrg
2064a49301eSmrg    return float24;
2074a49301eSmrg}
2084a49301eSmrg
2093464ebd5Sriastradhvoid r300_emit_fs(struct r300_context* r300, unsigned size, void *state)
2104a49301eSmrg{
2113464ebd5Sriastradh    struct r300_fragment_shader *fs = r300_fs(r300);
2123464ebd5Sriastradh    CS_LOCALS(r300);
2133464ebd5Sriastradh
2143464ebd5Sriastradh    WRITE_CS_TABLE(fs->shader->cb_code, fs->shader->cb_code_size);
2153464ebd5Sriastradh}
2163464ebd5Sriastradh
2173464ebd5Sriastradhvoid r300_emit_fs_constants(struct r300_context* r300, unsigned size, void *state)
2183464ebd5Sriastradh{
2193464ebd5Sriastradh    struct r300_fragment_shader *fs = r300_fs(r300);
2203464ebd5Sriastradh    struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
2213464ebd5Sriastradh    unsigned count = fs->shader->externals_count;
2223464ebd5Sriastradh    unsigned i, j;
2234a49301eSmrg    CS_LOCALS(r300);
2244a49301eSmrg
2253464ebd5Sriastradh    if (count == 0)
2263464ebd5Sriastradh        return;
2274a49301eSmrg
2283464ebd5Sriastradh    BEGIN_CS(size);
2293464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X, count * 4);
2303464ebd5Sriastradh    if (buf->remap_table){
2313464ebd5Sriastradh        for (i = 0; i < count; i++) {
2323464ebd5Sriastradh            float *data = (float*)&buf->ptr[buf->remap_table[i]*4];
2333464ebd5Sriastradh            for (j = 0; j < 4; j++)
2343464ebd5Sriastradh                OUT_CS(pack_float24(data[j]));
2353464ebd5Sriastradh        }
2363464ebd5Sriastradh    } else {
2373464ebd5Sriastradh        for (i = 0; i < count; i++)
2383464ebd5Sriastradh            for (j = 0; j < 4; j++)
2393464ebd5Sriastradh                OUT_CS(pack_float24(*(float*)&buf->ptr[i*4+j]));
2403464ebd5Sriastradh    }
2414a49301eSmrg
2423464ebd5Sriastradh    END_CS;
2433464ebd5Sriastradh}
2444a49301eSmrg
2453464ebd5Sriastradhvoid r300_emit_fs_rc_constant_state(struct r300_context* r300, unsigned size, void *state)
2463464ebd5Sriastradh{
2473464ebd5Sriastradh    struct r300_fragment_shader *fs = r300_fs(r300);
2483464ebd5Sriastradh    struct rc_constant_list *constants = &fs->shader->code.constants;
2493464ebd5Sriastradh    unsigned i;
2503464ebd5Sriastradh    unsigned count = fs->shader->rc_state_count;
2513464ebd5Sriastradh    unsigned first = fs->shader->externals_count;
2523464ebd5Sriastradh    unsigned end = constants->Count;
2533464ebd5Sriastradh    unsigned j;
2543464ebd5Sriastradh    CS_LOCALS(r300);
2554a49301eSmrg
2563464ebd5Sriastradh    if (count == 0)
2573464ebd5Sriastradh        return;
2584a49301eSmrg
2593464ebd5Sriastradh    BEGIN_CS(size);
2603464ebd5Sriastradh    for(i = first; i < end; ++i) {
2613464ebd5Sriastradh        if (constants->Constants[i].Type == RC_CONSTANT_STATE) {
2623464ebd5Sriastradh            float data[4];
2634a49301eSmrg
2643464ebd5Sriastradh            get_rc_constant_state(data, r300, &constants->Constants[i]);
2654a49301eSmrg
2663464ebd5Sriastradh            OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X + i * 16, 4);
2673464ebd5Sriastradh            for (j = 0; j < 4; j++)
2683464ebd5Sriastradh                OUT_CS(pack_float24(data[j]));
2693464ebd5Sriastradh        }
2704a49301eSmrg    }
2714a49301eSmrg    END_CS;
2724a49301eSmrg}
2734a49301eSmrg
2743464ebd5Sriastradhvoid r500_emit_fs(struct r300_context* r300, unsigned size, void *state)
2754a49301eSmrg{
2763464ebd5Sriastradh    struct r300_fragment_shader *fs = r300_fs(r300);
2773464ebd5Sriastradh    CS_LOCALS(r300);
2783464ebd5Sriastradh
2793464ebd5Sriastradh    WRITE_CS_TABLE(fs->shader->cb_code, fs->shader->cb_code_size);
2803464ebd5Sriastradh}
2813464ebd5Sriastradh
2823464ebd5Sriastradhvoid r500_emit_fs_constants(struct r300_context* r300, unsigned size, void *state)
2833464ebd5Sriastradh{
2843464ebd5Sriastradh    struct r300_fragment_shader *fs = r300_fs(r300);
2853464ebd5Sriastradh    struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
2863464ebd5Sriastradh    unsigned count = fs->shader->externals_count;
2874a49301eSmrg    CS_LOCALS(r300);
2884a49301eSmrg
2893464ebd5Sriastradh    if (count == 0)
2904a49301eSmrg        return;
2914a49301eSmrg
2923464ebd5Sriastradh    BEGIN_CS(size);
2933464ebd5Sriastradh    OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_CONST);
2943464ebd5Sriastradh    OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, count * 4);
2953464ebd5Sriastradh    if (buf->remap_table){
2963464ebd5Sriastradh        for (unsigned i = 0; i < count; i++) {
2973464ebd5Sriastradh            uint32_t *data = &buf->ptr[buf->remap_table[i]*4];
2983464ebd5Sriastradh            OUT_CS_TABLE(data, 4);
2993464ebd5Sriastradh        }
3003464ebd5Sriastradh    } else {
3013464ebd5Sriastradh        OUT_CS_TABLE(buf->ptr, count * 4);
3024a49301eSmrg    }
3034a49301eSmrg    END_CS;
3044a49301eSmrg}
3054a49301eSmrg
3063464ebd5Sriastradhvoid r500_emit_fs_rc_constant_state(struct r300_context* r300, unsigned size, void *state)
307cdc920a0Smrg{
3083464ebd5Sriastradh    struct r300_fragment_shader *fs = r300_fs(r300);
3093464ebd5Sriastradh    struct rc_constant_list *constants = &fs->shader->code.constants;
3103464ebd5Sriastradh    unsigned i;
3113464ebd5Sriastradh    unsigned count = fs->shader->rc_state_count;
3123464ebd5Sriastradh    unsigned first = fs->shader->externals_count;
3133464ebd5Sriastradh    unsigned end = constants->Count;
314cdc920a0Smrg    CS_LOCALS(r300);
315cdc920a0Smrg
3163464ebd5Sriastradh    if (count == 0)
3173464ebd5Sriastradh        return;
3183464ebd5Sriastradh
3193464ebd5Sriastradh    BEGIN_CS(size);
3203464ebd5Sriastradh    for(i = first; i < end; ++i) {
3213464ebd5Sriastradh        if (constants->Constants[i].Type == RC_CONSTANT_STATE) {
3223464ebd5Sriastradh            float data[4];
3233464ebd5Sriastradh
3243464ebd5Sriastradh            get_rc_constant_state(data, r300, &constants->Constants[i]);
3253464ebd5Sriastradh
3263464ebd5Sriastradh            OUT_CS_REG(R500_GA_US_VECTOR_INDEX,
3273464ebd5Sriastradh                       R500_GA_US_VECTOR_INDEX_TYPE_CONST |
3283464ebd5Sriastradh                       (i & R500_GA_US_VECTOR_INDEX_MASK));
3293464ebd5Sriastradh            OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA, 4);
3303464ebd5Sriastradh            OUT_CS_TABLE(data, 4);
3313464ebd5Sriastradh        }
332cdc920a0Smrg    }
333cdc920a0Smrg    END_CS;
334cdc920a0Smrg}
335cdc920a0Smrg
3363464ebd5Sriastradhvoid r300_emit_gpu_flush(struct r300_context *r300, unsigned size, void *state)
3374a49301eSmrg{
3383464ebd5Sriastradh    struct r300_gpu_flush *gpuflush = (struct r300_gpu_flush*)state;
3393464ebd5Sriastradh    struct pipe_framebuffer_state* fb =
3403464ebd5Sriastradh            (struct pipe_framebuffer_state*)r300->fb_state.state;
3413464ebd5Sriastradh    uint32_t height = fb->height;
3423464ebd5Sriastradh    uint32_t width = fb->width;
3434a49301eSmrg    CS_LOCALS(r300);
3444a49301eSmrg
3453464ebd5Sriastradh    if (r300->cbzb_clear) {
3463464ebd5Sriastradh        struct r300_surface *surf = r300_surface(fb->cbufs[0]);
3473464ebd5Sriastradh
3483464ebd5Sriastradh        height = surf->cbzb_height;
3493464ebd5Sriastradh        width = surf->cbzb_width;
3503464ebd5Sriastradh    }
3513464ebd5Sriastradh
3523464ebd5Sriastradh    DBG(r300, DBG_SCISSOR,
3533464ebd5Sriastradh	"r300: Scissor width: %i, height: %i, CBZB clear: %s\n",
3543464ebd5Sriastradh	width, height, r300->cbzb_clear ? "YES" : "NO");
3553464ebd5Sriastradh
3563464ebd5Sriastradh    BEGIN_CS(size);
3573464ebd5Sriastradh
3583464ebd5Sriastradh    /* Set up scissors.
3593464ebd5Sriastradh     * By writing to the SC registers, SC & US assert idle. */
3603464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL, 2);
3613464ebd5Sriastradh    if (r300->screen->caps.is_r500) {
3623464ebd5Sriastradh        OUT_CS(0);
3633464ebd5Sriastradh        OUT_CS(((width  - 1) << R300_SCISSORS_X_SHIFT) |
3643464ebd5Sriastradh               ((height - 1) << R300_SCISSORS_Y_SHIFT));
3653464ebd5Sriastradh    } else {
3663464ebd5Sriastradh        OUT_CS((1440 << R300_SCISSORS_X_SHIFT) |
3673464ebd5Sriastradh               (1440 << R300_SCISSORS_Y_SHIFT));
3683464ebd5Sriastradh        OUT_CS(((width  + 1440-1) << R300_SCISSORS_X_SHIFT) |
3693464ebd5Sriastradh               ((height + 1440-1) << R300_SCISSORS_Y_SHIFT));
3704a49301eSmrg    }
3714a49301eSmrg
3723464ebd5Sriastradh    /* Flush CB & ZB caches and wait until the 3D engine is idle and clean. */
3733464ebd5Sriastradh    OUT_CS_TABLE(gpuflush->cb_flush_clean, 6);
3744a49301eSmrg    END_CS;
3754a49301eSmrg}
3764a49301eSmrg
3773464ebd5Sriastradhvoid r300_emit_aa_state(struct r300_context *r300, unsigned size, void *state)
3784a49301eSmrg{
3793464ebd5Sriastradh    struct r300_aa_state *aa = (struct r300_aa_state*)state;
3804a49301eSmrg    CS_LOCALS(r300);
3814a49301eSmrg
3823464ebd5Sriastradh    BEGIN_CS(size);
3833464ebd5Sriastradh    OUT_CS_REG(R300_GB_AA_CONFIG, aa->aa_config);
3844a49301eSmrg
3853464ebd5Sriastradh    if (aa->dest) {
386af69d88dSmrg        OUT_CS_REG_SEQ(R300_RB3D_AARESOLVE_OFFSET, 3);
387af69d88dSmrg        OUT_CS(aa->dest->offset);
388af69d88dSmrg        OUT_CS(aa->dest->pitch & R300_RB3D_AARESOLVE_PITCH_MASK);
389af69d88dSmrg        OUT_CS(R300_RB3D_AARESOLVE_CTL_AARESOLVE_MODE_RESOLVE |
390af69d88dSmrg               R300_RB3D_AARESOLVE_CTL_AARESOLVE_ALPHA_AVERAGE);
3913464ebd5Sriastradh        OUT_CS_RELOC(aa->dest);
392af69d88dSmrg    } else {
393af69d88dSmrg        OUT_CS_REG(R300_RB3D_AARESOLVE_CTL, 0);
3944a49301eSmrg    }
3953464ebd5Sriastradh
3964a49301eSmrg    END_CS;
3974a49301eSmrg}
3984a49301eSmrg
399cdc920a0Smrgvoid r300_emit_fb_state(struct r300_context* r300, unsigned size, void* state)
4004a49301eSmrg{
401cdc920a0Smrg    struct pipe_framebuffer_state* fb = (struct pipe_framebuffer_state*)state;
4023464ebd5Sriastradh    struct r300_surface* surf;
4033464ebd5Sriastradh    unsigned i;
4043464ebd5Sriastradh    uint32_t rb3d_cctl = 0;
4053464ebd5Sriastradh
4064a49301eSmrg    CS_LOCALS(r300);
4074a49301eSmrg
408cdc920a0Smrg    BEGIN_CS(size);
409cdc920a0Smrg
4103464ebd5Sriastradh    if (r300->screen->caps.is_r500) {
4113464ebd5Sriastradh        rb3d_cctl = R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE;
412cdc920a0Smrg    }
413af69d88dSmrg    /* NUM_MULTIWRITES replicates COLOR[0] to all colorbuffers. */
4143464ebd5Sriastradh    if (fb->nr_cbufs && r300->fb_multiwrite) {
4153464ebd5Sriastradh        rb3d_cctl |= R300_RB3D_CCTL_NUM_MULTIWRITES(fb->nr_cbufs);
4163464ebd5Sriastradh    }
417af69d88dSmrg    if (r300->cmask_in_use) {
418af69d88dSmrg        rb3d_cctl |= R300_RB3D_CCTL_AA_COMPRESSION_ENABLE |
419af69d88dSmrg                     R300_RB3D_CCTL_CMASK_ENABLE;
420af69d88dSmrg    }
4213464ebd5Sriastradh
4223464ebd5Sriastradh    OUT_CS_REG(R300_RB3D_CCTL, rb3d_cctl);
423cdc920a0Smrg
424cdc920a0Smrg    /* Set up colorbuffers. */
4254a49301eSmrg    for (i = 0; i < fb->nr_cbufs; i++) {
426af69d88dSmrg        surf = r300_surface(r300_get_nonnull_cb(fb, i));
4273464ebd5Sriastradh
4283464ebd5Sriastradh        OUT_CS_REG(R300_RB3D_COLOROFFSET0 + (4 * i), surf->offset);
4293464ebd5Sriastradh        OUT_CS_RELOC(surf);
4303464ebd5Sriastradh
4313464ebd5Sriastradh        OUT_CS_REG(R300_RB3D_COLORPITCH0 + (4 * i), surf->pitch);
4323464ebd5Sriastradh        OUT_CS_RELOC(surf);
433af69d88dSmrg
434af69d88dSmrg        if (r300->cmask_in_use && i == 0) {
435af69d88dSmrg            OUT_CS_REG(R300_RB3D_CMASK_OFFSET0, 0);
436af69d88dSmrg            OUT_CS_REG(R300_RB3D_CMASK_PITCH0, surf->pitch_cmask);
437af69d88dSmrg            OUT_CS_REG(R300_RB3D_COLOR_CLEAR_VALUE, r300->color_clear_value);
438af69d88dSmrg            if (r300->screen->caps.is_r500 && r300->screen->info.drm_minor >= 29) {
439af69d88dSmrg                OUT_CS_REG_SEQ(R500_RB3D_COLOR_CLEAR_VALUE_AR, 2);
440af69d88dSmrg                OUT_CS(r300->color_clear_value_ar);
441af69d88dSmrg                OUT_CS(r300->color_clear_value_gb);
442af69d88dSmrg            }
443af69d88dSmrg        }
4443464ebd5Sriastradh    }
4454a49301eSmrg
4463464ebd5Sriastradh    /* Set up the ZB part of the CBZB clear. */
4473464ebd5Sriastradh    if (r300->cbzb_clear) {
4483464ebd5Sriastradh        surf = r300_surface(fb->cbufs[0]);
4494a49301eSmrg
4503464ebd5Sriastradh        OUT_CS_REG(R300_ZB_FORMAT, surf->cbzb_format);
4514a49301eSmrg
4523464ebd5Sriastradh        OUT_CS_REG(R300_ZB_DEPTHOFFSET, surf->cbzb_midpoint_offset);
4533464ebd5Sriastradh        OUT_CS_RELOC(surf);
4543464ebd5Sriastradh
4553464ebd5Sriastradh        OUT_CS_REG(R300_ZB_DEPTHPITCH, surf->cbzb_pitch);
4563464ebd5Sriastradh        OUT_CS_RELOC(surf);
4573464ebd5Sriastradh
4583464ebd5Sriastradh        DBG(r300, DBG_CBZB,
4593464ebd5Sriastradh            "CBZB clearing cbuf %08x %08x\n", surf->cbzb_format,
4603464ebd5Sriastradh            surf->cbzb_pitch);
461cdc920a0Smrg    }
4623464ebd5Sriastradh    /* Set up a zbuffer. */
4633464ebd5Sriastradh    else if (fb->zsbuf) {
4643464ebd5Sriastradh        surf = r300_surface(fb->zsbuf);
4653464ebd5Sriastradh
4663464ebd5Sriastradh        OUT_CS_REG(R300_ZB_FORMAT, surf->format);
4673464ebd5Sriastradh
4683464ebd5Sriastradh        OUT_CS_REG(R300_ZB_DEPTHOFFSET, surf->offset);
4693464ebd5Sriastradh        OUT_CS_RELOC(surf);
4703464ebd5Sriastradh
4713464ebd5Sriastradh        OUT_CS_REG(R300_ZB_DEPTHPITCH, surf->pitch);
4723464ebd5Sriastradh        OUT_CS_RELOC(surf);
4733464ebd5Sriastradh
4743464ebd5Sriastradh        if (r300->hyperz_enabled) {
4753464ebd5Sriastradh            /* HiZ RAM. */
4763464ebd5Sriastradh            OUT_CS_REG(R300_ZB_HIZ_OFFSET, 0);
4773464ebd5Sriastradh            OUT_CS_REG(R300_ZB_HIZ_PITCH, surf->pitch_hiz);
4783464ebd5Sriastradh            /* Z Mask RAM. (compressed zbuffer) */
4793464ebd5Sriastradh            OUT_CS_REG(R300_ZB_ZMASK_OFFSET, 0);
4803464ebd5Sriastradh            OUT_CS_REG(R300_ZB_ZMASK_PITCH, surf->pitch_zmask);
4813464ebd5Sriastradh        }
4824a49301eSmrg    }
4834a49301eSmrg
4843464ebd5Sriastradh    END_CS;
4853464ebd5Sriastradh}
4863464ebd5Sriastradh
4873464ebd5Sriastradhvoid r300_emit_hyperz_state(struct r300_context *r300,
4883464ebd5Sriastradh                            unsigned size, void *state)
4893464ebd5Sriastradh{
4903464ebd5Sriastradh    struct r300_hyperz_state *z = state;
4913464ebd5Sriastradh    CS_LOCALS(r300);
4923464ebd5Sriastradh
4933464ebd5Sriastradh    if (z->flush)
4943464ebd5Sriastradh        WRITE_CS_TABLE(&z->cb_flush_begin, size);
4953464ebd5Sriastradh    else
4963464ebd5Sriastradh        WRITE_CS_TABLE(&z->cb_begin, size - 2);
4973464ebd5Sriastradh}
4983464ebd5Sriastradh
4993464ebd5Sriastradhvoid r300_emit_hyperz_end(struct r300_context *r300)
5003464ebd5Sriastradh{
5013464ebd5Sriastradh    struct r300_hyperz_state z =
5023464ebd5Sriastradh            *(struct r300_hyperz_state*)r300->hyperz_state.state;
5033464ebd5Sriastradh
5043464ebd5Sriastradh    z.flush = 1;
5053464ebd5Sriastradh    z.zb_bw_cntl = 0;
5063464ebd5Sriastradh    z.zb_depthclearvalue = 0;
5073464ebd5Sriastradh    z.sc_hyperz = R300_SC_HYPERZ_ADJ_2;
5083464ebd5Sriastradh    z.gb_z_peq_config = 0;
5093464ebd5Sriastradh
5103464ebd5Sriastradh    r300_emit_hyperz_state(r300, r300->hyperz_state.size, &z);
5113464ebd5Sriastradh}
5123464ebd5Sriastradh
513af69d88dSmrg#define R300_NIBBLES(x0, y0, x1, y1, x2, y2, d0y, d0x)  \
514af69d88dSmrg    (((x0) & 0xf) | (((y0) & 0xf) << 4) |		   \
515af69d88dSmrg    (((x1) & 0xf) << 8) | (((y1) & 0xf) << 12) |	   \
516af69d88dSmrg    (((x2) & 0xf) << 16) | (((y2) & 0xf) << 20) |	   \
517af69d88dSmrg    (((d0y) & 0xf) << 24) | (((d0x) & 0xf) << 28))
518af69d88dSmrg
519af69d88dSmrgstatic unsigned r300_get_mspos(int index, unsigned *p)
520af69d88dSmrg{
521af69d88dSmrg    unsigned reg, i, distx, disty, dist;
522af69d88dSmrg
523af69d88dSmrg    if (index == 0) {
524af69d88dSmrg        /* MSPOS0 contains positions for samples 0,1,2 as (X,Y) pairs of nibbles,
525af69d88dSmrg         * followed by a (Y,X) pair containing the minimum distance from the pixel
526af69d88dSmrg         * edge:
527af69d88dSmrg         *     X0, Y0, X1, Y1, X2, Y2, D0_Y, D0_X
528af69d88dSmrg         *
529af69d88dSmrg         * There is a quirk when setting D0_X. The value represents the distance
530af69d88dSmrg         * from the left edge of the pixel quad to the first sample in subpixels.
531af69d88dSmrg         * All values less than eight should use the actual value, but „7‟ should
532af69d88dSmrg         * be used for the distance „8‟. The hardware will convert 7 into 8 internally.
533af69d88dSmrg         */
534af69d88dSmrg        distx = 11;
535af69d88dSmrg        for (i = 0; i < 12; i += 2) {
536af69d88dSmrg            if (p[i] < distx)
537af69d88dSmrg                distx = p[i];
538af69d88dSmrg        }
539af69d88dSmrg
540af69d88dSmrg        disty = 11;
541af69d88dSmrg        for (i = 1; i < 12; i += 2) {
542af69d88dSmrg            if (p[i] < disty)
543af69d88dSmrg                disty = p[i];
544af69d88dSmrg        }
545af69d88dSmrg
546af69d88dSmrg        if (distx == 8)
547af69d88dSmrg            distx = 7;
548af69d88dSmrg
549af69d88dSmrg        reg = R300_NIBBLES(p[0], p[1], p[2], p[3], p[4], p[5], disty, distx);
550af69d88dSmrg    } else {
551af69d88dSmrg        /* MSPOS1 contains positions for samples 3,4,5 as (X,Y) pairs of nibbles,
552af69d88dSmrg         * followed by the minimum distance from the pixel edge (not sure if X or Y):
553af69d88dSmrg         *     X3, Y3, X4, Y4, X5, Y5, D1
554af69d88dSmrg         */
555af69d88dSmrg        dist = 11;
556af69d88dSmrg        for (i = 0; i < 12; i++) {
557af69d88dSmrg            if (p[i] < dist)
558af69d88dSmrg                dist = p[i];
559af69d88dSmrg        }
560af69d88dSmrg
561af69d88dSmrg        reg = R300_NIBBLES(p[6], p[7], p[8], p[9], p[10], p[11], dist, 0);
562af69d88dSmrg    }
563af69d88dSmrg    return reg;
564af69d88dSmrg}
565af69d88dSmrg
5663464ebd5Sriastradhvoid r300_emit_fb_state_pipelined(struct r300_context *r300,
5673464ebd5Sriastradh                                  unsigned size, void *state)
5683464ebd5Sriastradh{
569af69d88dSmrg    /* The sample coordinates are in the range [0,11], because
570af69d88dSmrg     * GB_TILE_CONFIG.SUBPIXEL is set to the 1/12 subpixel precision.
571af69d88dSmrg     *
572af69d88dSmrg     * Some sample coordinates reach to neighboring pixels and should not be used.
573af69d88dSmrg     * (e.g. Y=11)
574af69d88dSmrg     *
575af69d88dSmrg     * The unused samples must be set to the positions of other valid samples. */
576af69d88dSmrg    static unsigned sample_locs_1x[12] = {
577af69d88dSmrg        6,6,  6,6,  6,6,  6,6,  6,6,  6,6
578af69d88dSmrg    };
579af69d88dSmrg    static unsigned sample_locs_2x[12] = {
580af69d88dSmrg        3,9,  9,3,  9,3,  9,3,  9,3,  9,3
581af69d88dSmrg    };
582af69d88dSmrg    static unsigned sample_locs_4x[12] = {
583af69d88dSmrg        4,4,  8,8,  2,10,  10,2,  10,2,  10,2
584af69d88dSmrg    };
585af69d88dSmrg    static unsigned sample_locs_6x[12] = {
586af69d88dSmrg        3,1,  7,3,  11,5,  1,7,  5,9,  9,10
587af69d88dSmrg    };
588af69d88dSmrg
5893464ebd5Sriastradh    struct pipe_framebuffer_state* fb =
5903464ebd5Sriastradh            (struct pipe_framebuffer_state*)r300->fb_state.state;
5913464ebd5Sriastradh    unsigned i, num_cbufs = fb->nr_cbufs;
5923464ebd5Sriastradh    unsigned mspos0, mspos1;
5933464ebd5Sriastradh    CS_LOCALS(r300);
5944a49301eSmrg
5953464ebd5Sriastradh    /* If we use the multiwrite feature, the colorbuffers 2,3,4 must be
5963464ebd5Sriastradh     * marked as UNUSED in the US block. */
5973464ebd5Sriastradh    if (r300->fb_multiwrite) {
5983464ebd5Sriastradh        num_cbufs = MIN2(num_cbufs, 1);
5993464ebd5Sriastradh    }
6003464ebd5Sriastradh
6013464ebd5Sriastradh    BEGIN_CS(size);
6023464ebd5Sriastradh
6033464ebd5Sriastradh    /* Colorbuffer format in the US block.
6043464ebd5Sriastradh     * (must be written after unpipelined regs) */
6053464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_US_OUT_FMT_0, 4);
6063464ebd5Sriastradh    for (i = 0; i < num_cbufs; i++) {
607af69d88dSmrg        OUT_CS(r300_surface(r300_get_nonnull_cb(fb, i))->format);
6083464ebd5Sriastradh    }
6093464ebd5Sriastradh    for (; i < 1; i++) {
6103464ebd5Sriastradh        OUT_CS(R300_US_OUT_FMT_C4_8 |
6113464ebd5Sriastradh               R300_C0_SEL_B | R300_C1_SEL_G |
6123464ebd5Sriastradh               R300_C2_SEL_R | R300_C3_SEL_A);
6133464ebd5Sriastradh    }
6143464ebd5Sriastradh    for (; i < 4; i++) {
6153464ebd5Sriastradh        OUT_CS(R300_US_OUT_FMT_UNUSED);
6163464ebd5Sriastradh    }
6174a49301eSmrg
618af69d88dSmrg    /* Set sample positions. It depends on the framebuffer sample count.
619af69d88dSmrg     * These are pipelined regs and as such cannot be moved to the AA state.
620af69d88dSmrg     */
621af69d88dSmrg    switch (r300->num_samples) {
622af69d88dSmrg    default:
623af69d88dSmrg        mspos0 = r300_get_mspos(0, sample_locs_1x);
624af69d88dSmrg        mspos1 = r300_get_mspos(1, sample_locs_1x);
625af69d88dSmrg        break;
626af69d88dSmrg    case 2:
627af69d88dSmrg        mspos0 = r300_get_mspos(0, sample_locs_2x);
628af69d88dSmrg        mspos1 = r300_get_mspos(1, sample_locs_2x);
629af69d88dSmrg        break;
630af69d88dSmrg    case 4:
631af69d88dSmrg        mspos0 = r300_get_mspos(0, sample_locs_4x);
632af69d88dSmrg        mspos1 = r300_get_mspos(1, sample_locs_4x);
633af69d88dSmrg        break;
634af69d88dSmrg    case 6:
635af69d88dSmrg        mspos0 = r300_get_mspos(0, sample_locs_6x);
636af69d88dSmrg        mspos1 = r300_get_mspos(1, sample_locs_6x);
637af69d88dSmrg        break;
6384a49301eSmrg    }
6394a49301eSmrg
6403464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_GB_MSPOS0, 2);
6413464ebd5Sriastradh    OUT_CS(mspos0);
6423464ebd5Sriastradh    OUT_CS(mspos1);
6434a49301eSmrg    END_CS;
6444a49301eSmrg}
6454a49301eSmrg
6463464ebd5Sriastradhvoid r300_emit_query_start(struct r300_context *r300, unsigned size, void*state)
6474a49301eSmrg{
6484a49301eSmrg    struct r300_query *query = r300->query_current;
6494a49301eSmrg    CS_LOCALS(r300);
6504a49301eSmrg
6514a49301eSmrg    if (!query)
6524a49301eSmrg	return;
6534a49301eSmrg
6543464ebd5Sriastradh    BEGIN_CS(size);
655af69d88dSmrg    if (r300->screen->caps.family == CHIP_RV530) {
6564a49301eSmrg        OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
6574a49301eSmrg    } else {
6584a49301eSmrg        OUT_CS_REG(R300_SU_REG_DEST, R300_RASTER_PIPE_SELECT_ALL);
6594a49301eSmrg    }
6604a49301eSmrg    OUT_CS_REG(R300_ZB_ZPASS_DATA, 0);
6614a49301eSmrg    END_CS;
6624a49301eSmrg    query->begin_emitted = TRUE;
6634a49301eSmrg}
6644a49301eSmrg
6653464ebd5Sriastradhstatic void r300_emit_query_end_frag_pipes(struct r300_context *r300,
6663464ebd5Sriastradh                                           struct r300_query *query)
6674a49301eSmrg{
6683464ebd5Sriastradh    struct r300_capabilities* caps = &r300->screen->caps;
669af69d88dSmrg    uint32_t gb_pipes = r300->screen->info.r300_num_gb_pipes;
6704a49301eSmrg    CS_LOCALS(r300);
6714a49301eSmrg
672af69d88dSmrg    assert(gb_pipes);
6734a49301eSmrg
674af69d88dSmrg    BEGIN_CS(6 * gb_pipes + 2);
6754a49301eSmrg    /* I'm not so sure I like this switch, but it's hard to be elegant
6764a49301eSmrg     * when there's so many special cases...
6774a49301eSmrg     *
6784a49301eSmrg     * So here's the basic idea. For each pipe, enable writes to it only,
6794a49301eSmrg     * then put out the relocation for ZPASS_ADDR, taking into account a
6804a49301eSmrg     * 4-byte offset for each pipe. RV380 and older are special; they have
6814a49301eSmrg     * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
6824a49301eSmrg     * so there's a chipset cap for that. */
683af69d88dSmrg    switch (gb_pipes) {
6844a49301eSmrg        case 4:
6854a49301eSmrg            /* pipe 3 only */
6864a49301eSmrg            OUT_CS_REG(R300_SU_REG_DEST, 1 << 3);
6873464ebd5Sriastradh            OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 3) * 4);
6883464ebd5Sriastradh            OUT_CS_RELOC(r300->query_current);
6897ec681f3Smrg            FALLTHROUGH;
6904a49301eSmrg        case 3:
6914a49301eSmrg            /* pipe 2 only */
6924a49301eSmrg            OUT_CS_REG(R300_SU_REG_DEST, 1 << 2);
6933464ebd5Sriastradh            OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 2) * 4);
6943464ebd5Sriastradh            OUT_CS_RELOC(r300->query_current);
6957ec681f3Smrg            FALLTHROUGH;
6964a49301eSmrg        case 2:
6974a49301eSmrg            /* pipe 1 only */
69801e04c3fSmrg            /* As mentioned above, accommodate RV380 and older. */
6994a49301eSmrg            OUT_CS_REG(R300_SU_REG_DEST,
7004a49301eSmrg                    1 << (caps->high_second_pipe ? 3 : 1));
7013464ebd5Sriastradh            OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 1) * 4);
7023464ebd5Sriastradh            OUT_CS_RELOC(r300->query_current);
7037ec681f3Smrg            FALLTHROUGH;
7044a49301eSmrg        case 1:
7054a49301eSmrg            /* pipe 0 only */
7064a49301eSmrg            OUT_CS_REG(R300_SU_REG_DEST, 1 << 0);
7073464ebd5Sriastradh            OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 0) * 4);
7083464ebd5Sriastradh            OUT_CS_RELOC(r300->query_current);
7094a49301eSmrg            break;
7104a49301eSmrg        default:
7113464ebd5Sriastradh            fprintf(stderr, "r300: Implementation error: Chipset reports %d"
712af69d88dSmrg                    " pixel pipes!\n", gb_pipes);
7133464ebd5Sriastradh            abort();
7144a49301eSmrg    }
7154a49301eSmrg
7164a49301eSmrg    /* And, finally, reset it to normal... */
7174a49301eSmrg    OUT_CS_REG(R300_SU_REG_DEST, 0xF);
7184a49301eSmrg    END_CS;
7194a49301eSmrg}
7204a49301eSmrg
7213464ebd5Sriastradhstatic void rv530_emit_query_end_single_z(struct r300_context *r300,
7223464ebd5Sriastradh                                          struct r300_query *query)
7234a49301eSmrg{
7244a49301eSmrg    CS_LOCALS(r300);
7254a49301eSmrg
7264a49301eSmrg    BEGIN_CS(8);
7274a49301eSmrg    OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
7283464ebd5Sriastradh    OUT_CS_REG(R300_ZB_ZPASS_ADDR, query->num_results * 4);
7293464ebd5Sriastradh    OUT_CS_RELOC(r300->query_current);
7304a49301eSmrg    OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
7314a49301eSmrg    END_CS;
7324a49301eSmrg}
7334a49301eSmrg
7343464ebd5Sriastradhstatic void rv530_emit_query_end_double_z(struct r300_context *r300,
7353464ebd5Sriastradh                                          struct r300_query *query)
7364a49301eSmrg{
7374a49301eSmrg    CS_LOCALS(r300);
7384a49301eSmrg
7394a49301eSmrg    BEGIN_CS(14);
7404a49301eSmrg    OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_0);
7413464ebd5Sriastradh    OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 0) * 4);
7423464ebd5Sriastradh    OUT_CS_RELOC(r300->query_current);
7434a49301eSmrg    OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_1);
7443464ebd5Sriastradh    OUT_CS_REG(R300_ZB_ZPASS_ADDR, (query->num_results + 1) * 4);
7453464ebd5Sriastradh    OUT_CS_RELOC(r300->query_current);
7464a49301eSmrg    OUT_CS_REG(RV530_FG_ZBREG_DEST, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL);
7474a49301eSmrg    END_CS;
7484a49301eSmrg}
7494a49301eSmrg
7504a49301eSmrgvoid r300_emit_query_end(struct r300_context* r300)
7514a49301eSmrg{
7523464ebd5Sriastradh    struct r300_capabilities *caps = &r300->screen->caps;
7534a49301eSmrg    struct r300_query *query = r300->query_current;
7544a49301eSmrg
7554a49301eSmrg    if (!query)
7564a49301eSmrg	return;
7574a49301eSmrg
7584a49301eSmrg    if (query->begin_emitted == FALSE)
7594a49301eSmrg        return;
7604a49301eSmrg
761af69d88dSmrg    if (caps->family == CHIP_RV530) {
762af69d88dSmrg        if (r300->screen->info.r300_num_z_pipes == 2)
7633464ebd5Sriastradh            rv530_emit_query_end_double_z(r300, query);
7644a49301eSmrg        else
7653464ebd5Sriastradh            rv530_emit_query_end_single_z(r300, query);
7664a49301eSmrg    } else
7673464ebd5Sriastradh        r300_emit_query_end_frag_pipes(r300, query);
7683464ebd5Sriastradh
7693464ebd5Sriastradh    query->begin_emitted = FALSE;
7703464ebd5Sriastradh    query->num_results += query->num_pipes;
7713464ebd5Sriastradh
7723464ebd5Sriastradh    /* XXX grab all the results and reset the counter. */
773af69d88dSmrg    if (query->num_results >= query->buf->size / 4 - 4) {
774af69d88dSmrg        query->num_results = (query->buf->size / 4) / 2;
7753464ebd5Sriastradh        fprintf(stderr, "r300: Rewinding OQBO...\n");
7763464ebd5Sriastradh    }
7773464ebd5Sriastradh}
7783464ebd5Sriastradh
7793464ebd5Sriastradhvoid r300_emit_invariant_state(struct r300_context *r300,
7803464ebd5Sriastradh                               unsigned size, void *state)
7813464ebd5Sriastradh{
7823464ebd5Sriastradh    CS_LOCALS(r300);
7833464ebd5Sriastradh    WRITE_CS_TABLE(state, size);
7844a49301eSmrg}
7854a49301eSmrg
786cdc920a0Smrgvoid r300_emit_rs_state(struct r300_context* r300, unsigned size, void* state)
7874a49301eSmrg{
7883464ebd5Sriastradh    struct r300_rs_state* rs = state;
7894a49301eSmrg    CS_LOCALS(r300);
7904a49301eSmrg
791cdc920a0Smrg    BEGIN_CS(size);
7923464ebd5Sriastradh    OUT_CS_TABLE(rs->cb_main, RS_STATE_MAIN_SIZE);
793cdc920a0Smrg    if (rs->polygon_offset_enable) {
7943464ebd5Sriastradh        if (r300->zbuffer_bpp == 16) {
7953464ebd5Sriastradh            OUT_CS_TABLE(rs->cb_poly_offset_zb16, 5);
7963464ebd5Sriastradh        } else {
7973464ebd5Sriastradh            OUT_CS_TABLE(rs->cb_poly_offset_zb24, 5);
798cdc920a0Smrg        }
799cdc920a0Smrg    }
8004a49301eSmrg    END_CS;
8014a49301eSmrg}
8024a49301eSmrg
8034a49301eSmrgvoid r300_emit_rs_block_state(struct r300_context* r300,
804cdc920a0Smrg                              unsigned size, void* state)
8054a49301eSmrg{
806cdc920a0Smrg    struct r300_rs_block* rs = (struct r300_rs_block*)state;
807cdc920a0Smrg    unsigned i;
808cdc920a0Smrg    /* It's the same for both INST and IP tables */
809cdc920a0Smrg    unsigned count = (rs->inst_count & R300_RS_INST_COUNT_MASK) + 1;
8104a49301eSmrg    CS_LOCALS(r300);
8114a49301eSmrg
8123464ebd5Sriastradh    if (DBG_ON(r300, DBG_RS_BLOCK)) {
8133464ebd5Sriastradh        r500_dump_rs_block(rs);
8143464ebd5Sriastradh
8153464ebd5Sriastradh        fprintf(stderr, "r300: RS emit:\n");
8163464ebd5Sriastradh
8173464ebd5Sriastradh        for (i = 0; i < count; i++)
8183464ebd5Sriastradh            fprintf(stderr, "    : ip %d: 0x%08x\n", i, rs->ip[i]);
8193464ebd5Sriastradh
8203464ebd5Sriastradh        for (i = 0; i < count; i++)
8213464ebd5Sriastradh            fprintf(stderr, "    : inst %d: 0x%08x\n", i, rs->inst[i]);
8223464ebd5Sriastradh
8233464ebd5Sriastradh        fprintf(stderr, "    : count: 0x%08x inst_count: 0x%08x\n",
8243464ebd5Sriastradh            rs->count, rs->inst_count);
8253464ebd5Sriastradh    }
8264a49301eSmrg
827cdc920a0Smrg    BEGIN_CS(size);
8283464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
8293464ebd5Sriastradh    OUT_CS(rs->vap_vtx_state_cntl);
8303464ebd5Sriastradh    OUT_CS(rs->vap_vsm_vtx_assm);
8313464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
8323464ebd5Sriastradh    OUT_CS(rs->vap_out_vtx_fmt[0]);
8333464ebd5Sriastradh    OUT_CS(rs->vap_out_vtx_fmt[1]);
8343464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_GB_ENABLE, 1);
8353464ebd5Sriastradh    OUT_CS(rs->gb_enable);
8363464ebd5Sriastradh
8373464ebd5Sriastradh    if (r300->screen->caps.is_r500) {
838cdc920a0Smrg        OUT_CS_REG_SEQ(R500_RS_IP_0, count);
8394a49301eSmrg    } else {
840cdc920a0Smrg        OUT_CS_REG_SEQ(R300_RS_IP_0, count);
8414a49301eSmrg    }
8423464ebd5Sriastradh    OUT_CS_TABLE(rs->ip, count);
8434a49301eSmrg
8444a49301eSmrg    OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
8454a49301eSmrg    OUT_CS(rs->count);
8464a49301eSmrg    OUT_CS(rs->inst_count);
8474a49301eSmrg
8483464ebd5Sriastradh    if (r300->screen->caps.is_r500) {
849cdc920a0Smrg        OUT_CS_REG_SEQ(R500_RS_INST_0, count);
8504a49301eSmrg    } else {
851cdc920a0Smrg        OUT_CS_REG_SEQ(R300_RS_INST_0, count);
8524a49301eSmrg    }
8533464ebd5Sriastradh    OUT_CS_TABLE(rs->inst, count);
8544a49301eSmrg    END_CS;
8554a49301eSmrg}
8564a49301eSmrg
857af69d88dSmrgvoid r300_emit_sample_mask(struct r300_context *r300,
858af69d88dSmrg                           unsigned size, void *state)
859af69d88dSmrg{
860af69d88dSmrg    unsigned mask = (*(unsigned*)state) & ((1 << 6)-1);
861af69d88dSmrg    CS_LOCALS(r300);
862af69d88dSmrg
863af69d88dSmrg    BEGIN_CS(size);
864af69d88dSmrg    OUT_CS_REG(R300_SC_SCREENDOOR,
865af69d88dSmrg               mask | (mask << 6) | (mask << 12) | (mask << 18));
866af69d88dSmrg    END_CS;
867af69d88dSmrg}
868af69d88dSmrg
8694a49301eSmrgvoid r300_emit_scissor_state(struct r300_context* r300,
870cdc920a0Smrg                             unsigned size, void* state)
8714a49301eSmrg{
872cdc920a0Smrg    struct pipe_scissor_state* scissor = (struct pipe_scissor_state*)state;
8734a49301eSmrg    CS_LOCALS(r300);
8744a49301eSmrg
8753464ebd5Sriastradh    BEGIN_CS(size);
8763464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0, 2);
8773464ebd5Sriastradh    if (r300->screen->caps.is_r500) {
8783464ebd5Sriastradh        OUT_CS((scissor->minx << R300_CLIPRECT_X_SHIFT) |
8793464ebd5Sriastradh               (scissor->miny << R300_CLIPRECT_Y_SHIFT));
8803464ebd5Sriastradh        OUT_CS(((scissor->maxx - 1) << R300_CLIPRECT_X_SHIFT) |
8813464ebd5Sriastradh               ((scissor->maxy - 1) << R300_CLIPRECT_Y_SHIFT));
882cdc920a0Smrg    } else {
8833464ebd5Sriastradh        OUT_CS(((scissor->minx + 1440) << R300_CLIPRECT_X_SHIFT) |
8843464ebd5Sriastradh               ((scissor->miny + 1440) << R300_CLIPRECT_Y_SHIFT));
8853464ebd5Sriastradh        OUT_CS(((scissor->maxx + 1440-1) << R300_CLIPRECT_X_SHIFT) |
8863464ebd5Sriastradh               ((scissor->maxy + 1440-1) << R300_CLIPRECT_Y_SHIFT));
887cdc920a0Smrg    }
8884a49301eSmrg    END_CS;
8894a49301eSmrg}
8904a49301eSmrg
891cdc920a0Smrgvoid r300_emit_textures_state(struct r300_context *r300,
892cdc920a0Smrg                              unsigned size, void *state)
8934a49301eSmrg{
894cdc920a0Smrg    struct r300_textures_state *allstate = (struct r300_textures_state*)state;
895cdc920a0Smrg    struct r300_texture_sampler_state *texstate;
8963464ebd5Sriastradh    struct r300_resource *tex;
897cdc920a0Smrg    unsigned i;
8983464ebd5Sriastradh    boolean has_us_format = r300->screen->caps.has_us_format;
8994a49301eSmrg    CS_LOCALS(r300);
9004a49301eSmrg
901cdc920a0Smrg    BEGIN_CS(size);
902cdc920a0Smrg    OUT_CS_REG(R300_TX_ENABLE, allstate->tx_enable);
903cdc920a0Smrg
904cdc920a0Smrg    for (i = 0; i < allstate->count; i++) {
905cdc920a0Smrg        if ((1 << i) & allstate->tx_enable) {
906cdc920a0Smrg            texstate = &allstate->regs[i];
9073464ebd5Sriastradh            tex = r300_resource(allstate->sampler_views[i]->base.texture);
908cdc920a0Smrg
9093464ebd5Sriastradh            OUT_CS_REG(R300_TX_FILTER0_0 + (i * 4), texstate->filter0);
9103464ebd5Sriastradh            OUT_CS_REG(R300_TX_FILTER1_0 + (i * 4), texstate->filter1);
911cdc920a0Smrg            OUT_CS_REG(R300_TX_BORDER_COLOR_0 + (i * 4),
912cdc920a0Smrg                       texstate->border_color);
9134a49301eSmrg
9143464ebd5Sriastradh            OUT_CS_REG(R300_TX_FORMAT0_0 + (i * 4), texstate->format.format0);
9153464ebd5Sriastradh            OUT_CS_REG(R300_TX_FORMAT1_0 + (i * 4), texstate->format.format1);
9163464ebd5Sriastradh            OUT_CS_REG(R300_TX_FORMAT2_0 + (i * 4), texstate->format.format2);
9174a49301eSmrg
9183464ebd5Sriastradh            OUT_CS_REG(R300_TX_OFFSET_0 + (i * 4), texstate->format.tile_config);
9193464ebd5Sriastradh            OUT_CS_RELOC(tex);
9203464ebd5Sriastradh
9213464ebd5Sriastradh            if (has_us_format) {
9223464ebd5Sriastradh                OUT_CS_REG(R500_US_FORMAT0_0 + (i * 4),
9233464ebd5Sriastradh                           texstate->format.us_format0);
9243464ebd5Sriastradh            }
925cdc920a0Smrg        }
926cdc920a0Smrg    }
9274a49301eSmrg    END_CS;
9284a49301eSmrg}
9294a49301eSmrg
9303464ebd5Sriastradhvoid r300_emit_vertex_arrays(struct r300_context* r300, int offset,
9313464ebd5Sriastradh                             boolean indexed, int instance_id)
9324a49301eSmrg{
933af69d88dSmrg    struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
9343464ebd5Sriastradh    struct pipe_vertex_element *velem = r300->velems->velem;
9353464ebd5Sriastradh    struct r300_resource *buf;
9364a49301eSmrg    int i;
9373464ebd5Sriastradh    unsigned vertex_array_count = r300->velems->count;
9383464ebd5Sriastradh    unsigned packet_size = (vertex_array_count * 3 + 1) / 2;
9393464ebd5Sriastradh    struct pipe_vertex_buffer *vb1, *vb2;
9403464ebd5Sriastradh    unsigned *hw_format_size = r300->velems->format_size;
9413464ebd5Sriastradh    unsigned size1, size2, offset1, offset2, stride1, stride2;
942cdc920a0Smrg    CS_LOCALS(r300);
943cdc920a0Smrg
9443464ebd5Sriastradh    BEGIN_CS(2 + packet_size + vertex_array_count * 2);
9454a49301eSmrg    OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, packet_size);
9463464ebd5Sriastradh    OUT_CS(vertex_array_count | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
9473464ebd5Sriastradh
9483464ebd5Sriastradh    if (instance_id == -1) {
9493464ebd5Sriastradh        /* Non-instanced arrays. This ignores instance_divisor and instance_id. */
9503464ebd5Sriastradh        for (i = 0; i < vertex_array_count - 1; i += 2) {
9513464ebd5Sriastradh            vb1 = &vbuf[velem[i].vertex_buffer_index];
9523464ebd5Sriastradh            vb2 = &vbuf[velem[i+1].vertex_buffer_index];
9533464ebd5Sriastradh            size1 = hw_format_size[i];
9543464ebd5Sriastradh            size2 = hw_format_size[i+1];
9553464ebd5Sriastradh
9563464ebd5Sriastradh            OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride) |
9573464ebd5Sriastradh                   R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(vb2->stride));
9583464ebd5Sriastradh            OUT_CS(vb1->buffer_offset + velem[i].src_offset   + offset * vb1->stride);
9593464ebd5Sriastradh            OUT_CS(vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride);
9603464ebd5Sriastradh        }
961cdc920a0Smrg
9623464ebd5Sriastradh        if (vertex_array_count & 1) {
9633464ebd5Sriastradh            vb1 = &vbuf[velem[i].vertex_buffer_index];
9643464ebd5Sriastradh            size1 = hw_format_size[i];
965cdc920a0Smrg
9663464ebd5Sriastradh            OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(vb1->stride));
9673464ebd5Sriastradh            OUT_CS(vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride);
9683464ebd5Sriastradh        }
9693464ebd5Sriastradh
9703464ebd5Sriastradh        for (i = 0; i < vertex_array_count; i++) {
97101e04c3fSmrg            buf = r300_resource(vbuf[velem[i].vertex_buffer_index].buffer.resource);
9723464ebd5Sriastradh            OUT_CS_RELOC(buf);
9733464ebd5Sriastradh        }
9743464ebd5Sriastradh    } else {
9753464ebd5Sriastradh        /* Instanced arrays. */
9763464ebd5Sriastradh        for (i = 0; i < vertex_array_count - 1; i += 2) {
9773464ebd5Sriastradh            vb1 = &vbuf[velem[i].vertex_buffer_index];
9783464ebd5Sriastradh            vb2 = &vbuf[velem[i+1].vertex_buffer_index];
9793464ebd5Sriastradh            size1 = hw_format_size[i];
9803464ebd5Sriastradh            size2 = hw_format_size[i+1];
9813464ebd5Sriastradh
9823464ebd5Sriastradh            if (velem[i].instance_divisor) {
9833464ebd5Sriastradh                stride1 = 0;
9843464ebd5Sriastradh                offset1 = vb1->buffer_offset + velem[i].src_offset +
9853464ebd5Sriastradh                          (instance_id / velem[i].instance_divisor) * vb1->stride;
9863464ebd5Sriastradh            } else {
9873464ebd5Sriastradh                stride1 = vb1->stride;
9883464ebd5Sriastradh                offset1 = vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride;
9893464ebd5Sriastradh            }
9903464ebd5Sriastradh            if (velem[i+1].instance_divisor) {
9913464ebd5Sriastradh                stride2 = 0;
9923464ebd5Sriastradh                offset2 = vb2->buffer_offset + velem[i+1].src_offset +
9933464ebd5Sriastradh                          (instance_id / velem[i+1].instance_divisor) * vb2->stride;
9943464ebd5Sriastradh            } else {
9953464ebd5Sriastradh                stride2 = vb2->stride;
9963464ebd5Sriastradh                offset2 = vb2->buffer_offset + velem[i+1].src_offset + offset * vb2->stride;
9973464ebd5Sriastradh            }
9983464ebd5Sriastradh
9993464ebd5Sriastradh            OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(stride1) |
10003464ebd5Sriastradh                   R300_VBPNTR_SIZE1(size2) | R300_VBPNTR_STRIDE1(stride2));
10013464ebd5Sriastradh            OUT_CS(offset1);
10023464ebd5Sriastradh            OUT_CS(offset2);
10033464ebd5Sriastradh        }
10043464ebd5Sriastradh
10053464ebd5Sriastradh        if (vertex_array_count & 1) {
10063464ebd5Sriastradh            vb1 = &vbuf[velem[i].vertex_buffer_index];
10073464ebd5Sriastradh            size1 = hw_format_size[i];
10083464ebd5Sriastradh
10093464ebd5Sriastradh            if (velem[i].instance_divisor) {
10103464ebd5Sriastradh                stride1 = 0;
10113464ebd5Sriastradh                offset1 = vb1->buffer_offset + velem[i].src_offset +
10123464ebd5Sriastradh                          (instance_id / velem[i].instance_divisor) * vb1->stride;
10133464ebd5Sriastradh            } else {
10143464ebd5Sriastradh                stride1 = vb1->stride;
10153464ebd5Sriastradh                offset1 = vb1->buffer_offset + velem[i].src_offset + offset * vb1->stride;
10163464ebd5Sriastradh            }
10173464ebd5Sriastradh
10183464ebd5Sriastradh            OUT_CS(R300_VBPNTR_SIZE0(size1) | R300_VBPNTR_STRIDE0(stride1));
10193464ebd5Sriastradh            OUT_CS(offset1);
10203464ebd5Sriastradh        }
10214a49301eSmrg
10223464ebd5Sriastradh        for (i = 0; i < vertex_array_count; i++) {
102301e04c3fSmrg            buf = r300_resource(vbuf[velem[i].vertex_buffer_index].buffer.resource);
10243464ebd5Sriastradh            OUT_CS_RELOC(buf);
10253464ebd5Sriastradh        }
10264a49301eSmrg    }
10274a49301eSmrg    END_CS;
10284a49301eSmrg}
1029cdc920a0Smrg
10303464ebd5Sriastradhvoid r300_emit_vertex_arrays_swtcl(struct r300_context *r300, boolean indexed)
10313464ebd5Sriastradh{
10323464ebd5Sriastradh    CS_LOCALS(r300);
10333464ebd5Sriastradh
10343464ebd5Sriastradh    DBG(r300, DBG_SWTCL, "r300: Preparing vertex buffer %p for render, "
10353464ebd5Sriastradh            "vertex size %d\n", r300->vbo,
10363464ebd5Sriastradh            r300->vertex_info.size);
10373464ebd5Sriastradh    /* Set the pointer to our vertex buffer. The emitted values are this:
10383464ebd5Sriastradh     * PACKET3 [3D_LOAD_VBPNTR]
10393464ebd5Sriastradh     * COUNT   [1]
10403464ebd5Sriastradh     * FORMAT  [size | stride << 8]
10413464ebd5Sriastradh     * OFFSET  [offset into BO]
10423464ebd5Sriastradh     * VBPNTR  [relocated BO]
10433464ebd5Sriastradh     */
10443464ebd5Sriastradh    BEGIN_CS(7);
10453464ebd5Sriastradh    OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR, 3);
10463464ebd5Sriastradh    OUT_CS(1 | (!indexed ? R300_VC_FORCE_PREFETCH : 0));
10473464ebd5Sriastradh    OUT_CS(r300->vertex_info.size |
10483464ebd5Sriastradh            (r300->vertex_info.size << 8));
10493464ebd5Sriastradh    OUT_CS(r300->draw_vbo_offset);
10503464ebd5Sriastradh    OUT_CS(0);
1051af69d88dSmrg
105201e04c3fSmrg    assert(r300->vbo);
1053af69d88dSmrg    OUT_CS(0xc0001000); /* PKT3_NOP */
10547ec681f3Smrg    OUT_CS(r300->rws->cs_lookup_buffer(&r300->cs, r300->vbo) * 4);
10553464ebd5Sriastradh    END_CS;
10563464ebd5Sriastradh}
10573464ebd5Sriastradh
1058cdc920a0Smrgvoid r300_emit_vertex_stream_state(struct r300_context* r300,
1059cdc920a0Smrg                                   unsigned size, void* state)
10604a49301eSmrg{
1061cdc920a0Smrg    struct r300_vertex_stream_state *streams =
1062cdc920a0Smrg        (struct r300_vertex_stream_state*)state;
1063cdc920a0Smrg    unsigned i;
10644a49301eSmrg    CS_LOCALS(r300);
10654a49301eSmrg
10663464ebd5Sriastradh    if (DBG_ON(r300, DBG_PSC)) {
10673464ebd5Sriastradh        fprintf(stderr, "r300: PSC emit:\n");
10683464ebd5Sriastradh
10693464ebd5Sriastradh        for (i = 0; i < streams->count; i++) {
10703464ebd5Sriastradh            fprintf(stderr, "    : prog_stream_cntl%d: 0x%08x\n", i,
10713464ebd5Sriastradh                   streams->vap_prog_stream_cntl[i]);
10723464ebd5Sriastradh        }
10733464ebd5Sriastradh
10743464ebd5Sriastradh        for (i = 0; i < streams->count; i++) {
10753464ebd5Sriastradh            fprintf(stderr, "    : prog_stream_cntl_ext%d: 0x%08x\n", i,
10763464ebd5Sriastradh                   streams->vap_prog_stream_cntl_ext[i]);
10773464ebd5Sriastradh        }
10783464ebd5Sriastradh    }
1079cdc920a0Smrg
1080cdc920a0Smrg    BEGIN_CS(size);
1081cdc920a0Smrg    OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, streams->count);
10823464ebd5Sriastradh    OUT_CS_TABLE(streams->vap_prog_stream_cntl, streams->count);
1083cdc920a0Smrg    OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, streams->count);
10843464ebd5Sriastradh    OUT_CS_TABLE(streams->vap_prog_stream_cntl_ext, streams->count);
10854a49301eSmrg    END_CS;
10864a49301eSmrg}
10874a49301eSmrg
10883464ebd5Sriastradhvoid r300_emit_pvs_flush(struct r300_context* r300, unsigned size, void* state)
10894a49301eSmrg{
10904a49301eSmrg    CS_LOCALS(r300);
10914a49301eSmrg
1092cdc920a0Smrg    BEGIN_CS(size);
10933464ebd5Sriastradh    OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG, 0x0);
1094cdc920a0Smrg    END_CS;
1095cdc920a0Smrg}
10964a49301eSmrg
10973464ebd5Sriastradhvoid r300_emit_vap_invariant_state(struct r300_context *r300,
10983464ebd5Sriastradh                                   unsigned size, void *state)
1099cdc920a0Smrg{
1100cdc920a0Smrg    CS_LOCALS(r300);
11013464ebd5Sriastradh    WRITE_CS_TABLE(state, size);
11024a49301eSmrg}
11034a49301eSmrg
1104cdc920a0Smrgvoid r300_emit_vs_state(struct r300_context* r300, unsigned size, void* state)
11054a49301eSmrg{
1106cdc920a0Smrg    struct r300_vertex_shader* vs = (struct r300_vertex_shader*)state;
1107cdc920a0Smrg    struct r300_vertex_program_code* code = &vs->code;
11083464ebd5Sriastradh    struct r300_screen* r300screen = r300->screen;
11094a49301eSmrg    unsigned instruction_count = code->length / 4;
1110cdc920a0Smrg
11113464ebd5Sriastradh    unsigned vtx_mem_size = r300screen->caps.is_r500 ? 128 : 72;
1112cdc920a0Smrg    unsigned input_count = MAX2(util_bitcount(code->InputsRead), 1);
1113cdc920a0Smrg    unsigned output_count = MAX2(util_bitcount(code->OutputsWritten), 1);
1114cdc920a0Smrg    unsigned temp_count = MAX2(code->num_temporaries, 1);
1115cdc920a0Smrg
1116cdc920a0Smrg    unsigned pvs_num_slots = MIN3(vtx_mem_size / input_count,
1117cdc920a0Smrg                                  vtx_mem_size / output_count, 10);
11183464ebd5Sriastradh    unsigned pvs_num_controllers = MIN2(vtx_mem_size / temp_count, 5);
1119cdc920a0Smrg
11204a49301eSmrg    CS_LOCALS(r300);
11214a49301eSmrg
1122cdc920a0Smrg    BEGIN_CS(size);
11233464ebd5Sriastradh
11244a49301eSmrg    /* R300_VAP_PVS_CODE_CNTL_0
11254a49301eSmrg     * R300_VAP_PVS_CONST_CNTL
11264a49301eSmrg     * R300_VAP_PVS_CODE_CNTL_1
1127cdc920a0Smrg     * See the r5xx docs for instructions on how to use these. */
11283464ebd5Sriastradh    OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_0, R300_PVS_FIRST_INST(0) |
11293464ebd5Sriastradh	       R300_PVS_XYZW_VALID_INST(instruction_count - 1) |
11303464ebd5Sriastradh	       R300_PVS_LAST_INST(instruction_count - 1));
11313464ebd5Sriastradh    OUT_CS_REG(R300_VAP_PVS_CODE_CNTL_1, instruction_count - 1);
11324a49301eSmrg
11334a49301eSmrg    OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG, 0);
11344a49301eSmrg    OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, code->length);
11353464ebd5Sriastradh    OUT_CS_TABLE(code->body.d, code->length);
11364a49301eSmrg
1137cdc920a0Smrg    OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(pvs_num_slots) |
1138cdc920a0Smrg            R300_PVS_NUM_CNTLRS(pvs_num_controllers) |
11393464ebd5Sriastradh            R300_PVS_NUM_FPUS(r300screen->caps.num_vert_fpus) |
1140cdc920a0Smrg            R300_PVS_VF_MAX_VTX_NUM(12) |
114101e04c3fSmrg            (r300->clip_halfz ? R300_DX_CLIP_SPACE_DEF : 0) |
11423464ebd5Sriastradh            (r300screen->caps.is_r500 ? R500_TCL_STATE_OPTIMIZATION : 0));
11433464ebd5Sriastradh
1144af69d88dSmrg    /* Emit flow control instructions.  Even if there are no fc instructions,
1145af69d88dSmrg     * we still need to write the registers to make sure they are cleared. */
1146af69d88dSmrg    OUT_CS_REG(R300_VAP_PVS_FLOW_CNTL_OPC, code->fc_ops);
1147af69d88dSmrg    if (r300screen->caps.is_r500) {
1148af69d88dSmrg        OUT_CS_REG_SEQ(R500_VAP_PVS_FLOW_CNTL_ADDRS_LW_0, R300_VS_MAX_FC_OPS * 2);
1149af69d88dSmrg        OUT_CS_TABLE(code->fc_op_addrs.r500, R300_VS_MAX_FC_OPS * 2);
1150af69d88dSmrg    } else {
1151af69d88dSmrg        OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_ADDRS_0, R300_VS_MAX_FC_OPS);
1152af69d88dSmrg        OUT_CS_TABLE(code->fc_op_addrs.r300, R300_VS_MAX_FC_OPS);
11533464ebd5Sriastradh    }
1154af69d88dSmrg    OUT_CS_REG_SEQ(R300_VAP_PVS_FLOW_CNTL_LOOP_INDEX_0, R300_VS_MAX_FC_OPS);
1155af69d88dSmrg    OUT_CS_TABLE(code->fc_loop_index, R300_VS_MAX_FC_OPS);
11563464ebd5Sriastradh
11574a49301eSmrg    END_CS;
11584a49301eSmrg}
11594a49301eSmrg
11603464ebd5Sriastradhvoid r300_emit_vs_constants(struct r300_context* r300,
11613464ebd5Sriastradh                            unsigned size, void *state)
11624a49301eSmrg{
11633464ebd5Sriastradh    unsigned count =
11643464ebd5Sriastradh        ((struct r300_vertex_shader*)r300->vs_state.state)->externals_count;
11653464ebd5Sriastradh    struct r300_constant_buffer *buf = (struct r300_constant_buffer*)state;
11663464ebd5Sriastradh    struct r300_vertex_shader *vs = (struct r300_vertex_shader*)r300->vs_state.state;
11673464ebd5Sriastradh    unsigned i;
11683464ebd5Sriastradh    int imm_first = vs->externals_count;
11693464ebd5Sriastradh    int imm_end = vs->code.constants.Count;
11703464ebd5Sriastradh    int imm_count = vs->immediates_count;
11714a49301eSmrg    CS_LOCALS(r300);
11724a49301eSmrg
11733464ebd5Sriastradh    BEGIN_CS(size);
11743464ebd5Sriastradh    OUT_CS_REG(R300_VAP_PVS_CONST_CNTL,
11753464ebd5Sriastradh               R300_PVS_CONST_BASE_OFFSET(buf->buffer_base) |
11763464ebd5Sriastradh               R300_PVS_MAX_CONST_ADDR(MAX2(imm_end - 1, 0)));
11773464ebd5Sriastradh    if (vs->externals_count) {
11783464ebd5Sriastradh        OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
11793464ebd5Sriastradh                   (r300->screen->caps.is_r500 ?
11803464ebd5Sriastradh                   R500_PVS_CONST_START : R300_PVS_CONST_START) + buf->buffer_base);
11813464ebd5Sriastradh        OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, count * 4);
11823464ebd5Sriastradh        if (buf->remap_table){
11833464ebd5Sriastradh            for (i = 0; i < count; i++) {
11843464ebd5Sriastradh                uint32_t *data = &buf->ptr[buf->remap_table[i]*4];
11853464ebd5Sriastradh                OUT_CS_TABLE(data, 4);
11863464ebd5Sriastradh            }
11873464ebd5Sriastradh        } else {
11883464ebd5Sriastradh            OUT_CS_TABLE(buf->ptr, count * 4);
11893464ebd5Sriastradh        }
11904a49301eSmrg    }
11914a49301eSmrg
11923464ebd5Sriastradh    /* Emit immediates. */
11933464ebd5Sriastradh    if (imm_count) {
11943464ebd5Sriastradh        OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG,
11953464ebd5Sriastradh                   (r300->screen->caps.is_r500 ?
11963464ebd5Sriastradh                   R500_PVS_CONST_START : R300_PVS_CONST_START) +
11973464ebd5Sriastradh                   buf->buffer_base + imm_first);
11983464ebd5Sriastradh        OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, imm_count * 4);
11993464ebd5Sriastradh        for (i = imm_first; i < imm_end; i++) {
12003464ebd5Sriastradh            const float *data = vs->code.constants.Constants[i].u.Immediate;
12013464ebd5Sriastradh            OUT_CS_TABLE(data, 4);
12023464ebd5Sriastradh        }
12034a49301eSmrg    }
12044a49301eSmrg    END_CS;
12054a49301eSmrg}
12064a49301eSmrg
12074a49301eSmrgvoid r300_emit_viewport_state(struct r300_context* r300,
1208cdc920a0Smrg                              unsigned size, void* state)
12094a49301eSmrg{
1210cdc920a0Smrg    struct r300_viewport_state* viewport = (struct r300_viewport_state*)state;
12114a49301eSmrg    CS_LOCALS(r300);
12124a49301eSmrg
12133464ebd5Sriastradh    BEGIN_CS(size);
12143464ebd5Sriastradh    OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
12153464ebd5Sriastradh    OUT_CS_TABLE(&viewport->xscale, 6);
12163464ebd5Sriastradh    OUT_CS_REG(R300_VAP_VTE_CNTL, viewport->vte_control);
12173464ebd5Sriastradh    END_CS;
12183464ebd5Sriastradh}
12193464ebd5Sriastradh
12203464ebd5Sriastradhvoid r300_emit_hiz_clear(struct r300_context *r300, unsigned size, void *state)
12213464ebd5Sriastradh{
12223464ebd5Sriastradh    struct pipe_framebuffer_state *fb =
12233464ebd5Sriastradh        (struct pipe_framebuffer_state*)r300->fb_state.state;
12243464ebd5Sriastradh    struct r300_resource* tex;
12253464ebd5Sriastradh    CS_LOCALS(r300);
12263464ebd5Sriastradh
12273464ebd5Sriastradh    tex = r300_resource(fb->zsbuf->texture);
12283464ebd5Sriastradh
12293464ebd5Sriastradh    BEGIN_CS(size);
12303464ebd5Sriastradh    OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_HIZ, 2);
12313464ebd5Sriastradh    OUT_CS(0);
12323464ebd5Sriastradh    OUT_CS(tex->tex.hiz_dwords[fb->zsbuf->u.tex.level]);
12333464ebd5Sriastradh    OUT_CS(r300->hiz_clear_value);
12343464ebd5Sriastradh    END_CS;
12353464ebd5Sriastradh
12363464ebd5Sriastradh    /* Mark the current zbuffer's hiz ram as in use. */
12373464ebd5Sriastradh    r300->hiz_in_use = TRUE;
12383464ebd5Sriastradh    r300->hiz_func = HIZ_FUNC_NONE;
12393464ebd5Sriastradh    r300_mark_atom_dirty(r300, &r300->hyperz_state);
12403464ebd5Sriastradh}
12413464ebd5Sriastradh
12423464ebd5Sriastradhvoid r300_emit_zmask_clear(struct r300_context *r300, unsigned size, void *state)
12433464ebd5Sriastradh{
12443464ebd5Sriastradh    struct pipe_framebuffer_state *fb =
12453464ebd5Sriastradh        (struct pipe_framebuffer_state*)r300->fb_state.state;
12463464ebd5Sriastradh    struct r300_resource *tex;
12473464ebd5Sriastradh    CS_LOCALS(r300);
12483464ebd5Sriastradh
12493464ebd5Sriastradh    tex = r300_resource(fb->zsbuf->texture);
12503464ebd5Sriastradh
12513464ebd5Sriastradh    BEGIN_CS(size);
12523464ebd5Sriastradh    OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_ZMASK, 2);
12533464ebd5Sriastradh    OUT_CS(0);
12543464ebd5Sriastradh    OUT_CS(tex->tex.zmask_dwords[fb->zsbuf->u.tex.level]);
12553464ebd5Sriastradh    OUT_CS(0);
12563464ebd5Sriastradh    END_CS;
12573464ebd5Sriastradh
12583464ebd5Sriastradh    /* Mark the current zbuffer's zmask as in use. */
12593464ebd5Sriastradh    r300->zmask_in_use = TRUE;
12603464ebd5Sriastradh    r300_mark_atom_dirty(r300, &r300->hyperz_state);
12614a49301eSmrg}
12624a49301eSmrg
1263af69d88dSmrgvoid r300_emit_cmask_clear(struct r300_context *r300, unsigned size, void *state)
1264af69d88dSmrg{
1265af69d88dSmrg    struct pipe_framebuffer_state *fb =
1266af69d88dSmrg        (struct pipe_framebuffer_state*)r300->fb_state.state;
1267af69d88dSmrg    struct r300_resource *tex;
1268af69d88dSmrg    CS_LOCALS(r300);
1269af69d88dSmrg
1270af69d88dSmrg    tex = r300_resource(fb->cbufs[0]->texture);
1271af69d88dSmrg
1272af69d88dSmrg    BEGIN_CS(size);
1273af69d88dSmrg    OUT_CS_PKT3(R300_PACKET3_3D_CLEAR_CMASK, 2);
1274af69d88dSmrg    OUT_CS(0);
1275af69d88dSmrg    OUT_CS(tex->tex.cmask_dwords);
1276af69d88dSmrg    OUT_CS(0);
1277af69d88dSmrg    END_CS;
1278af69d88dSmrg
1279af69d88dSmrg    /* Mark the current zbuffer's zmask as in use. */
1280af69d88dSmrg    r300->cmask_in_use = TRUE;
1281af69d88dSmrg    r300_mark_fb_state_dirty(r300, R300_CHANGED_CMASK_ENABLE);
1282af69d88dSmrg}
1283af69d88dSmrg
1284cdc920a0Smrgvoid r300_emit_ztop_state(struct r300_context* r300,
1285cdc920a0Smrg                          unsigned size, void* state)
12864a49301eSmrg{
1287cdc920a0Smrg    struct r300_ztop_state* ztop = (struct r300_ztop_state*)state;
12884a49301eSmrg    CS_LOCALS(r300);
12894a49301eSmrg
1290cdc920a0Smrg    BEGIN_CS(size);
1291cdc920a0Smrg    OUT_CS_REG(R300_ZB_ZTOP, ztop->z_buffer_top);
12924a49301eSmrg    END_CS;
12934a49301eSmrg}
12944a49301eSmrg
1295cdc920a0Smrgvoid r300_emit_texture_cache_inval(struct r300_context* r300, unsigned size, void* state)
12964a49301eSmrg{
12974a49301eSmrg    CS_LOCALS(r300);
12984a49301eSmrg
1299cdc920a0Smrg    BEGIN_CS(size);
1300cdc920a0Smrg    OUT_CS_REG(R300_TX_INVALTAGS, 0);
13014a49301eSmrg    END_CS;
13024a49301eSmrg}
13034a49301eSmrg
13043464ebd5Sriastradhboolean r300_emit_buffer_validate(struct r300_context *r300,
13053464ebd5Sriastradh                                  boolean do_validate_vertex_buffers,
13063464ebd5Sriastradh                                  struct pipe_resource *index_buffer)
13074a49301eSmrg{
13083464ebd5Sriastradh    struct pipe_framebuffer_state *fb =
1309cdc920a0Smrg        (struct pipe_framebuffer_state*)r300->fb_state.state;
1310af69d88dSmrg    struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
1311cdc920a0Smrg    struct r300_textures_state *texstate =
1312cdc920a0Smrg        (struct r300_textures_state*)r300->textures_state.state;
13133464ebd5Sriastradh    struct r300_resource *tex;
1314cdc920a0Smrg    unsigned i;
13153464ebd5Sriastradh    boolean flushed = FALSE;
13164a49301eSmrg
13174a49301eSmrgvalidate:
13183464ebd5Sriastradh    if (r300->fb_state.dirty) {
13193464ebd5Sriastradh        /* Color buffers... */
13203464ebd5Sriastradh        for (i = 0; i < fb->nr_cbufs; i++) {
1321af69d88dSmrg            if (!fb->cbufs[i])
1322af69d88dSmrg                continue;
13233464ebd5Sriastradh            tex = r300_resource(fb->cbufs[i]->texture);
13243464ebd5Sriastradh            assert(tex && tex->buf && "cbuf is marked, but NULL!");
13257ec681f3Smrg            r300->rws->cs_add_buffer(&r300->cs, tex->buf,
132601e04c3fSmrg                                    RADEON_USAGE_READWRITE | RADEON_USAGE_SYNCHRONIZED,
1327af69d88dSmrg                                    r300_surface(fb->cbufs[i])->domain,
13287ec681f3Smrg                                    tex->b.nr_samples > 1 ?
1329af69d88dSmrg                                    RADEON_PRIO_COLOR_BUFFER_MSAA :
1330af69d88dSmrg                                    RADEON_PRIO_COLOR_BUFFER);
13314a49301eSmrg        }
13323464ebd5Sriastradh        /* ...depth buffer... */
13333464ebd5Sriastradh        if (fb->zsbuf) {
13343464ebd5Sriastradh            tex = r300_resource(fb->zsbuf->texture);
13353464ebd5Sriastradh            assert(tex && tex->buf && "zsbuf is marked, but NULL!");
13367ec681f3Smrg            r300->rws->cs_add_buffer(&r300->cs, tex->buf,
133701e04c3fSmrg                                    RADEON_USAGE_READWRITE | RADEON_USAGE_SYNCHRONIZED,
1338af69d88dSmrg                                    r300_surface(fb->zsbuf)->domain,
13397ec681f3Smrg                                    tex->b.nr_samples > 1 ?
1340af69d88dSmrg                                    RADEON_PRIO_DEPTH_BUFFER_MSAA :
1341af69d88dSmrg                                    RADEON_PRIO_DEPTH_BUFFER);
1342af69d88dSmrg        }
1343af69d88dSmrg    }
1344af69d88dSmrg    /* The AA resolve buffer. */
1345af69d88dSmrg    if (r300->aa_state.dirty) {
1346af69d88dSmrg        if (aa->dest) {
13477ec681f3Smrg            r300->rws->cs_add_buffer(&r300->cs, aa->dest->buf,
134801e04c3fSmrg                                    RADEON_USAGE_WRITE | RADEON_USAGE_SYNCHRONIZED,
1349af69d88dSmrg                                    aa->dest->domain,
1350af69d88dSmrg                                    RADEON_PRIO_COLOR_BUFFER);
13514a49301eSmrg        }
13524a49301eSmrg    }
13533464ebd5Sriastradh    if (r300->textures_state.dirty) {
13543464ebd5Sriastradh        /* ...textures... */
13553464ebd5Sriastradh        for (i = 0; i < texstate->count; i++) {
13567ec681f3Smrg            if (!(texstate->tx_enable & (1U << i))) {
13573464ebd5Sriastradh                continue;
13583464ebd5Sriastradh            }
13593464ebd5Sriastradh
13603464ebd5Sriastradh            tex = r300_resource(texstate->sampler_views[i]->base.texture);
13617ec681f3Smrg            r300->rws->cs_add_buffer(&r300->cs, tex->buf,
136201e04c3fSmrg                                     RADEON_USAGE_READ | RADEON_USAGE_SYNCHRONIZED,
136301e04c3fSmrg                                    tex->domain, RADEON_PRIO_SAMPLER_TEXTURE);
13644a49301eSmrg        }
13654a49301eSmrg    }
13664a49301eSmrg    /* ...occlusion query buffer... */
13673464ebd5Sriastradh    if (r300->query_current)
13687ec681f3Smrg        r300->rws->cs_add_buffer(&r300->cs, r300->query_current->buf,
136901e04c3fSmrg                                 RADEON_USAGE_WRITE | RADEON_USAGE_SYNCHRONIZED,
137001e04c3fSmrg                                 RADEON_DOMAIN_GTT,
137101e04c3fSmrg                                RADEON_PRIO_QUERY);
1372cdc920a0Smrg    /* ...vertex buffer for SWTCL path... */
137301e04c3fSmrg    if (r300->vbo)
13747ec681f3Smrg        r300->rws->cs_add_buffer(&r300->cs, r300->vbo,
137501e04c3fSmrg                                 RADEON_USAGE_READ | RADEON_USAGE_SYNCHRONIZED,
137601e04c3fSmrg                                 RADEON_DOMAIN_GTT,
137701e04c3fSmrg                                RADEON_PRIO_VERTEX_BUFFER);
1378cdc920a0Smrg    /* ...vertex buffers for HWTCL path... */
13793464ebd5Sriastradh    if (do_validate_vertex_buffers && r300->vertex_arrays_dirty) {
1380af69d88dSmrg        struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
1381af69d88dSmrg        struct pipe_vertex_buffer *last = r300->vertex_buffer +
1382af69d88dSmrg                                      r300->nr_vertex_buffers;
13833464ebd5Sriastradh        struct pipe_resource *buf;
1384af69d88dSmrg
13853464ebd5Sriastradh        for (; vbuf != last; vbuf++) {
138601e04c3fSmrg            buf = vbuf->buffer.resource;
13873464ebd5Sriastradh            if (!buf)
13883464ebd5Sriastradh                continue;
13893464ebd5Sriastradh
13907ec681f3Smrg            r300->rws->cs_add_buffer(&r300->cs, r300_resource(buf)->buf,
139101e04c3fSmrg                                    RADEON_USAGE_READ | RADEON_USAGE_SYNCHRONIZED,
1392af69d88dSmrg                                    r300_resource(buf)->domain,
139301e04c3fSmrg                                    RADEON_PRIO_SAMPLER_BUFFER);
1394cdc920a0Smrg        }
1395cdc920a0Smrg    }
1396cdc920a0Smrg    /* ...and index buffer for HWTCL path. */
13973464ebd5Sriastradh    if (index_buffer)
13987ec681f3Smrg        r300->rws->cs_add_buffer(&r300->cs, r300_resource(index_buffer)->buf,
139901e04c3fSmrg                                RADEON_USAGE_READ | RADEON_USAGE_SYNCHRONIZED,
1400af69d88dSmrg                                r300_resource(index_buffer)->domain,
140101e04c3fSmrg                                RADEON_PRIO_INDEX_BUFFER);
1402cdc920a0Smrg
14033464ebd5Sriastradh    /* Now do the validation (flush is called inside cs_validate on failure). */
14047ec681f3Smrg    if (!r300->rws->cs_validate(&r300->cs)) {
14053464ebd5Sriastradh        /* Ooops, an infinite loop, give up. */
14063464ebd5Sriastradh        if (flushed)
14073464ebd5Sriastradh            return FALSE;
14083464ebd5Sriastradh
14093464ebd5Sriastradh        flushed = TRUE;
14104a49301eSmrg        goto validate;
14114a49301eSmrg    }
14123464ebd5Sriastradh
14133464ebd5Sriastradh    return TRUE;
1414cdc920a0Smrg}
14154a49301eSmrg
1416cdc920a0Smrgunsigned r300_get_num_dirty_dwords(struct r300_context *r300)
1417cdc920a0Smrg{
1418cdc920a0Smrg    struct r300_atom* atom;
1419cdc920a0Smrg    unsigned dwords = 0;
14204a49301eSmrg
14213464ebd5Sriastradh    foreach_dirty_atom(r300, atom) {
14223464ebd5Sriastradh        if (atom->dirty) {
1423cdc920a0Smrg            dwords += atom->size;
1424cdc920a0Smrg        }
14254a49301eSmrg    }
14264a49301eSmrg
14273464ebd5Sriastradh    /* let's reserve some more, just in case */
14283464ebd5Sriastradh    dwords += 32;
14293464ebd5Sriastradh
14303464ebd5Sriastradh    return dwords;
14313464ebd5Sriastradh}
14323464ebd5Sriastradh
14333464ebd5Sriastradhunsigned r300_get_num_cs_end_dwords(struct r300_context *r300)
14343464ebd5Sriastradh{
14353464ebd5Sriastradh    unsigned dwords = 0;
14363464ebd5Sriastradh
14373464ebd5Sriastradh    /* Emitted in flush. */
14383464ebd5Sriastradh    dwords += 26; /* emit_query_end */
14393464ebd5Sriastradh    dwords += r300->hyperz_state.size + 2; /* emit_hyperz_end + zcache flush */
14403464ebd5Sriastradh    if (r300->screen->caps.is_r500)
1441af69d88dSmrg        dwords += 2; /* emit_index_bias */
144201e04c3fSmrg    dwords += 3; /* MSPOS */
14434a49301eSmrg
1444cdc920a0Smrg    return dwords;
1445cdc920a0Smrg}
1446cdc920a0Smrg
1447cdc920a0Smrg/* Emit all dirty state. */
1448cdc920a0Smrgvoid r300_emit_dirty_state(struct r300_context* r300)
1449cdc920a0Smrg{
14503464ebd5Sriastradh    struct r300_atom *atom;
1451cdc920a0Smrg
14523464ebd5Sriastradh    foreach_dirty_atom(r300, atom) {
14533464ebd5Sriastradh        if (atom->dirty) {
1454cdc920a0Smrg            atom->emit(r300, atom->size, atom->state);
1455cdc920a0Smrg            atom->dirty = FALSE;
1456cdc920a0Smrg        }
14574a49301eSmrg    }
14584a49301eSmrg
14593464ebd5Sriastradh    r300->first_dirty = NULL;
14603464ebd5Sriastradh    r300->last_dirty = NULL;
14614a49301eSmrg    r300->dirty_hw++;
14624a49301eSmrg}
1463