Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_exec.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * TGSI interpreter/executor.
30 *
31 * Flow control information:
32 *
33 * Since we operate on 'quads' (4 pixels or 4 vertices in parallel)
34 * flow control statements (IF/ELSE/ENDIF, LOOP/ENDLOOP) require special
35 * care since a condition may be true for some quad components but false
36 * for other components.
37 *
38 * We basically execute all statements (even if they're in the part of
39 * an IF/ELSE clause that's "not taken") and use a special mask to
40 * control writing to destination registers. This is the ExecMask.
41 * See store_dest().
42 *
43 * The ExecMask is computed from three other masks (CondMask, LoopMask and
44 * ContMask) which are controlled by the flow control instructions (namely:
45 * (IF/ELSE/ENDIF, LOOP/ENDLOOP and CONT).
46 *
47 *
48 * Authors:
49 * Michal Krol
50 * Brian Paul
51 */
52
53 #include "pipe/p_compiler.h"
54 #include "pipe/p_state.h"
55 #include "pipe/p_shader_tokens.h"
56 #include "tgsi/tgsi_parse.h"
57 #include "tgsi/tgsi_util.h"
58 #include "tgsi_exec.h"
59 #include "util/u_memory.h"
60 #include "util/u_math.h"
61
62 #define FAST_MATH 1
63
64 #define TILE_TOP_LEFT 0
65 #define TILE_TOP_RIGHT 1
66 #define TILE_BOTTOM_LEFT 2
67 #define TILE_BOTTOM_RIGHT 3
68
69 #define CHAN_X 0
70 #define CHAN_Y 1
71 #define CHAN_Z 2
72 #define CHAN_W 3
73
74 /*
75 * Shorthand locations of various utility registers (_I = Index, _C = Channel)
76 */
77 #define TEMP_0_I TGSI_EXEC_TEMP_00000000_I
78 #define TEMP_0_C TGSI_EXEC_TEMP_00000000_C
79 #define TEMP_7F_I TGSI_EXEC_TEMP_7FFFFFFF_I
80 #define TEMP_7F_C TGSI_EXEC_TEMP_7FFFFFFF_C
81 #define TEMP_80_I TGSI_EXEC_TEMP_80000000_I
82 #define TEMP_80_C TGSI_EXEC_TEMP_80000000_C
83 #define TEMP_FF_I TGSI_EXEC_TEMP_FFFFFFFF_I
84 #define TEMP_FF_C TGSI_EXEC_TEMP_FFFFFFFF_C
85 #define TEMP_1_I TGSI_EXEC_TEMP_ONE_I
86 #define TEMP_1_C TGSI_EXEC_TEMP_ONE_C
87 #define TEMP_2_I TGSI_EXEC_TEMP_TWO_I
88 #define TEMP_2_C TGSI_EXEC_TEMP_TWO_C
89 #define TEMP_128_I TGSI_EXEC_TEMP_128_I
90 #define TEMP_128_C TGSI_EXEC_TEMP_128_C
91 #define TEMP_M128_I TGSI_EXEC_TEMP_MINUS_128_I
92 #define TEMP_M128_C TGSI_EXEC_TEMP_MINUS_128_C
93 #define TEMP_KILMASK_I TGSI_EXEC_TEMP_KILMASK_I
94 #define TEMP_KILMASK_C TGSI_EXEC_TEMP_KILMASK_C
95 #define TEMP_OUTPUT_I TGSI_EXEC_TEMP_OUTPUT_I
96 #define TEMP_OUTPUT_C TGSI_EXEC_TEMP_OUTPUT_C
97 #define TEMP_PRIMITIVE_I TGSI_EXEC_TEMP_PRIMITIVE_I
98 #define TEMP_PRIMITIVE_C TGSI_EXEC_TEMP_PRIMITIVE_C
99 #define TEMP_CC_I TGSI_EXEC_TEMP_CC_I
100 #define TEMP_CC_C TGSI_EXEC_TEMP_CC_C
101 #define TEMP_3_I TGSI_EXEC_TEMP_THREE_I
102 #define TEMP_3_C TGSI_EXEC_TEMP_THREE_C
103 #define TEMP_HALF_I TGSI_EXEC_TEMP_HALF_I
104 #define TEMP_HALF_C TGSI_EXEC_TEMP_HALF_C
105 #define TEMP_R0 TGSI_EXEC_TEMP_R0
106
107 #define IS_CHANNEL_ENABLED(INST, CHAN)\
108 ((INST).FullDstRegisters[0].DstRegister.WriteMask & (1 << (CHAN)))
109
110 #define IS_CHANNEL_ENABLED2(INST, CHAN)\
111 ((INST).FullDstRegisters[1].DstRegister.WriteMask & (1 << (CHAN)))
112
113 #define FOR_EACH_ENABLED_CHANNEL(INST, CHAN)\
114 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)\
115 if (IS_CHANNEL_ENABLED( INST, CHAN ))
116
117 #define FOR_EACH_ENABLED_CHANNEL2(INST, CHAN)\
118 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)\
119 if (IS_CHANNEL_ENABLED2( INST, CHAN ))
120
121
122 /** The execution mask depends on the conditional mask and the loop mask */
123 #define UPDATE_EXEC_MASK(MACH) \
124 MACH->ExecMask = MACH->CondMask & MACH->LoopMask & MACH->ContMask & MACH->FuncMask
125
126 /**
127 * Initialize machine state by expanding tokens to full instructions,
128 * allocating temporary storage, setting up constants, etc.
129 * After this, we can call tgsi_exec_machine_run() many times.
130 */
131 void
132 tgsi_exec_machine_bind_shader(
133 struct tgsi_exec_machine *mach,
134 const struct tgsi_token *tokens,
135 uint numSamplers,
136 struct tgsi_sampler *samplers)
137 {
138 uint k;
139 struct tgsi_parse_context parse;
140 struct tgsi_exec_labels *labels = &mach->Labels;
141 struct tgsi_full_instruction *instructions;
142 struct tgsi_full_declaration *declarations;
143 uint maxInstructions = 10, numInstructions = 0;
144 uint maxDeclarations = 10, numDeclarations = 0;
145 uint instno = 0;
146
147 #if 0
148 tgsi_dump(tokens, 0);
149 #endif
150
151 util_init_math();
152
153 mach->Tokens = tokens;
154 mach->Samplers = samplers;
155
156 k = tgsi_parse_init (&parse, mach->Tokens);
157 if (k != TGSI_PARSE_OK) {
158 debug_printf( "Problem parsing!\n" );
159 return;
160 }
161
162 mach->Processor = parse.FullHeader.Processor.Processor;
163 mach->ImmLimit = 0;
164 labels->count = 0;
165
166 declarations = (struct tgsi_full_declaration *)
167 MALLOC( maxDeclarations * sizeof(struct tgsi_full_declaration) );
168
169 if (!declarations) {
170 return;
171 }
172
173 instructions = (struct tgsi_full_instruction *)
174 MALLOC( maxInstructions * sizeof(struct tgsi_full_instruction) );
175
176 if (!instructions) {
177 FREE( declarations );
178 return;
179 }
180
181 while( !tgsi_parse_end_of_tokens( &parse ) ) {
182 uint pointer = parse.Position;
183 uint i;
184
185 tgsi_parse_token( &parse );
186 switch( parse.FullToken.Token.Type ) {
187 case TGSI_TOKEN_TYPE_DECLARATION:
188 /* save expanded declaration */
189 if (numDeclarations == maxDeclarations) {
190 declarations = REALLOC(declarations,
191 maxDeclarations
192 * sizeof(struct tgsi_full_declaration),
193 (maxDeclarations + 10)
194 * sizeof(struct tgsi_full_declaration));
195 maxDeclarations += 10;
196 }
197 memcpy(declarations + numDeclarations,
198 &parse.FullToken.FullDeclaration,
199 sizeof(declarations[0]));
200 numDeclarations++;
201 break;
202
203 case TGSI_TOKEN_TYPE_IMMEDIATE:
204 {
205 uint size = parse.FullToken.FullImmediate.Immediate.Size - 1;
206 assert( size % 4 == 0 );
207 assert( mach->ImmLimit + size / 4 <= TGSI_EXEC_NUM_IMMEDIATES );
208
209 for( i = 0; i < size; i++ ) {
210 mach->Imms[mach->ImmLimit + i / 4][i % 4] =
211 parse.FullToken.FullImmediate.u.ImmediateFloat32[i].Float;
212 }
213 mach->ImmLimit += size / 4;
214 }
215 break;
216
217 case TGSI_TOKEN_TYPE_INSTRUCTION:
218 assert( labels->count < MAX_LABELS );
219
220 labels->labels[labels->count][0] = instno;
221 labels->labels[labels->count][1] = pointer;
222 labels->count++;
223
224 /* save expanded instruction */
225 if (numInstructions == maxInstructions) {
226 instructions = REALLOC(instructions,
227 maxInstructions
228 * sizeof(struct tgsi_full_instruction),
229 (maxInstructions + 10)
230 * sizeof(struct tgsi_full_instruction));
231 maxInstructions += 10;
232 }
233 memcpy(instructions + numInstructions,
234 &parse.FullToken.FullInstruction,
235 sizeof(instructions[0]));
236 numInstructions++;
237 break;
238
239 default:
240 assert( 0 );
241 }
242 }
243 tgsi_parse_free (&parse);
244
245 if (mach->Declarations) {
246 FREE( mach->Declarations );
247 }
248 mach->Declarations = declarations;
249 mach->NumDeclarations = numDeclarations;
250
251 if (mach->Instructions) {
252 FREE( mach->Instructions );
253 }
254 mach->Instructions = instructions;
255 mach->NumInstructions = numInstructions;
256 }
257
258
259 void
260 tgsi_exec_machine_init(
261 struct tgsi_exec_machine *mach )
262 {
263 uint i;
264
265 mach->Temps = (struct tgsi_exec_vector *) tgsi_align_128bit( mach->_Temps);
266 mach->Addrs = &mach->Temps[TGSI_EXEC_TEMP_ADDR];
267
268 /* Setup constants. */
269 for( i = 0; i < 4; i++ ) {
270 mach->Temps[TEMP_0_I].xyzw[TEMP_0_C].u[i] = 0x00000000;
271 mach->Temps[TEMP_7F_I].xyzw[TEMP_7F_C].u[i] = 0x7FFFFFFF;
272 mach->Temps[TEMP_80_I].xyzw[TEMP_80_C].u[i] = 0x80000000;
273 mach->Temps[TEMP_FF_I].xyzw[TEMP_FF_C].u[i] = 0xFFFFFFFF;
274 mach->Temps[TEMP_1_I].xyzw[TEMP_1_C].f[i] = 1.0f;
275 mach->Temps[TEMP_2_I].xyzw[TEMP_2_C].f[i] = 2.0f;
276 mach->Temps[TEMP_128_I].xyzw[TEMP_128_C].f[i] = 128.0f;
277 mach->Temps[TEMP_M128_I].xyzw[TEMP_M128_C].f[i] = -128.0f;
278 mach->Temps[TEMP_3_I].xyzw[TEMP_3_C].f[i] = 3.0f;
279 mach->Temps[TEMP_HALF_I].xyzw[TEMP_HALF_C].f[i] = 0.5f;
280 }
281 }
282
283
284 void
285 tgsi_exec_machine_free_data(struct tgsi_exec_machine *mach)
286 {
287 if (mach->Instructions) {
288 FREE(mach->Instructions);
289 mach->Instructions = NULL;
290 mach->NumInstructions = 0;
291 }
292 if (mach->Declarations) {
293 FREE(mach->Declarations);
294 mach->Declarations = NULL;
295 mach->NumDeclarations = 0;
296 }
297 }
298
299
300 static void
301 micro_abs(
302 union tgsi_exec_channel *dst,
303 const union tgsi_exec_channel *src )
304 {
305 dst->f[0] = fabsf( src->f[0] );
306 dst->f[1] = fabsf( src->f[1] );
307 dst->f[2] = fabsf( src->f[2] );
308 dst->f[3] = fabsf( src->f[3] );
309 }
310
311 static void
312 micro_add(
313 union tgsi_exec_channel *dst,
314 const union tgsi_exec_channel *src0,
315 const union tgsi_exec_channel *src1 )
316 {
317 dst->f[0] = src0->f[0] + src1->f[0];
318 dst->f[1] = src0->f[1] + src1->f[1];
319 dst->f[2] = src0->f[2] + src1->f[2];
320 dst->f[3] = src0->f[3] + src1->f[3];
321 }
322
323 static void
324 micro_iadd(
325 union tgsi_exec_channel *dst,
326 const union tgsi_exec_channel *src0,
327 const union tgsi_exec_channel *src1 )
328 {
329 dst->i[0] = src0->i[0] + src1->i[0];
330 dst->i[1] = src0->i[1] + src1->i[1];
331 dst->i[2] = src0->i[2] + src1->i[2];
332 dst->i[3] = src0->i[3] + src1->i[3];
333 }
334
335 static void
336 micro_and(
337 union tgsi_exec_channel *dst,
338 const union tgsi_exec_channel *src0,
339 const union tgsi_exec_channel *src1 )
340 {
341 dst->u[0] = src0->u[0] & src1->u[0];
342 dst->u[1] = src0->u[1] & src1->u[1];
343 dst->u[2] = src0->u[2] & src1->u[2];
344 dst->u[3] = src0->u[3] & src1->u[3];
345 }
346
347 static void
348 micro_ceil(
349 union tgsi_exec_channel *dst,
350 const union tgsi_exec_channel *src )
351 {
352 dst->f[0] = ceilf( src->f[0] );
353 dst->f[1] = ceilf( src->f[1] );
354 dst->f[2] = ceilf( src->f[2] );
355 dst->f[3] = ceilf( src->f[3] );
356 }
357
358 static void
359 micro_cos(
360 union tgsi_exec_channel *dst,
361 const union tgsi_exec_channel *src )
362 {
363 dst->f[0] = cosf( src->f[0] );
364 dst->f[1] = cosf( src->f[1] );
365 dst->f[2] = cosf( src->f[2] );
366 dst->f[3] = cosf( src->f[3] );
367 }
368
369 static void
370 micro_ddx(
371 union tgsi_exec_channel *dst,
372 const union tgsi_exec_channel *src )
373 {
374 dst->f[0] =
375 dst->f[1] =
376 dst->f[2] =
377 dst->f[3] = src->f[TILE_BOTTOM_RIGHT] - src->f[TILE_BOTTOM_LEFT];
378 }
379
380 static void
381 micro_ddy(
382 union tgsi_exec_channel *dst,
383 const union tgsi_exec_channel *src )
384 {
385 dst->f[0] =
386 dst->f[1] =
387 dst->f[2] =
388 dst->f[3] = src->f[TILE_TOP_LEFT] - src->f[TILE_BOTTOM_LEFT];
389 }
390
391 static void
392 micro_div(
393 union tgsi_exec_channel *dst,
394 const union tgsi_exec_channel *src0,
395 const union tgsi_exec_channel *src1 )
396 {
397 if (src1->f[0] != 0) {
398 dst->f[0] = src0->f[0] / src1->f[0];
399 }
400 if (src1->f[1] != 0) {
401 dst->f[1] = src0->f[1] / src1->f[1];
402 }
403 if (src1->f[2] != 0) {
404 dst->f[2] = src0->f[2] / src1->f[2];
405 }
406 if (src1->f[3] != 0) {
407 dst->f[3] = src0->f[3] / src1->f[3];
408 }
409 }
410
411 static void
412 micro_udiv(
413 union tgsi_exec_channel *dst,
414 const union tgsi_exec_channel *src0,
415 const union tgsi_exec_channel *src1 )
416 {
417 dst->u[0] = src0->u[0] / src1->u[0];
418 dst->u[1] = src0->u[1] / src1->u[1];
419 dst->u[2] = src0->u[2] / src1->u[2];
420 dst->u[3] = src0->u[3] / src1->u[3];
421 }
422
423 static void
424 micro_eq(
425 union tgsi_exec_channel *dst,
426 const union tgsi_exec_channel *src0,
427 const union tgsi_exec_channel *src1,
428 const union tgsi_exec_channel *src2,
429 const union tgsi_exec_channel *src3 )
430 {
431 dst->f[0] = src0->f[0] == src1->f[0] ? src2->f[0] : src3->f[0];
432 dst->f[1] = src0->f[1] == src1->f[1] ? src2->f[1] : src3->f[1];
433 dst->f[2] = src0->f[2] == src1->f[2] ? src2->f[2] : src3->f[2];
434 dst->f[3] = src0->f[3] == src1->f[3] ? src2->f[3] : src3->f[3];
435 }
436
437 static void
438 micro_ieq(
439 union tgsi_exec_channel *dst,
440 const union tgsi_exec_channel *src0,
441 const union tgsi_exec_channel *src1,
442 const union tgsi_exec_channel *src2,
443 const union tgsi_exec_channel *src3 )
444 {
445 dst->i[0] = src0->i[0] == src1->i[0] ? src2->i[0] : src3->i[0];
446 dst->i[1] = src0->i[1] == src1->i[1] ? src2->i[1] : src3->i[1];
447 dst->i[2] = src0->i[2] == src1->i[2] ? src2->i[2] : src3->i[2];
448 dst->i[3] = src0->i[3] == src1->i[3] ? src2->i[3] : src3->i[3];
449 }
450
451 static void
452 micro_exp2(
453 union tgsi_exec_channel *dst,
454 const union tgsi_exec_channel *src)
455 {
456 #if FAST_MATH
457 dst->f[0] = util_fast_exp2( src->f[0] );
458 dst->f[1] = util_fast_exp2( src->f[1] );
459 dst->f[2] = util_fast_exp2( src->f[2] );
460 dst->f[3] = util_fast_exp2( src->f[3] );
461 #else
462 dst->f[0] = powf( 2.0f, src->f[0] );
463 dst->f[1] = powf( 2.0f, src->f[1] );
464 dst->f[2] = powf( 2.0f, src->f[2] );
465 dst->f[3] = powf( 2.0f, src->f[3] );
466 #endif
467 }
468
469 static void
470 micro_f2it(
471 union tgsi_exec_channel *dst,
472 const union tgsi_exec_channel *src )
473 {
474 dst->i[0] = (int) src->f[0];
475 dst->i[1] = (int) src->f[1];
476 dst->i[2] = (int) src->f[2];
477 dst->i[3] = (int) src->f[3];
478 }
479
480 static void
481 micro_f2ut(
482 union tgsi_exec_channel *dst,
483 const union tgsi_exec_channel *src )
484 {
485 dst->u[0] = (uint) src->f[0];
486 dst->u[1] = (uint) src->f[1];
487 dst->u[2] = (uint) src->f[2];
488 dst->u[3] = (uint) src->f[3];
489 }
490
491 static void
492 micro_flr(
493 union tgsi_exec_channel *dst,
494 const union tgsi_exec_channel *src )
495 {
496 dst->f[0] = floorf( src->f[0] );
497 dst->f[1] = floorf( src->f[1] );
498 dst->f[2] = floorf( src->f[2] );
499 dst->f[3] = floorf( src->f[3] );
500 }
501
502 static void
503 micro_frc(
504 union tgsi_exec_channel *dst,
505 const union tgsi_exec_channel *src )
506 {
507 dst->f[0] = src->f[0] - floorf( src->f[0] );
508 dst->f[1] = src->f[1] - floorf( src->f[1] );
509 dst->f[2] = src->f[2] - floorf( src->f[2] );
510 dst->f[3] = src->f[3] - floorf( src->f[3] );
511 }
512
513 static void
514 micro_ge(
515 union tgsi_exec_channel *dst,
516 const union tgsi_exec_channel *src0,
517 const union tgsi_exec_channel *src1,
518 const union tgsi_exec_channel *src2,
519 const union tgsi_exec_channel *src3 )
520 {
521 dst->f[0] = src0->f[0] >= src1->f[0] ? src2->f[0] : src3->f[0];
522 dst->f[1] = src0->f[1] >= src1->f[1] ? src2->f[1] : src3->f[1];
523 dst->f[2] = src0->f[2] >= src1->f[2] ? src2->f[2] : src3->f[2];
524 dst->f[3] = src0->f[3] >= src1->f[3] ? src2->f[3] : src3->f[3];
525 }
526
527 static void
528 micro_i2f(
529 union tgsi_exec_channel *dst,
530 const union tgsi_exec_channel *src )
531 {
532 dst->f[0] = (float) src->i[0];
533 dst->f[1] = (float) src->i[1];
534 dst->f[2] = (float) src->i[2];
535 dst->f[3] = (float) src->i[3];
536 }
537
538 static void
539 micro_lg2(
540 union tgsi_exec_channel *dst,
541 const union tgsi_exec_channel *src )
542 {
543 #if FAST_MATH
544 dst->f[0] = util_fast_log2( src->f[0] );
545 dst->f[1] = util_fast_log2( src->f[1] );
546 dst->f[2] = util_fast_log2( src->f[2] );
547 dst->f[3] = util_fast_log2( src->f[3] );
548 #else
549 dst->f[0] = logf( src->f[0] ) * 1.442695f;
550 dst->f[1] = logf( src->f[1] ) * 1.442695f;
551 dst->f[2] = logf( src->f[2] ) * 1.442695f;
552 dst->f[3] = logf( src->f[3] ) * 1.442695f;
553 #endif
554 }
555
556 static void
557 micro_le(
558 union tgsi_exec_channel *dst,
559 const union tgsi_exec_channel *src0,
560 const union tgsi_exec_channel *src1,
561 const union tgsi_exec_channel *src2,
562 const union tgsi_exec_channel *src3 )
563 {
564 dst->f[0] = src0->f[0] <= src1->f[0] ? src2->f[0] : src3->f[0];
565 dst->f[1] = src0->f[1] <= src1->f[1] ? src2->f[1] : src3->f[1];
566 dst->f[2] = src0->f[2] <= src1->f[2] ? src2->f[2] : src3->f[2];
567 dst->f[3] = src0->f[3] <= src1->f[3] ? src2->f[3] : src3->f[3];
568 }
569
570 static void
571 micro_lt(
572 union tgsi_exec_channel *dst,
573 const union tgsi_exec_channel *src0,
574 const union tgsi_exec_channel *src1,
575 const union tgsi_exec_channel *src2,
576 const union tgsi_exec_channel *src3 )
577 {
578 dst->f[0] = src0->f[0] < src1->f[0] ? src2->f[0] : src3->f[0];
579 dst->f[1] = src0->f[1] < src1->f[1] ? src2->f[1] : src3->f[1];
580 dst->f[2] = src0->f[2] < src1->f[2] ? src2->f[2] : src3->f[2];
581 dst->f[3] = src0->f[3] < src1->f[3] ? src2->f[3] : src3->f[3];
582 }
583
584 static void
585 micro_ilt(
586 union tgsi_exec_channel *dst,
587 const union tgsi_exec_channel *src0,
588 const union tgsi_exec_channel *src1,
589 const union tgsi_exec_channel *src2,
590 const union tgsi_exec_channel *src3 )
591 {
592 dst->i[0] = src0->i[0] < src1->i[0] ? src2->i[0] : src3->i[0];
593 dst->i[1] = src0->i[1] < src1->i[1] ? src2->i[1] : src3->i[1];
594 dst->i[2] = src0->i[2] < src1->i[2] ? src2->i[2] : src3->i[2];
595 dst->i[3] = src0->i[3] < src1->i[3] ? src2->i[3] : src3->i[3];
596 }
597
598 static void
599 micro_ult(
600 union tgsi_exec_channel *dst,
601 const union tgsi_exec_channel *src0,
602 const union tgsi_exec_channel *src1,
603 const union tgsi_exec_channel *src2,
604 const union tgsi_exec_channel *src3 )
605 {
606 dst->u[0] = src0->u[0] < src1->u[0] ? src2->u[0] : src3->u[0];
607 dst->u[1] = src0->u[1] < src1->u[1] ? src2->u[1] : src3->u[1];
608 dst->u[2] = src0->u[2] < src1->u[2] ? src2->u[2] : src3->u[2];
609 dst->u[3] = src0->u[3] < src1->u[3] ? src2->u[3] : src3->u[3];
610 }
611
612 static void
613 micro_max(
614 union tgsi_exec_channel *dst,
615 const union tgsi_exec_channel *src0,
616 const union tgsi_exec_channel *src1 )
617 {
618 dst->f[0] = src0->f[0] > src1->f[0] ? src0->f[0] : src1->f[0];
619 dst->f[1] = src0->f[1] > src1->f[1] ? src0->f[1] : src1->f[1];
620 dst->f[2] = src0->f[2] > src1->f[2] ? src0->f[2] : src1->f[2];
621 dst->f[3] = src0->f[3] > src1->f[3] ? src0->f[3] : src1->f[3];
622 }
623
624 static void
625 micro_imax(
626 union tgsi_exec_channel *dst,
627 const union tgsi_exec_channel *src0,
628 const union tgsi_exec_channel *src1 )
629 {
630 dst->i[0] = src0->i[0] > src1->i[0] ? src0->i[0] : src1->i[0];
631 dst->i[1] = src0->i[1] > src1->i[1] ? src0->i[1] : src1->i[1];
632 dst->i[2] = src0->i[2] > src1->i[2] ? src0->i[2] : src1->i[2];
633 dst->i[3] = src0->i[3] > src1->i[3] ? src0->i[3] : src1->i[3];
634 }
635
636 static void
637 micro_umax(
638 union tgsi_exec_channel *dst,
639 const union tgsi_exec_channel *src0,
640 const union tgsi_exec_channel *src1 )
641 {
642 dst->u[0] = src0->u[0] > src1->u[0] ? src0->u[0] : src1->u[0];
643 dst->u[1] = src0->u[1] > src1->u[1] ? src0->u[1] : src1->u[1];
644 dst->u[2] = src0->u[2] > src1->u[2] ? src0->u[2] : src1->u[2];
645 dst->u[3] = src0->u[3] > src1->u[3] ? src0->u[3] : src1->u[3];
646 }
647
648 static void
649 micro_min(
650 union tgsi_exec_channel *dst,
651 const union tgsi_exec_channel *src0,
652 const union tgsi_exec_channel *src1 )
653 {
654 dst->f[0] = src0->f[0] < src1->f[0] ? src0->f[0] : src1->f[0];
655 dst->f[1] = src0->f[1] < src1->f[1] ? src0->f[1] : src1->f[1];
656 dst->f[2] = src0->f[2] < src1->f[2] ? src0->f[2] : src1->f[2];
657 dst->f[3] = src0->f[3] < src1->f[3] ? src0->f[3] : src1->f[3];
658 }
659
660 static void
661 micro_imin(
662 union tgsi_exec_channel *dst,
663 const union tgsi_exec_channel *src0,
664 const union tgsi_exec_channel *src1 )
665 {
666 dst->i[0] = src0->i[0] < src1->i[0] ? src0->i[0] : src1->i[0];
667 dst->i[1] = src0->i[1] < src1->i[1] ? src0->i[1] : src1->i[1];
668 dst->i[2] = src0->i[2] < src1->i[2] ? src0->i[2] : src1->i[2];
669 dst->i[3] = src0->i[3] < src1->i[3] ? src0->i[3] : src1->i[3];
670 }
671
672 static void
673 micro_umin(
674 union tgsi_exec_channel *dst,
675 const union tgsi_exec_channel *src0,
676 const union tgsi_exec_channel *src1 )
677 {
678 dst->u[0] = src0->u[0] < src1->u[0] ? src0->u[0] : src1->u[0];
679 dst->u[1] = src0->u[1] < src1->u[1] ? src0->u[1] : src1->u[1];
680 dst->u[2] = src0->u[2] < src1->u[2] ? src0->u[2] : src1->u[2];
681 dst->u[3] = src0->u[3] < src1->u[3] ? src0->u[3] : src1->u[3];
682 }
683
684 static void
685 micro_umod(
686 union tgsi_exec_channel *dst,
687 const union tgsi_exec_channel *src0,
688 const union tgsi_exec_channel *src1 )
689 {
690 dst->u[0] = src0->u[0] % src1->u[0];
691 dst->u[1] = src0->u[1] % src1->u[1];
692 dst->u[2] = src0->u[2] % src1->u[2];
693 dst->u[3] = src0->u[3] % src1->u[3];
694 }
695
696 static void
697 micro_mul(
698 union tgsi_exec_channel *dst,
699 const union tgsi_exec_channel *src0,
700 const union tgsi_exec_channel *src1 )
701 {
702 dst->f[0] = src0->f[0] * src1->f[0];
703 dst->f[1] = src0->f[1] * src1->f[1];
704 dst->f[2] = src0->f[2] * src1->f[2];
705 dst->f[3] = src0->f[3] * src1->f[3];
706 }
707
708 static void
709 micro_imul(
710 union tgsi_exec_channel *dst,
711 const union tgsi_exec_channel *src0,
712 const union tgsi_exec_channel *src1 )
713 {
714 dst->i[0] = src0->i[0] * src1->i[0];
715 dst->i[1] = src0->i[1] * src1->i[1];
716 dst->i[2] = src0->i[2] * src1->i[2];
717 dst->i[3] = src0->i[3] * src1->i[3];
718 }
719
720 static void
721 micro_imul64(
722 union tgsi_exec_channel *dst0,
723 union tgsi_exec_channel *dst1,
724 const union tgsi_exec_channel *src0,
725 const union tgsi_exec_channel *src1 )
726 {
727 dst1->i[0] = src0->i[0] * src1->i[0];
728 dst1->i[1] = src0->i[1] * src1->i[1];
729 dst1->i[2] = src0->i[2] * src1->i[2];
730 dst1->i[3] = src0->i[3] * src1->i[3];
731 dst0->i[0] = 0;
732 dst0->i[1] = 0;
733 dst0->i[2] = 0;
734 dst0->i[3] = 0;
735 }
736
737 static void
738 micro_umul64(
739 union tgsi_exec_channel *dst0,
740 union tgsi_exec_channel *dst1,
741 const union tgsi_exec_channel *src0,
742 const union tgsi_exec_channel *src1 )
743 {
744 dst1->u[0] = src0->u[0] * src1->u[0];
745 dst1->u[1] = src0->u[1] * src1->u[1];
746 dst1->u[2] = src0->u[2] * src1->u[2];
747 dst1->u[3] = src0->u[3] * src1->u[3];
748 dst0->u[0] = 0;
749 dst0->u[1] = 0;
750 dst0->u[2] = 0;
751 dst0->u[3] = 0;
752 }
753
754 static void
755 micro_movc(
756 union tgsi_exec_channel *dst,
757 const union tgsi_exec_channel *src0,
758 const union tgsi_exec_channel *src1,
759 const union tgsi_exec_channel *src2 )
760 {
761 dst->u[0] = src0->u[0] ? src1->u[0] : src2->u[0];
762 dst->u[1] = src0->u[1] ? src1->u[1] : src2->u[1];
763 dst->u[2] = src0->u[2] ? src1->u[2] : src2->u[2];
764 dst->u[3] = src0->u[3] ? src1->u[3] : src2->u[3];
765 }
766
767 static void
768 micro_neg(
769 union tgsi_exec_channel *dst,
770 const union tgsi_exec_channel *src )
771 {
772 dst->f[0] = -src->f[0];
773 dst->f[1] = -src->f[1];
774 dst->f[2] = -src->f[2];
775 dst->f[3] = -src->f[3];
776 }
777
778 static void
779 micro_ineg(
780 union tgsi_exec_channel *dst,
781 const union tgsi_exec_channel *src )
782 {
783 dst->i[0] = -src->i[0];
784 dst->i[1] = -src->i[1];
785 dst->i[2] = -src->i[2];
786 dst->i[3] = -src->i[3];
787 }
788
789 static void
790 micro_not(
791 union tgsi_exec_channel *dst,
792 const union tgsi_exec_channel *src )
793 {
794 dst->u[0] = ~src->u[0];
795 dst->u[1] = ~src->u[1];
796 dst->u[2] = ~src->u[2];
797 dst->u[3] = ~src->u[3];
798 }
799
800 static void
801 micro_or(
802 union tgsi_exec_channel *dst,
803 const union tgsi_exec_channel *src0,
804 const union tgsi_exec_channel *src1 )
805 {
806 dst->u[0] = src0->u[0] | src1->u[0];
807 dst->u[1] = src0->u[1] | src1->u[1];
808 dst->u[2] = src0->u[2] | src1->u[2];
809 dst->u[3] = src0->u[3] | src1->u[3];
810 }
811
812 static void
813 micro_pow(
814 union tgsi_exec_channel *dst,
815 const union tgsi_exec_channel *src0,
816 const union tgsi_exec_channel *src1 )
817 {
818 #if FAST_MATH
819 dst->f[0] = util_fast_pow( src0->f[0], src1->f[0] );
820 dst->f[1] = util_fast_pow( src0->f[1], src1->f[1] );
821 dst->f[2] = util_fast_pow( src0->f[2], src1->f[2] );
822 dst->f[3] = util_fast_pow( src0->f[3], src1->f[3] );
823 #else
824 dst->f[0] = powf( src0->f[0], src1->f[0] );
825 dst->f[1] = powf( src0->f[1], src1->f[1] );
826 dst->f[2] = powf( src0->f[2], src1->f[2] );
827 dst->f[3] = powf( src0->f[3], src1->f[3] );
828 #endif
829 }
830
831 static void
832 micro_rnd(
833 union tgsi_exec_channel *dst,
834 const union tgsi_exec_channel *src )
835 {
836 dst->f[0] = floorf( src->f[0] + 0.5f );
837 dst->f[1] = floorf( src->f[1] + 0.5f );
838 dst->f[2] = floorf( src->f[2] + 0.5f );
839 dst->f[3] = floorf( src->f[3] + 0.5f );
840 }
841
842 static void
843 micro_shl(
844 union tgsi_exec_channel *dst,
845 const union tgsi_exec_channel *src0,
846 const union tgsi_exec_channel *src1 )
847 {
848 dst->i[0] = src0->i[0] << src1->i[0];
849 dst->i[1] = src0->i[1] << src1->i[1];
850 dst->i[2] = src0->i[2] << src1->i[2];
851 dst->i[3] = src0->i[3] << src1->i[3];
852 }
853
854 static void
855 micro_ishr(
856 union tgsi_exec_channel *dst,
857 const union tgsi_exec_channel *src0,
858 const union tgsi_exec_channel *src1 )
859 {
860 dst->i[0] = src0->i[0] >> src1->i[0];
861 dst->i[1] = src0->i[1] >> src1->i[1];
862 dst->i[2] = src0->i[2] >> src1->i[2];
863 dst->i[3] = src0->i[3] >> src1->i[3];
864 }
865
866 static void
867 micro_trunc(
868 union tgsi_exec_channel *dst,
869 const union tgsi_exec_channel *src0 )
870 {
871 dst->f[0] = (float) (int) src0->f[0];
872 dst->f[1] = (float) (int) src0->f[1];
873 dst->f[2] = (float) (int) src0->f[2];
874 dst->f[3] = (float) (int) src0->f[3];
875 }
876
877 static void
878 micro_ushr(
879 union tgsi_exec_channel *dst,
880 const union tgsi_exec_channel *src0,
881 const union tgsi_exec_channel *src1 )
882 {
883 dst->u[0] = src0->u[0] >> src1->u[0];
884 dst->u[1] = src0->u[1] >> src1->u[1];
885 dst->u[2] = src0->u[2] >> src1->u[2];
886 dst->u[3] = src0->u[3] >> src1->u[3];
887 }
888
889 static void
890 micro_sin(
891 union tgsi_exec_channel *dst,
892 const union tgsi_exec_channel *src )
893 {
894 dst->f[0] = sinf( src->f[0] );
895 dst->f[1] = sinf( src->f[1] );
896 dst->f[2] = sinf( src->f[2] );
897 dst->f[3] = sinf( src->f[3] );
898 }
899
900 static void
901 micro_sqrt( union tgsi_exec_channel *dst,
902 const union tgsi_exec_channel *src )
903 {
904 dst->f[0] = sqrtf( src->f[0] );
905 dst->f[1] = sqrtf( src->f[1] );
906 dst->f[2] = sqrtf( src->f[2] );
907 dst->f[3] = sqrtf( src->f[3] );
908 }
909
910 static void
911 micro_sub(
912 union tgsi_exec_channel *dst,
913 const union tgsi_exec_channel *src0,
914 const union tgsi_exec_channel *src1 )
915 {
916 dst->f[0] = src0->f[0] - src1->f[0];
917 dst->f[1] = src0->f[1] - src1->f[1];
918 dst->f[2] = src0->f[2] - src1->f[2];
919 dst->f[3] = src0->f[3] - src1->f[3];
920 }
921
922 static void
923 micro_u2f(
924 union tgsi_exec_channel *dst,
925 const union tgsi_exec_channel *src )
926 {
927 dst->f[0] = (float) src->u[0];
928 dst->f[1] = (float) src->u[1];
929 dst->f[2] = (float) src->u[2];
930 dst->f[3] = (float) src->u[3];
931 }
932
933 static void
934 micro_xor(
935 union tgsi_exec_channel *dst,
936 const union tgsi_exec_channel *src0,
937 const union tgsi_exec_channel *src1 )
938 {
939 dst->u[0] = src0->u[0] ^ src1->u[0];
940 dst->u[1] = src0->u[1] ^ src1->u[1];
941 dst->u[2] = src0->u[2] ^ src1->u[2];
942 dst->u[3] = src0->u[3] ^ src1->u[3];
943 }
944
945 static void
946 fetch_src_file_channel(
947 const struct tgsi_exec_machine *mach,
948 const uint file,
949 const uint swizzle,
950 const union tgsi_exec_channel *index,
951 union tgsi_exec_channel *chan )
952 {
953 switch( swizzle ) {
954 case TGSI_EXTSWIZZLE_X:
955 case TGSI_EXTSWIZZLE_Y:
956 case TGSI_EXTSWIZZLE_Z:
957 case TGSI_EXTSWIZZLE_W:
958 switch( file ) {
959 case TGSI_FILE_CONSTANT:
960 assert(mach->Consts);
961 assert(index->i[0] >= 0);
962 assert(index->i[1] >= 0);
963 assert(index->i[2] >= 0);
964 assert(index->i[3] >= 0);
965 chan->f[0] = mach->Consts[index->i[0]][swizzle];
966 chan->f[1] = mach->Consts[index->i[1]][swizzle];
967 chan->f[2] = mach->Consts[index->i[2]][swizzle];
968 chan->f[3] = mach->Consts[index->i[3]][swizzle];
969 break;
970
971 case TGSI_FILE_INPUT:
972 chan->u[0] = mach->Inputs[index->i[0]].xyzw[swizzle].u[0];
973 chan->u[1] = mach->Inputs[index->i[1]].xyzw[swizzle].u[1];
974 chan->u[2] = mach->Inputs[index->i[2]].xyzw[swizzle].u[2];
975 chan->u[3] = mach->Inputs[index->i[3]].xyzw[swizzle].u[3];
976 break;
977
978 case TGSI_FILE_TEMPORARY:
979 assert(index->i[0] < TGSI_EXEC_NUM_TEMPS);
980 chan->u[0] = mach->Temps[index->i[0]].xyzw[swizzle].u[0];
981 chan->u[1] = mach->Temps[index->i[1]].xyzw[swizzle].u[1];
982 chan->u[2] = mach->Temps[index->i[2]].xyzw[swizzle].u[2];
983 chan->u[3] = mach->Temps[index->i[3]].xyzw[swizzle].u[3];
984 break;
985
986 case TGSI_FILE_IMMEDIATE:
987 assert( index->i[0] < (int) mach->ImmLimit );
988 chan->f[0] = mach->Imms[index->i[0]][swizzle];
989 assert( index->i[1] < (int) mach->ImmLimit );
990 chan->f[1] = mach->Imms[index->i[1]][swizzle];
991 assert( index->i[2] < (int) mach->ImmLimit );
992 chan->f[2] = mach->Imms[index->i[2]][swizzle];
993 assert( index->i[3] < (int) mach->ImmLimit );
994 chan->f[3] = mach->Imms[index->i[3]][swizzle];
995 break;
996
997 case TGSI_FILE_ADDRESS:
998 chan->u[0] = mach->Addrs[index->i[0]].xyzw[swizzle].u[0];
999 chan->u[1] = mach->Addrs[index->i[1]].xyzw[swizzle].u[1];
1000 chan->u[2] = mach->Addrs[index->i[2]].xyzw[swizzle].u[2];
1001 chan->u[3] = mach->Addrs[index->i[3]].xyzw[swizzle].u[3];
1002 break;
1003
1004 case TGSI_FILE_OUTPUT:
1005 /* vertex/fragment output vars can be read too */
1006 chan->u[0] = mach->Outputs[index->i[0]].xyzw[swizzle].u[0];
1007 chan->u[1] = mach->Outputs[index->i[1]].xyzw[swizzle].u[1];
1008 chan->u[2] = mach->Outputs[index->i[2]].xyzw[swizzle].u[2];
1009 chan->u[3] = mach->Outputs[index->i[3]].xyzw[swizzle].u[3];
1010 break;
1011
1012 default:
1013 assert( 0 );
1014 }
1015 break;
1016
1017 case TGSI_EXTSWIZZLE_ZERO:
1018 *chan = mach->Temps[TEMP_0_I].xyzw[TEMP_0_C];
1019 break;
1020
1021 case TGSI_EXTSWIZZLE_ONE:
1022 *chan = mach->Temps[TEMP_1_I].xyzw[TEMP_1_C];
1023 break;
1024
1025 default:
1026 assert( 0 );
1027 }
1028 }
1029
1030 static void
1031 fetch_source(
1032 const struct tgsi_exec_machine *mach,
1033 union tgsi_exec_channel *chan,
1034 const struct tgsi_full_src_register *reg,
1035 const uint chan_index )
1036 {
1037 union tgsi_exec_channel index;
1038 uint swizzle;
1039
1040 index.i[0] =
1041 index.i[1] =
1042 index.i[2] =
1043 index.i[3] = reg->SrcRegister.Index;
1044
1045 if (reg->SrcRegister.Indirect) {
1046 union tgsi_exec_channel index2;
1047 union tgsi_exec_channel indir_index;
1048 const uint execmask = mach->ExecMask;
1049 uint i;
1050
1051 /* which address register (always zero now) */
1052 index2.i[0] =
1053 index2.i[1] =
1054 index2.i[2] =
1055 index2.i[3] = reg->SrcRegisterInd.Index;
1056
1057 /* get current value of address register[swizzle] */
1058 swizzle = tgsi_util_get_src_register_swizzle( &reg->SrcRegisterInd, CHAN_X );
1059 fetch_src_file_channel(
1060 mach,
1061 reg->SrcRegisterInd.File,
1062 swizzle,
1063 &index2,
1064 &indir_index );
1065
1066 /* add value of address register to the offset */
1067 index.i[0] += indir_index.i[0];
1068 index.i[1] += indir_index.i[1];
1069 index.i[2] += indir_index.i[2];
1070 index.i[3] += indir_index.i[3];
1071
1072 /* for disabled execution channels, zero-out the index to
1073 * avoid using a potential garbage value.
1074 */
1075 for (i = 0; i < QUAD_SIZE; i++) {
1076 if ((execmask & (1 << i)) == 0)
1077 index.i[i] = 0;
1078 }
1079 }
1080
1081 if( reg->SrcRegister.Dimension ) {
1082 switch( reg->SrcRegister.File ) {
1083 case TGSI_FILE_INPUT:
1084 index.i[0] *= 17;
1085 index.i[1] *= 17;
1086 index.i[2] *= 17;
1087 index.i[3] *= 17;
1088 break;
1089 case TGSI_FILE_CONSTANT:
1090 index.i[0] *= 4096;
1091 index.i[1] *= 4096;
1092 index.i[2] *= 4096;
1093 index.i[3] *= 4096;
1094 break;
1095 default:
1096 assert( 0 );
1097 }
1098
1099 index.i[0] += reg->SrcRegisterDim.Index;
1100 index.i[1] += reg->SrcRegisterDim.Index;
1101 index.i[2] += reg->SrcRegisterDim.Index;
1102 index.i[3] += reg->SrcRegisterDim.Index;
1103
1104 if (reg->SrcRegisterDim.Indirect) {
1105 union tgsi_exec_channel index2;
1106 union tgsi_exec_channel indir_index;
1107 const uint execmask = mach->ExecMask;
1108 uint i;
1109
1110 index2.i[0] =
1111 index2.i[1] =
1112 index2.i[2] =
1113 index2.i[3] = reg->SrcRegisterDimInd.Index;
1114
1115 swizzle = tgsi_util_get_src_register_swizzle( &reg->SrcRegisterDimInd, CHAN_X );
1116 fetch_src_file_channel(
1117 mach,
1118 reg->SrcRegisterDimInd.File,
1119 swizzle,
1120 &index2,
1121 &indir_index );
1122
1123 index.i[0] += indir_index.i[0];
1124 index.i[1] += indir_index.i[1];
1125 index.i[2] += indir_index.i[2];
1126 index.i[3] += indir_index.i[3];
1127
1128 /* for disabled execution channels, zero-out the index to
1129 * avoid using a potential garbage value.
1130 */
1131 for (i = 0; i < QUAD_SIZE; i++) {
1132 if ((execmask & (1 << i)) == 0)
1133 index.i[i] = 0;
1134 }
1135 }
1136 }
1137
1138 swizzle = tgsi_util_get_full_src_register_extswizzle( reg, chan_index );
1139 fetch_src_file_channel(
1140 mach,
1141 reg->SrcRegister.File,
1142 swizzle,
1143 &index,
1144 chan );
1145
1146 switch (tgsi_util_get_full_src_register_sign_mode( reg, chan_index )) {
1147 case TGSI_UTIL_SIGN_CLEAR:
1148 micro_abs( chan, chan );
1149 break;
1150
1151 case TGSI_UTIL_SIGN_SET:
1152 micro_abs( chan, chan );
1153 micro_neg( chan, chan );
1154 break;
1155
1156 case TGSI_UTIL_SIGN_TOGGLE:
1157 micro_neg( chan, chan );
1158 break;
1159
1160 case TGSI_UTIL_SIGN_KEEP:
1161 break;
1162 }
1163
1164 if (reg->SrcRegisterExtMod.Complement) {
1165 micro_sub( chan, &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], chan );
1166 }
1167 }
1168
1169 static void
1170 store_dest(
1171 struct tgsi_exec_machine *mach,
1172 const union tgsi_exec_channel *chan,
1173 const struct tgsi_full_dst_register *reg,
1174 const struct tgsi_full_instruction *inst,
1175 uint chan_index )
1176 {
1177 uint i;
1178 union tgsi_exec_channel null;
1179 union tgsi_exec_channel *dst;
1180 uint execmask = mach->ExecMask;
1181
1182 switch (reg->DstRegister.File) {
1183 case TGSI_FILE_NULL:
1184 dst = &null;
1185 break;
1186
1187 case TGSI_FILE_OUTPUT:
1188 dst = &mach->Outputs[mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0]
1189 + reg->DstRegister.Index].xyzw[chan_index];
1190 break;
1191
1192 case TGSI_FILE_TEMPORARY:
1193 assert( reg->DstRegister.Index < TGSI_EXEC_NUM_TEMPS );
1194 dst = &mach->Temps[reg->DstRegister.Index].xyzw[chan_index];
1195 break;
1196
1197 case TGSI_FILE_ADDRESS:
1198 dst = &mach->Addrs[reg->DstRegister.Index].xyzw[chan_index];
1199 break;
1200
1201 default:
1202 assert( 0 );
1203 return;
1204 }
1205
1206 if (inst->InstructionExtNv.CondFlowEnable) {
1207 union tgsi_exec_channel *cc = &mach->Temps[TEMP_CC_I].xyzw[TEMP_CC_C];
1208 uint swizzle;
1209 uint shift;
1210 uint mask;
1211 uint test;
1212
1213 /* Only CC0 supported.
1214 */
1215 assert( inst->InstructionExtNv.CondFlowIndex < 1 );
1216
1217 switch (chan_index) {
1218 case CHAN_X:
1219 swizzle = inst->InstructionExtNv.CondSwizzleX;
1220 break;
1221 case CHAN_Y:
1222 swizzle = inst->InstructionExtNv.CondSwizzleY;
1223 break;
1224 case CHAN_Z:
1225 swizzle = inst->InstructionExtNv.CondSwizzleZ;
1226 break;
1227 case CHAN_W:
1228 swizzle = inst->InstructionExtNv.CondSwizzleW;
1229 break;
1230 default:
1231 assert( 0 );
1232 return;
1233 }
1234
1235 switch (swizzle) {
1236 case TGSI_SWIZZLE_X:
1237 shift = TGSI_EXEC_CC_X_SHIFT;
1238 mask = TGSI_EXEC_CC_X_MASK;
1239 break;
1240 case TGSI_SWIZZLE_Y:
1241 shift = TGSI_EXEC_CC_Y_SHIFT;
1242 mask = TGSI_EXEC_CC_Y_MASK;
1243 break;
1244 case TGSI_SWIZZLE_Z:
1245 shift = TGSI_EXEC_CC_Z_SHIFT;
1246 mask = TGSI_EXEC_CC_Z_MASK;
1247 break;
1248 case TGSI_SWIZZLE_W:
1249 shift = TGSI_EXEC_CC_W_SHIFT;
1250 mask = TGSI_EXEC_CC_W_MASK;
1251 break;
1252 default:
1253 assert( 0 );
1254 return;
1255 }
1256
1257 switch (inst->InstructionExtNv.CondMask) {
1258 case TGSI_CC_GT:
1259 test = ~(TGSI_EXEC_CC_GT << shift) & mask;
1260 for (i = 0; i < QUAD_SIZE; i++)
1261 if (cc->u[i] & test)
1262 execmask &= ~(1 << i);
1263 break;
1264
1265 case TGSI_CC_EQ:
1266 test = ~(TGSI_EXEC_CC_EQ << shift) & mask;
1267 for (i = 0; i < QUAD_SIZE; i++)
1268 if (cc->u[i] & test)
1269 execmask &= ~(1 << i);
1270 break;
1271
1272 case TGSI_CC_LT:
1273 test = ~(TGSI_EXEC_CC_LT << shift) & mask;
1274 for (i = 0; i < QUAD_SIZE; i++)
1275 if (cc->u[i] & test)
1276 execmask &= ~(1 << i);
1277 break;
1278
1279 case TGSI_CC_GE:
1280 test = ~((TGSI_EXEC_CC_GT | TGSI_EXEC_CC_EQ) << shift) & mask;
1281 for (i = 0; i < QUAD_SIZE; i++)
1282 if (cc->u[i] & test)
1283 execmask &= ~(1 << i);
1284 break;
1285
1286 case TGSI_CC_LE:
1287 test = ~((TGSI_EXEC_CC_LT | TGSI_EXEC_CC_EQ) << shift) & mask;
1288 for (i = 0; i < QUAD_SIZE; i++)
1289 if (cc->u[i] & test)
1290 execmask &= ~(1 << i);
1291 break;
1292
1293 case TGSI_CC_NE:
1294 test = ~((TGSI_EXEC_CC_GT | TGSI_EXEC_CC_LT | TGSI_EXEC_CC_UN) << shift) & mask;
1295 for (i = 0; i < QUAD_SIZE; i++)
1296 if (cc->u[i] & test)
1297 execmask &= ~(1 << i);
1298 break;
1299
1300 case TGSI_CC_TR:
1301 break;
1302
1303 case TGSI_CC_FL:
1304 for (i = 0; i < QUAD_SIZE; i++)
1305 execmask &= ~(1 << i);
1306 break;
1307
1308 default:
1309 assert( 0 );
1310 return;
1311 }
1312 }
1313
1314 switch (inst->Instruction.Saturate) {
1315 case TGSI_SAT_NONE:
1316 for (i = 0; i < QUAD_SIZE; i++)
1317 if (execmask & (1 << i))
1318 dst->i[i] = chan->i[i];
1319 break;
1320
1321 case TGSI_SAT_ZERO_ONE:
1322 for (i = 0; i < QUAD_SIZE; i++)
1323 if (execmask & (1 << i)) {
1324 if (chan->f[i] < 0.0f)
1325 dst->f[i] = 0.0f;
1326 else if (chan->f[i] > 1.0f)
1327 dst->f[i] = 1.0f;
1328 else
1329 dst->i[i] = chan->i[i];
1330 }
1331 break;
1332
1333 case TGSI_SAT_MINUS_PLUS_ONE:
1334 for (i = 0; i < QUAD_SIZE; i++)
1335 if (execmask & (1 << i)) {
1336 if (chan->f[i] < -1.0f)
1337 dst->f[i] = -1.0f;
1338 else if (chan->f[i] > 1.0f)
1339 dst->f[i] = 1.0f;
1340 else
1341 dst->i[i] = chan->i[i];
1342 }
1343 break;
1344
1345 default:
1346 assert( 0 );
1347 }
1348
1349 if (inst->InstructionExtNv.CondDstUpdate) {
1350 union tgsi_exec_channel *cc = &mach->Temps[TEMP_CC_I].xyzw[TEMP_CC_C];
1351 uint shift;
1352 uint mask;
1353
1354 /* Only CC0 supported.
1355 */
1356 assert( inst->InstructionExtNv.CondDstIndex < 1 );
1357
1358 switch (chan_index) {
1359 case CHAN_X:
1360 shift = TGSI_EXEC_CC_X_SHIFT;
1361 mask = ~TGSI_EXEC_CC_X_MASK;
1362 break;
1363 case CHAN_Y:
1364 shift = TGSI_EXEC_CC_Y_SHIFT;
1365 mask = ~TGSI_EXEC_CC_Y_MASK;
1366 break;
1367 case CHAN_Z:
1368 shift = TGSI_EXEC_CC_Z_SHIFT;
1369 mask = ~TGSI_EXEC_CC_Z_MASK;
1370 break;
1371 case CHAN_W:
1372 shift = TGSI_EXEC_CC_W_SHIFT;
1373 mask = ~TGSI_EXEC_CC_W_MASK;
1374 break;
1375 default:
1376 assert( 0 );
1377 return;
1378 }
1379
1380 for (i = 0; i < QUAD_SIZE; i++)
1381 if (execmask & (1 << i)) {
1382 cc->u[i] &= mask;
1383 if (dst->f[i] < 0.0f)
1384 cc->u[i] |= TGSI_EXEC_CC_LT << shift;
1385 else if (dst->f[i] > 0.0f)
1386 cc->u[i] |= TGSI_EXEC_CC_GT << shift;
1387 else if (dst->f[i] == 0.0f)
1388 cc->u[i] |= TGSI_EXEC_CC_EQ << shift;
1389 else
1390 cc->u[i] |= TGSI_EXEC_CC_UN << shift;
1391 }
1392 }
1393 }
1394
1395 #define FETCH(VAL,INDEX,CHAN)\
1396 fetch_source (mach, VAL, &inst->FullSrcRegisters[INDEX], CHAN)
1397
1398 #define STORE(VAL,INDEX,CHAN)\
1399 store_dest (mach, VAL, &inst->FullDstRegisters[INDEX], inst, CHAN )
1400
1401
1402 /**
1403 * Execute ARB-style KIL which is predicated by a src register.
1404 * Kill fragment if any of the four values is less than zero.
1405 */
1406 static void
1407 exec_kil(struct tgsi_exec_machine *mach,
1408 const struct tgsi_full_instruction *inst)
1409 {
1410 uint uniquemask;
1411 uint chan_index;
1412 uint kilmask = 0; /* bit 0 = pixel 0, bit 1 = pixel 1, etc */
1413 union tgsi_exec_channel r[1];
1414
1415 /* This mask stores component bits that were already tested. Note that
1416 * we test if the value is less than zero, so 1.0 and 0.0 need not to be
1417 * tested. */
1418 uniquemask = (1 << TGSI_EXTSWIZZLE_ZERO) | (1 << TGSI_EXTSWIZZLE_ONE);
1419
1420 for (chan_index = 0; chan_index < 4; chan_index++)
1421 {
1422 uint swizzle;
1423 uint i;
1424
1425 /* unswizzle channel */
1426 swizzle = tgsi_util_get_full_src_register_extswizzle (
1427 &inst->FullSrcRegisters[0],
1428 chan_index);
1429
1430 /* check if the component has not been already tested */
1431 if (uniquemask & (1 << swizzle))
1432 continue;
1433 uniquemask |= 1 << swizzle;
1434
1435 FETCH(&r[0], 0, chan_index);
1436 for (i = 0; i < 4; i++)
1437 if (r[0].f[i] < 0.0f)
1438 kilmask |= 1 << i;
1439 }
1440
1441 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] |= kilmask;
1442 }
1443
1444 /**
1445 * Execute NVIDIA-style KIL which is predicated by a condition code.
1446 * Kill fragment if the condition code is TRUE.
1447 */
1448 static void
1449 exec_kilp(struct tgsi_exec_machine *mach,
1450 const struct tgsi_full_instruction *inst)
1451 {
1452 uint kilmask; /* bit 0 = pixel 0, bit 1 = pixel 1, etc */
1453
1454 if (inst->InstructionExtNv.CondFlowEnable) {
1455 uint swizzle[4];
1456 uint chan_index;
1457
1458 kilmask = 0x0;
1459
1460 swizzle[0] = inst->InstructionExtNv.CondSwizzleX;
1461 swizzle[1] = inst->InstructionExtNv.CondSwizzleY;
1462 swizzle[2] = inst->InstructionExtNv.CondSwizzleZ;
1463 swizzle[3] = inst->InstructionExtNv.CondSwizzleW;
1464
1465 for (chan_index = 0; chan_index < 4; chan_index++)
1466 {
1467 uint i;
1468
1469 for (i = 0; i < 4; i++) {
1470 /* TODO: evaluate the condition code */
1471 if (0)
1472 kilmask |= 1 << i;
1473 }
1474 }
1475 }
1476 else {
1477 /* "unconditional" kil */
1478 kilmask = mach->ExecMask;
1479 }
1480 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] |= kilmask;
1481 }
1482
1483
1484 /*
1485 * Fetch a texel using STR texture coordinates.
1486 */
1487 static void
1488 fetch_texel( struct tgsi_sampler *sampler,
1489 const union tgsi_exec_channel *s,
1490 const union tgsi_exec_channel *t,
1491 const union tgsi_exec_channel *p,
1492 float lodbias, /* XXX should be float[4] */
1493 union tgsi_exec_channel *r,
1494 union tgsi_exec_channel *g,
1495 union tgsi_exec_channel *b,
1496 union tgsi_exec_channel *a )
1497 {
1498 uint j;
1499 float rgba[NUM_CHANNELS][QUAD_SIZE];
1500
1501 sampler->get_samples(sampler, s->f, t->f, p->f, lodbias, rgba);
1502
1503 for (j = 0; j < 4; j++) {
1504 r->f[j] = rgba[0][j];
1505 g->f[j] = rgba[1][j];
1506 b->f[j] = rgba[2][j];
1507 a->f[j] = rgba[3][j];
1508 }
1509 }
1510
1511
1512 static void
1513 exec_tex(struct tgsi_exec_machine *mach,
1514 const struct tgsi_full_instruction *inst,
1515 boolean biasLod,
1516 boolean projected)
1517 {
1518 const uint unit = inst->FullSrcRegisters[1].SrcRegister.Index;
1519 union tgsi_exec_channel r[8];
1520 uint chan_index;
1521 float lodBias;
1522
1523 /* debug_printf("Sampler %u unit %u\n", sampler, unit); */
1524
1525 switch (inst->InstructionExtTexture.Texture) {
1526 case TGSI_TEXTURE_1D:
1527
1528 FETCH(&r[0], 0, CHAN_X);
1529
1530 if (projected) {
1531 FETCH(&r[1], 0, CHAN_W);
1532 micro_div( &r[0], &r[0], &r[1] );
1533 }
1534
1535 if (biasLod) {
1536 FETCH(&r[1], 0, CHAN_W);
1537 lodBias = r[2].f[0];
1538 }
1539 else
1540 lodBias = 0.0;
1541
1542 fetch_texel(&mach->Samplers[unit],
1543 &r[0], NULL, NULL, lodBias, /* S, T, P, BIAS */
1544 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
1545 break;
1546
1547 case TGSI_TEXTURE_2D:
1548 case TGSI_TEXTURE_RECT:
1549
1550 FETCH(&r[0], 0, CHAN_X);
1551 FETCH(&r[1], 0, CHAN_Y);
1552 FETCH(&r[2], 0, CHAN_Z);
1553
1554 if (projected) {
1555 FETCH(&r[3], 0, CHAN_W);
1556 micro_div( &r[0], &r[0], &r[3] );
1557 micro_div( &r[1], &r[1], &r[3] );
1558 micro_div( &r[2], &r[2], &r[3] );
1559 }
1560
1561 if (biasLod) {
1562 FETCH(&r[3], 0, CHAN_W);
1563 lodBias = r[3].f[0];
1564 }
1565 else
1566 lodBias = 0.0;
1567
1568 fetch_texel(&mach->Samplers[unit],
1569 &r[0], &r[1], &r[2], lodBias, /* inputs */
1570 &r[0], &r[1], &r[2], &r[3]); /* outputs */
1571 break;
1572
1573 case TGSI_TEXTURE_3D:
1574 case TGSI_TEXTURE_CUBE:
1575
1576 FETCH(&r[0], 0, CHAN_X);
1577 FETCH(&r[1], 0, CHAN_Y);
1578 FETCH(&r[2], 0, CHAN_Z);
1579
1580 if (projected) {
1581 FETCH(&r[3], 0, CHAN_W);
1582 micro_div( &r[0], &r[0], &r[3] );
1583 micro_div( &r[1], &r[1], &r[3] );
1584 micro_div( &r[2], &r[2], &r[3] );
1585 }
1586
1587 if (biasLod) {
1588 FETCH(&r[3], 0, CHAN_W);
1589 lodBias = r[3].f[0];
1590 }
1591 else
1592 lodBias = 0.0;
1593
1594 fetch_texel(&mach->Samplers[unit],
1595 &r[0], &r[1], &r[2], lodBias,
1596 &r[0], &r[1], &r[2], &r[3]);
1597 break;
1598
1599 default:
1600 assert (0);
1601 }
1602
1603 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1604 STORE( &r[chan_index], 0, chan_index );
1605 }
1606 }
1607
1608
1609 /**
1610 * Evaluate a constant-valued coefficient at the position of the
1611 * current quad.
1612 */
1613 static void
1614 eval_constant_coef(
1615 struct tgsi_exec_machine *mach,
1616 unsigned attrib,
1617 unsigned chan )
1618 {
1619 unsigned i;
1620
1621 for( i = 0; i < QUAD_SIZE; i++ ) {
1622 mach->Inputs[attrib].xyzw[chan].f[i] = mach->InterpCoefs[attrib].a0[chan];
1623 }
1624 }
1625
1626 /**
1627 * Evaluate a linear-valued coefficient at the position of the
1628 * current quad.
1629 */
1630 static void
1631 eval_linear_coef(
1632 struct tgsi_exec_machine *mach,
1633 unsigned attrib,
1634 unsigned chan )
1635 {
1636 const float x = mach->QuadPos.xyzw[0].f[0];
1637 const float y = mach->QuadPos.xyzw[1].f[0];
1638 const float dadx = mach->InterpCoefs[attrib].dadx[chan];
1639 const float dady = mach->InterpCoefs[attrib].dady[chan];
1640 const float a0 = mach->InterpCoefs[attrib].a0[chan] + dadx * x + dady * y;
1641 mach->Inputs[attrib].xyzw[chan].f[0] = a0;
1642 mach->Inputs[attrib].xyzw[chan].f[1] = a0 + dadx;
1643 mach->Inputs[attrib].xyzw[chan].f[2] = a0 + dady;
1644 mach->Inputs[attrib].xyzw[chan].f[3] = a0 + dadx + dady;
1645 }
1646
1647 /**
1648 * Evaluate a perspective-valued coefficient at the position of the
1649 * current quad.
1650 */
1651 static void
1652 eval_perspective_coef(
1653 struct tgsi_exec_machine *mach,
1654 unsigned attrib,
1655 unsigned chan )
1656 {
1657 const float x = mach->QuadPos.xyzw[0].f[0];
1658 const float y = mach->QuadPos.xyzw[1].f[0];
1659 const float dadx = mach->InterpCoefs[attrib].dadx[chan];
1660 const float dady = mach->InterpCoefs[attrib].dady[chan];
1661 const float a0 = mach->InterpCoefs[attrib].a0[chan] + dadx * x + dady * y;
1662 const float *w = mach->QuadPos.xyzw[3].f;
1663 /* divide by W here */
1664 mach->Inputs[attrib].xyzw[chan].f[0] = a0 / w[0];
1665 mach->Inputs[attrib].xyzw[chan].f[1] = (a0 + dadx) / w[1];
1666 mach->Inputs[attrib].xyzw[chan].f[2] = (a0 + dady) / w[2];
1667 mach->Inputs[attrib].xyzw[chan].f[3] = (a0 + dadx + dady) / w[3];
1668 }
1669
1670
1671 typedef void (* eval_coef_func)(
1672 struct tgsi_exec_machine *mach,
1673 unsigned attrib,
1674 unsigned chan );
1675
1676 static void
1677 exec_declaration(
1678 struct tgsi_exec_machine *mach,
1679 const struct tgsi_full_declaration *decl )
1680 {
1681 if( mach->Processor == TGSI_PROCESSOR_FRAGMENT ) {
1682 if( decl->Declaration.File == TGSI_FILE_INPUT ) {
1683 unsigned first, last, mask;
1684 eval_coef_func eval;
1685
1686 first = decl->DeclarationRange.First;
1687 last = decl->DeclarationRange.Last;
1688 mask = decl->Declaration.UsageMask;
1689
1690 switch( decl->Declaration.Interpolate ) {
1691 case TGSI_INTERPOLATE_CONSTANT:
1692 eval = eval_constant_coef;
1693 break;
1694
1695 case TGSI_INTERPOLATE_LINEAR:
1696 eval = eval_linear_coef;
1697 break;
1698
1699 case TGSI_INTERPOLATE_PERSPECTIVE:
1700 eval = eval_perspective_coef;
1701 break;
1702
1703 default:
1704 eval = NULL;
1705 assert( 0 );
1706 }
1707
1708 if( mask == TGSI_WRITEMASK_XYZW ) {
1709 unsigned i, j;
1710
1711 for( i = first; i <= last; i++ ) {
1712 for( j = 0; j < NUM_CHANNELS; j++ ) {
1713 eval( mach, i, j );
1714 }
1715 }
1716 }
1717 else {
1718 unsigned i, j;
1719
1720 for( j = 0; j < NUM_CHANNELS; j++ ) {
1721 if( mask & (1 << j) ) {
1722 for( i = first; i <= last; i++ ) {
1723 eval( mach, i, j );
1724 }
1725 }
1726 }
1727 }
1728 }
1729 }
1730 }
1731
1732 static void
1733 exec_instruction(
1734 struct tgsi_exec_machine *mach,
1735 const struct tgsi_full_instruction *inst,
1736 int *pc )
1737 {
1738 uint chan_index;
1739 union tgsi_exec_channel r[8];
1740
1741 (*pc)++;
1742
1743 switch (inst->Instruction.Opcode) {
1744 case TGSI_OPCODE_ARL:
1745 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1746 FETCH( &r[0], 0, chan_index );
1747 micro_f2it( &r[0], &r[0] );
1748 STORE( &r[0], 0, chan_index );
1749 }
1750 break;
1751
1752 case TGSI_OPCODE_MOV:
1753 case TGSI_OPCODE_SWZ:
1754 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1755 FETCH( &r[0], 0, chan_index );
1756 STORE( &r[0], 0, chan_index );
1757 }
1758 break;
1759
1760 case TGSI_OPCODE_LIT:
1761 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
1762 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_X );
1763 }
1764
1765 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y ) || IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
1766 FETCH( &r[0], 0, CHAN_X );
1767 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
1768 micro_max( &r[0], &r[0], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
1769 STORE( &r[0], 0, CHAN_Y );
1770 }
1771
1772 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
1773 FETCH( &r[1], 0, CHAN_Y );
1774 micro_max( &r[1], &r[1], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
1775
1776 FETCH( &r[2], 0, CHAN_W );
1777 micro_min( &r[2], &r[2], &mach->Temps[TEMP_128_I].xyzw[TEMP_128_C] );
1778 micro_max( &r[2], &r[2], &mach->Temps[TEMP_M128_I].xyzw[TEMP_M128_C] );
1779 micro_pow( &r[1], &r[1], &r[2] );
1780 micro_lt( &r[0], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], &r[0], &r[1], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
1781 STORE( &r[0], 0, CHAN_Z );
1782 }
1783 }
1784
1785 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
1786 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
1787 }
1788 break;
1789
1790 case TGSI_OPCODE_RCP:
1791 /* TGSI_OPCODE_RECIP */
1792 FETCH( &r[0], 0, CHAN_X );
1793 micro_div( &r[0], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &r[0] );
1794 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1795 STORE( &r[0], 0, chan_index );
1796 }
1797 break;
1798
1799 case TGSI_OPCODE_RSQ:
1800 /* TGSI_OPCODE_RECIPSQRT */
1801 FETCH( &r[0], 0, CHAN_X );
1802 micro_sqrt( &r[0], &r[0] );
1803 micro_div( &r[0], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &r[0] );
1804 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1805 STORE( &r[0], 0, chan_index );
1806 }
1807 break;
1808
1809 case TGSI_OPCODE_EXP:
1810 FETCH( &r[0], 0, CHAN_X );
1811 micro_flr( &r[1], &r[0] ); /* r1 = floor(r0) */
1812 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
1813 micro_exp2( &r[2], &r[1] ); /* r2 = 2 ^ r1 */
1814 STORE( &r[2], 0, CHAN_X ); /* store r2 */
1815 }
1816 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
1817 micro_sub( &r[2], &r[0], &r[1] ); /* r2 = r0 - r1 */
1818 STORE( &r[2], 0, CHAN_Y ); /* store r2 */
1819 }
1820 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
1821 micro_exp2( &r[2], &r[0] ); /* r2 = 2 ^ r0 */
1822 STORE( &r[2], 0, CHAN_Z ); /* store r2 */
1823 }
1824 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
1825 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
1826 }
1827 break;
1828
1829 case TGSI_OPCODE_LOG:
1830 FETCH( &r[0], 0, CHAN_X );
1831 micro_abs( &r[2], &r[0] ); /* r2 = abs(r0) */
1832 micro_lg2( &r[1], &r[2] ); /* r1 = lg2(r2) */
1833 micro_flr( &r[0], &r[1] ); /* r0 = floor(r1) */
1834 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
1835 STORE( &r[0], 0, CHAN_X );
1836 }
1837 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
1838 micro_exp2( &r[0], &r[0] ); /* r0 = 2 ^ r0 */
1839 micro_div( &r[0], &r[2], &r[0] ); /* r0 = r2 / r0 */
1840 STORE( &r[0], 0, CHAN_Y );
1841 }
1842 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
1843 STORE( &r[1], 0, CHAN_Z );
1844 }
1845 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
1846 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
1847 }
1848 break;
1849
1850 case TGSI_OPCODE_MUL:
1851 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index )
1852 {
1853 FETCH(&r[0], 0, chan_index);
1854 FETCH(&r[1], 1, chan_index);
1855
1856 micro_mul( &r[0], &r[0], &r[1] );
1857
1858 STORE(&r[0], 0, chan_index);
1859 }
1860 break;
1861
1862 case TGSI_OPCODE_ADD:
1863 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1864 FETCH( &r[0], 0, chan_index );
1865 FETCH( &r[1], 1, chan_index );
1866 micro_add( &r[0], &r[0], &r[1] );
1867 STORE( &r[0], 0, chan_index );
1868 }
1869 break;
1870
1871 case TGSI_OPCODE_DP3:
1872 /* TGSI_OPCODE_DOT3 */
1873 FETCH( &r[0], 0, CHAN_X );
1874 FETCH( &r[1], 1, CHAN_X );
1875 micro_mul( &r[0], &r[0], &r[1] );
1876
1877 FETCH( &r[1], 0, CHAN_Y );
1878 FETCH( &r[2], 1, CHAN_Y );
1879 micro_mul( &r[1], &r[1], &r[2] );
1880 micro_add( &r[0], &r[0], &r[1] );
1881
1882 FETCH( &r[1], 0, CHAN_Z );
1883 FETCH( &r[2], 1, CHAN_Z );
1884 micro_mul( &r[1], &r[1], &r[2] );
1885 micro_add( &r[0], &r[0], &r[1] );
1886
1887 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1888 STORE( &r[0], 0, chan_index );
1889 }
1890 break;
1891
1892 case TGSI_OPCODE_DP4:
1893 /* TGSI_OPCODE_DOT4 */
1894 FETCH(&r[0], 0, CHAN_X);
1895 FETCH(&r[1], 1, CHAN_X);
1896
1897 micro_mul( &r[0], &r[0], &r[1] );
1898
1899 FETCH(&r[1], 0, CHAN_Y);
1900 FETCH(&r[2], 1, CHAN_Y);
1901
1902 micro_mul( &r[1], &r[1], &r[2] );
1903 micro_add( &r[0], &r[0], &r[1] );
1904
1905 FETCH(&r[1], 0, CHAN_Z);
1906 FETCH(&r[2], 1, CHAN_Z);
1907
1908 micro_mul( &r[1], &r[1], &r[2] );
1909 micro_add( &r[0], &r[0], &r[1] );
1910
1911 FETCH(&r[1], 0, CHAN_W);
1912 FETCH(&r[2], 1, CHAN_W);
1913
1914 micro_mul( &r[1], &r[1], &r[2] );
1915 micro_add( &r[0], &r[0], &r[1] );
1916
1917 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1918 STORE( &r[0], 0, chan_index );
1919 }
1920 break;
1921
1922 case TGSI_OPCODE_DST:
1923 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
1924 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_X );
1925 }
1926
1927 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
1928 FETCH( &r[0], 0, CHAN_Y );
1929 FETCH( &r[1], 1, CHAN_Y);
1930 micro_mul( &r[0], &r[0], &r[1] );
1931 STORE( &r[0], 0, CHAN_Y );
1932 }
1933
1934 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
1935 FETCH( &r[0], 0, CHAN_Z );
1936 STORE( &r[0], 0, CHAN_Z );
1937 }
1938
1939 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
1940 FETCH( &r[0], 1, CHAN_W );
1941 STORE( &r[0], 0, CHAN_W );
1942 }
1943 break;
1944
1945 case TGSI_OPCODE_MIN:
1946 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1947 FETCH(&r[0], 0, chan_index);
1948 FETCH(&r[1], 1, chan_index);
1949
1950 /* XXX use micro_min()?? */
1951 micro_lt( &r[0], &r[0], &r[1], &r[0], &r[1] );
1952
1953 STORE(&r[0], 0, chan_index);
1954 }
1955 break;
1956
1957 case TGSI_OPCODE_MAX:
1958 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1959 FETCH(&r[0], 0, chan_index);
1960 FETCH(&r[1], 1, chan_index);
1961
1962 /* XXX use micro_max()?? */
1963 micro_lt( &r[0], &r[0], &r[1], &r[1], &r[0] );
1964
1965 STORE(&r[0], 0, chan_index );
1966 }
1967 break;
1968
1969 case TGSI_OPCODE_SLT:
1970 /* TGSI_OPCODE_SETLT */
1971 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1972 FETCH( &r[0], 0, chan_index );
1973 FETCH( &r[1], 1, chan_index );
1974 micro_lt( &r[0], &r[0], &r[1], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
1975 STORE( &r[0], 0, chan_index );
1976 }
1977 break;
1978
1979 case TGSI_OPCODE_SGE:
1980 /* TGSI_OPCODE_SETGE */
1981 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1982 FETCH( &r[0], 0, chan_index );
1983 FETCH( &r[1], 1, chan_index );
1984 micro_ge( &r[0], &r[0], &r[1], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
1985 STORE( &r[0], 0, chan_index );
1986 }
1987 break;
1988
1989 case TGSI_OPCODE_MAD:
1990 /* TGSI_OPCODE_MADD */
1991 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
1992 FETCH( &r[0], 0, chan_index );
1993 FETCH( &r[1], 1, chan_index );
1994 micro_mul( &r[0], &r[0], &r[1] );
1995 FETCH( &r[1], 2, chan_index );
1996 micro_add( &r[0], &r[0], &r[1] );
1997 STORE( &r[0], 0, chan_index );
1998 }
1999 break;
2000
2001 case TGSI_OPCODE_SUB:
2002 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2003 FETCH(&r[0], 0, chan_index);
2004 FETCH(&r[1], 1, chan_index);
2005
2006 micro_sub( &r[0], &r[0], &r[1] );
2007
2008 STORE(&r[0], 0, chan_index);
2009 }
2010 break;
2011
2012 case TGSI_OPCODE_LERP:
2013 /* TGSI_OPCODE_LRP */
2014 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2015 FETCH(&r[0], 0, chan_index);
2016 FETCH(&r[1], 1, chan_index);
2017 FETCH(&r[2], 2, chan_index);
2018
2019 micro_sub( &r[1], &r[1], &r[2] );
2020 micro_mul( &r[0], &r[0], &r[1] );
2021 micro_add( &r[0], &r[0], &r[2] );
2022
2023 STORE(&r[0], 0, chan_index);
2024 }
2025 break;
2026
2027 case TGSI_OPCODE_CND:
2028 assert (0);
2029 break;
2030
2031 case TGSI_OPCODE_CND0:
2032 assert (0);
2033 break;
2034
2035 case TGSI_OPCODE_DOT2ADD:
2036 /* TGSI_OPCODE_DP2A */
2037 FETCH( &r[0], 0, CHAN_X );
2038 FETCH( &r[1], 1, CHAN_X );
2039 micro_mul( &r[0], &r[0], &r[1] );
2040
2041 FETCH( &r[1], 0, CHAN_Y );
2042 FETCH( &r[2], 1, CHAN_Y );
2043 micro_mul( &r[1], &r[1], &r[2] );
2044 micro_add( &r[0], &r[0], &r[1] );
2045
2046 FETCH( &r[2], 2, CHAN_X );
2047 micro_add( &r[0], &r[0], &r[2] );
2048
2049 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2050 STORE( &r[0], 0, chan_index );
2051 }
2052 break;
2053
2054 case TGSI_OPCODE_INDEX:
2055 assert (0);
2056 break;
2057
2058 case TGSI_OPCODE_NEGATE:
2059 assert (0);
2060 break;
2061
2062 case TGSI_OPCODE_FRAC:
2063 /* TGSI_OPCODE_FRC */
2064 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2065 FETCH( &r[0], 0, chan_index );
2066 micro_frc( &r[0], &r[0] );
2067 STORE( &r[0], 0, chan_index );
2068 }
2069 break;
2070
2071 case TGSI_OPCODE_CLAMP:
2072 assert (0);
2073 break;
2074
2075 case TGSI_OPCODE_FLOOR:
2076 /* TGSI_OPCODE_FLR */
2077 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2078 FETCH( &r[0], 0, chan_index );
2079 micro_flr( &r[0], &r[0] );
2080 STORE( &r[0], 0, chan_index );
2081 }
2082 break;
2083
2084 case TGSI_OPCODE_ROUND:
2085 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2086 FETCH( &r[0], 0, chan_index );
2087 micro_rnd( &r[0], &r[0] );
2088 STORE( &r[0], 0, chan_index );
2089 }
2090 break;
2091
2092 case TGSI_OPCODE_EXPBASE2:
2093 /* TGSI_OPCODE_EX2 */
2094 FETCH(&r[0], 0, CHAN_X);
2095
2096 #if FAST_MATH
2097 micro_exp2( &r[0], &r[0] );
2098 #else
2099 micro_pow( &r[0], &mach->Temps[TEMP_2_I].xyzw[TEMP_2_C], &r[0] );
2100 #endif
2101
2102 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2103 STORE( &r[0], 0, chan_index );
2104 }
2105 break;
2106
2107 case TGSI_OPCODE_LOGBASE2:
2108 /* TGSI_OPCODE_LG2 */
2109 FETCH( &r[0], 0, CHAN_X );
2110 micro_lg2( &r[0], &r[0] );
2111 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2112 STORE( &r[0], 0, chan_index );
2113 }
2114 break;
2115
2116 case TGSI_OPCODE_POWER:
2117 /* TGSI_OPCODE_POW */
2118 FETCH(&r[0], 0, CHAN_X);
2119 FETCH(&r[1], 1, CHAN_X);
2120
2121 micro_pow( &r[0], &r[0], &r[1] );
2122
2123 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2124 STORE( &r[0], 0, chan_index );
2125 }
2126 break;
2127
2128 case TGSI_OPCODE_CROSSPRODUCT:
2129 /* TGSI_OPCODE_XPD */
2130 FETCH(&r[0], 0, CHAN_Y);
2131 FETCH(&r[1], 1, CHAN_Z);
2132
2133 micro_mul( &r[2], &r[0], &r[1] );
2134
2135 FETCH(&r[3], 0, CHAN_Z);
2136 FETCH(&r[4], 1, CHAN_Y);
2137
2138 micro_mul( &r[5], &r[3], &r[4] );
2139 micro_sub( &r[2], &r[2], &r[5] );
2140
2141 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
2142 STORE( &r[2], 0, CHAN_X );
2143 }
2144
2145 FETCH(&r[2], 1, CHAN_X);
2146
2147 micro_mul( &r[3], &r[3], &r[2] );
2148
2149 FETCH(&r[5], 0, CHAN_X);
2150
2151 micro_mul( &r[1], &r[1], &r[5] );
2152 micro_sub( &r[3], &r[3], &r[1] );
2153
2154 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
2155 STORE( &r[3], 0, CHAN_Y );
2156 }
2157
2158 micro_mul( &r[5], &r[5], &r[4] );
2159 micro_mul( &r[0], &r[0], &r[2] );
2160 micro_sub( &r[5], &r[5], &r[0] );
2161
2162 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
2163 STORE( &r[5], 0, CHAN_Z );
2164 }
2165
2166 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
2167 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
2168 }
2169 break;
2170
2171 case TGSI_OPCODE_MULTIPLYMATRIX:
2172 assert (0);
2173 break;
2174
2175 case TGSI_OPCODE_ABS:
2176 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2177 FETCH(&r[0], 0, chan_index);
2178
2179 micro_abs( &r[0], &r[0] );
2180
2181 STORE(&r[0], 0, chan_index);
2182 }
2183 break;
2184
2185 case TGSI_OPCODE_RCC:
2186 assert (0);
2187 break;
2188
2189 case TGSI_OPCODE_DPH:
2190 FETCH(&r[0], 0, CHAN_X);
2191 FETCH(&r[1], 1, CHAN_X);
2192
2193 micro_mul( &r[0], &r[0], &r[1] );
2194
2195 FETCH(&r[1], 0, CHAN_Y);
2196 FETCH(&r[2], 1, CHAN_Y);
2197
2198 micro_mul( &r[1], &r[1], &r[2] );
2199 micro_add( &r[0], &r[0], &r[1] );
2200
2201 FETCH(&r[1], 0, CHAN_Z);
2202 FETCH(&r[2], 1, CHAN_Z);
2203
2204 micro_mul( &r[1], &r[1], &r[2] );
2205 micro_add( &r[0], &r[0], &r[1] );
2206
2207 FETCH(&r[1], 1, CHAN_W);
2208
2209 micro_add( &r[0], &r[0], &r[1] );
2210
2211 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2212 STORE( &r[0], 0, chan_index );
2213 }
2214 break;
2215
2216 case TGSI_OPCODE_COS:
2217 FETCH(&r[0], 0, CHAN_X);
2218
2219 micro_cos( &r[0], &r[0] );
2220
2221 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2222 STORE( &r[0], 0, chan_index );
2223 }
2224 break;
2225
2226 case TGSI_OPCODE_DDX:
2227 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2228 FETCH( &r[0], 0, chan_index );
2229 micro_ddx( &r[0], &r[0] );
2230 STORE( &r[0], 0, chan_index );
2231 }
2232 break;
2233
2234 case TGSI_OPCODE_DDY:
2235 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2236 FETCH( &r[0], 0, chan_index );
2237 micro_ddy( &r[0], &r[0] );
2238 STORE( &r[0], 0, chan_index );
2239 }
2240 break;
2241
2242 case TGSI_OPCODE_KILP:
2243 exec_kilp (mach, inst);
2244 break;
2245
2246 case TGSI_OPCODE_KIL:
2247 exec_kil (mach, inst);
2248 break;
2249
2250 case TGSI_OPCODE_PK2H:
2251 assert (0);
2252 break;
2253
2254 case TGSI_OPCODE_PK2US:
2255 assert (0);
2256 break;
2257
2258 case TGSI_OPCODE_PK4B:
2259 assert (0);
2260 break;
2261
2262 case TGSI_OPCODE_PK4UB:
2263 assert (0);
2264 break;
2265
2266 case TGSI_OPCODE_RFL:
2267 assert (0);
2268 break;
2269
2270 case TGSI_OPCODE_SEQ:
2271 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2272 FETCH( &r[0], 0, chan_index );
2273 FETCH( &r[1], 1, chan_index );
2274 micro_eq( &r[0], &r[0], &r[1],
2275 &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C],
2276 &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
2277 STORE( &r[0], 0, chan_index );
2278 }
2279 break;
2280
2281 case TGSI_OPCODE_SFL:
2282 assert (0);
2283 break;
2284
2285 case TGSI_OPCODE_SGT:
2286 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2287 FETCH( &r[0], 0, chan_index );
2288 FETCH( &r[1], 1, chan_index );
2289 micro_le( &r[0], &r[0], &r[1], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C] );
2290 STORE( &r[0], 0, chan_index );
2291 }
2292 break;
2293
2294 case TGSI_OPCODE_SIN:
2295 FETCH( &r[0], 0, CHAN_X );
2296 micro_sin( &r[0], &r[0] );
2297 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2298 STORE( &r[0], 0, chan_index );
2299 }
2300 break;
2301
2302 case TGSI_OPCODE_SLE:
2303 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2304 FETCH( &r[0], 0, chan_index );
2305 FETCH( &r[1], 1, chan_index );
2306 micro_le( &r[0], &r[0], &r[1], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
2307 STORE( &r[0], 0, chan_index );
2308 }
2309 break;
2310
2311 case TGSI_OPCODE_SNE:
2312 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2313 FETCH( &r[0], 0, chan_index );
2314 FETCH( &r[1], 1, chan_index );
2315 micro_eq( &r[0], &r[0], &r[1], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C] );
2316 STORE( &r[0], 0, chan_index );
2317 }
2318 break;
2319
2320 case TGSI_OPCODE_STR:
2321 assert (0);
2322 break;
2323
2324 case TGSI_OPCODE_TEX:
2325 /* simple texture lookup */
2326 /* src[0] = texcoord */
2327 /* src[1] = sampler unit */
2328 exec_tex(mach, inst, FALSE, FALSE);
2329 break;
2330
2331 case TGSI_OPCODE_TXB:
2332 /* Texture lookup with lod bias */
2333 /* src[0] = texcoord (src[0].w = LOD bias) */
2334 /* src[1] = sampler unit */
2335 exec_tex(mach, inst, TRUE, FALSE);
2336 break;
2337
2338 case TGSI_OPCODE_TXD:
2339 /* Texture lookup with explict partial derivatives */
2340 /* src[0] = texcoord */
2341 /* src[1] = d[strq]/dx */
2342 /* src[2] = d[strq]/dy */
2343 /* src[3] = sampler unit */
2344 assert (0);
2345 break;
2346
2347 case TGSI_OPCODE_TXL:
2348 /* Texture lookup with explit LOD */
2349 /* src[0] = texcoord (src[0].w = LOD) */
2350 /* src[1] = sampler unit */
2351 exec_tex(mach, inst, TRUE, FALSE);
2352 break;
2353
2354 case TGSI_OPCODE_TXP:
2355 /* Texture lookup with projection */
2356 /* src[0] = texcoord (src[0].w = projection) */
2357 /* src[1] = sampler unit */
2358 exec_tex(mach, inst, FALSE, TRUE);
2359 break;
2360
2361 case TGSI_OPCODE_UP2H:
2362 assert (0);
2363 break;
2364
2365 case TGSI_OPCODE_UP2US:
2366 assert (0);
2367 break;
2368
2369 case TGSI_OPCODE_UP4B:
2370 assert (0);
2371 break;
2372
2373 case TGSI_OPCODE_UP4UB:
2374 assert (0);
2375 break;
2376
2377 case TGSI_OPCODE_X2D:
2378 assert (0);
2379 break;
2380
2381 case TGSI_OPCODE_ARA:
2382 assert (0);
2383 break;
2384
2385 case TGSI_OPCODE_ARR:
2386 assert (0);
2387 break;
2388
2389 case TGSI_OPCODE_BRA:
2390 assert (0);
2391 break;
2392
2393 case TGSI_OPCODE_CAL:
2394 /* skip the call if no execution channels are enabled */
2395 if (mach->ExecMask) {
2396 /* do the call */
2397
2398 /* push the Cond, Loop, Cont stacks */
2399 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
2400 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
2401 assert(mach->LoopStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
2402 mach->LoopStack[mach->LoopStackTop++] = mach->LoopMask;
2403 assert(mach->ContStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
2404 mach->ContStack[mach->ContStackTop++] = mach->ContMask;
2405
2406 assert(mach->FuncStackTop < TGSI_EXEC_MAX_CALL_NESTING);
2407 mach->FuncStack[mach->FuncStackTop++] = mach->FuncMask;
2408
2409 /* note that PC was already incremented above */
2410 mach->CallStack[mach->CallStackTop++] = *pc;
2411 *pc = inst->InstructionExtLabel.Label;
2412 }
2413 break;
2414
2415 case TGSI_OPCODE_RET:
2416 mach->FuncMask &= ~mach->ExecMask;
2417 UPDATE_EXEC_MASK(mach);
2418
2419 if (mach->FuncMask == 0x0) {
2420 /* really return now (otherwise, keep executing */
2421
2422 if (mach->CallStackTop == 0) {
2423 /* returning from main() */
2424 *pc = -1;
2425 return;
2426 }
2427 *pc = mach->CallStack[--mach->CallStackTop];
2428
2429 /* pop the Cond, Loop, Cont stacks */
2430 assert(mach->CondStackTop > 0);
2431 mach->CondMask = mach->CondStack[--mach->CondStackTop];
2432 assert(mach->LoopStackTop > 0);
2433 mach->LoopMask = mach->LoopStack[--mach->LoopStackTop];
2434 assert(mach->ContStackTop > 0);
2435 mach->ContMask = mach->ContStack[--mach->ContStackTop];
2436 assert(mach->FuncStackTop > 0);
2437 mach->FuncMask = mach->FuncStack[--mach->FuncStackTop];
2438
2439 UPDATE_EXEC_MASK(mach);
2440 }
2441 break;
2442
2443 case TGSI_OPCODE_SSG:
2444 assert (0);
2445 break;
2446
2447 case TGSI_OPCODE_CMP:
2448 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2449 FETCH(&r[0], 0, chan_index);
2450 FETCH(&r[1], 1, chan_index);
2451 FETCH(&r[2], 2, chan_index);
2452
2453 micro_lt( &r[0], &r[0], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], &r[1], &r[2] );
2454
2455 STORE(&r[0], 0, chan_index);
2456 }
2457 break;
2458
2459 case TGSI_OPCODE_SCS:
2460 if( IS_CHANNEL_ENABLED( *inst, CHAN_X ) || IS_CHANNEL_ENABLED( *inst, CHAN_Y ) ) {
2461 FETCH( &r[0], 0, CHAN_X );
2462 }
2463 if( IS_CHANNEL_ENABLED( *inst, CHAN_X ) ) {
2464 micro_cos( &r[1], &r[0] );
2465 STORE( &r[1], 0, CHAN_X );
2466 }
2467 if( IS_CHANNEL_ENABLED( *inst, CHAN_Y ) ) {
2468 micro_sin( &r[1], &r[0] );
2469 STORE( &r[1], 0, CHAN_Y );
2470 }
2471 if( IS_CHANNEL_ENABLED( *inst, CHAN_Z ) ) {
2472 STORE( &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], 0, CHAN_Z );
2473 }
2474 if( IS_CHANNEL_ENABLED( *inst, CHAN_W ) ) {
2475 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
2476 }
2477 break;
2478
2479 case TGSI_OPCODE_NRM:
2480 /* 3-component vector normalize */
2481 {
2482 union tgsi_exec_channel tmp, dot;
2483
2484 /* tmp = dp3(src0, src0): */
2485 FETCH( &r[0], 0, CHAN_X );
2486 micro_mul( &tmp, &r[0], &r[0] );
2487
2488 FETCH( &r[1], 0, CHAN_Y );
2489 micro_mul( &dot, &r[1], &r[1] );
2490 micro_add( &tmp, &tmp, &dot );
2491
2492 FETCH( &r[2], 0, CHAN_Z );
2493 micro_mul( &dot, &r[2], &r[2] );
2494 micro_add( &tmp, &tmp, &dot );
2495
2496 /* tmp = 1 / sqrt(tmp) */
2497 micro_sqrt( &tmp, &tmp );
2498 micro_div( &tmp, &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &tmp );
2499
2500 /* note: w channel is undefined */
2501 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2502 /* chan = chan * tmp */
2503 micro_mul( &r[chan_index], &tmp, &r[chan_index] );
2504 STORE( &r[chan_index], 0, chan_index );
2505 }
2506 }
2507 break;
2508
2509 case TGSI_OPCODE_NRM4:
2510 /* 4-component vector normalize */
2511 {
2512 union tgsi_exec_channel tmp, dot;
2513
2514 /* tmp = dp4(src0, src0): */
2515 FETCH( &r[0], 0, CHAN_X );
2516 micro_mul( &tmp, &r[0], &r[0] );
2517
2518 FETCH( &r[1], 0, CHAN_Y );
2519 micro_mul( &dot, &r[1], &r[1] );
2520 micro_add( &tmp, &tmp, &dot );
2521
2522 FETCH( &r[2], 0, CHAN_Z );
2523 micro_mul( &dot, &r[2], &r[2] );
2524 micro_add( &tmp, &tmp, &dot );
2525
2526 FETCH( &r[3], 0, CHAN_W );
2527 micro_mul( &dot, &r[3], &r[3] );
2528 micro_add( &tmp, &tmp, &dot );
2529
2530 /* tmp = 1 / sqrt(tmp) */
2531 micro_sqrt( &tmp, &tmp );
2532 micro_div( &tmp, &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &tmp );
2533
2534 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2535 /* chan = chan * tmp */
2536 micro_mul( &r[chan_index], &tmp, &r[chan_index] );
2537 STORE( &r[chan_index], 0, chan_index );
2538 }
2539 }
2540 break;
2541
2542 case TGSI_OPCODE_DIV:
2543 assert( 0 );
2544 break;
2545
2546 case TGSI_OPCODE_DP2:
2547 FETCH( &r[0], 0, CHAN_X );
2548 FETCH( &r[1], 1, CHAN_X );
2549 micro_mul( &r[0], &r[0], &r[1] );
2550
2551 FETCH( &r[1], 0, CHAN_Y );
2552 FETCH( &r[2], 1, CHAN_Y );
2553 micro_mul( &r[1], &r[1], &r[2] );
2554 micro_add( &r[0], &r[0], &r[1] );
2555
2556 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2557 STORE( &r[0], 0, chan_index );
2558 }
2559 break;
2560
2561 case TGSI_OPCODE_IF:
2562 /* push CondMask */
2563 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
2564 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
2565 FETCH( &r[0], 0, CHAN_X );
2566 /* update CondMask */
2567 if( ! r[0].u[0] ) {
2568 mach->CondMask &= ~0x1;
2569 }
2570 if( ! r[0].u[1] ) {
2571 mach->CondMask &= ~0x2;
2572 }
2573 if( ! r[0].u[2] ) {
2574 mach->CondMask &= ~0x4;
2575 }
2576 if( ! r[0].u[3] ) {
2577 mach->CondMask &= ~0x8;
2578 }
2579 UPDATE_EXEC_MASK(mach);
2580 /* Todo: If CondMask==0, jump to ELSE */
2581 break;
2582
2583 case TGSI_OPCODE_ELSE:
2584 /* invert CondMask wrt previous mask */
2585 {
2586 uint prevMask;
2587 assert(mach->CondStackTop > 0);
2588 prevMask = mach->CondStack[mach->CondStackTop - 1];
2589 mach->CondMask = ~mach->CondMask & prevMask;
2590 UPDATE_EXEC_MASK(mach);
2591 /* Todo: If CondMask==0, jump to ENDIF */
2592 }
2593 break;
2594
2595 case TGSI_OPCODE_ENDIF:
2596 /* pop CondMask */
2597 assert(mach->CondStackTop > 0);
2598 mach->CondMask = mach->CondStack[--mach->CondStackTop];
2599 UPDATE_EXEC_MASK(mach);
2600 break;
2601
2602 case TGSI_OPCODE_END:
2603 /* halt execution */
2604 *pc = -1;
2605 break;
2606
2607 case TGSI_OPCODE_REP:
2608 assert (0);
2609 break;
2610
2611 case TGSI_OPCODE_ENDREP:
2612 assert (0);
2613 break;
2614
2615 case TGSI_OPCODE_PUSHA:
2616 assert (0);
2617 break;
2618
2619 case TGSI_OPCODE_POPA:
2620 assert (0);
2621 break;
2622
2623 case TGSI_OPCODE_CEIL:
2624 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2625 FETCH( &r[0], 0, chan_index );
2626 micro_ceil( &r[0], &r[0] );
2627 STORE( &r[0], 0, chan_index );
2628 }
2629 break;
2630
2631 case TGSI_OPCODE_I2F:
2632 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2633 FETCH( &r[0], 0, chan_index );
2634 micro_i2f( &r[0], &r[0] );
2635 STORE( &r[0], 0, chan_index );
2636 }
2637 break;
2638
2639 case TGSI_OPCODE_NOT:
2640 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2641 FETCH( &r[0], 0, chan_index );
2642 micro_not( &r[0], &r[0] );
2643 STORE( &r[0], 0, chan_index );
2644 }
2645 break;
2646
2647 case TGSI_OPCODE_TRUNC:
2648 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2649 FETCH( &r[0], 0, chan_index );
2650 micro_trunc( &r[0], &r[0] );
2651 STORE( &r[0], 0, chan_index );
2652 }
2653 break;
2654
2655 case TGSI_OPCODE_SHL:
2656 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2657 FETCH( &r[0], 0, chan_index );
2658 FETCH( &r[1], 1, chan_index );
2659 micro_shl( &r[0], &r[0], &r[1] );
2660 STORE( &r[0], 0, chan_index );
2661 }
2662 break;
2663
2664 case TGSI_OPCODE_SHR:
2665 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2666 FETCH( &r[0], 0, chan_index );
2667 FETCH( &r[1], 1, chan_index );
2668 micro_ishr( &r[0], &r[0], &r[1] );
2669 STORE( &r[0], 0, chan_index );
2670 }
2671 break;
2672
2673 case TGSI_OPCODE_AND:
2674 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2675 FETCH( &r[0], 0, chan_index );
2676 FETCH( &r[1], 1, chan_index );
2677 micro_and( &r[0], &r[0], &r[1] );
2678 STORE( &r[0], 0, chan_index );
2679 }
2680 break;
2681
2682 case TGSI_OPCODE_OR:
2683 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2684 FETCH( &r[0], 0, chan_index );
2685 FETCH( &r[1], 1, chan_index );
2686 micro_or( &r[0], &r[0], &r[1] );
2687 STORE( &r[0], 0, chan_index );
2688 }
2689 break;
2690
2691 case TGSI_OPCODE_MOD:
2692 assert (0);
2693 break;
2694
2695 case TGSI_OPCODE_XOR:
2696 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2697 FETCH( &r[0], 0, chan_index );
2698 FETCH( &r[1], 1, chan_index );
2699 micro_xor( &r[0], &r[0], &r[1] );
2700 STORE( &r[0], 0, chan_index );
2701 }
2702 break;
2703
2704 case TGSI_OPCODE_SAD:
2705 assert (0);
2706 break;
2707
2708 case TGSI_OPCODE_TXF:
2709 assert (0);
2710 break;
2711
2712 case TGSI_OPCODE_TXQ:
2713 assert (0);
2714 break;
2715
2716 case TGSI_OPCODE_EMIT:
2717 mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0] += 16;
2718 mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]]++;
2719 break;
2720
2721 case TGSI_OPCODE_ENDPRIM:
2722 mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]++;
2723 mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]] = 0;
2724 break;
2725
2726 case TGSI_OPCODE_LOOP:
2727 /* fall-through (for now) */
2728 case TGSI_OPCODE_BGNLOOP2:
2729 /* push LoopMask and ContMasks */
2730 assert(mach->LoopStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
2731 mach->LoopStack[mach->LoopStackTop++] = mach->LoopMask;
2732 assert(mach->ContStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
2733 mach->ContStack[mach->ContStackTop++] = mach->ContMask;
2734 break;
2735
2736 case TGSI_OPCODE_ENDLOOP:
2737 /* fall-through (for now at least) */
2738 case TGSI_OPCODE_ENDLOOP2:
2739 /* Restore ContMask, but don't pop */
2740 assert(mach->ContStackTop > 0);
2741 mach->ContMask = mach->ContStack[mach->ContStackTop - 1];
2742 UPDATE_EXEC_MASK(mach);
2743 if (mach->ExecMask) {
2744 /* repeat loop: jump to instruction just past BGNLOOP */
2745 *pc = inst->InstructionExtLabel.Label + 1;
2746 }
2747 else {
2748 /* exit loop: pop LoopMask */
2749 assert(mach->LoopStackTop > 0);
2750 mach->LoopMask = mach->LoopStack[--mach->LoopStackTop];
2751 /* pop ContMask */
2752 assert(mach->ContStackTop > 0);
2753 mach->ContMask = mach->ContStack[--mach->ContStackTop];
2754 }
2755 UPDATE_EXEC_MASK(mach);
2756 break;
2757
2758 case TGSI_OPCODE_BRK:
2759 /* turn off loop channels for each enabled exec channel */
2760 mach->LoopMask &= ~mach->ExecMask;
2761 /* Todo: if mach->LoopMask == 0, jump to end of loop */
2762 UPDATE_EXEC_MASK(mach);
2763 break;
2764
2765 case TGSI_OPCODE_CONT:
2766 /* turn off cont channels for each enabled exec channel */
2767 mach->ContMask &= ~mach->ExecMask;
2768 /* Todo: if mach->LoopMask == 0, jump to end of loop */
2769 UPDATE_EXEC_MASK(mach);
2770 break;
2771
2772 case TGSI_OPCODE_BGNSUB:
2773 /* no-op */
2774 break;
2775
2776 case TGSI_OPCODE_ENDSUB:
2777 /* no-op */
2778 break;
2779
2780 case TGSI_OPCODE_NOISE1:
2781 assert( 0 );
2782 break;
2783
2784 case TGSI_OPCODE_NOISE2:
2785 assert( 0 );
2786 break;
2787
2788 case TGSI_OPCODE_NOISE3:
2789 assert( 0 );
2790 break;
2791
2792 case TGSI_OPCODE_NOISE4:
2793 assert( 0 );
2794 break;
2795
2796 case TGSI_OPCODE_NOP:
2797 break;
2798
2799 default:
2800 assert( 0 );
2801 }
2802 }
2803
2804
2805 /**
2806 * Run TGSI interpreter.
2807 * \return bitmask of "alive" quad components
2808 */
2809 uint
2810 tgsi_exec_machine_run( struct tgsi_exec_machine *mach )
2811 {
2812 uint i;
2813 int pc = 0;
2814
2815 mach->CondMask = 0xf;
2816 mach->LoopMask = 0xf;
2817 mach->ContMask = 0xf;
2818 mach->FuncMask = 0xf;
2819 mach->ExecMask = 0xf;
2820
2821 mach->CondStackTop = 0; /* temporarily subvert this assertion */
2822 assert(mach->CondStackTop == 0);
2823 assert(mach->LoopStackTop == 0);
2824 assert(mach->ContStackTop == 0);
2825 assert(mach->CallStackTop == 0);
2826
2827 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] = 0;
2828 mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0] = 0;
2829
2830 if( mach->Processor == TGSI_PROCESSOR_GEOMETRY ) {
2831 mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0] = 0;
2832 mach->Primitives[0] = 0;
2833 }
2834
2835 for (i = 0; i < QUAD_SIZE; i++) {
2836 mach->Temps[TEMP_CC_I].xyzw[TEMP_CC_C].u[i] =
2837 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_X_SHIFT) |
2838 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_Y_SHIFT) |
2839 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_Z_SHIFT) |
2840 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_W_SHIFT);
2841 }
2842
2843 /* execute declarations (interpolants) */
2844 for (i = 0; i < mach->NumDeclarations; i++) {
2845 exec_declaration( mach, mach->Declarations+i );
2846 }
2847
2848 /* execute instructions, until pc is set to -1 */
2849 while (pc != -1) {
2850 assert(pc < (int) mach->NumInstructions);
2851 exec_instruction( mach, mach->Instructions + pc, &pc );
2852 }
2853
2854 #if 0
2855 /* we scale from floats in [0,1] to Zbuffer ints in sp_quad_depth_test.c */
2856 if (mach->Processor == TGSI_PROCESSOR_FRAGMENT) {
2857 /*
2858 * Scale back depth component.
2859 */
2860 for (i = 0; i < 4; i++)
2861 mach->Outputs[0].xyzw[2].f[i] *= ctx->DrawBuffer->_DepthMaxF;
2862 }
2863 #endif
2864
2865 return ~mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
2866 }
2867
2868