Merge commit 'origin/gallium-0.1'
[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 "util/u_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     tgsi_scan_shader(shader->tokens, &fs->info);
439
440     return (void*)fs;
441 }
442
443 /* Bind fragment shader state. */
444 static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
445 {
446     struct r300_context* r300 = r300_context(pipe);
447     struct r3xx_fragment_shader* fs = (struct r3xx_fragment_shader*)shader;
448
449     if (fs == NULL) {
450         r300->fs = NULL;
451         return;
452     } else if (!fs->translated) {
453         if (r300_screen(r300->context.screen)->caps->is_r500) {
454             r500_translate_fragment_shader(r300, (struct r500_fragment_shader*)fs);
455         } else {
456             r300_translate_fragment_shader(r300, (struct r300_fragment_shader*)fs);
457         }
458     }
459
460     fs->translated = true;
461     r300->fs = fs;
462
463     r300->dirty_state |= R300_NEW_FRAGMENT_SHADER;
464 }
465
466 /* Delete fragment shader state. */
467 static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
468 {
469     FREE(shader);
470 }
471
472 static void r300_set_polygon_stipple(struct pipe_context* pipe,
473                                      const struct pipe_poly_stipple* state)
474 {
475     /* XXX */
476 }
477
478 static INLINE int pack_float_16_6x(float f) {
479     return ((int)(f * 6.0) & 0xffff);
480 }
481
482 /* Create a new rasterizer state based on the CSO rasterizer state.
483  *
484  * This is a very large chunk of state, and covers most of the graphics
485  * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
486  *
487  * In a not entirely unironic sidenote, this state has nearly nothing to do
488  * with the actual block on the Radeon called the rasterizer (RS). */
489 static void* r300_create_rs_state(struct pipe_context* pipe,
490                                   const struct pipe_rasterizer_state* state)
491 {
492     struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
493
494     /* XXX this is part of HW TCL */
495     /* XXX endian control */
496     rs->vap_control_status = R300_VAP_TCL_BYPASS;
497
498     rs->point_size = pack_float_16_6x(state->point_size) |
499         (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
500
501     rs->line_control = pack_float_16_6x(state->line_width) |
502         R300_GA_LINE_CNTL_END_TYPE_COMP;
503
504     /* Radeons don't think in "CW/CCW", they think in "front/back". */
505     if (state->front_winding == PIPE_WINDING_CW) {
506         rs->cull_mode = R300_FRONT_FACE_CW;
507
508         if (state->offset_cw) {
509             rs->polygon_offset_enable |= R300_FRONT_ENABLE;
510         }
511         if (state->offset_ccw) {
512             rs->polygon_offset_enable |= R300_BACK_ENABLE;
513         }
514     } else {
515         rs->cull_mode = R300_FRONT_FACE_CCW;
516
517         if (state->offset_ccw) {
518             rs->polygon_offset_enable |= R300_FRONT_ENABLE;
519         }
520         if (state->offset_cw) {
521             rs->polygon_offset_enable |= R300_BACK_ENABLE;
522         }
523     }
524     if (state->front_winding & state->cull_mode) {
525         rs->cull_mode |= R300_CULL_FRONT;
526     }
527     if (~(state->front_winding) & state->cull_mode) {
528         rs->cull_mode |= R300_CULL_BACK;
529     }
530
531     if (rs->polygon_offset_enable) {
532         rs->depth_offset_front = rs->depth_offset_back =
533                 pack_float_32(state->offset_units);
534         rs->depth_scale_front = rs->depth_scale_back =
535                 pack_float_32(state->offset_scale);
536     }
537
538     if (state->line_stipple_enable) {
539         rs->line_stipple_config =
540             R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
541             (pack_float_32((float)state->line_stipple_factor) &
542                 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
543         /* XXX this might need to be scaled up */
544         rs->line_stipple_value = state->line_stipple_pattern;
545     }
546
547     rs->rs = *state;
548
549     return (void*)rs;
550 }
551
552 /* Bind rasterizer state. */
553 static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
554 {
555     struct r300_context* r300 = r300_context(pipe);
556     struct r300_rs_state* rs = (struct r300_rs_state*)state;
557
558     draw_set_rasterizer_state(r300->draw, &rs->rs);
559
560     r300->rs_state = rs;
561     r300->dirty_state |= R300_NEW_RASTERIZER;
562 }
563
564 /* Free rasterizer state. */
565 static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
566 {
567     FREE(state);
568 }
569
570 static uint32_t translate_wrap(int wrap) {
571     switch (wrap) {
572         case PIPE_TEX_WRAP_REPEAT:
573             return R300_TX_REPEAT;
574         case PIPE_TEX_WRAP_CLAMP:
575             return R300_TX_CLAMP;
576         case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
577             return R300_TX_CLAMP_TO_EDGE;
578         case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
579             return R300_TX_CLAMP_TO_BORDER;
580         case PIPE_TEX_WRAP_MIRROR_REPEAT:
581             return R300_TX_REPEAT | R300_TX_MIRRORED;
582         case PIPE_TEX_WRAP_MIRROR_CLAMP:
583             return R300_TX_CLAMP | R300_TX_MIRRORED;
584         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
585             return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
586         case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
587             return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
588         default:
589             debug_printf("r300: Unknown texture wrap %d", wrap);
590             return 0;
591     }
592 }
593
594 static uint32_t translate_tex_filters(int min, int mag, int mip) {
595     uint32_t retval = 0;
596     switch (min) {
597         case PIPE_TEX_FILTER_NEAREST:
598             retval |= R300_TX_MIN_FILTER_NEAREST;
599         case PIPE_TEX_FILTER_LINEAR:
600             retval |= R300_TX_MIN_FILTER_LINEAR;
601         case PIPE_TEX_FILTER_ANISO:
602             retval |= R300_TX_MIN_FILTER_ANISO;
603         default:
604             debug_printf("r300: Unknown texture filter %d", min);
605             break;
606     }
607     switch (mag) {
608         case PIPE_TEX_FILTER_NEAREST:
609             retval |= R300_TX_MAG_FILTER_NEAREST;
610         case PIPE_TEX_FILTER_LINEAR:
611             retval |= R300_TX_MAG_FILTER_LINEAR;
612         case PIPE_TEX_FILTER_ANISO:
613             retval |= R300_TX_MAG_FILTER_ANISO;
614         default:
615             debug_printf("r300: Unknown texture filter %d", mag);
616             break;
617     }
618     switch (mip) {
619         case PIPE_TEX_MIPFILTER_NONE:
620             retval |= R300_TX_MIN_FILTER_MIP_NONE;
621         case PIPE_TEX_MIPFILTER_NEAREST:
622             retval |= R300_TX_MIN_FILTER_MIP_NEAREST;
623         case PIPE_TEX_MIPFILTER_LINEAR:
624             retval |= R300_TX_MIN_FILTER_MIP_LINEAR;
625         default:
626             debug_printf("r300: Unknown texture filter %d", mip);
627             break;
628     }
629
630     return retval;
631 }
632
633 static uint32_t anisotropy(float max_aniso) {
634     if (max_aniso >= 16.0f) {
635         return R300_TX_MAX_ANISO_16_TO_1;
636     } else if (max_aniso >= 8.0f) {
637         return R300_TX_MAX_ANISO_8_TO_1;
638     } else if (max_aniso >= 4.0f) {
639         return R300_TX_MAX_ANISO_4_TO_1;
640     } else if (max_aniso >= 2.0f) {
641         return R300_TX_MAX_ANISO_2_TO_1;
642     } else {
643         return R300_TX_MAX_ANISO_1_TO_1;
644     }
645 }
646
647 static void*
648         r300_create_sampler_state(struct pipe_context* pipe,
649                                   const struct pipe_sampler_state* state)
650 {
651     struct r300_context* r300 = r300_context(pipe);
652     struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
653     int lod_bias;
654
655     sampler->filter0 |=
656         (translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
657         (translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
658         (translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
659
660     sampler->filter0 |= translate_tex_filters(state->min_img_filter,
661                                               state->mag_img_filter,
662                                               state->min_mip_filter);
663
664     lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
665
666     sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
667
668     sampler->filter1 |= anisotropy(state->max_anisotropy);
669
670     util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM,
671                     &sampler->border_color);
672
673     /* R500-specific fixups and optimizations */
674     if (r300_screen(r300->context.screen)->caps->is_r500) {
675         sampler->filter1 |= R500_BORDER_FIX;
676     }
677
678     return (void*)sampler;
679 }
680
681 static void r300_bind_sampler_states(struct pipe_context* pipe,
682                                      unsigned count,
683                                      void** states)
684 {
685     struct r300_context* r300 = r300_context(pipe);
686     int i;
687
688     if (count > 8) {
689         return;
690     }
691
692     for (i = 0; i < count; i++) {
693         if (r300->sampler_states[i] != states[i]) {
694             r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
695             r300->dirty_state |= (R300_NEW_SAMPLER << i);
696         }
697     }
698
699     r300->sampler_count = count;
700 }
701
702 static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
703 {
704     FREE(state);
705 }
706
707 static void r300_set_sampler_textures(struct pipe_context* pipe,
708                                       unsigned count,
709                                       struct pipe_texture** texture)
710 {
711     struct r300_context* r300 = r300_context(pipe);
712     int i;
713
714     /* XXX magic num */
715     if (count > 8) {
716         return;
717     }
718
719     for (i = 0; i < count; i++) {
720         if (r300->textures[i] != (struct r300_texture*)texture[i]) {
721             pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
722                 texture[i]);
723             r300->dirty_state |= (R300_NEW_TEXTURE << i);
724         }
725     }
726
727     for (i = count; i < 8; i++) {
728         if (r300->textures[i]) {
729             pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
730                 NULL);
731             r300->dirty_state |= (R300_NEW_TEXTURE << i);
732         }
733     }
734
735     r300->texture_count = count;
736 }
737
738 static void r300_set_scissor_state(struct pipe_context* pipe,
739                                    const struct pipe_scissor_state* state)
740 {
741     struct r300_context* r300 = r300_context(pipe);
742     draw_flush(r300->draw);
743
744     r300->scissor_state->scissor_top_left =
745         (state->minx << R300_SCISSORS_X_SHIFT) |
746         (state->miny << R300_SCISSORS_Y_SHIFT);
747     r300->scissor_state->scissor_bottom_right =
748         (state->maxx << R300_SCISSORS_X_SHIFT) |
749         (state->maxy << R300_SCISSORS_Y_SHIFT);
750
751     r300->dirty_state |= R300_NEW_SCISSOR;
752 }
753
754 static void r300_set_viewport_state(struct pipe_context* pipe,
755                                     const struct pipe_viewport_state* state)
756 {
757     struct r300_context* r300 = r300_context(pipe);
758     /* XXX handing this off to Draw for now */
759     draw_set_viewport_state(r300->draw, state);
760 }
761
762 static void r300_set_vertex_buffers(struct pipe_context* pipe,
763                                     unsigned count,
764                                     const struct pipe_vertex_buffer* buffers)
765 {
766     struct r300_context* r300 = r300_context(pipe);
767
768     memcpy(r300->vertex_buffers, buffers,
769         sizeof(struct pipe_vertex_buffer) * count);
770
771     r300->vertex_buffer_count = count;
772
773     draw_flush(r300->draw);
774     draw_set_vertex_buffers(r300->draw, count, buffers);
775 }
776
777 static void r300_set_vertex_elements(struct pipe_context* pipe,
778                                     unsigned count,
779                                     const struct pipe_vertex_element* elements)
780 {
781     struct r300_context* r300 = r300_context(pipe);
782     /* XXX Draw */
783     draw_flush(r300->draw);
784     draw_set_vertex_elements(r300->draw, count, elements);
785 }
786
787 static void* r300_create_vs_state(struct pipe_context* pipe,
788                                   const struct pipe_shader_state* state)
789 {
790     struct r300_context* context = r300_context(pipe);
791     /* XXX handing this off to Draw for now */
792     return draw_create_vertex_shader(context->draw, state);
793 }
794
795 static void r300_bind_vs_state(struct pipe_context* pipe, void* state) {
796     struct r300_context* context = r300_context(pipe);
797     /* XXX handing this off to Draw for now */
798     draw_bind_vertex_shader(context->draw, (struct draw_vertex_shader*)state);
799 }
800
801 static void r300_delete_vs_state(struct pipe_context* pipe, void* state)
802 {
803     struct r300_context* context = r300_context(pipe);
804     /* XXX handing this off to Draw for now */
805     draw_delete_vertex_shader(context->draw, (struct draw_vertex_shader*)state);
806 }
807
808 void r300_init_state_functions(struct r300_context* r300)
809 {
810     r300->context.create_blend_state = r300_create_blend_state;
811     r300->context.bind_blend_state = r300_bind_blend_state;
812     r300->context.delete_blend_state = r300_delete_blend_state;
813
814     r300->context.set_blend_color = r300_set_blend_color;
815
816     r300->context.set_clip_state = r300_set_clip_state;
817
818     r300->context.set_constant_buffer = r300_set_constant_buffer;
819
820     r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
821     r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
822     r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
823
824     r300->context.set_edgeflags = r300_set_edgeflags;
825
826     r300->context.set_framebuffer_state = r300_set_framebuffer_state;
827
828     r300->context.create_fs_state = r300_create_fs_state;
829     r300->context.bind_fs_state = r300_bind_fs_state;
830     r300->context.delete_fs_state = r300_delete_fs_state;
831
832     r300->context.set_polygon_stipple = r300_set_polygon_stipple;
833
834     r300->context.create_rasterizer_state = r300_create_rs_state;
835     r300->context.bind_rasterizer_state = r300_bind_rs_state;
836     r300->context.delete_rasterizer_state = r300_delete_rs_state;
837
838     r300->context.create_sampler_state = r300_create_sampler_state;
839     r300->context.bind_sampler_states = r300_bind_sampler_states;
840     r300->context.delete_sampler_state = r300_delete_sampler_state;
841
842     r300->context.set_sampler_textures = r300_set_sampler_textures;
843
844     r300->context.set_scissor_state = r300_set_scissor_state;
845
846     r300->context.set_viewport_state = r300_set_viewport_state;
847
848     r300->context.set_vertex_buffers = r300_set_vertex_buffers;
849     r300->context.set_vertex_elements = r300_set_vertex_elements;
850
851     r300->context.create_vs_state = r300_create_vs_state;
852     r300->context.bind_vs_state = r300_bind_vs_state;
853     r300->context.delete_vs_state = r300_delete_vs_state;
854 }