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