gs: make sure we end primitives when finishing executing shaders
[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 * Copyright 2009-2010 VMware, Inc. All rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * 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, sub license, 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 portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * TGSI interpreter/executor.
31 *
32 * Flow control information:
33 *
34 * Since we operate on 'quads' (4 pixels or 4 vertices in parallel)
35 * flow control statements (IF/ELSE/ENDIF, LOOP/ENDLOOP) require special
36 * care since a condition may be true for some quad components but false
37 * for other components.
38 *
39 * We basically execute all statements (even if they're in the part of
40 * an IF/ELSE clause that's "not taken") and use a special mask to
41 * control writing to destination registers. This is the ExecMask.
42 * See store_dest().
43 *
44 * The ExecMask is computed from three other masks (CondMask, LoopMask and
45 * ContMask) which are controlled by the flow control instructions (namely:
46 * (IF/ELSE/ENDIF, LOOP/ENDLOOP and CONT).
47 *
48 *
49 * Authors:
50 * Michal Krol
51 * Brian Paul
52 */
53
54 #include "pipe/p_compiler.h"
55 #include "pipe/p_state.h"
56 #include "pipe/p_shader_tokens.h"
57 #include "tgsi/tgsi_dump.h"
58 #include "tgsi/tgsi_parse.h"
59 #include "tgsi/tgsi_util.h"
60 #include "tgsi_exec.h"
61 #include "util/u_memory.h"
62 #include "util/u_math.h"
63
64
65 #define FAST_MATH 1
66
67 #define TILE_TOP_LEFT 0
68 #define TILE_TOP_RIGHT 1
69 #define TILE_BOTTOM_LEFT 2
70 #define TILE_BOTTOM_RIGHT 3
71
72 static void
73 micro_abs(union tgsi_exec_channel *dst,
74 const union tgsi_exec_channel *src)
75 {
76 dst->f[0] = fabsf(src->f[0]);
77 dst->f[1] = fabsf(src->f[1]);
78 dst->f[2] = fabsf(src->f[2]);
79 dst->f[3] = fabsf(src->f[3]);
80 }
81
82 static void
83 micro_arl(union tgsi_exec_channel *dst,
84 const union tgsi_exec_channel *src)
85 {
86 dst->i[0] = (int)floorf(src->f[0]);
87 dst->i[1] = (int)floorf(src->f[1]);
88 dst->i[2] = (int)floorf(src->f[2]);
89 dst->i[3] = (int)floorf(src->f[3]);
90 }
91
92 static void
93 micro_arr(union tgsi_exec_channel *dst,
94 const union tgsi_exec_channel *src)
95 {
96 dst->i[0] = (int)floorf(src->f[0] + 0.5f);
97 dst->i[1] = (int)floorf(src->f[1] + 0.5f);
98 dst->i[2] = (int)floorf(src->f[2] + 0.5f);
99 dst->i[3] = (int)floorf(src->f[3] + 0.5f);
100 }
101
102 static void
103 micro_ceil(union tgsi_exec_channel *dst,
104 const union tgsi_exec_channel *src)
105 {
106 dst->f[0] = ceilf(src->f[0]);
107 dst->f[1] = ceilf(src->f[1]);
108 dst->f[2] = ceilf(src->f[2]);
109 dst->f[3] = ceilf(src->f[3]);
110 }
111
112 static void
113 micro_clamp(union tgsi_exec_channel *dst,
114 const union tgsi_exec_channel *src0,
115 const union tgsi_exec_channel *src1,
116 const union tgsi_exec_channel *src2)
117 {
118 dst->f[0] = src0->f[0] < src1->f[0] ? src1->f[0] : src0->f[0] > src2->f[0] ? src2->f[0] : src0->f[0];
119 dst->f[1] = src0->f[1] < src1->f[1] ? src1->f[1] : src0->f[1] > src2->f[1] ? src2->f[1] : src0->f[1];
120 dst->f[2] = src0->f[2] < src1->f[2] ? src1->f[2] : src0->f[2] > src2->f[2] ? src2->f[2] : src0->f[2];
121 dst->f[3] = src0->f[3] < src1->f[3] ? src1->f[3] : src0->f[3] > src2->f[3] ? src2->f[3] : src0->f[3];
122 }
123
124 static void
125 micro_cmp(union tgsi_exec_channel *dst,
126 const union tgsi_exec_channel *src0,
127 const union tgsi_exec_channel *src1,
128 const union tgsi_exec_channel *src2)
129 {
130 dst->f[0] = src0->f[0] < 0.0f ? src1->f[0] : src2->f[0];
131 dst->f[1] = src0->f[1] < 0.0f ? src1->f[1] : src2->f[1];
132 dst->f[2] = src0->f[2] < 0.0f ? src1->f[2] : src2->f[2];
133 dst->f[3] = src0->f[3] < 0.0f ? src1->f[3] : src2->f[3];
134 }
135
136 static void
137 micro_cnd(union tgsi_exec_channel *dst,
138 const union tgsi_exec_channel *src0,
139 const union tgsi_exec_channel *src1,
140 const union tgsi_exec_channel *src2)
141 {
142 dst->f[0] = src2->f[0] > 0.5f ? src0->f[0] : src1->f[0];
143 dst->f[1] = src2->f[1] > 0.5f ? src0->f[1] : src1->f[1];
144 dst->f[2] = src2->f[2] > 0.5f ? src0->f[2] : src1->f[2];
145 dst->f[3] = src2->f[3] > 0.5f ? src0->f[3] : src1->f[3];
146 }
147
148 static void
149 micro_cos(union tgsi_exec_channel *dst,
150 const union tgsi_exec_channel *src)
151 {
152 dst->f[0] = cosf(src->f[0]);
153 dst->f[1] = cosf(src->f[1]);
154 dst->f[2] = cosf(src->f[2]);
155 dst->f[3] = cosf(src->f[3]);
156 }
157
158 static void
159 micro_ddx(union tgsi_exec_channel *dst,
160 const union tgsi_exec_channel *src)
161 {
162 dst->f[0] =
163 dst->f[1] =
164 dst->f[2] =
165 dst->f[3] = src->f[TILE_BOTTOM_RIGHT] - src->f[TILE_BOTTOM_LEFT];
166 }
167
168 static void
169 micro_ddy(union tgsi_exec_channel *dst,
170 const union tgsi_exec_channel *src)
171 {
172 dst->f[0] =
173 dst->f[1] =
174 dst->f[2] =
175 dst->f[3] = src->f[TILE_BOTTOM_LEFT] - src->f[TILE_TOP_LEFT];
176 }
177
178 static void
179 micro_exp2(union tgsi_exec_channel *dst,
180 const union tgsi_exec_channel *src)
181 {
182 #if FAST_MATH
183 dst->f[0] = util_fast_exp2(src->f[0]);
184 dst->f[1] = util_fast_exp2(src->f[1]);
185 dst->f[2] = util_fast_exp2(src->f[2]);
186 dst->f[3] = util_fast_exp2(src->f[3]);
187 #else
188 #if DEBUG
189 /* Inf is okay for this instruction, so clamp it to silence assertions. */
190 uint i;
191 union tgsi_exec_channel clamped;
192
193 for (i = 0; i < 4; i++) {
194 if (src->f[i] > 127.99999f) {
195 clamped.f[i] = 127.99999f;
196 } else if (src->f[i] < -126.99999f) {
197 clamped.f[i] = -126.99999f;
198 } else {
199 clamped.f[i] = src->f[i];
200 }
201 }
202 src = &clamped;
203 #endif /* DEBUG */
204
205 dst->f[0] = powf(2.0f, src->f[0]);
206 dst->f[1] = powf(2.0f, src->f[1]);
207 dst->f[2] = powf(2.0f, src->f[2]);
208 dst->f[3] = powf(2.0f, src->f[3]);
209 #endif /* FAST_MATH */
210 }
211
212 static void
213 micro_flr(union tgsi_exec_channel *dst,
214 const union tgsi_exec_channel *src)
215 {
216 dst->f[0] = floorf(src->f[0]);
217 dst->f[1] = floorf(src->f[1]);
218 dst->f[2] = floorf(src->f[2]);
219 dst->f[3] = floorf(src->f[3]);
220 }
221
222 static void
223 micro_frc(union tgsi_exec_channel *dst,
224 const union tgsi_exec_channel *src)
225 {
226 dst->f[0] = src->f[0] - floorf(src->f[0]);
227 dst->f[1] = src->f[1] - floorf(src->f[1]);
228 dst->f[2] = src->f[2] - floorf(src->f[2]);
229 dst->f[3] = src->f[3] - floorf(src->f[3]);
230 }
231
232 static void
233 micro_iabs(union tgsi_exec_channel *dst,
234 const union tgsi_exec_channel *src)
235 {
236 dst->i[0] = src->i[0] >= 0 ? src->i[0] : -src->i[0];
237 dst->i[1] = src->i[1] >= 0 ? src->i[1] : -src->i[1];
238 dst->i[2] = src->i[2] >= 0 ? src->i[2] : -src->i[2];
239 dst->i[3] = src->i[3] >= 0 ? src->i[3] : -src->i[3];
240 }
241
242 static void
243 micro_ineg(union tgsi_exec_channel *dst,
244 const union tgsi_exec_channel *src)
245 {
246 dst->i[0] = -src->i[0];
247 dst->i[1] = -src->i[1];
248 dst->i[2] = -src->i[2];
249 dst->i[3] = -src->i[3];
250 }
251
252 static void
253 micro_lg2(union tgsi_exec_channel *dst,
254 const union tgsi_exec_channel *src)
255 {
256 #if FAST_MATH
257 dst->f[0] = util_fast_log2(src->f[0]);
258 dst->f[1] = util_fast_log2(src->f[1]);
259 dst->f[2] = util_fast_log2(src->f[2]);
260 dst->f[3] = util_fast_log2(src->f[3]);
261 #else
262 dst->f[0] = logf(src->f[0]) * 1.442695f;
263 dst->f[1] = logf(src->f[1]) * 1.442695f;
264 dst->f[2] = logf(src->f[2]) * 1.442695f;
265 dst->f[3] = logf(src->f[3]) * 1.442695f;
266 #endif
267 }
268
269 static void
270 micro_lrp(union tgsi_exec_channel *dst,
271 const union tgsi_exec_channel *src0,
272 const union tgsi_exec_channel *src1,
273 const union tgsi_exec_channel *src2)
274 {
275 dst->f[0] = src0->f[0] * (src1->f[0] - src2->f[0]) + src2->f[0];
276 dst->f[1] = src0->f[1] * (src1->f[1] - src2->f[1]) + src2->f[1];
277 dst->f[2] = src0->f[2] * (src1->f[2] - src2->f[2]) + src2->f[2];
278 dst->f[3] = src0->f[3] * (src1->f[3] - src2->f[3]) + src2->f[3];
279 }
280
281 static void
282 micro_mad(union tgsi_exec_channel *dst,
283 const union tgsi_exec_channel *src0,
284 const union tgsi_exec_channel *src1,
285 const union tgsi_exec_channel *src2)
286 {
287 dst->f[0] = src0->f[0] * src1->f[0] + src2->f[0];
288 dst->f[1] = src0->f[1] * src1->f[1] + src2->f[1];
289 dst->f[2] = src0->f[2] * src1->f[2] + src2->f[2];
290 dst->f[3] = src0->f[3] * src1->f[3] + src2->f[3];
291 }
292
293 static void
294 micro_mov(union tgsi_exec_channel *dst,
295 const union tgsi_exec_channel *src)
296 {
297 dst->u[0] = src->u[0];
298 dst->u[1] = src->u[1];
299 dst->u[2] = src->u[2];
300 dst->u[3] = src->u[3];
301 }
302
303 static void
304 micro_rcp(union tgsi_exec_channel *dst,
305 const union tgsi_exec_channel *src)
306 {
307 #if 0 /* for debugging */
308 assert(src->f[0] != 0.0f);
309 assert(src->f[1] != 0.0f);
310 assert(src->f[2] != 0.0f);
311 assert(src->f[3] != 0.0f);
312 #endif
313 dst->f[0] = 1.0f / src->f[0];
314 dst->f[1] = 1.0f / src->f[1];
315 dst->f[2] = 1.0f / src->f[2];
316 dst->f[3] = 1.0f / src->f[3];
317 }
318
319 static void
320 micro_rnd(union tgsi_exec_channel *dst,
321 const union tgsi_exec_channel *src)
322 {
323 dst->f[0] = floorf(src->f[0] + 0.5f);
324 dst->f[1] = floorf(src->f[1] + 0.5f);
325 dst->f[2] = floorf(src->f[2] + 0.5f);
326 dst->f[3] = floorf(src->f[3] + 0.5f);
327 }
328
329 static void
330 micro_rsq(union tgsi_exec_channel *dst,
331 const union tgsi_exec_channel *src)
332 {
333 #if 0 /* for debugging */
334 assert(src->f[0] != 0.0f);
335 assert(src->f[1] != 0.0f);
336 assert(src->f[2] != 0.0f);
337 assert(src->f[3] != 0.0f);
338 #endif
339 dst->f[0] = 1.0f / sqrtf(fabsf(src->f[0]));
340 dst->f[1] = 1.0f / sqrtf(fabsf(src->f[1]));
341 dst->f[2] = 1.0f / sqrtf(fabsf(src->f[2]));
342 dst->f[3] = 1.0f / sqrtf(fabsf(src->f[3]));
343 }
344
345 static void
346 micro_seq(union tgsi_exec_channel *dst,
347 const union tgsi_exec_channel *src0,
348 const union tgsi_exec_channel *src1)
349 {
350 dst->f[0] = src0->f[0] == src1->f[0] ? 1.0f : 0.0f;
351 dst->f[1] = src0->f[1] == src1->f[1] ? 1.0f : 0.0f;
352 dst->f[2] = src0->f[2] == src1->f[2] ? 1.0f : 0.0f;
353 dst->f[3] = src0->f[3] == src1->f[3] ? 1.0f : 0.0f;
354 }
355
356 static void
357 micro_sge(union tgsi_exec_channel *dst,
358 const union tgsi_exec_channel *src0,
359 const union tgsi_exec_channel *src1)
360 {
361 dst->f[0] = src0->f[0] >= src1->f[0] ? 1.0f : 0.0f;
362 dst->f[1] = src0->f[1] >= src1->f[1] ? 1.0f : 0.0f;
363 dst->f[2] = src0->f[2] >= src1->f[2] ? 1.0f : 0.0f;
364 dst->f[3] = src0->f[3] >= src1->f[3] ? 1.0f : 0.0f;
365 }
366
367 static void
368 micro_sgn(union tgsi_exec_channel *dst,
369 const union tgsi_exec_channel *src)
370 {
371 dst->f[0] = src->f[0] < 0.0f ? -1.0f : src->f[0] > 0.0f ? 1.0f : 0.0f;
372 dst->f[1] = src->f[1] < 0.0f ? -1.0f : src->f[1] > 0.0f ? 1.0f : 0.0f;
373 dst->f[2] = src->f[2] < 0.0f ? -1.0f : src->f[2] > 0.0f ? 1.0f : 0.0f;
374 dst->f[3] = src->f[3] < 0.0f ? -1.0f : src->f[3] > 0.0f ? 1.0f : 0.0f;
375 }
376
377 static void
378 micro_sgt(union tgsi_exec_channel *dst,
379 const union tgsi_exec_channel *src0,
380 const union tgsi_exec_channel *src1)
381 {
382 dst->f[0] = src0->f[0] > src1->f[0] ? 1.0f : 0.0f;
383 dst->f[1] = src0->f[1] > src1->f[1] ? 1.0f : 0.0f;
384 dst->f[2] = src0->f[2] > src1->f[2] ? 1.0f : 0.0f;
385 dst->f[3] = src0->f[3] > src1->f[3] ? 1.0f : 0.0f;
386 }
387
388 static void
389 micro_sin(union tgsi_exec_channel *dst,
390 const union tgsi_exec_channel *src)
391 {
392 dst->f[0] = sinf(src->f[0]);
393 dst->f[1] = sinf(src->f[1]);
394 dst->f[2] = sinf(src->f[2]);
395 dst->f[3] = sinf(src->f[3]);
396 }
397
398 static void
399 micro_sle(union tgsi_exec_channel *dst,
400 const union tgsi_exec_channel *src0,
401 const union tgsi_exec_channel *src1)
402 {
403 dst->f[0] = src0->f[0] <= src1->f[0] ? 1.0f : 0.0f;
404 dst->f[1] = src0->f[1] <= src1->f[1] ? 1.0f : 0.0f;
405 dst->f[2] = src0->f[2] <= src1->f[2] ? 1.0f : 0.0f;
406 dst->f[3] = src0->f[3] <= src1->f[3] ? 1.0f : 0.0f;
407 }
408
409 static void
410 micro_slt(union tgsi_exec_channel *dst,
411 const union tgsi_exec_channel *src0,
412 const union tgsi_exec_channel *src1)
413 {
414 dst->f[0] = src0->f[0] < src1->f[0] ? 1.0f : 0.0f;
415 dst->f[1] = src0->f[1] < src1->f[1] ? 1.0f : 0.0f;
416 dst->f[2] = src0->f[2] < src1->f[2] ? 1.0f : 0.0f;
417 dst->f[3] = src0->f[3] < src1->f[3] ? 1.0f : 0.0f;
418 }
419
420 static void
421 micro_sne(union tgsi_exec_channel *dst,
422 const union tgsi_exec_channel *src0,
423 const union tgsi_exec_channel *src1)
424 {
425 dst->f[0] = src0->f[0] != src1->f[0] ? 1.0f : 0.0f;
426 dst->f[1] = src0->f[1] != src1->f[1] ? 1.0f : 0.0f;
427 dst->f[2] = src0->f[2] != src1->f[2] ? 1.0f : 0.0f;
428 dst->f[3] = src0->f[3] != src1->f[3] ? 1.0f : 0.0f;
429 }
430
431 static void
432 micro_trunc(union tgsi_exec_channel *dst,
433 const union tgsi_exec_channel *src)
434 {
435 dst->f[0] = (float)(int)src->f[0];
436 dst->f[1] = (float)(int)src->f[1];
437 dst->f[2] = (float)(int)src->f[2];
438 dst->f[3] = (float)(int)src->f[3];
439 }
440
441
442 #define CHAN_X 0
443 #define CHAN_Y 1
444 #define CHAN_Z 2
445 #define CHAN_W 3
446
447 enum tgsi_exec_datatype {
448 TGSI_EXEC_DATA_FLOAT,
449 TGSI_EXEC_DATA_INT,
450 TGSI_EXEC_DATA_UINT
451 };
452
453 /*
454 * Shorthand locations of various utility registers (_I = Index, _C = Channel)
455 */
456 #define TEMP_0_I TGSI_EXEC_TEMP_00000000_I
457 #define TEMP_0_C TGSI_EXEC_TEMP_00000000_C
458 #define TEMP_7F_I TGSI_EXEC_TEMP_7FFFFFFF_I
459 #define TEMP_7F_C TGSI_EXEC_TEMP_7FFFFFFF_C
460 #define TEMP_80_I TGSI_EXEC_TEMP_80000000_I
461 #define TEMP_80_C TGSI_EXEC_TEMP_80000000_C
462 #define TEMP_FF_I TGSI_EXEC_TEMP_FFFFFFFF_I
463 #define TEMP_FF_C TGSI_EXEC_TEMP_FFFFFFFF_C
464 #define TEMP_1_I TGSI_EXEC_TEMP_ONE_I
465 #define TEMP_1_C TGSI_EXEC_TEMP_ONE_C
466 #define TEMP_2_I TGSI_EXEC_TEMP_TWO_I
467 #define TEMP_2_C TGSI_EXEC_TEMP_TWO_C
468 #define TEMP_128_I TGSI_EXEC_TEMP_128_I
469 #define TEMP_128_C TGSI_EXEC_TEMP_128_C
470 #define TEMP_M128_I TGSI_EXEC_TEMP_MINUS_128_I
471 #define TEMP_M128_C TGSI_EXEC_TEMP_MINUS_128_C
472 #define TEMP_KILMASK_I TGSI_EXEC_TEMP_KILMASK_I
473 #define TEMP_KILMASK_C TGSI_EXEC_TEMP_KILMASK_C
474 #define TEMP_OUTPUT_I TGSI_EXEC_TEMP_OUTPUT_I
475 #define TEMP_OUTPUT_C TGSI_EXEC_TEMP_OUTPUT_C
476 #define TEMP_PRIMITIVE_I TGSI_EXEC_TEMP_PRIMITIVE_I
477 #define TEMP_PRIMITIVE_C TGSI_EXEC_TEMP_PRIMITIVE_C
478 #define TEMP_CC_I TGSI_EXEC_TEMP_CC_I
479 #define TEMP_CC_C TGSI_EXEC_TEMP_CC_C
480 #define TEMP_3_I TGSI_EXEC_TEMP_THREE_I
481 #define TEMP_3_C TGSI_EXEC_TEMP_THREE_C
482 #define TEMP_HALF_I TGSI_EXEC_TEMP_HALF_I
483 #define TEMP_HALF_C TGSI_EXEC_TEMP_HALF_C
484 #define TEMP_R0 TGSI_EXEC_TEMP_R0
485 #define TEMP_P0 TGSI_EXEC_TEMP_P0
486
487 #define IS_CHANNEL_ENABLED(INST, CHAN)\
488 ((INST).Dst[0].Register.WriteMask & (1 << (CHAN)))
489
490 #define IS_CHANNEL_ENABLED2(INST, CHAN)\
491 ((INST).Dst[1].Register.WriteMask & (1 << (CHAN)))
492
493 #define FOR_EACH_ENABLED_CHANNEL(INST, CHAN)\
494 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)\
495 if (IS_CHANNEL_ENABLED( INST, CHAN ))
496
497 #define FOR_EACH_ENABLED_CHANNEL2(INST, CHAN)\
498 for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)\
499 if (IS_CHANNEL_ENABLED2( INST, CHAN ))
500
501
502 /** The execution mask depends on the conditional mask and the loop mask */
503 #define UPDATE_EXEC_MASK(MACH) \
504 MACH->ExecMask = MACH->CondMask & MACH->LoopMask & MACH->ContMask & MACH->Switch.mask & MACH->FuncMask
505
506
507 static const union tgsi_exec_channel ZeroVec =
508 { { 0.0, 0.0, 0.0, 0.0 } };
509
510 static const union tgsi_exec_channel OneVec = {
511 {1.0f, 1.0f, 1.0f, 1.0f}
512 };
513
514
515 /**
516 * Assert that none of the float values in 'chan' are infinite or NaN.
517 * NaN and Inf may occur normally during program execution and should
518 * not lead to crashes, etc. But when debugging, it's helpful to catch
519 * them.
520 */
521 static INLINE void
522 check_inf_or_nan(const union tgsi_exec_channel *chan)
523 {
524 assert(!util_is_inf_or_nan((chan)->f[0]));
525 assert(!util_is_inf_or_nan((chan)->f[1]));
526 assert(!util_is_inf_or_nan((chan)->f[2]));
527 assert(!util_is_inf_or_nan((chan)->f[3]));
528 }
529
530
531 #ifdef DEBUG
532 static void
533 print_chan(const char *msg, const union tgsi_exec_channel *chan)
534 {
535 debug_printf("%s = {%f, %f, %f, %f}\n",
536 msg, chan->f[0], chan->f[1], chan->f[2], chan->f[3]);
537 }
538 #endif
539
540
541 #ifdef DEBUG
542 static void
543 print_temp(const struct tgsi_exec_machine *mach, uint index)
544 {
545 const struct tgsi_exec_vector *tmp = &mach->Temps[index];
546 int i;
547 debug_printf("Temp[%u] =\n", index);
548 for (i = 0; i < 4; i++) {
549 debug_printf(" %c: { %f, %f, %f, %f }\n",
550 "XYZW"[i],
551 tmp->xyzw[i].f[0],
552 tmp->xyzw[i].f[1],
553 tmp->xyzw[i].f[2],
554 tmp->xyzw[i].f[3]);
555 }
556 }
557 #endif
558
559
560 /**
561 * Check if there's a potential src/dst register data dependency when
562 * using SOA execution.
563 * Example:
564 * MOV T, T.yxwz;
565 * This would expand into:
566 * MOV t0, t1;
567 * MOV t1, t0;
568 * MOV t2, t3;
569 * MOV t3, t2;
570 * The second instruction will have the wrong value for t0 if executed as-is.
571 */
572 boolean
573 tgsi_check_soa_dependencies(const struct tgsi_full_instruction *inst)
574 {
575 uint i, chan;
576
577 uint writemask = inst->Dst[0].Register.WriteMask;
578 if (writemask == TGSI_WRITEMASK_X ||
579 writemask == TGSI_WRITEMASK_Y ||
580 writemask == TGSI_WRITEMASK_Z ||
581 writemask == TGSI_WRITEMASK_W ||
582 writemask == TGSI_WRITEMASK_NONE) {
583 /* no chance of data dependency */
584 return FALSE;
585 }
586
587 /* loop over src regs */
588 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
589 if ((inst->Src[i].Register.File ==
590 inst->Dst[0].Register.File) &&
591 (inst->Src[i].Register.Index ==
592 inst->Dst[0].Register.Index)) {
593 /* loop over dest channels */
594 uint channelsWritten = 0x0;
595 FOR_EACH_ENABLED_CHANNEL(*inst, chan) {
596 /* check if we're reading a channel that's been written */
597 uint swizzle = tgsi_util_get_full_src_register_swizzle(&inst->Src[i], chan);
598 if (channelsWritten & (1 << swizzle)) {
599 return TRUE;
600 }
601
602 channelsWritten |= (1 << chan);
603 }
604 }
605 }
606 return FALSE;
607 }
608
609
610 /**
611 * Initialize machine state by expanding tokens to full instructions,
612 * allocating temporary storage, setting up constants, etc.
613 * After this, we can call tgsi_exec_machine_run() many times.
614 */
615 void
616 tgsi_exec_machine_bind_shader(
617 struct tgsi_exec_machine *mach,
618 const struct tgsi_token *tokens,
619 uint numSamplers,
620 struct tgsi_sampler **samplers)
621 {
622 uint k;
623 struct tgsi_parse_context parse;
624 struct tgsi_full_instruction *instructions;
625 struct tgsi_full_declaration *declarations;
626 uint maxInstructions = 10, numInstructions = 0;
627 uint maxDeclarations = 10, numDeclarations = 0;
628
629 #if 0
630 tgsi_dump(tokens, 0);
631 #endif
632
633 util_init_math();
634
635 mach->Tokens = tokens;
636 mach->Samplers = samplers;
637
638 if (!tokens) {
639 /* unbind and free all */
640 if (mach->Declarations) {
641 FREE( mach->Declarations );
642 }
643 mach->Declarations = NULL;
644 mach->NumDeclarations = 0;
645
646 if (mach->Instructions) {
647 FREE( mach->Instructions );
648 }
649 mach->Instructions = NULL;
650 mach->NumInstructions = 0;
651
652 return;
653 }
654
655 k = tgsi_parse_init (&parse, mach->Tokens);
656 if (k != TGSI_PARSE_OK) {
657 debug_printf( "Problem parsing!\n" );
658 return;
659 }
660
661 mach->Processor = parse.FullHeader.Processor.Processor;
662 mach->ImmLimit = 0;
663
664 declarations = (struct tgsi_full_declaration *)
665 MALLOC( maxDeclarations * sizeof(struct tgsi_full_declaration) );
666
667 if (!declarations) {
668 return;
669 }
670
671 instructions = (struct tgsi_full_instruction *)
672 MALLOC( maxInstructions * sizeof(struct tgsi_full_instruction) );
673
674 if (!instructions) {
675 FREE( declarations );
676 return;
677 }
678
679 while( !tgsi_parse_end_of_tokens( &parse ) ) {
680 uint i;
681
682 tgsi_parse_token( &parse );
683 switch( parse.FullToken.Token.Type ) {
684 case TGSI_TOKEN_TYPE_DECLARATION:
685 /* save expanded declaration */
686 if (numDeclarations == maxDeclarations) {
687 declarations = REALLOC(declarations,
688 maxDeclarations
689 * sizeof(struct tgsi_full_declaration),
690 (maxDeclarations + 10)
691 * sizeof(struct tgsi_full_declaration));
692 maxDeclarations += 10;
693 }
694 if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_OUTPUT) {
695 unsigned reg;
696 for (reg = parse.FullToken.FullDeclaration.Range.First;
697 reg <= parse.FullToken.FullDeclaration.Range.Last;
698 ++reg) {
699 ++mach->NumOutputs;
700 }
701 }
702 memcpy(declarations + numDeclarations,
703 &parse.FullToken.FullDeclaration,
704 sizeof(declarations[0]));
705 numDeclarations++;
706 break;
707
708 case TGSI_TOKEN_TYPE_IMMEDIATE:
709 {
710 uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
711 assert( size <= 4 );
712 assert( mach->ImmLimit + 1 <= TGSI_EXEC_NUM_IMMEDIATES );
713
714 for( i = 0; i < size; i++ ) {
715 mach->Imms[mach->ImmLimit][i] =
716 parse.FullToken.FullImmediate.u[i].Float;
717 }
718 mach->ImmLimit += 1;
719 }
720 break;
721
722 case TGSI_TOKEN_TYPE_INSTRUCTION:
723
724 /* save expanded instruction */
725 if (numInstructions == maxInstructions) {
726 instructions = REALLOC(instructions,
727 maxInstructions
728 * sizeof(struct tgsi_full_instruction),
729 (maxInstructions + 10)
730 * sizeof(struct tgsi_full_instruction));
731 maxInstructions += 10;
732 }
733
734 memcpy(instructions + numInstructions,
735 &parse.FullToken.FullInstruction,
736 sizeof(instructions[0]));
737
738 numInstructions++;
739 break;
740
741 case TGSI_TOKEN_TYPE_PROPERTY:
742 break;
743
744 default:
745 assert( 0 );
746 }
747 }
748 tgsi_parse_free (&parse);
749
750 if (mach->Declarations) {
751 FREE( mach->Declarations );
752 }
753 mach->Declarations = declarations;
754 mach->NumDeclarations = numDeclarations;
755
756 if (mach->Instructions) {
757 FREE( mach->Instructions );
758 }
759 mach->Instructions = instructions;
760 mach->NumInstructions = numInstructions;
761 }
762
763
764 struct tgsi_exec_machine *
765 tgsi_exec_machine_create( void )
766 {
767 struct tgsi_exec_machine *mach;
768 uint i;
769
770 mach = align_malloc( sizeof *mach, 16 );
771 if (!mach)
772 goto fail;
773
774 memset(mach, 0, sizeof(*mach));
775
776 mach->Addrs = &mach->Temps[TGSI_EXEC_TEMP_ADDR];
777 mach->MaxGeometryShaderOutputs = TGSI_MAX_TOTAL_VERTICES;
778 mach->Predicates = &mach->Temps[TGSI_EXEC_TEMP_P0];
779
780 /* Setup constants. */
781 for( i = 0; i < 4; i++ ) {
782 mach->Temps[TEMP_0_I].xyzw[TEMP_0_C].u[i] = 0x00000000;
783 mach->Temps[TEMP_7F_I].xyzw[TEMP_7F_C].u[i] = 0x7FFFFFFF;
784 mach->Temps[TEMP_80_I].xyzw[TEMP_80_C].u[i] = 0x80000000;
785 mach->Temps[TEMP_FF_I].xyzw[TEMP_FF_C].u[i] = 0xFFFFFFFF;
786 mach->Temps[TEMP_1_I].xyzw[TEMP_1_C].f[i] = 1.0f;
787 mach->Temps[TEMP_2_I].xyzw[TEMP_2_C].f[i] = 2.0f;
788 mach->Temps[TEMP_128_I].xyzw[TEMP_128_C].f[i] = 128.0f;
789 mach->Temps[TEMP_M128_I].xyzw[TEMP_M128_C].f[i] = -128.0f;
790 mach->Temps[TEMP_3_I].xyzw[TEMP_3_C].f[i] = 3.0f;
791 mach->Temps[TEMP_HALF_I].xyzw[TEMP_HALF_C].f[i] = 0.5f;
792 }
793
794 #ifdef DEBUG
795 /* silence warnings */
796 (void) print_chan;
797 (void) print_temp;
798 #endif
799
800 return mach;
801
802 fail:
803 align_free(mach);
804 return NULL;
805 }
806
807
808 void
809 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach)
810 {
811 if (mach) {
812 if (mach->Instructions)
813 FREE(mach->Instructions);
814 if (mach->Declarations)
815 FREE(mach->Declarations);
816 }
817
818 align_free(mach);
819 }
820
821 static void
822 micro_add(union tgsi_exec_channel *dst,
823 const union tgsi_exec_channel *src0,
824 const union tgsi_exec_channel *src1)
825 {
826 dst->f[0] = src0->f[0] + src1->f[0];
827 dst->f[1] = src0->f[1] + src1->f[1];
828 dst->f[2] = src0->f[2] + src1->f[2];
829 dst->f[3] = src0->f[3] + src1->f[3];
830 }
831
832 static void
833 micro_div(
834 union tgsi_exec_channel *dst,
835 const union tgsi_exec_channel *src0,
836 const union tgsi_exec_channel *src1 )
837 {
838 if (src1->f[0] != 0) {
839 dst->f[0] = src0->f[0] / src1->f[0];
840 }
841 if (src1->f[1] != 0) {
842 dst->f[1] = src0->f[1] / src1->f[1];
843 }
844 if (src1->f[2] != 0) {
845 dst->f[2] = src0->f[2] / src1->f[2];
846 }
847 if (src1->f[3] != 0) {
848 dst->f[3] = src0->f[3] / src1->f[3];
849 }
850 }
851
852 static void
853 micro_float_clamp(union tgsi_exec_channel *dst,
854 const union tgsi_exec_channel *src)
855 {
856 uint i;
857
858 for (i = 0; i < 4; i++) {
859 if (src->f[i] > 0.0f) {
860 if (src->f[i] > 1.884467e+019f)
861 dst->f[i] = 1.884467e+019f;
862 else if (src->f[i] < 5.42101e-020f)
863 dst->f[i] = 5.42101e-020f;
864 else
865 dst->f[i] = src->f[i];
866 }
867 else {
868 if (src->f[i] < -1.884467e+019f)
869 dst->f[i] = -1.884467e+019f;
870 else if (src->f[i] > -5.42101e-020f)
871 dst->f[i] = -5.42101e-020f;
872 else
873 dst->f[i] = src->f[i];
874 }
875 }
876 }
877
878 static void
879 micro_lt(
880 union tgsi_exec_channel *dst,
881 const union tgsi_exec_channel *src0,
882 const union tgsi_exec_channel *src1,
883 const union tgsi_exec_channel *src2,
884 const union tgsi_exec_channel *src3 )
885 {
886 dst->f[0] = src0->f[0] < src1->f[0] ? src2->f[0] : src3->f[0];
887 dst->f[1] = src0->f[1] < src1->f[1] ? src2->f[1] : src3->f[1];
888 dst->f[2] = src0->f[2] < src1->f[2] ? src2->f[2] : src3->f[2];
889 dst->f[3] = src0->f[3] < src1->f[3] ? src2->f[3] : src3->f[3];
890 }
891
892 static void
893 micro_max(union tgsi_exec_channel *dst,
894 const union tgsi_exec_channel *src0,
895 const union tgsi_exec_channel *src1)
896 {
897 dst->f[0] = src0->f[0] > src1->f[0] ? src0->f[0] : src1->f[0];
898 dst->f[1] = src0->f[1] > src1->f[1] ? src0->f[1] : src1->f[1];
899 dst->f[2] = src0->f[2] > src1->f[2] ? src0->f[2] : src1->f[2];
900 dst->f[3] = src0->f[3] > src1->f[3] ? src0->f[3] : src1->f[3];
901 }
902
903 static void
904 micro_min(union tgsi_exec_channel *dst,
905 const union tgsi_exec_channel *src0,
906 const union tgsi_exec_channel *src1)
907 {
908 dst->f[0] = src0->f[0] < src1->f[0] ? src0->f[0] : src1->f[0];
909 dst->f[1] = src0->f[1] < src1->f[1] ? src0->f[1] : src1->f[1];
910 dst->f[2] = src0->f[2] < src1->f[2] ? src0->f[2] : src1->f[2];
911 dst->f[3] = src0->f[3] < src1->f[3] ? src0->f[3] : src1->f[3];
912 }
913
914 static void
915 micro_mul(union tgsi_exec_channel *dst,
916 const union tgsi_exec_channel *src0,
917 const union tgsi_exec_channel *src1)
918 {
919 dst->f[0] = src0->f[0] * src1->f[0];
920 dst->f[1] = src0->f[1] * src1->f[1];
921 dst->f[2] = src0->f[2] * src1->f[2];
922 dst->f[3] = src0->f[3] * src1->f[3];
923 }
924
925 #if 0
926 static void
927 micro_imul64(
928 union tgsi_exec_channel *dst0,
929 union tgsi_exec_channel *dst1,
930 const union tgsi_exec_channel *src0,
931 const union tgsi_exec_channel *src1 )
932 {
933 dst1->i[0] = src0->i[0] * src1->i[0];
934 dst1->i[1] = src0->i[1] * src1->i[1];
935 dst1->i[2] = src0->i[2] * src1->i[2];
936 dst1->i[3] = src0->i[3] * src1->i[3];
937 dst0->i[0] = 0;
938 dst0->i[1] = 0;
939 dst0->i[2] = 0;
940 dst0->i[3] = 0;
941 }
942 #endif
943
944 #if 0
945 static void
946 micro_umul64(
947 union tgsi_exec_channel *dst0,
948 union tgsi_exec_channel *dst1,
949 const union tgsi_exec_channel *src0,
950 const union tgsi_exec_channel *src1 )
951 {
952 dst1->u[0] = src0->u[0] * src1->u[0];
953 dst1->u[1] = src0->u[1] * src1->u[1];
954 dst1->u[2] = src0->u[2] * src1->u[2];
955 dst1->u[3] = src0->u[3] * src1->u[3];
956 dst0->u[0] = 0;
957 dst0->u[1] = 0;
958 dst0->u[2] = 0;
959 dst0->u[3] = 0;
960 }
961 #endif
962
963
964 #if 0
965 static void
966 micro_movc(
967 union tgsi_exec_channel *dst,
968 const union tgsi_exec_channel *src0,
969 const union tgsi_exec_channel *src1,
970 const union tgsi_exec_channel *src2 )
971 {
972 dst->u[0] = src0->u[0] ? src1->u[0] : src2->u[0];
973 dst->u[1] = src0->u[1] ? src1->u[1] : src2->u[1];
974 dst->u[2] = src0->u[2] ? src1->u[2] : src2->u[2];
975 dst->u[3] = src0->u[3] ? src1->u[3] : src2->u[3];
976 }
977 #endif
978
979 static void
980 micro_neg(
981 union tgsi_exec_channel *dst,
982 const union tgsi_exec_channel *src )
983 {
984 dst->f[0] = -src->f[0];
985 dst->f[1] = -src->f[1];
986 dst->f[2] = -src->f[2];
987 dst->f[3] = -src->f[3];
988 }
989
990 static void
991 micro_pow(
992 union tgsi_exec_channel *dst,
993 const union tgsi_exec_channel *src0,
994 const union tgsi_exec_channel *src1 )
995 {
996 #if FAST_MATH
997 dst->f[0] = util_fast_pow( src0->f[0], src1->f[0] );
998 dst->f[1] = util_fast_pow( src0->f[1], src1->f[1] );
999 dst->f[2] = util_fast_pow( src0->f[2], src1->f[2] );
1000 dst->f[3] = util_fast_pow( src0->f[3], src1->f[3] );
1001 #else
1002 dst->f[0] = powf( src0->f[0], src1->f[0] );
1003 dst->f[1] = powf( src0->f[1], src1->f[1] );
1004 dst->f[2] = powf( src0->f[2], src1->f[2] );
1005 dst->f[3] = powf( src0->f[3], src1->f[3] );
1006 #endif
1007 }
1008
1009 static void
1010 micro_sub(union tgsi_exec_channel *dst,
1011 const union tgsi_exec_channel *src0,
1012 const union tgsi_exec_channel *src1)
1013 {
1014 dst->f[0] = src0->f[0] - src1->f[0];
1015 dst->f[1] = src0->f[1] - src1->f[1];
1016 dst->f[2] = src0->f[2] - src1->f[2];
1017 dst->f[3] = src0->f[3] - src1->f[3];
1018 }
1019
1020 static void
1021 fetch_src_file_channel(const struct tgsi_exec_machine *mach,
1022 const uint file,
1023 const uint swizzle,
1024 const union tgsi_exec_channel *index,
1025 const union tgsi_exec_channel *index2D,
1026 union tgsi_exec_channel *chan)
1027 {
1028 uint i;
1029
1030 switch (file) {
1031 case TGSI_FILE_CONSTANT:
1032 for (i = 0; i < QUAD_SIZE; i++) {
1033 assert(index2D->i[i] >= 0 && index2D->i[i] < PIPE_MAX_CONSTANT_BUFFERS);
1034 assert(mach->Consts[index2D->i[i]]);
1035
1036 if (index->i[i] < 0) {
1037 chan->u[i] = 0;
1038 } else {
1039 const uint *p = (const uint *)mach->Consts[index2D->i[i]];
1040
1041 chan->u[i] = p[index->i[i] * 4 + swizzle];
1042 }
1043 }
1044 break;
1045
1046 case TGSI_FILE_INPUT:
1047 case TGSI_FILE_SYSTEM_VALUE:
1048 for (i = 0; i < QUAD_SIZE; i++) {
1049 /*
1050 if (TGSI_PROCESSOR_GEOMETRY == mach->Processor) {
1051 debug_printf("Fetching Input[%d] (2d=%d, 1d=%d)\n",
1052 index2D->i[i] * TGSI_EXEC_MAX_INPUT_ATTRIBS + index->i[i],
1053 index2D->i[i], index->i[i]);
1054 }*/
1055 chan->u[i] = mach->Inputs[index2D->i[i] * TGSI_EXEC_MAX_INPUT_ATTRIBS + index->i[i]].xyzw[swizzle].u[i];
1056 }
1057 break;
1058
1059 case TGSI_FILE_TEMPORARY:
1060 for (i = 0; i < QUAD_SIZE; i++) {
1061 assert(index->i[i] < TGSI_EXEC_NUM_TEMPS);
1062 assert(index2D->i[i] == 0);
1063
1064 chan->u[i] = mach->Temps[index->i[i]].xyzw[swizzle].u[i];
1065 }
1066 break;
1067
1068 case TGSI_FILE_IMMEDIATE:
1069 for (i = 0; i < QUAD_SIZE; i++) {
1070 assert(index->i[i] >= 0 && index->i[i] < (int)mach->ImmLimit);
1071 assert(index2D->i[i] == 0);
1072
1073 chan->f[i] = mach->Imms[index->i[i]][swizzle];
1074 }
1075 break;
1076
1077 case TGSI_FILE_ADDRESS:
1078 for (i = 0; i < QUAD_SIZE; i++) {
1079 assert(index->i[i] >= 0);
1080 assert(index2D->i[i] == 0);
1081
1082 chan->u[i] = mach->Addrs[index->i[i]].xyzw[swizzle].u[i];
1083 }
1084 break;
1085
1086 case TGSI_FILE_PREDICATE:
1087 for (i = 0; i < QUAD_SIZE; i++) {
1088 assert(index->i[i] >= 0 && index->i[i] < TGSI_EXEC_NUM_PREDS);
1089 assert(index2D->i[i] == 0);
1090
1091 chan->u[i] = mach->Predicates[0].xyzw[swizzle].u[i];
1092 }
1093 break;
1094
1095 case TGSI_FILE_OUTPUT:
1096 /* vertex/fragment output vars can be read too */
1097 for (i = 0; i < QUAD_SIZE; i++) {
1098 assert(index->i[i] >= 0);
1099 assert(index2D->i[i] == 0);
1100
1101 chan->u[i] = mach->Outputs[index->i[i]].xyzw[swizzle].u[i];
1102 }
1103 break;
1104
1105 default:
1106 assert(0);
1107 for (i = 0; i < QUAD_SIZE; i++) {
1108 chan->u[i] = 0;
1109 }
1110 }
1111 }
1112
1113 static void
1114 fetch_source(const struct tgsi_exec_machine *mach,
1115 union tgsi_exec_channel *chan,
1116 const struct tgsi_full_src_register *reg,
1117 const uint chan_index,
1118 enum tgsi_exec_datatype src_datatype)
1119 {
1120 union tgsi_exec_channel index;
1121 union tgsi_exec_channel index2D;
1122 uint swizzle;
1123
1124 /* We start with a direct index into a register file.
1125 *
1126 * file[1],
1127 * where:
1128 * file = Register.File
1129 * [1] = Register.Index
1130 */
1131 index.i[0] =
1132 index.i[1] =
1133 index.i[2] =
1134 index.i[3] = reg->Register.Index;
1135
1136 /* There is an extra source register that indirectly subscripts
1137 * a register file. The direct index now becomes an offset
1138 * that is being added to the indirect register.
1139 *
1140 * file[ind[2].x+1],
1141 * where:
1142 * ind = Indirect.File
1143 * [2] = Indirect.Index
1144 * .x = Indirect.SwizzleX
1145 */
1146 if (reg->Register.Indirect) {
1147 union tgsi_exec_channel index2;
1148 union tgsi_exec_channel indir_index;
1149 const uint execmask = mach->ExecMask;
1150 uint i;
1151
1152 /* which address register (always zero now) */
1153 index2.i[0] =
1154 index2.i[1] =
1155 index2.i[2] =
1156 index2.i[3] = reg->Indirect.Index;
1157
1158 /* get current value of address register[swizzle] */
1159 swizzle = tgsi_util_get_src_register_swizzle( &reg->Indirect, CHAN_X );
1160 fetch_src_file_channel(mach,
1161 reg->Indirect.File,
1162 swizzle,
1163 &index2,
1164 &ZeroVec,
1165 &indir_index);
1166
1167 /* add value of address register to the offset */
1168 index.i[0] += indir_index.i[0];
1169 index.i[1] += indir_index.i[1];
1170 index.i[2] += indir_index.i[2];
1171 index.i[3] += indir_index.i[3];
1172
1173 /* for disabled execution channels, zero-out the index to
1174 * avoid using a potential garbage value.
1175 */
1176 for (i = 0; i < QUAD_SIZE; i++) {
1177 if ((execmask & (1 << i)) == 0)
1178 index.i[i] = 0;
1179 }
1180 }
1181
1182 /* There is an extra source register that is a second
1183 * subscript to a register file. Effectively it means that
1184 * the register file is actually a 2D array of registers.
1185 *
1186 * file[3][1],
1187 * where:
1188 * [3] = Dimension.Index
1189 */
1190 if (reg->Register.Dimension) {
1191 index2D.i[0] =
1192 index2D.i[1] =
1193 index2D.i[2] =
1194 index2D.i[3] = reg->Dimension.Index;
1195
1196 /* Again, the second subscript index can be addressed indirectly
1197 * identically to the first one.
1198 * Nothing stops us from indirectly addressing the indirect register,
1199 * but there is no need for that, so we won't exercise it.
1200 *
1201 * file[ind[4].y+3][1],
1202 * where:
1203 * ind = DimIndirect.File
1204 * [4] = DimIndirect.Index
1205 * .y = DimIndirect.SwizzleX
1206 */
1207 if (reg->Dimension.Indirect) {
1208 union tgsi_exec_channel index2;
1209 union tgsi_exec_channel indir_index;
1210 const uint execmask = mach->ExecMask;
1211 uint i;
1212
1213 index2.i[0] =
1214 index2.i[1] =
1215 index2.i[2] =
1216 index2.i[3] = reg->DimIndirect.Index;
1217
1218 swizzle = tgsi_util_get_src_register_swizzle( &reg->DimIndirect, CHAN_X );
1219 fetch_src_file_channel(mach,
1220 reg->DimIndirect.File,
1221 swizzle,
1222 &index2,
1223 &ZeroVec,
1224 &indir_index);
1225
1226 index2D.i[0] += indir_index.i[0];
1227 index2D.i[1] += indir_index.i[1];
1228 index2D.i[2] += indir_index.i[2];
1229 index2D.i[3] += indir_index.i[3];
1230
1231 /* for disabled execution channels, zero-out the index to
1232 * avoid using a potential garbage value.
1233 */
1234 for (i = 0; i < QUAD_SIZE; i++) {
1235 if ((execmask & (1 << i)) == 0) {
1236 index2D.i[i] = 0;
1237 }
1238 }
1239 }
1240
1241 /* If by any chance there was a need for a 3D array of register
1242 * files, we would have to check whether Dimension is followed
1243 * by a dimension register and continue the saga.
1244 */
1245 } else {
1246 index2D.i[0] =
1247 index2D.i[1] =
1248 index2D.i[2] =
1249 index2D.i[3] = 0;
1250 }
1251
1252 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
1253 fetch_src_file_channel(mach,
1254 reg->Register.File,
1255 swizzle,
1256 &index,
1257 &index2D,
1258 chan);
1259
1260 if (reg->Register.Absolute) {
1261 if (src_datatype == TGSI_EXEC_DATA_FLOAT) {
1262 micro_abs(chan, chan);
1263 } else {
1264 micro_iabs(chan, chan);
1265 }
1266 }
1267
1268 if (reg->Register.Negate) {
1269 if (src_datatype == TGSI_EXEC_DATA_FLOAT) {
1270 micro_neg(chan, chan);
1271 } else {
1272 micro_ineg(chan, chan);
1273 }
1274 }
1275 }
1276
1277 static void
1278 store_dest(struct tgsi_exec_machine *mach,
1279 const union tgsi_exec_channel *chan,
1280 const struct tgsi_full_dst_register *reg,
1281 const struct tgsi_full_instruction *inst,
1282 uint chan_index,
1283 enum tgsi_exec_datatype dst_datatype)
1284 {
1285 uint i;
1286 union tgsi_exec_channel null;
1287 union tgsi_exec_channel *dst;
1288 uint execmask = mach->ExecMask;
1289 int offset = 0; /* indirection offset */
1290 int index;
1291
1292 /* for debugging */
1293 if (0 && dst_datatype == TGSI_EXEC_DATA_FLOAT) {
1294 check_inf_or_nan(chan);
1295 }
1296
1297 /* There is an extra source register that indirectly subscripts
1298 * a register file. The direct index now becomes an offset
1299 * that is being added to the indirect register.
1300 *
1301 * file[ind[2].x+1],
1302 * where:
1303 * ind = Indirect.File
1304 * [2] = Indirect.Index
1305 * .x = Indirect.SwizzleX
1306 */
1307 if (reg->Register.Indirect) {
1308 union tgsi_exec_channel index;
1309 union tgsi_exec_channel indir_index;
1310 uint swizzle;
1311
1312 /* which address register (always zero for now) */
1313 index.i[0] =
1314 index.i[1] =
1315 index.i[2] =
1316 index.i[3] = reg->Indirect.Index;
1317
1318 /* get current value of address register[swizzle] */
1319 swizzle = tgsi_util_get_src_register_swizzle( &reg->Indirect, CHAN_X );
1320
1321 /* fetch values from the address/indirection register */
1322 fetch_src_file_channel(mach,
1323 reg->Indirect.File,
1324 swizzle,
1325 &index,
1326 &ZeroVec,
1327 &indir_index);
1328
1329 /* save indirection offset */
1330 offset = indir_index.i[0];
1331 }
1332
1333 switch (reg->Register.File) {
1334 case TGSI_FILE_NULL:
1335 dst = &null;
1336 break;
1337
1338 case TGSI_FILE_OUTPUT:
1339 index = mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0]
1340 + reg->Register.Index;
1341 dst = &mach->Outputs[offset + index].xyzw[chan_index];
1342 #if 0
1343 if (TGSI_PROCESSOR_GEOMETRY == mach->Processor) {
1344 fprintf(stderr, "STORING OUT[%d] mask(%d), = (", offset + index, execmask);
1345 for (i = 0; i < QUAD_SIZE; i++)
1346 if (execmask & (1 << i))
1347 fprintf(stderr, "%f, ", chan->f[i]);
1348 fprintf(stderr, ")\n");
1349 }
1350 #endif
1351 break;
1352
1353 case TGSI_FILE_TEMPORARY:
1354 index = reg->Register.Index;
1355 assert( index < TGSI_EXEC_NUM_TEMPS );
1356 dst = &mach->Temps[offset + index].xyzw[chan_index];
1357 break;
1358
1359 case TGSI_FILE_ADDRESS:
1360 index = reg->Register.Index;
1361 dst = &mach->Addrs[index].xyzw[chan_index];
1362 break;
1363
1364 case TGSI_FILE_PREDICATE:
1365 index = reg->Register.Index;
1366 assert(index < TGSI_EXEC_NUM_PREDS);
1367 dst = &mach->Predicates[index].xyzw[chan_index];
1368 break;
1369
1370 default:
1371 assert( 0 );
1372 return;
1373 }
1374
1375 if (inst->Instruction.Predicate) {
1376 uint swizzle;
1377 union tgsi_exec_channel *pred;
1378
1379 switch (chan_index) {
1380 case CHAN_X:
1381 swizzle = inst->Predicate.SwizzleX;
1382 break;
1383 case CHAN_Y:
1384 swizzle = inst->Predicate.SwizzleY;
1385 break;
1386 case CHAN_Z:
1387 swizzle = inst->Predicate.SwizzleZ;
1388 break;
1389 case CHAN_W:
1390 swizzle = inst->Predicate.SwizzleW;
1391 break;
1392 default:
1393 assert(0);
1394 return;
1395 }
1396
1397 assert(inst->Predicate.Index == 0);
1398
1399 pred = &mach->Predicates[inst->Predicate.Index].xyzw[swizzle];
1400
1401 if (inst->Predicate.Negate) {
1402 for (i = 0; i < QUAD_SIZE; i++) {
1403 if (pred->u[i]) {
1404 execmask &= ~(1 << i);
1405 }
1406 }
1407 } else {
1408 for (i = 0; i < QUAD_SIZE; i++) {
1409 if (!pred->u[i]) {
1410 execmask &= ~(1 << i);
1411 }
1412 }
1413 }
1414 }
1415
1416 switch (inst->Instruction.Saturate) {
1417 case TGSI_SAT_NONE:
1418 for (i = 0; i < QUAD_SIZE; i++)
1419 if (execmask & (1 << i))
1420 dst->i[i] = chan->i[i];
1421 break;
1422
1423 case TGSI_SAT_ZERO_ONE:
1424 for (i = 0; i < QUAD_SIZE; i++)
1425 if (execmask & (1 << i)) {
1426 if (chan->f[i] < 0.0f)
1427 dst->f[i] = 0.0f;
1428 else if (chan->f[i] > 1.0f)
1429 dst->f[i] = 1.0f;
1430 else
1431 dst->i[i] = chan->i[i];
1432 }
1433 break;
1434
1435 case TGSI_SAT_MINUS_PLUS_ONE:
1436 for (i = 0; i < QUAD_SIZE; i++)
1437 if (execmask & (1 << i)) {
1438 if (chan->f[i] < -1.0f)
1439 dst->f[i] = -1.0f;
1440 else if (chan->f[i] > 1.0f)
1441 dst->f[i] = 1.0f;
1442 else
1443 dst->i[i] = chan->i[i];
1444 }
1445 break;
1446
1447 default:
1448 assert( 0 );
1449 }
1450 }
1451
1452 #define FETCH(VAL,INDEX,CHAN)\
1453 fetch_source(mach, VAL, &inst->Src[INDEX], CHAN, TGSI_EXEC_DATA_FLOAT)
1454
1455 #define STORE(VAL,INDEX,CHAN)\
1456 store_dest(mach, VAL, &inst->Dst[INDEX], inst, CHAN, TGSI_EXEC_DATA_FLOAT)
1457
1458
1459 /**
1460 * Execute ARB-style KIL which is predicated by a src register.
1461 * Kill fragment if any of the four values is less than zero.
1462 */
1463 static void
1464 exec_kil(struct tgsi_exec_machine *mach,
1465 const struct tgsi_full_instruction *inst)
1466 {
1467 uint uniquemask;
1468 uint chan_index;
1469 uint kilmask = 0; /* bit 0 = pixel 0, bit 1 = pixel 1, etc */
1470 union tgsi_exec_channel r[1];
1471
1472 /* This mask stores component bits that were already tested. */
1473 uniquemask = 0;
1474
1475 for (chan_index = 0; chan_index < 4; chan_index++)
1476 {
1477 uint swizzle;
1478 uint i;
1479
1480 /* unswizzle channel */
1481 swizzle = tgsi_util_get_full_src_register_swizzle (
1482 &inst->Src[0],
1483 chan_index);
1484
1485 /* check if the component has not been already tested */
1486 if (uniquemask & (1 << swizzle))
1487 continue;
1488 uniquemask |= 1 << swizzle;
1489
1490 FETCH(&r[0], 0, chan_index);
1491 for (i = 0; i < 4; i++)
1492 if (r[0].f[i] < 0.0f)
1493 kilmask |= 1 << i;
1494 }
1495
1496 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] |= kilmask;
1497 }
1498
1499 /**
1500 * Execute NVIDIA-style KIL which is predicated by a condition code.
1501 * Kill fragment if the condition code is TRUE.
1502 */
1503 static void
1504 exec_kilp(struct tgsi_exec_machine *mach,
1505 const struct tgsi_full_instruction *inst)
1506 {
1507 uint kilmask; /* bit 0 = pixel 0, bit 1 = pixel 1, etc */
1508
1509 /* "unconditional" kil */
1510 kilmask = mach->ExecMask;
1511 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] |= kilmask;
1512 }
1513
1514 static void
1515 emit_vertex(struct tgsi_exec_machine *mach)
1516 {
1517 /* FIXME: check for exec mask correctly
1518 unsigned i;
1519 for (i = 0; i < QUAD_SIZE; ++i) {
1520 if ((mach->ExecMask & (1 << i)))
1521 */
1522 if (mach->ExecMask) {
1523 mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0] += mach->NumOutputs;
1524 mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]]++;
1525 }
1526 }
1527
1528 static void
1529 emit_primitive(struct tgsi_exec_machine *mach)
1530 {
1531 unsigned *prim_count = &mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0];
1532 /* FIXME: check for exec mask correctly
1533 unsigned i;
1534 for (i = 0; i < QUAD_SIZE; ++i) {
1535 if ((mach->ExecMask & (1 << i)))
1536 */
1537 if (mach->ExecMask) {
1538 ++(*prim_count);
1539 debug_assert((*prim_count * mach->NumOutputs) < mach->MaxGeometryShaderOutputs);
1540 mach->Primitives[*prim_count] = 0;
1541 }
1542 }
1543
1544 static void
1545 conditional_emit_primitive(struct tgsi_exec_machine *mach)
1546 {
1547 if (TGSI_PROCESSOR_GEOMETRY == mach->Processor) {
1548 int emitted_verts =
1549 mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]];
1550 if (emitted_verts) {
1551 emit_primitive(mach);
1552 }
1553 }
1554 }
1555
1556
1557 /*
1558 * Fetch four texture samples using STR texture coordinates.
1559 */
1560 static void
1561 fetch_texel( struct tgsi_sampler *sampler,
1562 const union tgsi_exec_channel *s,
1563 const union tgsi_exec_channel *t,
1564 const union tgsi_exec_channel *p,
1565 const union tgsi_exec_channel *c0,
1566 enum tgsi_sampler_control control,
1567 union tgsi_exec_channel *r,
1568 union tgsi_exec_channel *g,
1569 union tgsi_exec_channel *b,
1570 union tgsi_exec_channel *a )
1571 {
1572 uint j;
1573 float rgba[NUM_CHANNELS][QUAD_SIZE];
1574
1575 sampler->get_samples(sampler, s->f, t->f, p->f, c0->f, control, rgba);
1576
1577 for (j = 0; j < 4; j++) {
1578 r->f[j] = rgba[0][j];
1579 g->f[j] = rgba[1][j];
1580 b->f[j] = rgba[2][j];
1581 a->f[j] = rgba[3][j];
1582 }
1583 }
1584
1585
1586 #define TEX_MODIFIER_NONE 0
1587 #define TEX_MODIFIER_PROJECTED 1
1588 #define TEX_MODIFIER_LOD_BIAS 2
1589 #define TEX_MODIFIER_EXPLICIT_LOD 3
1590
1591
1592 static void
1593 exec_tex(struct tgsi_exec_machine *mach,
1594 const struct tgsi_full_instruction *inst,
1595 uint modifier)
1596 {
1597 const uint unit = inst->Src[1].Register.Index;
1598 union tgsi_exec_channel r[4];
1599 const union tgsi_exec_channel *lod = &ZeroVec;
1600 enum tgsi_sampler_control control;
1601 uint chan_index;
1602
1603 if (modifier != TEX_MODIFIER_NONE) {
1604 FETCH(&r[3], 0, CHAN_W);
1605 if (modifier != TEX_MODIFIER_PROJECTED) {
1606 lod = &r[3];
1607 }
1608 }
1609
1610 if (modifier == TEX_MODIFIER_EXPLICIT_LOD) {
1611 control = tgsi_sampler_lod_explicit;
1612 } else {
1613 control = tgsi_sampler_lod_bias;
1614 }
1615
1616 switch (inst->Texture.Texture) {
1617 case TGSI_TEXTURE_1D:
1618 case TGSI_TEXTURE_SHADOW1D:
1619 FETCH(&r[0], 0, CHAN_X);
1620
1621 if (modifier == TEX_MODIFIER_PROJECTED) {
1622 micro_div(&r[0], &r[0], &r[3]);
1623 }
1624
1625 fetch_texel(mach->Samplers[unit],
1626 &r[0], &ZeroVec, &ZeroVec, lod, /* S, T, P, LOD */
1627 control,
1628 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
1629 break;
1630
1631 case TGSI_TEXTURE_2D:
1632 case TGSI_TEXTURE_RECT:
1633 case TGSI_TEXTURE_SHADOW2D:
1634 case TGSI_TEXTURE_SHADOWRECT:
1635 FETCH(&r[0], 0, CHAN_X);
1636 FETCH(&r[1], 0, CHAN_Y);
1637 FETCH(&r[2], 0, CHAN_Z);
1638
1639 if (modifier == TEX_MODIFIER_PROJECTED) {
1640 micro_div(&r[0], &r[0], &r[3]);
1641 micro_div(&r[1], &r[1], &r[3]);
1642 micro_div(&r[2], &r[2], &r[3]);
1643 }
1644
1645 fetch_texel(mach->Samplers[unit],
1646 &r[0], &r[1], &r[2], lod, /* S, T, P, LOD */
1647 control,
1648 &r[0], &r[1], &r[2], &r[3]); /* outputs */
1649 break;
1650
1651 case TGSI_TEXTURE_3D:
1652 case TGSI_TEXTURE_CUBE:
1653 FETCH(&r[0], 0, CHAN_X);
1654 FETCH(&r[1], 0, CHAN_Y);
1655 FETCH(&r[2], 0, CHAN_Z);
1656
1657 if (modifier == TEX_MODIFIER_PROJECTED) {
1658 micro_div(&r[0], &r[0], &r[3]);
1659 micro_div(&r[1], &r[1], &r[3]);
1660 micro_div(&r[2], &r[2], &r[3]);
1661 }
1662
1663 fetch_texel(mach->Samplers[unit],
1664 &r[0], &r[1], &r[2], lod,
1665 control,
1666 &r[0], &r[1], &r[2], &r[3]);
1667 break;
1668
1669 default:
1670 assert(0);
1671 }
1672
1673 FOR_EACH_ENABLED_CHANNEL(*inst, chan_index) {
1674 STORE(&r[chan_index], 0, chan_index);
1675 }
1676 }
1677
1678 static void
1679 exec_txd(struct tgsi_exec_machine *mach,
1680 const struct tgsi_full_instruction *inst)
1681 {
1682 const uint unit = inst->Src[3].Register.Index;
1683 union tgsi_exec_channel r[4];
1684 uint chan_index;
1685
1686 /*
1687 * XXX: This is fake TXD -- the derivatives are not taken into account, yet.
1688 */
1689
1690 switch (inst->Texture.Texture) {
1691 case TGSI_TEXTURE_1D:
1692 case TGSI_TEXTURE_SHADOW1D:
1693
1694 FETCH(&r[0], 0, CHAN_X);
1695
1696 fetch_texel(mach->Samplers[unit],
1697 &r[0], &ZeroVec, &ZeroVec, &ZeroVec, /* S, T, P, BIAS */
1698 tgsi_sampler_lod_bias,
1699 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
1700 break;
1701
1702 case TGSI_TEXTURE_2D:
1703 case TGSI_TEXTURE_RECT:
1704 case TGSI_TEXTURE_SHADOW2D:
1705 case TGSI_TEXTURE_SHADOWRECT:
1706
1707 FETCH(&r[0], 0, CHAN_X);
1708 FETCH(&r[1], 0, CHAN_Y);
1709 FETCH(&r[2], 0, CHAN_Z);
1710
1711 fetch_texel(mach->Samplers[unit],
1712 &r[0], &r[1], &r[2], &ZeroVec, /* inputs */
1713 tgsi_sampler_lod_bias,
1714 &r[0], &r[1], &r[2], &r[3]); /* outputs */
1715 break;
1716
1717 case TGSI_TEXTURE_3D:
1718 case TGSI_TEXTURE_CUBE:
1719
1720 FETCH(&r[0], 0, CHAN_X);
1721 FETCH(&r[1], 0, CHAN_Y);
1722 FETCH(&r[2], 0, CHAN_Z);
1723
1724 fetch_texel(mach->Samplers[unit],
1725 &r[0], &r[1], &r[2], &ZeroVec,
1726 tgsi_sampler_lod_bias,
1727 &r[0], &r[1], &r[2], &r[3]);
1728 break;
1729
1730 default:
1731 assert(0);
1732 }
1733
1734 FOR_EACH_ENABLED_CHANNEL(*inst, chan_index) {
1735 STORE(&r[chan_index], 0, chan_index);
1736 }
1737 }
1738
1739
1740 /**
1741 * Evaluate a constant-valued coefficient at the position of the
1742 * current quad.
1743 */
1744 static void
1745 eval_constant_coef(
1746 struct tgsi_exec_machine *mach,
1747 unsigned attrib,
1748 unsigned chan )
1749 {
1750 unsigned i;
1751
1752 for( i = 0; i < QUAD_SIZE; i++ ) {
1753 mach->Inputs[attrib].xyzw[chan].f[i] = mach->InterpCoefs[attrib].a0[chan];
1754 }
1755 }
1756
1757 /**
1758 * Evaluate a linear-valued coefficient at the position of the
1759 * current quad.
1760 */
1761 static void
1762 eval_linear_coef(
1763 struct tgsi_exec_machine *mach,
1764 unsigned attrib,
1765 unsigned chan )
1766 {
1767 const float x = mach->QuadPos.xyzw[0].f[0];
1768 const float y = mach->QuadPos.xyzw[1].f[0];
1769 const float dadx = mach->InterpCoefs[attrib].dadx[chan];
1770 const float dady = mach->InterpCoefs[attrib].dady[chan];
1771 const float a0 = mach->InterpCoefs[attrib].a0[chan] + dadx * x + dady * y;
1772 mach->Inputs[attrib].xyzw[chan].f[0] = a0;
1773 mach->Inputs[attrib].xyzw[chan].f[1] = a0 + dadx;
1774 mach->Inputs[attrib].xyzw[chan].f[2] = a0 + dady;
1775 mach->Inputs[attrib].xyzw[chan].f[3] = a0 + dadx + dady;
1776 }
1777
1778 /**
1779 * Evaluate a perspective-valued coefficient at the position of the
1780 * current quad.
1781 */
1782 static void
1783 eval_perspective_coef(
1784 struct tgsi_exec_machine *mach,
1785 unsigned attrib,
1786 unsigned chan )
1787 {
1788 const float x = mach->QuadPos.xyzw[0].f[0];
1789 const float y = mach->QuadPos.xyzw[1].f[0];
1790 const float dadx = mach->InterpCoefs[attrib].dadx[chan];
1791 const float dady = mach->InterpCoefs[attrib].dady[chan];
1792 const float a0 = mach->InterpCoefs[attrib].a0[chan] + dadx * x + dady * y;
1793 const float *w = mach->QuadPos.xyzw[3].f;
1794 /* divide by W here */
1795 mach->Inputs[attrib].xyzw[chan].f[0] = a0 / w[0];
1796 mach->Inputs[attrib].xyzw[chan].f[1] = (a0 + dadx) / w[1];
1797 mach->Inputs[attrib].xyzw[chan].f[2] = (a0 + dady) / w[2];
1798 mach->Inputs[attrib].xyzw[chan].f[3] = (a0 + dadx + dady) / w[3];
1799 }
1800
1801
1802 typedef void (* eval_coef_func)(
1803 struct tgsi_exec_machine *mach,
1804 unsigned attrib,
1805 unsigned chan );
1806
1807 static void
1808 exec_declaration(struct tgsi_exec_machine *mach,
1809 const struct tgsi_full_declaration *decl)
1810 {
1811 if (mach->Processor == TGSI_PROCESSOR_FRAGMENT) {
1812 if (decl->Declaration.File == TGSI_FILE_INPUT ||
1813 decl->Declaration.File == TGSI_FILE_SYSTEM_VALUE) {
1814 uint first, last, mask;
1815
1816 first = decl->Range.First;
1817 last = decl->Range.Last;
1818 mask = decl->Declaration.UsageMask;
1819
1820 /* XXX we could remove this special-case code since
1821 * mach->InterpCoefs[first].a0 should already have the
1822 * front/back-face value. But we should first update the
1823 * ureg code to emit the right UsageMask value (WRITEMASK_X).
1824 * Then, we could remove the tgsi_exec_machine::Face field.
1825 */
1826 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
1827 uint i;
1828
1829 assert(decl->Semantic.Index == 0);
1830 assert(first == last);
1831
1832 for (i = 0; i < QUAD_SIZE; i++) {
1833 mach->Inputs[first].xyzw[0].f[i] = mach->Face;
1834 }
1835 } else {
1836 eval_coef_func eval;
1837 uint i, j;
1838
1839 switch (decl->Declaration.Interpolate) {
1840 case TGSI_INTERPOLATE_CONSTANT:
1841 eval = eval_constant_coef;
1842 break;
1843
1844 case TGSI_INTERPOLATE_LINEAR:
1845 eval = eval_linear_coef;
1846 break;
1847
1848 case TGSI_INTERPOLATE_PERSPECTIVE:
1849 eval = eval_perspective_coef;
1850 break;
1851
1852 default:
1853 assert(0);
1854 return;
1855 }
1856
1857 for (j = 0; j < NUM_CHANNELS; j++) {
1858 if (mask & (1 << j)) {
1859 for (i = first; i <= last; i++) {
1860 eval(mach, i, j);
1861 }
1862 }
1863 }
1864 }
1865 }
1866 }
1867 }
1868
1869 typedef void (* micro_unary_op)(union tgsi_exec_channel *dst,
1870 const union tgsi_exec_channel *src);
1871
1872 static void
1873 exec_scalar_unary(struct tgsi_exec_machine *mach,
1874 const struct tgsi_full_instruction *inst,
1875 micro_unary_op op,
1876 enum tgsi_exec_datatype dst_datatype,
1877 enum tgsi_exec_datatype src_datatype)
1878 {
1879 unsigned int chan;
1880 union tgsi_exec_channel src;
1881 union tgsi_exec_channel dst;
1882
1883 fetch_source(mach, &src, &inst->Src[0], CHAN_X, src_datatype);
1884 op(&dst, &src);
1885 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1886 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1887 store_dest(mach, &dst, &inst->Dst[0], inst, chan, dst_datatype);
1888 }
1889 }
1890 }
1891
1892 static void
1893 exec_vector_unary(struct tgsi_exec_machine *mach,
1894 const struct tgsi_full_instruction *inst,
1895 micro_unary_op op,
1896 enum tgsi_exec_datatype dst_datatype,
1897 enum tgsi_exec_datatype src_datatype)
1898 {
1899 unsigned int chan;
1900 struct tgsi_exec_vector dst;
1901
1902 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1903 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1904 union tgsi_exec_channel src;
1905
1906 fetch_source(mach, &src, &inst->Src[0], chan, src_datatype);
1907 op(&dst.xyzw[chan], &src);
1908 }
1909 }
1910 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1911 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1912 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
1913 }
1914 }
1915 }
1916
1917 typedef void (* micro_binary_op)(union tgsi_exec_channel *dst,
1918 const union tgsi_exec_channel *src0,
1919 const union tgsi_exec_channel *src1);
1920
1921 static void
1922 exec_vector_binary(struct tgsi_exec_machine *mach,
1923 const struct tgsi_full_instruction *inst,
1924 micro_binary_op op,
1925 enum tgsi_exec_datatype dst_datatype,
1926 enum tgsi_exec_datatype src_datatype)
1927 {
1928 unsigned int chan;
1929 struct tgsi_exec_vector dst;
1930
1931 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1932 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1933 union tgsi_exec_channel src[2];
1934
1935 fetch_source(mach, &src[0], &inst->Src[0], chan, src_datatype);
1936 fetch_source(mach, &src[1], &inst->Src[1], chan, src_datatype);
1937 op(&dst.xyzw[chan], &src[0], &src[1]);
1938 }
1939 }
1940 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1941 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1942 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
1943 }
1944 }
1945 }
1946
1947 typedef void (* micro_trinary_op)(union tgsi_exec_channel *dst,
1948 const union tgsi_exec_channel *src0,
1949 const union tgsi_exec_channel *src1,
1950 const union tgsi_exec_channel *src2);
1951
1952 static void
1953 exec_vector_trinary(struct tgsi_exec_machine *mach,
1954 const struct tgsi_full_instruction *inst,
1955 micro_trinary_op op,
1956 enum tgsi_exec_datatype dst_datatype,
1957 enum tgsi_exec_datatype src_datatype)
1958 {
1959 unsigned int chan;
1960 struct tgsi_exec_vector dst;
1961
1962 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1963 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1964 union tgsi_exec_channel src[3];
1965
1966 fetch_source(mach, &src[0], &inst->Src[0], chan, src_datatype);
1967 fetch_source(mach, &src[1], &inst->Src[1], chan, src_datatype);
1968 fetch_source(mach, &src[2], &inst->Src[2], chan, src_datatype);
1969 op(&dst.xyzw[chan], &src[0], &src[1], &src[2]);
1970 }
1971 }
1972 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1973 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1974 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
1975 }
1976 }
1977 }
1978
1979 static void
1980 exec_dp3(struct tgsi_exec_machine *mach,
1981 const struct tgsi_full_instruction *inst)
1982 {
1983 unsigned int chan;
1984 union tgsi_exec_channel arg[3];
1985
1986 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
1987 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_X, TGSI_EXEC_DATA_FLOAT);
1988 micro_mul(&arg[2], &arg[0], &arg[1]);
1989
1990 for (chan = CHAN_Y; chan <= CHAN_Z; chan++) {
1991 fetch_source(mach, &arg[0], &inst->Src[0], chan, TGSI_EXEC_DATA_FLOAT);
1992 fetch_source(mach, &arg[1], &inst->Src[1], chan, TGSI_EXEC_DATA_FLOAT);
1993 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
1994 }
1995
1996 for (chan = 0; chan < NUM_CHANNELS; chan++) {
1997 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1998 store_dest(mach, &arg[2], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
1999 }
2000 }
2001 }
2002
2003 static void
2004 exec_dp4(struct tgsi_exec_machine *mach,
2005 const struct tgsi_full_instruction *inst)
2006 {
2007 unsigned int chan;
2008 union tgsi_exec_channel arg[3];
2009
2010 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2011 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2012 micro_mul(&arg[2], &arg[0], &arg[1]);
2013
2014 for (chan = CHAN_Y; chan <= CHAN_W; chan++) {
2015 fetch_source(mach, &arg[0], &inst->Src[0], chan, TGSI_EXEC_DATA_FLOAT);
2016 fetch_source(mach, &arg[1], &inst->Src[1], chan, TGSI_EXEC_DATA_FLOAT);
2017 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
2018 }
2019
2020 for (chan = 0; chan < NUM_CHANNELS; chan++) {
2021 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2022 store_dest(mach, &arg[2], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2023 }
2024 }
2025 }
2026
2027 static void
2028 exec_dp2a(struct tgsi_exec_machine *mach,
2029 const struct tgsi_full_instruction *inst)
2030 {
2031 unsigned int chan;
2032 union tgsi_exec_channel arg[3];
2033
2034 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2035 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2036 micro_mul(&arg[2], &arg[0], &arg[1]);
2037
2038 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_Y, TGSI_EXEC_DATA_FLOAT);
2039 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_Y, TGSI_EXEC_DATA_FLOAT);
2040 micro_mad(&arg[0], &arg[0], &arg[1], &arg[2]);
2041
2042 fetch_source(mach, &arg[1], &inst->Src[2], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2043 micro_add(&arg[0], &arg[0], &arg[1]);
2044
2045 for (chan = 0; chan < NUM_CHANNELS; chan++) {
2046 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2047 store_dest(mach, &arg[0], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2048 }
2049 }
2050 }
2051
2052 static void
2053 exec_dph(struct tgsi_exec_machine *mach,
2054 const struct tgsi_full_instruction *inst)
2055 {
2056 unsigned int chan;
2057 union tgsi_exec_channel arg[3];
2058
2059 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2060 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2061 micro_mul(&arg[2], &arg[0], &arg[1]);
2062
2063 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_Y, TGSI_EXEC_DATA_FLOAT);
2064 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_Y, TGSI_EXEC_DATA_FLOAT);
2065 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
2066
2067 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_Z, TGSI_EXEC_DATA_FLOAT);
2068 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_Z, TGSI_EXEC_DATA_FLOAT);
2069 micro_mad(&arg[0], &arg[0], &arg[1], &arg[2]);
2070
2071 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_W, TGSI_EXEC_DATA_FLOAT);
2072 micro_add(&arg[0], &arg[0], &arg[1]);
2073
2074 for (chan = 0; chan < NUM_CHANNELS; chan++) {
2075 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2076 store_dest(mach, &arg[0], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2077 }
2078 }
2079 }
2080
2081 static void
2082 exec_dp2(struct tgsi_exec_machine *mach,
2083 const struct tgsi_full_instruction *inst)
2084 {
2085 unsigned int chan;
2086 union tgsi_exec_channel arg[3];
2087
2088 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2089 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2090 micro_mul(&arg[2], &arg[0], &arg[1]);
2091
2092 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_Y, TGSI_EXEC_DATA_FLOAT);
2093 fetch_source(mach, &arg[1], &inst->Src[1], CHAN_Y, TGSI_EXEC_DATA_FLOAT);
2094 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
2095
2096 for (chan = 0; chan < NUM_CHANNELS; chan++) {
2097 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2098 store_dest(mach, &arg[2], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2099 }
2100 }
2101 }
2102
2103 static void
2104 exec_nrm4(struct tgsi_exec_machine *mach,
2105 const struct tgsi_full_instruction *inst)
2106 {
2107 unsigned int chan;
2108 union tgsi_exec_channel arg[4];
2109 union tgsi_exec_channel scale;
2110
2111 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2112 micro_mul(&scale, &arg[0], &arg[0]);
2113
2114 for (chan = CHAN_Y; chan <= CHAN_W; chan++) {
2115 union tgsi_exec_channel product;
2116
2117 fetch_source(mach, &arg[chan], &inst->Src[0], chan, TGSI_EXEC_DATA_FLOAT);
2118 micro_mul(&product, &arg[chan], &arg[chan]);
2119 micro_add(&scale, &scale, &product);
2120 }
2121
2122 micro_rsq(&scale, &scale);
2123
2124 for (chan = CHAN_X; chan <= CHAN_W; chan++) {
2125 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2126 micro_mul(&arg[chan], &arg[chan], &scale);
2127 store_dest(mach, &arg[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2128 }
2129 }
2130 }
2131
2132 static void
2133 exec_nrm3(struct tgsi_exec_machine *mach,
2134 const struct tgsi_full_instruction *inst)
2135 {
2136 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XYZ) {
2137 unsigned int chan;
2138 union tgsi_exec_channel arg[3];
2139 union tgsi_exec_channel scale;
2140
2141 fetch_source(mach, &arg[0], &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_FLOAT);
2142 micro_mul(&scale, &arg[0], &arg[0]);
2143
2144 for (chan = CHAN_Y; chan <= CHAN_Z; chan++) {
2145 union tgsi_exec_channel product;
2146
2147 fetch_source(mach, &arg[chan], &inst->Src[0], chan, TGSI_EXEC_DATA_FLOAT);
2148 micro_mul(&product, &arg[chan], &arg[chan]);
2149 micro_add(&scale, &scale, &product);
2150 }
2151
2152 micro_rsq(&scale, &scale);
2153
2154 for (chan = CHAN_X; chan <= CHAN_Z; chan++) {
2155 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2156 micro_mul(&arg[chan], &arg[chan], &scale);
2157 store_dest(mach, &arg[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2158 }
2159 }
2160 }
2161
2162 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
2163 store_dest(mach, &OneVec, &inst->Dst[0], inst, CHAN_W, TGSI_EXEC_DATA_FLOAT);
2164 }
2165 }
2166
2167 static void
2168 exec_break(struct tgsi_exec_machine *mach)
2169 {
2170 if (mach->BreakType == TGSI_EXEC_BREAK_INSIDE_LOOP) {
2171 /* turn off loop channels for each enabled exec channel */
2172 mach->LoopMask &= ~mach->ExecMask;
2173 /* Todo: if mach->LoopMask == 0, jump to end of loop */
2174 UPDATE_EXEC_MASK(mach);
2175 } else {
2176 assert(mach->BreakType == TGSI_EXEC_BREAK_INSIDE_SWITCH);
2177
2178 mach->Switch.mask = 0x0;
2179
2180 UPDATE_EXEC_MASK(mach);
2181 }
2182 }
2183
2184 static void
2185 exec_switch(struct tgsi_exec_machine *mach,
2186 const struct tgsi_full_instruction *inst)
2187 {
2188 assert(mach->SwitchStackTop < TGSI_EXEC_MAX_SWITCH_NESTING);
2189 assert(mach->BreakStackTop < TGSI_EXEC_MAX_BREAK_STACK);
2190
2191 mach->SwitchStack[mach->SwitchStackTop++] = mach->Switch;
2192 fetch_source(mach, &mach->Switch.selector, &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_UINT);
2193 mach->Switch.mask = 0x0;
2194 mach->Switch.defaultMask = 0x0;
2195
2196 mach->BreakStack[mach->BreakStackTop++] = mach->BreakType;
2197 mach->BreakType = TGSI_EXEC_BREAK_INSIDE_SWITCH;
2198
2199 UPDATE_EXEC_MASK(mach);
2200 }
2201
2202 static void
2203 exec_case(struct tgsi_exec_machine *mach,
2204 const struct tgsi_full_instruction *inst)
2205 {
2206 uint prevMask = mach->SwitchStack[mach->SwitchStackTop - 1].mask;
2207 union tgsi_exec_channel src;
2208 uint mask = 0;
2209
2210 fetch_source(mach, &src, &inst->Src[0], CHAN_X, TGSI_EXEC_DATA_UINT);
2211
2212 if (mach->Switch.selector.u[0] == src.u[0]) {
2213 mask |= 0x1;
2214 }
2215 if (mach->Switch.selector.u[1] == src.u[1]) {
2216 mask |= 0x2;
2217 }
2218 if (mach->Switch.selector.u[2] == src.u[2]) {
2219 mask |= 0x4;
2220 }
2221 if (mach->Switch.selector.u[3] == src.u[3]) {
2222 mask |= 0x8;
2223 }
2224
2225 mach->Switch.defaultMask |= mask;
2226
2227 mach->Switch.mask |= mask & prevMask;
2228
2229 UPDATE_EXEC_MASK(mach);
2230 }
2231
2232 static void
2233 exec_default(struct tgsi_exec_machine *mach)
2234 {
2235 uint prevMask = mach->SwitchStack[mach->SwitchStackTop - 1].mask;
2236
2237 mach->Switch.mask |= ~mach->Switch.defaultMask & prevMask;
2238
2239 UPDATE_EXEC_MASK(mach);
2240 }
2241
2242 static void
2243 exec_endswitch(struct tgsi_exec_machine *mach)
2244 {
2245 mach->Switch = mach->SwitchStack[--mach->SwitchStackTop];
2246 mach->BreakType = mach->BreakStack[--mach->BreakStackTop];
2247
2248 UPDATE_EXEC_MASK(mach);
2249 }
2250
2251 static void
2252 micro_i2f(union tgsi_exec_channel *dst,
2253 const union tgsi_exec_channel *src)
2254 {
2255 dst->f[0] = (float)src->i[0];
2256 dst->f[1] = (float)src->i[1];
2257 dst->f[2] = (float)src->i[2];
2258 dst->f[3] = (float)src->i[3];
2259 }
2260
2261 static void
2262 micro_not(union tgsi_exec_channel *dst,
2263 const union tgsi_exec_channel *src)
2264 {
2265 dst->u[0] = ~src->u[0];
2266 dst->u[1] = ~src->u[1];
2267 dst->u[2] = ~src->u[2];
2268 dst->u[3] = ~src->u[3];
2269 }
2270
2271 static void
2272 micro_shl(union tgsi_exec_channel *dst,
2273 const union tgsi_exec_channel *src0,
2274 const union tgsi_exec_channel *src1)
2275 {
2276 dst->u[0] = src0->u[0] << src1->u[0];
2277 dst->u[1] = src0->u[1] << src1->u[1];
2278 dst->u[2] = src0->u[2] << src1->u[2];
2279 dst->u[3] = src0->u[3] << src1->u[3];
2280 }
2281
2282 static void
2283 micro_and(union tgsi_exec_channel *dst,
2284 const union tgsi_exec_channel *src0,
2285 const union tgsi_exec_channel *src1)
2286 {
2287 dst->u[0] = src0->u[0] & src1->u[0];
2288 dst->u[1] = src0->u[1] & src1->u[1];
2289 dst->u[2] = src0->u[2] & src1->u[2];
2290 dst->u[3] = src0->u[3] & src1->u[3];
2291 }
2292
2293 static void
2294 micro_or(union tgsi_exec_channel *dst,
2295 const union tgsi_exec_channel *src0,
2296 const union tgsi_exec_channel *src1)
2297 {
2298 dst->u[0] = src0->u[0] | src1->u[0];
2299 dst->u[1] = src0->u[1] | src1->u[1];
2300 dst->u[2] = src0->u[2] | src1->u[2];
2301 dst->u[3] = src0->u[3] | src1->u[3];
2302 }
2303
2304 static void
2305 micro_xor(union tgsi_exec_channel *dst,
2306 const union tgsi_exec_channel *src0,
2307 const union tgsi_exec_channel *src1)
2308 {
2309 dst->u[0] = src0->u[0] ^ src1->u[0];
2310 dst->u[1] = src0->u[1] ^ src1->u[1];
2311 dst->u[2] = src0->u[2] ^ src1->u[2];
2312 dst->u[3] = src0->u[3] ^ src1->u[3];
2313 }
2314
2315 static void
2316 micro_f2i(union tgsi_exec_channel *dst,
2317 const union tgsi_exec_channel *src)
2318 {
2319 dst->i[0] = (int)src->f[0];
2320 dst->i[1] = (int)src->f[1];
2321 dst->i[2] = (int)src->f[2];
2322 dst->i[3] = (int)src->f[3];
2323 }
2324
2325 static void
2326 micro_idiv(union tgsi_exec_channel *dst,
2327 const union tgsi_exec_channel *src0,
2328 const union tgsi_exec_channel *src1)
2329 {
2330 dst->i[0] = src0->i[0] / src1->i[0];
2331 dst->i[1] = src0->i[1] / src1->i[1];
2332 dst->i[2] = src0->i[2] / src1->i[2];
2333 dst->i[3] = src0->i[3] / src1->i[3];
2334 }
2335
2336 static void
2337 micro_imax(union tgsi_exec_channel *dst,
2338 const union tgsi_exec_channel *src0,
2339 const union tgsi_exec_channel *src1)
2340 {
2341 dst->i[0] = src0->i[0] > src1->i[0] ? src0->i[0] : src1->i[0];
2342 dst->i[1] = src0->i[1] > src1->i[1] ? src0->i[1] : src1->i[1];
2343 dst->i[2] = src0->i[2] > src1->i[2] ? src0->i[2] : src1->i[2];
2344 dst->i[3] = src0->i[3] > src1->i[3] ? src0->i[3] : src1->i[3];
2345 }
2346
2347 static void
2348 micro_imin(union tgsi_exec_channel *dst,
2349 const union tgsi_exec_channel *src0,
2350 const union tgsi_exec_channel *src1)
2351 {
2352 dst->i[0] = src0->i[0] < src1->i[0] ? src0->i[0] : src1->i[0];
2353 dst->i[1] = src0->i[1] < src1->i[1] ? src0->i[1] : src1->i[1];
2354 dst->i[2] = src0->i[2] < src1->i[2] ? src0->i[2] : src1->i[2];
2355 dst->i[3] = src0->i[3] < src1->i[3] ? src0->i[3] : src1->i[3];
2356 }
2357
2358 static void
2359 micro_isge(union tgsi_exec_channel *dst,
2360 const union tgsi_exec_channel *src0,
2361 const union tgsi_exec_channel *src1)
2362 {
2363 dst->i[0] = src0->i[0] >= src1->i[0] ? -1 : 0;
2364 dst->i[1] = src0->i[1] >= src1->i[1] ? -1 : 0;
2365 dst->i[2] = src0->i[2] >= src1->i[2] ? -1 : 0;
2366 dst->i[3] = src0->i[3] >= src1->i[3] ? -1 : 0;
2367 }
2368
2369 static void
2370 micro_ishr(union tgsi_exec_channel *dst,
2371 const union tgsi_exec_channel *src0,
2372 const union tgsi_exec_channel *src1)
2373 {
2374 dst->i[0] = src0->i[0] >> src1->i[0];
2375 dst->i[1] = src0->i[1] >> src1->i[1];
2376 dst->i[2] = src0->i[2] >> src1->i[2];
2377 dst->i[3] = src0->i[3] >> src1->i[3];
2378 }
2379
2380 static void
2381 micro_islt(union tgsi_exec_channel *dst,
2382 const union tgsi_exec_channel *src0,
2383 const union tgsi_exec_channel *src1)
2384 {
2385 dst->i[0] = src0->i[0] < src1->i[0] ? -1 : 0;
2386 dst->i[1] = src0->i[1] < src1->i[1] ? -1 : 0;
2387 dst->i[2] = src0->i[2] < src1->i[2] ? -1 : 0;
2388 dst->i[3] = src0->i[3] < src1->i[3] ? -1 : 0;
2389 }
2390
2391 static void
2392 micro_f2u(union tgsi_exec_channel *dst,
2393 const union tgsi_exec_channel *src)
2394 {
2395 dst->u[0] = (uint)src->f[0];
2396 dst->u[1] = (uint)src->f[1];
2397 dst->u[2] = (uint)src->f[2];
2398 dst->u[3] = (uint)src->f[3];
2399 }
2400
2401 static void
2402 micro_u2f(union tgsi_exec_channel *dst,
2403 const union tgsi_exec_channel *src)
2404 {
2405 dst->f[0] = (float)src->u[0];
2406 dst->f[1] = (float)src->u[1];
2407 dst->f[2] = (float)src->u[2];
2408 dst->f[3] = (float)src->u[3];
2409 }
2410
2411 static void
2412 micro_uadd(union tgsi_exec_channel *dst,
2413 const union tgsi_exec_channel *src0,
2414 const union tgsi_exec_channel *src1)
2415 {
2416 dst->u[0] = src0->u[0] + src1->u[0];
2417 dst->u[1] = src0->u[1] + src1->u[1];
2418 dst->u[2] = src0->u[2] + src1->u[2];
2419 dst->u[3] = src0->u[3] + src1->u[3];
2420 }
2421
2422 static void
2423 micro_udiv(union tgsi_exec_channel *dst,
2424 const union tgsi_exec_channel *src0,
2425 const union tgsi_exec_channel *src1)
2426 {
2427 dst->u[0] = src0->u[0] / src1->u[0];
2428 dst->u[1] = src0->u[1] / src1->u[1];
2429 dst->u[2] = src0->u[2] / src1->u[2];
2430 dst->u[3] = src0->u[3] / src1->u[3];
2431 }
2432
2433 static void
2434 micro_umad(union tgsi_exec_channel *dst,
2435 const union tgsi_exec_channel *src0,
2436 const union tgsi_exec_channel *src1,
2437 const union tgsi_exec_channel *src2)
2438 {
2439 dst->u[0] = src0->u[0] * src1->u[0] + src2->u[0];
2440 dst->u[1] = src0->u[1] * src1->u[1] + src2->u[1];
2441 dst->u[2] = src0->u[2] * src1->u[2] + src2->u[2];
2442 dst->u[3] = src0->u[3] * src1->u[3] + src2->u[3];
2443 }
2444
2445 static void
2446 micro_umax(union tgsi_exec_channel *dst,
2447 const union tgsi_exec_channel *src0,
2448 const union tgsi_exec_channel *src1)
2449 {
2450 dst->u[0] = src0->u[0] > src1->u[0] ? src0->u[0] : src1->u[0];
2451 dst->u[1] = src0->u[1] > src1->u[1] ? src0->u[1] : src1->u[1];
2452 dst->u[2] = src0->u[2] > src1->u[2] ? src0->u[2] : src1->u[2];
2453 dst->u[3] = src0->u[3] > src1->u[3] ? src0->u[3] : src1->u[3];
2454 }
2455
2456 static void
2457 micro_umin(union tgsi_exec_channel *dst,
2458 const union tgsi_exec_channel *src0,
2459 const union tgsi_exec_channel *src1)
2460 {
2461 dst->u[0] = src0->u[0] < src1->u[0] ? src0->u[0] : src1->u[0];
2462 dst->u[1] = src0->u[1] < src1->u[1] ? src0->u[1] : src1->u[1];
2463 dst->u[2] = src0->u[2] < src1->u[2] ? src0->u[2] : src1->u[2];
2464 dst->u[3] = src0->u[3] < src1->u[3] ? src0->u[3] : src1->u[3];
2465 }
2466
2467 static void
2468 micro_umod(union tgsi_exec_channel *dst,
2469 const union tgsi_exec_channel *src0,
2470 const union tgsi_exec_channel *src1)
2471 {
2472 dst->u[0] = src0->u[0] % src1->u[0];
2473 dst->u[1] = src0->u[1] % src1->u[1];
2474 dst->u[2] = src0->u[2] % src1->u[2];
2475 dst->u[3] = src0->u[3] % src1->u[3];
2476 }
2477
2478 static void
2479 micro_umul(union tgsi_exec_channel *dst,
2480 const union tgsi_exec_channel *src0,
2481 const union tgsi_exec_channel *src1)
2482 {
2483 dst->u[0] = src0->u[0] * src1->u[0];
2484 dst->u[1] = src0->u[1] * src1->u[1];
2485 dst->u[2] = src0->u[2] * src1->u[2];
2486 dst->u[3] = src0->u[3] * src1->u[3];
2487 }
2488
2489 static void
2490 micro_useq(union tgsi_exec_channel *dst,
2491 const union tgsi_exec_channel *src0,
2492 const union tgsi_exec_channel *src1)
2493 {
2494 dst->u[0] = src0->u[0] == src1->u[0] ? ~0 : 0;
2495 dst->u[1] = src0->u[1] == src1->u[1] ? ~0 : 0;
2496 dst->u[2] = src0->u[2] == src1->u[2] ? ~0 : 0;
2497 dst->u[3] = src0->u[3] == src1->u[3] ? ~0 : 0;
2498 }
2499
2500 static void
2501 micro_usge(union tgsi_exec_channel *dst,
2502 const union tgsi_exec_channel *src0,
2503 const union tgsi_exec_channel *src1)
2504 {
2505 dst->u[0] = src0->u[0] >= src1->u[0] ? ~0 : 0;
2506 dst->u[1] = src0->u[1] >= src1->u[1] ? ~0 : 0;
2507 dst->u[2] = src0->u[2] >= src1->u[2] ? ~0 : 0;
2508 dst->u[3] = src0->u[3] >= src1->u[3] ? ~0 : 0;
2509 }
2510
2511 static void
2512 micro_ushr(union tgsi_exec_channel *dst,
2513 const union tgsi_exec_channel *src0,
2514 const union tgsi_exec_channel *src1)
2515 {
2516 dst->u[0] = src0->u[0] >> src1->u[0];
2517 dst->u[1] = src0->u[1] >> src1->u[1];
2518 dst->u[2] = src0->u[2] >> src1->u[2];
2519 dst->u[3] = src0->u[3] >> src1->u[3];
2520 }
2521
2522 static void
2523 micro_uslt(union tgsi_exec_channel *dst,
2524 const union tgsi_exec_channel *src0,
2525 const union tgsi_exec_channel *src1)
2526 {
2527 dst->u[0] = src0->u[0] < src1->u[0] ? ~0 : 0;
2528 dst->u[1] = src0->u[1] < src1->u[1] ? ~0 : 0;
2529 dst->u[2] = src0->u[2] < src1->u[2] ? ~0 : 0;
2530 dst->u[3] = src0->u[3] < src1->u[3] ? ~0 : 0;
2531 }
2532
2533 static void
2534 micro_usne(union tgsi_exec_channel *dst,
2535 const union tgsi_exec_channel *src0,
2536 const union tgsi_exec_channel *src1)
2537 {
2538 dst->u[0] = src0->u[0] != src1->u[0] ? ~0 : 0;
2539 dst->u[1] = src0->u[1] != src1->u[1] ? ~0 : 0;
2540 dst->u[2] = src0->u[2] != src1->u[2] ? ~0 : 0;
2541 dst->u[3] = src0->u[3] != src1->u[3] ? ~0 : 0;
2542 }
2543
2544 static void
2545 exec_instruction(
2546 struct tgsi_exec_machine *mach,
2547 const struct tgsi_full_instruction *inst,
2548 int *pc )
2549 {
2550 uint chan_index;
2551 union tgsi_exec_channel r[10];
2552 union tgsi_exec_channel d[8];
2553
2554 (*pc)++;
2555
2556 switch (inst->Instruction.Opcode) {
2557 case TGSI_OPCODE_ARL:
2558 exec_vector_unary(mach, inst, micro_arl, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_FLOAT);
2559 break;
2560
2561 case TGSI_OPCODE_MOV:
2562 exec_vector_unary(mach, inst, micro_mov, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
2563 break;
2564
2565 case TGSI_OPCODE_LIT:
2566 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y ) || IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
2567 FETCH( &r[0], 0, CHAN_X );
2568 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
2569 micro_max(&d[CHAN_Y], &r[0], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C]);
2570 }
2571
2572 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
2573 FETCH( &r[1], 0, CHAN_Y );
2574 micro_max( &r[1], &r[1], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C] );
2575
2576 FETCH( &r[2], 0, CHAN_W );
2577 micro_min( &r[2], &r[2], &mach->Temps[TEMP_128_I].xyzw[TEMP_128_C] );
2578 micro_max( &r[2], &r[2], &mach->Temps[TEMP_M128_I].xyzw[TEMP_M128_C] );
2579 micro_pow( &r[1], &r[1], &r[2] );
2580 micro_lt(&d[CHAN_Z], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], &r[0], &r[1], &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C]);
2581 }
2582
2583 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y)) {
2584 STORE(&d[CHAN_Y], 0, CHAN_Y);
2585 }
2586 if (IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
2587 STORE(&d[CHAN_Z], 0, CHAN_Z);
2588 }
2589 }
2590 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
2591 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_X );
2592 }
2593 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
2594 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
2595 }
2596 break;
2597
2598 case TGSI_OPCODE_RCP:
2599 exec_scalar_unary(mach, inst, micro_rcp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2600 break;
2601
2602 case TGSI_OPCODE_RSQ:
2603 exec_scalar_unary(mach, inst, micro_rsq, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2604 break;
2605
2606 case TGSI_OPCODE_EXP:
2607 FETCH( &r[0], 0, CHAN_X );
2608 micro_flr( &r[1], &r[0] ); /* r1 = floor(r0) */
2609 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
2610 micro_exp2( &r[2], &r[1] ); /* r2 = 2 ^ r1 */
2611 STORE( &r[2], 0, CHAN_X ); /* store r2 */
2612 }
2613 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
2614 micro_sub( &r[2], &r[0], &r[1] ); /* r2 = r0 - r1 */
2615 STORE( &r[2], 0, CHAN_Y ); /* store r2 */
2616 }
2617 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
2618 micro_exp2( &r[2], &r[0] ); /* r2 = 2 ^ r0 */
2619 STORE( &r[2], 0, CHAN_Z ); /* store r2 */
2620 }
2621 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
2622 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
2623 }
2624 break;
2625
2626 case TGSI_OPCODE_LOG:
2627 FETCH( &r[0], 0, CHAN_X );
2628 micro_abs( &r[2], &r[0] ); /* r2 = abs(r0) */
2629 micro_lg2( &r[1], &r[2] ); /* r1 = lg2(r2) */
2630 micro_flr( &r[0], &r[1] ); /* r0 = floor(r1) */
2631 if (IS_CHANNEL_ENABLED( *inst, CHAN_X )) {
2632 STORE( &r[0], 0, CHAN_X );
2633 }
2634 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
2635 micro_exp2( &r[0], &r[0] ); /* r0 = 2 ^ r0 */
2636 micro_div( &r[0], &r[2], &r[0] ); /* r0 = r2 / r0 */
2637 STORE( &r[0], 0, CHAN_Y );
2638 }
2639 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
2640 STORE( &r[1], 0, CHAN_Z );
2641 }
2642 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
2643 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
2644 }
2645 break;
2646
2647 case TGSI_OPCODE_MUL:
2648 exec_vector_binary(mach, inst, micro_mul, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2649 break;
2650
2651 case TGSI_OPCODE_ADD:
2652 exec_vector_binary(mach, inst, micro_add, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2653 break;
2654
2655 case TGSI_OPCODE_DP3:
2656 exec_dp3(mach, inst);
2657 break;
2658
2659 case TGSI_OPCODE_DP4:
2660 exec_dp4(mach, inst);
2661 break;
2662
2663 case TGSI_OPCODE_DST:
2664 if (IS_CHANNEL_ENABLED( *inst, CHAN_Y )) {
2665 FETCH( &r[0], 0, CHAN_Y );
2666 FETCH( &r[1], 1, CHAN_Y);
2667 micro_mul(&d[CHAN_Y], &r[0], &r[1]);
2668 }
2669 if (IS_CHANNEL_ENABLED( *inst, CHAN_Z )) {
2670 FETCH(&d[CHAN_Z], 0, CHAN_Z);
2671 }
2672 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
2673 FETCH(&d[CHAN_W], 1, CHAN_W);
2674 }
2675
2676 if (IS_CHANNEL_ENABLED(*inst, CHAN_X)) {
2677 STORE(&mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_X);
2678 }
2679 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y)) {
2680 STORE(&d[CHAN_Y], 0, CHAN_Y);
2681 }
2682 if (IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
2683 STORE(&d[CHAN_Z], 0, CHAN_Z);
2684 }
2685 if (IS_CHANNEL_ENABLED(*inst, CHAN_W)) {
2686 STORE(&d[CHAN_W], 0, CHAN_W);
2687 }
2688 break;
2689
2690 case TGSI_OPCODE_MIN:
2691 exec_vector_binary(mach, inst, micro_min, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2692 break;
2693
2694 case TGSI_OPCODE_MAX:
2695 exec_vector_binary(mach, inst, micro_max, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2696 break;
2697
2698 case TGSI_OPCODE_SLT:
2699 exec_vector_binary(mach, inst, micro_slt, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2700 break;
2701
2702 case TGSI_OPCODE_SGE:
2703 exec_vector_binary(mach, inst, micro_sge, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2704 break;
2705
2706 case TGSI_OPCODE_MAD:
2707 exec_vector_trinary(mach, inst, micro_mad, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2708 break;
2709
2710 case TGSI_OPCODE_SUB:
2711 exec_vector_binary(mach, inst, micro_sub, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2712 break;
2713
2714 case TGSI_OPCODE_LRP:
2715 exec_vector_trinary(mach, inst, micro_lrp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2716 break;
2717
2718 case TGSI_OPCODE_CND:
2719 exec_vector_trinary(mach, inst, micro_cnd, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2720 break;
2721
2722 case TGSI_OPCODE_DP2A:
2723 exec_dp2a(mach, inst);
2724 break;
2725
2726 case TGSI_OPCODE_FRC:
2727 exec_vector_unary(mach, inst, micro_frc, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2728 break;
2729
2730 case TGSI_OPCODE_CLAMP:
2731 exec_vector_trinary(mach, inst, micro_clamp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2732 break;
2733
2734 case TGSI_OPCODE_FLR:
2735 exec_vector_unary(mach, inst, micro_flr, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2736 break;
2737
2738 case TGSI_OPCODE_ROUND:
2739 exec_vector_unary(mach, inst, micro_rnd, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2740 break;
2741
2742 case TGSI_OPCODE_EX2:
2743 exec_scalar_unary(mach, inst, micro_exp2, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2744 break;
2745
2746 case TGSI_OPCODE_LG2:
2747 exec_scalar_unary(mach, inst, micro_lg2, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2748 break;
2749
2750 case TGSI_OPCODE_POW:
2751 FETCH(&r[0], 0, CHAN_X);
2752 FETCH(&r[1], 1, CHAN_X);
2753
2754 micro_pow( &r[0], &r[0], &r[1] );
2755
2756 FOR_EACH_ENABLED_CHANNEL( *inst, chan_index ) {
2757 STORE( &r[0], 0, chan_index );
2758 }
2759 break;
2760
2761 case TGSI_OPCODE_XPD:
2762 FETCH(&r[0], 0, CHAN_Y);
2763 FETCH(&r[1], 1, CHAN_Z);
2764
2765 micro_mul( &r[2], &r[0], &r[1] );
2766
2767 FETCH(&r[3], 0, CHAN_Z);
2768 FETCH(&r[4], 1, CHAN_Y);
2769
2770 micro_mul( &r[5], &r[3], &r[4] );
2771 micro_sub(&d[CHAN_X], &r[2], &r[5]);
2772
2773 FETCH(&r[2], 1, CHAN_X);
2774
2775 micro_mul( &r[3], &r[3], &r[2] );
2776
2777 FETCH(&r[5], 0, CHAN_X);
2778
2779 micro_mul( &r[1], &r[1], &r[5] );
2780 micro_sub(&d[CHAN_Y], &r[3], &r[1]);
2781
2782 micro_mul( &r[5], &r[5], &r[4] );
2783 micro_mul( &r[0], &r[0], &r[2] );
2784 micro_sub(&d[CHAN_Z], &r[5], &r[0]);
2785
2786 if (IS_CHANNEL_ENABLED(*inst, CHAN_X)) {
2787 STORE(&d[CHAN_X], 0, CHAN_X);
2788 }
2789 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y)) {
2790 STORE(&d[CHAN_Y], 0, CHAN_Y);
2791 }
2792 if (IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
2793 STORE(&d[CHAN_Z], 0, CHAN_Z);
2794 }
2795 if (IS_CHANNEL_ENABLED( *inst, CHAN_W )) {
2796 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
2797 }
2798 break;
2799
2800 case TGSI_OPCODE_ABS:
2801 exec_vector_unary(mach, inst, micro_abs, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2802 break;
2803
2804 case TGSI_OPCODE_RCC:
2805 FETCH(&r[0], 0, CHAN_X);
2806 micro_div(&r[0], &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], &r[0]);
2807 micro_float_clamp(&r[0], &r[0]);
2808 FOR_EACH_ENABLED_CHANNEL(*inst, chan_index) {
2809 STORE(&r[0], 0, chan_index);
2810 }
2811 break;
2812
2813 case TGSI_OPCODE_DPH:
2814 exec_dph(mach, inst);
2815 break;
2816
2817 case TGSI_OPCODE_COS:
2818 exec_scalar_unary(mach, inst, micro_cos, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2819 break;
2820
2821 case TGSI_OPCODE_DDX:
2822 exec_vector_unary(mach, inst, micro_ddx, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2823 break;
2824
2825 case TGSI_OPCODE_DDY:
2826 exec_vector_unary(mach, inst, micro_ddy, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2827 break;
2828
2829 case TGSI_OPCODE_KILP:
2830 exec_kilp (mach, inst);
2831 break;
2832
2833 case TGSI_OPCODE_KIL:
2834 exec_kil (mach, inst);
2835 break;
2836
2837 case TGSI_OPCODE_PK2H:
2838 assert (0);
2839 break;
2840
2841 case TGSI_OPCODE_PK2US:
2842 assert (0);
2843 break;
2844
2845 case TGSI_OPCODE_PK4B:
2846 assert (0);
2847 break;
2848
2849 case TGSI_OPCODE_PK4UB:
2850 assert (0);
2851 break;
2852
2853 case TGSI_OPCODE_RFL:
2854 if (IS_CHANNEL_ENABLED(*inst, CHAN_X) ||
2855 IS_CHANNEL_ENABLED(*inst, CHAN_Y) ||
2856 IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
2857 /* r0 = dp3(src0, src0) */
2858 FETCH(&r[2], 0, CHAN_X);
2859 micro_mul(&r[0], &r[2], &r[2]);
2860 FETCH(&r[4], 0, CHAN_Y);
2861 micro_mul(&r[8], &r[4], &r[4]);
2862 micro_add(&r[0], &r[0], &r[8]);
2863 FETCH(&r[6], 0, CHAN_Z);
2864 micro_mul(&r[8], &r[6], &r[6]);
2865 micro_add(&r[0], &r[0], &r[8]);
2866
2867 /* r1 = dp3(src0, src1) */
2868 FETCH(&r[3], 1, CHAN_X);
2869 micro_mul(&r[1], &r[2], &r[3]);
2870 FETCH(&r[5], 1, CHAN_Y);
2871 micro_mul(&r[8], &r[4], &r[5]);
2872 micro_add(&r[1], &r[1], &r[8]);
2873 FETCH(&r[7], 1, CHAN_Z);
2874 micro_mul(&r[8], &r[6], &r[7]);
2875 micro_add(&r[1], &r[1], &r[8]);
2876
2877 /* r1 = 2 * r1 / r0 */
2878 micro_add(&r[1], &r[1], &r[1]);
2879 micro_div(&r[1], &r[1], &r[0]);
2880
2881 if (IS_CHANNEL_ENABLED(*inst, CHAN_X)) {
2882 micro_mul(&r[2], &r[2], &r[1]);
2883 micro_sub(&r[2], &r[2], &r[3]);
2884 STORE(&r[2], 0, CHAN_X);
2885 }
2886 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y)) {
2887 micro_mul(&r[4], &r[4], &r[1]);
2888 micro_sub(&r[4], &r[4], &r[5]);
2889 STORE(&r[4], 0, CHAN_Y);
2890 }
2891 if (IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
2892 micro_mul(&r[6], &r[6], &r[1]);
2893 micro_sub(&r[6], &r[6], &r[7]);
2894 STORE(&r[6], 0, CHAN_Z);
2895 }
2896 }
2897 if (IS_CHANNEL_ENABLED(*inst, CHAN_W)) {
2898 STORE(&mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W);
2899 }
2900 break;
2901
2902 case TGSI_OPCODE_SEQ:
2903 exec_vector_binary(mach, inst, micro_seq, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2904 break;
2905
2906 case TGSI_OPCODE_SFL:
2907 FOR_EACH_ENABLED_CHANNEL(*inst, chan_index) {
2908 STORE(&mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], 0, chan_index);
2909 }
2910 break;
2911
2912 case TGSI_OPCODE_SGT:
2913 exec_vector_binary(mach, inst, micro_sgt, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2914 break;
2915
2916 case TGSI_OPCODE_SIN:
2917 exec_scalar_unary(mach, inst, micro_sin, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2918 break;
2919
2920 case TGSI_OPCODE_SLE:
2921 exec_vector_binary(mach, inst, micro_sle, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2922 break;
2923
2924 case TGSI_OPCODE_SNE:
2925 exec_vector_binary(mach, inst, micro_sne, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
2926 break;
2927
2928 case TGSI_OPCODE_STR:
2929 FOR_EACH_ENABLED_CHANNEL(*inst, chan_index) {
2930 STORE(&mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, chan_index);
2931 }
2932 break;
2933
2934 case TGSI_OPCODE_TEX:
2935 /* simple texture lookup */
2936 /* src[0] = texcoord */
2937 /* src[1] = sampler unit */
2938 exec_tex(mach, inst, TEX_MODIFIER_NONE);
2939 break;
2940
2941 case TGSI_OPCODE_TXB:
2942 /* Texture lookup with lod bias */
2943 /* src[0] = texcoord (src[0].w = LOD bias) */
2944 /* src[1] = sampler unit */
2945 exec_tex(mach, inst, TEX_MODIFIER_LOD_BIAS);
2946 break;
2947
2948 case TGSI_OPCODE_TXD:
2949 /* Texture lookup with explict partial derivatives */
2950 /* src[0] = texcoord */
2951 /* src[1] = d[strq]/dx */
2952 /* src[2] = d[strq]/dy */
2953 /* src[3] = sampler unit */
2954 exec_txd(mach, inst);
2955 break;
2956
2957 case TGSI_OPCODE_TXL:
2958 /* Texture lookup with explit LOD */
2959 /* src[0] = texcoord (src[0].w = LOD) */
2960 /* src[1] = sampler unit */
2961 exec_tex(mach, inst, TEX_MODIFIER_EXPLICIT_LOD);
2962 break;
2963
2964 case TGSI_OPCODE_TXP:
2965 /* Texture lookup with projection */
2966 /* src[0] = texcoord (src[0].w = projection) */
2967 /* src[1] = sampler unit */
2968 exec_tex(mach, inst, TEX_MODIFIER_PROJECTED);
2969 break;
2970
2971 case TGSI_OPCODE_UP2H:
2972 assert (0);
2973 break;
2974
2975 case TGSI_OPCODE_UP2US:
2976 assert (0);
2977 break;
2978
2979 case TGSI_OPCODE_UP4B:
2980 assert (0);
2981 break;
2982
2983 case TGSI_OPCODE_UP4UB:
2984 assert (0);
2985 break;
2986
2987 case TGSI_OPCODE_X2D:
2988 FETCH(&r[0], 1, CHAN_X);
2989 FETCH(&r[1], 1, CHAN_Y);
2990 if (IS_CHANNEL_ENABLED(*inst, CHAN_X) ||
2991 IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
2992 FETCH(&r[2], 2, CHAN_X);
2993 micro_mul(&r[2], &r[2], &r[0]);
2994 FETCH(&r[3], 2, CHAN_Y);
2995 micro_mul(&r[3], &r[3], &r[1]);
2996 micro_add(&r[2], &r[2], &r[3]);
2997 FETCH(&r[3], 0, CHAN_X);
2998 micro_add(&d[CHAN_X], &r[2], &r[3]);
2999
3000 }
3001 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y) ||
3002 IS_CHANNEL_ENABLED(*inst, CHAN_W)) {
3003 FETCH(&r[2], 2, CHAN_Z);
3004 micro_mul(&r[2], &r[2], &r[0]);
3005 FETCH(&r[3], 2, CHAN_W);
3006 micro_mul(&r[3], &r[3], &r[1]);
3007 micro_add(&r[2], &r[2], &r[3]);
3008 FETCH(&r[3], 0, CHAN_Y);
3009 micro_add(&d[CHAN_Y], &r[2], &r[3]);
3010
3011 }
3012 if (IS_CHANNEL_ENABLED(*inst, CHAN_X)) {
3013 STORE(&d[CHAN_X], 0, CHAN_X);
3014 }
3015 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y)) {
3016 STORE(&d[CHAN_Y], 0, CHAN_Y);
3017 }
3018 if (IS_CHANNEL_ENABLED(*inst, CHAN_Z)) {
3019 STORE(&d[CHAN_X], 0, CHAN_Z);
3020 }
3021 if (IS_CHANNEL_ENABLED(*inst, CHAN_W)) {
3022 STORE(&d[CHAN_Y], 0, CHAN_W);
3023 }
3024 break;
3025
3026 case TGSI_OPCODE_ARA:
3027 assert (0);
3028 break;
3029
3030 case TGSI_OPCODE_ARR:
3031 exec_vector_unary(mach, inst, micro_arr, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_FLOAT);
3032 break;
3033
3034 case TGSI_OPCODE_BRA:
3035 assert (0);
3036 break;
3037
3038 case TGSI_OPCODE_CAL:
3039 /* skip the call if no execution channels are enabled */
3040 if (mach->ExecMask) {
3041 /* do the call */
3042
3043 /* First, record the depths of the execution stacks.
3044 * This is important for deeply nested/looped return statements.
3045 * We have to unwind the stacks by the correct amount. For a
3046 * real code generator, we could determine the number of entries
3047 * to pop off each stack with simple static analysis and avoid
3048 * implementing this data structure at run time.
3049 */
3050 mach->CallStack[mach->CallStackTop].CondStackTop = mach->CondStackTop;
3051 mach->CallStack[mach->CallStackTop].LoopStackTop = mach->LoopStackTop;
3052 mach->CallStack[mach->CallStackTop].ContStackTop = mach->ContStackTop;
3053 mach->CallStack[mach->CallStackTop].SwitchStackTop = mach->SwitchStackTop;
3054 mach->CallStack[mach->CallStackTop].BreakStackTop = mach->BreakStackTop;
3055 /* note that PC was already incremented above */
3056 mach->CallStack[mach->CallStackTop].ReturnAddr = *pc;
3057
3058 mach->CallStackTop++;
3059
3060 /* Second, push the Cond, Loop, Cont, Func stacks */
3061 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
3062 assert(mach->LoopStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
3063 assert(mach->ContStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
3064 assert(mach->SwitchStackTop < TGSI_EXEC_MAX_SWITCH_NESTING);
3065 assert(mach->BreakStackTop < TGSI_EXEC_MAX_BREAK_STACK);
3066 assert(mach->FuncStackTop < TGSI_EXEC_MAX_CALL_NESTING);
3067
3068 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
3069 mach->LoopStack[mach->LoopStackTop++] = mach->LoopMask;
3070 mach->ContStack[mach->ContStackTop++] = mach->ContMask;
3071 mach->SwitchStack[mach->SwitchStackTop++] = mach->Switch;
3072 mach->BreakStack[mach->BreakStackTop++] = mach->BreakType;
3073 mach->FuncStack[mach->FuncStackTop++] = mach->FuncMask;
3074
3075 /* Finally, jump to the subroutine */
3076 *pc = inst->Label.Label;
3077 }
3078 break;
3079
3080 case TGSI_OPCODE_RET:
3081 mach->FuncMask &= ~mach->ExecMask;
3082 UPDATE_EXEC_MASK(mach);
3083
3084 if (mach->FuncMask == 0x0) {
3085 /* really return now (otherwise, keep executing */
3086
3087 if (mach->CallStackTop == 0) {
3088 /* returning from main() */
3089 *pc = -1;
3090 return;
3091 }
3092
3093 assert(mach->CallStackTop > 0);
3094 mach->CallStackTop--;
3095
3096 mach->CondStackTop = mach->CallStack[mach->CallStackTop].CondStackTop;
3097 mach->CondMask = mach->CondStack[mach->CondStackTop];
3098
3099 mach->LoopStackTop = mach->CallStack[mach->CallStackTop].LoopStackTop;
3100 mach->LoopMask = mach->LoopStack[mach->LoopStackTop];
3101
3102 mach->ContStackTop = mach->CallStack[mach->CallStackTop].ContStackTop;
3103 mach->ContMask = mach->ContStack[mach->ContStackTop];
3104
3105 mach->SwitchStackTop = mach->CallStack[mach->CallStackTop].SwitchStackTop;
3106 mach->Switch = mach->SwitchStack[mach->SwitchStackTop];
3107
3108 mach->BreakStackTop = mach->CallStack[mach->CallStackTop].BreakStackTop;
3109 mach->BreakType = mach->BreakStack[mach->BreakStackTop];
3110
3111 assert(mach->FuncStackTop > 0);
3112 mach->FuncMask = mach->FuncStack[--mach->FuncStackTop];
3113
3114 *pc = mach->CallStack[mach->CallStackTop].ReturnAddr;
3115
3116 UPDATE_EXEC_MASK(mach);
3117 }
3118 break;
3119
3120 case TGSI_OPCODE_SSG:
3121 exec_vector_unary(mach, inst, micro_sgn, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
3122 break;
3123
3124 case TGSI_OPCODE_CMP:
3125 exec_vector_trinary(mach, inst, micro_cmp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
3126 break;
3127
3128 case TGSI_OPCODE_SCS:
3129 if( IS_CHANNEL_ENABLED( *inst, CHAN_X ) || IS_CHANNEL_ENABLED( *inst, CHAN_Y ) ) {
3130 FETCH( &r[0], 0, CHAN_X );
3131 if (IS_CHANNEL_ENABLED(*inst, CHAN_X)) {
3132 micro_cos(&r[1], &r[0]);
3133 STORE(&r[1], 0, CHAN_X);
3134 }
3135 if (IS_CHANNEL_ENABLED(*inst, CHAN_Y)) {
3136 micro_sin(&r[1], &r[0]);
3137 STORE(&r[1], 0, CHAN_Y);
3138 }
3139 }
3140 if( IS_CHANNEL_ENABLED( *inst, CHAN_Z ) ) {
3141 STORE( &mach->Temps[TEMP_0_I].xyzw[TEMP_0_C], 0, CHAN_Z );
3142 }
3143 if( IS_CHANNEL_ENABLED( *inst, CHAN_W ) ) {
3144 STORE( &mach->Temps[TEMP_1_I].xyzw[TEMP_1_C], 0, CHAN_W );
3145 }
3146 break;
3147
3148 case TGSI_OPCODE_NRM:
3149 exec_nrm3(mach, inst);
3150 break;
3151
3152 case TGSI_OPCODE_NRM4:
3153 exec_nrm4(mach, inst);
3154 break;
3155
3156 case TGSI_OPCODE_DIV:
3157 exec_vector_binary(mach, inst, micro_div, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
3158 break;
3159
3160 case TGSI_OPCODE_DP2:
3161 exec_dp2(mach, inst);
3162 break;
3163
3164 case TGSI_OPCODE_IF:
3165 /* push CondMask */
3166 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
3167 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
3168 FETCH( &r[0], 0, CHAN_X );
3169 /* update CondMask */
3170 if( ! r[0].u[0] ) {
3171 mach->CondMask &= ~0x1;
3172 }
3173 if( ! r[0].u[1] ) {
3174 mach->CondMask &= ~0x2;
3175 }
3176 if( ! r[0].u[2] ) {
3177 mach->CondMask &= ~0x4;
3178 }
3179 if( ! r[0].u[3] ) {
3180 mach->CondMask &= ~0x8;
3181 }
3182 UPDATE_EXEC_MASK(mach);
3183 /* Todo: If CondMask==0, jump to ELSE */
3184 break;
3185
3186 case TGSI_OPCODE_ELSE:
3187 /* invert CondMask wrt previous mask */
3188 {
3189 uint prevMask;
3190 assert(mach->CondStackTop > 0);
3191 prevMask = mach->CondStack[mach->CondStackTop - 1];
3192 mach->CondMask = ~mach->CondMask & prevMask;
3193 UPDATE_EXEC_MASK(mach);
3194 /* Todo: If CondMask==0, jump to ENDIF */
3195 }
3196 break;
3197
3198 case TGSI_OPCODE_ENDIF:
3199 /* pop CondMask */
3200 assert(mach->CondStackTop > 0);
3201 mach->CondMask = mach->CondStack[--mach->CondStackTop];
3202 UPDATE_EXEC_MASK(mach);
3203 break;
3204
3205 case TGSI_OPCODE_END:
3206 /* make sure we end primitives which haven't
3207 * been explicitly emitted */
3208 conditional_emit_primitive(mach);
3209 /* halt execution */
3210 *pc = -1;
3211 break;
3212
3213 case TGSI_OPCODE_PUSHA:
3214 assert (0);
3215 break;
3216
3217 case TGSI_OPCODE_POPA:
3218 assert (0);
3219 break;
3220
3221 case TGSI_OPCODE_CEIL:
3222 exec_vector_unary(mach, inst, micro_ceil, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
3223 break;
3224
3225 case TGSI_OPCODE_I2F:
3226 exec_vector_unary(mach, inst, micro_i2f, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_INT);
3227 break;
3228
3229 case TGSI_OPCODE_NOT:
3230 exec_vector_unary(mach, inst, micro_not, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3231 break;
3232
3233 case TGSI_OPCODE_TRUNC:
3234 exec_vector_unary(mach, inst, micro_trunc, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
3235 break;
3236
3237 case TGSI_OPCODE_SHL:
3238 exec_vector_binary(mach, inst, micro_shl, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3239 break;
3240
3241 case TGSI_OPCODE_AND:
3242 exec_vector_binary(mach, inst, micro_and, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3243 break;
3244
3245 case TGSI_OPCODE_OR:
3246 exec_vector_binary(mach, inst, micro_or, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3247 break;
3248
3249 case TGSI_OPCODE_MOD:
3250 assert (0);
3251 break;
3252
3253 case TGSI_OPCODE_XOR:
3254 exec_vector_binary(mach, inst, micro_xor, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3255 break;
3256
3257 case TGSI_OPCODE_SAD:
3258 assert (0);
3259 break;
3260
3261 case TGSI_OPCODE_TXF:
3262 assert (0);
3263 break;
3264
3265 case TGSI_OPCODE_TXQ:
3266 assert (0);
3267 break;
3268
3269 case TGSI_OPCODE_EMIT:
3270 emit_vertex(mach);
3271 break;
3272
3273 case TGSI_OPCODE_ENDPRIM:
3274 emit_primitive(mach);
3275 break;
3276
3277 case TGSI_OPCODE_BGNLOOP:
3278 /* push LoopMask and ContMasks */
3279 assert(mach->LoopStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
3280 assert(mach->ContStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
3281 assert(mach->LoopLabelStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
3282 assert(mach->BreakStackTop < TGSI_EXEC_MAX_BREAK_STACK);
3283
3284 mach->LoopStack[mach->LoopStackTop++] = mach->LoopMask;
3285 mach->ContStack[mach->ContStackTop++] = mach->ContMask;
3286 mach->LoopLabelStack[mach->LoopLabelStackTop++] = *pc - 1;
3287 mach->BreakStack[mach->BreakStackTop++] = mach->BreakType;
3288 mach->BreakType = TGSI_EXEC_BREAK_INSIDE_LOOP;
3289 break;
3290
3291 case TGSI_OPCODE_ENDLOOP:
3292 /* Restore ContMask, but don't pop */
3293 assert(mach->ContStackTop > 0);
3294 mach->ContMask = mach->ContStack[mach->ContStackTop - 1];
3295 UPDATE_EXEC_MASK(mach);
3296 if (mach->ExecMask) {
3297 /* repeat loop: jump to instruction just past BGNLOOP */
3298 assert(mach->LoopLabelStackTop > 0);
3299 *pc = mach->LoopLabelStack[mach->LoopLabelStackTop - 1] + 1;
3300 }
3301 else {
3302 /* exit loop: pop LoopMask */
3303 assert(mach->LoopStackTop > 0);
3304 mach->LoopMask = mach->LoopStack[--mach->LoopStackTop];
3305 /* pop ContMask */
3306 assert(mach->ContStackTop > 0);
3307 mach->ContMask = mach->ContStack[--mach->ContStackTop];
3308 assert(mach->LoopLabelStackTop > 0);
3309 --mach->LoopLabelStackTop;
3310
3311 mach->BreakType = mach->BreakStack[--mach->BreakStackTop];
3312 }
3313 UPDATE_EXEC_MASK(mach);
3314 break;
3315
3316 case TGSI_OPCODE_BRK:
3317 exec_break(mach);
3318 break;
3319
3320 case TGSI_OPCODE_CONT:
3321 /* turn off cont channels for each enabled exec channel */
3322 mach->ContMask &= ~mach->ExecMask;
3323 /* Todo: if mach->LoopMask == 0, jump to end of loop */
3324 UPDATE_EXEC_MASK(mach);
3325 break;
3326
3327 case TGSI_OPCODE_BGNSUB:
3328 /* no-op */
3329 break;
3330
3331 case TGSI_OPCODE_ENDSUB:
3332 /*
3333 * XXX: This really should be a no-op. We should never reach this opcode.
3334 */
3335
3336 assert(mach->CallStackTop > 0);
3337 mach->CallStackTop--;
3338
3339 mach->CondStackTop = mach->CallStack[mach->CallStackTop].CondStackTop;
3340 mach->CondMask = mach->CondStack[mach->CondStackTop];
3341
3342 mach->LoopStackTop = mach->CallStack[mach->CallStackTop].LoopStackTop;
3343 mach->LoopMask = mach->LoopStack[mach->LoopStackTop];
3344
3345 mach->ContStackTop = mach->CallStack[mach->CallStackTop].ContStackTop;
3346 mach->ContMask = mach->ContStack[mach->ContStackTop];
3347
3348 mach->SwitchStackTop = mach->CallStack[mach->CallStackTop].SwitchStackTop;
3349 mach->Switch = mach->SwitchStack[mach->SwitchStackTop];
3350
3351 mach->BreakStackTop = mach->CallStack[mach->CallStackTop].BreakStackTop;
3352 mach->BreakType = mach->BreakStack[mach->BreakStackTop];
3353
3354 assert(mach->FuncStackTop > 0);
3355 mach->FuncMask = mach->FuncStack[--mach->FuncStackTop];
3356
3357 *pc = mach->CallStack[mach->CallStackTop].ReturnAddr;
3358
3359 UPDATE_EXEC_MASK(mach);
3360 break;
3361
3362 case TGSI_OPCODE_NOP:
3363 break;
3364
3365 case TGSI_OPCODE_BREAKC:
3366 FETCH(&r[0], 0, CHAN_X);
3367 /* update CondMask */
3368 if (r[0].u[0] && (mach->ExecMask & 0x1)) {
3369 mach->LoopMask &= ~0x1;
3370 }
3371 if (r[0].u[1] && (mach->ExecMask & 0x2)) {
3372 mach->LoopMask &= ~0x2;
3373 }
3374 if (r[0].u[2] && (mach->ExecMask & 0x4)) {
3375 mach->LoopMask &= ~0x4;
3376 }
3377 if (r[0].u[3] && (mach->ExecMask & 0x8)) {
3378 mach->LoopMask &= ~0x8;
3379 }
3380 /* Todo: if mach->LoopMask == 0, jump to end of loop */
3381 UPDATE_EXEC_MASK(mach);
3382 break;
3383
3384 case TGSI_OPCODE_F2I:
3385 exec_vector_unary(mach, inst, micro_f2i, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_FLOAT);
3386 break;
3387
3388 case TGSI_OPCODE_IDIV:
3389 exec_vector_binary(mach, inst, micro_idiv, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3390 break;
3391
3392 case TGSI_OPCODE_IMAX:
3393 exec_vector_binary(mach, inst, micro_imax, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3394 break;
3395
3396 case TGSI_OPCODE_IMIN:
3397 exec_vector_binary(mach, inst, micro_imin, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3398 break;
3399
3400 case TGSI_OPCODE_INEG:
3401 exec_vector_unary(mach, inst, micro_ineg, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3402 break;
3403
3404 case TGSI_OPCODE_ISGE:
3405 exec_vector_binary(mach, inst, micro_isge, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3406 break;
3407
3408 case TGSI_OPCODE_ISHR:
3409 exec_vector_binary(mach, inst, micro_ishr, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3410 break;
3411
3412 case TGSI_OPCODE_ISLT:
3413 exec_vector_binary(mach, inst, micro_islt, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
3414 break;
3415
3416 case TGSI_OPCODE_F2U:
3417 exec_vector_unary(mach, inst, micro_f2u, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
3418 break;
3419
3420 case TGSI_OPCODE_U2F:
3421 exec_vector_unary(mach, inst, micro_u2f, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_UINT);
3422 break;
3423
3424 case TGSI_OPCODE_UADD:
3425 exec_vector_binary(mach, inst, micro_uadd, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3426 break;
3427
3428 case TGSI_OPCODE_UDIV:
3429 exec_vector_binary(mach, inst, micro_udiv, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3430 break;
3431
3432 case TGSI_OPCODE_UMAD:
3433 exec_vector_trinary(mach, inst, micro_umad, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3434 break;
3435
3436 case TGSI_OPCODE_UMAX:
3437 exec_vector_binary(mach, inst, micro_umax, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3438 break;
3439
3440 case TGSI_OPCODE_UMIN:
3441 exec_vector_binary(mach, inst, micro_umin, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3442 break;
3443
3444 case TGSI_OPCODE_UMOD:
3445 exec_vector_binary(mach, inst, micro_umod, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3446 break;
3447
3448 case TGSI_OPCODE_UMUL:
3449 exec_vector_binary(mach, inst, micro_umul, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3450 break;
3451
3452 case TGSI_OPCODE_USEQ:
3453 exec_vector_binary(mach, inst, micro_useq, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3454 break;
3455
3456 case TGSI_OPCODE_USGE:
3457 exec_vector_binary(mach, inst, micro_usge, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3458 break;
3459
3460 case TGSI_OPCODE_USHR:
3461 exec_vector_binary(mach, inst, micro_ushr, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3462 break;
3463
3464 case TGSI_OPCODE_USLT:
3465 exec_vector_binary(mach, inst, micro_uslt, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3466 break;
3467
3468 case TGSI_OPCODE_USNE:
3469 exec_vector_binary(mach, inst, micro_usne, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
3470 break;
3471
3472 case TGSI_OPCODE_SWITCH:
3473 exec_switch(mach, inst);
3474 break;
3475
3476 case TGSI_OPCODE_CASE:
3477 exec_case(mach, inst);
3478 break;
3479
3480 case TGSI_OPCODE_DEFAULT:
3481 exec_default(mach);
3482 break;
3483
3484 case TGSI_OPCODE_ENDSWITCH:
3485 exec_endswitch(mach);
3486 break;
3487
3488 default:
3489 assert( 0 );
3490 }
3491 }
3492
3493
3494 #define DEBUG_EXECUTION 0
3495
3496
3497 /**
3498 * Run TGSI interpreter.
3499 * \return bitmask of "alive" quad components
3500 */
3501 uint
3502 tgsi_exec_machine_run( struct tgsi_exec_machine *mach )
3503 {
3504 uint i;
3505 int pc = 0;
3506
3507 mach->CondMask = 0xf;
3508 mach->LoopMask = 0xf;
3509 mach->ContMask = 0xf;
3510 mach->FuncMask = 0xf;
3511 mach->ExecMask = 0xf;
3512
3513 mach->Switch.mask = 0xf;
3514
3515 assert(mach->CondStackTop == 0);
3516 assert(mach->LoopStackTop == 0);
3517 assert(mach->ContStackTop == 0);
3518 assert(mach->SwitchStackTop == 0);
3519 assert(mach->BreakStackTop == 0);
3520 assert(mach->CallStackTop == 0);
3521
3522 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] = 0;
3523 mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0] = 0;
3524
3525 if( mach->Processor == TGSI_PROCESSOR_GEOMETRY ) {
3526 mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0] = 0;
3527 mach->Primitives[0] = 0;
3528 }
3529
3530 for (i = 0; i < QUAD_SIZE; i++) {
3531 mach->Temps[TEMP_CC_I].xyzw[TEMP_CC_C].u[i] =
3532 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_X_SHIFT) |
3533 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_Y_SHIFT) |
3534 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_Z_SHIFT) |
3535 (TGSI_EXEC_CC_EQ << TGSI_EXEC_CC_W_SHIFT);
3536 }
3537
3538 /* execute declarations (interpolants) */
3539 for (i = 0; i < mach->NumDeclarations; i++) {
3540 exec_declaration( mach, mach->Declarations+i );
3541 }
3542
3543 {
3544 #if DEBUG_EXECUTION
3545 struct tgsi_exec_vector temps[TGSI_EXEC_NUM_TEMPS + TGSI_EXEC_NUM_TEMP_EXTRAS];
3546 struct tgsi_exec_vector outputs[PIPE_MAX_ATTRIBS];
3547 uint inst = 1;
3548
3549 memcpy(temps, mach->Temps, sizeof(temps));
3550 memcpy(outputs, mach->Outputs, sizeof(outputs));
3551 #endif
3552
3553 /* execute instructions, until pc is set to -1 */
3554 while (pc != -1) {
3555
3556 #if DEBUG_EXECUTION
3557 uint i;
3558
3559 tgsi_dump_instruction(&mach->Instructions[pc], inst++);
3560 #endif
3561
3562 assert(pc < (int) mach->NumInstructions);
3563 exec_instruction(mach, mach->Instructions + pc, &pc);
3564
3565 #if DEBUG_EXECUTION
3566 for (i = 0; i < TGSI_EXEC_NUM_TEMPS + TGSI_EXEC_NUM_TEMP_EXTRAS; i++) {
3567 if (memcmp(&temps[i], &mach->Temps[i], sizeof(temps[i]))) {
3568 uint j;
3569
3570 memcpy(&temps[i], &mach->Temps[i], sizeof(temps[i]));
3571 debug_printf("TEMP[%2u] = ", i);
3572 for (j = 0; j < 4; j++) {
3573 if (j > 0) {
3574 debug_printf(" ");
3575 }
3576 debug_printf("(%6f %u, %6f %u, %6f %u, %6f %u)\n",
3577 temps[i].xyzw[0].f[j], temps[i].xyzw[0].u[j],
3578 temps[i].xyzw[1].f[j], temps[i].xyzw[1].u[j],
3579 temps[i].xyzw[2].f[j], temps[i].xyzw[2].u[j],
3580 temps[i].xyzw[3].f[j], temps[i].xyzw[3].u[j]);
3581 }
3582 }
3583 }
3584 for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
3585 if (memcmp(&outputs[i], &mach->Outputs[i], sizeof(outputs[i]))) {
3586 uint j;
3587
3588 memcpy(&outputs[i], &mach->Outputs[i], sizeof(outputs[i]));
3589 debug_printf("OUT[%2u] = ", i);
3590 for (j = 0; j < 4; j++) {
3591 if (j > 0) {
3592 debug_printf(" ");
3593 }
3594 debug_printf("(%6f %u, %6f %u, %6f %u, %6f %u)\n",
3595 outputs[i].xyzw[0].f[j], outputs[i].xyzw[0].u[j],
3596 outputs[i].xyzw[1].f[j], outputs[i].xyzw[1].u[j],
3597 outputs[i].xyzw[2].f[j], outputs[i].xyzw[2].u[j],
3598 outputs[i].xyzw[3].f[j], outputs[i].xyzw[3].u[j]);
3599 }
3600 }
3601 }
3602 #endif
3603 }
3604 }
3605
3606 #if 0
3607 /* we scale from floats in [0,1] to Zbuffer ints in sp_quad_depth_test.c */
3608 if (mach->Processor == TGSI_PROCESSOR_FRAGMENT) {
3609 /*
3610 * Scale back depth component.
3611 */
3612 for (i = 0; i < 4; i++)
3613 mach->Outputs[0].xyzw[2].f[i] *= ctx->DrawBuffer->_DepthMaxF;
3614 }
3615 #endif
3616
3617 assert(mach->CondStackTop == 0);
3618 assert(mach->LoopStackTop == 0);
3619 assert(mach->ContStackTop == 0);
3620 assert(mach->SwitchStackTop == 0);
3621 assert(mach->BreakStackTop == 0);
3622 assert(mach->CallStackTop == 0);
3623
3624 return ~mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
3625 }