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