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"
49 * Max texture level for the alpha texture used for antialiasing
51 #define MAX_TEXTURE_LEVEL 5 /* 32 x 32 */
55 * Subclass of pipe_shader_state to carry extra fragment shader info.
57 struct aaline_fragment_shader
59 struct pipe_shader_state state
;
62 void *aapoint_fs
; /* not yet */
63 void *sprite_fs
; /* not yet */
65 int generic_attrib
; /**< texcoord/generic used for texture */
70 * Subclass of draw_stage
74 struct draw_stage stage
;
76 float half_line_width
;
78 /** For AA lines, this is the vertex attrib slot for the new texcoords */
82 struct pipe_texture
*texture
;
88 * Currently bound state
90 struct aaline_fragment_shader
*fs
;
92 void *sampler
[PIPE_MAX_SAMPLERS
];
93 struct pipe_texture
*texture
[PIPE_MAX_SAMPLERS
];
97 * Driver interface/override functions
99 void * (*driver_create_fs_state
)(struct pipe_context
*,
100 const struct pipe_shader_state
*);
101 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
102 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
104 void (*driver_bind_sampler_states
)(struct pipe_context
*, unsigned,
106 void (*driver_set_sampler_textures
)(struct pipe_context
*, unsigned,
107 struct pipe_texture
**);
109 struct pipe_context
*pipe
;
115 * Subclass of tgsi_transform_context, used for transforming the
116 * user's fragment shader to add the special AA instructions.
118 struct aa_transform_context
{
119 struct tgsi_transform_context base
;
120 uint tempsUsed
; /**< bitmask */
121 int colorOutput
; /**< which output is the primary color */
122 uint samplersUsed
; /**< bitfield of samplers used */
123 int freeSampler
; /** an available sampler for the pstipple */
124 int maxInput
, maxGeneric
; /**< max input index found */
125 int colorTemp
, texTemp
; /**< temp registers */
126 boolean firstInstruction
;
131 * TGSI declaration transform callback.
132 * Look for a free sampler, a free input attrib, and two free temp regs.
135 aa_transform_decl(struct tgsi_transform_context
*ctx
,
136 struct tgsi_full_declaration
*decl
)
138 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
140 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
141 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
&&
142 decl
->Semantic
.SemanticIndex
== 0) {
143 aactx
->colorOutput
= decl
->u
.DeclarationRange
.First
;
145 else if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
147 for (i
= decl
->u
.DeclarationRange
.First
;
148 i
<= decl
->u
.DeclarationRange
.Last
; i
++) {
149 aactx
->samplersUsed
|= 1 << i
;
152 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
153 if ((int) decl
->u
.DeclarationRange
.Last
> aactx
->maxInput
)
154 aactx
->maxInput
= decl
->u
.DeclarationRange
.Last
;
155 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
&&
156 (int) decl
->Semantic
.SemanticIndex
> aactx
->maxGeneric
) {
157 aactx
->maxGeneric
= decl
->Semantic
.SemanticIndex
;
160 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
162 for (i
= decl
->u
.DeclarationRange
.First
;
163 i
<= decl
->u
.DeclarationRange
.Last
; i
++) {
164 aactx
->tempsUsed
|= (1 << i
);
168 ctx
->emit_declaration(ctx
, decl
);
173 * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
176 free_bit(uint bitfield
)
179 for (i
= 0; i
< 32; i
++) {
180 if ((bitfield
& (1 << i
)) == 0)
188 * TGSI instruction transform callback.
189 * Replace writes to result.color w/ a temp reg.
190 * Upon END instruction, insert texture sampling code for antialiasing.
193 aa_transform_inst(struct tgsi_transform_context
*ctx
,
194 struct tgsi_full_instruction
*inst
)
196 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
198 if (aactx
->firstInstruction
) {
199 /* emit our new declarations before the first instruction */
201 struct tgsi_full_declaration decl
;
204 /* find free sampler */
205 aactx
->freeSampler
= free_bit(aactx
->samplersUsed
);
206 if (aactx
->freeSampler
>= PIPE_MAX_SAMPLERS
)
207 aactx
->freeSampler
= PIPE_MAX_SAMPLERS
- 1;
209 /* find two free temp regs */
210 for (i
= 0; i
< 32; i
++) {
211 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
212 /* found a free temp */
213 if (aactx
->colorTemp
< 0)
214 aactx
->colorTemp
= i
;
215 else if (aactx
->texTemp
< 0)
221 assert(aactx
->colorTemp
>= 0);
222 assert(aactx
->texTemp
>= 0);
224 /* declare new generic input/texcoord */
225 decl
= tgsi_default_full_declaration();
226 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
227 decl
.Declaration
.Semantic
= 1;
228 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
229 decl
.Semantic
.SemanticIndex
= aactx
->maxGeneric
+ 1;
230 decl
.Declaration
.Interpolate
= 1;
231 /* XXX this could be linear... */
232 decl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
233 decl
.u
.DeclarationRange
.First
=
234 decl
.u
.DeclarationRange
.Last
= aactx
->maxInput
+ 1;
235 ctx
->emit_declaration(ctx
, &decl
);
237 /* declare new sampler */
238 decl
= tgsi_default_full_declaration();
239 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
240 decl
.u
.DeclarationRange
.First
=
241 decl
.u
.DeclarationRange
.Last
= aactx
->freeSampler
;
242 ctx
->emit_declaration(ctx
, &decl
);
244 /* declare new temp regs */
245 decl
= tgsi_default_full_declaration();
246 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
247 decl
.u
.DeclarationRange
.First
=
248 decl
.u
.DeclarationRange
.Last
= aactx
->texTemp
;
249 ctx
->emit_declaration(ctx
, &decl
);
251 decl
= tgsi_default_full_declaration();
252 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
253 decl
.u
.DeclarationRange
.First
=
254 decl
.u
.DeclarationRange
.Last
= aactx
->colorTemp
;
255 ctx
->emit_declaration(ctx
, &decl
);
257 aactx
->firstInstruction
= FALSE
;
260 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
&&
261 aactx
->colorOutput
!= -1) {
262 struct tgsi_full_instruction newInst
;
265 newInst
= tgsi_default_full_instruction();
266 newInst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
267 newInst
.Instruction
.NumDstRegs
= 1;
268 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
269 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->texTemp
;
270 newInst
.Instruction
.NumSrcRegs
= 2;
271 newInst
.InstructionExtTexture
.Texture
= TGSI_TEXTURE_2D
;
272 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
273 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->maxInput
+ 1;
274 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
275 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->freeSampler
;
277 ctx
->emit_instruction(ctx
, &newInst
);
280 newInst
= tgsi_default_full_instruction();
281 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
282 newInst
.Instruction
.NumDstRegs
= 1;
283 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
284 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
285 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
286 newInst
.Instruction
.NumSrcRegs
= 1;
287 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
288 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
289 ctx
->emit_instruction(ctx
, &newInst
);
292 newInst
= tgsi_default_full_instruction();
293 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
294 newInst
.Instruction
.NumDstRegs
= 1;
295 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
296 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
297 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
298 newInst
.Instruction
.NumSrcRegs
= 2;
299 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
300 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
301 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
302 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->texTemp
;
303 ctx
->emit_instruction(ctx
, &newInst
);
306 newInst
= tgsi_default_full_instruction();
307 newInst
.Instruction
.Opcode
= TGSI_OPCODE_END
;
308 newInst
.Instruction
.NumDstRegs
= 0;
309 newInst
.Instruction
.NumSrcRegs
= 0;
310 ctx
->emit_instruction(ctx
, &newInst
);
313 /* Not an END instruction.
314 * Look for writes to result.color and replace with colorTemp reg.
318 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
319 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
320 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
&&
321 dst
->DstRegister
.Index
== aactx
->colorOutput
) {
322 dst
->DstRegister
.File
= TGSI_FILE_TEMPORARY
;
323 dst
->DstRegister
.Index
= aactx
->colorTemp
;
327 ctx
->emit_instruction(ctx
, inst
);
333 * Generate the frag shader we'll use for drawing AA lines.
334 * This will be the user's shader plus some texture/modulate instructions.
337 generate_aaline_fs(struct aaline_stage
*aaline
)
339 const struct pipe_shader_state
*orig_fs
= &aaline
->fs
->state
;
340 //struct draw_context *draw = aaline->stage.draw;
341 struct pipe_shader_state aaline_fs
;
342 struct aa_transform_context transform
;
346 aaline_fs
= *orig_fs
; /* copy to init */
347 aaline_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
349 memset(&transform
, 0, sizeof(transform
));
350 transform
.colorOutput
= -1;
351 transform
.maxInput
= -1;
352 transform
.maxGeneric
= -1;
353 transform
.colorTemp
= -1;
354 transform
.texTemp
= -1;
355 transform
.firstInstruction
= TRUE
;
356 transform
.base
.transform_instruction
= aa_transform_inst
;
357 transform
.base
.transform_declaration
= aa_transform_decl
;
359 tgsi_transform_shader(orig_fs
->tokens
,
360 (struct tgsi_token
*) aaline_fs
.tokens
,
361 MAX
, &transform
.base
);
364 tgsi_dump(orig_fs
->tokens
, 0);
365 tgsi_dump(aaline_fs
.tokens
, 0);
368 aaline
->fs
->sampler_unit
= transform
.freeSampler
;
370 aaline
->fs
->aaline_fs
371 = aaline
->driver_create_fs_state(aaline
->pipe
, &aaline_fs
);
373 aaline
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
378 * Create the texture map we'll use for antialiasing the lines.
381 aaline_create_texture(struct aaline_stage
*aaline
)
383 struct pipe_context
*pipe
= aaline
->pipe
;
384 struct pipe_screen
*screen
= pipe
->screen
;
385 struct pipe_texture texTemp
;
388 memset(&texTemp
, 0, sizeof(texTemp
));
389 texTemp
.target
= PIPE_TEXTURE_2D
;
390 texTemp
.format
= PIPE_FORMAT_U_A8
; /* XXX verify supported by driver! */
391 texTemp
.last_level
= MAX_TEXTURE_LEVEL
;
392 texTemp
.width
[0] = 1 << MAX_TEXTURE_LEVEL
;
393 texTemp
.height
[0] = 1 << MAX_TEXTURE_LEVEL
;
394 texTemp
.depth
[0] = 1;
397 aaline
->texture
= screen
->texture_create(screen
, &texTemp
);
399 /* Fill in mipmap images.
400 * Basically each level is solid opaque, except for the outermost
401 * texels which are zero. Special case the 1x1 and 2x2 levels.
403 for (level
= 0; level
<= MAX_TEXTURE_LEVEL
; level
++) {
404 struct pipe_surface
*surface
;
405 const uint size
= aaline
->texture
->width
[level
];
409 assert(aaline
->texture
->width
[level
] == aaline
->texture
->height
[level
]);
411 surface
= screen
->get_tex_surface(screen
, aaline
->texture
, 0, level
, 0);
412 data
= pipe_surface_map(surface
);
414 for (i
= 0; i
< size
; i
++) {
415 for (j
= 0; j
< size
; j
++) {
420 else if (size
== 2) {
421 d
= 200; /* tuneable */
423 else if (i
== 0 || j
== 0 || i
== size
- 1 || j
== size
- 1) {
429 data
[i
* surface
->pitch
+ j
] = d
;
434 pipe_surface_unmap(surface
);
435 pipe_surface_reference(&surface
, NULL
);
436 pipe
->texture_update(pipe
, aaline
->texture
, 0, (1 << level
));
442 * Create the sampler CSO that'll be used for antialiasing.
443 * By using a mipmapped texture, we don't have to generate a different
444 * texture image for each line size.
447 aaline_create_sampler(struct aaline_stage
*aaline
)
449 struct pipe_sampler_state sampler
;
450 struct pipe_context
*pipe
= aaline
->pipe
;
452 memset(&sampler
, 0, sizeof(sampler
));
453 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
454 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
455 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
456 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
457 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
458 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
459 sampler
.normalized_coords
= 1;
460 sampler
.min_lod
= 0.0f
;
461 sampler
.max_lod
= MAX_TEXTURE_LEVEL
;
463 aaline
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
468 * When we're about to draw our first AA line in a batch, this function is
469 * called to tell the driver to bind our modified fragment shader.
472 bind_aaline_fragment_shader(struct aaline_stage
*aaline
)
474 if (!aaline
->fs
->aaline_fs
) {
475 generate_aaline_fs(aaline
);
477 aaline
->driver_bind_fs_state(aaline
->pipe
, aaline
->fs
->aaline_fs
);
482 static INLINE
struct aaline_stage
*
483 aaline_stage( struct draw_stage
*stage
)
485 return (struct aaline_stage
*) stage
;
490 passthrough_point(struct draw_stage
*stage
, struct prim_header
*header
)
492 stage
->next
->point(stage
->next
, header
);
497 passthrough_tri(struct draw_stage
*stage
, struct prim_header
*header
)
499 stage
->next
->tri(stage
->next
, header
);
504 * Draw a wide line by drawing a quad, using geometry which will
505 * fullfill GL's antialiased line requirements.
508 aaline_line(struct draw_stage
*stage
, struct prim_header
*header
)
510 const struct aaline_stage
*aaline
= aaline_stage(stage
);
511 const float half_width
= aaline
->half_line_width
;
512 struct prim_header tri
;
513 struct vertex_header
*v
[8];
514 uint texPos
= aaline
->tex_slot
;
516 float dx
= header
->v
[1]->data
[0][0] - header
->v
[0]->data
[0][0];
517 float dy
= header
->v
[1]->data
[0][1] - header
->v
[0]->data
[0][1];
518 double a
= atan2(dy
, dx
);
519 float c_a
= (float) cos(a
), s_a
= (float) sin(a
);
522 /* XXX the ends of lines aren't quite perfect yet, but probably passable */
523 dx
= 0.5F
* half_width
;
526 /* allocate/dup new verts */
527 for (i
= 0; i
< 8; i
++) {
528 v
[i
] = dup_vert(stage
, header
->v
[i
/4], i
);
532 * Quad strip for line from v0 to v1 (*=endpoints):
535 * +---+---------------------+---+
539 * +---+---------------------+---+
545 pos
[0] += (-dx
* c_a
- dy
* s_a
);
546 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
549 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
550 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
553 pos
[0] += ( dx
* c_a
- dy
* s_a
);
554 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
557 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
558 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
561 pos
[0] += (-dx
* c_a
- dy
* s_a
);
562 pos
[1] += (-dx
* s_a
+ dy
* c_a
);
565 pos
[0] += (-dx
* c_a
- -dy
* s_a
);
566 pos
[1] += (-dx
* s_a
+ -dy
* c_a
);
569 pos
[0] += ( dx
* c_a
- dy
* s_a
);
570 pos
[1] += ( dx
* s_a
+ dy
* c_a
);
573 pos
[0] += ( dx
* c_a
- -dy
* s_a
);
574 pos
[1] += ( dx
* s_a
+ -dy
* c_a
);
577 tex
= v
[0]->data
[texPos
];
578 ASSIGN_4V(tex
, 0, 0, 0, 1);
580 tex
= v
[1]->data
[texPos
];
581 ASSIGN_4V(tex
, 0, 1, 0, 1);
583 tex
= v
[2]->data
[texPos
];
584 ASSIGN_4V(tex
, .5, 0, 0, 1);
586 tex
= v
[3]->data
[texPos
];
587 ASSIGN_4V(tex
, .5, 1, 0, 1);
589 tex
= v
[4]->data
[texPos
];
590 ASSIGN_4V(tex
, .5, 0, 0, 1);
592 tex
= v
[5]->data
[texPos
];
593 ASSIGN_4V(tex
, .5, 1, 0, 1);
595 tex
= v
[6]->data
[texPos
];
596 ASSIGN_4V(tex
, 1, 0, 0, 1);
598 tex
= v
[7]->data
[texPos
];
599 ASSIGN_4V(tex
, 1, 1, 0, 1);
601 /* emit 6 tris for the quad strip */
602 tri
.v
[0] = v
[2]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[0];
603 stage
->next
->tri( stage
->next
, &tri
);
605 tri
.v
[0] = v
[3]; tri
.v
[1] = v
[1]; tri
.v
[2] = v
[2];
606 stage
->next
->tri( stage
->next
, &tri
);
608 tri
.v
[0] = v
[4]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[2];
609 stage
->next
->tri( stage
->next
, &tri
);
611 tri
.v
[0] = v
[5]; tri
.v
[1] = v
[3]; tri
.v
[2] = v
[4];
612 stage
->next
->tri( stage
->next
, &tri
);
614 tri
.v
[0] = v
[6]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[4];
615 stage
->next
->tri( stage
->next
, &tri
);
617 tri
.v
[0] = v
[7]; tri
.v
[1] = v
[5]; tri
.v
[2] = v
[6];
618 stage
->next
->tri( stage
->next
, &tri
);
623 aaline_first_line(struct draw_stage
*stage
, struct prim_header
*header
)
625 auto struct aaline_stage
*aaline
= aaline_stage(stage
);
626 struct draw_context
*draw
= stage
->draw
;
627 struct pipe_context
*pipe
= aaline
->pipe
;
630 assert(draw
->rasterizer
->line_smooth
);
632 if (draw
->rasterizer
->line_width
<= 3.0)
633 aaline
->half_line_width
= 1.5f
;
635 aaline
->half_line_width
= 0.5f
* draw
->rasterizer
->line_width
;
638 * Bind (generate) our fragprog, sampler and texture
640 bind_aaline_fragment_shader(aaline
);
642 /* update vertex attrib info */
643 aaline
->tex_slot
= draw
->num_vs_outputs
;
644 assert(aaline
->tex_slot
> 0); /* output[0] is vertex pos */
646 /* advertise the extra post-transformed vertex attribute */
647 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
648 draw
->extra_vp_outputs
.semantic_index
= aaline
->fs
->generic_attrib
;
649 draw
->extra_vp_outputs
.slot
= aaline
->tex_slot
;
651 /* how many samplers? */
652 /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
653 num_samplers
= MAX2(aaline
->num_textures
, aaline
->num_samplers
);
654 num_samplers
= MAX2(num_samplers
, aaline
->fs
->sampler_unit
+ 1);
656 aaline
->state
.sampler
[aaline
->fs
->sampler_unit
] = aaline
->sampler_cso
;
657 pipe_texture_reference(&aaline
->state
.texture
[aaline
->fs
->sampler_unit
],
660 aaline
->driver_bind_sampler_states(pipe
, num_samplers
, aaline
->state
.sampler
);
661 aaline
->driver_set_sampler_textures(pipe
, num_samplers
, aaline
->state
.texture
);
663 /* now really draw first line */
664 stage
->line
= aaline_line
;
665 stage
->line(stage
, header
);
670 aaline_flush(struct draw_stage
*stage
, unsigned flags
)
672 struct draw_context
*draw
= stage
->draw
;
673 struct aaline_stage
*aaline
= aaline_stage(stage
);
674 struct pipe_context
*pipe
= aaline
->pipe
;
676 stage
->line
= aaline_first_line
;
677 stage
->next
->flush( stage
->next
, flags
);
679 /* restore original frag shader */
680 aaline
->driver_bind_fs_state(pipe
, aaline
->fs
->driver_fs
);
682 /* XXX restore original texture, sampler state */
683 aaline
->driver_bind_sampler_states(pipe
, aaline
->num_samplers
,
684 aaline
->state
.sampler
);
685 aaline
->driver_set_sampler_textures(pipe
, aaline
->num_textures
,
686 aaline
->state
.texture
);
688 draw
->extra_vp_outputs
.slot
= 0;
693 aaline_reset_stipple_counter(struct draw_stage
*stage
)
695 stage
->next
->reset_stipple_counter( stage
->next
);
700 aaline_destroy(struct draw_stage
*stage
)
702 struct aaline_stage
*aaline
= aaline_stage(stage
);
704 aaline
->pipe
->delete_sampler_state(aaline
->pipe
, aaline
->sampler_cso
);
706 pipe_texture_release(&aaline
->texture
);
708 draw_free_temp_verts( stage
);
714 static struct aaline_stage
*
715 draw_aaline_stage(struct draw_context
*draw
)
717 struct aaline_stage
*aaline
= CALLOC_STRUCT(aaline_stage
);
719 draw_alloc_temp_verts( &aaline
->stage
, 8 );
721 aaline
->stage
.draw
= draw
;
722 aaline
->stage
.next
= NULL
;
723 aaline
->stage
.point
= passthrough_point
;
724 aaline
->stage
.line
= aaline_first_line
;
725 aaline
->stage
.tri
= passthrough_tri
;
726 aaline
->stage
.flush
= aaline_flush
;
727 aaline
->stage
.reset_stipple_counter
= aaline_reset_stipple_counter
;
728 aaline
->stage
.destroy
= aaline_destroy
;
734 static struct aaline_stage
*
735 aaline_stage_from_pipe(struct pipe_context
*pipe
)
737 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
738 return aaline_stage(draw
->pipeline
.aaline
);
743 * This function overrides the driver's create_fs_state() function and
744 * will typically be called by the state tracker.
747 aaline_create_fs_state(struct pipe_context
*pipe
,
748 const struct pipe_shader_state
*fs
)
750 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
751 struct aaline_fragment_shader
*aafs
= CALLOC_STRUCT(aaline_fragment_shader
);
757 aafs
->driver_fs
= aaline
->driver_create_fs_state(aaline
->pipe
, fs
);
765 aaline_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
767 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
768 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
772 aaline
->driver_bind_fs_state(aaline
->pipe
,
773 (aafs
? aafs
->driver_fs
: NULL
));
778 aaline_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
780 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
781 struct aaline_fragment_shader
*aafs
= (struct aaline_fragment_shader
*) fs
;
783 aaline
->driver_delete_fs_state(aaline
->pipe
, aafs
->driver_fs
);
789 aaline_bind_sampler_states(struct pipe_context
*pipe
,
790 unsigned num
, void **sampler
)
792 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
794 memcpy(aaline
->state
.sampler
, sampler
, num
* sizeof(void *));
795 aaline
->num_samplers
= num
;
797 aaline
->driver_bind_sampler_states(aaline
->pipe
, num
, sampler
);
802 aaline_set_sampler_textures(struct pipe_context
*pipe
,
803 unsigned num
, struct pipe_texture
**texture
)
805 struct aaline_stage
*aaline
= aaline_stage_from_pipe(pipe
);
809 for (i
= 0; i
< num
; i
++) {
810 pipe_texture_reference(&aaline
->state
.texture
[i
], texture
[i
]);
812 aaline
->num_textures
= num
;
815 aaline
->driver_set_sampler_textures(aaline
->pipe
, num
, texture
);
820 * Called by drivers that want to install this AA line prim stage
821 * into the draw module's pipeline. This will not be used if the
822 * hardware has native support for AA lines.
825 draw_install_aaline_stage(struct draw_context
*draw
, struct pipe_context
*pipe
)
827 struct aaline_stage
*aaline
;
829 pipe
->draw
= (void *) draw
;
832 * Create / install AA line drawing / prim stage
834 aaline
= draw_aaline_stage( draw
);
836 draw
->pipeline
.aaline
= &aaline
->stage
;
840 /* create special texture, sampler state */
841 aaline_create_texture(aaline
);
842 aaline_create_sampler(aaline
);
844 /* save original driver functions */
845 aaline
->driver_create_fs_state
= pipe
->create_fs_state
;
846 aaline
->driver_bind_fs_state
= pipe
->bind_fs_state
;
847 aaline
->driver_delete_fs_state
= pipe
->delete_fs_state
;
849 aaline
->driver_bind_sampler_states
= pipe
->bind_sampler_states
;
850 aaline
->driver_set_sampler_textures
= pipe
->set_sampler_textures
;
852 /* override the driver's functions */
853 pipe
->create_fs_state
= aaline_create_fs_state
;
854 pipe
->bind_fs_state
= aaline_bind_fs_state
;
855 pipe
->delete_fs_state
= aaline_delete_fs_state
;
857 pipe
->bind_sampler_states
= aaline_bind_sampler_states
;
858 pipe
->set_sampler_textures
= aaline_set_sampler_textures
;