Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program_tex.c
1 /*
2  * Copyright (C) 2010 Corbin Simpson
3  * Copyright (C) 2010 Marek Olšák <maraeo@gmail.com>
4  *
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28
29 #include "radeon_program_tex.h"
30
31 #include "radeon_compiler_util.h"
32
33 /* Series of transformations to be done on textures. */
34
35 static struct rc_src_register shadow_fail_value(struct r300_fragment_program_compiler *compiler,
36                                                 int tmu)
37 {
38         struct rc_src_register reg = { 0, };
39
40         if (compiler->enable_shadow_ambient) {
41                 reg.File = RC_FILE_CONSTANT;
42                 reg.Index = rc_constants_add_state(&compiler->Base.Program.Constants,
43                                                    RC_STATE_SHADOW_AMBIENT, tmu);
44                 reg.Swizzle = RC_SWIZZLE_WWWW;
45         } else {
46                 reg.File = RC_FILE_NONE;
47                 reg.Swizzle = RC_SWIZZLE_0000;
48         }
49
50         reg.Swizzle = combine_swizzles(reg.Swizzle,
51                                 compiler->state.unit[tmu].texture_swizzle);
52         return reg;
53 }
54
55 static struct rc_src_register shadow_pass_value(struct r300_fragment_program_compiler *compiler,
56                                                 int tmu)
57 {
58         struct rc_src_register reg = { 0, };
59
60         reg.File = RC_FILE_NONE;
61         reg.Swizzle = combine_swizzles(RC_SWIZZLE_1111,
62                                 compiler->state.unit[tmu].texture_swizzle);
63         return reg;
64 }
65
66 static void scale_texcoords(struct r300_fragment_program_compiler *compiler,
67                             struct rc_instruction *inst,
68                             unsigned state_constant)
69 {
70         struct rc_instruction *inst_mov;
71
72         unsigned temp = rc_find_free_temporary(&compiler->Base);
73
74         inst_mov = rc_insert_new_instruction(&compiler->Base, inst->Prev);
75
76         inst_mov->U.I.Opcode = RC_OPCODE_MUL;
77         inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
78         inst_mov->U.I.DstReg.Index = temp;
79         inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
80         inst_mov->U.I.SrcReg[1].File = RC_FILE_CONSTANT;
81         inst_mov->U.I.SrcReg[1].Index =
82                         rc_constants_add_state(&compiler->Base.Program.Constants,
83                                                state_constant, inst->U.I.TexSrcUnit);
84
85         reset_srcreg(&inst->U.I.SrcReg[0]);
86         inst->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
87         inst->U.I.SrcReg[0].Index = temp;
88 }
89
90 static void projective_divide(struct r300_fragment_program_compiler *compiler,
91                               struct rc_instruction *inst)
92 {
93         struct rc_instruction *inst_mul, *inst_rcp;
94
95         unsigned temp = rc_find_free_temporary(&compiler->Base);
96
97         inst_rcp = rc_insert_new_instruction(&compiler->Base, inst->Prev);
98         inst_rcp->U.I.Opcode = RC_OPCODE_RCP;
99         inst_rcp->U.I.DstReg.File = RC_FILE_TEMPORARY;
100         inst_rcp->U.I.DstReg.Index = temp;
101         inst_rcp->U.I.DstReg.WriteMask = RC_MASK_W;
102         inst_rcp->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
103         /* Because the input can be arbitrarily swizzled,
104          * read the component mapped to W. */
105         inst_rcp->U.I.SrcReg[0].Swizzle =
106                 RC_MAKE_SWIZZLE_SMEAR(GET_SWZ(inst->U.I.SrcReg[0].Swizzle, 3));
107
108         inst_mul = rc_insert_new_instruction(&compiler->Base, inst->Prev);
109         inst_mul->U.I.Opcode = RC_OPCODE_MUL;
110         inst_mul->U.I.DstReg.File = RC_FILE_TEMPORARY;
111         inst_mul->U.I.DstReg.Index = temp;
112         inst_mul->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
113         inst_mul->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
114         inst_mul->U.I.SrcReg[1].Index = temp;
115         inst_mul->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_WWWW;
116
117         reset_srcreg(&inst->U.I.SrcReg[0]);
118         inst->U.I.Opcode = RC_OPCODE_TEX;
119         inst->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
120         inst->U.I.SrcReg[0].Index = temp;
121 }
122
123 /**
124  * Transform TEX, TXP, TXB, and KIL instructions in the following ways:
125  *  - implement texture compare (shadow extensions)
126  *  - extract non-native source / destination operands
127  *  - premultiply texture coordinates for RECT
128  *  - extract operand swizzles
129  *  - introduce a temporary register when write masks are needed
130  */
131 int radeonTransformTEX(
132         struct radeon_compiler * c,
133         struct rc_instruction * inst,
134         void* data)
135 {
136         struct r300_fragment_program_compiler *compiler =
137                 (struct r300_fragment_program_compiler*)data;
138         rc_wrap_mode wrapmode = compiler->state.unit[inst->U.I.TexSrcUnit].wrap_mode;
139         int is_rect = inst->U.I.TexSrcTarget == RC_TEXTURE_RECT ||
140                       compiler->state.unit[inst->U.I.TexSrcUnit].non_normalized_coords;
141
142         if (inst->U.I.Opcode != RC_OPCODE_TEX &&
143                 inst->U.I.Opcode != RC_OPCODE_TXB &&
144                 inst->U.I.Opcode != RC_OPCODE_TXP &&
145                 inst->U.I.Opcode != RC_OPCODE_TXD &&
146                 inst->U.I.Opcode != RC_OPCODE_TXL &&
147                 inst->U.I.Opcode != RC_OPCODE_KIL)
148                 return 0;
149
150         /* ARB_shadow & EXT_shadow_funcs */
151         if (inst->U.I.Opcode != RC_OPCODE_KIL &&
152                 ((c->Program.ShadowSamplers & (1 << inst->U.I.TexSrcUnit)) ||
153                  (compiler->state.unit[inst->U.I.TexSrcUnit].compare_mode_enabled))) {
154                 rc_compare_func comparefunc = compiler->state.unit[inst->U.I.TexSrcUnit].texture_compare_func;
155
156                 if (comparefunc == RC_COMPARE_FUNC_NEVER || comparefunc == RC_COMPARE_FUNC_ALWAYS) {
157                         inst->U.I.Opcode = RC_OPCODE_MOV;
158
159                         if (comparefunc == RC_COMPARE_FUNC_ALWAYS) {
160                                 inst->U.I.SrcReg[0] = shadow_pass_value(compiler, inst->U.I.TexSrcUnit);
161                         } else {
162                                 inst->U.I.SrcReg[0] = shadow_fail_value(compiler, inst->U.I.TexSrcUnit);
163                         }
164
165                         return 1;
166                 } else {
167                         struct rc_instruction * inst_rcp = NULL;
168                         struct rc_instruction *inst_mul, *inst_add, *inst_cmp;
169                         unsigned tmp_texsample;
170                         unsigned tmp_sum;
171                         int pass, fail;
172
173                         /* Save the output register. */
174                         struct rc_dst_register output_reg = inst->U.I.DstReg;
175                         unsigned saturate_mode = inst->U.I.SaturateMode;
176
177                         /* Redirect TEX to a new temp. */
178                         tmp_texsample = rc_find_free_temporary(c);
179                         inst->U.I.SaturateMode = 0;
180                         inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
181                         inst->U.I.DstReg.Index = tmp_texsample;
182                         inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
183
184                         tmp_sum = rc_find_free_temporary(c);
185
186                         if (inst->U.I.Opcode == RC_OPCODE_TXP) {
187                                 /* Compute 1/W. */
188                                 inst_rcp = rc_insert_new_instruction(c, inst);
189                                 inst_rcp->U.I.Opcode = RC_OPCODE_RCP;
190                                 inst_rcp->U.I.DstReg.File = RC_FILE_TEMPORARY;
191                                 inst_rcp->U.I.DstReg.Index = tmp_sum;
192                                 inst_rcp->U.I.DstReg.WriteMask = RC_MASK_W;
193                                 inst_rcp->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
194                                 inst_rcp->U.I.SrcReg[0].Swizzle =
195                                         RC_MAKE_SWIZZLE_SMEAR(GET_SWZ(inst->U.I.SrcReg[0].Swizzle, 3));
196                         }
197
198                         /* Divide Z by W (if it's TXP) and saturate. */
199                         inst_mul = rc_insert_new_instruction(c, inst_rcp ? inst_rcp : inst);
200                         inst_mul->U.I.Opcode = inst->U.I.Opcode == RC_OPCODE_TXP ? RC_OPCODE_MUL : RC_OPCODE_MOV;
201                         inst_mul->U.I.DstReg.File = RC_FILE_TEMPORARY;
202                         inst_mul->U.I.DstReg.Index = tmp_sum;
203                         inst_mul->U.I.DstReg.WriteMask = RC_MASK_W;
204                         inst_mul->U.I.SaturateMode = RC_SATURATE_ZERO_ONE;
205                         inst_mul->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
206                         inst_mul->U.I.SrcReg[0].Swizzle =
207                                 RC_MAKE_SWIZZLE_SMEAR(GET_SWZ(inst->U.I.SrcReg[0].Swizzle, 2));
208                         if (inst->U.I.Opcode == RC_OPCODE_TXP) {
209                                 inst_mul->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
210                                 inst_mul->U.I.SrcReg[1].Index = tmp_sum;
211                                 inst_mul->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_WWWW;
212                         }
213
214                         /* Add the depth texture value. */
215                         inst_add = rc_insert_new_instruction(c, inst_mul);
216                         inst_add->U.I.Opcode = RC_OPCODE_ADD;
217                         inst_add->U.I.DstReg.File = RC_FILE_TEMPORARY;
218                         inst_add->U.I.DstReg.Index = tmp_sum;
219                         inst_add->U.I.DstReg.WriteMask = RC_MASK_W;
220                         inst_add->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
221                         inst_add->U.I.SrcReg[0].Index = tmp_sum;
222                         inst_add->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_WWWW;
223                         inst_add->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
224                         inst_add->U.I.SrcReg[1].Index = tmp_texsample;
225                         inst_add->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XXXX;
226
227                         /* Note that SrcReg[0] is r, SrcReg[1] is tex and:
228                          *   LESS:    r  < tex  <=>      -tex+r < 0
229                          *   GEQUAL:  r >= tex  <=> not (-tex+r < 0)
230                          *   GREATER: r  > tex  <=>       tex-r < 0
231                          *   LEQUAL:  r <= tex  <=> not ( tex-r < 0)
232                          *   EQUAL:   GEQUAL
233                          *   NOTEQUAL:LESS
234                          */
235
236                         /* This negates either r or tex: */
237                         if (comparefunc == RC_COMPARE_FUNC_LESS || comparefunc == RC_COMPARE_FUNC_GEQUAL ||
238                             comparefunc == RC_COMPARE_FUNC_EQUAL || comparefunc == RC_COMPARE_FUNC_NOTEQUAL)
239                                 inst_add->U.I.SrcReg[1].Negate = inst_add->U.I.SrcReg[1].Negate ^ RC_MASK_XYZW;
240                         else
241                                 inst_add->U.I.SrcReg[0].Negate = inst_add->U.I.SrcReg[0].Negate ^ RC_MASK_XYZW;
242
243                         /* This negates the whole expresion: */
244                         if (comparefunc == RC_COMPARE_FUNC_LESS || comparefunc == RC_COMPARE_FUNC_GREATER ||
245                             comparefunc == RC_COMPARE_FUNC_NOTEQUAL) {
246                                 pass = 1;
247                                 fail = 2;
248                         } else {
249                                 pass = 2;
250                                 fail = 1;
251                         }
252
253                         inst_cmp = rc_insert_new_instruction(c, inst_add);
254                         inst_cmp->U.I.Opcode = RC_OPCODE_CMP;
255                         inst_cmp->U.I.SaturateMode = saturate_mode;
256                         inst_cmp->U.I.DstReg = output_reg;
257                         inst_cmp->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
258                         inst_cmp->U.I.SrcReg[0].Index = tmp_sum;
259                         inst_cmp->U.I.SrcReg[0].Swizzle =
260                                         combine_swizzles(RC_SWIZZLE_WWWW,
261                                                          compiler->state.unit[inst->U.I.TexSrcUnit].texture_swizzle);
262                         inst_cmp->U.I.SrcReg[pass] = shadow_pass_value(compiler, inst->U.I.TexSrcUnit);
263                         inst_cmp->U.I.SrcReg[fail] = shadow_fail_value(compiler, inst->U.I.TexSrcUnit);
264
265                         assert(tmp_texsample != tmp_sum);
266                 }
267         }
268
269         /* R300 cannot sample from rectangles and the wrap mode fallback needs
270          * normalized coordinates anyway. */
271         if (inst->U.I.Opcode != RC_OPCODE_KIL &&
272             is_rect && (!c->is_r500 || wrapmode != RC_WRAP_NONE)) {
273                 scale_texcoords(compiler, inst, RC_STATE_R300_TEXRECT_FACTOR);
274                 inst->U.I.TexSrcTarget = RC_TEXTURE_2D;
275         }
276
277         /* Divide by W if needed. */
278         if (inst->U.I.Opcode == RC_OPCODE_TXP &&
279             (wrapmode == RC_WRAP_REPEAT || wrapmode == RC_WRAP_MIRRORED_REPEAT ||
280              compiler->state.unit[inst->U.I.TexSrcUnit].clamp_and_scale_before_fetch)) {
281                 projective_divide(compiler, inst);
282         }
283
284         /* Texture wrap modes don't work on NPOT textures.
285          *
286          * Non-wrapped/clamped texcoords with NPOT are free in HW. Repeat and
287          * mirroring are not. If we need to repeat, we do:
288          *
289          * MUL temp, texcoord, <scaling factor constant>
290          * FRC temp, temp ; Discard integer portion of coords
291          *
292          * This gives us coords in [0, 1].
293          *
294          * Mirroring is trickier. We're going to start out like repeat:
295          *
296          * MUL temp, texcoord, <scaling factor constant> ; De-mirror across axes
297          * MUL temp, temp, 0.5 ; Pattern repeats in [0, 2]
298          *                            ; so scale to [0, 1]
299          * FRC temp, temp ; Make the pattern repeat
300          * MAD temp, temp, 2, -1 ; Move the pattern to [-1, 1]
301          * ADD temp, 1, -abs(temp) ; Now comes a neat trick: use abs to mirror the pattern.
302          *                              ; The pattern is backwards, so reverse it (1-x).
303          *
304          * This gives us coords in [0, 1].
305          *
306          * ~ C & M. ;)
307          */
308         if (inst->U.I.Opcode != RC_OPCODE_KIL &&
309             wrapmode != RC_WRAP_NONE) {
310                 struct rc_instruction *inst_mov;
311                 unsigned temp = rc_find_free_temporary(c);
312
313                 if (wrapmode == RC_WRAP_REPEAT) {
314                         /* Both instructions will be paired up. */
315                         struct rc_instruction *inst_frc = rc_insert_new_instruction(c, inst->Prev);
316
317                         inst_frc->U.I.Opcode = RC_OPCODE_FRC;
318                         inst_frc->U.I.DstReg.File = RC_FILE_TEMPORARY;
319                         inst_frc->U.I.DstReg.Index = temp;
320                         inst_frc->U.I.DstReg.WriteMask = RC_MASK_XYZ;
321                         inst_frc->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
322                 } else if (wrapmode == RC_WRAP_MIRRORED_REPEAT) {
323                         /*
324                          * Function:
325                          *   f(v) = 1 - abs(frac(v * 0.5) * 2 - 1)
326                          *
327                          * Code:
328                          *   MUL temp, src0, 0.5
329                          *   FRC temp, temp
330                          *   MAD temp, temp, 2, -1
331                          *   ADD temp, 1, -abs(temp)
332                          */
333
334                         struct rc_instruction *inst_mul, *inst_frc, *inst_mad, *inst_add;
335                         unsigned two, two_swizzle;
336
337                         inst_mul = rc_insert_new_instruction(c, inst->Prev);
338
339                         inst_mul->U.I.Opcode = RC_OPCODE_MUL;
340                         inst_mul->U.I.DstReg.File = RC_FILE_TEMPORARY;
341                         inst_mul->U.I.DstReg.Index = temp;
342                         inst_mul->U.I.DstReg.WriteMask = RC_MASK_XYZ;
343                         inst_mul->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
344                         inst_mul->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_HHHH;
345
346                         inst_frc = rc_insert_new_instruction(c, inst->Prev);
347
348                         inst_frc->U.I.Opcode = RC_OPCODE_FRC;
349                         inst_frc->U.I.DstReg.File = RC_FILE_TEMPORARY;
350                         inst_frc->U.I.DstReg.Index = temp;
351                         inst_frc->U.I.DstReg.WriteMask = RC_MASK_XYZ;
352                         inst_frc->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
353                         inst_frc->U.I.SrcReg[0].Index = temp;
354                         inst_frc->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZ0;
355
356                         two = rc_constants_add_immediate_scalar(&c->Program.Constants, 2, &two_swizzle);
357                         inst_mad = rc_insert_new_instruction(c, inst->Prev);
358
359                         inst_mad->U.I.Opcode = RC_OPCODE_MAD;
360                         inst_mad->U.I.DstReg.File = RC_FILE_TEMPORARY;
361                         inst_mad->U.I.DstReg.Index = temp;
362                         inst_mad->U.I.DstReg.WriteMask = RC_MASK_XYZ;
363                         inst_mad->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
364                         inst_mad->U.I.SrcReg[0].Index = temp;
365                         inst_mad->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZ0;
366                         inst_mad->U.I.SrcReg[1].File = RC_FILE_CONSTANT;
367                         inst_mad->U.I.SrcReg[1].Index = two;
368                         inst_mad->U.I.SrcReg[1].Swizzle = two_swizzle;
369                         inst_mad->U.I.SrcReg[2].Swizzle = RC_SWIZZLE_1111;
370                         inst_mad->U.I.SrcReg[2].Negate = RC_MASK_XYZ;
371
372                         inst_add = rc_insert_new_instruction(c, inst->Prev);
373
374                         inst_add->U.I.Opcode = RC_OPCODE_ADD;
375                         inst_add->U.I.DstReg.File = RC_FILE_TEMPORARY;
376                         inst_add->U.I.DstReg.Index = temp;
377                         inst_add->U.I.DstReg.WriteMask = RC_MASK_XYZ;
378                         inst_add->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_1111;
379                         inst_add->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
380                         inst_add->U.I.SrcReg[1].Index = temp;
381                         inst_add->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XYZ0;
382                         inst_add->U.I.SrcReg[1].Abs = 1;
383                         inst_add->U.I.SrcReg[1].Negate = RC_MASK_XYZ;
384                 } else if (wrapmode == RC_WRAP_MIRRORED_CLAMP) {
385                         /*
386                          * Mirrored clamp modes are bloody simple, we just use abs
387                          * to mirror [0, 1] into [-1, 0]. This works for
388                          * all modes i.e. CLAMP, CLAMP_TO_EDGE, and CLAMP_TO_BORDER.
389                          */
390                         struct rc_instruction *inst_mov;
391
392                         inst_mov = rc_insert_new_instruction(c, inst->Prev);
393
394                         inst_mov->U.I.Opcode = RC_OPCODE_MOV;
395                         inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
396                         inst_mov->U.I.DstReg.Index = temp;
397                         inst_mov->U.I.DstReg.WriteMask = RC_MASK_XYZ;
398                         inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
399                         inst_mov->U.I.SrcReg[0].Abs = 1;
400                 }
401
402                 /* Preserve W for TXP/TXB. */
403                 inst_mov = rc_insert_new_instruction(c, inst->Prev);
404
405                 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
406                 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
407                 inst_mov->U.I.DstReg.Index = temp;
408                 inst_mov->U.I.DstReg.WriteMask = RC_MASK_W;
409                 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
410
411                 reset_srcreg(&inst->U.I.SrcReg[0]);
412                 inst->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
413                 inst->U.I.SrcReg[0].Index = temp;
414         }
415
416         /* NPOT -> POT conversion for 3D textures. */
417         if (inst->U.I.Opcode != RC_OPCODE_KIL &&
418             compiler->state.unit[inst->U.I.TexSrcUnit].clamp_and_scale_before_fetch) {
419                 struct rc_instruction *inst_mov;
420                 unsigned temp = rc_find_free_temporary(c);
421
422                 /* Saturate XYZ. */
423                 inst_mov = rc_insert_new_instruction(c, inst->Prev);
424                 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
425                 inst_mov->U.I.SaturateMode = RC_SATURATE_ZERO_ONE;
426                 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
427                 inst_mov->U.I.DstReg.Index = temp;
428                 inst_mov->U.I.DstReg.WriteMask = RC_MASK_XYZ;
429                 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
430
431                 /* Copy W. */
432                 inst_mov = rc_insert_new_instruction(c, inst->Prev);
433                 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
434                 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
435                 inst_mov->U.I.DstReg.Index = temp;
436                 inst_mov->U.I.DstReg.WriteMask = RC_MASK_W;
437                 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
438
439                 reset_srcreg(&inst->U.I.SrcReg[0]);
440                 inst->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
441                 inst->U.I.SrcReg[0].Index = temp;
442
443                 scale_texcoords(compiler, inst, RC_STATE_R300_TEXSCALE_FACTOR);
444         }
445
446         /* Convert SNORM-encoded ATI1N sampled as UNORM to SNORM.
447          * Formula: dst = tex > 0.5 ? tex*2-2 : tex*2
448          */
449         if (inst->U.I.Opcode != RC_OPCODE_KIL &&
450             compiler->state.unit[inst->U.I.TexSrcUnit].convert_unorm_to_snorm) {
451                 unsigned two, two_swizzle;
452                 struct rc_instruction *inst_mul, *inst_mad, *inst_cnd;
453
454                 two = rc_constants_add_immediate_scalar(&c->Program.Constants, 2.35, &two_swizzle);
455
456                 inst_mul = rc_insert_new_instruction(c, inst);
457                 inst_mul->U.I.Opcode = RC_OPCODE_MUL;
458                 inst_mul->U.I.DstReg.File = RC_FILE_TEMPORARY;
459                 inst_mul->U.I.DstReg.Index = rc_find_free_temporary(c);
460                 inst_mul->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
461                 inst_mul->U.I.SrcReg[0].Index = rc_find_free_temporary(c); /* redirected TEX output */
462                 inst_mul->U.I.SrcReg[1].File = RC_FILE_CONSTANT; /* 2 */
463                 inst_mul->U.I.SrcReg[1].Index = two;
464                 inst_mul->U.I.SrcReg[1].Swizzle = two_swizzle;
465
466                 inst_mad = rc_insert_new_instruction(c, inst_mul);
467                 inst_mad->U.I.Opcode = RC_OPCODE_MAD;
468                 inst_mad->U.I.DstReg.File = RC_FILE_TEMPORARY;
469                 inst_mad->U.I.DstReg.Index = rc_find_free_temporary(c);
470                 inst_mad->U.I.SrcReg[0] = inst_mul->U.I.SrcReg[0]; /* redirected TEX output */
471                 inst_mad->U.I.SrcReg[1] = inst_mul->U.I.SrcReg[1]; /* 2 */
472                 inst_mad->U.I.SrcReg[2] = inst_mul->U.I.SrcReg[1]; /* 2 */
473                 inst_mad->U.I.SrcReg[2].Negate = RC_MASK_XYZW;
474
475                 inst_cnd = rc_insert_new_instruction(c, inst_mad);
476                 inst_cnd->U.I.Opcode = RC_OPCODE_CND;
477                 inst_cnd->U.I.SaturateMode = inst->U.I.SaturateMode;
478                 inst_cnd->U.I.DstReg = inst->U.I.DstReg;
479                 inst_cnd->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
480                 inst_cnd->U.I.SrcReg[0].Index = inst_mad->U.I.DstReg.Index;
481                 inst_cnd->U.I.SrcReg[0].Swizzle = compiler->state.unit[inst->U.I.TexSrcUnit].texture_swizzle;
482                 inst_cnd->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
483                 inst_cnd->U.I.SrcReg[1].Index = inst_mul->U.I.DstReg.Index;
484                 inst_cnd->U.I.SrcReg[1].Swizzle = compiler->state.unit[inst->U.I.TexSrcUnit].texture_swizzle;
485                 inst_cnd->U.I.SrcReg[2] = inst_mul->U.I.SrcReg[0]; /* redirected TEX output */
486
487                 inst->U.I.SaturateMode = 0;
488                 inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
489                 inst->U.I.DstReg.Index = inst_mul->U.I.SrcReg[0].Index;
490                 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
491         }
492
493         /* Cannot write texture to output registers or with saturate (all chips),
494          * or with masks (non-r500). */
495         if (inst->U.I.Opcode != RC_OPCODE_KIL &&
496                 (inst->U.I.DstReg.File != RC_FILE_TEMPORARY ||
497                  inst->U.I.SaturateMode ||
498                  (!c->is_r500 && inst->U.I.DstReg.WriteMask != RC_MASK_XYZW))) {
499                 struct rc_instruction * inst_mov = rc_insert_new_instruction(c, inst);
500
501                 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
502                 inst_mov->U.I.SaturateMode = inst->U.I.SaturateMode;
503                 inst_mov->U.I.DstReg = inst->U.I.DstReg;
504                 inst_mov->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
505                 inst_mov->U.I.SrcReg[0].Index = rc_find_free_temporary(c);
506
507                 inst->U.I.SaturateMode = 0;
508                 inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
509                 inst->U.I.DstReg.Index = inst_mov->U.I.SrcReg[0].Index;
510                 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
511         }
512
513         /* Cannot read texture coordinate from constants file */
514         if (inst->U.I.SrcReg[0].File != RC_FILE_TEMPORARY && inst->U.I.SrcReg[0].File != RC_FILE_INPUT) {
515                 struct rc_instruction * inst_mov = rc_insert_new_instruction(c, inst->Prev);
516
517                 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
518                 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
519                 inst_mov->U.I.DstReg.Index = rc_find_free_temporary(c);
520                 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[0];
521
522                 reset_srcreg(&inst->U.I.SrcReg[0]);
523                 inst->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
524                 inst->U.I.SrcReg[0].Index = inst_mov->U.I.DstReg.Index;
525         }
526
527         return 1;
528 }