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"
39 #include "util/u_format.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
43 #include "tgsi/tgsi_transform.h"
44 #include "tgsi/tgsi_dump.h"
46 #include "draw_context.h"
47 #include "draw_private.h"
48 #include "draw_pipe.h"
52 * Max texture level for the alpha texture used for antialiasing
54 #define MAX_TEXTURE_LEVEL 5 /* 32 x 32 */
58 * Subclass of pipe_shader_state to carry extra fragment shader info.
60 struct aaline_fragment_shader
62 struct pipe_shader_state state
;
66 int generic_attrib
; /**< texcoord/generic used for texture */
71 * Subclass of draw_stage
75 struct draw_stage stage
;
77 float half_line_width
;
79 /** For AA lines, this is the vertex attrib slot for the new texcoords */
81 /** position, not necessarily output zero */
85 struct pipe_texture
*texture
;
91 * Currently bound state
93 struct aaline_fragment_shader
*fs
;
95 void *sampler
[PIPE_MAX_SAMPLERS
];
96 struct pipe_texture
*texture
[PIPE_MAX_SAMPLERS
];
100 * Driver interface/override functions
102 void * (*driver_create_fs_state
)(struct pipe_context
*,
103 const struct pipe_shader_state
*);
104 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
105 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
107 void (*driver_bind_sampler_states
)(struct pipe_context
*, unsigned,
109 void (*driver_set_sampler_textures
)(struct pipe_context
*, unsigned,
110 struct pipe_texture
**);
112 struct pipe_context
*pipe
;
118 * Subclass of tgsi_transform_context, used for transforming the
119 * user's fragment shader to add the special AA instructions.
121 struct aa_transform_context
{
122 struct tgsi_transform_context base
;
123 uint tempsUsed
; /**< bitmask */
124 int colorOutput
; /**< which output is the primary color */
125 uint samplersUsed
; /**< bitfield of samplers used */
126 int freeSampler
; /** an available sampler for the pstipple */
127 int maxInput
, maxGeneric
; /**< max input index found */
128 int colorTemp
, texTemp
; /**< temp registers */
129 boolean firstInstruction
;
134 * TGSI declaration transform callback.
135 * Look for a free sampler, a free input attrib, and two free temp regs.
138 aa_transform_decl(struct tgsi_transform_context
*ctx
,
139 struct tgsi_full_declaration
*decl
)
141 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
143 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
144 decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
145 decl
->Semantic
.Index
== 0) {
146 aactx
->colorOutput
= decl
->Range
.First
;
148 else if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
150 for (i
= decl
->Range
.First
;
151 i
<= decl
->Range
.Last
; i
++) {
152 aactx
->samplersUsed
|= 1 << i
;
155 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
156 if ((int) decl
->Range
.Last
> aactx
->maxInput
)
157 aactx
->maxInput
= decl
->Range
.Last
;
158 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
&&
159 (int) decl
->Semantic
.Index
> aactx
->maxGeneric
) {
160 aactx
->maxGeneric
= decl
->Semantic
.Index
;
163 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
165 for (i
= decl
->Range
.First
;
166 i
<= decl
->Range
.Last
; i
++) {
167 aactx
->tempsUsed
|= (1 << i
);
171 ctx
->emit_declaration(ctx
, decl
);
176 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
179 free_bit(uint bitfield
)
182 for (i
= 0; i
< 32; i
++) {
183 if ((bitfield
& (1 << i
)) == 0)
191 * TGSI instruction transform callback.
192 * Replace writes to result.color w/ a temp reg.
193 * Upon END instruction, insert texture sampling code for antialiasing.
196 aa_transform_inst(struct tgsi_transform_context
*ctx
,
197 struct tgsi_full_instruction
*inst
)
199 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
201 if (aactx
->firstInstruction
) {
202 /* emit our new declarations before the first instruction */
204 struct tgsi_full_declaration decl
;
207 /* find free sampler */
208 aactx
->freeSampler
= free_bit(aactx
->samplersUsed
);
209 if (aactx
->freeSampler
>= PIPE_MAX_SAMPLERS
)
210 aactx
->freeSampler
= PIPE_MAX_SAMPLERS
- 1;
212 /* find two free temp regs */
213 for (i
= 0; i
< 32; i
++) {
214 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
215 /* found a free temp */
216 if (aactx
->colorTemp
< 0)
217 aactx
->colorTemp
= i
;
218 else if (aactx
->texTemp
< 0)
224 assert(aactx
->colorTemp
>= 0);
225 assert(aactx
->texTemp
>= 0);
227 /* declare new generic input/texcoord */
228 decl
= tgsi_default_full_declaration();
229 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
230 /* XXX this could be linear... */
231 decl
.Declaration
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
232 decl
.Declaration
.Semantic
= 1;
233 decl
.Semantic
.Name
= TGSI_SEMANTIC_GENERIC
;
234 decl
.Semantic
.Index
= aactx
->maxGeneric
+ 1;
236 decl
.Range
.Last
= aactx
->maxInput
+ 1;
237 ctx
->emit_declaration(ctx
, &decl
);
239 /* declare new sampler */
240 decl
= tgsi_default_full_declaration();
241 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
243 decl
.Range
.Last
= aactx
->freeSampler
;
244 ctx
->emit_declaration(ctx
, &decl
);
246 /* declare new temp regs */
247 decl
= tgsi_default_full_declaration();
248 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
250 decl
.Range
.Last
= aactx
->texTemp
;
251 ctx
->emit_declaration(ctx
, &decl
);
253 decl
= tgsi_default_full_declaration();
254 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
256 decl
.Range
.Last
= aactx
->colorTemp
;
257 ctx
->emit_declaration(ctx
, &decl
);
259 aactx
->firstInstruction
= FALSE
;
262 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
&&
263 aactx
->colorOutput
!= -1) {
264 struct tgsi_full_instruction newInst
;
267 newInst
= tgsi_default_full_instruction();
268 newInst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
269 newInst
.Instruction
.NumDstRegs
= 1;
270 newInst
.Dst
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
271 newInst
.Dst
[0].Register
.Index
= aactx
->texTemp
;
272 newInst
.Instruction
.NumSrcRegs
= 2;
273 newInst
.Instruction
.Texture
= TRUE
;
274 newInst
.Texture
.Texture
= TGSI_TEXTURE_2D
;
275 newInst
.Src
[0].Register
.File
= TGSI_FILE_INPUT
;
276 newInst
.Src
[0].Register
.Index
= aactx
->maxInput
+ 1;
277 newInst
.Src
[1].Register
.File
= TGSI_FILE_SAMPLER
;
278 newInst
.Src
[1].Register
.Index
= aactx
->freeSampler
;
280 ctx
->emit_instruction(ctx
, &newInst
);
283 newInst
= tgsi_default_full_instruction();
284 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
285 newInst
.Instruction
.NumDstRegs
= 1;
286 newInst
.Dst
[0].Register
.File
= TGSI_FILE_OUTPUT
;
287 newInst
.Dst
[0].Register
.Index
= aactx
->colorOutput
;
288 newInst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_XYZ
;
289 newInst
.Instruction
.NumSrcRegs
= 1;
290 newInst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
291 newInst
.Src
[0].Register
.Index
= aactx
->colorTemp
;
292 ctx
->emit_instruction(ctx
, &newInst
);
295 newInst
= tgsi_default_full_instruction();
296 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
297 newInst
.Instruction
.NumDstRegs
= 1;
298 newInst
.Dst
[0].Register
.File
= TGSI_FILE_OUTPUT
;
299 newInst
.Dst
[0].Register
.Index
= aactx
->colorOutput
;
300 newInst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_W
;
301 newInst
.Instruction
.NumSrcRegs
= 2;
302 newInst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
303 newInst
.Src
[0].Register
.Index
= aactx
->colorTemp
;
304 newInst
.Src
[1].Register
.File
= TGSI_FILE_TEMPORARY
;
305 newInst
.Src
[1].Register
.Index
= aactx
->texTemp
;
306 ctx
->emit_instruction(ctx
, &newInst
);
309 newInst
= tgsi_default_full_instruction();
310 newInst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
311 newInst
.Instruction
.NumDstRegs
= 0;
312 newInst
.Instruction
.NumSrcRegs
= 0;
313 ctx
->emit_instruction(ctx
, &newInst
);
316 /* Not an END instruction.
317 * Look for writes to result.color and replace with colorTemp reg.
321 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
322 struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
323 if (dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
324 dst
->Register
.Index
== aactx
->colorOutput
) {
325 dst
->Register
.File
= TGSI_FILE_TEMPORARY
;
326 dst
->Register
.Index
= aactx
->colorTemp
;
330 ctx
->emit_instruction(ctx
, inst
);
336 * Generate the frag shader we'll use for drawing AA lines.
337 * This will be the user's shader plus some texture/modulate instructions.
340 generate_aaline_fs(struct aaline_stage
*aaline
)
342 const struct pipe_shader_state
*orig_fs
= &aaline
->fs
->state
;
343 struct pipe_shader_state aaline_fs
;
344 struct aa_transform_context transform
;
348 aaline_fs
= *orig_fs
; /* copy to init */
349 aaline_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
350 if (aaline_fs
.tokens
== NULL
)
353 memset(&transform
, 0, sizeof(transform
));
354 transform
.colorOutput
= -1;
355 transform
.maxInput
= -1;
356 transform
.maxGeneric
= -1;
357 transform
.colorTemp
= -1;
358 transform
.texTemp
= -1;
359 transform
.firstInstruction
= TRUE
;
360 transform
.base
.transform_instruction
= aa_transform_inst
;
361 transform
.base
.transform_declaration
= aa_transform_decl
;
363 tgsi_transform_shader(orig_fs
->tokens
,
364 (struct tgsi_token
*) aaline_fs
.tokens
,
365 MAX
, &transform
.base
);
368 tgsi_dump(orig_fs
->tokens
, 0);
369 tgsi_dump(aaline_fs
.tokens
, 0);
372 aaline
->fs
->sampler_unit
= transform
.freeSampler
;
374 aaline
->fs
->aaline_fs
375 = aaline
->driver_create_fs_state(aaline
->pipe
, &aaline_fs
);
376 if (aaline
->fs
->aaline_fs
== NULL
)
379 aaline
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
380 FREE((void *)aaline_fs
.tokens
);
384 FREE((void *)aaline_fs
.tokens
);
390 * Create the texture map we'll use for antialiasing the lines.
393 aaline_create_texture(struct aaline_stage
*aaline
)
395 struct pipe_context
*pipe
= aaline
->pipe
;
396 struct pipe_screen
*screen
= pipe
->screen
;
397 struct pipe_texture texTemp
;
400 memset(&texTemp
, 0, sizeof(texTemp
));
401 texTemp
.target
= PIPE_TEXTURE_2D
;
402 texTemp
.format
= PIPE_FORMAT_A8_UNORM
; /* XXX verify supported by driver! */
403 texTemp
.last_level
= MAX_TEXTURE_LEVEL
;
404 texTemp
.width0
= 1 << MAX_TEXTURE_LEVEL
;
405 texTemp
.height0
= 1 << MAX_TEXTURE_LEVEL
;
407 util_format_get_block(texTemp
.format
, &texTemp
.block
);
409 aaline
->texture
= screen
->texture_create(screen
, &texTemp
);
410 if (!aaline
->texture
)
413 /* Fill in mipmap images.
414 * Basically each level is solid opaque, except for the outermost
415 * texels which are zero. Special case the 1x1 and 2x2 levels.
417 for (level
= 0; level
<= MAX_TEXTURE_LEVEL
; level
++) {
418 struct pipe_transfer
*transfer
;
419 const uint size
= u_minify(aaline
->texture
->width0
, level
);
423 assert(aaline
->texture
->width0
== aaline
->texture
->height0
);
425 /* This texture is new, no need to flush.
427 transfer
= screen
->get_tex_transfer(screen
, aaline
->texture
, 0, level
, 0,
428 PIPE_TRANSFER_WRITE
, 0, 0, size
, size
);
429 data
= screen
->transfer_map(screen
, transfer
);
433 for (i
= 0; i
< size
; i
++) {
434 for (j
= 0; j
< size
; j
++) {
439 else if (size
== 2) {
440 d
= 200; /* tuneable */
442 else if (i
== 0 || j
== 0 || i
== size
- 1 || j
== size
- 1) {
448 data
[i
* transfer
->stride
+ j
] = d
;
453 screen
->transfer_unmap(screen
, transfer
);
454 screen
->tex_transfer_destroy(transfer
);
461 * Create the sampler CSO that'll be used for antialiasing.
462 * By using a mipmapped texture, we don't have to generate a different
463 * texture image for each line size.
466 aaline_create_sampler(struct aaline_stage
*aaline
)
468 struct pipe_sampler_state sampler
;
469 struct pipe_context
*pipe
= aaline
->pipe
;
471 memset(&sampler
, 0, sizeof(sampler
));
472 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
473 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
474 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
475 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
476 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
477 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
478 sampler
.normalized_coords
= 1;
479 sampler
.min_lod
= 0.0f
;
480 sampler
.max_lod
= MAX_TEXTURE_LEVEL
;
482 aaline
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
483 if (aaline
->sampler_cso
== NULL
)
491 * When we're about to draw our first AA line in a batch, this function is
492 * called to tell the driver to bind our modified fragment shader.
495 bind_aaline_fragment_shader(struct aaline_stage
*aaline
)
497 struct draw_context
*draw
= aaline
->stage
.draw
;
499 if (!aaline
->fs
->aaline_fs
&&
500 !generate_aaline_fs(aaline
))
503 draw
->suspend_flushing
= TRUE
;
504 aaline
->driver_bind_fs_state(aaline
->pipe
, aaline
->fs
->aaline_fs
);
505 draw
->suspend_flushing
= FALSE
;
512 static INLINE
struct aaline_stage
*
513 aaline_stage( struct draw_stage
*stage
)
515 return (struct aaline_stage
*) stage
;
520 * Draw a wide line by drawing a quad, using geometry which will
521 * fullfill GL's antialiased line requirements.
524 aaline_line(struct draw_stage
*stage
, struct prim_header
*header
)
526 const struct aaline_stage
*aaline
= aaline_stage(stage
);
527 const float half_width
= aaline
->half_line_width
;
528 struct prim_header tri
;
529 struct vertex_header
*v
[8];
530 uint texPos
= aaline
->tex_slot
;
531 uint posPos
= aaline
->pos_slot
;
533 float dx
= header
->v
[1]->data
[posPos
][0] - header
->v
[0]->data
[posPos
][0];
534 float dy
= header
->v
[1]->data
[posPos
][1] - header
->v
[0]->data
[posPos
][1];
535 double a
= atan2(dy
, dx
);
536 float c_a
= (float) cos(a
), s_a
= (float) sin(a
);
539 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
540 dx
= 0.5F
* half_width
;
543 /* allocate/dup new verts */
544 for (i
= 0; i
< 8; i
++) {
545 v
[i
] = dup_vert(stage
, header
->v
[i
/4], i
);
549 * Quad strip for line from v0 to v1 (*=endpoints):
552 * +---+---------------------+---+
556 * +---+---------------------+---+
561 pos
= v
[0]->data
[posPos
];
562 pos
[0] += (-dx
* c_a
- dy
* s_a
);
563 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
565 pos
= v
[1]->data
[posPos
];
566 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
567 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
569 pos
= v
[2]->data
[posPos
];
570 pos
[0] += ( dx
* c_a
- dy
* s_a
);
571 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
573 pos
= v
[3]->data
[posPos
];
574 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
575 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
577 pos
= v
[4]->data
[posPos
];
578 pos
[0] += (-dx
* c_a
- dy
* s_a
);
579 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
581 pos
= v
[5]->data
[posPos
];
582 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
583 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
585 pos
= v
[6]->data
[posPos
];
586 pos
[0] += ( dx
* c_a
- dy
* s_a
);
587 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
589 pos
= v
[7]->data
[posPos
];
590 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
591 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
594 tex
= v
[0]->data
[texPos
];
595 ASSIGN_4V(tex
, 0, 0, 0, 1);
597 tex
= v
[1]->data
[texPos
];
598 ASSIGN_4V(tex
, 0, 1, 0, 1);
600 tex
= v
[2]->data
[texPos
];
601 ASSIGN_4V(tex
, .5, 0, 0, 1);
603 tex
= v
[3]->data
[texPos
];
604 ASSIGN_4V(tex
, .5, 1, 0, 1);
606 tex
= v
[4]->data
[texPos
];
607 ASSIGN_4V(tex
, .5, 0, 0, 1);
609 tex
= v
[5]->data
[texPos
];
610 ASSIGN_4V(tex
, .5, 1, 0, 1);
612 tex
= v
[6]->data
[texPos
];
613 ASSIGN_4V(tex
, 1, 0, 0, 1);
615 tex
= v
[7]->data
[texPos
];
616 ASSIGN_4V(tex
, 1, 1, 0, 1);
618 /* emit 6 tris for the quad strip */
619 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
620 stage
->next
->tri( stage
->next
, &tri
);
622 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
623 stage
->next
->tri( stage
->next
, &tri
);
625 tri
.v
[0] = v
[4]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[2];
626 stage
->next
->tri( stage
->next
, &tri
);
628 tri
.v
[0] = v
[5]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[4];
629 stage
->next
->tri( stage
->next
, &tri
);
631 tri
.v
[0] = v
[6]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[4];
632 stage
->next
->tri( stage
->next
, &tri
);
634 tri
.v
[0] = v
[7]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[6];
635 stage
->next
->tri( stage
->next
, &tri
);
640 aaline_first_line(struct draw_stage
*stage
, struct prim_header
*header
)
642 auto struct aaline_stage
*aaline
= aaline_stage(stage
);
643 struct draw_context
*draw
= stage
->draw
;
644 struct pipe_context
*pipe
= aaline
->pipe
;
647 assert(draw
->rasterizer
->line_smooth
);
649 if (draw
->rasterizer
->line_width
<= 3.0)
650 aaline
->half_line_width
= 1.5f
;
652 aaline
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
655 * Bind (generate) our fragprog, sampler and texture
657 if (!bind_aaline_fragment_shader(aaline
)) {
658 stage
->line
= draw_pipe_passthrough_line
;
659 stage
->line(stage
, header
);
663 /* update vertex attrib info */
664 aaline
->tex_slot
= draw
->vs
.num_vs_outputs
;
665 aaline
->pos_slot
= draw
->vs
.position_output
;
667 /* advertise the extra post-transformed vertex attribute */
668 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
669 draw
->extra_vp_outputs
.semantic_index
= aaline
->fs
->generic_attrib
;
670 draw
->extra_vp_outputs
.slot
= aaline
->tex_slot
;
672 /* how many samplers? */
673 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
674 num_samplers
= MAX2(aaline
->num_textures
, aaline
->num_samplers
);
675 num_samplers
= MAX2(num_samplers
, aaline
->fs
->sampler_unit
+ 1);
677 aaline
->state
.sampler
[aaline
->fs
->sampler_unit
] = aaline
->sampler_cso
;
678 pipe_texture_reference(&aaline
->state
.texture
[aaline
->fs
->sampler_unit
],
681 draw
->suspend_flushing
= TRUE
;
682 aaline
->driver_bind_sampler_states(pipe
, num_samplers
, aaline
->state
.sampler
);
683 aaline
->driver_set_sampler_textures(pipe
, num_samplers
, aaline
->state
.texture
);
684 draw
->suspend_flushing
= FALSE
;
686 /* now really draw first line */
687 stage
->line
= aaline_line
;
688 stage
->line(stage
, header
);
693 aaline_flush(struct draw_stage
*stage
, unsigned flags
)
695 struct draw_context
*draw
= stage
->draw
;
696 struct aaline_stage
*aaline
= aaline_stage(stage
);
697 struct pipe_context
*pipe
= aaline
->pipe
;
699 stage
->line
= aaline_first_line
;
700 stage
->next
->flush( stage
->next
, flags
);
702 /* restore original frag shader, texture, sampler state */
703 draw
->suspend_flushing
= TRUE
;
704 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->driver_fs
);
705 aaline
->driver_bind_sampler_states(pipe
, aaline
->num_samplers
,
706 aaline
->state
.sampler
);
707 aaline
->driver_set_sampler_textures(pipe
, aaline
->num_textures
,
708 aaline
->state
.texture
);
709 draw
->suspend_flushing
= FALSE
;
711 draw
->extra_vp_outputs
.slot
= 0;
716 aaline_reset_stipple_counter(struct draw_stage
*stage
)
718 stage
->next
->reset_stipple_counter( stage
->next
);
723 aaline_destroy(struct draw_stage
*stage
)
725 struct aaline_stage
*aaline
= aaline_stage(stage
);
728 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
729 pipe_texture_reference(&aaline
->state
.texture
[i
], NULL
);
732 if (aaline
->sampler_cso
)
733 aaline
->pipe
->delete_sampler_state(aaline
->pipe
, aaline
->sampler_cso
);
736 pipe_texture_reference(&aaline
->texture
, NULL
);
738 draw_free_temp_verts( stage
);
744 static struct aaline_stage
*
745 draw_aaline_stage(struct draw_context
*draw
)
747 struct aaline_stage
*aaline
= CALLOC_STRUCT(aaline_stage
);
751 if (!draw_alloc_temp_verts( &aaline
->stage
, 8 ))
754 aaline
->stage
.draw
= draw
;
755 aaline
->stage
.name
= "aaline";
756 aaline
->stage
.next
= NULL
;
757 aaline
->stage
.point
= draw_pipe_passthrough_point
;
758 aaline
->stage
.line
= aaline_first_line
;
759 aaline
->stage
.tri
= draw_pipe_passthrough_tri
;
760 aaline
->stage
.flush
= aaline_flush
;
761 aaline
->stage
.reset_stipple_counter
= aaline_reset_stipple_counter
;
762 aaline
->stage
.destroy
= aaline_destroy
;
768 aaline_destroy(&aaline
->stage
);
774 static struct aaline_stage
*
775 aaline_stage_from_pipe(struct pipe_context
*pipe
)
777 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
778 return aaline_stage(draw
->pipeline
.aaline
);
783 * This function overrides the driver's create_fs_state() function and
784 * will typically be called by the state tracker.
787 aaline_create_fs_state(struct pipe_context
*pipe
,
788 const struct pipe_shader_state
*fs
)
790 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
791 struct aaline_fragment_shader
*aafs
= CALLOC_STRUCT(aaline_fragment_shader
);
798 aafs
->driver_fs
= aaline
->driver_create_fs_state(aaline
->pipe
, fs
);
805 aaline_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
807 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
808 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
813 aaline
->driver_bind_fs_state(aaline
->pipe
,
814 (aafs
? aafs
->driver_fs
: NULL
));
819 aaline_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
821 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
822 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
824 aaline
->driver_delete_fs_state(aaline
->pipe
, aafs
->driver_fs
);
827 aaline
->driver_delete_fs_state(aaline
->pipe
, aafs
->aaline_fs
);
834 aaline_bind_sampler_states(struct pipe_context
*pipe
,
835 unsigned num
, void **sampler
)
837 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
840 memcpy(aaline
->state
.sampler
, sampler
, num
* sizeof(void *));
841 aaline
->num_samplers
= num
;
844 aaline
->driver_bind_sampler_states(aaline
->pipe
, num
, sampler
);
849 aaline_set_sampler_textures(struct pipe_context
*pipe
,
850 unsigned num
, struct pipe_texture
**texture
)
852 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
856 for (i
= 0; i
< num
; i
++) {
857 pipe_texture_reference(&aaline
->state
.texture
[i
], texture
[i
]);
859 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
860 pipe_texture_reference(&aaline
->state
.texture
[i
], NULL
);
862 aaline
->num_textures
= num
;
865 aaline
->driver_set_sampler_textures(aaline
->pipe
, num
, texture
);
870 * Called by drivers that want to install this AA line prim stage
871 * into the draw module's pipeline. This will not be used if the
872 * hardware has native support for AA lines.
875 draw_install_aaline_stage(struct draw_context
*draw
, struct pipe_context
*pipe
)
877 struct aaline_stage
*aaline
;
879 pipe
->draw
= (void *) draw
;
882 * Create / install AA line drawing / prim stage
884 aaline
= draw_aaline_stage( draw
);
890 /* create special texture, sampler state */
891 if (!aaline_create_texture(aaline
))
894 if (!aaline_create_sampler(aaline
))
897 /* save original driver functions */
898 aaline
->driver_create_fs_state
= pipe
->create_fs_state
;
899 aaline
->driver_bind_fs_state
= pipe
->bind_fs_state
;
900 aaline
->driver_delete_fs_state
= pipe
->delete_fs_state
;
902 aaline
->driver_bind_sampler_states
= pipe
->bind_fragment_sampler_states
;
903 aaline
->driver_set_sampler_textures
= pipe
->set_fragment_sampler_textures
;
905 /* override the driver's functions */
906 pipe
->create_fs_state
= aaline_create_fs_state
;
907 pipe
->bind_fs_state
= aaline_bind_fs_state
;
908 pipe
->delete_fs_state
= aaline_delete_fs_state
;
910 pipe
->bind_fragment_sampler_states
= aaline_bind_sampler_states
;
911 pipe
->set_fragment_sampler_textures
= aaline_set_sampler_textures
;
913 /* Install once everything is known to be OK:
915 draw
->pipeline
.aaline
= &aaline
->stage
;
921 aaline
->stage
.destroy( &aaline
->stage
);