Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[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_util.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
40
41 #include "tgsi/util/tgsi_transform.h"
42 #include "tgsi/util/tgsi_dump.h"
43
44 #include "draw_context.h"
45 #include "draw_private.h"
46 #include "draw_pipe.h"
47
48
49 /**
50 * Max texture level for the alpha texture used for antialiasing
51 */
52 #define MAX_TEXTURE_LEVEL 5 /* 32 x 32 */
53
54
55 /**
56 * Subclass of pipe_shader_state to carry extra fragment shader info.
57 */
58 struct aaline_fragment_shader
59 {
60 struct pipe_shader_state state;
61 void *driver_fs;
62 void *aaline_fs;
63 void *aapoint_fs; /* not yet */
64 void *sprite_fs; /* not yet */
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.SemanticName == TGSI_SEMANTIC_COLOR &&
145 decl->Semantic.SemanticIndex == 0) {
146 aactx->colorOutput = decl->DeclarationRange.First;
147 }
148 else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
149 uint i;
150 for (i = decl->DeclarationRange.First;
151 i <= decl->DeclarationRange.Last; i++) {
152 aactx->samplersUsed |= 1 << i;
153 }
154 }
155 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
156 if ((int) decl->DeclarationRange.Last > aactx->maxInput)
157 aactx->maxInput = decl->DeclarationRange.Last;
158 if (decl->Semantic.SemanticName == TGSI_SEMANTIC_GENERIC &&
159 (int) decl->Semantic.SemanticIndex > aactx->maxGeneric) {
160 aactx->maxGeneric = decl->Semantic.SemanticIndex;
161 }
162 }
163 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
164 uint i;
165 for (i = decl->DeclarationRange.First;
166 i <= decl->DeclarationRange.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.SemanticName = TGSI_SEMANTIC_GENERIC;
234 decl.Semantic.SemanticIndex = aactx->maxGeneric + 1;
235 decl.DeclarationRange.First =
236 decl.DeclarationRange.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.DeclarationRange.First =
243 decl.DeclarationRange.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.DeclarationRange.First =
250 decl.DeclarationRange.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.DeclarationRange.First =
256 decl.DeclarationRange.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.FullDstRegisters[0].DstRegister.File = TGSI_FILE_TEMPORARY;
271 newInst.FullDstRegisters[0].DstRegister.Index = aactx->texTemp;
272 newInst.Instruction.NumSrcRegs = 2;
273 newInst.InstructionExtTexture.Texture = TGSI_TEXTURE_2D;
274 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_INPUT;
275 newInst.FullSrcRegisters[0].SrcRegister.Index = aactx->maxInput + 1;
276 newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
277 newInst.FullSrcRegisters[1].SrcRegister.Index = aactx->freeSampler;
278
279 ctx->emit_instruction(ctx, &newInst);
280
281 /* MOV rgb */
282 newInst = tgsi_default_full_instruction();
283 newInst.Instruction.Opcode = TGSI_OPCODE_MOV;
284 newInst.Instruction.NumDstRegs = 1;
285 newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
286 newInst.FullDstRegisters[0].DstRegister.Index = aactx->colorOutput;
287 newInst.FullDstRegisters[0].DstRegister.WriteMask = TGSI_WRITEMASK_XYZ;
288 newInst.Instruction.NumSrcRegs = 1;
289 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
290 newInst.FullSrcRegisters[0].SrcRegister.Index = aactx->colorTemp;
291 ctx->emit_instruction(ctx, &newInst);
292
293 /* MUL alpha */
294 newInst = tgsi_default_full_instruction();
295 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
296 newInst.Instruction.NumDstRegs = 1;
297 newInst.FullDstRegisters[0].DstRegister.File = TGSI_FILE_OUTPUT;
298 newInst.FullDstRegisters[0].DstRegister.Index = aactx->colorOutput;
299 newInst.FullDstRegisters[0].DstRegister.WriteMask = TGSI_WRITEMASK_W;
300 newInst.Instruction.NumSrcRegs = 2;
301 newInst.FullSrcRegisters[0].SrcRegister.File = TGSI_FILE_TEMPORARY;
302 newInst.FullSrcRegisters[0].SrcRegister.Index = aactx->colorTemp;
303 newInst.FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_TEMPORARY;
304 newInst.FullSrcRegisters[1].SrcRegister.Index = aactx->texTemp;
305 ctx->emit_instruction(ctx, &newInst);
306
307 /* END */
308 newInst = tgsi_default_full_instruction();
309 newInst.Instruction.Opcode = TGSI_OPCODE_END;
310 newInst.Instruction.NumDstRegs = 0;
311 newInst.Instruction.NumSrcRegs = 0;
312 ctx->emit_instruction(ctx, &newInst);
313 }
314 else {
315 /* Not an END instruction.
316 * Look for writes to result.color and replace with colorTemp reg.
317 */
318 uint i;
319
320 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
321 struct tgsi_full_dst_register *dst = &inst->FullDstRegisters[i];
322 if (dst->DstRegister.File == TGSI_FILE_OUTPUT &&
323 dst->DstRegister.Index == aactx->colorOutput) {
324 dst->DstRegister.File = TGSI_FILE_TEMPORARY;
325 dst->DstRegister.Index = aactx->colorTemp;
326 }
327 }
328
329 ctx->emit_instruction(ctx, inst);
330 }
331 }
332
333
334 /**
335 * Generate the frag shader we'll use for drawing AA lines.
336 * This will be the user's shader plus some texture/modulate instructions.
337 */
338 static boolean
339 generate_aaline_fs(struct aaline_stage *aaline)
340 {
341 const struct pipe_shader_state *orig_fs = &aaline->fs->state;
342 struct pipe_shader_state aaline_fs;
343 struct aa_transform_context transform;
344
345 #define MAX 1000
346
347 aaline_fs = *orig_fs; /* copy to init */
348 aaline_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX);
349 if (aaline_fs.tokens == NULL)
350 return FALSE;
351
352 memset(&transform, 0, sizeof(transform));
353 transform.colorOutput = -1;
354 transform.maxInput = -1;
355 transform.maxGeneric = -1;
356 transform.colorTemp = -1;
357 transform.texTemp = -1;
358 transform.firstInstruction = TRUE;
359 transform.base.transform_instruction = aa_transform_inst;
360 transform.base.transform_declaration = aa_transform_decl;
361
362 tgsi_transform_shader(orig_fs->tokens,
363 (struct tgsi_token *) aaline_fs.tokens,
364 MAX, &transform.base);
365
366 #if 0 /* DEBUG */
367 tgsi_dump(orig_fs->tokens, 0);
368 tgsi_dump(aaline_fs.tokens, 0);
369 #endif
370
371 aaline->fs->sampler_unit = transform.freeSampler;
372
373 aaline->fs->aaline_fs
374 = aaline->driver_create_fs_state(aaline->pipe, &aaline_fs);
375 if (aaline->fs->aaline_fs == NULL)
376 return FALSE;
377
378 aaline->fs->generic_attrib = transform.maxGeneric + 1;
379 return TRUE;
380 }
381
382
383 /**
384 * Create the texture map we'll use for antialiasing the lines.
385 */
386 static boolean
387 aaline_create_texture(struct aaline_stage *aaline)
388 {
389 struct pipe_context *pipe = aaline->pipe;
390 struct pipe_screen *screen = pipe->screen;
391 struct pipe_texture texTemp;
392 uint level;
393
394 memset(&texTemp, 0, sizeof(texTemp));
395 texTemp.target = PIPE_TEXTURE_2D;
396 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
397 texTemp.last_level = MAX_TEXTURE_LEVEL;
398 texTemp.width[0] = 1 << MAX_TEXTURE_LEVEL;
399 texTemp.height[0] = 1 << MAX_TEXTURE_LEVEL;
400 texTemp.depth[0] = 1;
401 pf_get_block(texTemp.format, &texTemp.block);
402
403 aaline->texture = screen->texture_create(screen, &texTemp);
404 if (!aaline->texture)
405 return FALSE;
406
407 /* Fill in mipmap images.
408 * Basically each level is solid opaque, except for the outermost
409 * texels which are zero. Special case the 1x1 and 2x2 levels.
410 */
411 for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
412 struct pipe_surface *surface;
413 const uint size = aaline->texture->width[level];
414 ubyte *data;
415 uint i, j;
416
417 assert(aaline->texture->width[level] == aaline->texture->height[level]);
418
419 /* This texture is new, no need to flush.
420 */
421 surface = screen->get_tex_surface(screen, aaline->texture, 0, level, 0,
422 PIPE_BUFFER_USAGE_CPU_WRITE);
423 data = screen->surface_map(screen, surface, PIPE_BUFFER_USAGE_CPU_WRITE);
424 if (data == NULL)
425 return FALSE;
426
427 for (i = 0; i < size; i++) {
428 for (j = 0; j < size; j++) {
429 ubyte d;
430 if (size == 1) {
431 d = 255;
432 }
433 else if (size == 2) {
434 d = 200; /* tuneable */
435 }
436 else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
437 d = 0;
438 }
439 else {
440 d = 255;
441 }
442 data[i * surface->stride + j] = d;
443 }
444 }
445
446 /* unmap */
447 screen->surface_unmap(screen, surface);
448 screen->tex_surface_release(screen, &surface);
449 }
450 return TRUE;
451 }
452
453
454 /**
455 * Create the sampler CSO that'll be used for antialiasing.
456 * By using a mipmapped texture, we don't have to generate a different
457 * texture image for each line size.
458 */
459 static boolean
460 aaline_create_sampler(struct aaline_stage *aaline)
461 {
462 struct pipe_sampler_state sampler;
463 struct pipe_context *pipe = aaline->pipe;
464
465 memset(&sampler, 0, sizeof(sampler));
466 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
467 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
468 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
469 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
470 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
471 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
472 sampler.normalized_coords = 1;
473 sampler.min_lod = 0.0f;
474 sampler.max_lod = MAX_TEXTURE_LEVEL;
475
476 aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
477 if (aaline->sampler_cso == NULL)
478 return FALSE;
479
480 return TRUE;
481 }
482
483
484 /**
485 * When we're about to draw our first AA line in a batch, this function is
486 * called to tell the driver to bind our modified fragment shader.
487 */
488 static boolean
489 bind_aaline_fragment_shader(struct aaline_stage *aaline)
490 {
491 struct draw_context *draw = aaline->stage.draw;
492
493 if (!aaline->fs->aaline_fs &&
494 !generate_aaline_fs(aaline))
495 return FALSE;
496
497 draw->suspend_flushing = TRUE;
498 aaline->driver_bind_fs_state(aaline->pipe, aaline->fs->aaline_fs);
499 draw->suspend_flushing = FALSE;
500
501 return TRUE;
502 }
503
504
505
506 static INLINE struct aaline_stage *
507 aaline_stage( struct draw_stage *stage )
508 {
509 return (struct aaline_stage *) stage;
510 }
511
512
513 /**
514 * Draw a wide line by drawing a quad, using geometry which will
515 * fullfill GL's antialiased line requirements.
516 */
517 static void
518 aaline_line(struct draw_stage *stage, struct prim_header *header)
519 {
520 const struct aaline_stage *aaline = aaline_stage(stage);
521 const float half_width = aaline->half_line_width;
522 struct prim_header tri;
523 struct vertex_header *v[8];
524 uint texPos = aaline->tex_slot;
525 uint posPos = aaline->pos_slot;
526 float *pos, *tex;
527 float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
528 float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
529 double a = atan2(dy, dx);
530 float c_a = (float) cos(a), s_a = (float) sin(a);
531 uint i;
532
533 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
534 dx = 0.5F * half_width;
535 dy = half_width;
536
537 /* allocate/dup new verts */
538 for (i = 0; i < 8; i++) {
539 v[i] = dup_vert(stage, header->v[i/4], i);
540 }
541
542 /*
543 * Quad strip for line from v0 to v1 (*=endpoints):
544 *
545 * 1 3 5 7
546 * +---+---------------------+---+
547 * | |
548 * | *v0 v1* |
549 * | |
550 * +---+---------------------+---+
551 * 0 2 4 6
552 */
553
554 /* new verts */
555 pos = v[0]->data[posPos];
556 pos[0] += (-dx * c_a - dy * s_a);
557 pos[1] += (-dx * s_a + dy * c_a);
558
559 pos = v[1]->data[posPos];
560 pos[0] += (-dx * c_a - -dy * s_a);
561 pos[1] += (-dx * s_a + -dy * c_a);
562
563 pos = v[2]->data[posPos];
564 pos[0] += ( dx * c_a - dy * s_a);
565 pos[1] += ( dx * s_a + dy * c_a);
566
567 pos = v[3]->data[posPos];
568 pos[0] += ( dx * c_a - -dy * s_a);
569 pos[1] += ( dx * s_a + -dy * c_a);
570
571 pos = v[4]->data[posPos];
572 pos[0] += (-dx * c_a - dy * s_a);
573 pos[1] += (-dx * s_a + dy * c_a);
574
575 pos = v[5]->data[posPos];
576 pos[0] += (-dx * c_a - -dy * s_a);
577 pos[1] += (-dx * s_a + -dy * c_a);
578
579 pos = v[6]->data[posPos];
580 pos[0] += ( dx * c_a - dy * s_a);
581 pos[1] += ( dx * s_a + dy * c_a);
582
583 pos = v[7]->data[posPos];
584 pos[0] += ( dx * c_a - -dy * s_a);
585 pos[1] += ( dx * s_a + -dy * c_a);
586
587 /* new texcoords */
588 tex = v[0]->data[texPos];
589 ASSIGN_4V(tex, 0, 0, 0, 1);
590
591 tex = v[1]->data[texPos];
592 ASSIGN_4V(tex, 0, 1, 0, 1);
593
594 tex = v[2]->data[texPos];
595 ASSIGN_4V(tex, .5, 0, 0, 1);
596
597 tex = v[3]->data[texPos];
598 ASSIGN_4V(tex, .5, 1, 0, 1);
599
600 tex = v[4]->data[texPos];
601 ASSIGN_4V(tex, .5, 0, 0, 1);
602
603 tex = v[5]->data[texPos];
604 ASSIGN_4V(tex, .5, 1, 0, 1);
605
606 tex = v[6]->data[texPos];
607 ASSIGN_4V(tex, 1, 0, 0, 1);
608
609 tex = v[7]->data[texPos];
610 ASSIGN_4V(tex, 1, 1, 0, 1);
611
612 /* emit 6 tris for the quad strip */
613 tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
614 stage->next->tri( stage->next, &tri );
615
616 tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
617 stage->next->tri( stage->next, &tri );
618
619 tri.v[0] = v[4]; tri.v[1] = v[3]; tri.v[2] = v[2];
620 stage->next->tri( stage->next, &tri );
621
622 tri.v[0] = v[5]; tri.v[1] = v[3]; tri.v[2] = v[4];
623 stage->next->tri( stage->next, &tri );
624
625 tri.v[0] = v[6]; tri.v[1] = v[5]; tri.v[2] = v[4];
626 stage->next->tri( stage->next, &tri );
627
628 tri.v[0] = v[7]; tri.v[1] = v[5]; tri.v[2] = v[6];
629 stage->next->tri( stage->next, &tri );
630 }
631
632
633 static void
634 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
635 {
636 auto struct aaline_stage *aaline = aaline_stage(stage);
637 struct draw_context *draw = stage->draw;
638 struct pipe_context *pipe = aaline->pipe;
639 uint num_samplers;
640
641 assert(draw->rasterizer->line_smooth);
642
643 if (draw->rasterizer->line_width <= 3.0)
644 aaline->half_line_width = 1.5f;
645 else
646 aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
647
648 /*
649 * Bind (generate) our fragprog, sampler and texture
650 */
651 if (!bind_aaline_fragment_shader(aaline)) {
652 stage->line = draw_pipe_passthrough_line;
653 stage->line(stage, header);
654 return;
655 }
656
657 /* update vertex attrib info */
658 aaline->tex_slot = draw->vs.num_vs_outputs;
659 aaline->pos_slot = draw->vs.position_output;
660
661 /* advertise the extra post-transformed vertex attribute */
662 draw->extra_vp_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
663 draw->extra_vp_outputs.semantic_index = aaline->fs->generic_attrib;
664 draw->extra_vp_outputs.slot = aaline->tex_slot;
665
666 /* how many samplers? */
667 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
668 num_samplers = MAX2(aaline->num_textures, aaline->num_samplers);
669 num_samplers = MAX2(num_samplers, aaline->fs->sampler_unit + 1);
670
671 aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
672 pipe_texture_reference(&aaline->state.texture[aaline->fs->sampler_unit],
673 aaline->texture);
674
675 draw->suspend_flushing = TRUE;
676 aaline->driver_bind_sampler_states(pipe, num_samplers, aaline->state.sampler);
677 aaline->driver_set_sampler_textures(pipe, num_samplers, aaline->state.texture);
678 draw->suspend_flushing = FALSE;
679
680 /* now really draw first line */
681 stage->line = aaline_line;
682 stage->line(stage, header);
683 }
684
685
686 static void
687 aaline_flush(struct draw_stage *stage, unsigned flags)
688 {
689 struct draw_context *draw = stage->draw;
690 struct aaline_stage *aaline = aaline_stage(stage);
691 struct pipe_context *pipe = aaline->pipe;
692
693 stage->line = aaline_first_line;
694 stage->next->flush( stage->next, flags );
695
696 /* restore original frag shader, texture, sampler state */
697 draw->suspend_flushing = TRUE;
698 aaline->driver_bind_fs_state(pipe, aaline->fs->driver_fs);
699 aaline->driver_bind_sampler_states(pipe, aaline->num_samplers,
700 aaline->state.sampler);
701 aaline->driver_set_sampler_textures(pipe, aaline->num_textures,
702 aaline->state.texture);
703 draw->suspend_flushing = FALSE;
704
705 draw->extra_vp_outputs.slot = 0;
706 }
707
708
709 static void
710 aaline_reset_stipple_counter(struct draw_stage *stage)
711 {
712 stage->next->reset_stipple_counter( stage->next );
713 }
714
715
716 static void
717 aaline_destroy(struct draw_stage *stage)
718 {
719 struct aaline_stage *aaline = aaline_stage(stage);
720 uint i;
721
722 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
723 pipe_texture_reference(&aaline->state.texture[i], NULL);
724 }
725
726 if (aaline->sampler_cso)
727 aaline->pipe->delete_sampler_state(aaline->pipe, aaline->sampler_cso);
728
729 if (aaline->texture)
730 pipe_texture_release(&aaline->texture);
731
732 draw_free_temp_verts( stage );
733
734 FREE( stage );
735 }
736
737
738 static struct aaline_stage *
739 draw_aaline_stage(struct draw_context *draw)
740 {
741 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
742 if (aaline == NULL)
743 return NULL;
744
745 if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
746 goto fail;
747
748 aaline->stage.draw = draw;
749 aaline->stage.next = NULL;
750 aaline->stage.point = draw_pipe_passthrough_point;
751 aaline->stage.line = aaline_first_line;
752 aaline->stage.tri = draw_pipe_passthrough_tri;
753 aaline->stage.flush = aaline_flush;
754 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
755 aaline->stage.destroy = aaline_destroy;
756
757 return aaline;
758
759 fail:
760 if (aaline)
761 aaline_destroy(&aaline->stage);
762
763 return NULL;
764 }
765
766
767 static struct aaline_stage *
768 aaline_stage_from_pipe(struct pipe_context *pipe)
769 {
770 struct draw_context *draw = (struct draw_context *) pipe->draw;
771 return aaline_stage(draw->pipeline.aaline);
772 }
773
774
775 /**
776 * This function overrides the driver's create_fs_state() function and
777 * will typically be called by the state tracker.
778 */
779 static void *
780 aaline_create_fs_state(struct pipe_context *pipe,
781 const struct pipe_shader_state *fs)
782 {
783 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
784 struct aaline_fragment_shader *aafs = CALLOC_STRUCT(aaline_fragment_shader);
785 if (aafs == NULL)
786 return NULL;
787
788 aafs->state = *fs;
789
790 /* pass-through */
791 aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
792
793 return aafs;
794 }
795
796
797 static void
798 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
799 {
800 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
801 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
802
803 /* save current */
804 aaline->fs = aafs;
805 /* pass-through */
806 aaline->driver_bind_fs_state(aaline->pipe,
807 (aafs ? aafs->driver_fs : NULL));
808 }
809
810
811 static void
812 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
813 {
814 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
815 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
816 /* pass-through */
817 aaline->driver_delete_fs_state(aaline->pipe, aafs->driver_fs);
818 FREE(aafs);
819 }
820
821
822 static void
823 aaline_bind_sampler_states(struct pipe_context *pipe,
824 unsigned num, void **sampler)
825 {
826 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
827
828 /* save current */
829 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
830 aaline->num_samplers = num;
831
832 /* pass-through */
833 aaline->driver_bind_sampler_states(aaline->pipe, num, sampler);
834 }
835
836
837 static void
838 aaline_set_sampler_textures(struct pipe_context *pipe,
839 unsigned num, struct pipe_texture **texture)
840 {
841 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
842 uint i;
843
844 /* save current */
845 for (i = 0; i < num; i++) {
846 pipe_texture_reference(&aaline->state.texture[i], texture[i]);
847 }
848 for ( ; i < PIPE_MAX_SAMPLERS; i++) {
849 pipe_texture_reference(&aaline->state.texture[i], NULL);
850 }
851 aaline->num_textures = num;
852
853 /* pass-through */
854 aaline->driver_set_sampler_textures(aaline->pipe, num, texture);
855 }
856
857
858 /**
859 * Called by drivers that want to install this AA line prim stage
860 * into the draw module's pipeline. This will not be used if the
861 * hardware has native support for AA lines.
862 */
863 boolean
864 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
865 {
866 struct aaline_stage *aaline;
867
868 pipe->draw = (void *) draw;
869
870 /*
871 * Create / install AA line drawing / prim stage
872 */
873 aaline = draw_aaline_stage( draw );
874 if (!aaline)
875 goto fail;
876
877 aaline->pipe = pipe;
878
879 /* create special texture, sampler state */
880 if (!aaline_create_texture(aaline))
881 goto fail;
882
883 if (!aaline_create_sampler(aaline))
884 goto fail;
885
886 /* save original driver functions */
887 aaline->driver_create_fs_state = pipe->create_fs_state;
888 aaline->driver_bind_fs_state = pipe->bind_fs_state;
889 aaline->driver_delete_fs_state = pipe->delete_fs_state;
890
891 aaline->driver_bind_sampler_states = pipe->bind_sampler_states;
892 aaline->driver_set_sampler_textures = pipe->set_sampler_textures;
893
894 /* override the driver's functions */
895 pipe->create_fs_state = aaline_create_fs_state;
896 pipe->bind_fs_state = aaline_bind_fs_state;
897 pipe->delete_fs_state = aaline_delete_fs_state;
898
899 pipe->bind_sampler_states = aaline_bind_sampler_states;
900 pipe->set_sampler_textures = aaline_set_sampler_textures;
901
902 /* Install once everything is known to be OK:
903 */
904 draw->pipeline.aaline = &aaline->stage;
905
906 return TRUE;
907
908 fail:
909 if (aaline)
910 aaline->stage.destroy( &aaline->stage );
911
912 return FALSE;
913 }