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_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_shader_tokens.h"
45 #include "tgsi/tgsi_transform.h"
46 #include "tgsi/tgsi_dump.h"
48 #include "util/u_math.h"
49 #include "util/u_memory.h"
51 #include "draw_context.h"
53 #include "draw_pipe.h"
57 * Enabling NORMALIZE might give _slightly_ better results.
58 * Basically, it controls whether we compute distance as d=sqrt(x*x+y*y) or
59 * d=x*x+y*y. Since we're working with a unit circle, the later seems
60 * close enough and saves some costly instructions.
66 * Subclass of pipe_shader_state to carry extra fragment shader info.
68 struct aapoint_fragment_shader
70 struct pipe_shader_state state
;
71 void *driver_fs
; /**< the regular shader */
72 void *aapoint_fs
; /**< the aa point-augmented shader */
73 int generic_attrib
; /**< The generic input attrib/texcoord we'll use */
78 * Subclass of draw_stage
82 struct draw_stage stage
;
87 /** this is the vertex attrib slot for the new texcoords */
92 * Currently bound state
94 struct aapoint_fragment_shader
*fs
;
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 struct pipe_context
*pipe
;
110 * Subclass of tgsi_transform_context, used for transforming the
111 * user's fragment shader to add the special AA instructions.
113 struct aa_transform_context
{
114 struct tgsi_transform_context base
;
115 uint tempsUsed
; /**< bitmask */
116 int colorOutput
; /**< which output is the primary color */
117 int maxInput
, maxGeneric
; /**< max input index found */
118 int tmp0
, colorTemp
; /**< temp registers */
119 boolean firstInstruction
;
124 * TGSI declaration transform callback.
125 * Look for two free temp regs and available input reg for new texcoords.
128 aa_transform_decl(struct tgsi_transform_context
*ctx
,
129 struct tgsi_full_declaration
*decl
)
131 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
133 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
134 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
&&
135 decl
->Semantic
.SemanticIndex
== 0) {
136 aactx
->colorOutput
= decl
->DeclarationRange
.First
;
138 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
139 if ((int) decl
->DeclarationRange
.Last
> aactx
->maxInput
)
140 aactx
->maxInput
= decl
->DeclarationRange
.Last
;
141 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
&&
142 (int) decl
->Semantic
.SemanticIndex
> aactx
->maxGeneric
) {
143 aactx
->maxGeneric
= decl
->Semantic
.SemanticIndex
;
146 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
148 for (i
= decl
->DeclarationRange
.First
;
149 i
<= decl
->DeclarationRange
.Last
; i
++) {
150 aactx
->tempsUsed
|= (1 << i
);
154 ctx
->emit_declaration(ctx
, decl
);
159 * TGSI instruction transform callback.
160 * Replace writes to result.color w/ a temp reg.
161 * Upon END instruction, insert texture sampling code for antialiasing.
164 aa_transform_inst(struct tgsi_transform_context
*ctx
,
165 struct tgsi_full_instruction
*inst
)
167 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
168 struct tgsi_full_instruction newInst
;
170 if (aactx
->firstInstruction
) {
171 /* emit our new declarations before the first instruction */
173 struct tgsi_full_declaration decl
;
174 const int texInput
= aactx
->maxInput
+ 1;
178 /* find two free temp regs */
179 for (i
= 0; i
< 32; i
++) {
180 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
181 /* found a free temp */
184 else if (aactx
->colorTemp
< 0)
185 aactx
->colorTemp
= i
;
191 assert(aactx
->colorTemp
!= aactx
->tmp0
);
195 /* declare new generic input/texcoord */
196 decl
= tgsi_default_full_declaration();
197 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
198 /* XXX this could be linear... */
199 decl
.Declaration
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
200 decl
.Declaration
.Semantic
= 1;
201 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
202 decl
.Semantic
.SemanticIndex
= aactx
->maxGeneric
+ 1;
203 decl
.DeclarationRange
.First
=
204 decl
.DeclarationRange
.Last
= texInput
;
205 ctx
->emit_declaration(ctx
, &decl
);
207 /* declare new temp regs */
208 decl
= tgsi_default_full_declaration();
209 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
210 decl
.DeclarationRange
.First
=
211 decl
.DeclarationRange
.Last
= tmp0
;
212 ctx
->emit_declaration(ctx
, &decl
);
214 decl
= tgsi_default_full_declaration();
215 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
216 decl
.DeclarationRange
.First
=
217 decl
.DeclarationRange
.Last
= aactx
->colorTemp
;
218 ctx
->emit_declaration(ctx
, &decl
);
220 aactx
->firstInstruction
= FALSE
;
224 * Emit code to compute fragment coverage, kill if outside point radius
227 * t0.x = distance of fragment from center point
228 * t0.y = boolean, is t0.x > 1.0, also misc temp usage
229 * t0.z = temporary for computing 1/(1-k) value
230 * t0.w = final coverage value
233 /* MUL t0.xy, tex, tex; # compute x^2, y^2 */
234 newInst
= tgsi_default_full_instruction();
235 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
236 newInst
.Instruction
.NumDstRegs
= 1;
237 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
238 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
239 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XY
;
240 newInst
.Instruction
.NumSrcRegs
= 2;
241 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
242 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
243 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
244 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
245 ctx
->emit_instruction(ctx
, &newInst
);
247 /* ADD t0.x, t0.x, t0.y; # x^2 + y^2 */
248 newInst
= tgsi_default_full_instruction();
249 newInst
.Instruction
.Opcode
= TGSI_OPCODE_ADD
;
250 newInst
.Instruction
.NumDstRegs
= 1;
251 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
252 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
253 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
254 newInst
.Instruction
.NumSrcRegs
= 2;
255 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
256 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
257 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_X
;
258 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
259 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
260 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
261 ctx
->emit_instruction(ctx
, &newInst
);
263 #if NORMALIZE /* OPTIONAL normalization of length */
264 /* RSQ t0.x, t0.x; */
265 newInst
= tgsi_default_full_instruction();
266 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
267 newInst
.Instruction
.NumDstRegs
= 1;
268 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
269 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
270 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
271 newInst
.Instruction
.NumSrcRegs
= 1;
272 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
273 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
274 ctx
->emit_instruction(ctx
, &newInst
);
276 /* RCP t0.x, t0.x; */
277 newInst
= tgsi_default_full_instruction();
278 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
279 newInst
.Instruction
.NumDstRegs
= 1;
280 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
281 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
282 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
283 newInst
.Instruction
.NumSrcRegs
= 1;
284 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
285 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
286 ctx
->emit_instruction(ctx
, &newInst
);
289 /* SGT t0.y, t0.xxxx, tex.wwww; # bool b = d > 1 (NOTE tex.w == 1) */
290 newInst
= tgsi_default_full_instruction();
291 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SGT
;
292 newInst
.Instruction
.NumDstRegs
= 1;
293 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
294 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
295 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
296 newInst
.Instruction
.NumSrcRegs
= 2;
297 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
298 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
299 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
300 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
301 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
302 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
303 ctx
->emit_instruction(ctx
, &newInst
);
305 /* KIL -tmp0.yyyy; # if -tmp0.y < 0, KILL */
306 newInst
= tgsi_default_full_instruction();
307 newInst
.Instruction
.Opcode
= TGSI_OPCODE_KIL
;
308 newInst
.Instruction
.NumDstRegs
= 0;
309 newInst
.Instruction
.NumSrcRegs
= 1;
310 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
311 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
312 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
313 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
314 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
315 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
316 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
317 ctx
->emit_instruction(ctx
, &newInst
);
320 /* compute coverage factor = (1-d)/(1-k) */
322 /* SUB t0.z, tex.w, tex.z; # m = 1 - k */
323 newInst
= tgsi_default_full_instruction();
324 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
325 newInst
.Instruction
.NumDstRegs
= 1;
326 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
327 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
328 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
329 newInst
.Instruction
.NumSrcRegs
= 2;
330 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
331 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
332 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
333 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
334 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
335 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Z
;
336 ctx
->emit_instruction(ctx
, &newInst
);
338 /* RCP t0.z, t0.z; # t0.z = 1 / m */
339 newInst
= tgsi_default_full_instruction();
340 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
341 newInst
.Instruction
.NumDstRegs
= 1;
342 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
343 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
344 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
345 newInst
.Instruction
.NumSrcRegs
= 1;
346 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
347 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
348 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Z
;
349 ctx
->emit_instruction(ctx
, &newInst
);
351 /* SUB t0.y, 1, t0.x; # d = 1 - d */
352 newInst
= tgsi_default_full_instruction();
353 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
354 newInst
.Instruction
.NumDstRegs
= 1;
355 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
356 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
357 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
358 newInst
.Instruction
.NumSrcRegs
= 2;
359 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
360 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
361 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
362 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
363 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
364 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
365 ctx
->emit_instruction(ctx
, &newInst
);
367 /* MUL t0.w, t0.y, t0.z; # coverage = d * m */
368 newInst
= tgsi_default_full_instruction();
369 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
370 newInst
.Instruction
.NumDstRegs
= 1;
371 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
372 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
373 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
374 newInst
.Instruction
.NumSrcRegs
= 2;
375 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
376 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
377 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
378 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
379 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
380 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Z
;
381 ctx
->emit_instruction(ctx
, &newInst
);
383 /* SLE t0.y, t0.x, tex.z; # bool b = distance <= k */
384 newInst
= tgsi_default_full_instruction();
385 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SLE
;
386 newInst
.Instruction
.NumDstRegs
= 1;
387 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
388 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
389 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
390 newInst
.Instruction
.NumSrcRegs
= 2;
391 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
392 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
393 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
394 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
395 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
396 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Z
;
397 ctx
->emit_instruction(ctx
, &newInst
);
399 /* CMP t0.w, -t0.y, tex.w, t0.w;
400 * # if -t0.y < 0 then
405 newInst
= tgsi_default_full_instruction();
406 newInst
.Instruction
.Opcode
= TGSI_OPCODE_CMP
;
407 newInst
.Instruction
.NumDstRegs
= 1;
408 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
409 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
410 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
411 newInst
.Instruction
.NumSrcRegs
= 3;
412 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
413 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
414 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
415 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
416 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
417 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
418 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
419 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
420 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
421 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
422 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
423 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
424 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
425 newInst
.FullSrcRegisters
[2].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
426 newInst
.FullSrcRegisters
[2].SrcRegister
.Index
= tmp0
;
427 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
428 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
429 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
430 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
431 ctx
->emit_instruction(ctx
, &newInst
);
435 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
436 /* add alpha modulation code at tail of program */
438 /* MOV result.color.xyz, colorTemp; */
439 newInst
= tgsi_default_full_instruction();
440 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
441 newInst
.Instruction
.NumDstRegs
= 1;
442 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
443 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
444 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
445 newInst
.Instruction
.NumSrcRegs
= 1;
446 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
447 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
448 ctx
->emit_instruction(ctx
, &newInst
);
450 /* MUL result.color.w, colorTemp, tmp0.w; */
451 newInst
= tgsi_default_full_instruction();
452 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
453 newInst
.Instruction
.NumDstRegs
= 1;
454 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
455 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
456 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
457 newInst
.Instruction
.NumSrcRegs
= 2;
458 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
459 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
460 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
461 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->tmp0
;
462 ctx
->emit_instruction(ctx
, &newInst
);
465 /* Not an END instruction.
466 * Look for writes to result.color and replace with colorTemp reg.
470 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
471 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
472 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
&&
473 dst
->DstRegister
.Index
== aactx
->colorOutput
) {
474 dst
->DstRegister
.File
= TGSI_FILE_TEMPORARY
;
475 dst
->DstRegister
.Index
= aactx
->colorTemp
;
480 ctx
->emit_instruction(ctx
, inst
);
485 * Generate the frag shader we'll use for drawing AA points.
486 * This will be the user's shader plus some texture/modulate instructions.
489 generate_aapoint_fs(struct aapoint_stage
*aapoint
)
491 const struct pipe_shader_state
*orig_fs
= &aapoint
->fs
->state
;
492 struct pipe_shader_state aapoint_fs
;
493 struct aa_transform_context transform
;
497 aapoint_fs
= *orig_fs
; /* copy to init */
498 aapoint_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
499 if (aapoint_fs
.tokens
== NULL
)
502 memset(&transform
, 0, sizeof(transform
));
503 transform
.colorOutput
= -1;
504 transform
.maxInput
= -1;
505 transform
.maxGeneric
= -1;
506 transform
.colorTemp
= -1;
508 transform
.firstInstruction
= TRUE
;
509 transform
.base
.transform_instruction
= aa_transform_inst
;
510 transform
.base
.transform_declaration
= aa_transform_decl
;
512 tgsi_transform_shader(orig_fs
->tokens
,
513 (struct tgsi_token
*) aapoint_fs
.tokens
,
514 MAX
, &transform
.base
);
517 printf("draw_aapoint, orig shader:\n");
518 tgsi_dump(orig_fs
->tokens
, 0);
519 printf("draw_aapoint, new shader:\n");
520 tgsi_dump(aapoint_fs
.tokens
, 0);
523 aapoint
->fs
->aapoint_fs
524 = aapoint
->driver_create_fs_state(aapoint
->pipe
, &aapoint_fs
);
525 if (aapoint
->fs
->aapoint_fs
== NULL
)
528 aapoint
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
529 FREE((void *)aapoint_fs
.tokens
);
533 FREE((void *)aapoint_fs
.tokens
);
539 * When we're about to draw our first AA point in a batch, this function is
540 * called to tell the driver to bind our modified fragment shader.
543 bind_aapoint_fragment_shader(struct aapoint_stage
*aapoint
)
545 struct draw_context
*draw
= aapoint
->stage
.draw
;
547 if (!aapoint
->fs
->aapoint_fs
&&
548 !generate_aapoint_fs(aapoint
))
551 draw
->suspend_flushing
= TRUE
;
552 aapoint
->driver_bind_fs_state(aapoint
->pipe
, aapoint
->fs
->aapoint_fs
);
553 draw
->suspend_flushing
= FALSE
;
560 static INLINE
struct aapoint_stage
*
561 aapoint_stage( struct draw_stage
*stage
)
563 return (struct aapoint_stage
*) stage
;
570 * Draw an AA point by drawing a quad.
573 aapoint_point(struct draw_stage
*stage
, struct prim_header
*header
)
575 const struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
576 struct prim_header tri
;
577 struct vertex_header
*v
[4];
578 uint texPos
= aapoint
->tex_slot
;
579 uint pos_slot
= aapoint
->pos_slot
;
580 float radius
, *pos
, *tex
;
584 if (aapoint
->psize_slot
>= 0) {
585 radius
= 0.5f
* header
->v
[0]->data
[aapoint
->psize_slot
][0];
588 radius
= aapoint
->radius
;
592 * Note: the texcoords (generic attrib, really) we use are special:
593 * The S and T components simply vary from -1 to +1.
594 * The R component is k, below.
595 * The Q component is 1.0 and will used as a handy constant in the
600 * k is the threshold distance from the point's center at which
601 * we begin alpha attenuation (the coverage value).
602 * Operating within a unit circle, we'll compute the fragment's
603 * distance 'd' from the center point using the texcoords.
607 * compute coverage in [0,1] proportional to d in [k, 1].
609 * coverage = 1.0; // full coverage
612 * Note: the ELSEIF and ELSE clauses are actually implemented with CMP to
613 * avoid using IF/ELSE/ENDIF TGSI opcodes.
618 k
= 1.0f
- 2.0f
* k
+ k
* k
;
620 k
= 1.0f
- 1.0f
/ radius
;
623 /* allocate/dup new verts */
624 for (i
= 0; i
< 4; i
++) {
625 v
[i
] = dup_vert(stage
, header
->v
[0], i
);
629 pos
= v
[0]->data
[pos_slot
];
633 pos
= v
[1]->data
[pos_slot
];
637 pos
= v
[2]->data
[pos_slot
];
641 pos
= v
[3]->data
[pos_slot
];
646 tex
= v
[0]->data
[texPos
];
647 ASSIGN_4V(tex
, -1, -1, k
, 1);
649 tex
= v
[1]->data
[texPos
];
650 ASSIGN_4V(tex
, 1, -1, k
, 1);
652 tex
= v
[2]->data
[texPos
];
653 ASSIGN_4V(tex
, 1, 1, k
, 1);
655 tex
= v
[3]->data
[texPos
];
656 ASSIGN_4V(tex
, -1, 1, k
, 1);
658 /* emit 2 tris for the quad strip */
662 stage
->next
->tri( stage
->next
, &tri
);
667 stage
->next
->tri( stage
->next
, &tri
);
672 aapoint_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
674 auto struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
675 struct draw_context
*draw
= stage
->draw
;
677 assert(draw
->rasterizer
->point_smooth
);
679 if (draw
->rasterizer
->point_size
<= 2.0)
680 aapoint
->radius
= 1.0;
682 aapoint
->radius
= 0.5f
* draw
->rasterizer
->point_size
;
685 * Bind (generate) our fragprog.
687 bind_aapoint_fragment_shader(aapoint
);
689 /* update vertex attrib info */
690 aapoint
->tex_slot
= draw
->vs
.num_vs_outputs
;
691 assert(aapoint
->tex_slot
> 0); /* output[0] is vertex pos */
693 aapoint
->pos_slot
= draw
->vs
.position_output
;
695 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
696 draw
->extra_vp_outputs
.semantic_index
= aapoint
->fs
->generic_attrib
;
697 draw
->extra_vp_outputs
.slot
= aapoint
->tex_slot
;
699 /* find psize slot in post-transform vertex */
700 aapoint
->psize_slot
= -1;
701 if (draw
->rasterizer
->point_size_per_vertex
) {
702 /* find PSIZ vertex output */
703 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
705 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
706 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
707 aapoint
->psize_slot
= i
;
713 /* now really draw first point */
714 stage
->point
= aapoint_point
;
715 stage
->point(stage
, header
);
720 aapoint_flush(struct draw_stage
*stage
, unsigned flags
)
722 struct draw_context
*draw
= stage
->draw
;
723 struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
724 struct pipe_context
*pipe
= aapoint
->pipe
;
726 stage
->point
= aapoint_first_point
;
727 stage
->next
->flush( stage
->next
, flags
);
729 /* restore original frag shader */
730 draw
->suspend_flushing
= TRUE
;
731 aapoint
->driver_bind_fs_state(pipe
, aapoint
->fs
->driver_fs
);
732 draw
->suspend_flushing
= FALSE
;
734 draw
->extra_vp_outputs
.slot
= 0;
739 aapoint_reset_stipple_counter(struct draw_stage
*stage
)
741 stage
->next
->reset_stipple_counter( stage
->next
);
746 aapoint_destroy(struct draw_stage
*stage
)
748 draw_free_temp_verts( stage
);
753 static struct aapoint_stage
*
754 draw_aapoint_stage(struct draw_context
*draw
)
756 struct aapoint_stage
*aapoint
= CALLOC_STRUCT(aapoint_stage
);
760 if (!draw_alloc_temp_verts( &aapoint
->stage
, 4 ))
763 aapoint
->stage
.draw
= draw
;
764 aapoint
->stage
.name
= "aapoint";
765 aapoint
->stage
.next
= NULL
;
766 aapoint
->stage
.point
= aapoint_first_point
;
767 aapoint
->stage
.line
= draw_pipe_passthrough_line
;
768 aapoint
->stage
.tri
= draw_pipe_passthrough_tri
;
769 aapoint
->stage
.flush
= aapoint_flush
;
770 aapoint
->stage
.reset_stipple_counter
= aapoint_reset_stipple_counter
;
771 aapoint
->stage
.destroy
= aapoint_destroy
;
777 aapoint_destroy(&aapoint
->stage
);
784 static struct aapoint_stage
*
785 aapoint_stage_from_pipe(struct pipe_context
*pipe
)
787 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
788 return aapoint_stage(draw
->pipeline
.aapoint
);
793 * This function overrides the driver's create_fs_state() function and
794 * will typically be called by the state tracker.
797 aapoint_create_fs_state(struct pipe_context
*pipe
,
798 const struct pipe_shader_state
*fs
)
800 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
801 struct aapoint_fragment_shader
*aafs
= CALLOC_STRUCT(aapoint_fragment_shader
);
808 aafs
->driver_fs
= aapoint
->driver_create_fs_state(aapoint
->pipe
, fs
);
815 aapoint_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
817 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
818 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
822 aapoint
->driver_bind_fs_state(aapoint
->pipe
,
823 (aafs
? aafs
->driver_fs
: NULL
));
828 aapoint_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
830 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
831 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
834 aapoint
->driver_delete_fs_state(aapoint
->pipe
, aafs
->driver_fs
);
836 if (aafs
->aapoint_fs
)
837 aapoint
->driver_delete_fs_state(aapoint
->pipe
, aafs
->aapoint_fs
);
844 * Called by drivers that want to install this AA point prim stage
845 * into the draw module's pipeline. This will not be used if the
846 * hardware has native support for AA points.
849 draw_install_aapoint_stage(struct draw_context
*draw
,
850 struct pipe_context
*pipe
)
852 struct aapoint_stage
*aapoint
;
854 pipe
->draw
= (void *) draw
;
857 * Create / install AA point drawing / prim stage
859 aapoint
= draw_aapoint_stage( draw
);
863 aapoint
->pipe
= pipe
;
865 /* save original driver functions */
866 aapoint
->driver_create_fs_state
= pipe
->create_fs_state
;
867 aapoint
->driver_bind_fs_state
= pipe
->bind_fs_state
;
868 aapoint
->driver_delete_fs_state
= pipe
->delete_fs_state
;
870 /* override the driver's functions */
871 pipe
->create_fs_state
= aapoint_create_fs_state
;
872 pipe
->bind_fs_state
= aapoint_bind_fs_state
;
873 pipe
->delete_fs_state
= aapoint_delete_fs_state
;
875 draw
->pipeline
.aapoint
= &aapoint
->stage
;
881 aapoint
->stage
.destroy( &aapoint
->stage
);