gallium: don't use arrays for texture width,height,depth
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_aaline.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * AA line stage: AA lines are converted to texture mapped triangles.
30 *
31 * Authors: Brian Paul
32 */
33
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_shader_tokens.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40
41 #include "tgsi/tgsi_transform.h"
42 #include "tgsi/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 uint sampler_unit;
64 int generic_attrib; /**< texcoord/generic used for texture */
65 };
66
67
68 /**
69 * Subclass of draw_stage
70 */
71 struct aaline_stage
72 {
73 struct draw_stage stage;
74
75 float half_line_width;
76
77 /** For AA lines, this is the vertex attrib slot for the new texcoords */
78 uint tex_slot;
79 /** position, not necessarily output zero */
80 uint pos_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->DeclarationRange.First;
145 }
146 else if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
147 uint i;
148 for (i = decl->DeclarationRange.First;
149 i <= decl->DeclarationRange.Last; i++) {
150 aactx->samplersUsed |= 1 << i;
151 }
152 }
153 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
154 if ((int) decl->DeclarationRange.Last > aactx->maxInput)
155 aactx->maxInput = decl->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->DeclarationRange.First;
164 i <= decl->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 /* XXX this could be linear... */
229 decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
230 decl.Declaration.Semantic = 1;
231 decl.Semantic.SemanticName = TGSI_SEMANTIC_GENERIC;
232 decl.Semantic.SemanticIndex = aactx->maxGeneric + 1;
233 decl.DeclarationRange.First =
234 decl.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.DeclarationRange.First =
241 decl.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.DeclarationRange.First =
248 decl.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.DeclarationRange.First =
254 decl.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 boolean
337 generate_aaline_fs(struct aaline_stage *aaline)
338 {
339 const struct pipe_shader_state *orig_fs = &aaline->fs->state;
340 struct pipe_shader_state aaline_fs;
341 struct aa_transform_context transform;
342
343 #define MAX 1000
344
345 aaline_fs = *orig_fs; /* copy to init */
346 aaline_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX);
347 if (aaline_fs.tokens == NULL)
348 return FALSE;
349
350 memset(&transform, 0, sizeof(transform));
351 transform.colorOutput = -1;
352 transform.maxInput = -1;
353 transform.maxGeneric = -1;
354 transform.colorTemp = -1;
355 transform.texTemp = -1;
356 transform.firstInstruction = TRUE;
357 transform.base.transform_instruction = aa_transform_inst;
358 transform.base.transform_declaration = aa_transform_decl;
359
360 tgsi_transform_shader(orig_fs->tokens,
361 (struct tgsi_token *) aaline_fs.tokens,
362 MAX, &transform.base);
363
364 #if 0 /* DEBUG */
365 tgsi_dump(orig_fs->tokens, 0);
366 tgsi_dump(aaline_fs.tokens, 0);
367 #endif
368
369 aaline->fs->sampler_unit = transform.freeSampler;
370
371 aaline->fs->aaline_fs
372 = aaline->driver_create_fs_state(aaline->pipe, &aaline_fs);
373 if (aaline->fs->aaline_fs == NULL)
374 goto fail;
375
376 aaline->fs->generic_attrib = transform.maxGeneric + 1;
377 FREE((void *)aaline_fs.tokens);
378 return TRUE;
379
380 fail:
381 FREE((void *)aaline_fs.tokens);
382 return FALSE;
383 }
384
385
386 /**
387 * Create the texture map we'll use for antialiasing the lines.
388 */
389 static boolean
390 aaline_create_texture(struct aaline_stage *aaline)
391 {
392 struct pipe_context *pipe = aaline->pipe;
393 struct pipe_screen *screen = pipe->screen;
394 struct pipe_texture texTemp;
395 uint level;
396
397 memset(&texTemp, 0, sizeof(texTemp));
398 texTemp.target = PIPE_TEXTURE_2D;
399 texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
400 texTemp.last_level = MAX_TEXTURE_LEVEL;
401 texTemp.width0 = 1 << MAX_TEXTURE_LEVEL;
402 texTemp.height0 = 1 << MAX_TEXTURE_LEVEL;
403 texTemp.depth0 = 1;
404 pf_get_block(texTemp.format, &texTemp.block);
405
406 aaline->texture = screen->texture_create(screen, &texTemp);
407 if (!aaline->texture)
408 return FALSE;
409
410 /* Fill in mipmap images.
411 * Basically each level is solid opaque, except for the outermost
412 * texels which are zero. Special case the 1x1 and 2x2 levels.
413 */
414 for (level = 0; level <= MAX_TEXTURE_LEVEL; level++) {
415 struct pipe_transfer *transfer;
416 const uint size = u_minify(aaline->texture->width0, level);
417 ubyte *data;
418 uint i, j;
419
420 assert(aaline->texture->width0 == aaline->texture->height0);
421
422 /* This texture is new, no need to flush.
423 */
424 transfer = screen->get_tex_transfer(screen, aaline->texture, 0, level, 0,
425 PIPE_TRANSFER_WRITE, 0, 0, size, size);
426 data = screen->transfer_map(screen, transfer);
427 if (data == NULL)
428 return FALSE;
429
430 for (i = 0; i < size; i++) {
431 for (j = 0; j < size; j++) {
432 ubyte d;
433 if (size == 1) {
434 d = 255;
435 }
436 else if (size == 2) {
437 d = 200; /* tuneable */
438 }
439 else if (i == 0 || j == 0 || i == size - 1 || j == size - 1) {
440 d = 0;
441 }
442 else {
443 d = 255;
444 }
445 data[i * transfer->stride + j] = d;
446 }
447 }
448
449 /* unmap */
450 screen->transfer_unmap(screen, transfer);
451 screen->tex_transfer_destroy(transfer);
452 }
453 return TRUE;
454 }
455
456
457 /**
458 * Create the sampler CSO that'll be used for antialiasing.
459 * By using a mipmapped texture, we don't have to generate a different
460 * texture image for each line size.
461 */
462 static boolean
463 aaline_create_sampler(struct aaline_stage *aaline)
464 {
465 struct pipe_sampler_state sampler;
466 struct pipe_context *pipe = aaline->pipe;
467
468 memset(&sampler, 0, sizeof(sampler));
469 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
470 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
471 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
472 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_LINEAR;
473 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
474 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
475 sampler.normalized_coords = 1;
476 sampler.min_lod = 0.0f;
477 sampler.max_lod = MAX_TEXTURE_LEVEL;
478
479 aaline->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
480 if (aaline->sampler_cso == NULL)
481 return FALSE;
482
483 return TRUE;
484 }
485
486
487 /**
488 * When we're about to draw our first AA line in a batch, this function is
489 * called to tell the driver to bind our modified fragment shader.
490 */
491 static boolean
492 bind_aaline_fragment_shader(struct aaline_stage *aaline)
493 {
494 struct draw_context *draw = aaline->stage.draw;
495
496 if (!aaline->fs->aaline_fs &&
497 !generate_aaline_fs(aaline))
498 return FALSE;
499
500 draw->suspend_flushing = TRUE;
501 aaline->driver_bind_fs_state(aaline->pipe, aaline->fs->aaline_fs);
502 draw->suspend_flushing = FALSE;
503
504 return TRUE;
505 }
506
507
508
509 static INLINE struct aaline_stage *
510 aaline_stage( struct draw_stage *stage )
511 {
512 return (struct aaline_stage *) stage;
513 }
514
515
516 /**
517 * Draw a wide line by drawing a quad, using geometry which will
518 * fullfill GL's antialiased line requirements.
519 */
520 static void
521 aaline_line(struct draw_stage *stage, struct prim_header *header)
522 {
523 const struct aaline_stage *aaline = aaline_stage(stage);
524 const float half_width = aaline->half_line_width;
525 struct prim_header tri;
526 struct vertex_header *v[8];
527 uint texPos = aaline->tex_slot;
528 uint posPos = aaline->pos_slot;
529 float *pos, *tex;
530 float dx = header->v[1]->data[posPos][0] - header->v[0]->data[posPos][0];
531 float dy = header->v[1]->data[posPos][1] - header->v[0]->data[posPos][1];
532 double a = atan2(dy, dx);
533 float c_a = (float) cos(a), s_a = (float) sin(a);
534 uint i;
535
536 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
537 dx = 0.5F * half_width;
538 dy = half_width;
539
540 /* allocate/dup new verts */
541 for (i = 0; i < 8; i++) {
542 v[i] = dup_vert(stage, header->v[i/4], i);
543 }
544
545 /*
546 * Quad strip for line from v0 to v1 (*=endpoints):
547 *
548 * 1 3 5 7
549 * +---+---------------------+---+
550 * | |
551 * | *v0 v1* |
552 * | |
553 * +---+---------------------+---+
554 * 0 2 4 6
555 */
556
557 /* new verts */
558 pos = v[0]->data[posPos];
559 pos[0] += (-dx * c_a - dy * s_a);
560 pos[1] += (-dx * s_a + dy * c_a);
561
562 pos = v[1]->data[posPos];
563 pos[0] += (-dx * c_a - -dy * s_a);
564 pos[1] += (-dx * s_a + -dy * c_a);
565
566 pos = v[2]->data[posPos];
567 pos[0] += ( dx * c_a - dy * s_a);
568 pos[1] += ( dx * s_a + dy * c_a);
569
570 pos = v[3]->data[posPos];
571 pos[0] += ( dx * c_a - -dy * s_a);
572 pos[1] += ( dx * s_a + -dy * c_a);
573
574 pos = v[4]->data[posPos];
575 pos[0] += (-dx * c_a - dy * s_a);
576 pos[1] += (-dx * s_a + dy * c_a);
577
578 pos = v[5]->data[posPos];
579 pos[0] += (-dx * c_a - -dy * s_a);
580 pos[1] += (-dx * s_a + -dy * c_a);
581
582 pos = v[6]->data[posPos];
583 pos[0] += ( dx * c_a - dy * s_a);
584 pos[1] += ( dx * s_a + dy * c_a);
585
586 pos = v[7]->data[posPos];
587 pos[0] += ( dx * c_a - -dy * s_a);
588 pos[1] += ( dx * s_a + -dy * c_a);
589
590 /* new texcoords */
591 tex = v[0]->data[texPos];
592 ASSIGN_4V(tex, 0, 0, 0, 1);
593
594 tex = v[1]->data[texPos];
595 ASSIGN_4V(tex, 0, 1, 0, 1);
596
597 tex = v[2]->data[texPos];
598 ASSIGN_4V(tex, .5, 0, 0, 1);
599
600 tex = v[3]->data[texPos];
601 ASSIGN_4V(tex, .5, 1, 0, 1);
602
603 tex = v[4]->data[texPos];
604 ASSIGN_4V(tex, .5, 0, 0, 1);
605
606 tex = v[5]->data[texPos];
607 ASSIGN_4V(tex, .5, 1, 0, 1);
608
609 tex = v[6]->data[texPos];
610 ASSIGN_4V(tex, 1, 0, 0, 1);
611
612 tex = v[7]->data[texPos];
613 ASSIGN_4V(tex, 1, 1, 0, 1);
614
615 /* emit 6 tris for the quad strip */
616 tri.v[0] = v[2]; tri.v[1] = v[1]; tri.v[2] = v[0];
617 stage->next->tri( stage->next, &tri );
618
619 tri.v[0] = v[3]; tri.v[1] = v[1]; tri.v[2] = v[2];
620 stage->next->tri( stage->next, &tri );
621
622 tri.v[0] = v[4]; tri.v[1] = v[3]; tri.v[2] = v[2];
623 stage->next->tri( stage->next, &tri );
624
625 tri.v[0] = v[5]; tri.v[1] = v[3]; tri.v[2] = v[4];
626 stage->next->tri( stage->next, &tri );
627
628 tri.v[0] = v[6]; tri.v[1] = v[5]; tri.v[2] = v[4];
629 stage->next->tri( stage->next, &tri );
630
631 tri.v[0] = v[7]; tri.v[1] = v[5]; tri.v[2] = v[6];
632 stage->next->tri( stage->next, &tri );
633 }
634
635
636 static void
637 aaline_first_line(struct draw_stage *stage, struct prim_header *header)
638 {
639 auto struct aaline_stage *aaline = aaline_stage(stage);
640 struct draw_context *draw = stage->draw;
641 struct pipe_context *pipe = aaline->pipe;
642 uint num_samplers;
643
644 assert(draw->rasterizer->line_smooth);
645
646 if (draw->rasterizer->line_width <= 3.0)
647 aaline->half_line_width = 1.5f;
648 else
649 aaline->half_line_width = 0.5f * draw->rasterizer->line_width;
650
651 /*
652 * Bind (generate) our fragprog, sampler and texture
653 */
654 if (!bind_aaline_fragment_shader(aaline)) {
655 stage->line = draw_pipe_passthrough_line;
656 stage->line(stage, header);
657 return;
658 }
659
660 /* update vertex attrib info */
661 aaline->tex_slot = draw->vs.num_vs_outputs;
662 aaline->pos_slot = draw->vs.position_output;
663
664 /* advertise the extra post-transformed vertex attribute */
665 draw->extra_vp_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
666 draw->extra_vp_outputs.semantic_index = aaline->fs->generic_attrib;
667 draw->extra_vp_outputs.slot = aaline->tex_slot;
668
669 /* how many samplers? */
670 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
671 num_samplers = MAX2(aaline->num_textures, aaline->num_samplers);
672 num_samplers = MAX2(num_samplers, aaline->fs->sampler_unit + 1);
673
674 aaline->state.sampler[aaline->fs->sampler_unit] = aaline->sampler_cso;
675 pipe_texture_reference(&aaline->state.texture[aaline->fs->sampler_unit],
676 aaline->texture);
677
678 draw->suspend_flushing = TRUE;
679 aaline->driver_bind_sampler_states(pipe, num_samplers, aaline->state.sampler);
680 aaline->driver_set_sampler_textures(pipe, num_samplers, aaline->state.texture);
681 draw->suspend_flushing = FALSE;
682
683 /* now really draw first line */
684 stage->line = aaline_line;
685 stage->line(stage, header);
686 }
687
688
689 static void
690 aaline_flush(struct draw_stage *stage, unsigned flags)
691 {
692 struct draw_context *draw = stage->draw;
693 struct aaline_stage *aaline = aaline_stage(stage);
694 struct pipe_context *pipe = aaline->pipe;
695
696 stage->line = aaline_first_line;
697 stage->next->flush( stage->next, flags );
698
699 /* restore original frag shader, texture, sampler state */
700 draw->suspend_flushing = TRUE;
701 aaline->driver_bind_fs_state(pipe, aaline->fs->driver_fs);
702 aaline->driver_bind_sampler_states(pipe, aaline->num_samplers,
703 aaline->state.sampler);
704 aaline->driver_set_sampler_textures(pipe, aaline->num_textures,
705 aaline->state.texture);
706 draw->suspend_flushing = FALSE;
707
708 draw->extra_vp_outputs.slot = 0;
709 }
710
711
712 static void
713 aaline_reset_stipple_counter(struct draw_stage *stage)
714 {
715 stage->next->reset_stipple_counter( stage->next );
716 }
717
718
719 static void
720 aaline_destroy(struct draw_stage *stage)
721 {
722 struct aaline_stage *aaline = aaline_stage(stage);
723 uint i;
724
725 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
726 pipe_texture_reference(&aaline->state.texture[i], NULL);
727 }
728
729 if (aaline->sampler_cso)
730 aaline->pipe->delete_sampler_state(aaline->pipe, aaline->sampler_cso);
731
732 if (aaline->texture)
733 pipe_texture_reference(&aaline->texture, NULL);
734
735 draw_free_temp_verts( stage );
736
737 FREE( stage );
738 }
739
740
741 static struct aaline_stage *
742 draw_aaline_stage(struct draw_context *draw)
743 {
744 struct aaline_stage *aaline = CALLOC_STRUCT(aaline_stage);
745 if (aaline == NULL)
746 return NULL;
747
748 if (!draw_alloc_temp_verts( &aaline->stage, 8 ))
749 goto fail;
750
751 aaline->stage.draw = draw;
752 aaline->stage.name = "aaline";
753 aaline->stage.next = NULL;
754 aaline->stage.point = draw_pipe_passthrough_point;
755 aaline->stage.line = aaline_first_line;
756 aaline->stage.tri = draw_pipe_passthrough_tri;
757 aaline->stage.flush = aaline_flush;
758 aaline->stage.reset_stipple_counter = aaline_reset_stipple_counter;
759 aaline->stage.destroy = aaline_destroy;
760
761 return aaline;
762
763 fail:
764 if (aaline)
765 aaline_destroy(&aaline->stage);
766
767 return NULL;
768 }
769
770
771 static struct aaline_stage *
772 aaline_stage_from_pipe(struct pipe_context *pipe)
773 {
774 struct draw_context *draw = (struct draw_context *) pipe->draw;
775 return aaline_stage(draw->pipeline.aaline);
776 }
777
778
779 /**
780 * This function overrides the driver's create_fs_state() function and
781 * will typically be called by the state tracker.
782 */
783 static void *
784 aaline_create_fs_state(struct pipe_context *pipe,
785 const struct pipe_shader_state *fs)
786 {
787 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
788 struct aaline_fragment_shader *aafs = CALLOC_STRUCT(aaline_fragment_shader);
789 if (aafs == NULL)
790 return NULL;
791
792 aafs->state = *fs;
793
794 /* pass-through */
795 aafs->driver_fs = aaline->driver_create_fs_state(aaline->pipe, fs);
796
797 return aafs;
798 }
799
800
801 static void
802 aaline_bind_fs_state(struct pipe_context *pipe, void *fs)
803 {
804 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
805 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
806
807 /* save current */
808 aaline->fs = aafs;
809 /* pass-through */
810 aaline->driver_bind_fs_state(aaline->pipe,
811 (aafs ? aafs->driver_fs : NULL));
812 }
813
814
815 static void
816 aaline_delete_fs_state(struct pipe_context *pipe, void *fs)
817 {
818 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
819 struct aaline_fragment_shader *aafs = (struct aaline_fragment_shader *) fs;
820 /* pass-through */
821 aaline->driver_delete_fs_state(aaline->pipe, aafs->driver_fs);
822
823 if (aafs->aaline_fs)
824 aaline->driver_delete_fs_state(aaline->pipe, aafs->aaline_fs);
825
826 FREE(aafs);
827 }
828
829
830 static void
831 aaline_bind_sampler_states(struct pipe_context *pipe,
832 unsigned num, void **sampler)
833 {
834 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
835
836 /* save current */
837 memcpy(aaline->state.sampler, sampler, num * sizeof(void *));
838 aaline->num_samplers = num;
839
840 /* pass-through */
841 aaline->driver_bind_sampler_states(aaline->pipe, num, sampler);
842 }
843
844
845 static void
846 aaline_set_sampler_textures(struct pipe_context *pipe,
847 unsigned num, struct pipe_texture **texture)
848 {
849 struct aaline_stage *aaline = aaline_stage_from_pipe(pipe);
850 uint i;
851
852 /* save current */
853 for (i = 0; i < num; i++) {
854 pipe_texture_reference(&aaline->state.texture[i], texture[i]);
855 }
856 for ( ; i < PIPE_MAX_SAMPLERS; i++) {
857 pipe_texture_reference(&aaline->state.texture[i], NULL);
858 }
859 aaline->num_textures = num;
860
861 /* pass-through */
862 aaline->driver_set_sampler_textures(aaline->pipe, num, texture);
863 }
864
865
866 /**
867 * Called by drivers that want to install this AA line prim stage
868 * into the draw module's pipeline. This will not be used if the
869 * hardware has native support for AA lines.
870 */
871 boolean
872 draw_install_aaline_stage(struct draw_context *draw, struct pipe_context *pipe)
873 {
874 struct aaline_stage *aaline;
875
876 pipe->draw = (void *) draw;
877
878 /*
879 * Create / install AA line drawing / prim stage
880 */
881 aaline = draw_aaline_stage( draw );
882 if (!aaline)
883 goto fail;
884
885 aaline->pipe = pipe;
886
887 /* create special texture, sampler state */
888 if (!aaline_create_texture(aaline))
889 goto fail;
890
891 if (!aaline_create_sampler(aaline))
892 goto fail;
893
894 /* save original driver functions */
895 aaline->driver_create_fs_state = pipe->create_fs_state;
896 aaline->driver_bind_fs_state = pipe->bind_fs_state;
897 aaline->driver_delete_fs_state = pipe->delete_fs_state;
898
899 aaline->driver_bind_sampler_states = pipe->bind_sampler_states;
900 aaline->driver_set_sampler_textures = pipe->set_sampler_textures;
901
902 /* override the driver's functions */
903 pipe->create_fs_state = aaline_create_fs_state;
904 pipe->bind_fs_state = aaline_bind_fs_state;
905 pipe->delete_fs_state = aaline_delete_fs_state;
906
907 pipe->bind_sampler_states = aaline_bind_sampler_states;
908 pipe->set_sampler_textures = aaline_set_sampler_textures;
909
910 /* Install once everything is known to be OK:
911 */
912 draw->pipeline.aaline = &aaline->stage;
913
914 return TRUE;
915
916 fail:
917 if (aaline)
918 aaline->stage.destroy( &aaline->stage );
919
920 return FALSE;
921 }