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