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