2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
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:
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
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. */
23 #include "util/u_math.h"
24 #include "util/u_pack_color.h"
25 #include "pipe/p_debug.h"
27 #include "r300_context.h"
30 /* r300_state: Functions used to intialize state context by translating
31 * Gallium state objects into semi-native r300 state objects.
33 * XXX break this file up into pieces if it gets too big! */
35 /* Pack a float into a dword. */
36 static uint32_t pack_float_32(float f
)
47 static uint32_t translate_blend_function(int blend_func
) {
50 return R300_COMB_FCN_ADD_CLAMP
;
51 case PIPE_BLEND_SUBTRACT
:
52 return R300_COMB_FCN_SUB_CLAMP
;
53 case PIPE_BLEND_REVERSE_SUBTRACT
:
54 return R300_COMB_FCN_RSUB_CLAMP
;
56 return R300_COMB_FCN_MIN
;
58 return R300_COMB_FCN_MAX
;
60 debug_printf("r300: Unknown blend function %d\n", blend_func
);
66 /* XXX we can also offer the D3D versions of some of these... */
67 static uint32_t translate_blend_factor(int blend_fact
) {
69 case PIPE_BLENDFACTOR_ONE
:
70 return R300_BLEND_GL_ONE
;
71 case PIPE_BLENDFACTOR_SRC_COLOR
:
72 return R300_BLEND_GL_SRC_COLOR
;
73 case PIPE_BLENDFACTOR_SRC_ALPHA
:
74 return R300_BLEND_GL_SRC_ALPHA
;
75 case PIPE_BLENDFACTOR_DST_ALPHA
:
76 return R300_BLEND_GL_DST_ALPHA
;
77 case PIPE_BLENDFACTOR_DST_COLOR
:
78 return R300_BLEND_GL_DST_COLOR
;
79 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
80 return R300_BLEND_GL_SRC_ALPHA_SATURATE
;
81 case PIPE_BLENDFACTOR_CONST_COLOR
:
82 return R300_BLEND_GL_CONST_COLOR
;
83 case PIPE_BLENDFACTOR_CONST_ALPHA
:
84 return R300_BLEND_GL_CONST_ALPHA
;
86 case PIPE_BLENDFACTOR_SRC1_COLOR:
87 case PIPE_BLENDFACTOR_SRC1_ALPHA: */
88 case PIPE_BLENDFACTOR_ZERO
:
89 return R300_BLEND_GL_ZERO
;
90 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
91 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR
;
92 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
93 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA
;
94 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
95 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA
;
96 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
97 return R300_BLEND_GL_ONE_MINUS_DST_COLOR
;
98 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
99 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR
;
100 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
101 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA
;
103 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
104 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
106 debug_printf("r300: Unknown blend factor %d\n", blend_fact
);
112 /* Create a new blend state based on the CSO blend state.
114 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
115 static void* r300_create_blend_state(struct pipe_context
* pipe
,
116 const struct pipe_blend_state
* state
)
118 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
120 if (state
->blend_enable
) {
121 /* XXX for now, always do separate alpha...
122 * is it faster to do it with one reg? */
123 blend
->blend_control
= R300_ALPHA_BLEND_ENABLE
|
124 R300_SEPARATE_ALPHA_ENABLE
|
126 translate_blend_function(state
->rgb_func
) |
127 (translate_blend_factor(state
->rgb_src_factor
) <<
128 R300_SRC_BLEND_SHIFT
) |
129 (translate_blend_factor(state
->rgb_dst_factor
) <<
130 R300_DST_BLEND_SHIFT
);
131 blend
->alpha_blend_control
=
132 translate_blend_function(state
->alpha_func
) |
133 (translate_blend_factor(state
->alpha_src_factor
) <<
134 R300_SRC_BLEND_SHIFT
) |
135 (translate_blend_factor(state
->alpha_dst_factor
) <<
136 R300_DST_BLEND_SHIFT
);
139 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
140 /* XXX are logicops still allowed if blending's disabled?
141 * Does Gallium take care of it for us? */
142 if (state
->logicop_enable
) {
143 blend
->rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
144 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
148 blend
->dither
= R300_RB3D_DITHER_CTL_DITHER_MODE_LUT
|
149 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT
;
155 /* Bind blend state. */
156 static void r300_bind_blend_state(struct pipe_context
* pipe
,
159 struct r300_context
* r300
= r300_context(pipe
);
161 r300
->blend_state
= (struct r300_blend_state
*)state
;
162 r300
->dirty_state
|= R300_NEW_BLEND
;
165 /* Free blend state. */
166 static void r300_delete_blend_state(struct pipe_context
* pipe
,
173 * Setup both R300 and R500 registers, figure out later which one to write. */
174 static void r300_set_blend_color(struct pipe_context
* pipe
,
175 const struct pipe_blend_color
* color
)
177 struct r300_context
* r300
= r300_context(pipe
);
179 ubyte ur
, ug
, ub
, ua
;
181 r
= util_iround(color
->color
[0] * 1023.0f
);
182 g
= util_iround(color
->color
[1] * 1023.0f
);
183 b
= util_iround(color
->color
[2] * 1023.0f
);
184 a
= util_iround(color
->color
[3] * 1023.0f
);
186 ur
= float_to_ubyte(color
->color
[0]);
187 ug
= float_to_ubyte(color
->color
[1]);
188 ub
= float_to_ubyte(color
->color
[2]);
189 ua
= float_to_ubyte(color
->color
[3]);
191 r300
->blend_color_state
->blend_color
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
193 r300
->blend_color_state
->blend_color_red_alpha
= ur
| (ua
<< 16);
194 r300
->blend_color_state
->blend_color_green_blue
= ub
| (ug
<< 16);
196 r300
->dirty_state
|= R300_NEW_BLEND_COLOR
;
199 static void r300_set_clip_state(struct pipe_context
* pipe
,
200 const struct pipe_clip_state
* state
)
202 struct r300_context
* r300
= r300_context(pipe
);
204 draw_flush(r300
->draw
);
205 draw_set_clip_state(r300
->draw
, state
);
209 r300_set_constant_buffer(struct pipe_context
* pipe
,
210 uint shader
, uint index
,
211 const struct pipe_constant_buffer
* buffer
)
216 static uint32_t translate_depth_stencil_function(int zs_func
) {
218 case PIPE_FUNC_NEVER
:
219 return R300_ZS_NEVER
;
222 case PIPE_FUNC_EQUAL
:
223 return R300_ZS_EQUAL
;
224 case PIPE_FUNC_LEQUAL
:
225 return R300_ZS_LEQUAL
;
226 case PIPE_FUNC_GREATER
:
227 return R300_ZS_GREATER
;
228 case PIPE_FUNC_NOTEQUAL
:
229 return R300_ZS_NOTEQUAL
;
230 case PIPE_FUNC_GEQUAL
:
231 return R300_ZS_GEQUAL
;
232 case PIPE_FUNC_ALWAYS
:
233 return R300_ZS_ALWAYS
;
235 debug_printf("r300: Unknown depth/stencil function %d\n",
242 static uint32_t translate_stencil_op(int s_op
) {
244 case PIPE_STENCIL_OP_KEEP
:
246 case PIPE_STENCIL_OP_ZERO
:
248 case PIPE_STENCIL_OP_REPLACE
:
249 return R300_ZS_REPLACE
;
250 case PIPE_STENCIL_OP_INCR
:
252 case PIPE_STENCIL_OP_DECR
:
254 case PIPE_STENCIL_OP_INCR_WRAP
:
255 return R300_ZS_INCR_WRAP
;
256 case PIPE_STENCIL_OP_DECR_WRAP
:
257 return R300_ZS_DECR_WRAP
;
258 case PIPE_STENCIL_OP_INVERT
:
259 return R300_ZS_INVERT
;
261 debug_printf("r300: Unknown stencil op %d", s_op
);
267 static uint32_t translate_alpha_function(int alpha_func
) {
268 switch (alpha_func
) {
269 case PIPE_FUNC_NEVER
:
270 return R300_FG_ALPHA_FUNC_NEVER
;
272 return R300_FG_ALPHA_FUNC_LESS
;
273 case PIPE_FUNC_EQUAL
:
274 return R300_FG_ALPHA_FUNC_EQUAL
;
275 case PIPE_FUNC_LEQUAL
:
276 return R300_FG_ALPHA_FUNC_LE
;
277 case PIPE_FUNC_GREATER
:
278 return R300_FG_ALPHA_FUNC_GREATER
;
279 case PIPE_FUNC_NOTEQUAL
:
280 return R300_FG_ALPHA_FUNC_NOTEQUAL
;
281 case PIPE_FUNC_GEQUAL
:
282 return R300_FG_ALPHA_FUNC_GE
;
283 case PIPE_FUNC_ALWAYS
:
284 return R300_FG_ALPHA_FUNC_ALWAYS
;
286 debug_printf("r300: Unknown alpha function %d", alpha_func
);
292 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
294 * This contains the depth buffer, stencil buffer, alpha test, and such.
295 * On the Radeon, depth and stencil buffer setup are intertwined, which is
296 * the reason for some of the strange-looking assignments across registers. */
298 r300_create_dsa_state(struct pipe_context
* pipe
,
299 const struct pipe_depth_stencil_alpha_state
* state
)
301 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
303 /* Depth test setup. */
304 if (state
->depth
.enabled
) {
305 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
307 if (state
->depth
.writemask
) {
308 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
311 dsa
->z_stencil_control
|=
312 (translate_depth_stencil_function(state
->depth
.func
) <<
316 /* Stencil buffer setup. */
317 if (state
->stencil
[0].enabled
) {
318 dsa
->z_buffer_control
|= R300_STENCIL_ENABLE
;
319 dsa
->z_stencil_control
|=
320 (translate_depth_stencil_function(state
->stencil
[0].func
) <<
321 R300_S_FRONT_FUNC_SHIFT
) |
322 (translate_stencil_op(state
->stencil
[0].fail_op
) <<
323 R300_S_FRONT_SFAIL_OP_SHIFT
) |
324 (translate_stencil_op(state
->stencil
[0].zpass_op
) <<
325 R300_S_FRONT_ZPASS_OP_SHIFT
) |
326 (translate_stencil_op(state
->stencil
[0].zfail_op
) <<
327 R300_S_FRONT_ZFAIL_OP_SHIFT
);
329 dsa
->stencil_ref_mask
= (state
->stencil
[0].ref_value
) |
330 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
331 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
333 if (state
->stencil
[1].enabled
) {
334 dsa
->z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
335 dsa
->z_stencil_control
|=
336 (translate_depth_stencil_function(state
->stencil
[1].func
) <<
337 R300_S_BACK_FUNC_SHIFT
) |
338 (translate_stencil_op(state
->stencil
[1].fail_op
) <<
339 R300_S_BACK_SFAIL_OP_SHIFT
) |
340 (translate_stencil_op(state
->stencil
[1].zpass_op
) <<
341 R300_S_BACK_ZPASS_OP_SHIFT
) |
342 (translate_stencil_op(state
->stencil
[1].zfail_op
) <<
343 R300_S_BACK_ZFAIL_OP_SHIFT
);
345 dsa
->stencil_ref_bf
= (state
->stencil
[1].ref_value
) |
346 (state
->stencil
[1].valuemask
<< R300_STENCILMASK_SHIFT
) |
347 (state
->stencil
[1].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
351 /* Alpha test setup. */
352 if (state
->alpha
.enabled
) {
353 dsa
->alpha_function
= translate_alpha_function(state
->alpha
.func
) |
354 R300_FG_ALPHA_FUNC_ENABLE
;
355 dsa
->alpha_reference
= CLAMP(state
->alpha
.ref_value
* 1023.0f
,
358 dsa
->z_buffer_top
= R300_ZTOP_ENABLE
;
364 /* Bind DSA state. */
365 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
368 struct r300_context
* r300
= r300_context(pipe
);
370 r300
->dsa_state
= (struct r300_dsa_state
*)state
;
371 r300
->dirty_state
|= R300_NEW_DSA
;
374 /* Free DSA state. */
375 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
381 static void r300_set_edgeflags(struct pipe_context
* pipe
,
382 const unsigned* bitfield
)
384 /* XXX you know it's bad when i915 has this blank too */
388 r300_set_framebuffer_state(struct pipe_context
* pipe
,
389 const struct pipe_framebuffer_state
* state
)
391 struct r300_context
* r300
= r300_context(pipe
);
393 draw_flush(r300
->draw
);
395 r300
->framebuffer_state
= *state
;
397 r300
->dirty_state
|= R300_NEW_FRAMEBUFFERS
;
400 /* Create fragment shader state. */
401 static void* r300_create_fs_state(struct pipe_context
* pipe
,
402 const struct pipe_shader_state
* shader
)
404 struct r300_context
* r300
= r300_context(pipe
);
405 struct r3xx_fragment_shader
* fs
= NULL
;
407 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
409 (struct r3xx_fragment_shader
*)CALLOC_STRUCT(r500_fragment_shader
);
412 (struct r3xx_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
415 /* Copy state directly into shader. */
421 /* Bind fragment shader state. */
422 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
424 struct r300_context
* r300
= r300_context(pipe
);
425 struct r3xx_fragment_shader
* fs
= (struct r3xx_fragment_shader
*)shader
;
427 if (!fs
->translated
) {
428 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
429 r500_translate_shader(r300
, fs
);
431 r300_translate_shader(r300
, fs
);
437 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
;
440 /* Delete fragment shader state. */
441 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
446 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
447 const struct pipe_poly_stipple
* state
)
452 static INLINE
int pack_float_16_6x(float f
) {
453 return ((int)(f
* 6.0) & 0xffff);
456 /* Create a new rasterizer state based on the CSO rasterizer state.
458 * This is a very large chunk of state, and covers most of the graphics
459 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
461 * In a not entirely unironic sidenote, this state has nearly nothing to do
462 * with the actual block on the Radeon called the rasterizer (RS). */
463 static void* r300_create_rs_state(struct pipe_context
* pipe
,
464 const struct pipe_rasterizer_state
* state
)
466 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
468 /* XXX this is part of HW TCL */
469 /* XXX endian control */
470 rs
->vap_control_status
= R300_VAP_TCL_BYPASS
;
472 rs
->point_size
= pack_float_16_6x(state
->point_size
) |
473 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
475 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
476 R300_GA_LINE_CNTL_END_TYPE_COMP
;
478 /* Radeons don't think in "CW/CCW", they think in "front/back". */
479 if (state
->front_winding
== PIPE_WINDING_CW
) {
480 rs
->cull_mode
= R300_FRONT_FACE_CW
;
482 if (state
->offset_cw
) {
483 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
485 if (state
->offset_ccw
) {
486 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
489 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
491 if (state
->offset_ccw
) {
492 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
494 if (state
->offset_cw
) {
495 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
498 if (state
->front_winding
& state
->cull_mode
) {
499 rs
->cull_mode
|= R300_CULL_FRONT
;
501 if (~(state
->front_winding
) & state
->cull_mode
) {
502 rs
->cull_mode
|= R300_CULL_BACK
;
505 if (rs
->polygon_offset_enable
) {
506 rs
->depth_offset_front
= rs
->depth_offset_back
=
507 pack_float_32(state
->offset_units
);
508 rs
->depth_scale_front
= rs
->depth_scale_back
=
509 pack_float_32(state
->offset_scale
);
512 if (state
->line_stipple_enable
) {
513 rs
->line_stipple_config
=
514 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
515 (pack_float_32((float)state
->line_stipple_factor
) &
516 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
517 /* XXX this might need to be scaled up */
518 rs
->line_stipple_value
= state
->line_stipple_pattern
;
524 /* Bind rasterizer state. */
525 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
527 struct r300_context
* r300
= r300_context(pipe
);
529 r300
->rs_state
= (struct r300_rs_state
*)state
;
530 r300
->dirty_state
|= R300_NEW_RASTERIZER
;
533 /* Free rasterizer state. */
534 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
539 static uint32_t translate_wrap(int wrap
) {
541 case PIPE_TEX_WRAP_REPEAT
:
542 return R300_TX_REPEAT
;
543 case PIPE_TEX_WRAP_CLAMP
:
544 return R300_TX_CLAMP
;
545 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
546 return R300_TX_CLAMP_TO_EDGE
;
547 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
548 return R300_TX_CLAMP_TO_BORDER
;
549 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
550 return R300_TX_REPEAT
| R300_TX_MIRRORED
;
551 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
552 return R300_TX_CLAMP
| R300_TX_MIRRORED
;
553 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
554 return R300_TX_CLAMP_TO_EDGE
| R300_TX_MIRRORED
;
555 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
556 return R300_TX_CLAMP_TO_EDGE
| R300_TX_MIRRORED
;
558 debug_printf("r300: Unknown texture wrap %d", wrap
);
563 static uint32_t translate_tex_filters(int min
, int mag
, int mip
) {
566 case PIPE_TEX_FILTER_NEAREST
:
567 retval
|= R300_TX_MIN_FILTER_NEAREST
;
568 case PIPE_TEX_FILTER_LINEAR
:
569 retval
|= R300_TX_MIN_FILTER_LINEAR
;
570 case PIPE_TEX_FILTER_ANISO
:
571 retval
|= R300_TX_MIN_FILTER_ANISO
;
573 debug_printf("r300: Unknown texture filter %d", min
);
577 case PIPE_TEX_FILTER_NEAREST
:
578 retval
|= R300_TX_MAG_FILTER_NEAREST
;
579 case PIPE_TEX_FILTER_LINEAR
:
580 retval
|= R300_TX_MAG_FILTER_LINEAR
;
581 case PIPE_TEX_FILTER_ANISO
:
582 retval
|= R300_TX_MAG_FILTER_ANISO
;
584 debug_printf("r300: Unknown texture filter %d", mag
);
588 case PIPE_TEX_MIPFILTER_NONE
:
589 retval
|= R300_TX_MIN_FILTER_MIP_NONE
;
590 case PIPE_TEX_MIPFILTER_NEAREST
:
591 retval
|= R300_TX_MIN_FILTER_MIP_NEAREST
;
592 case PIPE_TEX_MIPFILTER_LINEAR
:
593 retval
|= R300_TX_MIN_FILTER_MIP_LINEAR
;
595 debug_printf("r300: Unknown texture filter %d", mip
);
602 static uint32_t anisotropy(float max_aniso
) {
603 if (max_aniso
>= 16.0f
) {
604 return R300_TX_MAX_ANISO_16_TO_1
;
605 } else if (max_aniso
>= 8.0f
) {
606 return R300_TX_MAX_ANISO_8_TO_1
;
607 } else if (max_aniso
>= 4.0f
) {
608 return R300_TX_MAX_ANISO_4_TO_1
;
609 } else if (max_aniso
>= 2.0f
) {
610 return R300_TX_MAX_ANISO_2_TO_1
;
612 return R300_TX_MAX_ANISO_1_TO_1
;
617 r300_create_sampler_state(struct pipe_context
* pipe
,
618 const struct pipe_sampler_state
* state
)
620 struct r300_context
* r300
= r300_context(pipe
);
621 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
625 (translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
626 (translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
627 (translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
629 sampler
->filter0
|= translate_tex_filters(state
->min_img_filter
,
630 state
->mag_img_filter
,
631 state
->min_mip_filter
);
633 lod_bias
= CLAMP((int)(state
->lod_bias
* 32), -(1 << 9), (1 << 9) - 1);
635 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
637 sampler
->filter1
|= anisotropy(state
->max_anisotropy
);
639 util_pack_color(state
->border_color
, PIPE_FORMAT_A8R8G8B8_UNORM
,
640 &sampler
->border_color
);
642 /* R500-specific fixups and optimizations */
643 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
644 sampler
->filter1
|= R500_BORDER_FIX
;
647 return (void*)sampler
;
650 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
654 struct r300_context
* r300
= r300_context(pipe
);
661 for (i
= 0; i
< count
; i
++) {
662 if (r300
->sampler_states
[i
] != states
[i
]) {
663 r300
->sampler_states
[i
] = (struct r300_sampler_state
*)states
[i
];
664 r300
->dirty_state
|= (R300_NEW_SAMPLER
<< i
);
668 r300
->sampler_count
= count
;
671 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
676 static void r300_set_sampler_textures(struct pipe_context
* pipe
,
678 struct pipe_texture
** texture
)
680 struct r300_context
* r300
= r300_context(pipe
);
688 for (i
= 0; i
< count
; i
++) {
689 if (r300
->textures
[i
] != (struct r300_texture
*)texture
[i
]) {
690 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
692 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
696 for (i
= count
; i
< 8; i
++) {
697 if (r300
->textures
[i
]) {
698 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
700 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
704 r300
->texture_count
= count
;
707 static void r300_set_scissor_state(struct pipe_context
* pipe
,
708 const struct pipe_scissor_state
* state
)
710 struct r300_context
* r300
= r300_context(pipe
);
711 draw_flush(r300
->draw
);
713 uint32_t left
, top
, right
, bottom
;
715 /* So, a bit of info. The scissors are offset by R300_SCISSORS_OFFSET in
716 * both directions for all values, and can only be 13 bits wide. Why?
717 * We may never know. */
718 left
= (state
->minx
+ R300_SCISSORS_OFFSET
) & 0x1fff;
719 top
= (state
->miny
+ R300_SCISSORS_OFFSET
) & 0x1fff;
720 right
= (state
->maxx
+ R300_SCISSORS_OFFSET
) & 0x1fff;
721 bottom
= (state
->maxy
+ R300_SCISSORS_OFFSET
) & 0x1fff;
723 r300
->scissor_state
->scissor_top_left
= (left
<< R300_SCISSORS_X_SHIFT
) |
724 (top
<< R300_SCISSORS_Y_SHIFT
);
725 r300
->scissor_state
->scissor_bottom_right
=
726 (right
<< R300_SCISSORS_X_SHIFT
) | (bottom
<< R300_SCISSORS_Y_SHIFT
);
728 r300
->dirty_state
|= R300_NEW_SCISSOR
;
731 static void r300_set_viewport_state(struct pipe_context
* pipe
,
732 const struct pipe_viewport_state
* state
)
734 struct r300_context
* r300
= r300_context(pipe
);
735 /* XXX handing this off to Draw for now */
736 draw_set_viewport_state(r300
->draw
, state
);
739 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
741 const struct pipe_vertex_buffer
* buffers
)
743 struct r300_context
* r300
= r300_context(pipe
);
745 draw_flush(r300
->draw
);
746 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
749 static void r300_set_vertex_elements(struct pipe_context
* pipe
,
751 const struct pipe_vertex_element
* elements
)
753 struct r300_context
* r300
= r300_context(pipe
);
755 draw_flush(r300
->draw
);
756 draw_set_vertex_elements(r300
->draw
, count
, elements
);
759 static void* r300_create_vs_state(struct pipe_context
* pipe
,
760 const struct pipe_shader_state
* state
)
762 struct r300_context
* context
= r300_context(pipe
);
763 /* XXX handing this off to Draw for now */
764 return draw_create_vertex_shader(context
->draw
, state
);
767 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* state
) {
768 struct r300_context
* context
= r300_context(pipe
);
769 /* XXX handing this off to Draw for now */
770 draw_bind_vertex_shader(context
->draw
, (struct draw_vertex_shader
*)state
);
773 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* state
)
775 struct r300_context
* context
= r300_context(pipe
);
776 /* XXX handing this off to Draw for now */
777 draw_delete_vertex_shader(context
->draw
, (struct draw_vertex_shader
*)state
);
780 void r300_init_state_functions(struct r300_context
* r300
)
782 r300
->context
.create_blend_state
= r300_create_blend_state
;
783 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
784 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
786 r300
->context
.set_blend_color
= r300_set_blend_color
;
788 r300
->context
.set_clip_state
= r300_set_clip_state
;
790 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
792 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
793 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
794 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
796 r300
->context
.set_edgeflags
= r300_set_edgeflags
;
798 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
800 r300
->context
.create_fs_state
= r300_create_fs_state
;
801 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
802 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
804 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
806 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
807 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
808 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
810 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
811 r300
->context
.bind_sampler_states
= r300_bind_sampler_states
;
812 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
814 r300
->context
.set_sampler_textures
= r300_set_sampler_textures
;
816 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
818 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
820 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
821 r300
->context
.set_vertex_elements
= r300_set_vertex_elements
;
823 r300
->context
.create_vs_state
= r300_create_vs_state
;
824 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
825 r300
->context
.delete_vs_state
= r300_delete_vs_state
;