1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
29 * AA line stage: AA lines are converted to texture mapped triangles.
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_shader_tokens.h"
38 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_sampler.h"
45 #include "tgsi/tgsi_transform.h"
46 #include "tgsi/tgsi_dump.h"
48 #include "draw_context.h"
49 #include "draw_private.h"
50 #include "draw_pipe.h"
53 /** Approx number of new tokens for instructions in aa_transform_inst() */
54 #define NUM_NEW_TOKENS 50
58 * Size for the alpha texture used for antialiasing
60 #define TEXTURE_SIZE_LOG2 5 /* 32 x 32 */
63 * Max texture level for the alpha texture used for antialiasing
65 * Don't use the 1x1 and 2x2 mipmap levels.
67 #define MAX_TEXTURE_LEVEL (TEXTURE_SIZE_LOG2 - 2)
71 * Subclass of pipe_shader_state to carry extra fragment shader info.
73 struct aaline_fragment_shader
75 struct pipe_shader_state state
;
79 int generic_attrib
; /**< texcoord/generic used for texture */
84 * Subclass of draw_stage
88 struct draw_stage stage
;
90 float half_line_width
;
92 /** For AA lines, this is the vertex attrib slot for the new texcoords */
94 /** position, not necessarily output zero */
98 struct pipe_resource
*texture
;
99 struct pipe_sampler_view
*sampler_view
;
101 uint num_sampler_views
;
105 * Currently bound state
107 struct aaline_fragment_shader
*fs
;
109 void *sampler
[PIPE_MAX_SAMPLERS
];
110 struct pipe_sampler_view
*sampler_views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
114 * Driver interface/override functions
116 void * (*driver_create_fs_state
)(struct pipe_context
*,
117 const struct pipe_shader_state
*);
118 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
119 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
121 void (*driver_bind_sampler_states
)(struct pipe_context
*, unsigned, unsigned,
124 void (*driver_set_sampler_views
)(struct pipe_context
*, unsigned shader
,
125 unsigned start
, unsigned count
,
126 struct pipe_sampler_view
**);
132 * Subclass of tgsi_transform_context, used for transforming the
133 * user's fragment shader to add the special AA instructions.
135 struct aa_transform_context
{
136 struct tgsi_transform_context base
;
137 uint tempsUsed
; /**< bitmask */
138 int colorOutput
; /**< which output is the primary color */
139 uint samplersUsed
; /**< bitfield of samplers used */
140 int freeSampler
; /** an available sampler for the pstipple */
141 int maxInput
, maxGeneric
; /**< max input index found */
142 int colorTemp
, texTemp
; /**< temp registers */
147 * TGSI declaration transform callback.
148 * Look for a free sampler, a free input attrib, and two free temp regs.
151 aa_transform_decl(struct tgsi_transform_context
*ctx
,
152 struct tgsi_full_declaration
*decl
)
154 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
156 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
157 decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
158 decl
->Semantic
.Index
== 0) {
159 aactx
->colorOutput
= decl
->Range
.First
;
161 else if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
163 for (i
= decl
->Range
.First
;
164 i
<= decl
->Range
.Last
; i
++) {
165 aactx
->samplersUsed
|= 1 << i
;
168 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
169 if ((int) decl
->Range
.Last
> aactx
->maxInput
)
170 aactx
->maxInput
= decl
->Range
.Last
;
171 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
&&
172 (int) decl
->Semantic
.Index
> aactx
->maxGeneric
) {
173 aactx
->maxGeneric
= decl
->Semantic
.Index
;
176 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
178 for (i
= decl
->Range
.First
;
179 i
<= decl
->Range
.Last
; i
++) {
180 aactx
->tempsUsed
|= (1 << i
);
184 ctx
->emit_declaration(ctx
, decl
);
189 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
192 free_bit(uint bitfield
)
194 return ffs(~bitfield
) - 1;
199 * TGSI transform prolog callback.
202 aa_transform_prolog(struct tgsi_transform_context
*ctx
)
204 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
205 struct tgsi_full_declaration decl
;
208 /* find free sampler */
209 aactx
->freeSampler
= free_bit(aactx
->samplersUsed
);
210 if (aactx
->freeSampler
>= PIPE_MAX_SAMPLERS
)
211 aactx
->freeSampler
= PIPE_MAX_SAMPLERS
- 1;
213 /* find two free temp regs */
214 for (i
= 0; i
< 32; i
++) {
215 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
216 /* found a free temp */
217 if (aactx
->colorTemp
< 0)
218 aactx
->colorTemp
= i
;
219 else if (aactx
->texTemp
< 0)
225 assert(aactx
->colorTemp
>= 0);
226 assert(aactx
->texTemp
>= 0);
228 /* declare new generic input/texcoord */
229 decl
= tgsi_default_full_declaration();
230 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
231 /* XXX this could be linear... */
232 decl
.Declaration
.Interpolate
= 1;
233 decl
.Declaration
.Semantic
= 1;
234 decl
.Semantic
.Name
= TGSI_SEMANTIC_GENERIC
;
235 decl
.Semantic
.Index
= aactx
->maxGeneric
+ 1;
237 decl
.Range
.Last
= aactx
->maxInput
+ 1;
238 decl
.Interp
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
239 ctx
->emit_declaration(ctx
, &decl
);
241 /* declare new sampler */
242 decl
= tgsi_default_full_declaration();
243 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
245 decl
.Range
.Last
= aactx
->freeSampler
;
246 ctx
->emit_declaration(ctx
, &decl
);
248 /* declare new temp regs */
249 decl
= tgsi_default_full_declaration();
250 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
252 decl
.Range
.Last
= aactx
->texTemp
;
253 ctx
->emit_declaration(ctx
, &decl
);
255 decl
= tgsi_default_full_declaration();
256 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
258 decl
.Range
.Last
= aactx
->colorTemp
;
259 ctx
->emit_declaration(ctx
, &decl
);
264 * TGSI transform epilog callback.
267 aa_transform_epilog(struct tgsi_transform_context
*ctx
)
269 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
271 if (aactx
->colorOutput
!= -1) {
272 /* insert texture sampling code for antialiasing. */
273 struct tgsi_full_instruction newInst
;
276 newInst
= tgsi_default_full_instruction();
277 newInst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
278 newInst
.Instruction
.NumDstRegs
= 1;
279 newInst
.Dst
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
280 newInst
.Dst
[0].Register
.Index
= aactx
->texTemp
;
281 newInst
.Instruction
.NumSrcRegs
= 2;
282 newInst
.Instruction
.Texture
= TRUE
;
283 newInst
.Texture
.Texture
= TGSI_TEXTURE_2D
;
284 newInst
.Src
[0].Register
.File
= TGSI_FILE_INPUT
;
285 newInst
.Src
[0].Register
.Index
= aactx
->maxInput
+ 1;
286 newInst
.Src
[1].Register
.File
= TGSI_FILE_SAMPLER
;
287 newInst
.Src
[1].Register
.Index
= aactx
->freeSampler
;
288 ctx
->emit_instruction(ctx
, &newInst
);
291 newInst
= tgsi_default_full_instruction();
292 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
293 newInst
.Instruction
.NumDstRegs
= 1;
294 newInst
.Dst
[0].Register
.File
= TGSI_FILE_OUTPUT
;
295 newInst
.Dst
[0].Register
.Index
= aactx
->colorOutput
;
296 newInst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_XYZ
;
297 newInst
.Instruction
.NumSrcRegs
= 1;
298 newInst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
299 newInst
.Src
[0].Register
.Index
= aactx
->colorTemp
;
300 ctx
->emit_instruction(ctx
, &newInst
);
303 newInst
= tgsi_default_full_instruction();
304 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
305 newInst
.Instruction
.NumDstRegs
= 1;
306 newInst
.Dst
[0].Register
.File
= TGSI_FILE_OUTPUT
;
307 newInst
.Dst
[0].Register
.Index
= aactx
->colorOutput
;
308 newInst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_W
;
309 newInst
.Instruction
.NumSrcRegs
= 2;
310 newInst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
311 newInst
.Src
[0].Register
.Index
= aactx
->colorTemp
;
312 newInst
.Src
[1].Register
.File
= TGSI_FILE_TEMPORARY
;
313 newInst
.Src
[1].Register
.Index
= aactx
->texTemp
;
314 ctx
->emit_instruction(ctx
, &newInst
);
320 * TGSI instruction transform callback.
321 * Replace writes to result.color w/ a temp reg.
324 aa_transform_inst(struct tgsi_transform_context
*ctx
,
325 struct tgsi_full_instruction
*inst
)
327 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
331 * Look for writes to result.color and replace with colorTemp reg.
333 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
334 struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
335 if (dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
336 dst
->Register
.Index
== aactx
->colorOutput
) {
337 dst
->Register
.File
= TGSI_FILE_TEMPORARY
;
338 dst
->Register
.Index
= aactx
->colorTemp
;
342 ctx
->emit_instruction(ctx
, inst
);
347 * Generate the frag shader we'll use for drawing AA lines.
348 * This will be the user's shader plus some texture/modulate instructions.
351 generate_aaline_fs(struct aaline_stage
*aaline
)
353 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
354 const struct pipe_shader_state
*orig_fs
= &aaline
->fs
->state
;
355 struct pipe_shader_state aaline_fs
;
356 struct aa_transform_context transform
;
357 const uint newLen
= tgsi_num_tokens(orig_fs
->tokens
) + NUM_NEW_TOKENS
;
359 aaline_fs
= *orig_fs
; /* copy to init */
360 aaline_fs
.tokens
= tgsi_alloc_tokens(newLen
);
361 if (aaline_fs
.tokens
== NULL
)
364 memset(&transform
, 0, sizeof(transform
));
365 transform
.colorOutput
= -1;
366 transform
.maxInput
= -1;
367 transform
.maxGeneric
= -1;
368 transform
.colorTemp
= -1;
369 transform
.texTemp
= -1;
370 transform
.base
.prolog
= aa_transform_prolog
;
371 transform
.base
.epilog
= aa_transform_epilog
;
372 transform
.base
.transform_instruction
= aa_transform_inst
;
373 transform
.base
.transform_declaration
= aa_transform_decl
;
375 tgsi_transform_shader(orig_fs
->tokens
,
376 (struct tgsi_token
*) aaline_fs
.tokens
,
377 newLen
, &transform
.base
);
380 debug_printf("draw_aaline, orig shader:\n");
381 tgsi_dump(orig_fs
->tokens
, 0);
382 debug_printf("draw_aaline, new shader:\n");
383 tgsi_dump(aaline_fs
.tokens
, 0);
386 aaline
->fs
->sampler_unit
= transform
.freeSampler
;
388 aaline
->fs
->aaline_fs
= aaline
->driver_create_fs_state(pipe
, &aaline_fs
);
389 if (aaline
->fs
->aaline_fs
== NULL
)
392 aaline
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
393 FREE((void *)aaline_fs
.tokens
);
397 FREE((void *)aaline_fs
.tokens
);
403 * Create the texture map we'll use for antialiasing the lines.
406 aaline_create_texture(struct aaline_stage
*aaline
)
408 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
409 struct pipe_screen
*screen
= pipe
->screen
;
410 struct pipe_resource texTemp
;
411 struct pipe_sampler_view viewTempl
;
414 memset(&texTemp
, 0, sizeof(texTemp
));
415 texTemp
.target
= PIPE_TEXTURE_2D
;
416 texTemp
.format
= PIPE_FORMAT_A8_UNORM
; /* XXX verify supported by driver! */
417 texTemp
.last_level
= MAX_TEXTURE_LEVEL
;
418 texTemp
.width0
= 1 << TEXTURE_SIZE_LOG2
;
419 texTemp
.height0
= 1 << TEXTURE_SIZE_LOG2
;
421 texTemp
.array_size
= 1;
422 texTemp
.bind
= PIPE_BIND_SAMPLER_VIEW
;
424 aaline
->texture
= screen
->resource_create(screen
, &texTemp
);
425 if (!aaline
->texture
)
428 u_sampler_view_default_template(&viewTempl
,
430 aaline
->texture
->format
);
431 aaline
->sampler_view
= pipe
->create_sampler_view(pipe
,
434 if (!aaline
->sampler_view
) {
438 /* Fill in mipmap images.
439 * Basically each level is solid opaque, except for the outermost
440 * texels which are zero. Special case the 1x1 and 2x2 levels
441 * (though, those levels shouldn't be used - see the max_lod setting).
443 for (level
= 0; level
<= MAX_TEXTURE_LEVEL
; level
++) {
444 struct pipe_transfer
*transfer
;
446 const uint size
= u_minify(aaline
->texture
->width0
, level
);
450 assert(aaline
->texture
->width0
== aaline
->texture
->height0
);
452 u_box_origin_2d( size
, size
, &box
);
454 /* This texture is new, no need to flush.
456 data
= pipe
->transfer_map(pipe
,
465 for (i
= 0; i
< size
; i
++) {
466 for (j
= 0; j
< size
; j
++) {
471 else if (size
== 2) {
472 d
= 200; /* tuneable */
474 else if (i
== 0 || j
== 0 || i
== size
- 1 || j
== size
- 1) {
475 d
= 35; /* edge texel */
480 data
[i
* transfer
->stride
+ j
] = d
;
485 pipe
->transfer_unmap(pipe
, transfer
);
492 * Create the sampler CSO that'll be used for antialiasing.
493 * By using a mipmapped texture, we don't have to generate a different
494 * texture image for each line size.
497 aaline_create_sampler(struct aaline_stage
*aaline
)
499 struct pipe_sampler_state sampler
;
500 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
502 memset(&sampler
, 0, sizeof(sampler
));
503 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
504 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
505 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
506 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
507 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
508 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
509 sampler
.normalized_coords
= 1;
510 sampler
.min_lod
= 0.0f
;
511 sampler
.max_lod
= MAX_TEXTURE_LEVEL
;
513 aaline
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
514 if (aaline
->sampler_cso
== NULL
)
522 * When we're about to draw our first AA line in a batch, this function is
523 * called to tell the driver to bind our modified fragment shader.
526 bind_aaline_fragment_shader(struct aaline_stage
*aaline
)
528 struct draw_context
*draw
= aaline
->stage
.draw
;
529 struct pipe_context
*pipe
= draw
->pipe
;
531 if (!aaline
->fs
->aaline_fs
&&
532 !generate_aaline_fs(aaline
))
535 draw
->suspend_flushing
= TRUE
;
536 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->aaline_fs
);
537 draw
->suspend_flushing
= FALSE
;
544 static INLINE
struct aaline_stage
*
545 aaline_stage( struct draw_stage
*stage
)
547 return (struct aaline_stage
*) stage
;
552 * Draw a wide line by drawing a quad, using geometry which will
553 * fullfill GL's antialiased line requirements.
556 aaline_line(struct draw_stage
*stage
, struct prim_header
*header
)
558 const struct aaline_stage
*aaline
= aaline_stage(stage
);
559 const float half_width
= aaline
->half_line_width
;
560 struct prim_header tri
;
561 struct vertex_header
*v
[8];
562 uint texPos
= aaline
->tex_slot
;
563 uint posPos
= aaline
->pos_slot
;
565 float dx
= header
->v
[1]->data
[posPos
][0] - header
->v
[0]->data
[posPos
][0];
566 float dy
= header
->v
[1]->data
[posPos
][1] - header
->v
[0]->data
[posPos
][1];
567 double a
= atan2(dy
, dx
);
568 float c_a
= (float) cos(a
), s_a
= (float) sin(a
);
571 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
572 dx
= 0.5F
* half_width
;
575 /* allocate/dup new verts */
576 for (i
= 0; i
< 8; i
++) {
577 v
[i
] = dup_vert(stage
, header
->v
[i
/4], i
);
581 * Quad strip for line from v0 to v1 (*=endpoints):
584 * +---+---------------------+---+
588 * +---+---------------------+---+
593 pos
= v
[0]->data
[posPos
];
594 pos
[0] += (-dx
* c_a
- dy
* s_a
);
595 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
597 pos
= v
[1]->data
[posPos
];
598 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
599 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
601 pos
= v
[2]->data
[posPos
];
602 pos
[0] += ( dx
* c_a
- dy
* s_a
);
603 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
605 pos
= v
[3]->data
[posPos
];
606 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
607 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
609 pos
= v
[4]->data
[posPos
];
610 pos
[0] += (-dx
* c_a
- dy
* s_a
);
611 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
613 pos
= v
[5]->data
[posPos
];
614 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
615 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
617 pos
= v
[6]->data
[posPos
];
618 pos
[0] += ( dx
* c_a
- dy
* s_a
);
619 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
621 pos
= v
[7]->data
[posPos
];
622 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
623 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
626 tex
= v
[0]->data
[texPos
];
627 ASSIGN_4V(tex
, 0, 0, 0, 1);
629 tex
= v
[1]->data
[texPos
];
630 ASSIGN_4V(tex
, 0, 1, 0, 1);
632 tex
= v
[2]->data
[texPos
];
633 ASSIGN_4V(tex
, .5, 0, 0, 1);
635 tex
= v
[3]->data
[texPos
];
636 ASSIGN_4V(tex
, .5, 1, 0, 1);
638 tex
= v
[4]->data
[texPos
];
639 ASSIGN_4V(tex
, .5, 0, 0, 1);
641 tex
= v
[5]->data
[texPos
];
642 ASSIGN_4V(tex
, .5, 1, 0, 1);
644 tex
= v
[6]->data
[texPos
];
645 ASSIGN_4V(tex
, 1, 0, 0, 1);
647 tex
= v
[7]->data
[texPos
];
648 ASSIGN_4V(tex
, 1, 1, 0, 1);
650 /* emit 6 tris for the quad strip */
651 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
652 stage
->next
->tri( stage
->next
, &tri
);
654 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
655 stage
->next
->tri( stage
->next
, &tri
);
657 tri
.v
[0] = v
[4]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[2];
658 stage
->next
->tri( stage
->next
, &tri
);
660 tri
.v
[0] = v
[5]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[4];
661 stage
->next
->tri( stage
->next
, &tri
);
663 tri
.v
[0] = v
[6]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[4];
664 stage
->next
->tri( stage
->next
, &tri
);
666 tri
.v
[0] = v
[7]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[6];
667 stage
->next
->tri( stage
->next
, &tri
);
672 aaline_first_line(struct draw_stage
*stage
, struct prim_header
*header
)
674 auto struct aaline_stage
*aaline
= aaline_stage(stage
);
675 struct draw_context
*draw
= stage
->draw
;
676 struct pipe_context
*pipe
= draw
->pipe
;
677 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
681 assert(draw
->rasterizer
->line_smooth
);
683 if (draw
->rasterizer
->line_width
<= 2.2)
684 aaline
->half_line_width
= 1.1f
;
686 aaline
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
689 * Bind (generate) our fragprog, sampler and texture
691 if (!bind_aaline_fragment_shader(aaline
)) {
692 stage
->line
= draw_pipe_passthrough_line
;
693 stage
->line(stage
, header
);
697 draw_aaline_prepare_outputs(draw
, draw
->pipeline
.aaline
);
699 /* how many samplers? */
700 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
701 num_samplers
= MAX2(aaline
->num_sampler_views
, aaline
->num_samplers
);
702 num_samplers
= MAX2(num_samplers
, aaline
->fs
->sampler_unit
+ 1);
704 aaline
->state
.sampler
[aaline
->fs
->sampler_unit
] = aaline
->sampler_cso
;
705 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[aaline
->fs
->sampler_unit
],
706 aaline
->sampler_view
);
708 draw
->suspend_flushing
= TRUE
;
710 aaline
->driver_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
711 num_samplers
, aaline
->state
.sampler
);
713 aaline
->driver_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
714 num_samplers
, aaline
->state
.sampler_views
);
716 /* Disable triangle culling, stippling, unfilled mode etc. */
717 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
718 pipe
->bind_rasterizer_state(pipe
, r
);
720 draw
->suspend_flushing
= FALSE
;
722 /* now really draw first line */
723 stage
->line
= aaline_line
;
724 stage
->line(stage
, header
);
729 aaline_flush(struct draw_stage
*stage
, unsigned flags
)
731 struct draw_context
*draw
= stage
->draw
;
732 struct aaline_stage
*aaline
= aaline_stage(stage
);
733 struct pipe_context
*pipe
= draw
->pipe
;
735 stage
->line
= aaline_first_line
;
736 stage
->next
->flush( stage
->next
, flags
);
738 /* restore original frag shader, texture, sampler state */
739 draw
->suspend_flushing
= TRUE
;
740 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
? aaline
->fs
->driver_fs
: NULL
);
742 aaline
->driver_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
743 aaline
->num_samplers
,
744 aaline
->state
.sampler
);
746 aaline
->driver_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
747 aaline
->num_samplers
,
748 aaline
->state
.sampler_views
);
750 /* restore original rasterizer state */
751 if (draw
->rast_handle
) {
752 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
755 draw
->suspend_flushing
= FALSE
;
757 draw_remove_extra_vertex_attribs(draw
);
762 aaline_reset_stipple_counter(struct draw_stage
*stage
)
764 stage
->next
->reset_stipple_counter( stage
->next
);
769 aaline_destroy(struct draw_stage
*stage
)
771 struct aaline_stage
*aaline
= aaline_stage(stage
);
772 struct pipe_context
*pipe
= stage
->draw
->pipe
;
775 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
776 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[i
], NULL
);
779 if (aaline
->sampler_cso
)
780 pipe
->delete_sampler_state(pipe
, aaline
->sampler_cso
);
783 pipe_resource_reference(&aaline
->texture
, NULL
);
785 if (aaline
->sampler_view
) {
786 pipe_sampler_view_reference(&aaline
->sampler_view
, NULL
);
789 draw_free_temp_verts( stage
);
791 /* restore the old entry points */
792 pipe
->create_fs_state
= aaline
->driver_create_fs_state
;
793 pipe
->bind_fs_state
= aaline
->driver_bind_fs_state
;
794 pipe
->delete_fs_state
= aaline
->driver_delete_fs_state
;
796 pipe
->bind_sampler_states
= aaline
->driver_bind_sampler_states
;
797 pipe
->set_sampler_views
= aaline
->driver_set_sampler_views
;
803 static struct aaline_stage
*
804 draw_aaline_stage(struct draw_context
*draw
)
806 struct aaline_stage
*aaline
= CALLOC_STRUCT(aaline_stage
);
810 aaline
->stage
.draw
= draw
;
811 aaline
->stage
.name
= "aaline";
812 aaline
->stage
.next
= NULL
;
813 aaline
->stage
.point
= draw_pipe_passthrough_point
;
814 aaline
->stage
.line
= aaline_first_line
;
815 aaline
->stage
.tri
= draw_pipe_passthrough_tri
;
816 aaline
->stage
.flush
= aaline_flush
;
817 aaline
->stage
.reset_stipple_counter
= aaline_reset_stipple_counter
;
818 aaline
->stage
.destroy
= aaline_destroy
;
820 if (!draw_alloc_temp_verts( &aaline
->stage
, 8 ))
827 aaline
->stage
.destroy(&aaline
->stage
);
833 static struct aaline_stage
*
834 aaline_stage_from_pipe(struct pipe_context
*pipe
)
836 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
839 return aaline_stage(draw
->pipeline
.aaline
);
847 * This function overrides the driver's create_fs_state() function and
848 * will typically be called by the state tracker.
851 aaline_create_fs_state(struct pipe_context
*pipe
,
852 const struct pipe_shader_state
*fs
)
854 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
855 struct aaline_fragment_shader
*aafs
= NULL
;
860 aafs
= CALLOC_STRUCT(aaline_fragment_shader
);
865 aafs
->state
.tokens
= tgsi_dup_tokens(fs
->tokens
);
868 aafs
->driver_fs
= aaline
->driver_create_fs_state(pipe
, fs
);
875 aaline_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
877 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
878 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
880 if (aaline
== NULL
) {
887 aaline
->driver_bind_fs_state(pipe
, (aafs
? aafs
->driver_fs
: NULL
));
892 aaline_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
894 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
895 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
901 if (aaline
!= NULL
) {
903 aaline
->driver_delete_fs_state(pipe
, aafs
->driver_fs
);
906 aaline
->driver_delete_fs_state(pipe
, aafs
->aaline_fs
);
909 FREE((void*)aafs
->state
.tokens
);
915 aaline_bind_sampler_states(struct pipe_context
*pipe
, unsigned shader
,
916 unsigned start
, unsigned num
, void **sampler
)
918 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
922 if (aaline
== NULL
) {
926 if (shader
== PIPE_SHADER_FRAGMENT
) {
928 memcpy(aaline
->state
.sampler
, sampler
, num
* sizeof(void *));
929 aaline
->num_samplers
= num
;
933 aaline
->driver_bind_sampler_states(pipe
, shader
, start
, num
, sampler
);
938 aaline_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
939 unsigned start
, unsigned num
,
940 struct pipe_sampler_view
**views
)
942 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
945 if (aaline
== NULL
) {
949 if (shader
== PIPE_SHADER_FRAGMENT
) {
951 for (i
= 0; i
< num
; i
++) {
952 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[start
+ i
],
955 aaline
->num_sampler_views
= num
;
959 aaline
->driver_set_sampler_views(pipe
, shader
, start
, num
, views
);
964 draw_aaline_prepare_outputs(struct draw_context
*draw
,
965 struct draw_stage
*stage
)
967 struct aaline_stage
*aaline
= aaline_stage(stage
);
968 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
970 /* update vertex attrib info */
971 aaline
->pos_slot
= draw_current_shader_position_output(draw
);;
973 if (!rast
->line_smooth
)
976 /* allocate the extra post-transformed vertex attribute */
977 aaline
->tex_slot
= draw_alloc_extra_vertex_attrib(draw
,
978 TGSI_SEMANTIC_GENERIC
,
979 aaline
->fs
->generic_attrib
);
983 * Called by drivers that want to install this AA line prim stage
984 * into the draw module's pipeline. This will not be used if the
985 * hardware has native support for AA lines.
988 draw_install_aaline_stage(struct draw_context
*draw
, struct pipe_context
*pipe
)
990 struct aaline_stage
*aaline
;
992 pipe
->draw
= (void *) draw
;
995 * Create / install AA line drawing / prim stage
997 aaline
= draw_aaline_stage( draw
);
1001 /* save original driver functions */
1002 aaline
->driver_create_fs_state
= pipe
->create_fs_state
;
1003 aaline
->driver_bind_fs_state
= pipe
->bind_fs_state
;
1004 aaline
->driver_delete_fs_state
= pipe
->delete_fs_state
;
1006 aaline
->driver_bind_sampler_states
= pipe
->bind_sampler_states
;
1007 aaline
->driver_set_sampler_views
= pipe
->set_sampler_views
;
1009 /* create special texture, sampler state */
1010 if (!aaline_create_texture(aaline
))
1013 if (!aaline_create_sampler(aaline
))
1016 /* override the driver's functions */
1017 pipe
->create_fs_state
= aaline_create_fs_state
;
1018 pipe
->bind_fs_state
= aaline_bind_fs_state
;
1019 pipe
->delete_fs_state
= aaline_delete_fs_state
;
1021 pipe
->bind_sampler_states
= aaline_bind_sampler_states
;
1022 pipe
->set_sampler_views
= aaline_set_sampler_views
;
1024 /* Install once everything is known to be OK:
1026 draw
->pipeline
.aaline
= &aaline
->stage
;
1032 aaline
->stage
.destroy( &aaline
->stage
);