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"
26 #include "pipe/p_debug.h"
27 #include "pipe/internal/p_winsys_screen.h"
29 #include "r300_context.h"
31 #include "r300_state_shader.h"
33 /* r300_state: Functions used to intialize state context by translating
34 * Gallium state objects into semi-native r300 state objects.
36 * XXX break this file up into pieces if it gets too big! */
38 /* Pack a float into a dword. */
39 static uint32_t pack_float_32(float f)
50 static uint32_t translate_blend_function(int blend_func) {
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;
59 return R300_COMB_FCN_MIN;
61 return R300_COMB_FCN_MAX;
63 debug_printf("r300: Unknown blend function %d\n", blend_func);
69 /* XXX we can also offer the D3D versions of some of these... */
70 static uint32_t translate_blend_factor(int 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;
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;
106 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
107 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
109 debug_printf("r300: Unknown blend factor %d\n", blend_fact);
115 /* Create a new blend state based on the CSO blend state.
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)
121 struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
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 |
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);
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;
151 blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
152 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
158 /* Bind blend state. */
159 static void r300_bind_blend_state(struct pipe_context* pipe,
162 struct r300_context* r300 = r300_context(pipe);
164 r300->blend_state = (struct r300_blend_state*)state;
165 r300->dirty_state |= R300_NEW_BLEND;
168 /* Free blend state. */
169 static void r300_delete_blend_state(struct pipe_context* pipe,
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)
180 struct r300_context* r300 = r300_context(pipe);
182 ubyte ur, ug, ub, ua;
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);
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]);
194 r300->blend_color_state->blend_color = (a << 24) | (r << 16) | (g << 8) | b;
196 r300->blend_color_state->blend_color_red_alpha = ur | (ua << 16);
197 r300->blend_color_state->blend_color_green_blue = ub | (ug << 16);
199 r300->dirty_state |= R300_NEW_BLEND_COLOR;
202 static void r300_set_clip_state(struct pipe_context* pipe,
203 const struct pipe_clip_state* state)
205 struct r300_context* r300 = r300_context(pipe);
207 draw_flush(r300->draw);
208 draw_set_clip_state(r300->draw, state);
212 r300_set_constant_buffer(struct pipe_context* pipe,
213 uint shader, uint index,
214 const struct pipe_constant_buffer* buffer)
216 struct r300_context* r300 = r300_context(pipe);
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);
227 r300->shader_constants[shader].user_count =
228 buffer->buffer->size / (sizeof(float) * 4);
230 r300->shader_constants[shader].user_count = 0;
233 r300->dirty_state |= R300_NEW_CONSTANTS;
236 static uint32_t translate_depth_stencil_function(int zs_func) {
238 case PIPE_FUNC_NEVER:
239 return R300_ZS_NEVER;
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;
255 debug_printf("r300: Unknown depth/stencil function %d\n",
262 static uint32_t translate_stencil_op(int s_op) {
264 case PIPE_STENCIL_OP_KEEP:
266 case PIPE_STENCIL_OP_ZERO:
268 case PIPE_STENCIL_OP_REPLACE:
269 return R300_ZS_REPLACE;
270 case PIPE_STENCIL_OP_INCR:
272 case PIPE_STENCIL_OP_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;
281 debug_printf("r300: Unknown stencil op %d", s_op);
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;
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;
306 debug_printf("r300: Unknown alpha function %d", alpha_func);
312 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
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. */
318 r300_create_dsa_state(struct pipe_context* pipe,
319 const struct pipe_depth_stencil_alpha_state* state)
321 struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
323 /* Depth test setup. */
324 if (state->depth.enabled) {
325 dsa->z_buffer_control |= R300_Z_ENABLE;
327 if (state->depth.writemask) {
328 dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
331 dsa->z_stencil_control |=
332 (translate_depth_stencil_function(state->depth.func) <<
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);
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);
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);
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);
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,
378 dsa->z_buffer_top = R300_ZTOP_ENABLE;
384 /* Bind DSA state. */
385 static void r300_bind_dsa_state(struct pipe_context* pipe,
388 struct r300_context* r300 = r300_context(pipe);
390 r300->dsa_state = (struct r300_dsa_state*)state;
391 r300->dirty_state |= R300_NEW_DSA;
394 /* Free DSA state. */
395 static void r300_delete_dsa_state(struct pipe_context* pipe,
401 static void r300_set_edgeflags(struct pipe_context* pipe,
402 const unsigned* bitfield)
404 /* XXX you know it's bad when i915 has this blank too */
408 r300_set_framebuffer_state(struct pipe_context* pipe,
409 const struct pipe_framebuffer_state* state)
411 struct r300_context* r300 = r300_context(pipe);
413 draw_flush(r300->draw);
415 r300->framebuffer_state = *state;
417 r300->dirty_state |= R300_NEW_FRAMEBUFFERS;
420 /* Create fragment shader state. */
421 static void* r300_create_fs_state(struct pipe_context* pipe,
422 const struct pipe_shader_state* shader)
424 struct r300_context* r300 = r300_context(pipe);
425 struct r3xx_fragment_shader* fs = NULL;
427 if (r300_screen(r300->context.screen)->caps->is_r500) {
429 (struct r3xx_fragment_shader*)CALLOC_STRUCT(r500_fragment_shader);
432 (struct r3xx_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
435 /* Copy state directly into shader. */
441 /* Bind fragment shader state. */
442 static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
444 struct r300_context* r300 = r300_context(pipe);
445 struct r3xx_fragment_shader* fs = (struct r3xx_fragment_shader*)shader;
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);
454 r300_translate_fragment_shader(r300, (struct r300_fragment_shader*)fs);
458 fs->translated = true;
461 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER;
464 /* Delete fragment shader state. */
465 static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
470 static void r300_set_polygon_stipple(struct pipe_context* pipe,
471 const struct pipe_poly_stipple* state)
476 static INLINE int pack_float_16_6x(float f) {
477 return ((int)(f * 6.0) & 0xffff);
480 /* Create a new rasterizer state based on the CSO rasterizer state.
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.
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)
490 struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
492 /* XXX this is part of HW TCL */
493 /* XXX endian control */
494 rs->vap_control_status = R300_VAP_TCL_BYPASS;
496 rs->point_size = pack_float_16_6x(state->point_size) |
497 (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
499 rs->line_control = pack_float_16_6x(state->line_width) |
500 R300_GA_LINE_CNTL_END_TYPE_COMP;
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;
506 if (state->offset_cw) {
507 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
509 if (state->offset_ccw) {
510 rs->polygon_offset_enable |= R300_BACK_ENABLE;
513 rs->cull_mode = R300_FRONT_FACE_CCW;
515 if (state->offset_ccw) {
516 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
518 if (state->offset_cw) {
519 rs->polygon_offset_enable |= R300_BACK_ENABLE;
522 if (state->front_winding & state->cull_mode) {
523 rs->cull_mode |= R300_CULL_FRONT;
525 if (~(state->front_winding) & state->cull_mode) {
526 rs->cull_mode |= R300_CULL_BACK;
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);
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;
550 /* Bind rasterizer state. */
551 static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
553 struct r300_context* r300 = r300_context(pipe);
554 struct r300_rs_state* rs = (struct r300_rs_state*)state;
556 draw_set_rasterizer_state(r300->draw, &rs->rs);
559 r300->dirty_state |= R300_NEW_RASTERIZER;
562 /* Free rasterizer state. */
563 static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
568 static uint32_t translate_wrap(int 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;
587 debug_printf("r300: Unknown texture wrap %d", wrap);
592 static uint32_t translate_tex_filters(int min, int mag, int mip) {
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;
602 debug_printf("r300: Unknown texture filter %d", min);
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;
613 debug_printf("r300: Unknown texture filter %d", mag);
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;
624 debug_printf("r300: Unknown texture filter %d", mip);
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;
641 return R300_TX_MAX_ANISO_1_TO_1;
646 r300_create_sampler_state(struct pipe_context* pipe,
647 const struct pipe_sampler_state* state)
649 struct r300_context* r300 = r300_context(pipe);
650 struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
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);
658 sampler->filter0 |= translate_tex_filters(state->min_img_filter,
659 state->mag_img_filter,
660 state->min_mip_filter);
662 lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
664 sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
666 sampler->filter1 |= anisotropy(state->max_anisotropy);
668 util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM,
669 &sampler->border_color);
671 /* R500-specific fixups and optimizations */
672 if (r300_screen(r300->context.screen)->caps->is_r500) {
673 sampler->filter1 |= R500_BORDER_FIX;
676 return (void*)sampler;
679 static void r300_bind_sampler_states(struct pipe_context* pipe,
683 struct r300_context* r300 = r300_context(pipe);
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);
697 r300->sampler_count = count;
700 static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
705 static void r300_set_sampler_textures(struct pipe_context* pipe,
707 struct pipe_texture** texture)
709 struct r300_context* r300 = r300_context(pipe);
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],
721 r300->dirty_state |= (R300_NEW_TEXTURE << i);
725 for (i = count; i < 8; i++) {
726 if (r300->textures[i]) {
727 pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
729 r300->dirty_state |= (R300_NEW_TEXTURE << i);
733 r300->texture_count = count;
736 static void r300_set_scissor_state(struct pipe_context* pipe,
737 const struct pipe_scissor_state* state)
739 struct r300_context* r300 = r300_context(pipe);
740 draw_flush(r300->draw);
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);
749 r300->dirty_state |= R300_NEW_SCISSOR;
752 static void r300_set_viewport_state(struct pipe_context* pipe,
753 const struct pipe_viewport_state* state)
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);
760 static void r300_set_vertex_buffers(struct pipe_context* pipe,
762 const struct pipe_vertex_buffer* buffers)
764 struct r300_context* r300 = r300_context(pipe);
766 memcpy(r300->vertex_buffers, buffers,
767 sizeof(struct pipe_vertex_buffer) * count);
769 r300->vertex_buffer_count = count;
771 draw_flush(r300->draw);
772 draw_set_vertex_buffers(r300->draw, count, buffers);
775 static void r300_set_vertex_elements(struct pipe_context* pipe,
777 const struct pipe_vertex_element* elements)
779 struct r300_context* r300 = r300_context(pipe);
781 draw_flush(r300->draw);
782 draw_set_vertex_elements(r300->draw, count, elements);
785 static void* r300_create_vs_state(struct pipe_context* pipe,
786 const struct pipe_shader_state* state)
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);
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);
799 static void r300_delete_vs_state(struct pipe_context* pipe, void* state)
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);
806 void r300_init_state_functions(struct r300_context* r300)
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;
812 r300->context.set_blend_color = r300_set_blend_color;
814 r300->context.set_clip_state = r300_set_clip_state;
816 r300->context.set_constant_buffer = r300_set_constant_buffer;
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;
822 r300->context.set_edgeflags = r300_set_edgeflags;
824 r300->context.set_framebuffer_state = r300_set_framebuffer_state;
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;
830 r300->context.set_polygon_stipple = r300_set_polygon_stipple;
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;
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;
840 r300->context.set_sampler_textures = r300_set_sampler_textures;
842 r300->context.set_scissor_state = r300_set_scissor_state;
844 r300->context.set_viewport_state = r300_set_viewport_state;
846 r300->context.set_vertex_buffers = r300_set_vertex_buffers;
847 r300->context.set_vertex_elements = r300_set_vertex_elements;
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;