1 /**************************************************************************
3 * Copyright 2008 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 * Polygon stipple stage: implement polygon stipple with texture map and
30 * fragment program. The fragment program samples the texture and does
31 * a fragment kill for the stipple-failing fragments.
37 #include "pipe/p_util.h"
38 #include "pipe/p_inlines.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_shader_tokens.h"
43 #include "tgsi/util/tgsi_transform.h"
44 #include "tgsi/util/tgsi_dump.h"
46 #include "draw_context.h"
47 #include "draw_private.h"
52 * Subclass of pipe_shader_state to carry extra fragment shader info.
54 struct pstip_fragment_shader
56 struct pipe_shader_state state
;
63 * Subclass of draw_stage
67 struct draw_stage stage
;
70 struct pipe_texture
*texture
;
74 * Currently bound state
76 struct pstip_fragment_shader
*fs
;
78 void *sampler
[PIPE_MAX_SAMPLERS
];
79 struct pipe_texture
*texture
[PIPE_MAX_SAMPLERS
];
80 const struct pipe_poly_stipple
*stipple
;
84 * Driver interface/override functions
86 void * (*driver_create_fs_state
)(struct pipe_context
*,
87 const struct pipe_shader_state
*);
88 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
89 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
91 void (*driver_bind_sampler_state
)(struct pipe_context
*, unsigned, void *);
93 void (*driver_set_sampler_texture
)(struct pipe_context
*,
95 struct pipe_texture
*);
97 void (*driver_set_polygon_stipple
)(struct pipe_context
*,
98 const struct pipe_poly_stipple
*);
100 struct pipe_context
*pipe
;
106 * Subclass of tgsi_transform_context, used for transforming the
107 * user's fragment shader to add the special AA instructions.
109 struct pstip_transform_context
{
110 struct tgsi_transform_context base
;
111 uint tempsUsed
; /**< bitmask */
114 int maxSampler
; /**< max sampler index found */
115 int texTemp
; /**< temp registers */
117 boolean firstInstruction
;
122 * TGSI declaration transform callback.
123 * Look for a free sampler, a free input attrib, and two free temp regs.
126 pstip_transform_decl(struct tgsi_transform_context
*ctx
,
127 struct tgsi_full_declaration
*decl
)
129 struct pstip_transform_context
*pctx
= (struct pstip_transform_context
*) ctx
;
131 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
132 if ((int) decl
->u
.DeclarationRange
.Last
> pctx
->maxSampler
)
133 pctx
->maxSampler
= (int) decl
->u
.DeclarationRange
.Last
;
135 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
136 pctx
->maxInput
= MAX2(pctx
->maxInput
, (int) decl
->u
.DeclarationRange
.Last
);
137 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_POSITION
)
138 pctx
->wincoordInput
= (int) decl
->u
.DeclarationRange
.First
;
140 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
142 for (i
= decl
->u
.DeclarationRange
.First
;
143 i
<= decl
->u
.DeclarationRange
.Last
; i
++) {
144 pctx
->tempsUsed
|= (1 << i
);
148 ctx
->emit_declaration(ctx
, decl
);
153 pstip_transform_immed(struct tgsi_transform_context
*ctx
,
154 struct tgsi_full_immediate
*immed
)
156 struct pstip_transform_context
*pctx
= (struct pstip_transform_context
*) ctx
;
162 * TGSI instruction transform callback.
163 * Replace writes to result.color w/ a temp reg.
164 * Upon END instruction, insert texture sampling code for antialiasing.
167 pstip_transform_inst(struct tgsi_transform_context
*ctx
,
168 struct tgsi_full_instruction
*inst
)
170 struct pstip_transform_context
*pctx
= (struct pstip_transform_context
*) ctx
;
172 if (pctx
->firstInstruction
) {
173 /* emit our new declarations before the first instruction */
175 struct tgsi_full_declaration decl
;
176 struct tgsi_full_instruction newInst
;
179 const int sampler
= pctx
->maxSampler
+ 1;
181 if (pctx
->wincoordInput
< 0)
182 wincoordInput
= pctx
->maxInput
+ 1;
184 wincoordInput
= pctx
->wincoordInput
;
186 /* find one free temp reg */
187 for (i
= 0; i
< 32; i
++) {
188 if ((pctx
->tempsUsed
& (1 << i
)) == 0) {
189 /* found a free temp */
190 if (pctx
->texTemp
< 0)
196 assert(pctx
->texTemp
>= 0);
198 if (pctx
->wincoordInput
< 0) {
199 /* declare new position input reg */
200 decl
= tgsi_default_full_declaration();
201 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
202 decl
.Declaration
.Semantic
= 1;
203 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_POSITION
;
204 decl
.Semantic
.SemanticIndex
= 0;
205 decl
.Declaration
.Interpolate
= 1;
206 decl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_LINEAR
; /* XXX? */
207 decl
.u
.DeclarationRange
.First
=
208 decl
.u
.DeclarationRange
.Last
= wincoordInput
;
209 ctx
->emit_declaration(ctx
, &decl
);
212 /* declare new sampler */
213 decl
= tgsi_default_full_declaration();
214 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
215 decl
.u
.DeclarationRange
.First
=
216 decl
.u
.DeclarationRange
.Last
= sampler
;
217 ctx
->emit_declaration(ctx
, &decl
);
219 /* declare new temp regs */
220 decl
= tgsi_default_full_declaration();
221 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
222 decl
.u
.DeclarationRange
.First
=
223 decl
.u
.DeclarationRange
.Last
= pctx
->texTemp
;
224 ctx
->emit_declaration(ctx
, &decl
);
226 /* emit immediate = {1/32, 1/32, 1, 1}
227 * The index/position of this immediate will be pctx->numImmed
230 static const float value
[4] = { 1.0/32, 1.0/32, 1.0, 1.0 };
231 struct tgsi_full_immediate immed
;
233 immed
= tgsi_default_full_immediate();
234 immed
.Immediate
.Size
= 1 + size
; /* one for the token itself */
235 immed
.u
.ImmediateFloat32
= (struct tgsi_immediate_float32
*) value
;
236 ctx
->emit_immediate(ctx
, &immed
);
239 pctx
->firstInstruction
= FALSE
;
243 * Insert new MUL/TEX/KILP instructions at start of program
244 * Take gl_FragCoord, divide by 32 (stipple size), sample the
245 * texture and kill fragment if needed.
247 * We'd like to use non-normalized texcoords to index into a RECT
248 * texture, but we can only use GL_REPEAT wrap mode with normalized
252 /* MUL texTemp, INPUT[wincoord], 1/32; */
253 newInst
= tgsi_default_full_instruction();
254 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
255 newInst
.Instruction
.NumDstRegs
= 1;
256 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
257 newInst
.FullDstRegisters
[0].DstRegister
.Index
= pctx
->texTemp
;
258 newInst
.Instruction
.NumSrcRegs
= 2;
259 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
260 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= wincoordInput
;
261 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_IMMEDIATE
;
262 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= pctx
->numImmed
;
263 ctx
->emit_instruction(ctx
, &newInst
);
265 /* TEX texTemp, texTemp, sampler; */
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
= pctx
->texTemp
;
271 newInst
.Instruction
.NumSrcRegs
= 2;
272 newInst
.InstructionExtTexture
.Texture
= TGSI_TEXTURE_2D
;
273 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
274 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= pctx
->texTemp
;
275 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_SAMPLER
;
276 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= sampler
;
277 ctx
->emit_instruction(ctx
, &newInst
);
279 /* KILP texTemp; # if texTemp < 0, KILL fragment */
280 newInst
= tgsi_default_full_instruction();
281 newInst
.Instruction
.Opcode
= TGSI_OPCODE_KILP
;
282 newInst
.Instruction
.NumDstRegs
= 0;
283 newInst
.Instruction
.NumSrcRegs
= 1;
284 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
285 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= pctx
->texTemp
;
286 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
287 ctx
->emit_instruction(ctx
, &newInst
);
290 /* emit this instruction */
291 ctx
->emit_instruction(ctx
, inst
);
296 * Generate the frag shader we'll use for doing polygon stipple.
297 * This will be the user's shader prefixed with a TEX and KIL instruction.
300 generate_pstip_fs(struct pstip_stage
*pstip
)
302 const struct pipe_shader_state
*orig_fs
= &pstip
->fs
->state
;
303 /*struct draw_context *draw = pstip->stage.draw;*/
304 struct pipe_shader_state pstip_fs
;
305 struct pstip_transform_context transform
;
309 pstip_fs
= *orig_fs
; /* copy to init */
310 pstip_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
312 memset(&transform
, 0, sizeof(transform
));
313 transform
.wincoordInput
= -1;
314 transform
.maxInput
= -1;
315 transform
.maxSampler
= -1;
316 transform
.texTemp
= -1;
317 transform
.firstInstruction
= TRUE
;
318 transform
.base
.transform_instruction
= pstip_transform_inst
;
319 transform
.base
.transform_declaration
= pstip_transform_decl
;
320 transform
.base
.transform_immediate
= pstip_transform_immed
;
322 tgsi_transform_shader(orig_fs
->tokens
,
323 (struct tgsi_token
*) pstip_fs
.tokens
,
324 MAX
, &transform
.base
);
327 tgsi_dump(orig_fs
->tokens
, 0);
328 tgsi_dump(pstip_fs
.tokens
, 0);
331 pstip
->sampler_unit
= transform
.maxSampler
+ 1;
333 #if 1 /* XXX remove */
334 if (transform
.wincoordInput
< 0) {
335 pstip_fs
.input_semantic_name
[pstip_fs
.num_inputs
] = TGSI_SEMANTIC_POSITION
;
336 pstip_fs
.input_semantic_index
[pstip_fs
.num_inputs
] = (ubyte
)transform
.maxInput
;
337 pstip_fs
.num_inputs
++;
341 pstip
->fs
->pstip_fs
= pstip
->driver_create_fs_state(pstip
->pipe
, &pstip_fs
);
346 * Load texture image with current stipple pattern.
349 pstip_update_texture(struct pstip_stage
*pstip
)
351 static const uint bit31
= 1 << 31;
352 struct pipe_context
*pipe
= pstip
->pipe
;
353 struct pipe_screen
*screen
= pipe
->screen
;
354 struct pipe_surface
*surface
;
355 const uint
*stipple
= pstip
->state
.stipple
->stipple
;
359 surface
= screen
->get_tex_surface(screen
, pstip
->texture
, 0, 0, 0);
360 data
= pipe_surface_map(surface
);
363 * Load alpha texture.
364 * Note: 0 means keep the fragment, 255 means kill it.
365 * We'll negate the texel value and use KILP which kills if value
368 for (i
= 0; i
< 32; i
++) {
369 for (j
= 0; j
< 32; j
++) {
370 if (stipple
[i
] & (bit31
>> j
)) {
372 data
[i
* surface
->pitch
+ j
] = 0;
376 data
[i
* surface
->pitch
+ j
] = 255;
382 pipe_surface_unmap(surface
);
383 pipe_surface_reference(&surface
, NULL
);
384 pipe
->texture_update(pipe
, pstip
->texture
);
389 * Create the texture map we'll use for stippling.
392 pstip_create_texture(struct pstip_stage
*pstip
)
394 struct pipe_context
*pipe
= pstip
->pipe
;
395 struct pipe_screen
*screen
= pipe
->screen
;
396 struct pipe_texture texTemp
;
398 memset(&texTemp
, 0, sizeof(texTemp
));
399 texTemp
.target
= PIPE_TEXTURE_2D
;
400 texTemp
.format
= PIPE_FORMAT_U_A8
; /* XXX verify supported by driver! */
401 texTemp
.last_level
= 0;
402 texTemp
.width
[0] = 32;
403 texTemp
.height
[0] = 32;
404 texTemp
.depth
[0] = 1;
407 pstip
->texture
= screen
->texture_create(screen
, &texTemp
);
409 //pstip_update_texture(pstip);
414 * Create the sampler CSO that'll be used for antialiasing.
415 * By using a mipmapped texture, we don't have to generate a different
416 * texture image for each line size.
419 pstip_create_sampler(struct pstip_stage
*pstip
)
421 struct pipe_sampler_state sampler
;
422 struct pipe_context
*pipe
= pstip
->pipe
;
424 memset(&sampler
, 0, sizeof(sampler
));
425 sampler
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
426 sampler
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
427 sampler
.wrap_r
= PIPE_TEX_WRAP_REPEAT
;
428 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
429 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
430 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
431 sampler
.normalized_coords
= 1;
432 sampler
.min_lod
= 0.0f
;
433 sampler
.max_lod
= 0.0f
;
435 pstip
->sampler_cso
= pipe
->create_sampler_state(pipe
, &sampler
);
440 * When we're about to draw our first AA line in a batch, this function is
441 * called to tell the driver to bind our modified fragment shader.
444 bind_pstip_fragment_shader(struct pstip_stage
*pstip
)
446 if (!pstip
->fs
->pstip_fs
) {
447 generate_pstip_fs(pstip
);
449 pstip
->driver_bind_fs_state(pstip
->pipe
, pstip
->fs
->pstip_fs
);
454 static INLINE
struct pstip_stage
*
455 pstip_stage( struct draw_stage
*stage
)
457 return (struct pstip_stage
*) stage
;
462 passthrough_point(struct draw_stage
*stage
, struct prim_header
*header
)
464 stage
->next
->point(stage
->next
, header
);
469 passthrough_line(struct draw_stage
*stage
, struct prim_header
*header
)
471 stage
->next
->line(stage
->next
, header
);
476 passthrough_tri(struct draw_stage
*stage
, struct prim_header
*header
)
478 stage
->next
->tri(stage
->next
, header
);
484 pstip_first_tri(struct draw_stage
*stage
, struct prim_header
*header
)
486 struct pstip_stage
*pstip
= pstip_stage(stage
);
487 struct draw_context
*draw
= stage
->draw
;
488 struct pipe_context
*pipe
= pstip
->pipe
;
490 assert(draw
->rasterizer
->poly_stipple_enable
);
493 * Bind our fragprog, sampler and texture
495 bind_pstip_fragment_shader(pstip
);
497 pstip
->driver_bind_sampler_state(pipe
, pstip
->sampler_unit
, pstip
->sampler_cso
);
498 pstip
->driver_set_sampler_texture(pipe
, pstip
->sampler_unit
, pstip
->texture
);
500 /* now really draw first line */
501 stage
->tri
= passthrough_tri
;
502 stage
->tri(stage
, header
);
507 pstip_flush(struct draw_stage
*stage
, unsigned flags
)
509 /*struct draw_context *draw = stage->draw;*/
510 struct pstip_stage
*pstip
= pstip_stage(stage
);
511 struct pipe_context
*pipe
= pstip
->pipe
;
513 stage
->tri
= pstip_first_tri
;
514 stage
->next
->flush( stage
->next
, flags
);
516 /* restore original frag shader */
517 pstip
->driver_bind_fs_state(pipe
, pstip
->fs
->driver_fs
);
519 /* XXX restore original texture, sampler state */
520 pstip
->driver_bind_sampler_state(pipe
, pstip
->sampler_unit
,
521 pstip
->state
.sampler
[pstip
->sampler_unit
]);
522 pstip
->driver_set_sampler_texture(pipe
, pstip
->sampler_unit
,
523 pstip
->state
.texture
[pstip
->sampler_unit
]);
528 pstip_reset_stipple_counter(struct draw_stage
*stage
)
530 stage
->next
->reset_stipple_counter( stage
->next
);
535 pstip_destroy(struct draw_stage
*stage
)
537 draw_free_temp_verts( stage
);
542 static struct pstip_stage
*
543 draw_pstip_stage(struct draw_context
*draw
)
545 struct pstip_stage
*pstip
= CALLOC_STRUCT(pstip_stage
);
547 draw_alloc_temp_verts( &pstip
->stage
, 8 );
549 pstip
->stage
.draw
= draw
;
550 pstip
->stage
.next
= NULL
;
551 pstip
->stage
.point
= passthrough_point
;
552 pstip
->stage
.line
= passthrough_line
;
553 pstip
->stage
.tri
= pstip_first_tri
;
554 pstip
->stage
.flush
= pstip_flush
;
555 pstip
->stage
.reset_stipple_counter
= pstip_reset_stipple_counter
;
556 pstip
->stage
.destroy
= pstip_destroy
;
562 static struct pstip_stage
*
563 pstip_stage_from_pipe(struct pipe_context
*pipe
)
565 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
566 return pstip_stage(draw
->pipeline
.pstipple
);
571 * This function overrides the driver's create_fs_state() function and
572 * will typically be called by the state tracker.
575 pstip_create_fs_state(struct pipe_context
*pipe
,
576 const struct pipe_shader_state
*fs
)
578 struct pstip_stage
*pstip
= pstip_stage_from_pipe(pipe
);
579 struct pstip_fragment_shader
*aafs
= CALLOC_STRUCT(pstip_fragment_shader
);
585 aafs
->driver_fs
= pstip
->driver_create_fs_state(pstip
->pipe
, fs
);
593 pstip_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
595 struct pstip_stage
*pstip
= pstip_stage_from_pipe(pipe
);
596 struct pstip_fragment_shader
*aafs
= (struct pstip_fragment_shader
*) fs
;
600 pstip
->driver_bind_fs_state(pstip
->pipe
, aafs
->driver_fs
);
605 pstip_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
607 struct pstip_stage
*pstip
= pstip_stage_from_pipe(pipe
);
608 struct pstip_fragment_shader
*aafs
= (struct pstip_fragment_shader
*) fs
;
610 pstip
->driver_delete_fs_state(pstip
->pipe
, aafs
->driver_fs
);
616 pstip_bind_sampler_state(struct pipe_context
*pipe
,
617 unsigned unit
, void *sampler
)
619 struct pstip_stage
*pstip
= pstip_stage_from_pipe(pipe
);
621 pstip
->state
.sampler
[unit
] = sampler
;
623 pstip
->driver_bind_sampler_state(pstip
->pipe
, unit
, sampler
);
628 pstip_set_sampler_texture(struct pipe_context
*pipe
,
629 unsigned sampler
, struct pipe_texture
*texture
)
631 struct pstip_stage
*pstip
= pstip_stage_from_pipe(pipe
);
633 pstip
->state
.texture
[sampler
] = texture
;
635 pstip
->driver_set_sampler_texture(pstip
->pipe
, sampler
, texture
);
640 pstip_set_polygon_stipple(struct pipe_context
*pipe
,
641 const struct pipe_poly_stipple
*stipple
)
643 struct pstip_stage
*pstip
= pstip_stage_from_pipe(pipe
);
645 pstip
->state
.stipple
= stipple
;
647 pstip
->driver_set_polygon_stipple(pstip
->pipe
, stipple
);
649 pstip_update_texture(pstip
);
655 * Called by drivers that want to install this AA line prim stage
656 * into the draw module's pipeline. This will not be used if the
657 * hardware has native support for AA lines.
660 draw_install_pstipple_stage(struct draw_context
*draw
,
661 struct pipe_context
*pipe
)
663 struct pstip_stage
*pstip
;
665 pipe
->draw
= (void *) draw
;
668 * Create / install AA line drawing / prim stage
670 pstip
= draw_pstip_stage( draw
);
672 draw
->pipeline
.pstipple
= &pstip
->stage
;
676 /* create special texture, sampler state */
677 pstip_create_texture(pstip
);
678 pstip_create_sampler(pstip
);
680 /* save original driver functions */
681 pstip
->driver_create_fs_state
= pipe
->create_fs_state
;
682 pstip
->driver_bind_fs_state
= pipe
->bind_fs_state
;
683 pstip
->driver_delete_fs_state
= pipe
->delete_fs_state
;
685 pstip
->driver_bind_sampler_state
= pipe
->bind_sampler_state
;
686 pstip
->driver_set_sampler_texture
= pipe
->set_sampler_texture
;
687 pstip
->driver_set_polygon_stipple
= pipe
->set_polygon_stipple
;
689 /* override the driver's functions */
690 pipe
->create_fs_state
= pstip_create_fs_state
;
691 pipe
->bind_fs_state
= pstip_bind_fs_state
;
692 pipe
->delete_fs_state
= pstip_delete_fs_state
;
694 pipe
->bind_sampler_state
= pstip_bind_sampler_state
;
695 pipe
->set_sampler_texture
= pstip_set_sampler_texture
;
696 pipe
->set_polygon_stipple
= pstip_set_polygon_stipple
;