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_util.h"
42 #include "pipe/p_inlines.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_shader_tokens.h"
47 #include "tgsi/tgsi_transform.h"
48 #include "tgsi/tgsi_dump.h"
50 #include "draw_context.h"
52 #include "draw_pipe.h"
56 * Enabling NORMALIZE might give _slightly_ better results.
57 * Basically, it controls whether we compute distance as d=sqrt(x*x+y*y) or
58 * d=x*x+y*y. Since we're working with a unit circle, the later seems
59 * close enough and saves some costly instructions.
65 * Subclass of pipe_shader_state to carry extra fragment shader info.
67 struct aapoint_fragment_shader
69 struct pipe_shader_state state
;
70 void *driver_fs
; /**< the regular shader */
71 void *aapoint_fs
; /**< the aa point-augmented shader */
72 int generic_attrib
; /**< The generic input attrib/texcoord we'll use */
77 * Subclass of draw_stage
81 struct draw_stage stage
;
86 /** this is the vertex attrib slot for the new texcoords */
91 * Currently bound state
93 struct aapoint_fragment_shader
*fs
;
96 * Driver interface/override functions
98 void * (*driver_create_fs_state
)(struct pipe_context
*,
99 const struct pipe_shader_state
*);
100 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
101 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
103 struct pipe_context
*pipe
;
109 * Subclass of tgsi_transform_context, used for transforming the
110 * user's fragment shader to add the special AA instructions.
112 struct aa_transform_context
{
113 struct tgsi_transform_context base
;
114 uint tempsUsed
; /**< bitmask */
115 int colorOutput
; /**< which output is the primary color */
116 int maxInput
, maxGeneric
; /**< max input index found */
117 int tmp0
, colorTemp
; /**< temp registers */
118 boolean firstInstruction
;
123 * TGSI declaration transform callback.
124 * Look for two free temp regs and available input reg for new texcoords.
127 aa_transform_decl(struct tgsi_transform_context
*ctx
,
128 struct tgsi_full_declaration
*decl
)
130 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
132 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
133 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
&&
134 decl
->Semantic
.SemanticIndex
== 0) {
135 aactx
->colorOutput
= decl
->DeclarationRange
.First
;
137 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
138 if ((int) decl
->DeclarationRange
.Last
> aactx
->maxInput
)
139 aactx
->maxInput
= decl
->DeclarationRange
.Last
;
140 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
&&
141 (int) decl
->Semantic
.SemanticIndex
> aactx
->maxGeneric
) {
142 aactx
->maxGeneric
= decl
->Semantic
.SemanticIndex
;
145 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
147 for (i
= decl
->DeclarationRange
.First
;
148 i
<= decl
->DeclarationRange
.Last
; i
++) {
149 aactx
->tempsUsed
|= (1 << i
);
153 ctx
->emit_declaration(ctx
, decl
);
158 * TGSI instruction transform callback.
159 * Replace writes to result.color w/ a temp reg.
160 * Upon END instruction, insert texture sampling code for antialiasing.
163 aa_transform_inst(struct tgsi_transform_context
*ctx
,
164 struct tgsi_full_instruction
*inst
)
166 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
167 struct tgsi_full_instruction newInst
;
169 if (aactx
->firstInstruction
) {
170 /* emit our new declarations before the first instruction */
172 struct tgsi_full_declaration decl
;
173 const int texInput
= aactx
->maxInput
+ 1;
177 /* find two free temp regs */
178 for (i
= 0; i
< 32; i
++) {
179 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
180 /* found a free temp */
183 else if (aactx
->colorTemp
< 0)
184 aactx
->colorTemp
= i
;
190 assert(aactx
->colorTemp
!= aactx
->tmp0
);
194 /* declare new generic input/texcoord */
195 decl
= tgsi_default_full_declaration();
196 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
197 /* XXX this could be linear... */
198 decl
.Declaration
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
199 decl
.Declaration
.Semantic
= 1;
200 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
201 decl
.Semantic
.SemanticIndex
= aactx
->maxGeneric
+ 1;
202 decl
.DeclarationRange
.First
=
203 decl
.DeclarationRange
.Last
= texInput
;
204 ctx
->emit_declaration(ctx
, &decl
);
206 /* declare new temp regs */
207 decl
= tgsi_default_full_declaration();
208 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
209 decl
.DeclarationRange
.First
=
210 decl
.DeclarationRange
.Last
= tmp0
;
211 ctx
->emit_declaration(ctx
, &decl
);
213 decl
= tgsi_default_full_declaration();
214 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
215 decl
.DeclarationRange
.First
=
216 decl
.DeclarationRange
.Last
= aactx
->colorTemp
;
217 ctx
->emit_declaration(ctx
, &decl
);
219 aactx
->firstInstruction
= FALSE
;
223 * Emit code to compute fragment coverage, kill if outside point radius
226 * t0.x = distance of fragment from center point
227 * t0.y = boolean, is t0.x > 1.0, also misc temp usage
228 * t0.z = temporary for computing 1/(1-k) value
229 * t0.w = final coverage value
232 /* MUL t0.xy, tex, tex; # compute x^2, y^2 */
233 newInst
= tgsi_default_full_instruction();
234 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
235 newInst
.Instruction
.NumDstRegs
= 1;
236 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
237 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
238 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XY
;
239 newInst
.Instruction
.NumSrcRegs
= 2;
240 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
241 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
242 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
243 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
244 ctx
->emit_instruction(ctx
, &newInst
);
246 /* ADD t0.x, t0.x, t0.y; # x^2 + y^2 */
247 newInst
= tgsi_default_full_instruction();
248 newInst
.Instruction
.Opcode
= TGSI_OPCODE_ADD
;
249 newInst
.Instruction
.NumDstRegs
= 1;
250 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
251 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
252 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
253 newInst
.Instruction
.NumSrcRegs
= 2;
254 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
255 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
256 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_X
;
257 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
258 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
259 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
260 ctx
->emit_instruction(ctx
, &newInst
);
262 #if NORMALIZE /* OPTIONAL normalization of length */
263 /* RSQ t0.x, t0.x; */
264 newInst
= tgsi_default_full_instruction();
265 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
266 newInst
.Instruction
.NumDstRegs
= 1;
267 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
268 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
269 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
270 newInst
.Instruction
.NumSrcRegs
= 1;
271 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
272 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
273 ctx
->emit_instruction(ctx
, &newInst
);
275 /* RCP t0.x, t0.x; */
276 newInst
= tgsi_default_full_instruction();
277 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
278 newInst
.Instruction
.NumDstRegs
= 1;
279 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
280 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
281 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
282 newInst
.Instruction
.NumSrcRegs
= 1;
283 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
284 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
285 ctx
->emit_instruction(ctx
, &newInst
);
288 /* SGT t0.y, t0.xxxx, t0.wwww; # bool b = d > 1 (NOTE t0.w == 1) */
289 newInst
= tgsi_default_full_instruction();
290 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SGT
;
291 newInst
.Instruction
.NumDstRegs
= 1;
292 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
293 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
294 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
295 newInst
.Instruction
.NumSrcRegs
= 2;
296 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
297 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
298 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
299 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
300 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
301 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
302 ctx
->emit_instruction(ctx
, &newInst
);
304 /* KIL -tmp0.yyyy; # if -tmp0.y < 0, KILL */
305 newInst
= tgsi_default_full_instruction();
306 newInst
.Instruction
.Opcode
= TGSI_OPCODE_KIL
;
307 newInst
.Instruction
.NumDstRegs
= 0;
308 newInst
.Instruction
.NumSrcRegs
= 1;
309 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
310 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
311 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
312 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
313 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
314 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
315 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
316 ctx
->emit_instruction(ctx
, &newInst
);
319 /* compute coverage factor = (1-d)/(1-k) */
321 /* SUB t0.z, tex.w, tex.z; # m = 1 - k */
322 newInst
= tgsi_default_full_instruction();
323 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
324 newInst
.Instruction
.NumDstRegs
= 1;
325 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
326 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
327 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
328 newInst
.Instruction
.NumSrcRegs
= 2;
329 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
330 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
331 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
332 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
333 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
334 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Z
;
335 ctx
->emit_instruction(ctx
, &newInst
);
337 /* RCP t0.z, t0.z; # t0.z = 1 / m */
338 newInst
= tgsi_default_full_instruction();
339 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
340 newInst
.Instruction
.NumDstRegs
= 1;
341 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
342 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
343 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
344 newInst
.Instruction
.NumSrcRegs
= 1;
345 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
346 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
347 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Z
;
348 ctx
->emit_instruction(ctx
, &newInst
);
350 /* SUB t0.y, 1, t0.x; # d = 1 - d */
351 newInst
= tgsi_default_full_instruction();
352 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
353 newInst
.Instruction
.NumDstRegs
= 1;
354 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
355 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
356 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
357 newInst
.Instruction
.NumSrcRegs
= 2;
358 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
359 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
360 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
361 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
362 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
363 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
364 ctx
->emit_instruction(ctx
, &newInst
);
366 /* MUL t0.w, t0.y, t0.z; # coverage = d * m */
367 newInst
= tgsi_default_full_instruction();
368 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
369 newInst
.Instruction
.NumDstRegs
= 1;
370 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
371 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
372 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
373 newInst
.Instruction
.NumSrcRegs
= 2;
374 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
375 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
376 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
377 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
378 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
379 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Z
;
380 ctx
->emit_instruction(ctx
, &newInst
);
382 /* SLE t0.y, t0.x, tex.z; # bool b = distance <= k */
383 newInst
= tgsi_default_full_instruction();
384 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SLE
;
385 newInst
.Instruction
.NumDstRegs
= 1;
386 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
387 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
388 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
389 newInst
.Instruction
.NumSrcRegs
= 2;
390 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
391 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
392 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
393 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
394 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
395 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Z
;
396 ctx
->emit_instruction(ctx
, &newInst
);
398 /* CMP t0.w, -t0.y, tex.w, t0.w;
399 * # if -t0.y < 0 then
404 newInst
= tgsi_default_full_instruction();
405 newInst
.Instruction
.Opcode
= TGSI_OPCODE_CMP
;
406 newInst
.Instruction
.NumDstRegs
= 1;
407 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
408 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
409 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
410 newInst
.Instruction
.NumSrcRegs
= 3;
411 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
412 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
413 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
414 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
415 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
416 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
417 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
418 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
419 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
420 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
421 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
422 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
423 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
424 newInst
.FullSrcRegisters
[2].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
425 newInst
.FullSrcRegisters
[2].SrcRegister
.Index
= tmp0
;
426 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
427 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
428 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
429 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
430 ctx
->emit_instruction(ctx
, &newInst
);
434 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
435 /* add alpha modulation code at tail of program */
437 /* MOV result.color.xyz, colorTemp; */
438 newInst
= tgsi_default_full_instruction();
439 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
440 newInst
.Instruction
.NumDstRegs
= 1;
441 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
442 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
443 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
444 newInst
.Instruction
.NumSrcRegs
= 1;
445 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
446 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
447 ctx
->emit_instruction(ctx
, &newInst
);
449 /* MUL result.color.w, colorTemp, tmp0.w; */
450 newInst
= tgsi_default_full_instruction();
451 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
452 newInst
.Instruction
.NumDstRegs
= 1;
453 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
454 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
455 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
456 newInst
.Instruction
.NumSrcRegs
= 2;
457 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
458 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
459 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
460 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->tmp0
;
461 ctx
->emit_instruction(ctx
, &newInst
);
464 /* Not an END instruction.
465 * Look for writes to result.color and replace with colorTemp reg.
469 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
470 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
471 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
&&
472 dst
->DstRegister
.Index
== aactx
->colorOutput
) {
473 dst
->DstRegister
.File
= TGSI_FILE_TEMPORARY
;
474 dst
->DstRegister
.Index
= aactx
->colorTemp
;
479 ctx
->emit_instruction(ctx
, inst
);
484 * Generate the frag shader we'll use for drawing AA points.
485 * This will be the user's shader plus some texture/modulate instructions.
488 generate_aapoint_fs(struct aapoint_stage
*aapoint
)
490 const struct pipe_shader_state
*orig_fs
= &aapoint
->fs
->state
;
491 struct pipe_shader_state aapoint_fs
;
492 struct aa_transform_context transform
;
496 aapoint_fs
= *orig_fs
; /* copy to init */
497 aapoint_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
498 if (aapoint_fs
.tokens
== NULL
)
501 memset(&transform
, 0, sizeof(transform
));
502 transform
.colorOutput
= -1;
503 transform
.maxInput
= -1;
504 transform
.maxGeneric
= -1;
505 transform
.colorTemp
= -1;
507 transform
.firstInstruction
= TRUE
;
508 transform
.base
.transform_instruction
= aa_transform_inst
;
509 transform
.base
.transform_declaration
= aa_transform_decl
;
511 tgsi_transform_shader(orig_fs
->tokens
,
512 (struct tgsi_token
*) aapoint_fs
.tokens
,
513 MAX
, &transform
.base
);
516 printf("draw_aapoint, orig shader:\n");
517 tgsi_dump(orig_fs
->tokens
, 0);
518 printf("draw_aapoint, new shader:\n");
519 tgsi_dump(aapoint_fs
.tokens
, 0);
522 aapoint
->fs
->aapoint_fs
523 = aapoint
->driver_create_fs_state(aapoint
->pipe
, &aapoint_fs
);
524 if (aapoint
->fs
->aapoint_fs
== NULL
)
527 aapoint
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
534 * When we're about to draw our first AA point in a batch, this function is
535 * called to tell the driver to bind our modified fragment shader.
538 bind_aapoint_fragment_shader(struct aapoint_stage
*aapoint
)
540 struct draw_context
*draw
= aapoint
->stage
.draw
;
542 if (!aapoint
->fs
->aapoint_fs
&&
543 !generate_aapoint_fs(aapoint
))
546 draw
->suspend_flushing
= TRUE
;
547 aapoint
->driver_bind_fs_state(aapoint
->pipe
, aapoint
->fs
->aapoint_fs
);
548 draw
->suspend_flushing
= FALSE
;
555 static INLINE
struct aapoint_stage
*
556 aapoint_stage( struct draw_stage
*stage
)
558 return (struct aapoint_stage
*) stage
;
565 * Draw an AA point by drawing a quad.
568 aapoint_point(struct draw_stage
*stage
, struct prim_header
*header
)
570 const struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
571 struct prim_header tri
;
572 struct vertex_header
*v
[4];
573 uint texPos
= aapoint
->tex_slot
;
574 uint pos_slot
= aapoint
->pos_slot
;
575 float radius
, *pos
, *tex
;
579 if (aapoint
->psize_slot
>= 0) {
580 radius
= 0.5f
* header
->v
[0]->data
[aapoint
->psize_slot
][0];
583 radius
= aapoint
->radius
;
587 * Note: the texcoords (generic attrib, really) we use are special:
588 * The S and T components simply vary from -1 to +1.
589 * The R component is k, below.
590 * The Q component is 1.0 and will used as a handy constant in the
595 * k is the threshold distance from the point's center at which
596 * we begin alpha attenuation (the coverage value).
597 * Operating within a unit circle, we'll compute the fragment's
598 * distance 'd' from the center point using the texcoords.
602 * compute coverage in [0,1] proportional to d in [k, 1].
604 * coverage = 1.0; // full coverage
607 * Note: the ELSEIF and ELSE clauses are actually implemented with CMP to
608 * avoid using IF/ELSE/ENDIF TGSI opcodes.
613 k
= 1.0f
- 2.0f
* k
+ k
* k
;
615 k
= 1.0f
- 1.0f
/ radius
;
618 /* allocate/dup new verts */
619 for (i
= 0; i
< 4; i
++) {
620 v
[i
] = dup_vert(stage
, header
->v
[0], i
);
624 pos
= v
[0]->data
[pos_slot
];
628 pos
= v
[1]->data
[pos_slot
];
632 pos
= v
[2]->data
[pos_slot
];
636 pos
= v
[3]->data
[pos_slot
];
641 tex
= v
[0]->data
[texPos
];
642 ASSIGN_4V(tex
, -1, -1, k
, 1);
644 tex
= v
[1]->data
[texPos
];
645 ASSIGN_4V(tex
, 1, -1, k
, 1);
647 tex
= v
[2]->data
[texPos
];
648 ASSIGN_4V(tex
, 1, 1, k
, 1);
650 tex
= v
[3]->data
[texPos
];
651 ASSIGN_4V(tex
, -1, 1, k
, 1);
653 /* emit 2 tris for the quad strip */
657 stage
->next
->tri( stage
->next
, &tri
);
662 stage
->next
->tri( stage
->next
, &tri
);
667 aapoint_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
669 auto struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
670 struct draw_context
*draw
= stage
->draw
;
672 assert(draw
->rasterizer
->point_smooth
);
674 if (draw
->rasterizer
->point_size
<= 2.0)
675 aapoint
->radius
= 1.0;
677 aapoint
->radius
= 0.5f
* draw
->rasterizer
->point_size
;
680 * Bind (generate) our fragprog.
682 bind_aapoint_fragment_shader(aapoint
);
684 /* update vertex attrib info */
685 aapoint
->tex_slot
= draw
->vs
.num_vs_outputs
;
686 assert(aapoint
->tex_slot
> 0); /* output[0] is vertex pos */
688 aapoint
->pos_slot
= draw
->vs
.position_output
;
690 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
691 draw
->extra_vp_outputs
.semantic_index
= aapoint
->fs
->generic_attrib
;
692 draw
->extra_vp_outputs
.slot
= aapoint
->tex_slot
;
694 /* find psize slot in post-transform vertex */
695 aapoint
->psize_slot
= -1;
696 if (draw
->rasterizer
->point_size_per_vertex
) {
697 /* find PSIZ vertex output */
698 const struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
700 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
701 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
702 aapoint
->psize_slot
= i
;
708 /* now really draw first point */
709 stage
->point
= aapoint_point
;
710 stage
->point(stage
, header
);
715 aapoint_flush(struct draw_stage
*stage
, unsigned flags
)
717 struct draw_context
*draw
= stage
->draw
;
718 struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
719 struct pipe_context
*pipe
= aapoint
->pipe
;
721 stage
->point
= aapoint_first_point
;
722 stage
->next
->flush( stage
->next
, flags
);
724 /* restore original frag shader */
725 draw
->suspend_flushing
= TRUE
;
726 aapoint
->driver_bind_fs_state(pipe
, aapoint
->fs
->driver_fs
);
727 draw
->suspend_flushing
= FALSE
;
729 draw
->extra_vp_outputs
.slot
= 0;
734 aapoint_reset_stipple_counter(struct draw_stage
*stage
)
736 stage
->next
->reset_stipple_counter( stage
->next
);
741 aapoint_destroy(struct draw_stage
*stage
)
743 draw_free_temp_verts( stage
);
748 static struct aapoint_stage
*
749 draw_aapoint_stage(struct draw_context
*draw
)
751 struct aapoint_stage
*aapoint
= CALLOC_STRUCT(aapoint_stage
);
755 if (!draw_alloc_temp_verts( &aapoint
->stage
, 4 ))
758 aapoint
->stage
.draw
= draw
;
759 aapoint
->stage
.next
= NULL
;
760 aapoint
->stage
.point
= aapoint_first_point
;
761 aapoint
->stage
.line
= draw_pipe_passthrough_line
;
762 aapoint
->stage
.tri
= draw_pipe_passthrough_tri
;
763 aapoint
->stage
.flush
= aapoint_flush
;
764 aapoint
->stage
.reset_stipple_counter
= aapoint_reset_stipple_counter
;
765 aapoint
->stage
.destroy
= aapoint_destroy
;
771 aapoint_destroy(&aapoint
->stage
);
778 static struct aapoint_stage
*
779 aapoint_stage_from_pipe(struct pipe_context
*pipe
)
781 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
782 return aapoint_stage(draw
->pipeline
.aapoint
);
787 * This function overrides the driver's create_fs_state() function and
788 * will typically be called by the state tracker.
791 aapoint_create_fs_state(struct pipe_context
*pipe
,
792 const struct pipe_shader_state
*fs
)
794 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
795 struct aapoint_fragment_shader
*aafs
= CALLOC_STRUCT(aapoint_fragment_shader
);
802 aafs
->driver_fs
= aapoint
->driver_create_fs_state(aapoint
->pipe
, fs
);
809 aapoint_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
811 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
812 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
816 aapoint
->driver_bind_fs_state(aapoint
->pipe
,
817 (aafs
? aafs
->driver_fs
: NULL
));
822 aapoint_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
824 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
825 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
827 aapoint
->driver_delete_fs_state(aapoint
->pipe
, aafs
->driver_fs
);
833 * Called by drivers that want to install this AA point prim stage
834 * into the draw module's pipeline. This will not be used if the
835 * hardware has native support for AA points.
838 draw_install_aapoint_stage(struct draw_context
*draw
,
839 struct pipe_context
*pipe
)
841 struct aapoint_stage
*aapoint
;
843 pipe
->draw
= (void *) draw
;
846 * Create / install AA point drawing / prim stage
848 aapoint
= draw_aapoint_stage( draw
);
852 aapoint
->pipe
= pipe
;
854 /* save original driver functions */
855 aapoint
->driver_create_fs_state
= pipe
->create_fs_state
;
856 aapoint
->driver_bind_fs_state
= pipe
->bind_fs_state
;
857 aapoint
->driver_delete_fs_state
= pipe
->delete_fs_state
;
859 /* override the driver's functions */
860 pipe
->create_fs_state
= aapoint_create_fs_state
;
861 pipe
->bind_fs_state
= aapoint_bind_fs_state
;
862 pipe
->delete_fs_state
= aapoint_delete_fs_state
;
864 draw
->pipeline
.aapoint
= &aapoint
->stage
;
870 aapoint
->stage
.destroy( &aapoint
->stage
);