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