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_util.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_shader_tokens.h"
41 #include "tgsi/util/tgsi_transform.h"
42 #include "tgsi/util/tgsi_dump.h"
44 #include "draw_context.h"
45 #include "draw_private.h"
46 #include "draw_pipe.h"
50 * Max texture level for the alpha texture used for antialiasing
52 #define MAX_TEXTURE_LEVEL 5 /* 32 x 32 */
56 * Subclass of pipe_shader_state to carry extra fragment shader info.
58 struct aaline_fragment_shader
60 struct pipe_shader_state state
;
63 void *aapoint_fs
; /* not yet */
64 void *sprite_fs
; /* not yet */
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 */
83 struct pipe_texture
*texture
;
89 * Currently bound state
91 struct aaline_fragment_shader
*fs
;
93 void *sampler
[PIPE_MAX_SAMPLERS
];
94 struct pipe_texture
*texture
[PIPE_MAX_SAMPLERS
];
98 * Driver interface/override functions
100 void * (*driver_create_fs_state
)(struct pipe_context
*,
101 const struct pipe_shader_state
*);
102 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
103 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
105 void (*driver_bind_sampler_states
)(struct pipe_context
*, unsigned,
107 void (*driver_set_sampler_textures
)(struct pipe_context
*, unsigned,
108 struct pipe_texture
**);
110 struct pipe_context
*pipe
;
116 * Subclass of tgsi_transform_context, used for transforming the
117 * user's fragment shader to add the special AA instructions.
119 struct aa_transform_context
{
120 struct tgsi_transform_context base
;
121 uint tempsUsed
; /**< bitmask */
122 int colorOutput
; /**< which output is the primary color */
123 uint samplersUsed
; /**< bitfield of samplers used */
124 int freeSampler
; /** an available sampler for the pstipple */
125 int maxInput
, maxGeneric
; /**< max input index found */
126 int colorTemp
, texTemp
; /**< temp registers */
127 boolean firstInstruction
;
132 * TGSI declaration transform callback.
133 * Look for a free sampler, a free input attrib, and two free temp regs.
136 aa_transform_decl(struct tgsi_transform_context
*ctx
,
137 struct tgsi_full_declaration
*decl
)
139 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
141 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
142 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
&&
143 decl
->Semantic
.SemanticIndex
== 0) {
144 aactx
->colorOutput
= decl
->u
.DeclarationRange
.First
;
146 else if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
148 for (i
= decl
->u
.DeclarationRange
.First
;
149 i
<= decl
->u
.DeclarationRange
.Last
; i
++) {
150 aactx
->samplersUsed
|= 1 << i
;
153 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
154 if ((int) decl
->u
.DeclarationRange
.Last
> aactx
->maxInput
)
155 aactx
->maxInput
= decl
->u
.DeclarationRange
.Last
;
156 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
&&
157 (int) decl
->Semantic
.SemanticIndex
> aactx
->maxGeneric
) {
158 aactx
->maxGeneric
= decl
->Semantic
.SemanticIndex
;
161 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
163 for (i
= decl
->u
.DeclarationRange
.First
;
164 i
<= decl
->u
.DeclarationRange
.Last
; i
++) {
165 aactx
->tempsUsed
|= (1 << i
);
169 ctx
->emit_declaration(ctx
, decl
);
174 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
177 free_bit(uint bitfield
)
180 for (i
= 0; i
< 32; i
++) {
181 if ((bitfield
& (1 << i
)) == 0)
189 * TGSI instruction transform callback.
190 * Replace writes to result.color w/ a temp reg.
191 * Upon END instruction, insert texture sampling code for antialiasing.
194 aa_transform_inst(struct tgsi_transform_context
*ctx
,
195 struct tgsi_full_instruction
*inst
)
197 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
199 if (aactx
->firstInstruction
) {
200 /* emit our new declarations before the first instruction */
202 struct tgsi_full_declaration decl
;
205 /* find free sampler */
206 aactx
->freeSampler
= free_bit(aactx
->samplersUsed
);
207 if (aactx
->freeSampler
>= PIPE_MAX_SAMPLERS
)
208 aactx
->freeSampler
= PIPE_MAX_SAMPLERS
- 1;
210 /* find two free temp regs */
211 for (i
= 0; i
< 32; i
++) {
212 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
213 /* found a free temp */
214 if (aactx
->colorTemp
< 0)
215 aactx
->colorTemp
= i
;
216 else if (aactx
->texTemp
< 0)
222 assert(aactx
->colorTemp
>= 0);
223 assert(aactx
->texTemp
>= 0);
225 /* declare new generic input/texcoord */
226 decl
= tgsi_default_full_declaration();
227 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
228 decl
.Declaration
.Semantic
= 1;
229 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
230 decl
.Semantic
.SemanticIndex
= aactx
->maxGeneric
+ 1;
231 decl
.Declaration
.Interpolate
= 1;
232 /* XXX this could be linear... */
233 decl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
234 decl
.u
.DeclarationRange
.First
=
235 decl
.u
.DeclarationRange
.Last
= aactx
->maxInput
+ 1;
236 ctx
->emit_declaration(ctx
, &decl
);
238 /* declare new sampler */
239 decl
= tgsi_default_full_declaration();
240 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
241 decl
.u
.DeclarationRange
.First
=
242 decl
.u
.DeclarationRange
.Last
= aactx
->freeSampler
;
243 ctx
->emit_declaration(ctx
, &decl
);
245 /* declare new temp regs */
246 decl
= tgsi_default_full_declaration();
247 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
248 decl
.u
.DeclarationRange
.First
=
249 decl
.u
.DeclarationRange
.Last
= aactx
->texTemp
;
250 ctx
->emit_declaration(ctx
, &decl
);
252 decl
= tgsi_default_full_declaration();
253 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
254 decl
.u
.DeclarationRange
.First
=
255 decl
.u
.DeclarationRange
.Last
= aactx
->colorTemp
;
256 ctx
->emit_declaration(ctx
, &decl
);
258 aactx
->firstInstruction
= FALSE
;
261 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
&&
262 aactx
->colorOutput
!= -1) {
263 struct tgsi_full_instruction newInst
;
266 newInst
= tgsi_default_full_instruction();
267 newInst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
268 newInst
.Instruction
.NumDstRegs
= 1;
269 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
270 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->texTemp
;
271 newInst
.Instruction
.NumSrcRegs
= 2;
272 newInst
.InstructionExtTexture
.Texture
= TGSI_TEXTURE_2D
;
273 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
274 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->maxInput
+ 1;
275 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
276 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->freeSampler
;
278 ctx
->emit_instruction(ctx
, &newInst
);
281 newInst
= tgsi_default_full_instruction();
282 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
283 newInst
.Instruction
.NumDstRegs
= 1;
284 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
285 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
286 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
287 newInst
.Instruction
.NumSrcRegs
= 1;
288 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
289 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
290 ctx
->emit_instruction(ctx
, &newInst
);
293 newInst
= tgsi_default_full_instruction();
294 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
295 newInst
.Instruction
.NumDstRegs
= 1;
296 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
297 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
298 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
299 newInst
.Instruction
.NumSrcRegs
= 2;
300 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
301 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
302 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
303 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->texTemp
;
304 ctx
->emit_instruction(ctx
, &newInst
);
307 newInst
= tgsi_default_full_instruction();
308 newInst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
309 newInst
.Instruction
.NumDstRegs
= 0;
310 newInst
.Instruction
.NumSrcRegs
= 0;
311 ctx
->emit_instruction(ctx
, &newInst
);
314 /* Not an END instruction.
315 * Look for writes to result.color and replace with colorTemp reg.
319 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
320 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
321 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
&&
322 dst
->DstRegister
.Index
== aactx
->colorOutput
) {
323 dst
->DstRegister
.File
= TGSI_FILE_TEMPORARY
;
324 dst
->DstRegister
.Index
= aactx
->colorTemp
;
328 ctx
->emit_instruction(ctx
, inst
);
334 * Generate the frag shader we'll use for drawing AA lines.
335 * This will be the user's shader plus some texture/modulate instructions.
338 generate_aaline_fs(struct aaline_stage
*aaline
)
340 const struct pipe_shader_state
*orig_fs
= &aaline
->fs
->state
;
341 //struct draw_context *draw = aaline->stage.draw;
342 struct pipe_shader_state aaline_fs
;
343 struct aa_transform_context transform
;
347 aaline_fs
= *orig_fs
; /* copy to init */
348 aaline_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
350 memset(&transform
, 0, sizeof(transform
));
351 transform
.colorOutput
= -1;
352 transform
.maxInput
= -1;
353 transform
.maxGeneric
= -1;
354 transform
.colorTemp
= -1;
355 transform
.texTemp
= -1;
356 transform
.firstInstruction
= TRUE
;
357 transform
.base
.transform_instruction
= aa_transform_inst
;
358 transform
.base
.transform_declaration
= aa_transform_decl
;
360 tgsi_transform_shader(orig_fs
->tokens
,
361 (struct tgsi_token
*) aaline_fs
.tokens
,
362 MAX
, &transform
.base
);
365 tgsi_dump(orig_fs
->tokens
, 0);
366 tgsi_dump(aaline_fs
.tokens
, 0);
369 aaline
->fs
->sampler_unit
= transform
.freeSampler
;
371 aaline
->fs
->aaline_fs
372 = aaline
->driver_create_fs_state(aaline
->pipe
, &aaline_fs
);
374 aaline
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
379 * Create the texture map we'll use for antialiasing the lines.
382 aaline_create_texture(struct aaline_stage
*aaline
)
384 struct pipe_context
*pipe
= aaline
->pipe
;
385 struct pipe_screen
*screen
= pipe
->screen
;
386 struct pipe_texture texTemp
;
389 memset(&texTemp
, 0, sizeof(texTemp
));
390 texTemp
.target
= PIPE_TEXTURE_2D
;
391 texTemp
.format
= PIPE_FORMAT_U_A8
; /* XXX verify supported by driver! */
392 texTemp
.last_level
= MAX_TEXTURE_LEVEL
;
393 texTemp
.width
[0] = 1 << MAX_TEXTURE_LEVEL
;
394 texTemp
.height
[0] = 1 << MAX_TEXTURE_LEVEL
;
395 texTemp
.depth
[0] = 1;
398 aaline
->texture
= screen
->texture_create(screen
, &texTemp
);
400 /* Fill in mipmap images.
401 * Basically each level is solid opaque, except for the outermost
402 * texels which are zero. Special case the 1x1 and 2x2 levels.
404 for (level
= 0; level
<= MAX_TEXTURE_LEVEL
; level
++) {
405 struct pipe_surface
*surface
;
406 const uint size
= aaline
->texture
->width
[level
];
410 assert(aaline
->texture
->width
[level
] == aaline
->texture
->height
[level
]);
412 surface
= screen
->get_tex_surface(screen
, aaline
->texture
, 0, level
, 0);
413 data
= pipe_surface_map(surface
);
415 for (i
= 0; i
< size
; i
++) {
416 for (j
= 0; j
< size
; j
++) {
421 else if (size
== 2) {
422 d
= 200; /* tuneable */
424 else if (i
== 0 || j
== 0 || i
== size
- 1 || j
== size
- 1) {
430 data
[i
* surface
->pitch
+ j
] = d
;
435 pipe_surface_unmap(surface
);
436 pipe_surface_reference(&surface
, NULL
);
437 pipe
->texture_update(pipe
, aaline
->texture
, 0, (1 << level
));
443 * Create the sampler CSO that'll be used for antialiasing.
444 * By using a mipmapped texture, we don't have to generate a different
445 * texture image for each line size.
448 aaline_create_sampler(struct aaline_stage
*aaline
)
450 struct pipe_sampler_state sampler
;
451 struct pipe_context
*pipe
= aaline
->pipe
;
453 memset(&sampler
, 0, sizeof(sampler
));
454 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
455 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
456 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
457 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
458 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
459 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
460 sampler
.normalized_coords
= 1;
461 sampler
.min_lod
= 0.0f
;
462 sampler
.max_lod
= MAX_TEXTURE_LEVEL
;
464 aaline
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
469 * When we're about to draw our first AA line in a batch, this function is
470 * called to tell the driver to bind our modified fragment shader.
473 bind_aaline_fragment_shader(struct aaline_stage
*aaline
)
475 if (!aaline
->fs
->aaline_fs
) {
476 generate_aaline_fs(aaline
);
478 aaline
->driver_bind_fs_state(aaline
->pipe
, aaline
->fs
->aaline_fs
);
483 static INLINE
struct aaline_stage
*
484 aaline_stage( struct draw_stage
*stage
)
486 return (struct aaline_stage
*) stage
;
491 passthrough_point(struct draw_stage
*stage
, struct prim_header
*header
)
493 stage
->next
->point(stage
->next
, header
);
498 passthrough_tri(struct draw_stage
*stage
, struct prim_header
*header
)
500 stage
->next
->tri(stage
->next
, header
);
505 * Draw a wide line by drawing a quad, using geometry which will
506 * fullfill GL's antialiased line requirements.
509 aaline_line(struct draw_stage
*stage
, struct prim_header
*header
)
511 const struct aaline_stage
*aaline
= aaline_stage(stage
);
512 const float half_width
= aaline
->half_line_width
;
513 struct prim_header tri
;
514 struct vertex_header
*v
[8];
515 uint texPos
= aaline
->tex_slot
;
517 float dx
= header
->v
[1]->data
[0][0] - header
->v
[0]->data
[0][0];
518 float dy
= header
->v
[1]->data
[0][1] - header
->v
[0]->data
[0][1];
519 double a
= atan2(dy
, dx
);
520 float c_a
= (float) cos(a
), s_a
= (float) sin(a
);
523 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
524 dx
= 0.5F
* half_width
;
527 /* allocate/dup new verts */
528 for (i
= 0; i
< 8; i
++) {
529 v
[i
] = dup_vert(stage
, header
->v
[i
/4], i
);
533 * Quad strip for line from v0 to v1 (*=endpoints):
536 * +---+---------------------+---+
540 * +---+---------------------+---+
546 pos
[0] += (-dx
* c_a
- dy
* s_a
);
547 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
550 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
551 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
554 pos
[0] += ( dx
* c_a
- dy
* s_a
);
555 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
558 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
559 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
562 pos
[0] += (-dx
* c_a
- dy
* s_a
);
563 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
566 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
567 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
570 pos
[0] += ( dx
* c_a
- dy
* s_a
);
571 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
574 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
575 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
578 tex
= v
[0]->data
[texPos
];
579 ASSIGN_4V(tex
, 0, 0, 0, 1);
581 tex
= v
[1]->data
[texPos
];
582 ASSIGN_4V(tex
, 0, 1, 0, 1);
584 tex
= v
[2]->data
[texPos
];
585 ASSIGN_4V(tex
, .5, 0, 0, 1);
587 tex
= v
[3]->data
[texPos
];
588 ASSIGN_4V(tex
, .5, 1, 0, 1);
590 tex
= v
[4]->data
[texPos
];
591 ASSIGN_4V(tex
, .5, 0, 0, 1);
593 tex
= v
[5]->data
[texPos
];
594 ASSIGN_4V(tex
, .5, 1, 0, 1);
596 tex
= v
[6]->data
[texPos
];
597 ASSIGN_4V(tex
, 1, 0, 0, 1);
599 tex
= v
[7]->data
[texPos
];
600 ASSIGN_4V(tex
, 1, 1, 0, 1);
602 /* emit 6 tris for the quad strip */
603 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
604 stage
->next
->tri( stage
->next
, &tri
);
606 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
607 stage
->next
->tri( stage
->next
, &tri
);
609 tri
.v
[0] = v
[4]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[2];
610 stage
->next
->tri( stage
->next
, &tri
);
612 tri
.v
[0] = v
[5]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[4];
613 stage
->next
->tri( stage
->next
, &tri
);
615 tri
.v
[0] = v
[6]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[4];
616 stage
->next
->tri( stage
->next
, &tri
);
618 tri
.v
[0] = v
[7]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[6];
619 stage
->next
->tri( stage
->next
, &tri
);
624 aaline_first_line(struct draw_stage
*stage
, struct prim_header
*header
)
626 auto struct aaline_stage
*aaline
= aaline_stage(stage
);
627 struct draw_context
*draw
= stage
->draw
;
628 struct pipe_context
*pipe
= aaline
->pipe
;
631 assert(draw
->rasterizer
->line_smooth
);
633 if (draw
->rasterizer
->line_width
<= 3.0)
634 aaline
->half_line_width
= 1.5f
;
636 aaline
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
639 * Bind (generate) our fragprog, sampler and texture
641 bind_aaline_fragment_shader(aaline
);
643 /* update vertex attrib info */
644 aaline
->tex_slot
= draw
->num_vs_outputs
;
645 assert(aaline
->tex_slot
> 0); /* output[0] is vertex pos */
647 /* advertise the extra post-transformed vertex attribute */
648 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
649 draw
->extra_vp_outputs
.semantic_index
= aaline
->fs
->generic_attrib
;
650 draw
->extra_vp_outputs
.slot
= aaline
->tex_slot
;
652 /* how many samplers? */
653 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
654 num_samplers
= MAX2(aaline
->num_textures
, aaline
->num_samplers
);
655 num_samplers
= MAX2(num_samplers
, aaline
->fs
->sampler_unit
+ 1);
657 aaline
->state
.sampler
[aaline
->fs
->sampler_unit
] = aaline
->sampler_cso
;
658 pipe_texture_reference(&aaline
->state
.texture
[aaline
->fs
->sampler_unit
],
661 aaline
->driver_bind_sampler_states(pipe
, num_samplers
, aaline
->state
.sampler
);
662 aaline
->driver_set_sampler_textures(pipe
, num_samplers
, aaline
->state
.texture
);
664 /* now really draw first line */
665 stage
->line
= aaline_line
;
666 stage
->line(stage
, header
);
671 aaline_flush(struct draw_stage
*stage
, unsigned flags
)
673 struct draw_context
*draw
= stage
->draw
;
674 struct aaline_stage
*aaline
= aaline_stage(stage
);
675 struct pipe_context
*pipe
= aaline
->pipe
;
677 stage
->line
= aaline_first_line
;
678 stage
->next
->flush( stage
->next
, flags
);
680 /* restore original frag shader */
681 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->driver_fs
);
683 /* XXX restore original texture, sampler state */
684 aaline
->driver_bind_sampler_states(pipe
, aaline
->num_samplers
,
685 aaline
->state
.sampler
);
686 aaline
->driver_set_sampler_textures(pipe
, aaline
->num_textures
,
687 aaline
->state
.texture
);
689 draw
->extra_vp_outputs
.slot
= 0;
694 aaline_reset_stipple_counter(struct draw_stage
*stage
)
696 stage
->next
->reset_stipple_counter( stage
->next
);
701 aaline_destroy(struct draw_stage
*stage
)
703 struct aaline_stage
*aaline
= aaline_stage(stage
);
705 aaline
->pipe
->delete_sampler_state(aaline
->pipe
, aaline
->sampler_cso
);
707 pipe_texture_release(&aaline
->texture
);
709 draw_free_temp_verts( stage
);
715 static struct aaline_stage
*
716 draw_aaline_stage(struct draw_context
*draw
)
718 struct aaline_stage
*aaline
= CALLOC_STRUCT(aaline_stage
);
720 draw_alloc_temp_verts( &aaline
->stage
, 8 );
722 aaline
->stage
.draw
= draw
;
723 aaline
->stage
.next
= NULL
;
724 aaline
->stage
.point
= passthrough_point
;
725 aaline
->stage
.line
= aaline_first_line
;
726 aaline
->stage
.tri
= passthrough_tri
;
727 aaline
->stage
.flush
= aaline_flush
;
728 aaline
->stage
.reset_stipple_counter
= aaline_reset_stipple_counter
;
729 aaline
->stage
.destroy
= aaline_destroy
;
735 static struct aaline_stage
*
736 aaline_stage_from_pipe(struct pipe_context
*pipe
)
738 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
739 return aaline_stage(draw
->pipeline
.aaline
);
744 * This function overrides the driver's create_fs_state() function and
745 * will typically be called by the state tracker.
748 aaline_create_fs_state(struct pipe_context
*pipe
,
749 const struct pipe_shader_state
*fs
)
751 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
752 struct aaline_fragment_shader
*aafs
= CALLOC_STRUCT(aaline_fragment_shader
);
758 aafs
->driver_fs
= aaline
->driver_create_fs_state(aaline
->pipe
, fs
);
766 aaline_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
768 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
769 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
773 aaline
->driver_bind_fs_state(aaline
->pipe
,
774 (aafs
? aafs
->driver_fs
: NULL
));
779 aaline_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
781 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
782 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
784 aaline
->driver_delete_fs_state(aaline
->pipe
, aafs
->driver_fs
);
790 aaline_bind_sampler_states(struct pipe_context
*pipe
,
791 unsigned num
, void **sampler
)
793 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
795 memcpy(aaline
->state
.sampler
, sampler
, num
* sizeof(void *));
796 aaline
->num_samplers
= num
;
798 aaline
->driver_bind_sampler_states(aaline
->pipe
, num
, sampler
);
803 aaline_set_sampler_textures(struct pipe_context
*pipe
,
804 unsigned num
, struct pipe_texture
**texture
)
806 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
810 for (i
= 0; i
< num
; i
++) {
811 pipe_texture_reference(&aaline
->state
.texture
[i
], texture
[i
]);
813 aaline
->num_textures
= num
;
816 aaline
->driver_set_sampler_textures(aaline
->pipe
, num
, texture
);
821 * Called by drivers that want to install this AA line prim stage
822 * into the draw module's pipeline. This will not be used if the
823 * hardware has native support for AA lines.
826 draw_install_aaline_stage(struct draw_context
*draw
, struct pipe_context
*pipe
)
828 struct aaline_stage
*aaline
;
830 pipe
->draw
= (void *) draw
;
833 * Create / install AA line drawing / prim stage
835 aaline
= draw_aaline_stage( draw
);
837 draw
->pipeline
.aaline
= &aaline
->stage
;
841 /* create special texture, sampler state */
842 aaline_create_texture(aaline
);
843 aaline_create_sampler(aaline
);
845 /* save original driver functions */
846 aaline
->driver_create_fs_state
= pipe
->create_fs_state
;
847 aaline
->driver_bind_fs_state
= pipe
->bind_fs_state
;
848 aaline
->driver_delete_fs_state
= pipe
->delete_fs_state
;
850 aaline
->driver_bind_sampler_states
= pipe
->bind_sampler_states
;
851 aaline
->driver_set_sampler_textures
= pipe
->set_sampler_textures
;
853 /* override the driver's functions */
854 pipe
->create_fs_state
= aaline_create_fs_state
;
855 pipe
->bind_fs_state
= aaline_bind_fs_state
;
856 pipe
->delete_fs_state
= aaline_delete_fs_state
;
858 pipe
->bind_sampler_states
= aaline_bind_sampler_states
;
859 pipe
->set_sampler_textures
= aaline_set_sampler_textures
;