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 * AA point stage: AA points are converted to quads and rendered with a
30 * special fragment shader. Another approach would be to use a texture
31 * map image of a point, but experiments indicate the quality isn't nearly
32 * as good as this approach.
34 * Note: this looks a lot like draw_aaline.c but there's actually little
35 * if any code that can be shared.
41 #include "pipe/p_inlines.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_shader_tokens.h"
46 #include "tgsi/tgsi_transform.h"
47 #include "tgsi/tgsi_dump.h"
49 #include "util/u_math.h"
50 #include "util/u_memory.h"
52 #include "draw_context.h"
54 #include "draw_pipe.h"
58 * Enabling NORMALIZE might give _slightly_ better results.
59 * Basically, it controls whether we compute distance as d=sqrt(x*x+y*y) or
60 * d=x*x+y*y. Since we're working with a unit circle, the later seems
61 * close enough and saves some costly instructions.
67 * Subclass of pipe_shader_state to carry extra fragment shader info.
69 struct aapoint_fragment_shader
71 struct pipe_shader_state state
;
72 void *driver_fs
; /**< the regular shader */
73 void *aapoint_fs
; /**< the aa point-augmented shader */
74 int generic_attrib
; /**< The generic input attrib/texcoord we'll use */
79 * Subclass of draw_stage
83 struct draw_stage stage
;
88 /** this is the vertex attrib slot for the new texcoords */
93 * Currently bound state
95 struct aapoint_fragment_shader
*fs
;
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 struct pipe_context
*pipe
;
111 * Subclass of tgsi_transform_context, used for transforming the
112 * user's fragment shader to add the special AA instructions.
114 struct aa_transform_context
{
115 struct tgsi_transform_context base
;
116 uint tempsUsed
; /**< bitmask */
117 int colorOutput
; /**< which output is the primary color */
118 int maxInput
, maxGeneric
; /**< max input index found */
119 int tmp0
, colorTemp
; /**< temp registers */
120 boolean firstInstruction
;
125 * TGSI declaration transform callback.
126 * Look for two free temp regs and available input reg for new texcoords.
129 aa_transform_decl(struct tgsi_transform_context
*ctx
,
130 struct tgsi_full_declaration
*decl
)
132 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
134 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
135 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
&&
136 decl
->Semantic
.SemanticIndex
== 0) {
137 aactx
->colorOutput
= decl
->DeclarationRange
.First
;
139 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
140 if ((int) decl
->DeclarationRange
.Last
> aactx
->maxInput
)
141 aactx
->maxInput
= decl
->DeclarationRange
.Last
;
142 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
&&
143 (int) decl
->Semantic
.SemanticIndex
> aactx
->maxGeneric
) {
144 aactx
->maxGeneric
= decl
->Semantic
.SemanticIndex
;
147 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
149 for (i
= decl
->DeclarationRange
.First
;
150 i
<= decl
->DeclarationRange
.Last
; i
++) {
151 aactx
->tempsUsed
|= (1 << i
);
155 ctx
->emit_declaration(ctx
, decl
);
160 * TGSI instruction transform callback.
161 * Replace writes to result.color w/ a temp reg.
162 * Upon END instruction, insert texture sampling code for antialiasing.
165 aa_transform_inst(struct tgsi_transform_context
*ctx
,
166 struct tgsi_full_instruction
*inst
)
168 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
169 struct tgsi_full_instruction newInst
;
171 if (aactx
->firstInstruction
) {
172 /* emit our new declarations before the first instruction */
174 struct tgsi_full_declaration decl
;
175 const int texInput
= aactx
->maxInput
+ 1;
179 /* find two free temp regs */
180 for (i
= 0; i
< 32; i
++) {
181 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
182 /* found a free temp */
185 else if (aactx
->colorTemp
< 0)
186 aactx
->colorTemp
= i
;
192 assert(aactx
->colorTemp
!= aactx
->tmp0
);
196 /* declare new generic input/texcoord */
197 decl
= tgsi_default_full_declaration();
198 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
199 /* XXX this could be linear... */
200 decl
.Declaration
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
201 decl
.Declaration
.Semantic
= 1;
202 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
203 decl
.Semantic
.SemanticIndex
= aactx
->maxGeneric
+ 1;
204 decl
.DeclarationRange
.First
=
205 decl
.DeclarationRange
.Last
= texInput
;
206 ctx
->emit_declaration(ctx
, &decl
);
208 /* declare new temp regs */
209 decl
= tgsi_default_full_declaration();
210 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
211 decl
.DeclarationRange
.First
=
212 decl
.DeclarationRange
.Last
= tmp0
;
213 ctx
->emit_declaration(ctx
, &decl
);
215 decl
= tgsi_default_full_declaration();
216 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
217 decl
.DeclarationRange
.First
=
218 decl
.DeclarationRange
.Last
= aactx
->colorTemp
;
219 ctx
->emit_declaration(ctx
, &decl
);
221 aactx
->firstInstruction
= FALSE
;
225 * Emit code to compute fragment coverage, kill if outside point radius
228 * t0.x = distance of fragment from center point
229 * t0.y = boolean, is t0.x > 1.0, also misc temp usage
230 * t0.z = temporary for computing 1/(1-k) value
231 * t0.w = final coverage value
234 /* MUL t0.xy, tex, tex; # compute x^2, y^2 */
235 newInst
= tgsi_default_full_instruction();
236 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
237 newInst
.Instruction
.NumDstRegs
= 1;
238 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
239 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
240 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XY
;
241 newInst
.Instruction
.NumSrcRegs
= 2;
242 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
243 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
244 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
245 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
246 ctx
->emit_instruction(ctx
, &newInst
);
248 /* ADD t0.x, t0.x, t0.y; # x^2 + y^2 */
249 newInst
= tgsi_default_full_instruction();
250 newInst
.Instruction
.Opcode
= TGSI_OPCODE_ADD
;
251 newInst
.Instruction
.NumDstRegs
= 1;
252 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
253 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
254 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
255 newInst
.Instruction
.NumSrcRegs
= 2;
256 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
257 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
258 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_X
;
259 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
260 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
261 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
262 ctx
->emit_instruction(ctx
, &newInst
);
264 #if NORMALIZE /* OPTIONAL normalization of length */
265 /* RSQ t0.x, t0.x; */
266 newInst
= tgsi_default_full_instruction();
267 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
268 newInst
.Instruction
.NumDstRegs
= 1;
269 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
270 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
271 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
272 newInst
.Instruction
.NumSrcRegs
= 1;
273 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
274 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
275 ctx
->emit_instruction(ctx
, &newInst
);
277 /* RCP t0.x, t0.x; */
278 newInst
= tgsi_default_full_instruction();
279 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
280 newInst
.Instruction
.NumDstRegs
= 1;
281 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
282 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
283 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
284 newInst
.Instruction
.NumSrcRegs
= 1;
285 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
286 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
287 ctx
->emit_instruction(ctx
, &newInst
);
290 /* SGT t0.y, t0.xxxx, t0.wwww; # bool b = d > 1 (NOTE t0.w == 1) */
291 newInst
= tgsi_default_full_instruction();
292 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SGT
;
293 newInst
.Instruction
.NumDstRegs
= 1;
294 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
295 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
296 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
297 newInst
.Instruction
.NumSrcRegs
= 2;
298 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
299 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
300 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
301 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
302 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
303 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
304 ctx
->emit_instruction(ctx
, &newInst
);
306 /* KIL -tmp0.yyyy; # if -tmp0.y < 0, KILL */
307 newInst
= tgsi_default_full_instruction();
308 newInst
.Instruction
.Opcode
= TGSI_OPCODE_KIL
;
309 newInst
.Instruction
.NumDstRegs
= 0;
310 newInst
.Instruction
.NumSrcRegs
= 1;
311 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
312 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
313 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
314 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
315 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
316 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
317 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
318 ctx
->emit_instruction(ctx
, &newInst
);
321 /* compute coverage factor = (1-d)/(1-k) */
323 /* SUB t0.z, tex.w, tex.z; # m = 1 - k */
324 newInst
= tgsi_default_full_instruction();
325 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
326 newInst
.Instruction
.NumDstRegs
= 1;
327 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
328 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
329 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
330 newInst
.Instruction
.NumSrcRegs
= 2;
331 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
332 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
333 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
334 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
335 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
336 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Z
;
337 ctx
->emit_instruction(ctx
, &newInst
);
339 /* RCP t0.z, t0.z; # t0.z = 1 / m */
340 newInst
= tgsi_default_full_instruction();
341 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
342 newInst
.Instruction
.NumDstRegs
= 1;
343 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
344 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
345 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
346 newInst
.Instruction
.NumSrcRegs
= 1;
347 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
348 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
349 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Z
;
350 ctx
->emit_instruction(ctx
, &newInst
);
352 /* SUB t0.y, 1, t0.x; # d = 1 - d */
353 newInst
= tgsi_default_full_instruction();
354 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
355 newInst
.Instruction
.NumDstRegs
= 1;
356 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
357 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
358 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
359 newInst
.Instruction
.NumSrcRegs
= 2;
360 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
361 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
362 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
363 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
364 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
365 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
366 ctx
->emit_instruction(ctx
, &newInst
);
368 /* MUL t0.w, t0.y, t0.z; # coverage = d * m */
369 newInst
= tgsi_default_full_instruction();
370 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
371 newInst
.Instruction
.NumDstRegs
= 1;
372 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
373 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
374 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
375 newInst
.Instruction
.NumSrcRegs
= 2;
376 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
377 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
378 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
379 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
380 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
381 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Z
;
382 ctx
->emit_instruction(ctx
, &newInst
);
384 /* SLE t0.y, t0.x, tex.z; # bool b = distance <= k */
385 newInst
= tgsi_default_full_instruction();
386 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SLE
;
387 newInst
.Instruction
.NumDstRegs
= 1;
388 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
389 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
390 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
391 newInst
.Instruction
.NumSrcRegs
= 2;
392 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
393 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
394 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
395 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
396 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
397 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Z
;
398 ctx
->emit_instruction(ctx
, &newInst
);
400 /* CMP t0.w, -t0.y, tex.w, t0.w;
401 * # if -t0.y < 0 then
406 newInst
= tgsi_default_full_instruction();
407 newInst
.Instruction
.Opcode
= TGSI_OPCODE_CMP
;
408 newInst
.Instruction
.NumDstRegs
= 1;
409 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
410 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
411 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
412 newInst
.Instruction
.NumSrcRegs
= 3;
413 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
414 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
415 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
416 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
417 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
418 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
419 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
420 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
421 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
422 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
423 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
424 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
425 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
426 newInst
.FullSrcRegisters
[2].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
427 newInst
.FullSrcRegisters
[2].SrcRegister
.Index
= tmp0
;
428 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
429 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
430 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
431 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
432 ctx
->emit_instruction(ctx
, &newInst
);
436 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
437 /* add alpha modulation code at tail of program */
439 /* MOV result.color.xyz, colorTemp; */
440 newInst
= tgsi_default_full_instruction();
441 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
442 newInst
.Instruction
.NumDstRegs
= 1;
443 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
444 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
445 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
446 newInst
.Instruction
.NumSrcRegs
= 1;
447 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
448 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
449 ctx
->emit_instruction(ctx
, &newInst
);
451 /* MUL result.color.w, colorTemp, tmp0.w; */
452 newInst
= tgsi_default_full_instruction();
453 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
454 newInst
.Instruction
.NumDstRegs
= 1;
455 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
456 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
457 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
458 newInst
.Instruction
.NumSrcRegs
= 2;
459 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
460 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
461 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
462 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->tmp0
;
463 ctx
->emit_instruction(ctx
, &newInst
);
466 /* Not an END instruction.
467 * Look for writes to result.color and replace with colorTemp reg.
471 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
472 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
473 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
&&
474 dst
->DstRegister
.Index
== aactx
->colorOutput
) {
475 dst
->DstRegister
.File
= TGSI_FILE_TEMPORARY
;
476 dst
->DstRegister
.Index
= aactx
->colorTemp
;
481 ctx
->emit_instruction(ctx
, inst
);
486 * Generate the frag shader we'll use for drawing AA points.
487 * This will be the user's shader plus some texture/modulate instructions.
490 generate_aapoint_fs(struct aapoint_stage
*aapoint
)
492 const struct pipe_shader_state
*orig_fs
= &aapoint
->fs
->state
;
493 struct pipe_shader_state aapoint_fs
;
494 struct aa_transform_context transform
;
498 aapoint_fs
= *orig_fs
; /* copy to init */
499 aapoint_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
500 if (aapoint_fs
.tokens
== NULL
)
503 memset(&transform
, 0, sizeof(transform
));
504 transform
.colorOutput
= -1;
505 transform
.maxInput
= -1;
506 transform
.maxGeneric
= -1;
507 transform
.colorTemp
= -1;
509 transform
.firstInstruction
= TRUE
;
510 transform
.base
.transform_instruction
= aa_transform_inst
;
511 transform
.base
.transform_declaration
= aa_transform_decl
;
513 tgsi_transform_shader(orig_fs
->tokens
,
514 (struct tgsi_token
*) aapoint_fs
.tokens
,
515 MAX
, &transform
.base
);
518 printf("draw_aapoint, orig shader:\n");
519 tgsi_dump(orig_fs
->tokens
, 0);
520 printf("draw_aapoint, new shader:\n");
521 tgsi_dump(aapoint_fs
.tokens
, 0);
524 aapoint
->fs
->aapoint_fs
525 = aapoint
->driver_create_fs_state(aapoint
->pipe
, &aapoint_fs
);
526 if (aapoint
->fs
->aapoint_fs
== NULL
)
529 aapoint
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
536 * When we're about to draw our first AA point in a batch, this function is
537 * called to tell the driver to bind our modified fragment shader.
540 bind_aapoint_fragment_shader(struct aapoint_stage
*aapoint
)
542 struct draw_context
*draw
= aapoint
->stage
.draw
;
544 if (!aapoint
->fs
->aapoint_fs
&&
545 !generate_aapoint_fs(aapoint
))
548 draw
->suspend_flushing
= TRUE
;
549 aapoint
->driver_bind_fs_state(aapoint
->pipe
, aapoint
->fs
->aapoint_fs
);
550 draw
->suspend_flushing
= FALSE
;
557 static INLINE
struct aapoint_stage
*
558 aapoint_stage( struct draw_stage
*stage
)
560 return (struct aapoint_stage
*) stage
;
567 * Draw an AA point by drawing a quad.
570 aapoint_point(struct draw_stage
*stage
, struct prim_header
*header
)
572 const struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
573 struct prim_header tri
;
574 struct vertex_header
*v
[4];
575 uint texPos
= aapoint
->tex_slot
;
576 uint pos_slot
= aapoint
->pos_slot
;
577 float radius
, *pos
, *tex
;
581 if (aapoint
->psize_slot
>= 0) {
582 radius
= 0.5f
* header
->v
[0]->data
[aapoint
->psize_slot
][0];
585 radius
= aapoint
->radius
;
589 * Note: the texcoords (generic attrib, really) we use are special:
590 * The S and T components simply vary from -1 to +1.
591 * The R component is k, below.
592 * The Q component is 1.0 and will used as a handy constant in the
597 * k is the threshold distance from the point's center at which
598 * we begin alpha attenuation (the coverage value).
599 * Operating within a unit circle, we'll compute the fragment's
600 * distance 'd' from the center point using the texcoords.
604 * compute coverage in [0,1] proportional to d in [k, 1].
606 * coverage = 1.0; // full coverage
609 * Note: the ELSEIF and ELSE clauses are actually implemented with CMP to
610 * avoid using IF/ELSE/ENDIF TGSI opcodes.
615 k
= 1.0f
- 2.0f
* k
+ k
* k
;
617 k
= 1.0f
- 1.0f
/ radius
;
620 /* allocate/dup new verts */
621 for (i
= 0; i
< 4; i
++) {
622 v
[i
] = dup_vert(stage
, header
->v
[0], i
);
626 pos
= v
[0]->data
[pos_slot
];
630 pos
= v
[1]->data
[pos_slot
];
634 pos
= v
[2]->data
[pos_slot
];
638 pos
= v
[3]->data
[pos_slot
];
643 tex
= v
[0]->data
[texPos
];
644 ASSIGN_4V(tex
, -1, -1, k
, 1);
646 tex
= v
[1]->data
[texPos
];
647 ASSIGN_4V(tex
, 1, -1, k
, 1);
649 tex
= v
[2]->data
[texPos
];
650 ASSIGN_4V(tex
, 1, 1, k
, 1);
652 tex
= v
[3]->data
[texPos
];
653 ASSIGN_4V(tex
, -1, 1, k
, 1);
655 /* emit 2 tris for the quad strip */
659 stage
->next
->tri( stage
->next
, &tri
);
664 stage
->next
->tri( stage
->next
, &tri
);
669 aapoint_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
671 auto struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
672 struct draw_context
*draw
= stage
->draw
;
674 assert(draw
->rasterizer
->point_smooth
);
676 if (draw
->rasterizer
->point_size
<= 2.0)
677 aapoint
->radius
= 1.0;
679 aapoint
->radius
= 0.5f
* draw
->rasterizer
->point_size
;
682 * Bind (generate) our fragprog.
684 bind_aapoint_fragment_shader(aapoint
);
686 /* update vertex attrib info */
687 aapoint
->tex_slot
= draw
->vs
.num_vs_outputs
;
688 assert(aapoint
->tex_slot
> 0); /* output[0] is vertex pos */
690 aapoint
->pos_slot
= draw
->vs
.position_output
;
692 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
693 draw
->extra_vp_outputs
.semantic_index
= aapoint
->fs
->generic_attrib
;
694 draw
->extra_vp_outputs
.slot
= aapoint
->tex_slot
;
696 /* find psize slot in post-transform vertex */
697 aapoint
->psize_slot
= -1;
698 if (draw
->rasterizer
->point_size_per_vertex
) {
699 /* find PSIZ vertex output */
700 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
702 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
703 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
704 aapoint
->psize_slot
= i
;
710 /* now really draw first point */
711 stage
->point
= aapoint_point
;
712 stage
->point(stage
, header
);
717 aapoint_flush(struct draw_stage
*stage
, unsigned flags
)
719 struct draw_context
*draw
= stage
->draw
;
720 struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
721 struct pipe_context
*pipe
= aapoint
->pipe
;
723 stage
->point
= aapoint_first_point
;
724 stage
->next
->flush( stage
->next
, flags
);
726 /* restore original frag shader */
727 draw
->suspend_flushing
= TRUE
;
728 aapoint
->driver_bind_fs_state(pipe
, aapoint
->fs
->driver_fs
);
729 draw
->suspend_flushing
= FALSE
;
731 draw
->extra_vp_outputs
.slot
= 0;
736 aapoint_reset_stipple_counter(struct draw_stage
*stage
)
738 stage
->next
->reset_stipple_counter( stage
->next
);
743 aapoint_destroy(struct draw_stage
*stage
)
745 draw_free_temp_verts( stage
);
750 static struct aapoint_stage
*
751 draw_aapoint_stage(struct draw_context
*draw
)
753 struct aapoint_stage
*aapoint
= CALLOC_STRUCT(aapoint_stage
);
757 if (!draw_alloc_temp_verts( &aapoint
->stage
, 4 ))
760 aapoint
->stage
.draw
= draw
;
761 aapoint
->stage
.next
= NULL
;
762 aapoint
->stage
.point
= aapoint_first_point
;
763 aapoint
->stage
.line
= draw_pipe_passthrough_line
;
764 aapoint
->stage
.tri
= draw_pipe_passthrough_tri
;
765 aapoint
->stage
.flush
= aapoint_flush
;
766 aapoint
->stage
.reset_stipple_counter
= aapoint_reset_stipple_counter
;
767 aapoint
->stage
.destroy
= aapoint_destroy
;
773 aapoint_destroy(&aapoint
->stage
);
780 static struct aapoint_stage
*
781 aapoint_stage_from_pipe(struct pipe_context
*pipe
)
783 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
784 return aapoint_stage(draw
->pipeline
.aapoint
);
789 * This function overrides the driver's create_fs_state() function and
790 * will typically be called by the state tracker.
793 aapoint_create_fs_state(struct pipe_context
*pipe
,
794 const struct pipe_shader_state
*fs
)
796 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
797 struct aapoint_fragment_shader
*aafs
= CALLOC_STRUCT(aapoint_fragment_shader
);
804 aafs
->driver_fs
= aapoint
->driver_create_fs_state(aapoint
->pipe
, fs
);
811 aapoint_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
813 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
814 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
818 aapoint
->driver_bind_fs_state(aapoint
->pipe
,
819 (aafs
? aafs
->driver_fs
: NULL
));
824 aapoint_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
826 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
827 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
829 aapoint
->driver_delete_fs_state(aapoint
->pipe
, aafs
->driver_fs
);
835 * Called by drivers that want to install this AA point prim stage
836 * into the draw module's pipeline. This will not be used if the
837 * hardware has native support for AA points.
840 draw_install_aapoint_stage(struct draw_context
*draw
,
841 struct pipe_context
*pipe
)
843 struct aapoint_stage
*aapoint
;
845 pipe
->draw
= (void *) draw
;
848 * Create / install AA point drawing / prim stage
850 aapoint
= draw_aapoint_stage( draw
);
854 aapoint
->pipe
= pipe
;
856 /* save original driver functions */
857 aapoint
->driver_create_fs_state
= pipe
->create_fs_state
;
858 aapoint
->driver_bind_fs_state
= pipe
->bind_fs_state
;
859 aapoint
->driver_delete_fs_state
= pipe
->delete_fs_state
;
861 /* override the driver's functions */
862 pipe
->create_fs_state
= aapoint_create_fs_state
;
863 pipe
->bind_fs_state
= aapoint_bind_fs_state
;
864 pipe
->delete_fs_state
= aapoint_delete_fs_state
;
866 draw
->pipeline
.aapoint
= &aapoint
->stage
;
872 aapoint
->stage
.destroy( &aapoint
->stage
);