1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
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_SAMPLERS
];
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,
124 void (*driver_set_sampler_views
)(struct pipe_context
*,
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 */
143 boolean firstInstruction
;
148 * TGSI declaration transform callback.
149 * Look for a free sampler, a free input attrib, and two free temp regs.
152 aa_transform_decl(struct tgsi_transform_context
*ctx
,
153 struct tgsi_full_declaration
*decl
)
155 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
157 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
158 decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
159 decl
->Semantic
.Index
== 0) {
160 aactx
->colorOutput
= decl
->Range
.First
;
162 else if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
164 for (i
= decl
->Range
.First
;
165 i
<= decl
->Range
.Last
; i
++) {
166 aactx
->samplersUsed
|= 1 << i
;
169 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
170 if ((int) decl
->Range
.Last
> aactx
->maxInput
)
171 aactx
->maxInput
= decl
->Range
.Last
;
172 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
&&
173 (int) decl
->Semantic
.Index
> aactx
->maxGeneric
) {
174 aactx
->maxGeneric
= decl
->Semantic
.Index
;
177 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
179 for (i
= decl
->Range
.First
;
180 i
<= decl
->Range
.Last
; i
++) {
181 aactx
->tempsUsed
|= (1 << i
);
185 ctx
->emit_declaration(ctx
, decl
);
190 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
193 free_bit(uint bitfield
)
195 return ffs(~bitfield
) - 1;
200 * TGSI instruction transform callback.
201 * Replace writes to result.color w/ a temp reg.
202 * Upon END instruction, insert texture sampling code for antialiasing.
205 aa_transform_inst(struct tgsi_transform_context
*ctx
,
206 struct tgsi_full_instruction
*inst
)
208 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
210 if (aactx
->firstInstruction
) {
211 /* emit our new declarations before the first instruction */
213 struct tgsi_full_declaration decl
;
216 /* find free sampler */
217 aactx
->freeSampler
= free_bit(aactx
->samplersUsed
);
218 if (aactx
->freeSampler
>= PIPE_MAX_SAMPLERS
)
219 aactx
->freeSampler
= PIPE_MAX_SAMPLERS
- 1;
221 /* find two free temp regs */
222 for (i
= 0; i
< 32; i
++) {
223 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
224 /* found a free temp */
225 if (aactx
->colorTemp
< 0)
226 aactx
->colorTemp
= i
;
227 else if (aactx
->texTemp
< 0)
233 assert(aactx
->colorTemp
>= 0);
234 assert(aactx
->texTemp
>= 0);
236 /* declare new generic input/texcoord */
237 decl
= tgsi_default_full_declaration();
238 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
239 /* XXX this could be linear... */
240 decl
.Declaration
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
241 decl
.Declaration
.Semantic
= 1;
242 decl
.Semantic
.Name
= TGSI_SEMANTIC_GENERIC
;
243 decl
.Semantic
.Index
= aactx
->maxGeneric
+ 1;
245 decl
.Range
.Last
= aactx
->maxInput
+ 1;
246 ctx
->emit_declaration(ctx
, &decl
);
248 /* declare new sampler */
249 decl
= tgsi_default_full_declaration();
250 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
252 decl
.Range
.Last
= aactx
->freeSampler
;
253 ctx
->emit_declaration(ctx
, &decl
);
255 /* declare new temp regs */
256 decl
= tgsi_default_full_declaration();
257 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
259 decl
.Range
.Last
= aactx
->texTemp
;
260 ctx
->emit_declaration(ctx
, &decl
);
262 decl
= tgsi_default_full_declaration();
263 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
265 decl
.Range
.Last
= aactx
->colorTemp
;
266 ctx
->emit_declaration(ctx
, &decl
);
268 aactx
->firstInstruction
= FALSE
;
271 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
&&
272 aactx
->colorOutput
!= -1) {
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
;
289 ctx
->emit_instruction(ctx
, &newInst
);
292 newInst
= tgsi_default_full_instruction();
293 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
294 newInst
.Instruction
.NumDstRegs
= 1;
295 newInst
.Dst
[0].Register
.File
= TGSI_FILE_OUTPUT
;
296 newInst
.Dst
[0].Register
.Index
= aactx
->colorOutput
;
297 newInst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_XYZ
;
298 newInst
.Instruction
.NumSrcRegs
= 1;
299 newInst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
300 newInst
.Src
[0].Register
.Index
= aactx
->colorTemp
;
301 ctx
->emit_instruction(ctx
, &newInst
);
304 newInst
= tgsi_default_full_instruction();
305 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
306 newInst
.Instruction
.NumDstRegs
= 1;
307 newInst
.Dst
[0].Register
.File
= TGSI_FILE_OUTPUT
;
308 newInst
.Dst
[0].Register
.Index
= aactx
->colorOutput
;
309 newInst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_W
;
310 newInst
.Instruction
.NumSrcRegs
= 2;
311 newInst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
312 newInst
.Src
[0].Register
.Index
= aactx
->colorTemp
;
313 newInst
.Src
[1].Register
.File
= TGSI_FILE_TEMPORARY
;
314 newInst
.Src
[1].Register
.Index
= aactx
->texTemp
;
315 ctx
->emit_instruction(ctx
, &newInst
);
318 newInst
= tgsi_default_full_instruction();
319 newInst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
320 newInst
.Instruction
.NumDstRegs
= 0;
321 newInst
.Instruction
.NumSrcRegs
= 0;
322 ctx
->emit_instruction(ctx
, &newInst
);
325 /* Not an END instruction.
326 * Look for writes to result.color and replace with colorTemp reg.
330 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
331 struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
332 if (dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
333 dst
->Register
.Index
== aactx
->colorOutput
) {
334 dst
->Register
.File
= TGSI_FILE_TEMPORARY
;
335 dst
->Register
.Index
= aactx
->colorTemp
;
339 ctx
->emit_instruction(ctx
, inst
);
345 * Generate the frag shader we'll use for drawing AA lines.
346 * This will be the user's shader plus some texture/modulate instructions.
349 generate_aaline_fs(struct aaline_stage
*aaline
)
351 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
352 const struct pipe_shader_state
*orig_fs
= &aaline
->fs
->state
;
353 struct pipe_shader_state aaline_fs
;
354 struct aa_transform_context transform
;
355 const uint newLen
= tgsi_num_tokens(orig_fs
->tokens
) + NUM_NEW_TOKENS
;
357 aaline_fs
= *orig_fs
; /* copy to init */
358 aaline_fs
.tokens
= tgsi_alloc_tokens(newLen
);
359 if (aaline_fs
.tokens
== NULL
)
362 memset(&transform
, 0, sizeof(transform
));
363 transform
.colorOutput
= -1;
364 transform
.maxInput
= -1;
365 transform
.maxGeneric
= -1;
366 transform
.colorTemp
= -1;
367 transform
.texTemp
= -1;
368 transform
.firstInstruction
= TRUE
;
369 transform
.base
.transform_instruction
= aa_transform_inst
;
370 transform
.base
.transform_declaration
= aa_transform_decl
;
372 tgsi_transform_shader(orig_fs
->tokens
,
373 (struct tgsi_token
*) aaline_fs
.tokens
,
374 newLen
, &transform
.base
);
377 tgsi_dump(orig_fs
->tokens
, 0);
378 tgsi_dump(aaline_fs
.tokens
, 0);
381 aaline
->fs
->sampler_unit
= transform
.freeSampler
;
383 aaline
->fs
->aaline_fs
= aaline
->driver_create_fs_state(pipe
, &aaline_fs
);
384 if (aaline
->fs
->aaline_fs
== NULL
)
387 aaline
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
388 FREE((void *)aaline_fs
.tokens
);
392 FREE((void *)aaline_fs
.tokens
);
398 * Create the texture map we'll use for antialiasing the lines.
401 aaline_create_texture(struct aaline_stage
*aaline
)
403 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
404 struct pipe_screen
*screen
= pipe
->screen
;
405 struct pipe_resource texTemp
;
406 struct pipe_sampler_view viewTempl
;
409 memset(&texTemp
, 0, sizeof(texTemp
));
410 texTemp
.target
= PIPE_TEXTURE_2D
;
411 texTemp
.format
= PIPE_FORMAT_A8_UNORM
; /* XXX verify supported by driver! */
412 texTemp
.last_level
= MAX_TEXTURE_LEVEL
;
413 texTemp
.width0
= 1 << TEXTURE_SIZE_LOG2
;
414 texTemp
.height0
= 1 << TEXTURE_SIZE_LOG2
;
416 texTemp
.array_size
= 1;
417 texTemp
.bind
= PIPE_BIND_SAMPLER_VIEW
;
419 aaline
->texture
= screen
->resource_create(screen
, &texTemp
);
420 if (!aaline
->texture
)
423 u_sampler_view_default_template(&viewTempl
,
425 aaline
->texture
->format
);
426 aaline
->sampler_view
= pipe
->create_sampler_view(pipe
,
429 if (!aaline
->sampler_view
) {
433 /* Fill in mipmap images.
434 * Basically each level is solid opaque, except for the outermost
435 * texels which are zero. Special case the 1x1 and 2x2 levels
436 * (though, those levels shouldn't be used - see the max_lod setting).
438 for (level
= 0; level
<= MAX_TEXTURE_LEVEL
; level
++) {
439 struct pipe_transfer
*transfer
;
441 const uint size
= u_minify(aaline
->texture
->width0
, level
);
445 assert(aaline
->texture
->width0
== aaline
->texture
->height0
);
447 u_box_origin_2d( size
, size
, &box
);
449 /* This texture is new, no need to flush.
451 transfer
= pipe
->get_transfer(pipe
,
457 data
= pipe
->transfer_map(pipe
, transfer
);
461 for (i
= 0; i
< size
; i
++) {
462 for (j
= 0; j
< size
; j
++) {
467 else if (size
== 2) {
468 d
= 200; /* tuneable */
470 else if (i
== 0 || j
== 0 || i
== size
- 1 || j
== size
- 1) {
471 d
= 35; /* edge texel */
476 data
[i
* transfer
->stride
+ j
] = d
;
481 pipe
->transfer_unmap(pipe
, transfer
);
482 pipe
->transfer_destroy(pipe
, transfer
);
489 * Create the sampler CSO that'll be used for antialiasing.
490 * By using a mipmapped texture, we don't have to generate a different
491 * texture image for each line size.
494 aaline_create_sampler(struct aaline_stage
*aaline
)
496 struct pipe_sampler_state sampler
;
497 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
499 memset(&sampler
, 0, sizeof(sampler
));
500 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
501 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
502 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
503 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
504 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
505 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
506 sampler
.normalized_coords
= 1;
507 sampler
.min_lod
= 0.0f
;
508 sampler
.max_lod
= MAX_TEXTURE_LEVEL
;
510 aaline
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
511 if (aaline
->sampler_cso
== NULL
)
519 * When we're about to draw our first AA line in a batch, this function is
520 * called to tell the driver to bind our modified fragment shader.
523 bind_aaline_fragment_shader(struct aaline_stage
*aaline
)
525 struct draw_context
*draw
= aaline
->stage
.draw
;
526 struct pipe_context
*pipe
= draw
->pipe
;
528 if (!aaline
->fs
->aaline_fs
&&
529 !generate_aaline_fs(aaline
))
532 draw
->suspend_flushing
= TRUE
;
533 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->aaline_fs
);
534 draw
->suspend_flushing
= FALSE
;
541 static INLINE
struct aaline_stage
*
542 aaline_stage( struct draw_stage
*stage
)
544 return (struct aaline_stage
*) stage
;
549 * Draw a wide line by drawing a quad, using geometry which will
550 * fullfill GL's antialiased line requirements.
553 aaline_line(struct draw_stage
*stage
, struct prim_header
*header
)
555 const struct aaline_stage
*aaline
= aaline_stage(stage
);
556 const float half_width
= aaline
->half_line_width
;
557 struct prim_header tri
;
558 struct vertex_header
*v
[8];
559 uint texPos
= aaline
->tex_slot
;
560 uint posPos
= aaline
->pos_slot
;
562 float dx
= header
->v
[1]->data
[posPos
][0] - header
->v
[0]->data
[posPos
][0];
563 float dy
= header
->v
[1]->data
[posPos
][1] - header
->v
[0]->data
[posPos
][1];
564 double a
= atan2(dy
, dx
);
565 float c_a
= (float) cos(a
), s_a
= (float) sin(a
);
568 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
569 dx
= 0.5F
* half_width
;
572 /* allocate/dup new verts */
573 for (i
= 0; i
< 8; i
++) {
574 v
[i
] = dup_vert(stage
, header
->v
[i
/4], i
);
578 * Quad strip for line from v0 to v1 (*=endpoints):
581 * +---+---------------------+---+
585 * +---+---------------------+---+
590 pos
= v
[0]->data
[posPos
];
591 pos
[0] += (-dx
* c_a
- dy
* s_a
);
592 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
594 pos
= v
[1]->data
[posPos
];
595 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
596 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
598 pos
= v
[2]->data
[posPos
];
599 pos
[0] += ( dx
* c_a
- dy
* s_a
);
600 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
602 pos
= v
[3]->data
[posPos
];
603 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
604 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
606 pos
= v
[4]->data
[posPos
];
607 pos
[0] += (-dx
* c_a
- dy
* s_a
);
608 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
610 pos
= v
[5]->data
[posPos
];
611 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
612 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
614 pos
= v
[6]->data
[posPos
];
615 pos
[0] += ( dx
* c_a
- dy
* s_a
);
616 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
618 pos
= v
[7]->data
[posPos
];
619 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
620 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
623 tex
= v
[0]->data
[texPos
];
624 ASSIGN_4V(tex
, 0, 0, 0, 1);
626 tex
= v
[1]->data
[texPos
];
627 ASSIGN_4V(tex
, 0, 1, 0, 1);
629 tex
= v
[2]->data
[texPos
];
630 ASSIGN_4V(tex
, .5, 0, 0, 1);
632 tex
= v
[3]->data
[texPos
];
633 ASSIGN_4V(tex
, .5, 1, 0, 1);
635 tex
= v
[4]->data
[texPos
];
636 ASSIGN_4V(tex
, .5, 0, 0, 1);
638 tex
= v
[5]->data
[texPos
];
639 ASSIGN_4V(tex
, .5, 1, 0, 1);
641 tex
= v
[6]->data
[texPos
];
642 ASSIGN_4V(tex
, 1, 0, 0, 1);
644 tex
= v
[7]->data
[texPos
];
645 ASSIGN_4V(tex
, 1, 1, 0, 1);
647 /* emit 6 tris for the quad strip */
648 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
649 stage
->next
->tri( stage
->next
, &tri
);
651 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
652 stage
->next
->tri( stage
->next
, &tri
);
654 tri
.v
[0] = v
[4]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[2];
655 stage
->next
->tri( stage
->next
, &tri
);
657 tri
.v
[0] = v
[5]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[4];
658 stage
->next
->tri( stage
->next
, &tri
);
660 tri
.v
[0] = v
[6]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[4];
661 stage
->next
->tri( stage
->next
, &tri
);
663 tri
.v
[0] = v
[7]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[6];
664 stage
->next
->tri( stage
->next
, &tri
);
669 aaline_first_line(struct draw_stage
*stage
, struct prim_header
*header
)
671 auto struct aaline_stage
*aaline
= aaline_stage(stage
);
672 struct draw_context
*draw
= stage
->draw
;
673 struct pipe_context
*pipe
= draw
->pipe
;
674 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
678 assert(draw
->rasterizer
->line_smooth
);
680 if (draw
->rasterizer
->line_width
<= 2.2)
681 aaline
->half_line_width
= 1.1f
;
683 aaline
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
686 * Bind (generate) our fragprog, sampler and texture
688 if (!bind_aaline_fragment_shader(aaline
)) {
689 stage
->line
= draw_pipe_passthrough_line
;
690 stage
->line(stage
, header
);
694 /* update vertex attrib info */
695 aaline
->tex_slot
= draw_current_shader_outputs(draw
);
696 aaline
->pos_slot
= draw_current_shader_position_output(draw
);;
698 /* allocate the extra post-transformed vertex attribute */
699 (void) draw_alloc_extra_vertex_attrib(draw
, TGSI_SEMANTIC_GENERIC
,
700 aaline
->fs
->generic_attrib
);
702 /* how many samplers? */
703 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
704 num_samplers
= MAX2(aaline
->num_sampler_views
, aaline
->num_samplers
);
705 num_samplers
= MAX2(num_samplers
, aaline
->fs
->sampler_unit
+ 1);
707 aaline
->state
.sampler
[aaline
->fs
->sampler_unit
] = aaline
->sampler_cso
;
708 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[aaline
->fs
->sampler_unit
],
709 aaline
->sampler_view
);
711 draw
->suspend_flushing
= TRUE
;
712 aaline
->driver_bind_sampler_states(pipe
, num_samplers
, aaline
->state
.sampler
);
713 aaline
->driver_set_sampler_views(pipe
, num_samplers
, aaline
->state
.sampler_views
);
715 /* Disable triangle culling, stippling, unfilled mode etc. */
716 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
717 pipe
->bind_rasterizer_state(pipe
, r
);
719 draw
->suspend_flushing
= FALSE
;
721 /* now really draw first line */
722 stage
->line
= aaline_line
;
723 stage
->line(stage
, header
);
728 aaline_flush(struct draw_stage
*stage
, unsigned flags
)
730 struct draw_context
*draw
= stage
->draw
;
731 struct aaline_stage
*aaline
= aaline_stage(stage
);
732 struct pipe_context
*pipe
= draw
->pipe
;
734 stage
->line
= aaline_first_line
;
735 stage
->next
->flush( stage
->next
, flags
);
737 /* restore original frag shader, texture, sampler state */
738 draw
->suspend_flushing
= TRUE
;
739 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->driver_fs
);
740 aaline
->driver_bind_sampler_states(pipe
, aaline
->num_samplers
,
741 aaline
->state
.sampler
);
742 aaline
->driver_set_sampler_views(pipe
,
743 aaline
->num_sampler_views
,
744 aaline
->state
.sampler_views
);
746 /* restore original rasterizer state */
747 if (draw
->rast_handle
) {
748 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
751 draw
->suspend_flushing
= FALSE
;
753 draw_remove_extra_vertex_attribs(draw
);
758 aaline_reset_stipple_counter(struct draw_stage
*stage
)
760 stage
->next
->reset_stipple_counter( stage
->next
);
765 aaline_destroy(struct draw_stage
*stage
)
767 struct aaline_stage
*aaline
= aaline_stage(stage
);
768 struct pipe_context
*pipe
= stage
->draw
->pipe
;
771 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
772 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[i
], NULL
);
775 if (aaline
->sampler_cso
)
776 pipe
->delete_sampler_state(pipe
, aaline
->sampler_cso
);
779 pipe_resource_reference(&aaline
->texture
, NULL
);
781 if (aaline
->sampler_view
) {
782 pipe_sampler_view_reference(&aaline
->sampler_view
, NULL
);
785 draw_free_temp_verts( stage
);
791 static struct aaline_stage
*
792 draw_aaline_stage(struct draw_context
*draw
)
794 struct aaline_stage
*aaline
= CALLOC_STRUCT(aaline_stage
);
798 aaline
->stage
.draw
= draw
;
799 aaline
->stage
.name
= "aaline";
800 aaline
->stage
.next
= NULL
;
801 aaline
->stage
.point
= draw_pipe_passthrough_point
;
802 aaline
->stage
.line
= aaline_first_line
;
803 aaline
->stage
.tri
= draw_pipe_passthrough_tri
;
804 aaline
->stage
.flush
= aaline_flush
;
805 aaline
->stage
.reset_stipple_counter
= aaline_reset_stipple_counter
;
806 aaline
->stage
.destroy
= aaline_destroy
;
808 if (!draw_alloc_temp_verts( &aaline
->stage
, 8 ))
815 aaline
->stage
.destroy(&aaline
->stage
);
821 static struct aaline_stage
*
822 aaline_stage_from_pipe(struct pipe_context
*pipe
)
824 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
825 return aaline_stage(draw
->pipeline
.aaline
);
830 * This function overrides the driver's create_fs_state() function and
831 * will typically be called by the state tracker.
834 aaline_create_fs_state(struct pipe_context
*pipe
,
835 const struct pipe_shader_state
*fs
)
837 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
838 struct aaline_fragment_shader
*aafs
= CALLOC_STRUCT(aaline_fragment_shader
);
846 aafs
->driver_fs
= aaline
->driver_create_fs_state(pipe
, fs
);
853 aaline_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
855 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
856 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
861 aaline
->driver_bind_fs_state(pipe
, (aafs
? aafs
->driver_fs
: NULL
));
866 aaline_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
868 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
869 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
872 aaline
->driver_delete_fs_state(pipe
, aafs
->driver_fs
);
875 aaline
->driver_delete_fs_state(pipe
, aafs
->aaline_fs
);
882 aaline_bind_sampler_states(struct pipe_context
*pipe
,
883 unsigned num
, void **sampler
)
885 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
888 memcpy(aaline
->state
.sampler
, sampler
, num
* sizeof(void *));
889 aaline
->num_samplers
= num
;
892 aaline
->driver_bind_sampler_states(pipe
, num
, sampler
);
897 aaline_set_sampler_views(struct pipe_context
*pipe
,
899 struct pipe_sampler_view
**views
)
901 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
905 for (i
= 0; i
< num
; i
++) {
906 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[i
], views
[i
]);
908 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
909 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[i
], NULL
);
911 aaline
->num_sampler_views
= num
;
914 aaline
->driver_set_sampler_views(pipe
, num
, views
);
919 * Called by drivers that want to install this AA line prim stage
920 * into the draw module's pipeline. This will not be used if the
921 * hardware has native support for AA lines.
924 draw_install_aaline_stage(struct draw_context
*draw
, struct pipe_context
*pipe
)
926 struct aaline_stage
*aaline
;
928 pipe
->draw
= (void *) draw
;
931 * Create / install AA line drawing / prim stage
933 aaline
= draw_aaline_stage( draw
);
937 /* create special texture, sampler state */
938 if (!aaline_create_texture(aaline
))
941 if (!aaline_create_sampler(aaline
))
944 /* save original driver functions */
945 aaline
->driver_create_fs_state
= pipe
->create_fs_state
;
946 aaline
->driver_bind_fs_state
= pipe
->bind_fs_state
;
947 aaline
->driver_delete_fs_state
= pipe
->delete_fs_state
;
949 aaline
->driver_bind_sampler_states
= pipe
->bind_fragment_sampler_states
;
950 aaline
->driver_set_sampler_views
= pipe
->set_fragment_sampler_views
;
952 /* override the driver's functions */
953 pipe
->create_fs_state
= aaline_create_fs_state
;
954 pipe
->bind_fs_state
= aaline_bind_fs_state
;
955 pipe
->delete_fs_state
= aaline_delete_fs_state
;
957 pipe
->bind_fragment_sampler_states
= aaline_bind_sampler_states
;
958 pipe
->set_fragment_sampler_views
= aaline_set_sampler_views
;
960 /* Install once everything is known to be OK:
962 draw
->pipeline
.aaline
= &aaline
->stage
;
968 aaline
->stage
.destroy( &aaline
->stage
);