r300_state.c 68.2 KB
Newer Older
1
2
/*
 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3
 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE. */

24
25
#include "draw/draw_context.h"

26
#include "util/u_blitter.h"
Corbin Simpson's avatar
Corbin Simpson committed
27
#include "util/u_math.h"
28
#include "util/u_mm.h"
29
#include "util/u_memory.h"
30
#include "util/u_pack_color.h"
31

32
#include "tgsi/tgsi_parse.h"
33
34

#include "pipe/p_config.h"
Corbin Simpson's avatar
Corbin Simpson committed
35

36
#include "r300_cb.h"
Corbin Simpson's avatar
Corbin Simpson committed
37
#include "r300_context.h"
Marek Olšák's avatar
Marek Olšák committed
38
#include "r300_emit.h"
Corbin Simpson's avatar
Corbin Simpson committed
39
#include "r300_reg.h"
40
#include "r300_screen.h"
41
#include "r300_screen_buffer.h"
42
#include "r300_state_inlines.h"
43
#include "r300_fs.h"
44
#include "r300_texture.h"
45
#include "r300_vs.h"
Marek Olšák's avatar
Marek Olšák committed
46
#include "r300_winsys.h"
47
#include "r300_hyperz.h"
48

Corbin Simpson's avatar
Corbin Simpson committed
49
/* r300_state: Functions used to intialize state context by translating
50
 * Gallium state objects into semi-native r300 state objects. */
Corbin Simpson's avatar
Corbin Simpson committed
51

52
53
54
55
56
57
#define UPDATE_STATE(cso, atom) \
    if (cso != atom.state) { \
        atom.state = cso;    \
        atom.dirty = TRUE;   \
    }

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
                                            unsigned dstRGB, unsigned dstA)
{
    /* If the blend equation is ADD or REVERSE_SUBTRACT,
     * SRC_ALPHA == 0, and the following state is set, the colorbuffer
     * will not be changed.
     * Notice that the dst factors are the src factors inverted. */
    return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
            srcA == PIPE_BLENDFACTOR_ZERO) &&
           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            dstRGB == PIPE_BLENDFACTOR_ONE) &&
           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            dstA == PIPE_BLENDFACTOR_ONE);
}

static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
                                            unsigned dstRGB, unsigned dstA)
{
    /* If the blend equation is ADD or REVERSE_SUBTRACT,
     * SRC_ALPHA == 1, and the following state is set, the colorbuffer
     * will not be changed.
     * Notice that the dst factors are the src factors inverted. */
    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            srcA == PIPE_BLENDFACTOR_ZERO) &&
           (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
            dstRGB == PIPE_BLENDFACTOR_ONE) &&
           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
            dstA == PIPE_BLENDFACTOR_ONE);
}

static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
                                            unsigned dstRGB, unsigned dstA)
{
    /* If the blend equation is ADD or REVERSE_SUBTRACT,
     * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
     * will not be changed.
     * Notice that the dst factors are the src factors inverted. */
    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
           (srcA == PIPE_BLENDFACTOR_ZERO) &&
           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            dstRGB == PIPE_BLENDFACTOR_ONE) &&
           (dstA == PIPE_BLENDFACTOR_ONE);
}

static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
                                            unsigned dstRGB, unsigned dstA)
{
    /* If the blend equation is ADD or REVERSE_SUBTRACT,
     * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
     * will not be changed.
     * Notice that the dst factors are the src factors inverted. */
    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
           (srcA == PIPE_BLENDFACTOR_ZERO) &&
           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
            dstRGB == PIPE_BLENDFACTOR_ONE) &&
           (dstA == PIPE_BLENDFACTOR_ONE);
}

static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
                                                  unsigned dstRGB, unsigned dstA)
{
    /* If the blend equation is ADD or REVERSE_SUBTRACT,
     * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
     * the colorbuffer will not be changed.
     * Notice that the dst factors are the src factors inverted. */
    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
            srcA == PIPE_BLENDFACTOR_ZERO) &&
           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            dstRGB == PIPE_BLENDFACTOR_ONE) &&
           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            dstA == PIPE_BLENDFACTOR_ONE);
}

static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
                                                  unsigned dstRGB, unsigned dstA)
{
    /* If the blend equation is ADD or REVERSE_SUBTRACT,
     * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
     * the colorbuffer will not be changed.
     * Notice that the dst factors are the src factors inverted. */
    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
            srcA == PIPE_BLENDFACTOR_ZERO) &&
           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
            dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
            dstRGB == PIPE_BLENDFACTOR_ONE) &&
           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
            dstA == PIPE_BLENDFACTOR_ONE);
}

Marek Olšák's avatar
Marek Olšák committed
172
173
174
175
176
177
178
179
180
static unsigned bgra_cmask(unsigned mask)
{
    /* Gallium uses RGBA color ordering while R300 expects BGRA. */

    return ((mask & PIPE_MASK_R) << 2) |
           ((mask & PIPE_MASK_B) >> 2) |
           (mask & (PIPE_MASK_G | PIPE_MASK_A));
}

181
182
183
/* Create a new blend state based on the CSO blend state.
 *
 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
Corbin Simpson's avatar
Corbin Simpson committed
184
static void* r300_create_blend_state(struct pipe_context* pipe,
185
                                     const struct pipe_blend_state* state)
Corbin Simpson's avatar
Corbin Simpson committed
186
{
187
    struct r300_screen* r300screen = r300_screen(pipe->screen);
Corbin Simpson's avatar
Corbin Simpson committed
188
    struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
189
190
191
192
193
194
    uint32_t blend_control = 0;       /* R300_RB3D_CBLEND: 0x4e04 */
    uint32_t alpha_blend_control = 0; /* R300_RB3D_ABLEND: 0x4e08 */
    uint32_t color_channel_mask = 0;  /* R300_RB3D_COLOR_CHANNEL_MASK: 0x4e0c */
    uint32_t rop = 0;                 /* R300_RB3D_ROPCNTL: 0x4e18 */
    uint32_t dither = 0;              /* R300_RB3D_DITHER_CTL: 0x4e50 */
    CB_LOCALS;
Corbin Simpson's avatar
Corbin Simpson committed
195

196
    if (state->rt[0].blend_enable)
197
    {
198
199
200
        unsigned eqRGB = state->rt[0].rgb_func;
        unsigned srcRGB = state->rt[0].rgb_src_factor;
        unsigned dstRGB = state->rt[0].rgb_dst_factor;
201

202
203
204
        unsigned eqA = state->rt[0].alpha_func;
        unsigned srcA = state->rt[0].alpha_src_factor;
        unsigned dstA = state->rt[0].alpha_dst_factor;
205
206
207

        /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
         * this is just the crappy D3D naming */
208
        blend_control = R300_ALPHA_BLEND_ENABLE |
209
210
211
212
            r300_translate_blend_function(eqRGB) |
            ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
            ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);

213
214
215
216
217
        /* Optimization: some operations do not require the destination color.
         *
         * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
         * otherwise blending gives incorrect results. It seems to be
         * a hardware bug. */
218
219
220
221
222
223
224
225
        if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
            eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
            dstRGB != PIPE_BLENDFACTOR_ZERO ||
            dstA != PIPE_BLENDFACTOR_ZERO ||
            srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
            srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
            srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
            srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
226
            srcA == PIPE_BLENDFACTOR_DST_COLOR ||
227
            srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
228
            srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
229
            srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
230
231
            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) {
            /* Enable reading from the colorbuffer. */
232
            blend_control |= R300_READ_ENABLE;
233

234
            if (r300screen->caps.is_r500) {
235
236
237
238
239
240
241
242
243
244
                /* Optimization: Depending on incoming pixels, we can
                 * conditionally disable the reading in hardware... */
                if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
                    eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
                    /* Disable reading if SRC_ALPHA == 0. */
                    if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
                         dstRGB == PIPE_BLENDFACTOR_ZERO) &&
                        (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
                         dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
                         dstA == PIPE_BLENDFACTOR_ZERO)) {
245
                         blend_control |= R500_SRC_ALPHA_0_NO_READ;
246
247
248
249
250
251
252
253
                    }

                    /* Disable reading if SRC_ALPHA == 1. */
                    if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
                         dstRGB == PIPE_BLENDFACTOR_ZERO) &&
                        (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
                         dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
                         dstA == PIPE_BLENDFACTOR_ZERO)) {
254
                         blend_control |= R500_SRC_ALPHA_1_NO_READ;
255
256
257
258
259
                    }
                }
            }
        }

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        /* Optimization: discard pixels which don't change the colorbuffer.
         *
         * The code below is non-trivial and some math is involved.
         *
         * Discarding pixels must be disabled when FP16 AA is enabled.
         * This is a hardware bug. Also, this implementation wouldn't work
         * with FP blending enabled and equation clamping disabled.
         *
         * Equations other than ADD are rarely used and therefore won't be
         * optimized. */
        if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
            (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
            /* ADD: X+Y
             * REVERSE_SUBTRACT: Y-X
             *
             * The idea is:
             * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
             * then CB will not be changed.
             *
             * Given the srcFactor and dstFactor variables, we can derive
             * what src and dst should be equal to and discard appropriate
             * pixels.
             */
            if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
284
                blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
285
286
            } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
                                                    dstRGB, dstA)) {
287
                blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
288
289
            } else if (blend_discard_if_src_color_0(srcRGB, srcA,
                                                    dstRGB, dstA)) {
290
                blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
291
292
            } else if (blend_discard_if_src_color_1(srcRGB, srcA,
                                                    dstRGB, dstA)) {
293
                blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
294
295
            } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
                                                          dstRGB, dstA)) {
296
                blend_control |=
297
298
299
                    R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
            } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
                                                          dstRGB, dstA)) {
300
                blend_control |=
301
302
303
304
                    R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
            }
        }

305
306
        /* separate alpha */
        if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
307
308
            blend_control |= R300_SEPARATE_ALPHA_ENABLE;
            alpha_blend_control =
309
310
311
312
                r300_translate_blend_function(eqA) |
                (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
                (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
        }
Corbin Simpson's avatar
Corbin Simpson committed
313
314
315
316
    }

    /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
    if (state->logicop_enable) {
317
        rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
Corbin Simpson's avatar
Corbin Simpson committed
318
319
320
                (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
    }

321
    /* Color channel masks for all MRTs. */
322
    color_channel_mask = bgra_cmask(state->rt[0].colormask);
323
    if (r300screen->caps.is_r500 && state->independent_blend_enable) {
324
        if (state->rt[1].blend_enable) {
325
            color_channel_mask |= bgra_cmask(state->rt[1].colormask) << 4;
326
327
        }
        if (state->rt[2].blend_enable) {
328
            color_channel_mask |= bgra_cmask(state->rt[2].colormask) << 8;
329
330
        }
        if (state->rt[3].blend_enable) {
331
            color_channel_mask |= bgra_cmask(state->rt[3].colormask) << 12;
332
        }
333
334
    }

335
336
337
338
339
340
    /* Neither fglrx nor classic r300 ever set this, regardless of dithering
     * state. Since it's an optional implementation detail, we can leave it
     * out and never dither.
     *
     * This could be revisited if we ever get quality or conformance hints.
     *
Corbin Simpson's avatar
Corbin Simpson committed
341
    if (state->dither) {
342
        dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
343
                        R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
Corbin Simpson's avatar
Corbin Simpson committed
344
    }
345
    */
Corbin Simpson's avatar
Corbin Simpson committed
346

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
    /* Build a command buffer. */
    BEGIN_CB(blend->cb, 8);
    OUT_CB_REG(R300_RB3D_ROPCNTL, rop);
    OUT_CB_REG_SEQ(R300_RB3D_CBLEND, 3);
    OUT_CB(blend_control);
    OUT_CB(alpha_blend_control);
    OUT_CB(color_channel_mask);
    OUT_CB_REG(R300_RB3D_DITHER_CTL, dither);
    END_CB;

    /* The same as above, but with no colorbuffer reads and writes. */
    BEGIN_CB(blend->cb_no_readwrite, 8);
    OUT_CB_REG(R300_RB3D_ROPCNTL, rop);
    OUT_CB_REG_SEQ(R300_RB3D_CBLEND, 3);
    OUT_CB(0);
    OUT_CB(0);
    OUT_CB(0);
    OUT_CB_REG(R300_RB3D_DITHER_CTL, dither);
    END_CB;

Corbin Simpson's avatar
Corbin Simpson committed
367
368
369
    return (void*)blend;
}

370
/* Bind blend state. */
Corbin Simpson's avatar
Corbin Simpson committed
371
372
373
374
375
static void r300_bind_blend_state(struct pipe_context* pipe,
                                  void* state)
{
    struct r300_context* r300 = r300_context(pipe);

376
    UPDATE_STATE(state, r300->blend_state);
Corbin Simpson's avatar
Corbin Simpson committed
377
378
}

379
/* Free blend state. */
Corbin Simpson's avatar
Corbin Simpson committed
380
381
382
383
384
static void r300_delete_blend_state(struct pipe_context* pipe,
                                    void* state)
{
    FREE(state);
}
Corbin Simpson's avatar
Corbin Simpson committed
385

386
387
388
389
390
391
/* Convert float to 10bit integer */
static unsigned float_to_fixed10(float f)
{
    return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
}

Corbin Simpson's avatar
Corbin Simpson committed
392
393
394
395
396
397
/* Set blend color.
 * Setup both R300 and R500 registers, figure out later which one to write. */
static void r300_set_blend_color(struct pipe_context* pipe,
                                 const struct pipe_blend_color* color)
{
    struct r300_context* r300 = r300_context(pipe);
Corbin Simpson's avatar
Corbin Simpson committed
398
399
    struct r300_blend_color_state* state =
        (struct r300_blend_color_state*)r300->blend_color_state.state;
400
    CB_LOCALS;
Corbin Simpson's avatar
Corbin Simpson committed
401

402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    if (r300->screen->caps.is_r500) {
        /* XXX if FP16 blending is enabled, we should use the FP16 format */
        BEGIN_CB(state->cb, 3);
        OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
        OUT_CB(float_to_fixed10(color->color[0]) |
               (float_to_fixed10(color->color[3]) << 16));
        OUT_CB(float_to_fixed10(color->color[2]) |
               (float_to_fixed10(color->color[1]) << 16));
        END_CB;
    } else {
        union util_color uc;
        util_pack_color(color->color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);

        BEGIN_CB(state->cb, 2);
        OUT_CB_REG(R300_RB3D_BLEND_COLOR, uc.ui);
        END_CB;
    }
Corbin Simpson's avatar
Corbin Simpson committed
419

Corbin Simpson's avatar
Corbin Simpson committed
420
    r300->blend_color_state.dirty = TRUE;
Corbin Simpson's avatar
Corbin Simpson committed
421
422
}

Corbin Simpson's avatar
Corbin Simpson committed
423
424
425
426
static void r300_set_clip_state(struct pipe_context* pipe,
                                const struct pipe_clip_state* state)
{
    struct r300_context* r300 = r300_context(pipe);
427
428
429
    struct r300_clip_state *clip =
            (struct r300_clip_state*)r300->clip_state.state;
    CB_LOCALS;
Corbin Simpson's avatar
Corbin Simpson committed
430

431
    clip->clip = *state;
432

433
    if (r300->screen->caps.has_tcl) {
434
435
436
437
438
439
440
441
442
443
        r300->clip_state.size = 2 + !!state->nr * 3 + state->nr * 4;

        BEGIN_CB(clip->cb, r300->clip_state.size);
        if (state->nr) {
           OUT_CB_REG(R300_VAP_PVS_VECTOR_INDX_REG,
                   (r300->screen->caps.is_r500 ?
                    R500_PVS_UCP_START : R300_PVS_UCP_START));
           OUT_CB_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, state->nr * 4);
           OUT_CB_TABLE(state->ucp, state->nr * 4);
        }
444
        OUT_CB_REG(R300_VAP_CLIP_CNTL, ((1 << state->nr) - 1) |
Marek Olšák's avatar
Marek Olšák committed
445
446
                R300_PS_UCP_MODE_CLIP_AS_TRIFAN |
                (state->depth_clamp ? R300_CLIP_DISABLE : 0));
447
        END_CB;
448
449

        r300->clip_state.dirty = TRUE;
450
451
452
    } else {
        draw_set_clip_state(r300->draw, state);
    }
Corbin Simpson's avatar
Corbin Simpson committed
453
454
}

455
456
457
458
459
460
461
static void
r300_set_sample_mask(struct pipe_context *pipe,
                     unsigned sample_mask)
{
}


Corbin Simpson's avatar
Corbin Simpson committed
462
463
464
465
466
/* Create a new depth, stencil, and alpha state based on the CSO dsa state.
 *
 * This contains the depth buffer, stencil buffer, alpha test, and such.
 * On the Radeon, depth and stencil buffer setup are intertwined, which is
 * the reason for some of the strange-looking assignments across registers. */
467
468
469
static void*
        r300_create_dsa_state(struct pipe_context* pipe,
                              const struct pipe_depth_stencil_alpha_state* state)
Corbin Simpson's avatar
Corbin Simpson committed
470
{
471
    struct r300_capabilities *caps = &r300_screen(pipe->screen)->caps;
Corbin Simpson's avatar
Corbin Simpson committed
472
    struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
473
    CB_LOCALS;
Corbin Simpson's avatar
Corbin Simpson committed
474

475
476
    dsa->dsa = *state;

477
478
479
480
481
    /* Depth test setup. - separate write mask depth for decomp flush */
    if (state->depth.writemask) {
        dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
    }

Corbin Simpson's avatar
Corbin Simpson committed
482
483
484
485
    if (state->depth.enabled) {
        dsa->z_buffer_control |= R300_Z_ENABLE;

        dsa->z_stencil_control |=
486
            (r300_translate_depth_stencil_function(state->depth.func) <<
487
                R300_Z_FUNC_SHIFT);
Corbin Simpson's avatar
Corbin Simpson committed
488
489
490
491
492
493
    }

    /* Stencil buffer setup. */
    if (state->stencil[0].enabled) {
        dsa->z_buffer_control |= R300_STENCIL_ENABLE;
        dsa->z_stencil_control |=
494
495
496
497
498
499
500
501
            (r300_translate_depth_stencil_function(state->stencil[0].func) <<
                R300_S_FRONT_FUNC_SHIFT) |
            (r300_translate_stencil_op(state->stencil[0].fail_op) <<
                R300_S_FRONT_SFAIL_OP_SHIFT) |
            (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
                R300_S_FRONT_ZPASS_OP_SHIFT) |
            (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
                R300_S_FRONT_ZFAIL_OP_SHIFT);
Corbin Simpson's avatar
Corbin Simpson committed
502

503
        dsa->stencil_ref_mask =
Corbin Simpson's avatar
Corbin Simpson committed
504
505
                (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
                (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
Corbin Simpson's avatar
Corbin Simpson committed
506
507

        if (state->stencil[1].enabled) {
508
509
            dsa->two_sided = TRUE;

Corbin Simpson's avatar
Corbin Simpson committed
510
511
            dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
            dsa->z_stencil_control |=
512
513
514
515
516
517
518
519
            (r300_translate_depth_stencil_function(state->stencil[1].func) <<
                R300_S_BACK_FUNC_SHIFT) |
            (r300_translate_stencil_op(state->stencil[1].fail_op) <<
                R300_S_BACK_SFAIL_OP_SHIFT) |
            (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
                R300_S_BACK_ZPASS_OP_SHIFT) |
            (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
                R300_S_BACK_ZFAIL_OP_SHIFT);
Corbin Simpson's avatar
Corbin Simpson committed
520

521
522
523
524
525
            dsa->stencil_ref_bf =
                (state->stencil[1].valuemask << R300_STENCILMASK_SHIFT) |
                (state->stencil[1].writemask << R300_STENCILWRITEMASK_SHIFT);

            if (caps->is_r500) {
526
                dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
527
            } else {
528
                dsa->two_sided_stencil_ref =
529
530
                  (state->stencil[0].valuemask != state->stencil[1].valuemask ||
                   state->stencil[0].writemask != state->stencil[1].writemask);
531
            }
Corbin Simpson's avatar
Corbin Simpson committed
532
533
534
535
536
        }
    }

    /* Alpha test setup. */
    if (state->alpha.enabled) {
537
538
        dsa->alpha_function =
            r300_translate_alpha_function(state->alpha.func) |
539
            R300_FG_ALPHA_FUNC_ENABLE;
540

541
        /* We could use 10bit alpha ref but who needs that? */
542
543
544
545
        dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);

        if (caps->is_r500)
            dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
Corbin Simpson's avatar
Corbin Simpson committed
546
547
    }

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    BEGIN_CB(&dsa->cb_begin, 8);
    OUT_CB_REG(R300_FG_ALPHA_FUNC, dsa->alpha_function);
    OUT_CB_REG_SEQ(R300_ZB_CNTL, 3);
    OUT_CB(dsa->z_buffer_control);
    OUT_CB(dsa->z_stencil_control);
    OUT_CB(dsa->stencil_ref_mask);
    OUT_CB_REG(R500_ZB_STENCILREFMASK_BF, dsa->stencil_ref_bf);
    END_CB;

    BEGIN_CB(dsa->cb_no_readwrite, 8);
    OUT_CB_REG(R300_FG_ALPHA_FUNC, dsa->alpha_function);
    OUT_CB_REG_SEQ(R300_ZB_CNTL, 3);
    OUT_CB(0);
    OUT_CB(0);
    OUT_CB(0);
    OUT_CB_REG(R500_ZB_STENCILREFMASK_BF, 0);
    END_CB;

Corbin Simpson's avatar
Corbin Simpson committed
566
567
568
    return (void*)dsa;
}

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
static void r300_dsa_inject_stencilref(struct r300_context *r300)
{
    struct r300_dsa_state *dsa =
            (struct r300_dsa_state*)r300->dsa_state.state;

    if (!dsa)
        return;

    dsa->stencil_ref_mask =
        (dsa->stencil_ref_mask & ~R300_STENCILREF_MASK) |
        r300->stencil_ref.ref_value[0];
    dsa->stencil_ref_bf =
        (dsa->stencil_ref_bf & ~R300_STENCILREF_MASK) |
        r300->stencil_ref.ref_value[1];
}

Corbin Simpson's avatar
Corbin Simpson committed
585
586
/* Bind DSA state. */
static void r300_bind_dsa_state(struct pipe_context* pipe,
587
                                void* state)
Corbin Simpson's avatar
Corbin Simpson committed
588
589
590
{
    struct r300_context* r300 = r300_context(pipe);

591
592
593
594
    if (!state) {
        return;
    }

595
    UPDATE_STATE(state, r300->dsa_state);
596

597
    r300->hyperz_state.dirty = TRUE; /* Will be updated before the emission. */
598
    r300_dsa_inject_stencilref(r300);
Corbin Simpson's avatar
Corbin Simpson committed
599
600
601
602
}

/* Free DSA state. */
static void r300_delete_dsa_state(struct pipe_context* pipe,
603
                                  void* state)
Corbin Simpson's avatar
Corbin Simpson committed
604
605
606
{
    FREE(state);
}
607

608
609
610
611
static void r300_set_stencil_ref(struct pipe_context* pipe,
                                 const struct pipe_stencil_ref* sr)
{
    struct r300_context* r300 = r300_context(pipe);
612

613
    r300->stencil_ref = *sr;
614
615

    r300_dsa_inject_stencilref(r300);
616
617
618
    r300->dsa_state.dirty = TRUE;
}

619
620
static void r300_tex_set_tiling_flags(struct r300_context *r300,
                                      struct r300_texture *tex, unsigned level)
621
{
622
623
    /* Check if the macrotile flag needs to be changed.
     * Skip changing the flags otherwise. */
624
625
    if (tex->desc.macrotile[tex->surface_level] !=
        tex->desc.macrotile[level]) {
626
627
        /* Tiling determines how DRM treats the buffer data.
         * We must flush CS when changing it if the buffer is referenced. */
628
629
        if (r300->rws->cs_is_buffer_referenced(r300->cs,
                                               tex->buffer, R300_REF_CS))
630
            r300->context.flush(&r300->context, 0, NULL);
631

632
        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
633
634
                tex->desc.microtile, tex->desc.macrotile[level],
                tex->desc.stride_in_bytes[0]);
635
636

        tex->surface_level = level;
637
    }
638
}
639

640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
static void r300_fb_set_tiling_flags(struct r300_context *r300,
                               const struct pipe_framebuffer_state *state)
{
    unsigned i;

    /* Set tiling flags for new surfaces. */
    for (i = 0; i < state->nr_cbufs; i++) {
        r300_tex_set_tiling_flags(r300,
                                  r300_texture(state->cbufs[i]->texture),
                                  state->cbufs[i]->level);
    }
    if (state->zsbuf) {
        r300_tex_set_tiling_flags(r300,
                                  r300_texture(state->zsbuf->texture),
                                  state->zsbuf->level);
656
657
658
    }
}

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
static void r300_print_fb_surf_info(struct pipe_surface *surf, unsigned index,
                                    const char *binding)
{
    struct pipe_resource *tex = surf->texture;
    struct r300_texture *rtex = r300_texture(tex);

    fprintf(stderr,
            "r300:   %s[%i] Dim: %ix%i, Offset: %i, ZSlice: %i, "
            "Face: %i, Level: %i, Format: %s\n"

            "r300:     TEX: Macro: %s, Micro: %s, Pitch: %i, "
            "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",

            binding, index, surf->width, surf->height, surf->offset,
            surf->zslice, surf->face, surf->level,
            util_format_short_name(surf->format),

676
677
678
679
            rtex->desc.macrotile[0] ? "YES" : " NO",
            rtex->desc.microtile ? "YES" : " NO",
            rtex->desc.stride_in_pixels[0],
            tex->width0, tex->height0, tex->depth0,
680
681
682
            tex->last_level, util_format_short_name(tex->format));
}

683
684
685
686
void r300_mark_fb_state_dirty(struct r300_context *r300,
                              enum r300_fb_state_change change)
{
    struct pipe_framebuffer_state *state = r300->fb_state.state;
Marek Olšák's avatar
Marek Olšák committed
687
    boolean has_hyperz = r300->rws->get_value(r300->rws, R300_CAN_HYPERZ);
688
689
690
691

    /* What is marked as dirty depends on the enum r300_fb_state_change. */
    r300->gpu_flush.dirty = TRUE;
    r300->fb_state.dirty = TRUE;
692
    r300->hyperz_state.dirty = TRUE;
693
694
695
696
697
698
699
700
701

    if (change == R300_CHANGED_FB_STATE) {
        r300->aa_state.dirty = TRUE;
        r300->fb_state_pipelined.dirty = TRUE;
    }

    /* Now compute the fb_state atom size. */
    r300->fb_state.size = 2 + (8 * state->nr_cbufs);

702
703
    if (r300->cbzb_clear)
        r300->fb_state.size += 10;
Marek Olšák's avatar
Marek Olšák committed
704
705
706
707
708
    else if (state->zsbuf) {
        r300->fb_state.size += 10;
        if (has_hyperz)
            r300->fb_state.size += r300->screen->caps.hiz_ram ? 8 : 4;
    }
709
710
711
712

    /* The size of the rest of atoms stays the same. */
}

Corbin Simpson's avatar
Corbin Simpson committed
713
714
715
716
717
static void
    r300_set_framebuffer_state(struct pipe_context* pipe,
                               const struct pipe_framebuffer_state* state)
{
    struct r300_context* r300 = r300_context(pipe);
718
    struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
719
    struct pipe_framebuffer_state *old_state = r300->fb_state.state;
720
    boolean has_hyperz = r300->rws->get_value(r300->rws, R300_CAN_HYPERZ);
721
    unsigned max_width, max_height, i;
Marek Olšák's avatar
Marek Olšák committed
722
    uint32_t zbuffer_bpp = 0;
723
    int blocksize;
Corbin Simpson's avatar
Corbin Simpson committed
724

725
    if (r300->screen->caps.is_r500) {
726
        max_width = max_height = 4096;
727
    } else if (r300->screen->caps.is_r400) {
728
729
730
731
732
733
        max_width = max_height = 4021;
    } else {
        max_width = max_height = 2560;
    }

    if (state->width > max_width || state->height > max_height) {
734
        fprintf(stderr, "r300: Implementation error: Render targets are too "
735
736
737
738
        "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__);
        return;
    }

739
740
741
742
743
744
745
746
    /* If nr_cbufs is changed from zero to non-zero or vice versa... */
    if (!!old_state->nr_cbufs != !!state->nr_cbufs) {
        r300->blend_state.dirty = TRUE;
    }
    /* If zsbuf is set from NULL to non-NULL or vice versa.. */
    if (!!old_state->zsbuf != !!state->zsbuf) {
        r300->dsa_state.dirty = TRUE;
    }
747

748
    /* The tiling flags are dependent on the surface miplevel, unfortunately. */
749
    r300_fb_set_tiling_flags(r300, state);
750

751
    util_assign_framebuffer_state(r300->fb_state.state, state);
Corbin Simpson's avatar
Corbin Simpson committed
752

753
    r300_mark_fb_state_dirty(r300, R300_CHANGED_FB_STATE);
Marek Olšák's avatar
Marek Olšák committed
754

755
756
    r300->z_compression = false;
    
757
    if (state->zsbuf) {
758
759
760
761
762
763
764
765
        blocksize = util_format_get_blocksize(state->zsbuf->texture->format);
        switch (blocksize) {
        case 2:
            zbuffer_bpp = 16;
            break;
        case 4:
            zbuffer_bpp = 24;
            break;
Marek Olšák's avatar
Marek Olšák committed
766
        }
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
        if (has_hyperz) {
            struct r300_surface *zs_surf = r300_surface(state->zsbuf);
            struct r300_texture *tex;
            int compress = r300->screen->caps.is_rv350 ? RV350_Z_COMPRESS_88 : R300_Z_COMPRESS_44;
            int level = zs_surf->base.level;

            tex = r300_texture(zs_surf->base.texture);

            /* work out whether we can support hiz on this buffer */
            r300_hiz_alloc_block(r300, zs_surf);
        
            /* work out whether we can support zmask features on this buffer */
            r300_zmask_alloc_block(r300, zs_surf, compress);

            if (tex->zmask_mem[level]) {
                /* compression causes hangs on 16-bit */
                if (zbuffer_bpp == 24)
                    r300->z_compression = compress;
            }
            DBG(r300, DBG_HYPERZ,
787
                "hyper-z features: hiz: %d @ %08x z-compression: %d z-fastfill: %d @ %08x\n", tex->hiz_mem[level] ? 1 : 0,
788
                tex->hiz_mem[level] ? tex->hiz_mem[level]->ofs : 0xdeadbeef,
789
                r300->z_compression, tex->zmask_mem[level] ? 1 : 0,
790
791
                tex->zmask_mem[level] ? tex->zmask_mem[level]->ofs : 0xdeadbeef);
        }
792

793
        /* Polygon offset depends on the zbuffer bit depth. */
Marek Olšák's avatar
Marek Olšák committed
794
795
        if (r300->zbuffer_bpp != zbuffer_bpp) {
            r300->zbuffer_bpp = zbuffer_bpp;
796
797
798

            if (r300->polygon_offset_enabled)
                r300->rs_state.dirty = TRUE;
Marek Olšák's avatar
Marek Olšák committed
799
800
        }
    }
801

802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
    /* Set up AA config. */
    if (r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0)) {
        if (state->nr_cbufs && state->cbufs[0]->texture->nr_samples > 1) {
            aa->aa_config = R300_GB_AA_CONFIG_AA_ENABLE;

            switch (state->cbufs[0]->texture->nr_samples) {
                case 2:
                    aa->aa_config |= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_2;
                    break;
                case 3:
                    aa->aa_config |= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_3;
                    break;
                case 4:
                    aa->aa_config |= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_4;
                    break;
                case 6:
                    aa->aa_config |= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_6;
                    break;
            }
        } else {
            aa->aa_config = 0;
        }
    }

826
827
828
829
830
831
832
833
834
    if (DBG_ON(r300, DBG_FB)) {
        fprintf(stderr, "r300: set_framebuffer_state:\n");
        for (i = 0; i < state->nr_cbufs; i++) {
            r300_print_fb_surf_info(state->cbufs[i], i, "CB");
        }
        if (state->zsbuf) {
            r300_print_fb_surf_info(state->zsbuf, 0, "ZB");
        }
    }
Corbin Simpson's avatar
Corbin Simpson committed
835
836
}

837
838
/* Create fragment shader state. */
static void* r300_create_fs_state(struct pipe_context* pipe,
Corbin Simpson's avatar
Corbin Simpson committed
839
                                  const struct pipe_shader_state* shader)
840
{
841
    struct r300_fragment_shader* fs = NULL;
Corbin Simpson's avatar
Corbin Simpson committed
842

843
    fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
Corbin Simpson's avatar
Corbin Simpson committed
844
845
846

    /* Copy state directly into shader. */
    fs->state = *shader;
847
    fs->state.tokens = tgsi_dup_tokens(shader->tokens);
848
849
850
851

    return (void*)fs;
}

852
void r300_mark_fs_code_dirty(struct r300_context *r300)
Marek Olšák's avatar
Marek Olšák committed
853
854
855
856
{
    struct r300_fragment_shader* fs = r300_fs(r300);

    r300->fs.dirty = TRUE;
857
    r300->fs_rc_constant_state.dirty = TRUE;
858
    r300->fs_constants.dirty = TRUE;
859
    r300->fs.size = fs->shader->cb_code_size;
Marek Olšák's avatar
Marek Olšák committed
860
861

    if (r300->screen->caps.is_r500) {
862
        r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 7;
863
        r300->fs_constants.size = fs->shader->externals_count * 4 + 3;
Marek Olšák's avatar
Marek Olšák committed
864
    } else {
865
        r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 5;
866
        r300->fs_constants.size = fs->shader->externals_count * 4 + 1;
Marek Olšák's avatar
Marek Olšák committed
867
    }
868
869
870

    ((struct r300_constant_buffer*)r300->fs_constants.state)->remap_table =
            fs->shader->code.constants_remap_table;
Marek Olšák's avatar
Marek Olšák committed
871
872
}

873
/* Bind fragment shader state. */
Corbin Simpson's avatar
Corbin Simpson committed
874
static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
875
876
{
    struct r300_context* r300 = r300_context(pipe);
877
    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
878

879
    if (fs == NULL) {
Marek Olšák's avatar
Marek Olšák committed
880
        r300->fs.state = NULL;
881
        return;
Corbin Simpson's avatar
Corbin Simpson committed
882
883
    }

Marek Olšák's avatar
Marek Olšák committed
884
    r300->fs.state = fs;
885
    r300_pick_fragment_shader(r300);
Marek Olšák's avatar
Marek Olšák committed
886
    r300_mark_fs_code_dirty(r300);
887

888
    r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
889
890
}

Corbin Simpson's avatar
Corbin Simpson committed
891
892
/* Delete fragment shader state. */
static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
893
{
894
    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
895
896
897
898
899
900
    struct r300_fragment_shader_code *tmp, *ptr = fs->first;

    while (ptr) {
        tmp = ptr;
        ptr = ptr->next;
        rc_constants_destroy(&tmp->code.constants);
901
        FREE(tmp->cb_code);
902
903
        FREE(tmp);
    }
904
    FREE((void*)fs->state.tokens);
Corbin Simpson's avatar
Corbin Simpson committed
905
    FREE(shader);
906
907
}

Corbin Simpson's avatar
Corbin Simpson committed
908
909
910
static void r300_set_polygon_stipple(struct pipe_context* pipe,
                                     const struct pipe_poly_stipple* state)
{
Corbin Simpson's avatar
Corbin Simpson committed
911
    /* XXX no idea how to set this up, but not terribly important */
Corbin Simpson's avatar
Corbin Simpson committed
912
913
}

Corbin Simpson's avatar
Corbin Simpson committed
914
915
916
917
918
919
920
921
/* Create a new rasterizer state based on the CSO rasterizer state.
 *
 * This is a very large chunk of state, and covers most of the graphics
 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
 *
 * In a not entirely unironic sidenote, this state has nearly nothing to do
 * with the actual block on the Radeon called the rasterizer (RS). */
static void* r300_create_rs_state(struct pipe_context* pipe,
922
                                  const struct pipe_rasterizer_state* state)
Corbin Simpson's avatar
Corbin Simpson committed
923
924
{
    struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
925
    int i;
926
    float psiz;
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
    uint32_t vap_control_status;    /* R300_VAP_CNTL_STATUS: 0x2140 */
    uint32_t point_size;            /* R300_GA_POINT_SIZE: 0x421c */
    uint32_t point_minmax;          /* R300_GA_POINT_MINMAX: 0x4230 */
    uint32_t line_control;          /* R300_GA_LINE_CNTL: 0x4234 */
    uint32_t polygon_offset_enable; /* R300_SU_POLY_OFFSET_ENABLE: 0x42b4 */
    uint32_t cull_mode;             /* R300_SU_CULL_MODE: 0x42b8 */
    uint32_t line_stipple_config;   /* R300_GA_LINE_STIPPLE_CONFIG: 0x4328 */
    uint32_t line_stipple_value;    /* R300_GA_LINE_STIPPLE_VALUE: 0x4260 */
    uint32_t polygon_mode;          /* R300_GA_POLY_MODE: 0x4288 */
    uint32_t clip_rule;             /* R300_SC_CLIP_RULE: 0x43D0 */

    /* Specifies top of Raster pipe specific enable controls,
     * i.e. texture coordinates stuffing for points, lines, triangles */
    uint32_t stuffing_enable;       /* R300_GB_ENABLE: 0x4008 */

    /* Point sprites texture coordinates, 0: lower left, 1: upper right */
    float point_texcoord_left;      /* R300_GA_POINT_S0: 0x4200 */
Marek Olšák's avatar
Marek Olšák committed
944
    float point_texcoord_bottom = 0;/* R300_GA_POINT_T0: 0x4204 */
945
    float point_texcoord_right;     /* R300_GA_POINT_S1: 0x4208 */
Marek Olšák's avatar
Marek Olšák committed
946
    float point_texcoord_top = 0;   /* R300_GA_POINT_T1: 0x420c */
947
    CB_LOCALS;
Corbin Simpson's avatar
Corbin Simpson committed
948

949
    /* Copy rasterizer state. */
950
    rs->rs = *state;
951
952
953
954
    rs->rs_draw = *state;

    /* Override some states for Draw. */
    rs->rs_draw.sprite_coord_enable = 0; /* We can do this in HW. */
955

956
#ifdef PIPE_ARCH_LITTLE_ENDIAN
957
    vap_control_status = R300_VC_NO_SWAP;
958
#else
959
    vap_control_status = R300_VC_32BIT_SWAP;
960
961
#endif

962
    /* If no TCL engine is present, turn off the HW TCL. */
963
    if (!r300_screen(pipe->screen)->caps.has_tcl) {
964
        vap_control_status |= R300_VAP_TCL_BYPASS;
965
    }
966

967
    /* Point size width and height. */
968
    point_size =
969
        pack_float_16_6x(state->point_size) |
970
971
        (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);

972
    /* Point size clamping. */
973
974
975
976
977
    if (state->point_size_per_vertex) {
        /* Per-vertex point size.
         * Clamp to [0, max FB size] */
        psiz = pipe->screen->get_paramf(pipe->screen,
                                        PIPE_CAP_MAX_POINT_WIDTH);
978
        point_minmax =
979
980
981
982
983
            pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT;
    } else {
        /* We cannot disable the point-size vertex output,
         * so clamp it. */
        psiz = state->point_size;
984
        point_minmax =
985
986
987
            (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
            (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT);
    }
988
989

    /* Line control. */
990
    line_control = pack_float_16_6x(state->line_width) |
991
992
        R300_GA_LINE_CNTL_END_TYPE_COMP;

Marek Olšák's avatar
Marek Olšák committed
993
    /* Enable polygon mode */
994
    polygon_mode = 0;
995
996
    if (state->fill_front != PIPE_POLYGON_MODE_FILL ||
        state->fill_back != PIPE_POLYGON_MODE_FILL) {
997
        polygon_mode = R300_GA_POLY_MODE_DUAL;
Marek Olšák's avatar
Marek Olšák committed
998
999
    }

1000
1001
    /* Front face */
    if (state->front_ccw) 
1002
        cull_mode = R300_FRONT_FACE_CCW;
1003
    else
1004
        cull_mode = R300_FRONT_FACE_CW;
Corbin Simpson's avatar
Corbin Simpson committed
1005

1006
    /* Polygon offset */
1007
    polygon_offset_enable = 0;
1008
    if (util_get_offset(state, state->fill_front)) {
1009
       polygon_offset_enable |= R300_FRONT_ENABLE;
1010
    }
1011
    if (util_get_offset(state, state->fill_back)) {
1012
       polygon_offset_enable |= R300_BACK_ENABLE;
1013
    }
Marek Olšák's avatar
Marek Olšák committed
1014

1015
1016
    rs->polygon_offset_enable = polygon_offset_enable != 0;

1017
    /* Polygon mode */
1018
1019
    if (polygon_mode) {
       polygon_mode |=
1020
          r300_translate_polygon_mode_front(state->fill_front);
1021
       polygon_mode |=
1022
          r300_translate_polygon_mode_back(state->fill_back);
Corbin Simpson's avatar
Corbin Simpson committed
1023
    }
1024
1025

    if (state->cull_face & PIPE_FACE_FRONT) {
1026
        cull_mode |= R300_CULL_FRONT;
Corbin Simpson's avatar
Corbin Simpson committed
1027
    }
1028
    if (state->cull_face & PIPE_FACE_BACK) {
1029
        cull_mode |= R300_CULL_BACK;
Corbin Simpson's avatar
Corbin Simpson committed
1030
1031
    }

1032
    if (state->line_stipple_enable) {
1033
        line_stipple_config =
1034
            R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
1035
            (fui((float)state->line_stipple_factor) &
1036
1037
                R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
        /* XXX this might need to be scaled up */
1038
        line_stipple_value = state->line_stipple_pattern;
Marek Olšák's avatar
Marek Olšák committed
1039
1040
1041
    } else {
        line_stipple_config = 0;
        line_stipple_value = 0;
1042
1043
    }

1044
1045
1046
1047
1048
1049
    if (state->flatshade) {
        rs->color_control = R300_SHADE_MODEL_FLAT;
    } else {
        rs->color_control = R300_SHADE_MODEL_SMOOTH;
    }

1050
    clip_rule = state->scissor ? 0xAAAA : 0xFFFF;
1051

1052
    /* Point sprites */
1053
    stuffing_enable = 0;
1054
    if (state->sprite_coord_enable) {
1055
        stuffing_enable = R300_GB_POINT_STUFF_ENABLE;
Marek Olšák's avatar
Marek Olšák committed
1056
1057
        for (i = 0; i < 8; i++) {
            if (state->sprite_coord_enable & (1 << i))
1058
                stuffing_enable |=
1059
                    R300_GB_TEX_ST << (R300_GB_TEX0_SOURCE_SHIFT + (i*2));
Marek Olšák's avatar
Marek Olšák committed
1060
        }
1061

1062
1063
        point_texcoord_left = 0.0f;
        point_texcoord_right = 1.0f;
1064
1065
1066

        switch (state->sprite_coord_mode) {
            case PIPE_SPRITE_COORD_UPPER_LEFT:
1067
1068
                point_texcoord_top = 0.0f;
                point_texcoord_bottom = 1.0f;
1069
1070
                break;
            case PIPE_SPRITE_COORD_LOWER_LEFT:
1071
1072
                point_texcoord_top = 1.0f;
                point_texcoord_bottom = 0.0f;
1073
1074
1075
1076
                break;
        }
    }

1077
1078
1079
1080
1081
1082
1083
1084
1085
    /* Build the main command buffer. */
    BEGIN_CB(rs->cb_main, 25);
    OUT_CB_REG(R300_VAP_CNTL_STATUS, vap_control_status);
    OUT_CB_REG(R300_GA_POINT_SIZE, point_size);
    OUT_CB_REG_SEQ(R300_GA_POINT_MINMAX, 2);
    OUT_CB(point_minmax);
    OUT_CB(line_control);
    OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE, 2);
    OUT_CB(polygon_offset_enable);
1086
    rs->cull_mode_index = 9;
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    OUT_CB(cull_mode);
    OUT_CB_REG(R300_GA_LINE_STIPPLE_CONFIG, line_stipple_config);
    OUT_CB_REG(R300_GA_LINE_STIPPLE_VALUE, line_stipple_value);
    OUT_CB_REG(R300_GA_POLY_MODE, polygon_mode);
    OUT_CB_REG(R300_SC_CLIP_RULE, clip_rule);
    OUT_CB_REG(R300_GB_ENABLE, stuffing_enable);
    OUT_CB_REG_SEQ(R300_GA_POINT_S0, 4);
    OUT_CB_32F(point_texcoord_left);
    OUT_CB_32F(point_texcoord_bottom);
    OUT_CB_32F(point_texcoord_right);
    OUT_CB_32F(point_texcoord_top);
    END_CB;

    /* Build the two command buffers for polygon offset setup. */
    if (polygon_offset_enable) {
        float scale = state->offset_scale * 12;
        float offset = state->offset_units * 4;

        BEGIN_CB(rs->cb_poly_offset_zb16, 5);
        OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 4);
        OUT_CB_32F(scale);
        OUT_CB_32F(offset);
        OUT_CB_32F(scale);
        OUT_CB_32F(offset);
        END_CB;

        offset = state->offset_units * 2;

        BEGIN_CB(rs->cb_poly_offset_zb24, 5);
        OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 4);
        OUT_CB_32F(scale);
        OUT_CB_32F(offset);
        OUT_CB_32F(scale);
        OUT_CB_32F(offset);
        END_CB;
    }

Corbin Simpson's avatar
Corbin Simpson committed
1124
1125
1126
1127
1128
1129
1130
    return (void*)rs;
}

/* Bind rasterizer state. */
static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
{
    struct r300_context* r300 = r300_context(pipe);
1131
    struct r300_rs_state* rs = (struct r300_rs_state*)state;
1132
    int last_sprite_coord_enable = r300->sprite_coord_enable;
1133
    boolean last_two_sided_color = r300->two_sided_color;
1134

1135
1136
    if (r300->draw && rs) {
        draw_set_rasterizer_state(r300->draw, &rs->rs_draw, state);
1137
    }
Corbin Simpson's avatar
Corbin Simpson committed
1138

1139
    if (rs) {
1140
        r300->polygon_offset_enabled = rs->polygon_offset_enable;