draw_pipe_pstipple.c revision 6df42d80234d13676fc3207cf44f0e371e3372b5
1/************************************************************************** 2 * 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * Polygon stipple stage: implement polygon stipple with texture map and 30 * fragment program. The fragment program samples the texture and does 31 * a fragment kill for the stipple-failing fragments. 32 * 33 * Authors: Brian Paul 34 */ 35 36 37#include "pipe/p_context.h" 38#include "pipe/p_defines.h" 39#include "pipe/p_shader_tokens.h" 40 41#include "util/u_format.h" 42#include "util/u_math.h" 43#include "util/u_memory.h" 44 45#include "tgsi/tgsi_transform.h" 46#include "tgsi/tgsi_dump.h" 47 48#include "draw_context.h" 49#include "draw_pipe.h" 50 51 52 53/** 54 * Subclass of pipe_shader_state to carry extra fragment shader info. 55 */ 56struct pstip_fragment_shader 57{ 58 struct pipe_shader_state state; 59 void *driver_fs; 60 void *pstip_fs; 61 uint sampler_unit; 62}; 63 64 65/** 66 * Subclass of draw_stage 67 */ 68struct pstip_stage 69{ 70 struct draw_stage stage; 71 72 void *sampler_cso; 73 struct pipe_texture *texture; 74 uint num_samplers; 75 uint num_textures; 76 77 /* 78 * Currently bound state 79 */ 80 struct pstip_fragment_shader *fs; 81 struct { 82 void *samplers[PIPE_MAX_SAMPLERS]; 83 struct pipe_texture *textures[PIPE_MAX_SAMPLERS]; 84 const struct pipe_poly_stipple *stipple; 85 } state; 86 87 /* 88 * Driver interface/override functions 89 */ 90 void * (*driver_create_fs_state)(struct pipe_context *, 91 const struct pipe_shader_state *); 92 void (*driver_bind_fs_state)(struct pipe_context *, void *); 93 void (*driver_delete_fs_state)(struct pipe_context *, void *); 94 95 void (*driver_bind_sampler_states)(struct pipe_context *, unsigned, void **); 96 97 void (*driver_set_sampler_textures)(struct pipe_context *, unsigned, 98 struct pipe_texture **); 99 100 void (*driver_set_polygon_stipple)(struct pipe_context *, 101 const struct pipe_poly_stipple *); 102 103 struct pipe_context *pipe; 104}; 105 106 107 108/** 109 * Subclass of tgsi_transform_context, used for transforming the 110 * user's fragment shader to add the special AA instructions. 111 */ 112struct pstip_transform_context { 113 struct tgsi_transform_context base; 114 uint tempsUsed; /**< bitmask */ 115 int wincoordInput; 116 int maxInput; 117 uint samplersUsed; /**< bitfield of samplers used */ 118 int freeSampler; /** an available sampler for the pstipple */ 119 int texTemp; /**< temp registers */ 120 int numImmed; 121 boolean firstInstruction; 122}; 123 124 125/** 126 * TGSI declaration transform callback. 127 * Look for a free sampler, a free input attrib, and two free temp regs. 128 */ 129static void 130pstip_transform_decl(struct tgsi_transform_context *ctx, 131 struct tgsi_full_declaration *decl) 132{ 133 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx; 134 135 if (decl->Declaration.File == TGSI_FILE_SAMPLER) { 136 uint i; 137 for (i = decl->Range.First; 138 i <= decl->Range.Last; i++) { 139 pctx->samplersUsed |= 1 << i; 140 } 141 } 142 else if (decl->Declaration.File == TGSI_FILE_INPUT) { 143 pctx->maxInput = MAX2(pctx->maxInput, (int) decl->Range.Last); 144 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) 145 pctx->wincoordInput = (int) decl->Range.First; 146 } 147 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) { 148 uint i; 149 for (i = decl->Range.First; 150 i <= decl->Range.Last; i++) { 151 pctx->tempsUsed |= (1 << i); 152 } 153 } 154 155 ctx->emit_declaration(ctx, decl); 156} 157 158 159static void 160pstip_transform_immed(struct tgsi_transform_context *ctx, 161 struct tgsi_full_immediate *immed) 162{ 163 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx; 164 pctx->numImmed++; 165} 166 167 168/** 169 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones. 170 */ 171static int 172free_bit(uint bitfield) 173{ 174 int i; 175 for (i = 0; i < 32; i++) { 176 if ((bitfield & (1 << i)) == 0) 177 return i; 178 } 179 return -1; 180} 181 182 183/** 184 * TGSI instruction transform callback. 185 * Replace writes to result.color w/ a temp reg. 186 * Upon END instruction, insert texture sampling code for antialiasing. 187 */ 188static void 189pstip_transform_inst(struct tgsi_transform_context *ctx, 190 struct tgsi_full_instruction *inst) 191{ 192 struct pstip_transform_context *pctx = (struct pstip_transform_context *) ctx; 193 194 if (pctx->firstInstruction) { 195 /* emit our new declarations before the first instruction */ 196 197 struct tgsi_full_declaration decl; 198 struct tgsi_full_instruction newInst; 199 uint i; 200 int wincoordInput; 201 202 /* find free sampler */ 203 pctx->freeSampler = free_bit(pctx->samplersUsed); 204 if (pctx->freeSampler >= PIPE_MAX_SAMPLERS) 205 pctx->freeSampler = PIPE_MAX_SAMPLERS - 1; 206 207 if (pctx->wincoordInput < 0) 208 wincoordInput = pctx->maxInput + 1; 209 else 210 wincoordInput = pctx->wincoordInput; 211 212 /* find one free temp reg */ 213 for (i = 0; i < 32; i++) { 214 if ((pctx->tempsUsed & (1 << i)) == 0) { 215 /* found a free temp */ 216 if (pctx->texTemp < 0) 217 pctx->texTemp = i; 218 else 219 break; 220 } 221 } 222 assert(pctx->texTemp >= 0); 223 224 if (pctx->wincoordInput < 0) { 225 /* declare new position input reg */ 226 decl = tgsi_default_full_declaration(); 227 decl.Declaration.File = TGSI_FILE_INPUT; 228 decl.Declaration.Interpolate = TGSI_INTERPOLATE_LINEAR; /* XXX? */ 229 decl.Declaration.Semantic = 1; 230 decl.Semantic.Name = TGSI_SEMANTIC_POSITION; 231 decl.Semantic.Index = 0; 232 decl.Range.First = 233 decl.Range.Last = wincoordInput; 234 ctx->emit_declaration(ctx, &decl); 235 } 236 237 /* declare new sampler */ 238 decl = tgsi_default_full_declaration(); 239 decl.Declaration.File = TGSI_FILE_SAMPLER; 240 decl.Range.First = 241 decl.Range.Last = pctx->freeSampler; 242 ctx->emit_declaration(ctx, &decl); 243 244 /* declare new temp regs */ 245 decl = tgsi_default_full_declaration(); 246 decl.Declaration.File = TGSI_FILE_TEMPORARY; 247 decl.Range.First = 248 decl.Range.Last = pctx->texTemp; 249 ctx->emit_declaration(ctx, &decl); 250 251 /* emit immediate = {1/32, 1/32, 1, 1} 252 * The index/position of this immediate will be pctx->numImmed 253 */ 254 { 255 static const float value[4] = { 1.0/32, 1.0/32, 1.0, 1.0 }; 256 struct tgsi_full_immediate immed; 257 uint size = 4; 258 immed = tgsi_default_full_immediate(); 259 immed.Immediate.NrTokens = 1 + size; /* one for the token itself */ 260 immed.u[0].Float = value[0]; 261 immed.u[1].Float = value[1]; 262 immed.u[2].Float = value[2]; 263 immed.u[3].Float = value[3]; 264 ctx->emit_immediate(ctx, &immed); 265 } 266 267 pctx->firstInstruction = FALSE; 268 269 270 /* 271 * Insert new MUL/TEX/KILP instructions at start of program 272 * Take gl_FragCoord, divide by 32 (stipple size), sample the 273 * texture and kill fragment if needed. 274 * 275 * We'd like to use non-normalized texcoords to index into a RECT 276 * texture, but we can only use GL_REPEAT wrap mode with normalized 277 * texcoords. Darn. 278 */ 279 280 /* MUL texTemp, INPUT[wincoord], 1/32; */ 281 newInst = tgsi_default_full_instruction(); 282 newInst.Instruction.Opcode = TGSI_OPCODE_MUL; 283 newInst.Instruction.NumDstRegs = 1; 284 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; 285 newInst.Dst[0].Register.Index = pctx->texTemp; 286 newInst.Instruction.NumSrcRegs = 2; 287 newInst.Src[0].Register.File = TGSI_FILE_INPUT; 288 newInst.Src[0].Register.Index = wincoordInput; 289 newInst.Src[1].Register.File = TGSI_FILE_IMMEDIATE; 290 newInst.Src[1].Register.Index = pctx->numImmed; 291 ctx->emit_instruction(ctx, &newInst); 292 293 /* TEX texTemp, texTemp, sampler; */ 294 newInst = tgsi_default_full_instruction(); 295 newInst.Instruction.Opcode = TGSI_OPCODE_TEX; 296 newInst.Instruction.NumDstRegs = 1; 297 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY; 298 newInst.Dst[0].Register.Index = pctx->texTemp; 299 newInst.Instruction.NumSrcRegs = 2; 300 newInst.Instruction.Texture = TRUE; 301 newInst.Texture.Texture = TGSI_TEXTURE_2D; 302 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY; 303 newInst.Src[0].Register.Index = pctx->texTemp; 304 newInst.Src[1].Register.File = TGSI_FILE_SAMPLER; 305 newInst.Src[1].Register.Index = pctx->freeSampler; 306 ctx->emit_instruction(ctx, &newInst); 307 308 /* KIL -texTemp; # if -texTemp < 0, KILL fragment */ 309 newInst = tgsi_default_full_instruction(); 310 newInst.Instruction.Opcode = TGSI_OPCODE_KIL; 311 newInst.Instruction.NumDstRegs = 0; 312 newInst.Instruction.NumSrcRegs = 1; 313 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY; 314 newInst.Src[0].Register.Index = pctx->texTemp; 315 newInst.Src[0].Register.Negate = 1; 316 ctx->emit_instruction(ctx, &newInst); 317 } 318 319 /* emit this instruction */ 320 ctx->emit_instruction(ctx, inst); 321} 322 323 324/** 325 * Generate the frag shader we'll use for doing polygon stipple. 326 * This will be the user's shader prefixed with a TEX and KIL instruction. 327 */ 328static boolean 329generate_pstip_fs(struct pstip_stage *pstip) 330{ 331 const struct pipe_shader_state *orig_fs = &pstip->fs->state; 332 /*struct draw_context *draw = pstip->stage.draw;*/ 333 struct pipe_shader_state pstip_fs; 334 struct pstip_transform_context transform; 335 336#define MAX 1000 337 338 pstip_fs = *orig_fs; /* copy to init */ 339 pstip_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX); 340 if (pstip_fs.tokens == NULL) 341 return FALSE; 342 343 memset(&transform, 0, sizeof(transform)); 344 transform.wincoordInput = -1; 345 transform.maxInput = -1; 346 transform.texTemp = -1; 347 transform.firstInstruction = TRUE; 348 transform.base.transform_instruction = pstip_transform_inst; 349 transform.base.transform_declaration = pstip_transform_decl; 350 transform.base.transform_immediate = pstip_transform_immed; 351 352 tgsi_transform_shader(orig_fs->tokens, 353 (struct tgsi_token *) pstip_fs.tokens, 354 MAX, &transform.base); 355 356#if 0 /* DEBUG */ 357 tgsi_dump(orig_fs->tokens, 0); 358 tgsi_dump(pstip_fs.tokens, 0); 359#endif 360 361 pstip->fs->sampler_unit = transform.freeSampler; 362 assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS); 363 364 pstip->fs->pstip_fs = pstip->driver_create_fs_state(pstip->pipe, &pstip_fs); 365 366 FREE((void *)pstip_fs.tokens); 367 return TRUE; 368} 369 370 371/** 372 * Load texture image with current stipple pattern. 373 */ 374static void 375pstip_update_texture(struct pstip_stage *pstip) 376{ 377 static const uint bit31 = 1 << 31; 378 struct pipe_context *pipe = pstip->pipe; 379 struct pipe_screen *screen = pipe->screen; 380 struct pipe_transfer *transfer; 381 const uint *stipple = pstip->state.stipple->stipple; 382 uint i, j; 383 ubyte *data; 384 385 /* XXX: want to avoid flushing just because we use stipple: 386 */ 387 pipe->flush( pipe, PIPE_FLUSH_TEXTURE_CACHE, NULL ); 388 389 transfer = screen->get_tex_transfer(screen, pstip->texture, 0, 0, 0, 390 PIPE_TRANSFER_WRITE, 0, 0, 32, 32); 391 data = screen->transfer_map(screen, transfer); 392 393 /* 394 * Load alpha texture. 395 * Note: 0 means keep the fragment, 255 means kill it. 396 * We'll negate the texel value and use KILP which kills if value 397 * is negative. 398 */ 399 for (i = 0; i < 32; i++) { 400 for (j = 0; j < 32; j++) { 401 if (stipple[i] & (bit31 >> j)) { 402 /* fragment "on" */ 403 data[i * transfer->stride + j] = 0; 404 } 405 else { 406 /* fragment "off" */ 407 data[i * transfer->stride + j] = 255; 408 } 409 } 410 } 411 412 /* unmap */ 413 screen->transfer_unmap(screen, transfer); 414 screen->tex_transfer_destroy(transfer); 415} 416 417 418/** 419 * Create the texture map we'll use for stippling. 420 */ 421static boolean 422pstip_create_texture(struct pstip_stage *pstip) 423{ 424 struct pipe_context *pipe = pstip->pipe; 425 struct pipe_screen *screen = pipe->screen; 426 struct pipe_texture texTemp; 427 428 memset(&texTemp, 0, sizeof(texTemp)); 429 texTemp.target = PIPE_TEXTURE_2D; 430 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */ 431 texTemp.last_level = 0; 432 texTemp.width0 = 32; 433 texTemp.height0 = 32; 434 texTemp.depth0 = 1; 435 util_format_get_block(texTemp.format, &texTemp.block); 436 437 pstip->texture = screen->texture_create(screen, &texTemp); 438 if (pstip->texture == NULL) 439 return FALSE; 440 441 return TRUE; 442} 443 444 445/** 446 * Create the sampler CSO that'll be used for stippling. 447 */ 448static boolean 449pstip_create_sampler(struct pstip_stage *pstip) 450{ 451 struct pipe_sampler_state sampler; 452 struct pipe_context *pipe = pstip->pipe; 453 454 memset(&sampler, 0, sizeof(sampler)); 455 sampler.wrap_s = PIPE_TEX_WRAP_REPEAT; 456 sampler.wrap_t = PIPE_TEX_WRAP_REPEAT; 457 sampler.wrap_r = PIPE_TEX_WRAP_REPEAT; 458 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 459 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 460 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 461 sampler.normalized_coords = 1; 462 sampler.min_lod = 0.0f; 463 sampler.max_lod = 0.0f; 464 465 pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler); 466 if (pstip->sampler_cso == NULL) 467 return FALSE; 468 469 return TRUE; 470} 471 472 473/** 474 * When we're about to draw our first stipple polygon in a batch, this function 475 * is called to tell the driver to bind our modified fragment shader. 476 */ 477static boolean 478bind_pstip_fragment_shader(struct pstip_stage *pstip) 479{ 480 struct draw_context *draw = pstip->stage.draw; 481 if (!pstip->fs->pstip_fs && 482 !generate_pstip_fs(pstip)) 483 return FALSE; 484 485 draw->suspend_flushing = TRUE; 486 pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs); 487 draw->suspend_flushing = FALSE; 488 return TRUE; 489} 490 491 492static INLINE struct pstip_stage * 493pstip_stage( struct draw_stage *stage ) 494{ 495 return (struct pstip_stage *) stage; 496} 497 498 499static void 500pstip_first_tri(struct draw_stage *stage, struct prim_header *header) 501{ 502 struct pstip_stage *pstip = pstip_stage(stage); 503 struct pipe_context *pipe = pstip->pipe; 504 struct draw_context *draw = stage->draw; 505 uint num_samplers; 506 507 assert(stage->draw->rasterizer->poly_stipple_enable); 508 509 /* bind our fragprog */ 510 if (!bind_pstip_fragment_shader(pstip)) { 511 stage->tri = draw_pipe_passthrough_tri; 512 stage->tri(stage, header); 513 return; 514 } 515 516 517 /* how many samplers? */ 518 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */ 519 num_samplers = MAX2(pstip->num_textures, pstip->num_samplers); 520 num_samplers = MAX2(num_samplers, pstip->fs->sampler_unit + 1); 521 522 /* plug in our sampler, texture */ 523 pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso; 524 pipe_texture_reference(&pstip->state.textures[pstip->fs->sampler_unit], 525 pstip->texture); 526 527 assert(num_samplers <= PIPE_MAX_SAMPLERS); 528 529 draw->suspend_flushing = TRUE; 530 pstip->driver_bind_sampler_states(pipe, num_samplers, pstip->state.samplers); 531 pstip->driver_set_sampler_textures(pipe, num_samplers, pstip->state.textures); 532 draw->suspend_flushing = FALSE; 533 534 /* now really draw first triangle */ 535 stage->tri = draw_pipe_passthrough_tri; 536 stage->tri(stage, header); 537} 538 539 540static void 541pstip_flush(struct draw_stage *stage, unsigned flags) 542{ 543 struct draw_context *draw = stage->draw; 544 struct pstip_stage *pstip = pstip_stage(stage); 545 struct pipe_context *pipe = pstip->pipe; 546 547 stage->tri = pstip_first_tri; 548 stage->next->flush( stage->next, flags ); 549 550 /* restore original frag shader, texture, sampler state */ 551 draw->suspend_flushing = TRUE; 552 pstip->driver_bind_fs_state(pipe, pstip->fs->driver_fs); 553 pstip->driver_bind_sampler_states(pipe, pstip->num_samplers, 554 pstip->state.samplers); 555 pstip->driver_set_sampler_textures(pipe, pstip->num_textures, 556 pstip->state.textures); 557 draw->suspend_flushing = FALSE; 558} 559 560 561static void 562pstip_reset_stipple_counter(struct draw_stage *stage) 563{ 564 stage->next->reset_stipple_counter( stage->next ); 565} 566 567 568static void 569pstip_destroy(struct draw_stage *stage) 570{ 571 struct pstip_stage *pstip = pstip_stage(stage); 572 uint i; 573 574 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) { 575 pipe_texture_reference(&pstip->state.textures[i], NULL); 576 } 577 578 pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso); 579 580 pipe_texture_reference(&pstip->texture, NULL); 581 582 draw_free_temp_verts( stage ); 583 FREE( stage ); 584} 585 586 587static struct pstip_stage * 588draw_pstip_stage(struct draw_context *draw) 589{ 590 struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage); 591 592 draw_alloc_temp_verts( &pstip->stage, 8 ); 593 594 pstip->stage.draw = draw; 595 pstip->stage.name = "pstip"; 596 pstip->stage.next = NULL; 597 pstip->stage.point = draw_pipe_passthrough_point; 598 pstip->stage.line = draw_pipe_passthrough_line; 599 pstip->stage.tri = pstip_first_tri; 600 pstip->stage.flush = pstip_flush; 601 pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter; 602 pstip->stage.destroy = pstip_destroy; 603 604 return pstip; 605} 606 607 608static struct pstip_stage * 609pstip_stage_from_pipe(struct pipe_context *pipe) 610{ 611 struct draw_context *draw = (struct draw_context *) pipe->draw; 612 return pstip_stage(draw->pipeline.pstipple); 613} 614 615 616/** 617 * This function overrides the driver's create_fs_state() function and 618 * will typically be called by the state tracker. 619 */ 620static void * 621pstip_create_fs_state(struct pipe_context *pipe, 622 const struct pipe_shader_state *fs) 623{ 624 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 625 struct pstip_fragment_shader *aafs = CALLOC_STRUCT(pstip_fragment_shader); 626 627 if (aafs) { 628 aafs->state = *fs; 629 630 /* pass-through */ 631 aafs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs); 632 } 633 634 return aafs; 635} 636 637 638static void 639pstip_bind_fs_state(struct pipe_context *pipe, void *fs) 640{ 641 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 642 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs; 643 /* save current */ 644 pstip->fs = aafs; 645 /* pass-through */ 646 pstip->driver_bind_fs_state(pstip->pipe, 647 (aafs ? aafs->driver_fs : NULL)); 648} 649 650 651static void 652pstip_delete_fs_state(struct pipe_context *pipe, void *fs) 653{ 654 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 655 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs; 656 /* pass-through */ 657 pstip->driver_delete_fs_state(pstip->pipe, aafs->driver_fs); 658 659 if (aafs->pstip_fs) 660 pstip->driver_delete_fs_state(pstip->pipe, aafs->pstip_fs); 661 662 FREE(aafs); 663} 664 665 666static void 667pstip_bind_sampler_states(struct pipe_context *pipe, 668 unsigned num, void **sampler) 669{ 670 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 671 uint i; 672 673 /* save current */ 674 memcpy(pstip->state.samplers, sampler, num * sizeof(void *)); 675 for (i = num; i < PIPE_MAX_SAMPLERS; i++) { 676 pstip->state.samplers[i] = NULL; 677 } 678 679 pstip->num_samplers = num; 680 /* pass-through */ 681 pstip->driver_bind_sampler_states(pstip->pipe, num, sampler); 682} 683 684 685static void 686pstip_set_sampler_textures(struct pipe_context *pipe, 687 unsigned num, struct pipe_texture **texture) 688{ 689 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 690 uint i; 691 692 /* save current */ 693 for (i = 0; i < num; i++) { 694 pipe_texture_reference(&pstip->state.textures[i], texture[i]); 695 } 696 for (; i < PIPE_MAX_SAMPLERS; i++) { 697 pipe_texture_reference(&pstip->state.textures[i], NULL); 698 } 699 700 pstip->num_textures = num; 701 702 /* pass-through */ 703 pstip->driver_set_sampler_textures(pstip->pipe, num, texture); 704} 705 706 707static void 708pstip_set_polygon_stipple(struct pipe_context *pipe, 709 const struct pipe_poly_stipple *stipple) 710{ 711 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 712 713 /* save current */ 714 pstip->state.stipple = stipple; 715 716 /* pass-through */ 717 pstip->driver_set_polygon_stipple(pstip->pipe, stipple); 718 719 pstip_update_texture(pstip); 720} 721 722 723/** 724 * Called by drivers that want to install this polygon stipple stage 725 * into the draw module's pipeline. This will not be used if the 726 * hardware has native support for polygon stipple. 727 */ 728boolean 729draw_install_pstipple_stage(struct draw_context *draw, 730 struct pipe_context *pipe) 731{ 732 struct pstip_stage *pstip; 733 734 pipe->draw = (void *) draw; 735 736 /* 737 * Create / install pgon stipple drawing / prim stage 738 */ 739 pstip = draw_pstip_stage( draw ); 740 if (pstip == NULL) 741 goto fail; 742 743 draw->pipeline.pstipple = &pstip->stage; 744 745 pstip->pipe = pipe; 746 747 /* create special texture, sampler state */ 748 if (!pstip_create_texture(pstip)) 749 goto fail; 750 751 if (!pstip_create_sampler(pstip)) 752 goto fail; 753 754 /* save original driver functions */ 755 pstip->driver_create_fs_state = pipe->create_fs_state; 756 pstip->driver_bind_fs_state = pipe->bind_fs_state; 757 pstip->driver_delete_fs_state = pipe->delete_fs_state; 758 759 pstip->driver_bind_sampler_states = pipe->bind_fragment_sampler_states; 760 pstip->driver_set_sampler_textures = pipe->set_fragment_sampler_textures; 761 pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple; 762 763 /* override the driver's functions */ 764 pipe->create_fs_state = pstip_create_fs_state; 765 pipe->bind_fs_state = pstip_bind_fs_state; 766 pipe->delete_fs_state = pstip_delete_fs_state; 767 768 pipe->bind_fragment_sampler_states = pstip_bind_sampler_states; 769 pipe->set_fragment_sampler_textures = pstip_set_sampler_textures; 770 pipe->set_polygon_stipple = pstip_set_polygon_stipple; 771 772 return TRUE; 773 774 fail: 775 if (pstip) 776 pstip->stage.destroy( &pstip->stage ); 777 778 return FALSE; 779} 780