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/util/tgsi_transform.h"
48 #include "tgsi/util/tgsi_dump.h"
50 #include "draw_context.h"
51 #include "draw_private.h"
55 * Enabling NORMALIZE might give _slightly_ better results.
56 * Basically, it controls whether we compute distance as d=sqrt(x*x+y*y) or
57 * d=x*x+y*y. Since we're working with a unit circle, the later seems
58 * close enough and saves some costly instructions.
64 * Subclass of pipe_shader_state to carry extra fragment shader info.
66 struct aapoint_fragment_shader
68 struct pipe_shader_state state
;
69 void *driver_fs
; /**< the regular shader */
70 void *aapoint_fs
; /**< the aa point-augmented shader */
71 int generic_attrib
; /**< The generic input attrib/texcoord we'll use */
76 * Subclass of draw_stage
80 struct draw_stage stage
;
85 /** this is the vertex attrib slot for the new texcoords */
89 * Currently bound state
91 struct aapoint_fragment_shader
*fs
;
94 * Driver interface/override functions
96 void * (*driver_create_fs_state
)(struct pipe_context
*,
97 const struct pipe_shader_state
*);
98 void (*driver_bind_fs_state
)(struct pipe_context
*, void *);
99 void (*driver_delete_fs_state
)(struct pipe_context
*, void *);
101 struct pipe_context
*pipe
;
107 * Subclass of tgsi_transform_context, used for transforming the
108 * user's fragment shader to add the special AA instructions.
110 struct aa_transform_context
{
111 struct tgsi_transform_context base
;
112 uint tempsUsed
; /**< bitmask */
113 int colorOutput
; /**< which output is the primary color */
114 int maxInput
, maxGeneric
; /**< max input index found */
115 int tmp0
, colorTemp
; /**< temp registers */
116 boolean firstInstruction
;
121 * TGSI declaration transform callback.
122 * Look for two free temp regs and available input reg for new texcoords.
125 aa_transform_decl(struct tgsi_transform_context
*ctx
,
126 struct tgsi_full_declaration
*decl
)
128 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
130 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
131 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
&&
132 decl
->Semantic
.SemanticIndex
== 0) {
133 aactx
->colorOutput
= decl
->u
.DeclarationRange
.First
;
135 else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
136 if ((int) decl
->u
.DeclarationRange
.Last
> aactx
->maxInput
)
137 aactx
->maxInput
= decl
->u
.DeclarationRange
.Last
;
138 if (decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
&&
139 (int) decl
->Semantic
.SemanticIndex
> aactx
->maxGeneric
) {
140 aactx
->maxGeneric
= decl
->Semantic
.SemanticIndex
;
143 else if (decl
->Declaration
.File
== TGSI_FILE_TEMPORARY
) {
145 for (i
= decl
->u
.DeclarationRange
.First
;
146 i
<= decl
->u
.DeclarationRange
.Last
; i
++) {
147 aactx
->tempsUsed
|= (1 << i
);
151 ctx
->emit_declaration(ctx
, decl
);
156 * TGSI instruction transform callback.
157 * Replace writes to result.color w/ a temp reg.
158 * Upon END instruction, insert texture sampling code for antialiasing.
161 aa_transform_inst(struct tgsi_transform_context
*ctx
,
162 struct tgsi_full_instruction
*inst
)
164 struct aa_transform_context
*aactx
= (struct aa_transform_context
*) ctx
;
165 struct tgsi_full_instruction newInst
;
167 if (aactx
->firstInstruction
) {
168 /* emit our new declarations before the first instruction */
170 struct tgsi_full_declaration decl
;
171 const int texInput
= aactx
->maxInput
+ 1;
175 /* find two free temp regs */
176 for (i
= 0; i
< 32; i
++) {
177 if ((aactx
->tempsUsed
& (1 << i
)) == 0) {
178 /* found a free temp */
181 else if (aactx
->colorTemp
< 0)
182 aactx
->colorTemp
= i
;
188 assert(aactx
->colorTemp
!= aactx
->tmp0
);
192 /* declare new generic input/texcoord */
193 decl
= tgsi_default_full_declaration();
194 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
195 decl
.Declaration
.Semantic
= 1;
196 decl
.Semantic
.SemanticName
= TGSI_SEMANTIC_GENERIC
;
197 decl
.Semantic
.SemanticIndex
= aactx
->maxGeneric
+ 1;
198 decl
.Declaration
.Interpolate
= 1;
199 /* XXX this could be linear... */
200 decl
.Interpolation
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
201 decl
.u
.DeclarationRange
.First
=
202 decl
.u
.DeclarationRange
.Last
= texInput
;
203 ctx
->emit_declaration(ctx
, &decl
);
205 /* declare new temp regs */
206 decl
= tgsi_default_full_declaration();
207 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
208 decl
.u
.DeclarationRange
.First
=
209 decl
.u
.DeclarationRange
.Last
= tmp0
;
210 ctx
->emit_declaration(ctx
, &decl
);
212 decl
= tgsi_default_full_declaration();
213 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
214 decl
.u
.DeclarationRange
.First
=
215 decl
.u
.DeclarationRange
.Last
= aactx
->colorTemp
;
216 ctx
->emit_declaration(ctx
, &decl
);
218 aactx
->firstInstruction
= FALSE
;
222 * Emit code to compute fragment coverage, kill if outside point radius
225 * t0.x = distance of fragment from center point
226 * t0.y = boolean, is t0.x > 1.0, also misc temp usage
227 * t0.z = temporary for computing 1/(1-k) value
228 * t0.w = final coverage value
231 /* MUL t0.xy, tex, tex; # compute x^2, y^2 */
232 newInst
= tgsi_default_full_instruction();
233 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
234 newInst
.Instruction
.NumDstRegs
= 1;
235 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
236 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
237 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XY
;
238 newInst
.Instruction
.NumSrcRegs
= 2;
239 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
240 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
241 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
242 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
243 ctx
->emit_instruction(ctx
, &newInst
);
245 /* ADD t0.x, t0.x, t0.y; # x^2 + y^2 */
246 newInst
= tgsi_default_full_instruction();
247 newInst
.Instruction
.Opcode
= TGSI_OPCODE_ADD
;
248 newInst
.Instruction
.NumDstRegs
= 1;
249 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
250 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
251 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
252 newInst
.Instruction
.NumSrcRegs
= 2;
253 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
254 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
255 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_X
;
256 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
257 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
258 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
259 ctx
->emit_instruction(ctx
, &newInst
);
261 #if NORMALIZE /* OPTIONAL normalization of length */
262 /* RSQ t0.x, t0.x; */
263 newInst
= tgsi_default_full_instruction();
264 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RSQ
;
265 newInst
.Instruction
.NumDstRegs
= 1;
266 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
267 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
268 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
269 newInst
.Instruction
.NumSrcRegs
= 1;
270 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
271 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
272 ctx
->emit_instruction(ctx
, &newInst
);
274 /* RCP t0.x, t0.x; */
275 newInst
= tgsi_default_full_instruction();
276 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
277 newInst
.Instruction
.NumDstRegs
= 1;
278 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
279 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
280 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_X
;
281 newInst
.Instruction
.NumSrcRegs
= 1;
282 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
283 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
284 ctx
->emit_instruction(ctx
, &newInst
);
287 /* SGT t0.y, t0.xxxx, t0.wwww; # bool b = d > 1 (NOTE t0.w == 1) */
288 newInst
= tgsi_default_full_instruction();
289 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SGT
;
290 newInst
.Instruction
.NumDstRegs
= 1;
291 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
292 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
293 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
294 newInst
.Instruction
.NumSrcRegs
= 2;
295 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
296 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
297 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
298 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
299 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
300 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
301 ctx
->emit_instruction(ctx
, &newInst
);
303 /* KILP -t0.yyyy; # if b, KILL */
304 newInst
= tgsi_default_full_instruction();
305 newInst
.Instruction
.Opcode
= TGSI_OPCODE_KILP
;
306 newInst
.Instruction
.NumDstRegs
= 0;
307 newInst
.Instruction
.NumSrcRegs
= 1;
308 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
309 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
310 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
311 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
312 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
313 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
314 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
315 ctx
->emit_instruction(ctx
, &newInst
);
318 /* compute coverage factor = (1-d)/(1-k) */
320 /* SUB t0.z, tex.w, tex.z; # m = 1 - k */
321 newInst
= tgsi_default_full_instruction();
322 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
323 newInst
.Instruction
.NumDstRegs
= 1;
324 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
325 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
326 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
327 newInst
.Instruction
.NumSrcRegs
= 2;
328 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
329 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
330 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
331 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
332 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
333 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Z
;
334 ctx
->emit_instruction(ctx
, &newInst
);
336 /* RCP t0.z, t0.z; # t0.z = 1 / m */
337 newInst
= tgsi_default_full_instruction();
338 newInst
.Instruction
.Opcode
= TGSI_OPCODE_RCP
;
339 newInst
.Instruction
.NumDstRegs
= 1;
340 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
341 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
342 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Z
;
343 newInst
.Instruction
.NumSrcRegs
= 1;
344 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
345 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
346 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Z
;
347 ctx
->emit_instruction(ctx
, &newInst
);
349 /* SUB t0.y, 1, t0.x; # d = 1 - d */
350 newInst
= tgsi_default_full_instruction();
351 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SUB
;
352 newInst
.Instruction
.NumDstRegs
= 1;
353 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
354 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
355 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
356 newInst
.Instruction
.NumSrcRegs
= 2;
357 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_INPUT
;
358 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= texInput
;
359 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
360 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
361 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
362 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
363 ctx
->emit_instruction(ctx
, &newInst
);
365 /* MUL t0.w, t0.y, t0.z; # coverage = d * m */
366 newInst
= tgsi_default_full_instruction();
367 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
368 newInst
.Instruction
.NumDstRegs
= 1;
369 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
370 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
371 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
372 newInst
.Instruction
.NumSrcRegs
= 2;
373 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
374 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
375 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
376 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
377 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= tmp0
;
378 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Z
;
379 ctx
->emit_instruction(ctx
, &newInst
);
381 /* SLE t0.y, t0.x, tex.z; # bool b = distance <= k */
382 newInst
= tgsi_default_full_instruction();
383 newInst
.Instruction
.Opcode
= TGSI_OPCODE_SLE
;
384 newInst
.Instruction
.NumDstRegs
= 1;
385 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
386 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
387 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_Y
;
388 newInst
.Instruction
.NumSrcRegs
= 2;
389 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
390 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
391 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_X
;
392 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
393 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
394 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Z
;
395 ctx
->emit_instruction(ctx
, &newInst
);
397 /* CMP t0.w, -t0.y, tex.w, t0.w;
398 * # if -t0.y < 0 then
403 newInst
= tgsi_default_full_instruction();
404 newInst
.Instruction
.Opcode
= TGSI_OPCODE_CMP
;
405 newInst
.Instruction
.NumDstRegs
= 1;
406 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_TEMPORARY
;
407 newInst
.FullDstRegisters
[0].DstRegister
.Index
= tmp0
;
408 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
409 newInst
.Instruction
.NumSrcRegs
= 3;
410 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
411 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= tmp0
;
412 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_Y
;
413 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_Y
;
414 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_Y
;
415 newInst
.FullSrcRegisters
[0].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_Y
;
416 newInst
.FullSrcRegisters
[0].SrcRegister
.Negate
= 1;
417 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_INPUT
;
418 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= texInput
;
419 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
420 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
421 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
422 newInst
.FullSrcRegisters
[1].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
423 newInst
.FullSrcRegisters
[2].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
424 newInst
.FullSrcRegisters
[2].SrcRegister
.Index
= tmp0
;
425 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleX
= TGSI_SWIZZLE_W
;
426 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleY
= TGSI_SWIZZLE_W
;
427 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleZ
= TGSI_SWIZZLE_W
;
428 newInst
.FullSrcRegisters
[2].SrcRegister
.SwizzleW
= TGSI_SWIZZLE_W
;
429 ctx
->emit_instruction(ctx
, &newInst
);
433 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
434 /* add alpha modulation code at tail of program */
436 /* MOV result.color.xyz, colorTemp; */
437 newInst
= tgsi_default_full_instruction();
438 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MOV
;
439 newInst
.Instruction
.NumDstRegs
= 1;
440 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
441 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
442 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_XYZ
;
443 newInst
.Instruction
.NumSrcRegs
= 1;
444 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
445 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
446 ctx
->emit_instruction(ctx
, &newInst
);
448 /* MUL result.color.w, colorTemp, tmp0.w; */
449 newInst
= tgsi_default_full_instruction();
450 newInst
.Instruction
.Opcode
= TGSI_OPCODE_MUL
;
451 newInst
.Instruction
.NumDstRegs
= 1;
452 newInst
.FullDstRegisters
[0].DstRegister
.File
= TGSI_FILE_OUTPUT
;
453 newInst
.FullDstRegisters
[0].DstRegister
.Index
= aactx
->colorOutput
;
454 newInst
.FullDstRegisters
[0].DstRegister
.WriteMask
= TGSI_WRITEMASK_W
;
455 newInst
.Instruction
.NumSrcRegs
= 2;
456 newInst
.FullSrcRegisters
[0].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
457 newInst
.FullSrcRegisters
[0].SrcRegister
.Index
= aactx
->colorTemp
;
458 newInst
.FullSrcRegisters
[1].SrcRegister
.File
= TGSI_FILE_TEMPORARY
;
459 newInst
.FullSrcRegisters
[1].SrcRegister
.Index
= aactx
->tmp0
;
460 ctx
->emit_instruction(ctx
, &newInst
);
463 /* Not an END instruction.
464 * Look for writes to result.color and replace with colorTemp reg.
468 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
469 struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
470 if (dst
->DstRegister
.File
== TGSI_FILE_OUTPUT
&&
471 dst
->DstRegister
.Index
== aactx
->colorOutput
) {
472 dst
->DstRegister
.File
= TGSI_FILE_TEMPORARY
;
473 dst
->DstRegister
.Index
= aactx
->colorTemp
;
478 ctx
->emit_instruction(ctx
, inst
);
483 * Generate the frag shader we'll use for drawing AA lines.
484 * This will be the user's shader plus some texture/modulate instructions.
487 generate_aapoint_fs(struct aapoint_stage
*aapoint
)
489 const struct pipe_shader_state
*orig_fs
= &aapoint
->fs
->state
;
490 struct pipe_shader_state aapoint_fs
;
491 struct aa_transform_context transform
;
495 aapoint_fs
= *orig_fs
; /* copy to init */
496 aapoint_fs
.tokens
= MALLOC(sizeof(struct tgsi_token
) * MAX
);
498 memset(&transform
, 0, sizeof(transform
));
499 transform
.colorOutput
= -1;
500 transform
.maxInput
= -1;
501 transform
.maxGeneric
= -1;
502 transform
.colorTemp
= -1;
504 transform
.firstInstruction
= TRUE
;
505 transform
.base
.transform_instruction
= aa_transform_inst
;
506 transform
.base
.transform_declaration
= aa_transform_decl
;
508 tgsi_transform_shader(orig_fs
->tokens
,
509 (struct tgsi_token
*) aapoint_fs
.tokens
,
510 MAX
, &transform
.base
);
513 printf("draw_aapoint, orig shader:\n");
514 tgsi_dump(orig_fs
->tokens
, 0);
515 printf("draw_aapoint, new shader:\n");
516 tgsi_dump(aapoint_fs
.tokens
, 0);
519 aapoint
->fs
->aapoint_fs
520 = aapoint
->driver_create_fs_state(aapoint
->pipe
, &aapoint_fs
);
522 aapoint
->fs
->generic_attrib
= transform
.maxGeneric
+ 1;
527 * When we're about to draw our first AA line in a batch, this function is
528 * called to tell the driver to bind our modified fragment shader.
531 bind_aapoint_fragment_shader(struct aapoint_stage
*aapoint
)
533 if (!aapoint
->fs
->aapoint_fs
) {
534 generate_aapoint_fs(aapoint
);
536 aapoint
->driver_bind_fs_state(aapoint
->pipe
, aapoint
->fs
->aapoint_fs
);
541 static INLINE
struct aapoint_stage
*
542 aapoint_stage( struct draw_stage
*stage
)
544 return (struct aapoint_stage
*) stage
;
549 passthrough_line(struct draw_stage
*stage
, struct prim_header
*header
)
551 stage
->next
->line(stage
->next
, header
);
556 passthrough_tri(struct draw_stage
*stage
, struct prim_header
*header
)
558 stage
->next
->tri(stage
->next
, header
);
563 * Draw an AA point by drawing a quad.
566 aapoint_point(struct draw_stage
*stage
, struct prim_header
*header
)
568 const struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
569 struct prim_header tri
;
570 struct vertex_header
*v
[4];
571 uint texPos
= aapoint
->tex_slot
;
572 float radius
, *pos
, *tex
;
576 if (aapoint
->psize_slot
>= 0) {
577 radius
= 0.5f
* header
->v
[0]->data
[aapoint
->psize_slot
][0];
580 radius
= aapoint
->radius
;
584 * Note: the texcoords (generic attrib, really) we use are special:
585 * The S and T components simply vary from -1 to +1.
586 * The R component is k, below.
587 * The Q component is 1.0 and will used as a handy constant in the
592 * k is the threshold distance from the point's center at which
593 * we begin alpha attenuation (the coverage value).
594 * Operating within a unit circle, we'll compute the fragment's
595 * distance 'd' from the center point using the texcoords.
599 * compute coverage in [0,1] proportional to d in [k, 1].
601 * coverage = 1.0; // full coverage
604 * Note: the ELSEIF and ELSE clauses are actually implemented with CMP to
605 * avoid using IF/ELSE/ENDIF TGSI opcodes.
610 k
= 1.0f
- 2.0f
* k
+ k
* k
;
612 k
= 1.0f
- 1.0f
/ radius
;
615 /* allocate/dup new verts */
616 for (i
= 0; i
< 4; i
++) {
617 v
[i
] = dup_vert(stage
, header
->v
[0], i
);
638 tex
= v
[0]->data
[texPos
];
639 ASSIGN_4V(tex
, -1, -1, k
, 1);
641 tex
= v
[1]->data
[texPos
];
642 ASSIGN_4V(tex
, 1, -1, k
, 1);
644 tex
= v
[2]->data
[texPos
];
645 ASSIGN_4V(tex
, 1, 1, k
, 1);
647 tex
= v
[3]->data
[texPos
];
648 ASSIGN_4V(tex
, -1, 1, k
, 1);
650 /* emit 2 tris for the quad strip */
654 stage
->next
->tri( stage
->next
, &tri
);
659 stage
->next
->tri( stage
->next
, &tri
);
664 aapoint_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
666 auto struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
667 struct draw_context
*draw
= stage
->draw
;
669 assert(draw
->rasterizer
->point_smooth
);
671 if (draw
->rasterizer
->point_size
<= 2.0)
672 aapoint
->radius
= 1.0;
674 aapoint
->radius
= 0.5f
* draw
->rasterizer
->point_size
;
677 * Bind (generate) our fragprog.
679 bind_aapoint_fragment_shader(aapoint
);
681 /* update vertex attrib info */
682 aapoint
->tex_slot
= draw
->num_vs_outputs
;
683 assert(aapoint
->tex_slot
> 0); /* output[0] is vertex pos */
685 draw
->extra_vp_outputs
.semantic_name
= TGSI_SEMANTIC_GENERIC
;
686 draw
->extra_vp_outputs
.semantic_index
= aapoint
->fs
->generic_attrib
;
687 draw
->extra_vp_outputs
.slot
= aapoint
->tex_slot
;
689 /* find psize slot in post-transform vertex */
690 aapoint
->psize_slot
= -1;
691 if (draw
->rasterizer
->point_size_per_vertex
) {
692 /* find PSIZ vertex output */
693 const struct draw_vertex_shader
*vs
= draw
->vertex_shader
;
695 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
696 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_PSIZE
) {
697 aapoint
->psize_slot
= i
;
703 /* now really draw first line */
704 stage
->point
= aapoint_point
;
705 stage
->point(stage
, header
);
710 aapoint_flush(struct draw_stage
*stage
, unsigned flags
)
712 struct draw_context
*draw
= stage
->draw
;
713 struct aapoint_stage
*aapoint
= aapoint_stage(stage
);
714 struct pipe_context
*pipe
= aapoint
->pipe
;
716 stage
->point
= aapoint_first_point
;
717 stage
->next
->flush( stage
->next
, flags
);
719 /* restore original frag shader */
720 aapoint
->driver_bind_fs_state(pipe
, aapoint
->fs
->driver_fs
);
722 draw
->extra_vp_outputs
.slot
= 0;
727 aapoint_reset_stipple_counter(struct draw_stage
*stage
)
729 stage
->next
->reset_stipple_counter( stage
->next
);
734 aapoint_destroy(struct draw_stage
*stage
)
736 draw_free_temp_verts( stage
);
741 static struct aapoint_stage
*
742 draw_aapoint_stage(struct draw_context
*draw
)
744 struct aapoint_stage
*aapoint
= CALLOC_STRUCT(aapoint_stage
);
746 draw_alloc_temp_verts( &aapoint
->stage
, 4 );
748 aapoint
->stage
.draw
= draw
;
749 aapoint
->stage
.next
= NULL
;
750 aapoint
->stage
.point
= aapoint_first_point
;
751 aapoint
->stage
.line
= passthrough_line
;
752 aapoint
->stage
.tri
= passthrough_tri
;
753 aapoint
->stage
.flush
= aapoint_flush
;
754 aapoint
->stage
.reset_stipple_counter
= aapoint_reset_stipple_counter
;
755 aapoint
->stage
.destroy
= aapoint_destroy
;
761 static struct aapoint_stage
*
762 aapoint_stage_from_pipe(struct pipe_context
*pipe
)
764 struct draw_context
*draw
= (struct draw_context
*) pipe
->draw
;
765 return aapoint_stage(draw
->pipeline
.aapoint
);
770 * This function overrides the driver's create_fs_state() function and
771 * will typically be called by the state tracker.
774 aapoint_create_fs_state(struct pipe_context
*pipe
,
775 const struct pipe_shader_state
*fs
)
777 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
778 struct aapoint_fragment_shader
*aafs
= CALLOC_STRUCT(aapoint_fragment_shader
);
784 aafs
->driver_fs
= aapoint
->driver_create_fs_state(aapoint
->pipe
, fs
);
792 aapoint_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
794 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
795 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
799 aapoint
->driver_bind_fs_state(aapoint
->pipe
,
800 (aafs
? aafs
->driver_fs
: NULL
));
805 aapoint_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
807 struct aapoint_stage
*aapoint
= aapoint_stage_from_pipe(pipe
);
808 struct aapoint_fragment_shader
*aafs
= (struct aapoint_fragment_shader
*) fs
;
810 aapoint
->driver_delete_fs_state(aapoint
->pipe
, aafs
->driver_fs
);
816 * Called by drivers that want to install this AA point prim stage
817 * into the draw module's pipeline. This will not be used if the
818 * hardware has native support for AA points.
821 draw_install_aapoint_stage(struct draw_context
*draw
,
822 struct pipe_context
*pipe
)
824 struct aapoint_stage
*aapoint
;
826 pipe
->draw
= (void *) draw
;
829 * Create / install AA point drawing / prim stage
831 aapoint
= draw_aapoint_stage( draw
);
833 draw
->pipeline
.aapoint
= &aapoint
->stage
;
835 aapoint
->pipe
= pipe
;
837 /* save original driver functions */
838 aapoint
->driver_create_fs_state
= pipe
->create_fs_state
;
839 aapoint
->driver_bind_fs_state
= pipe
->bind_fs_state
;
840 aapoint
->driver_delete_fs_state
= pipe
->delete_fs_state
;
842 /* override the driver's functions */
843 pipe
->create_fs_state
= aapoint_create_fs_state
;
844 pipe
->bind_fs_state
= aapoint_bind_fs_state
;
845 pipe
->delete_fs_state
= aapoint_delete_fs_state
;