Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_aaline.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 * AA line stage: AA lines are converted to texture mapped triangles.
30 *
31 * Authors: Brian Paul
32 */
33
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_shader_tokens.h"
38
39 #include "util/u_format.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42
43 #include "tgsi/tgsi_transform.h"
44 #include "tgsi/tgsi_dump.h"
45
46 #include "draw_context.h"
47 #include "draw_private.h"
48 #include "draw_pipe.h"
49
50
51 /**
52 * Max texture level for the alpha texture used for antialiasing
53 */
54 #define MAX_TEXTURE_LEVEL 5 /* 32 x 32 */
55
56
57 /**
58 * Subclass of pipe_shader_state to carry extra fragment shader info.
59 */
60 struct aaline_fragment_shader
61 {
62 struct pipe_shader_state state;
63 void *driver_fs;
64 void *aaline_fs;
65 uint sampler_unit;
66 int generic_attrib; /**< texcoord/generic used for texture */
67 };
68
69
70 /**
71 * Subclass of draw_stage
72 */
73 struct aaline_stage
74 {
75 struct draw_stage stage;
76
77 float half_line_width;
78
79 /** For AA lines, this is the vertex attrib slot for the new texcoords */
80 uint tex_slot;
81 /** position, not necessarily output zero */
82 uint pos_slot;
83
84 void *sampler_cso;
85 struct pipe_texture *texture;
86 uint num_samplers;
87 uint num_textures;
88
89
90 /*
91 * Currently bound state
92 */
93 struct aaline_fragment_shader *fs;
94 struct {
95 void *sampler[PIPE_MAX_SAMPLERS];
96 struct pipe_texture *texture[PIPE_MAX_SAMPLERS];
97 } state;
98
99 /*
100 * Driver interface/override functions
101 */
102 void * (*driver_create_fs_state)(struct pipe_context *,
103 const struct pipe_shader_state *);
104 void (*driver_bind_fs_state)(struct pipe_context *, void *);
105 void (*driver_delete_fs_state)(struct pipe_context *, void *);
106
107 void (*driver_bind_sampler_states)(struct pipe_context *, unsigned,
108 void **);
109 void (*driver_set_sampler_textures)(struct pipe_context *, unsigned,
110 struct pipe_texture **);
111
112 struct pipe_context *pipe;
113 };
114
115
116
117 /**
118 * Subclass of tgsi_transform_context, used for transforming the
119 * user's fragment shader to add the special AA instructions.
120 */
121 struct aa_transform_context {
122 struct tgsi_transform_context base;
123 uint tempsUsed; /**< bitmask */
124 int colorOutput; /**< which output is the primary color */
125 uint samplersUsed; /**< bitfield of samplers used */
126 int freeSampler; /** an available sampler for the pstipple */
127 int maxInput, maxGeneric; /**< max input index found */
128 int colorTemp, texTemp; /**< temp registers */
129 boolean firstInstruction;
130 };
131
132
133 /**
134 * TGSI declaration transform callback.
135 * Look for a free sampler, a free input attrib, and two free temp regs.
136 */
137 static void
138 aa_transform_decl(struct tgsi_transform_context *ctx,
139 struct tgsi_full_declaration *decl)
140 {
141 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
142
143 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
144 decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
145 decl->Semantic.Index == 0) {
146 aactx->colorOutput = decl->Range.First;
147 }
148 else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
149 uint i;
150 for (i = decl->Range.First;
151 i <= decl->Range.Last; i++) {
152 aactx->samplersUsed |= 1 << i;
153 }
154 }
155 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
156 if ((int) decl->Range.Last > aactx->maxInput)
157 aactx->maxInput = decl->Range.Last;
158 if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
159 (int) decl->Semantic.Index > aactx->maxGeneric) {
160 aactx->maxGeneric = decl->Semantic.Index;
161 }
162 }
163 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
164 uint i;
165 for (i = decl->Range.First;
166 i <= decl->Range.Last; i++) {
167 aactx->tempsUsed |= (1 << i);
168 }
169 }
170
171 ctx->emit_declaration(ctx, decl);
172 }
173
174
175 /**
176 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
177 */
178 static int
179 free_bit(uint bitfield)
180 {
181 int i;
182 for (i = 0; i < 32; i++) {
183 if ((bitfield & (1 << i)) == 0)
184 return i;
185 }
186 return -1;
187 }
188
189
190 /**
191 * TGSI instruction transform callback.
192 * Replace writes to result.color w/ a temp reg.
193 * Upon END instruction, insert texture sampling code for antialiasing.
194 */
195 static void
196 aa_transform_inst(struct tgsi_transform_context *ctx,
197 struct tgsi_full_instruction *inst)
198 {
199 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
200
201 if (aactx->firstInstruction) {
202 /* emit our new declarations before the first instruction */
203
204 struct tgsi_full_declaration decl;
205 uint i;
206
207 /* find free sampler */
208 aactx->freeSampler = free_bit(aactx->samplersUsed);
209 if (aactx->freeSampler >= PIPE_MAX_SAMPLERS)
210 aactx->freeSampler = PIPE_MAX_SAMPLERS - 1;
211
212 /* find two free temp regs */
213 for (i = 0; i < 32; i++) {
214 if ((aactx->tempsUsed & (1 << i)) == 0) {
215 /* found a free temp */
216 if (aactx->colorTemp < 0)
217 aactx->colorTemp = i;
218 else if (aactx->texTemp < 0)
219 aactx->texTemp = i;
220 else
221 break;
222 }
223 }
224 assert(aactx->colorTemp >= 0);
225 assert(aactx->texTemp >= 0);
226
227 /* declare new generic input/texcoord */
228 decl = tgsi_default_full_declaration();
229 decl.Declaration.File = TGSI_FILE_INPUT;
230 /* XXX this could be linear... */
231 decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
232 decl.Declaration.Semantic = 1;
233 decl.Semantic.Name = TGSI_SEMANTIC_GENERIC;
234 decl.Semantic.Index = aactx->maxGeneric + 1;
235 decl.Range.First =
236 decl.Range.Last = aactx->maxInput + 1;
237 ctx->emit_declaration(ctx, &decl);
238
239 /* declare new sampler */
240 decl = tgsi_default_full_declaration();
241 decl.Declaration.File = TGSI_FILE_SAMPLER;
242 decl.Range.First =
243 decl.Range.Last = aactx->freeSampler;
244 ctx->emit_declaration(ctx, &decl);
245
246 /* declare new temp regs */
247 decl = tgsi_default_full_declaration();
248 decl.Declaration.File = TGSI_FILE_TEMPORARY;
249 decl.Range.First =
250 decl.Range.Last = aactx->texTemp;
251 ctx->emit_declaration(ctx, &decl);
252
253 decl = tgsi_default_full_declaration();
254 decl.Declaration.File = TGSI_FILE_TEMPORARY;
255 decl.Range.First =
256 decl.Range.Last = aactx->colorTemp;
257 ctx->emit_declaration(ctx, &decl);
258
259 aactx->firstInstruction = FALSE;
260 }
261
262 if (inst->Instruction.Opcode == TGSI_OPCODE_END &&
263 aactx->colorOutput != -1) {
264 struct tgsi_full_instruction newInst;
265
266 /* TEX */
267 newInst = tgsi_default_full_instruction();
268 newInst.Instruction.Opcode = TGSI_OPCODE_TEX;
269 newInst.Instruction.NumDstRegs = 1;
270 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
271 newInst.Dst[0].Register.Index = aactx->texTemp;
272 newInst.Instruction.NumSrcRegs = 2;
273 newInst.Instruction.Texture = TRUE;
274 newInst.Texture.Texture = TGSI_TEXTURE_2D;
275 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
276 newInst.Src[0].Register.Index = aactx->maxInput + 1;
277 newInst.Src[1].Register.File = TGSI_FILE_SAMPLER;
278 newInst.Src[1].Register.Index = aactx->freeSampler;
279
280 ctx->emit_instruction(ctx, &newInst);
281
282 /* MOV rgb */
283 newInst = tgsi_default_full_instruction();
284 newInst.Instruction.Opcode = TGSI_OPCODE_MOV;
285 newInst.Instruction.NumDstRegs = 1;
286 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
287 newInst.Dst[0].Register.Index = aactx->colorOutput;
288 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
289 newInst.Instruction.NumSrcRegs = 1;
290 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
291 newInst.Src[0].Register.Index = aactx->colorTemp;
292 ctx->emit_instruction(ctx, &newInst);
293
294 /* MUL alpha */
295 newInst = tgsi_default_full_instruction();
296 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
297 newInst.Instruction.NumDstRegs = 1;
298 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
299 newInst.Dst[0].Register.Index = aactx->colorOutput;
300 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
301 newInst.Instruction.NumSrcRegs = 2;
302 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
303 newInst.Src[0].Register.Index = aactx->colorTemp;
304 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
305 newInst.Src[1].Register.Index = aactx->texTemp;
306 ctx->emit_instruction(ctx, &newInst);
307
308 /* END */
309 newInst = tgsi_default_full_instruction();
310 newInst.Instruction.Opcode = TGSI_OPCODE_END;
311 newInst.Instruction.NumDstRegs = 0;
312 newInst.Instruction.NumSrcRegs = 0;
313 ctx->emit_instruction(ctx, &newInst);
314 }
315 else {
316 /* Not an END instruction.
317 * Look for writes to result.color and replace with colorTemp reg.
318 */
319 uint i;
320
321 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
322 struct tgsi_full_dst_register *dst = &inst->Dst[i];
323 if (dst->Register.File == TGSI_FILE_OUTPUT &&
324 dst->Register.Index == aactx->colorOutput) {
325 dst->Register.File = TGSI_FILE_TEMPORARY;
326 dst->Register.Index = aactx->colorTemp;
327 }
328 }
329
330 ctx->emit_instruction(ctx, inst);
331 }
332 }
333
334
335 /**
336 * Generate the frag shader we'll use for drawing AA lines.
337 * This will be the user's shader plus some texture/modulate instructions.
338 */
339 static boolean
340 generate_aaline_fs(struct aaline_stage *aaline)
341 {
342 const struct pipe_shader_state *orig_fs = &aaline->fs->state;
343 struct pipe_shader_state aaline_fs;
344 struct aa_transform_context transform;
345
346 #define MAX 1000
347
348 aaline_fs = *orig_fs; /* copy to init */
349 aaline_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX);
350 if (aaline_fs.tokens == NULL)
351 return FALSE;
352
353 memset(&transform, 0, sizeof(transform));
354 transform.colorOutput = -1;
355 transform.maxInput = -1;
356 transform.maxGeneric = -1;
357 transform.colorTemp = -1;
358 transform.texTemp = -1;
359 transform.firstInstruction = TRUE;
360 transform.base.transform_instruction = aa_transform_inst;
361 transform.base.transform_declaration = aa_transform_decl;
362
363 tgsi_transform_shader(orig_fs->tokens,
364 (struct tgsi_token *) aaline_fs.tokens,
365 MAX, &transform.base);
366
367 #if 0 /* DEBUG */
368 tgsi_dump(orig_fs->tokens, 0);
369 tgsi_dump(aaline_fs.tokens, 0);
370 #endif
371
372 aaline->fs->sampler_unit = transform.freeSampler;
373
374 aaline->fs->aaline_fs
375 = aaline->driver_create_fs_state(aaline->pipe, &aaline_fs);
376 if (aaline->fs->aaline_fs == NULL)
377 goto fail;
378
379 aaline->fs->generic_attrib = transform.maxGeneric + 1;
380 FREE((void *)aaline_fs.tokens);
381 return TRUE;
382
383 fail:
384 FREE((void *)aaline_fs.tokens);
385 return FALSE;
386 }
387
388
389 /**
390 * Create the texture map we'll use for antialiasing the lines.
391 */
392 static boolean
393 aaline_create_texture(struct aaline_stage *aaline)
394 {
395 struct pipe_context *pipe = aaline->pipe;
396 struct pipe_screen *screen = pipe->screen;
397 struct pipe_texture texTemp;
398 uint level;
399
400 memset(&texTemp, 0, sizeof(texTemp));
401 texTemp.target = PIPE_TEXTURE_2D;
402 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
403 texTemp.last_level = MAX_TEXTURE_LEVEL;
404 texTemp.width0 = 1 << MAX_TEXTURE_LEVEL;
405 texTemp.height0 = 1 << MAX_TEXTURE_LEVEL;
406 texTemp.depth0 = 1;
407
408 aaline->texture = screen->texture_create(screen, &texTemp);
409 if (!aaline->texture)
410 return FALSE;
411
412 /* Fill in mipmap images.
413 * Basically each level is solid opaque, except for the outermost
414 * texels which are zero. Special case the 1x1 and 2x2 levels.
415 */
416 for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
417 struct pipe_transfer *transfer;
418 const uint size = u_minify(aaline->texture->width0, level);
419 ubyte *data;
420 uint i, j;
421
422 assert(aaline->texture->width0 == aaline->texture->height0);
423
424 /* This texture is new, no need to flush.
425 */
426 transfer = screen->get_tex_transfer(screen, aaline->texture, 0, level, 0,
427 PIPE_TRANSFER_WRITE, 0, 0, size, size);
428 data = screen->transfer_map(screen, transfer);
429 if (data == NULL)
430 return FALSE;
431
432 for (i = 0; i < size; i++) {
433 for (j = 0; j < size; j++) {
434 ubyte d;
435 if (size == 1) {
436 d = 255;
437 }
438 else if (size == 2) {
439 d = 200; /* tuneable */
440 }
441 else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
442 d = 0;
443 }
444 else {
445 d = 255;
446 }
447 data[i * transfer->stride + j] = d;
448 }
449 }
450
451 /* unmap */
452 screen->transfer_unmap(screen, transfer);
453 screen->tex_transfer_destroy(transfer);
454 }
455 return TRUE;
456 }
457
458
459 /**
460 * Create the sampler CSO that'll be used for antialiasing.
461 * By using a mipmapped texture, we don't have to generate a different
462 * texture image for each line size.
463 */
464 static boolean
465 aaline_create_sampler(struct aaline_stage *aaline)
466 {
467 struct pipe_sampler_state sampler;
468 struct pipe_context *pipe = aaline->pipe;
469
470 memset(&sampler, 0, sizeof(sampler));
471 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
472 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
473 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
474 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
475 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
476 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
477 sampler.normalized_coords = 1;
478 sampler.min_lod = 0.0f;
479 sampler.max_lod = MAX_TEXTURE_LEVEL;
480
481 aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
482 if (aaline->sampler_cso == NULL)
483 return FALSE;
484
485 return TRUE;
486 }
487
488
489 /**
490 * When we're about to draw our first AA line in a batch, this function is
491 * called to tell the driver to bind our modified fragment shader.
492 */
493 static boolean
494 bind_aaline_fragment_shader(struct aaline_stage *aaline)
495 {
496 struct draw_context *draw = aaline->stage.draw;
497
498 if (!aaline->fs->aaline_fs &&
499 !generate_aaline_fs(aaline))
500 return FALSE;
501
502 draw->suspend_flushing = TRUE;
503 aaline->driver_bind_fs_state(aaline->pipe, aaline->fs->aaline_fs);
504 draw->suspend_flushing = FALSE;
505
506 return TRUE;
507 }
508
509
510
511 static INLINE struct aaline_stage *
512 aaline_stage( struct draw_stage *stage )
513 {
514 return (struct aaline_stage *) stage;
515 }
516
517
518 /**
519 * Draw a wide line by drawing a quad, using geometry which will
520 * fullfill GL's antialiased line requirements.
521 */
522 static void
523 aaline_line(struct draw_stage *stage, struct prim_header *header)
524 {
525 const struct aaline_stage *aaline = aaline_stage(stage);
526 const float half_width = aaline->half_line_width;
527 struct prim_header tri;
528 struct vertex_header *v[8];
529 uint texPos = aaline->tex_slot;
530 uint posPos = aaline->pos_slot;
531 float *pos, *tex;
532 float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
533 float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
534 double a = atan2(dy, dx);
535 float c_a = (float) cos(a), s_a = (float) sin(a);
536 uint i;
537
538 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
539 dx = 0.5F * half_width;
540 dy = half_width;
541
542 /* allocate/dup new verts */
543 for (i = 0; i < 8; i++) {
544 v[i] = dup_vert(stage, header->v[i/4], i);
545 }
546
547 /*
548 * Quad strip for line from v0 to v1 (*=endpoints):
549 *
550 * 1 3 5 7
551 * +---+---------------------+---+
552 * | |
553 * | *v0 v1* |
554 * | |
555 * +---+---------------------+---+
556 * 0 2 4 6
557 */
558
559 /* new verts */
560 pos = v[0]->data[posPos];
561 pos[0] += (-dx * c_a - dy * s_a);
562 pos[1] += (-dx * s_a + dy * c_a);
563
564 pos = v[1]->data[posPos];
565 pos[0] += (-dx * c_a - -dy * s_a);
566 pos[1] += (-dx * s_a + -dy * c_a);
567
568 pos = v[2]->data[posPos];
569 pos[0] += ( dx * c_a - dy * s_a);
570 pos[1] += ( dx * s_a + dy * c_a);
571
572 pos = v[3]->data[posPos];
573 pos[0] += ( dx * c_a - -dy * s_a);
574 pos[1] += ( dx * s_a + -dy * c_a);
575
576 pos = v[4]->data[posPos];
577 pos[0] += (-dx * c_a - dy * s_a);
578 pos[1] += (-dx * s_a + dy * c_a);
579
580 pos = v[5]->data[posPos];
581 pos[0] += (-dx * c_a - -dy * s_a);
582 pos[1] += (-dx * s_a + -dy * c_a);
583
584 pos = v[6]->data[posPos];
585 pos[0] += ( dx * c_a - dy * s_a);
586 pos[1] += ( dx * s_a + dy * c_a);
587
588 pos = v[7]->data[posPos];
589 pos[0] += ( dx * c_a - -dy * s_a);
590 pos[1] += ( dx * s_a + -dy * c_a);
591
592 /* new texcoords */
593 tex = v[0]->data[texPos];
594 ASSIGN_4V(tex, 0, 0, 0, 1);
595
596 tex = v[1]->data[texPos];
597 ASSIGN_4V(tex, 0, 1, 0, 1);
598
599 tex = v[2]->data[texPos];
600 ASSIGN_4V(tex, .5, 0, 0, 1);
601
602 tex = v[3]->data[texPos];
603 ASSIGN_4V(tex, .5, 1, 0, 1);
604
605 tex = v[4]->data[texPos];
606 ASSIGN_4V(tex, .5, 0, 0, 1);
607
608 tex = v[5]->data[texPos];
609 ASSIGN_4V(tex, .5, 1, 0, 1);
610
611 tex = v[6]->data[texPos];
612 ASSIGN_4V(tex, 1, 0, 0, 1);
613
614 tex = v[7]->data[texPos];
615 ASSIGN_4V(tex, 1, 1, 0, 1);
616
617 /* emit 6 tris for the quad strip */
618 tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
619 stage->next->tri( stage->next, &tri );
620
621 tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
622 stage->next->tri( stage->next, &tri );
623
624 tri.v[0] = v[4]; tri.v[1] = v[3]; tri.v[2] = v[2];
625 stage->next->tri( stage->next, &tri );
626
627 tri.v[0] = v[5]; tri.v[1] = v[3]; tri.v[2] = v[4];
628 stage->next->tri( stage->next, &tri );
629
630 tri.v[0] = v[6]; tri.v[1] = v[5]; tri.v[2] = v[4];
631 stage->next->tri( stage->next, &tri );
632
633 tri.v[0] = v[7]; tri.v[1] = v[5]; tri.v[2] = v[6];
634 stage->next->tri( stage->next, &tri );
635 }
636
637
638 static void
639 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
640 {
641 auto struct aaline_stage *aaline = aaline_stage(stage);
642 struct draw_context *draw = stage->draw;
643 struct pipe_context *pipe = aaline->pipe;
644 uint num_samplers;
645
646 assert(draw->rasterizer->line_smooth);
647
648 if (draw->rasterizer->line_width <= 3.0)
649 aaline->half_line_width = 1.5f;
650 else
651 aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
652
653 /*
654 * Bind (generate) our fragprog, sampler and texture
655 */
656 if (!bind_aaline_fragment_shader(aaline)) {
657 stage->line = draw_pipe_passthrough_line;
658 stage->line(stage, header);
659 return;
660 }
661
662 /* update vertex attrib info */
663 aaline->tex_slot = draw_current_shader_outputs(draw);
664 aaline->pos_slot = draw_current_shader_position_output(draw);;
665
666 /* advertise the extra post-transformed vertex attribute */
667 draw->extra_shader_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
668 draw->extra_shader_outputs.semantic_index = aaline->fs->generic_attrib;
669 draw->extra_shader_outputs.slot = aaline->tex_slot;
670
671 /* how many samplers? */
672 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
673 num_samplers = MAX2(aaline->num_textures, aaline->num_samplers);
674 num_samplers = MAX2(num_samplers, aaline->fs->sampler_unit + 1);
675
676 aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
677 pipe_texture_reference(&aaline->state.texture[aaline->fs->sampler_unit],
678 aaline->texture);
679
680 draw->suspend_flushing = TRUE;
681 aaline->driver_bind_sampler_states(pipe, num_samplers, aaline->state.sampler);
682 aaline->driver_set_sampler_textures(pipe, num_samplers, aaline->state.texture);
683 draw->suspend_flushing = FALSE;
684
685 /* now really draw first line */
686 stage->line = aaline_line;
687 stage->line(stage, header);
688 }
689
690
691 static void
692 aaline_flush(struct draw_stage *stage, unsigned flags)
693 {
694 struct draw_context *draw = stage->draw;
695 struct aaline_stage *aaline = aaline_stage(stage);
696 struct pipe_context *pipe = aaline->pipe;
697
698 stage->line = aaline_first_line;
699 stage->next->flush( stage->next, flags );
700
701 /* restore original frag shader, texture, sampler state */
702 draw->suspend_flushing = TRUE;
703 aaline->driver_bind_fs_state(pipe, aaline->fs->driver_fs);
704 aaline->driver_bind_sampler_states(pipe, aaline->num_samplers,
705 aaline->state.sampler);
706 aaline->driver_set_sampler_textures(pipe, aaline->num_textures,
707 aaline->state.texture);
708 draw->suspend_flushing = FALSE;
709
710 draw->extra_shader_outputs.slot = 0;
711 }
712
713
714 static void
715 aaline_reset_stipple_counter(struct draw_stage *stage)
716 {
717 stage->next->reset_stipple_counter( stage->next );
718 }
719
720
721 static void
722 aaline_destroy(struct draw_stage *stage)
723 {
724 struct aaline_stage *aaline = aaline_stage(stage);
725 uint i;
726
727 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
728 pipe_texture_reference(&aaline->state.texture[i], NULL);
729 }
730
731 if (aaline->sampler_cso)
732 aaline->pipe->delete_sampler_state(aaline->pipe, aaline->sampler_cso);
733
734 if (aaline->texture)
735 pipe_texture_reference(&aaline->texture, NULL);
736
737 draw_free_temp_verts( stage );
738
739 FREE( stage );
740 }
741
742
743 static struct aaline_stage *
744 draw_aaline_stage(struct draw_context *draw)
745 {
746 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
747 if (aaline == NULL)
748 return NULL;
749
750 if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
751 goto fail;
752
753 aaline->stage.draw = draw;
754 aaline->stage.name = "aaline";
755 aaline->stage.next = NULL;
756 aaline->stage.point = draw_pipe_passthrough_point;
757 aaline->stage.line = aaline_first_line;
758 aaline->stage.tri = draw_pipe_passthrough_tri;
759 aaline->stage.flush = aaline_flush;
760 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
761 aaline->stage.destroy = aaline_destroy;
762
763 return aaline;
764
765 fail:
766 if (aaline)
767 aaline_destroy(&aaline->stage);
768
769 return NULL;
770 }
771
772
773 static struct aaline_stage *
774 aaline_stage_from_pipe(struct pipe_context *pipe)
775 {
776 struct draw_context *draw = (struct draw_context *) pipe->draw;
777 return aaline_stage(draw->pipeline.aaline);
778 }
779
780
781 /**
782 * This function overrides the driver's create_fs_state() function and
783 * will typically be called by the state tracker.
784 */
785 static void *
786 aaline_create_fs_state(struct pipe_context *pipe,
787 const struct pipe_shader_state *fs)
788 {
789 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
790 struct aaline_fragment_shader *aafs = CALLOC_STRUCT(aaline_fragment_shader);
791 if (aafs == NULL)
792 return NULL;
793
794 aafs->state = *fs;
795
796 /* pass-through */
797 aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
798
799 return aafs;
800 }
801
802
803 static void
804 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
805 {
806 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
807 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
808
809 /* save current */
810 aaline->fs = aafs;
811 /* pass-through */
812 aaline->driver_bind_fs_state(aaline->pipe,
813 (aafs ? aafs->driver_fs : NULL));
814 }
815
816
817 static void
818 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
819 {
820 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
821 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
822 /* pass-through */
823 aaline->driver_delete_fs_state(aaline->pipe, aafs->driver_fs);
824
825 if (aafs->aaline_fs)
826 aaline->driver_delete_fs_state(aaline->pipe, aafs->aaline_fs);
827
828 FREE(aafs);
829 }
830
831
832 static void
833 aaline_bind_sampler_states(struct pipe_context *pipe,
834 unsigned num, void **sampler)
835 {
836 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
837
838 /* save current */
839 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
840 aaline->num_samplers = num;
841
842 /* pass-through */
843 aaline->driver_bind_sampler_states(aaline->pipe, num, sampler);
844 }
845
846
847 static void
848 aaline_set_sampler_textures(struct pipe_context *pipe,
849 unsigned num, struct pipe_texture **texture)
850 {
851 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
852 uint i;
853
854 /* save current */
855 for (i = 0; i < num; i++) {
856 pipe_texture_reference(&aaline->state.texture[i], texture[i]);
857 }
858 for ( ; i < PIPE_MAX_SAMPLERS; i++) {
859 pipe_texture_reference(&aaline->state.texture[i], NULL);
860 }
861 aaline->num_textures = num;
862
863 /* pass-through */
864 aaline->driver_set_sampler_textures(aaline->pipe, num, texture);
865 }
866
867
868 /**
869 * Called by drivers that want to install this AA line prim stage
870 * into the draw module's pipeline. This will not be used if the
871 * hardware has native support for AA lines.
872 */
873 boolean
874 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
875 {
876 struct aaline_stage *aaline;
877
878 pipe->draw = (void *) draw;
879
880 /*
881 * Create / install AA line drawing / prim stage
882 */
883 aaline = draw_aaline_stage( draw );
884 if (!aaline)
885 goto fail;
886
887 aaline->pipe = pipe;
888
889 /* create special texture, sampler state */
890 if (!aaline_create_texture(aaline))
891 goto fail;
892
893 if (!aaline_create_sampler(aaline))
894 goto fail;
895
896 /* save original driver functions */
897 aaline->driver_create_fs_state = pipe->create_fs_state;
898 aaline->driver_bind_fs_state = pipe->bind_fs_state;
899 aaline->driver_delete_fs_state = pipe->delete_fs_state;
900
901 aaline->driver_bind_sampler_states = pipe->bind_fragment_sampler_states;
902 aaline->driver_set_sampler_textures = pipe->set_fragment_sampler_textures;
903
904 /* override the driver's functions */
905 pipe->create_fs_state = aaline_create_fs_state;
906 pipe->bind_fs_state = aaline_bind_fs_state;
907 pipe->delete_fs_state = aaline_delete_fs_state;
908
909 pipe->bind_fragment_sampler_states = aaline_bind_sampler_states;
910 pipe->set_fragment_sampler_textures = aaline_set_sampler_textures;
911
912 /* Install once everything is known to be OK:
913 */
914 draw->pipeline.aaline = &aaline->stage;
915
916 return TRUE;
917
918 fail:
919 if (aaline)
920 aaline->stage.destroy( &aaline->stage );
921
922 return FALSE;
923 }