1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * Copyright 2010 VMware, Inc.
5bf215546Sopenharmony_ci * All Rights Reserved.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
9bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
10bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
11bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
12bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
13bf215546Sopenharmony_ci * the following conditions:
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
16bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
17bf215546Sopenharmony_ci * of the Software.
18bf215546Sopenharmony_ci *
19bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci **************************************************************************/
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/**
30bf215546Sopenharmony_ci * Polygon stipple helper module.  Drivers/GPUs which don't support polygon
31bf215546Sopenharmony_ci * stipple natively can use this module to simulate it.
32bf215546Sopenharmony_ci *
33bf215546Sopenharmony_ci * Basically, modify fragment shader to sample the 32x32 stipple pattern
34bf215546Sopenharmony_ci * texture and do a fragment kill for the 'off' bits.
35bf215546Sopenharmony_ci *
36bf215546Sopenharmony_ci * This was originally a 'draw' module stage, but since we don't need
37bf215546Sopenharmony_ci * vertex window coords or anything, it can be a stand-alone utility module.
38bf215546Sopenharmony_ci *
39bf215546Sopenharmony_ci * Authors:  Brian Paul
40bf215546Sopenharmony_ci */
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci#include "pipe/p_context.h"
44bf215546Sopenharmony_ci#include "pipe/p_defines.h"
45bf215546Sopenharmony_ci#include "pipe/p_shader_tokens.h"
46bf215546Sopenharmony_ci#include "util/u_inlines.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#include "util/format/u_format.h"
49bf215546Sopenharmony_ci#include "util/u_memory.h"
50bf215546Sopenharmony_ci#include "util/u_pstipple.h"
51bf215546Sopenharmony_ci#include "util/u_sampler.h"
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci#include "tgsi/tgsi_transform.h"
54bf215546Sopenharmony_ci#include "tgsi/tgsi_dump.h"
55bf215546Sopenharmony_ci#include "tgsi/tgsi_scan.h"
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci/** Approx number of new tokens for instructions in pstip_transform_inst() */
58bf215546Sopenharmony_ci#define NUM_NEW_TOKENS 53
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_civoid
62bf215546Sopenharmony_ciutil_pstipple_update_stipple_texture(struct pipe_context *pipe,
63bf215546Sopenharmony_ci                                     struct pipe_resource *tex,
64bf215546Sopenharmony_ci                                     const uint32_t pattern[32])
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci   static const uint bit31 = 1u << 31;
67bf215546Sopenharmony_ci   struct pipe_transfer *transfer;
68bf215546Sopenharmony_ci   ubyte *data;
69bf215546Sopenharmony_ci   int i, j;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   /* map texture memory */
72bf215546Sopenharmony_ci   data = pipe_texture_map(pipe, tex, 0, 0,
73bf215546Sopenharmony_ci                            PIPE_MAP_WRITE, 0, 0, 32, 32, &transfer);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   /*
76bf215546Sopenharmony_ci    * Load alpha texture.
77bf215546Sopenharmony_ci    * Note: 0 means keep the fragment, 255 means kill it.
78bf215546Sopenharmony_ci    * We'll negate the texel value and use KILL_IF which kills if value
79bf215546Sopenharmony_ci    * is negative.
80bf215546Sopenharmony_ci    */
81bf215546Sopenharmony_ci   for (i = 0; i < 32; i++) {
82bf215546Sopenharmony_ci      for (j = 0; j < 32; j++) {
83bf215546Sopenharmony_ci         if (pattern[i] & (bit31 >> j)) {
84bf215546Sopenharmony_ci            /* fragment "on" */
85bf215546Sopenharmony_ci            data[i * transfer->stride + j] = 0;
86bf215546Sopenharmony_ci         }
87bf215546Sopenharmony_ci         else {
88bf215546Sopenharmony_ci            /* fragment "off" */
89bf215546Sopenharmony_ci            data[i * transfer->stride + j] = 255;
90bf215546Sopenharmony_ci         }
91bf215546Sopenharmony_ci      }
92bf215546Sopenharmony_ci   }
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   /* unmap */
95bf215546Sopenharmony_ci   pipe->texture_unmap(pipe, transfer);
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci/**
100bf215546Sopenharmony_ci * Create a 32x32 alpha8 texture that encodes the given stipple pattern.
101bf215546Sopenharmony_ci */
102bf215546Sopenharmony_cistruct pipe_resource *
103bf215546Sopenharmony_ciutil_pstipple_create_stipple_texture(struct pipe_context *pipe,
104bf215546Sopenharmony_ci                                     const uint32_t pattern[32])
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   struct pipe_screen *screen = pipe->screen;
107bf215546Sopenharmony_ci   struct pipe_resource templat, *tex;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
110bf215546Sopenharmony_ci   templat.target = PIPE_TEXTURE_2D;
111bf215546Sopenharmony_ci   templat.format = PIPE_FORMAT_A8_UNORM;
112bf215546Sopenharmony_ci   templat.last_level = 0;
113bf215546Sopenharmony_ci   templat.width0 = 32;
114bf215546Sopenharmony_ci   templat.height0 = 32;
115bf215546Sopenharmony_ci   templat.depth0 = 1;
116bf215546Sopenharmony_ci   templat.array_size = 1;
117bf215546Sopenharmony_ci   templat.bind = PIPE_BIND_SAMPLER_VIEW;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   tex = screen->resource_create(screen, &templat);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   if (tex && pattern)
122bf215546Sopenharmony_ci      util_pstipple_update_stipple_texture(pipe, tex, pattern);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   return tex;
125bf215546Sopenharmony_ci}
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci/**
129bf215546Sopenharmony_ci * Create sampler view to sample the stipple texture.
130bf215546Sopenharmony_ci */
131bf215546Sopenharmony_cistruct pipe_sampler_view *
132bf215546Sopenharmony_ciutil_pstipple_create_sampler_view(struct pipe_context *pipe,
133bf215546Sopenharmony_ci                                  struct pipe_resource *tex)
134bf215546Sopenharmony_ci{
135bf215546Sopenharmony_ci   struct pipe_sampler_view templat, *sv;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   u_sampler_view_default_template(&templat, tex, tex->format);
138bf215546Sopenharmony_ci   sv = pipe->create_sampler_view(pipe, tex, &templat);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   return sv;
141bf215546Sopenharmony_ci}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci/**
145bf215546Sopenharmony_ci * Create the sampler CSO that'll be used for stippling.
146bf215546Sopenharmony_ci */
147bf215546Sopenharmony_civoid *
148bf215546Sopenharmony_ciutil_pstipple_create_sampler(struct pipe_context *pipe)
149bf215546Sopenharmony_ci{
150bf215546Sopenharmony_ci   struct pipe_sampler_state templat;
151bf215546Sopenharmony_ci   void *s;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   memset(&templat, 0, sizeof(templat));
154bf215546Sopenharmony_ci   templat.wrap_s = PIPE_TEX_WRAP_REPEAT;
155bf215546Sopenharmony_ci   templat.wrap_t = PIPE_TEX_WRAP_REPEAT;
156bf215546Sopenharmony_ci   templat.wrap_r = PIPE_TEX_WRAP_REPEAT;
157bf215546Sopenharmony_ci   templat.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
158bf215546Sopenharmony_ci   templat.min_img_filter = PIPE_TEX_FILTER_NEAREST;
159bf215546Sopenharmony_ci   templat.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
160bf215546Sopenharmony_ci   templat.normalized_coords = 1;
161bf215546Sopenharmony_ci   templat.min_lod = 0.0f;
162bf215546Sopenharmony_ci   templat.max_lod = 0.0f;
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   s = pipe->create_sampler_state(pipe, &templat);
165bf215546Sopenharmony_ci   return s;
166bf215546Sopenharmony_ci}
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci/**
171bf215546Sopenharmony_ci * Subclass of tgsi_transform_context, used for transforming the
172bf215546Sopenharmony_ci * user's fragment shader to add the extra texture sample and fragment kill
173bf215546Sopenharmony_ci * instructions.
174bf215546Sopenharmony_ci */
175bf215546Sopenharmony_cistruct pstip_transform_context {
176bf215546Sopenharmony_ci   struct tgsi_transform_context base;
177bf215546Sopenharmony_ci   struct tgsi_shader_info info;
178bf215546Sopenharmony_ci   uint tempsUsed;  /**< bitmask */
179bf215546Sopenharmony_ci   int wincoordInput;
180bf215546Sopenharmony_ci   unsigned wincoordFile;
181bf215546Sopenharmony_ci   int maxInput;
182bf215546Sopenharmony_ci   uint samplersUsed;  /**< bitfield of samplers used */
183bf215546Sopenharmony_ci   int freeSampler;  /** an available sampler for the pstipple */
184bf215546Sopenharmony_ci   int numImmed;
185bf215546Sopenharmony_ci   uint coordOrigin;
186bf215546Sopenharmony_ci   unsigned fixedUnit;
187bf215546Sopenharmony_ci   bool hasFixedUnit;
188bf215546Sopenharmony_ci};
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci/**
192bf215546Sopenharmony_ci * TGSI declaration transform callback.
193bf215546Sopenharmony_ci * Track samplers used, temps used, inputs used.
194bf215546Sopenharmony_ci */
195bf215546Sopenharmony_cistatic void
196bf215546Sopenharmony_cipstip_transform_decl(struct tgsi_transform_context *ctx,
197bf215546Sopenharmony_ci                     struct tgsi_full_declaration *decl)
198bf215546Sopenharmony_ci{
199bf215546Sopenharmony_ci   struct pstip_transform_context *pctx =
200bf215546Sopenharmony_ci      (struct pstip_transform_context *) ctx;
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   /* XXX we can use tgsi_shader_info instead of some of this */
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   if (decl->Declaration.File == TGSI_FILE_SAMPLER) {
205bf215546Sopenharmony_ci      uint i;
206bf215546Sopenharmony_ci      for (i = decl->Range.First; i <= decl->Range.Last; i++) {
207bf215546Sopenharmony_ci         pctx->samplersUsed |= 1u << i;
208bf215546Sopenharmony_ci      }
209bf215546Sopenharmony_ci   }
210bf215546Sopenharmony_ci   else if (decl->Declaration.File == pctx->wincoordFile) {
211bf215546Sopenharmony_ci      pctx->maxInput = MAX2(pctx->maxInput, (int) decl->Range.Last);
212bf215546Sopenharmony_ci      if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION)
213bf215546Sopenharmony_ci         pctx->wincoordInput = (int) decl->Range.First;
214bf215546Sopenharmony_ci   }
215bf215546Sopenharmony_ci   else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
216bf215546Sopenharmony_ci      uint i;
217bf215546Sopenharmony_ci      for (i = decl->Range.First; i <= decl->Range.Last; i++) {
218bf215546Sopenharmony_ci         pctx->tempsUsed |= (1 << i);
219bf215546Sopenharmony_ci      }
220bf215546Sopenharmony_ci   }
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   ctx->emit_declaration(ctx, decl);
223bf215546Sopenharmony_ci}
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_cistatic void
227bf215546Sopenharmony_cipstip_transform_immed(struct tgsi_transform_context *ctx,
228bf215546Sopenharmony_ci                      struct tgsi_full_immediate *immed)
229bf215546Sopenharmony_ci{
230bf215546Sopenharmony_ci   struct pstip_transform_context *pctx =
231bf215546Sopenharmony_ci      (struct pstip_transform_context *) ctx;
232bf215546Sopenharmony_ci   pctx->numImmed++;
233bf215546Sopenharmony_ci   ctx->emit_immediate(ctx, immed);
234bf215546Sopenharmony_ci}
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci/**
238bf215546Sopenharmony_ci * Find the lowest zero bit in the given word, or -1 if bitfield is all ones.
239bf215546Sopenharmony_ci */
240bf215546Sopenharmony_cistatic int
241bf215546Sopenharmony_cifree_bit(uint bitfield)
242bf215546Sopenharmony_ci{
243bf215546Sopenharmony_ci   return ffs(~bitfield) - 1;
244bf215546Sopenharmony_ci}
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci/**
248bf215546Sopenharmony_ci * TGSI transform prolog
249bf215546Sopenharmony_ci * Before the first instruction, insert our new code to sample the
250bf215546Sopenharmony_ci * stipple texture (using the fragment coord register) then kill the
251bf215546Sopenharmony_ci * fragment if the stipple texture bit is off.
252bf215546Sopenharmony_ci *
253bf215546Sopenharmony_ci * Insert:
254bf215546Sopenharmony_ci *   declare new registers
255bf215546Sopenharmony_ci *   MUL texTemp, INPUT[wincoord], 1/32;
256bf215546Sopenharmony_ci *   TEX texTemp, texTemp, sampler;
257bf215546Sopenharmony_ci *   KILL_IF -texTemp;   # if -texTemp < 0, kill fragment
258bf215546Sopenharmony_ci *   [...original code...]
259bf215546Sopenharmony_ci */
260bf215546Sopenharmony_cistatic void
261bf215546Sopenharmony_cipstip_transform_prolog(struct tgsi_transform_context *ctx)
262bf215546Sopenharmony_ci{
263bf215546Sopenharmony_ci   struct pstip_transform_context *pctx =
264bf215546Sopenharmony_ci      (struct pstip_transform_context *) ctx;
265bf215546Sopenharmony_ci   int wincoordInput;
266bf215546Sopenharmony_ci   int texTemp;
267bf215546Sopenharmony_ci   int sampIdx;
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   STATIC_ASSERT(sizeof(pctx->samplersUsed) * 8 >= PIPE_MAX_SAMPLERS);
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci   /* find free texture sampler */
272bf215546Sopenharmony_ci   pctx->freeSampler = free_bit(pctx->samplersUsed);
273bf215546Sopenharmony_ci   if (pctx->freeSampler < 0 || pctx->freeSampler >= PIPE_MAX_SAMPLERS)
274bf215546Sopenharmony_ci      pctx->freeSampler = PIPE_MAX_SAMPLERS - 1;
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci   if (pctx->wincoordInput < 0)
277bf215546Sopenharmony_ci      wincoordInput = pctx->maxInput + 1;
278bf215546Sopenharmony_ci   else
279bf215546Sopenharmony_ci      wincoordInput = pctx->wincoordInput;
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   if (pctx->wincoordInput < 0) {
282bf215546Sopenharmony_ci      struct tgsi_full_declaration decl;
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci      decl = tgsi_default_full_declaration();
285bf215546Sopenharmony_ci      /* declare new position input reg */
286bf215546Sopenharmony_ci      decl.Declaration.File = pctx->wincoordFile;
287bf215546Sopenharmony_ci      decl.Declaration.Semantic = 1;
288bf215546Sopenharmony_ci      decl.Semantic.Name = TGSI_SEMANTIC_POSITION;
289bf215546Sopenharmony_ci      decl.Range.First =
290bf215546Sopenharmony_ci      decl.Range.Last = wincoordInput;
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci      if (pctx->wincoordFile == TGSI_FILE_INPUT) {
293bf215546Sopenharmony_ci         decl.Declaration.Interpolate = 1;
294bf215546Sopenharmony_ci         decl.Interp.Interpolate = TGSI_INTERPOLATE_LINEAR;
295bf215546Sopenharmony_ci      }
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci      ctx->emit_declaration(ctx, &decl);
298bf215546Sopenharmony_ci   }
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   sampIdx = pctx->hasFixedUnit ? (int)pctx->fixedUnit : pctx->freeSampler;
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci   /* declare new sampler */
303bf215546Sopenharmony_ci   tgsi_transform_sampler_decl(ctx, sampIdx);
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   /* if the src shader has SVIEW decl's for each SAMP decl, we
306bf215546Sopenharmony_ci    * need to continue the trend and ensure there is a matching
307bf215546Sopenharmony_ci    * SVIEW for the new SAMP we just created
308bf215546Sopenharmony_ci    */
309bf215546Sopenharmony_ci   if (pctx->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
310bf215546Sopenharmony_ci      tgsi_transform_sampler_view_decl(ctx,
311bf215546Sopenharmony_ci                                       sampIdx,
312bf215546Sopenharmony_ci                                       TGSI_TEXTURE_2D,
313bf215546Sopenharmony_ci                                       TGSI_RETURN_TYPE_FLOAT);
314bf215546Sopenharmony_ci   }
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci   /* Declare temp[0] reg if not already declared.
317bf215546Sopenharmony_ci    * We can always use temp[0] since this code is before
318bf215546Sopenharmony_ci    * the rest of the shader.
319bf215546Sopenharmony_ci    */
320bf215546Sopenharmony_ci   texTemp = 0;
321bf215546Sopenharmony_ci   if ((pctx->tempsUsed & (1 << texTemp)) == 0) {
322bf215546Sopenharmony_ci      tgsi_transform_temp_decl(ctx, texTemp);
323bf215546Sopenharmony_ci   }
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   /* emit immediate = {1/32, 1/32, 1, 1}
326bf215546Sopenharmony_ci    * The index/position of this immediate will be pctx->numImmed
327bf215546Sopenharmony_ci    */
328bf215546Sopenharmony_ci   tgsi_transform_immediate_decl(ctx, 1.0/32.0, 1.0/32.0, 1.0, 1.0);
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci   /*
331bf215546Sopenharmony_ci    * Insert new MUL/TEX/KILL_IF instructions at start of program
332bf215546Sopenharmony_ci    * Take gl_FragCoord, divide by 32 (stipple size), sample the
333bf215546Sopenharmony_ci    * texture and kill fragment if needed.
334bf215546Sopenharmony_ci    *
335bf215546Sopenharmony_ci    * We'd like to use non-normalized texcoords to index into a RECT
336bf215546Sopenharmony_ci    * texture, but we can only use REPEAT wrap mode with normalized
337bf215546Sopenharmony_ci    * texcoords.  Darn.
338bf215546Sopenharmony_ci    */
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_ci   /* XXX invert wincoord if origin isn't lower-left... */
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   /* MUL texTemp, INPUT[wincoord], 1/32; */
343bf215546Sopenharmony_ci   tgsi_transform_op2_inst(ctx, TGSI_OPCODE_MUL,
344bf215546Sopenharmony_ci                           TGSI_FILE_TEMPORARY, texTemp,
345bf215546Sopenharmony_ci                           TGSI_WRITEMASK_XYZW,
346bf215546Sopenharmony_ci                           pctx->wincoordFile, wincoordInput,
347bf215546Sopenharmony_ci                           TGSI_FILE_IMMEDIATE, pctx->numImmed, false);
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_ci   /* TEX texTemp, texTemp, sampler, 2D; */
350bf215546Sopenharmony_ci   tgsi_transform_tex_inst(ctx,
351bf215546Sopenharmony_ci                           TGSI_FILE_TEMPORARY, texTemp,
352bf215546Sopenharmony_ci                           TGSI_FILE_TEMPORARY, texTemp,
353bf215546Sopenharmony_ci                           TGSI_TEXTURE_2D, sampIdx);
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   /* KILL_IF -texTemp;   # if -texTemp < 0, kill fragment */
356bf215546Sopenharmony_ci   tgsi_transform_kill_inst(ctx,
357bf215546Sopenharmony_ci                            TGSI_FILE_TEMPORARY, texTemp,
358bf215546Sopenharmony_ci                            TGSI_SWIZZLE_W, TRUE);
359bf215546Sopenharmony_ci}
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci
362bf215546Sopenharmony_ci/**
363bf215546Sopenharmony_ci * Given a fragment shader, return a new fragment shader which
364bf215546Sopenharmony_ci * samples a stipple texture and executes KILL.
365bf215546Sopenharmony_ci *
366bf215546Sopenharmony_ci * \param samplerUnitOut  returns the index of the sampler unit which
367bf215546Sopenharmony_ci *                        will be used to sample the stipple texture;
368bf215546Sopenharmony_ci *                        if NULL, the fixed unit is used
369bf215546Sopenharmony_ci * \param fixedUnit       fixed texture unit used for the stipple texture
370bf215546Sopenharmony_ci * \param wincoordFile    TGSI_FILE_INPUT or TGSI_FILE_SYSTEM_VALUE,
371bf215546Sopenharmony_ci *                        depending on which one is supported by the driver
372bf215546Sopenharmony_ci *                        for TGSI_SEMANTIC_POSITION in the fragment shader
373bf215546Sopenharmony_ci */
374bf215546Sopenharmony_cistruct tgsi_token *
375bf215546Sopenharmony_ciutil_pstipple_create_fragment_shader(const struct tgsi_token *tokens,
376bf215546Sopenharmony_ci                                     unsigned *samplerUnitOut,
377bf215546Sopenharmony_ci                                     unsigned fixedUnit,
378bf215546Sopenharmony_ci                                     unsigned wincoordFile)
379bf215546Sopenharmony_ci{
380bf215546Sopenharmony_ci   struct pstip_transform_context transform;
381bf215546Sopenharmony_ci   const uint newLen = tgsi_num_tokens(tokens) + NUM_NEW_TOKENS;
382bf215546Sopenharmony_ci   struct tgsi_token *new_tokens;
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci   /* Setup shader transformation info/context.
385bf215546Sopenharmony_ci    */
386bf215546Sopenharmony_ci   memset(&transform, 0, sizeof(transform));
387bf215546Sopenharmony_ci   transform.wincoordInput = -1;
388bf215546Sopenharmony_ci   transform.wincoordFile = wincoordFile;
389bf215546Sopenharmony_ci   transform.maxInput = -1;
390bf215546Sopenharmony_ci   transform.coordOrigin = TGSI_FS_COORD_ORIGIN_UPPER_LEFT;
391bf215546Sopenharmony_ci   transform.hasFixedUnit = !samplerUnitOut;
392bf215546Sopenharmony_ci   transform.fixedUnit = fixedUnit;
393bf215546Sopenharmony_ci   transform.base.prolog = pstip_transform_prolog;
394bf215546Sopenharmony_ci   transform.base.transform_declaration = pstip_transform_decl;
395bf215546Sopenharmony_ci   transform.base.transform_immediate = pstip_transform_immed;
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   tgsi_scan_shader(tokens, &transform.info);
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci   transform.coordOrigin =
400bf215546Sopenharmony_ci      transform.info.properties[TGSI_PROPERTY_FS_COORD_ORIGIN];
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_ci   new_tokens = tgsi_transform_shader(tokens, newLen, &transform.base);
403bf215546Sopenharmony_ci   if (!new_tokens)
404bf215546Sopenharmony_ci      return NULL;
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci#if 0 /* DEBUG */
407bf215546Sopenharmony_ci   tgsi_dump(fs->tokens, 0);
408bf215546Sopenharmony_ci   tgsi_dump(new_fs->tokens, 0);
409bf215546Sopenharmony_ci#endif
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci   if (samplerUnitOut) {
412bf215546Sopenharmony_ci      assert(transform.freeSampler < PIPE_MAX_SAMPLERS);
413bf215546Sopenharmony_ci      *samplerUnitOut = transform.freeSampler;
414bf215546Sopenharmony_ci   }
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci   return new_tokens;
417bf215546Sopenharmony_ci}
418