r300_state.c revision 084580fa43320b1a0878b572c6804aa5a40b2f9d
1/*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24#include "draw/draw_context.h"
25
26#include "util/u_math.h"
27#include "util/u_memory.h"
28#include "util/u_pack_color.h"
29
30#include "tgsi/tgsi_parse.h"
31
32#include "pipe/p_config.h"
33
34#include "r300_context.h"
35#include "r300_emit.h"
36#include "r300_reg.h"
37#include "r300_screen.h"
38#include "r300_screen_buffer.h"
39#include "r300_state.h"
40#include "r300_state_inlines.h"
41#include "r300_fs.h"
42#include "r300_texture.h"
43#include "r300_vs.h"
44#include "r300_winsys.h"
45
46/* r300_state: Functions used to intialize state context by translating
47 * Gallium state objects into semi-native r300 state objects. */
48
49#define UPDATE_STATE(cso, atom) \
50    if (cso != atom.state) { \
51        atom.state = cso;    \
52        atom.dirty = TRUE;   \
53    }
54
55static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
56                                            unsigned dstRGB, unsigned dstA)
57{
58    /* If the blend equation is ADD or REVERSE_SUBTRACT,
59     * SRC_ALPHA == 0, and the following state is set, the colorbuffer
60     * will not be changed.
61     * Notice that the dst factors are the src factors inverted. */
62    return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
63            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
64            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
65           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
66            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
67            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
68            srcA == PIPE_BLENDFACTOR_ZERO) &&
69           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
70            dstRGB == PIPE_BLENDFACTOR_ONE) &&
71           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
72            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
73            dstA == PIPE_BLENDFACTOR_ONE);
74}
75
76static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
77                                            unsigned dstRGB, unsigned dstA)
78{
79    /* If the blend equation is ADD or REVERSE_SUBTRACT,
80     * SRC_ALPHA == 1, and the following state is set, the colorbuffer
81     * will not be changed.
82     * Notice that the dst factors are the src factors inverted. */
83    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
84            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
85           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
86            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
87            srcA == PIPE_BLENDFACTOR_ZERO) &&
88           (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
89            dstRGB == PIPE_BLENDFACTOR_ONE) &&
90           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
91            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
92            dstA == PIPE_BLENDFACTOR_ONE);
93}
94
95static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
96                                            unsigned dstRGB, unsigned dstA)
97{
98    /* If the blend equation is ADD or REVERSE_SUBTRACT,
99     * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
100     * will not be changed.
101     * Notice that the dst factors are the src factors inverted. */
102    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
103            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
104           (srcA == PIPE_BLENDFACTOR_ZERO) &&
105           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
106            dstRGB == PIPE_BLENDFACTOR_ONE) &&
107           (dstA == PIPE_BLENDFACTOR_ONE);
108}
109
110static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
111                                            unsigned dstRGB, unsigned dstA)
112{
113    /* If the blend equation is ADD or REVERSE_SUBTRACT,
114     * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
115     * will not be changed.
116     * Notice that the dst factors are the src factors inverted. */
117    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
118            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
119           (srcA == PIPE_BLENDFACTOR_ZERO) &&
120           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
121            dstRGB == PIPE_BLENDFACTOR_ONE) &&
122           (dstA == PIPE_BLENDFACTOR_ONE);
123}
124
125static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
126                                                  unsigned dstRGB, unsigned dstA)
127{
128    /* If the blend equation is ADD or REVERSE_SUBTRACT,
129     * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
130     * the colorbuffer will not be changed.
131     * Notice that the dst factors are the src factors inverted. */
132    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
133            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
134            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
135            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
136           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
137            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
138            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
139            srcA == PIPE_BLENDFACTOR_ZERO) &&
140           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
141            dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
142            dstRGB == PIPE_BLENDFACTOR_ONE) &&
143           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
144            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
145            dstA == PIPE_BLENDFACTOR_ONE);
146}
147
148static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
149                                                  unsigned dstRGB, unsigned dstA)
150{
151    /* If the blend equation is ADD or REVERSE_SUBTRACT,
152     * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
153     * the colorbuffer will not be changed.
154     * Notice that the dst factors are the src factors inverted. */
155    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
156            srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
157            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
158           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
159            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
160            srcA == PIPE_BLENDFACTOR_ZERO) &&
161           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
162            dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
163            dstRGB == PIPE_BLENDFACTOR_ONE) &&
164           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
165            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
166            dstA == PIPE_BLENDFACTOR_ONE);
167}
168
169static unsigned bgra_cmask(unsigned mask)
170{
171    /* Gallium uses RGBA color ordering while R300 expects BGRA. */
172
173    return ((mask & PIPE_MASK_R) << 2) |
174           ((mask & PIPE_MASK_B) >> 2) |
175           (mask & (PIPE_MASK_G | PIPE_MASK_A));
176}
177
178/* Create a new blend state based on the CSO blend state.
179 *
180 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
181static void* r300_create_blend_state(struct pipe_context* pipe,
182                                     const struct pipe_blend_state* state)
183{
184    struct r300_screen* r300screen = r300_screen(pipe->screen);
185    struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
186
187    if (state->rt[0].blend_enable)
188    {
189        unsigned eqRGB = state->rt[0].rgb_func;
190        unsigned srcRGB = state->rt[0].rgb_src_factor;
191        unsigned dstRGB = state->rt[0].rgb_dst_factor;
192
193        unsigned eqA = state->rt[0].alpha_func;
194        unsigned srcA = state->rt[0].alpha_src_factor;
195        unsigned dstA = state->rt[0].alpha_dst_factor;
196
197        /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
198         * this is just the crappy D3D naming */
199        blend->blend_control = R300_ALPHA_BLEND_ENABLE |
200            r300_translate_blend_function(eqRGB) |
201            ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
202            ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
203
204        /* Optimization: some operations do not require the destination color.
205         *
206         * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
207         * otherwise blending gives incorrect results. It seems to be
208         * a hardware bug. */
209        if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
210            eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
211            dstRGB != PIPE_BLENDFACTOR_ZERO ||
212            dstA != PIPE_BLENDFACTOR_ZERO ||
213            srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
214            srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
215            srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
216            srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
217            srcA == PIPE_BLENDFACTOR_DST_COLOR ||
218            srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
219            srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
220            srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
221            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) {
222            /* Enable reading from the colorbuffer. */
223            blend->blend_control |= R300_READ_ENABLE;
224
225            if (r300screen->caps.is_r500) {
226                /* Optimization: Depending on incoming pixels, we can
227                 * conditionally disable the reading in hardware... */
228                if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
229                    eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
230                    /* Disable reading if SRC_ALPHA == 0. */
231                    if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
232                         dstRGB == PIPE_BLENDFACTOR_ZERO) &&
233                        (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
234                         dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
235                         dstA == PIPE_BLENDFACTOR_ZERO)) {
236                         blend->blend_control |= R500_SRC_ALPHA_0_NO_READ;
237                    }
238
239                    /* Disable reading if SRC_ALPHA == 1. */
240                    if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
241                         dstRGB == PIPE_BLENDFACTOR_ZERO) &&
242                        (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
243                         dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
244                         dstA == PIPE_BLENDFACTOR_ZERO)) {
245                         blend->blend_control |= R500_SRC_ALPHA_1_NO_READ;
246                    }
247                }
248            }
249        }
250
251        /* Optimization: discard pixels which don't change the colorbuffer.
252         *
253         * The code below is non-trivial and some math is involved.
254         *
255         * Discarding pixels must be disabled when FP16 AA is enabled.
256         * This is a hardware bug. Also, this implementation wouldn't work
257         * with FP blending enabled and equation clamping disabled.
258         *
259         * Equations other than ADD are rarely used and therefore won't be
260         * optimized. */
261        if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
262            (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
263            /* ADD: X+Y
264             * REVERSE_SUBTRACT: Y-X
265             *
266             * The idea is:
267             * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
268             * then CB will not be changed.
269             *
270             * Given the srcFactor and dstFactor variables, we can derive
271             * what src and dst should be equal to and discard appropriate
272             * pixels.
273             */
274            if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
275                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
276            } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
277                                                    dstRGB, dstA)) {
278                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
279            } else if (blend_discard_if_src_color_0(srcRGB, srcA,
280                                                    dstRGB, dstA)) {
281                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
282            } else if (blend_discard_if_src_color_1(srcRGB, srcA,
283                                                    dstRGB, dstA)) {
284                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
285            } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
286                                                          dstRGB, dstA)) {
287                blend->blend_control |=
288                    R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
289            } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
290                                                          dstRGB, dstA)) {
291                blend->blend_control |=
292                    R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
293            }
294        }
295
296        /* separate alpha */
297        if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
298            blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
299            blend->alpha_blend_control =
300                r300_translate_blend_function(eqA) |
301                (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
302                (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
303        }
304    }
305
306    /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
307    if (state->logicop_enable) {
308        blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
309                (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
310    }
311
312    /* Color channel masks for all MRTs. */
313    blend->color_channel_mask = bgra_cmask(state->rt[0].colormask);
314    if (r300screen->caps.is_r500 && state->independent_blend_enable) {
315        if (state->rt[1].blend_enable) {
316            blend->color_channel_mask |= bgra_cmask(state->rt[1].colormask) << 4;
317        }
318        if (state->rt[2].blend_enable) {
319            blend->color_channel_mask |= bgra_cmask(state->rt[2].colormask) << 8;
320        }
321        if (state->rt[3].blend_enable) {
322            blend->color_channel_mask |= bgra_cmask(state->rt[3].colormask) << 12;
323        }
324    }
325
326    /* Neither fglrx nor classic r300 ever set this, regardless of dithering
327     * state. Since it's an optional implementation detail, we can leave it
328     * out and never dither.
329     *
330     * This could be revisited if we ever get quality or conformance hints.
331     *
332    if (state->dither) {
333        blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
334                        R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
335    }
336    */
337
338    return (void*)blend;
339}
340
341/* Bind blend state. */
342static void r300_bind_blend_state(struct pipe_context* pipe,
343                                  void* state)
344{
345    struct r300_context* r300 = r300_context(pipe);
346
347    UPDATE_STATE(state, r300->blend_state);
348}
349
350/* Free blend state. */
351static void r300_delete_blend_state(struct pipe_context* pipe,
352                                    void* state)
353{
354    FREE(state);
355}
356
357/* Convert float to 10bit integer */
358static unsigned float_to_fixed10(float f)
359{
360    return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
361}
362
363/* Set blend color.
364 * Setup both R300 and R500 registers, figure out later which one to write. */
365static void r300_set_blend_color(struct pipe_context* pipe,
366                                 const struct pipe_blend_color* color)
367{
368    struct r300_context* r300 = r300_context(pipe);
369    struct r300_blend_color_state* state =
370        (struct r300_blend_color_state*)r300->blend_color_state.state;
371    union util_color uc;
372
373    util_pack_color(color->color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
374    state->blend_color = uc.ui;
375
376    /* XXX if FP16 blending is enabled, we should use the FP16 format */
377    state->blend_color_red_alpha =
378        float_to_fixed10(color->color[0]) |
379        (float_to_fixed10(color->color[3]) << 16);
380    state->blend_color_green_blue =
381        float_to_fixed10(color->color[2]) |
382        (float_to_fixed10(color->color[1]) << 16);
383
384    r300->blend_color_state.size = r300->screen->caps.is_r500 ? 3 : 2;
385    r300->blend_color_state.dirty = TRUE;
386}
387
388static void r300_set_clip_state(struct pipe_context* pipe,
389                                const struct pipe_clip_state* state)
390{
391    struct r300_context* r300 = r300_context(pipe);
392
393    r300->clip = *state;
394
395    if (r300->screen->caps.has_tcl) {
396        memcpy(r300->clip_state.state, state, sizeof(struct pipe_clip_state));
397        r300->clip_state.size = 29;
398    } else {
399        draw_flush(r300->draw);
400        draw_set_clip_state(r300->draw, state);
401        r300->clip_state.size = 2;
402    }
403
404    r300->clip_state.dirty = TRUE;
405}
406
407/* Create a new depth, stencil, and alpha state based on the CSO dsa state.
408 *
409 * This contains the depth buffer, stencil buffer, alpha test, and such.
410 * On the Radeon, depth and stencil buffer setup are intertwined, which is
411 * the reason for some of the strange-looking assignments across registers. */
412static void*
413        r300_create_dsa_state(struct pipe_context* pipe,
414                              const struct pipe_depth_stencil_alpha_state* state)
415{
416    struct r300_capabilities *caps = &r300_screen(pipe->screen)->caps;
417    struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
418
419    /* Depth test setup. */
420    if (state->depth.enabled) {
421        dsa->z_buffer_control |= R300_Z_ENABLE;
422
423        if (state->depth.writemask) {
424            dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
425        }
426
427        dsa->z_stencil_control |=
428            (r300_translate_depth_stencil_function(state->depth.func) <<
429                R300_Z_FUNC_SHIFT);
430    }
431
432    /* Stencil buffer setup. */
433    if (state->stencil[0].enabled) {
434        dsa->z_buffer_control |= R300_STENCIL_ENABLE;
435        dsa->z_stencil_control |=
436            (r300_translate_depth_stencil_function(state->stencil[0].func) <<
437                R300_S_FRONT_FUNC_SHIFT) |
438            (r300_translate_stencil_op(state->stencil[0].fail_op) <<
439                R300_S_FRONT_SFAIL_OP_SHIFT) |
440            (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
441                R300_S_FRONT_ZPASS_OP_SHIFT) |
442            (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
443                R300_S_FRONT_ZFAIL_OP_SHIFT);
444
445        dsa->stencil_ref_mask =
446                (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
447                (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
448
449        if (state->stencil[1].enabled) {
450            dsa->two_sided = TRUE;
451
452            dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
453            dsa->z_stencil_control |=
454            (r300_translate_depth_stencil_function(state->stencil[1].func) <<
455                R300_S_BACK_FUNC_SHIFT) |
456            (r300_translate_stencil_op(state->stencil[1].fail_op) <<
457                R300_S_BACK_SFAIL_OP_SHIFT) |
458            (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
459                R300_S_BACK_ZPASS_OP_SHIFT) |
460            (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
461                R300_S_BACK_ZFAIL_OP_SHIFT);
462
463            dsa->stencil_ref_bf =
464                (state->stencil[1].valuemask << R300_STENCILMASK_SHIFT) |
465                (state->stencil[1].writemask << R300_STENCILWRITEMASK_SHIFT);
466
467            if (caps->is_r500) {
468                dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
469            } else {
470                dsa->stencil_ref_bf_fallback =
471                  (state->stencil[0].valuemask != state->stencil[1].valuemask ||
472                   state->stencil[0].writemask != state->stencil[1].writemask);
473            }
474        }
475    }
476
477    /* Alpha test setup. */
478    if (state->alpha.enabled) {
479        dsa->alpha_function =
480            r300_translate_alpha_function(state->alpha.func) |
481            R300_FG_ALPHA_FUNC_ENABLE;
482
483        /* We could use 10bit alpha ref but who needs that? */
484        dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
485
486        if (caps->is_r500)
487            dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
488    }
489
490    return (void*)dsa;
491}
492
493static void r300_update_stencil_ref_fallback_status(struct r300_context *r300)
494{
495    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
496
497    if (r300->screen->caps.is_r500) {
498        return;
499    }
500
501    r300->stencil_ref_bf_fallback =
502        dsa->stencil_ref_bf_fallback ||
503        (dsa->two_sided &&
504         r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);
505}
506
507/* Bind DSA state. */
508static void r300_bind_dsa_state(struct pipe_context* pipe,
509                                void* state)
510{
511    struct r300_context* r300 = r300_context(pipe);
512
513    if (!state) {
514        return;
515    }
516
517    UPDATE_STATE(state, r300->dsa_state);
518
519    r300_update_stencil_ref_fallback_status(r300);
520}
521
522/* Free DSA state. */
523static void r300_delete_dsa_state(struct pipe_context* pipe,
524                                  void* state)
525{
526    FREE(state);
527}
528
529static void r300_set_stencil_ref(struct pipe_context* pipe,
530                                 const struct pipe_stencil_ref* sr)
531{
532    struct r300_context* r300 = r300_context(pipe);
533
534    r300->stencil_ref = *sr;
535    r300->dsa_state.dirty = TRUE;
536
537    r300_update_stencil_ref_fallback_status(r300);
538}
539
540/* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
541static void r300_fb_set_tiling_flags(struct r300_context *r300,
542                               const struct pipe_framebuffer_state *old_state,
543                               const struct pipe_framebuffer_state *new_state)
544{
545    struct r300_texture *tex;
546    unsigned i, level;
547
548    /* Set tiling flags for new surfaces. */
549    for (i = 0; i < new_state->nr_cbufs; i++) {
550        tex = r300_texture(new_state->cbufs[i]->texture);
551        level = new_state->cbufs[i]->level;
552
553        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
554                                        tex->pitch[0],
555                                        tex->microtile,
556                                        tex->mip_macrotile[level]);
557    }
558    if (new_state->zsbuf) {
559        tex = r300_texture(new_state->zsbuf->texture);
560        level = new_state->zsbuf->level;
561
562        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
563                                        tex->pitch[0],
564                                        tex->microtile,
565                                        tex->mip_macrotile[level]);
566    }
567}
568
569static void
570    r300_set_framebuffer_state(struct pipe_context* pipe,
571                               const struct pipe_framebuffer_state* state)
572{
573    struct r300_context* r300 = r300_context(pipe);
574    struct pipe_framebuffer_state *old_state = r300->fb_state.state;
575    unsigned max_width, max_height;
576    uint32_t zbuffer_bpp = 0;
577
578    if (state->nr_cbufs > 4) {
579        fprintf(stderr, "r300: Implementation error: Too many MRTs in %s, "
580            "refusing to bind framebuffer state!\n", __FUNCTION__);
581        return;
582    }
583
584    if (r300->screen->caps.is_r500) {
585        max_width = max_height = 4096;
586    } else if (r300->screen->caps.is_r400) {
587        max_width = max_height = 4021;
588    } else {
589        max_width = max_height = 2560;
590    }
591
592    if (state->width > max_width || state->height > max_height) {
593        fprintf(stderr, "r300: Implementation error: Render targets are too "
594        "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__);
595        return;
596    }
597
598    if (r300->draw) {
599        draw_flush(r300->draw);
600    }
601
602    r300->fb_state.dirty = TRUE;
603
604    /* If nr_cbufs is changed from zero to non-zero or vice versa... */
605    if (!!old_state->nr_cbufs != !!state->nr_cbufs) {
606        r300->blend_state.dirty = TRUE;
607    }
608    /* If zsbuf is set from NULL to non-NULL or vice versa.. */
609    if (!!old_state->zsbuf != !!state->zsbuf) {
610        r300->dsa_state.dirty = TRUE;
611    }
612
613    /* The tiling flags are dependent on the surface miplevel, unfortunately. */
614    r300_fb_set_tiling_flags(r300, r300->fb_state.state, state);
615
616    memcpy(r300->fb_state.state, state, sizeof(struct pipe_framebuffer_state));
617
618    r300->fb_state.size = (10 * state->nr_cbufs) + (2 * (4 - state->nr_cbufs)) +
619                          (state->zsbuf ? 10 : 0) + 9;
620
621    /* Polygon offset depends on the zbuffer bit depth. */
622    if (state->zsbuf && r300->polygon_offset_enabled) {
623        switch (util_format_get_blocksize(state->zsbuf->texture->format)) {
624            case 2:
625                zbuffer_bpp = 16;
626                break;
627            case 4:
628                zbuffer_bpp = 24;
629                break;
630        }
631
632        if (r300->zbuffer_bpp != zbuffer_bpp) {
633            r300->zbuffer_bpp = zbuffer_bpp;
634            r300->rs_state.dirty = TRUE;
635        }
636    }
637}
638
639/* Create fragment shader state. */
640static void* r300_create_fs_state(struct pipe_context* pipe,
641                                  const struct pipe_shader_state* shader)
642{
643    struct r300_fragment_shader* fs = NULL;
644
645    fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
646
647    /* Copy state directly into shader. */
648    fs->state = *shader;
649    fs->state.tokens = tgsi_dup_tokens(shader->tokens);
650
651    return (void*)fs;
652}
653
654void r300_mark_fs_code_dirty(struct r300_context *r300)
655{
656    struct r300_fragment_shader* fs = r300_fs(r300);
657
658    r300->fs.dirty = TRUE;
659    r300->fs_rc_constant_state.dirty = TRUE;
660    r300->fs_constants.dirty = TRUE;
661
662    if (r300->screen->caps.is_r500) {
663        r300->fs.size = r500_get_fs_atom_size(r300);
664        r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 7;
665        r300->fs_constants.size = fs->shader->externals_count * 4 + 3;
666    } else {
667        r300->fs.size = r300_get_fs_atom_size(r300);
668        r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 5;
669        r300->fs_constants.size = fs->shader->externals_count * 4 + 1;
670    }
671}
672
673/* Bind fragment shader state. */
674static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
675{
676    struct r300_context* r300 = r300_context(pipe);
677    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
678
679    if (fs == NULL) {
680        r300->fs.state = NULL;
681        return;
682    }
683
684    r300->fs.state = fs;
685    r300_pick_fragment_shader(r300);
686    r300_mark_fs_code_dirty(r300);
687
688    r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
689}
690
691/* Delete fragment shader state. */
692static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
693{
694    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
695    struct r300_fragment_shader_code *tmp, *ptr = fs->first;
696
697    while (ptr) {
698        tmp = ptr;
699        ptr = ptr->next;
700        rc_constants_destroy(&tmp->code.constants);
701        FREE(tmp);
702    }
703    FREE((void*)fs->state.tokens);
704    FREE(shader);
705}
706
707static void r300_set_polygon_stipple(struct pipe_context* pipe,
708                                     const struct pipe_poly_stipple* state)
709{
710    /* XXX no idea how to set this up, but not terribly important */
711}
712
713/* Create a new rasterizer state based on the CSO rasterizer state.
714 *
715 * This is a very large chunk of state, and covers most of the graphics
716 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
717 *
718 * In a not entirely unironic sidenote, this state has nearly nothing to do
719 * with the actual block on the Radeon called the rasterizer (RS). */
720static void* r300_create_rs_state(struct pipe_context* pipe,
721                                  const struct pipe_rasterizer_state* state)
722{
723    struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
724    int i;
725    float psiz;
726
727    /* Copy rasterizer state for Draw. */
728    rs->rs = *state;
729
730#ifdef PIPE_ARCH_LITTLE_ENDIAN
731    rs->vap_control_status = R300_VC_NO_SWAP;
732#else
733    rs->vap_control_status = R300_VC_32BIT_SWAP;
734#endif
735
736    /* If no TCL engine is present, turn off the HW TCL. */
737    if (!r300_screen(pipe->screen)->caps.has_tcl) {
738        rs->vap_control_status |= R300_VAP_TCL_BYPASS;
739    }
740
741    /* Point size width and height. */
742    rs->point_size =
743        pack_float_16_6x(state->point_size) |
744        (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
745
746    /* Point size clamping. */
747    if (state->point_size_per_vertex) {
748        /* Per-vertex point size.
749         * Clamp to [0, max FB size] */
750        psiz = pipe->screen->get_paramf(pipe->screen,
751                                        PIPE_CAP_MAX_POINT_WIDTH);
752        rs->point_minmax =
753            pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT;
754    } else {
755        /* We cannot disable the point-size vertex output,
756         * so clamp it. */
757        psiz = state->point_size;
758        rs->point_minmax =
759            (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
760            (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT);
761    }
762
763    /* Line control. */
764    rs->line_control = pack_float_16_6x(state->line_width) |
765        R300_GA_LINE_CNTL_END_TYPE_COMP;
766
767    /* Enable polygon mode */
768    if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
769        state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
770        rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
771    }
772
773    /* Radeons don't think in "CW/CCW", they think in "front/back". */
774    if (state->front_winding == PIPE_WINDING_CW) {
775        rs->cull_mode = R300_FRONT_FACE_CW;
776
777        /* Polygon offset */
778        if (state->offset_cw) {
779            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
780        }
781        if (state->offset_ccw) {
782            rs->polygon_offset_enable |= R300_BACK_ENABLE;
783        }
784
785        /* Polygon mode */
786        if (rs->polygon_mode) {
787            rs->polygon_mode |=
788                r300_translate_polygon_mode_front(state->fill_cw);
789            rs->polygon_mode |=
790                r300_translate_polygon_mode_back(state->fill_ccw);
791        }
792    } else {
793        rs->cull_mode = R300_FRONT_FACE_CCW;
794
795        /* Polygon offset */
796        if (state->offset_ccw) {
797            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
798        }
799        if (state->offset_cw) {
800            rs->polygon_offset_enable |= R300_BACK_ENABLE;
801        }
802
803        /* Polygon mode */
804        if (rs->polygon_mode) {
805            rs->polygon_mode |=
806                r300_translate_polygon_mode_front(state->fill_ccw);
807            rs->polygon_mode |=
808                r300_translate_polygon_mode_back(state->fill_cw);
809        }
810    }
811    if (state->front_winding & state->cull_mode) {
812        rs->cull_mode |= R300_CULL_FRONT;
813    }
814    if (~(state->front_winding) & state->cull_mode) {
815        rs->cull_mode |= R300_CULL_BACK;
816    }
817
818    if (rs->polygon_offset_enable) {
819        rs->depth_offset = state->offset_units;
820        rs->depth_scale = state->offset_scale;
821    }
822
823    if (state->line_stipple_enable) {
824        rs->line_stipple_config =
825            R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
826            (fui((float)state->line_stipple_factor) &
827                R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
828        /* XXX this might need to be scaled up */
829        rs->line_stipple_value = state->line_stipple_pattern;
830    }
831
832    if (state->flatshade) {
833        rs->color_control = R300_SHADE_MODEL_FLAT;
834    } else {
835        rs->color_control = R300_SHADE_MODEL_SMOOTH;
836    }
837
838    rs->clip_rule = state->scissor ? 0xAAAA : 0xFFFF;
839
840    /* Point sprites */
841    if (state->sprite_coord_enable) {
842        rs->stuffing_enable = R300_GB_POINT_STUFF_ENABLE;
843	for (i = 0; i < 8; i++) {
844	    if (state->sprite_coord_enable & (1 << i))
845		rs->stuffing_enable |=
846		    R300_GB_TEX_STR << (R300_GB_TEX0_SOURCE_SHIFT + (i*2));
847	}
848
849        rs->point_texcoord_left = 0.0f;
850        rs->point_texcoord_right = 1.0f;
851
852        switch (state->sprite_coord_mode) {
853            case PIPE_SPRITE_COORD_UPPER_LEFT:
854                rs->point_texcoord_top = 0.0f;
855                rs->point_texcoord_bottom = 1.0f;
856                break;
857            case PIPE_SPRITE_COORD_LOWER_LEFT:
858                rs->point_texcoord_top = 1.0f;
859                rs->point_texcoord_bottom = 0.0f;
860                break;
861        }
862    }
863
864    return (void*)rs;
865}
866
867/* Bind rasterizer state. */
868static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
869{
870    struct r300_context* r300 = r300_context(pipe);
871    struct r300_rs_state* rs = (struct r300_rs_state*)state;
872    int last_sprite_coord_enable = r300->sprite_coord_enable;
873
874    if (r300->draw) {
875        draw_flush(r300->draw);
876        draw_set_rasterizer_state(r300->draw, &rs->rs, state);
877    }
878
879    if (rs) {
880        r300->polygon_offset_enabled = rs->rs.offset_cw || rs->rs.offset_ccw;
881        r300->sprite_coord_enable = rs->rs.sprite_coord_enable;
882    } else {
883        r300->polygon_offset_enabled = FALSE;
884        r300->sprite_coord_enable = 0;
885    }
886
887    UPDATE_STATE(state, r300->rs_state);
888    r300->rs_state.size = 27 + (r300->polygon_offset_enabled ? 5 : 0);
889
890    if (last_sprite_coord_enable != r300->sprite_coord_enable) {
891        r300->rs_block_state.dirty = TRUE;
892    }
893}
894
895/* Free rasterizer state. */
896static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
897{
898    FREE(state);
899}
900
901static void*
902        r300_create_sampler_state(struct pipe_context* pipe,
903                                  const struct pipe_sampler_state* state)
904{
905    struct r300_context* r300 = r300_context(pipe);
906    struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
907    boolean is_r500 = r300->screen->caps.is_r500;
908    int lod_bias;
909    union util_color uc;
910
911    sampler->state = *state;
912
913    sampler->filter0 |=
914        (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
915        (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
916        (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
917
918    sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
919                                                   state->mag_img_filter,
920                                                   state->min_mip_filter,
921                                                   state->max_anisotropy > 0);
922
923    sampler->filter0 |= r300_anisotropy(state->max_anisotropy);
924
925    /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
926    /* We must pass these to the merge function to clamp them properly. */
927    sampler->min_lod = MAX2((unsigned)state->min_lod, 0);
928    sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0);
929
930    lod_bias = CLAMP((int)(state->lod_bias * 32 + 1), -(1 << 9), (1 << 9) - 1);
931
932    sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
933
934    /* This is very high quality anisotropic filtering for R5xx.
935     * It's good for benchmarking the performance of texturing but
936     * in practice we don't want to slow down the driver because it's
937     * a pretty good performance killer. Feel free to play with it. */
938    if (DBG_ON(r300, DBG_ANISOHQ) && is_r500) {
939        sampler->filter1 |= r500_anisotropy(state->max_anisotropy);
940    }
941
942    util_pack_color(state->border_color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
943    sampler->border_color = uc.ui;
944
945    /* R500-specific fixups and optimizations */
946    if (r300->screen->caps.is_r500) {
947        sampler->filter1 |= R500_BORDER_FIX;
948    }
949
950    return (void*)sampler;
951}
952
953static void r300_bind_sampler_states(struct pipe_context* pipe,
954                                     unsigned count,
955                                     void** states)
956{
957    struct r300_context* r300 = r300_context(pipe);
958    struct r300_textures_state* state =
959        (struct r300_textures_state*)r300->textures_state.state;
960    unsigned tex_units = r300->screen->caps.num_tex_units;
961
962    if (count > tex_units) {
963        return;
964    }
965
966    memcpy(state->sampler_states, states, sizeof(void*) * count);
967    state->sampler_state_count = count;
968
969    r300->textures_state.dirty = TRUE;
970}
971
972static void r300_lacks_vertex_textures(struct pipe_context* pipe,
973                                       unsigned count,
974                                       void** states)
975{
976}
977
978static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
979{
980    FREE(state);
981}
982
983static void r300_set_fragment_sampler_views(struct pipe_context* pipe,
984                                            unsigned count,
985                                            struct pipe_sampler_view** views)
986{
987    struct r300_context* r300 = r300_context(pipe);
988    struct r300_textures_state* state =
989        (struct r300_textures_state*)r300->textures_state.state;
990    struct r300_texture *texture;
991    unsigned i;
992    unsigned tex_units = r300->screen->caps.num_tex_units;
993    boolean dirty_tex = FALSE;
994
995    if (count > tex_units) {
996        return;
997    }
998
999    for (i = 0; i < count; i++) {
1000        if (&state->sampler_views[i]->base != views[i]) {
1001            pipe_sampler_view_reference(
1002                    (struct pipe_sampler_view**)&state->sampler_views[i],
1003                    views[i]);
1004
1005            if (!views[i]) {
1006                continue;
1007            }
1008
1009            /* A new sampler view (= texture)... */
1010            dirty_tex = TRUE;
1011
1012            /* Set the texrect factor in the fragment shader.
1013             * Needed for RECT and NPOT fallback. */
1014            texture = r300_texture(views[i]->texture);
1015            if (texture->uses_pitch) {
1016                r300->fs_rc_constant_state.dirty = TRUE;
1017            }
1018        }
1019    }
1020
1021    for (i = count; i < tex_units; i++) {
1022        if (state->sampler_views[i]) {
1023            pipe_sampler_view_reference(
1024                    (struct pipe_sampler_view**)&state->sampler_views[i],
1025                    NULL);
1026        }
1027    }
1028
1029    state->sampler_view_count = count;
1030
1031    r300->textures_state.dirty = TRUE;
1032
1033    if (dirty_tex) {
1034        r300->texture_cache_inval.dirty = TRUE;
1035    }
1036}
1037
1038static struct pipe_sampler_view *
1039r300_create_sampler_view(struct pipe_context *pipe,
1040                         struct pipe_resource *texture,
1041                         const struct pipe_sampler_view *templ)
1042{
1043    struct r300_sampler_view *view = CALLOC_STRUCT(r300_sampler_view);
1044    struct r300_texture *tex = r300_texture(texture);
1045
1046    if (view) {
1047        view->base = *templ;
1048        view->base.reference.count = 1;
1049        view->base.context = pipe;
1050        view->base.texture = NULL;
1051        pipe_resource_reference(&view->base.texture, texture);
1052
1053        view->swizzle[0] = templ->swizzle_r;
1054        view->swizzle[1] = templ->swizzle_g;
1055        view->swizzle[2] = templ->swizzle_b;
1056        view->swizzle[3] = templ->swizzle_a;
1057
1058        view->format = tex->tx_format;
1059        view->format.format1 |= r300_translate_texformat(templ->format,
1060                                                         view->swizzle);
1061        if (r300_screen(pipe->screen)->caps.is_r500) {
1062            view->format.format2 |= r500_tx_format_msb_bit(templ->format);
1063        }
1064    }
1065
1066    return (struct pipe_sampler_view*)view;
1067}
1068
1069static void
1070r300_sampler_view_destroy(struct pipe_context *pipe,
1071                          struct pipe_sampler_view *view)
1072{
1073   pipe_resource_reference(&view->texture, NULL);
1074   FREE(view);
1075}
1076
1077static void r300_set_scissor_state(struct pipe_context* pipe,
1078                                   const struct pipe_scissor_state* state)
1079{
1080    struct r300_context* r300 = r300_context(pipe);
1081
1082    memcpy(r300->scissor_state.state, state,
1083        sizeof(struct pipe_scissor_state));
1084
1085    r300->scissor_state.dirty = TRUE;
1086}
1087
1088static void r300_set_viewport_state(struct pipe_context* pipe,
1089                                    const struct pipe_viewport_state* state)
1090{
1091    struct r300_context* r300 = r300_context(pipe);
1092    struct r300_viewport_state* viewport =
1093        (struct r300_viewport_state*)r300->viewport_state.state;
1094
1095    r300->viewport = *state;
1096
1097    /* Do the transform in HW. */
1098    viewport->vte_control = R300_VTX_W0_FMT;
1099
1100    if (state->scale[0] != 1.0f) {
1101        viewport->xscale = state->scale[0];
1102        viewport->vte_control |= R300_VPORT_X_SCALE_ENA;
1103    }
1104    if (state->scale[1] != 1.0f) {
1105        viewport->yscale = state->scale[1];
1106        viewport->vte_control |= R300_VPORT_Y_SCALE_ENA;
1107    }
1108    if (state->scale[2] != 1.0f) {
1109        viewport->zscale = state->scale[2];
1110        viewport->vte_control |= R300_VPORT_Z_SCALE_ENA;
1111    }
1112    if (state->translate[0] != 0.0f) {
1113        viewport->xoffset = state->translate[0];
1114        viewport->vte_control |= R300_VPORT_X_OFFSET_ENA;
1115    }
1116    if (state->translate[1] != 0.0f) {
1117        viewport->yoffset = state->translate[1];
1118        viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA;
1119    }
1120    if (state->translate[2] != 0.0f) {
1121        viewport->zoffset = state->translate[2];
1122        viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA;
1123    }
1124
1125    r300->viewport_state.dirty = TRUE;
1126    if (r300->fs.state && r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED) {
1127        r300->fs_rc_constant_state.dirty = TRUE;
1128    }
1129}
1130
1131static void r300_set_vertex_buffers(struct pipe_context* pipe,
1132                                    unsigned count,
1133                                    const struct pipe_vertex_buffer* buffers)
1134{
1135    struct r300_context* r300 = r300_context(pipe);
1136    struct pipe_vertex_buffer *vbo;
1137    unsigned i, max_index = (1 << 24) - 1;
1138    boolean any_user_buffer = FALSE;
1139
1140    if (count == r300->vertex_buffer_count &&
1141        memcmp(r300->vertex_buffer, buffers,
1142            sizeof(struct pipe_vertex_buffer) * count) == 0) {
1143        return;
1144    }
1145
1146    /* Check if the stride is aligned to the size of DWORD. */
1147    for (i = 0; i < count; i++) {
1148        if (buffers[i].buffer) {
1149            if (buffers[i].stride % 4 != 0) {
1150                // XXX Shouldn't we align the buffer?
1151                fprintf(stderr, "r300: set_vertex_buffers: "
1152                        "Unaligned buffer stride %i isn't supported.\n",
1153                        buffers[i].stride);
1154                abort();
1155            }
1156        }
1157    }
1158
1159    for (i = 0; i < count; i++) {
1160        /* Why, yes, I AM casting away constness. How did you know? */
1161        vbo = (struct pipe_vertex_buffer*)&buffers[i];
1162
1163        /* Reference our buffer. */
1164        pipe_resource_reference(&r300->vertex_buffer[i].buffer, vbo->buffer);
1165
1166        /* Skip NULL buffers */
1167        if (!buffers[i].buffer) {
1168            continue;
1169        }
1170
1171        if (r300_buffer_is_user_buffer(vbo->buffer)) {
1172            any_user_buffer = TRUE;
1173        }
1174
1175        if (vbo->max_index == ~0) {
1176	    /* if no VBO stride then only one vertex value so max index is 1 */
1177	    /* should think about converting to VS constants like svga does */
1178	    if (!vbo->stride)
1179		vbo->max_index = 1;
1180 	    else
1181            	vbo->max_index =
1182               		 (vbo->buffer->width0 - vbo->buffer_offset) / vbo->stride;
1183        }
1184
1185        max_index = MIN2(vbo->max_index, max_index);
1186    }
1187
1188    for (; i < r300->vertex_buffer_count; i++) {
1189        /* Dereference any old buffers. */
1190        pipe_resource_reference(&r300->vertex_buffer[i].buffer, NULL);
1191    }
1192
1193    memcpy(r300->vertex_buffer, buffers,
1194        sizeof(struct pipe_vertex_buffer) * count);
1195
1196    r300->vertex_buffer_count = count;
1197    r300->vertex_buffer_max_index = max_index;
1198    r300->any_user_vbs = any_user_buffer;
1199
1200    if (r300->draw) {
1201        draw_flush(r300->draw);
1202        draw_set_vertex_buffers(r300->draw, count, buffers);
1203    }
1204}
1205
1206/* Update the PSC tables. */
1207static void r300_vertex_psc(struct r300_vertex_element_state *velems)
1208{
1209    struct r300_vertex_stream_state *vstream = &velems->vertex_stream;
1210    uint16_t type, swizzle;
1211    enum pipe_format format;
1212    unsigned i;
1213
1214    if (velems->count > 16) {
1215        fprintf(stderr, "r300: More than 16 vertex elements are not supported,"
1216                " requested %i, using 16.\n", velems->count);
1217        velems->count = 16;
1218    }
1219
1220    /* Vertex shaders have no semantics on their inputs,
1221     * so PSC should just route stuff based on the vertex elements,
1222     * and not on attrib information. */
1223    for (i = 0; i < velems->count; i++) {
1224        format = velems->velem[i].src_format;
1225
1226        type = r300_translate_vertex_data_type(format) |
1227            (i << R300_DST_VEC_LOC_SHIFT);
1228        swizzle = r300_translate_vertex_data_swizzle(format);
1229
1230        if (i & 1) {
1231            vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
1232            vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
1233        } else {
1234            vstream->vap_prog_stream_cntl[i >> 1] |= type;
1235            vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
1236        }
1237    }
1238
1239    /* Set the last vector in the PSC. */
1240    if (i) {
1241        i -= 1;
1242    }
1243    vstream->vap_prog_stream_cntl[i >> 1] |=
1244        (R300_LAST_VEC << (i & 1 ? 16 : 0));
1245
1246    vstream->count = (i >> 1) + 1;
1247}
1248
1249static void* r300_create_vertex_elements_state(struct pipe_context* pipe,
1250                                               unsigned count,
1251                                               const struct pipe_vertex_element* attribs)
1252{
1253    struct r300_vertex_element_state *velems;
1254    unsigned i, size;
1255    enum pipe_format *format;
1256
1257    assert(count <= PIPE_MAX_ATTRIBS);
1258    velems = CALLOC_STRUCT(r300_vertex_element_state);
1259    if (velems != NULL) {
1260        velems->count = count;
1261        memcpy(velems->velem, attribs, sizeof(struct pipe_vertex_element) * count);
1262
1263        if (r300_screen(pipe->screen)->caps.has_tcl) {
1264            r300_vertex_psc(velems);
1265
1266            /* Check if the format is aligned to the size of DWORD.
1267             * We only care about the blocksizes of the formats since
1268             * swizzles are already set up. */
1269            for (i = 0; i < count; i++) {
1270                format = &velems->velem[i].src_format;
1271
1272                /* Replace some formats with their aligned counterparts,
1273                 * this is OK because we check for aligned strides too. */
1274                switch (*format) {
1275                    /* Align to RGBA8. */
1276                    case PIPE_FORMAT_R8_UNORM:
1277                    case PIPE_FORMAT_R8G8_UNORM:
1278                    case PIPE_FORMAT_R8G8B8_UNORM:
1279                        *format = PIPE_FORMAT_R8G8B8A8_UNORM;
1280                        continue;
1281                    case PIPE_FORMAT_R8_SNORM:
1282                    case PIPE_FORMAT_R8G8_SNORM:
1283                    case PIPE_FORMAT_R8G8B8_SNORM:
1284                        *format = PIPE_FORMAT_R8G8B8A8_SNORM;
1285                        continue;
1286                    case PIPE_FORMAT_R8_USCALED:
1287                    case PIPE_FORMAT_R8G8_USCALED:
1288                    case PIPE_FORMAT_R8G8B8_USCALED:
1289                        *format = PIPE_FORMAT_R8G8B8A8_USCALED;
1290                        continue;
1291                    case PIPE_FORMAT_R8_SSCALED:
1292                    case PIPE_FORMAT_R8G8_SSCALED:
1293                    case PIPE_FORMAT_R8G8B8_SSCALED:
1294                        *format = PIPE_FORMAT_R8G8B8A8_SSCALED;
1295                        continue;
1296
1297                    /* Align to RG16. */
1298                    case PIPE_FORMAT_R16_UNORM:
1299                        *format = PIPE_FORMAT_R16G16_UNORM;
1300                        continue;
1301                    case PIPE_FORMAT_R16_SNORM:
1302                        *format = PIPE_FORMAT_R16G16_SNORM;
1303                        continue;
1304                    case PIPE_FORMAT_R16_USCALED:
1305                        *format = PIPE_FORMAT_R16G16_USCALED;
1306                        continue;
1307                    case PIPE_FORMAT_R16_SSCALED:
1308                        *format = PIPE_FORMAT_R16G16_SSCALED;
1309                        continue;
1310                    case PIPE_FORMAT_R16_FLOAT:
1311                        *format = PIPE_FORMAT_R16G16_FLOAT;
1312                        continue;
1313
1314                    /* Align to RGBA16. */
1315                    case PIPE_FORMAT_R16G16B16_UNORM:
1316                        *format = PIPE_FORMAT_R16G16B16A16_UNORM;
1317                        continue;
1318                    case PIPE_FORMAT_R16G16B16_SNORM:
1319                        *format = PIPE_FORMAT_R16G16B16A16_SNORM;
1320                        continue;
1321                    case PIPE_FORMAT_R16G16B16_USCALED:
1322                        *format = PIPE_FORMAT_R16G16B16A16_USCALED;
1323                        continue;
1324                    case PIPE_FORMAT_R16G16B16_SSCALED:
1325                        *format = PIPE_FORMAT_R16G16B16A16_SSCALED;
1326                        continue;
1327                    case PIPE_FORMAT_R16G16B16_FLOAT:
1328                        *format = PIPE_FORMAT_R16G16B16A16_FLOAT;
1329                        continue;
1330
1331                    default:;
1332                }
1333
1334                size = util_format_get_blocksize(*format);
1335
1336                if (size % 4 != 0) {
1337                    /* XXX Shouldn't we align the format? */
1338                    fprintf(stderr, "r300_create_vertex_elements_state: "
1339                            "Unaligned format %s:%i isn't supported\n",
1340                            util_format_short_name(*format), size);
1341                    assert(0);
1342                    abort();
1343                }
1344            }
1345
1346        }
1347    }
1348    return velems;
1349}
1350
1351static void r300_bind_vertex_elements_state(struct pipe_context *pipe,
1352                                            void *state)
1353{
1354    struct r300_context *r300 = r300_context(pipe);
1355    struct r300_vertex_element_state *velems = state;
1356
1357    if (velems == NULL) {
1358        return;
1359    }
1360
1361    r300->velems = velems;
1362
1363    if (r300->draw) {
1364        draw_flush(r300->draw);
1365        draw_set_vertex_elements(r300->draw, velems->count, velems->velem);
1366    }
1367
1368    UPDATE_STATE(&velems->vertex_stream, r300->vertex_stream_state);
1369    r300->vertex_stream_state.size = (1 + velems->vertex_stream.count) * 2;
1370}
1371
1372static void r300_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
1373{
1374   FREE(state);
1375}
1376
1377static void* r300_create_vs_state(struct pipe_context* pipe,
1378                                  const struct pipe_shader_state* shader)
1379{
1380    struct r300_context* r300 = r300_context(pipe);
1381
1382    struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
1383
1384    /* Copy state directly into shader. */
1385    vs->state = *shader;
1386    vs->state.tokens = tgsi_dup_tokens(shader->tokens);
1387
1388    if (r300->screen->caps.has_tcl) {
1389        r300_translate_vertex_shader(r300, vs, vs->state.tokens);
1390    } else {
1391        vs->draw_vs = draw_create_vertex_shader(r300->draw, shader);
1392    }
1393
1394    return vs;
1395}
1396
1397static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
1398{
1399    struct r300_context* r300 = r300_context(pipe);
1400    struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1401
1402    if (vs == NULL) {
1403        r300->vs_state.state = NULL;
1404        return;
1405    }
1406    if (vs == r300->vs_state.state) {
1407        return;
1408    }
1409    r300->vs_state.state = vs;
1410
1411    /* The majority of the RS block bits is dependent on the vertex shader. */
1412    r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
1413
1414    if (r300->screen->caps.has_tcl) {
1415        r300->vs_state.dirty = TRUE;
1416        r300->vs_state.size =
1417                vs->code.length + 9 +
1418                (vs->immediates_count ? vs->immediates_count * 4 + 3 : 0);
1419
1420        if (vs->externals_count) {
1421            r300->vs_constants.dirty = TRUE;
1422            r300->vs_constants.size = vs->externals_count * 4 + 3;
1423        } else {
1424            r300->vs_constants.size = 0;
1425        }
1426
1427        r300->pvs_flush.dirty = TRUE;
1428    } else {
1429        draw_flush(r300->draw);
1430        draw_bind_vertex_shader(r300->draw,
1431                (struct draw_vertex_shader*)vs->draw_vs);
1432    }
1433}
1434
1435static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
1436{
1437    struct r300_context* r300 = r300_context(pipe);
1438    struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1439
1440    if (r300->screen->caps.has_tcl) {
1441        rc_constants_destroy(&vs->code.constants);
1442    } else {
1443        draw_delete_vertex_shader(r300->draw,
1444                (struct draw_vertex_shader*)vs->draw_vs);
1445    }
1446
1447    FREE((void*)vs->state.tokens);
1448    FREE(shader);
1449}
1450
1451static void r300_set_constant_buffer(struct pipe_context *pipe,
1452                                     uint shader, uint index,
1453                                     struct pipe_resource *buf)
1454{
1455    struct r300_context* r300 = r300_context(pipe);
1456    struct r300_constant_buffer *cbuf;
1457    struct pipe_transfer *tr;
1458    void *mapped;
1459    int max_size = 0;
1460
1461    switch (shader) {
1462        case PIPE_SHADER_VERTEX:
1463            cbuf = (struct r300_constant_buffer*)r300->vs_constants.state;
1464            max_size = 256;
1465            break;
1466        case PIPE_SHADER_FRAGMENT:
1467            cbuf = (struct r300_constant_buffer*)r300->fs_constants.state;
1468            if (r300->screen->caps.is_r500) {
1469                max_size = 256;
1470            } else {
1471                max_size = 32;
1472            }
1473            break;
1474        default:
1475            assert(0);
1476            return;
1477    }
1478
1479    if (buf == NULL || buf->width0 == 0 ||
1480        (mapped = pipe_buffer_map(pipe, buf, PIPE_TRANSFER_READ, &tr)) == NULL)
1481    {
1482        cbuf->count = 0;
1483        return;
1484    }
1485
1486    assert((buf->width0 % 4 * sizeof(float)) == 0);
1487
1488    /* Check the size of the constant buffer. */
1489    /* XXX Subtract immediates and RC_STATE_* variables. */
1490    if (buf->width0 > (sizeof(float) * 4 * max_size)) {
1491        fprintf(stderr, "r300: Max size of the constant buffer is "
1492                      "%i*4 floats.\n", max_size);
1493        abort();
1494    }
1495
1496    memcpy(cbuf->constants, mapped, buf->width0);
1497    cbuf->count = buf->width0 / (4 * sizeof(float));
1498    pipe_buffer_unmap(pipe, buf, tr);
1499
1500    if (shader == PIPE_SHADER_VERTEX) {
1501        if (r300->screen->caps.has_tcl) {
1502            if (r300->vs_constants.size) {
1503                r300->vs_constants.dirty = TRUE;
1504            }
1505            r300->pvs_flush.dirty = TRUE;
1506        } else if (r300->draw) {
1507            draw_set_mapped_constant_buffer(r300->draw, PIPE_SHADER_VERTEX,
1508                0, cbuf->constants,
1509                buf->width0);
1510        }
1511    } else if (shader == PIPE_SHADER_FRAGMENT) {
1512        r300->fs_constants.dirty = TRUE;
1513    }
1514}
1515
1516void r300_init_state_functions(struct r300_context* r300)
1517{
1518    r300->context.create_blend_state = r300_create_blend_state;
1519    r300->context.bind_blend_state = r300_bind_blend_state;
1520    r300->context.delete_blend_state = r300_delete_blend_state;
1521
1522    r300->context.set_blend_color = r300_set_blend_color;
1523
1524    r300->context.set_clip_state = r300_set_clip_state;
1525
1526    r300->context.set_constant_buffer = r300_set_constant_buffer;
1527
1528    r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
1529    r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
1530    r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
1531
1532    r300->context.set_stencil_ref = r300_set_stencil_ref;
1533
1534    r300->context.set_framebuffer_state = r300_set_framebuffer_state;
1535
1536    r300->context.create_fs_state = r300_create_fs_state;
1537    r300->context.bind_fs_state = r300_bind_fs_state;
1538    r300->context.delete_fs_state = r300_delete_fs_state;
1539
1540    r300->context.set_polygon_stipple = r300_set_polygon_stipple;
1541
1542    r300->context.create_rasterizer_state = r300_create_rs_state;
1543    r300->context.bind_rasterizer_state = r300_bind_rs_state;
1544    r300->context.delete_rasterizer_state = r300_delete_rs_state;
1545
1546    r300->context.create_sampler_state = r300_create_sampler_state;
1547    r300->context.bind_fragment_sampler_states = r300_bind_sampler_states;
1548    r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures;
1549    r300->context.delete_sampler_state = r300_delete_sampler_state;
1550
1551    r300->context.set_fragment_sampler_views = r300_set_fragment_sampler_views;
1552    r300->context.create_sampler_view = r300_create_sampler_view;
1553    r300->context.sampler_view_destroy = r300_sampler_view_destroy;
1554
1555    r300->context.set_scissor_state = r300_set_scissor_state;
1556
1557    r300->context.set_viewport_state = r300_set_viewport_state;
1558
1559    r300->context.set_vertex_buffers = r300_set_vertex_buffers;
1560
1561    r300->context.create_vertex_elements_state = r300_create_vertex_elements_state;
1562    r300->context.bind_vertex_elements_state = r300_bind_vertex_elements_state;
1563    r300->context.delete_vertex_elements_state = r300_delete_vertex_elements_state;
1564
1565    r300->context.create_vs_state = r300_create_vs_state;
1566    r300->context.bind_vs_state = r300_bind_vs_state;
1567    r300->context.delete_vs_state = r300_delete_vs_state;
1568}
1569