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