r300c/r300g: add 3155 rv380 pci id
[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 /** Approx number of new tokens for instructions in aa_transform_inst() */
57 #define NUM_NEW_TOKENS 200
58
59
60 /*
61 * Enabling NORMALIZE might give _slightly_ better results.
62 * Basically, it controls whether we compute distance as d=sqrt(x*x+y*y) or
63 * d=x*x+y*y. Since we're working with a unit circle, the later seems
64 * close enough and saves some costly instructions.
65 */
66 #define NORMALIZE 0
67
68
69 /**
70 * Subclass of pipe_shader_state to carry extra fragment shader info.
71 */
72 struct aapoint_fragment_shader
73 {
74 struct pipe_shader_state state;
75 void *driver_fs; /**< the regular shader */
76 void *aapoint_fs; /**< the aa point-augmented shader */
77 int generic_attrib; /**< The generic input attrib/texcoord we'll use */
78 };
79
80
81 /**
82 * Subclass of draw_stage
83 */
84 struct aapoint_stage
85 {
86 struct draw_stage stage;
87
88 /** half of pipe_rasterizer_state::point_size */
89 float radius;
90
91 /** vertex attrib slot containing point size */
92 int psize_slot;
93
94 /** this is the vertex attrib slot for the new texcoords */
95 uint tex_slot;
96
97 /** vertex attrib slot containing position */
98 uint pos_slot;
99
100 /** Currently bound fragment shader */
101 struct aapoint_fragment_shader *fs;
102
103 /*
104 * Driver interface/override functions
105 */
106 void * (*driver_create_fs_state)(struct pipe_context *,
107 const struct pipe_shader_state *);
108 void (*driver_bind_fs_state)(struct pipe_context *, void *);
109 void (*driver_delete_fs_state)(struct pipe_context *, void *);
110
111 struct pipe_context *pipe;
112 };
113
114
115
116 /**
117 * Subclass of tgsi_transform_context, used for transforming the
118 * user's fragment shader to add the special AA instructions.
119 */
120 struct aa_transform_context {
121 struct tgsi_transform_context base;
122 uint tempsUsed; /**< bitmask */
123 int colorOutput; /**< which output is the primary color */
124 int maxInput, maxGeneric; /**< max input index found */
125 int tmp0, colorTemp; /**< temp registers */
126 boolean firstInstruction;
127 };
128
129
130 /**
131 * TGSI declaration transform callback.
132 * Look for two free temp regs and available input reg for new texcoords.
133 */
134 static void
135 aa_transform_decl(struct tgsi_transform_context *ctx,
136 struct tgsi_full_declaration *decl)
137 {
138 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
139
140 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
141 decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
142 decl->Semantic.Index == 0) {
143 aactx->colorOutput = decl->Range.First;
144 }
145 else if (decl->Declaration.File == TGSI_FILE_INPUT) {
146 if ((int) decl->Range.Last > aactx->maxInput)
147 aactx->maxInput = decl->Range.Last;
148 if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
149 (int) decl->Semantic.Index > aactx->maxGeneric) {
150 aactx->maxGeneric = decl->Semantic.Index;
151 }
152 }
153 else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
154 uint i;
155 for (i = decl->Range.First;
156 i <= decl->Range.Last; i++) {
157 aactx->tempsUsed |= (1 << i);
158 }
159 }
160
161 ctx->emit_declaration(ctx, decl);
162 }
163
164
165 /**
166 * TGSI instruction transform callback.
167 * Replace writes to result.color w/ a temp reg.
168 * Upon END instruction, insert texture sampling code for antialiasing.
169 */
170 static void
171 aa_transform_inst(struct tgsi_transform_context *ctx,
172 struct tgsi_full_instruction *inst)
173 {
174 struct aa_transform_context *aactx = (struct aa_transform_context *) ctx;
175 struct tgsi_full_instruction newInst;
176
177 if (aactx->firstInstruction) {
178 /* emit our new declarations before the first instruction */
179
180 struct tgsi_full_declaration decl;
181 const int texInput = aactx->maxInput + 1;
182 int tmp0;
183 uint i;
184
185 /* find two free temp regs */
186 for (i = 0; i < 32; i++) {
187 if ((aactx->tempsUsed & (1 << i)) == 0) {
188 /* found a free temp */
189 if (aactx->tmp0 < 0)
190 aactx->tmp0 = i;
191 else if (aactx->colorTemp < 0)
192 aactx->colorTemp = i;
193 else
194 break;
195 }
196 }
197
198 assert(aactx->colorTemp != aactx->tmp0);
199
200 tmp0 = aactx->tmp0;
201
202 /* declare new generic input/texcoord */
203 decl = tgsi_default_full_declaration();
204 decl.Declaration.File = TGSI_FILE_INPUT;
205 /* XXX this could be linear... */
206 decl.Declaration.Interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
207 decl.Declaration.Semantic = 1;
208 decl.Semantic.Name = TGSI_SEMANTIC_GENERIC;
209 decl.Semantic.Index = aactx->maxGeneric + 1;
210 decl.Range.First =
211 decl.Range.Last = texInput;
212 ctx->emit_declaration(ctx, &decl);
213
214 /* declare new temp regs */
215 decl = tgsi_default_full_declaration();
216 decl.Declaration.File = TGSI_FILE_TEMPORARY;
217 decl.Range.First =
218 decl.Range.Last = tmp0;
219 ctx->emit_declaration(ctx, &decl);
220
221 decl = tgsi_default_full_declaration();
222 decl.Declaration.File = TGSI_FILE_TEMPORARY;
223 decl.Range.First =
224 decl.Range.Last = aactx->colorTemp;
225 ctx->emit_declaration(ctx, &decl);
226
227 aactx->firstInstruction = FALSE;
228
229
230 /*
231 * Emit code to compute fragment coverage, kill if outside point radius
232 *
233 * Temp reg0 usage:
234 * t0.x = distance of fragment from center point
235 * t0.y = boolean, is t0.x > 1.0, also misc temp usage
236 * t0.z = temporary for computing 1/(1-k) value
237 * t0.w = final coverage value
238 */
239
240 /* MUL t0.xy, tex, tex; # compute x^2, y^2 */
241 newInst = tgsi_default_full_instruction();
242 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
243 newInst.Instruction.NumDstRegs = 1;
244 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
245 newInst.Dst[0].Register.Index = tmp0;
246 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XY;
247 newInst.Instruction.NumSrcRegs = 2;
248 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
249 newInst.Src[0].Register.Index = texInput;
250 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
251 newInst.Src[1].Register.Index = texInput;
252 ctx->emit_instruction(ctx, &newInst);
253
254 /* ADD t0.x, t0.x, t0.y; # x^2 + y^2 */
255 newInst = tgsi_default_full_instruction();
256 newInst.Instruction.Opcode = TGSI_OPCODE_ADD;
257 newInst.Instruction.NumDstRegs = 1;
258 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
259 newInst.Dst[0].Register.Index = tmp0;
260 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X;
261 newInst.Instruction.NumSrcRegs = 2;
262 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
263 newInst.Src[0].Register.Index = tmp0;
264 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X;
265 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
266 newInst.Src[1].Register.Index = tmp0;
267 newInst.Src[1].Register.SwizzleX = TGSI_SWIZZLE_Y;
268 ctx->emit_instruction(ctx, &newInst);
269
270 #if NORMALIZE /* OPTIONAL normalization of length */
271 /* RSQ t0.x, t0.x; */
272 newInst = tgsi_default_full_instruction();
273 newInst.Instruction.Opcode = TGSI_OPCODE_RSQ;
274 newInst.Instruction.NumDstRegs = 1;
275 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
276 newInst.Dst[0].Register.Index = tmp0;
277 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X;
278 newInst.Instruction.NumSrcRegs = 1;
279 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
280 newInst.Src[0].Register.Index = tmp0;
281 ctx->emit_instruction(ctx, &newInst);
282
283 /* RCP t0.x, t0.x; */
284 newInst = tgsi_default_full_instruction();
285 newInst.Instruction.Opcode = TGSI_OPCODE_RCP;
286 newInst.Instruction.NumDstRegs = 1;
287 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
288 newInst.Dst[0].Register.Index = tmp0;
289 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X;
290 newInst.Instruction.NumSrcRegs = 1;
291 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
292 newInst.Src[0].Register.Index = tmp0;
293 ctx->emit_instruction(ctx, &newInst);
294 #endif
295
296 /* SGT t0.y, t0.xxxx, tex.wwww; # bool b = d > 1 (NOTE tex.w == 1) */
297 newInst = tgsi_default_full_instruction();
298 newInst.Instruction.Opcode = TGSI_OPCODE_SGT;
299 newInst.Instruction.NumDstRegs = 1;
300 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
301 newInst.Dst[0].Register.Index = tmp0;
302 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Y;
303 newInst.Instruction.NumSrcRegs = 2;
304 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
305 newInst.Src[0].Register.Index = tmp0;
306 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X;
307 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
308 newInst.Src[1].Register.Index = texInput;
309 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_W;
310 ctx->emit_instruction(ctx, &newInst);
311
312 /* KIL -tmp0.yyyy; # if -tmp0.y < 0, KILL */
313 newInst = tgsi_default_full_instruction();
314 newInst.Instruction.Opcode = TGSI_OPCODE_KIL;
315 newInst.Instruction.NumDstRegs = 0;
316 newInst.Instruction.NumSrcRegs = 1;
317 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
318 newInst.Src[0].Register.Index = tmp0;
319 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Y;
320 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y;
321 newInst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Y;
322 newInst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Y;
323 newInst.Src[0].Register.Negate = 1;
324 ctx->emit_instruction(ctx, &newInst);
325
326
327 /* compute coverage factor = (1-d)/(1-k) */
328
329 /* SUB t0.z, tex.w, tex.z; # m = 1 - k */
330 newInst = tgsi_default_full_instruction();
331 newInst.Instruction.Opcode = TGSI_OPCODE_SUB;
332 newInst.Instruction.NumDstRegs = 1;
333 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
334 newInst.Dst[0].Register.Index = tmp0;
335 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Z;
336 newInst.Instruction.NumSrcRegs = 2;
337 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
338 newInst.Src[0].Register.Index = texInput;
339 newInst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_W;
340 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
341 newInst.Src[1].Register.Index = texInput;
342 newInst.Src[1].Register.SwizzleZ = TGSI_SWIZZLE_Z;
343 ctx->emit_instruction(ctx, &newInst);
344
345 /* RCP t0.z, t0.z; # t0.z = 1 / m */
346 newInst = tgsi_default_full_instruction();
347 newInst.Instruction.Opcode = TGSI_OPCODE_RCP;
348 newInst.Instruction.NumDstRegs = 1;
349 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
350 newInst.Dst[0].Register.Index = tmp0;
351 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Z;
352 newInst.Instruction.NumSrcRegs = 1;
353 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
354 newInst.Src[0].Register.Index = tmp0;
355 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Z;
356 ctx->emit_instruction(ctx, &newInst);
357
358 /* SUB t0.y, 1, t0.x; # d = 1 - d */
359 newInst = tgsi_default_full_instruction();
360 newInst.Instruction.Opcode = TGSI_OPCODE_SUB;
361 newInst.Instruction.NumDstRegs = 1;
362 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
363 newInst.Dst[0].Register.Index = tmp0;
364 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Y;
365 newInst.Instruction.NumSrcRegs = 2;
366 newInst.Src[0].Register.File = TGSI_FILE_INPUT;
367 newInst.Src[0].Register.Index = texInput;
368 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_W;
369 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
370 newInst.Src[1].Register.Index = tmp0;
371 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_X;
372 ctx->emit_instruction(ctx, &newInst);
373
374 /* MUL t0.w, t0.y, t0.z; # coverage = d * m */
375 newInst = tgsi_default_full_instruction();
376 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
377 newInst.Instruction.NumDstRegs = 1;
378 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
379 newInst.Dst[0].Register.Index = tmp0;
380 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
381 newInst.Instruction.NumSrcRegs = 2;
382 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
383 newInst.Src[0].Register.Index = tmp0;
384 newInst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Y;
385 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
386 newInst.Src[1].Register.Index = tmp0;
387 newInst.Src[1].Register.SwizzleW = TGSI_SWIZZLE_Z;
388 ctx->emit_instruction(ctx, &newInst);
389
390 /* SLE t0.y, t0.x, tex.z; # bool b = distance <= k */
391 newInst = tgsi_default_full_instruction();
392 newInst.Instruction.Opcode = TGSI_OPCODE_SLE;
393 newInst.Instruction.NumDstRegs = 1;
394 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
395 newInst.Dst[0].Register.Index = tmp0;
396 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_Y;
397 newInst.Instruction.NumSrcRegs = 2;
398 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
399 newInst.Src[0].Register.Index = tmp0;
400 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X;
401 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
402 newInst.Src[1].Register.Index = texInput;
403 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_Z;
404 ctx->emit_instruction(ctx, &newInst);
405
406 /* CMP t0.w, -t0.y, tex.w, t0.w;
407 * # if -t0.y < 0 then
408 * t0.w = 1
409 * else
410 * t0.w = t0.w
411 */
412 newInst = tgsi_default_full_instruction();
413 newInst.Instruction.Opcode = TGSI_OPCODE_CMP;
414 newInst.Instruction.NumDstRegs = 1;
415 newInst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
416 newInst.Dst[0].Register.Index = tmp0;
417 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
418 newInst.Instruction.NumSrcRegs = 3;
419 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
420 newInst.Src[0].Register.Index = tmp0;
421 newInst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_Y;
422 newInst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y;
423 newInst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Y;
424 newInst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Y;
425 newInst.Src[0].Register.Negate = 1;
426 newInst.Src[1].Register.File = TGSI_FILE_INPUT;
427 newInst.Src[1].Register.Index = texInput;
428 newInst.Src[1].Register.SwizzleX = TGSI_SWIZZLE_W;
429 newInst.Src[1].Register.SwizzleY = TGSI_SWIZZLE_W;
430 newInst.Src[1].Register.SwizzleZ = TGSI_SWIZZLE_W;
431 newInst.Src[1].Register.SwizzleW = TGSI_SWIZZLE_W;
432 newInst.Src[2].Register.File = TGSI_FILE_TEMPORARY;
433 newInst.Src[2].Register.Index = tmp0;
434 newInst.Src[2].Register.SwizzleX = TGSI_SWIZZLE_W;
435 newInst.Src[2].Register.SwizzleY = TGSI_SWIZZLE_W;
436 newInst.Src[2].Register.SwizzleZ = TGSI_SWIZZLE_W;
437 newInst.Src[2].Register.SwizzleW = TGSI_SWIZZLE_W;
438 ctx->emit_instruction(ctx, &newInst);
439
440 }
441
442 if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
443 /* add alpha modulation code at tail of program */
444
445 /* MOV result.color.xyz, colorTemp; */
446 newInst = tgsi_default_full_instruction();
447 newInst.Instruction.Opcode = TGSI_OPCODE_MOV;
448 newInst.Instruction.NumDstRegs = 1;
449 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
450 newInst.Dst[0].Register.Index = aactx->colorOutput;
451 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
452 newInst.Instruction.NumSrcRegs = 1;
453 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
454 newInst.Src[0].Register.Index = aactx->colorTemp;
455 ctx->emit_instruction(ctx, &newInst);
456
457 /* MUL result.color.w, colorTemp, tmp0.w; */
458 newInst = tgsi_default_full_instruction();
459 newInst.Instruction.Opcode = TGSI_OPCODE_MUL;
460 newInst.Instruction.NumDstRegs = 1;
461 newInst.Dst[0].Register.File = TGSI_FILE_OUTPUT;
462 newInst.Dst[0].Register.Index = aactx->colorOutput;
463 newInst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_W;
464 newInst.Instruction.NumSrcRegs = 2;
465 newInst.Src[0].Register.File = TGSI_FILE_TEMPORARY;
466 newInst.Src[0].Register.Index = aactx->colorTemp;
467 newInst.Src[1].Register.File = TGSI_FILE_TEMPORARY;
468 newInst.Src[1].Register.Index = aactx->tmp0;
469 ctx->emit_instruction(ctx, &newInst);
470 }
471 else {
472 /* Not an END instruction.
473 * Look for writes to result.color and replace with colorTemp reg.
474 */
475 uint i;
476
477 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
478 struct tgsi_full_dst_register *dst = &inst->Dst[i];
479 if (dst->Register.File == TGSI_FILE_OUTPUT &&
480 dst->Register.Index == aactx->colorOutput) {
481 dst->Register.File = TGSI_FILE_TEMPORARY;
482 dst->Register.Index = aactx->colorTemp;
483 }
484 }
485 }
486
487 ctx->emit_instruction(ctx, inst);
488 }
489
490
491 /**
492 * Generate the frag shader we'll use for drawing AA points.
493 * This will be the user's shader plus some texture/modulate instructions.
494 */
495 static boolean
496 generate_aapoint_fs(struct aapoint_stage *aapoint)
497 {
498 const struct pipe_shader_state *orig_fs = &aapoint->fs->state;
499 struct pipe_shader_state aapoint_fs;
500 struct aa_transform_context transform;
501 const uint newLen = tgsi_num_tokens(orig_fs->tokens) + NUM_NEW_TOKENS;
502
503 aapoint_fs = *orig_fs; /* copy to init */
504 aapoint_fs.tokens = tgsi_alloc_tokens(newLen);
505 if (aapoint_fs.tokens == NULL)
506 return FALSE;
507
508 memset(&transform, 0, sizeof(transform));
509 transform.colorOutput = -1;
510 transform.maxInput = -1;
511 transform.maxGeneric = -1;
512 transform.colorTemp = -1;
513 transform.tmp0 = -1;
514 transform.firstInstruction = TRUE;
515 transform.base.transform_instruction = aa_transform_inst;
516 transform.base.transform_declaration = aa_transform_decl;
517
518 tgsi_transform_shader(orig_fs->tokens,
519 (struct tgsi_token *) aapoint_fs.tokens,
520 newLen, &transform.base);
521
522 #if 0 /* DEBUG */
523 printf("draw_aapoint, orig shader:\n");
524 tgsi_dump(orig_fs->tokens, 0);
525 printf("draw_aapoint, new shader:\n");
526 tgsi_dump(aapoint_fs.tokens, 0);
527 #endif
528
529 aapoint->fs->aapoint_fs
530 = aapoint->driver_create_fs_state(aapoint->pipe, &aapoint_fs);
531 if (aapoint->fs->aapoint_fs == NULL)
532 goto fail;
533
534 aapoint->fs->generic_attrib = transform.maxGeneric + 1;
535 FREE((void *)aapoint_fs.tokens);
536 return TRUE;
537
538 fail:
539 FREE((void *)aapoint_fs.tokens);
540 return FALSE;
541 }
542
543
544 /**
545 * When we're about to draw our first AA point in a batch, this function is
546 * called to tell the driver to bind our modified fragment shader.
547 */
548 static boolean
549 bind_aapoint_fragment_shader(struct aapoint_stage *aapoint)
550 {
551 struct draw_context *draw = aapoint->stage.draw;
552
553 if (!aapoint->fs->aapoint_fs &&
554 !generate_aapoint_fs(aapoint))
555 return FALSE;
556
557 draw->suspend_flushing = TRUE;
558 aapoint->driver_bind_fs_state(aapoint->pipe, aapoint->fs->aapoint_fs);
559 draw->suspend_flushing = FALSE;
560
561 return TRUE;
562 }
563
564
565
566 static INLINE struct aapoint_stage *
567 aapoint_stage( struct draw_stage *stage )
568 {
569 return (struct aapoint_stage *) stage;
570 }
571
572
573
574
575 /**
576 * Draw an AA point by drawing a quad.
577 */
578 static void
579 aapoint_point(struct draw_stage *stage, struct prim_header *header)
580 {
581 const struct aapoint_stage *aapoint = aapoint_stage(stage);
582 struct prim_header tri;
583 struct vertex_header *v[4];
584 const uint tex_slot = aapoint->tex_slot;
585 const uint pos_slot = aapoint->pos_slot;
586 float radius, *pos, *tex;
587 uint i;
588 float k;
589
590 if (aapoint->psize_slot >= 0) {
591 radius = 0.5f * header->v[0]->data[aapoint->psize_slot][0];
592 }
593 else {
594 radius = aapoint->radius;
595 }
596
597 /*
598 * Note: the texcoords (generic attrib, really) we use are special:
599 * The S and T components simply vary from -1 to +1.
600 * The R component is k, below.
601 * The Q component is 1.0 and will used as a handy constant in the
602 * fragment shader.
603 */
604
605 /*
606 * k is the threshold distance from the point's center at which
607 * we begin alpha attenuation (the coverage value).
608 * Operating within a unit circle, we'll compute the fragment's
609 * distance 'd' from the center point using the texcoords.
610 * IF d > 1.0 THEN
611 * KILL fragment
612 * ELSE IF d > k THEN
613 * compute coverage in [0,1] proportional to d in [k, 1].
614 * ELSE
615 * coverage = 1.0; // full coverage
616 * ENDIF
617 *
618 * Note: the ELSEIF and ELSE clauses are actually implemented with CMP to
619 * avoid using IF/ELSE/ENDIF TGSI opcodes.
620 */
621
622 #if !NORMALIZE
623 k = 1.0f / radius;
624 k = 1.0f - 2.0f * k + k * k;
625 #else
626 k = 1.0f - 1.0f / radius;
627 #endif
628
629 /* allocate/dup new verts */
630 for (i = 0; i < 4; i++) {
631 v[i] = dup_vert(stage, header->v[0], i);
632 }
633
634 /* new verts */
635 pos = v[0]->data[pos_slot];
636 pos[0] -= radius;
637 pos[1] -= radius;
638
639 pos = v[1]->data[pos_slot];
640 pos[0] += radius;
641 pos[1] -= radius;
642
643 pos = v[2]->data[pos_slot];
644 pos[0] += radius;
645 pos[1] += radius;
646
647 pos = v[3]->data[pos_slot];
648 pos[0] -= radius;
649 pos[1] += radius;
650
651 /* new texcoords */
652 tex = v[0]->data[tex_slot];
653 ASSIGN_4V(tex, -1, -1, k, 1);
654
655 tex = v[1]->data[tex_slot];
656 ASSIGN_4V(tex, 1, -1, k, 1);
657
658 tex = v[2]->data[tex_slot];
659 ASSIGN_4V(tex, 1, 1, k, 1);
660
661 tex = v[3]->data[tex_slot];
662 ASSIGN_4V(tex, -1, 1, k, 1);
663
664 /* emit 2 tris for the quad strip */
665 tri.v[0] = v[0];
666 tri.v[1] = v[1];
667 tri.v[2] = v[2];
668 stage->next->tri( stage->next, &tri );
669
670 tri.v[0] = v[0];
671 tri.v[1] = v[2];
672 tri.v[2] = v[3];
673 stage->next->tri( stage->next, &tri );
674 }
675
676
677 static void
678 aapoint_first_point(struct draw_stage *stage, struct prim_header *header)
679 {
680 auto struct aapoint_stage *aapoint = aapoint_stage(stage);
681 struct draw_context *draw = stage->draw;
682
683 assert(draw->rasterizer->point_smooth);
684
685 if (draw->rasterizer->point_size <= 2.0)
686 aapoint->radius = 1.0;
687 else
688 aapoint->radius = 0.5f * draw->rasterizer->point_size;
689
690 /*
691 * Bind (generate) our fragprog.
692 */
693 bind_aapoint_fragment_shader(aapoint);
694
695 /* update vertex attrib info */
696 aapoint->tex_slot = draw_current_shader_outputs(draw);
697 assert(aapoint->tex_slot > 0); /* output[0] is vertex pos */
698
699 aapoint->pos_slot = draw_current_shader_position_output(draw);
700
701 draw->extra_shader_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
702 draw->extra_shader_outputs.semantic_index = aapoint->fs->generic_attrib;
703 draw->extra_shader_outputs.slot = aapoint->tex_slot;
704
705 /* find psize slot in post-transform vertex */
706 aapoint->psize_slot = -1;
707 if (draw->rasterizer->point_size_per_vertex) {
708 /* find PSIZ vertex output */
709 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
710 uint i;
711 for (i = 0; i < vs->info.num_outputs; i++) {
712 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
713 aapoint->psize_slot = i;
714 break;
715 }
716 }
717 }
718
719 /* now really draw first point */
720 stage->point = aapoint_point;
721 stage->point(stage, header);
722 }
723
724
725 static void
726 aapoint_flush(struct draw_stage *stage, unsigned flags)
727 {
728 struct draw_context *draw = stage->draw;
729 struct aapoint_stage *aapoint = aapoint_stage(stage);
730 struct pipe_context *pipe = aapoint->pipe;
731
732 stage->point = aapoint_first_point;
733 stage->next->flush( stage->next, flags );
734
735 /* restore original frag shader */
736 draw->suspend_flushing = TRUE;
737 aapoint->driver_bind_fs_state(pipe, aapoint->fs->driver_fs);
738 draw->suspend_flushing = FALSE;
739
740 draw->extra_shader_outputs.slot = 0;
741 }
742
743
744 static void
745 aapoint_reset_stipple_counter(struct draw_stage *stage)
746 {
747 stage->next->reset_stipple_counter( stage->next );
748 }
749
750
751 static void
752 aapoint_destroy(struct draw_stage *stage)
753 {
754 draw_free_temp_verts( stage );
755 FREE( stage );
756 }
757
758
759 static struct aapoint_stage *
760 draw_aapoint_stage(struct draw_context *draw)
761 {
762 struct aapoint_stage *aapoint = CALLOC_STRUCT(aapoint_stage);
763 if (aapoint == NULL)
764 goto fail;
765
766 if (!draw_alloc_temp_verts( &aapoint->stage, 4 ))
767 goto fail;
768
769 aapoint->stage.draw = draw;
770 aapoint->stage.name = "aapoint";
771 aapoint->stage.next = NULL;
772 aapoint->stage.point = aapoint_first_point;
773 aapoint->stage.line = draw_pipe_passthrough_line;
774 aapoint->stage.tri = draw_pipe_passthrough_tri;
775 aapoint->stage.flush = aapoint_flush;
776 aapoint->stage.reset_stipple_counter = aapoint_reset_stipple_counter;
777 aapoint->stage.destroy = aapoint_destroy;
778
779 return aapoint;
780
781 fail:
782 if (aapoint)
783 aapoint_destroy(&aapoint->stage);
784
785 return NULL;
786
787 }
788
789
790 static struct aapoint_stage *
791 aapoint_stage_from_pipe(struct pipe_context *pipe)
792 {
793 struct draw_context *draw = (struct draw_context *) pipe->draw;
794 return aapoint_stage(draw->pipeline.aapoint);
795 }
796
797
798 /**
799 * This function overrides the driver's create_fs_state() function and
800 * will typically be called by the state tracker.
801 */
802 static void *
803 aapoint_create_fs_state(struct pipe_context *pipe,
804 const struct pipe_shader_state *fs)
805 {
806 struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);
807 struct aapoint_fragment_shader *aafs = CALLOC_STRUCT(aapoint_fragment_shader);
808 if (aafs == NULL)
809 return NULL;
810
811 aafs->state = *fs;
812
813 /* pass-through */
814 aafs->driver_fs = aapoint->driver_create_fs_state(aapoint->pipe, fs);
815
816 return aafs;
817 }
818
819
820 static void
821 aapoint_bind_fs_state(struct pipe_context *pipe, void *fs)
822 {
823 struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);
824 struct aapoint_fragment_shader *aafs = (struct aapoint_fragment_shader *) fs;
825 /* save current */
826 aapoint->fs = aafs;
827 /* pass-through */
828 aapoint->driver_bind_fs_state(aapoint->pipe,
829 (aafs ? aafs->driver_fs : NULL));
830 }
831
832
833 static void
834 aapoint_delete_fs_state(struct pipe_context *pipe, void *fs)
835 {
836 struct aapoint_stage *aapoint = aapoint_stage_from_pipe(pipe);
837 struct aapoint_fragment_shader *aafs = (struct aapoint_fragment_shader *) fs;
838
839 /* pass-through */
840 aapoint->driver_delete_fs_state(aapoint->pipe, aafs->driver_fs);
841
842 if (aafs->aapoint_fs)
843 aapoint->driver_delete_fs_state(aapoint->pipe, aafs->aapoint_fs);
844
845 FREE(aafs);
846 }
847
848
849 /**
850 * Called by drivers that want to install this AA point prim stage
851 * into the draw module's pipeline. This will not be used if the
852 * hardware has native support for AA points.
853 */
854 boolean
855 draw_install_aapoint_stage(struct draw_context *draw,
856 struct pipe_context *pipe)
857 {
858 struct aapoint_stage *aapoint;
859
860 pipe->draw = (void *) draw;
861
862 /*
863 * Create / install AA point drawing / prim stage
864 */
865 aapoint = draw_aapoint_stage( draw );
866 if (aapoint == NULL)
867 return FALSE;
868
869 aapoint->pipe = pipe;
870
871 /* save original driver functions */
872 aapoint->driver_create_fs_state = pipe->create_fs_state;
873 aapoint->driver_bind_fs_state = pipe->bind_fs_state;
874 aapoint->driver_delete_fs_state = pipe->delete_fs_state;
875
876 /* override the driver's functions */
877 pipe->create_fs_state = aapoint_create_fs_state;
878 pipe->bind_fs_state = aapoint_bind_fs_state;
879 pipe->delete_fs_state = aapoint_delete_fs_state;
880
881 draw->pipeline.aapoint = &aapoint->stage;
882
883 return TRUE;
884 }