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 53
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 */
141 int freeSampler
; /** an available sampler for the pstipple */
142 int maxInput
, maxGeneric
; /**< max input index found */
143 int colorTemp
, texTemp
; /**< temp registers */
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_SAMPLER_VIEW
) {
170 aactx
->hasSview
= true;
172 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
173 if ((int) decl
->Range
.Last
> aactx
->maxInput
)
174 aactx
->maxInput
= decl
->Range
.Last
;
175 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
&&
176 (int) decl
->Semantic
.Index
> aactx
->maxGeneric
) {
177 aactx
->maxGeneric
= decl
->Semantic
.Index
;
180 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
182 for (i
= decl
->Range
.First
;
183 i
<= decl
->Range
.Last
; i
++) {
184 aactx
->tempsUsed
|= (1 << i
);
188 ctx
->emit_declaration(ctx
, decl
);
193 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
196 free_bit(uint bitfield
)
198 return ffs(~bitfield
) - 1;
203 * TGSI transform prolog callback.
206 aa_transform_prolog(struct tgsi_transform_context
*ctx
)
208 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
211 /* find free sampler */
212 aactx
->freeSampler
= free_bit(aactx
->samplersUsed
);
213 if (aactx
->freeSampler
>= PIPE_MAX_SAMPLERS
)
214 aactx
->freeSampler
= PIPE_MAX_SAMPLERS
- 1;
216 /* find two free temp regs */
217 for (i
= 0; i
< 32; i
++) {
218 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
219 /* found a free temp */
220 if (aactx
->colorTemp
< 0)
221 aactx
->colorTemp
= i
;
222 else if (aactx
->texTemp
< 0)
228 assert(aactx
->colorTemp
>= 0);
229 assert(aactx
->texTemp
>= 0);
231 /* declare new generic input/texcoord */
232 tgsi_transform_input_decl(ctx
, aactx
->maxInput
+ 1,
233 TGSI_SEMANTIC_GENERIC
, aactx
->maxGeneric
+ 1,
234 TGSI_INTERPOLATE_LINEAR
);
236 /* declare new sampler */
237 tgsi_transform_sampler_decl(ctx
, aactx
->freeSampler
);
239 /* if the src shader has SVIEW decl's for each SAMP decl, we
240 * need to continue the trend and ensure there is a matching
241 * SVIEW for the new SAMP we just created
243 if (aactx
->hasSview
) {
244 tgsi_transform_sampler_view_decl(ctx
,
247 TGSI_RETURN_TYPE_FLOAT
);
250 /* declare new temp regs */
251 tgsi_transform_temp_decl(ctx
, aactx
->texTemp
);
252 tgsi_transform_temp_decl(ctx
, aactx
->colorTemp
);
257 * TGSI transform epilog callback.
260 aa_transform_epilog(struct tgsi_transform_context
*ctx
)
262 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
264 if (aactx
->colorOutput
!= -1) {
265 /* insert texture sampling code for antialiasing. */
267 /* TEX texTemp, input_coord, sampler */
268 tgsi_transform_tex_2d_inst(ctx
,
269 TGSI_FILE_TEMPORARY
, aactx
->texTemp
,
270 TGSI_FILE_INPUT
, aactx
->maxInput
+ 1,
274 tgsi_transform_op1_inst(ctx
, TGSI_OPCODE_MOV
,
275 TGSI_FILE_OUTPUT
, aactx
->colorOutput
,
277 TGSI_FILE_TEMPORARY
, aactx
->colorTemp
);
280 tgsi_transform_op2_inst(ctx
, TGSI_OPCODE_MUL
,
281 TGSI_FILE_OUTPUT
, aactx
->colorOutput
,
283 TGSI_FILE_TEMPORARY
, aactx
->colorTemp
,
284 TGSI_FILE_TEMPORARY
, aactx
->texTemp
);
290 * TGSI instruction transform callback.
291 * Replace writes to result.color w/ a temp reg.
294 aa_transform_inst(struct tgsi_transform_context
*ctx
,
295 struct tgsi_full_instruction
*inst
)
297 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
301 * Look for writes to result.color and replace with colorTemp reg.
303 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
304 struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
305 if (dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
306 dst
->Register
.Index
== aactx
->colorOutput
) {
307 dst
->Register
.File
= TGSI_FILE_TEMPORARY
;
308 dst
->Register
.Index
= aactx
->colorTemp
;
312 ctx
->emit_instruction(ctx
, inst
);
317 * Generate the frag shader we'll use for drawing AA lines.
318 * This will be the user's shader plus some texture/modulate instructions.
321 generate_aaline_fs(struct aaline_stage
*aaline
)
323 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
324 const struct pipe_shader_state
*orig_fs
= &aaline
->fs
->state
;
325 struct pipe_shader_state aaline_fs
;
326 struct aa_transform_context transform
;
327 const uint newLen
= tgsi_num_tokens(orig_fs
->tokens
) + NUM_NEW_TOKENS
;
329 aaline_fs
= *orig_fs
; /* copy to init */
330 aaline_fs
.tokens
= tgsi_alloc_tokens(newLen
);
331 if (aaline_fs
.tokens
== NULL
)
334 memset(&transform
, 0, sizeof(transform
));
335 transform
.colorOutput
= -1;
336 transform
.maxInput
= -1;
337 transform
.maxGeneric
= -1;
338 transform
.colorTemp
= -1;
339 transform
.texTemp
= -1;
340 transform
.base
.prolog
= aa_transform_prolog
;
341 transform
.base
.epilog
= aa_transform_epilog
;
342 transform
.base
.transform_instruction
= aa_transform_inst
;
343 transform
.base
.transform_declaration
= aa_transform_decl
;
345 tgsi_transform_shader(orig_fs
->tokens
,
346 (struct tgsi_token
*) aaline_fs
.tokens
,
347 newLen
, &transform
.base
);
350 debug_printf("draw_aaline, orig shader:\n");
351 tgsi_dump(orig_fs
->tokens
, 0);
352 debug_printf("draw_aaline, new shader:\n");
353 tgsi_dump(aaline_fs
.tokens
, 0);
356 aaline
->fs
->sampler_unit
= transform
.freeSampler
;
358 aaline
->fs
->aaline_fs
= aaline
->driver_create_fs_state(pipe
, &aaline_fs
);
359 if (aaline
->fs
->aaline_fs
== NULL
)
362 aaline
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
363 FREE((void *)aaline_fs
.tokens
);
367 FREE((void *)aaline_fs
.tokens
);
373 * Create the texture map we'll use for antialiasing the lines.
376 aaline_create_texture(struct aaline_stage
*aaline
)
378 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
379 struct pipe_screen
*screen
= pipe
->screen
;
380 struct pipe_resource texTemp
;
381 struct pipe_sampler_view viewTempl
;
384 memset(&texTemp
, 0, sizeof(texTemp
));
385 texTemp
.target
= PIPE_TEXTURE_2D
;
386 texTemp
.format
= PIPE_FORMAT_A8_UNORM
; /* XXX verify supported by driver! */
387 texTemp
.last_level
= MAX_TEXTURE_LEVEL
;
388 texTemp
.width0
= 1 << TEXTURE_SIZE_LOG2
;
389 texTemp
.height0
= 1 << TEXTURE_SIZE_LOG2
;
391 texTemp
.array_size
= 1;
392 texTemp
.bind
= PIPE_BIND_SAMPLER_VIEW
;
394 aaline
->texture
= screen
->resource_create(screen
, &texTemp
);
395 if (!aaline
->texture
)
398 u_sampler_view_default_template(&viewTempl
,
400 aaline
->texture
->format
);
401 aaline
->sampler_view
= pipe
->create_sampler_view(pipe
,
404 if (!aaline
->sampler_view
) {
408 /* Fill in mipmap images.
409 * Basically each level is solid opaque, except for the outermost
410 * texels which are zero. Special case the 1x1 and 2x2 levels
411 * (though, those levels shouldn't be used - see the max_lod setting).
413 for (level
= 0; level
<= MAX_TEXTURE_LEVEL
; level
++) {
414 struct pipe_transfer
*transfer
;
416 const uint size
= u_minify(aaline
->texture
->width0
, level
);
420 assert(aaline
->texture
->width0
== aaline
->texture
->height0
);
422 u_box_origin_2d( size
, size
, &box
);
424 /* This texture is new, no need to flush.
426 data
= pipe
->transfer_map(pipe
,
435 for (i
= 0; i
< size
; i
++) {
436 for (j
= 0; j
< size
; j
++) {
441 else if (size
== 2) {
442 d
= 200; /* tuneable */
444 else if (i
== 0 || j
== 0 || i
== size
- 1 || j
== size
- 1) {
445 d
= 35; /* edge texel */
450 data
[i
* transfer
->stride
+ j
] = d
;
455 pipe
->transfer_unmap(pipe
, transfer
);
462 * Create the sampler CSO that'll be used for antialiasing.
463 * By using a mipmapped texture, we don't have to generate a different
464 * texture image for each line size.
467 aaline_create_sampler(struct aaline_stage
*aaline
)
469 struct pipe_sampler_state sampler
;
470 struct pipe_context
*pipe
= aaline
->stage
.draw
->pipe
;
472 memset(&sampler
, 0, sizeof(sampler
));
473 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
474 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
475 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
476 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
477 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
478 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
479 sampler
.normalized_coords
= 1;
480 sampler
.min_lod
= 0.0f
;
481 sampler
.max_lod
= MAX_TEXTURE_LEVEL
;
483 aaline
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
484 if (aaline
->sampler_cso
== NULL
)
492 * When we're about to draw our first AA line in a batch, this function is
493 * called to tell the driver to bind our modified fragment shader.
496 bind_aaline_fragment_shader(struct aaline_stage
*aaline
)
498 struct draw_context
*draw
= aaline
->stage
.draw
;
499 struct pipe_context
*pipe
= draw
->pipe
;
501 if (!aaline
->fs
->aaline_fs
&&
502 !generate_aaline_fs(aaline
))
505 draw
->suspend_flushing
= TRUE
;
506 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->aaline_fs
);
507 draw
->suspend_flushing
= FALSE
;
514 static INLINE
struct aaline_stage
*
515 aaline_stage( struct draw_stage
*stage
)
517 return (struct aaline_stage
*) stage
;
522 * Draw a wide line by drawing a quad, using geometry which will
523 * fullfill GL's antialiased line requirements.
526 aaline_line(struct draw_stage
*stage
, struct prim_header
*header
)
528 const struct aaline_stage
*aaline
= aaline_stage(stage
);
529 const float half_width
= aaline
->half_line_width
;
530 struct prim_header tri
;
531 struct vertex_header
*v
[8];
532 uint texPos
= aaline
->tex_slot
;
533 uint posPos
= aaline
->pos_slot
;
535 float dx
= header
->v
[1]->data
[posPos
][0] - header
->v
[0]->data
[posPos
][0];
536 float dy
= header
->v
[1]->data
[posPos
][1] - header
->v
[0]->data
[posPos
][1];
537 double a
= atan2(dy
, dx
);
538 float c_a
= (float) cos(a
), s_a
= (float) sin(a
);
541 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
542 dx
= 0.5F
* half_width
;
545 /* allocate/dup new verts */
546 for (i
= 0; i
< 8; i
++) {
547 v
[i
] = dup_vert(stage
, header
->v
[i
/4], i
);
551 * Quad strip for line from v0 to v1 (*=endpoints):
554 * +---+---------------------+---+
558 * +---+---------------------+---+
563 pos
= v
[0]->data
[posPos
];
564 pos
[0] += (-dx
* c_a
- dy
* s_a
);
565 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
567 pos
= v
[1]->data
[posPos
];
568 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
569 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
571 pos
= v
[2]->data
[posPos
];
572 pos
[0] += ( dx
* c_a
- dy
* s_a
);
573 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
575 pos
= v
[3]->data
[posPos
];
576 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
577 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
579 pos
= v
[4]->data
[posPos
];
580 pos
[0] += (-dx
* c_a
- dy
* s_a
);
581 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
583 pos
= v
[5]->data
[posPos
];
584 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
585 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
587 pos
= v
[6]->data
[posPos
];
588 pos
[0] += ( dx
* c_a
- dy
* s_a
);
589 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
591 pos
= v
[7]->data
[posPos
];
592 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
593 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
596 tex
= v
[0]->data
[texPos
];
597 ASSIGN_4V(tex
, 0, 0, 0, 1);
599 tex
= v
[1]->data
[texPos
];
600 ASSIGN_4V(tex
, 0, 1, 0, 1);
602 tex
= v
[2]->data
[texPos
];
603 ASSIGN_4V(tex
, .5, 0, 0, 1);
605 tex
= v
[3]->data
[texPos
];
606 ASSIGN_4V(tex
, .5, 1, 0, 1);
608 tex
= v
[4]->data
[texPos
];
609 ASSIGN_4V(tex
, .5, 0, 0, 1);
611 tex
= v
[5]->data
[texPos
];
612 ASSIGN_4V(tex
, .5, 1, 0, 1);
614 tex
= v
[6]->data
[texPos
];
615 ASSIGN_4V(tex
, 1, 0, 0, 1);
617 tex
= v
[7]->data
[texPos
];
618 ASSIGN_4V(tex
, 1, 1, 0, 1);
620 /* emit 6 tris for the quad strip */
621 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
622 stage
->next
->tri( stage
->next
, &tri
);
624 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
625 stage
->next
->tri( stage
->next
, &tri
);
627 tri
.v
[0] = v
[4]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[2];
628 stage
->next
->tri( stage
->next
, &tri
);
630 tri
.v
[0] = v
[5]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[4];
631 stage
->next
->tri( stage
->next
, &tri
);
633 tri
.v
[0] = v
[6]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[4];
634 stage
->next
->tri( stage
->next
, &tri
);
636 tri
.v
[0] = v
[7]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[6];
637 stage
->next
->tri( stage
->next
, &tri
);
642 aaline_first_line(struct draw_stage
*stage
, struct prim_header
*header
)
644 auto struct aaline_stage
*aaline
= aaline_stage(stage
);
645 struct draw_context
*draw
= stage
->draw
;
646 struct pipe_context
*pipe
= draw
->pipe
;
647 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
651 assert(draw
->rasterizer
->line_smooth
);
653 if (draw
->rasterizer
->line_width
<= 2.2)
654 aaline
->half_line_width
= 1.1f
;
656 aaline
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
659 * Bind (generate) our fragprog, sampler and texture
661 if (!bind_aaline_fragment_shader(aaline
)) {
662 stage
->line
= draw_pipe_passthrough_line
;
663 stage
->line(stage
, header
);
667 draw_aaline_prepare_outputs(draw
, draw
->pipeline
.aaline
);
669 /* how many samplers? */
670 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
671 num_samplers
= MAX2(aaline
->num_sampler_views
, aaline
->num_samplers
);
672 num_samplers
= MAX2(num_samplers
, aaline
->fs
->sampler_unit
+ 1);
674 aaline
->state
.sampler
[aaline
->fs
->sampler_unit
] = aaline
->sampler_cso
;
675 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[aaline
->fs
->sampler_unit
],
676 aaline
->sampler_view
);
678 draw
->suspend_flushing
= TRUE
;
680 aaline
->driver_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
681 num_samplers
, aaline
->state
.sampler
);
683 aaline
->driver_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
684 num_samplers
, aaline
->state
.sampler_views
);
686 /* Disable triangle culling, stippling, unfilled mode etc. */
687 r
= draw_get_rasterizer_no_cull(draw
, rast
->scissor
, rast
->flatshade
);
688 pipe
->bind_rasterizer_state(pipe
, r
);
690 draw
->suspend_flushing
= FALSE
;
692 /* now really draw first line */
693 stage
->line
= aaline_line
;
694 stage
->line(stage
, header
);
699 aaline_flush(struct draw_stage
*stage
, unsigned flags
)
701 struct draw_context
*draw
= stage
->draw
;
702 struct aaline_stage
*aaline
= aaline_stage(stage
);
703 struct pipe_context
*pipe
= draw
->pipe
;
705 stage
->line
= aaline_first_line
;
706 stage
->next
->flush( stage
->next
, flags
);
708 /* restore original frag shader, texture, sampler state */
709 draw
->suspend_flushing
= TRUE
;
710 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
? aaline
->fs
->driver_fs
: NULL
);
712 aaline
->driver_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
713 aaline
->num_samplers
,
714 aaline
->state
.sampler
);
716 aaline
->driver_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
717 aaline
->num_samplers
,
718 aaline
->state
.sampler_views
);
720 /* restore original rasterizer state */
721 if (draw
->rast_handle
) {
722 pipe
->bind_rasterizer_state(pipe
, draw
->rast_handle
);
725 draw
->suspend_flushing
= FALSE
;
727 draw_remove_extra_vertex_attribs(draw
);
732 aaline_reset_stipple_counter(struct draw_stage
*stage
)
734 stage
->next
->reset_stipple_counter( stage
->next
);
739 aaline_destroy(struct draw_stage
*stage
)
741 struct aaline_stage
*aaline
= aaline_stage(stage
);
742 struct pipe_context
*pipe
= stage
->draw
->pipe
;
745 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
746 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[i
], NULL
);
749 if (aaline
->sampler_cso
)
750 pipe
->delete_sampler_state(pipe
, aaline
->sampler_cso
);
753 pipe_resource_reference(&aaline
->texture
, NULL
);
755 if (aaline
->sampler_view
) {
756 pipe_sampler_view_reference(&aaline
->sampler_view
, NULL
);
759 draw_free_temp_verts( stage
);
761 /* restore the old entry points */
762 pipe
->create_fs_state
= aaline
->driver_create_fs_state
;
763 pipe
->bind_fs_state
= aaline
->driver_bind_fs_state
;
764 pipe
->delete_fs_state
= aaline
->driver_delete_fs_state
;
766 pipe
->bind_sampler_states
= aaline
->driver_bind_sampler_states
;
767 pipe
->set_sampler_views
= aaline
->driver_set_sampler_views
;
773 static struct aaline_stage
*
774 draw_aaline_stage(struct draw_context
*draw
)
776 struct aaline_stage
*aaline
= CALLOC_STRUCT(aaline_stage
);
780 aaline
->stage
.draw
= draw
;
781 aaline
->stage
.name
= "aaline";
782 aaline
->stage
.next
= NULL
;
783 aaline
->stage
.point
= draw_pipe_passthrough_point
;
784 aaline
->stage
.line
= aaline_first_line
;
785 aaline
->stage
.tri
= draw_pipe_passthrough_tri
;
786 aaline
->stage
.flush
= aaline_flush
;
787 aaline
->stage
.reset_stipple_counter
= aaline_reset_stipple_counter
;
788 aaline
->stage
.destroy
= aaline_destroy
;
790 if (!draw_alloc_temp_verts( &aaline
->stage
, 8 ))
797 aaline
->stage
.destroy(&aaline
->stage
);
803 static struct aaline_stage
*
804 aaline_stage_from_pipe(struct pipe_context
*pipe
)
806 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
809 return aaline_stage(draw
->pipeline
.aaline
);
817 * This function overrides the driver's create_fs_state() function and
818 * will typically be called by the state tracker.
821 aaline_create_fs_state(struct pipe_context
*pipe
,
822 const struct pipe_shader_state
*fs
)
824 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
825 struct aaline_fragment_shader
*aafs
= NULL
;
830 aafs
= CALLOC_STRUCT(aaline_fragment_shader
);
835 aafs
->state
.tokens
= tgsi_dup_tokens(fs
->tokens
);
838 aafs
->driver_fs
= aaline
->driver_create_fs_state(pipe
, fs
);
845 aaline_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
847 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
848 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
850 if (aaline
== NULL
) {
857 aaline
->driver_bind_fs_state(pipe
, (aafs
? aafs
->driver_fs
: NULL
));
862 aaline_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
864 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
865 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
871 if (aaline
!= NULL
) {
873 aaline
->driver_delete_fs_state(pipe
, aafs
->driver_fs
);
876 aaline
->driver_delete_fs_state(pipe
, aafs
->aaline_fs
);
879 FREE((void*)aafs
->state
.tokens
);
885 aaline_bind_sampler_states(struct pipe_context
*pipe
, unsigned shader
,
886 unsigned start
, unsigned num
, void **sampler
)
888 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
892 if (aaline
== NULL
) {
896 if (shader
== PIPE_SHADER_FRAGMENT
) {
898 memcpy(aaline
->state
.sampler
, sampler
, num
* sizeof(void *));
899 aaline
->num_samplers
= num
;
903 aaline
->driver_bind_sampler_states(pipe
, shader
, start
, num
, sampler
);
908 aaline_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
909 unsigned start
, unsigned num
,
910 struct pipe_sampler_view
**views
)
912 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
915 if (aaline
== NULL
) {
919 if (shader
== PIPE_SHADER_FRAGMENT
) {
921 for (i
= 0; i
< num
; i
++) {
922 pipe_sampler_view_reference(&aaline
->state
.sampler_views
[start
+ i
],
925 aaline
->num_sampler_views
= num
;
929 aaline
->driver_set_sampler_views(pipe
, shader
, start
, num
, views
);
934 draw_aaline_prepare_outputs(struct draw_context
*draw
,
935 struct draw_stage
*stage
)
937 struct aaline_stage
*aaline
= aaline_stage(stage
);
938 const struct pipe_rasterizer_state
*rast
= draw
->rasterizer
;
940 /* update vertex attrib info */
941 aaline
->pos_slot
= draw_current_shader_position_output(draw
);;
943 if (!rast
->line_smooth
)
946 /* allocate the extra post-transformed vertex attribute */
947 aaline
->tex_slot
= draw_alloc_extra_vertex_attrib(draw
,
948 TGSI_SEMANTIC_GENERIC
,
949 aaline
->fs
->generic_attrib
);
953 * Called by drivers that want to install this AA line prim stage
954 * into the draw module's pipeline. This will not be used if the
955 * hardware has native support for AA lines.
958 draw_install_aaline_stage(struct draw_context
*draw
, struct pipe_context
*pipe
)
960 struct aaline_stage
*aaline
;
962 pipe
->draw
= (void *) draw
;
965 * Create / install AA line drawing / prim stage
967 aaline
= draw_aaline_stage( draw
);
971 /* save original driver functions */
972 aaline
->driver_create_fs_state
= pipe
->create_fs_state
;
973 aaline
->driver_bind_fs_state
= pipe
->bind_fs_state
;
974 aaline
->driver_delete_fs_state
= pipe
->delete_fs_state
;
976 aaline
->driver_bind_sampler_states
= pipe
->bind_sampler_states
;
977 aaline
->driver_set_sampler_views
= pipe
->set_sampler_views
;
979 /* create special texture, sampler state */
980 if (!aaline_create_texture(aaline
))
983 if (!aaline_create_sampler(aaline
))
986 /* override the driver's functions */
987 pipe
->create_fs_state
= aaline_create_fs_state
;
988 pipe
->bind_fs_state
= aaline_bind_fs_state
;
989 pipe
->delete_fs_state
= aaline_delete_fs_state
;
991 pipe
->bind_sampler_states
= aaline_bind_sampler_states
;
992 pipe
->set_sampler_views
= aaline_set_sampler_views
;
994 /* Install once everything is known to be OK:
996 draw
->pipeline
.aaline
= &aaline
->stage
;
1002 aaline
->stage
.destroy( &aaline
->stage
);