draw_pipe_pstipple.c revision e3028baff2a313baac4a5aea494532605bb8f37a
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_inlines.h" 38#include "pipe/p_context.h" 39#include "pipe/p_defines.h" 40#include "pipe/p_shader_tokens.h" 41 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->DeclarationRange.First; 138 i <= decl->DeclarationRange.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->DeclarationRange.Last); 144 if (decl->Semantic.SemanticName == TGSI_SEMANTIC_POSITION) 145 pctx->wincoordInput = (int) decl->DeclarationRange.First; 146 } 147 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) { 148 uint i; 149 for (i = decl->DeclarationRange.First; 150 i <= decl->DeclarationRange.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.SemanticName = TGSI_SEMANTIC_POSITION; 231 decl.Semantic.SemanticIndex = 0; 232 decl.DeclarationRange.First = 233 decl.DeclarationRange.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.DeclarationRange.First = 241 decl.DeclarationRange.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.DeclarationRange.First = 248 decl.DeclarationRange.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.Pointer = (void *) value; 261 ctx->emit_immediate(ctx, &immed); 262 } 263 264 pctx->firstInstruction = FALSE; 265 266 267 /* 268 * Insert new MUL/TEX/KILP instructions at start of program 269 * Take gl_FragCoord, divide by 32 (stipple size), sample the 270 * texture and kill fragment if needed. 271 * 272 * We'd like to use non-normalized texcoords to index into a RECT 273 * texture, but we can only use GL_REPEAT wrap mode with normalized 274 * texcoords. Darn. 275 */ 276 277 /* MUL texTemp, INPUT[wincoord], 1/32; */ 278 newInst = tgsi_default_full_instruction(); 279 newInst.Instruction.Opcode = TGSI_OPCODE_MUL; 280 newInst.Instruction.NumDstRegs = 1; 281 newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY; 282 newInst.FullDstRegisters[0].DstRegister.Index = pctx->texTemp; 283 newInst.Instruction.NumSrcRegs = 2; 284 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT; 285 newInst.FullSrcRegisters[0].SrcRegister.Index = wincoordInput; 286 newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_IMMEDIATE; 287 newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->numImmed; 288 ctx->emit_instruction(ctx, &newInst); 289 290 /* TEX texTemp, texTemp, sampler; */ 291 newInst = tgsi_default_full_instruction(); 292 newInst.Instruction.Opcode = TGSI_OPCODE_TEX; 293 newInst.Instruction.NumDstRegs = 1; 294 newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY; 295 newInst.FullDstRegisters[0].DstRegister.Index = pctx->texTemp; 296 newInst.Instruction.NumSrcRegs = 2; 297 newInst.InstructionExtTexture.Texture = TGSI_TEXTURE_2D; 298 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY; 299 newInst.FullSrcRegisters[0].SrcRegister.Index = pctx->texTemp; 300 newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER; 301 newInst.FullSrcRegisters[1].SrcRegister.Index = pctx->freeSampler; 302 ctx->emit_instruction(ctx, &newInst); 303 304 /* KIL -texTemp; # if -texTemp < 0, KILL fragment */ 305 newInst = tgsi_default_full_instruction(); 306 newInst.Instruction.Opcode = TGSI_OPCODE_KIL; 307 newInst.Instruction.NumDstRegs = 0; 308 newInst.Instruction.NumSrcRegs = 1; 309 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY; 310 newInst.FullSrcRegisters[0].SrcRegister.Index = pctx->texTemp; 311 newInst.FullSrcRegisters[0].SrcRegister.Negate = 1; 312 ctx->emit_instruction(ctx, &newInst); 313 } 314 315 /* emit this instruction */ 316 ctx->emit_instruction(ctx, inst); 317} 318 319 320/** 321 * Generate the frag shader we'll use for doing polygon stipple. 322 * This will be the user's shader prefixed with a TEX and KIL instruction. 323 */ 324static boolean 325generate_pstip_fs(struct pstip_stage *pstip) 326{ 327 const struct pipe_shader_state *orig_fs = &pstip->fs->state; 328 /*struct draw_context *draw = pstip->stage.draw;*/ 329 struct pipe_shader_state pstip_fs; 330 struct pstip_transform_context transform; 331 332#define MAX 1000 333 334 pstip_fs = *orig_fs; /* copy to init */ 335 pstip_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX); 336 if (pstip_fs.tokens == NULL) 337 return FALSE; 338 339 memset(&transform, 0, sizeof(transform)); 340 transform.wincoordInput = -1; 341 transform.maxInput = -1; 342 transform.texTemp = -1; 343 transform.firstInstruction = TRUE; 344 transform.base.transform_instruction = pstip_transform_inst; 345 transform.base.transform_declaration = pstip_transform_decl; 346 transform.base.transform_immediate = pstip_transform_immed; 347 348 tgsi_transform_shader(orig_fs->tokens, 349 (struct tgsi_token *) pstip_fs.tokens, 350 MAX, &transform.base); 351 352#if 0 /* DEBUG */ 353 tgsi_dump(orig_fs->tokens, 0); 354 tgsi_dump(pstip_fs.tokens, 0); 355#endif 356 357 pstip->fs->sampler_unit = transform.freeSampler; 358 assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS); 359 360 pstip->fs->pstip_fs = pstip->driver_create_fs_state(pstip->pipe, &pstip_fs); 361 362 return TRUE; 363} 364 365 366/** 367 * Load texture image with current stipple pattern. 368 */ 369static void 370pstip_update_texture(struct pstip_stage *pstip) 371{ 372 static const uint bit31 = 1 << 31; 373 struct pipe_context *pipe = pstip->pipe; 374 struct pipe_screen *screen = pipe->screen; 375 struct pipe_surface *surface; 376 const uint *stipple = pstip->state.stipple->stipple; 377 uint i, j; 378 ubyte *data; 379 380 /* XXX: want to avoid flushing just because we use stipple: 381 */ 382 pipe->flush( pipe, PIPE_FLUSH_TEXTURE_CACHE, NULL ); 383 384 surface = screen->get_tex_surface(screen, pstip->texture, 0, 0, 0, 385 PIPE_BUFFER_USAGE_CPU_WRITE); 386 data = screen->surface_map(screen, surface, 387 PIPE_BUFFER_USAGE_CPU_WRITE); 388 389 /* 390 * Load alpha texture. 391 * Note: 0 means keep the fragment, 255 means kill it. 392 * We'll negate the texel value and use KILP which kills if value 393 * is negative. 394 */ 395 for (i = 0; i < 32; i++) { 396 for (j = 0; j < 32; j++) { 397 if (stipple[i] & (bit31 >> j)) { 398 /* fragment "on" */ 399 data[i * surface->stride + j] = 0; 400 } 401 else { 402 /* fragment "off" */ 403 data[i * surface->stride + j] = 255; 404 } 405 } 406 } 407 408 /* unmap */ 409 screen->surface_unmap(screen, surface); 410 screen->tex_surface_release(screen, &surface); 411} 412 413 414/** 415 * Create the texture map we'll use for stippling. 416 */ 417static boolean 418pstip_create_texture(struct pstip_stage *pstip) 419{ 420 struct pipe_context *pipe = pstip->pipe; 421 struct pipe_screen *screen = pipe->screen; 422 struct pipe_texture texTemp; 423 424 memset(&texTemp, 0, sizeof(texTemp)); 425 texTemp.target = PIPE_TEXTURE_2D; 426 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */ 427 texTemp.last_level = 0; 428 texTemp.width[0] = 32; 429 texTemp.height[0] = 32; 430 texTemp.depth[0] = 1; 431 pf_get_block(texTemp.format, &texTemp.block); 432 433 pstip->texture = screen->texture_create(screen, &texTemp); 434 if (pstip->texture == NULL) 435 return FALSE; 436 437 return TRUE; 438} 439 440 441/** 442 * Create the sampler CSO that'll be used for stippling. 443 */ 444static boolean 445pstip_create_sampler(struct pstip_stage *pstip) 446{ 447 struct pipe_sampler_state sampler; 448 struct pipe_context *pipe = pstip->pipe; 449 450 memset(&sampler, 0, sizeof(sampler)); 451 sampler.wrap_s = PIPE_TEX_WRAP_REPEAT; 452 sampler.wrap_t = PIPE_TEX_WRAP_REPEAT; 453 sampler.wrap_r = PIPE_TEX_WRAP_REPEAT; 454 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 455 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 456 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 457 sampler.normalized_coords = 1; 458 sampler.min_lod = 0.0f; 459 sampler.max_lod = 0.0f; 460 461 pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler); 462 if (pstip->sampler_cso == NULL) 463 return FALSE; 464 465 return TRUE; 466} 467 468 469/** 470 * When we're about to draw our first stipple polygon in a batch, this function 471 * is called to tell the driver to bind our modified fragment shader. 472 */ 473static boolean 474bind_pstip_fragment_shader(struct pstip_stage *pstip) 475{ 476 struct draw_context *draw = pstip->stage.draw; 477 if (!pstip->fs->pstip_fs && 478 !generate_pstip_fs(pstip)) 479 return FALSE; 480 481 draw->suspend_flushing = TRUE; 482 pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs); 483 draw->suspend_flushing = FALSE; 484 return TRUE; 485} 486 487 488static INLINE struct pstip_stage * 489pstip_stage( struct draw_stage *stage ) 490{ 491 return (struct pstip_stage *) stage; 492} 493 494 495static void 496pstip_first_tri(struct draw_stage *stage, struct prim_header *header) 497{ 498 struct pstip_stage *pstip = pstip_stage(stage); 499 struct pipe_context *pipe = pstip->pipe; 500 struct draw_context *draw = stage->draw; 501 uint num_samplers; 502 503 assert(stage->draw->rasterizer->poly_stipple_enable); 504 505 /* bind our fragprog */ 506 if (!bind_pstip_fragment_shader(pstip)) { 507 stage->tri = draw_pipe_passthrough_tri; 508 stage->tri(stage, header); 509 return; 510 } 511 512 513 /* how many samplers? */ 514 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */ 515 num_samplers = MAX2(pstip->num_textures, pstip->num_samplers); 516 num_samplers = MAX2(num_samplers, pstip->fs->sampler_unit + 1); 517 518 /* plug in our sampler, texture */ 519 pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso; 520 pipe_texture_reference(&pstip->state.textures[pstip->fs->sampler_unit], 521 pstip->texture); 522 523 assert(num_samplers <= PIPE_MAX_SAMPLERS); 524 525 draw->suspend_flushing = TRUE; 526 pstip->driver_bind_sampler_states(pipe, num_samplers, pstip->state.samplers); 527 pstip->driver_set_sampler_textures(pipe, num_samplers, pstip->state.textures); 528 draw->suspend_flushing = FALSE; 529 530 /* now really draw first triangle */ 531 stage->tri = draw_pipe_passthrough_tri; 532 stage->tri(stage, header); 533} 534 535 536static void 537pstip_flush(struct draw_stage *stage, unsigned flags) 538{ 539 struct draw_context *draw = stage->draw; 540 struct pstip_stage *pstip = pstip_stage(stage); 541 struct pipe_context *pipe = pstip->pipe; 542 543 stage->tri = pstip_first_tri; 544 stage->next->flush( stage->next, flags ); 545 546 /* restore original frag shader, texture, sampler state */ 547 draw->suspend_flushing = TRUE; 548 pstip->driver_bind_fs_state(pipe, pstip->fs->driver_fs); 549 pstip->driver_bind_sampler_states(pipe, pstip->num_samplers, 550 pstip->state.samplers); 551 pstip->driver_set_sampler_textures(pipe, pstip->num_textures, 552 pstip->state.textures); 553 draw->suspend_flushing = FALSE; 554} 555 556 557static void 558pstip_reset_stipple_counter(struct draw_stage *stage) 559{ 560 stage->next->reset_stipple_counter( stage->next ); 561} 562 563 564static void 565pstip_destroy(struct draw_stage *stage) 566{ 567 struct pstip_stage *pstip = pstip_stage(stage); 568 uint i; 569 570 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) { 571 pipe_texture_reference(&pstip->state.textures[i], NULL); 572 } 573 574 pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso); 575 576 pipe_texture_release(&pstip->texture); 577 578 draw_free_temp_verts( stage ); 579 FREE( stage ); 580} 581 582 583static struct pstip_stage * 584draw_pstip_stage(struct draw_context *draw) 585{ 586 struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage); 587 588 draw_alloc_temp_verts( &pstip->stage, 8 ); 589 590 pstip->stage.draw = draw; 591 pstip->stage.next = NULL; 592 pstip->stage.point = draw_pipe_passthrough_point; 593 pstip->stage.line = draw_pipe_passthrough_line; 594 pstip->stage.tri = pstip_first_tri; 595 pstip->stage.flush = pstip_flush; 596 pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter; 597 pstip->stage.destroy = pstip_destroy; 598 599 return pstip; 600} 601 602 603static struct pstip_stage * 604pstip_stage_from_pipe(struct pipe_context *pipe) 605{ 606 struct draw_context *draw = (struct draw_context *) pipe->draw; 607 return pstip_stage(draw->pipeline.pstipple); 608} 609 610 611/** 612 * This function overrides the driver's create_fs_state() function and 613 * will typically be called by the state tracker. 614 */ 615static void * 616pstip_create_fs_state(struct pipe_context *pipe, 617 const struct pipe_shader_state *fs) 618{ 619 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 620 struct pstip_fragment_shader *aafs = CALLOC_STRUCT(pstip_fragment_shader); 621 622 if (aafs) { 623 aafs->state = *fs; 624 625 /* pass-through */ 626 aafs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs); 627 } 628 629 return aafs; 630} 631 632 633static void 634pstip_bind_fs_state(struct pipe_context *pipe, void *fs) 635{ 636 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 637 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs; 638 /* save current */ 639 pstip->fs = aafs; 640 /* pass-through */ 641 pstip->driver_bind_fs_state(pstip->pipe, 642 (aafs ? aafs->driver_fs : NULL)); 643} 644 645 646static void 647pstip_delete_fs_state(struct pipe_context *pipe, void *fs) 648{ 649 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 650 struct pstip_fragment_shader *aafs = (struct pstip_fragment_shader *) fs; 651 /* pass-through */ 652 pstip->driver_delete_fs_state(pstip->pipe, aafs->driver_fs); 653 FREE(aafs); 654} 655 656 657static void 658pstip_bind_sampler_states(struct pipe_context *pipe, 659 unsigned num, void **sampler) 660{ 661 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 662 uint i; 663 664 /* save current */ 665 memcpy(pstip->state.samplers, sampler, num * sizeof(void *)); 666 for (i = num; i < PIPE_MAX_SAMPLERS; i++) { 667 pstip->state.samplers[i] = NULL; 668 } 669 670 pstip->num_samplers = num; 671 /* pass-through */ 672 pstip->driver_bind_sampler_states(pstip->pipe, num, sampler); 673} 674 675 676static void 677pstip_set_sampler_textures(struct pipe_context *pipe, 678 unsigned num, struct pipe_texture **texture) 679{ 680 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 681 uint i; 682 683 /* save current */ 684 for (i = 0; i < num; i++) { 685 pipe_texture_reference(&pstip->state.textures[i], texture[i]); 686 } 687 for (; i < PIPE_MAX_SAMPLERS; i++) { 688 pipe_texture_reference(&pstip->state.textures[i], NULL); 689 } 690 691 pstip->num_textures = num; 692 693 /* pass-through */ 694 pstip->driver_set_sampler_textures(pstip->pipe, num, texture); 695} 696 697 698static void 699pstip_set_polygon_stipple(struct pipe_context *pipe, 700 const struct pipe_poly_stipple *stipple) 701{ 702 struct pstip_stage *pstip = pstip_stage_from_pipe(pipe); 703 704 /* save current */ 705 pstip->state.stipple = stipple; 706 707 /* pass-through */ 708 pstip->driver_set_polygon_stipple(pstip->pipe, stipple); 709 710 pstip_update_texture(pstip); 711} 712 713 714/** 715 * Called by drivers that want to install this polygon stipple stage 716 * into the draw module's pipeline. This will not be used if the 717 * hardware has native support for polygon stipple. 718 */ 719boolean 720draw_install_pstipple_stage(struct draw_context *draw, 721 struct pipe_context *pipe) 722{ 723 struct pstip_stage *pstip; 724 725 pipe->draw = (void *) draw; 726 727 /* 728 * Create / install pgon stipple drawing / prim stage 729 */ 730 pstip = draw_pstip_stage( draw ); 731 if (pstip == NULL) 732 goto fail; 733 734 draw->pipeline.pstipple = &pstip->stage; 735 736 pstip->pipe = pipe; 737 738 /* create special texture, sampler state */ 739 if (!pstip_create_texture(pstip)) 740 goto fail; 741 742 if (!pstip_create_sampler(pstip)) 743 goto fail; 744 745 /* save original driver functions */ 746 pstip->driver_create_fs_state = pipe->create_fs_state; 747 pstip->driver_bind_fs_state = pipe->bind_fs_state; 748 pstip->driver_delete_fs_state = pipe->delete_fs_state; 749 750 pstip->driver_bind_sampler_states = pipe->bind_sampler_states; 751 pstip->driver_set_sampler_textures = pipe->set_sampler_textures; 752 pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple; 753 754 /* override the driver's functions */ 755 pipe->create_fs_state = pstip_create_fs_state; 756 pipe->bind_fs_state = pstip_bind_fs_state; 757 pipe->delete_fs_state = pstip_delete_fs_state; 758 759 pipe->bind_sampler_states = pstip_bind_sampler_states; 760 pipe->set_sampler_textures = pstip_set_sampler_textures; 761 pipe->set_polygon_stipple = pstip_set_polygon_stipple; 762 763 return TRUE; 764 765 fail: 766 if (pstip) 767 pstip->stage.destroy( &pstip->stage ); 768 769 return FALSE; 770} 771