r300_state.c revision fff5be8e7b4557c221f2425dcafc2e7cbbba76ba
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_reg.h"
36#include "r300_screen.h"
37#include "r300_screen_buffer.h"
38#include "r300_state_inlines.h"
39#include "r300_fs.h"
40#include "r300_vs.h"
41
42#include "radeon_winsys.h"
43
44/* r300_state: Functions used to intialize state context by translating
45 * Gallium state objects into semi-native r300 state objects. */
46
47static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
48                                            unsigned dstRGB, unsigned dstA)
49{
50    /* If the blend equation is ADD or REVERSE_SUBTRACT,
51     * SRC_ALPHA == 0, and the following state is set, the colorbuffer
52     * will not be changed.
53     * Notice that the dst factors are the src factors inverted. */
54    return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
55            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
56            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
57           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
58            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
59            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
60            srcA == PIPE_BLENDFACTOR_ZERO) &&
61           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
62            dstRGB == PIPE_BLENDFACTOR_ONE) &&
63           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
64            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
65            dstA == PIPE_BLENDFACTOR_ONE);
66}
67
68static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
69                                            unsigned dstRGB, unsigned dstA)
70{
71    /* If the blend equation is ADD or REVERSE_SUBTRACT,
72     * SRC_ALPHA == 1, and the following state is set, the colorbuffer
73     * will not be changed.
74     * Notice that the dst factors are the src factors inverted. */
75    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
76            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
77           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
78            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
79            srcA == PIPE_BLENDFACTOR_ZERO) &&
80           (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
81            dstRGB == PIPE_BLENDFACTOR_ONE) &&
82           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
83            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
84            dstA == PIPE_BLENDFACTOR_ONE);
85}
86
87static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
88                                            unsigned dstRGB, unsigned dstA)
89{
90    /* If the blend equation is ADD or REVERSE_SUBTRACT,
91     * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
92     * will not be changed.
93     * Notice that the dst factors are the src factors inverted. */
94    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
95            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
96           (srcA == PIPE_BLENDFACTOR_ZERO) &&
97           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
98            dstRGB == PIPE_BLENDFACTOR_ONE) &&
99           (dstA == PIPE_BLENDFACTOR_ONE);
100}
101
102static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
103                                            unsigned dstRGB, unsigned dstA)
104{
105    /* If the blend equation is ADD or REVERSE_SUBTRACT,
106     * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
107     * will not be changed.
108     * Notice that the dst factors are the src factors inverted. */
109    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
110            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
111           (srcA == PIPE_BLENDFACTOR_ZERO) &&
112           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
113            dstRGB == PIPE_BLENDFACTOR_ONE) &&
114           (dstA == PIPE_BLENDFACTOR_ONE);
115}
116
117static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
118                                                  unsigned dstRGB, unsigned dstA)
119{
120    /* If the blend equation is ADD or REVERSE_SUBTRACT,
121     * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
122     * the colorbuffer will not be changed.
123     * Notice that the dst factors are the src factors inverted. */
124    return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
125            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
126            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
127            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
128           (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
129            srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
130            srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
131            srcA == PIPE_BLENDFACTOR_ZERO) &&
132           (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
133            dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
134            dstRGB == PIPE_BLENDFACTOR_ONE) &&
135           (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
136            dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
137            dstA == PIPE_BLENDFACTOR_ONE);
138}
139
140static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
141                                                  unsigned dstRGB, unsigned dstA)
142{
143    /* If the blend equation is ADD or REVERSE_SUBTRACT,
144     * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
145     * the colorbuffer will not be changed.
146     * Notice that the dst factors are the src factors inverted. */
147    return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
148            srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
149            srcRGB == PIPE_BLENDFACTOR_ZERO) &&
150           (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
151            srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
152            srcA == PIPE_BLENDFACTOR_ZERO) &&
153           (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
154            dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
155            dstRGB == PIPE_BLENDFACTOR_ONE) &&
156           (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
157            dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
158            dstA == PIPE_BLENDFACTOR_ONE);
159}
160
161static unsigned bgra_cmask(unsigned mask)
162{
163    /* Gallium uses RGBA color ordering while R300 expects BGRA. */
164
165    return ((mask & PIPE_MASK_R) << 2) |
166           ((mask & PIPE_MASK_B) >> 2) |
167           (mask & (PIPE_MASK_G | PIPE_MASK_A));
168}
169
170/* Create a new blend state based on the CSO blend state.
171 *
172 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
173static void* r300_create_blend_state(struct pipe_context* pipe,
174                                     const struct pipe_blend_state* state)
175{
176    struct r300_screen* r300screen = r300_screen(pipe->screen);
177    struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
178
179    if (state->rt[0].blend_enable)
180    {
181        unsigned eqRGB = state->rt[0].rgb_func;
182        unsigned srcRGB = state->rt[0].rgb_src_factor;
183        unsigned dstRGB = state->rt[0].rgb_dst_factor;
184
185        unsigned eqA = state->rt[0].alpha_func;
186        unsigned srcA = state->rt[0].alpha_src_factor;
187        unsigned dstA = state->rt[0].alpha_dst_factor;
188
189        /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
190         * this is just the crappy D3D naming */
191        blend->blend_control = R300_ALPHA_BLEND_ENABLE |
192            r300_translate_blend_function(eqRGB) |
193            ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
194            ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
195
196        /* Optimization: some operations do not require the destination color.
197         *
198         * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
199         * otherwise blending gives incorrect results. It seems to be
200         * a hardware bug. */
201        if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
202            eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
203            dstRGB != PIPE_BLENDFACTOR_ZERO ||
204            dstA != PIPE_BLENDFACTOR_ZERO ||
205            srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
206            srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
207            srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
208            srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
209            srcA == PIPE_BLENDFACTOR_DST_COLOR ||
210            srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
211            srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
212            srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
213            srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) {
214            /* Enable reading from the colorbuffer. */
215            blend->blend_control |= R300_READ_ENABLE;
216
217            if (r300_screen(r300_context(pipe)->context.screen)->caps->is_r500) {
218                /* Optimization: Depending on incoming pixels, we can
219                 * conditionally disable the reading in hardware... */
220                if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
221                    eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
222                    /* Disable reading if SRC_ALPHA == 0. */
223                    if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
224                         dstRGB == PIPE_BLENDFACTOR_ZERO) &&
225                        (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
226                         dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
227                         dstA == PIPE_BLENDFACTOR_ZERO)) {
228                         blend->blend_control |= R500_SRC_ALPHA_0_NO_READ;
229                    }
230
231                    /* Disable reading if SRC_ALPHA == 1. */
232                    if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
233                         dstRGB == PIPE_BLENDFACTOR_ZERO) &&
234                        (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
235                         dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
236                         dstA == PIPE_BLENDFACTOR_ZERO)) {
237                         blend->blend_control |= R500_SRC_ALPHA_1_NO_READ;
238                    }
239                }
240            }
241        }
242
243        /* Optimization: discard pixels which don't change the colorbuffer.
244         *
245         * The code below is non-trivial and some math is involved.
246         *
247         * Discarding pixels must be disabled when FP16 AA is enabled.
248         * This is a hardware bug. Also, this implementation wouldn't work
249         * with FP blending enabled and equation clamping disabled.
250         *
251         * Equations other than ADD are rarely used and therefore won't be
252         * optimized. */
253        if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
254            (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
255            /* ADD: X+Y
256             * REVERSE_SUBTRACT: Y-X
257             *
258             * The idea is:
259             * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
260             * then CB will not be changed.
261             *
262             * Given the srcFactor and dstFactor variables, we can derive
263             * what src and dst should be equal to and discard appropriate
264             * pixels.
265             */
266            if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
267                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
268            } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
269                                                    dstRGB, dstA)) {
270                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
271            } else if (blend_discard_if_src_color_0(srcRGB, srcA,
272                                                    dstRGB, dstA)) {
273                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
274            } else if (blend_discard_if_src_color_1(srcRGB, srcA,
275                                                    dstRGB, dstA)) {
276                blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
277            } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
278                                                          dstRGB, dstA)) {
279                blend->blend_control |=
280                    R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
281            } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
282                                                          dstRGB, dstA)) {
283                blend->blend_control |=
284                    R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
285            }
286        }
287
288        /* separate alpha */
289        if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
290            blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
291            blend->alpha_blend_control =
292                r300_translate_blend_function(eqA) |
293                (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
294                (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
295        }
296    }
297
298    /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
299    if (state->logicop_enable) {
300        blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
301                (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
302    }
303
304    /* Color channel masks for all MRTs. */
305    blend->color_channel_mask = bgra_cmask(state->rt[0].colormask);
306    if (r300screen->caps->is_r500 && state->independent_blend_enable) {
307        if (state->rt[1].blend_enable) {
308            blend->color_channel_mask |= bgra_cmask(state->rt[1].colormask) << 4;
309        }
310        if (state->rt[2].blend_enable) {
311            blend->color_channel_mask |= bgra_cmask(state->rt[2].colormask) << 8;
312        }
313        if (state->rt[3].blend_enable) {
314            blend->color_channel_mask |= bgra_cmask(state->rt[3].colormask) << 12;
315        }
316    }
317
318    if (state->dither) {
319        blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
320                R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
321    }
322
323    return (void*)blend;
324}
325
326/* Bind blend state. */
327static void r300_bind_blend_state(struct pipe_context* pipe,
328                                  void* state)
329{
330    struct r300_context* r300 = r300_context(pipe);
331
332    r300->blend_state.state = state;
333    r300->blend_state.dirty = TRUE;
334}
335
336/* Free blend state. */
337static void r300_delete_blend_state(struct pipe_context* pipe,
338                                    void* state)
339{
340    FREE(state);
341}
342
343/* Convert float to 10bit integer */
344static unsigned float_to_fixed10(float f)
345{
346    return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
347}
348
349/* Set blend color.
350 * Setup both R300 and R500 registers, figure out later which one to write. */
351static void r300_set_blend_color(struct pipe_context* pipe,
352                                 const struct pipe_blend_color* color)
353{
354    struct r300_context* r300 = r300_context(pipe);
355    struct r300_screen* r300screen = r300_screen(pipe->screen);
356    struct r300_blend_color_state* state =
357        (struct r300_blend_color_state*)r300->blend_color_state.state;
358    union util_color uc;
359
360    util_pack_color(color->color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc);
361    state->blend_color = uc.ui;
362
363    /* XXX if FP16 blending is enabled, we should use the FP16 format */
364    state->blend_color_red_alpha =
365        float_to_fixed10(color->color[0]) |
366        (float_to_fixed10(color->color[3]) << 16);
367    state->blend_color_green_blue =
368        float_to_fixed10(color->color[2]) |
369        (float_to_fixed10(color->color[1]) << 16);
370
371    r300->blend_color_state.size = r300screen->caps->is_r500 ? 3 : 2;
372    r300->blend_color_state.dirty = TRUE;
373}
374
375static void r300_set_clip_state(struct pipe_context* pipe,
376                                const struct pipe_clip_state* state)
377{
378    struct r300_context* r300 = r300_context(pipe);
379
380    if (r300_screen(pipe->screen)->caps->has_tcl) {
381        memcpy(r300->clip_state.state, state, sizeof(struct pipe_clip_state));
382        r300->clip_state.size = 29;
383    } else {
384        draw_flush(r300->draw);
385        draw_set_clip_state(r300->draw, state);
386        r300->clip_state.size = 2;
387    }
388
389    r300->clip_state.dirty = TRUE;
390}
391
392/* Create a new depth, stencil, and alpha state based on the CSO dsa state.
393 *
394 * This contains the depth buffer, stencil buffer, alpha test, and such.
395 * On the Radeon, depth and stencil buffer setup are intertwined, which is
396 * the reason for some of the strange-looking assignments across registers. */
397static void*
398        r300_create_dsa_state(struct pipe_context* pipe,
399                              const struct pipe_depth_stencil_alpha_state* state)
400{
401    struct r300_capabilities *caps =
402        r300_screen(r300_context(pipe)->context.screen)->caps;
403    struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
404
405    /* Depth test setup. */
406    if (state->depth.enabled) {
407        dsa->z_buffer_control |= R300_Z_ENABLE;
408
409        if (state->depth.writemask) {
410            dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
411        }
412
413        dsa->z_stencil_control |=
414            (r300_translate_depth_stencil_function(state->depth.func) <<
415                R300_Z_FUNC_SHIFT);
416    }
417
418    /* Stencil buffer setup. */
419    if (state->stencil[0].enabled) {
420        dsa->z_buffer_control |= R300_STENCIL_ENABLE;
421        dsa->z_stencil_control |=
422            (r300_translate_depth_stencil_function(state->stencil[0].func) <<
423                R300_S_FRONT_FUNC_SHIFT) |
424            (r300_translate_stencil_op(state->stencil[0].fail_op) <<
425                R300_S_FRONT_SFAIL_OP_SHIFT) |
426            (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
427                R300_S_FRONT_ZPASS_OP_SHIFT) |
428            (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
429                R300_S_FRONT_ZFAIL_OP_SHIFT);
430
431        dsa->stencil_ref_mask =
432                (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
433                (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
434
435        if (state->stencil[1].enabled) {
436            dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
437            dsa->z_stencil_control |=
438            (r300_translate_depth_stencil_function(state->stencil[1].func) <<
439                R300_S_BACK_FUNC_SHIFT) |
440            (r300_translate_stencil_op(state->stencil[1].fail_op) <<
441                R300_S_BACK_SFAIL_OP_SHIFT) |
442            (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
443                R300_S_BACK_ZPASS_OP_SHIFT) |
444            (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
445                R300_S_BACK_ZFAIL_OP_SHIFT);
446
447            if (caps->is_r500)
448            {
449                dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
450                dsa->stencil_ref_bf =
451                    (state->stencil[1].valuemask <<
452                    R300_STENCILMASK_SHIFT) |
453                    (state->stencil[1].writemask <<
454                    R300_STENCILWRITEMASK_SHIFT);
455            }
456        }
457    }
458
459    /* Alpha test setup. */
460    if (state->alpha.enabled) {
461        dsa->alpha_function =
462            r300_translate_alpha_function(state->alpha.func) |
463            R300_FG_ALPHA_FUNC_ENABLE;
464
465        /* We could use 10bit alpha ref but who needs that? */
466        dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
467
468        if (caps->is_r500)
469            dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
470    }
471
472    return (void*)dsa;
473}
474
475/* Bind DSA state. */
476static void r300_bind_dsa_state(struct pipe_context* pipe,
477                                void* state)
478{
479    struct r300_context* r300 = r300_context(pipe);
480    struct r300_screen* r300screen = r300_screen(pipe->screen);
481
482    r300->dsa_state.state = state;
483    r300->dsa_state.size = r300screen->caps->is_r500 ? 8 : 6;
484    r300->dsa_state.dirty = TRUE;
485}
486
487/* Free DSA state. */
488static void r300_delete_dsa_state(struct pipe_context* pipe,
489                                  void* state)
490{
491    FREE(state);
492}
493
494static void r300_set_stencil_ref(struct pipe_context* pipe,
495                                 const struct pipe_stencil_ref* sr)
496{
497    struct r300_context* r300 = r300_context(pipe);
498    r300->stencil_ref = *sr;
499    r300->dsa_state.dirty = TRUE;
500}
501
502/* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
503static void r300_fb_update_tiling_flags(struct r300_context *r300,
504                               const struct pipe_framebuffer_state *old_state,
505                               const struct pipe_framebuffer_state *new_state)
506{
507    struct r300_texture *tex;
508    unsigned i, j, level;
509
510    /* Reset tiling flags for old surfaces to default values. */
511    for (i = 0; i < old_state->nr_cbufs; i++) {
512        for (j = 0; j < new_state->nr_cbufs; j++) {
513            if (old_state->cbufs[i]->texture == new_state->cbufs[j]->texture) {
514                break;
515            }
516        }
517        /* If not binding the surface again... */
518        if (j != new_state->nr_cbufs) {
519            continue;
520        }
521
522        tex = (struct r300_texture*)old_state->cbufs[i]->texture;
523
524        if (tex) {
525            r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
526                                            tex->pitch[0],
527                                            tex->microtile != 0,
528                                            tex->macrotile != 0);
529        }
530    }
531    if (old_state->zsbuf &&
532        (!new_state->zsbuf ||
533         old_state->zsbuf->texture != new_state->zsbuf->texture)) {
534        tex = (struct r300_texture*)old_state->zsbuf->texture;
535
536        if (tex) {
537            r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
538                                            tex->pitch[0],
539                                            tex->microtile != 0,
540                                            tex->macrotile != 0);
541        }
542    }
543
544    /* Set tiling flags for new surfaces. */
545    for (i = 0; i < new_state->nr_cbufs; i++) {
546        tex = (struct r300_texture*)new_state->cbufs[i]->texture;
547        level = new_state->cbufs[i]->level;
548
549        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
550                                        tex->pitch[level],
551                                        tex->microtile != 0,
552                                        tex->mip_macrotile[level] != 0);
553    }
554    if (new_state->zsbuf) {
555        tex = (struct r300_texture*)new_state->zsbuf->texture;
556        level = new_state->zsbuf->level;
557
558        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
559                                        tex->pitch[level],
560                                        tex->microtile != 0,
561                                        tex->mip_macrotile[level] != 0);
562    }
563}
564
565static void
566    r300_set_framebuffer_state(struct pipe_context* pipe,
567                               const struct pipe_framebuffer_state* state)
568{
569    struct r300_context* r300 = r300_context(pipe);
570    struct r300_screen* r300screen = r300_screen(pipe->screen);
571    unsigned max_width, max_height;
572    uint32_t zbuffer_bpp = 0;
573
574
575    if (state->nr_cbufs > 4) {
576        debug_printf("r300: Implementation error: Too many MRTs in %s, "
577            "refusing to bind framebuffer state!\n", __FUNCTION__);
578        return;
579    }
580
581    if (r300screen->caps->is_r500) {
582        max_width = max_height = 4096;
583    } else if (r300screen->caps->is_r400) {
584        max_width = max_height = 4021;
585    } else {
586        max_width = max_height = 2560;
587    }
588
589    if (state->width > max_width || state->height > max_height) {
590        debug_printf("r300: Implementation error: Render targets are too "
591        "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__);
592        return;
593    }
594
595
596    if (r300->draw) {
597        draw_flush(r300->draw);
598    }
599
600    memcpy(r300->fb_state.state, state, sizeof(struct pipe_framebuffer_state));
601
602    r300->fb_state.size = (10 * state->nr_cbufs) + (state->zsbuf ? 10 : 0) + 6;
603
604    r300_fb_update_tiling_flags(r300, r300->fb_state.state, state);
605
606    /* XXX wait what */
607    r300->blend_state.dirty = TRUE;
608    r300->dsa_state.dirty = TRUE;
609    r300->fb_state.dirty = TRUE;
610    r300->scissor_state.dirty = TRUE;
611
612    /* Polygon offset depends on the zbuffer bit depth. */
613    if (state->zsbuf && r300->polygon_offset_enabled) {
614        switch (util_format_get_blocksize(state->zsbuf->texture->format)) {
615            case 2:
616                zbuffer_bpp = 16;
617                break;
618            case 4:
619                zbuffer_bpp = 24;
620                break;
621        }
622
623        if (r300->zbuffer_bpp != zbuffer_bpp) {
624            r300->zbuffer_bpp = zbuffer_bpp;
625            r300->rs_state.dirty = TRUE;
626        }
627    }
628}
629
630/* Create fragment shader state. */
631static void* r300_create_fs_state(struct pipe_context* pipe,
632                                  const struct pipe_shader_state* shader)
633{
634    struct r300_fragment_shader* fs = NULL;
635
636    fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
637
638    /* Copy state directly into shader. */
639    fs->state = *shader;
640    fs->state.tokens = tgsi_dup_tokens(shader->tokens);
641
642    tgsi_scan_shader(shader->tokens, &fs->info);
643    r300_shader_read_fs_inputs(&fs->info, &fs->inputs);
644
645    return (void*)fs;
646}
647
648/* Bind fragment shader state. */
649static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
650{
651    struct r300_context* r300 = r300_context(pipe);
652    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
653
654    if (fs == NULL) {
655        r300->fs = NULL;
656        return;
657    }
658
659    r300->fs = fs;
660    r300_pick_fragment_shader(r300);
661
662    if (r300->vs && r300_vertex_shader_setup_wpos(r300)) {
663        r300->vertex_format_state.dirty = TRUE;
664    }
665
666    r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | R300_NEW_FRAGMENT_SHADER_CONSTANTS;
667}
668
669/* Delete fragment shader state. */
670static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
671{
672    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
673    struct r300_fragment_shader_code *tmp, *ptr = fs->first;
674
675    while (ptr) {
676        tmp = ptr;
677        ptr = ptr->next;
678        rc_constants_destroy(&tmp->code.constants);
679        FREE(tmp);
680    }
681    FREE((void*)fs->state.tokens);
682    FREE(shader);
683}
684
685static void r300_set_polygon_stipple(struct pipe_context* pipe,
686                                     const struct pipe_poly_stipple* state)
687{
688    /* XXX no idea how to set this up, but not terribly important */
689}
690
691/* Create a new rasterizer state based on the CSO rasterizer state.
692 *
693 * This is a very large chunk of state, and covers most of the graphics
694 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
695 *
696 * In a not entirely unironic sidenote, this state has nearly nothing to do
697 * with the actual block on the Radeon called the rasterizer (RS). */
698static void* r300_create_rs_state(struct pipe_context* pipe,
699                                  const struct pipe_rasterizer_state* state)
700{
701    struct r300_screen* r300screen = r300_screen(pipe->screen);
702    struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
703
704    /* Copy rasterizer state for Draw. */
705    rs->rs = *state;
706
707#ifdef PIPE_ARCH_LITTLE_ENDIAN
708    rs->vap_control_status = R300_VC_NO_SWAP;
709#else
710    rs->vap_control_status = R300_VC_32BIT_SWAP;
711#endif
712
713    /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
714     * Else, enable HW TCL and force Draw's TCL off. */
715    if (state->bypass_vs_clip_and_viewport ||
716            !r300screen->caps->has_tcl) {
717        rs->vap_control_status |= R300_VAP_TCL_BYPASS;
718    }
719
720    rs->point_size = pack_float_16_6x(state->point_size) |
721        (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
722
723        /* Point minimum and maximum sizes. This register has to be emitted,
724         * and it'd be a step backwards to put it in invariant state. */
725        if (r300screen->caps->is_r500) {
726            rs->point_minmax =
727            ((int)(0.0 * 6.0) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
728            ((int)(4096.0 * 6.0) << R300_GA_POINT_MINMAX_MAX_SHIFT);
729        } else if (r300screen->caps->is_r400) {
730            rs->point_minmax =
731            ((int)(0.0 * 6.0) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
732            ((int)(4021.0 * 6.0) << R300_GA_POINT_MINMAX_MAX_SHIFT);
733        } else {
734            rs->point_minmax =
735            ((int)(0.0 * 6.0) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
736            ((int)(2560.0 * 6.0) << R300_GA_POINT_MINMAX_MAX_SHIFT);
737        }
738
739    rs->line_control = pack_float_16_6x(state->line_width) |
740        R300_GA_LINE_CNTL_END_TYPE_COMP;
741
742    /* Enable polygon mode */
743    if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
744        state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
745        rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
746    }
747
748    /* Radeons don't think in "CW/CCW", they think in "front/back". */
749    if (state->front_winding == PIPE_WINDING_CW) {
750        rs->cull_mode = R300_FRONT_FACE_CW;
751
752        /* Polygon offset */
753        if (state->offset_cw) {
754            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
755        }
756        if (state->offset_ccw) {
757            rs->polygon_offset_enable |= R300_BACK_ENABLE;
758        }
759
760        /* Polygon mode */
761        if (rs->polygon_mode) {
762            rs->polygon_mode |=
763                r300_translate_polygon_mode_front(state->fill_cw);
764            rs->polygon_mode |=
765                r300_translate_polygon_mode_back(state->fill_ccw);
766        }
767    } else {
768        rs->cull_mode = R300_FRONT_FACE_CCW;
769
770        /* Polygon offset */
771        if (state->offset_ccw) {
772            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
773        }
774        if (state->offset_cw) {
775            rs->polygon_offset_enable |= R300_BACK_ENABLE;
776        }
777
778        /* Polygon mode */
779        if (rs->polygon_mode) {
780            rs->polygon_mode |=
781                r300_translate_polygon_mode_front(state->fill_ccw);
782            rs->polygon_mode |=
783                r300_translate_polygon_mode_back(state->fill_cw);
784        }
785    }
786    if (state->front_winding & state->cull_mode) {
787        rs->cull_mode |= R300_CULL_FRONT;
788    }
789    if (~(state->front_winding) & state->cull_mode) {
790        rs->cull_mode |= R300_CULL_BACK;
791    }
792
793    if (rs->polygon_offset_enable) {
794        rs->depth_offset = state->offset_units;
795        rs->depth_scale = state->offset_scale;
796    }
797
798    if (state->line_stipple_enable) {
799        rs->line_stipple_config =
800            R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
801            (fui((float)state->line_stipple_factor) &
802                R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
803        /* XXX this might need to be scaled up */
804        rs->line_stipple_value = state->line_stipple_pattern;
805    }
806
807    if (state->flatshade) {
808        rs->color_control = R300_SHADE_MODEL_FLAT;
809    } else {
810        rs->color_control = R300_SHADE_MODEL_SMOOTH;
811    }
812
813    return (void*)rs;
814}
815
816/* Bind rasterizer state. */
817static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
818{
819    struct r300_context* r300 = r300_context(pipe);
820    struct r300_rs_state* rs = (struct r300_rs_state*)state;
821
822    if (r300->draw) {
823        draw_flush(r300->draw);
824        draw_set_rasterizer_state(r300->draw, &rs->rs);
825    }
826
827    if (rs) {
828        r300->tcl_bypass = rs->rs.bypass_vs_clip_and_viewport;
829        r300->polygon_offset_enabled = rs->rs.offset_cw || rs->rs.offset_ccw;
830    } else {
831        r300->tcl_bypass = FALSE;
832        r300->polygon_offset_enabled = FALSE;
833    }
834
835    r300->rs_state.state = rs;
836    r300->rs_state.dirty = TRUE;
837    /* XXX Why is this still needed, dammit!? */
838    r300->scissor_state.dirty = TRUE;
839    r300->viewport_state.dirty = TRUE;
840
841    /* XXX Clean these up when we move to atom emits */
842    if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) {
843        r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
844    }
845}
846
847/* Free rasterizer state. */
848static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
849{
850    FREE(state);
851}
852
853static void*
854        r300_create_sampler_state(struct pipe_context* pipe,
855                                  const struct pipe_sampler_state* state)
856{
857    struct r300_context* r300 = r300_context(pipe);
858    struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
859    int lod_bias;
860    union util_color uc;
861
862    sampler->state = *state;
863
864    sampler->filter0 |=
865        (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
866        (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
867        (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
868
869    sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
870                                                   state->mag_img_filter,
871                                                   state->min_mip_filter,
872                                                   state->max_anisotropy > 0);
873
874    /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
875    /* We must pass these to the emit function to clamp them properly. */
876    sampler->min_lod = MAX2((unsigned)state->min_lod, 0);
877    sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0);
878
879    lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
880
881    sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
882
883    sampler->filter1 |= r300_anisotropy(state->max_anisotropy);
884
885    util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc);
886    sampler->border_color = uc.ui;
887
888    /* R500-specific fixups and optimizations */
889    if (r300_screen(r300->context.screen)->caps->is_r500) {
890        sampler->filter1 |= R500_BORDER_FIX;
891    }
892
893    return (void*)sampler;
894}
895
896static void r300_bind_sampler_states(struct pipe_context* pipe,
897                                     unsigned count,
898                                     void** states)
899{
900    struct r300_context* r300 = r300_context(pipe);
901    int i;
902
903    if (count > 8) {
904        return;
905    }
906
907    for (i = 0; i < count; i++) {
908        if (r300->sampler_states[i] != states[i]) {
909            r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
910            r300->dirty_state |= (R300_NEW_SAMPLER << i);
911        }
912    }
913
914    r300->sampler_count = count;
915
916    /* Pick a fragment shader based on the texture compare state. */
917    if (r300->fs && (r300->dirty_state & R300_ANY_NEW_SAMPLERS)) {
918        if (r300_pick_fragment_shader(r300)) {
919            r300->dirty_state |= R300_NEW_FRAGMENT_SHADER |
920                                 R300_NEW_FRAGMENT_SHADER_CONSTANTS;
921        }
922    }
923}
924
925static void r300_lacks_vertex_textures(struct pipe_context* pipe,
926                                       unsigned count,
927                                       void** states)
928{
929}
930
931static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
932{
933    FREE(state);
934}
935
936static void r300_set_sampler_textures(struct pipe_context* pipe,
937                                      unsigned count,
938                                      struct pipe_texture** texture)
939{
940    struct r300_context* r300 = r300_context(pipe);
941    boolean is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
942    int i;
943
944    /* XXX magic num */
945    if (count > 8) {
946        return;
947    }
948
949    for (i = 0; i < count; i++) {
950        if (r300->textures[i] != (struct r300_texture*)texture[i]) {
951            pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
952                texture[i]);
953            r300->dirty_state |= (R300_NEW_TEXTURE << i);
954
955            /* R300-specific - set the texrect factor in a fragment shader */
956            if (!is_r500 && r300->textures[i]->is_npot) {
957                /* XXX It would be nice to re-emit just 1 constant,
958                 * XXX not all of them */
959                r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
960            }
961        }
962    }
963
964    for (i = count; i < 8; i++) {
965        if (r300->textures[i]) {
966            pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
967                NULL);
968            r300->dirty_state |= (R300_NEW_TEXTURE << i);
969        }
970    }
971
972    r300->texture_count = count;
973}
974
975static void r300_set_scissor_state(struct pipe_context* pipe,
976                                   const struct pipe_scissor_state* state)
977{
978    struct r300_context* r300 = r300_context(pipe);
979
980    memcpy(r300->scissor_state.state, state,
981        sizeof(struct pipe_scissor_state));
982
983    r300->scissor_state.dirty = TRUE;
984}
985
986static void r300_set_viewport_state(struct pipe_context* pipe,
987                                    const struct pipe_viewport_state* state)
988{
989    struct r300_context* r300 = r300_context(pipe);
990    struct r300_viewport_state* viewport =
991        (struct r300_viewport_state*)r300->viewport_state.state;
992
993    /* Do the transform in HW. */
994    viewport->vte_control = R300_VTX_W0_FMT;
995
996    if (state->scale[0] != 1.0f) {
997        viewport->xscale = state->scale[0];
998        viewport->vte_control |= R300_VPORT_X_SCALE_ENA;
999    }
1000    if (state->scale[1] != 1.0f) {
1001        viewport->yscale = state->scale[1];
1002        viewport->vte_control |= R300_VPORT_Y_SCALE_ENA;
1003    }
1004    if (state->scale[2] != 1.0f) {
1005        viewport->zscale = state->scale[2];
1006        viewport->vte_control |= R300_VPORT_Z_SCALE_ENA;
1007    }
1008    if (state->translate[0] != 0.0f) {
1009        viewport->xoffset = state->translate[0];
1010        viewport->vte_control |= R300_VPORT_X_OFFSET_ENA;
1011    }
1012    if (state->translate[1] != 0.0f) {
1013        viewport->yoffset = state->translate[1];
1014        viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA;
1015    }
1016    if (state->translate[2] != 0.0f) {
1017        viewport->zoffset = state->translate[2];
1018        viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA;
1019    }
1020
1021    r300->viewport_state.dirty = TRUE;
1022    if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) {
1023        r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
1024    }
1025}
1026
1027static void r300_set_vertex_buffers(struct pipe_context* pipe,
1028                                    unsigned count,
1029                                    const struct pipe_vertex_buffer* buffers)
1030{
1031    struct r300_context* r300 = r300_context(pipe);
1032    boolean any_user_buffer = false;
1033    int i;
1034
1035    if (count == r300->vertex_buffer_count &&
1036	memcmp(r300->vertex_buffer, buffers, count * sizeof(buffers[0])) == 0)
1037        return;
1038
1039    for (i = 0; i < count; i++) {
1040	pipe_buffer_reference(&r300->vertex_buffer[i].buffer, buffers[i].buffer);
1041	if (r300_buffer_is_user_buffer(buffers[i].buffer))
1042	    any_user_buffer = true;
1043    }
1044
1045    for ( ; i < r300->vertex_buffer_count; i++)
1046	pipe_buffer_reference(&r300->vertex_buffer[i].buffer, NULL);
1047
1048    memcpy(r300->vertex_buffer, buffers,
1049        sizeof(struct pipe_vertex_buffer) * count);
1050    r300->vertex_buffer_count = count;
1051    r300->any_user_vbs = any_user_buffer;
1052
1053    if (r300->draw) {
1054        draw_flush(r300->draw);
1055        draw_set_vertex_buffers(r300->draw, count, buffers);
1056    }
1057
1058    r300->vertex_format_state.dirty = TRUE;
1059}
1060
1061static boolean r300_validate_aos(struct r300_context *r300)
1062{
1063    struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
1064    struct pipe_vertex_element *velem = r300->vertex_element;
1065    int i;
1066
1067    /* Check if formats and strides are aligned to the size of DWORD. */
1068    for (i = 0; i < r300->vertex_element_count; i++) {
1069        if (vbuf[velem[i].vertex_buffer_index].stride % 4 != 0 ||
1070            util_format_get_blocksize(velem[i].src_format) % 4 != 0) {
1071            return FALSE;
1072        }
1073    }
1074    return TRUE;
1075}
1076
1077static void r300_set_vertex_elements(struct pipe_context* pipe,
1078                                    unsigned count,
1079                                    const struct pipe_vertex_element* elements)
1080{
1081    struct r300_context* r300 = r300_context(pipe);
1082
1083    memcpy(r300->vertex_element,
1084           elements,
1085           sizeof(struct pipe_vertex_element) * count);
1086    r300->vertex_element_count = count;
1087
1088    if (r300->draw) {
1089        draw_flush(r300->draw);
1090        draw_set_vertex_elements(r300->draw, count, elements);
1091    }
1092
1093    if (!r300_validate_aos(r300)) {
1094        /* XXX We should fallback using draw. */
1095        assert(0);
1096        abort();
1097    }
1098}
1099
1100static void* r300_create_vs_state(struct pipe_context* pipe,
1101                                  const struct pipe_shader_state* shader)
1102{
1103    struct r300_context* r300 = r300_context(pipe);
1104
1105    if (r300_screen(pipe->screen)->caps->has_tcl) {
1106        struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
1107        /* Copy state directly into shader. */
1108        vs->state = *shader;
1109        vs->state.tokens = tgsi_dup_tokens(shader->tokens);
1110
1111        tgsi_scan_shader(shader->tokens, &vs->info);
1112
1113        return (void*)vs;
1114    } else {
1115        return draw_create_vertex_shader(r300->draw, shader);
1116    }
1117}
1118
1119static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
1120{
1121    struct r300_context* r300 = r300_context(pipe);
1122
1123    if (r300_screen(pipe->screen)->caps->has_tcl) {
1124        struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1125
1126        if (vs == NULL) {
1127            r300->vs = NULL;
1128            return;
1129        } else if (!vs->translated) {
1130            r300_translate_vertex_shader(r300, vs);
1131        }
1132
1133        r300->vs = vs;
1134        if (r300->fs) {
1135            r300_vertex_shader_setup_wpos(r300);
1136        }
1137
1138        r300->vertex_format_state.dirty = TRUE;
1139
1140        r300->dirty_state |=
1141            R300_NEW_VERTEX_SHADER | R300_NEW_VERTEX_SHADER_CONSTANTS;
1142    } else {
1143        draw_flush(r300->draw);
1144        draw_bind_vertex_shader(r300->draw,
1145                (struct draw_vertex_shader*)shader);
1146    }
1147}
1148
1149static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
1150{
1151    struct r300_context* r300 = r300_context(pipe);
1152
1153    if (r300_screen(pipe->screen)->caps->has_tcl) {
1154        struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1155
1156        rc_constants_destroy(&vs->code.constants);
1157        FREE((void*)vs->state.tokens);
1158        FREE(shader);
1159    } else {
1160        draw_delete_vertex_shader(r300->draw,
1161                (struct draw_vertex_shader*)shader);
1162    }
1163}
1164
1165static void r300_set_constant_buffer(struct pipe_context *pipe,
1166                                     uint shader, uint index,
1167                                     struct pipe_buffer *buf)
1168{
1169    struct r300_context* r300 = r300_context(pipe);
1170    struct r300_screen *r300screen = r300_screen(pipe->screen);
1171    void *mapped;
1172    int max_size = 0;
1173
1174    if (buf == NULL || buf->size == 0 ||
1175        (mapped = pipe_buffer_map(pipe->screen, buf, PIPE_BUFFER_USAGE_CPU_READ)) == NULL)
1176    {
1177        r300->shader_constants[shader].count = 0;
1178        return;
1179    }
1180
1181    assert((buf->size % 4 * sizeof(float)) == 0);
1182
1183    /* Check the size of the constant buffer. */
1184    switch (shader) {
1185        case PIPE_SHADER_VERTEX:
1186            max_size = 256;
1187            break;
1188        case PIPE_SHADER_FRAGMENT:
1189            if (r300screen->caps->is_r500) {
1190                max_size = 256;
1191            /* XXX Implement emission of r400's extended constant buffer. */
1192            /*} else if (r300screen->caps->is_r400) {
1193                max_size = 64;*/
1194            } else {
1195                max_size = 32;
1196            }
1197            break;
1198        default:
1199            assert(0);
1200    }
1201
1202    /* XXX Subtract immediates and RC_STATE_* variables. */
1203    if (buf->size > (sizeof(float) * 4 * max_size)) {
1204        debug_printf("r300: Max size of the constant buffer is "
1205                      "%i*4 floats.\n", max_size);
1206        abort();
1207    }
1208
1209    memcpy(r300->shader_constants[shader].constants, mapped, buf->size);
1210    r300->shader_constants[shader].count = buf->size / (4 * sizeof(float));
1211    pipe_buffer_unmap(pipe->screen, buf);
1212
1213    if (shader == PIPE_SHADER_VERTEX)
1214        r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS;
1215    else if (shader == PIPE_SHADER_FRAGMENT)
1216        r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
1217}
1218
1219void r300_init_state_functions(struct r300_context* r300)
1220{
1221    r300->context.create_blend_state = r300_create_blend_state;
1222    r300->context.bind_blend_state = r300_bind_blend_state;
1223    r300->context.delete_blend_state = r300_delete_blend_state;
1224
1225    r300->context.set_blend_color = r300_set_blend_color;
1226
1227    r300->context.set_clip_state = r300_set_clip_state;
1228
1229    r300->context.set_constant_buffer = r300_set_constant_buffer;
1230
1231    r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
1232    r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
1233    r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
1234
1235    r300->context.set_stencil_ref = r300_set_stencil_ref;
1236
1237    r300->context.set_framebuffer_state = r300_set_framebuffer_state;
1238
1239    r300->context.create_fs_state = r300_create_fs_state;
1240    r300->context.bind_fs_state = r300_bind_fs_state;
1241    r300->context.delete_fs_state = r300_delete_fs_state;
1242
1243    r300->context.set_polygon_stipple = r300_set_polygon_stipple;
1244
1245    r300->context.create_rasterizer_state = r300_create_rs_state;
1246    r300->context.bind_rasterizer_state = r300_bind_rs_state;
1247    r300->context.delete_rasterizer_state = r300_delete_rs_state;
1248
1249    r300->context.create_sampler_state = r300_create_sampler_state;
1250    r300->context.bind_fragment_sampler_states = r300_bind_sampler_states;
1251    r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures;
1252    r300->context.delete_sampler_state = r300_delete_sampler_state;
1253
1254    r300->context.set_fragment_sampler_textures = r300_set_sampler_textures;
1255
1256    r300->context.set_scissor_state = r300_set_scissor_state;
1257
1258    r300->context.set_viewport_state = r300_set_viewport_state;
1259
1260    r300->context.set_vertex_buffers = r300_set_vertex_buffers;
1261    r300->context.set_vertex_elements = r300_set_vertex_elements;
1262
1263    r300->context.create_vs_state = r300_create_vs_state;
1264    r300->context.bind_vs_state = r300_bind_vs_state;
1265    r300->context.delete_vs_state = r300_delete_vs_state;
1266}
1267