1 /*
2  * Copyright (C) 2016 Miklós Máté
3  * Copyright (C) 2020 Google LLC
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "main/mtypes.h"
25 #include "main/atifragshader.h"
26 #include "main/errors.h"
27 #include "program/prog_parameter.h"
28 #include "program/prog_instruction.h"
29 #include "program/prog_to_nir.h"
30 
31 #include "st_program.h"
32 #include "st_atifs_to_nir.h"
33 #include "compiler/nir/nir_builder.h"
34 
35 #define FOG_PARAMS_UNIFORM (MAX_NUM_FRAGMENT_CONSTANTS_ATI + 0)
36 #define FOG_COLOR_UNIFORM (MAX_NUM_FRAGMENT_CONSTANTS_ATI + 1)
37 
38 /**
39  * Intermediate state used during shader translation.
40  */
41 struct st_translate {
42    nir_builder *b;
43    struct ati_fragment_shader *atifs;
44    const struct st_fp_variant_key *key;
45 
46    nir_ssa_def *temps[MAX_PROGRAM_TEMPS];
47 
48    nir_variable *fragcolor;
49    nir_variable *constants;
50    nir_variable *samplers[MAX_TEXTURE_UNITS];
51 
52    nir_ssa_def *inputs[VARYING_SLOT_MAX];
53 
54    unsigned current_pass;
55 
56    bool regs_written[MAX_NUM_PASSES_ATI][MAX_NUM_FRAGMENT_REGISTERS_ATI];
57 
58    boolean error;
59 };
60 
61 static nir_ssa_def *
nir_channel_vec4(nir_builder * b,nir_ssa_def * src,unsigned channel)62 nir_channel_vec4(nir_builder *b, nir_ssa_def *src, unsigned channel)
63 {
64    unsigned swizzle[4] = { channel, channel, channel, channel };
65    return nir_swizzle(b, src, swizzle, 4);
66 }
67 
68 static nir_ssa_def *
nir_imm_vec4_float(nir_builder * b,float f)69 nir_imm_vec4_float(nir_builder *b, float f)
70 {
71    return nir_channel_vec4(b, nir_imm_float(b, f), 0);
72 }
73 
74 static nir_ssa_def *
get_temp(struct st_translate * t,unsigned index)75 get_temp(struct st_translate *t, unsigned index)
76 {
77    if (!t->temps[index])
78       t->temps[index] = nir_ssa_undef(t->b, 4, 32);
79    return t->temps[index];
80 }
81 
82 static nir_ssa_def *
apply_swizzle(struct st_translate * t,struct nir_ssa_def * src,GLuint swizzle)83 apply_swizzle(struct st_translate *t,
84               struct nir_ssa_def *src, GLuint swizzle)
85 {
86    /* From the ATI_fs spec:
87     *
88     *     "Table 3.20 shows the <swizzle> modes:
89     *
90     *                           Coordinates Used for 1D or      Coordinates Used for
91     *      Swizzle              2D SampleMap and PassTexCoord   3D or cubemap SampleMap
92     *      -------              -----------------------------   -----------------------
93     *      SWIZZLE_STR_ATI      (s, t, r, undefined)            (s, t, r, undefined)
94     *      SWIZZLE_STQ_ATI      (s, t, q, undefined)            (s, t, q, undefined)
95     *      SWIZZLE_STR_DR_ATI   (s/r, t/r, 1/r, undefined)      (undefined)
96     *      SWIZZLE_STQ_DQ_ATI   (s/q, t/q, 1/q, undefined)      (undefined)
97     */
98    if (swizzle == GL_SWIZZLE_STR_ATI) {
99       return src;
100    } else if (swizzle == GL_SWIZZLE_STQ_ATI) {
101       static unsigned xywz[4] = { 0, 1, 3, 2 };
102       return nir_swizzle(t->b, src, xywz, 4);
103    } else {
104       nir_ssa_def *rcp = nir_frcp(t->b, nir_channel(t->b, src,
105                                                     swizzle == GL_SWIZZLE_STR_DR_ATI ? 2 : 3));
106 
107       nir_ssa_def *st_mul = nir_fmul(t->b, nir_channels(t->b, src, 0x3), rcp);
108 
109       return nir_vec4(t->b,
110                       nir_channel(t->b, st_mul, 0),
111                       nir_channel(t->b, st_mul, 1),
112                       rcp,
113                       rcp);
114    }
115 }
116 
117 static nir_ssa_def *
load_input(struct st_translate * t,gl_varying_slot slot)118 load_input(struct st_translate *t, gl_varying_slot slot)
119 {
120    if (!t->inputs[slot]) {
121       const char *slot_name =
122          gl_varying_slot_name_for_stage(slot, MESA_SHADER_FRAGMENT);
123       nir_variable *var = nir_variable_create(t->b->shader, nir_var_shader_in,
124                                               slot == VARYING_SLOT_FOGC ?
125                                               glsl_float_type() : glsl_vec4_type(),
126                                               slot_name);
127       var->data.location = slot;
128       var->data.interpolation = INTERP_MODE_NONE;
129 
130       t->inputs[slot] = nir_load_var(t->b, var);
131    }
132 
133    return t->inputs[slot];
134 }
135 
136 static nir_ssa_def *
atifs_load_uniform(struct st_translate * t,int index)137 atifs_load_uniform(struct st_translate *t, int index)
138 {
139    nir_deref_instr *deref = nir_build_deref_array(t->b,
140                                                   nir_build_deref_var(t->b, t->constants),
141                                                   nir_imm_int(t->b, index));
142    return nir_load_deref(t->b, deref);
143 }
144 
145 static struct nir_ssa_def *
get_source(struct st_translate * t,GLenum src_type)146 get_source(struct st_translate *t, GLenum src_type)
147 {
148    if (src_type >= GL_REG_0_ATI && src_type <= GL_REG_5_ATI) {
149       if (t->regs_written[t->current_pass][src_type - GL_REG_0_ATI]) {
150          return get_temp(t, src_type - GL_REG_0_ATI);
151       } else {
152          return nir_imm_vec4_float(t->b, 0.0);
153       }
154    } else if (src_type >= GL_CON_0_ATI && src_type <= GL_CON_7_ATI) {
155       int index = src_type - GL_CON_0_ATI;
156       if (t->atifs->LocalConstDef & (1 << index)) {
157          return nir_imm_vec4(t->b,
158                              t->atifs->Constants[index][0],
159                              t->atifs->Constants[index][1],
160                              t->atifs->Constants[index][2],
161                              t->atifs->Constants[index][3]);
162       } else {
163          return atifs_load_uniform(t, index);
164       }
165    } else if (src_type == GL_ZERO) {
166       return nir_imm_vec4_float(t->b, 0.0);
167    } else if (src_type == GL_ONE) {
168       return nir_imm_vec4_float(t->b, 1.0);
169    } else if (src_type == GL_PRIMARY_COLOR_ARB) {
170       return load_input(t, VARYING_SLOT_COL0);
171    } else if (src_type == GL_SECONDARY_INTERPOLATOR_ATI) {
172       return load_input(t, VARYING_SLOT_COL1);
173    } else {
174       /* frontend prevents this */
175       unreachable("unknown source");
176    }
177 }
178 
179 static nir_ssa_def *
prepare_argument(struct st_translate * t,const struct atifs_instruction * inst,const unsigned argId,bool alpha)180 prepare_argument(struct st_translate *t, const struct atifs_instruction *inst,
181                  const unsigned argId, bool alpha)
182 {
183    if (argId >= inst->ArgCount[alpha]) {
184       _mesa_warning(0, "Using 0 for missing argument %d\n", argId);
185       return nir_imm_vec4_float(t->b, 0.0f);
186    }
187 
188    const struct atifragshader_src_register *srcReg = &inst->SrcReg[alpha][argId];
189 
190    nir_ssa_def *src = get_source(t, srcReg->Index);
191 
192    switch (srcReg->argRep) {
193    case GL_NONE:
194       break;
195    case GL_RED:
196       src = nir_channel_vec4(t->b, src, 0);
197       break;
198    case GL_GREEN:
199       src = nir_channel_vec4(t->b, src, 1);
200       break;
201    case GL_BLUE:
202       src = nir_channel_vec4(t->b, src, 2);
203       break;
204    case GL_ALPHA:
205       src = nir_channel_vec4(t->b, src, 3);
206       break;
207    }
208 
209    t->temps[MAX_NUM_FRAGMENT_REGISTERS_ATI + argId] = src;
210 
211    if (srcReg->argMod & GL_COMP_BIT_ATI)
212       src = nir_fsub(t->b, nir_imm_vec4_float(t->b, 1.0), src);
213    if (srcReg->argMod & GL_BIAS_BIT_ATI)
214       src = nir_fadd(t->b, src, nir_imm_vec4_float(t->b, -0.5));
215    if (srcReg->argMod & GL_2X_BIT_ATI)
216       src = nir_fadd(t->b, src, src);
217    if (srcReg->argMod & GL_NEGATE_BIT_ATI)
218       src = nir_fneg(t->b, src);
219 
220    return src;
221 }
222 
223 static nir_ssa_def *
emit_arith_inst(struct st_translate * t,const struct atifs_instruction * inst,bool alpha)224 emit_arith_inst(struct st_translate *t,
225                 const struct atifs_instruction *inst,
226                 bool alpha)
227 {
228    nir_ssa_def *src[3] = {0};
229    for (int i = 0; i < inst->ArgCount[alpha]; i++)
230       src[i] = prepare_argument(t, inst, i, alpha);
231 
232    switch (inst->Opcode[alpha]) {
233    case GL_MOV_ATI:
234       return src[0];
235 
236    case GL_ADD_ATI:
237       return nir_fadd(t->b, src[0], src[1]);
238 
239    case GL_SUB_ATI:
240       return nir_fsub(t->b, src[0], src[1]);
241 
242    case GL_MUL_ATI:
243       return nir_fmul(t->b, src[0], src[1]);
244 
245    case GL_MAD_ATI:
246       return nir_ffma(t->b, src[0], src[1], src[2]);
247 
248    case GL_LERP_ATI:
249       return nir_flrp(t->b, src[2], src[1], src[0]);
250 
251    case GL_CND_ATI:
252       return nir_bcsel(t->b,
253                        nir_fge(t->b, nir_imm_vec4_float(t->b, 0.5), src[2]),
254                        src[1],
255                        src[0]);
256 
257    case GL_CND0_ATI:
258       return nir_bcsel(t->b,
259                        nir_fge(t->b, src[2], nir_imm_vec4_float(t->b, 0.0)),
260                        src[0],
261                        src[1]);
262 
263    case GL_DOT2_ADD_ATI:
264       return nir_channel_vec4(t->b,
265                               nir_fadd(t->b,
266                                        nir_fdot2(t->b, src[0], src[1]),
267                                        nir_channel(t->b, src[1], 2)),
268                               0);
269 
270    case GL_DOT3_ATI:
271       return nir_channel_vec4(t->b, nir_fdot3(t->b,src[0], src[1]), 0);
272 
273    case GL_DOT4_ATI:
274       return nir_channel_vec4(t->b, nir_fdot4(t->b,src[0], src[1]), 0);
275 
276    default:
277       unreachable("Unknown ATI_fs opcode");
278    }
279 }
280 
281 static nir_ssa_def *
emit_dstmod(struct st_translate * t,struct nir_ssa_def * dst,GLuint dstMod)282 emit_dstmod(struct st_translate *t,
283             struct nir_ssa_def *dst, GLuint dstMod)
284 {
285    switch (dstMod & ~GL_SATURATE_BIT_ATI) {
286    case GL_2X_BIT_ATI:
287       dst = nir_fmul_imm(t->b, dst, 2.0f);
288       break;
289    case GL_4X_BIT_ATI:
290       dst = nir_fmul_imm(t->b, dst, 4.0f);
291       break;
292    case GL_8X_BIT_ATI:
293       dst = nir_fmul_imm(t->b, dst, 8.0f);
294       break;
295    case GL_HALF_BIT_ATI:
296       dst = nir_fmul_imm(t->b, dst, 0.5f);
297       break;
298    case GL_QUARTER_BIT_ATI:
299       dst = nir_fmul_imm(t->b, dst, 0.25f);
300       break;
301    case GL_EIGHTH_BIT_ATI:
302       dst = nir_fmul_imm(t->b, dst, 0.125f);
303       break;
304    default:
305       break;
306    }
307 
308    if (dstMod & GL_SATURATE_BIT_ATI)
309       dst = nir_fsat(t->b, dst);
310 
311    return dst;
312 }
313 
314 /**
315  * Compile one setup instruction to NIR instructions.
316  */
317 static void
compile_setupinst(struct st_translate * t,const unsigned r,const struct atifs_setupinst * texinst)318 compile_setupinst(struct st_translate *t,
319                   const unsigned r,
320                   const struct atifs_setupinst *texinst)
321 {
322    if (!texinst->Opcode)
323       return;
324 
325    GLuint pass_tex = texinst->src;
326 
327    nir_ssa_def *coord;
328 
329    if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) {
330       unsigned attr = pass_tex - GL_TEXTURE0_ARB;
331 
332       coord = load_input(t, VARYING_SLOT_TEX0 + attr);
333    } else if (pass_tex >= GL_REG_0_ATI && pass_tex <= GL_REG_5_ATI) {
334       unsigned reg = pass_tex - GL_REG_0_ATI;
335 
336       /* the frontend already validated that REG is only allowed in second pass */
337       if (t->regs_written[0][reg]) {
338          coord = t->temps[reg];
339       } else {
340          coord = nir_imm_vec4_float(t->b, 0.0f);
341       }
342    } else {
343       coord = nir_ssa_undef(t->b, 4, 32);
344    }
345    coord = apply_swizzle(t, coord, texinst->swizzle);
346 
347    if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP) {
348       nir_variable *tex_var = t->samplers[r];
349       if (!tex_var) {
350          bool is_array;
351          enum glsl_sampler_dim sampler_dim =
352              _mesa_texture_index_to_sampler_dim(t->key->texture_index[r], &is_array);
353          const struct glsl_type *sampler_type =
354              glsl_sampler_type(sampler_dim, false, false, GLSL_TYPE_FLOAT);
355 
356          tex_var = nir_variable_create(t->b->shader, nir_var_uniform, sampler_type, "tex");
357          tex_var->data.binding = r;
358          tex_var->data.explicit_binding = true;
359          t->samplers[r] = tex_var;
360       }
361       nir_deref_instr *tex_deref = nir_build_deref_var(t->b, t->samplers[r]);
362 
363       nir_tex_instr *tex = nir_tex_instr_create(t->b->shader, 3);
364       tex->op = nir_texop_tex;
365       tex->sampler_dim = glsl_get_sampler_dim(tex_var->type);
366       tex->dest_type = nir_type_float32;
367       tex->coord_components =
368          glsl_get_sampler_dim_coordinate_components(tex->sampler_dim);
369 
370       tex->src[0].src_type = nir_tex_src_texture_deref;
371       tex->src[0].src = nir_src_for_ssa(&tex_deref->dest.ssa);
372       tex->src[1].src_type = nir_tex_src_sampler_deref;
373       tex->src[1].src = nir_src_for_ssa(&tex_deref->dest.ssa);
374       tex->src[2].src_type = nir_tex_src_coord;
375       tex->src[2].src =
376          nir_src_for_ssa(nir_channels(t->b, coord,
377                                     (1 << tex->coord_components) - 1));
378 
379       nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
380       nir_builder_instr_insert(t->b, &tex->instr);
381 
382       t->temps[r] = &tex->dest.ssa;
383    } else if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP) {
384       t->temps[r] = coord;
385    }
386 
387    t->regs_written[t->current_pass][r] = true;
388 }
389 
390 /**
391  * Compile one arithmetic operation COLOR&ALPHA pair into NIR instructions.
392  */
393 static void
compile_instruction(struct st_translate * t,const struct atifs_instruction * inst)394 compile_instruction(struct st_translate *t,
395                     const struct atifs_instruction *inst)
396 {
397    unsigned optype;
398 
399    for (optype = 0; optype < 2; optype++) { /* color, alpha */
400       unsigned dstreg = inst->DstReg[optype].Index - GL_REG_0_ATI;
401 
402       if (!inst->Opcode[optype])
403          continue;
404 
405       /* Execute the op */
406       nir_ssa_def *result = emit_arith_inst(t, inst, optype);
407       result = emit_dstmod(t, result, inst->DstReg[optype].dstMod);
408 
409       /* Do the writemask */
410       nir_const_value wrmask[4] = { 0 };
411       for (int i = 0; i < 4; i++) {
412          if (inst->DstReg[optype].dstMask & (1 << i))
413             wrmask[i].b = 1;
414       }
415 
416       t->temps[dstreg] = nir_bcsel(t->b,
417                                    nir_build_imm(t->b, 4, 1, wrmask),
418                                    result,
419                                    get_temp(t, dstreg));
420       t->regs_written[t->current_pass][dstreg] = true;
421    }
422 }
423 
424 
425 /* Creates the uniform variable referencing the ATI_fragment_shader constants
426  * plus the optimized fog state.
427  */
428 static void
st_atifs_setup_uniforms(struct st_translate * t,struct gl_program * program)429 st_atifs_setup_uniforms(struct st_translate *t, struct gl_program *program)
430 {
431    const struct glsl_type *type =
432       glsl_array_type(glsl_vec4_type(), program->Parameters->NumParameters, 0);
433    t->constants =
434       nir_variable_create(t->b->shader, nir_var_uniform, type,
435                           "gl_ATI_fragment_shader_constants");
436 }
437 
438 /**
439  * Called when a new variant is needed, we need to translate
440  * the ATI fragment shader to NIR
441  */
442 nir_shader *
st_translate_atifs_program(struct ati_fragment_shader * atifs,const struct st_fp_variant_key * key,struct gl_program * program,const nir_shader_compiler_options * options)443 st_translate_atifs_program(struct ati_fragment_shader *atifs,
444                            const struct st_fp_variant_key *key,
445                            struct gl_program *program,
446                            const nir_shader_compiler_options *options)
447 {
448    nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options, "ATI_fs");
449 
450    struct st_translate translate = {
451       .atifs = atifs,
452       .b = &b,
453       .key = key,
454    };
455    struct st_translate *t = &translate;
456 
457    /* Copy the shader_info from the gl_program */
458    t->b->shader->info = program->info;
459 
460    nir_shader *s = t->b->shader;
461    s->info.name = ralloc_asprintf(s, "ATIFS%d", program->Id);
462 
463    t->fragcolor = nir_variable_create(b.shader, nir_var_shader_out,
464                                       glsl_vec4_type(), "gl_FragColor");
465    t->fragcolor->data.location = FRAG_RESULT_COLOR;
466 
467    st_atifs_setup_uniforms(t, program);
468 
469    /* emit instructions */
470    for (unsigned pass = 0; pass < atifs->NumPasses; pass++) {
471       t->current_pass = pass;
472       for (unsigned r = 0; r < MAX_NUM_FRAGMENT_REGISTERS_ATI; r++) {
473          struct atifs_setupinst *texinst = &atifs->SetupInst[pass][r];
474          compile_setupinst(t, r, texinst);
475       }
476       for (unsigned i = 0; i < atifs->numArithInstr[pass]; i++) {
477          struct atifs_instruction *inst = &atifs->Instructions[pass][i];
478          compile_instruction(t, inst);
479       }
480    }
481 
482    if (t->regs_written[atifs->NumPasses-1][0]) {
483       nir_ssa_def *color = t->temps[0];
484 
485       if (key->fog) {
486          nir_ssa_def *fogc = load_input(t, VARYING_SLOT_FOGC);
487 
488          nir_ssa_def *params = atifs_load_uniform(t, FOG_PARAMS_UNIFORM);
489 
490          /* compute the 1 component fog factor f */
491          nir_ssa_def *f = NULL;
492          if (key->fog == FOG_LINEAR) {
493             f = nir_ffma(t->b, fogc,
494                          nir_channel(t->b, params, 0),
495                          nir_channel(t->b, params, 1));
496          } else if (key->fog == FOG_EXP) {
497             /* EXP formula: f = exp(-dens * z)
498              * with optimized parameters:
499              *    f = MUL(fogcoord, oparams.z); f= EX2(-f)
500              */
501             f = nir_fmul(t->b, fogc, nir_channel(t->b, params, 2));
502             f = nir_fexp2(t->b, nir_fneg(t->b, f));
503          } else if (key->fog == FOG_EXP2) {
504             /* EXP2 formula: f = exp(-(dens * z)^2)
505              * with optimized parameters:
506              *    f = MUL(fogcoord, oparams.w); f=MUL(f, f); f= EX2(-f)
507              */
508             f = nir_fmul(t->b, fogc, nir_channel(t->b, params, 3));
509             f = nir_fmul(t->b, f, f);
510             f = nir_fexp2(t->b, nir_fneg(t->b, f));
511          }
512          f = nir_fsat(t->b, f);
513 
514          nir_ssa_def *fog_color = nir_flrp(t->b,
515                                            atifs_load_uniform(t, FOG_COLOR_UNIFORM),
516                                            color,
517                                            f);
518          color = nir_vec4(t->b,
519                           nir_channel(t->b, fog_color, 0),
520                           nir_channel(t->b, fog_color, 1),
521                           nir_channel(t->b, fog_color, 2),
522                           nir_channel(t->b, color, 3));
523       }
524 
525       nir_store_var(t->b, t->fragcolor, color, 0xf);
526    }
527 
528    return b.shader;
529 }
530 
531 /**
532  * Called in ProgramStringNotify, we need to fill the metadata of the
533  * gl_program attached to the ati_fragment_shader
534  */
535 void
st_init_atifs_prog(struct gl_context * ctx,struct gl_program * prog)536 st_init_atifs_prog(struct gl_context *ctx, struct gl_program *prog)
537 {
538    /* we know this is st_fragment_program, because of st_new_ati_fs() */
539    struct st_program *stfp = (struct st_program *) prog;
540    struct ati_fragment_shader *atifs = stfp->ati_fs;
541 
542    unsigned pass, i, r, optype, arg;
543 
544    static const gl_state_index16 fog_params_state[STATE_LENGTH] =
545       {STATE_FOG_PARAMS_OPTIMIZED, 0, 0};
546    static const gl_state_index16 fog_color[STATE_LENGTH] =
547       {STATE_FOG_COLOR, 0, 0, 0};
548 
549    prog->info.inputs_read = 0;
550    prog->info.outputs_written = BITFIELD64_BIT(FRAG_RESULT_COLOR);
551    prog->SamplersUsed = 0;
552    prog->Parameters = _mesa_new_parameter_list();
553 
554    /* fill in inputs_read, SamplersUsed, TexturesUsed */
555    for (pass = 0; pass < atifs->NumPasses; pass++) {
556       for (r = 0; r < MAX_NUM_FRAGMENT_REGISTERS_ATI; r++) {
557          struct atifs_setupinst *texinst = &atifs->SetupInst[pass][r];
558          GLuint pass_tex = texinst->src;
559 
560          if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP) {
561             /* mark which texcoords are used */
562             prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + pass_tex - GL_TEXTURE0_ARB);
563             /* by default there is 1:1 mapping between samplers and textures */
564             prog->SamplersUsed |= (1 << r);
565             /* the target is unknown here, it will be fixed in the draw call */
566             prog->TexturesUsed[r] = TEXTURE_2D_BIT;
567          } else if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP) {
568             if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) {
569                prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + pass_tex - GL_TEXTURE0_ARB);
570             }
571          }
572       }
573    }
574    for (pass = 0; pass < atifs->NumPasses; pass++) {
575       for (i = 0; i < atifs->numArithInstr[pass]; i++) {
576          struct atifs_instruction *inst = &atifs->Instructions[pass][i];
577 
578          for (optype = 0; optype < 2; optype++) { /* color, alpha */
579             if (inst->Opcode[optype]) {
580                for (arg = 0; arg < inst->ArgCount[optype]; arg++) {
581                   GLint index = inst->SrcReg[optype][arg].Index;
582                   if (index == GL_PRIMARY_COLOR_EXT) {
583                      prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_COL0);
584                   } else if (index == GL_SECONDARY_INTERPOLATOR_ATI) {
585                      /* note: ATI_fragment_shader.txt never specifies what
586                       * GL_SECONDARY_INTERPOLATOR_ATI is, swrast uses
587                       * VARYING_SLOT_COL1 for this input */
588                      prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_COL1);
589                   }
590                }
591             }
592          }
593       }
594    }
595    /* we may need fog */
596    prog->info.inputs_read |= BITFIELD64_BIT(VARYING_SLOT_FOGC);
597 
598    /* we always have the ATI_fs constants, and the fog params */
599    for (i = 0; i < MAX_NUM_FRAGMENT_CONSTANTS_ATI; i++) {
600       _mesa_add_parameter(prog->Parameters, PROGRAM_UNIFORM,
601                           NULL, 4, GL_FLOAT, NULL, NULL, true);
602    }
603    ASSERTED uint32_t ref;
604    ref = _mesa_add_state_reference(prog->Parameters, fog_params_state);
605    assert(ref == FOG_PARAMS_UNIFORM);
606    ref = _mesa_add_state_reference(prog->Parameters, fog_color);
607    assert(ref == FOG_COLOR_UNIFORM);
608 }
609