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