Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_aapoint.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
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.
33 *
34 * Note: this looks a lot like draw_aaline.c but there's actually little
35 * if any code that can be shared.
36 *
37 * Authors: Brian Paul
38 */
39
40
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_shader_tokens.h"
44
45 #include "tgsi/tgsi_transform.h"
46 #include "tgsi/tgsi_dump.h"
47
48 #include "util/u_math.h"
49 #include "util/u_memory.h"
50
51 #include "draw_context.h"
52 #include "draw_vs.h"
53 #include "draw_pipe.h"
54
55
56 /*
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.
61 */
62 #define NORMALIZE 0
63
64
65 /**
66 * Subclass of pipe_shader_state to carry extra fragment shader info.
67 */
68 struct aapoint_fragment_shader
69 {
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 */
74 };
75
76
77 /**
78 * Subclass of draw_stage
79 */
80 struct aapoint_stage
81 {
82 struct draw_stage stage;
83
84 int psize_slot;
85 float radius;
86
87 /** this is the vertex attrib slot for the new texcoords */
88 uint tex_slot;
89 uint pos_slot;
90
91 /*
92 * Currently bound state
93 */
94 struct aapoint_fragment_shader *fs;
95
96 /*
97 * Driver interface/override functions
98 */
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 *);
103
104 struct pipe_context *pipe;
105 };
106
107
108
109 /**
110 * Subclass of tgsi_transform_context, used for transforming the
111 * user's fragment shader to add the special AA instructions.
112 */
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;
120 };
121
122
123 /**
124 * TGSI declaration transform callback.
125 * Look for two free temp regs and available input reg for new texcoords.
126 */
127 static void
128 aa_transform_decl(struct tgsi_transform_context *ctx,
129 struct tgsi_full_declaration *decl)
130 {
131 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
132
133 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
134 decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
135 decl->Semantic.Index == 0) {
136 aactx->colorOutput = decl->Range.First;
137 }
138 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
139 if ((int) decl->Range.Last > aactx->maxInput)
140 aactx->maxInput = decl->Range.Last;
141 if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
142 (int) decl->Semantic.Index > aactx->maxGeneric) {
143 aactx->maxGeneric = decl->Semantic.Index;
144 }
145 }
146 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
147 uint i;
148 for (i = decl->Range.First;
149 i <= decl->Range.Last; i++) {
150 aactx->tempsUsed |= (1 << i);
151 }
152 }
153
154 ctx->emit_declaration(ctx, decl);
155 }
156
157
158 /**
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.
162 */
163 static void
164 aa_transform_inst(struct tgsi_transform_context *ctx,
165 struct tgsi_full_instruction *inst)
166 {
167 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
168 struct tgsi_full_instruction newInst;
169
170 if (aactx->firstInstruction) {
171 /* emit our new declarations before the first instruction */
172
173 struct tgsi_full_declaration decl;
174 const int texInput = aactx->maxInput + 1;
175 int tmp0;
176 uint i;
177
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 */
182 if (aactx->tmp0 < 0)
183 aactx->tmp0 = i;
184 else if (aactx->colorTemp < 0)
185 aactx->colorTemp = i;
186 else
187 break;
188 }
189 }
190
191 assert(aactx->colorTemp != aactx->tmp0);
192
193 tmp0 = aactx->tmp0;
194
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.Name = TGSI_SEMANTIC_GENERIC;
202 decl.Semantic.Index = aactx->maxGeneric + 1;
203 decl.Range.First =
204 decl.Range.Last = texInput;
205 ctx->emit_declaration(ctx, &decl);
206
207 /* declare new temp regs */
208 decl = tgsi_default_full_declaration();
209 decl.Declaration.File = TGSI_FILE_TEMPORARY;
210 decl.Range.First =
211 decl.Range.Last = tmp0;
212 ctx->emit_declaration(ctx, &decl);
213
214 decl = tgsi_default_full_declaration();
215 decl.Declaration.File = TGSI_FILE_TEMPORARY;
216 decl.Range.First =
217 decl.Range.Last = aactx->colorTemp;
218 ctx->emit_declaration(ctx, &decl);
219
220 aactx->firstInstruction = FALSE;
221
222
223 /*
224 * Emit code to compute fragment coverage, kill if outside point radius
225 *
226 * Temp reg0 usage:
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
231 */
232
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
238 newInst.Dst[0].Register.Index = tmp0;
239 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XY;
240 newInst.Instruction.NumSrcRegs = 2;
241 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
242 newInst.Src[0].Register.Index = texInput;
243 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
244 newInst.Src[1].Register.Index = texInput;
245 ctx->emit_instruction(ctx, &newInst);
246
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
252 newInst.Dst[0].Register.Index = tmp0;
253 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X;
254 newInst.Instruction.NumSrcRegs = 2;
255 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
256 newInst.Src[0].Register.Index = tmp0;
257 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X;
258 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
259 newInst.Src[1].Register.Index = tmp0;
260 newInst.Src[1].Register.SwizzleX = TGSI_SWIZZLE_Y;
261 ctx->emit_instruction(ctx, &newInst);
262
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
269 newInst.Dst[0].Register.Index = tmp0;
270 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X;
271 newInst.Instruction.NumSrcRegs = 1;
272 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
273 newInst.Src[0].Register.Index = tmp0;
274 ctx->emit_instruction(ctx, &newInst);
275
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
281 newInst.Dst[0].Register.Index = tmp0;
282 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X;
283 newInst.Instruction.NumSrcRegs = 1;
284 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
285 newInst.Src[0].Register.Index = tmp0;
286 ctx->emit_instruction(ctx, &newInst);
287 #endif
288
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
294 newInst.Dst[0].Register.Index = tmp0;
295 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Y;
296 newInst.Instruction.NumSrcRegs = 2;
297 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
298 newInst.Src[0].Register.Index = tmp0;
299 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X;
300 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
301 newInst.Src[1].Register.Index = texInput;
302 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_W;
303 ctx->emit_instruction(ctx, &newInst);
304
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.Src[0].Register.File = TGSI_FILE_TEMPORARY;
311 newInst.Src[0].Register.Index = tmp0;
312 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Y;
313 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y;
314 newInst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Y;
315 newInst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Y;
316 newInst.Src[0].Register.Negate = 1;
317 ctx->emit_instruction(ctx, &newInst);
318
319
320 /* compute coverage factor = (1-d)/(1-k) */
321
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
327 newInst.Dst[0].Register.Index = tmp0;
328 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Z;
329 newInst.Instruction.NumSrcRegs = 2;
330 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
331 newInst.Src[0].Register.Index = texInput;
332 newInst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_W;
333 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
334 newInst.Src[1].Register.Index = texInput;
335 newInst.Src[1].Register.SwizzleZ = TGSI_SWIZZLE_Z;
336 ctx->emit_instruction(ctx, &newInst);
337
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
343 newInst.Dst[0].Register.Index = tmp0;
344 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Z;
345 newInst.Instruction.NumSrcRegs = 1;
346 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
347 newInst.Src[0].Register.Index = tmp0;
348 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Z;
349 ctx->emit_instruction(ctx, &newInst);
350
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
356 newInst.Dst[0].Register.Index = tmp0;
357 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Y;
358 newInst.Instruction.NumSrcRegs = 2;
359 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
360 newInst.Src[0].Register.Index = texInput;
361 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_W;
362 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
363 newInst.Src[1].Register.Index = tmp0;
364 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_X;
365 ctx->emit_instruction(ctx, &newInst);
366
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
372 newInst.Dst[0].Register.Index = tmp0;
373 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
374 newInst.Instruction.NumSrcRegs = 2;
375 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
376 newInst.Src[0].Register.Index = tmp0;
377 newInst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Y;
378 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
379 newInst.Src[1].Register.Index = tmp0;
380 newInst.Src[1].Register.SwizzleW = TGSI_SWIZZLE_Z;
381 ctx->emit_instruction(ctx, &newInst);
382
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.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
388 newInst.Dst[0].Register.Index = tmp0;
389 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Y;
390 newInst.Instruction.NumSrcRegs = 2;
391 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
392 newInst.Src[0].Register.Index = tmp0;
393 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X;
394 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
395 newInst.Src[1].Register.Index = texInput;
396 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_Z;
397 ctx->emit_instruction(ctx, &newInst);
398
399 /* CMP t0.w, -t0.y, tex.w, t0.w;
400 * # if -t0.y < 0 then
401 * t0.w = 1
402 * else
403 * t0.w = t0.w
404 */
405 newInst = tgsi_default_full_instruction();
406 newInst.Instruction.Opcode = TGSI_OPCODE_CMP;
407 newInst.Instruction.NumDstRegs = 1;
408 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
409 newInst.Dst[0].Register.Index = tmp0;
410 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
411 newInst.Instruction.NumSrcRegs = 3;
412 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
413 newInst.Src[0].Register.Index = tmp0;
414 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Y;
415 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y;
416 newInst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Y;
417 newInst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Y;
418 newInst.Src[0].Register.Negate = 1;
419 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
420 newInst.Src[1].Register.Index = texInput;
421 newInst.Src[1].Register.SwizzleX = TGSI_SWIZZLE_W;
422 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_W;
423 newInst.Src[1].Register.SwizzleZ = TGSI_SWIZZLE_W;
424 newInst.Src[1].Register.SwizzleW = TGSI_SWIZZLE_W;
425 newInst.Src[2].Register.File = TGSI_FILE_TEMPORARY;
426 newInst.Src[2].Register.Index = tmp0;
427 newInst.Src[2].Register.SwizzleX = TGSI_SWIZZLE_W;
428 newInst.Src[2].Register.SwizzleY = TGSI_SWIZZLE_W;
429 newInst.Src[2].Register.SwizzleZ = TGSI_SWIZZLE_W;
430 newInst.Src[2].Register.SwizzleW = TGSI_SWIZZLE_W;
431 ctx->emit_instruction(ctx, &newInst);
432
433 }
434
435 if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
436 /* add alpha modulation code at tail of program */
437
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.Dst[0].Register.File = TGSI_FILE_OUTPUT;
443 newInst.Dst[0].Register.Index = aactx->colorOutput;
444 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
445 newInst.Instruction.NumSrcRegs = 1;
446 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
447 newInst.Src[0].Register.Index = aactx->colorTemp;
448 ctx->emit_instruction(ctx, &newInst);
449
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.Dst[0].Register.File = TGSI_FILE_OUTPUT;
455 newInst.Dst[0].Register.Index = aactx->colorOutput;
456 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
457 newInst.Instruction.NumSrcRegs = 2;
458 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
459 newInst.Src[0].Register.Index = aactx->colorTemp;
460 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
461 newInst.Src[1].Register.Index = aactx->tmp0;
462 ctx->emit_instruction(ctx, &newInst);
463 }
464 else {
465 /* Not an END instruction.
466 * Look for writes to result.color and replace with colorTemp reg.
467 */
468 uint i;
469
470 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
471 struct tgsi_full_dst_register *dst = &inst->Dst[i];
472 if (dst->Register.File == TGSI_FILE_OUTPUT &&
473 dst->Register.Index == aactx->colorOutput) {
474 dst->Register.File = TGSI_FILE_TEMPORARY;
475 dst->Register.Index = aactx->colorTemp;
476 }
477 }
478 }
479
480 ctx->emit_instruction(ctx, inst);
481 }
482
483
484 /**
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.
487 */
488 static boolean
489 generate_aapoint_fs(struct aapoint_stage *aapoint)
490 {
491 const struct pipe_shader_state *orig_fs = &aapoint->fs->state;
492 struct pipe_shader_state aapoint_fs;
493 struct aa_transform_context transform;
494
495 #define MAX 1000
496
497 aapoint_fs = *orig_fs; /* copy to init */
498 aapoint_fs.tokens = MALLOC(sizeof(struct tgsi_token) * MAX);
499 if (aapoint_fs.tokens == NULL)
500 return FALSE;
501
502 memset(&transform, 0, sizeof(transform));
503 transform.colorOutput = -1;
504 transform.maxInput = -1;
505 transform.maxGeneric = -1;
506 transform.colorTemp = -1;
507 transform.tmp0 = -1;
508 transform.firstInstruction = TRUE;
509 transform.base.transform_instruction = aa_transform_inst;
510 transform.base.transform_declaration = aa_transform_decl;
511
512 tgsi_transform_shader(orig_fs->tokens,
513 (struct tgsi_token *) aapoint_fs.tokens,
514 MAX, &transform.base);
515
516 #if 0 /* DEBUG */
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);
521 #endif
522
523 aapoint->fs->aapoint_fs
524 = aapoint->driver_create_fs_state(aapoint->pipe, &aapoint_fs);
525 if (aapoint->fs->aapoint_fs == NULL)
526 goto fail;
527
528 aapoint->fs->generic_attrib = transform.maxGeneric + 1;
529 FREE((void *)aapoint_fs.tokens);
530 return TRUE;
531
532 fail:
533 FREE((void *)aapoint_fs.tokens);
534 return FALSE;
535 }
536
537
538 /**
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.
541 */
542 static boolean
543 bind_aapoint_fragment_shader(struct aapoint_stage *aapoint)
544 {
545 struct draw_context *draw = aapoint->stage.draw;
546
547 if (!aapoint->fs->aapoint_fs &&
548 !generate_aapoint_fs(aapoint))
549 return FALSE;
550
551 draw->suspend_flushing = TRUE;
552 aapoint->driver_bind_fs_state(aapoint->pipe, aapoint->fs->aapoint_fs);
553 draw->suspend_flushing = FALSE;
554
555 return TRUE;
556 }
557
558
559
560 static INLINE struct aapoint_stage *
561 aapoint_stage( struct draw_stage *stage )
562 {
563 return (struct aapoint_stage *) stage;
564 }
565
566
567
568
569 /**
570 * Draw an AA point by drawing a quad.
571 */
572 static void
573 aapoint_point(struct draw_stage *stage, struct prim_header *header)
574 {
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;
581 uint i;
582 float k;
583
584 if (aapoint->psize_slot >= 0) {
585 radius = 0.5f * header->v[0]->data[aapoint->psize_slot][0];
586 }
587 else {
588 radius = aapoint->radius;
589 }
590
591 /*
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
596 * fragment shader.
597 */
598
599 /*
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.
604 * IF d > 1.0 THEN
605 * KILL fragment
606 * ELSE IF d > k THEN
607 * compute coverage in [0,1] proportional to d in [k, 1].
608 * ELSE
609 * coverage = 1.0; // full coverage
610 * ENDIF
611 *
612 * Note: the ELSEIF and ELSE clauses are actually implemented with CMP to
613 * avoid using IF/ELSE/ENDIF TGSI opcodes.
614 */
615
616 #if !NORMALIZE
617 k = 1.0f / radius;
618 k = 1.0f - 2.0f * k + k * k;
619 #else
620 k = 1.0f - 1.0f / radius;
621 #endif
622
623 /* allocate/dup new verts */
624 for (i = 0; i < 4; i++) {
625 v[i] = dup_vert(stage, header->v[0], i);
626 }
627
628 /* new verts */
629 pos = v[0]->data[pos_slot];
630 pos[0] -= radius;
631 pos[1] -= radius;
632
633 pos = v[1]->data[pos_slot];
634 pos[0] += radius;
635 pos[1] -= radius;
636
637 pos = v[2]->data[pos_slot];
638 pos[0] += radius;
639 pos[1] += radius;
640
641 pos = v[3]->data[pos_slot];
642 pos[0] -= radius;
643 pos[1] += radius;
644
645 /* new texcoords */
646 tex = v[0]->data[texPos];
647 ASSIGN_4V(tex, -1, -1, k, 1);
648
649 tex = v[1]->data[texPos];
650 ASSIGN_4V(tex, 1, -1, k, 1);
651
652 tex = v[2]->data[texPos];
653 ASSIGN_4V(tex, 1, 1, k, 1);
654
655 tex = v[3]->data[texPos];
656 ASSIGN_4V(tex, -1, 1, k, 1);
657
658 /* emit 2 tris for the quad strip */
659 tri.v[0] = v[0];
660 tri.v[1] = v[1];
661 tri.v[2] = v[2];
662 stage->next->tri( stage->next, &tri );
663
664 tri.v[0] = v[0];
665 tri.v[1] = v[2];
666 tri.v[2] = v[3];
667 stage->next->tri( stage->next, &tri );
668 }
669
670
671 static void
672 aapoint_first_point(struct draw_stage *stage, struct prim_header *header)
673 {
674 auto struct aapoint_stage *aapoint = aapoint_stage(stage);
675 struct draw_context *draw = stage->draw;
676
677 assert(draw->rasterizer->point_smooth);
678
679 if (draw->rasterizer->point_size <= 2.0)
680 aapoint->radius = 1.0;
681 else
682 aapoint->radius = 0.5f * draw->rasterizer->point_size;
683
684 /*
685 * Bind (generate) our fragprog.
686 */
687 bind_aapoint_fragment_shader(aapoint);
688
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 */
692
693 aapoint->pos_slot = draw->vs.position_output;
694
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;
698
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;
704 uint i;
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;
708 break;
709 }
710 }
711 }
712
713 /* now really draw first point */
714 stage->point = aapoint_point;
715 stage->point(stage, header);
716 }
717
718
719 static void
720 aapoint_flush(struct draw_stage *stage, unsigned flags)
721 {
722 struct draw_context *draw = stage->draw;
723 struct aapoint_stage *aapoint = aapoint_stage(stage);
724 struct pipe_context *pipe = aapoint->pipe;
725
726 stage->point = aapoint_first_point;
727 stage->next->flush( stage->next, flags );
728
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;
733
734 draw->extra_vp_outputs.slot = 0;
735 }
736
737
738 static void
739 aapoint_reset_stipple_counter(struct draw_stage *stage)
740 {
741 stage->next->reset_stipple_counter( stage->next );
742 }
743
744
745 static void
746 aapoint_destroy(struct draw_stage *stage)
747 {
748 draw_free_temp_verts( stage );
749 FREE( stage );
750 }
751
752
753 static struct aapoint_stage *
754 draw_aapoint_stage(struct draw_context *draw)
755 {
756 struct aapoint_stage *aapoint = CALLOC_STRUCT(aapoint_stage);
757 if (aapoint == NULL)
758 goto fail;
759
760 if (!draw_alloc_temp_verts( &aapoint->stage, 4 ))
761 goto fail;
762
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;
772
773 return aapoint;
774
775 fail:
776 if (aapoint)
777 aapoint_destroy(&aapoint->stage);
778
779 return NULL;
780
781 }
782
783
784 static struct aapoint_stage *
785 aapoint_stage_from_pipe(struct pipe_context *pipe)
786 {
787 struct draw_context *draw = (struct draw_context *) pipe->draw;
788 return aapoint_stage(draw->pipeline.aapoint);
789 }
790
791
792 /**
793 * This function overrides the driver's create_fs_state() function and
794 * will typically be called by the state tracker.
795 */
796 static void *
797 aapoint_create_fs_state(struct pipe_context *pipe,
798 const struct pipe_shader_state *fs)
799 {
800 struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);
801 struct aapoint_fragment_shader *aafs = CALLOC_STRUCT(aapoint_fragment_shader);
802 if (aafs == NULL)
803 return NULL;
804
805 aafs->state = *fs;
806
807 /* pass-through */
808 aafs->driver_fs = aapoint->driver_create_fs_state(aapoint->pipe, fs);
809
810 return aafs;
811 }
812
813
814 static void
815 aapoint_bind_fs_state(struct pipe_context *pipe, void *fs)
816 {
817 struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);
818 struct aapoint_fragment_shader *aafs = (struct aapoint_fragment_shader *) fs;
819 /* save current */
820 aapoint->fs = aafs;
821 /* pass-through */
822 aapoint->driver_bind_fs_state(aapoint->pipe,
823 (aafs ? aafs->driver_fs : NULL));
824 }
825
826
827 static void
828 aapoint_delete_fs_state(struct pipe_context *pipe, void *fs)
829 {
830 struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);
831 struct aapoint_fragment_shader *aafs = (struct aapoint_fragment_shader *) fs;
832
833 /* pass-through */
834 aapoint->driver_delete_fs_state(aapoint->pipe, aafs->driver_fs);
835
836 if (aafs->aapoint_fs)
837 aapoint->driver_delete_fs_state(aapoint->pipe, aafs->aapoint_fs);
838
839 FREE(aafs);
840 }
841
842
843 /**
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.
847 */
848 boolean
849 draw_install_aapoint_stage(struct draw_context *draw,
850 struct pipe_context *pipe)
851 {
852 struct aapoint_stage *aapoint;
853
854 pipe->draw = (void *) draw;
855
856 /*
857 * Create / install AA point drawing / prim stage
858 */
859 aapoint = draw_aapoint_stage( draw );
860 if (aapoint == NULL)
861 goto fail;
862
863 aapoint->pipe = pipe;
864
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;
869
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;
874
875 draw->pipeline.aapoint = &aapoint->stage;
876
877 return TRUE;
878
879 fail:
880 if (aapoint)
881 aapoint->stage.destroy( &aapoint->stage );
882
883 return FALSE;
884 }