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