r300_state.c revision f4041b37e2d305cff0a97eb836250e9f8b1840a8
12880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/*
22880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
32880df2609eba09b555ca37be04b6ad89290c765Tom Hudson *
42880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * Permission is hereby granted, free of charge, to any person obtaining a
52880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * copy of this software and associated documentation files (the "Software"),
62880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * to deal in the Software without restriction, including without limitation
72880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * on the rights to use, copy, modify, merge, publish, distribute, sub
82880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * license, and/or sell copies of the Software, and to permit persons to whom
92880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * the Software is furnished to do so, subject to the following conditions:
102880df2609eba09b555ca37be04b6ad89290c765Tom Hudson *
112880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * The above copyright notice and this permission notice (including the next
122880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * paragraph) shall be included in all copies or substantial portions of the
132880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * Software.
142880df2609eba09b555ca37be04b6ad89290c765Tom Hudson *
152880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
182880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
192880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
202880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
212880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * USE OR OTHER DEALINGS IN THE SOFTWARE. */
222880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
232880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "draw/draw_context.h"
242880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
252880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "util/u_math.h"
262880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "util/u_memory.h"
272880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "util/u_pack_color.h"
282880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
292880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "tgsi/tgsi_parse.h"
302880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
312880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "pipe/p_config.h"
322880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "pipe/internal/p_winsys_screen.h"
332880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
342880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "r300_context.h"
352880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "r300_reg.h"
362880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "r300_screen.h"
372880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "r300_state_inlines.h"
382880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "r300_fs.h"
392880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "r300_vs.h"
402880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
412880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* r300_state: Functions used to intialize state context by translating
422880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * Gallium state objects into semi-native r300 state objects. */
432880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
442880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* Create a new blend state based on the CSO blend state.
452880df2609eba09b555ca37be04b6ad89290c765Tom Hudson *
462880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * This encompasses alpha blending, logic/raster ops, and blend dithering. */
472880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic void* r300_create_blend_state(struct pipe_context* pipe,
482880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                                     const struct pipe_blend_state* state)
492880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
502880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
512880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
522880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->blend_enable)
532880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    {
542880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        unsigned eqRGB = state->rgb_func;
552880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        unsigned srcRGB = state->rgb_src_factor;
562880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        unsigned dstRGB = state->rgb_dst_factor;
572880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
582880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        unsigned eqA = state->alpha_func;
592880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        unsigned srcA = state->alpha_src_factor;
602880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        unsigned dstA = state->alpha_dst_factor;
612880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
622880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
632880df2609eba09b555ca37be04b6ad89290c765Tom Hudson         * this is just the crappy D3D naming */
642880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->blend_control = R300_ALPHA_BLEND_ENABLE |
652880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            r300_translate_blend_function(eqRGB) |
662880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
672880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
682880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
692880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        /* optimization: some operations do not require the destination color */
702880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
712880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
722880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            dstRGB != PIPE_BLENDFACTOR_ZERO ||
732880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            dstA != PIPE_BLENDFACTOR_ZERO ||
742880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
752880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
762880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
772880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
782880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcA == PIPE_BLENDFACTOR_DST_COLOR ||
792880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
802880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
812880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA)
822880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            blend->blend_control |= R300_READ_ENABLE;
832880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
842880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        /* XXX implement the optimization with DISCARD_SRC_PIXELS*/
852880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        /* XXX implement the optimization with SRC_ALPHA_?_NO_READ */
862880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
872880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        /* separate alpha */
882880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
892880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
902880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            blend->alpha_blend_control =
912880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                r300_translate_blend_function(eqA) |
922880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
932880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
942880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        }
952880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
962880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
972880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
982880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->logicop_enable) {
992880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
1002880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
1012880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1022880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1032880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    /* Color Channel Mask */
1042880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->colormask & PIPE_MASK_R) {
1052880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_RED_MASK0;
1062880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1072880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->colormask & PIPE_MASK_G) {
1082880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0;
1092880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1102880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->colormask & PIPE_MASK_B) {
1112880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0;
1122880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1132880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->colormask & PIPE_MASK_A) {
1142880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0;
1152880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1162880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1172880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->dither) {
1182880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
1192880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
1202880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1212880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1222880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    return (void*)blend;
1232880df2609eba09b555ca37be04b6ad89290c765Tom Hudson}
1242880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1252880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* Bind blend state. */
1262880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic void r300_bind_blend_state(struct pipe_context* pipe,
1272880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                                  void* state)
1282880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
1292880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    struct r300_context* r300 = r300_context(pipe);
1302880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1312880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    r300->blend_state = (struct r300_blend_state*)state;
1322880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    r300->dirty_state |= R300_NEW_BLEND;
1332880df2609eba09b555ca37be04b6ad89290c765Tom Hudson}
1342880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1352880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* Free blend state. */
1362880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic void r300_delete_blend_state(struct pipe_context* pipe,
1372880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                                    void* state)
1382880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
1392880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    FREE(state);
1402880df2609eba09b555ca37be04b6ad89290c765Tom Hudson}
1412880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1422880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* Convert float to 10bit integer */
1432880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic unsigned float_to_fixed10(float f)
1442880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
1452880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
1462880df2609eba09b555ca37be04b6ad89290c765Tom Hudson}
1472880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1482880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* Set blend color.
1492880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * Setup both R300 and R500 registers, figure out later which one to write. */
1502880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic void r300_set_blend_color(struct pipe_context* pipe,
1512880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                                 const struct pipe_blend_color* color)
1522880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
1532880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    struct r300_context* r300 = r300_context(pipe);
1542880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1552880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    util_pack_color(color->color, PIPE_FORMAT_A8R8G8B8_UNORM,
1562880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            &r300->blend_color_state->blend_color);
1572880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1582880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    /* XXX if FP16 blending is enabled, we should use the FP16 format */
1592880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    r300->blend_color_state->blend_color_red_alpha =
1602880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        float_to_fixed10(color->color[0]) |
1612880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        (float_to_fixed10(color->color[3]) << 16);
1622880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    r300->blend_color_state->blend_color_green_blue =
1632880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        float_to_fixed10(color->color[2]) |
1642880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        (float_to_fixed10(color->color[1]) << 16);
1652880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1662880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    r300->dirty_state |= R300_NEW_BLEND_COLOR;
1672880df2609eba09b555ca37be04b6ad89290c765Tom Hudson}
1682880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1692880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic void r300_set_clip_state(struct pipe_context* pipe,
1702880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                                const struct pipe_clip_state* state)
1712880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
1722880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    struct r300_context* r300 = r300_context(pipe);
1732880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1742880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (r300_screen(pipe->screen)->caps->has_tcl) {
1752880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        r300->clip_state = *state;
1762880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        r300->dirty_state |= R300_NEW_CLIP;
1772880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    } else {
1782880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        draw_flush(r300->draw);
1792880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        draw_set_clip_state(r300->draw, state);
1802880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
1812880df2609eba09b555ca37be04b6ad89290c765Tom Hudson}
1822880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1832880df2609eba09b555ca37be04b6ad89290c765Tom Hudson/* Create a new depth, stencil, and alpha state based on the CSO dsa state.
1842880df2609eba09b555ca37be04b6ad89290c765Tom Hudson *
1852880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * This contains the depth buffer, stencil buffer, alpha test, and such.
1862880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * On the Radeon, depth and stencil buffer setup are intertwined, which is
1872880df2609eba09b555ca37be04b6ad89290c765Tom Hudson * the reason for some of the strange-looking assignments across registers. */
1882880df2609eba09b555ca37be04b6ad89290c765Tom Hudsonstatic void*
1892880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        r300_create_dsa_state(struct pipe_context* pipe,
1902880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                              const struct pipe_depth_stencil_alpha_state* state)
1912880df2609eba09b555ca37be04b6ad89290c765Tom Hudson{
1922880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    struct r300_capabilities *caps =
1932880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        r300_screen(r300_context(pipe)->context.screen)->caps;
1942880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
1952880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
1962880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    /* Depth test setup. */
1972880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->depth.enabled) {
1982880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        dsa->z_buffer_control |= R300_Z_ENABLE;
1992880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2002880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        if (state->depth.writemask) {
2012880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
2022880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        }
2032880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2042880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        dsa->z_stencil_control |=
2052880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_depth_stencil_function(state->depth.func) <<
2062880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_Z_FUNC_SHIFT);
2072880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
2082880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2092880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    /* Stencil buffer setup. */
2102880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->stencil[0].enabled) {
2112880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        dsa->z_buffer_control |= R300_STENCIL_ENABLE;
2122880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        dsa->z_stencil_control |=
2132880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_depth_stencil_function(state->stencil[0].func) <<
2142880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_FRONT_FUNC_SHIFT) |
2152880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_stencil_op(state->stencil[0].fail_op) <<
2162880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_FRONT_SFAIL_OP_SHIFT) |
2172880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
2182880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_FRONT_ZPASS_OP_SHIFT) |
2192880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
2202880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_FRONT_ZFAIL_OP_SHIFT);
2212880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2222880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        dsa->stencil_ref_mask = (state->stencil[0].ref_value) |
2232880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
2242880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
2252880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2262880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        if (state->stencil[1].enabled) {
2272880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
2282880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            dsa->z_stencil_control |=
2292880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_depth_stencil_function(state->stencil[1].func) <<
2302880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_BACK_FUNC_SHIFT) |
2312880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_stencil_op(state->stencil[1].fail_op) <<
2322880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_BACK_SFAIL_OP_SHIFT) |
2332880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
2342880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_BACK_ZPASS_OP_SHIFT) |
2352880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
2362880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                R300_S_BACK_ZFAIL_OP_SHIFT);
2372880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2382880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            /* XXX it seems r3xx doesn't support STENCILREFMASK_BF */
2392880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            if (caps->is_r500)
2402880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            {
2412880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
2422880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                dsa->stencil_ref_bf = (state->stencil[1].ref_value) |
2432880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                    (state->stencil[1].valuemask <<
2442880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                    R300_STENCILMASK_SHIFT) |
2452880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                    (state->stencil[1].writemask <<
2462880df2609eba09b555ca37be04b6ad89290c765Tom Hudson                    R300_STENCILWRITEMASK_SHIFT);
2472880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            }
2482880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        }
2492880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
2502880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
2512880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    /* Alpha test setup. */
2522880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (state->alpha.enabled) {
2532880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        dsa->alpha_function =
2542880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            r300_translate_alpha_function(state->alpha.func) |
2552880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            R300_FG_ALPHA_FUNC_ENABLE;
2562880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
257        /* XXX figure out why emitting 10bit alpha ref causes CS to dump */
258        /* always use 8bit alpha ref */
259        dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
260
261        if (caps->is_r500)
262            dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
263    }
264
265    return (void*)dsa;
266}
267
268/* Bind DSA state. */
269static void r300_bind_dsa_state(struct pipe_context* pipe,
270                                void* state)
271{
272    struct r300_context* r300 = r300_context(pipe);
273
274    r300->dsa_state = (struct r300_dsa_state*)state;
275    r300->dirty_state |= R300_NEW_DSA;
276}
277
278/* Free DSA state. */
279static void r300_delete_dsa_state(struct pipe_context* pipe,
280                                  void* state)
281{
282    FREE(state);
283}
284
285static void r300_set_edgeflags(struct pipe_context* pipe,
286                               const unsigned* bitfield)
287{
288    /* XXX you know it's bad when i915 has this blank too */
289    /* XXX and even worse, I have no idea WTF the bitfield is */
290}
291
292static void
293    r300_set_framebuffer_state(struct pipe_context* pipe,
294                               const struct pipe_framebuffer_state* state)
295{
296    struct r300_context* r300 = r300_context(pipe);
297
298    if (r300->draw) {
299        draw_flush(r300->draw);
300    }
301
302    r300->framebuffer_state = *state;
303
304    r300->dirty_state |= R300_NEW_FRAMEBUFFERS;
305}
306
307/* Create fragment shader state. */
308static void* r300_create_fs_state(struct pipe_context* pipe,
309                                  const struct pipe_shader_state* shader)
310{
311    struct r300_fragment_shader* fs = NULL;
312
313    fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
314
315    /* Copy state directly into shader. */
316    fs->state = *shader;
317    fs->state.tokens = tgsi_dup_tokens(shader->tokens);
318
319    tgsi_scan_shader(shader->tokens, &fs->info);
320
321    return (void*)fs;
322}
323
324/* Bind fragment shader state. */
325static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
326{
327    struct r300_context* r300 = r300_context(pipe);
328    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
329
330    if (fs == NULL) {
331        r300->fs = NULL;
332        return;
333    } else if (!fs->translated) {
334        r300_translate_fragment_shader(r300, fs);
335    }
336
337    r300->fs = fs;
338
339    r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | R300_NEW_FRAGMENT_SHADER_CONSTANTS;
340}
341
342/* Delete fragment shader state. */
343static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
344{
345    struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
346    rc_constants_destroy(&fs->code.constants);
347    FREE((void*)fs->state.tokens);
348    FREE(shader);
349}
350
351static void r300_set_polygon_stipple(struct pipe_context* pipe,
352                                     const struct pipe_poly_stipple* state)
353{
354    /* XXX no idea how to set this up, but not terribly important */
355}
356
357/* Create a new rasterizer state based on the CSO rasterizer state.
358 *
359 * This is a very large chunk of state, and covers most of the graphics
360 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
361 *
362 * In a not entirely unironic sidenote, this state has nearly nothing to do
363 * with the actual block on the Radeon called the rasterizer (RS). */
364static void* r300_create_rs_state(struct pipe_context* pipe,
365                                  const struct pipe_rasterizer_state* state)
366{
367    struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
368
369    /* Copy rasterizer state for Draw. */
370    rs->rs = *state;
371
372    rs->enable_vte = !state->bypass_vs_clip_and_viewport;
373
374#ifdef PIPE_ARCH_LITTLE_ENDIAN
375    rs->vap_control_status = R300_VC_NO_SWAP;
376#else
377    rs->vap_control_status = R300_VC_32BIT_SWAP;
378#endif
379
380    /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
381     * Else, enable HW TCL and force Draw's TCL off. */
382    if (state->bypass_vs_clip_and_viewport ||
383            !r300_screen(pipe->screen)->caps->has_tcl) {
384        rs->vap_control_status |= R300_VAP_TCL_BYPASS;
385    } else {
386        rs->rs.bypass_vs_clip_and_viewport = TRUE;
387    }
388
389    rs->point_size = pack_float_16_6x(state->point_size) |
390        (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
391
392    rs->point_minmax =
393        ((int)(state->point_size_min * 6.0) <<
394         R300_GA_POINT_MINMAX_MIN_SHIFT) |
395        ((int)(state->point_size_max * 6.0) <<
396         R300_GA_POINT_MINMAX_MAX_SHIFT);
397
398    rs->line_control = pack_float_16_6x(state->line_width) |
399        R300_GA_LINE_CNTL_END_TYPE_COMP;
400
401    /* XXX I think there is something wrong with the polygon mode,
402     * XXX re-test when r300g is in a better shape */
403
404    /* Enable polygon mode */
405    if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
406        state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
407        rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
408    }
409
410    /* Radeons don't think in "CW/CCW", they think in "front/back". */
411    if (state->front_winding == PIPE_WINDING_CW) {
412        rs->cull_mode = R300_FRONT_FACE_CW;
413
414        /* Polygon offset */
415        if (state->offset_cw) {
416            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
417        }
418        if (state->offset_ccw) {
419            rs->polygon_offset_enable |= R300_BACK_ENABLE;
420        }
421
422        /* Polygon mode */
423        if (rs->polygon_mode) {
424            rs->polygon_mode |=
425                r300_translate_polygon_mode_front(state->fill_cw);
426            rs->polygon_mode |=
427                r300_translate_polygon_mode_back(state->fill_ccw);
428        }
429    } else {
430        rs->cull_mode = R300_FRONT_FACE_CCW;
431
432        /* Polygon offset */
433        if (state->offset_ccw) {
434            rs->polygon_offset_enable |= R300_FRONT_ENABLE;
435        }
436        if (state->offset_cw) {
437            rs->polygon_offset_enable |= R300_BACK_ENABLE;
438        }
439
440        /* Polygon mode */
441        if (rs->polygon_mode) {
442            rs->polygon_mode |=
443                r300_translate_polygon_mode_front(state->fill_ccw);
444            rs->polygon_mode |=
445                r300_translate_polygon_mode_back(state->fill_cw);
446        }
447    }
448    if (state->front_winding & state->cull_mode) {
449        rs->cull_mode |= R300_CULL_FRONT;
450    }
451    if (~(state->front_winding) & state->cull_mode) {
452        rs->cull_mode |= R300_CULL_BACK;
453    }
454
455    if (rs->polygon_offset_enable) {
456        rs->depth_offset_front = rs->depth_offset_back =
457            fui(state->offset_units);
458        rs->depth_scale_front = rs->depth_scale_back =
459            fui(state->offset_scale);
460    }
461
462    if (state->line_stipple_enable) {
463        rs->line_stipple_config =
464            R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
465            (fui((float)state->line_stipple_factor) &
466                R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
467        /* XXX this might need to be scaled up */
468        rs->line_stipple_value = state->line_stipple_pattern;
469    }
470
471    if (state->flatshade) {
472        rs->color_control = R300_SHADE_MODEL_FLAT;
473    } else {
474        rs->color_control = R300_SHADE_MODEL_SMOOTH;
475    }
476
477    if (!state->flatshade_first) {
478        rs->color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
479    }
480
481    return (void*)rs;
482}
483
484/* Bind rasterizer state. */
485static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
486{
487    struct r300_context* r300 = r300_context(pipe);
488    struct r300_rs_state* rs = (struct r300_rs_state*)state;
489
490    if (r300->draw) {
491        draw_flush(r300->draw);
492        draw_set_rasterizer_state(r300->draw, &rs->rs);
493    }
494
495    r300->rs_state = rs;
496    /* XXX Clean these up when we move to atom emits */
497    r300->dirty_state |= R300_NEW_RASTERIZER;
498    r300->dirty_state |= R300_NEW_RS_BLOCK;
499    r300->dirty_state |= R300_NEW_SCISSOR;
500    r300->dirty_state |= R300_NEW_VIEWPORT;
501}
502
503/* Free rasterizer state. */
504static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
505{
506    FREE(state);
507}
508
509static void*
510        r300_create_sampler_state(struct pipe_context* pipe,
511                                  const struct pipe_sampler_state* state)
512{
513    struct r300_context* r300 = r300_context(pipe);
514    struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
515    int lod_bias;
516
517    sampler->filter0 |=
518        (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
519        (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
520        (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
521
522    sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
523                                                   state->mag_img_filter,
524                                                   state->min_mip_filter);
525
526    lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
527
528    sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
529
530    sampler->filter1 |= r300_anisotropy(state->max_anisotropy);
531
532    util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM,
533                    &sampler->border_color);
534
535    /* R500-specific fixups and optimizations */
536    if (r300_screen(r300->context.screen)->caps->is_r500) {
537        sampler->filter1 |= R500_BORDER_FIX;
538    }
539
540    return (void*)sampler;
541}
542
543static void r300_bind_sampler_states(struct pipe_context* pipe,
544                                     unsigned count,
545                                     void** states)
546{
547    struct r300_context* r300 = r300_context(pipe);
548    int i;
549
550    if (count > 8) {
551        return;
552    }
553
554    for (i = 0; i < count; i++) {
555        if (r300->sampler_states[i] != states[i]) {
556            r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
557            r300->dirty_state |= (R300_NEW_SAMPLER << i);
558        }
559    }
560
561    r300->sampler_count = count;
562}
563
564static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
565{
566    FREE(state);
567}
568
569static void r300_set_sampler_textures(struct pipe_context* pipe,
570                                      unsigned count,
571                                      struct pipe_texture** texture)
572{
573    struct r300_context* r300 = r300_context(pipe);
574    boolean is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
575    int i;
576
577    /* XXX magic num */
578    if (count > 8) {
579        return;
580    }
581
582    r300->context.flush(&r300->context, 0, NULL);
583
584    for (i = 0; i < count; i++) {
585        if (r300->textures[i] != (struct r300_texture*)texture[i]) {
586            pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
587                texture[i]);
588            r300->dirty_state |= (R300_NEW_TEXTURE << i);
589
590            /* R300-specific - set the texrect factor in a fragment shader */
591            if (!is_r500 && r300->textures[i]->is_npot) {
592                /* XXX It would be nice to re-emit just 1 constant,
593                 * XXX not all of them */
594                r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
595            }
596        }
597    }
598
599    for (i = count; i < 8; i++) {
600        if (r300->textures[i]) {
601            pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
602                NULL);
603            r300->dirty_state |= (R300_NEW_TEXTURE << i);
604        }
605    }
606
607    r300->texture_count = count;
608}
609
610static void r300_set_scissor_state(struct pipe_context* pipe,
611                                   const struct pipe_scissor_state* state)
612{
613    struct r300_context* r300 = r300_context(pipe);
614
615    if (r300_screen(r300->context.screen)->caps->is_r500) {
616        r300->scissor_state->scissor_top_left =
617            (state->minx << R300_SCISSORS_X_SHIFT) |
618            (state->miny << R300_SCISSORS_Y_SHIFT);
619        r300->scissor_state->scissor_bottom_right =
620            ((state->maxx - 1) << R300_SCISSORS_X_SHIFT) |
621            ((state->maxy - 1) << R300_SCISSORS_Y_SHIFT);
622    } else {
623        /* Offset of 1440 in non-R500 chipsets. */
624        r300->scissor_state->scissor_top_left =
625            ((state->minx + 1440) << R300_SCISSORS_X_SHIFT) |
626            ((state->miny + 1440) << R300_SCISSORS_Y_SHIFT);
627        r300->scissor_state->scissor_bottom_right =
628            (((state->maxx - 1) + 1440) << R300_SCISSORS_X_SHIFT) |
629            (((state->maxy - 1) + 1440) << R300_SCISSORS_Y_SHIFT);
630    }
631
632    r300->dirty_state |= R300_NEW_SCISSOR;
633}
634
635static void r300_set_viewport_state(struct pipe_context* pipe,
636                                    const struct pipe_viewport_state* state)
637{
638    struct r300_context* r300 = r300_context(pipe);
639
640    /* Do the transform in HW. */
641    r300->viewport_state->vte_control = R300_VTX_W0_FMT;
642
643    if (state->scale[0] != 1.0f) {
644        r300->viewport_state->xscale = state->scale[0];
645        r300->viewport_state->vte_control |= R300_VPORT_X_SCALE_ENA;
646    }
647    if (state->scale[1] != 1.0f) {
648        r300->viewport_state->yscale = state->scale[1];
649        r300->viewport_state->vte_control |= R300_VPORT_Y_SCALE_ENA;
650    }
651    if (state->scale[2] != 1.0f) {
652        r300->viewport_state->zscale = state->scale[2];
653        r300->viewport_state->vte_control |= R300_VPORT_Z_SCALE_ENA;
654    }
655    if (state->translate[0] != 0.0f) {
656        r300->viewport_state->xoffset = state->translate[0];
657        r300->viewport_state->vte_control |= R300_VPORT_X_OFFSET_ENA;
658    }
659    if (state->translate[1] != 0.0f) {
660        r300->viewport_state->yoffset = state->translate[1];
661        r300->viewport_state->vte_control |= R300_VPORT_Y_OFFSET_ENA;
662    }
663    if (state->translate[2] != 0.0f) {
664        r300->viewport_state->zoffset = state->translate[2];
665        r300->viewport_state->vte_control |= R300_VPORT_Z_OFFSET_ENA;
666    }
667
668    r300->dirty_state |= R300_NEW_VIEWPORT;
669}
670
671static void r300_set_vertex_buffers(struct pipe_context* pipe,
672                                    unsigned count,
673                                    const struct pipe_vertex_buffer* buffers)
674{
675    struct r300_context* r300 = r300_context(pipe);
676
677    memcpy(r300->vertex_buffer, buffers,
678        sizeof(struct pipe_vertex_buffer) * count);
679    r300->vertex_buffer_count = count;
680
681    if (r300->draw) {
682        draw_flush(r300->draw);
683        draw_set_vertex_buffers(r300->draw, count, buffers);
684    }
685}
686
687static void r300_set_vertex_elements(struct pipe_context* pipe,
688                                    unsigned count,
689                                    const struct pipe_vertex_element* elements)
690{
691    struct r300_context* r300 = r300_context(pipe);
692
693    memcpy(r300->vertex_element,
694           elements,
695           sizeof(struct pipe_vertex_element) * count);
696    r300->vertex_element_count = count;
697
698    if (r300->draw) {
699        draw_flush(r300->draw);
700        draw_set_vertex_elements(r300->draw, count, elements);
701    }
702}
703
704static void* r300_create_vs_state(struct pipe_context* pipe,
705                                  const struct pipe_shader_state* shader)
706{
707    struct r300_context* r300 = r300_context(pipe);
708
709    if (r300_screen(pipe->screen)->caps->has_tcl) {
710        struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
711        /* Copy state directly into shader. */
712        vs->state = *shader;
713        vs->state.tokens = tgsi_dup_tokens(shader->tokens);
714
715        tgsi_scan_shader(shader->tokens, &vs->info);
716
717        /* Appease Draw. */
718        vs->draw = draw_create_vertex_shader(r300->draw, shader);
719
720        return (void*)vs;
721    } else {
722        return draw_create_vertex_shader(r300->draw, shader);
723    }
724}
725
726static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
727{
728    struct r300_context* r300 = r300_context(pipe);
729
730    draw_flush(r300->draw);
731
732    if (r300_screen(pipe->screen)->caps->has_tcl) {
733        struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
734
735        if (vs == NULL) {
736            r300->vs = NULL;
737            return;
738        } else if (!vs->translated) {
739            r300_translate_vertex_shader(r300, vs);
740        }
741
742        draw_bind_vertex_shader(r300->draw, vs->draw);
743        r300->vs = vs;
744        r300->dirty_state |= R300_NEW_VERTEX_SHADER | R300_NEW_VERTEX_SHADER_CONSTANTS;
745    } else {
746        draw_bind_vertex_shader(r300->draw,
747                (struct draw_vertex_shader*)shader);
748    }
749}
750
751static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
752{
753    struct r300_context* r300 = r300_context(pipe);
754
755    if (r300_screen(pipe->screen)->caps->has_tcl) {
756        struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
757
758        rc_constants_destroy(&vs->code.constants);
759        draw_delete_vertex_shader(r300->draw, vs->draw);
760        FREE((void*)vs->state.tokens);
761        FREE(shader);
762    } else {
763        draw_delete_vertex_shader(r300->draw,
764                (struct draw_vertex_shader*)shader);
765    }
766}
767
768static void r300_set_constant_buffer(struct pipe_context *pipe,
769                                     uint shader, uint index,
770                                     const struct pipe_constant_buffer *buf)
771{
772    struct r300_context* r300 = r300_context(pipe);
773    void *mapped;
774
775    if (buf == NULL || buf->buffer->size == 0 ||
776        (mapped = pipe_buffer_map(pipe->screen, buf->buffer, PIPE_BUFFER_USAGE_CPU_READ)) == NULL)
777    {
778        r300->shader_constants[shader].count = 0;
779        return;
780    }
781
782    assert((buf->buffer->size % 4 * sizeof(float)) == 0);
783    memcpy(r300->shader_constants[shader].constants, mapped, buf->buffer->size);
784    r300->shader_constants[shader].count = buf->buffer->size / (4 * sizeof(float));
785    pipe_buffer_unmap(pipe->screen, buf->buffer);
786
787    if (shader == PIPE_SHADER_VERTEX)
788        r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS;
789    else if (shader == PIPE_SHADER_FRAGMENT)
790        r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
791}
792
793void r300_init_state_functions(struct r300_context* r300)
794{
795    r300->context.create_blend_state = r300_create_blend_state;
796    r300->context.bind_blend_state = r300_bind_blend_state;
797    r300->context.delete_blend_state = r300_delete_blend_state;
798
799    r300->context.set_blend_color = r300_set_blend_color;
800
801    r300->context.set_clip_state = r300_set_clip_state;
802
803    r300->context.set_constant_buffer = r300_set_constant_buffer;
804
805    r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
806    r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
807    r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
808
809    r300->context.set_edgeflags = r300_set_edgeflags;
810
811    r300->context.set_framebuffer_state = r300_set_framebuffer_state;
812
813    r300->context.create_fs_state = r300_create_fs_state;
814    r300->context.bind_fs_state = r300_bind_fs_state;
815    r300->context.delete_fs_state = r300_delete_fs_state;
816
817    r300->context.set_polygon_stipple = r300_set_polygon_stipple;
818
819    r300->context.create_rasterizer_state = r300_create_rs_state;
820    r300->context.bind_rasterizer_state = r300_bind_rs_state;
821    r300->context.delete_rasterizer_state = r300_delete_rs_state;
822
823    r300->context.create_sampler_state = r300_create_sampler_state;
824    r300->context.bind_sampler_states = r300_bind_sampler_states;
825    r300->context.delete_sampler_state = r300_delete_sampler_state;
826
827    r300->context.set_sampler_textures = r300_set_sampler_textures;
828
829    r300->context.set_scissor_state = r300_set_scissor_state;
830
831    r300->context.set_viewport_state = r300_set_viewport_state;
832
833    r300->context.set_vertex_buffers = r300_set_vertex_buffers;
834    r300->context.set_vertex_elements = r300_set_vertex_elements;
835
836    r300->context.create_vs_state = r300_create_vs_state;
837    r300->context.bind_vs_state = r300_bind_vs_state;
838    r300->context.delete_vs_state = r300_delete_vs_state;
839}
840