gallium/tgsi: Treat UCMP sources as floats to match the GLSL-to-TGSI pass expectations.
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_exec.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 VMware, Inc.
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 VMWARE 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_half.h"
62 #include "util/u_memory.h"
63 #include "util/u_math.h"
64 #include "util/rounding.h"
65
66
67 #define DEBUG_EXECUTION 0
68
69
70 #define FAST_MATH 0
71
72 #define TILE_TOP_LEFT 0
73 #define TILE_TOP_RIGHT 1
74 #define TILE_BOTTOM_LEFT 2
75 #define TILE_BOTTOM_RIGHT 3
76
77 union tgsi_double_channel {
78 double d[TGSI_QUAD_SIZE];
79 unsigned u[TGSI_QUAD_SIZE][2];
80 uint64_t u64[TGSI_QUAD_SIZE];
81 int64_t i64[TGSI_QUAD_SIZE];
82 };
83
84 struct tgsi_double_vector {
85 union tgsi_double_channel xy;
86 union tgsi_double_channel zw;
87 };
88
89 static void
90 micro_abs(union tgsi_exec_channel *dst,
91 const union tgsi_exec_channel *src)
92 {
93 dst->f[0] = fabsf(src->f[0]);
94 dst->f[1] = fabsf(src->f[1]);
95 dst->f[2] = fabsf(src->f[2]);
96 dst->f[3] = fabsf(src->f[3]);
97 }
98
99 static void
100 micro_arl(union tgsi_exec_channel *dst,
101 const union tgsi_exec_channel *src)
102 {
103 dst->i[0] = (int)floorf(src->f[0]);
104 dst->i[1] = (int)floorf(src->f[1]);
105 dst->i[2] = (int)floorf(src->f[2]);
106 dst->i[3] = (int)floorf(src->f[3]);
107 }
108
109 static void
110 micro_arr(union tgsi_exec_channel *dst,
111 const union tgsi_exec_channel *src)
112 {
113 dst->i[0] = (int)floorf(src->f[0] + 0.5f);
114 dst->i[1] = (int)floorf(src->f[1] + 0.5f);
115 dst->i[2] = (int)floorf(src->f[2] + 0.5f);
116 dst->i[3] = (int)floorf(src->f[3] + 0.5f);
117 }
118
119 static void
120 micro_ceil(union tgsi_exec_channel *dst,
121 const union tgsi_exec_channel *src)
122 {
123 dst->f[0] = ceilf(src->f[0]);
124 dst->f[1] = ceilf(src->f[1]);
125 dst->f[2] = ceilf(src->f[2]);
126 dst->f[3] = ceilf(src->f[3]);
127 }
128
129 static void
130 micro_cmp(union tgsi_exec_channel *dst,
131 const union tgsi_exec_channel *src0,
132 const union tgsi_exec_channel *src1,
133 const union tgsi_exec_channel *src2)
134 {
135 dst->f[0] = src0->f[0] < 0.0f ? src1->f[0] : src2->f[0];
136 dst->f[1] = src0->f[1] < 0.0f ? src1->f[1] : src2->f[1];
137 dst->f[2] = src0->f[2] < 0.0f ? src1->f[2] : src2->f[2];
138 dst->f[3] = src0->f[3] < 0.0f ? src1->f[3] : src2->f[3];
139 }
140
141 static void
142 micro_cos(union tgsi_exec_channel *dst,
143 const union tgsi_exec_channel *src)
144 {
145 dst->f[0] = cosf(src->f[0]);
146 dst->f[1] = cosf(src->f[1]);
147 dst->f[2] = cosf(src->f[2]);
148 dst->f[3] = cosf(src->f[3]);
149 }
150
151 static void
152 micro_d2f(union tgsi_exec_channel *dst,
153 const union tgsi_double_channel *src)
154 {
155 dst->f[0] = (float)src->d[0];
156 dst->f[1] = (float)src->d[1];
157 dst->f[2] = (float)src->d[2];
158 dst->f[3] = (float)src->d[3];
159 }
160
161 static void
162 micro_d2i(union tgsi_exec_channel *dst,
163 const union tgsi_double_channel *src)
164 {
165 dst->i[0] = (int)src->d[0];
166 dst->i[1] = (int)src->d[1];
167 dst->i[2] = (int)src->d[2];
168 dst->i[3] = (int)src->d[3];
169 }
170
171 static void
172 micro_d2u(union tgsi_exec_channel *dst,
173 const union tgsi_double_channel *src)
174 {
175 dst->u[0] = (unsigned)src->d[0];
176 dst->u[1] = (unsigned)src->d[1];
177 dst->u[2] = (unsigned)src->d[2];
178 dst->u[3] = (unsigned)src->d[3];
179 }
180 static void
181 micro_dabs(union tgsi_double_channel *dst,
182 const union tgsi_double_channel *src)
183 {
184 dst->d[0] = src->d[0] >= 0.0 ? src->d[0] : -src->d[0];
185 dst->d[1] = src->d[1] >= 0.0 ? src->d[1] : -src->d[1];
186 dst->d[2] = src->d[2] >= 0.0 ? src->d[2] : -src->d[2];
187 dst->d[3] = src->d[3] >= 0.0 ? src->d[3] : -src->d[3];
188 }
189
190 static void
191 micro_dadd(union tgsi_double_channel *dst,
192 const union tgsi_double_channel *src)
193 {
194 dst->d[0] = src[0].d[0] + src[1].d[0];
195 dst->d[1] = src[0].d[1] + src[1].d[1];
196 dst->d[2] = src[0].d[2] + src[1].d[2];
197 dst->d[3] = src[0].d[3] + src[1].d[3];
198 }
199
200 static void
201 micro_ddiv(union tgsi_double_channel *dst,
202 const union tgsi_double_channel *src)
203 {
204 dst->d[0] = src[0].d[0] / src[1].d[0];
205 dst->d[1] = src[0].d[1] / src[1].d[1];
206 dst->d[2] = src[0].d[2] / src[1].d[2];
207 dst->d[3] = src[0].d[3] / src[1].d[3];
208 }
209
210 static void
211 micro_ddx(union tgsi_exec_channel *dst,
212 const union tgsi_exec_channel *src)
213 {
214 dst->f[0] =
215 dst->f[1] =
216 dst->f[2] =
217 dst->f[3] = src->f[TILE_BOTTOM_RIGHT] - src->f[TILE_BOTTOM_LEFT];
218 }
219
220 static void
221 micro_ddy(union tgsi_exec_channel *dst,
222 const union tgsi_exec_channel *src)
223 {
224 dst->f[0] =
225 dst->f[1] =
226 dst->f[2] =
227 dst->f[3] = src->f[TILE_BOTTOM_LEFT] - src->f[TILE_TOP_LEFT];
228 }
229
230 static void
231 micro_dmul(union tgsi_double_channel *dst,
232 const union tgsi_double_channel *src)
233 {
234 dst->d[0] = src[0].d[0] * src[1].d[0];
235 dst->d[1] = src[0].d[1] * src[1].d[1];
236 dst->d[2] = src[0].d[2] * src[1].d[2];
237 dst->d[3] = src[0].d[3] * src[1].d[3];
238 }
239
240 static void
241 micro_dmax(union tgsi_double_channel *dst,
242 const union tgsi_double_channel *src)
243 {
244 dst->d[0] = src[0].d[0] > src[1].d[0] ? src[0].d[0] : src[1].d[0];
245 dst->d[1] = src[0].d[1] > src[1].d[1] ? src[0].d[1] : src[1].d[1];
246 dst->d[2] = src[0].d[2] > src[1].d[2] ? src[0].d[2] : src[1].d[2];
247 dst->d[3] = src[0].d[3] > src[1].d[3] ? src[0].d[3] : src[1].d[3];
248 }
249
250 static void
251 micro_dmin(union tgsi_double_channel *dst,
252 const union tgsi_double_channel *src)
253 {
254 dst->d[0] = src[0].d[0] < src[1].d[0] ? src[0].d[0] : src[1].d[0];
255 dst->d[1] = src[0].d[1] < src[1].d[1] ? src[0].d[1] : src[1].d[1];
256 dst->d[2] = src[0].d[2] < src[1].d[2] ? src[0].d[2] : src[1].d[2];
257 dst->d[3] = src[0].d[3] < src[1].d[3] ? src[0].d[3] : src[1].d[3];
258 }
259
260 static void
261 micro_dneg(union tgsi_double_channel *dst,
262 const union tgsi_double_channel *src)
263 {
264 dst->d[0] = -src->d[0];
265 dst->d[1] = -src->d[1];
266 dst->d[2] = -src->d[2];
267 dst->d[3] = -src->d[3];
268 }
269
270 static void
271 micro_dslt(union tgsi_double_channel *dst,
272 const union tgsi_double_channel *src)
273 {
274 dst->u[0][0] = src[0].d[0] < src[1].d[0] ? ~0U : 0U;
275 dst->u[1][0] = src[0].d[1] < src[1].d[1] ? ~0U : 0U;
276 dst->u[2][0] = src[0].d[2] < src[1].d[2] ? ~0U : 0U;
277 dst->u[3][0] = src[0].d[3] < src[1].d[3] ? ~0U : 0U;
278 }
279
280 static void
281 micro_dsne(union tgsi_double_channel *dst,
282 const union tgsi_double_channel *src)
283 {
284 dst->u[0][0] = src[0].d[0] != src[1].d[0] ? ~0U : 0U;
285 dst->u[1][0] = src[0].d[1] != src[1].d[1] ? ~0U : 0U;
286 dst->u[2][0] = src[0].d[2] != src[1].d[2] ? ~0U : 0U;
287 dst->u[3][0] = src[0].d[3] != src[1].d[3] ? ~0U : 0U;
288 }
289
290 static void
291 micro_dsge(union tgsi_double_channel *dst,
292 const union tgsi_double_channel *src)
293 {
294 dst->u[0][0] = src[0].d[0] >= src[1].d[0] ? ~0U : 0U;
295 dst->u[1][0] = src[0].d[1] >= src[1].d[1] ? ~0U : 0U;
296 dst->u[2][0] = src[0].d[2] >= src[1].d[2] ? ~0U : 0U;
297 dst->u[3][0] = src[0].d[3] >= src[1].d[3] ? ~0U : 0U;
298 }
299
300 static void
301 micro_dseq(union tgsi_double_channel *dst,
302 const union tgsi_double_channel *src)
303 {
304 dst->u[0][0] = src[0].d[0] == src[1].d[0] ? ~0U : 0U;
305 dst->u[1][0] = src[0].d[1] == src[1].d[1] ? ~0U : 0U;
306 dst->u[2][0] = src[0].d[2] == src[1].d[2] ? ~0U : 0U;
307 dst->u[3][0] = src[0].d[3] == src[1].d[3] ? ~0U : 0U;
308 }
309
310 static void
311 micro_drcp(union tgsi_double_channel *dst,
312 const union tgsi_double_channel *src)
313 {
314 dst->d[0] = 1.0 / src->d[0];
315 dst->d[1] = 1.0 / src->d[1];
316 dst->d[2] = 1.0 / src->d[2];
317 dst->d[3] = 1.0 / src->d[3];
318 }
319
320 static void
321 micro_dsqrt(union tgsi_double_channel *dst,
322 const union tgsi_double_channel *src)
323 {
324 dst->d[0] = sqrt(src->d[0]);
325 dst->d[1] = sqrt(src->d[1]);
326 dst->d[2] = sqrt(src->d[2]);
327 dst->d[3] = sqrt(src->d[3]);
328 }
329
330 static void
331 micro_drsq(union tgsi_double_channel *dst,
332 const union tgsi_double_channel *src)
333 {
334 dst->d[0] = 1.0 / sqrt(src->d[0]);
335 dst->d[1] = 1.0 / sqrt(src->d[1]);
336 dst->d[2] = 1.0 / sqrt(src->d[2]);
337 dst->d[3] = 1.0 / sqrt(src->d[3]);
338 }
339
340 static void
341 micro_dmad(union tgsi_double_channel *dst,
342 const union tgsi_double_channel *src)
343 {
344 dst->d[0] = src[0].d[0] * src[1].d[0] + src[2].d[0];
345 dst->d[1] = src[0].d[1] * src[1].d[1] + src[2].d[1];
346 dst->d[2] = src[0].d[2] * src[1].d[2] + src[2].d[2];
347 dst->d[3] = src[0].d[3] * src[1].d[3] + src[2].d[3];
348 }
349
350 static void
351 micro_dfrac(union tgsi_double_channel *dst,
352 const union tgsi_double_channel *src)
353 {
354 dst->d[0] = src->d[0] - floor(src->d[0]);
355 dst->d[1] = src->d[1] - floor(src->d[1]);
356 dst->d[2] = src->d[2] - floor(src->d[2]);
357 dst->d[3] = src->d[3] - floor(src->d[3]);
358 }
359
360 static void
361 micro_dldexp(union tgsi_double_channel *dst,
362 const union tgsi_double_channel *src0,
363 union tgsi_exec_channel *src1)
364 {
365 dst->d[0] = ldexp(src0->d[0], src1->i[0]);
366 dst->d[1] = ldexp(src0->d[1], src1->i[1]);
367 dst->d[2] = ldexp(src0->d[2], src1->i[2]);
368 dst->d[3] = ldexp(src0->d[3], src1->i[3]);
369 }
370
371 static void
372 micro_dfracexp(union tgsi_double_channel *dst,
373 union tgsi_exec_channel *dst_exp,
374 const union tgsi_double_channel *src)
375 {
376 dst->d[0] = frexp(src->d[0], &dst_exp->i[0]);
377 dst->d[1] = frexp(src->d[1], &dst_exp->i[1]);
378 dst->d[2] = frexp(src->d[2], &dst_exp->i[2]);
379 dst->d[3] = frexp(src->d[3], &dst_exp->i[3]);
380 }
381
382 static void
383 micro_exp2(union tgsi_exec_channel *dst,
384 const union tgsi_exec_channel *src)
385 {
386 #if FAST_MATH
387 dst->f[0] = util_fast_exp2(src->f[0]);
388 dst->f[1] = util_fast_exp2(src->f[1]);
389 dst->f[2] = util_fast_exp2(src->f[2]);
390 dst->f[3] = util_fast_exp2(src->f[3]);
391 #else
392 #if DEBUG
393 /* Inf is okay for this instruction, so clamp it to silence assertions. */
394 uint i;
395 union tgsi_exec_channel clamped;
396
397 for (i = 0; i < 4; i++) {
398 if (src->f[i] > 127.99999f) {
399 clamped.f[i] = 127.99999f;
400 } else if (src->f[i] < -126.99999f) {
401 clamped.f[i] = -126.99999f;
402 } else {
403 clamped.f[i] = src->f[i];
404 }
405 }
406 src = &clamped;
407 #endif /* DEBUG */
408
409 dst->f[0] = powf(2.0f, src->f[0]);
410 dst->f[1] = powf(2.0f, src->f[1]);
411 dst->f[2] = powf(2.0f, src->f[2]);
412 dst->f[3] = powf(2.0f, src->f[3]);
413 #endif /* FAST_MATH */
414 }
415
416 static void
417 micro_f2d(union tgsi_double_channel *dst,
418 const union tgsi_exec_channel *src)
419 {
420 dst->d[0] = (double)src->f[0];
421 dst->d[1] = (double)src->f[1];
422 dst->d[2] = (double)src->f[2];
423 dst->d[3] = (double)src->f[3];
424 }
425
426 static void
427 micro_flr(union tgsi_exec_channel *dst,
428 const union tgsi_exec_channel *src)
429 {
430 dst->f[0] = floorf(src->f[0]);
431 dst->f[1] = floorf(src->f[1]);
432 dst->f[2] = floorf(src->f[2]);
433 dst->f[3] = floorf(src->f[3]);
434 }
435
436 static void
437 micro_frc(union tgsi_exec_channel *dst,
438 const union tgsi_exec_channel *src)
439 {
440 dst->f[0] = src->f[0] - floorf(src->f[0]);
441 dst->f[1] = src->f[1] - floorf(src->f[1]);
442 dst->f[2] = src->f[2] - floorf(src->f[2]);
443 dst->f[3] = src->f[3] - floorf(src->f[3]);
444 }
445
446 static void
447 micro_i2d(union tgsi_double_channel *dst,
448 const union tgsi_exec_channel *src)
449 {
450 dst->d[0] = (double)src->i[0];
451 dst->d[1] = (double)src->i[1];
452 dst->d[2] = (double)src->i[2];
453 dst->d[3] = (double)src->i[3];
454 }
455
456 static void
457 micro_iabs(union tgsi_exec_channel *dst,
458 const union tgsi_exec_channel *src)
459 {
460 dst->i[0] = src->i[0] >= 0 ? src->i[0] : -src->i[0];
461 dst->i[1] = src->i[1] >= 0 ? src->i[1] : -src->i[1];
462 dst->i[2] = src->i[2] >= 0 ? src->i[2] : -src->i[2];
463 dst->i[3] = src->i[3] >= 0 ? src->i[3] : -src->i[3];
464 }
465
466 static void
467 micro_ineg(union tgsi_exec_channel *dst,
468 const union tgsi_exec_channel *src)
469 {
470 dst->i[0] = -src->i[0];
471 dst->i[1] = -src->i[1];
472 dst->i[2] = -src->i[2];
473 dst->i[3] = -src->i[3];
474 }
475
476 static void
477 micro_lg2(union tgsi_exec_channel *dst,
478 const union tgsi_exec_channel *src)
479 {
480 #if FAST_MATH
481 dst->f[0] = util_fast_log2(src->f[0]);
482 dst->f[1] = util_fast_log2(src->f[1]);
483 dst->f[2] = util_fast_log2(src->f[2]);
484 dst->f[3] = util_fast_log2(src->f[3]);
485 #else
486 dst->f[0] = logf(src->f[0]) * 1.442695f;
487 dst->f[1] = logf(src->f[1]) * 1.442695f;
488 dst->f[2] = logf(src->f[2]) * 1.442695f;
489 dst->f[3] = logf(src->f[3]) * 1.442695f;
490 #endif
491 }
492
493 static void
494 micro_lrp(union tgsi_exec_channel *dst,
495 const union tgsi_exec_channel *src0,
496 const union tgsi_exec_channel *src1,
497 const union tgsi_exec_channel *src2)
498 {
499 dst->f[0] = src0->f[0] * (src1->f[0] - src2->f[0]) + src2->f[0];
500 dst->f[1] = src0->f[1] * (src1->f[1] - src2->f[1]) + src2->f[1];
501 dst->f[2] = src0->f[2] * (src1->f[2] - src2->f[2]) + src2->f[2];
502 dst->f[3] = src0->f[3] * (src1->f[3] - src2->f[3]) + src2->f[3];
503 }
504
505 static void
506 micro_mad(union tgsi_exec_channel *dst,
507 const union tgsi_exec_channel *src0,
508 const union tgsi_exec_channel *src1,
509 const union tgsi_exec_channel *src2)
510 {
511 dst->f[0] = src0->f[0] * src1->f[0] + src2->f[0];
512 dst->f[1] = src0->f[1] * src1->f[1] + src2->f[1];
513 dst->f[2] = src0->f[2] * src1->f[2] + src2->f[2];
514 dst->f[3] = src0->f[3] * src1->f[3] + src2->f[3];
515 }
516
517 static void
518 micro_mov(union tgsi_exec_channel *dst,
519 const union tgsi_exec_channel *src)
520 {
521 dst->u[0] = src->u[0];
522 dst->u[1] = src->u[1];
523 dst->u[2] = src->u[2];
524 dst->u[3] = src->u[3];
525 }
526
527 static void
528 micro_rcp(union tgsi_exec_channel *dst,
529 const union tgsi_exec_channel *src)
530 {
531 #if 0 /* for debugging */
532 assert(src->f[0] != 0.0f);
533 assert(src->f[1] != 0.0f);
534 assert(src->f[2] != 0.0f);
535 assert(src->f[3] != 0.0f);
536 #endif
537 dst->f[0] = 1.0f / src->f[0];
538 dst->f[1] = 1.0f / src->f[1];
539 dst->f[2] = 1.0f / src->f[2];
540 dst->f[3] = 1.0f / src->f[3];
541 }
542
543 static void
544 micro_rnd(union tgsi_exec_channel *dst,
545 const union tgsi_exec_channel *src)
546 {
547 dst->f[0] = _mesa_roundevenf(src->f[0]);
548 dst->f[1] = _mesa_roundevenf(src->f[1]);
549 dst->f[2] = _mesa_roundevenf(src->f[2]);
550 dst->f[3] = _mesa_roundevenf(src->f[3]);
551 }
552
553 static void
554 micro_rsq(union tgsi_exec_channel *dst,
555 const union tgsi_exec_channel *src)
556 {
557 #if 0 /* for debugging */
558 assert(src->f[0] != 0.0f);
559 assert(src->f[1] != 0.0f);
560 assert(src->f[2] != 0.0f);
561 assert(src->f[3] != 0.0f);
562 #endif
563 dst->f[0] = 1.0f / sqrtf(src->f[0]);
564 dst->f[1] = 1.0f / sqrtf(src->f[1]);
565 dst->f[2] = 1.0f / sqrtf(src->f[2]);
566 dst->f[3] = 1.0f / sqrtf(src->f[3]);
567 }
568
569 static void
570 micro_sqrt(union tgsi_exec_channel *dst,
571 const union tgsi_exec_channel *src)
572 {
573 dst->f[0] = sqrtf(src->f[0]);
574 dst->f[1] = sqrtf(src->f[1]);
575 dst->f[2] = sqrtf(src->f[2]);
576 dst->f[3] = sqrtf(src->f[3]);
577 }
578
579 static void
580 micro_seq(union tgsi_exec_channel *dst,
581 const union tgsi_exec_channel *src0,
582 const union tgsi_exec_channel *src1)
583 {
584 dst->f[0] = src0->f[0] == src1->f[0] ? 1.0f : 0.0f;
585 dst->f[1] = src0->f[1] == src1->f[1] ? 1.0f : 0.0f;
586 dst->f[2] = src0->f[2] == src1->f[2] ? 1.0f : 0.0f;
587 dst->f[3] = src0->f[3] == src1->f[3] ? 1.0f : 0.0f;
588 }
589
590 static void
591 micro_sge(union tgsi_exec_channel *dst,
592 const union tgsi_exec_channel *src0,
593 const union tgsi_exec_channel *src1)
594 {
595 dst->f[0] = src0->f[0] >= src1->f[0] ? 1.0f : 0.0f;
596 dst->f[1] = src0->f[1] >= src1->f[1] ? 1.0f : 0.0f;
597 dst->f[2] = src0->f[2] >= src1->f[2] ? 1.0f : 0.0f;
598 dst->f[3] = src0->f[3] >= src1->f[3] ? 1.0f : 0.0f;
599 }
600
601 static void
602 micro_sgn(union tgsi_exec_channel *dst,
603 const union tgsi_exec_channel *src)
604 {
605 dst->f[0] = src->f[0] < 0.0f ? -1.0f : src->f[0] > 0.0f ? 1.0f : 0.0f;
606 dst->f[1] = src->f[1] < 0.0f ? -1.0f : src->f[1] > 0.0f ? 1.0f : 0.0f;
607 dst->f[2] = src->f[2] < 0.0f ? -1.0f : src->f[2] > 0.0f ? 1.0f : 0.0f;
608 dst->f[3] = src->f[3] < 0.0f ? -1.0f : src->f[3] > 0.0f ? 1.0f : 0.0f;
609 }
610
611 static void
612 micro_isgn(union tgsi_exec_channel *dst,
613 const union tgsi_exec_channel *src)
614 {
615 dst->i[0] = src->i[0] < 0 ? -1 : src->i[0] > 0 ? 1 : 0;
616 dst->i[1] = src->i[1] < 0 ? -1 : src->i[1] > 0 ? 1 : 0;
617 dst->i[2] = src->i[2] < 0 ? -1 : src->i[2] > 0 ? 1 : 0;
618 dst->i[3] = src->i[3] < 0 ? -1 : src->i[3] > 0 ? 1 : 0;
619 }
620
621 static void
622 micro_sgt(union tgsi_exec_channel *dst,
623 const union tgsi_exec_channel *src0,
624 const union tgsi_exec_channel *src1)
625 {
626 dst->f[0] = src0->f[0] > src1->f[0] ? 1.0f : 0.0f;
627 dst->f[1] = src0->f[1] > src1->f[1] ? 1.0f : 0.0f;
628 dst->f[2] = src0->f[2] > src1->f[2] ? 1.0f : 0.0f;
629 dst->f[3] = src0->f[3] > src1->f[3] ? 1.0f : 0.0f;
630 }
631
632 static void
633 micro_sin(union tgsi_exec_channel *dst,
634 const union tgsi_exec_channel *src)
635 {
636 dst->f[0] = sinf(src->f[0]);
637 dst->f[1] = sinf(src->f[1]);
638 dst->f[2] = sinf(src->f[2]);
639 dst->f[3] = sinf(src->f[3]);
640 }
641
642 static void
643 micro_sle(union tgsi_exec_channel *dst,
644 const union tgsi_exec_channel *src0,
645 const union tgsi_exec_channel *src1)
646 {
647 dst->f[0] = src0->f[0] <= src1->f[0] ? 1.0f : 0.0f;
648 dst->f[1] = src0->f[1] <= src1->f[1] ? 1.0f : 0.0f;
649 dst->f[2] = src0->f[2] <= src1->f[2] ? 1.0f : 0.0f;
650 dst->f[3] = src0->f[3] <= src1->f[3] ? 1.0f : 0.0f;
651 }
652
653 static void
654 micro_slt(union tgsi_exec_channel *dst,
655 const union tgsi_exec_channel *src0,
656 const union tgsi_exec_channel *src1)
657 {
658 dst->f[0] = src0->f[0] < src1->f[0] ? 1.0f : 0.0f;
659 dst->f[1] = src0->f[1] < src1->f[1] ? 1.0f : 0.0f;
660 dst->f[2] = src0->f[2] < src1->f[2] ? 1.0f : 0.0f;
661 dst->f[3] = src0->f[3] < src1->f[3] ? 1.0f : 0.0f;
662 }
663
664 static void
665 micro_sne(union tgsi_exec_channel *dst,
666 const union tgsi_exec_channel *src0,
667 const union tgsi_exec_channel *src1)
668 {
669 dst->f[0] = src0->f[0] != src1->f[0] ? 1.0f : 0.0f;
670 dst->f[1] = src0->f[1] != src1->f[1] ? 1.0f : 0.0f;
671 dst->f[2] = src0->f[2] != src1->f[2] ? 1.0f : 0.0f;
672 dst->f[3] = src0->f[3] != src1->f[3] ? 1.0f : 0.0f;
673 }
674
675 static void
676 micro_trunc(union tgsi_exec_channel *dst,
677 const union tgsi_exec_channel *src)
678 {
679 dst->f[0] = truncf(src->f[0]);
680 dst->f[1] = truncf(src->f[1]);
681 dst->f[2] = truncf(src->f[2]);
682 dst->f[3] = truncf(src->f[3]);
683 }
684
685 static void
686 micro_u2d(union tgsi_double_channel *dst,
687 const union tgsi_exec_channel *src)
688 {
689 dst->d[0] = (double)src->u[0];
690 dst->d[1] = (double)src->u[1];
691 dst->d[2] = (double)src->u[2];
692 dst->d[3] = (double)src->u[3];
693 }
694
695 static void
696 micro_i64abs(union tgsi_double_channel *dst,
697 const union tgsi_double_channel *src)
698 {
699 dst->i64[0] = src->i64[0] >= 0.0 ? src->i64[0] : -src->i64[0];
700 dst->i64[1] = src->i64[1] >= 0.0 ? src->i64[1] : -src->i64[1];
701 dst->i64[2] = src->i64[2] >= 0.0 ? src->i64[2] : -src->i64[2];
702 dst->i64[3] = src->i64[3] >= 0.0 ? src->i64[3] : -src->i64[3];
703 }
704
705 static void
706 micro_i64sgn(union tgsi_double_channel *dst,
707 const union tgsi_double_channel *src)
708 {
709 dst->i64[0] = src->i64[0] < 0 ? -1 : src->i64[0] > 0 ? 1 : 0;
710 dst->i64[1] = src->i64[1] < 0 ? -1 : src->i64[1] > 0 ? 1 : 0;
711 dst->i64[2] = src->i64[2] < 0 ? -1 : src->i64[2] > 0 ? 1 : 0;
712 dst->i64[3] = src->i64[3] < 0 ? -1 : src->i64[3] > 0 ? 1 : 0;
713 }
714
715 static void
716 micro_i64neg(union tgsi_double_channel *dst,
717 const union tgsi_double_channel *src)
718 {
719 dst->i64[0] = -src->i64[0];
720 dst->i64[1] = -src->i64[1];
721 dst->i64[2] = -src->i64[2];
722 dst->i64[3] = -src->i64[3];
723 }
724
725 static void
726 micro_u64seq(union tgsi_double_channel *dst,
727 const union tgsi_double_channel *src)
728 {
729 dst->u[0][0] = src[0].u64[0] == src[1].u64[0] ? ~0U : 0U;
730 dst->u[1][0] = src[0].u64[1] == src[1].u64[1] ? ~0U : 0U;
731 dst->u[2][0] = src[0].u64[2] == src[1].u64[2] ? ~0U : 0U;
732 dst->u[3][0] = src[0].u64[3] == src[1].u64[3] ? ~0U : 0U;
733 }
734
735 static void
736 micro_u64sne(union tgsi_double_channel *dst,
737 const union tgsi_double_channel *src)
738 {
739 dst->u[0][0] = src[0].u64[0] != src[1].u64[0] ? ~0U : 0U;
740 dst->u[1][0] = src[0].u64[1] != src[1].u64[1] ? ~0U : 0U;
741 dst->u[2][0] = src[0].u64[2] != src[1].u64[2] ? ~0U : 0U;
742 dst->u[3][0] = src[0].u64[3] != src[1].u64[3] ? ~0U : 0U;
743 }
744
745 static void
746 micro_i64slt(union tgsi_double_channel *dst,
747 const union tgsi_double_channel *src)
748 {
749 dst->u[0][0] = src[0].i64[0] < src[1].i64[0] ? ~0U : 0U;
750 dst->u[1][0] = src[0].i64[1] < src[1].i64[1] ? ~0U : 0U;
751 dst->u[2][0] = src[0].i64[2] < src[1].i64[2] ? ~0U : 0U;
752 dst->u[3][0] = src[0].i64[3] < src[1].i64[3] ? ~0U : 0U;
753 }
754
755 static void
756 micro_u64slt(union tgsi_double_channel *dst,
757 const union tgsi_double_channel *src)
758 {
759 dst->u[0][0] = src[0].u64[0] < src[1].u64[0] ? ~0U : 0U;
760 dst->u[1][0] = src[0].u64[1] < src[1].u64[1] ? ~0U : 0U;
761 dst->u[2][0] = src[0].u64[2] < src[1].u64[2] ? ~0U : 0U;
762 dst->u[3][0] = src[0].u64[3] < src[1].u64[3] ? ~0U : 0U;
763 }
764
765 static void
766 micro_i64sge(union tgsi_double_channel *dst,
767 const union tgsi_double_channel *src)
768 {
769 dst->u[0][0] = src[0].i64[0] >= src[1].i64[0] ? ~0U : 0U;
770 dst->u[1][0] = src[0].i64[1] >= src[1].i64[1] ? ~0U : 0U;
771 dst->u[2][0] = src[0].i64[2] >= src[1].i64[2] ? ~0U : 0U;
772 dst->u[3][0] = src[0].i64[3] >= src[1].i64[3] ? ~0U : 0U;
773 }
774
775 static void
776 micro_u64sge(union tgsi_double_channel *dst,
777 const union tgsi_double_channel *src)
778 {
779 dst->u[0][0] = src[0].u64[0] >= src[1].u64[0] ? ~0U : 0U;
780 dst->u[1][0] = src[0].u64[1] >= src[1].u64[1] ? ~0U : 0U;
781 dst->u[2][0] = src[0].u64[2] >= src[1].u64[2] ? ~0U : 0U;
782 dst->u[3][0] = src[0].u64[3] >= src[1].u64[3] ? ~0U : 0U;
783 }
784
785 static void
786 micro_u64max(union tgsi_double_channel *dst,
787 const union tgsi_double_channel *src)
788 {
789 dst->u64[0] = src[0].u64[0] > src[1].u64[0] ? src[0].u64[0] : src[1].u64[0];
790 dst->u64[1] = src[0].u64[1] > src[1].u64[1] ? src[0].u64[1] : src[1].u64[1];
791 dst->u64[2] = src[0].u64[2] > src[1].u64[2] ? src[0].u64[2] : src[1].u64[2];
792 dst->u64[3] = src[0].u64[3] > src[1].u64[3] ? src[0].u64[3] : src[1].u64[3];
793 }
794
795 static void
796 micro_i64max(union tgsi_double_channel *dst,
797 const union tgsi_double_channel *src)
798 {
799 dst->i64[0] = src[0].i64[0] > src[1].i64[0] ? src[0].i64[0] : src[1].i64[0];
800 dst->i64[1] = src[0].i64[1] > src[1].i64[1] ? src[0].i64[1] : src[1].i64[1];
801 dst->i64[2] = src[0].i64[2] > src[1].i64[2] ? src[0].i64[2] : src[1].i64[2];
802 dst->i64[3] = src[0].i64[3] > src[1].i64[3] ? src[0].i64[3] : src[1].i64[3];
803 }
804
805 static void
806 micro_u64min(union tgsi_double_channel *dst,
807 const union tgsi_double_channel *src)
808 {
809 dst->u64[0] = src[0].u64[0] < src[1].u64[0] ? src[0].u64[0] : src[1].u64[0];
810 dst->u64[1] = src[0].u64[1] < src[1].u64[1] ? src[0].u64[1] : src[1].u64[1];
811 dst->u64[2] = src[0].u64[2] < src[1].u64[2] ? src[0].u64[2] : src[1].u64[2];
812 dst->u64[3] = src[0].u64[3] < src[1].u64[3] ? src[0].u64[3] : src[1].u64[3];
813 }
814
815 static void
816 micro_i64min(union tgsi_double_channel *dst,
817 const union tgsi_double_channel *src)
818 {
819 dst->i64[0] = src[0].i64[0] < src[1].i64[0] ? src[0].i64[0] : src[1].i64[0];
820 dst->i64[1] = src[0].i64[1] < src[1].i64[1] ? src[0].i64[1] : src[1].i64[1];
821 dst->i64[2] = src[0].i64[2] < src[1].i64[2] ? src[0].i64[2] : src[1].i64[2];
822 dst->i64[3] = src[0].i64[3] < src[1].i64[3] ? src[0].i64[3] : src[1].i64[3];
823 }
824
825 static void
826 micro_u64add(union tgsi_double_channel *dst,
827 const union tgsi_double_channel *src)
828 {
829 dst->u64[0] = src[0].u64[0] + src[1].u64[0];
830 dst->u64[1] = src[0].u64[1] + src[1].u64[1];
831 dst->u64[2] = src[0].u64[2] + src[1].u64[2];
832 dst->u64[3] = src[0].u64[3] + src[1].u64[3];
833 }
834
835 static void
836 micro_u64mul(union tgsi_double_channel *dst,
837 const union tgsi_double_channel *src)
838 {
839 dst->u64[0] = src[0].u64[0] * src[1].u64[0];
840 dst->u64[1] = src[0].u64[1] * src[1].u64[1];
841 dst->u64[2] = src[0].u64[2] * src[1].u64[2];
842 dst->u64[3] = src[0].u64[3] * src[1].u64[3];
843 }
844
845 static void
846 micro_u64div(union tgsi_double_channel *dst,
847 const union tgsi_double_channel *src)
848 {
849 dst->u64[0] = src[0].u64[0] / src[1].u64[0];
850 dst->u64[1] = src[0].u64[1] / src[1].u64[1];
851 dst->u64[2] = src[0].u64[2] / src[1].u64[2];
852 dst->u64[3] = src[0].u64[3] / src[1].u64[3];
853 }
854
855 static void
856 micro_i64div(union tgsi_double_channel *dst,
857 const union tgsi_double_channel *src)
858 {
859 dst->i64[0] = src[0].i64[0] / src[1].i64[0];
860 dst->i64[1] = src[0].i64[1] / src[1].i64[1];
861 dst->i64[2] = src[0].i64[2] / src[1].i64[2];
862 dst->i64[3] = src[0].i64[3] / src[1].i64[3];
863 }
864
865 static void
866 micro_u64mod(union tgsi_double_channel *dst,
867 const union tgsi_double_channel *src)
868 {
869 dst->u64[0] = src[0].u64[0] % src[1].u64[0];
870 dst->u64[1] = src[0].u64[1] % src[1].u64[1];
871 dst->u64[2] = src[0].u64[2] % src[1].u64[2];
872 dst->u64[3] = src[0].u64[3] % src[1].u64[3];
873 }
874
875 static void
876 micro_i64mod(union tgsi_double_channel *dst,
877 const union tgsi_double_channel *src)
878 {
879 dst->i64[0] = src[0].i64[0] % src[1].i64[0];
880 dst->i64[1] = src[0].i64[1] % src[1].i64[1];
881 dst->i64[2] = src[0].i64[2] % src[1].i64[2];
882 dst->i64[3] = src[0].i64[3] % src[1].i64[3];
883 }
884
885 static void
886 micro_u64shl(union tgsi_double_channel *dst,
887 const union tgsi_double_channel *src0,
888 union tgsi_exec_channel *src1)
889 {
890 unsigned masked_count;
891 masked_count = src1->u[0] & 0x3f;
892 dst->u64[0] = src0->u64[0] << masked_count;
893 masked_count = src1->u[1] & 0x3f;
894 dst->u64[1] = src0->u64[1] << masked_count;
895 masked_count = src1->u[2] & 0x3f;
896 dst->u64[2] = src0->u64[2] << masked_count;
897 masked_count = src1->u[3] & 0x3f;
898 dst->u64[3] = src0->u64[3] << masked_count;
899 }
900
901 static void
902 micro_i64shr(union tgsi_double_channel *dst,
903 const union tgsi_double_channel *src0,
904 union tgsi_exec_channel *src1)
905 {
906 unsigned masked_count;
907 masked_count = src1->u[0] & 0x3f;
908 dst->i64[0] = src0->i64[0] >> masked_count;
909 masked_count = src1->u[1] & 0x3f;
910 dst->i64[1] = src0->i64[1] >> masked_count;
911 masked_count = src1->u[2] & 0x3f;
912 dst->i64[2] = src0->i64[2] >> masked_count;
913 masked_count = src1->u[3] & 0x3f;
914 dst->i64[3] = src0->i64[3] >> masked_count;
915 }
916
917 static void
918 micro_u64shr(union tgsi_double_channel *dst,
919 const union tgsi_double_channel *src0,
920 union tgsi_exec_channel *src1)
921 {
922 unsigned masked_count;
923 masked_count = src1->u[0] & 0x3f;
924 dst->u64[0] = src0->u64[0] >> masked_count;
925 masked_count = src1->u[1] & 0x3f;
926 dst->u64[1] = src0->u64[1] >> masked_count;
927 masked_count = src1->u[2] & 0x3f;
928 dst->u64[2] = src0->u64[2] >> masked_count;
929 masked_count = src1->u[3] & 0x3f;
930 dst->u64[3] = src0->u64[3] >> masked_count;
931 }
932
933 enum tgsi_exec_datatype {
934 TGSI_EXEC_DATA_FLOAT,
935 TGSI_EXEC_DATA_INT,
936 TGSI_EXEC_DATA_UINT,
937 TGSI_EXEC_DATA_DOUBLE,
938 TGSI_EXEC_DATA_INT64,
939 TGSI_EXEC_DATA_UINT64,
940 };
941
942 /*
943 * Shorthand locations of various utility registers (_I = Index, _C = Channel)
944 */
945 #define TEMP_KILMASK_I TGSI_EXEC_TEMP_KILMASK_I
946 #define TEMP_KILMASK_C TGSI_EXEC_TEMP_KILMASK_C
947 #define TEMP_OUTPUT_I TGSI_EXEC_TEMP_OUTPUT_I
948 #define TEMP_OUTPUT_C TGSI_EXEC_TEMP_OUTPUT_C
949 #define TEMP_PRIMITIVE_I TGSI_EXEC_TEMP_PRIMITIVE_I
950 #define TEMP_PRIMITIVE_C TGSI_EXEC_TEMP_PRIMITIVE_C
951
952
953 /** The execution mask depends on the conditional mask and the loop mask */
954 #define UPDATE_EXEC_MASK(MACH) \
955 MACH->ExecMask = MACH->CondMask & MACH->LoopMask & MACH->ContMask & MACH->Switch.mask & MACH->FuncMask
956
957
958 static const union tgsi_exec_channel ZeroVec =
959 { { 0.0, 0.0, 0.0, 0.0 } };
960
961 static const union tgsi_exec_channel OneVec = {
962 {1.0f, 1.0f, 1.0f, 1.0f}
963 };
964
965 static const union tgsi_exec_channel P128Vec = {
966 {128.0f, 128.0f, 128.0f, 128.0f}
967 };
968
969 static const union tgsi_exec_channel M128Vec = {
970 {-128.0f, -128.0f, -128.0f, -128.0f}
971 };
972
973
974 /**
975 * Assert that none of the float values in 'chan' are infinite or NaN.
976 * NaN and Inf may occur normally during program execution and should
977 * not lead to crashes, etc. But when debugging, it's helpful to catch
978 * them.
979 */
980 static inline void
981 check_inf_or_nan(const union tgsi_exec_channel *chan)
982 {
983 assert(!util_is_inf_or_nan((chan)->f[0]));
984 assert(!util_is_inf_or_nan((chan)->f[1]));
985 assert(!util_is_inf_or_nan((chan)->f[2]));
986 assert(!util_is_inf_or_nan((chan)->f[3]));
987 }
988
989
990 #ifdef DEBUG
991 static void
992 print_chan(const char *msg, const union tgsi_exec_channel *chan)
993 {
994 debug_printf("%s = {%f, %f, %f, %f}\n",
995 msg, chan->f[0], chan->f[1], chan->f[2], chan->f[3]);
996 }
997 #endif
998
999
1000 #ifdef DEBUG
1001 static void
1002 print_temp(const struct tgsi_exec_machine *mach, uint index)
1003 {
1004 const struct tgsi_exec_vector *tmp = &mach->Temps[index];
1005 int i;
1006 debug_printf("Temp[%u] =\n", index);
1007 for (i = 0; i < 4; i++) {
1008 debug_printf(" %c: { %f, %f, %f, %f }\n",
1009 "XYZW"[i],
1010 tmp->xyzw[i].f[0],
1011 tmp->xyzw[i].f[1],
1012 tmp->xyzw[i].f[2],
1013 tmp->xyzw[i].f[3]);
1014 }
1015 }
1016 #endif
1017
1018
1019 void
1020 tgsi_exec_set_constant_buffers(struct tgsi_exec_machine *mach,
1021 unsigned num_bufs,
1022 const void **bufs,
1023 const unsigned *buf_sizes)
1024 {
1025 unsigned i;
1026
1027 for (i = 0; i < num_bufs; i++) {
1028 mach->Consts[i] = bufs[i];
1029 mach->ConstsSize[i] = buf_sizes[i];
1030 }
1031 }
1032
1033
1034 /**
1035 * Check if there's a potential src/dst register data dependency when
1036 * using SOA execution.
1037 * Example:
1038 * MOV T, T.yxwz;
1039 * This would expand into:
1040 * MOV t0, t1;
1041 * MOV t1, t0;
1042 * MOV t2, t3;
1043 * MOV t3, t2;
1044 * The second instruction will have the wrong value for t0 if executed as-is.
1045 */
1046 boolean
1047 tgsi_check_soa_dependencies(const struct tgsi_full_instruction *inst)
1048 {
1049 uint i, chan;
1050
1051 uint writemask = inst->Dst[0].Register.WriteMask;
1052 if (writemask == TGSI_WRITEMASK_X ||
1053 writemask == TGSI_WRITEMASK_Y ||
1054 writemask == TGSI_WRITEMASK_Z ||
1055 writemask == TGSI_WRITEMASK_W ||
1056 writemask == TGSI_WRITEMASK_NONE) {
1057 /* no chance of data dependency */
1058 return FALSE;
1059 }
1060
1061 /* loop over src regs */
1062 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
1063 if ((inst->Src[i].Register.File ==
1064 inst->Dst[0].Register.File) &&
1065 ((inst->Src[i].Register.Index ==
1066 inst->Dst[0].Register.Index) ||
1067 inst->Src[i].Register.Indirect ||
1068 inst->Dst[0].Register.Indirect)) {
1069 /* loop over dest channels */
1070 uint channelsWritten = 0x0;
1071 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1072 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
1073 /* check if we're reading a channel that's been written */
1074 uint swizzle = tgsi_util_get_full_src_register_swizzle(&inst->Src[i], chan);
1075 if (channelsWritten & (1 << swizzle)) {
1076 return TRUE;
1077 }
1078
1079 channelsWritten |= (1 << chan);
1080 }
1081 }
1082 }
1083 }
1084 return FALSE;
1085 }
1086
1087
1088 /**
1089 * Initialize machine state by expanding tokens to full instructions,
1090 * allocating temporary storage, setting up constants, etc.
1091 * After this, we can call tgsi_exec_machine_run() many times.
1092 */
1093 void
1094 tgsi_exec_machine_bind_shader(
1095 struct tgsi_exec_machine *mach,
1096 const struct tgsi_token *tokens,
1097 struct tgsi_sampler *sampler,
1098 struct tgsi_image *image,
1099 struct tgsi_buffer *buffer)
1100 {
1101 uint k;
1102 struct tgsi_parse_context parse;
1103 struct tgsi_full_instruction *instructions;
1104 struct tgsi_full_declaration *declarations;
1105 uint maxInstructions = 10, numInstructions = 0;
1106 uint maxDeclarations = 10, numDeclarations = 0;
1107
1108 #if 0
1109 tgsi_dump(tokens, 0);
1110 #endif
1111
1112 util_init_math();
1113
1114
1115 mach->Tokens = tokens;
1116 mach->Sampler = sampler;
1117 mach->Image = image;
1118 mach->Buffer = buffer;
1119
1120 if (!tokens) {
1121 /* unbind and free all */
1122 FREE(mach->Declarations);
1123 mach->Declarations = NULL;
1124 mach->NumDeclarations = 0;
1125
1126 FREE(mach->Instructions);
1127 mach->Instructions = NULL;
1128 mach->NumInstructions = 0;
1129
1130 return;
1131 }
1132
1133 k = tgsi_parse_init (&parse, mach->Tokens);
1134 if (k != TGSI_PARSE_OK) {
1135 debug_printf( "Problem parsing!\n" );
1136 return;
1137 }
1138
1139 mach->ImmLimit = 0;
1140 mach->NumOutputs = 0;
1141
1142 for (k = 0; k < TGSI_SEMANTIC_COUNT; k++)
1143 mach->SysSemanticToIndex[k] = -1;
1144
1145 if (mach->ShaderType == PIPE_SHADER_GEOMETRY &&
1146 !mach->UsedGeometryShader) {
1147 struct tgsi_exec_vector *inputs;
1148 struct tgsi_exec_vector *outputs;
1149
1150 inputs = align_malloc(sizeof(struct tgsi_exec_vector) *
1151 TGSI_MAX_PRIM_VERTICES * PIPE_MAX_SHADER_INPUTS,
1152 16);
1153
1154 if (!inputs)
1155 return;
1156
1157 outputs = align_malloc(sizeof(struct tgsi_exec_vector) *
1158 TGSI_MAX_TOTAL_VERTICES, 16);
1159
1160 if (!outputs) {
1161 align_free(inputs);
1162 return;
1163 }
1164
1165 align_free(mach->Inputs);
1166 align_free(mach->Outputs);
1167
1168 mach->Inputs = inputs;
1169 mach->Outputs = outputs;
1170 mach->UsedGeometryShader = TRUE;
1171 }
1172
1173 declarations = (struct tgsi_full_declaration *)
1174 MALLOC( maxDeclarations * sizeof(struct tgsi_full_declaration) );
1175
1176 if (!declarations) {
1177 return;
1178 }
1179
1180 instructions = (struct tgsi_full_instruction *)
1181 MALLOC( maxInstructions * sizeof(struct tgsi_full_instruction) );
1182
1183 if (!instructions) {
1184 FREE( declarations );
1185 return;
1186 }
1187
1188 while( !tgsi_parse_end_of_tokens( &parse ) ) {
1189 uint i;
1190
1191 tgsi_parse_token( &parse );
1192 switch( parse.FullToken.Token.Type ) {
1193 case TGSI_TOKEN_TYPE_DECLARATION:
1194 /* save expanded declaration */
1195 if (numDeclarations == maxDeclarations) {
1196 declarations = REALLOC(declarations,
1197 maxDeclarations
1198 * sizeof(struct tgsi_full_declaration),
1199 (maxDeclarations + 10)
1200 * sizeof(struct tgsi_full_declaration));
1201 maxDeclarations += 10;
1202 }
1203 if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_OUTPUT) {
1204 unsigned reg;
1205 for (reg = parse.FullToken.FullDeclaration.Range.First;
1206 reg <= parse.FullToken.FullDeclaration.Range.Last;
1207 ++reg) {
1208 ++mach->NumOutputs;
1209 }
1210 }
1211 else if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_SYSTEM_VALUE) {
1212 const struct tgsi_full_declaration *decl = &parse.FullToken.FullDeclaration;
1213 mach->SysSemanticToIndex[decl->Semantic.Name] = decl->Range.First;
1214 }
1215
1216 memcpy(declarations + numDeclarations,
1217 &parse.FullToken.FullDeclaration,
1218 sizeof(declarations[0]));
1219 numDeclarations++;
1220 break;
1221
1222 case TGSI_TOKEN_TYPE_IMMEDIATE:
1223 {
1224 uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
1225 assert( size <= 4 );
1226 assert( mach->ImmLimit + 1 <= TGSI_EXEC_NUM_IMMEDIATES );
1227
1228 for( i = 0; i < size; i++ ) {
1229 mach->Imms[mach->ImmLimit][i] =
1230 parse.FullToken.FullImmediate.u[i].Float;
1231 }
1232 mach->ImmLimit += 1;
1233 }
1234 break;
1235
1236 case TGSI_TOKEN_TYPE_INSTRUCTION:
1237
1238 /* save expanded instruction */
1239 if (numInstructions == maxInstructions) {
1240 instructions = REALLOC(instructions,
1241 maxInstructions
1242 * sizeof(struct tgsi_full_instruction),
1243 (maxInstructions + 10)
1244 * sizeof(struct tgsi_full_instruction));
1245 maxInstructions += 10;
1246 }
1247
1248 memcpy(instructions + numInstructions,
1249 &parse.FullToken.FullInstruction,
1250 sizeof(instructions[0]));
1251
1252 numInstructions++;
1253 break;
1254
1255 case TGSI_TOKEN_TYPE_PROPERTY:
1256 if (mach->ShaderType == PIPE_SHADER_GEOMETRY) {
1257 if (parse.FullToken.FullProperty.Property.PropertyName == TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES) {
1258 mach->MaxOutputVertices = parse.FullToken.FullProperty.u[0].Data;
1259 }
1260 }
1261 break;
1262
1263 default:
1264 assert( 0 );
1265 }
1266 }
1267 tgsi_parse_free (&parse);
1268
1269 FREE(mach->Declarations);
1270 mach->Declarations = declarations;
1271 mach->NumDeclarations = numDeclarations;
1272
1273 FREE(mach->Instructions);
1274 mach->Instructions = instructions;
1275 mach->NumInstructions = numInstructions;
1276 }
1277
1278
1279 struct tgsi_exec_machine *
1280 tgsi_exec_machine_create(enum pipe_shader_type shader_type)
1281 {
1282 struct tgsi_exec_machine *mach;
1283 uint i;
1284
1285 mach = align_malloc( sizeof *mach, 16 );
1286 if (!mach)
1287 goto fail;
1288
1289 memset(mach, 0, sizeof(*mach));
1290
1291 mach->ShaderType = shader_type;
1292 mach->Addrs = &mach->Temps[TGSI_EXEC_TEMP_ADDR];
1293 mach->MaxGeometryShaderOutputs = TGSI_MAX_TOTAL_VERTICES;
1294 mach->Predicates = &mach->Temps[TGSI_EXEC_TEMP_P0];
1295
1296 if (shader_type != PIPE_SHADER_COMPUTE) {
1297 mach->Inputs = align_malloc(sizeof(struct tgsi_exec_vector) * PIPE_MAX_SHADER_INPUTS, 16);
1298 mach->Outputs = align_malloc(sizeof(struct tgsi_exec_vector) * PIPE_MAX_SHADER_OUTPUTS, 16);
1299 if (!mach->Inputs || !mach->Outputs)
1300 goto fail;
1301 }
1302
1303 /* Setup constants needed by the SSE2 executor. */
1304 for( i = 0; i < 4; i++ ) {
1305 mach->Temps[TGSI_EXEC_TEMP_00000000_I].xyzw[TGSI_EXEC_TEMP_00000000_C].u[i] = 0x00000000;
1306 mach->Temps[TGSI_EXEC_TEMP_7FFFFFFF_I].xyzw[TGSI_EXEC_TEMP_7FFFFFFF_C].u[i] = 0x7FFFFFFF;
1307 mach->Temps[TGSI_EXEC_TEMP_80000000_I].xyzw[TGSI_EXEC_TEMP_80000000_C].u[i] = 0x80000000;
1308 mach->Temps[TGSI_EXEC_TEMP_FFFFFFFF_I].xyzw[TGSI_EXEC_TEMP_FFFFFFFF_C].u[i] = 0xFFFFFFFF; /* not used */
1309 mach->Temps[TGSI_EXEC_TEMP_ONE_I].xyzw[TGSI_EXEC_TEMP_ONE_C].f[i] = 1.0f;
1310 mach->Temps[TGSI_EXEC_TEMP_TWO_I].xyzw[TGSI_EXEC_TEMP_TWO_C].f[i] = 2.0f; /* not used */
1311 mach->Temps[TGSI_EXEC_TEMP_128_I].xyzw[TGSI_EXEC_TEMP_128_C].f[i] = 128.0f;
1312 mach->Temps[TGSI_EXEC_TEMP_MINUS_128_I].xyzw[TGSI_EXEC_TEMP_MINUS_128_C].f[i] = -128.0f;
1313 mach->Temps[TGSI_EXEC_TEMP_THREE_I].xyzw[TGSI_EXEC_TEMP_THREE_C].f[i] = 3.0f;
1314 mach->Temps[TGSI_EXEC_TEMP_HALF_I].xyzw[TGSI_EXEC_TEMP_HALF_C].f[i] = 0.5f;
1315 }
1316
1317 #ifdef DEBUG
1318 /* silence warnings */
1319 (void) print_chan;
1320 (void) print_temp;
1321 #endif
1322
1323 return mach;
1324
1325 fail:
1326 if (mach) {
1327 align_free(mach->Inputs);
1328 align_free(mach->Outputs);
1329 align_free(mach);
1330 }
1331 return NULL;
1332 }
1333
1334
1335 void
1336 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach)
1337 {
1338 if (mach) {
1339 FREE(mach->Instructions);
1340 FREE(mach->Declarations);
1341
1342 align_free(mach->Inputs);
1343 align_free(mach->Outputs);
1344
1345 align_free(mach);
1346 }
1347 }
1348
1349 static void
1350 micro_add(union tgsi_exec_channel *dst,
1351 const union tgsi_exec_channel *src0,
1352 const union tgsi_exec_channel *src1)
1353 {
1354 dst->f[0] = src0->f[0] + src1->f[0];
1355 dst->f[1] = src0->f[1] + src1->f[1];
1356 dst->f[2] = src0->f[2] + src1->f[2];
1357 dst->f[3] = src0->f[3] + src1->f[3];
1358 }
1359
1360 static void
1361 micro_div(
1362 union tgsi_exec_channel *dst,
1363 const union tgsi_exec_channel *src0,
1364 const union tgsi_exec_channel *src1 )
1365 {
1366 if (src1->f[0] != 0) {
1367 dst->f[0] = src0->f[0] / src1->f[0];
1368 }
1369 if (src1->f[1] != 0) {
1370 dst->f[1] = src0->f[1] / src1->f[1];
1371 }
1372 if (src1->f[2] != 0) {
1373 dst->f[2] = src0->f[2] / src1->f[2];
1374 }
1375 if (src1->f[3] != 0) {
1376 dst->f[3] = src0->f[3] / src1->f[3];
1377 }
1378 }
1379
1380 static void
1381 micro_lt(
1382 union tgsi_exec_channel *dst,
1383 const union tgsi_exec_channel *src0,
1384 const union tgsi_exec_channel *src1,
1385 const union tgsi_exec_channel *src2,
1386 const union tgsi_exec_channel *src3 )
1387 {
1388 dst->f[0] = src0->f[0] < src1->f[0] ? src2->f[0] : src3->f[0];
1389 dst->f[1] = src0->f[1] < src1->f[1] ? src2->f[1] : src3->f[1];
1390 dst->f[2] = src0->f[2] < src1->f[2] ? src2->f[2] : src3->f[2];
1391 dst->f[3] = src0->f[3] < src1->f[3] ? src2->f[3] : src3->f[3];
1392 }
1393
1394 static void
1395 micro_max(union tgsi_exec_channel *dst,
1396 const union tgsi_exec_channel *src0,
1397 const union tgsi_exec_channel *src1)
1398 {
1399 dst->f[0] = src0->f[0] > src1->f[0] ? src0->f[0] : src1->f[0];
1400 dst->f[1] = src0->f[1] > src1->f[1] ? src0->f[1] : src1->f[1];
1401 dst->f[2] = src0->f[2] > src1->f[2] ? src0->f[2] : src1->f[2];
1402 dst->f[3] = src0->f[3] > src1->f[3] ? src0->f[3] : src1->f[3];
1403 }
1404
1405 static void
1406 micro_min(union tgsi_exec_channel *dst,
1407 const union tgsi_exec_channel *src0,
1408 const union tgsi_exec_channel *src1)
1409 {
1410 dst->f[0] = src0->f[0] < src1->f[0] ? src0->f[0] : src1->f[0];
1411 dst->f[1] = src0->f[1] < src1->f[1] ? src0->f[1] : src1->f[1];
1412 dst->f[2] = src0->f[2] < src1->f[2] ? src0->f[2] : src1->f[2];
1413 dst->f[3] = src0->f[3] < src1->f[3] ? src0->f[3] : src1->f[3];
1414 }
1415
1416 static void
1417 micro_mul(union tgsi_exec_channel *dst,
1418 const union tgsi_exec_channel *src0,
1419 const union tgsi_exec_channel *src1)
1420 {
1421 dst->f[0] = src0->f[0] * src1->f[0];
1422 dst->f[1] = src0->f[1] * src1->f[1];
1423 dst->f[2] = src0->f[2] * src1->f[2];
1424 dst->f[3] = src0->f[3] * src1->f[3];
1425 }
1426
1427 static void
1428 micro_neg(
1429 union tgsi_exec_channel *dst,
1430 const union tgsi_exec_channel *src )
1431 {
1432 dst->f[0] = -src->f[0];
1433 dst->f[1] = -src->f[1];
1434 dst->f[2] = -src->f[2];
1435 dst->f[3] = -src->f[3];
1436 }
1437
1438 static void
1439 micro_pow(
1440 union tgsi_exec_channel *dst,
1441 const union tgsi_exec_channel *src0,
1442 const union tgsi_exec_channel *src1 )
1443 {
1444 #if FAST_MATH
1445 dst->f[0] = util_fast_pow( src0->f[0], src1->f[0] );
1446 dst->f[1] = util_fast_pow( src0->f[1], src1->f[1] );
1447 dst->f[2] = util_fast_pow( src0->f[2], src1->f[2] );
1448 dst->f[3] = util_fast_pow( src0->f[3], src1->f[3] );
1449 #else
1450 dst->f[0] = powf( src0->f[0], src1->f[0] );
1451 dst->f[1] = powf( src0->f[1], src1->f[1] );
1452 dst->f[2] = powf( src0->f[2], src1->f[2] );
1453 dst->f[3] = powf( src0->f[3], src1->f[3] );
1454 #endif
1455 }
1456
1457 static void
1458 micro_sub(union tgsi_exec_channel *dst,
1459 const union tgsi_exec_channel *src0,
1460 const union tgsi_exec_channel *src1)
1461 {
1462 dst->f[0] = src0->f[0] - src1->f[0];
1463 dst->f[1] = src0->f[1] - src1->f[1];
1464 dst->f[2] = src0->f[2] - src1->f[2];
1465 dst->f[3] = src0->f[3] - src1->f[3];
1466 }
1467
1468 static void
1469 fetch_src_file_channel(const struct tgsi_exec_machine *mach,
1470 const uint chan_index,
1471 const uint file,
1472 const uint swizzle,
1473 const union tgsi_exec_channel *index,
1474 const union tgsi_exec_channel *index2D,
1475 union tgsi_exec_channel *chan)
1476 {
1477 uint i;
1478
1479 assert(swizzle < 4);
1480
1481 switch (file) {
1482 case TGSI_FILE_CONSTANT:
1483 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1484 assert(index2D->i[i] >= 0 && index2D->i[i] < PIPE_MAX_CONSTANT_BUFFERS);
1485 assert(mach->Consts[index2D->i[i]]);
1486
1487 if (index->i[i] < 0) {
1488 chan->u[i] = 0;
1489 } else {
1490 /* NOTE: copying the const value as a uint instead of float */
1491 const uint constbuf = index2D->i[i];
1492 const uint *buf = (const uint *)mach->Consts[constbuf];
1493 const int pos = index->i[i] * 4 + swizzle;
1494 /* const buffer bounds check */
1495 if (pos < 0 || pos >= (int) mach->ConstsSize[constbuf]) {
1496 if (0) {
1497 /* Debug: print warning */
1498 static int count = 0;
1499 if (count++ < 100)
1500 debug_printf("TGSI Exec: const buffer index %d"
1501 " out of bounds\n", pos);
1502 }
1503 chan->u[i] = 0;
1504 }
1505 else
1506 chan->u[i] = buf[pos];
1507 }
1508 }
1509 break;
1510
1511 case TGSI_FILE_INPUT:
1512 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1513 /*
1514 if (PIPE_SHADER_GEOMETRY == mach->ShaderType) {
1515 debug_printf("Fetching Input[%d] (2d=%d, 1d=%d)\n",
1516 index2D->i[i] * TGSI_EXEC_MAX_INPUT_ATTRIBS + index->i[i],
1517 index2D->i[i], index->i[i]);
1518 }*/
1519 int pos = index2D->i[i] * TGSI_EXEC_MAX_INPUT_ATTRIBS + index->i[i];
1520 assert(pos >= 0);
1521 assert(pos < TGSI_MAX_PRIM_VERTICES * PIPE_MAX_ATTRIBS);
1522 chan->u[i] = mach->Inputs[pos].xyzw[swizzle].u[i];
1523 }
1524 break;
1525
1526 case TGSI_FILE_SYSTEM_VALUE:
1527 /* XXX no swizzling at this point. Will be needed if we put
1528 * gl_FragCoord, for example, in a sys value register.
1529 */
1530 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1531 chan->u[i] = mach->SystemValue[index->i[i]].xyzw[swizzle].u[i];
1532 }
1533 break;
1534
1535 case TGSI_FILE_TEMPORARY:
1536 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1537 assert(index->i[i] < TGSI_EXEC_NUM_TEMPS);
1538 assert(index2D->i[i] == 0);
1539
1540 chan->u[i] = mach->Temps[index->i[i]].xyzw[swizzle].u[i];
1541 }
1542 break;
1543
1544 case TGSI_FILE_IMMEDIATE:
1545 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1546 assert(index->i[i] >= 0 && index->i[i] < (int)mach->ImmLimit);
1547 assert(index2D->i[i] == 0);
1548
1549 chan->f[i] = mach->Imms[index->i[i]][swizzle];
1550 }
1551 break;
1552
1553 case TGSI_FILE_ADDRESS:
1554 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1555 assert(index->i[i] >= 0);
1556 assert(index2D->i[i] == 0);
1557
1558 chan->u[i] = mach->Addrs[index->i[i]].xyzw[swizzle].u[i];
1559 }
1560 break;
1561
1562 case TGSI_FILE_PREDICATE:
1563 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1564 assert(index->i[i] >= 0 && index->i[i] < TGSI_EXEC_NUM_PREDS);
1565 assert(index2D->i[i] == 0);
1566
1567 chan->u[i] = mach->Predicates[0].xyzw[swizzle].u[i];
1568 }
1569 break;
1570
1571 case TGSI_FILE_OUTPUT:
1572 /* vertex/fragment output vars can be read too */
1573 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1574 assert(index->i[i] >= 0);
1575 assert(index2D->i[i] == 0);
1576
1577 chan->u[i] = mach->Outputs[index->i[i]].xyzw[swizzle].u[i];
1578 }
1579 break;
1580
1581 default:
1582 assert(0);
1583 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1584 chan->u[i] = 0;
1585 }
1586 }
1587 }
1588
1589 static void
1590 fetch_source_d(const struct tgsi_exec_machine *mach,
1591 union tgsi_exec_channel *chan,
1592 const struct tgsi_full_src_register *reg,
1593 const uint chan_index,
1594 enum tgsi_exec_datatype src_datatype)
1595 {
1596 union tgsi_exec_channel index;
1597 union tgsi_exec_channel index2D;
1598 uint swizzle;
1599
1600 /* We start with a direct index into a register file.
1601 *
1602 * file[1],
1603 * where:
1604 * file = Register.File
1605 * [1] = Register.Index
1606 */
1607 index.i[0] =
1608 index.i[1] =
1609 index.i[2] =
1610 index.i[3] = reg->Register.Index;
1611
1612 /* There is an extra source register that indirectly subscripts
1613 * a register file. The direct index now becomes an offset
1614 * that is being added to the indirect register.
1615 *
1616 * file[ind[2].x+1],
1617 * where:
1618 * ind = Indirect.File
1619 * [2] = Indirect.Index
1620 * .x = Indirect.SwizzleX
1621 */
1622 if (reg->Register.Indirect) {
1623 union tgsi_exec_channel index2;
1624 union tgsi_exec_channel indir_index;
1625 const uint execmask = mach->ExecMask;
1626 uint i;
1627
1628 /* which address register (always zero now) */
1629 index2.i[0] =
1630 index2.i[1] =
1631 index2.i[2] =
1632 index2.i[3] = reg->Indirect.Index;
1633 /* get current value of address register[swizzle] */
1634 swizzle = reg->Indirect.Swizzle;
1635 fetch_src_file_channel(mach,
1636 chan_index,
1637 reg->Indirect.File,
1638 swizzle,
1639 &index2,
1640 &ZeroVec,
1641 &indir_index);
1642
1643 /* add value of address register to the offset */
1644 index.i[0] += indir_index.i[0];
1645 index.i[1] += indir_index.i[1];
1646 index.i[2] += indir_index.i[2];
1647 index.i[3] += indir_index.i[3];
1648
1649 /* for disabled execution channels, zero-out the index to
1650 * avoid using a potential garbage value.
1651 */
1652 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1653 if ((execmask & (1 << i)) == 0)
1654 index.i[i] = 0;
1655 }
1656 }
1657
1658 /* There is an extra source register that is a second
1659 * subscript to a register file. Effectively it means that
1660 * the register file is actually a 2D array of registers.
1661 *
1662 * file[3][1],
1663 * where:
1664 * [3] = Dimension.Index
1665 */
1666 if (reg->Register.Dimension) {
1667 index2D.i[0] =
1668 index2D.i[1] =
1669 index2D.i[2] =
1670 index2D.i[3] = reg->Dimension.Index;
1671
1672 /* Again, the second subscript index can be addressed indirectly
1673 * identically to the first one.
1674 * Nothing stops us from indirectly addressing the indirect register,
1675 * but there is no need for that, so we won't exercise it.
1676 *
1677 * file[ind[4].y+3][1],
1678 * where:
1679 * ind = DimIndirect.File
1680 * [4] = DimIndirect.Index
1681 * .y = DimIndirect.SwizzleX
1682 */
1683 if (reg->Dimension.Indirect) {
1684 union tgsi_exec_channel index2;
1685 union tgsi_exec_channel indir_index;
1686 const uint execmask = mach->ExecMask;
1687 uint i;
1688
1689 index2.i[0] =
1690 index2.i[1] =
1691 index2.i[2] =
1692 index2.i[3] = reg->DimIndirect.Index;
1693
1694 swizzle = reg->DimIndirect.Swizzle;
1695 fetch_src_file_channel(mach,
1696 chan_index,
1697 reg->DimIndirect.File,
1698 swizzle,
1699 &index2,
1700 &ZeroVec,
1701 &indir_index);
1702
1703 index2D.i[0] += indir_index.i[0];
1704 index2D.i[1] += indir_index.i[1];
1705 index2D.i[2] += indir_index.i[2];
1706 index2D.i[3] += indir_index.i[3];
1707
1708 /* for disabled execution channels, zero-out the index to
1709 * avoid using a potential garbage value.
1710 */
1711 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1712 if ((execmask & (1 << i)) == 0) {
1713 index2D.i[i] = 0;
1714 }
1715 }
1716 }
1717
1718 /* If by any chance there was a need for a 3D array of register
1719 * files, we would have to check whether Dimension is followed
1720 * by a dimension register and continue the saga.
1721 */
1722 } else {
1723 index2D.i[0] =
1724 index2D.i[1] =
1725 index2D.i[2] =
1726 index2D.i[3] = 0;
1727 }
1728
1729 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
1730 fetch_src_file_channel(mach,
1731 chan_index,
1732 reg->Register.File,
1733 swizzle,
1734 &index,
1735 &index2D,
1736 chan);
1737 }
1738
1739 static void
1740 fetch_source(const struct tgsi_exec_machine *mach,
1741 union tgsi_exec_channel *chan,
1742 const struct tgsi_full_src_register *reg,
1743 const uint chan_index,
1744 enum tgsi_exec_datatype src_datatype)
1745 {
1746 fetch_source_d(mach, chan, reg, chan_index, src_datatype);
1747
1748 if (reg->Register.Absolute) {
1749 if (src_datatype == TGSI_EXEC_DATA_FLOAT) {
1750 micro_abs(chan, chan);
1751 } else {
1752 micro_iabs(chan, chan);
1753 }
1754 }
1755
1756 if (reg->Register.Negate) {
1757 if (src_datatype == TGSI_EXEC_DATA_FLOAT) {
1758 micro_neg(chan, chan);
1759 } else {
1760 micro_ineg(chan, chan);
1761 }
1762 }
1763 }
1764
1765 static union tgsi_exec_channel *
1766 store_dest_dstret(struct tgsi_exec_machine *mach,
1767 const union tgsi_exec_channel *chan,
1768 const struct tgsi_full_dst_register *reg,
1769 const struct tgsi_full_instruction *inst,
1770 uint chan_index,
1771 enum tgsi_exec_datatype dst_datatype)
1772 {
1773 uint i;
1774 static union tgsi_exec_channel null;
1775 union tgsi_exec_channel *dst;
1776 union tgsi_exec_channel index2D;
1777 uint execmask = mach->ExecMask;
1778 int offset = 0; /* indirection offset */
1779 int index;
1780
1781 /* for debugging */
1782 if (0 && dst_datatype == TGSI_EXEC_DATA_FLOAT) {
1783 check_inf_or_nan(chan);
1784 }
1785
1786 /* There is an extra source register that indirectly subscripts
1787 * a register file. The direct index now becomes an offset
1788 * that is being added to the indirect register.
1789 *
1790 * file[ind[2].x+1],
1791 * where:
1792 * ind = Indirect.File
1793 * [2] = Indirect.Index
1794 * .x = Indirect.SwizzleX
1795 */
1796 if (reg->Register.Indirect) {
1797 union tgsi_exec_channel index;
1798 union tgsi_exec_channel indir_index;
1799 uint swizzle;
1800
1801 /* which address register (always zero for now) */
1802 index.i[0] =
1803 index.i[1] =
1804 index.i[2] =
1805 index.i[3] = reg->Indirect.Index;
1806
1807 /* get current value of address register[swizzle] */
1808 swizzle = reg->Indirect.Swizzle;
1809
1810 /* fetch values from the address/indirection register */
1811 fetch_src_file_channel(mach,
1812 chan_index,
1813 reg->Indirect.File,
1814 swizzle,
1815 &index,
1816 &ZeroVec,
1817 &indir_index);
1818
1819 /* save indirection offset */
1820 offset = indir_index.i[0];
1821 }
1822
1823 /* There is an extra source register that is a second
1824 * subscript to a register file. Effectively it means that
1825 * the register file is actually a 2D array of registers.
1826 *
1827 * file[3][1],
1828 * where:
1829 * [3] = Dimension.Index
1830 */
1831 if (reg->Register.Dimension) {
1832 index2D.i[0] =
1833 index2D.i[1] =
1834 index2D.i[2] =
1835 index2D.i[3] = reg->Dimension.Index;
1836
1837 /* Again, the second subscript index can be addressed indirectly
1838 * identically to the first one.
1839 * Nothing stops us from indirectly addressing the indirect register,
1840 * but there is no need for that, so we won't exercise it.
1841 *
1842 * file[ind[4].y+3][1],
1843 * where:
1844 * ind = DimIndirect.File
1845 * [4] = DimIndirect.Index
1846 * .y = DimIndirect.SwizzleX
1847 */
1848 if (reg->Dimension.Indirect) {
1849 union tgsi_exec_channel index2;
1850 union tgsi_exec_channel indir_index;
1851 const uint execmask = mach->ExecMask;
1852 unsigned swizzle;
1853 uint i;
1854
1855 index2.i[0] =
1856 index2.i[1] =
1857 index2.i[2] =
1858 index2.i[3] = reg->DimIndirect.Index;
1859
1860 swizzle = reg->DimIndirect.Swizzle;
1861 fetch_src_file_channel(mach,
1862 chan_index,
1863 reg->DimIndirect.File,
1864 swizzle,
1865 &index2,
1866 &ZeroVec,
1867 &indir_index);
1868
1869 index2D.i[0] += indir_index.i[0];
1870 index2D.i[1] += indir_index.i[1];
1871 index2D.i[2] += indir_index.i[2];
1872 index2D.i[3] += indir_index.i[3];
1873
1874 /* for disabled execution channels, zero-out the index to
1875 * avoid using a potential garbage value.
1876 */
1877 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1878 if ((execmask & (1 << i)) == 0) {
1879 index2D.i[i] = 0;
1880 }
1881 }
1882 }
1883
1884 /* If by any chance there was a need for a 3D array of register
1885 * files, we would have to check whether Dimension is followed
1886 * by a dimension register and continue the saga.
1887 */
1888 } else {
1889 index2D.i[0] =
1890 index2D.i[1] =
1891 index2D.i[2] =
1892 index2D.i[3] = 0;
1893 }
1894
1895 switch (reg->Register.File) {
1896 case TGSI_FILE_NULL:
1897 dst = &null;
1898 break;
1899
1900 case TGSI_FILE_OUTPUT:
1901 index = mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0]
1902 + reg->Register.Index;
1903 dst = &mach->Outputs[offset + index].xyzw[chan_index];
1904 #if 0
1905 debug_printf("NumOutputs = %d, TEMP_O_C/I = %d, redindex = %d\n",
1906 mach->NumOutputs, mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0],
1907 reg->Register.Index);
1908 if (PIPE_SHADER_GEOMETRY == mach->ShaderType) {
1909 debug_printf("STORING OUT[%d] mask(%d), = (", offset + index, execmask);
1910 for (i = 0; i < TGSI_QUAD_SIZE; i++)
1911 if (execmask & (1 << i))
1912 debug_printf("%f, ", chan->f[i]);
1913 debug_printf(")\n");
1914 }
1915 #endif
1916 break;
1917
1918 case TGSI_FILE_TEMPORARY:
1919 index = reg->Register.Index;
1920 assert( index < TGSI_EXEC_NUM_TEMPS );
1921 dst = &mach->Temps[offset + index].xyzw[chan_index];
1922 break;
1923
1924 case TGSI_FILE_ADDRESS:
1925 index = reg->Register.Index;
1926 dst = &mach->Addrs[index].xyzw[chan_index];
1927 break;
1928
1929 case TGSI_FILE_PREDICATE:
1930 index = reg->Register.Index;
1931 assert(index < TGSI_EXEC_NUM_PREDS);
1932 dst = &mach->Predicates[index].xyzw[chan_index];
1933 break;
1934
1935 default:
1936 assert( 0 );
1937 return NULL;
1938 }
1939
1940 if (inst->Instruction.Predicate) {
1941 uint swizzle;
1942 union tgsi_exec_channel *pred;
1943
1944 switch (chan_index) {
1945 case TGSI_CHAN_X:
1946 swizzle = inst->Predicate.SwizzleX;
1947 break;
1948 case TGSI_CHAN_Y:
1949 swizzle = inst->Predicate.SwizzleY;
1950 break;
1951 case TGSI_CHAN_Z:
1952 swizzle = inst->Predicate.SwizzleZ;
1953 break;
1954 case TGSI_CHAN_W:
1955 swizzle = inst->Predicate.SwizzleW;
1956 break;
1957 default:
1958 assert(0);
1959 return NULL;
1960 }
1961
1962 assert(inst->Predicate.Index == 0);
1963
1964 pred = &mach->Predicates[inst->Predicate.Index].xyzw[swizzle];
1965
1966 if (inst->Predicate.Negate) {
1967 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1968 if (pred->u[i]) {
1969 execmask &= ~(1 << i);
1970 }
1971 }
1972 } else {
1973 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
1974 if (!pred->u[i]) {
1975 execmask &= ~(1 << i);
1976 }
1977 }
1978 }
1979 }
1980
1981 return dst;
1982 }
1983
1984 static void
1985 store_dest_double(struct tgsi_exec_machine *mach,
1986 const union tgsi_exec_channel *chan,
1987 const struct tgsi_full_dst_register *reg,
1988 const struct tgsi_full_instruction *inst,
1989 uint chan_index,
1990 enum tgsi_exec_datatype dst_datatype)
1991 {
1992 union tgsi_exec_channel *dst;
1993 const uint execmask = mach->ExecMask;
1994 int i;
1995
1996 dst = store_dest_dstret(mach, chan, reg, inst, chan_index,
1997 dst_datatype);
1998 if (!dst)
1999 return;
2000
2001 /* doubles path */
2002 for (i = 0; i < TGSI_QUAD_SIZE; i++)
2003 if (execmask & (1 << i))
2004 dst->i[i] = chan->i[i];
2005 }
2006
2007 static void
2008 store_dest(struct tgsi_exec_machine *mach,
2009 const union tgsi_exec_channel *chan,
2010 const struct tgsi_full_dst_register *reg,
2011 const struct tgsi_full_instruction *inst,
2012 uint chan_index,
2013 enum tgsi_exec_datatype dst_datatype)
2014 {
2015 union tgsi_exec_channel *dst;
2016 const uint execmask = mach->ExecMask;
2017 int i;
2018
2019 dst = store_dest_dstret(mach, chan, reg, inst, chan_index,
2020 dst_datatype);
2021 if (!dst)
2022 return;
2023
2024 if (!inst->Instruction.Saturate) {
2025 for (i = 0; i < TGSI_QUAD_SIZE; i++)
2026 if (execmask & (1 << i))
2027 dst->i[i] = chan->i[i];
2028 }
2029 else {
2030 for (i = 0; i < TGSI_QUAD_SIZE; i++)
2031 if (execmask & (1 << i)) {
2032 if (chan->f[i] < 0.0f)
2033 dst->f[i] = 0.0f;
2034 else if (chan->f[i] > 1.0f)
2035 dst->f[i] = 1.0f;
2036 else
2037 dst->i[i] = chan->i[i];
2038 }
2039 }
2040 }
2041
2042 #define FETCH(VAL,INDEX,CHAN)\
2043 fetch_source(mach, VAL, &inst->Src[INDEX], CHAN, TGSI_EXEC_DATA_FLOAT)
2044
2045 #define IFETCH(VAL,INDEX,CHAN)\
2046 fetch_source(mach, VAL, &inst->Src[INDEX], CHAN, TGSI_EXEC_DATA_INT)
2047
2048
2049 /**
2050 * Execute ARB-style KIL which is predicated by a src register.
2051 * Kill fragment if any of the four values is less than zero.
2052 */
2053 static void
2054 exec_kill_if(struct tgsi_exec_machine *mach,
2055 const struct tgsi_full_instruction *inst)
2056 {
2057 uint uniquemask;
2058 uint chan_index;
2059 uint kilmask = 0; /* bit 0 = pixel 0, bit 1 = pixel 1, etc */
2060 union tgsi_exec_channel r[1];
2061
2062 /* This mask stores component bits that were already tested. */
2063 uniquemask = 0;
2064
2065 for (chan_index = 0; chan_index < 4; chan_index++)
2066 {
2067 uint swizzle;
2068 uint i;
2069
2070 /* unswizzle channel */
2071 swizzle = tgsi_util_get_full_src_register_swizzle (
2072 &inst->Src[0],
2073 chan_index);
2074
2075 /* check if the component has not been already tested */
2076 if (uniquemask & (1 << swizzle))
2077 continue;
2078 uniquemask |= 1 << swizzle;
2079
2080 FETCH(&r[0], 0, chan_index);
2081 for (i = 0; i < 4; i++)
2082 if (r[0].f[i] < 0.0f)
2083 kilmask |= 1 << i;
2084 }
2085
2086 /* restrict to fragments currently executing */
2087 kilmask &= mach->ExecMask;
2088
2089 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] |= kilmask;
2090 }
2091
2092 /**
2093 * Unconditional fragment kill/discard.
2094 */
2095 static void
2096 exec_kill(struct tgsi_exec_machine *mach,
2097 const struct tgsi_full_instruction *inst)
2098 {
2099 uint kilmask; /* bit 0 = pixel 0, bit 1 = pixel 1, etc */
2100
2101 /* kill fragment for all fragments currently executing */
2102 kilmask = mach->ExecMask;
2103 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] |= kilmask;
2104 }
2105
2106 static void
2107 emit_vertex(struct tgsi_exec_machine *mach)
2108 {
2109 /* FIXME: check for exec mask correctly
2110 unsigned i;
2111 for (i = 0; i < TGSI_QUAD_SIZE; ++i) {
2112 if ((mach->ExecMask & (1 << i)))
2113 */
2114 if (mach->ExecMask) {
2115 if (mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]] >= mach->MaxOutputVertices)
2116 return;
2117
2118 mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0] += mach->NumOutputs;
2119 mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]]++;
2120 }
2121 }
2122
2123 static void
2124 emit_primitive(struct tgsi_exec_machine *mach)
2125 {
2126 unsigned *prim_count = &mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0];
2127 /* FIXME: check for exec mask correctly
2128 unsigned i;
2129 for (i = 0; i < TGSI_QUAD_SIZE; ++i) {
2130 if ((mach->ExecMask & (1 << i)))
2131 */
2132 if (mach->ExecMask) {
2133 ++(*prim_count);
2134 debug_assert((*prim_count * mach->NumOutputs) < mach->MaxGeometryShaderOutputs);
2135 mach->Primitives[*prim_count] = 0;
2136 }
2137 }
2138
2139 static void
2140 conditional_emit_primitive(struct tgsi_exec_machine *mach)
2141 {
2142 if (PIPE_SHADER_GEOMETRY == mach->ShaderType) {
2143 int emitted_verts =
2144 mach->Primitives[mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0]];
2145 if (emitted_verts) {
2146 emit_primitive(mach);
2147 }
2148 }
2149 }
2150
2151
2152 /*
2153 * Fetch four texture samples using STR texture coordinates.
2154 */
2155 static void
2156 fetch_texel( struct tgsi_sampler *sampler,
2157 const unsigned sview_idx,
2158 const unsigned sampler_idx,
2159 const union tgsi_exec_channel *s,
2160 const union tgsi_exec_channel *t,
2161 const union tgsi_exec_channel *p,
2162 const union tgsi_exec_channel *c0,
2163 const union tgsi_exec_channel *c1,
2164 float derivs[3][2][TGSI_QUAD_SIZE],
2165 const int8_t offset[3],
2166 enum tgsi_sampler_control control,
2167 union tgsi_exec_channel *r,
2168 union tgsi_exec_channel *g,
2169 union tgsi_exec_channel *b,
2170 union tgsi_exec_channel *a )
2171 {
2172 uint j;
2173 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
2174
2175 /* FIXME: handle explicit derivs, offsets */
2176 sampler->get_samples(sampler, sview_idx, sampler_idx,
2177 s->f, t->f, p->f, c0->f, c1->f, derivs, offset, control, rgba);
2178
2179 for (j = 0; j < 4; j++) {
2180 r->f[j] = rgba[0][j];
2181 g->f[j] = rgba[1][j];
2182 b->f[j] = rgba[2][j];
2183 a->f[j] = rgba[3][j];
2184 }
2185 }
2186
2187
2188 #define TEX_MODIFIER_NONE 0
2189 #define TEX_MODIFIER_PROJECTED 1
2190 #define TEX_MODIFIER_LOD_BIAS 2
2191 #define TEX_MODIFIER_EXPLICIT_LOD 3
2192 #define TEX_MODIFIER_LEVEL_ZERO 4
2193 #define TEX_MODIFIER_GATHER 5
2194
2195 /*
2196 * Fetch all 3 (for s,t,r coords) texel offsets, put them into int array.
2197 */
2198 static void
2199 fetch_texel_offsets(struct tgsi_exec_machine *mach,
2200 const struct tgsi_full_instruction *inst,
2201 int8_t offsets[3])
2202 {
2203 if (inst->Texture.NumOffsets == 1) {
2204 union tgsi_exec_channel index;
2205 union tgsi_exec_channel offset[3];
2206 index.i[0] = index.i[1] = index.i[2] = index.i[3] = inst->TexOffsets[0].Index;
2207 fetch_src_file_channel(mach, 0, inst->TexOffsets[0].File,
2208 inst->TexOffsets[0].SwizzleX, &index, &ZeroVec, &offset[0]);
2209 fetch_src_file_channel(mach, 0, inst->TexOffsets[0].File,
2210 inst->TexOffsets[0].SwizzleY, &index, &ZeroVec, &offset[1]);
2211 fetch_src_file_channel(mach, 0, inst->TexOffsets[0].File,
2212 inst->TexOffsets[0].SwizzleZ, &index, &ZeroVec, &offset[2]);
2213 offsets[0] = offset[0].i[0];
2214 offsets[1] = offset[1].i[0];
2215 offsets[2] = offset[2].i[0];
2216 } else {
2217 assert(inst->Texture.NumOffsets == 0);
2218 offsets[0] = offsets[1] = offsets[2] = 0;
2219 }
2220 }
2221
2222
2223 /*
2224 * Fetch dx and dy values for one channel (s, t or r).
2225 * Put dx values into one float array, dy values into another.
2226 */
2227 static void
2228 fetch_assign_deriv_channel(struct tgsi_exec_machine *mach,
2229 const struct tgsi_full_instruction *inst,
2230 unsigned regdsrcx,
2231 unsigned chan,
2232 float derivs[2][TGSI_QUAD_SIZE])
2233 {
2234 union tgsi_exec_channel d;
2235 FETCH(&d, regdsrcx, chan);
2236 derivs[0][0] = d.f[0];
2237 derivs[0][1] = d.f[1];
2238 derivs[0][2] = d.f[2];
2239 derivs[0][3] = d.f[3];
2240 FETCH(&d, regdsrcx + 1, chan);
2241 derivs[1][0] = d.f[0];
2242 derivs[1][1] = d.f[1];
2243 derivs[1][2] = d.f[2];
2244 derivs[1][3] = d.f[3];
2245 }
2246
2247 static uint
2248 fetch_sampler_unit(struct tgsi_exec_machine *mach,
2249 const struct tgsi_full_instruction *inst,
2250 uint sampler)
2251 {
2252 uint unit = 0;
2253 int i;
2254 if (inst->Src[sampler].Register.Indirect) {
2255 const struct tgsi_full_src_register *reg = &inst->Src[sampler];
2256 union tgsi_exec_channel indir_index, index2;
2257 const uint execmask = mach->ExecMask;
2258 index2.i[0] =
2259 index2.i[1] =
2260 index2.i[2] =
2261 index2.i[3] = reg->Indirect.Index;
2262
2263 fetch_src_file_channel(mach,
2264 0,
2265 reg->Indirect.File,
2266 reg->Indirect.Swizzle,
2267 &index2,
2268 &ZeroVec,
2269 &indir_index);
2270 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
2271 if (execmask & (1 << i)) {
2272 unit = inst->Src[sampler].Register.Index + indir_index.i[i];
2273 break;
2274 }
2275 }
2276
2277 } else {
2278 unit = inst->Src[sampler].Register.Index;
2279 }
2280 return unit;
2281 }
2282
2283 /*
2284 * execute a texture instruction.
2285 *
2286 * modifier is used to control the channel routing for the
2287 * instruction variants like proj, lod, and texture with lod bias.
2288 * sampler indicates which src register the sampler is contained in.
2289 */
2290 static void
2291 exec_tex(struct tgsi_exec_machine *mach,
2292 const struct tgsi_full_instruction *inst,
2293 uint modifier, uint sampler)
2294 {
2295 const union tgsi_exec_channel *args[5], *proj = NULL;
2296 union tgsi_exec_channel r[5];
2297 enum tgsi_sampler_control control = TGSI_SAMPLER_LOD_NONE;
2298 uint chan;
2299 uint unit;
2300 int8_t offsets[3];
2301 int dim, shadow_ref, i;
2302
2303 unit = fetch_sampler_unit(mach, inst, sampler);
2304 /* always fetch all 3 offsets, overkill but keeps code simple */
2305 fetch_texel_offsets(mach, inst, offsets);
2306
2307 assert(modifier != TEX_MODIFIER_LEVEL_ZERO);
2308 assert(inst->Texture.Texture != TGSI_TEXTURE_BUFFER);
2309
2310 dim = tgsi_util_get_texture_coord_dim(inst->Texture.Texture);
2311 shadow_ref = tgsi_util_get_shadow_ref_src_index(inst->Texture.Texture);
2312
2313 assert(dim <= 4);
2314 if (shadow_ref >= 0)
2315 assert(shadow_ref >= dim && shadow_ref < ARRAY_SIZE(args));
2316
2317 /* fetch modifier to the last argument */
2318 if (modifier != TEX_MODIFIER_NONE) {
2319 const int last = ARRAY_SIZE(args) - 1;
2320
2321 /* fetch modifier from src0.w or src1.x */
2322 if (sampler == 1) {
2323 assert(dim <= TGSI_CHAN_W && shadow_ref != TGSI_CHAN_W);
2324 FETCH(&r[last], 0, TGSI_CHAN_W);
2325 }
2326 else {
2327 assert(shadow_ref != 4);
2328 FETCH(&r[last], 1, TGSI_CHAN_X);
2329 }
2330
2331 if (modifier != TEX_MODIFIER_PROJECTED) {
2332 args[last] = &r[last];
2333 }
2334 else {
2335 proj = &r[last];
2336 args[last] = &ZeroVec;
2337 }
2338
2339 /* point unused arguments to zero vector */
2340 for (i = dim; i < last; i++)
2341 args[i] = &ZeroVec;
2342
2343 if (modifier == TEX_MODIFIER_EXPLICIT_LOD)
2344 control = TGSI_SAMPLER_LOD_EXPLICIT;
2345 else if (modifier == TEX_MODIFIER_LOD_BIAS)
2346 control = TGSI_SAMPLER_LOD_BIAS;
2347 else if (modifier == TEX_MODIFIER_GATHER)
2348 control = TGSI_SAMPLER_GATHER;
2349 }
2350 else {
2351 for (i = dim; i < ARRAY_SIZE(args); i++)
2352 args[i] = &ZeroVec;
2353 }
2354
2355 /* fetch coordinates */
2356 for (i = 0; i < dim; i++) {
2357 FETCH(&r[i], 0, TGSI_CHAN_X + i);
2358
2359 if (proj)
2360 micro_div(&r[i], &r[i], proj);
2361
2362 args[i] = &r[i];
2363 }
2364
2365 /* fetch reference value */
2366 if (shadow_ref >= 0) {
2367 FETCH(&r[shadow_ref], shadow_ref / 4, TGSI_CHAN_X + (shadow_ref % 4));
2368
2369 if (proj)
2370 micro_div(&r[shadow_ref], &r[shadow_ref], proj);
2371
2372 args[shadow_ref] = &r[shadow_ref];
2373 }
2374
2375 fetch_texel(mach->Sampler, unit, unit,
2376 args[0], args[1], args[2], args[3], args[4],
2377 NULL, offsets, control,
2378 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2379
2380 #if 0
2381 debug_printf("fetch r: %g %g %g %g\n",
2382 r[0].f[0], r[0].f[1], r[0].f[2], r[0].f[3]);
2383 debug_printf("fetch g: %g %g %g %g\n",
2384 r[1].f[0], r[1].f[1], r[1].f[2], r[1].f[3]);
2385 debug_printf("fetch b: %g %g %g %g\n",
2386 r[2].f[0], r[2].f[1], r[2].f[2], r[2].f[3]);
2387 debug_printf("fetch a: %g %g %g %g\n",
2388 r[3].f[0], r[3].f[1], r[3].f[2], r[3].f[3]);
2389 #endif
2390
2391 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2392 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2393 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2394 }
2395 }
2396 }
2397
2398 static void
2399 exec_lodq(struct tgsi_exec_machine *mach,
2400 const struct tgsi_full_instruction *inst)
2401 {
2402 uint unit;
2403 int dim;
2404 int i;
2405 union tgsi_exec_channel coords[4];
2406 const union tgsi_exec_channel *args[ARRAY_SIZE(coords)];
2407 union tgsi_exec_channel r[2];
2408
2409 unit = fetch_sampler_unit(mach, inst, 1);
2410 dim = tgsi_util_get_texture_coord_dim(inst->Texture.Texture);
2411 assert(dim <= ARRAY_SIZE(coords));
2412 /* fetch coordinates */
2413 for (i = 0; i < dim; i++) {
2414 FETCH(&coords[i], 0, TGSI_CHAN_X + i);
2415 args[i] = &coords[i];
2416 }
2417 for (i = dim; i < ARRAY_SIZE(coords); i++) {
2418 args[i] = &ZeroVec;
2419 }
2420 mach->Sampler->query_lod(mach->Sampler, unit, unit,
2421 args[0]->f,
2422 args[1]->f,
2423 args[2]->f,
2424 args[3]->f,
2425 TGSI_SAMPLER_LOD_NONE,
2426 r[0].f,
2427 r[1].f);
2428
2429 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
2430 store_dest(mach, &r[0], &inst->Dst[0], inst, TGSI_CHAN_X,
2431 TGSI_EXEC_DATA_FLOAT);
2432 }
2433 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
2434 store_dest(mach, &r[1], &inst->Dst[0], inst, TGSI_CHAN_Y,
2435 TGSI_EXEC_DATA_FLOAT);
2436 }
2437 }
2438
2439 static void
2440 exec_txd(struct tgsi_exec_machine *mach,
2441 const struct tgsi_full_instruction *inst)
2442 {
2443 union tgsi_exec_channel r[4];
2444 float derivs[3][2][TGSI_QUAD_SIZE];
2445 uint chan;
2446 uint unit;
2447 int8_t offsets[3];
2448
2449 unit = fetch_sampler_unit(mach, inst, 3);
2450 /* always fetch all 3 offsets, overkill but keeps code simple */
2451 fetch_texel_offsets(mach, inst, offsets);
2452
2453 switch (inst->Texture.Texture) {
2454 case TGSI_TEXTURE_1D:
2455 FETCH(&r[0], 0, TGSI_CHAN_X);
2456
2457 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_X, derivs[0]);
2458
2459 fetch_texel(mach->Sampler, unit, unit,
2460 &r[0], &ZeroVec, &ZeroVec, &ZeroVec, &ZeroVec, /* S, T, P, C, LOD */
2461 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2462 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2463 break;
2464
2465 case TGSI_TEXTURE_SHADOW1D:
2466 case TGSI_TEXTURE_1D_ARRAY:
2467 case TGSI_TEXTURE_SHADOW1D_ARRAY:
2468 /* SHADOW1D/1D_ARRAY would not need Y/Z respectively, but don't bother */
2469 FETCH(&r[0], 0, TGSI_CHAN_X);
2470 FETCH(&r[1], 0, TGSI_CHAN_Y);
2471 FETCH(&r[2], 0, TGSI_CHAN_Z);
2472
2473 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_X, derivs[0]);
2474
2475 fetch_texel(mach->Sampler, unit, unit,
2476 &r[0], &r[1], &r[2], &ZeroVec, &ZeroVec, /* S, T, P, C, LOD */
2477 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2478 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2479 break;
2480
2481 case TGSI_TEXTURE_2D:
2482 case TGSI_TEXTURE_RECT:
2483 FETCH(&r[0], 0, TGSI_CHAN_X);
2484 FETCH(&r[1], 0, TGSI_CHAN_Y);
2485
2486 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_X, derivs[0]);
2487 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_Y, derivs[1]);
2488
2489 fetch_texel(mach->Sampler, unit, unit,
2490 &r[0], &r[1], &ZeroVec, &ZeroVec, &ZeroVec, /* S, T, P, C, LOD */
2491 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2492 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2493 break;
2494
2495
2496 case TGSI_TEXTURE_SHADOW2D:
2497 case TGSI_TEXTURE_SHADOWRECT:
2498 case TGSI_TEXTURE_2D_ARRAY:
2499 case TGSI_TEXTURE_SHADOW2D_ARRAY:
2500 /* only SHADOW2D_ARRAY actually needs W */
2501 FETCH(&r[0], 0, TGSI_CHAN_X);
2502 FETCH(&r[1], 0, TGSI_CHAN_Y);
2503 FETCH(&r[2], 0, TGSI_CHAN_Z);
2504 FETCH(&r[3], 0, TGSI_CHAN_W);
2505
2506 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_X, derivs[0]);
2507 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_Y, derivs[1]);
2508
2509 fetch_texel(mach->Sampler, unit, unit,
2510 &r[0], &r[1], &r[2], &r[3], &ZeroVec, /* inputs */
2511 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2512 &r[0], &r[1], &r[2], &r[3]); /* outputs */
2513 break;
2514
2515 case TGSI_TEXTURE_3D:
2516 case TGSI_TEXTURE_CUBE:
2517 case TGSI_TEXTURE_CUBE_ARRAY:
2518 case TGSI_TEXTURE_SHADOWCUBE:
2519 /* only TEXTURE_CUBE_ARRAY and TEXTURE_SHADOWCUBE actually need W */
2520 FETCH(&r[0], 0, TGSI_CHAN_X);
2521 FETCH(&r[1], 0, TGSI_CHAN_Y);
2522 FETCH(&r[2], 0, TGSI_CHAN_Z);
2523 FETCH(&r[3], 0, TGSI_CHAN_W);
2524
2525 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_X, derivs[0]);
2526 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_Y, derivs[1]);
2527 fetch_assign_deriv_channel(mach, inst, 1, TGSI_CHAN_Z, derivs[2]);
2528
2529 fetch_texel(mach->Sampler, unit, unit,
2530 &r[0], &r[1], &r[2], &r[3], &ZeroVec, /* inputs */
2531 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2532 &r[0], &r[1], &r[2], &r[3]); /* outputs */
2533 break;
2534
2535 default:
2536 assert(0);
2537 }
2538
2539 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2540 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2541 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2542 }
2543 }
2544 }
2545
2546
2547 static void
2548 exec_txf(struct tgsi_exec_machine *mach,
2549 const struct tgsi_full_instruction *inst)
2550 {
2551 union tgsi_exec_channel r[4];
2552 uint chan;
2553 uint unit;
2554 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
2555 int j;
2556 int8_t offsets[3];
2557 unsigned target;
2558
2559 unit = fetch_sampler_unit(mach, inst, 1);
2560 /* always fetch all 3 offsets, overkill but keeps code simple */
2561 fetch_texel_offsets(mach, inst, offsets);
2562
2563 IFETCH(&r[3], 0, TGSI_CHAN_W);
2564
2565 if (inst->Instruction.Opcode == TGSI_OPCODE_SAMPLE_I ||
2566 inst->Instruction.Opcode == TGSI_OPCODE_SAMPLE_I_MS) {
2567 target = mach->SamplerViews[unit].Resource;
2568 }
2569 else {
2570 target = inst->Texture.Texture;
2571 }
2572 switch(target) {
2573 case TGSI_TEXTURE_3D:
2574 case TGSI_TEXTURE_2D_ARRAY:
2575 case TGSI_TEXTURE_SHADOW2D_ARRAY:
2576 case TGSI_TEXTURE_2D_ARRAY_MSAA:
2577 IFETCH(&r[2], 0, TGSI_CHAN_Z);
2578 /* fallthrough */
2579 case TGSI_TEXTURE_2D:
2580 case TGSI_TEXTURE_RECT:
2581 case TGSI_TEXTURE_SHADOW1D_ARRAY:
2582 case TGSI_TEXTURE_SHADOW2D:
2583 case TGSI_TEXTURE_SHADOWRECT:
2584 case TGSI_TEXTURE_1D_ARRAY:
2585 case TGSI_TEXTURE_2D_MSAA:
2586 IFETCH(&r[1], 0, TGSI_CHAN_Y);
2587 /* fallthrough */
2588 case TGSI_TEXTURE_BUFFER:
2589 case TGSI_TEXTURE_1D:
2590 case TGSI_TEXTURE_SHADOW1D:
2591 IFETCH(&r[0], 0, TGSI_CHAN_X);
2592 break;
2593 default:
2594 assert(0);
2595 break;
2596 }
2597
2598 mach->Sampler->get_texel(mach->Sampler, unit, r[0].i, r[1].i, r[2].i, r[3].i,
2599 offsets, rgba);
2600
2601 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
2602 r[0].f[j] = rgba[0][j];
2603 r[1].f[j] = rgba[1][j];
2604 r[2].f[j] = rgba[2][j];
2605 r[3].f[j] = rgba[3][j];
2606 }
2607
2608 if (inst->Instruction.Opcode == TGSI_OPCODE_SAMPLE_I ||
2609 inst->Instruction.Opcode == TGSI_OPCODE_SAMPLE_I_MS) {
2610 unsigned char swizzles[4];
2611 swizzles[0] = inst->Src[1].Register.SwizzleX;
2612 swizzles[1] = inst->Src[1].Register.SwizzleY;
2613 swizzles[2] = inst->Src[1].Register.SwizzleZ;
2614 swizzles[3] = inst->Src[1].Register.SwizzleW;
2615
2616 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2617 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2618 store_dest(mach, &r[swizzles[chan]],
2619 &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2620 }
2621 }
2622 }
2623 else {
2624 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2625 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2626 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2627 }
2628 }
2629 }
2630 }
2631
2632 static void
2633 exec_txq(struct tgsi_exec_machine *mach,
2634 const struct tgsi_full_instruction *inst)
2635 {
2636 int result[4];
2637 union tgsi_exec_channel r[4], src;
2638 uint chan;
2639 uint unit;
2640 int i,j;
2641
2642 unit = fetch_sampler_unit(mach, inst, 1);
2643
2644 fetch_source(mach, &src, &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_INT);
2645
2646 /* XXX: This interface can't return per-pixel values */
2647 mach->Sampler->get_dims(mach->Sampler, unit, src.i[0], result);
2648
2649 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
2650 for (j = 0; j < 4; j++) {
2651 r[j].i[i] = result[j];
2652 }
2653 }
2654
2655 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2656 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2657 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan,
2658 TGSI_EXEC_DATA_INT);
2659 }
2660 }
2661 }
2662
2663 static void
2664 exec_sample(struct tgsi_exec_machine *mach,
2665 const struct tgsi_full_instruction *inst,
2666 uint modifier, boolean compare)
2667 {
2668 const uint resource_unit = inst->Src[1].Register.Index;
2669 const uint sampler_unit = inst->Src[2].Register.Index;
2670 union tgsi_exec_channel r[5], c1;
2671 const union tgsi_exec_channel *lod = &ZeroVec;
2672 enum tgsi_sampler_control control = TGSI_SAMPLER_LOD_NONE;
2673 uint chan;
2674 unsigned char swizzles[4];
2675 int8_t offsets[3];
2676
2677 /* always fetch all 3 offsets, overkill but keeps code simple */
2678 fetch_texel_offsets(mach, inst, offsets);
2679
2680 assert(modifier != TEX_MODIFIER_PROJECTED);
2681
2682 if (modifier != TEX_MODIFIER_NONE) {
2683 if (modifier == TEX_MODIFIER_LOD_BIAS) {
2684 FETCH(&c1, 3, TGSI_CHAN_X);
2685 lod = &c1;
2686 control = TGSI_SAMPLER_LOD_BIAS;
2687 }
2688 else if (modifier == TEX_MODIFIER_EXPLICIT_LOD) {
2689 FETCH(&c1, 3, TGSI_CHAN_X);
2690 lod = &c1;
2691 control = TGSI_SAMPLER_LOD_EXPLICIT;
2692 }
2693 else {
2694 assert(modifier == TEX_MODIFIER_LEVEL_ZERO);
2695 control = TGSI_SAMPLER_LOD_ZERO;
2696 }
2697 }
2698
2699 FETCH(&r[0], 0, TGSI_CHAN_X);
2700
2701 switch (mach->SamplerViews[resource_unit].Resource) {
2702 case TGSI_TEXTURE_1D:
2703 if (compare) {
2704 FETCH(&r[2], 3, TGSI_CHAN_X);
2705 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2706 &r[0], &ZeroVec, &r[2], &ZeroVec, lod, /* S, T, P, C, LOD */
2707 NULL, offsets, control,
2708 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2709 }
2710 else {
2711 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2712 &r[0], &ZeroVec, &ZeroVec, &ZeroVec, lod, /* S, T, P, C, LOD */
2713 NULL, offsets, control,
2714 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2715 }
2716 break;
2717
2718 case TGSI_TEXTURE_1D_ARRAY:
2719 case TGSI_TEXTURE_2D:
2720 case TGSI_TEXTURE_RECT:
2721 FETCH(&r[1], 0, TGSI_CHAN_Y);
2722 if (compare) {
2723 FETCH(&r[2], 3, TGSI_CHAN_X);
2724 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2725 &r[0], &r[1], &r[2], &ZeroVec, lod, /* S, T, P, C, LOD */
2726 NULL, offsets, control,
2727 &r[0], &r[1], &r[2], &r[3]); /* outputs */
2728 }
2729 else {
2730 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2731 &r[0], &r[1], &ZeroVec, &ZeroVec, lod, /* S, T, P, C, LOD */
2732 NULL, offsets, control,
2733 &r[0], &r[1], &r[2], &r[3]); /* outputs */
2734 }
2735 break;
2736
2737 case TGSI_TEXTURE_2D_ARRAY:
2738 case TGSI_TEXTURE_3D:
2739 case TGSI_TEXTURE_CUBE:
2740 FETCH(&r[1], 0, TGSI_CHAN_Y);
2741 FETCH(&r[2], 0, TGSI_CHAN_Z);
2742 if(compare) {
2743 FETCH(&r[3], 3, TGSI_CHAN_X);
2744 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2745 &r[0], &r[1], &r[2], &r[3], lod,
2746 NULL, offsets, control,
2747 &r[0], &r[1], &r[2], &r[3]);
2748 }
2749 else {
2750 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2751 &r[0], &r[1], &r[2], &ZeroVec, lod,
2752 NULL, offsets, control,
2753 &r[0], &r[1], &r[2], &r[3]);
2754 }
2755 break;
2756
2757 case TGSI_TEXTURE_CUBE_ARRAY:
2758 FETCH(&r[1], 0, TGSI_CHAN_Y);
2759 FETCH(&r[2], 0, TGSI_CHAN_Z);
2760 FETCH(&r[3], 0, TGSI_CHAN_W);
2761 if(compare) {
2762 FETCH(&r[4], 3, TGSI_CHAN_X);
2763 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2764 &r[0], &r[1], &r[2], &r[3], &r[4],
2765 NULL, offsets, control,
2766 &r[0], &r[1], &r[2], &r[3]);
2767 }
2768 else {
2769 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2770 &r[0], &r[1], &r[2], &r[3], lod,
2771 NULL, offsets, control,
2772 &r[0], &r[1], &r[2], &r[3]);
2773 }
2774 break;
2775
2776
2777 default:
2778 assert(0);
2779 }
2780
2781 swizzles[0] = inst->Src[1].Register.SwizzleX;
2782 swizzles[1] = inst->Src[1].Register.SwizzleY;
2783 swizzles[2] = inst->Src[1].Register.SwizzleZ;
2784 swizzles[3] = inst->Src[1].Register.SwizzleW;
2785
2786 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2787 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2788 store_dest(mach, &r[swizzles[chan]],
2789 &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2790 }
2791 }
2792 }
2793
2794 static void
2795 exec_sample_d(struct tgsi_exec_machine *mach,
2796 const struct tgsi_full_instruction *inst)
2797 {
2798 const uint resource_unit = inst->Src[1].Register.Index;
2799 const uint sampler_unit = inst->Src[2].Register.Index;
2800 union tgsi_exec_channel r[4];
2801 float derivs[3][2][TGSI_QUAD_SIZE];
2802 uint chan;
2803 unsigned char swizzles[4];
2804 int8_t offsets[3];
2805
2806 /* always fetch all 3 offsets, overkill but keeps code simple */
2807 fetch_texel_offsets(mach, inst, offsets);
2808
2809 FETCH(&r[0], 0, TGSI_CHAN_X);
2810
2811 switch (mach->SamplerViews[resource_unit].Resource) {
2812 case TGSI_TEXTURE_1D:
2813 case TGSI_TEXTURE_1D_ARRAY:
2814 /* only 1D array actually needs Y */
2815 FETCH(&r[1], 0, TGSI_CHAN_Y);
2816
2817 fetch_assign_deriv_channel(mach, inst, 3, TGSI_CHAN_X, derivs[0]);
2818
2819 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2820 &r[0], &r[1], &ZeroVec, &ZeroVec, &ZeroVec, /* S, T, P, C, LOD */
2821 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2822 &r[0], &r[1], &r[2], &r[3]); /* R, G, B, A */
2823 break;
2824
2825 case TGSI_TEXTURE_2D:
2826 case TGSI_TEXTURE_RECT:
2827 case TGSI_TEXTURE_2D_ARRAY:
2828 /* only 2D array actually needs Z */
2829 FETCH(&r[1], 0, TGSI_CHAN_Y);
2830 FETCH(&r[2], 0, TGSI_CHAN_Z);
2831
2832 fetch_assign_deriv_channel(mach, inst, 3, TGSI_CHAN_X, derivs[0]);
2833 fetch_assign_deriv_channel(mach, inst, 3, TGSI_CHAN_Y, derivs[1]);
2834
2835 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2836 &r[0], &r[1], &r[2], &ZeroVec, &ZeroVec, /* inputs */
2837 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2838 &r[0], &r[1], &r[2], &r[3]); /* outputs */
2839 break;
2840
2841 case TGSI_TEXTURE_3D:
2842 case TGSI_TEXTURE_CUBE:
2843 case TGSI_TEXTURE_CUBE_ARRAY:
2844 /* only cube array actually needs W */
2845 FETCH(&r[1], 0, TGSI_CHAN_Y);
2846 FETCH(&r[2], 0, TGSI_CHAN_Z);
2847 FETCH(&r[3], 0, TGSI_CHAN_W);
2848
2849 fetch_assign_deriv_channel(mach, inst, 3, TGSI_CHAN_X, derivs[0]);
2850 fetch_assign_deriv_channel(mach, inst, 3, TGSI_CHAN_Y, derivs[1]);
2851 fetch_assign_deriv_channel(mach, inst, 3, TGSI_CHAN_Z, derivs[2]);
2852
2853 fetch_texel(mach->Sampler, resource_unit, sampler_unit,
2854 &r[0], &r[1], &r[2], &r[3], &ZeroVec,
2855 derivs, offsets, TGSI_SAMPLER_DERIVS_EXPLICIT,
2856 &r[0], &r[1], &r[2], &r[3]);
2857 break;
2858
2859 default:
2860 assert(0);
2861 }
2862
2863 swizzles[0] = inst->Src[1].Register.SwizzleX;
2864 swizzles[1] = inst->Src[1].Register.SwizzleY;
2865 swizzles[2] = inst->Src[1].Register.SwizzleZ;
2866 swizzles[3] = inst->Src[1].Register.SwizzleW;
2867
2868 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2869 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
2870 store_dest(mach, &r[swizzles[chan]],
2871 &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
2872 }
2873 }
2874 }
2875
2876
2877 /**
2878 * Evaluate a constant-valued coefficient at the position of the
2879 * current quad.
2880 */
2881 static void
2882 eval_constant_coef(
2883 struct tgsi_exec_machine *mach,
2884 unsigned attrib,
2885 unsigned chan )
2886 {
2887 unsigned i;
2888
2889 for( i = 0; i < TGSI_QUAD_SIZE; i++ ) {
2890 mach->Inputs[attrib].xyzw[chan].f[i] = mach->InterpCoefs[attrib].a0[chan];
2891 }
2892 }
2893
2894 /**
2895 * Evaluate a linear-valued coefficient at the position of the
2896 * current quad.
2897 */
2898 static void
2899 eval_linear_coef(
2900 struct tgsi_exec_machine *mach,
2901 unsigned attrib,
2902 unsigned chan )
2903 {
2904 const float x = mach->QuadPos.xyzw[0].f[0];
2905 const float y = mach->QuadPos.xyzw[1].f[0];
2906 const float dadx = mach->InterpCoefs[attrib].dadx[chan];
2907 const float dady = mach->InterpCoefs[attrib].dady[chan];
2908 const float a0 = mach->InterpCoefs[attrib].a0[chan] + dadx * x + dady * y;
2909 mach->Inputs[attrib].xyzw[chan].f[0] = a0;
2910 mach->Inputs[attrib].xyzw[chan].f[1] = a0 + dadx;
2911 mach->Inputs[attrib].xyzw[chan].f[2] = a0 + dady;
2912 mach->Inputs[attrib].xyzw[chan].f[3] = a0 + dadx + dady;
2913 }
2914
2915 /**
2916 * Evaluate a perspective-valued coefficient at the position of the
2917 * current quad.
2918 */
2919 static void
2920 eval_perspective_coef(
2921 struct tgsi_exec_machine *mach,
2922 unsigned attrib,
2923 unsigned chan )
2924 {
2925 const float x = mach->QuadPos.xyzw[0].f[0];
2926 const float y = mach->QuadPos.xyzw[1].f[0];
2927 const float dadx = mach->InterpCoefs[attrib].dadx[chan];
2928 const float dady = mach->InterpCoefs[attrib].dady[chan];
2929 const float a0 = mach->InterpCoefs[attrib].a0[chan] + dadx * x + dady * y;
2930 const float *w = mach->QuadPos.xyzw[3].f;
2931 /* divide by W here */
2932 mach->Inputs[attrib].xyzw[chan].f[0] = a0 / w[0];
2933 mach->Inputs[attrib].xyzw[chan].f[1] = (a0 + dadx) / w[1];
2934 mach->Inputs[attrib].xyzw[chan].f[2] = (a0 + dady) / w[2];
2935 mach->Inputs[attrib].xyzw[chan].f[3] = (a0 + dadx + dady) / w[3];
2936 }
2937
2938
2939 typedef void (* eval_coef_func)(
2940 struct tgsi_exec_machine *mach,
2941 unsigned attrib,
2942 unsigned chan );
2943
2944 static void
2945 exec_declaration(struct tgsi_exec_machine *mach,
2946 const struct tgsi_full_declaration *decl)
2947 {
2948 if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
2949 mach->SamplerViews[decl->Range.First] = decl->SamplerView;
2950 return;
2951 }
2952
2953 if (mach->ShaderType == PIPE_SHADER_FRAGMENT) {
2954 if (decl->Declaration.File == TGSI_FILE_INPUT) {
2955 uint first, last, mask;
2956
2957 first = decl->Range.First;
2958 last = decl->Range.Last;
2959 mask = decl->Declaration.UsageMask;
2960
2961 /* XXX we could remove this special-case code since
2962 * mach->InterpCoefs[first].a0 should already have the
2963 * front/back-face value. But we should first update the
2964 * ureg code to emit the right UsageMask value (WRITEMASK_X).
2965 * Then, we could remove the tgsi_exec_machine::Face field.
2966 */
2967 /* XXX make FACE a system value */
2968 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
2969 uint i;
2970
2971 assert(decl->Semantic.Index == 0);
2972 assert(first == last);
2973
2974 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
2975 mach->Inputs[first].xyzw[0].f[i] = mach->Face;
2976 }
2977 } else {
2978 eval_coef_func eval;
2979 uint i, j;
2980
2981 switch (decl->Interp.Interpolate) {
2982 case TGSI_INTERPOLATE_CONSTANT:
2983 eval = eval_constant_coef;
2984 break;
2985
2986 case TGSI_INTERPOLATE_LINEAR:
2987 eval = eval_linear_coef;
2988 break;
2989
2990 case TGSI_INTERPOLATE_PERSPECTIVE:
2991 eval = eval_perspective_coef;
2992 break;
2993
2994 case TGSI_INTERPOLATE_COLOR:
2995 eval = mach->flatshade_color ? eval_constant_coef : eval_perspective_coef;
2996 break;
2997
2998 default:
2999 assert(0);
3000 return;
3001 }
3002
3003 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
3004 if (mask & (1 << j)) {
3005 for (i = first; i <= last; i++) {
3006 eval(mach, i, j);
3007 }
3008 }
3009 }
3010 }
3011
3012 if (DEBUG_EXECUTION) {
3013 uint i, j;
3014 for (i = first; i <= last; ++i) {
3015 debug_printf("IN[%2u] = ", i);
3016 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
3017 if (j > 0) {
3018 debug_printf(" ");
3019 }
3020 debug_printf("(%6f %u, %6f %u, %6f %u, %6f %u)\n",
3021 mach->Inputs[i].xyzw[0].f[j], mach->Inputs[i].xyzw[0].u[j],
3022 mach->Inputs[i].xyzw[1].f[j], mach->Inputs[i].xyzw[1].u[j],
3023 mach->Inputs[i].xyzw[2].f[j], mach->Inputs[i].xyzw[2].u[j],
3024 mach->Inputs[i].xyzw[3].f[j], mach->Inputs[i].xyzw[3].u[j]);
3025 }
3026 }
3027 }
3028 }
3029 }
3030
3031 }
3032
3033 typedef void (* micro_unary_op)(union tgsi_exec_channel *dst,
3034 const union tgsi_exec_channel *src);
3035
3036 static void
3037 exec_scalar_unary(struct tgsi_exec_machine *mach,
3038 const struct tgsi_full_instruction *inst,
3039 micro_unary_op op,
3040 enum tgsi_exec_datatype dst_datatype,
3041 enum tgsi_exec_datatype src_datatype)
3042 {
3043 unsigned int chan;
3044 union tgsi_exec_channel src;
3045 union tgsi_exec_channel dst;
3046
3047 fetch_source(mach, &src, &inst->Src[0], TGSI_CHAN_X, src_datatype);
3048 op(&dst, &src);
3049 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3050 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3051 store_dest(mach, &dst, &inst->Dst[0], inst, chan, dst_datatype);
3052 }
3053 }
3054 }
3055
3056 static void
3057 exec_vector_unary(struct tgsi_exec_machine *mach,
3058 const struct tgsi_full_instruction *inst,
3059 micro_unary_op op,
3060 enum tgsi_exec_datatype dst_datatype,
3061 enum tgsi_exec_datatype src_datatype)
3062 {
3063 unsigned int chan;
3064 struct tgsi_exec_vector dst;
3065
3066 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3067 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3068 union tgsi_exec_channel src;
3069
3070 fetch_source(mach, &src, &inst->Src[0], chan, src_datatype);
3071 op(&dst.xyzw[chan], &src);
3072 }
3073 }
3074 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3075 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3076 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
3077 }
3078 }
3079 }
3080
3081 typedef void (* micro_binary_op)(union tgsi_exec_channel *dst,
3082 const union tgsi_exec_channel *src0,
3083 const union tgsi_exec_channel *src1);
3084
3085 static void
3086 exec_scalar_binary(struct tgsi_exec_machine *mach,
3087 const struct tgsi_full_instruction *inst,
3088 micro_binary_op op,
3089 enum tgsi_exec_datatype dst_datatype,
3090 enum tgsi_exec_datatype src_datatype)
3091 {
3092 unsigned int chan;
3093 union tgsi_exec_channel src[2];
3094 union tgsi_exec_channel dst;
3095
3096 fetch_source(mach, &src[0], &inst->Src[0], TGSI_CHAN_X, src_datatype);
3097 fetch_source(mach, &src[1], &inst->Src[1], TGSI_CHAN_X, src_datatype);
3098 op(&dst, &src[0], &src[1]);
3099 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3100 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3101 store_dest(mach, &dst, &inst->Dst[0], inst, chan, dst_datatype);
3102 }
3103 }
3104 }
3105
3106 static void
3107 exec_vector_binary(struct tgsi_exec_machine *mach,
3108 const struct tgsi_full_instruction *inst,
3109 micro_binary_op op,
3110 enum tgsi_exec_datatype dst_datatype,
3111 enum tgsi_exec_datatype src_datatype)
3112 {
3113 unsigned int chan;
3114 struct tgsi_exec_vector dst;
3115
3116 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3117 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3118 union tgsi_exec_channel src[2];
3119
3120 fetch_source(mach, &src[0], &inst->Src[0], chan, src_datatype);
3121 fetch_source(mach, &src[1], &inst->Src[1], chan, src_datatype);
3122 op(&dst.xyzw[chan], &src[0], &src[1]);
3123 }
3124 }
3125 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3126 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3127 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
3128 }
3129 }
3130 }
3131
3132 typedef void (* micro_trinary_op)(union tgsi_exec_channel *dst,
3133 const union tgsi_exec_channel *src0,
3134 const union tgsi_exec_channel *src1,
3135 const union tgsi_exec_channel *src2);
3136
3137 static void
3138 exec_vector_trinary(struct tgsi_exec_machine *mach,
3139 const struct tgsi_full_instruction *inst,
3140 micro_trinary_op op,
3141 enum tgsi_exec_datatype dst_datatype,
3142 enum tgsi_exec_datatype src_datatype)
3143 {
3144 unsigned int chan;
3145 struct tgsi_exec_vector dst;
3146
3147 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3148 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3149 union tgsi_exec_channel src[3];
3150
3151 fetch_source(mach, &src[0], &inst->Src[0], chan, src_datatype);
3152 fetch_source(mach, &src[1], &inst->Src[1], chan, src_datatype);
3153 fetch_source(mach, &src[2], &inst->Src[2], chan, src_datatype);
3154 op(&dst.xyzw[chan], &src[0], &src[1], &src[2]);
3155 }
3156 }
3157 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3158 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3159 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
3160 }
3161 }
3162 }
3163
3164 typedef void (* micro_quaternary_op)(union tgsi_exec_channel *dst,
3165 const union tgsi_exec_channel *src0,
3166 const union tgsi_exec_channel *src1,
3167 const union tgsi_exec_channel *src2,
3168 const union tgsi_exec_channel *src3);
3169
3170 static void
3171 exec_vector_quaternary(struct tgsi_exec_machine *mach,
3172 const struct tgsi_full_instruction *inst,
3173 micro_quaternary_op op,
3174 enum tgsi_exec_datatype dst_datatype,
3175 enum tgsi_exec_datatype src_datatype)
3176 {
3177 unsigned int chan;
3178 struct tgsi_exec_vector dst;
3179
3180 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3181 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3182 union tgsi_exec_channel src[4];
3183
3184 fetch_source(mach, &src[0], &inst->Src[0], chan, src_datatype);
3185 fetch_source(mach, &src[1], &inst->Src[1], chan, src_datatype);
3186 fetch_source(mach, &src[2], &inst->Src[2], chan, src_datatype);
3187 fetch_source(mach, &src[3], &inst->Src[3], chan, src_datatype);
3188 op(&dst.xyzw[chan], &src[0], &src[1], &src[2], &src[3]);
3189 }
3190 }
3191 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3192 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3193 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan, dst_datatype);
3194 }
3195 }
3196 }
3197
3198 static void
3199 exec_dp3(struct tgsi_exec_machine *mach,
3200 const struct tgsi_full_instruction *inst)
3201 {
3202 unsigned int chan;
3203 union tgsi_exec_channel arg[3];
3204
3205 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3206 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3207 micro_mul(&arg[2], &arg[0], &arg[1]);
3208
3209 for (chan = TGSI_CHAN_Y; chan <= TGSI_CHAN_Z; chan++) {
3210 fetch_source(mach, &arg[0], &inst->Src[0], chan, TGSI_EXEC_DATA_FLOAT);
3211 fetch_source(mach, &arg[1], &inst->Src[1], chan, TGSI_EXEC_DATA_FLOAT);
3212 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
3213 }
3214
3215 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3216 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3217 store_dest(mach, &arg[2], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
3218 }
3219 }
3220 }
3221
3222 static void
3223 exec_dp4(struct tgsi_exec_machine *mach,
3224 const struct tgsi_full_instruction *inst)
3225 {
3226 unsigned int chan;
3227 union tgsi_exec_channel arg[3];
3228
3229 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3230 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3231 micro_mul(&arg[2], &arg[0], &arg[1]);
3232
3233 for (chan = TGSI_CHAN_Y; chan <= TGSI_CHAN_W; chan++) {
3234 fetch_source(mach, &arg[0], &inst->Src[0], chan, TGSI_EXEC_DATA_FLOAT);
3235 fetch_source(mach, &arg[1], &inst->Src[1], chan, TGSI_EXEC_DATA_FLOAT);
3236 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
3237 }
3238
3239 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3240 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3241 store_dest(mach, &arg[2], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
3242 }
3243 }
3244 }
3245
3246 static void
3247 exec_dp2a(struct tgsi_exec_machine *mach,
3248 const struct tgsi_full_instruction *inst)
3249 {
3250 unsigned int chan;
3251 union tgsi_exec_channel arg[3];
3252
3253 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3254 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3255 micro_mul(&arg[2], &arg[0], &arg[1]);
3256
3257 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3258 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3259 micro_mad(&arg[0], &arg[0], &arg[1], &arg[2]);
3260
3261 fetch_source(mach, &arg[1], &inst->Src[2], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3262 micro_add(&arg[0], &arg[0], &arg[1]);
3263
3264 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3265 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3266 store_dest(mach, &arg[0], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
3267 }
3268 }
3269 }
3270
3271 static void
3272 exec_dph(struct tgsi_exec_machine *mach,
3273 const struct tgsi_full_instruction *inst)
3274 {
3275 unsigned int chan;
3276 union tgsi_exec_channel arg[3];
3277
3278 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3279 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3280 micro_mul(&arg[2], &arg[0], &arg[1]);
3281
3282 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3283 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3284 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
3285
3286 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3287 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3288 micro_mad(&arg[0], &arg[0], &arg[1], &arg[2]);
3289
3290 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3291 micro_add(&arg[0], &arg[0], &arg[1]);
3292
3293 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3294 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3295 store_dest(mach, &arg[0], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
3296 }
3297 }
3298 }
3299
3300 static void
3301 exec_dp2(struct tgsi_exec_machine *mach,
3302 const struct tgsi_full_instruction *inst)
3303 {
3304 unsigned int chan;
3305 union tgsi_exec_channel arg[3];
3306
3307 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3308 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3309 micro_mul(&arg[2], &arg[0], &arg[1]);
3310
3311 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3312 fetch_source(mach, &arg[1], &inst->Src[1], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3313 micro_mad(&arg[2], &arg[0], &arg[1], &arg[2]);
3314
3315 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3316 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3317 store_dest(mach, &arg[2], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
3318 }
3319 }
3320 }
3321
3322 static void
3323 exec_pk2h(struct tgsi_exec_machine *mach,
3324 const struct tgsi_full_instruction *inst)
3325 {
3326 unsigned chan;
3327 union tgsi_exec_channel arg[2], dst;
3328
3329 fetch_source(mach, &arg[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3330 fetch_source(mach, &arg[1], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3331 for (chan = 0; chan < TGSI_QUAD_SIZE; chan++) {
3332 dst.u[chan] = util_float_to_half(arg[0].f[chan]) |
3333 (util_float_to_half(arg[1].f[chan]) << 16);
3334 }
3335 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3336 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3337 store_dest(mach, &dst, &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_UINT);
3338 }
3339 }
3340 }
3341
3342 static void
3343 exec_up2h(struct tgsi_exec_machine *mach,
3344 const struct tgsi_full_instruction *inst)
3345 {
3346 unsigned chan;
3347 union tgsi_exec_channel arg, dst[2];
3348
3349 fetch_source(mach, &arg, &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_UINT);
3350 for (chan = 0; chan < TGSI_QUAD_SIZE; chan++) {
3351 dst[0].f[chan] = util_half_to_float(arg.u[chan] & 0xffff);
3352 dst[1].f[chan] = util_half_to_float(arg.u[chan] >> 16);
3353 }
3354 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3355 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3356 store_dest(mach, &dst[chan & 1], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
3357 }
3358 }
3359 }
3360
3361 static void
3362 micro_ucmp(union tgsi_exec_channel *dst,
3363 const union tgsi_exec_channel *src0,
3364 const union tgsi_exec_channel *src1,
3365 const union tgsi_exec_channel *src2)
3366 {
3367 dst->f[0] = src0->u[0] ? src1->f[0] : src2->f[0];
3368 dst->f[1] = src0->u[1] ? src1->f[1] : src2->f[1];
3369 dst->f[2] = src0->u[2] ? src1->f[2] : src2->f[2];
3370 dst->f[3] = src0->u[3] ? src1->f[3] : src2->f[3];
3371 }
3372
3373 static void
3374 exec_ucmp(struct tgsi_exec_machine *mach,
3375 const struct tgsi_full_instruction *inst)
3376 {
3377 unsigned int chan;
3378 struct tgsi_exec_vector dst;
3379
3380 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3381 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3382 union tgsi_exec_channel src[3];
3383
3384 fetch_source(mach, &src[0], &inst->Src[0], chan,
3385 TGSI_EXEC_DATA_UINT);
3386 fetch_source(mach, &src[1], &inst->Src[1], chan,
3387 TGSI_EXEC_DATA_FLOAT);
3388 fetch_source(mach, &src[2], &inst->Src[2], chan,
3389 TGSI_EXEC_DATA_FLOAT);
3390 micro_ucmp(&dst.xyzw[chan], &src[0], &src[1], &src[2]);
3391 }
3392 }
3393 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
3394 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
3395 store_dest(mach, &dst.xyzw[chan], &inst->Dst[0], inst, chan,
3396 TGSI_EXEC_DATA_FLOAT);
3397 }
3398 }
3399 }
3400
3401 static void
3402 exec_scs(struct tgsi_exec_machine *mach,
3403 const struct tgsi_full_instruction *inst)
3404 {
3405 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY) {
3406 union tgsi_exec_channel arg;
3407 union tgsi_exec_channel result;
3408
3409 fetch_source(mach, &arg, &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3410
3411 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
3412 micro_cos(&result, &arg);
3413 store_dest(mach, &result, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3414 }
3415 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3416 micro_sin(&result, &arg);
3417 store_dest(mach, &result, &inst->Dst[0], inst, TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3418 }
3419 }
3420 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3421 store_dest(mach, &ZeroVec, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3422 }
3423 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3424 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3425 }
3426 }
3427
3428 static void
3429 exec_xpd(struct tgsi_exec_machine *mach,
3430 const struct tgsi_full_instruction *inst)
3431 {
3432 union tgsi_exec_channel r[6];
3433 union tgsi_exec_channel d[3];
3434
3435 fetch_source(mach, &r[0], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3436 fetch_source(mach, &r[1], &inst->Src[1], TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3437
3438 micro_mul(&r[2], &r[0], &r[1]);
3439
3440 fetch_source(mach, &r[3], &inst->Src[0], TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3441 fetch_source(mach, &r[4], &inst->Src[1], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3442
3443 micro_mul(&r[5], &r[3], &r[4] );
3444 micro_sub(&d[TGSI_CHAN_X], &r[2], &r[5]);
3445
3446 fetch_source(mach, &r[2], &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3447
3448 micro_mul(&r[3], &r[3], &r[2]);
3449
3450 fetch_source(mach, &r[5], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3451
3452 micro_mul(&r[1], &r[1], &r[5]);
3453 micro_sub(&d[TGSI_CHAN_Y], &r[3], &r[1]);
3454
3455 micro_mul(&r[5], &r[5], &r[4]);
3456 micro_mul(&r[0], &r[0], &r[2]);
3457 micro_sub(&d[TGSI_CHAN_Z], &r[5], &r[0]);
3458
3459 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
3460 store_dest(mach, &d[TGSI_CHAN_X], &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3461 }
3462 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3463 store_dest(mach, &d[TGSI_CHAN_Y], &inst->Dst[0], inst, TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3464 }
3465 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3466 store_dest(mach, &d[TGSI_CHAN_Z], &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3467 }
3468 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3469 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3470 }
3471 }
3472
3473 static void
3474 exec_dst(struct tgsi_exec_machine *mach,
3475 const struct tgsi_full_instruction *inst)
3476 {
3477 union tgsi_exec_channel r[2];
3478 union tgsi_exec_channel d[4];
3479
3480 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3481 fetch_source(mach, &r[0], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3482 fetch_source(mach, &r[1], &inst->Src[1], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3483 micro_mul(&d[TGSI_CHAN_Y], &r[0], &r[1]);
3484 }
3485 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3486 fetch_source(mach, &d[TGSI_CHAN_Z], &inst->Src[0], TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3487 }
3488 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3489 fetch_source(mach, &d[TGSI_CHAN_W], &inst->Src[1], TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3490 }
3491
3492 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
3493 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3494 }
3495 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3496 store_dest(mach, &d[TGSI_CHAN_Y], &inst->Dst[0], inst, TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3497 }
3498 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3499 store_dest(mach, &d[TGSI_CHAN_Z], &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3500 }
3501 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3502 store_dest(mach, &d[TGSI_CHAN_W], &inst->Dst[0], inst, TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3503 }
3504 }
3505
3506 static void
3507 exec_log(struct tgsi_exec_machine *mach,
3508 const struct tgsi_full_instruction *inst)
3509 {
3510 union tgsi_exec_channel r[3];
3511
3512 fetch_source(mach, &r[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3513 micro_abs(&r[2], &r[0]); /* r2 = abs(r0) */
3514 micro_lg2(&r[1], &r[2]); /* r1 = lg2(r2) */
3515 micro_flr(&r[0], &r[1]); /* r0 = floor(r1) */
3516 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
3517 store_dest(mach, &r[0], &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3518 }
3519 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3520 micro_exp2(&r[0], &r[0]); /* r0 = 2 ^ r0 */
3521 micro_div(&r[0], &r[2], &r[0]); /* r0 = r2 / r0 */
3522 store_dest(mach, &r[0], &inst->Dst[0], inst, TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3523 }
3524 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3525 store_dest(mach, &r[1], &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3526 }
3527 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3528 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3529 }
3530 }
3531
3532 static void
3533 exec_exp(struct tgsi_exec_machine *mach,
3534 const struct tgsi_full_instruction *inst)
3535 {
3536 union tgsi_exec_channel r[3];
3537
3538 fetch_source(mach, &r[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3539 micro_flr(&r[1], &r[0]); /* r1 = floor(r0) */
3540 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
3541 micro_exp2(&r[2], &r[1]); /* r2 = 2 ^ r1 */
3542 store_dest(mach, &r[2], &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3543 }
3544 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3545 micro_sub(&r[2], &r[0], &r[1]); /* r2 = r0 - r1 */
3546 store_dest(mach, &r[2], &inst->Dst[0], inst, TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3547 }
3548 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3549 micro_exp2(&r[2], &r[0]); /* r2 = 2 ^ r0 */
3550 store_dest(mach, &r[2], &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3551 }
3552 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3553 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3554 }
3555 }
3556
3557 static void
3558 exec_lit(struct tgsi_exec_machine *mach,
3559 const struct tgsi_full_instruction *inst)
3560 {
3561 union tgsi_exec_channel r[3];
3562 union tgsi_exec_channel d[3];
3563
3564 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_YZ) {
3565 fetch_source(mach, &r[0], &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3566 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
3567 fetch_source(mach, &r[1], &inst->Src[0], TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3568 micro_max(&r[1], &r[1], &ZeroVec);
3569
3570 fetch_source(mach, &r[2], &inst->Src[0], TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3571 micro_min(&r[2], &r[2], &P128Vec);
3572 micro_max(&r[2], &r[2], &M128Vec);
3573 micro_pow(&r[1], &r[1], &r[2]);
3574 micro_lt(&d[TGSI_CHAN_Z], &ZeroVec, &r[0], &r[1], &ZeroVec);
3575 store_dest(mach, &d[TGSI_CHAN_Z], &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_EXEC_DATA_FLOAT);
3576 }
3577 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
3578 micro_max(&d[TGSI_CHAN_Y], &r[0], &ZeroVec);
3579 store_dest(mach, &d[TGSI_CHAN_Y], &inst->Dst[0], inst, TGSI_CHAN_Y, TGSI_EXEC_DATA_FLOAT);
3580 }
3581 }
3582 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
3583 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_EXEC_DATA_FLOAT);
3584 }
3585
3586 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
3587 store_dest(mach, &OneVec, &inst->Dst[0], inst, TGSI_CHAN_W, TGSI_EXEC_DATA_FLOAT);
3588 }
3589 }
3590
3591 static void
3592 exec_break(struct tgsi_exec_machine *mach)
3593 {
3594 if (mach->BreakType == TGSI_EXEC_BREAK_INSIDE_LOOP) {
3595 /* turn off loop channels for each enabled exec channel */
3596 mach->LoopMask &= ~mach->ExecMask;
3597 /* Todo: if mach->LoopMask == 0, jump to end of loop */
3598 UPDATE_EXEC_MASK(mach);
3599 } else {
3600 assert(mach->BreakType == TGSI_EXEC_BREAK_INSIDE_SWITCH);
3601
3602 mach->Switch.mask = 0x0;
3603
3604 UPDATE_EXEC_MASK(mach);
3605 }
3606 }
3607
3608 static void
3609 exec_switch(struct tgsi_exec_machine *mach,
3610 const struct tgsi_full_instruction *inst)
3611 {
3612 assert(mach->SwitchStackTop < TGSI_EXEC_MAX_SWITCH_NESTING);
3613 assert(mach->BreakStackTop < TGSI_EXEC_MAX_BREAK_STACK);
3614
3615 mach->SwitchStack[mach->SwitchStackTop++] = mach->Switch;
3616 fetch_source(mach, &mach->Switch.selector, &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_UINT);
3617 mach->Switch.mask = 0x0;
3618 mach->Switch.defaultMask = 0x0;
3619
3620 mach->BreakStack[mach->BreakStackTop++] = mach->BreakType;
3621 mach->BreakType = TGSI_EXEC_BREAK_INSIDE_SWITCH;
3622
3623 UPDATE_EXEC_MASK(mach);
3624 }
3625
3626 static void
3627 exec_case(struct tgsi_exec_machine *mach,
3628 const struct tgsi_full_instruction *inst)
3629 {
3630 uint prevMask = mach->SwitchStack[mach->SwitchStackTop - 1].mask;
3631 union tgsi_exec_channel src;
3632 uint mask = 0;
3633
3634 fetch_source(mach, &src, &inst->Src[0], TGSI_CHAN_X, TGSI_EXEC_DATA_UINT);
3635
3636 if (mach->Switch.selector.u[0] == src.u[0]) {
3637 mask |= 0x1;
3638 }
3639 if (mach->Switch.selector.u[1] == src.u[1]) {
3640 mask |= 0x2;
3641 }
3642 if (mach->Switch.selector.u[2] == src.u[2]) {
3643 mask |= 0x4;
3644 }
3645 if (mach->Switch.selector.u[3] == src.u[3]) {
3646 mask |= 0x8;
3647 }
3648
3649 mach->Switch.defaultMask |= mask;
3650
3651 mach->Switch.mask |= mask & prevMask;
3652
3653 UPDATE_EXEC_MASK(mach);
3654 }
3655
3656 /* FIXME: this will only work if default is last */
3657 static void
3658 exec_default(struct tgsi_exec_machine *mach)
3659 {
3660 uint prevMask = mach->SwitchStack[mach->SwitchStackTop - 1].mask;
3661
3662 mach->Switch.mask |= ~mach->Switch.defaultMask & prevMask;
3663
3664 UPDATE_EXEC_MASK(mach);
3665 }
3666
3667 static void
3668 exec_endswitch(struct tgsi_exec_machine *mach)
3669 {
3670 mach->Switch = mach->SwitchStack[--mach->SwitchStackTop];
3671 mach->BreakType = mach->BreakStack[--mach->BreakStackTop];
3672
3673 UPDATE_EXEC_MASK(mach);
3674 }
3675
3676 typedef void (* micro_dop)(union tgsi_double_channel *dst,
3677 const union tgsi_double_channel *src);
3678
3679 typedef void (* micro_dop_sop)(union tgsi_double_channel *dst,
3680 const union tgsi_double_channel *src0,
3681 union tgsi_exec_channel *src1);
3682
3683 typedef void (* micro_dop_s)(union tgsi_double_channel *dst,
3684 const union tgsi_exec_channel *src);
3685
3686 typedef void (* micro_sop_d)(union tgsi_exec_channel *dst,
3687 const union tgsi_double_channel *src);
3688
3689 static void
3690 fetch_double_channel(struct tgsi_exec_machine *mach,
3691 union tgsi_double_channel *chan,
3692 const struct tgsi_full_src_register *reg,
3693 uint chan_0,
3694 uint chan_1)
3695 {
3696 union tgsi_exec_channel src[2];
3697 uint i;
3698
3699 fetch_source_d(mach, &src[0], reg, chan_0, TGSI_EXEC_DATA_UINT);
3700 fetch_source_d(mach, &src[1], reg, chan_1, TGSI_EXEC_DATA_UINT);
3701
3702 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
3703 chan->u[i][0] = src[0].u[i];
3704 chan->u[i][1] = src[1].u[i];
3705 }
3706 if (reg->Register.Absolute) {
3707 micro_dabs(chan, chan);
3708 }
3709 if (reg->Register.Negate) {
3710 micro_dneg(chan, chan);
3711 }
3712 }
3713
3714 static void
3715 store_double_channel(struct tgsi_exec_machine *mach,
3716 const union tgsi_double_channel *chan,
3717 const struct tgsi_full_dst_register *reg,
3718 const struct tgsi_full_instruction *inst,
3719 uint chan_0,
3720 uint chan_1)
3721 {
3722 union tgsi_exec_channel dst[2];
3723 uint i;
3724 union tgsi_double_channel temp;
3725 const uint execmask = mach->ExecMask;
3726
3727 if (!inst->Instruction.Saturate) {
3728 for (i = 0; i < TGSI_QUAD_SIZE; i++)
3729 if (execmask & (1 << i)) {
3730 dst[0].u[i] = chan->u[i][0];
3731 dst[1].u[i] = chan->u[i][1];
3732 }
3733 }
3734 else {
3735 for (i = 0; i < TGSI_QUAD_SIZE; i++)
3736 if (execmask & (1 << i)) {
3737 if (chan->d[i] < 0.0)
3738 temp.d[i] = 0.0;
3739 else if (chan->d[i] > 1.0)
3740 temp.d[i] = 1.0;
3741 else
3742 temp.d[i] = chan->d[i];
3743
3744 dst[0].u[i] = temp.u[i][0];
3745 dst[1].u[i] = temp.u[i][1];
3746 }
3747 }
3748
3749 store_dest_double(mach, &dst[0], reg, inst, chan_0, TGSI_EXEC_DATA_UINT);
3750 if (chan_1 != -1)
3751 store_dest_double(mach, &dst[1], reg, inst, chan_1, TGSI_EXEC_DATA_UINT);
3752 }
3753
3754 static void
3755 exec_double_unary(struct tgsi_exec_machine *mach,
3756 const struct tgsi_full_instruction *inst,
3757 micro_dop op)
3758 {
3759 union tgsi_double_channel src;
3760 union tgsi_double_channel dst;
3761
3762 if ((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY) == TGSI_WRITEMASK_XY) {
3763 fetch_double_channel(mach, &src, &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
3764 op(&dst, &src);
3765 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_CHAN_Y);
3766 }
3767 if ((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_ZW) == TGSI_WRITEMASK_ZW) {
3768 fetch_double_channel(mach, &src, &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
3769 op(&dst, &src);
3770 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_CHAN_W);
3771 }
3772 }
3773
3774 static void
3775 exec_double_binary(struct tgsi_exec_machine *mach,
3776 const struct tgsi_full_instruction *inst,
3777 micro_dop op,
3778 enum tgsi_exec_datatype dst_datatype)
3779 {
3780 union tgsi_double_channel src[2];
3781 union tgsi_double_channel dst;
3782 int first_dest_chan, second_dest_chan;
3783 int wmask;
3784
3785 wmask = inst->Dst[0].Register.WriteMask;
3786 /* these are & because of the way DSLT etc store their destinations */
3787 if (wmask & TGSI_WRITEMASK_XY) {
3788 first_dest_chan = TGSI_CHAN_X;
3789 second_dest_chan = TGSI_CHAN_Y;
3790 if (dst_datatype == TGSI_EXEC_DATA_UINT) {
3791 first_dest_chan = (wmask & TGSI_WRITEMASK_X) ? TGSI_CHAN_X : TGSI_CHAN_Y;
3792 second_dest_chan = -1;
3793 }
3794
3795 fetch_double_channel(mach, &src[0], &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
3796 fetch_double_channel(mach, &src[1], &inst->Src[1], TGSI_CHAN_X, TGSI_CHAN_Y);
3797 op(&dst, src);
3798 store_double_channel(mach, &dst, &inst->Dst[0], inst, first_dest_chan, second_dest_chan);
3799 }
3800
3801 if (wmask & TGSI_WRITEMASK_ZW) {
3802 first_dest_chan = TGSI_CHAN_Z;
3803 second_dest_chan = TGSI_CHAN_W;
3804 if (dst_datatype == TGSI_EXEC_DATA_UINT) {
3805 first_dest_chan = (wmask & TGSI_WRITEMASK_Z) ? TGSI_CHAN_Z : TGSI_CHAN_W;
3806 second_dest_chan = -1;
3807 }
3808
3809 fetch_double_channel(mach, &src[0], &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
3810 fetch_double_channel(mach, &src[1], &inst->Src[1], TGSI_CHAN_Z, TGSI_CHAN_W);
3811 op(&dst, src);
3812 store_double_channel(mach, &dst, &inst->Dst[0], inst, first_dest_chan, second_dest_chan);
3813 }
3814 }
3815
3816 static void
3817 exec_double_trinary(struct tgsi_exec_machine *mach,
3818 const struct tgsi_full_instruction *inst,
3819 micro_dop op)
3820 {
3821 union tgsi_double_channel src[3];
3822 union tgsi_double_channel dst;
3823
3824 if ((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY) == TGSI_WRITEMASK_XY) {
3825 fetch_double_channel(mach, &src[0], &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
3826 fetch_double_channel(mach, &src[1], &inst->Src[1], TGSI_CHAN_X, TGSI_CHAN_Y);
3827 fetch_double_channel(mach, &src[2], &inst->Src[2], TGSI_CHAN_X, TGSI_CHAN_Y);
3828 op(&dst, src);
3829 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_CHAN_Y);
3830 }
3831 if ((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_ZW) == TGSI_WRITEMASK_ZW) {
3832 fetch_double_channel(mach, &src[0], &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
3833 fetch_double_channel(mach, &src[1], &inst->Src[1], TGSI_CHAN_Z, TGSI_CHAN_W);
3834 fetch_double_channel(mach, &src[2], &inst->Src[2], TGSI_CHAN_Z, TGSI_CHAN_W);
3835 op(&dst, src);
3836 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_CHAN_W);
3837 }
3838 }
3839
3840 static void
3841 exec_dldexp(struct tgsi_exec_machine *mach,
3842 const struct tgsi_full_instruction *inst)
3843 {
3844 union tgsi_double_channel src0;
3845 union tgsi_exec_channel src1;
3846 union tgsi_double_channel dst;
3847 int wmask;
3848
3849 wmask = inst->Dst[0].Register.WriteMask;
3850 if (wmask & TGSI_WRITEMASK_XY) {
3851 fetch_double_channel(mach, &src0, &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
3852 fetch_source(mach, &src1, &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_INT);
3853 micro_dldexp(&dst, &src0, &src1);
3854 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_CHAN_Y);
3855 }
3856
3857 if (wmask & TGSI_WRITEMASK_ZW) {
3858 fetch_double_channel(mach, &src0, &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
3859 fetch_source(mach, &src1, &inst->Src[1], TGSI_CHAN_Z, TGSI_EXEC_DATA_INT);
3860 micro_dldexp(&dst, &src0, &src1);
3861 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_CHAN_W);
3862 }
3863 }
3864
3865 static void
3866 exec_dfracexp(struct tgsi_exec_machine *mach,
3867 const struct tgsi_full_instruction *inst)
3868 {
3869 union tgsi_double_channel src;
3870 union tgsi_double_channel dst;
3871 union tgsi_exec_channel dst_exp;
3872
3873 if (((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY) == TGSI_WRITEMASK_XY)) {
3874 fetch_double_channel(mach, &src, &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
3875 micro_dfracexp(&dst, &dst_exp, &src);
3876 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_CHAN_Y);
3877 store_dest(mach, &dst_exp, &inst->Dst[1], inst, ffs(inst->Dst[1].Register.WriteMask) - 1, TGSI_EXEC_DATA_INT);
3878 }
3879 if (((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_ZW) == TGSI_WRITEMASK_ZW)) {
3880 fetch_double_channel(mach, &src, &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
3881 micro_dfracexp(&dst, &dst_exp, &src);
3882 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_CHAN_W);
3883 store_dest(mach, &dst_exp, &inst->Dst[1], inst, ffs(inst->Dst[1].Register.WriteMask) - 1, TGSI_EXEC_DATA_INT);
3884 }
3885 }
3886
3887 static void
3888 exec_arg0_64_arg1_32(struct tgsi_exec_machine *mach,
3889 const struct tgsi_full_instruction *inst,
3890 micro_dop_sop op)
3891 {
3892 union tgsi_double_channel src0;
3893 union tgsi_exec_channel src1;
3894 union tgsi_double_channel dst;
3895 int wmask;
3896
3897 wmask = inst->Dst[0].Register.WriteMask;
3898 if (wmask & TGSI_WRITEMASK_XY) {
3899 fetch_double_channel(mach, &src0, &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
3900 fetch_source(mach, &src1, &inst->Src[1], TGSI_CHAN_X, TGSI_EXEC_DATA_INT);
3901 op(&dst, &src0, &src1);
3902 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_CHAN_Y);
3903 }
3904
3905 if (wmask & TGSI_WRITEMASK_ZW) {
3906 fetch_double_channel(mach, &src0, &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
3907 fetch_source(mach, &src1, &inst->Src[1], TGSI_CHAN_Z, TGSI_EXEC_DATA_INT);
3908 op(&dst, &src0, &src1);
3909 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_CHAN_W);
3910 }
3911 }
3912
3913 static int
3914 get_image_coord_dim(unsigned tgsi_tex)
3915 {
3916 int dim;
3917 switch (tgsi_tex) {
3918 case TGSI_TEXTURE_BUFFER:
3919 case TGSI_TEXTURE_1D:
3920 dim = 1;
3921 break;
3922 case TGSI_TEXTURE_2D:
3923 case TGSI_TEXTURE_RECT:
3924 case TGSI_TEXTURE_1D_ARRAY:
3925 case TGSI_TEXTURE_2D_MSAA:
3926 dim = 2;
3927 break;
3928 case TGSI_TEXTURE_3D:
3929 case TGSI_TEXTURE_CUBE:
3930 case TGSI_TEXTURE_2D_ARRAY:
3931 case TGSI_TEXTURE_2D_ARRAY_MSAA:
3932 case TGSI_TEXTURE_CUBE_ARRAY:
3933 dim = 3;
3934 break;
3935 default:
3936 assert(!"unknown texture target");
3937 dim = 0;
3938 break;
3939 }
3940
3941 return dim;
3942 }
3943
3944 static int
3945 get_image_coord_sample(unsigned tgsi_tex)
3946 {
3947 int sample = 0;
3948 switch (tgsi_tex) {
3949 case TGSI_TEXTURE_2D_MSAA:
3950 sample = 3;
3951 break;
3952 case TGSI_TEXTURE_2D_ARRAY_MSAA:
3953 sample = 4;
3954 break;
3955 default:
3956 break;
3957 }
3958 return sample;
3959 }
3960
3961 static void
3962 exec_load_img(struct tgsi_exec_machine *mach,
3963 const struct tgsi_full_instruction *inst)
3964 {
3965 union tgsi_exec_channel r[4], sample_r;
3966 uint unit;
3967 int sample;
3968 int i, j;
3969 int dim;
3970 uint chan;
3971 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
3972 struct tgsi_image_params params;
3973 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
3974
3975 unit = fetch_sampler_unit(mach, inst, 0);
3976 dim = get_image_coord_dim(inst->Memory.Texture);
3977 sample = get_image_coord_sample(inst->Memory.Texture);
3978 assert(dim <= 3);
3979
3980 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
3981 params.unit = unit;
3982 params.tgsi_tex_instr = inst->Memory.Texture;
3983 params.format = inst->Memory.Format;
3984
3985 for (i = 0; i < dim; i++) {
3986 IFETCH(&r[i], 1, TGSI_CHAN_X + i);
3987 }
3988
3989 if (sample)
3990 IFETCH(&sample_r, 1, TGSI_CHAN_X + sample);
3991
3992 mach->Image->load(mach->Image, &params,
3993 r[0].i, r[1].i, r[2].i, sample_r.i,
3994 rgba);
3995 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
3996 r[0].f[j] = rgba[0][j];
3997 r[1].f[j] = rgba[1][j];
3998 r[2].f[j] = rgba[2][j];
3999 r[3].f[j] = rgba[3][j];
4000 }
4001 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4002 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4003 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
4004 }
4005 }
4006 }
4007
4008 static void
4009 exec_load_buf(struct tgsi_exec_machine *mach,
4010 const struct tgsi_full_instruction *inst)
4011 {
4012 union tgsi_exec_channel r[4];
4013 uint unit;
4014 int j;
4015 uint chan;
4016 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4017 struct tgsi_buffer_params params;
4018 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4019
4020 unit = fetch_sampler_unit(mach, inst, 0);
4021
4022 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4023 params.unit = unit;
4024 IFETCH(&r[0], 1, TGSI_CHAN_X);
4025
4026 mach->Buffer->load(mach->Buffer, &params,
4027 r[0].i, rgba);
4028 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4029 r[0].f[j] = rgba[0][j];
4030 r[1].f[j] = rgba[1][j];
4031 r[2].f[j] = rgba[2][j];
4032 r[3].f[j] = rgba[3][j];
4033 }
4034 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4035 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4036 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
4037 }
4038 }
4039 }
4040
4041 static void
4042 exec_load_mem(struct tgsi_exec_machine *mach,
4043 const struct tgsi_full_instruction *inst)
4044 {
4045 union tgsi_exec_channel r[4];
4046 uint chan;
4047 char *ptr = mach->LocalMem;
4048 uint32_t offset;
4049 int j;
4050
4051 IFETCH(&r[0], 1, TGSI_CHAN_X);
4052 if (r[0].u[0] >= mach->LocalMemSize)
4053 return;
4054
4055 offset = r[0].u[0];
4056 ptr += offset;
4057
4058 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4059 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4060 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4061 memcpy(&r[chan].u[j], ptr + (4 * chan), 4);
4062 }
4063 }
4064 }
4065
4066 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4067 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4068 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
4069 }
4070 }
4071 }
4072
4073 static void
4074 exec_load(struct tgsi_exec_machine *mach,
4075 const struct tgsi_full_instruction *inst)
4076 {
4077 if (inst->Src[0].Register.File == TGSI_FILE_IMAGE)
4078 exec_load_img(mach, inst);
4079 else if (inst->Src[0].Register.File == TGSI_FILE_BUFFER)
4080 exec_load_buf(mach, inst);
4081 else if (inst->Src[0].Register.File == TGSI_FILE_MEMORY)
4082 exec_load_mem(mach, inst);
4083 }
4084
4085 static void
4086 exec_store_img(struct tgsi_exec_machine *mach,
4087 const struct tgsi_full_instruction *inst)
4088 {
4089 union tgsi_exec_channel r[3], sample_r;
4090 union tgsi_exec_channel value[4];
4091 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4092 struct tgsi_image_params params;
4093 int dim;
4094 int sample;
4095 int i, j;
4096 uint unit;
4097 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4098 unit = inst->Dst[0].Register.Index;
4099 dim = get_image_coord_dim(inst->Memory.Texture);
4100 sample = get_image_coord_sample(inst->Memory.Texture);
4101 assert(dim <= 3);
4102
4103 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4104 params.unit = unit;
4105 params.tgsi_tex_instr = inst->Memory.Texture;
4106 params.format = inst->Memory.Format;
4107
4108 for (i = 0; i < dim; i++) {
4109 IFETCH(&r[i], 0, TGSI_CHAN_X + i);
4110 }
4111
4112 for (i = 0; i < 4; i++) {
4113 FETCH(&value[i], 1, TGSI_CHAN_X + i);
4114 }
4115 if (sample)
4116 IFETCH(&sample_r, 0, TGSI_CHAN_X + sample);
4117
4118 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4119 rgba[0][j] = value[0].f[j];
4120 rgba[1][j] = value[1].f[j];
4121 rgba[2][j] = value[2].f[j];
4122 rgba[3][j] = value[3].f[j];
4123 }
4124
4125 mach->Image->store(mach->Image, &params,
4126 r[0].i, r[1].i, r[2].i, sample_r.i,
4127 rgba);
4128 }
4129
4130 static void
4131 exec_store_buf(struct tgsi_exec_machine *mach,
4132 const struct tgsi_full_instruction *inst)
4133 {
4134 union tgsi_exec_channel r[3];
4135 union tgsi_exec_channel value[4];
4136 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4137 struct tgsi_buffer_params params;
4138 int i, j;
4139 uint unit;
4140 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4141
4142 unit = inst->Dst[0].Register.Index;
4143
4144 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4145 params.unit = unit;
4146 params.writemask = inst->Dst[0].Register.WriteMask;
4147
4148 IFETCH(&r[0], 0, TGSI_CHAN_X);
4149 for (i = 0; i < 4; i++) {
4150 FETCH(&value[i], 1, TGSI_CHAN_X + i);
4151 }
4152
4153 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4154 rgba[0][j] = value[0].f[j];
4155 rgba[1][j] = value[1].f[j];
4156 rgba[2][j] = value[2].f[j];
4157 rgba[3][j] = value[3].f[j];
4158 }
4159
4160 mach->Buffer->store(mach->Buffer, &params,
4161 r[0].i,
4162 rgba);
4163 }
4164
4165 static void
4166 exec_store_mem(struct tgsi_exec_machine *mach,
4167 const struct tgsi_full_instruction *inst)
4168 {
4169 union tgsi_exec_channel r[3];
4170 union tgsi_exec_channel value[4];
4171 uint i, chan;
4172 char *ptr = mach->LocalMem;
4173 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4174 int execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4175
4176 IFETCH(&r[0], 0, TGSI_CHAN_X);
4177
4178 for (i = 0; i < 4; i++) {
4179 FETCH(&value[i], 1, TGSI_CHAN_X + i);
4180 }
4181
4182 if (r[0].u[0] >= mach->LocalMemSize)
4183 return;
4184 ptr += r[0].u[0];
4185
4186 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
4187 if (execmask & (1 << i)) {
4188 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4189 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4190 memcpy(ptr + (chan * 4), &value[chan].u[0], 4);
4191 }
4192 }
4193 }
4194 }
4195 }
4196
4197 static void
4198 exec_store(struct tgsi_exec_machine *mach,
4199 const struct tgsi_full_instruction *inst)
4200 {
4201 if (inst->Dst[0].Register.File == TGSI_FILE_IMAGE)
4202 exec_store_img(mach, inst);
4203 else if (inst->Dst[0].Register.File == TGSI_FILE_BUFFER)
4204 exec_store_buf(mach, inst);
4205 else if (inst->Dst[0].Register.File == TGSI_FILE_MEMORY)
4206 exec_store_mem(mach, inst);
4207 }
4208
4209 static void
4210 exec_atomop_img(struct tgsi_exec_machine *mach,
4211 const struct tgsi_full_instruction *inst)
4212 {
4213 union tgsi_exec_channel r[4], sample_r;
4214 union tgsi_exec_channel value[4], value2[4];
4215 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4216 float rgba2[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4217 struct tgsi_image_params params;
4218 int dim;
4219 int sample;
4220 int i, j;
4221 uint unit, chan;
4222 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4223 unit = fetch_sampler_unit(mach, inst, 0);
4224 dim = get_image_coord_dim(inst->Memory.Texture);
4225 sample = get_image_coord_sample(inst->Memory.Texture);
4226 assert(dim <= 3);
4227
4228 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4229 params.unit = unit;
4230 params.tgsi_tex_instr = inst->Memory.Texture;
4231 params.format = inst->Memory.Format;
4232
4233 for (i = 0; i < dim; i++) {
4234 IFETCH(&r[i], 1, TGSI_CHAN_X + i);
4235 }
4236
4237 for (i = 0; i < 4; i++) {
4238 FETCH(&value[i], 2, TGSI_CHAN_X + i);
4239 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS)
4240 FETCH(&value2[i], 3, TGSI_CHAN_X + i);
4241 }
4242 if (sample)
4243 IFETCH(&sample_r, 1, TGSI_CHAN_X + sample);
4244
4245 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4246 rgba[0][j] = value[0].f[j];
4247 rgba[1][j] = value[1].f[j];
4248 rgba[2][j] = value[2].f[j];
4249 rgba[3][j] = value[3].f[j];
4250 }
4251 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
4252 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4253 rgba2[0][j] = value2[0].f[j];
4254 rgba2[1][j] = value2[1].f[j];
4255 rgba2[2][j] = value2[2].f[j];
4256 rgba2[3][j] = value2[3].f[j];
4257 }
4258 }
4259
4260 mach->Image->op(mach->Image, &params, inst->Instruction.Opcode,
4261 r[0].i, r[1].i, r[2].i, sample_r.i,
4262 rgba, rgba2);
4263
4264 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4265 r[0].f[j] = rgba[0][j];
4266 r[1].f[j] = rgba[1][j];
4267 r[2].f[j] = rgba[2][j];
4268 r[3].f[j] = rgba[3][j];
4269 }
4270 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4271 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4272 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
4273 }
4274 }
4275 }
4276
4277 static void
4278 exec_atomop_buf(struct tgsi_exec_machine *mach,
4279 const struct tgsi_full_instruction *inst)
4280 {
4281 union tgsi_exec_channel r[4];
4282 union tgsi_exec_channel value[4], value2[4];
4283 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4284 float rgba2[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];
4285 struct tgsi_buffer_params params;
4286 int i, j;
4287 uint unit, chan;
4288 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4289
4290 unit = fetch_sampler_unit(mach, inst, 0);
4291
4292 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4293 params.unit = unit;
4294 params.writemask = inst->Dst[0].Register.WriteMask;
4295
4296 IFETCH(&r[0], 1, TGSI_CHAN_X);
4297
4298 for (i = 0; i < 4; i++) {
4299 FETCH(&value[i], 2, TGSI_CHAN_X + i);
4300 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS)
4301 FETCH(&value2[i], 3, TGSI_CHAN_X + i);
4302 }
4303
4304 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4305 rgba[0][j] = value[0].f[j];
4306 rgba[1][j] = value[1].f[j];
4307 rgba[2][j] = value[2].f[j];
4308 rgba[3][j] = value[3].f[j];
4309 }
4310 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
4311 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4312 rgba2[0][j] = value2[0].f[j];
4313 rgba2[1][j] = value2[1].f[j];
4314 rgba2[2][j] = value2[2].f[j];
4315 rgba2[3][j] = value2[3].f[j];
4316 }
4317 }
4318
4319 mach->Buffer->op(mach->Buffer, &params, inst->Instruction.Opcode,
4320 r[0].i,
4321 rgba, rgba2);
4322
4323 for (j = 0; j < TGSI_QUAD_SIZE; j++) {
4324 r[0].f[j] = rgba[0][j];
4325 r[1].f[j] = rgba[1][j];
4326 r[2].f[j] = rgba[2][j];
4327 r[3].f[j] = rgba[3][j];
4328 }
4329 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4330 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4331 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
4332 }
4333 }
4334 }
4335
4336 static void
4337 exec_atomop_mem(struct tgsi_exec_machine *mach,
4338 const struct tgsi_full_instruction *inst)
4339 {
4340 union tgsi_exec_channel r[4];
4341 union tgsi_exec_channel value[4], value2[4];
4342 char *ptr = mach->LocalMem;
4343 uint32_t val;
4344 uint chan, i;
4345 uint32_t offset;
4346 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4347 int execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4348 IFETCH(&r[0], 1, TGSI_CHAN_X);
4349
4350 if (r[0].u[0] >= mach->LocalMemSize)
4351 return;
4352
4353 offset = r[0].u[0];
4354 ptr += offset;
4355 for (i = 0; i < 4; i++) {
4356 FETCH(&value[i], 2, TGSI_CHAN_X + i);
4357 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS)
4358 FETCH(&value2[i], 3, TGSI_CHAN_X + i);
4359 }
4360
4361 memcpy(&r[0].u[0], ptr, 4);
4362 val = r[0].u[0];
4363 switch (inst->Instruction.Opcode) {
4364 case TGSI_OPCODE_ATOMUADD:
4365 val += value[0].u[0];
4366 break;
4367 case TGSI_OPCODE_ATOMXOR:
4368 val ^= value[0].u[0];
4369 break;
4370 case TGSI_OPCODE_ATOMOR:
4371 val |= value[0].u[0];
4372 break;
4373 case TGSI_OPCODE_ATOMAND:
4374 val &= value[0].u[0];
4375 break;
4376 case TGSI_OPCODE_ATOMUMIN:
4377 val = MIN2(val, value[0].u[0]);
4378 break;
4379 case TGSI_OPCODE_ATOMUMAX:
4380 val = MAX2(val, value[0].u[0]);
4381 break;
4382 case TGSI_OPCODE_ATOMIMIN:
4383 val = MIN2(r[0].i[0], value[0].i[0]);
4384 break;
4385 case TGSI_OPCODE_ATOMIMAX:
4386 val = MAX2(r[0].i[0], value[0].i[0]);
4387 break;
4388 case TGSI_OPCODE_ATOMXCHG:
4389 val = value[0].i[0];
4390 break;
4391 case TGSI_OPCODE_ATOMCAS:
4392 if (val == value[0].u[0])
4393 val = value2[0].u[0];
4394 break;
4395 default:
4396 break;
4397 }
4398 for (i = 0; i < TGSI_QUAD_SIZE; i++)
4399 if (execmask & (1 << i))
4400 memcpy(ptr, &val, 4);
4401
4402 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4403 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4404 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan, TGSI_EXEC_DATA_FLOAT);
4405 }
4406 }
4407 }
4408
4409 static void
4410 exec_atomop(struct tgsi_exec_machine *mach,
4411 const struct tgsi_full_instruction *inst)
4412 {
4413 if (inst->Src[0].Register.File == TGSI_FILE_IMAGE)
4414 exec_atomop_img(mach, inst);
4415 else if (inst->Src[0].Register.File == TGSI_FILE_BUFFER)
4416 exec_atomop_buf(mach, inst);
4417 else if (inst->Src[0].Register.File == TGSI_FILE_MEMORY)
4418 exec_atomop_mem(mach, inst);
4419 }
4420
4421 static void
4422 exec_resq_img(struct tgsi_exec_machine *mach,
4423 const struct tgsi_full_instruction *inst)
4424 {
4425 int result[4];
4426 union tgsi_exec_channel r[4];
4427 uint unit;
4428 int i, chan, j;
4429 struct tgsi_image_params params;
4430 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4431
4432 unit = fetch_sampler_unit(mach, inst, 0);
4433
4434 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4435 params.unit = unit;
4436 params.tgsi_tex_instr = inst->Memory.Texture;
4437 params.format = inst->Memory.Format;
4438
4439 mach->Image->get_dims(mach->Image, &params, result);
4440
4441 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
4442 for (j = 0; j < 4; j++) {
4443 r[j].i[i] = result[j];
4444 }
4445 }
4446
4447 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4448 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4449 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan,
4450 TGSI_EXEC_DATA_INT);
4451 }
4452 }
4453 }
4454
4455 static void
4456 exec_resq_buf(struct tgsi_exec_machine *mach,
4457 const struct tgsi_full_instruction *inst)
4458 {
4459 int result;
4460 union tgsi_exec_channel r[4];
4461 uint unit;
4462 int i, chan;
4463 struct tgsi_buffer_params params;
4464 int kilmask = mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
4465
4466 unit = fetch_sampler_unit(mach, inst, 0);
4467
4468 params.execmask = mach->ExecMask & mach->NonHelperMask & ~kilmask;
4469 params.unit = unit;
4470
4471 mach->Buffer->get_dims(mach->Buffer, &params, &result);
4472
4473 for (i = 0; i < TGSI_QUAD_SIZE; i++) {
4474 r[0].i[i] = result;
4475 }
4476
4477 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
4478 if (inst->Dst[0].Register.WriteMask & (1 << chan)) {
4479 store_dest(mach, &r[chan], &inst->Dst[0], inst, chan,
4480 TGSI_EXEC_DATA_INT);
4481 }
4482 }
4483 }
4484
4485 static void
4486 exec_resq(struct tgsi_exec_machine *mach,
4487 const struct tgsi_full_instruction *inst)
4488 {
4489 if (inst->Src[0].Register.File == TGSI_FILE_IMAGE)
4490 exec_resq_img(mach, inst);
4491 else
4492 exec_resq_buf(mach, inst);
4493 }
4494
4495 static void
4496 micro_f2u64(union tgsi_double_channel *dst,
4497 const union tgsi_exec_channel *src)
4498 {
4499 dst->u64[0] = (uint64_t)src->f[0];
4500 dst->u64[1] = (uint64_t)src->f[1];
4501 dst->u64[2] = (uint64_t)src->f[2];
4502 dst->u64[3] = (uint64_t)src->f[3];
4503 }
4504
4505 static void
4506 micro_f2i64(union tgsi_double_channel *dst,
4507 const union tgsi_exec_channel *src)
4508 {
4509 dst->i64[0] = (int64_t)src->f[0];
4510 dst->i64[1] = (int64_t)src->f[1];
4511 dst->i64[2] = (int64_t)src->f[2];
4512 dst->i64[3] = (int64_t)src->f[3];
4513 }
4514
4515 static void
4516 micro_u2i64(union tgsi_double_channel *dst,
4517 const union tgsi_exec_channel *src)
4518 {
4519 dst->u64[0] = (uint64_t)src->u[0];
4520 dst->u64[1] = (uint64_t)src->u[1];
4521 dst->u64[2] = (uint64_t)src->u[2];
4522 dst->u64[3] = (uint64_t)src->u[3];
4523 }
4524
4525 static void
4526 micro_i2i64(union tgsi_double_channel *dst,
4527 const union tgsi_exec_channel *src)
4528 {
4529 dst->i64[0] = (int64_t)src->i[0];
4530 dst->i64[1] = (int64_t)src->i[1];
4531 dst->i64[2] = (int64_t)src->i[2];
4532 dst->i64[3] = (int64_t)src->i[3];
4533 }
4534
4535 static void
4536 micro_d2u64(union tgsi_double_channel *dst,
4537 const union tgsi_double_channel *src)
4538 {
4539 dst->u64[0] = (uint64_t)src->d[0];
4540 dst->u64[1] = (uint64_t)src->d[1];
4541 dst->u64[2] = (uint64_t)src->d[2];
4542 dst->u64[3] = (uint64_t)src->d[3];
4543 }
4544
4545 static void
4546 micro_d2i64(union tgsi_double_channel *dst,
4547 const union tgsi_double_channel *src)
4548 {
4549 dst->i64[0] = (int64_t)src->d[0];
4550 dst->i64[1] = (int64_t)src->d[1];
4551 dst->i64[2] = (int64_t)src->d[2];
4552 dst->i64[3] = (int64_t)src->d[3];
4553 }
4554
4555 static void
4556 micro_u642d(union tgsi_double_channel *dst,
4557 const union tgsi_double_channel *src)
4558 {
4559 dst->d[0] = (double)src->u64[0];
4560 dst->d[1] = (double)src->u64[1];
4561 dst->d[2] = (double)src->u64[2];
4562 dst->d[3] = (double)src->u64[3];
4563 }
4564
4565 static void
4566 micro_i642d(union tgsi_double_channel *dst,
4567 const union tgsi_double_channel *src)
4568 {
4569 dst->d[0] = (double)src->i64[0];
4570 dst->d[1] = (double)src->i64[1];
4571 dst->d[2] = (double)src->i64[2];
4572 dst->d[3] = (double)src->i64[3];
4573 }
4574
4575 static void
4576 micro_u642f(union tgsi_exec_channel *dst,
4577 const union tgsi_double_channel *src)
4578 {
4579 dst->f[0] = (float)src->u64[0];
4580 dst->f[1] = (float)src->u64[1];
4581 dst->f[2] = (float)src->u64[2];
4582 dst->f[3] = (float)src->u64[3];
4583 }
4584
4585 static void
4586 micro_i642f(union tgsi_exec_channel *dst,
4587 const union tgsi_double_channel *src)
4588 {
4589 dst->f[0] = (float)src->i64[0];
4590 dst->f[1] = (float)src->i64[1];
4591 dst->f[2] = (float)src->i64[2];
4592 dst->f[3] = (float)src->i64[3];
4593 }
4594
4595 static void
4596 exec_t_2_64(struct tgsi_exec_machine *mach,
4597 const struct tgsi_full_instruction *inst,
4598 micro_dop_s op,
4599 enum tgsi_exec_datatype src_datatype)
4600 {
4601 union tgsi_exec_channel src;
4602 union tgsi_double_channel dst;
4603
4604 if ((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY) == TGSI_WRITEMASK_XY) {
4605 fetch_source(mach, &src, &inst->Src[0], TGSI_CHAN_X, src_datatype);
4606 op(&dst, &src);
4607 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_X, TGSI_CHAN_Y);
4608 }
4609 if ((inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_ZW) == TGSI_WRITEMASK_ZW) {
4610 fetch_source(mach, &src, &inst->Src[0], TGSI_CHAN_Y, src_datatype);
4611 op(&dst, &src);
4612 store_double_channel(mach, &dst, &inst->Dst[0], inst, TGSI_CHAN_Z, TGSI_CHAN_W);
4613 }
4614 }
4615
4616 static void
4617 exec_64_2_t(struct tgsi_exec_machine *mach,
4618 const struct tgsi_full_instruction *inst,
4619 micro_sop_d op,
4620 enum tgsi_exec_datatype dst_datatype)
4621 {
4622 union tgsi_double_channel src;
4623 union tgsi_exec_channel dst;
4624 int wm = inst->Dst[0].Register.WriteMask;
4625 int i;
4626 int bit;
4627 for (i = 0; i < 2; i++) {
4628 bit = ffs(wm);
4629 if (bit) {
4630 wm &= ~(1 << (bit - 1));
4631 if (i == 0)
4632 fetch_double_channel(mach, &src, &inst->Src[0], TGSI_CHAN_X, TGSI_CHAN_Y);
4633 else
4634 fetch_double_channel(mach, &src, &inst->Src[0], TGSI_CHAN_Z, TGSI_CHAN_W);
4635 op(&dst, &src);
4636 store_dest(mach, &dst, &inst->Dst[0], inst, bit - 1, dst_datatype);
4637 }
4638 }
4639 }
4640
4641 static void
4642 micro_i2f(union tgsi_exec_channel *dst,
4643 const union tgsi_exec_channel *src)
4644 {
4645 dst->f[0] = (float)src->i[0];
4646 dst->f[1] = (float)src->i[1];
4647 dst->f[2] = (float)src->i[2];
4648 dst->f[3] = (float)src->i[3];
4649 }
4650
4651 static void
4652 micro_not(union tgsi_exec_channel *dst,
4653 const union tgsi_exec_channel *src)
4654 {
4655 dst->u[0] = ~src->u[0];
4656 dst->u[1] = ~src->u[1];
4657 dst->u[2] = ~src->u[2];
4658 dst->u[3] = ~src->u[3];
4659 }
4660
4661 static void
4662 micro_shl(union tgsi_exec_channel *dst,
4663 const union tgsi_exec_channel *src0,
4664 const union tgsi_exec_channel *src1)
4665 {
4666 unsigned masked_count;
4667 masked_count = src1->u[0] & 0x1f;
4668 dst->u[0] = src0->u[0] << masked_count;
4669 masked_count = src1->u[1] & 0x1f;
4670 dst->u[1] = src0->u[1] << masked_count;
4671 masked_count = src1->u[2] & 0x1f;
4672 dst->u[2] = src0->u[2] << masked_count;
4673 masked_count = src1->u[3] & 0x1f;
4674 dst->u[3] = src0->u[3] << masked_count;
4675 }
4676
4677 static void
4678 micro_and(union tgsi_exec_channel *dst,
4679 const union tgsi_exec_channel *src0,
4680 const union tgsi_exec_channel *src1)
4681 {
4682 dst->u[0] = src0->u[0] & src1->u[0];
4683 dst->u[1] = src0->u[1] & src1->u[1];
4684 dst->u[2] = src0->u[2] & src1->u[2];
4685 dst->u[3] = src0->u[3] & src1->u[3];
4686 }
4687
4688 static void
4689 micro_or(union tgsi_exec_channel *dst,
4690 const union tgsi_exec_channel *src0,
4691 const union tgsi_exec_channel *src1)
4692 {
4693 dst->u[0] = src0->u[0] | src1->u[0];
4694 dst->u[1] = src0->u[1] | src1->u[1];
4695 dst->u[2] = src0->u[2] | src1->u[2];
4696 dst->u[3] = src0->u[3] | src1->u[3];
4697 }
4698
4699 static void
4700 micro_xor(union tgsi_exec_channel *dst,
4701 const union tgsi_exec_channel *src0,
4702 const union tgsi_exec_channel *src1)
4703 {
4704 dst->u[0] = src0->u[0] ^ src1->u[0];
4705 dst->u[1] = src0->u[1] ^ src1->u[1];
4706 dst->u[2] = src0->u[2] ^ src1->u[2];
4707 dst->u[3] = src0->u[3] ^ src1->u[3];
4708 }
4709
4710 static void
4711 micro_mod(union tgsi_exec_channel *dst,
4712 const union tgsi_exec_channel *src0,
4713 const union tgsi_exec_channel *src1)
4714 {
4715 dst->i[0] = src0->i[0] % src1->i[0];
4716 dst->i[1] = src0->i[1] % src1->i[1];
4717 dst->i[2] = src0->i[2] % src1->i[2];
4718 dst->i[3] = src0->i[3] % src1->i[3];
4719 }
4720
4721 static void
4722 micro_f2i(union tgsi_exec_channel *dst,
4723 const union tgsi_exec_channel *src)
4724 {
4725 dst->i[0] = (int)src->f[0];
4726 dst->i[1] = (int)src->f[1];
4727 dst->i[2] = (int)src->f[2];
4728 dst->i[3] = (int)src->f[3];
4729 }
4730
4731 static void
4732 micro_fseq(union tgsi_exec_channel *dst,
4733 const union tgsi_exec_channel *src0,
4734 const union tgsi_exec_channel *src1)
4735 {
4736 dst->u[0] = src0->f[0] == src1->f[0] ? ~0 : 0;
4737 dst->u[1] = src0->f[1] == src1->f[1] ? ~0 : 0;
4738 dst->u[2] = src0->f[2] == src1->f[2] ? ~0 : 0;
4739 dst->u[3] = src0->f[3] == src1->f[3] ? ~0 : 0;
4740 }
4741
4742 static void
4743 micro_fsge(union tgsi_exec_channel *dst,
4744 const union tgsi_exec_channel *src0,
4745 const union tgsi_exec_channel *src1)
4746 {
4747 dst->u[0] = src0->f[0] >= src1->f[0] ? ~0 : 0;
4748 dst->u[1] = src0->f[1] >= src1->f[1] ? ~0 : 0;
4749 dst->u[2] = src0->f[2] >= src1->f[2] ? ~0 : 0;
4750 dst->u[3] = src0->f[3] >= src1->f[3] ? ~0 : 0;
4751 }
4752
4753 static void
4754 micro_fslt(union tgsi_exec_channel *dst,
4755 const union tgsi_exec_channel *src0,
4756 const union tgsi_exec_channel *src1)
4757 {
4758 dst->u[0] = src0->f[0] < src1->f[0] ? ~0 : 0;
4759 dst->u[1] = src0->f[1] < src1->f[1] ? ~0 : 0;
4760 dst->u[2] = src0->f[2] < src1->f[2] ? ~0 : 0;
4761 dst->u[3] = src0->f[3] < src1->f[3] ? ~0 : 0;
4762 }
4763
4764 static void
4765 micro_fsne(union tgsi_exec_channel *dst,
4766 const union tgsi_exec_channel *src0,
4767 const union tgsi_exec_channel *src1)
4768 {
4769 dst->u[0] = src0->f[0] != src1->f[0] ? ~0 : 0;
4770 dst->u[1] = src0->f[1] != src1->f[1] ? ~0 : 0;
4771 dst->u[2] = src0->f[2] != src1->f[2] ? ~0 : 0;
4772 dst->u[3] = src0->f[3] != src1->f[3] ? ~0 : 0;
4773 }
4774
4775 static void
4776 micro_idiv(union tgsi_exec_channel *dst,
4777 const union tgsi_exec_channel *src0,
4778 const union tgsi_exec_channel *src1)
4779 {
4780 dst->i[0] = src1->i[0] ? src0->i[0] / src1->i[0] : 0;
4781 dst->i[1] = src1->i[1] ? src0->i[1] / src1->i[1] : 0;
4782 dst->i[2] = src1->i[2] ? src0->i[2] / src1->i[2] : 0;
4783 dst->i[3] = src1->i[3] ? src0->i[3] / src1->i[3] : 0;
4784 }
4785
4786 static void
4787 micro_imax(union tgsi_exec_channel *dst,
4788 const union tgsi_exec_channel *src0,
4789 const union tgsi_exec_channel *src1)
4790 {
4791 dst->i[0] = src0->i[0] > src1->i[0] ? src0->i[0] : src1->i[0];
4792 dst->i[1] = src0->i[1] > src1->i[1] ? src0->i[1] : src1->i[1];
4793 dst->i[2] = src0->i[2] > src1->i[2] ? src0->i[2] : src1->i[2];
4794 dst->i[3] = src0->i[3] > src1->i[3] ? src0->i[3] : src1->i[3];
4795 }
4796
4797 static void
4798 micro_imin(union tgsi_exec_channel *dst,
4799 const union tgsi_exec_channel *src0,
4800 const union tgsi_exec_channel *src1)
4801 {
4802 dst->i[0] = src0->i[0] < src1->i[0] ? src0->i[0] : src1->i[0];
4803 dst->i[1] = src0->i[1] < src1->i[1] ? src0->i[1] : src1->i[1];
4804 dst->i[2] = src0->i[2] < src1->i[2] ? src0->i[2] : src1->i[2];
4805 dst->i[3] = src0->i[3] < src1->i[3] ? src0->i[3] : src1->i[3];
4806 }
4807
4808 static void
4809 micro_isge(union tgsi_exec_channel *dst,
4810 const union tgsi_exec_channel *src0,
4811 const union tgsi_exec_channel *src1)
4812 {
4813 dst->i[0] = src0->i[0] >= src1->i[0] ? -1 : 0;
4814 dst->i[1] = src0->i[1] >= src1->i[1] ? -1 : 0;
4815 dst->i[2] = src0->i[2] >= src1->i[2] ? -1 : 0;
4816 dst->i[3] = src0->i[3] >= src1->i[3] ? -1 : 0;
4817 }
4818
4819 static void
4820 micro_ishr(union tgsi_exec_channel *dst,
4821 const union tgsi_exec_channel *src0,
4822 const union tgsi_exec_channel *src1)
4823 {
4824 unsigned masked_count;
4825 masked_count = src1->i[0] & 0x1f;
4826 dst->i[0] = src0->i[0] >> masked_count;
4827 masked_count = src1->i[1] & 0x1f;
4828 dst->i[1] = src0->i[1] >> masked_count;
4829 masked_count = src1->i[2] & 0x1f;
4830 dst->i[2] = src0->i[2] >> masked_count;
4831 masked_count = src1->i[3] & 0x1f;
4832 dst->i[3] = src0->i[3] >> masked_count;
4833 }
4834
4835 static void
4836 micro_islt(union tgsi_exec_channel *dst,
4837 const union tgsi_exec_channel *src0,
4838 const union tgsi_exec_channel *src1)
4839 {
4840 dst->i[0] = src0->i[0] < src1->i[0] ? -1 : 0;
4841 dst->i[1] = src0->i[1] < src1->i[1] ? -1 : 0;
4842 dst->i[2] = src0->i[2] < src1->i[2] ? -1 : 0;
4843 dst->i[3] = src0->i[3] < src1->i[3] ? -1 : 0;
4844 }
4845
4846 static void
4847 micro_f2u(union tgsi_exec_channel *dst,
4848 const union tgsi_exec_channel *src)
4849 {
4850 dst->u[0] = (uint)src->f[0];
4851 dst->u[1] = (uint)src->f[1];
4852 dst->u[2] = (uint)src->f[2];
4853 dst->u[3] = (uint)src->f[3];
4854 }
4855
4856 static void
4857 micro_u2f(union tgsi_exec_channel *dst,
4858 const union tgsi_exec_channel *src)
4859 {
4860 dst->f[0] = (float)src->u[0];
4861 dst->f[1] = (float)src->u[1];
4862 dst->f[2] = (float)src->u[2];
4863 dst->f[3] = (float)src->u[3];
4864 }
4865
4866 static void
4867 micro_uadd(union tgsi_exec_channel *dst,
4868 const union tgsi_exec_channel *src0,
4869 const union tgsi_exec_channel *src1)
4870 {
4871 dst->u[0] = src0->u[0] + src1->u[0];
4872 dst->u[1] = src0->u[1] + src1->u[1];
4873 dst->u[2] = src0->u[2] + src1->u[2];
4874 dst->u[3] = src0->u[3] + src1->u[3];
4875 }
4876
4877 static void
4878 micro_udiv(union tgsi_exec_channel *dst,
4879 const union tgsi_exec_channel *src0,
4880 const union tgsi_exec_channel *src1)
4881 {
4882 dst->u[0] = src1->u[0] ? src0->u[0] / src1->u[0] : ~0u;
4883 dst->u[1] = src1->u[1] ? src0->u[1] / src1->u[1] : ~0u;
4884 dst->u[2] = src1->u[2] ? src0->u[2] / src1->u[2] : ~0u;
4885 dst->u[3] = src1->u[3] ? src0->u[3] / src1->u[3] : ~0u;
4886 }
4887
4888 static void
4889 micro_umad(union tgsi_exec_channel *dst,
4890 const union tgsi_exec_channel *src0,
4891 const union tgsi_exec_channel *src1,
4892 const union tgsi_exec_channel *src2)
4893 {
4894 dst->u[0] = src0->u[0] * src1->u[0] + src2->u[0];
4895 dst->u[1] = src0->u[1] * src1->u[1] + src2->u[1];
4896 dst->u[2] = src0->u[2] * src1->u[2] + src2->u[2];
4897 dst->u[3] = src0->u[3] * src1->u[3] + src2->u[3];
4898 }
4899
4900 static void
4901 micro_umax(union tgsi_exec_channel *dst,
4902 const union tgsi_exec_channel *src0,
4903 const union tgsi_exec_channel *src1)
4904 {
4905 dst->u[0] = src0->u[0] > src1->u[0] ? src0->u[0] : src1->u[0];
4906 dst->u[1] = src0->u[1] > src1->u[1] ? src0->u[1] : src1->u[1];
4907 dst->u[2] = src0->u[2] > src1->u[2] ? src0->u[2] : src1->u[2];
4908 dst->u[3] = src0->u[3] > src1->u[3] ? src0->u[3] : src1->u[3];
4909 }
4910
4911 static void
4912 micro_umin(union tgsi_exec_channel *dst,
4913 const union tgsi_exec_channel *src0,
4914 const union tgsi_exec_channel *src1)
4915 {
4916 dst->u[0] = src0->u[0] < src1->u[0] ? src0->u[0] : src1->u[0];
4917 dst->u[1] = src0->u[1] < src1->u[1] ? src0->u[1] : src1->u[1];
4918 dst->u[2] = src0->u[2] < src1->u[2] ? src0->u[2] : src1->u[2];
4919 dst->u[3] = src0->u[3] < src1->u[3] ? src0->u[3] : src1->u[3];
4920 }
4921
4922 static void
4923 micro_umod(union tgsi_exec_channel *dst,
4924 const union tgsi_exec_channel *src0,
4925 const union tgsi_exec_channel *src1)
4926 {
4927 dst->u[0] = src1->u[0] ? src0->u[0] % src1->u[0] : ~0u;
4928 dst->u[1] = src1->u[1] ? src0->u[1] % src1->u[1] : ~0u;
4929 dst->u[2] = src1->u[2] ? src0->u[2] % src1->u[2] : ~0u;
4930 dst->u[3] = src1->u[3] ? src0->u[3] % src1->u[3] : ~0u;
4931 }
4932
4933 static void
4934 micro_umul(union tgsi_exec_channel *dst,
4935 const union tgsi_exec_channel *src0,
4936 const union tgsi_exec_channel *src1)
4937 {
4938 dst->u[0] = src0->u[0] * src1->u[0];
4939 dst->u[1] = src0->u[1] * src1->u[1];
4940 dst->u[2] = src0->u[2] * src1->u[2];
4941 dst->u[3] = src0->u[3] * src1->u[3];
4942 }
4943
4944 static void
4945 micro_imul_hi(union tgsi_exec_channel *dst,
4946 const union tgsi_exec_channel *src0,
4947 const union tgsi_exec_channel *src1)
4948 {
4949 #define I64M(x, y) ((((int64_t)x) * ((int64_t)y)) >> 32)
4950 dst->i[0] = I64M(src0->i[0], src1->i[0]);
4951 dst->i[1] = I64M(src0->i[1], src1->i[1]);
4952 dst->i[2] = I64M(src0->i[2], src1->i[2]);
4953 dst->i[3] = I64M(src0->i[3], src1->i[3]);
4954 #undef I64M
4955 }
4956
4957 static void
4958 micro_umul_hi(union tgsi_exec_channel *dst,
4959 const union tgsi_exec_channel *src0,
4960 const union tgsi_exec_channel *src1)
4961 {
4962 #define U64M(x, y) ((((uint64_t)x) * ((uint64_t)y)) >> 32)
4963 dst->u[0] = U64M(src0->u[0], src1->u[0]);
4964 dst->u[1] = U64M(src0->u[1], src1->u[1]);
4965 dst->u[2] = U64M(src0->u[2], src1->u[2]);
4966 dst->u[3] = U64M(src0->u[3], src1->u[3]);
4967 #undef U64M
4968 }
4969
4970 static void
4971 micro_useq(union tgsi_exec_channel *dst,
4972 const union tgsi_exec_channel *src0,
4973 const union tgsi_exec_channel *src1)
4974 {
4975 dst->u[0] = src0->u[0] == src1->u[0] ? ~0 : 0;
4976 dst->u[1] = src0->u[1] == src1->u[1] ? ~0 : 0;
4977 dst->u[2] = src0->u[2] == src1->u[2] ? ~0 : 0;
4978 dst->u[3] = src0->u[3] == src1->u[3] ? ~0 : 0;
4979 }
4980
4981 static void
4982 micro_usge(union tgsi_exec_channel *dst,
4983 const union tgsi_exec_channel *src0,
4984 const union tgsi_exec_channel *src1)
4985 {
4986 dst->u[0] = src0->u[0] >= src1->u[0] ? ~0 : 0;
4987 dst->u[1] = src0->u[1] >= src1->u[1] ? ~0 : 0;
4988 dst->u[2] = src0->u[2] >= src1->u[2] ? ~0 : 0;
4989 dst->u[3] = src0->u[3] >= src1->u[3] ? ~0 : 0;
4990 }
4991
4992 static void
4993 micro_ushr(union tgsi_exec_channel *dst,
4994 const union tgsi_exec_channel *src0,
4995 const union tgsi_exec_channel *src1)
4996 {
4997 unsigned masked_count;
4998 masked_count = src1->u[0] & 0x1f;
4999 dst->u[0] = src0->u[0] >> masked_count;
5000 masked_count = src1->u[1] & 0x1f;
5001 dst->u[1] = src0->u[1] >> masked_count;
5002 masked_count = src1->u[2] & 0x1f;
5003 dst->u[2] = src0->u[2] >> masked_count;
5004 masked_count = src1->u[3] & 0x1f;
5005 dst->u[3] = src0->u[3] >> masked_count;
5006 }
5007
5008 static void
5009 micro_uslt(union tgsi_exec_channel *dst,
5010 const union tgsi_exec_channel *src0,
5011 const union tgsi_exec_channel *src1)
5012 {
5013 dst->u[0] = src0->u[0] < src1->u[0] ? ~0 : 0;
5014 dst->u[1] = src0->u[1] < src1->u[1] ? ~0 : 0;
5015 dst->u[2] = src0->u[2] < src1->u[2] ? ~0 : 0;
5016 dst->u[3] = src0->u[3] < src1->u[3] ? ~0 : 0;
5017 }
5018
5019 static void
5020 micro_usne(union tgsi_exec_channel *dst,
5021 const union tgsi_exec_channel *src0,
5022 const union tgsi_exec_channel *src1)
5023 {
5024 dst->u[0] = src0->u[0] != src1->u[0] ? ~0 : 0;
5025 dst->u[1] = src0->u[1] != src1->u[1] ? ~0 : 0;
5026 dst->u[2] = src0->u[2] != src1->u[2] ? ~0 : 0;
5027 dst->u[3] = src0->u[3] != src1->u[3] ? ~0 : 0;
5028 }
5029
5030 static void
5031 micro_uarl(union tgsi_exec_channel *dst,
5032 const union tgsi_exec_channel *src)
5033 {
5034 dst->i[0] = src->u[0];
5035 dst->i[1] = src->u[1];
5036 dst->i[2] = src->u[2];
5037 dst->i[3] = src->u[3];
5038 }
5039
5040 /**
5041 * Signed bitfield extract (i.e. sign-extend the extracted bits)
5042 */
5043 static void
5044 micro_ibfe(union tgsi_exec_channel *dst,
5045 const union tgsi_exec_channel *src0,
5046 const union tgsi_exec_channel *src1,
5047 const union tgsi_exec_channel *src2)
5048 {
5049 int i;
5050 for (i = 0; i < 4; i++) {
5051 int width = src2->i[i] & 0x1f;
5052 int offset = src1->i[i] & 0x1f;
5053 if (width == 0)
5054 dst->i[i] = 0;
5055 else if (width + offset < 32)
5056 dst->i[i] = (src0->i[i] << (32 - width - offset)) >> (32 - width);
5057 else
5058 dst->i[i] = src0->i[i] >> offset;
5059 }
5060 }
5061
5062 /**
5063 * Unsigned bitfield extract
5064 */
5065 static void
5066 micro_ubfe(union tgsi_exec_channel *dst,
5067 const union tgsi_exec_channel *src0,
5068 const union tgsi_exec_channel *src1,
5069 const union tgsi_exec_channel *src2)
5070 {
5071 int i;
5072 for (i = 0; i < 4; i++) {
5073 int width = src2->u[i] & 0x1f;
5074 int offset = src1->u[i] & 0x1f;
5075 if (width == 0)
5076 dst->u[i] = 0;
5077 else if (width + offset < 32)
5078 dst->u[i] = (src0->u[i] << (32 - width - offset)) >> (32 - width);
5079 else
5080 dst->u[i] = src0->u[i] >> offset;
5081 }
5082 }
5083
5084 /**
5085 * Bitfield insert: copy low bits from src1 into a region of src0.
5086 */
5087 static void
5088 micro_bfi(union tgsi_exec_channel *dst,
5089 const union tgsi_exec_channel *src0,
5090 const union tgsi_exec_channel *src1,
5091 const union tgsi_exec_channel *src2,
5092 const union tgsi_exec_channel *src3)
5093 {
5094 int i;
5095 for (i = 0; i < 4; i++) {
5096 int width = src3->u[i] & 0x1f;
5097 int offset = src2->u[i] & 0x1f;
5098 int bitmask = ((1 << width) - 1) << offset;
5099 dst->u[i] = ((src1->u[i] << offset) & bitmask) | (src0->u[i] & ~bitmask);
5100 }
5101 }
5102
5103 static void
5104 micro_brev(union tgsi_exec_channel *dst,
5105 const union tgsi_exec_channel *src)
5106 {
5107 dst->u[0] = util_bitreverse(src->u[0]);
5108 dst->u[1] = util_bitreverse(src->u[1]);
5109 dst->u[2] = util_bitreverse(src->u[2]);
5110 dst->u[3] = util_bitreverse(src->u[3]);
5111 }
5112
5113 static void
5114 micro_popc(union tgsi_exec_channel *dst,
5115 const union tgsi_exec_channel *src)
5116 {
5117 dst->u[0] = util_bitcount(src->u[0]);
5118 dst->u[1] = util_bitcount(src->u[1]);
5119 dst->u[2] = util_bitcount(src->u[2]);
5120 dst->u[3] = util_bitcount(src->u[3]);
5121 }
5122
5123 static void
5124 micro_lsb(union tgsi_exec_channel *dst,
5125 const union tgsi_exec_channel *src)
5126 {
5127 dst->i[0] = ffs(src->u[0]) - 1;
5128 dst->i[1] = ffs(src->u[1]) - 1;
5129 dst->i[2] = ffs(src->u[2]) - 1;
5130 dst->i[3] = ffs(src->u[3]) - 1;
5131 }
5132
5133 static void
5134 micro_imsb(union tgsi_exec_channel *dst,
5135 const union tgsi_exec_channel *src)
5136 {
5137 dst->i[0] = util_last_bit_signed(src->i[0]) - 1;
5138 dst->i[1] = util_last_bit_signed(src->i[1]) - 1;
5139 dst->i[2] = util_last_bit_signed(src->i[2]) - 1;
5140 dst->i[3] = util_last_bit_signed(src->i[3]) - 1;
5141 }
5142
5143 static void
5144 micro_umsb(union tgsi_exec_channel *dst,
5145 const union tgsi_exec_channel *src)
5146 {
5147 dst->i[0] = util_last_bit(src->u[0]) - 1;
5148 dst->i[1] = util_last_bit(src->u[1]) - 1;
5149 dst->i[2] = util_last_bit(src->u[2]) - 1;
5150 dst->i[3] = util_last_bit(src->u[3]) - 1;
5151 }
5152
5153 /**
5154 * Execute a TGSI instruction.
5155 * Returns TRUE if a barrier instruction is hit,
5156 * otherwise FALSE.
5157 */
5158 static boolean
5159 exec_instruction(
5160 struct tgsi_exec_machine *mach,
5161 const struct tgsi_full_instruction *inst,
5162 int *pc )
5163 {
5164 union tgsi_exec_channel r[10];
5165
5166 (*pc)++;
5167
5168 switch (inst->Instruction.Opcode) {
5169 case TGSI_OPCODE_ARL:
5170 exec_vector_unary(mach, inst, micro_arl, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_FLOAT);
5171 break;
5172
5173 case TGSI_OPCODE_MOV:
5174 exec_vector_unary(mach, inst, micro_mov, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
5175 break;
5176
5177 case TGSI_OPCODE_LIT:
5178 exec_lit(mach, inst);
5179 break;
5180
5181 case TGSI_OPCODE_RCP:
5182 exec_scalar_unary(mach, inst, micro_rcp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5183 break;
5184
5185 case TGSI_OPCODE_RSQ:
5186 exec_scalar_unary(mach, inst, micro_rsq, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5187 break;
5188
5189 case TGSI_OPCODE_EXP:
5190 exec_exp(mach, inst);
5191 break;
5192
5193 case TGSI_OPCODE_LOG:
5194 exec_log(mach, inst);
5195 break;
5196
5197 case TGSI_OPCODE_MUL:
5198 exec_vector_binary(mach, inst, micro_mul, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5199 break;
5200
5201 case TGSI_OPCODE_ADD:
5202 exec_vector_binary(mach, inst, micro_add, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5203 break;
5204
5205 case TGSI_OPCODE_DP3:
5206 exec_dp3(mach, inst);
5207 break;
5208
5209 case TGSI_OPCODE_DP4:
5210 exec_dp4(mach, inst);
5211 break;
5212
5213 case TGSI_OPCODE_DST:
5214 exec_dst(mach, inst);
5215 break;
5216
5217 case TGSI_OPCODE_MIN:
5218 exec_vector_binary(mach, inst, micro_min, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5219 break;
5220
5221 case TGSI_OPCODE_MAX:
5222 exec_vector_binary(mach, inst, micro_max, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5223 break;
5224
5225 case TGSI_OPCODE_SLT:
5226 exec_vector_binary(mach, inst, micro_slt, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5227 break;
5228
5229 case TGSI_OPCODE_SGE:
5230 exec_vector_binary(mach, inst, micro_sge, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5231 break;
5232
5233 case TGSI_OPCODE_MAD:
5234 exec_vector_trinary(mach, inst, micro_mad, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5235 break;
5236
5237 case TGSI_OPCODE_LRP:
5238 exec_vector_trinary(mach, inst, micro_lrp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5239 break;
5240
5241 case TGSI_OPCODE_SQRT:
5242 exec_scalar_unary(mach, inst, micro_sqrt, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5243 break;
5244
5245 case TGSI_OPCODE_DP2A:
5246 exec_dp2a(mach, inst);
5247 break;
5248
5249 case TGSI_OPCODE_FRC:
5250 exec_vector_unary(mach, inst, micro_frc, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5251 break;
5252
5253 case TGSI_OPCODE_FLR:
5254 exec_vector_unary(mach, inst, micro_flr, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5255 break;
5256
5257 case TGSI_OPCODE_ROUND:
5258 exec_vector_unary(mach, inst, micro_rnd, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5259 break;
5260
5261 case TGSI_OPCODE_EX2:
5262 exec_scalar_unary(mach, inst, micro_exp2, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5263 break;
5264
5265 case TGSI_OPCODE_LG2:
5266 exec_scalar_unary(mach, inst, micro_lg2, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5267 break;
5268
5269 case TGSI_OPCODE_POW:
5270 exec_scalar_binary(mach, inst, micro_pow, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5271 break;
5272
5273 case TGSI_OPCODE_XPD:
5274 exec_xpd(mach, inst);
5275 break;
5276
5277 case TGSI_OPCODE_DPH:
5278 exec_dph(mach, inst);
5279 break;
5280
5281 case TGSI_OPCODE_COS:
5282 exec_scalar_unary(mach, inst, micro_cos, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5283 break;
5284
5285 case TGSI_OPCODE_DDX:
5286 exec_vector_unary(mach, inst, micro_ddx, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5287 break;
5288
5289 case TGSI_OPCODE_DDY:
5290 exec_vector_unary(mach, inst, micro_ddy, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5291 break;
5292
5293 case TGSI_OPCODE_KILL:
5294 exec_kill (mach, inst);
5295 break;
5296
5297 case TGSI_OPCODE_KILL_IF:
5298 exec_kill_if (mach, inst);
5299 break;
5300
5301 case TGSI_OPCODE_PK2H:
5302 exec_pk2h(mach, inst);
5303 break;
5304
5305 case TGSI_OPCODE_PK2US:
5306 assert (0);
5307 break;
5308
5309 case TGSI_OPCODE_PK4B:
5310 assert (0);
5311 break;
5312
5313 case TGSI_OPCODE_PK4UB:
5314 assert (0);
5315 break;
5316
5317 case TGSI_OPCODE_SEQ:
5318 exec_vector_binary(mach, inst, micro_seq, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5319 break;
5320
5321 case TGSI_OPCODE_SGT:
5322 exec_vector_binary(mach, inst, micro_sgt, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5323 break;
5324
5325 case TGSI_OPCODE_SIN:
5326 exec_scalar_unary(mach, inst, micro_sin, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5327 break;
5328
5329 case TGSI_OPCODE_SLE:
5330 exec_vector_binary(mach, inst, micro_sle, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5331 break;
5332
5333 case TGSI_OPCODE_SNE:
5334 exec_vector_binary(mach, inst, micro_sne, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5335 break;
5336
5337 case TGSI_OPCODE_TEX:
5338 /* simple texture lookup */
5339 /* src[0] = texcoord */
5340 /* src[1] = sampler unit */
5341 exec_tex(mach, inst, TEX_MODIFIER_NONE, 1);
5342 break;
5343
5344 case TGSI_OPCODE_TXB:
5345 /* Texture lookup with lod bias */
5346 /* src[0] = texcoord (src[0].w = LOD bias) */
5347 /* src[1] = sampler unit */
5348 exec_tex(mach, inst, TEX_MODIFIER_LOD_BIAS, 1);
5349 break;
5350
5351 case TGSI_OPCODE_TXD:
5352 /* Texture lookup with explict partial derivatives */
5353 /* src[0] = texcoord */
5354 /* src[1] = d[strq]/dx */
5355 /* src[2] = d[strq]/dy */
5356 /* src[3] = sampler unit */
5357 exec_txd(mach, inst);
5358 break;
5359
5360 case TGSI_OPCODE_TXL:
5361 /* Texture lookup with explit LOD */
5362 /* src[0] = texcoord (src[0].w = LOD) */
5363 /* src[1] = sampler unit */
5364 exec_tex(mach, inst, TEX_MODIFIER_EXPLICIT_LOD, 1);
5365 break;
5366
5367 case TGSI_OPCODE_TXP:
5368 /* Texture lookup with projection */
5369 /* src[0] = texcoord (src[0].w = projection) */
5370 /* src[1] = sampler unit */
5371 exec_tex(mach, inst, TEX_MODIFIER_PROJECTED, 1);
5372 break;
5373
5374 case TGSI_OPCODE_TG4:
5375 /* src[0] = texcoord */
5376 /* src[1] = component */
5377 /* src[2] = sampler unit */
5378 exec_tex(mach, inst, TEX_MODIFIER_GATHER, 2);
5379 break;
5380
5381 case TGSI_OPCODE_LODQ:
5382 /* src[0] = texcoord */
5383 /* src[1] = sampler unit */
5384 exec_lodq(mach, inst);
5385 break;
5386
5387 case TGSI_OPCODE_UP2H:
5388 exec_up2h(mach, inst);
5389 break;
5390
5391 case TGSI_OPCODE_UP2US:
5392 assert (0);
5393 break;
5394
5395 case TGSI_OPCODE_UP4B:
5396 assert (0);
5397 break;
5398
5399 case TGSI_OPCODE_UP4UB:
5400 assert (0);
5401 break;
5402
5403 case TGSI_OPCODE_ARR:
5404 exec_vector_unary(mach, inst, micro_arr, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_FLOAT);
5405 break;
5406
5407 case TGSI_OPCODE_CAL:
5408 /* skip the call if no execution channels are enabled */
5409 if (mach->ExecMask) {
5410 /* do the call */
5411
5412 /* First, record the depths of the execution stacks.
5413 * This is important for deeply nested/looped return statements.
5414 * We have to unwind the stacks by the correct amount. For a
5415 * real code generator, we could determine the number of entries
5416 * to pop off each stack with simple static analysis and avoid
5417 * implementing this data structure at run time.
5418 */
5419 mach->CallStack[mach->CallStackTop].CondStackTop = mach->CondStackTop;
5420 mach->CallStack[mach->CallStackTop].LoopStackTop = mach->LoopStackTop;
5421 mach->CallStack[mach->CallStackTop].ContStackTop = mach->ContStackTop;
5422 mach->CallStack[mach->CallStackTop].SwitchStackTop = mach->SwitchStackTop;
5423 mach->CallStack[mach->CallStackTop].BreakStackTop = mach->BreakStackTop;
5424 /* note that PC was already incremented above */
5425 mach->CallStack[mach->CallStackTop].ReturnAddr = *pc;
5426
5427 mach->CallStackTop++;
5428
5429 /* Second, push the Cond, Loop, Cont, Func stacks */
5430 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
5431 assert(mach->LoopStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
5432 assert(mach->ContStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
5433 assert(mach->SwitchStackTop < TGSI_EXEC_MAX_SWITCH_NESTING);
5434 assert(mach->BreakStackTop < TGSI_EXEC_MAX_BREAK_STACK);
5435 assert(mach->FuncStackTop < TGSI_EXEC_MAX_CALL_NESTING);
5436
5437 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
5438 mach->LoopStack[mach->LoopStackTop++] = mach->LoopMask;
5439 mach->ContStack[mach->ContStackTop++] = mach->ContMask;
5440 mach->SwitchStack[mach->SwitchStackTop++] = mach->Switch;
5441 mach->BreakStack[mach->BreakStackTop++] = mach->BreakType;
5442 mach->FuncStack[mach->FuncStackTop++] = mach->FuncMask;
5443
5444 /* Finally, jump to the subroutine. The label is a pointer
5445 * (an instruction number) to the BGNSUB instruction.
5446 */
5447 *pc = inst->Label.Label;
5448 assert(mach->Instructions[*pc].Instruction.Opcode
5449 == TGSI_OPCODE_BGNSUB);
5450 }
5451 break;
5452
5453 case TGSI_OPCODE_RET:
5454 mach->FuncMask &= ~mach->ExecMask;
5455 UPDATE_EXEC_MASK(mach);
5456
5457 if (mach->FuncMask == 0x0) {
5458 /* really return now (otherwise, keep executing */
5459
5460 if (mach->CallStackTop == 0) {
5461 /* returning from main() */
5462 mach->CondStackTop = 0;
5463 mach->LoopStackTop = 0;
5464 mach->ContStackTop = 0;
5465 mach->LoopLabelStackTop = 0;
5466 mach->SwitchStackTop = 0;
5467 mach->BreakStackTop = 0;
5468 *pc = -1;
5469 return FALSE;
5470 }
5471
5472 assert(mach->CallStackTop > 0);
5473 mach->CallStackTop--;
5474
5475 mach->CondStackTop = mach->CallStack[mach->CallStackTop].CondStackTop;
5476 mach->CondMask = mach->CondStack[mach->CondStackTop];
5477
5478 mach->LoopStackTop = mach->CallStack[mach->CallStackTop].LoopStackTop;
5479 mach->LoopMask = mach->LoopStack[mach->LoopStackTop];
5480
5481 mach->ContStackTop = mach->CallStack[mach->CallStackTop].ContStackTop;
5482 mach->ContMask = mach->ContStack[mach->ContStackTop];
5483
5484 mach->SwitchStackTop = mach->CallStack[mach->CallStackTop].SwitchStackTop;
5485 mach->Switch = mach->SwitchStack[mach->SwitchStackTop];
5486
5487 mach->BreakStackTop = mach->CallStack[mach->CallStackTop].BreakStackTop;
5488 mach->BreakType = mach->BreakStack[mach->BreakStackTop];
5489
5490 assert(mach->FuncStackTop > 0);
5491 mach->FuncMask = mach->FuncStack[--mach->FuncStackTop];
5492
5493 *pc = mach->CallStack[mach->CallStackTop].ReturnAddr;
5494
5495 UPDATE_EXEC_MASK(mach);
5496 }
5497 break;
5498
5499 case TGSI_OPCODE_SSG:
5500 exec_vector_unary(mach, inst, micro_sgn, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5501 break;
5502
5503 case TGSI_OPCODE_CMP:
5504 exec_vector_trinary(mach, inst, micro_cmp, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5505 break;
5506
5507 case TGSI_OPCODE_SCS:
5508 exec_scs(mach, inst);
5509 break;
5510
5511 case TGSI_OPCODE_DIV:
5512 exec_vector_binary(mach, inst, micro_div, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5513 break;
5514
5515 case TGSI_OPCODE_DP2:
5516 exec_dp2(mach, inst);
5517 break;
5518
5519 case TGSI_OPCODE_IF:
5520 /* push CondMask */
5521 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
5522 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
5523 FETCH( &r[0], 0, TGSI_CHAN_X );
5524 /* update CondMask */
5525 if( ! r[0].f[0] ) {
5526 mach->CondMask &= ~0x1;
5527 }
5528 if( ! r[0].f[1] ) {
5529 mach->CondMask &= ~0x2;
5530 }
5531 if( ! r[0].f[2] ) {
5532 mach->CondMask &= ~0x4;
5533 }
5534 if( ! r[0].f[3] ) {
5535 mach->CondMask &= ~0x8;
5536 }
5537 UPDATE_EXEC_MASK(mach);
5538 /* Todo: If CondMask==0, jump to ELSE */
5539 break;
5540
5541 case TGSI_OPCODE_UIF:
5542 /* push CondMask */
5543 assert(mach->CondStackTop < TGSI_EXEC_MAX_COND_NESTING);
5544 mach->CondStack[mach->CondStackTop++] = mach->CondMask;
5545 IFETCH( &r[0], 0, TGSI_CHAN_X );
5546 /* update CondMask */
5547 if( ! r[0].u[0] ) {
5548 mach->CondMask &= ~0x1;
5549 }
5550 if( ! r[0].u[1] ) {
5551 mach->CondMask &= ~0x2;
5552 }
5553 if( ! r[0].u[2] ) {
5554 mach->CondMask &= ~0x4;
5555 }
5556 if( ! r[0].u[3] ) {
5557 mach->CondMask &= ~0x8;
5558 }
5559 UPDATE_EXEC_MASK(mach);
5560 /* Todo: If CondMask==0, jump to ELSE */
5561 break;
5562
5563 case TGSI_OPCODE_ELSE:
5564 /* invert CondMask wrt previous mask */
5565 {
5566 uint prevMask;
5567 assert(mach->CondStackTop > 0);
5568 prevMask = mach->CondStack[mach->CondStackTop - 1];
5569 mach->CondMask = ~mach->CondMask & prevMask;
5570 UPDATE_EXEC_MASK(mach);
5571 /* Todo: If CondMask==0, jump to ENDIF */
5572 }
5573 break;
5574
5575 case TGSI_OPCODE_ENDIF:
5576 /* pop CondMask */
5577 assert(mach->CondStackTop > 0);
5578 mach->CondMask = mach->CondStack[--mach->CondStackTop];
5579 UPDATE_EXEC_MASK(mach);
5580 break;
5581
5582 case TGSI_OPCODE_END:
5583 /* make sure we end primitives which haven't
5584 * been explicitly emitted */
5585 conditional_emit_primitive(mach);
5586 /* halt execution */
5587 *pc = -1;
5588 break;
5589
5590 case TGSI_OPCODE_PUSHA:
5591 assert (0);
5592 break;
5593
5594 case TGSI_OPCODE_POPA:
5595 assert (0);
5596 break;
5597
5598 case TGSI_OPCODE_CEIL:
5599 exec_vector_unary(mach, inst, micro_ceil, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5600 break;
5601
5602 case TGSI_OPCODE_I2F:
5603 exec_vector_unary(mach, inst, micro_i2f, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_INT);
5604 break;
5605
5606 case TGSI_OPCODE_NOT:
5607 exec_vector_unary(mach, inst, micro_not, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5608 break;
5609
5610 case TGSI_OPCODE_TRUNC:
5611 exec_vector_unary(mach, inst, micro_trunc, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_FLOAT);
5612 break;
5613
5614 case TGSI_OPCODE_SHL:
5615 exec_vector_binary(mach, inst, micro_shl, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5616 break;
5617
5618 case TGSI_OPCODE_AND:
5619 exec_vector_binary(mach, inst, micro_and, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5620 break;
5621
5622 case TGSI_OPCODE_OR:
5623 exec_vector_binary(mach, inst, micro_or, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5624 break;
5625
5626 case TGSI_OPCODE_MOD:
5627 exec_vector_binary(mach, inst, micro_mod, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5628 break;
5629
5630 case TGSI_OPCODE_XOR:
5631 exec_vector_binary(mach, inst, micro_xor, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5632 break;
5633
5634 case TGSI_OPCODE_SAD:
5635 assert (0);
5636 break;
5637
5638 case TGSI_OPCODE_TXF:
5639 exec_txf(mach, inst);
5640 break;
5641
5642 case TGSI_OPCODE_TXQ:
5643 exec_txq(mach, inst);
5644 break;
5645
5646 case TGSI_OPCODE_EMIT:
5647 emit_vertex(mach);
5648 break;
5649
5650 case TGSI_OPCODE_ENDPRIM:
5651 emit_primitive(mach);
5652 break;
5653
5654 case TGSI_OPCODE_BGNLOOP:
5655 /* push LoopMask and ContMasks */
5656 assert(mach->LoopStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
5657 assert(mach->ContStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
5658 assert(mach->LoopLabelStackTop < TGSI_EXEC_MAX_LOOP_NESTING);
5659 assert(mach->BreakStackTop < TGSI_EXEC_MAX_BREAK_STACK);
5660
5661 mach->LoopStack[mach->LoopStackTop++] = mach->LoopMask;
5662 mach->ContStack[mach->ContStackTop++] = mach->ContMask;
5663 mach->LoopLabelStack[mach->LoopLabelStackTop++] = *pc - 1;
5664 mach->BreakStack[mach->BreakStackTop++] = mach->BreakType;
5665 mach->BreakType = TGSI_EXEC_BREAK_INSIDE_LOOP;
5666 break;
5667
5668 case TGSI_OPCODE_ENDLOOP:
5669 /* Restore ContMask, but don't pop */
5670 assert(mach->ContStackTop > 0);
5671 mach->ContMask = mach->ContStack[mach->ContStackTop - 1];
5672 UPDATE_EXEC_MASK(mach);
5673 if (mach->ExecMask) {
5674 /* repeat loop: jump to instruction just past BGNLOOP */
5675 assert(mach->LoopLabelStackTop > 0);
5676 *pc = mach->LoopLabelStack[mach->LoopLabelStackTop - 1] + 1;
5677 }
5678 else {
5679 /* exit loop: pop LoopMask */
5680 assert(mach->LoopStackTop > 0);
5681 mach->LoopMask = mach->LoopStack[--mach->LoopStackTop];
5682 /* pop ContMask */
5683 assert(mach->ContStackTop > 0);
5684 mach->ContMask = mach->ContStack[--mach->ContStackTop];
5685 assert(mach->LoopLabelStackTop > 0);
5686 --mach->LoopLabelStackTop;
5687
5688 mach->BreakType = mach->BreakStack[--mach->BreakStackTop];
5689 }
5690 UPDATE_EXEC_MASK(mach);
5691 break;
5692
5693 case TGSI_OPCODE_BRK:
5694 exec_break(mach);
5695 break;
5696
5697 case TGSI_OPCODE_CONT:
5698 /* turn off cont channels for each enabled exec channel */
5699 mach->ContMask &= ~mach->ExecMask;
5700 /* Todo: if mach->LoopMask == 0, jump to end of loop */
5701 UPDATE_EXEC_MASK(mach);
5702 break;
5703
5704 case TGSI_OPCODE_BGNSUB:
5705 /* no-op */
5706 break;
5707
5708 case TGSI_OPCODE_ENDSUB:
5709 /*
5710 * XXX: This really should be a no-op. We should never reach this opcode.
5711 */
5712
5713 assert(mach->CallStackTop > 0);
5714 mach->CallStackTop--;
5715
5716 mach->CondStackTop = mach->CallStack[mach->CallStackTop].CondStackTop;
5717 mach->CondMask = mach->CondStack[mach->CondStackTop];
5718
5719 mach->LoopStackTop = mach->CallStack[mach->CallStackTop].LoopStackTop;
5720 mach->LoopMask = mach->LoopStack[mach->LoopStackTop];
5721
5722 mach->ContStackTop = mach->CallStack[mach->CallStackTop].ContStackTop;
5723 mach->ContMask = mach->ContStack[mach->ContStackTop];
5724
5725 mach->SwitchStackTop = mach->CallStack[mach->CallStackTop].SwitchStackTop;
5726 mach->Switch = mach->SwitchStack[mach->SwitchStackTop];
5727
5728 mach->BreakStackTop = mach->CallStack[mach->CallStackTop].BreakStackTop;
5729 mach->BreakType = mach->BreakStack[mach->BreakStackTop];
5730
5731 assert(mach->FuncStackTop > 0);
5732 mach->FuncMask = mach->FuncStack[--mach->FuncStackTop];
5733
5734 *pc = mach->CallStack[mach->CallStackTop].ReturnAddr;
5735
5736 UPDATE_EXEC_MASK(mach);
5737 break;
5738
5739 case TGSI_OPCODE_NOP:
5740 break;
5741
5742 case TGSI_OPCODE_BREAKC:
5743 IFETCH(&r[0], 0, TGSI_CHAN_X);
5744 /* update CondMask */
5745 if (r[0].u[0] && (mach->ExecMask & 0x1)) {
5746 mach->LoopMask &= ~0x1;
5747 }
5748 if (r[0].u[1] && (mach->ExecMask & 0x2)) {
5749 mach->LoopMask &= ~0x2;
5750 }
5751 if (r[0].u[2] && (mach->ExecMask & 0x4)) {
5752 mach->LoopMask &= ~0x4;
5753 }
5754 if (r[0].u[3] && (mach->ExecMask & 0x8)) {
5755 mach->LoopMask &= ~0x8;
5756 }
5757 /* Todo: if mach->LoopMask == 0, jump to end of loop */
5758 UPDATE_EXEC_MASK(mach);
5759 break;
5760
5761 case TGSI_OPCODE_F2I:
5762 exec_vector_unary(mach, inst, micro_f2i, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_FLOAT);
5763 break;
5764
5765 case TGSI_OPCODE_FSEQ:
5766 exec_vector_binary(mach, inst, micro_fseq, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
5767 break;
5768
5769 case TGSI_OPCODE_FSGE:
5770 exec_vector_binary(mach, inst, micro_fsge, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
5771 break;
5772
5773 case TGSI_OPCODE_FSLT:
5774 exec_vector_binary(mach, inst, micro_fslt, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
5775 break;
5776
5777 case TGSI_OPCODE_FSNE:
5778 exec_vector_binary(mach, inst, micro_fsne, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
5779 break;
5780
5781 case TGSI_OPCODE_IDIV:
5782 exec_vector_binary(mach, inst, micro_idiv, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5783 break;
5784
5785 case TGSI_OPCODE_IMAX:
5786 exec_vector_binary(mach, inst, micro_imax, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5787 break;
5788
5789 case TGSI_OPCODE_IMIN:
5790 exec_vector_binary(mach, inst, micro_imin, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5791 break;
5792
5793 case TGSI_OPCODE_INEG:
5794 exec_vector_unary(mach, inst, micro_ineg, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5795 break;
5796
5797 case TGSI_OPCODE_ISGE:
5798 exec_vector_binary(mach, inst, micro_isge, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5799 break;
5800
5801 case TGSI_OPCODE_ISHR:
5802 exec_vector_binary(mach, inst, micro_ishr, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5803 break;
5804
5805 case TGSI_OPCODE_ISLT:
5806 exec_vector_binary(mach, inst, micro_islt, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5807 break;
5808
5809 case TGSI_OPCODE_F2U:
5810 exec_vector_unary(mach, inst, micro_f2u, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_FLOAT);
5811 break;
5812
5813 case TGSI_OPCODE_U2F:
5814 exec_vector_unary(mach, inst, micro_u2f, TGSI_EXEC_DATA_FLOAT, TGSI_EXEC_DATA_UINT);
5815 break;
5816
5817 case TGSI_OPCODE_UADD:
5818 exec_vector_binary(mach, inst, micro_uadd, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5819 break;
5820
5821 case TGSI_OPCODE_UDIV:
5822 exec_vector_binary(mach, inst, micro_udiv, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5823 break;
5824
5825 case TGSI_OPCODE_UMAD:
5826 exec_vector_trinary(mach, inst, micro_umad, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5827 break;
5828
5829 case TGSI_OPCODE_UMAX:
5830 exec_vector_binary(mach, inst, micro_umax, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5831 break;
5832
5833 case TGSI_OPCODE_UMIN:
5834 exec_vector_binary(mach, inst, micro_umin, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5835 break;
5836
5837 case TGSI_OPCODE_UMOD:
5838 exec_vector_binary(mach, inst, micro_umod, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5839 break;
5840
5841 case TGSI_OPCODE_UMUL:
5842 exec_vector_binary(mach, inst, micro_umul, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5843 break;
5844
5845 case TGSI_OPCODE_IMUL_HI:
5846 exec_vector_binary(mach, inst, micro_imul_hi, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5847 break;
5848
5849 case TGSI_OPCODE_UMUL_HI:
5850 exec_vector_binary(mach, inst, micro_umul_hi, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5851 break;
5852
5853 case TGSI_OPCODE_USEQ:
5854 exec_vector_binary(mach, inst, micro_useq, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5855 break;
5856
5857 case TGSI_OPCODE_USGE:
5858 exec_vector_binary(mach, inst, micro_usge, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5859 break;
5860
5861 case TGSI_OPCODE_USHR:
5862 exec_vector_binary(mach, inst, micro_ushr, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5863 break;
5864
5865 case TGSI_OPCODE_USLT:
5866 exec_vector_binary(mach, inst, micro_uslt, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5867 break;
5868
5869 case TGSI_OPCODE_USNE:
5870 exec_vector_binary(mach, inst, micro_usne, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5871 break;
5872
5873 case TGSI_OPCODE_SWITCH:
5874 exec_switch(mach, inst);
5875 break;
5876
5877 case TGSI_OPCODE_CASE:
5878 exec_case(mach, inst);
5879 break;
5880
5881 case TGSI_OPCODE_DEFAULT:
5882 exec_default(mach);
5883 break;
5884
5885 case TGSI_OPCODE_ENDSWITCH:
5886 exec_endswitch(mach);
5887 break;
5888
5889 case TGSI_OPCODE_SAMPLE_I:
5890 exec_txf(mach, inst);
5891 break;
5892
5893 case TGSI_OPCODE_SAMPLE_I_MS:
5894 exec_txf(mach, inst);
5895 break;
5896
5897 case TGSI_OPCODE_SAMPLE:
5898 exec_sample(mach, inst, TEX_MODIFIER_NONE, FALSE);
5899 break;
5900
5901 case TGSI_OPCODE_SAMPLE_B:
5902 exec_sample(mach, inst, TEX_MODIFIER_LOD_BIAS, FALSE);
5903 break;
5904
5905 case TGSI_OPCODE_SAMPLE_C:
5906 exec_sample(mach, inst, TEX_MODIFIER_NONE, TRUE);
5907 break;
5908
5909 case TGSI_OPCODE_SAMPLE_C_LZ:
5910 exec_sample(mach, inst, TEX_MODIFIER_LEVEL_ZERO, TRUE);
5911 break;
5912
5913 case TGSI_OPCODE_SAMPLE_D:
5914 exec_sample_d(mach, inst);
5915 break;
5916
5917 case TGSI_OPCODE_SAMPLE_L:
5918 exec_sample(mach, inst, TEX_MODIFIER_EXPLICIT_LOD, FALSE);
5919 break;
5920
5921 case TGSI_OPCODE_GATHER4:
5922 assert(0);
5923 break;
5924
5925 case TGSI_OPCODE_SVIEWINFO:
5926 exec_txq(mach, inst);
5927 break;
5928
5929 case TGSI_OPCODE_SAMPLE_POS:
5930 assert(0);
5931 break;
5932
5933 case TGSI_OPCODE_SAMPLE_INFO:
5934 assert(0);
5935 break;
5936
5937 case TGSI_OPCODE_UARL:
5938 exec_vector_unary(mach, inst, micro_uarl, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_UINT);
5939 break;
5940
5941 case TGSI_OPCODE_UCMP:
5942 exec_ucmp(mach, inst);
5943 break;
5944
5945 case TGSI_OPCODE_IABS:
5946 exec_vector_unary(mach, inst, micro_iabs, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5947 break;
5948
5949 case TGSI_OPCODE_ISSG:
5950 exec_vector_unary(mach, inst, micro_isgn, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5951 break;
5952
5953 case TGSI_OPCODE_TEX2:
5954 /* simple texture lookup */
5955 /* src[0] = texcoord */
5956 /* src[1] = compare */
5957 /* src[2] = sampler unit */
5958 exec_tex(mach, inst, TEX_MODIFIER_NONE, 2);
5959 break;
5960 case TGSI_OPCODE_TXB2:
5961 /* simple texture lookup */
5962 /* src[0] = texcoord */
5963 /* src[1] = bias */
5964 /* src[2] = sampler unit */
5965 exec_tex(mach, inst, TEX_MODIFIER_LOD_BIAS, 2);
5966 break;
5967 case TGSI_OPCODE_TXL2:
5968 /* simple texture lookup */
5969 /* src[0] = texcoord */
5970 /* src[1] = lod */
5971 /* src[2] = sampler unit */
5972 exec_tex(mach, inst, TEX_MODIFIER_EXPLICIT_LOD, 2);
5973 break;
5974
5975 case TGSI_OPCODE_IBFE:
5976 exec_vector_trinary(mach, inst, micro_ibfe, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5977 break;
5978 case TGSI_OPCODE_UBFE:
5979 exec_vector_trinary(mach, inst, micro_ubfe, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5980 break;
5981 case TGSI_OPCODE_BFI:
5982 exec_vector_quaternary(mach, inst, micro_bfi, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5983 break;
5984 case TGSI_OPCODE_BREV:
5985 exec_vector_unary(mach, inst, micro_brev, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5986 break;
5987 case TGSI_OPCODE_POPC:
5988 exec_vector_unary(mach, inst, micro_popc, TGSI_EXEC_DATA_UINT, TGSI_EXEC_DATA_UINT);
5989 break;
5990 case TGSI_OPCODE_LSB:
5991 exec_vector_unary(mach, inst, micro_lsb, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_UINT);
5992 break;
5993 case TGSI_OPCODE_IMSB:
5994 exec_vector_unary(mach, inst, micro_imsb, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_INT);
5995 break;
5996 case TGSI_OPCODE_UMSB:
5997 exec_vector_unary(mach, inst, micro_umsb, TGSI_EXEC_DATA_INT, TGSI_EXEC_DATA_UINT);
5998 break;
5999
6000 case TGSI_OPCODE_F2D:
6001 exec_t_2_64(mach, inst, micro_f2d, TGSI_EXEC_DATA_FLOAT);
6002 break;
6003
6004 case TGSI_OPCODE_D2F:
6005 exec_64_2_t(mach, inst, micro_d2f, TGSI_EXEC_DATA_FLOAT);
6006 break;
6007
6008 case TGSI_OPCODE_DABS:
6009 exec_double_unary(mach, inst, micro_dabs);
6010 break;
6011
6012 case TGSI_OPCODE_DNEG:
6013 exec_double_unary(mach, inst, micro_dneg);
6014 break;
6015
6016 case TGSI_OPCODE_DADD:
6017 exec_double_binary(mach, inst, micro_dadd, TGSI_EXEC_DATA_DOUBLE);
6018 break;
6019
6020 case TGSI_OPCODE_DDIV:
6021 exec_double_binary(mach, inst, micro_ddiv, TGSI_EXEC_DATA_DOUBLE);
6022 break;
6023
6024 case TGSI_OPCODE_DMUL:
6025 exec_double_binary(mach, inst, micro_dmul, TGSI_EXEC_DATA_DOUBLE);
6026 break;
6027
6028 case TGSI_OPCODE_DMAX:
6029 exec_double_binary(mach, inst, micro_dmax, TGSI_EXEC_DATA_DOUBLE);
6030 break;
6031
6032 case TGSI_OPCODE_DMIN:
6033 exec_double_binary(mach, inst, micro_dmin, TGSI_EXEC_DATA_DOUBLE);
6034 break;
6035
6036 case TGSI_OPCODE_DSLT:
6037 exec_double_binary(mach, inst, micro_dslt, TGSI_EXEC_DATA_UINT);
6038 break;
6039
6040 case TGSI_OPCODE_DSGE:
6041 exec_double_binary(mach, inst, micro_dsge, TGSI_EXEC_DATA_UINT);
6042 break;
6043
6044 case TGSI_OPCODE_DSEQ:
6045 exec_double_binary(mach, inst, micro_dseq, TGSI_EXEC_DATA_UINT);
6046 break;
6047
6048 case TGSI_OPCODE_DSNE:
6049 exec_double_binary(mach, inst, micro_dsne, TGSI_EXEC_DATA_UINT);
6050 break;
6051
6052 case TGSI_OPCODE_DRCP:
6053 exec_double_unary(mach, inst, micro_drcp);
6054 break;
6055
6056 case TGSI_OPCODE_DSQRT:
6057 exec_double_unary(mach, inst, micro_dsqrt);
6058 break;
6059
6060 case TGSI_OPCODE_DRSQ:
6061 exec_double_unary(mach, inst, micro_drsq);
6062 break;
6063
6064 case TGSI_OPCODE_DMAD:
6065 exec_double_trinary(mach, inst, micro_dmad);
6066 break;
6067
6068 case TGSI_OPCODE_DFRAC:
6069 exec_double_unary(mach, inst, micro_dfrac);
6070 break;
6071
6072 case TGSI_OPCODE_DLDEXP:
6073 exec_dldexp(mach, inst);
6074 break;
6075
6076 case TGSI_OPCODE_DFRACEXP:
6077 exec_dfracexp(mach, inst);
6078 break;
6079
6080 case TGSI_OPCODE_I2D:
6081 exec_t_2_64(mach, inst, micro_i2d, TGSI_EXEC_DATA_INT);
6082 break;
6083
6084 case TGSI_OPCODE_D2I:
6085 exec_64_2_t(mach, inst, micro_d2i, TGSI_EXEC_DATA_INT);
6086 break;
6087
6088 case TGSI_OPCODE_U2D:
6089 exec_t_2_64(mach, inst, micro_u2d, TGSI_EXEC_DATA_UINT);
6090 break;
6091
6092 case TGSI_OPCODE_D2U:
6093 exec_64_2_t(mach, inst, micro_d2u, TGSI_EXEC_DATA_INT);
6094 break;
6095
6096 case TGSI_OPCODE_LOAD:
6097 exec_load(mach, inst);
6098 break;
6099
6100 case TGSI_OPCODE_STORE:
6101 exec_store(mach, inst);
6102 break;
6103
6104 case TGSI_OPCODE_ATOMUADD:
6105 case TGSI_OPCODE_ATOMXCHG:
6106 case TGSI_OPCODE_ATOMCAS:
6107 case TGSI_OPCODE_ATOMAND:
6108 case TGSI_OPCODE_ATOMOR:
6109 case TGSI_OPCODE_ATOMXOR:
6110 case TGSI_OPCODE_ATOMUMIN:
6111 case TGSI_OPCODE_ATOMUMAX:
6112 case TGSI_OPCODE_ATOMIMIN:
6113 case TGSI_OPCODE_ATOMIMAX:
6114 exec_atomop(mach, inst);
6115 break;
6116
6117 case TGSI_OPCODE_RESQ:
6118 exec_resq(mach, inst);
6119 break;
6120 case TGSI_OPCODE_BARRIER:
6121 case TGSI_OPCODE_MEMBAR:
6122 return TRUE;
6123 break;
6124
6125 case TGSI_OPCODE_I64ABS:
6126 exec_double_unary(mach, inst, micro_i64abs);
6127 break;
6128
6129 case TGSI_OPCODE_I64SSG:
6130 exec_double_unary(mach, inst, micro_i64sgn);
6131 break;
6132
6133 case TGSI_OPCODE_I64NEG:
6134 exec_double_unary(mach, inst, micro_i64neg);
6135 break;
6136
6137 case TGSI_OPCODE_U64SEQ:
6138 exec_double_binary(mach, inst, micro_u64seq, TGSI_EXEC_DATA_UINT);
6139 break;
6140
6141 case TGSI_OPCODE_U64SNE:
6142 exec_double_binary(mach, inst, micro_u64sne, TGSI_EXEC_DATA_UINT);
6143 break;
6144
6145 case TGSI_OPCODE_I64SLT:
6146 exec_double_binary(mach, inst, micro_i64slt, TGSI_EXEC_DATA_UINT);
6147 break;
6148 case TGSI_OPCODE_U64SLT:
6149 exec_double_binary(mach, inst, micro_u64slt, TGSI_EXEC_DATA_UINT);
6150 break;
6151
6152 case TGSI_OPCODE_I64SGE:
6153 exec_double_binary(mach, inst, micro_i64sge, TGSI_EXEC_DATA_UINT);
6154 break;
6155 case TGSI_OPCODE_U64SGE:
6156 exec_double_binary(mach, inst, micro_u64sge, TGSI_EXEC_DATA_UINT);
6157 break;
6158
6159 case TGSI_OPCODE_I64MIN:
6160 exec_double_binary(mach, inst, micro_i64min, TGSI_EXEC_DATA_INT64);
6161 break;
6162 case TGSI_OPCODE_U64MIN:
6163 exec_double_binary(mach, inst, micro_u64min, TGSI_EXEC_DATA_UINT64);
6164 break;
6165 case TGSI_OPCODE_I64MAX:
6166 exec_double_binary(mach, inst, micro_i64max, TGSI_EXEC_DATA_INT64);
6167 break;
6168 case TGSI_OPCODE_U64MAX:
6169 exec_double_binary(mach, inst, micro_u64max, TGSI_EXEC_DATA_UINT64);
6170 break;
6171 case TGSI_OPCODE_U64ADD:
6172 exec_double_binary(mach, inst, micro_u64add, TGSI_EXEC_DATA_UINT64);
6173 break;
6174 case TGSI_OPCODE_U64MUL:
6175 exec_double_binary(mach, inst, micro_u64mul, TGSI_EXEC_DATA_UINT64);
6176 break;
6177 case TGSI_OPCODE_U64SHL:
6178 exec_arg0_64_arg1_32(mach, inst, micro_u64shl);
6179 break;
6180 case TGSI_OPCODE_I64SHR:
6181 exec_arg0_64_arg1_32(mach, inst, micro_i64shr);
6182 break;
6183 case TGSI_OPCODE_U64SHR:
6184 exec_arg0_64_arg1_32(mach, inst, micro_u64shr);
6185 break;
6186 case TGSI_OPCODE_U64DIV:
6187 exec_double_binary(mach, inst, micro_u64div, TGSI_EXEC_DATA_UINT64);
6188 break;
6189 case TGSI_OPCODE_I64DIV:
6190 exec_double_binary(mach, inst, micro_i64div, TGSI_EXEC_DATA_INT64);
6191 break;
6192 case TGSI_OPCODE_U64MOD:
6193 exec_double_binary(mach, inst, micro_u64mod, TGSI_EXEC_DATA_UINT64);
6194 break;
6195 case TGSI_OPCODE_I64MOD:
6196 exec_double_binary(mach, inst, micro_i64mod, TGSI_EXEC_DATA_INT64);
6197 break;
6198
6199 case TGSI_OPCODE_F2U64:
6200 exec_t_2_64(mach, inst, micro_f2u64, TGSI_EXEC_DATA_FLOAT);
6201 break;
6202
6203 case TGSI_OPCODE_F2I64:
6204 exec_t_2_64(mach, inst, micro_f2i64, TGSI_EXEC_DATA_FLOAT);
6205 break;
6206
6207 case TGSI_OPCODE_U2I64:
6208 exec_t_2_64(mach, inst, micro_u2i64, TGSI_EXEC_DATA_INT);
6209 break;
6210 case TGSI_OPCODE_I2I64:
6211 exec_t_2_64(mach, inst, micro_i2i64, TGSI_EXEC_DATA_INT);
6212 break;
6213
6214 case TGSI_OPCODE_D2U64:
6215 exec_double_unary(mach, inst, micro_d2u64);
6216 break;
6217
6218 case TGSI_OPCODE_D2I64:
6219 exec_double_unary(mach, inst, micro_d2i64);
6220 break;
6221
6222 case TGSI_OPCODE_U642F:
6223 exec_64_2_t(mach, inst, micro_u642f, TGSI_EXEC_DATA_FLOAT);
6224 break;
6225 case TGSI_OPCODE_I642F:
6226 exec_64_2_t(mach, inst, micro_i642f, TGSI_EXEC_DATA_FLOAT);
6227 break;
6228
6229 case TGSI_OPCODE_U642D:
6230 exec_double_unary(mach, inst, micro_u642d);
6231 break;
6232 case TGSI_OPCODE_I642D:
6233 exec_double_unary(mach, inst, micro_i642d);
6234 break;
6235
6236 default:
6237 assert( 0 );
6238 }
6239 return FALSE;
6240 }
6241
6242 static void
6243 tgsi_exec_machine_setup_masks(struct tgsi_exec_machine *mach)
6244 {
6245 uint default_mask = 0xf;
6246
6247 mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0] = 0;
6248 mach->Temps[TEMP_OUTPUT_I].xyzw[TEMP_OUTPUT_C].u[0] = 0;
6249
6250 if (mach->ShaderType == PIPE_SHADER_GEOMETRY) {
6251 mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0] = 0;
6252 mach->Primitives[0] = 0;
6253 /* GS runs on a single primitive for now */
6254 default_mask = 0x1;
6255 }
6256
6257 if (mach->NonHelperMask == 0)
6258 mach->NonHelperMask = default_mask;
6259 mach->CondMask = default_mask;
6260 mach->LoopMask = default_mask;
6261 mach->ContMask = default_mask;
6262 mach->FuncMask = default_mask;
6263 mach->ExecMask = default_mask;
6264
6265 mach->Switch.mask = default_mask;
6266
6267 assert(mach->CondStackTop == 0);
6268 assert(mach->LoopStackTop == 0);
6269 assert(mach->ContStackTop == 0);
6270 assert(mach->SwitchStackTop == 0);
6271 assert(mach->BreakStackTop == 0);
6272 assert(mach->CallStackTop == 0);
6273 }
6274
6275 /**
6276 * Run TGSI interpreter.
6277 * \return bitmask of "alive" quad components
6278 */
6279 uint
6280 tgsi_exec_machine_run( struct tgsi_exec_machine *mach, int start_pc )
6281 {
6282 uint i;
6283
6284 mach->pc = start_pc;
6285
6286 if (!start_pc) {
6287 tgsi_exec_machine_setup_masks(mach);
6288
6289 /* execute declarations (interpolants) */
6290 for (i = 0; i < mach->NumDeclarations; i++) {
6291 exec_declaration( mach, mach->Declarations+i );
6292 }
6293 }
6294
6295 {
6296 #if DEBUG_EXECUTION
6297 struct tgsi_exec_vector temps[TGSI_EXEC_NUM_TEMPS + TGSI_EXEC_NUM_TEMP_EXTRAS];
6298 struct tgsi_exec_vector outputs[PIPE_MAX_ATTRIBS];
6299 uint inst = 1;
6300
6301 if (!start_pc) {
6302 memset(mach->Temps, 0, sizeof(temps));
6303 if (mach->Outputs)
6304 memset(mach->Outputs, 0, sizeof(outputs));
6305 memset(temps, 0, sizeof(temps));
6306 memset(outputs, 0, sizeof(outputs));
6307 }
6308 #endif
6309
6310 /* execute instructions, until pc is set to -1 */
6311 while (mach->pc != -1) {
6312 boolean barrier_hit;
6313 #if DEBUG_EXECUTION
6314 uint i;
6315
6316 tgsi_dump_instruction(&mach->Instructions[mach->pc], inst++);
6317 #endif
6318
6319 assert(mach->pc < (int) mach->NumInstructions);
6320 barrier_hit = exec_instruction(mach, mach->Instructions + mach->pc, &mach->pc);
6321
6322 /* for compute shaders if we hit a barrier return now for later rescheduling */
6323 if (barrier_hit && mach->ShaderType == PIPE_SHADER_COMPUTE)
6324 return 0;
6325
6326 #if DEBUG_EXECUTION
6327 for (i = 0; i < TGSI_EXEC_NUM_TEMPS + TGSI_EXEC_NUM_TEMP_EXTRAS; i++) {
6328 if (memcmp(&temps[i], &mach->Temps[i], sizeof(temps[i]))) {
6329 uint j;
6330
6331 memcpy(&temps[i], &mach->Temps[i], sizeof(temps[i]));
6332 debug_printf("TEMP[%2u] = ", i);
6333 for (j = 0; j < 4; j++) {
6334 if (j > 0) {
6335 debug_printf(" ");
6336 }
6337 debug_printf("(%6f %u, %6f %u, %6f %u, %6f %u)\n",
6338 temps[i].xyzw[0].f[j], temps[i].xyzw[0].u[j],
6339 temps[i].xyzw[1].f[j], temps[i].xyzw[1].u[j],
6340 temps[i].xyzw[2].f[j], temps[i].xyzw[2].u[j],
6341 temps[i].xyzw[3].f[j], temps[i].xyzw[3].u[j]);
6342 }
6343 }
6344 }
6345 if (mach->Outputs) {
6346 for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
6347 if (memcmp(&outputs[i], &mach->Outputs[i], sizeof(outputs[i]))) {
6348 uint j;
6349
6350 memcpy(&outputs[i], &mach->Outputs[i], sizeof(outputs[i]));
6351 debug_printf("OUT[%2u] = ", i);
6352 for (j = 0; j < 4; j++) {
6353 if (j > 0) {
6354 debug_printf(" ");
6355 }
6356 debug_printf("(%6f %u, %6f %u, %6f %u, %6f %u)\n",
6357 outputs[i].xyzw[0].f[j], outputs[i].xyzw[0].u[j],
6358 outputs[i].xyzw[1].f[j], outputs[i].xyzw[1].u[j],
6359 outputs[i].xyzw[2].f[j], outputs[i].xyzw[2].u[j],
6360 outputs[i].xyzw[3].f[j], outputs[i].xyzw[3].u[j]);
6361 }
6362 }
6363 }
6364 }
6365 #endif
6366 }
6367 }
6368
6369 #if 0
6370 /* we scale from floats in [0,1] to Zbuffer ints in sp_quad_depth_test.c */
6371 if (mach->ShaderType == PIPE_SHADER_FRAGMENT) {
6372 /*
6373 * Scale back depth component.
6374 */
6375 for (i = 0; i < 4; i++)
6376 mach->Outputs[0].xyzw[2].f[i] *= ctx->DrawBuffer->_DepthMaxF;
6377 }
6378 #endif
6379
6380 /* Strictly speaking, these assertions aren't really needed but they
6381 * can potentially catch some bugs in the control flow code.
6382 */
6383 assert(mach->CondStackTop == 0);
6384 assert(mach->LoopStackTop == 0);
6385 assert(mach->ContStackTop == 0);
6386 assert(mach->SwitchStackTop == 0);
6387 assert(mach->BreakStackTop == 0);
6388 assert(mach->CallStackTop == 0);
6389
6390 return ~mach->Temps[TEMP_KILMASK_I].xyzw[TEMP_KILMASK_C].u[0];
6391 }