aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/tgsi/tgsi_point_sprite.c
blob: cb8dbcb29ec8a279647b0ff574553363042e2655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
 * Copyright 2014 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/**
 * This utility transforms the geometry shader to emulate point sprite by
 * drawing a quad. It also adds an extra output for the original point position
 * if the point position is to be written to a stream output buffer.
 * Note: It assumes the driver will add a constant for the inverse viewport
 *       after the user defined constants.
 */

#include "util/u_debug.h"
#include "util/u_math.h"
#include "tgsi_info.h"
#include "tgsi_point_sprite.h"
#include "tgsi_transform.h"
#include "pipe/p_state.h"

#define INVALID_INDEX 9999

/* Set swizzle based on the immediates (0, 1, 0, -1) */
static inline unsigned
set_swizzle(int x, int y, int z, int w)
{
   static const unsigned map[3] = {TGSI_SWIZZLE_W, TGSI_SWIZZLE_X,
                                   TGSI_SWIZZLE_Y};
   assert(x >= -1);
   assert(x <= 1);
   assert(y >= -1);
   assert(y <= 1);
   assert(z >= -1);
   assert(z <= 1);
   assert(w >= -1);
   assert(w <= 1);

   return map[x+1] | (map[y+1] << 2) | (map[z+1] << 4) | (map[w+1] << 6);
}

static inline unsigned
get_swizzle(unsigned swizzle, unsigned component)
{
   assert(component < 4);
   return (swizzle >> (component * 2)) & 0x3;
}

struct psprite_transform_context
{
   struct tgsi_transform_context base;
   unsigned num_tmp;
   unsigned num_out;
   unsigned num_orig_out;
   unsigned num_const;
   unsigned num_imm;
   unsigned point_size_in;          // point size input
   unsigned point_size_out;         // point size output
   unsigned point_size_tmp;         // point size temp
   unsigned point_pos_in;           // point pos input
   unsigned point_pos_out;          // point pos output
   unsigned point_pos_sout;         // original point pos for streamout
   unsigned point_pos_tmp;          // point pos temp
   unsigned point_scale_tmp;        // point scale temp
   unsigned point_color_out;        // point color output
   unsigned point_color_tmp;        // point color temp
   unsigned point_imm;              // point immediates
   unsigned point_ivp;              // point inverseViewport constant
   unsigned point_dir_swz[4];       // point direction swizzle
   unsigned point_coord_swz[4];     // point coord swizzle
   unsigned point_coord_enable;     // point coord enable mask
   unsigned point_coord_decl;       // point coord output declared mask
   unsigned point_coord_out;        // point coord output starting index
   unsigned point_coord_aa;         // aa point coord semantic index
   unsigned point_coord_k;          // aa point coord threshold distance
   unsigned stream_out_point_pos:1; // set if to stream out original point pos
   unsigned aa_point:1;             // set if doing aa point
   unsigned out_tmp_index[PIPE_MAX_SHADER_OUTPUTS];
   int max_generic;
};

static inline struct psprite_transform_context *
psprite_transform_context(struct tgsi_transform_context *ctx)
{
   return (struct psprite_transform_context *) ctx;
}


/**
 * TGSI declaration transform callback.
 */
static void
psprite_decl(struct tgsi_transform_context *ctx,
             struct tgsi_full_declaration *decl)
{
   struct psprite_transform_context *ts = psprite_transform_context(ctx);

   if (decl->Declaration.File == TGSI_FILE_INPUT) {
      if (decl->Semantic.Name == TGSI_SEMANTIC_PSIZE) {
         ts->point_size_in = decl->Range.First;
      }
      else if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
         ts->point_pos_in = decl->Range.First;
      }
   }
   else if (decl->Declaration.File == TGSI_FILE_OUTPUT) {
      if (decl->Semantic.Name == TGSI_SEMANTIC_PSIZE) {
         ts->point_size_out = decl->Range.First;
      }
      else if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
         ts->point_pos_out = decl->Range.First;
      }
      else if (decl->Semantic.Name == TGSI_SEMANTIC_GENERIC &&
               decl->Semantic.Index < 32) {
         ts->point_coord_decl |= 1 << decl->Semantic.Index;
         ts->max_generic = MAX2(ts->max_generic, decl->Semantic.Index);
      }
      ts->num_out = MAX2(ts->num_out, decl->Range.Last + 1);
   }
   else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
      ts->num_tmp = MAX2(ts->num_tmp, decl->Range.Last + 1);
   }
   else if (decl->Declaration.File == TGSI_FILE_CONSTANT) {
      ts->num_const = MAX2(ts->num_const, decl->Range.Last + 1);
   }

   ctx->emit_declaration(ctx, decl);
}

/**
 * TGSI immediate declaration transform callback.
 */
static void
psprite_immediate(struct tgsi_transform_context *ctx,
                  struct tgsi_full_immediate *imm)
{
   struct psprite_transform_context *ts = psprite_transform_context(ctx);

   ctx->emit_immediate(ctx, imm);
   ts->num_imm++;
}


/**
 * TGSI transform prolog callback.
 */
static void
psprite_prolog(struct tgsi_transform_context *ctx)
{
   struct psprite_transform_context *ts = psprite_transform_context(ctx);
   unsigned point_coord_enable, en;
   int i;

   /* Replace output registers with temporary registers */
   for (i = 0; i < ts->num_out; i++) {
      ts->out_tmp_index[i] = ts->num_tmp++;
   }
   ts->num_orig_out = ts->num_out;

   /* Declare a tmp register for point scale */
   ts->point_scale_tmp = ts->num_tmp++;

   if (ts->point_size_out != INVALID_INDEX)
      ts->point_size_tmp = ts->out_tmp_index[ts->point_size_out];
   else
      ts->point_size_tmp = ts->num_tmp++;

   assert(ts->point_pos_out != INVALID_INDEX);
   ts->point_pos_tmp = ts->out_tmp_index[ts->point_pos_out];
   ts->out_tmp_index[ts->point_pos_out] = INVALID_INDEX;

   /* Declare one more tmp register for point coord threshold distance
    * if we are generating anti-aliased point.
    */
   if (ts->aa_point)
      ts->point_coord_k = ts->num_tmp++;

   tgsi_transform_temps_decl(ctx, ts->point_size_tmp, ts->num_tmp-1);

   /* Declare an extra output for the original point position for stream out */
   if (ts->stream_out_point_pos) {
      ts->point_pos_sout = ts->num_out++;
      tgsi_transform_output_decl(ctx, ts->point_pos_sout,
                                 TGSI_SEMANTIC_GENERIC, 0, 0);
   }

   /* point coord outputs to be declared */
   point_coord_enable = ts->point_coord_enable & ~ts->point_coord_decl;

   /* Declare outputs for those point coord that are enabled but are not
    * already declared in this shader.
    */
   ts->point_coord_out = ts->num_out;
   if (point_coord_enable) {
      for (i = 0, en = point_coord_enable; en; en>>=1, i++) {
         if (en & 0x1) {
            tgsi_transform_output_decl(ctx, ts->num_out++,
                                       TGSI_SEMANTIC_GENERIC, i, 0);
            ts->max_generic = MAX2(ts->max_generic, i);
         }
      }
   }

   /* add an extra generic output for aa point texcoord */
   if (ts->aa_point) {
      ts->point_coord_aa = ts->max_generic + 1;
      assert((ts->point_coord_enable & (1 << ts->point_coord_aa)) == 0);
      ts->point_coord_enable |= 1 << (ts->point_coord_aa);
      tgsi_transform_output_decl(ctx, ts->num_out++, TGSI_SEMANTIC_GENERIC,
                                 ts->point_coord_aa, 0);
   }

   /* Declare extra immediates */
   ts->point_imm = ts->num_imm;
   tgsi_transform_immediate_decl(ctx, 0, 1, 0.5, -1);

   /* Declare point constant -
    * constant.xy -- inverseViewport
    * constant.z -- current point size
    * constant.w -- max point size
    * The driver needs to add this constant to the constant buffer
    */
   ts->point_ivp = ts->num_const++;
   tgsi_transform_const_decl(ctx, ts->point_ivp, ts->point_ivp);

   /* If this geometry shader does not specify point size,
    * get the current point size from the point constant.
    */
   if (ts->point_size_out == INVALID_INDEX) {
      struct tgsi_full_instruction inst;

      inst = tgsi_default_full_instruction();
      inst.Instruction.Opcode = TGSI_OPCODE_MOV;
      inst.Instruction.NumDstRegs = 1;
      tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
                             ts->point_size_tmp, TGSI_WRITEMASK_XYZW);
      inst.Instruction.NumSrcRegs = 1;
      tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_CONSTANT,
                             ts->point_ivp, TGSI_SWIZZLE_Z,
                             TGSI_SWIZZLE_Z, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_Z);
      ctx->emit_instruction(ctx, &inst);
   }
}


/**
 * Add the point sprite emulation instructions at the emit vertex instruction
 */
static void
psprite_emit_vertex_inst(struct tgsi_transform_context *ctx,
                         struct tgsi_full_instruction *vert_inst)
{
   struct psprite_transform_context *ts = psprite_transform_context(ctx);
   struct tgsi_full_instruction inst;
   unsigned point_coord_enable, en;
   unsigned i, j, s;

   /* new point coord outputs */
   point_coord_enable = ts->point_coord_enable & ~ts->point_coord_decl;

   /* OUTPUT[pos_sout] = TEMP[pos] */
   if (ts->point_pos_sout != INVALID_INDEX) {
      tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
                              TGSI_FILE_OUTPUT, ts->point_pos_sout,
                              TGSI_WRITEMASK_XYZW,
                              TGSI_FILE_TEMPORARY, ts->point_pos_tmp);
   }

   /**
    * Set up the point scale vector
    * scale = pointSize * pos.w * inverseViewport
    */

   /* MUL point_scale.x, point_size.x, point_pos.w */
   tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_MUL,
                  TGSI_FILE_TEMPORARY, ts->point_scale_tmp, TGSI_WRITEMASK_X,
                  TGSI_FILE_TEMPORARY, ts->point_size_tmp, TGSI_SWIZZLE_X,
                  TGSI_FILE_TEMPORARY, ts->point_pos_tmp, TGSI_SWIZZLE_W);

   /* MUL point_scale.xy, point_scale.xx, inverseViewport.xy */
   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = TGSI_OPCODE_MUL;
   inst.Instruction.NumDstRegs = 1;
   tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
                          ts->point_scale_tmp, TGSI_WRITEMASK_XY);
   inst.Instruction.NumSrcRegs = 2;
   tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY,
                          ts->point_scale_tmp, TGSI_SWIZZLE_X,
                          TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
   tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_CONSTANT,
                          ts->point_ivp, TGSI_SWIZZLE_X,
                          TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_Z);
   ctx->emit_instruction(ctx, &inst);

   /**
    * Set up the point coord threshold distance
    * k = 0.5 - 1 / pointsize
    */
   if (ts->aa_point) {
      tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_DIV,
                                  TGSI_FILE_TEMPORARY, ts->point_coord_k,
                                  TGSI_WRITEMASK_X,
                                  TGSI_FILE_IMMEDIATE, ts->point_imm,
                                  TGSI_SWIZZLE_Y,
                                  TGSI_FILE_TEMPORARY, ts->point_size_tmp,
                                  TGSI_SWIZZLE_X);

      tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_SUB,
                                  TGSI_FILE_TEMPORARY, ts->point_coord_k,
                                  TGSI_WRITEMASK_X,
                                  TGSI_FILE_IMMEDIATE, ts->point_imm,
                                  TGSI_SWIZZLE_Z,
                                  TGSI_FILE_TEMPORARY, ts->point_coord_k,
                                  TGSI_SWIZZLE_X);
   }


   for (i = 0; i < 4; i++) {
      unsigned point_dir_swz = ts->point_dir_swz[i];
      unsigned point_coord_swz = ts->point_coord_swz[i];

      /* All outputs need to be emitted for each vertex */
      for (j = 0; j < ts->num_orig_out; j++) {
         if (ts->out_tmp_index[j] != INVALID_INDEX) {
            tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
                                    TGSI_FILE_OUTPUT, j,
                                    TGSI_WRITEMASK_XYZW,
                                    TGSI_FILE_TEMPORARY, ts->out_tmp_index[j]);
         }
      }

      /* pos = point_scale * point_dir + point_pos */
      inst = tgsi_default_full_instruction();
      inst.Instruction.Opcode = TGSI_OPCODE_MAD;
      inst.Instruction.NumDstRegs = 1;
      tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_OUTPUT, ts->point_pos_out,
                             TGSI_WRITEMASK_XYZW);
      inst.Instruction.NumSrcRegs = 3;
      tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY, ts->point_scale_tmp,
                             TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y, TGSI_SWIZZLE_X,
                             TGSI_SWIZZLE_X);
      tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_IMMEDIATE, ts->point_imm,
                             get_swizzle(point_dir_swz, 0),
                             get_swizzle(point_dir_swz, 1),
                             get_swizzle(point_dir_swz, 2),
                             get_swizzle(point_dir_swz, 3));
      tgsi_transform_src_reg(&inst.Src[2], TGSI_FILE_TEMPORARY, ts->point_pos_tmp,
                             TGSI_SWIZZLE_X, TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Z,
                             TGSI_SWIZZLE_W);
      ctx->emit_instruction(ctx, &inst);

      /* point coord */
      for (j = 0, s = 0, en = point_coord_enable; en; en>>=1, s++) {
         unsigned dstReg;

         if (en & 0x1) {
            dstReg = ts->point_coord_out + j;

            inst = tgsi_default_full_instruction();
            inst.Instruction.Opcode = TGSI_OPCODE_MOV;
            inst.Instruction.NumDstRegs = 1;
            tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_OUTPUT,
                                   dstReg, TGSI_WRITEMASK_XYZW);
            inst.Instruction.NumSrcRegs = 1;
            tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_IMMEDIATE, ts->point_imm,
                                   get_swizzle(point_coord_swz, 0),
                                   get_swizzle(point_coord_swz, 1),
                                   get_swizzle(point_coord_swz, 2),
                                   get_swizzle(point_coord_swz, 3));
            ctx->emit_instruction(ctx, &inst);

            /* MOV point_coord.z  point_coord_k.x */
            if (s == ts->point_coord_aa) {
               tgsi_transform_op1_swz_inst(ctx, TGSI_OPCODE_MOV,
                                           TGSI_FILE_OUTPUT, dstReg, TGSI_WRITEMASK_Z,
                                           TGSI_FILE_TEMPORARY, ts->point_coord_k,
                                           TGSI_SWIZZLE_X);
            }
            j++;  /* the next point coord output offset */
         }
      }

      /* Emit the EMIT instruction for each vertex of the quad */
      ctx->emit_instruction(ctx, vert_inst);
   }

   /* Emit the ENDPRIM instruction for the quad */
   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = TGSI_OPCODE_ENDPRIM;
   inst.Instruction.NumDstRegs = 0;
   inst.Instruction.NumSrcRegs = 1;
   inst.Src[0] = vert_inst->Src[0];
   ctx->emit_instruction(ctx, &inst);
}


/**
 * TGSI instruction transform callback.
 */
static void
psprite_inst(struct tgsi_transform_context *ctx,
             struct tgsi_full_instruction *inst)
{
   struct psprite_transform_context *ts = psprite_transform_context(ctx);

   if (inst->Instruction.Opcode == TGSI_OPCODE_EMIT) {
      psprite_emit_vertex_inst(ctx, inst);
   }
   else if (inst->Dst[0].Register.File == TGSI_FILE_OUTPUT &&
            inst->Dst[0].Register.Index == ts->point_size_out) {
      /**
       * Replace point size output reg with tmp reg.
       * The tmp reg will be later used as a src reg for computing
       * the point scale factor.
       */
      inst->Dst[0].Register.File = TGSI_FILE_TEMPORARY;
      inst->Dst[0].Register.Index = ts->point_size_tmp;
      ctx->emit_instruction(ctx, inst);

      /* Clamp the point size */
      /* MAX point_size_tmp.x, point_size_tmp.x, point_imm.y */
      tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_MAX,
                 TGSI_FILE_TEMPORARY, ts->point_size_tmp, TGSI_WRITEMASK_X,
                 TGSI_FILE_TEMPORARY, ts->point_size_tmp, TGSI_SWIZZLE_X,
                 TGSI_FILE_IMMEDIATE, ts->point_imm, TGSI_SWIZZLE_Y);

      /* MIN point_size_tmp.x, point_size_tmp.x, point_ivp.w */
      tgsi_transform_op2_swz_inst(ctx, TGSI_OPCODE_MIN,
                 TGSI_FILE_TEMPORARY, ts->point_size_tmp, TGSI_WRITEMASK_X,
                 TGSI_FILE_TEMPORARY, ts->point_size_tmp, TGSI_SWIZZLE_X,
                 TGSI_FILE_CONSTANT, ts->point_ivp, TGSI_SWIZZLE_W);
   }
   else if (inst->Dst[0].Register.File == TGSI_FILE_OUTPUT &&
            inst->Dst[0].Register.Index == ts->point_pos_out) {
      /**
       * Replace point pos output reg with tmp reg.
       */
      inst->Dst[0].Register.File = TGSI_FILE_TEMPORARY;
      inst->Dst[0].Register.Index = ts->point_pos_tmp;
      ctx->emit_instruction(ctx, inst);
   }
   else if (inst->Dst[0].Register.File == TGSI_FILE_OUTPUT) {
      /**
       * Replace output reg with tmp reg.
       */
      inst->Dst[0].Register.File = TGSI_FILE_TEMPORARY;
      inst->Dst[0].Register.Index = ts->out_tmp_index[inst->Dst[0].Register.Index];
      ctx->emit_instruction(ctx, inst);
   }
   else {
      ctx->emit_instruction(ctx, inst);
   }
}


/**
 * TGSI property instruction transform callback.
 * Transforms a point into a 4-vertex triangle strip.
 */
static void
psprite_property(struct tgsi_transform_context *ctx,
                 struct tgsi_full_property *prop)
{
   switch (prop->Property.PropertyName) {
   case TGSI_PROPERTY_GS_OUTPUT_PRIM:
       prop->u[0].Data = PIPE_PRIM_TRIANGLE_STRIP;
       break;
   case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
       prop->u[0].Data *= 4;
       break;
   default:
       break;
   }
   ctx->emit_property(ctx, prop);
}

/**
 * TGSI utility to transform a geometry shader to support point sprite.
 */
struct tgsi_token *
tgsi_add_point_sprite(const struct tgsi_token *tokens_in,
                      const unsigned point_coord_enable,
                      const bool sprite_origin_lower_left,
                      const bool stream_out_point_pos,
                      int *aa_point_coord_index)
{
   struct psprite_transform_context transform;
   const uint num_new_tokens = 200; /* should be enough */
   const uint new_len = tgsi_num_tokens(tokens_in) + num_new_tokens;
   struct tgsi_token *new_tokens;

   /* setup transformation context */
   memset(&transform, 0, sizeof(transform));
   transform.base.transform_declaration = psprite_decl;
   transform.base.transform_instruction = psprite_inst;
   transform.base.transform_property = psprite_property;
   transform.base.transform_immediate = psprite_immediate;
   transform.base.prolog = psprite_prolog;

   transform.point_size_in = INVALID_INDEX;
   transform.point_size_out = INVALID_INDEX;
   transform.point_size_tmp = INVALID_INDEX;
   transform.point_pos_in = INVALID_INDEX;
   transform.point_pos_out = INVALID_INDEX;
   transform.point_pos_sout = INVALID_INDEX;
   transform.point_pos_tmp = INVALID_INDEX;
   transform.point_scale_tmp = INVALID_INDEX;
   transform.point_imm = INVALID_INDEX;
   transform.point_coord_aa = INVALID_INDEX;
   transform.point_coord_k = INVALID_INDEX;

   transform.stream_out_point_pos = stream_out_point_pos;
   transform.point_coord_enable = point_coord_enable;
   transform.aa_point = aa_point_coord_index != NULL;
   transform.max_generic = -1;

   /* point sprite directions based on the immediates (0, 1, 0.5, -1) */
   /* (-1, -1, 0, 0) */
   transform.point_dir_swz[0] = set_swizzle(-1, -1, 0, 0);
   /* (-1, 1, 0, 0) */
   transform.point_dir_swz[1] = set_swizzle(-1, 1, 0, 0);
   /* (1, -1, 0, 0) */
   transform.point_dir_swz[2] = set_swizzle(1, -1, 0, 0);
   /* (1, 1, 0, 0) */
   transform.point_dir_swz[3] = set_swizzle(1, 1, 0, 0);

   /* point coord based on the immediates (0, 1, 0, -1) */
   if (sprite_origin_lower_left) {
      /* (0, 0, 0, 1) */
      transform.point_coord_swz[0] = set_swizzle(0, 0, 0, 1);
      /* (0, 1, 0, 1) */
      transform.point_coord_swz[1] = set_swizzle(0, 1, 0, 1);
      /* (1, 0, 0, 1) */
      transform.point_coord_swz[2] = set_swizzle(1, 0, 0, 1);
      /* (1, 1, 0, 1) */
      transform.point_coord_swz[3] = set_swizzle(1, 1, 0, 1);
   }
   else {
      /* (0, 1, 0, 1) */
      transform.point_coord_swz[0] = set_swizzle(0, 1, 0, 1);
      /* (0, 0, 0, 1) */
      transform.point_coord_swz[1] = set_swizzle(0, 0, 0, 1);
      /* (1, 1, 0, 1) */
      transform.point_coord_swz[2] = set_swizzle(1, 1, 0, 1);
      /* (1, 0, 0, 1) */
      transform.point_coord_swz[3] = set_swizzle(1, 0, 0, 1);
   }


   /* allocate new tokens buffer */
   new_tokens = tgsi_alloc_tokens(new_len);
   if (!new_tokens)
      return NULL;

   /* transform the shader */
   tgsi_transform_shader(tokens_in, new_tokens, new_len, &transform.base);

   if (aa_point_coord_index)
      *aa_point_coord_index = transform.point_coord_aa;

   return new_tokens;
}