r300_state.c revision 63cb6f59eac91ba34cf80ff3736568e40b094fe1
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 34#include "r300_context.h" 35#include "r300_reg.h" 36#include "r300_screen.h" 37#include "r300_state_inlines.h" 38#include "r300_fs.h" 39#include "r300_vs.h" 40 41#include "radeon_winsys.h" 42 43/* r300_state: Functions used to intialize state context by translating 44 * Gallium state objects into semi-native r300 state objects. */ 45 46static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA, 47 unsigned dstRGB, unsigned dstA) 48{ 49 /* If the blend equation is ADD or REVERSE_SUBTRACT, 50 * SRC_ALPHA == 0, and the following state is set, the colorbuffer 51 * will not be changed. 52 * Notice that the dst factors are the src factors inverted. */ 53 return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA || 54 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE || 55 srcRGB == PIPE_BLENDFACTOR_ZERO) && 56 (srcA == PIPE_BLENDFACTOR_SRC_COLOR || 57 srcA == PIPE_BLENDFACTOR_SRC_ALPHA || 58 srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE || 59 srcA == PIPE_BLENDFACTOR_ZERO) && 60 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 61 dstRGB == PIPE_BLENDFACTOR_ONE) && 62 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR || 63 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 64 dstA == PIPE_BLENDFACTOR_ONE); 65} 66 67static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA, 68 unsigned dstRGB, unsigned dstA) 69{ 70 /* If the blend equation is ADD or REVERSE_SUBTRACT, 71 * SRC_ALPHA == 1, and the following state is set, the colorbuffer 72 * will not be changed. 73 * Notice that the dst factors are the src factors inverted. */ 74 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 75 srcRGB == PIPE_BLENDFACTOR_ZERO) && 76 (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR || 77 srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 78 srcA == PIPE_BLENDFACTOR_ZERO) && 79 (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA || 80 dstRGB == PIPE_BLENDFACTOR_ONE) && 81 (dstA == PIPE_BLENDFACTOR_SRC_COLOR || 82 dstA == PIPE_BLENDFACTOR_SRC_ALPHA || 83 dstA == PIPE_BLENDFACTOR_ONE); 84} 85 86static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA, 87 unsigned dstRGB, unsigned dstA) 88{ 89 /* If the blend equation is ADD or REVERSE_SUBTRACT, 90 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer 91 * will not be changed. 92 * Notice that the dst factors are the src factors inverted. */ 93 return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR || 94 srcRGB == PIPE_BLENDFACTOR_ZERO) && 95 (srcA == PIPE_BLENDFACTOR_ZERO) && 96 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR || 97 dstRGB == PIPE_BLENDFACTOR_ONE) && 98 (dstA == PIPE_BLENDFACTOR_ONE); 99} 100 101static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA, 102 unsigned dstRGB, unsigned dstA) 103{ 104 /* If the blend equation is ADD or REVERSE_SUBTRACT, 105 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer 106 * will not be changed. 107 * Notice that the dst factors are the src factors inverted. */ 108 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR || 109 srcRGB == PIPE_BLENDFACTOR_ZERO) && 110 (srcA == PIPE_BLENDFACTOR_ZERO) && 111 (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR || 112 dstRGB == PIPE_BLENDFACTOR_ONE) && 113 (dstA == PIPE_BLENDFACTOR_ONE); 114} 115 116static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA, 117 unsigned dstRGB, unsigned dstA) 118{ 119 /* If the blend equation is ADD or REVERSE_SUBTRACT, 120 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set, 121 * the colorbuffer will not be changed. 122 * Notice that the dst factors are the src factors inverted. */ 123 return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR || 124 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA || 125 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE || 126 srcRGB == PIPE_BLENDFACTOR_ZERO) && 127 (srcA == PIPE_BLENDFACTOR_SRC_COLOR || 128 srcA == PIPE_BLENDFACTOR_SRC_ALPHA || 129 srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE || 130 srcA == PIPE_BLENDFACTOR_ZERO) && 131 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR || 132 dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 133 dstRGB == PIPE_BLENDFACTOR_ONE) && 134 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR || 135 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 136 dstA == PIPE_BLENDFACTOR_ONE); 137} 138 139static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA, 140 unsigned dstRGB, unsigned dstA) 141{ 142 /* If the blend equation is ADD or REVERSE_SUBTRACT, 143 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set, 144 * the colorbuffer will not be changed. 145 * Notice that the dst factors are the src factors inverted. */ 146 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR || 147 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 148 srcRGB == PIPE_BLENDFACTOR_ZERO) && 149 (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR || 150 srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 151 srcA == PIPE_BLENDFACTOR_ZERO) && 152 (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR || 153 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA || 154 dstRGB == PIPE_BLENDFACTOR_ONE) && 155 (dstA == PIPE_BLENDFACTOR_SRC_COLOR || 156 dstA == PIPE_BLENDFACTOR_SRC_ALPHA || 157 dstA == PIPE_BLENDFACTOR_ONE); 158} 159 160static unsigned bgra_cmask(unsigned mask) 161{ 162 /* Gallium uses RGBA color ordering while R300 expects BGRA. */ 163 164 return ((mask & PIPE_MASK_R) << 2) | 165 ((mask & PIPE_MASK_B) >> 2) | 166 (mask & (PIPE_MASK_G | PIPE_MASK_A)); 167} 168 169/* Create a new blend state based on the CSO blend state. 170 * 171 * This encompasses alpha blending, logic/raster ops, and blend dithering. */ 172static void* r300_create_blend_state(struct pipe_context* pipe, 173 const struct pipe_blend_state* state) 174{ 175 struct r300_screen* r300screen = r300_screen(pipe->screen); 176 struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state); 177 178 if (state->rt[0].blend_enable) 179 { 180 unsigned eqRGB = state->rt[0].rgb_func; 181 unsigned srcRGB = state->rt[0].rgb_src_factor; 182 unsigned dstRGB = state->rt[0].rgb_dst_factor; 183 184 unsigned eqA = state->rt[0].alpha_func; 185 unsigned srcA = state->rt[0].alpha_src_factor; 186 unsigned dstA = state->rt[0].alpha_dst_factor; 187 188 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha, 189 * this is just the crappy D3D naming */ 190 blend->blend_control = R300_ALPHA_BLEND_ENABLE | 191 r300_translate_blend_function(eqRGB) | 192 ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) | 193 ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT); 194 195 /* Optimization: some operations do not require the destination color. 196 * 197 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled, 198 * otherwise blending gives incorrect results. It seems to be 199 * a hardware bug. */ 200 if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN || 201 eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX || 202 dstRGB != PIPE_BLENDFACTOR_ZERO || 203 dstA != PIPE_BLENDFACTOR_ZERO || 204 srcRGB == PIPE_BLENDFACTOR_DST_COLOR || 205 srcRGB == PIPE_BLENDFACTOR_DST_ALPHA || 206 srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR || 207 srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA || 208 srcA == PIPE_BLENDFACTOR_DST_COLOR || 209 srcA == PIPE_BLENDFACTOR_DST_ALPHA || 210 srcA == PIPE_BLENDFACTOR_INV_DST_COLOR || 211 srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA || 212 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) { 213 /* Enable reading from the colorbuffer. */ 214 blend->blend_control |= R300_READ_ENABLE; 215 216 if (r300_screen(r300_context(pipe)->context.screen)->caps->is_r500) { 217 /* Optimization: Depending on incoming pixels, we can 218 * conditionally disable the reading in hardware... */ 219 if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN && 220 eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) { 221 /* Disable reading if SRC_ALPHA == 0. */ 222 if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA || 223 dstRGB == PIPE_BLENDFACTOR_ZERO) && 224 (dstA == PIPE_BLENDFACTOR_SRC_COLOR || 225 dstA == PIPE_BLENDFACTOR_SRC_ALPHA || 226 dstA == PIPE_BLENDFACTOR_ZERO)) { 227 blend->blend_control |= R500_SRC_ALPHA_0_NO_READ; 228 } 229 230 /* Disable reading if SRC_ALPHA == 1. */ 231 if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 232 dstRGB == PIPE_BLENDFACTOR_ZERO) && 233 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR || 234 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA || 235 dstA == PIPE_BLENDFACTOR_ZERO)) { 236 blend->blend_control |= R500_SRC_ALPHA_1_NO_READ; 237 } 238 } 239 } 240 } 241 242 /* Optimization: discard pixels which don't change the colorbuffer. 243 * 244 * The code below is non-trivial and some math is involved. 245 * 246 * Discarding pixels must be disabled when FP16 AA is enabled. 247 * This is a hardware bug. Also, this implementation wouldn't work 248 * with FP blending enabled and equation clamping disabled. 249 * 250 * Equations other than ADD are rarely used and therefore won't be 251 * optimized. */ 252 if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) && 253 (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) { 254 /* ADD: X+Y 255 * REVERSE_SUBTRACT: Y-X 256 * 257 * The idea is: 258 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1, 259 * then CB will not be changed. 260 * 261 * Given the srcFactor and dstFactor variables, we can derive 262 * what src and dst should be equal to and discard appropriate 263 * pixels. 264 */ 265 if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) { 266 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0; 267 } else if (blend_discard_if_src_alpha_1(srcRGB, srcA, 268 dstRGB, dstA)) { 269 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1; 270 } else if (blend_discard_if_src_color_0(srcRGB, srcA, 271 dstRGB, dstA)) { 272 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0; 273 } else if (blend_discard_if_src_color_1(srcRGB, srcA, 274 dstRGB, dstA)) { 275 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1; 276 } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA, 277 dstRGB, dstA)) { 278 blend->blend_control |= 279 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0; 280 } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA, 281 dstRGB, dstA)) { 282 blend->blend_control |= 283 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1; 284 } 285 } 286 287 /* separate alpha */ 288 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) { 289 blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE; 290 blend->alpha_blend_control = 291 r300_translate_blend_function(eqA) | 292 (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) | 293 (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT); 294 } 295 } 296 297 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */ 298 if (state->logicop_enable) { 299 blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE | 300 (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT; 301 } 302 303 /* Color channel masks for all MRTs. */ 304 blend->color_channel_mask = bgra_cmask(state->rt[0].colormask); 305 if (r300screen->caps->is_r500 && state->independent_blend_enable) { 306 if (state->rt[1].blend_enable) { 307 blend->color_channel_mask |= bgra_cmask(state->rt[1].colormask) << 4; 308 } 309 if (state->rt[2].blend_enable) { 310 blend->color_channel_mask |= bgra_cmask(state->rt[2].colormask) << 8; 311 } 312 if (state->rt[3].blend_enable) { 313 blend->color_channel_mask |= bgra_cmask(state->rt[3].colormask) << 12; 314 } 315 } 316 317 if (state->dither) { 318 blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT | 319 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT; 320 } 321 322 return (void*)blend; 323} 324 325/* Bind blend state. */ 326static void r300_bind_blend_state(struct pipe_context* pipe, 327 void* state) 328{ 329 struct r300_context* r300 = r300_context(pipe); 330 331 r300->blend_state.state = state; 332 r300->blend_state.dirty = TRUE; 333} 334 335/* Free blend state. */ 336static void r300_delete_blend_state(struct pipe_context* pipe, 337 void* state) 338{ 339 FREE(state); 340} 341 342/* Convert float to 10bit integer */ 343static unsigned float_to_fixed10(float f) 344{ 345 return CLAMP((unsigned)(f * 1023.9f), 0, 1023); 346} 347 348/* Set blend color. 349 * Setup both R300 and R500 registers, figure out later which one to write. */ 350static void r300_set_blend_color(struct pipe_context* pipe, 351 const struct pipe_blend_color* color) 352{ 353 struct r300_context* r300 = r300_context(pipe); 354 struct r300_screen* r300screen = r300_screen(pipe->screen); 355 struct r300_blend_color_state* state = 356 (struct r300_blend_color_state*)r300->blend_color_state.state; 357 union util_color uc; 358 359 util_pack_color(color->color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc); 360 state->blend_color = uc.ui; 361 362 /* XXX if FP16 blending is enabled, we should use the FP16 format */ 363 state->blend_color_red_alpha = 364 float_to_fixed10(color->color[0]) | 365 (float_to_fixed10(color->color[3]) << 16); 366 state->blend_color_green_blue = 367 float_to_fixed10(color->color[2]) | 368 (float_to_fixed10(color->color[1]) << 16); 369 370 r300->blend_color_state.size = r300screen->caps->is_r500 ? 3 : 2; 371 r300->blend_color_state.dirty = TRUE; 372} 373 374static void r300_set_clip_state(struct pipe_context* pipe, 375 const struct pipe_clip_state* state) 376{ 377 struct r300_context* r300 = r300_context(pipe); 378 379 if (r300_screen(pipe->screen)->caps->has_tcl) { 380 memcpy(r300->clip_state.state, state, sizeof(struct pipe_clip_state)); 381 r300->clip_state.size = 29; 382 } else { 383 draw_flush(r300->draw); 384 draw_set_clip_state(r300->draw, state); 385 r300->clip_state.size = 2; 386 } 387 388 r300->clip_state.dirty = TRUE; 389} 390 391/* Create a new depth, stencil, and alpha state based on the CSO dsa state. 392 * 393 * This contains the depth buffer, stencil buffer, alpha test, and such. 394 * On the Radeon, depth and stencil buffer setup are intertwined, which is 395 * the reason for some of the strange-looking assignments across registers. */ 396static void* 397 r300_create_dsa_state(struct pipe_context* pipe, 398 const struct pipe_depth_stencil_alpha_state* state) 399{ 400 struct r300_capabilities *caps = 401 r300_screen(r300_context(pipe)->context.screen)->caps; 402 struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state); 403 404 /* Depth test setup. */ 405 if (state->depth.enabled) { 406 dsa->z_buffer_control |= R300_Z_ENABLE; 407 408 if (state->depth.writemask) { 409 dsa->z_buffer_control |= R300_Z_WRITE_ENABLE; 410 } 411 412 dsa->z_stencil_control |= 413 (r300_translate_depth_stencil_function(state->depth.func) << 414 R300_Z_FUNC_SHIFT); 415 } 416 417 /* Stencil buffer setup. */ 418 if (state->stencil[0].enabled) { 419 dsa->z_buffer_control |= R300_STENCIL_ENABLE; 420 dsa->z_stencil_control |= 421 (r300_translate_depth_stencil_function(state->stencil[0].func) << 422 R300_S_FRONT_FUNC_SHIFT) | 423 (r300_translate_stencil_op(state->stencil[0].fail_op) << 424 R300_S_FRONT_SFAIL_OP_SHIFT) | 425 (r300_translate_stencil_op(state->stencil[0].zpass_op) << 426 R300_S_FRONT_ZPASS_OP_SHIFT) | 427 (r300_translate_stencil_op(state->stencil[0].zfail_op) << 428 R300_S_FRONT_ZFAIL_OP_SHIFT); 429 430 dsa->stencil_ref_mask = 431 (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) | 432 (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT); 433 434 if (state->stencil[1].enabled) { 435 dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK; 436 dsa->z_stencil_control |= 437 (r300_translate_depth_stencil_function(state->stencil[1].func) << 438 R300_S_BACK_FUNC_SHIFT) | 439 (r300_translate_stencil_op(state->stencil[1].fail_op) << 440 R300_S_BACK_SFAIL_OP_SHIFT) | 441 (r300_translate_stencil_op(state->stencil[1].zpass_op) << 442 R300_S_BACK_ZPASS_OP_SHIFT) | 443 (r300_translate_stencil_op(state->stencil[1].zfail_op) << 444 R300_S_BACK_ZFAIL_OP_SHIFT); 445 446 if (caps->is_r500) 447 { 448 dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK; 449 dsa->stencil_ref_bf = 450 (state->stencil[1].valuemask << 451 R300_STENCILMASK_SHIFT) | 452 (state->stencil[1].writemask << 453 R300_STENCILWRITEMASK_SHIFT); 454 } 455 } 456 } 457 458 /* Alpha test setup. */ 459 if (state->alpha.enabled) { 460 dsa->alpha_function = 461 r300_translate_alpha_function(state->alpha.func) | 462 R300_FG_ALPHA_FUNC_ENABLE; 463 464 /* We could use 10bit alpha ref but who needs that? */ 465 dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value); 466 467 if (caps->is_r500) 468 dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT; 469 } 470 471 return (void*)dsa; 472} 473 474/* Bind DSA state. */ 475static void r300_bind_dsa_state(struct pipe_context* pipe, 476 void* state) 477{ 478 struct r300_context* r300 = r300_context(pipe); 479 struct r300_screen* r300screen = r300_screen(pipe->screen); 480 481 r300->dsa_state.state = state; 482 r300->dsa_state.size = r300screen->caps->is_r500 ? 8 : 6; 483 r300->dsa_state.dirty = TRUE; 484} 485 486/* Free DSA state. */ 487static void r300_delete_dsa_state(struct pipe_context* pipe, 488 void* state) 489{ 490 FREE(state); 491} 492 493static void r300_set_stencil_ref(struct pipe_context* pipe, 494 const struct pipe_stencil_ref* sr) 495{ 496 struct r300_context* r300 = r300_context(pipe); 497 r300->stencil_ref = *sr; 498 r300->dsa_state.dirty = TRUE; 499} 500 501/* This switcheroo is needed just because of goddamned MACRO_SWITCH. */ 502static void r300_fb_update_tiling_flags(struct r300_context *r300, 503 const struct pipe_framebuffer_state *old_state, 504 const struct pipe_framebuffer_state *new_state) 505{ 506 struct r300_texture *tex; 507 unsigned i, j, level; 508 509 /* Reset tiling flags for old surfaces to default values. */ 510 for (i = 0; i < old_state->nr_cbufs; i++) { 511 for (j = 0; j < new_state->nr_cbufs; j++) { 512 if (old_state->cbufs[i]->texture == new_state->cbufs[j]->texture) { 513 break; 514 } 515 } 516 /* If not binding the surface again... */ 517 if (j != new_state->nr_cbufs) { 518 continue; 519 } 520 521 tex = (struct r300_texture*)old_state->cbufs[i]->texture; 522 523 if (tex) { 524 r300->winsys->buffer_set_tiling(r300->winsys, tex->buffer, 525 tex->pitch[0], 526 tex->microtile != 0, 527 tex->macrotile != 0); 528 } 529 } 530 if (old_state->zsbuf && 531 (!new_state->zsbuf || 532 old_state->zsbuf->texture != new_state->zsbuf->texture)) { 533 tex = (struct r300_texture*)old_state->zsbuf->texture; 534 535 if (tex) { 536 r300->winsys->buffer_set_tiling(r300->winsys, tex->buffer, 537 tex->pitch[0], 538 tex->microtile != 0, 539 tex->macrotile != 0); 540 } 541 } 542 543 /* Set tiling flags for new surfaces. */ 544 for (i = 0; i < new_state->nr_cbufs; i++) { 545 tex = (struct r300_texture*)new_state->cbufs[i]->texture; 546 level = new_state->cbufs[i]->level; 547 548 r300->winsys->buffer_set_tiling(r300->winsys, tex->buffer, 549 tex->pitch[level], 550 tex->microtile != 0, 551 tex->mip_macrotile[level] != 0); 552 } 553 if (new_state->zsbuf) { 554 tex = (struct r300_texture*)new_state->zsbuf->texture; 555 level = new_state->zsbuf->level; 556 557 r300->winsys->buffer_set_tiling(r300->winsys, tex->buffer, 558 tex->pitch[level], 559 tex->microtile != 0, 560 tex->mip_macrotile[level] != 0); 561 } 562} 563 564static void 565 r300_set_framebuffer_state(struct pipe_context* pipe, 566 const struct pipe_framebuffer_state* state) 567{ 568 struct r300_context* r300 = r300_context(pipe); 569 struct r300_screen* r300screen = r300_screen(pipe->screen); 570 unsigned max_width, max_height; 571 uint32_t zbuffer_bpp = 0; 572 573 574 if (state->nr_cbufs > 4) { 575 debug_printf("r300: Implementation error: Too many MRTs in %s, " 576 "refusing to bind framebuffer state!\n", __FUNCTION__); 577 return; 578 } 579 580 if (r300screen->caps->is_r500) { 581 max_width = max_height = 4096; 582 } else if (r300screen->caps->is_r400) { 583 max_width = max_height = 4021; 584 } else { 585 max_width = max_height = 2560; 586 } 587 588 if (state->width > max_width || state->height > max_height) { 589 debug_printf("r300: Implementation error: Render targets are too " 590 "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__); 591 return; 592 } 593 594 595 if (r300->draw) { 596 draw_flush(r300->draw); 597 } 598 599 memcpy(r300->fb_state.state, state, sizeof(struct pipe_framebuffer_state)); 600 601 r300->fb_state.size = (10 * state->nr_cbufs) + (state->zsbuf ? 10 : 0) + 6; 602 603 r300_fb_update_tiling_flags(r300, r300->fb_state.state, state); 604 605 /* XXX wait what */ 606 r300->blend_state.dirty = TRUE; 607 r300->dsa_state.dirty = TRUE; 608 r300->fb_state.dirty = TRUE; 609 r300->scissor_state.dirty = TRUE; 610 611 /* Polygon offset depends on the zbuffer bit depth. */ 612 if (state->zsbuf && r300->polygon_offset_enabled) { 613 switch (util_format_get_blocksize(state->zsbuf->texture->format)) { 614 case 2: 615 zbuffer_bpp = 16; 616 break; 617 case 4: 618 zbuffer_bpp = 24; 619 break; 620 } 621 622 if (r300->zbuffer_bpp != zbuffer_bpp) { 623 r300->zbuffer_bpp = zbuffer_bpp; 624 r300->rs_state.dirty = TRUE; 625 } 626 } 627} 628 629/* Create fragment shader state. */ 630static void* r300_create_fs_state(struct pipe_context* pipe, 631 const struct pipe_shader_state* shader) 632{ 633 struct r300_fragment_shader* fs = NULL; 634 635 fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader); 636 637 /* Copy state directly into shader. */ 638 fs->state = *shader; 639 fs->state.tokens = tgsi_dup_tokens(shader->tokens); 640 641 tgsi_scan_shader(shader->tokens, &fs->info); 642 r300_shader_read_fs_inputs(&fs->info, &fs->inputs); 643 644 return (void*)fs; 645} 646 647/* Bind fragment shader state. */ 648static void r300_bind_fs_state(struct pipe_context* pipe, void* shader) 649{ 650 struct r300_context* r300 = r300_context(pipe); 651 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader; 652 653 if (fs == NULL) { 654 r300->fs = NULL; 655 return; 656 } 657 658 r300->fs = fs; 659 r300_pick_fragment_shader(r300); 660 661 if (r300->vs && r300_vertex_shader_setup_wpos(r300)) { 662 r300->vertex_format_state.dirty = TRUE; 663 } 664 665 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | R300_NEW_FRAGMENT_SHADER_CONSTANTS; 666} 667 668/* Delete fragment shader state. */ 669static void r300_delete_fs_state(struct pipe_context* pipe, void* shader) 670{ 671 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader; 672 struct r300_fragment_shader_code *tmp, *ptr = fs->first; 673 674 while (ptr) { 675 tmp = ptr; 676 ptr = ptr->next; 677 rc_constants_destroy(&tmp->code.constants); 678 FREE(tmp); 679 } 680 FREE((void*)fs->state.tokens); 681 FREE(shader); 682} 683 684static void r300_set_polygon_stipple(struct pipe_context* pipe, 685 const struct pipe_poly_stipple* state) 686{ 687 /* XXX no idea how to set this up, but not terribly important */ 688} 689 690/* Create a new rasterizer state based on the CSO rasterizer state. 691 * 692 * This is a very large chunk of state, and covers most of the graphics 693 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks. 694 * 695 * In a not entirely unironic sidenote, this state has nearly nothing to do 696 * with the actual block on the Radeon called the rasterizer (RS). */ 697static void* r300_create_rs_state(struct pipe_context* pipe, 698 const struct pipe_rasterizer_state* state) 699{ 700 struct r300_screen* r300screen = r300_screen(pipe->screen); 701 struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state); 702 703 /* Copy rasterizer state for Draw. */ 704 rs->rs = *state; 705 706#ifdef PIPE_ARCH_LITTLE_ENDIAN 707 rs->vap_control_status = R300_VC_NO_SWAP; 708#else 709 rs->vap_control_status = R300_VC_32BIT_SWAP; 710#endif 711 712 /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL. 713 * Else, enable HW TCL and force Draw's TCL off. */ 714 if (!r300screen->caps->has_tcl) { 715 rs->vap_control_status |= R300_VAP_TCL_BYPASS; 716 } 717 718 rs->point_size = pack_float_16_6x(state->point_size) | 719 (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT); 720 721 /* Point minimum and maximum sizes. This register has to be emitted, 722 * and it'd be a step backwards to put it in invariant state. */ 723 if (r300screen->caps->is_r500) { 724 rs->point_minmax = 725 ((int)(0.0 * 6.0) << R300_GA_POINT_MINMAX_MIN_SHIFT) | 726 ((int)(4096.0 * 6.0) << R300_GA_POINT_MINMAX_MAX_SHIFT); 727 } else if (r300screen->caps->is_r400) { 728 rs->point_minmax = 729 ((int)(0.0 * 6.0) << R300_GA_POINT_MINMAX_MIN_SHIFT) | 730 ((int)(4021.0 * 6.0) << R300_GA_POINT_MINMAX_MAX_SHIFT); 731 } else { 732 rs->point_minmax = 733 ((int)(0.0 * 6.0) << R300_GA_POINT_MINMAX_MIN_SHIFT) | 734 ((int)(2560.0 * 6.0) << R300_GA_POINT_MINMAX_MAX_SHIFT); 735 } 736 737 rs->line_control = pack_float_16_6x(state->line_width) | 738 R300_GA_LINE_CNTL_END_TYPE_COMP; 739 740 /* Enable polygon mode */ 741 if (state->fill_cw != PIPE_POLYGON_MODE_FILL || 742 state->fill_ccw != PIPE_POLYGON_MODE_FILL) { 743 rs->polygon_mode = R300_GA_POLY_MODE_DUAL; 744 } 745 746 /* Radeons don't think in "CW/CCW", they think in "front/back". */ 747 if (state->front_winding == PIPE_WINDING_CW) { 748 rs->cull_mode = R300_FRONT_FACE_CW; 749 750 /* Polygon offset */ 751 if (state->offset_cw) { 752 rs->polygon_offset_enable |= R300_FRONT_ENABLE; 753 } 754 if (state->offset_ccw) { 755 rs->polygon_offset_enable |= R300_BACK_ENABLE; 756 } 757 758 /* Polygon mode */ 759 if (rs->polygon_mode) { 760 rs->polygon_mode |= 761 r300_translate_polygon_mode_front(state->fill_cw); 762 rs->polygon_mode |= 763 r300_translate_polygon_mode_back(state->fill_ccw); 764 } 765 } else { 766 rs->cull_mode = R300_FRONT_FACE_CCW; 767 768 /* Polygon offset */ 769 if (state->offset_ccw) { 770 rs->polygon_offset_enable |= R300_FRONT_ENABLE; 771 } 772 if (state->offset_cw) { 773 rs->polygon_offset_enable |= R300_BACK_ENABLE; 774 } 775 776 /* Polygon mode */ 777 if (rs->polygon_mode) { 778 rs->polygon_mode |= 779 r300_translate_polygon_mode_front(state->fill_ccw); 780 rs->polygon_mode |= 781 r300_translate_polygon_mode_back(state->fill_cw); 782 } 783 } 784 if (state->front_winding & state->cull_mode) { 785 rs->cull_mode |= R300_CULL_FRONT; 786 } 787 if (~(state->front_winding) & state->cull_mode) { 788 rs->cull_mode |= R300_CULL_BACK; 789 } 790 791 if (rs->polygon_offset_enable) { 792 rs->depth_offset = state->offset_units; 793 rs->depth_scale = state->offset_scale; 794 } 795 796 if (state->line_stipple_enable) { 797 rs->line_stipple_config = 798 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE | 799 (fui((float)state->line_stipple_factor) & 800 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK); 801 /* XXX this might need to be scaled up */ 802 rs->line_stipple_value = state->line_stipple_pattern; 803 } 804 805 if (state->flatshade) { 806 rs->color_control = R300_SHADE_MODEL_FLAT; 807 } else { 808 rs->color_control = R300_SHADE_MODEL_SMOOTH; 809 } 810 811 return (void*)rs; 812} 813 814/* Bind rasterizer state. */ 815static void r300_bind_rs_state(struct pipe_context* pipe, void* state) 816{ 817 struct r300_context* r300 = r300_context(pipe); 818 struct r300_rs_state* rs = (struct r300_rs_state*)state; 819 820 if (r300->draw) { 821 draw_flush(r300->draw); 822 draw_set_rasterizer_state(r300->draw, &rs->rs); 823 } 824 825 if (rs) { 826 r300->polygon_offset_enabled = rs->rs.offset_cw || rs->rs.offset_ccw; 827 } else { 828 r300->polygon_offset_enabled = FALSE; 829 } 830 831 r300->rs_state.state = rs; 832 r300->rs_state.dirty = TRUE; 833 /* XXX Why is this still needed, dammit!? */ 834 r300->scissor_state.dirty = TRUE; 835 r300->viewport_state.dirty = TRUE; 836 837 /* XXX Clean these up when we move to atom emits */ 838 if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) { 839 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS; 840 } 841} 842 843/* Free rasterizer state. */ 844static void r300_delete_rs_state(struct pipe_context* pipe, void* state) 845{ 846 FREE(state); 847} 848 849static void* 850 r300_create_sampler_state(struct pipe_context* pipe, 851 const struct pipe_sampler_state* state) 852{ 853 struct r300_context* r300 = r300_context(pipe); 854 struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state); 855 int lod_bias; 856 union util_color uc; 857 858 sampler->state = *state; 859 860 sampler->filter0 |= 861 (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) | 862 (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) | 863 (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT); 864 865 sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter, 866 state->mag_img_filter, 867 state->min_mip_filter, 868 state->max_anisotropy > 0); 869 870 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */ 871 /* We must pass these to the emit function to clamp them properly. */ 872 sampler->min_lod = MAX2((unsigned)state->min_lod, 0); 873 sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0); 874 875 lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1); 876 877 sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT; 878 879 sampler->filter1 |= r300_anisotropy(state->max_anisotropy); 880 881 util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc); 882 sampler->border_color = uc.ui; 883 884 /* R500-specific fixups and optimizations */ 885 if (r300_screen(r300->context.screen)->caps->is_r500) { 886 sampler->filter1 |= R500_BORDER_FIX; 887 } 888 889 return (void*)sampler; 890} 891 892static void r300_bind_sampler_states(struct pipe_context* pipe, 893 unsigned count, 894 void** states) 895{ 896 struct r300_context* r300 = r300_context(pipe); 897 int i; 898 899 if (count > 8) { 900 return; 901 } 902 903 for (i = 0; i < count; i++) { 904 if (r300->sampler_states[i] != states[i]) { 905 r300->sampler_states[i] = (struct r300_sampler_state*)states[i]; 906 r300->dirty_state |= (R300_NEW_SAMPLER << i); 907 } 908 } 909 910 r300->sampler_count = count; 911 912 /* Pick a fragment shader based on the texture compare state. */ 913 if (r300->fs && (r300->dirty_state & R300_ANY_NEW_SAMPLERS)) { 914 if (r300_pick_fragment_shader(r300)) { 915 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | 916 R300_NEW_FRAGMENT_SHADER_CONSTANTS; 917 } 918 } 919} 920 921static void r300_lacks_vertex_textures(struct pipe_context* pipe, 922 unsigned count, 923 void** states) 924{ 925} 926 927static void r300_delete_sampler_state(struct pipe_context* pipe, void* state) 928{ 929 FREE(state); 930} 931 932static void r300_set_sampler_textures(struct pipe_context* pipe, 933 unsigned count, 934 struct pipe_texture** texture) 935{ 936 struct r300_context* r300 = r300_context(pipe); 937 boolean is_r500 = r300_screen(r300->context.screen)->caps->is_r500; 938 int i; 939 940 /* XXX magic num */ 941 if (count > 8) { 942 return; 943 } 944 945 for (i = 0; i < count; i++) { 946 if (r300->textures[i] != (struct r300_texture*)texture[i]) { 947 pipe_texture_reference((struct pipe_texture**)&r300->textures[i], 948 texture[i]); 949 r300->dirty_state |= (R300_NEW_TEXTURE << i); 950 951 /* R300-specific - set the texrect factor in a fragment shader */ 952 if (!is_r500 && r300->textures[i]->is_npot) { 953 /* XXX It would be nice to re-emit just 1 constant, 954 * XXX not all of them */ 955 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS; 956 } 957 } 958 } 959 960 for (i = count; i < 8; i++) { 961 if (r300->textures[i]) { 962 pipe_texture_reference((struct pipe_texture**)&r300->textures[i], 963 NULL); 964 r300->dirty_state |= (R300_NEW_TEXTURE << i); 965 } 966 } 967 968 r300->texture_count = count; 969} 970 971static void r300_set_scissor_state(struct pipe_context* pipe, 972 const struct pipe_scissor_state* state) 973{ 974 struct r300_context* r300 = r300_context(pipe); 975 976 memcpy(r300->scissor_state.state, state, 977 sizeof(struct pipe_scissor_state)); 978 979 r300->scissor_state.dirty = TRUE; 980} 981 982static void r300_set_viewport_state(struct pipe_context* pipe, 983 const struct pipe_viewport_state* state) 984{ 985 struct r300_context* r300 = r300_context(pipe); 986 struct r300_viewport_state* viewport = 987 (struct r300_viewport_state*)r300->viewport_state.state; 988 989 /* Do the transform in HW. */ 990 viewport->vte_control = R300_VTX_W0_FMT; 991 992 if (state->scale[0] != 1.0f) { 993 viewport->xscale = state->scale[0]; 994 viewport->vte_control |= R300_VPORT_X_SCALE_ENA; 995 } 996 if (state->scale[1] != 1.0f) { 997 viewport->yscale = state->scale[1]; 998 viewport->vte_control |= R300_VPORT_Y_SCALE_ENA; 999 } 1000 if (state->scale[2] != 1.0f) { 1001 viewport->zscale = state->scale[2]; 1002 viewport->vte_control |= R300_VPORT_Z_SCALE_ENA; 1003 } 1004 if (state->translate[0] != 0.0f) { 1005 viewport->xoffset = state->translate[0]; 1006 viewport->vte_control |= R300_VPORT_X_OFFSET_ENA; 1007 } 1008 if (state->translate[1] != 0.0f) { 1009 viewport->yoffset = state->translate[1]; 1010 viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA; 1011 } 1012 if (state->translate[2] != 0.0f) { 1013 viewport->zoffset = state->translate[2]; 1014 viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA; 1015 } 1016 1017 r300->viewport_state.dirty = TRUE; 1018 if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) { 1019 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS; 1020 } 1021} 1022 1023static void r300_set_vertex_buffers(struct pipe_context* pipe, 1024 unsigned count, 1025 const struct pipe_vertex_buffer* buffers) 1026{ 1027 struct r300_context* r300 = r300_context(pipe); 1028 1029 memcpy(r300->vertex_buffer, buffers, 1030 sizeof(struct pipe_vertex_buffer) * count); 1031 r300->vertex_buffer_count = count; 1032 1033 if (r300->draw) { 1034 draw_flush(r300->draw); 1035 draw_set_vertex_buffers(r300->draw, count, buffers); 1036 } 1037 1038 r300->vertex_format_state.dirty = TRUE; 1039} 1040 1041static boolean r300_validate_aos(struct r300_context *r300) 1042{ 1043 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer; 1044 struct pipe_vertex_element *velem = r300->vertex_element; 1045 int i; 1046 1047 /* Check if formats and strides are aligned to the size of DWORD. */ 1048 for (i = 0; i < r300->vertex_element_count; i++) { 1049 if (vbuf[velem[i].vertex_buffer_index].stride % 4 != 0 || 1050 util_format_get_blocksize(velem[i].src_format) % 4 != 0) { 1051 return FALSE; 1052 } 1053 } 1054 return TRUE; 1055} 1056 1057static void r300_set_vertex_elements(struct pipe_context* pipe, 1058 unsigned count, 1059 const struct pipe_vertex_element* elements) 1060{ 1061 struct r300_context* r300 = r300_context(pipe); 1062 1063 memcpy(r300->vertex_element, 1064 elements, 1065 sizeof(struct pipe_vertex_element) * count); 1066 r300->vertex_element_count = count; 1067 1068 if (r300->draw) { 1069 draw_flush(r300->draw); 1070 draw_set_vertex_elements(r300->draw, count, elements); 1071 } 1072 1073 if (!r300_validate_aos(r300)) { 1074 /* XXX We should fallback using draw. */ 1075 assert(0); 1076 abort(); 1077 } 1078} 1079 1080static void* r300_create_vs_state(struct pipe_context* pipe, 1081 const struct pipe_shader_state* shader) 1082{ 1083 struct r300_context* r300 = r300_context(pipe); 1084 1085 if (r300_screen(pipe->screen)->caps->has_tcl) { 1086 struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader); 1087 /* Copy state directly into shader. */ 1088 vs->state = *shader; 1089 vs->state.tokens = tgsi_dup_tokens(shader->tokens); 1090 1091 tgsi_scan_shader(shader->tokens, &vs->info); 1092 1093 return (void*)vs; 1094 } else { 1095 return draw_create_vertex_shader(r300->draw, shader); 1096 } 1097} 1098 1099static void r300_bind_vs_state(struct pipe_context* pipe, void* shader) 1100{ 1101 struct r300_context* r300 = r300_context(pipe); 1102 1103 if (r300_screen(pipe->screen)->caps->has_tcl) { 1104 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader; 1105 1106 if (vs == NULL) { 1107 r300->vs = NULL; 1108 return; 1109 } else if (!vs->translated) { 1110 r300_translate_vertex_shader(r300, vs); 1111 } 1112 1113 r300->vs = vs; 1114 if (r300->fs) { 1115 r300_vertex_shader_setup_wpos(r300); 1116 } 1117 1118 r300->vertex_format_state.dirty = TRUE; 1119 1120 r300->dirty_state |= 1121 R300_NEW_VERTEX_SHADER | R300_NEW_VERTEX_SHADER_CONSTANTS; 1122 } else { 1123 draw_flush(r300->draw); 1124 draw_bind_vertex_shader(r300->draw, 1125 (struct draw_vertex_shader*)shader); 1126 } 1127} 1128 1129static void r300_delete_vs_state(struct pipe_context* pipe, void* shader) 1130{ 1131 struct r300_context* r300 = r300_context(pipe); 1132 1133 if (r300_screen(pipe->screen)->caps->has_tcl) { 1134 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader; 1135 1136 rc_constants_destroy(&vs->code.constants); 1137 FREE((void*)vs->state.tokens); 1138 FREE(shader); 1139 } else { 1140 draw_delete_vertex_shader(r300->draw, 1141 (struct draw_vertex_shader*)shader); 1142 } 1143} 1144 1145static void r300_set_constant_buffer(struct pipe_context *pipe, 1146 uint shader, uint index, 1147 struct pipe_buffer *buf) 1148{ 1149 struct r300_context* r300 = r300_context(pipe); 1150 struct r300_screen *r300screen = r300_screen(pipe->screen); 1151 void *mapped; 1152 int max_size = 0; 1153 1154 if (buf == NULL || buf->size == 0 || 1155 (mapped = pipe_buffer_map(pipe->screen, buf, PIPE_BUFFER_USAGE_CPU_READ)) == NULL) 1156 { 1157 r300->shader_constants[shader].count = 0; 1158 return; 1159 } 1160 1161 assert((buf->size % 4 * sizeof(float)) == 0); 1162 1163 /* Check the size of the constant buffer. */ 1164 switch (shader) { 1165 case PIPE_SHADER_VERTEX: 1166 max_size = 256; 1167 break; 1168 case PIPE_SHADER_FRAGMENT: 1169 if (r300screen->caps->is_r500) { 1170 max_size = 256; 1171 /* XXX Implement emission of r400's extended constant buffer. */ 1172 /*} else if (r300screen->caps->is_r400) { 1173 max_size = 64;*/ 1174 } else { 1175 max_size = 32; 1176 } 1177 break; 1178 default: 1179 assert(0); 1180 } 1181 1182 /* XXX Subtract immediates and RC_STATE_* variables. */ 1183 if (buf->size > (sizeof(float) * 4 * max_size)) { 1184 debug_printf("r300: Max size of the constant buffer is " 1185 "%i*4 floats.\n", max_size); 1186 abort(); 1187 } 1188 1189 memcpy(r300->shader_constants[shader].constants, mapped, buf->size); 1190 r300->shader_constants[shader].count = buf->size / (4 * sizeof(float)); 1191 pipe_buffer_unmap(pipe->screen, buf); 1192 1193 if (shader == PIPE_SHADER_VERTEX) 1194 r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS; 1195 else if (shader == PIPE_SHADER_FRAGMENT) 1196 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS; 1197} 1198 1199void r300_init_state_functions(struct r300_context* r300) 1200{ 1201 r300->context.create_blend_state = r300_create_blend_state; 1202 r300->context.bind_blend_state = r300_bind_blend_state; 1203 r300->context.delete_blend_state = r300_delete_blend_state; 1204 1205 r300->context.set_blend_color = r300_set_blend_color; 1206 1207 r300->context.set_clip_state = r300_set_clip_state; 1208 1209 r300->context.set_constant_buffer = r300_set_constant_buffer; 1210 1211 r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state; 1212 r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state; 1213 r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state; 1214 1215 r300->context.set_stencil_ref = r300_set_stencil_ref; 1216 1217 r300->context.set_framebuffer_state = r300_set_framebuffer_state; 1218 1219 r300->context.create_fs_state = r300_create_fs_state; 1220 r300->context.bind_fs_state = r300_bind_fs_state; 1221 r300->context.delete_fs_state = r300_delete_fs_state; 1222 1223 r300->context.set_polygon_stipple = r300_set_polygon_stipple; 1224 1225 r300->context.create_rasterizer_state = r300_create_rs_state; 1226 r300->context.bind_rasterizer_state = r300_bind_rs_state; 1227 r300->context.delete_rasterizer_state = r300_delete_rs_state; 1228 1229 r300->context.create_sampler_state = r300_create_sampler_state; 1230 r300->context.bind_fragment_sampler_states = r300_bind_sampler_states; 1231 r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures; 1232 r300->context.delete_sampler_state = r300_delete_sampler_state; 1233 1234 r300->context.set_fragment_sampler_textures = r300_set_sampler_textures; 1235 1236 r300->context.set_scissor_state = r300_set_scissor_state; 1237 1238 r300->context.set_viewport_state = r300_set_viewport_state; 1239 1240 r300->context.set_vertex_buffers = r300_set_vertex_buffers; 1241 r300->context.set_vertex_elements = r300_set_vertex_elements; 1242 1243 r300->context.create_vs_state = r300_create_vs_state; 1244 r300->context.bind_vs_state = r300_bind_vs_state; 1245 r300->context.delete_vs_state = r300_delete_vs_state; 1246} 1247