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