r300-gallium: Set up draw rasterizer.
[platform/upstream/mesa.git] / src / gallium / drivers / r300 / r300_state.c
1 /*
2  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "util/u_math.h"
24 #include "util/u_pack_color.h"
25
26 #include "pipe/p_debug.h"
27 #include "pipe/internal/p_winsys_screen.h"
28
29 #include "r300_context.h"
30 #include "r300_reg.h"
31 #include "r300_state_shader.h"
32
33 /* r300_state: Functions used to intialize state context by translating
34  * Gallium state objects into semi-native r300 state objects.
35  *
36  * XXX break this file up into pieces if it gets too big! */
37
38 /* Pack a float into a dword. */
39 static uint32_t pack_float_32(float f)
40 {
41     union {
42         float f;
43         uint32_t u;
44     } u;
45
46     u.f = f;
47     return u.u;
48 }
49
50 static uint32_t translate_blend_function(int blend_func) {
51     switch (blend_func) {
52         case PIPE_BLEND_ADD:
53             return R300_COMB_FCN_ADD_CLAMP;
54         case PIPE_BLEND_SUBTRACT:
55             return R300_COMB_FCN_SUB_CLAMP;
56         case PIPE_BLEND_REVERSE_SUBTRACT:
57             return R300_COMB_FCN_RSUB_CLAMP;
58         case PIPE_BLEND_MIN:
59             return R300_COMB_FCN_MIN;
60         case PIPE_BLEND_MAX:
61             return R300_COMB_FCN_MAX;
62         default:
63             debug_printf("r300: Unknown blend function %d\n", blend_func);
64             break;
65     }
66     return 0;
67 }
68
69 /* XXX we can also offer the D3D versions of some of these... */
70 static uint32_t translate_blend_factor(int blend_fact) {
71     switch (blend_fact) {
72         case PIPE_BLENDFACTOR_ONE:
73             return R300_BLEND_GL_ONE;
74         case PIPE_BLENDFACTOR_SRC_COLOR:
75             return R300_BLEND_GL_SRC_COLOR;
76         case PIPE_BLENDFACTOR_SRC_ALPHA:
77             return R300_BLEND_GL_SRC_ALPHA;
78         case PIPE_BLENDFACTOR_DST_ALPHA:
79             return R300_BLEND_GL_DST_ALPHA;
80         case PIPE_BLENDFACTOR_DST_COLOR:
81             return R300_BLEND_GL_DST_COLOR;
82         case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
83             return R300_BLEND_GL_SRC_ALPHA_SATURATE;
84         case PIPE_BLENDFACTOR_CONST_COLOR:
85             return R300_BLEND_GL_CONST_COLOR;
86         case PIPE_BLENDFACTOR_CONST_ALPHA:
87             return R300_BLEND_GL_CONST_ALPHA;
88         /* XXX WTF are these?
89         case PIPE_BLENDFACTOR_SRC1_COLOR:
90         case PIPE_BLENDFACTOR_SRC1_ALPHA: */
91         case PIPE_BLENDFACTOR_ZERO:
92             return R300_BLEND_GL_ZERO;
93         case PIPE_BLENDFACTOR_INV_SRC_COLOR:
94             return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
95         case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
96             return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
97         case PIPE_BLENDFACTOR_INV_DST_ALPHA:
98             return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
99         case PIPE_BLENDFACTOR_INV_DST_COLOR:
100             return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
101         case PIPE_BLENDFACTOR_INV_CONST_COLOR:
102             return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
103         case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
104             return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
105         /* XXX see above
106         case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
107         case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
108         default:
109             debug_printf("r300: Unknown blend factor %d\n", blend_fact);
110             break;
111     }
112     return 0;
113 }
114
115 /* Create a new blend state based on the CSO blend state.
116  *
117  * This encompasses alpha blending, logic/raster ops, and blend dithering. */
118 static void* r300_create_blend_state(struct pipe_context* pipe,
119                                      const struct pipe_blend_state* state)
120 {
121     struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
122
123     if (state->blend_enable) {
124         /* XXX for now, always do separate alpha...
125          * is it faster to do it with one reg? */
126         blend->blend_control = R300_ALPHA_BLEND_ENABLE |
127                 R300_SEPARATE_ALPHA_ENABLE |
128                 R300_READ_ENABLE |
129                 translate_blend_function(state->rgb_func) |
130                 (translate_blend_factor(state->rgb_src_factor) <<
131                     R300_SRC_BLEND_SHIFT) |
132                 (translate_blend_factor(state->rgb_dst_factor) <<
133                     R300_DST_BLEND_SHIFT);
134         blend->alpha_blend_control =
135                 translate_blend_function(state->alpha_func) |
136                 (translate_blend_factor(state->alpha_src_factor) <<
137                     R300_SRC_BLEND_SHIFT) |
138                 (translate_blend_factor(state->alpha_dst_factor) <<
139                     R300_DST_BLEND_SHIFT);
140     }
141
142     /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
143     /* XXX are logicops still allowed if blending's disabled?
144      * Does Gallium take care of it for us? */
145     if (state->logicop_enable) {
146         blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
147                 (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
148     }
149
150     if (state->dither) {
151         blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
152                 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
153     }
154
155     return (void*)blend;
156 }
157
158 /* Bind blend state. */
159 static void r300_bind_blend_state(struct pipe_context* pipe,
160                                   void* state)
161 {
162     struct r300_context* r300 = r300_context(pipe);
163
164     r300->blend_state = (struct r300_blend_state*)state;
165     r300->dirty_state |= R300_NEW_BLEND;
166 }
167
168 /* Free blend state. */
169 static void r300_delete_blend_state(struct pipe_context* pipe,
170                                     void* state)
171 {
172     FREE(state);
173 }
174
175 /* Set blend color.
176  * Setup both R300 and R500 registers, figure out later which one to write. */
177 static void r300_set_blend_color(struct pipe_context* pipe,
178                                  const struct pipe_blend_color* color)
179 {
180     struct r300_context* r300 = r300_context(pipe);
181     uint32_t r, g, b, a;
182     ubyte ur, ug, ub, ua;
183
184     r = util_iround(color->color[0] * 1023.0f);
185     g = util_iround(color->color[1] * 1023.0f);
186     b = util_iround(color->color[2] * 1023.0f);
187     a = util_iround(color->color[3] * 1023.0f);
188
189     ur = float_to_ubyte(color->color[0]);
190     ug = float_to_ubyte(color->color[1]);
191     ub = float_to_ubyte(color->color[2]);
192     ua = float_to_ubyte(color->color[3]);
193
194     r300->blend_color_state->blend_color = (a << 24) | (r << 16) | (g << 8) | b;
195
196     r300->blend_color_state->blend_color_red_alpha = ur | (ua << 16);
197     r300->blend_color_state->blend_color_green_blue = ub | (ug << 16);
198
199     r300->dirty_state |= R300_NEW_BLEND_COLOR;
200 }
201
202 static void r300_set_clip_state(struct pipe_context* pipe,
203                                 const struct pipe_clip_state* state)
204 {
205     struct r300_context* r300 = r300_context(pipe);
206     /* XXX Draw */
207     draw_flush(r300->draw);
208     draw_set_clip_state(r300->draw, state);
209 }
210
211 static void
212     r300_set_constant_buffer(struct pipe_context* pipe,
213                              uint shader, uint index,
214                              const struct pipe_constant_buffer* buffer)
215 {
216     struct r300_context* r300 = r300_context(pipe);
217
218     /* This entire chunk of code seems ever-so-slightly baked.
219      * It's as if I've got pipe_buffer* matryoshkas... */
220     if (buffer && buffer->buffer && buffer->buffer->size) {
221         void* map = pipe->winsys->buffer_map(pipe->winsys, buffer->buffer,
222                                              PIPE_BUFFER_USAGE_CPU_READ);
223         memcpy(r300->shader_constants[shader].constants,
224             map, buffer->buffer->size);
225         pipe->winsys->buffer_unmap(pipe->winsys, buffer->buffer);
226
227         r300->shader_constants[shader].user_count =
228             buffer->buffer->size / (sizeof(float) * 4);
229     } else {
230         r300->shader_constants[shader].user_count = 0;
231     }
232
233     r300->dirty_state |= R300_NEW_CONSTANTS;
234 }
235
236 static uint32_t translate_depth_stencil_function(int zs_func) {
237     switch (zs_func) {
238         case PIPE_FUNC_NEVER:
239             return R300_ZS_NEVER;
240         case PIPE_FUNC_LESS:
241             return R300_ZS_LESS;
242         case PIPE_FUNC_EQUAL:
243             return R300_ZS_EQUAL;
244         case PIPE_FUNC_LEQUAL:
245             return R300_ZS_LEQUAL;
246         case PIPE_FUNC_GREATER:
247             return R300_ZS_GREATER;
248         case PIPE_FUNC_NOTEQUAL:
249             return R300_ZS_NOTEQUAL;
250         case PIPE_FUNC_GEQUAL:
251             return R300_ZS_GEQUAL;
252         case PIPE_FUNC_ALWAYS:
253             return R300_ZS_ALWAYS;
254         default:
255             debug_printf("r300: Unknown depth/stencil function %d\n",
256                 zs_func);
257             break;
258     }
259     return 0;
260 }
261
262 static uint32_t translate_stencil_op(int s_op) {
263     switch (s_op) {
264         case PIPE_STENCIL_OP_KEEP:
265             return R300_ZS_KEEP;
266         case PIPE_STENCIL_OP_ZERO:
267             return R300_ZS_ZERO;
268         case PIPE_STENCIL_OP_REPLACE:
269             return R300_ZS_REPLACE;
270         case PIPE_STENCIL_OP_INCR:
271             return R300_ZS_INCR;
272         case PIPE_STENCIL_OP_DECR:
273             return R300_ZS_DECR;
274         case PIPE_STENCIL_OP_INCR_WRAP:
275             return R300_ZS_INCR_WRAP;
276         case PIPE_STENCIL_OP_DECR_WRAP:
277             return R300_ZS_DECR_WRAP;
278         case PIPE_STENCIL_OP_INVERT:
279             return R300_ZS_INVERT;
280         default:
281             debug_printf("r300: Unknown stencil op %d", s_op);
282             break;
283     }
284     return 0;
285 }
286
287 static uint32_t translate_alpha_function(int alpha_func) {
288     switch (alpha_func) {
289         case PIPE_FUNC_NEVER:
290             return R300_FG_ALPHA_FUNC_NEVER;
291         case PIPE_FUNC_LESS:
292             return R300_FG_ALPHA_FUNC_LESS;
293         case PIPE_FUNC_EQUAL:
294             return R300_FG_ALPHA_FUNC_EQUAL;
295         case PIPE_FUNC_LEQUAL:
296             return R300_FG_ALPHA_FUNC_LE;
297         case PIPE_FUNC_GREATER:
298             return R300_FG_ALPHA_FUNC_GREATER;
299         case PIPE_FUNC_NOTEQUAL:
300             return R300_FG_ALPHA_FUNC_NOTEQUAL;
301         case PIPE_FUNC_GEQUAL:
302             return R300_FG_ALPHA_FUNC_GE;
303         case PIPE_FUNC_ALWAYS:
304             return R300_FG_ALPHA_FUNC_ALWAYS;
305         default:
306             debug_printf("r300: Unknown alpha function %d", alpha_func);
307             break;
308     }
309     return 0;
310 }
311
312 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
313  *
314  * This contains the depth buffer, stencil buffer, alpha test, and such.
315  * On the Radeon, depth and stencil buffer setup are intertwined, which is
316  * the reason for some of the strange-looking assignments across registers. */
317 static void*
318         r300_create_dsa_state(struct pipe_context* pipe,
319                               const struct pipe_depth_stencil_alpha_state* state)
320 {
321     struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
322
323     /* Depth test setup. */
324     if (state->depth.enabled) {
325         dsa->z_buffer_control |= R300_Z_ENABLE;
326
327         if (state->depth.writemask) {
328             dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
329         }
330
331         dsa->z_stencil_control |=
332             (translate_depth_stencil_function(state->depth.func) <<
333                 R300_Z_FUNC_SHIFT);
334     }
335
336     /* Stencil buffer setup. */
337     if (state->stencil[0].enabled) {
338         dsa->z_buffer_control |= R300_STENCIL_ENABLE;
339         dsa->z_stencil_control |=
340                 (translate_depth_stencil_function(state->stencil[0].func) <<
341                     R300_S_FRONT_FUNC_SHIFT) |
342                 (translate_stencil_op(state->stencil[0].fail_op) <<
343                     R300_S_FRONT_SFAIL_OP_SHIFT) |
344                 (translate_stencil_op(state->stencil[0].zpass_op) <<
345                     R300_S_FRONT_ZPASS_OP_SHIFT) |
346                 (translate_stencil_op(state->stencil[0].zfail_op) <<
347                     R300_S_FRONT_ZFAIL_OP_SHIFT);
348
349         dsa->stencil_ref_mask = (state->stencil[0].ref_value) |
350                 (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
351                 (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
352
353         if (state->stencil[1].enabled) {
354             dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
355             dsa->z_stencil_control |=
356                 (translate_depth_stencil_function(state->stencil[1].func) <<
357                     R300_S_BACK_FUNC_SHIFT) |
358                 (translate_stencil_op(state->stencil[1].fail_op) <<
359                     R300_S_BACK_SFAIL_OP_SHIFT) |
360                 (translate_stencil_op(state->stencil[1].zpass_op) <<
361                     R300_S_BACK_ZPASS_OP_SHIFT) |
362                 (translate_stencil_op(state->stencil[1].zfail_op) <<
363                     R300_S_BACK_ZFAIL_OP_SHIFT);
364
365             dsa->stencil_ref_bf = (state->stencil[1].ref_value) |
366                 (state->stencil[1].valuemask << R300_STENCILMASK_SHIFT) |
367                 (state->stencil[1].writemask << R300_STENCILWRITEMASK_SHIFT);
368         }
369     }
370
371     /* Alpha test setup. */
372     if (state->alpha.enabled) {
373         dsa->alpha_function = translate_alpha_function(state->alpha.func) |
374             R300_FG_ALPHA_FUNC_ENABLE;
375         dsa->alpha_reference = CLAMP(state->alpha.ref_value * 1023.0f,
376                                      0, 1023);
377     } else {
378         dsa->z_buffer_top = R300_ZTOP_ENABLE;
379     }
380
381     return (void*)dsa;
382 }
383
384 /* Bind DSA state. */
385 static void r300_bind_dsa_state(struct pipe_context* pipe,
386                                 void* state)
387 {
388     struct r300_context* r300 = r300_context(pipe);
389
390     r300->dsa_state = (struct r300_dsa_state*)state;
391     r300->dirty_state |= R300_NEW_DSA;
392 }
393
394 /* Free DSA state. */
395 static void r300_delete_dsa_state(struct pipe_context* pipe,
396                                   void* state)
397 {
398     FREE(state);
399 }
400
401 static void r300_set_edgeflags(struct pipe_context* pipe,
402                                const unsigned* bitfield)
403 {
404     /* XXX you know it's bad when i915 has this blank too */
405 }
406
407 static void
408     r300_set_framebuffer_state(struct pipe_context* pipe,
409                                const struct pipe_framebuffer_state* state)
410 {
411     struct r300_context* r300 = r300_context(pipe);
412
413     draw_flush(r300->draw);
414
415     r300->framebuffer_state = *state;
416
417     r300->dirty_state |= R300_NEW_FRAMEBUFFERS;
418 }
419
420 /* Create fragment shader state. */
421 static void* r300_create_fs_state(struct pipe_context* pipe,
422                                   const struct pipe_shader_state* shader)
423 {
424     struct r300_context* r300 = r300_context(pipe);
425     struct r3xx_fragment_shader* fs = NULL;
426
427     if (r300_screen(r300->context.screen)->caps->is_r500) {
428         fs =
429             (struct r3xx_fragment_shader*)CALLOC_STRUCT(r500_fragment_shader);
430     } else {
431         fs =
432             (struct r3xx_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
433     }
434
435     /* Copy state directly into shader. */
436     fs->state = *shader;
437
438     return (void*)fs;
439 }
440
441 /* Bind fragment shader state. */
442 static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
443 {
444     struct r300_context* r300 = r300_context(pipe);
445     struct r3xx_fragment_shader* fs = (struct r3xx_fragment_shader*)shader;
446
447     if (fs == NULL) {
448         r300->fs = NULL;
449         return;
450     } else if (!fs->translated) {
451         if (r300_screen(r300->context.screen)->caps->is_r500) {
452             r500_translate_fragment_shader(r300, (struct r500_fragment_shader*)fs);
453         } else {
454             r300_translate_fragment_shader(r300, (struct r300_fragment_shader*)fs);
455         }
456     }
457
458     fs->translated = true;
459     r300->fs = fs;
460
461     r300->dirty_state |= R300_NEW_FRAGMENT_SHADER;
462 }
463
464 /* Delete fragment shader state. */
465 static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
466 {
467     FREE(shader);
468 }
469
470 static void r300_set_polygon_stipple(struct pipe_context* pipe,
471                                      const struct pipe_poly_stipple* state)
472 {
473     /* XXX */
474 }
475
476 static INLINE int pack_float_16_6x(float f) {
477     return ((int)(f * 6.0) & 0xffff);
478 }
479
480 /* Create a new rasterizer state based on the CSO rasterizer state.
481  *
482  * This is a very large chunk of state, and covers most of the graphics
483  * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
484  *
485  * In a not entirely unironic sidenote, this state has nearly nothing to do
486  * with the actual block on the Radeon called the rasterizer (RS). */
487 static void* r300_create_rs_state(struct pipe_context* pipe,
488                                   const struct pipe_rasterizer_state* state)
489 {
490     struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
491
492     /* XXX this is part of HW TCL */
493     /* XXX endian control */
494     rs->vap_control_status = R300_VAP_TCL_BYPASS;
495
496     rs->point_size = pack_float_16_6x(state->point_size) |
497         (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
498
499     rs->line_control = pack_float_16_6x(state->line_width) |
500         R300_GA_LINE_CNTL_END_TYPE_COMP;
501
502     /* Radeons don't think in "CW/CCW", they think in "front/back". */
503     if (state->front_winding == PIPE_WINDING_CW) {
504         rs->cull_mode = R300_FRONT_FACE_CW;
505
506         if (state->offset_cw) {
507             rs->polygon_offset_enable |= R300_FRONT_ENABLE;
508         }
509         if (state->offset_ccw) {
510             rs->polygon_offset_enable |= R300_BACK_ENABLE;
511         }
512     } else {
513         rs->cull_mode = R300_FRONT_FACE_CCW;
514
515         if (state->offset_ccw) {
516             rs->polygon_offset_enable |= R300_FRONT_ENABLE;
517         }
518         if (state->offset_cw) {
519             rs->polygon_offset_enable |= R300_BACK_ENABLE;
520         }
521     }
522     if (state->front_winding & state->cull_mode) {
523         rs->cull_mode |= R300_CULL_FRONT;
524     }
525     if (~(state->front_winding) & state->cull_mode) {
526         rs->cull_mode |= R300_CULL_BACK;
527     }
528
529     if (rs->polygon_offset_enable) {
530         rs->depth_offset_front = rs->depth_offset_back =
531                 pack_float_32(state->offset_units);
532         rs->depth_scale_front = rs->depth_scale_back =
533                 pack_float_32(state->offset_scale);
534     }
535
536     if (state->line_stipple_enable) {
537         rs->line_stipple_config =
538             R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
539             (pack_float_32((float)state->line_stipple_factor) &
540                 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
541         /* XXX this might need to be scaled up */
542         rs->line_stipple_value = state->line_stipple_pattern;
543     }
544
545     rs->rs = *state;
546
547     return (void*)rs;
548 }
549
550 /* Bind rasterizer state. */
551 static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
552 {
553     struct r300_context* r300 = r300_context(pipe);
554     struct r300_rs_state* rs = (struct r300_rs_state*)state;
555
556     draw_set_rasterizer_state(r300->draw, &rs->rs);
557
558     r300->rs_state = rs;
559     r300->dirty_state |= R300_NEW_RASTERIZER;
560 }
561
562 /* Free rasterizer state. */
563 static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
564 {
565     FREE(state);
566 }
567
568 static uint32_t translate_wrap(int wrap) {
569     switch (wrap) {
570         case PIPE_TEX_WRAP_REPEAT:
571             return R300_TX_REPEAT;
572         case PIPE_TEX_WRAP_CLAMP:
573             return R300_TX_CLAMP;
574         case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
575             return R300_TX_CLAMP_TO_EDGE;
576         case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
577             return R300_TX_CLAMP_TO_BORDER;
578         case PIPE_TEX_WRAP_MIRROR_REPEAT:
579             return R300_TX_REPEAT | R300_TX_MIRRORED;
580         case PIPE_TEX_WRAP_MIRROR_CLAMP:
581             return R300_TX_CLAMP | R300_TX_MIRRORED;
582         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
583             return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
584         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
585             return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
586         default:
587             debug_printf("r300: Unknown texture wrap %d", wrap);
588             return 0;
589     }
590 }
591
592 static uint32_t translate_tex_filters(int min, int mag, int mip) {
593     uint32_t retval = 0;
594     switch (min) {
595         case PIPE_TEX_FILTER_NEAREST:
596             retval |= R300_TX_MIN_FILTER_NEAREST;
597         case PIPE_TEX_FILTER_LINEAR:
598             retval |= R300_TX_MIN_FILTER_LINEAR;
599         case PIPE_TEX_FILTER_ANISO:
600             retval |= R300_TX_MIN_FILTER_ANISO;
601         default:
602             debug_printf("r300: Unknown texture filter %d", min);
603             break;
604     }
605     switch (mag) {
606         case PIPE_TEX_FILTER_NEAREST:
607             retval |= R300_TX_MAG_FILTER_NEAREST;
608         case PIPE_TEX_FILTER_LINEAR:
609             retval |= R300_TX_MAG_FILTER_LINEAR;
610         case PIPE_TEX_FILTER_ANISO:
611             retval |= R300_TX_MAG_FILTER_ANISO;
612         default:
613             debug_printf("r300: Unknown texture filter %d", mag);
614             break;
615     }
616     switch (mip) {
617         case PIPE_TEX_MIPFILTER_NONE:
618             retval |= R300_TX_MIN_FILTER_MIP_NONE;
619         case PIPE_TEX_MIPFILTER_NEAREST:
620             retval |= R300_TX_MIN_FILTER_MIP_NEAREST;
621         case PIPE_TEX_MIPFILTER_LINEAR:
622             retval |= R300_TX_MIN_FILTER_MIP_LINEAR;
623         default:
624             debug_printf("r300: Unknown texture filter %d", mip);
625             break;
626     }
627
628     return retval;
629 }
630
631 static uint32_t anisotropy(float max_aniso) {
632     if (max_aniso >= 16.0f) {
633         return R300_TX_MAX_ANISO_16_TO_1;
634     } else if (max_aniso >= 8.0f) {
635         return R300_TX_MAX_ANISO_8_TO_1;
636     } else if (max_aniso >= 4.0f) {
637         return R300_TX_MAX_ANISO_4_TO_1;
638     } else if (max_aniso >= 2.0f) {
639         return R300_TX_MAX_ANISO_2_TO_1;
640     } else {
641         return R300_TX_MAX_ANISO_1_TO_1;
642     }
643 }
644
645 static void*
646         r300_create_sampler_state(struct pipe_context* pipe,
647                                   const struct pipe_sampler_state* state)
648 {
649     struct r300_context* r300 = r300_context(pipe);
650     struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
651     int lod_bias;
652
653     sampler->filter0 |=
654         (translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
655         (translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
656         (translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
657
658     sampler->filter0 |= translate_tex_filters(state->min_img_filter,
659                                               state->mag_img_filter,
660                                               state->min_mip_filter);
661
662     lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
663
664     sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
665
666     sampler->filter1 |= anisotropy(state->max_anisotropy);
667
668     util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM,
669                     &sampler->border_color);
670
671     /* R500-specific fixups and optimizations */
672     if (r300_screen(r300->context.screen)->caps->is_r500) {
673         sampler->filter1 |= R500_BORDER_FIX;
674     }
675
676     return (void*)sampler;
677 }
678
679 static void r300_bind_sampler_states(struct pipe_context* pipe,
680                                      unsigned count,
681                                      void** states)
682 {
683     struct r300_context* r300 = r300_context(pipe);
684     int i;
685
686     if (count > 8) {
687         return;
688     }
689
690     for (i = 0; i < count; i++) {
691         if (r300->sampler_states[i] != states[i]) {
692             r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
693             r300->dirty_state |= (R300_NEW_SAMPLER << i);
694         }
695     }
696
697     r300->sampler_count = count;
698 }
699
700 static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
701 {
702     FREE(state);
703 }
704
705 static void r300_set_sampler_textures(struct pipe_context* pipe,
706                                       unsigned count,
707                                       struct pipe_texture** texture)
708 {
709     struct r300_context* r300 = r300_context(pipe);
710     int i;
711
712     /* XXX magic num */
713     if (count > 8) {
714         return;
715     }
716
717     for (i = 0; i < count; i++) {
718         if (r300->textures[i] != (struct r300_texture*)texture[i]) {
719             pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
720                 texture[i]);
721             r300->dirty_state |= (R300_NEW_TEXTURE << i);
722         }
723     }
724
725     for (i = count; i < 8; i++) {
726         if (r300->textures[i]) {
727             pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
728                 NULL);
729             r300->dirty_state |= (R300_NEW_TEXTURE << i);
730         }
731     }
732
733     r300->texture_count = count;
734 }
735
736 static void r300_set_scissor_state(struct pipe_context* pipe,
737                                    const struct pipe_scissor_state* state)
738 {
739     struct r300_context* r300 = r300_context(pipe);
740     draw_flush(r300->draw);
741
742     r300->scissor_state->scissor_top_left =
743         (state->minx << R300_SCISSORS_X_SHIFT) |
744         (state->miny << R300_SCISSORS_Y_SHIFT);
745     r300->scissor_state->scissor_bottom_right =
746         (state->maxx << R300_SCISSORS_X_SHIFT) |
747         (state->maxy << R300_SCISSORS_Y_SHIFT);
748
749     r300->dirty_state |= R300_NEW_SCISSOR;
750 }
751
752 static void r300_set_viewport_state(struct pipe_context* pipe,
753                                     const struct pipe_viewport_state* state)
754 {
755     struct r300_context* r300 = r300_context(pipe);
756     /* XXX handing this off to Draw for now */
757     draw_set_viewport_state(r300->draw, state);
758 }
759
760 static void r300_set_vertex_buffers(struct pipe_context* pipe,
761                                     unsigned count,
762                                     const struct pipe_vertex_buffer* buffers)
763 {
764     struct r300_context* r300 = r300_context(pipe);
765
766     memcpy(r300->vertex_buffers, buffers,
767         sizeof(struct pipe_vertex_buffer) * count);
768
769     r300->vertex_buffer_count = count;
770
771     draw_flush(r300->draw);
772     draw_set_vertex_buffers(r300->draw, count, buffers);
773 }
774
775 static void r300_set_vertex_elements(struct pipe_context* pipe,
776                                     unsigned count,
777                                     const struct pipe_vertex_element* elements)
778 {
779     struct r300_context* r300 = r300_context(pipe);
780     /* XXX Draw */
781     draw_flush(r300->draw);
782     draw_set_vertex_elements(r300->draw, count, elements);
783 }
784
785 static void* r300_create_vs_state(struct pipe_context* pipe,
786                                   const struct pipe_shader_state* state)
787 {
788     struct r300_context* context = r300_context(pipe);
789     /* XXX handing this off to Draw for now */
790     return draw_create_vertex_shader(context->draw, state);
791 }
792
793 static void r300_bind_vs_state(struct pipe_context* pipe, void* state) {
794     struct r300_context* context = r300_context(pipe);
795     /* XXX handing this off to Draw for now */
796     draw_bind_vertex_shader(context->draw, (struct draw_vertex_shader*)state);
797 }
798
799 static void r300_delete_vs_state(struct pipe_context* pipe, void* state)
800 {
801     struct r300_context* context = r300_context(pipe);
802     /* XXX handing this off to Draw for now */
803     draw_delete_vertex_shader(context->draw, (struct draw_vertex_shader*)state);
804 }
805
806 void r300_init_state_functions(struct r300_context* r300)
807 {
808     r300->context.create_blend_state = r300_create_blend_state;
809     r300->context.bind_blend_state = r300_bind_blend_state;
810     r300->context.delete_blend_state = r300_delete_blend_state;
811
812     r300->context.set_blend_color = r300_set_blend_color;
813
814     r300->context.set_clip_state = r300_set_clip_state;
815
816     r300->context.set_constant_buffer = r300_set_constant_buffer;
817
818     r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
819     r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
820     r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
821
822     r300->context.set_edgeflags = r300_set_edgeflags;
823
824     r300->context.set_framebuffer_state = r300_set_framebuffer_state;
825
826     r300->context.create_fs_state = r300_create_fs_state;
827     r300->context.bind_fs_state = r300_bind_fs_state;
828     r300->context.delete_fs_state = r300_delete_fs_state;
829
830     r300->context.set_polygon_stipple = r300_set_polygon_stipple;
831
832     r300->context.create_rasterizer_state = r300_create_rs_state;
833     r300->context.bind_rasterizer_state = r300_bind_rs_state;
834     r300->context.delete_rasterizer_state = r300_delete_rs_state;
835
836     r300->context.create_sampler_state = r300_create_sampler_state;
837     r300->context.bind_sampler_states = r300_bind_sampler_states;
838     r300->context.delete_sampler_state = r300_delete_sampler_state;
839
840     r300->context.set_sampler_textures = r300_set_sampler_textures;
841
842     r300->context.set_scissor_state = r300_set_scissor_state;
843
844     r300->context.set_viewport_state = r300_set_viewport_state;
845
846     r300->context.set_vertex_buffers = r300_set_vertex_buffers;
847     r300->context.set_vertex_elements = r300_set_vertex_elements;
848
849     r300->context.create_vs_state = r300_create_vs_state;
850     r300->context.bind_vs_state = r300_bind_vs_state;
851     r300->context.delete_vs_state = r300_delete_vs_state;
852 }