Simplify makefile boilerplate code.
[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 (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 (decl->u.DeclarationRange.Last > aactx->maxInput)
148 aactx->maxInput = decl->u.DeclarationRange.Last;
149 if (decl->Semantic.SemanticName == TGSI_SEMANTIC_GENERIC &&
150 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 aaline_fs.input_semantic_name[aaline_fs.num_inputs] = TGSI_SEMANTIC_GENERIC;
344 aaline_fs.input_semantic_index[aaline_fs.num_inputs] = transform.maxGeneric + 1;
345 aaline_fs.num_inputs++;
346
347 aaline->fs->aaline_fs
348 = aaline->driver_create_fs_state(aaline->pipe, &aaline_fs);
349
350 /* advertise the extra post-transform vertex attributes which will have
351 * the texcoords.
352 */
353 draw->extra_vp_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
354 draw->extra_vp_outputs.semantic_index = transform.maxGeneric + 1;
355 }
356
357
358 /**
359 * Create the texture map we'll use for antialiasing the lines.
360 */
361 static void
362 aaline_create_texture(struct aaline_stage *aaline)
363 {
364 struct pipe_context *pipe = aaline->pipe;
365 struct pipe_texture texTemp;
366 uint level;
367
368 memset(&texTemp, 0, sizeof(texTemp));
369 texTemp.target = PIPE_TEXTURE_2D;
370 texTemp.format = PIPE_FORMAT_U_A8; /* XXX verify supported by driver! */
371 texTemp.last_level = MAX_TEXTURE_LEVEL;
372 texTemp.width[0] = 1 << MAX_TEXTURE_LEVEL;
373 texTemp.height[0] = 1 << MAX_TEXTURE_LEVEL;
374 texTemp.depth[0] = 1;
375 texTemp.cpp = 1;
376
377 aaline->texture = pipe->texture_create(pipe, &texTemp);
378
379 /* Fill in mipmap images.
380 * Basically each level is solid opaque, except for the outermost
381 * texels which are zero. Special case the 1x1 and 2x2 levels.
382 */
383 for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
384 struct pipe_surface *surface;
385 const uint size = aaline->texture->width[level];
386 ubyte *data;
387 uint i, j;
388
389 assert(aaline->texture->width[level] == aaline->texture->height[level]);
390
391 surface = pipe->get_tex_surface(pipe, aaline->texture, 0, level, 0);
392 data = pipe_surface_map(surface);
393
394 for (i = 0; i < size; i++) {
395 for (j = 0; j < size; j++) {
396 uint d;
397 if (size == 1) {
398 d = 255;
399 }
400 else if (size == 2) {
401 d = 200; /* tuneable */
402 }
403 else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
404 d = 0;
405 }
406 else {
407 d = 255;
408 }
409 data[i * surface->pitch + j] = d;
410 }
411 }
412
413 /* unmap */
414 pipe_surface_unmap(surface);
415 pipe_surface_reference(&surface, NULL);
416 }
417 }
418
419
420 /**
421 * Create the sampler CSO that'll be used for antialiasing.
422 * By using a mipmapped texture, we don't have to generate a different
423 * texture image for each line size.
424 */
425 static void
426 aaline_create_sampler(struct aaline_stage *aaline)
427 {
428 struct pipe_sampler_state sampler;
429 struct pipe_context *pipe = aaline->pipe;
430
431 memset(&sampler, 0, sizeof(sampler));
432 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
433 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
434 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
435 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
436 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
437 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
438 sampler.normalized_coords = 1;
439 sampler.min_lod = 0.0f;
440 sampler.max_lod = MAX_TEXTURE_LEVEL;
441
442 aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
443 }
444
445
446 /**
447 * When we're about to draw our first AA line in a batch, this function is
448 * called to tell the driver to bind our modified fragment shader.
449 */
450 static void
451 bind_aaline_fragment_shader(struct aaline_stage *aaline)
452 {
453 if (!aaline->fs->aaline_fs) {
454 generate_aaline_fs(aaline);
455 }
456 aaline->driver_bind_fs_state(aaline->pipe, aaline->fs->aaline_fs);
457 }
458
459
460
461 static INLINE struct aaline_stage *
462 aaline_stage( struct draw_stage *stage )
463 {
464 return (struct aaline_stage *) stage;
465 }
466
467
468 static void
469 passthrough_point(struct draw_stage *stage, struct prim_header *header)
470 {
471 stage->next->point(stage->next, header);
472 }
473
474
475 static void
476 passthrough_tri(struct draw_stage *stage, struct prim_header *header)
477 {
478 stage->next->tri(stage->next, header);
479 }
480
481
482 /**
483 * Draw a wide line by drawing a quad, using geometry which will
484 * fullfill GL's antialiased line requirements.
485 */
486 static void
487 aaline_line(struct draw_stage *stage, struct prim_header *header)
488 {
489 const struct aaline_stage *aaline = aaline_stage(stage);
490 const float half_width = aaline->half_line_width;
491 struct prim_header tri;
492 struct vertex_header *v[8];
493 uint texPos = aaline->tex_slot;
494 float *pos, *tex;
495 float dx = header->v[1]->data[0][0] - header->v[0]->data[0][0];
496 float dy = header->v[1]->data[0][1] - header->v[0]->data[0][1];
497 float a = atan2(dy, dx);
498 float c_a = cos(a), s_a = sin(a);
499 uint i;
500
501 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
502 dx = 0.5 * half_width;
503 dy = half_width;
504
505 /* allocate/dup new verts */
506 for (i = 0; i < 8; i++) {
507 v[i] = dup_vert(stage, header->v[i/4], i);
508 }
509
510 /*
511 * Quad strip for line from v0 to v1 (*=endpoints):
512 *
513 * 1 3 5 7
514 * +---+---------------------+---+
515 * | |
516 * | *v0 v1* |
517 * | |
518 * +---+---------------------+---+
519 * 0 2 4 6
520 */
521
522 /* new verts */
523 pos = v[0]->data[0];
524 pos[0] += (-dx * c_a - dy * s_a);
525 pos[1] += (-dx * s_a + dy * c_a);
526
527 pos = v[1]->data[0];
528 pos[0] += (-dx * c_a - -dy * s_a);
529 pos[1] += (-dx * s_a + -dy * c_a);
530
531 pos = v[2]->data[0];
532 pos[0] += ( dx * c_a - dy * s_a);
533 pos[1] += ( dx * s_a + dy * c_a);
534
535 pos = v[3]->data[0];
536 pos[0] += ( dx * c_a - -dy * s_a);
537 pos[1] += ( dx * s_a + -dy * c_a);
538
539 pos = v[4]->data[0];
540 pos[0] += (-dx * c_a - dy * s_a);
541 pos[1] += (-dx * s_a + dy * c_a);
542
543 pos = v[5]->data[0];
544 pos[0] += (-dx * c_a - -dy * s_a);
545 pos[1] += (-dx * s_a + -dy * c_a);
546
547 pos = v[6]->data[0];
548 pos[0] += ( dx * c_a - dy * s_a);
549 pos[1] += ( dx * s_a + dy * c_a);
550
551 pos = v[7]->data[0];
552 pos[0] += ( dx * c_a - -dy * s_a);
553 pos[1] += ( dx * s_a + -dy * c_a);
554
555 /* new texcoords */
556 tex = v[0]->data[texPos];
557 ASSIGN_4V(tex, 0, 0, 0, 1);
558
559 tex = v[1]->data[texPos];
560 ASSIGN_4V(tex, 0, 1, 0, 1);
561
562 tex = v[2]->data[texPos];
563 ASSIGN_4V(tex, .5, 0, 0, 1);
564
565 tex = v[3]->data[texPos];
566 ASSIGN_4V(tex, .5, 1, 0, 1);
567
568 tex = v[4]->data[texPos];
569 ASSIGN_4V(tex, .5, 0, 0, 1);
570
571 tex = v[5]->data[texPos];
572 ASSIGN_4V(tex, .5, 1, 0, 1);
573
574 tex = v[6]->data[texPos];
575 ASSIGN_4V(tex, 1, 0, 0, 1);
576
577 tex = v[7]->data[texPos];
578 ASSIGN_4V(tex, 1, 1, 0, 1);
579
580 /* emit 6 tris for the quad strip */
581 tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
582 stage->next->tri( stage->next, &tri );
583
584 tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
585 stage->next->tri( stage->next, &tri );
586
587 tri.v[0] = v[4]; tri.v[1] = v[3]; tri.v[2] = v[2];
588 stage->next->tri( stage->next, &tri );
589
590 tri.v[0] = v[5]; tri.v[1] = v[3]; tri.v[2] = v[4];
591 stage->next->tri( stage->next, &tri );
592
593 tri.v[0] = v[6]; tri.v[1] = v[5]; tri.v[2] = v[4];
594 stage->next->tri( stage->next, &tri );
595
596 tri.v[0] = v[7]; tri.v[1] = v[5]; tri.v[2] = v[6];
597 stage->next->tri( stage->next, &tri );
598 }
599
600
601 static void
602 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
603 {
604 auto struct aaline_stage *aaline = aaline_stage(stage);
605 struct draw_context *draw = stage->draw;
606 struct pipe_context *pipe = aaline->pipe;
607
608 assert(draw->rasterizer->line_smooth);
609
610 if (draw->rasterizer->line_width <= 3.0)
611 aaline->half_line_width = 1.5f;
612 else
613 aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
614
615 aaline->tex_slot = draw->num_vs_outputs;
616 assert(aaline->tex_slot > 0); /* output[0] is vertex pos */
617 draw->extra_vp_outputs.slot = aaline->tex_slot;
618
619 /*
620 * Bind our fragprog, sampler and texture
621 */
622 bind_aaline_fragment_shader(aaline);
623
624 aaline->driver_bind_sampler_state(pipe, aaline->sampler_unit, aaline->sampler_cso);
625 aaline->driver_set_sampler_texture(pipe, aaline->sampler_unit, aaline->texture);
626
627 /* now really draw first line */
628 stage->line = aaline_line;
629 stage->line(stage, header);
630 }
631
632
633 static void
634 aaline_flush(struct draw_stage *stage, unsigned flags)
635 {
636 struct draw_context *draw = stage->draw;
637 struct aaline_stage *aaline = aaline_stage(stage);
638 struct pipe_context *pipe = aaline->pipe;
639
640 stage->line = aaline_first_line;
641 stage->next->flush( stage->next, flags );
642
643 /* restore original frag shader */
644 aaline->driver_bind_fs_state(pipe, aaline->fs->driver_fs);
645
646 /* XXX restore original texture, sampler state */
647 aaline->driver_bind_sampler_state(pipe, aaline->sampler_unit,
648 aaline->state.sampler[aaline->sampler_unit]);
649 aaline->driver_set_sampler_texture(pipe, aaline->sampler_unit,
650 aaline->state.texture[aaline->sampler_unit]);
651
652 draw->extra_vp_outputs.slot = 0;
653 }
654
655
656 static void
657 aaline_reset_stipple_counter(struct draw_stage *stage)
658 {
659 stage->next->reset_stipple_counter( stage->next );
660 }
661
662
663 static void
664 aaline_destroy(struct draw_stage *stage)
665 {
666 draw_free_temp_verts( stage );
667 FREE( stage );
668 }
669
670
671 static struct aaline_stage *
672 draw_aaline_stage(struct draw_context *draw)
673 {
674 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
675
676 draw_alloc_temp_verts( &aaline->stage, 8 );
677
678 aaline->stage.draw = draw;
679 aaline->stage.next = NULL;
680 aaline->stage.point = passthrough_point;
681 aaline->stage.line = aaline_first_line;
682 aaline->stage.tri = passthrough_tri;
683 aaline->stage.flush = aaline_flush;
684 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
685 aaline->stage.destroy = aaline_destroy;
686
687 return aaline;
688 }
689
690
691 /*
692 * XXX temporary? solution to mapping a pipe_context to a aaline_stage.
693 */
694
695 #define MAX_CONTEXTS 10
696
697 static struct pipe_context *Pipe[MAX_CONTEXTS];
698 static struct aaline_stage *Stage[MAX_CONTEXTS];
699 static uint NumContexts;
700
701 static void
702 add_aa_pipe_context(struct pipe_context *pipe, struct aaline_stage *aa)
703 {
704 assert(NumContexts < MAX_CONTEXTS);
705 Pipe[NumContexts] = pipe;
706 Stage[NumContexts] = aa;
707 NumContexts++;
708 }
709
710 static struct aaline_stage *
711 aaline_stage_from_pipe(struct pipe_context *pipe)
712 {
713 uint i;
714 for (i = 0; i < NumContexts; i++) {
715 if (Pipe[i] == pipe)
716 return Stage[i];
717 }
718 assert(0);
719 return NULL;
720 }
721
722
723 /**
724 * This function overrides the driver's create_fs_state() function and
725 * will typically be called by the state tracker.
726 */
727 static void *
728 aaline_create_fs_state(struct pipe_context *pipe,
729 const struct pipe_shader_state *fs)
730 {
731 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
732 struct aaline_fragment_shader *aafs = CALLOC_STRUCT(aaline_fragment_shader);
733
734 if (aafs) {
735 aafs->state = *fs;
736
737 /* pass-through */
738 aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
739 }
740
741 return aafs;
742 }
743
744
745 static void
746 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
747 {
748 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
749 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
750 /* save current */
751 aaline->fs = aafs;
752 /* pass-through */
753 aaline->driver_bind_fs_state(aaline->pipe, aafs->driver_fs);
754 }
755
756
757 static void
758 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
759 {
760 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
761 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
762 /* pass-through */
763 aaline->driver_delete_fs_state(aaline->pipe, aafs->driver_fs);
764 FREE(aafs);
765 }
766
767
768 static void
769 aaline_bind_sampler_state(struct pipe_context *pipe,
770 unsigned unit, void *sampler)
771 {
772 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
773 /* save current */
774 aaline->state.sampler[unit] = sampler;
775 /* pass-through */
776 aaline->driver_bind_sampler_state(aaline->pipe, unit, sampler);
777 }
778
779
780 static void
781 aaline_set_sampler_texture(struct pipe_context *pipe,
782 unsigned sampler, struct pipe_texture *texture)
783 {
784 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
785 /* save current */
786 aaline->state.texture[sampler] = texture;
787 /* pass-through */
788 aaline->driver_set_sampler_texture(aaline->pipe, sampler, texture);
789 }
790
791
792 /**
793 * Called by drivers that want to install this AA line prim stage
794 * into the draw module's pipeline. This will not be used if the
795 * hardware has native support for AA lines.
796 */
797 void
798 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
799 {
800 struct aaline_stage *aaline;
801
802 /*
803 * Create / install AA line drawing / prim stage
804 */
805 aaline = draw_aaline_stage( draw );
806 assert(aaline);
807 draw->pipeline.aaline = &aaline->stage;
808
809 aaline->pipe = pipe;
810
811 /* create special texture, sampler state */
812 aaline_create_texture(aaline);
813 aaline_create_sampler(aaline);
814
815 /* save original driver functions */
816 aaline->driver_create_fs_state = pipe->create_fs_state;
817 aaline->driver_bind_fs_state = pipe->bind_fs_state;
818 aaline->driver_delete_fs_state = pipe->delete_fs_state;
819
820 aaline->driver_bind_sampler_state = pipe->bind_sampler_state;
821 aaline->driver_set_sampler_texture = pipe->set_sampler_texture;
822
823 /* override the driver's functions */
824 pipe->create_fs_state = aaline_create_fs_state;
825 pipe->bind_fs_state = aaline_bind_fs_state;
826 pipe->delete_fs_state = aaline_delete_fs_state;
827
828 pipe->bind_sampler_state = aaline_bind_sampler_state;
829 pipe->set_sampler_texture = aaline_set_sampler_texture;
830
831 add_aa_pipe_context(pipe, aaline);
832 }