r300_state.c revision 9f10b16790d7e4e224fc30cf105df944275d6353
1fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko/*
2fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko *
4fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * Permission is hereby granted, free of charge, to any person obtaining a
5fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * copy of this software and associated documentation files (the "Software"),
6fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * to deal in the Software without restriction, including without limitation
7fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * on the rights to use, copy, modify, merge, publish, distribute, sub
8fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * license, and/or sell copies of the Software, and to permit persons to whom
9fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * the Software is furnished to do so, subject to the following conditions:
10fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko *
11fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * The above copyright notice and this permission notice (including the next
12fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * paragraph) shall be included in all copies or substantial portions of the
13fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * Software.
14fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko *
15fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19851df20225593b10e698a760ac3cd5243620700bAndreas Gampe * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20851df20225593b10e698a760ac3cd5243620700bAndreas Gampe * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21851df20225593b10e698a760ac3cd5243620700bAndreas Gampe * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
23928f72bd75c385ba2708c58521171a77264d4486Andreas Gampe#include "util/u_math.h"
245a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe#include "util/u_pack_color.h"
25851df20225593b10e698a760ac3cd5243620700bAndreas Gampe#include "pipe/p_debug.h"
26fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko
27fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko#include "r300_context.h"
28fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko#include "r300_reg.h"
29fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko
30fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko/* r300_state: Functions used to intialize state context by translating
31fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * Gallium state objects into semi-native r300 state objects.
32fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko *
33fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko * XXX break this file up into pieces if it gets too big! */
34fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko
35fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko/* Pack a float into a dword. */
36fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenkostatic uint32_t pack_float_32(float f)
37fca82208f7128fcda09b6a4743199308332558a2Dmitry Petrochenko{
38851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    union {
39851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        float f;
40851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        uint32_t u;
41851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } u;
42851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
43851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    u.f = f;
44851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return u.u;
45851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
46851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
47ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampestatic uint32_t translate_blend_function(int blend_func) {
48851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    switch (blend_func) {
49851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLEND_ADD:
50ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            return R300_COMB_FCN_ADD_CLAMP;
51851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLEND_SUBTRACT:
52851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_COMB_FCN_SUB_CLAMP;
53851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLEND_REVERSE_SUBTRACT:
54851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_COMB_FCN_RSUB_CLAMP;
55851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLEND_MIN:
56851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_COMB_FCN_MIN;
57851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLEND_MAX:
58ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            return R300_COMB_FCN_MAX;
59851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        default:
60851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            debug_printf("r300: Unknown blend function %d\n", blend_func);
61851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            break;
62851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
63ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe    return 0;
64851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
65851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
66851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* XXX we can also offer the D3D versions of some of these... */
67ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampestatic uint32_t translate_blend_factor(int blend_fact) {
68851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    switch (blend_fact) {
69851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_ONE:
70851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_ONE;
71851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_SRC_COLOR:
72851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_SRC_COLOR;
73851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_SRC_ALPHA:
74ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            return R300_BLEND_GL_SRC_ALPHA;
75851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_DST_ALPHA:
76851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_DST_ALPHA;
77851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_DST_COLOR:
78851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_DST_COLOR;
79ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe        case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
80851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_SRC_ALPHA_SATURATE;
81851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_CONST_COLOR:
82ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            return R300_BLEND_GL_CONST_COLOR;
83851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_CONST_ALPHA:
84851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_CONST_ALPHA;
85851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        /* XXX WTF are these?
86851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_SRC1_COLOR:
87851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_SRC1_ALPHA: */
88ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe        case PIPE_BLENDFACTOR_ZERO:
89851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_ZERO;
90851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_SRC_COLOR:
91851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
92851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
93ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
94851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_DST_ALPHA:
95851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
96851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_DST_COLOR:
97ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
98851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_CONST_COLOR:
99851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
100851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
101851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
102851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        /* XXX see above
103851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
104ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe        case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
105851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        default:
106ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            debug_printf("r300: Unknown blend factor %d\n", blend_fact);
107ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe            break;
108ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe    }
109ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe    return 0;
110ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe}
111ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe
112ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe/* Create a new blend state based on the CSO blend state.
113ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe *
114ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe * This encompasses alpha blending, logic/raster ops, and blend dithering. */
115ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampestatic void* r300_create_blend_state(struct pipe_context* pipe,
116ab1eb0d1d047e3478ebb891e5259d2f1d1dd78bdAndreas Gampe                                     const struct pipe_blend_state* state)
117851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
118851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
119851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
120851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (state->blend_enable) {
121851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        /* XXX for now, always do separate alpha...
122851df20225593b10e698a760ac3cd5243620700bAndreas Gampe         * is it faster to do it with one reg? */
123851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        blend->blend_control = R300_ALPHA_BLEND_ENABLE |
124851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                R300_SEPARATE_ALPHA_ENABLE |
1255a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                R300_READ_ENABLE |
126851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                translate_blend_function(state->rgb_func) |
127851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (translate_blend_factor(state->rgb_src_factor) <<
128851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    R300_SRC_BLEND_SHIFT) |
129851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (translate_blend_factor(state->rgb_dst_factor) <<
130851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    R300_DST_BLEND_SHIFT);
1315a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        blend->alpha_blend_control =
1325a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                translate_blend_function(state->alpha_func) |
1335a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                (translate_blend_factor(state->alpha_src_factor) <<
1345a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                    R300_SRC_BLEND_SHIFT) |
1355a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                (translate_blend_factor(state->alpha_dst_factor) <<
1365a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                    R300_DST_BLEND_SHIFT);
1375a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    }
1385a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
1395a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
1405a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    /* XXX are logicops still allowed if blending's disabled?
1415a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe     * Does Gallium take care of it for us? */
1425a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    if (state->logicop_enable) {
1435a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
1445a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
1455a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    }
1465a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
1475a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    if (state->dither) {
1485a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
1495a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
1505a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    }
1515a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
1525a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    return (void*)blend;
1535a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe}
1545a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
1555a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe/* Bind blend state. */
1565a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampestatic void r300_bind_blend_state(struct pipe_context* pipe,
1575a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                                  void* state)
1585a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe{
159851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
160851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
161851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->blend_state = (struct r300_blend_state*)state;
162851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->dirty_state |= R300_NEW_BLEND;
163851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
164851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
165851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Free blend state. */
166851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_delete_blend_state(struct pipe_context* pipe,
167851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                    void* state)
168851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
169851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    FREE(state);
170851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
171851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
172851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Set blend color.
173851df20225593b10e698a760ac3cd5243620700bAndreas Gampe * Setup both R300 and R500 registers, figure out later which one to write. */
174851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_set_blend_color(struct pipe_context* pipe,
175851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                 const struct pipe_blend_color* color)
176851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
177851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
178851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    uint32_t r, g, b, a;
179851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    ubyte ur, ug, ub, ua;
180851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
181851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r = util_iround(color->color[0] * 1023.0f);
182851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    g = util_iround(color->color[1] * 1023.0f);
183851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    b = util_iround(color->color[2] * 1023.0f);
184851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    a = util_iround(color->color[3] * 1023.0f);
185851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
186851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    ur = float_to_ubyte(color->color[0]);
187851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    ug = float_to_ubyte(color->color[1]);
188851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    ub = float_to_ubyte(color->color[2]);
189851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    ua = float_to_ubyte(color->color[3]);
190851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
191851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->blend_color_state->blend_color = (a << 24) | (r << 16) | (g << 8) | b;
192851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
1935a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->blend_color_state->blend_color_red_alpha = ur | (ua << 16);
1945a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->blend_color_state->blend_color_green_blue = ub | (ug << 16);
1955a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
196928f72bd75c385ba2708c58521171a77264d4486Andreas Gampe    r300->dirty_state |= R300_NEW_BLEND_COLOR;
197928f72bd75c385ba2708c58521171a77264d4486Andreas Gampe}
198928f72bd75c385ba2708c58521171a77264d4486Andreas Gampe
199851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_set_clip_state(struct pipe_context* pipe,
200928f72bd75c385ba2708c58521171a77264d4486Andreas Gampe                                const struct pipe_clip_state* state)
201928f72bd75c385ba2708c58521171a77264d4486Andreas Gampe{
2025a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
2035a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    /* XXX Draw */
2045a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    draw_flush(r300->draw);
2055a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    draw_set_clip_state(r300->draw, state);
206851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
207851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
208851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void
209851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300_set_constant_buffer(struct pipe_context* pipe,
210cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers                             uint shader, uint index,
211cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers                             const struct pipe_constant_buffer* buffer)
2125a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe{
2135a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    /* XXX */
214851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
215851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
216851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic uint32_t translate_depth_stencil_function(int zs_func) {
217851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    switch (zs_func) {
218851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_NEVER:
2195a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_NEVER;
2205a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_LESS:
221851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_ZS_LESS;
222851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_EQUAL:
223851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_ZS_EQUAL;
2245a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_LEQUAL:
2255a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_LEQUAL;
2265a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_GREATER:
2275a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_GREATER;
2285a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_NOTEQUAL:
2295a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_NOTEQUAL;
2305a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_GEQUAL:
2315a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_GEQUAL;
2325a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_ALWAYS:
2335a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_ALWAYS;
2345a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        default:
2355a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            debug_printf("r300: Unknown depth/stencil function %d\n",
2365a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                zs_func);
2375a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            break;
2385a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    }
2395a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    return 0;
2405a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe}
2415a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
2425a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampestatic uint32_t translate_stencil_op(int s_op) {
2435a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    switch (s_op) {
2445a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_STENCIL_OP_KEEP:
2455a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_KEEP;
2465a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_STENCIL_OP_ZERO:
2475a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_ZERO;
248851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_STENCIL_OP_REPLACE:
249851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_ZS_REPLACE;
250851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_STENCIL_OP_INCR:
251851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_ZS_INCR;
252851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_STENCIL_OP_DECR:
253851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_ZS_DECR;
254851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_STENCIL_OP_INCR_WRAP:
2555a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_INCR_WRAP;
2565a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_STENCIL_OP_DECR_WRAP:
2575a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_DECR_WRAP;
2585a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_STENCIL_OP_INVERT:
2595a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_ZS_INVERT;
2605a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        default:
2615a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            debug_printf("r300: Unknown stencil op %d", s_op);
2625a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            break;
2635a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    }
264851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return 0;
265851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
266851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
267851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic uint32_t translate_alpha_function(int alpha_func) {
268851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    switch (alpha_func) {
269851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_NEVER:
270851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_FG_ALPHA_FUNC_NEVER;
271851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_LESS:
27234bacdf7eb46c0ffbf24ba7aa14a904bc9176fb2Calin Juravle            return R300_FG_ALPHA_FUNC_LESS;
27334bacdf7eb46c0ffbf24ba7aa14a904bc9176fb2Calin Juravle        case PIPE_FUNC_EQUAL:
27434bacdf7eb46c0ffbf24ba7aa14a904bc9176fb2Calin Juravle            return R300_FG_ALPHA_FUNC_EQUAL;
2755a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_FUNC_LEQUAL:
276851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_FG_ALPHA_FUNC_LE;
277851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_GREATER:
278851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_FG_ALPHA_FUNC_GREATER;
279851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_NOTEQUAL:
280851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_FG_ALPHA_FUNC_NOTEQUAL;
281851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_GEQUAL:
282851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_FG_ALPHA_FUNC_GE;
283851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_FUNC_ALWAYS:
284851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_FG_ALPHA_FUNC_ALWAYS;
285851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        default:
286851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            debug_printf("r300: Unknown alpha function %d", alpha_func);
287851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            break;
288851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
289851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return 0;
290851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
291851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
292851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Create a new depth, stencil, and alpha state based on the CSO dsa state.
293851df20225593b10e698a760ac3cd5243620700bAndreas Gampe *
2945a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe * This contains the depth buffer, stencil buffer, alpha test, and such.
2955a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe * On the Radeon, depth and stencil buffer setup are intertwined, which is
2965a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe * the reason for some of the strange-looking assignments across registers. */
2975a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampestatic void*
2985a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        r300_create_dsa_state(struct pipe_context* pipe,
2995a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe                              const struct pipe_depth_stencil_alpha_state* state)
3005a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe{
3015a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
302851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
303851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* Depth test setup. */
304851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (state->depth.enabled) {
305851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        dsa->z_buffer_control |= R300_Z_ENABLE;
306851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
307851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (state->depth.writemask) {
308851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
309851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
3109aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
311851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        dsa->z_stencil_control |=
312851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            (translate_depth_stencil_function(state->depth.func) <<
313851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                R300_Z_FUNC_SHIFT);
314851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
315851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
316851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* Stencil buffer setup. */
317851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (state->stencil[0].enabled) {
318851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        dsa->z_buffer_control |= R300_STENCIL_ENABLE;
319851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        dsa->z_stencil_control |=
320851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (translate_depth_stencil_function(state->stencil[0].func) <<
321851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    R300_S_FRONT_FUNC_SHIFT) |
322851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (translate_stencil_op(state->stencil[0].fail_op) <<
323851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    R300_S_FRONT_SFAIL_OP_SHIFT) |
324851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (translate_stencil_op(state->stencil[0].zpass_op) <<
325851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    R300_S_FRONT_ZPASS_OP_SHIFT) |
326851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (translate_stencil_op(state->stencil[0].zfail_op) <<
327851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    R300_S_FRONT_ZFAIL_OP_SHIFT);
328851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
329851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        dsa->stencil_ref_mask = (state->stencil[0].ref_value) |
330851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
331851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
332851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
3339aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle        if (state->stencil[1].enabled) {
3349aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle            dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
3359aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle            dsa->z_stencil_control |=
3369aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                (translate_depth_stencil_function(state->stencil[1].func) <<
3379aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                    R300_S_BACK_FUNC_SHIFT) |
3389aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                (translate_stencil_op(state->stencil[1].fail_op) <<
3399aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                    R300_S_BACK_SFAIL_OP_SHIFT) |
3409aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                (translate_stencil_op(state->stencil[1].zpass_op) <<
3419aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                    R300_S_BACK_ZPASS_OP_SHIFT) |
3429aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                (translate_stencil_op(state->stencil[1].zfail_op) <<
3439aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                    R300_S_BACK_ZFAIL_OP_SHIFT);
3449aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3459aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle            dsa->stencil_ref_bf = (state->stencil[1].ref_value) |
3469aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                (state->stencil[1].valuemask << R300_STENCILMASK_SHIFT) |
3479aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                (state->stencil[1].writemask << R300_STENCILWRITEMASK_SHIFT);
3489aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle        }
3499aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    }
3509aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3519aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    /* Alpha test setup. */
3529aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    if (state->alpha.enabled) {
3539aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle        dsa->alpha_function = translate_alpha_function(state->alpha.func) |
3549aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle            R300_FG_ALPHA_FUNC_ENABLE;
3559aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle        dsa->alpha_reference = CLAMP(state->alpha.ref_value * 1023.0f,
3569aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                                     0, 1023);
357851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } else {
358851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        dsa->z_buffer_top = R300_ZTOP_ENABLE;
359851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
360851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
361851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return (void*)dsa;
362851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
363851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
364851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Bind DSA state. */
365851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_bind_dsa_state(struct pipe_context* pipe,
366851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                void* state)
367851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
368851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
369851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
370851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->dsa_state = (struct r300_dsa_state*)state;
371851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->dirty_state |= R300_NEW_DSA;
372851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
373851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
374851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Free DSA state. */
375851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_delete_dsa_state(struct pipe_context* pipe,
376851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                  void* state)
377851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
378851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    FREE(state);
3799aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle}
3809aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3819aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravlestatic void r300_set_edgeflags(struct pipe_context* pipe,
3829aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                               const unsigned* bitfield)
3839aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle{
3849aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    /* XXX you know it's bad when i915 has this blank too */
3859aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle}
3869aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3879aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravlestatic void
3889aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    r300_set_framebuffer_state(struct pipe_context* pipe,
3899aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                               const struct pipe_framebuffer_state* state)
3909aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle{
3919aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    struct r300_context* r300 = r300_context(pipe);
3929aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3939aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    draw_flush(r300->draw);
3949aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3959aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    r300->framebuffer_state = *state;
3969aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
3979aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    r300->dirty_state |= R300_NEW_FRAMEBUFFERS;
3989aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle}
3999aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
4009aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle/* Create fragment shader state. */
4019aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravlestatic void* r300_create_fs_state(struct pipe_context* pipe,
4029aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                                  const struct pipe_shader_state* state)
403851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
404851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_fs_state* fs = CALLOC_STRUCT(r300_fs_state);
405851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
406851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return (void*)fs;
407851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
408851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
409851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Bind fragment shader state. */
410851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_bind_fs_state(struct pipe_context* pipe, void* state)
411851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
412851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
413851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
414851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->fs_state = (struct r300_fs_state*)state;
415851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
416851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->dirty_state |= R300_NEW_FRAGMENT_SHADER;
417851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
418851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
419851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Delect fragment shader state. */
420851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_delete_fs_state(struct pipe_context* pipe, void* state)
421851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
422851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    FREE(state);
423851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
4245a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
4259aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravlestatic void r300_set_polygon_stipple(struct pipe_context* pipe,
4269aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                                     const struct pipe_poly_stipple* state)
4279aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle{
4289aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    /* XXX */
4299aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle}
4309aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
4319aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravlestatic INLINE int pack_float_16_6x(float f) {
4329aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    return ((int)(f * 6.0) & 0xffff);
4339aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle}
4349aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
4359aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle/* Create a new rasterizer state based on the CSO rasterizer state.
4369aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle *
4379aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle * This is a very large chunk of state, and covers most of the graphics
4389aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
4399aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle *
4409aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle * In a not entirely unironic sidenote, this state has nearly nothing to do
4419aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle * with the actual block on the Radeon called the rasterizer (RS). */
4429aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravlestatic void* r300_create_rs_state(struct pipe_context* pipe,
4439aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle                                  const struct pipe_rasterizer_state* state)
4449aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle{
4459aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
4469aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle
4479aec02fc5df5518c16f1e5a9b6cb198a192db973Calin Juravle    /* XXX this is part of HW TCL */
4485a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    /* XXX endian control */
4495a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    rs->vap_control_status = R300_VAP_TCL_BYPASS;
4505a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
4515a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    rs->point_size = pack_float_16_6x(state->point_size) |
452851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
453851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
454851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    rs->line_control = pack_float_16_6x(state->line_width) |
455851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        R300_GA_LINE_CNTL_END_TYPE_COMP;
456851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
457851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* Radeons don't think in "CW/CCW", they think in "front/back". */
458851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (state->front_winding == PIPE_WINDING_CW) {
459851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->cull_mode = R300_FRONT_FACE_CW;
460851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
461851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (state->offset_cw) {
462851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
463851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
464851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (state->offset_ccw) {
465851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            rs->polygon_offset_enable |= R300_BACK_ENABLE;
466851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
467851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } else {
468851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->cull_mode = R300_FRONT_FACE_CCW;
469851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
470851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (state->offset_ccw) {
471851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
472851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
473851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (state->offset_cw) {
474851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            rs->polygon_offset_enable |= R300_BACK_ENABLE;
475851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
476851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
477851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (state->front_winding & state->cull_mode) {
478851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->cull_mode |= R300_CULL_FRONT;
479851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
480851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (~(state->front_winding) & state->cull_mode) {
481851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->cull_mode |= R300_CULL_BACK;
482851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
483851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
484851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (rs->polygon_offset_enable) {
485851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->depth_offset_front = rs->depth_offset_back =
486851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                pack_float_32(state->offset_units);
487851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->depth_scale_front = rs->depth_scale_back =
488851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                pack_float_32(state->offset_scale);
489851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
490851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
491851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (state->line_stipple_enable) {
492851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->line_stipple_config =
493851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
494851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            (pack_float_32((float)state->line_stipple_factor) &
495851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
496851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        /* XXX this might need to be scaled up */
497851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        rs->line_stipple_value = state->line_stipple_pattern;
498851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
499851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
500851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return (void*)rs;
501851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
502851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
503851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Bind rasterizer state. */
504851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_bind_rs_state(struct pipe_context* pipe, void* state)
505851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
506851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
507851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
508851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->rs_state = (struct r300_rs_state*)state;
509851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->dirty_state |= R300_NEW_RASTERIZER;
510851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
511851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
512851df20225593b10e698a760ac3cd5243620700bAndreas Gampe/* Free rasterizer state. */
513851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_delete_rs_state(struct pipe_context* pipe, void* state)
514851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
515851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    FREE(state);
516851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
517851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
518851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic uint32_t translate_wrap(int wrap) {
5195a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    switch (wrap) {
5205a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_TEX_WRAP_REPEAT:
5215a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_TX_REPEAT;
5225a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe        case PIPE_TEX_WRAP_CLAMP:
5235a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe            return R300_TX_CLAMP;
524851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
525851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_TX_CLAMP_TO_EDGE;
526851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
527851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_TX_CLAMP_TO_BORDER;
528851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_WRAP_MIRROR_REPEAT:
529851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_TX_REPEAT | R300_TX_MIRRORED;
530851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_WRAP_MIRROR_CLAMP:
531851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_TX_CLAMP | R300_TX_MIRRORED;
532851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
533851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
534851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
535851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
536851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        default:
537851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            debug_printf("r300: Unknown texture wrap %d", wrap);
538851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            return 0;
539851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
540102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray}
541102cbed1e52b7c5f09458b44903fe97bb3e14d5fNicolas Geoffray
542ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffraystatic uint32_t translate_tex_filters(int min, int mag, int mip) {
54330687af6830f1d09aa510d864557528038b33284Nicolas Geoffray    uint32_t retval = 0;
54430687af6830f1d09aa510d864557528038b33284Nicolas Geoffray    switch (min) {
54530687af6830f1d09aa510d864557528038b33284Nicolas Geoffray        case PIPE_TEX_FILTER_NEAREST:
54630687af6830f1d09aa510d864557528038b33284Nicolas Geoffray            retval |= R300_TX_MIN_FILTER_NEAREST;
54730687af6830f1d09aa510d864557528038b33284Nicolas Geoffray        case PIPE_TEX_FILTER_LINEAR:
54830687af6830f1d09aa510d864557528038b33284Nicolas Geoffray            retval |= R300_TX_MIN_FILTER_LINEAR;
549784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray        case PIPE_TEX_FILTER_ANISO:
550784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray            retval |= R300_TX_MIN_FILTER_ANISO;
551784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray        default:
552784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray            debug_printf("r300: Unknown texture filter %d", min);
553ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray            break;
55430687af6830f1d09aa510d864557528038b33284Nicolas Geoffray    }
55530687af6830f1d09aa510d864557528038b33284Nicolas Geoffray    switch (mag) {
556784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray        case PIPE_TEX_FILTER_NEAREST:
557784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray            retval |= R300_TX_MAG_FILTER_NEAREST;
558784cc5c37f382838f89e281758040c6620ccfd01Nicolas Geoffray        case PIPE_TEX_FILTER_LINEAR:
559ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray            retval |= R300_TX_MAG_FILTER_LINEAR;
560ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray        case PIPE_TEX_FILTER_ANISO:
561ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray            retval |= R300_TX_MAG_FILTER_ANISO;
562ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741Nicolas Geoffray        default:
563e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray            debug_printf("r300: Unknown texture filter %d", mag);
564e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray            break;
565e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray    }
566e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray    switch (mip) {
567e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray        case PIPE_TEX_MIPFILTER_NONE:
568e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray            retval |= R300_TX_MIN_FILTER_MIP_NONE;
569e4ded41ae2648973d5fed8c6bafaebf917ea7d17Nicolas Geoffray        case PIPE_TEX_MIPFILTER_NEAREST:
570851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            retval |= R300_TX_MIN_FILTER_MIP_NEAREST;
571851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        case PIPE_TEX_MIPFILTER_LINEAR:
572851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            retval |= R300_TX_MIN_FILTER_MIP_LINEAR;
573851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        default:
574851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            debug_printf("r300: Unknown texture filter %d", mip);
575851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            break;
576851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
577851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
578851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return retval;
579851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
580851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
581851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic uint32_t anisotropy(float max_aniso) {
582851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (max_aniso >= 16.0f) {
583851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return R300_TX_MAX_ANISO_16_TO_1;
584851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } else if (max_aniso >= 8.0f) {
585851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return R300_TX_MAX_ANISO_8_TO_1;
586851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } else if (max_aniso >= 4.0f) {
587851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return R300_TX_MAX_ANISO_4_TO_1;
588851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } else if (max_aniso >= 2.0f) {
589851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return R300_TX_MAX_ANISO_2_TO_1;
590851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    } else {
591851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return R300_TX_MAX_ANISO_1_TO_1;
592851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
593851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
594851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
595851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void*
596851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        r300_create_sampler_state(struct pipe_context* pipe,
597851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                  const struct pipe_sampler_state* state)
598851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
599851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
600851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
601851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    int lod_bias;
602851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
603851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    sampler->filter0 |=
604851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        (translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
605851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        (translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
606851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        (translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
607851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
608851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    sampler->filter0 |= translate_tex_filters(state->min_img_filter,
609851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                              state->mag_img_filter,
610851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                              state->min_mip_filter);
611851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
612851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
613851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
614851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
615851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
616851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    sampler->filter1 |= anisotropy(state->max_anisotropy);
617851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
618851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM,
619851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                    &sampler->border_color);
620851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
621851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* R500-specific fixups and optimizations */
622851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (r300_screen(r300->context.screen)->caps->is_r500) {
623851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        sampler->filter1 |= R500_BORDER_FIX;
624851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
625851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
626851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return (void*)sampler;
627851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
628851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
629851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_bind_sampler_states(struct pipe_context* pipe,
630851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                     unsigned count,
631851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                     void** states)
632851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
633851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
634851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    int i;
635851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
636851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (count > 8) {
637851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return;
638851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
639851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
640851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    for (i = 0; i < count; i++) {
641851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (r300->sampler_states[i] != states[i]) {
642851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
643851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            r300->dirty_state |= (R300_NEW_SAMPLER << i);
644851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
645851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
646851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
647851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->sampler_count = count;
648851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
649851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
650851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
651851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
652851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    FREE(state);
653851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
654851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
655851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_set_sampler_textures(struct pipe_context* pipe,
656851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                      unsigned count,
657851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                      struct pipe_texture** texture)
658851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
659851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
660851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    int i;
661851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
662851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* XXX magic num */
663851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    if (count > 8) {
664851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        return;
665851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
666851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
667851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    for (i = 0; i < count; i++) {
668851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (r300->textures[i] != (struct r300_texture*)texture[i]) {
669851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
670851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                texture[i]);
671851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            r300->dirty_state |= (R300_NEW_TEXTURE << i);
672851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        }
673851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    }
674851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
675851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    for (i = count; i < 8; i++) {
676851df20225593b10e698a760ac3cd5243620700bAndreas Gampe        if (r300->textures[i]) {
677851df20225593b10e698a760ac3cd5243620700bAndreas Gampe            pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
678851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                NULL);
679ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle            r300->dirty_state |= (R300_NEW_TEXTURE << i);
680ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle        }
681ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle    }
682ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle
683ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle    r300->texture_count = count;
684ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle}
685ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravle
686ddb7df25af45d7cd19ed1138e537973735cc78a5Calin Juravlestatic void r300_set_scissor_state(struct pipe_context* pipe,
687851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                   const struct pipe_scissor_state* state)
688851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
689851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
690851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_flush(r300->draw);
691851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
692851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    uint32_t left, top, right, bottom;
693851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
694851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* So, a bit of info. The scissors are offset by R300_SCISSORS_OFFSET in
695851df20225593b10e698a760ac3cd5243620700bAndreas Gampe     * both directions for all values, and can only be 13 bits wide. Why?
696851df20225593b10e698a760ac3cd5243620700bAndreas Gampe     * We may never know. */
697851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    left = (state->minx + R300_SCISSORS_OFFSET) & 0x1fff;
698851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    top = (state->miny + R300_SCISSORS_OFFSET) & 0x1fff;
699851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    right = (state->maxx + R300_SCISSORS_OFFSET) & 0x1fff;
700851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    bottom = (state->maxy + R300_SCISSORS_OFFSET) & 0x1fff;
701851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
702851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->scissor_state->scissor_top_left = (left << R300_SCISSORS_X_SHIFT) |
70371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe            (top << R300_SCISSORS_Y_SHIFT);
70471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->scissor_state->scissor_bottom_right =
70571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe        (right << R300_SCISSORS_X_SHIFT) | (bottom << R300_SCISSORS_Y_SHIFT);
70671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
70771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->dirty_state |= R300_NEW_SCISSOR;
70871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe}
70971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
71071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampestatic void r300_set_viewport_state(struct pipe_context* pipe,
71171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe                                    const struct pipe_viewport_state* state)
71271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe{
71371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    struct r300_context* r300 = r300_context(pipe);
71471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    /* XXX handing this off to Draw for now */
71571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    draw_set_viewport_state(r300->draw, state);
71671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe}
71771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
71871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampestatic void r300_set_vertex_buffers(struct pipe_context* pipe,
719851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                    unsigned count,
720851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                    const struct pipe_vertex_buffer* buffers)
721851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
722851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
723851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* XXX Draw */
724851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_flush(r300->draw);
725851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_set_vertex_buffers(r300->draw, count, buffers);
726851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
727851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
728851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_set_vertex_elements(struct pipe_context* pipe,
729851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                    unsigned count,
730851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                    const struct pipe_vertex_element* elements)
731851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
732851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* r300 = r300_context(pipe);
733851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* XXX Draw */
734851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_flush(r300->draw);
735851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_set_vertex_elements(r300->draw, count, elements);
736851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
737851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
738851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void* r300_create_vs_state(struct pipe_context* pipe,
739851df20225593b10e698a760ac3cd5243620700bAndreas Gampe                                  const struct pipe_shader_state* state)
740851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
741851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* context = r300_context(pipe);
742851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* XXX handing this off to Draw for now */
743851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    return draw_create_vertex_shader(context->draw, state);
744851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
745851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
746851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_bind_vs_state(struct pipe_context* pipe, void* state) {
747851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* context = r300_context(pipe);
748851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* XXX handing this off to Draw for now */
749851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_bind_vertex_shader(context->draw, (struct draw_vertex_shader*)state);
750851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
751851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
752851df20225593b10e698a760ac3cd5243620700bAndreas Gampestatic void r300_delete_vs_state(struct pipe_context* pipe, void* state)
753851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
754851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    struct r300_context* context = r300_context(pipe);
755851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    /* XXX handing this off to Draw for now */
756851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    draw_delete_vertex_shader(context->draw, (struct draw_vertex_shader*)state);
757851df20225593b10e698a760ac3cd5243620700bAndreas Gampe}
758851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
759851df20225593b10e698a760ac3cd5243620700bAndreas Gampevoid r300_init_state_functions(struct r300_context* r300)
760851df20225593b10e698a760ac3cd5243620700bAndreas Gampe{
761851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.create_blend_state = r300_create_blend_state;
762851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.bind_blend_state = r300_bind_blend_state;
763851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.delete_blend_state = r300_delete_blend_state;
764851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
765851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_blend_color = r300_set_blend_color;
766851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
767851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_clip_state = r300_set_clip_state;
768851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
769851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_constant_buffer = r300_set_constant_buffer;
770851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
771851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
772851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
773851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
774851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
775851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_edgeflags = r300_set_edgeflags;
776851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
777851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_framebuffer_state = r300_set_framebuffer_state;
778851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
779851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.create_fs_state = r300_create_fs_state;
780851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.bind_fs_state = r300_bind_fs_state;
781b5de00f1c8f53e6552f1778702673c6274a98bb3Nicolas Geoffray    r300->context.delete_fs_state = r300_delete_fs_state;
782b5de00f1c8f53e6552f1778702673c6274a98bb3Nicolas Geoffray
783851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_polygon_stipple = r300_set_polygon_stipple;
784851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
785851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.create_rasterizer_state = r300_create_rs_state;
7865a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.bind_rasterizer_state = r300_bind_rs_state;
78771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->context.delete_rasterizer_state = r300_delete_rs_state;
78871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
78971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->context.create_sampler_state = r300_create_sampler_state;
79071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->context.bind_sampler_states = r300_bind_sampler_states;
79171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->context.delete_sampler_state = r300_delete_sampler_state;
79271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
79371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    r300->context.set_sampler_textures = r300_set_sampler_textures;
79471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
795851df20225593b10e698a760ac3cd5243620700bAndreas Gampe    r300->context.set_scissor_state = r300_set_scissor_state;
796851df20225593b10e698a760ac3cd5243620700bAndreas Gampe
7975a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.set_viewport_state = r300_set_viewport_state;
7985a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
7995a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.set_vertex_buffers = r300_set_vertex_buffers;
8005a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.set_vertex_elements = r300_set_vertex_elements;
8015a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe
8025a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.create_vs_state = r300_create_vs_state;
8035a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.bind_vs_state = r300_bind_vs_state;
8045a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe    r300->context.delete_vs_state = r300_delete_vs_state;
8055a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe}
8065a4fa82ab42af6e728a60e3261963aa243c3e2cdAndreas Gampe