r600g: get rid of trailing whitespace
[mesa.git] / src / gallium / drivers / r600 / r600_shader.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include "r600_sq.h"
24 #include "r600_formats.h"
25 #include "r600_opcodes.h"
26 #include "r600_shader.h"
27 #include "r600d.h"
28
29 #include "sb/sb_public.h"
30
31 #include "pipe/p_shader_tokens.h"
32 #include "tgsi/tgsi_info.h"
33 #include "tgsi/tgsi_parse.h"
34 #include "tgsi/tgsi_scan.h"
35 #include "tgsi/tgsi_dump.h"
36 #include "util/u_bitcast.h"
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
39 #include <stdio.h>
40 #include <errno.h>
41
42 /* CAYMAN notes
43 Why CAYMAN got loops for lots of instructions is explained here.
44
45 -These 8xx t-slot only ops are implemented in all vector slots.
46 MUL_LIT, FLT_TO_UINT, INT_TO_FLT, UINT_TO_FLT
47 These 8xx t-slot only opcodes become vector ops, with all four
48 slots expecting the arguments on sources a and b. Result is
49 broadcast to all channels.
50 MULLO_INT, MULHI_INT, MULLO_UINT, MULHI_UINT, MUL_64
51 These 8xx t-slot only opcodes become vector ops in the z, y, and
52 x slots.
53 EXP_IEEE, LOG_IEEE/CLAMPED, RECIP_IEEE/CLAMPED/FF/INT/UINT/_64/CLAMPED_64
54 RECIPSQRT_IEEE/CLAMPED/FF/_64/CLAMPED_64
55 SQRT_IEEE/_64
56 SIN/COS
57 The w slot may have an independent co-issued operation, or if the
58 result is required to be in the w slot, the opcode above may be
59 issued in the w slot as well.
60 The compiler must issue the source argument to slots z, y, and x
61 */
62
63 /* Contents of r0 on entry to various shaders
64
65 VS - .x = VertexID
66 .y = RelVertexID (??)
67 .w = InstanceID
68
69 GS - r0.xyw, r1.xyz = per-vertex offsets
70 r0.z = PrimitiveID
71
72 TCS - .x = PatchID
73 .y = RelPatchID (??)
74 .z = InvocationID
75 .w = tess factor base.
76
77 TES - .x = TessCoord.x
78 - .y = TessCoord.y
79 - .z = RelPatchID (??)
80 - .w = PrimitiveID
81
82 PS - face_gpr.z = SampleMask
83 face_gpr.w = SampleID
84 */
85 #define R600_SHADER_BUFFER_INFO_SEL (512 + R600_BUFFER_INFO_OFFSET / 16)
86 static int r600_shader_from_tgsi(struct r600_context *rctx,
87 struct r600_pipe_shader *pipeshader,
88 union r600_shader_key key);
89
90 static void r600_add_gpr_array(struct r600_shader *ps, int start_gpr,
91 int size, unsigned comp_mask) {
92
93 if (!size)
94 return;
95
96 if (ps->num_arrays == ps->max_arrays) {
97 ps->max_arrays += 64;
98 ps->arrays = realloc(ps->arrays, ps->max_arrays *
99 sizeof(struct r600_shader_array));
100 }
101
102 int n = ps->num_arrays;
103 ++ps->num_arrays;
104
105 ps->arrays[n].comp_mask = comp_mask;
106 ps->arrays[n].gpr_start = start_gpr;
107 ps->arrays[n].gpr_count = size;
108 }
109
110 static void r600_dump_streamout(struct pipe_stream_output_info *so)
111 {
112 unsigned i;
113
114 fprintf(stderr, "STREAMOUT\n");
115 for (i = 0; i < so->num_outputs; i++) {
116 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
117 so->output[i].start_component;
118 fprintf(stderr, " %i: MEM_STREAM%d_BUF%i[%i..%i] <- OUT[%i].%s%s%s%s%s\n",
119 i,
120 so->output[i].stream,
121 so->output[i].output_buffer,
122 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
123 so->output[i].register_index,
124 mask & 1 ? "x" : "",
125 mask & 2 ? "y" : "",
126 mask & 4 ? "z" : "",
127 mask & 8 ? "w" : "",
128 so->output[i].dst_offset < so->output[i].start_component ? " (will lower)" : "");
129 }
130 }
131
132 static int store_shader(struct pipe_context *ctx,
133 struct r600_pipe_shader *shader)
134 {
135 struct r600_context *rctx = (struct r600_context *)ctx;
136 uint32_t *ptr, i;
137
138 if (shader->bo == NULL) {
139 shader->bo = (struct r600_resource*)
140 pipe_buffer_create(ctx->screen, 0, PIPE_USAGE_IMMUTABLE, shader->shader.bc.ndw * 4);
141 if (shader->bo == NULL) {
142 return -ENOMEM;
143 }
144 ptr = r600_buffer_map_sync_with_rings(&rctx->b, shader->bo, PIPE_TRANSFER_WRITE);
145 if (R600_BIG_ENDIAN) {
146 for (i = 0; i < shader->shader.bc.ndw; ++i) {
147 ptr[i] = util_cpu_to_le32(shader->shader.bc.bytecode[i]);
148 }
149 } else {
150 memcpy(ptr, shader->shader.bc.bytecode, shader->shader.bc.ndw * sizeof(*ptr));
151 }
152 rctx->b.ws->buffer_unmap(shader->bo->buf);
153 }
154
155 return 0;
156 }
157
158 int r600_pipe_shader_create(struct pipe_context *ctx,
159 struct r600_pipe_shader *shader,
160 union r600_shader_key key)
161 {
162 struct r600_context *rctx = (struct r600_context *)ctx;
163 struct r600_pipe_shader_selector *sel = shader->selector;
164 int r;
165 bool dump = r600_can_dump_shader(&rctx->screen->b,
166 tgsi_get_processor_type(sel->tokens));
167 unsigned use_sb = !(rctx->screen->b.debug_flags & DBG_NO_SB);
168 unsigned sb_disasm = use_sb || (rctx->screen->b.debug_flags & DBG_SB_DISASM);
169 unsigned export_shader;
170
171 shader->shader.bc.isa = rctx->isa;
172
173 if (dump) {
174 fprintf(stderr, "--------------------------------------------------------------\n");
175 tgsi_dump(sel->tokens, 0);
176
177 if (sel->so.num_outputs) {
178 r600_dump_streamout(&sel->so);
179 }
180 }
181 r = r600_shader_from_tgsi(rctx, shader, key);
182 if (r) {
183 R600_ERR("translation from TGSI failed !\n");
184 goto error;
185 }
186 if (shader->shader.processor_type == PIPE_SHADER_VERTEX) {
187 /* only disable for vertex shaders in tess paths */
188 if (key.vs.as_ls)
189 use_sb = 0;
190 }
191 use_sb &= (shader->shader.processor_type != PIPE_SHADER_TESS_CTRL);
192 use_sb &= (shader->shader.processor_type != PIPE_SHADER_TESS_EVAL);
193
194 /* disable SB for shaders using doubles */
195 use_sb &= !shader->shader.uses_doubles;
196
197 /* Check if the bytecode has already been built. */
198 if (!shader->shader.bc.bytecode) {
199 r = r600_bytecode_build(&shader->shader.bc);
200 if (r) {
201 R600_ERR("building bytecode failed !\n");
202 goto error;
203 }
204 }
205
206 if (dump && !sb_disasm) {
207 fprintf(stderr, "--------------------------------------------------------------\n");
208 r600_bytecode_disasm(&shader->shader.bc);
209 fprintf(stderr, "______________________________________________________________\n");
210 } else if ((dump && sb_disasm) || use_sb) {
211 r = r600_sb_bytecode_process(rctx, &shader->shader.bc, &shader->shader,
212 dump, use_sb);
213 if (r) {
214 R600_ERR("r600_sb_bytecode_process failed !\n");
215 goto error;
216 }
217 }
218
219 if (shader->gs_copy_shader) {
220 if (dump) {
221 // dump copy shader
222 r = r600_sb_bytecode_process(rctx, &shader->gs_copy_shader->shader.bc,
223 &shader->gs_copy_shader->shader, dump, 0);
224 if (r)
225 goto error;
226 }
227
228 if ((r = store_shader(ctx, shader->gs_copy_shader)))
229 goto error;
230 }
231
232 /* Store the shader in a buffer. */
233 if ((r = store_shader(ctx, shader)))
234 goto error;
235
236 /* Build state. */
237 switch (shader->shader.processor_type) {
238 case PIPE_SHADER_TESS_CTRL:
239 evergreen_update_hs_state(ctx, shader);
240 break;
241 case PIPE_SHADER_TESS_EVAL:
242 if (key.tes.as_es)
243 evergreen_update_es_state(ctx, shader);
244 else
245 evergreen_update_vs_state(ctx, shader);
246 break;
247 case PIPE_SHADER_GEOMETRY:
248 if (rctx->b.chip_class >= EVERGREEN) {
249 evergreen_update_gs_state(ctx, shader);
250 evergreen_update_vs_state(ctx, shader->gs_copy_shader);
251 } else {
252 r600_update_gs_state(ctx, shader);
253 r600_update_vs_state(ctx, shader->gs_copy_shader);
254 }
255 break;
256 case PIPE_SHADER_VERTEX:
257 export_shader = key.vs.as_es;
258 if (rctx->b.chip_class >= EVERGREEN) {
259 if (key.vs.as_ls)
260 evergreen_update_ls_state(ctx, shader);
261 else if (key.vs.as_es)
262 evergreen_update_es_state(ctx, shader);
263 else
264 evergreen_update_vs_state(ctx, shader);
265 } else {
266 if (export_shader)
267 r600_update_es_state(ctx, shader);
268 else
269 r600_update_vs_state(ctx, shader);
270 }
271 break;
272 case PIPE_SHADER_FRAGMENT:
273 if (rctx->b.chip_class >= EVERGREEN) {
274 evergreen_update_ps_state(ctx, shader);
275 } else {
276 r600_update_ps_state(ctx, shader);
277 }
278 break;
279 default:
280 r = -EINVAL;
281 goto error;
282 }
283 return 0;
284
285 error:
286 r600_pipe_shader_destroy(ctx, shader);
287 return r;
288 }
289
290 void r600_pipe_shader_destroy(struct pipe_context *ctx, struct r600_pipe_shader *shader)
291 {
292 r600_resource_reference(&shader->bo, NULL);
293 r600_bytecode_clear(&shader->shader.bc);
294 r600_release_command_buffer(&shader->command_buffer);
295 }
296
297 /*
298 * tgsi -> r600 shader
299 */
300 struct r600_shader_tgsi_instruction;
301
302 struct r600_shader_src {
303 unsigned sel;
304 unsigned swizzle[4];
305 unsigned neg;
306 unsigned abs;
307 unsigned rel;
308 unsigned kc_bank;
309 boolean kc_rel; /* true if cache bank is indexed */
310 uint32_t value[4];
311 };
312
313 struct eg_interp {
314 boolean enabled;
315 unsigned ij_index;
316 };
317
318 struct r600_shader_ctx {
319 struct tgsi_shader_info info;
320 struct tgsi_parse_context parse;
321 const struct tgsi_token *tokens;
322 unsigned type;
323 unsigned file_offset[TGSI_FILE_COUNT];
324 unsigned temp_reg;
325 const struct r600_shader_tgsi_instruction *inst_info;
326 struct r600_bytecode *bc;
327 struct r600_shader *shader;
328 struct r600_shader_src src[4];
329 uint32_t *literals;
330 uint32_t nliterals;
331 uint32_t max_driver_temp_used;
332 /* needed for evergreen interpolation */
333 struct eg_interp eg_interpolators[6]; // indexed by Persp/Linear * 3 + sample/center/centroid
334 /* evergreen/cayman also store sample mask in face register */
335 int face_gpr;
336 /* sample id is .w component stored in fixed point position register */
337 int fixed_pt_position_gpr;
338 int colors_used;
339 boolean clip_vertex_write;
340 unsigned cv_output;
341 unsigned edgeflag_output;
342 int fragcoord_input;
343 int native_integers;
344 int next_ring_offset;
345 int gs_out_ring_offset;
346 int gs_next_vertex;
347 struct r600_shader *gs_for_vs;
348 int gs_export_gpr_tregs[4];
349 const struct pipe_stream_output_info *gs_stream_output_info;
350 unsigned enabled_stream_buffers_mask;
351 unsigned tess_input_info; /* temp with tess input offsets */
352 unsigned tess_output_info; /* temp with tess input offsets */
353 };
354
355 struct r600_shader_tgsi_instruction {
356 unsigned op;
357 int (*process)(struct r600_shader_ctx *ctx);
358 };
359
360 static int emit_gs_ring_writes(struct r600_shader_ctx *ctx, const struct pipe_stream_output_info *so, int stream, bool ind);
361 static const struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[], eg_shader_tgsi_instruction[], cm_shader_tgsi_instruction[];
362 static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx);
363 static inline void callstack_push(struct r600_shader_ctx *ctx, unsigned reason);
364 static void fc_pushlevel(struct r600_shader_ctx *ctx, int type);
365 static int tgsi_else(struct r600_shader_ctx *ctx);
366 static int tgsi_endif(struct r600_shader_ctx *ctx);
367 static int tgsi_bgnloop(struct r600_shader_ctx *ctx);
368 static int tgsi_endloop(struct r600_shader_ctx *ctx);
369 static int tgsi_loop_brk_cont(struct r600_shader_ctx *ctx);
370 static int tgsi_fetch_rel_const(struct r600_shader_ctx *ctx,
371 unsigned int cb_idx, unsigned cb_rel, unsigned int offset, unsigned ar_chan,
372 unsigned int dst_reg);
373 static void r600_bytecode_src(struct r600_bytecode_alu_src *bc_src,
374 const struct r600_shader_src *shader_src,
375 unsigned chan);
376 static int do_lds_fetch_values(struct r600_shader_ctx *ctx, unsigned temp_reg,
377 unsigned dst_reg);
378
379 static int tgsi_last_instruction(unsigned writemask)
380 {
381 int i, lasti = 0;
382
383 for (i = 0; i < 4; i++) {
384 if (writemask & (1 << i)) {
385 lasti = i;
386 }
387 }
388 return lasti;
389 }
390
391 static int tgsi_is_supported(struct r600_shader_ctx *ctx)
392 {
393 struct tgsi_full_instruction *i = &ctx->parse.FullToken.FullInstruction;
394 unsigned j;
395
396 if (i->Instruction.NumDstRegs > 1 && i->Instruction.Opcode != TGSI_OPCODE_DFRACEXP) {
397 R600_ERR("too many dst (%d)\n", i->Instruction.NumDstRegs);
398 return -EINVAL;
399 }
400 #if 0
401 if (i->Instruction.Label) {
402 R600_ERR("label unsupported\n");
403 return -EINVAL;
404 }
405 #endif
406 for (j = 0; j < i->Instruction.NumSrcRegs; j++) {
407 if (i->Src[j].Register.Dimension) {
408 switch (i->Src[j].Register.File) {
409 case TGSI_FILE_CONSTANT:
410 break;
411 case TGSI_FILE_INPUT:
412 if (ctx->type == PIPE_SHADER_GEOMETRY ||
413 ctx->type == PIPE_SHADER_TESS_CTRL ||
414 ctx->type == PIPE_SHADER_TESS_EVAL)
415 break;
416 case TGSI_FILE_OUTPUT:
417 if (ctx->type == PIPE_SHADER_TESS_CTRL)
418 break;
419 default:
420 R600_ERR("unsupported src %d (file %d, dimension %d)\n", j,
421 i->Src[j].Register.File,
422 i->Src[j].Register.Dimension);
423 return -EINVAL;
424 }
425 }
426 }
427 for (j = 0; j < i->Instruction.NumDstRegs; j++) {
428 if (i->Dst[j].Register.Dimension) {
429 if (ctx->type == PIPE_SHADER_TESS_CTRL)
430 continue;
431 R600_ERR("unsupported dst (dimension)\n");
432 return -EINVAL;
433 }
434 }
435 return 0;
436 }
437
438 int eg_get_interpolator_index(unsigned interpolate, unsigned location)
439 {
440 if (interpolate == TGSI_INTERPOLATE_COLOR ||
441 interpolate == TGSI_INTERPOLATE_LINEAR ||
442 interpolate == TGSI_INTERPOLATE_PERSPECTIVE)
443 {
444 int is_linear = interpolate == TGSI_INTERPOLATE_LINEAR;
445 int loc;
446
447 switch(location) {
448 case TGSI_INTERPOLATE_LOC_CENTER:
449 loc = 1;
450 break;
451 case TGSI_INTERPOLATE_LOC_CENTROID:
452 loc = 2;
453 break;
454 case TGSI_INTERPOLATE_LOC_SAMPLE:
455 default:
456 loc = 0; break;
457 }
458
459 return is_linear * 3 + loc;
460 }
461
462 return -1;
463 }
464
465 static void evergreen_interp_assign_ij_index(struct r600_shader_ctx *ctx,
466 int input)
467 {
468 int i = eg_get_interpolator_index(
469 ctx->shader->input[input].interpolate,
470 ctx->shader->input[input].interpolate_location);
471 assert(i >= 0);
472 ctx->shader->input[input].ij_index = ctx->eg_interpolators[i].ij_index;
473 }
474
475 static int evergreen_interp_alu(struct r600_shader_ctx *ctx, int input)
476 {
477 int i, r;
478 struct r600_bytecode_alu alu;
479 int gpr = 0, base_chan = 0;
480 int ij_index = ctx->shader->input[input].ij_index;
481
482 /* work out gpr and base_chan from index */
483 gpr = ij_index / 2;
484 base_chan = (2 * (ij_index % 2)) + 1;
485
486 for (i = 0; i < 8; i++) {
487 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
488
489 if (i < 4)
490 alu.op = ALU_OP2_INTERP_ZW;
491 else
492 alu.op = ALU_OP2_INTERP_XY;
493
494 if ((i > 1) && (i < 6)) {
495 alu.dst.sel = ctx->shader->input[input].gpr;
496 alu.dst.write = 1;
497 }
498
499 alu.dst.chan = i % 4;
500
501 alu.src[0].sel = gpr;
502 alu.src[0].chan = (base_chan - (i % 2));
503
504 alu.src[1].sel = V_SQ_ALU_SRC_PARAM_BASE + ctx->shader->input[input].lds_pos;
505
506 alu.bank_swizzle_force = SQ_ALU_VEC_210;
507 if ((i % 4) == 3)
508 alu.last = 1;
509 r = r600_bytecode_add_alu(ctx->bc, &alu);
510 if (r)
511 return r;
512 }
513 return 0;
514 }
515
516 static int evergreen_interp_flat(struct r600_shader_ctx *ctx, int input)
517 {
518 int i, r;
519 struct r600_bytecode_alu alu;
520
521 for (i = 0; i < 4; i++) {
522 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
523
524 alu.op = ALU_OP1_INTERP_LOAD_P0;
525
526 alu.dst.sel = ctx->shader->input[input].gpr;
527 alu.dst.write = 1;
528
529 alu.dst.chan = i;
530
531 alu.src[0].sel = V_SQ_ALU_SRC_PARAM_BASE + ctx->shader->input[input].lds_pos;
532 alu.src[0].chan = i;
533
534 if (i == 3)
535 alu.last = 1;
536 r = r600_bytecode_add_alu(ctx->bc, &alu);
537 if (r)
538 return r;
539 }
540 return 0;
541 }
542
543 /*
544 * Special export handling in shaders
545 *
546 * shader export ARRAY_BASE for EXPORT_POS:
547 * 60 is position
548 * 61 is misc vector
549 * 62, 63 are clip distance vectors
550 *
551 * The use of the values exported in 61-63 are controlled by PA_CL_VS_OUT_CNTL:
552 * VS_OUT_MISC_VEC_ENA - enables the use of all fields in export 61
553 * USE_VTX_POINT_SIZE - point size in the X channel of export 61
554 * USE_VTX_EDGE_FLAG - edge flag in the Y channel of export 61
555 * USE_VTX_RENDER_TARGET_INDX - render target index in the Z channel of export 61
556 * USE_VTX_VIEWPORT_INDX - viewport index in the W channel of export 61
557 * USE_VTX_KILL_FLAG - kill flag in the Z channel of export 61 (mutually
558 * exclusive from render target index)
559 * VS_OUT_CCDIST0_VEC_ENA/VS_OUT_CCDIST1_VEC_ENA - enable clip distance vectors
560 *
561 *
562 * shader export ARRAY_BASE for EXPORT_PIXEL:
563 * 0-7 CB targets
564 * 61 computed Z vector
565 *
566 * The use of the values exported in the computed Z vector are controlled
567 * by DB_SHADER_CONTROL:
568 * Z_EXPORT_ENABLE - Z as a float in RED
569 * STENCIL_REF_EXPORT_ENABLE - stencil ref as int in GREEN
570 * COVERAGE_TO_MASK_ENABLE - alpha to mask in ALPHA
571 * MASK_EXPORT_ENABLE - pixel sample mask in BLUE
572 * DB_SOURCE_FORMAT - export control restrictions
573 *
574 */
575
576
577 /* Map name/sid pair from tgsi to the 8-bit semantic index for SPI setup */
578 static int r600_spi_sid(struct r600_shader_io * io)
579 {
580 int index, name = io->name;
581
582 /* These params are handled differently, they don't need
583 * semantic indices, so we'll use 0 for them.
584 */
585 if (name == TGSI_SEMANTIC_POSITION ||
586 name == TGSI_SEMANTIC_PSIZE ||
587 name == TGSI_SEMANTIC_EDGEFLAG ||
588 name == TGSI_SEMANTIC_FACE ||
589 name == TGSI_SEMANTIC_SAMPLEMASK)
590 index = 0;
591 else {
592 if (name == TGSI_SEMANTIC_GENERIC) {
593 /* For generic params simply use sid from tgsi */
594 index = io->sid;
595 } else {
596 /* For non-generic params - pack name and sid into 8 bits */
597 index = 0x80 | (name<<3) | (io->sid);
598 }
599
600 /* Make sure that all really used indices have nonzero value, so
601 * we can just compare it to 0 later instead of comparing the name
602 * with different values to detect special cases. */
603 index++;
604 }
605
606 return index;
607 };
608
609 /* we need this to get a common lds index for vs/tcs/tes input/outputs */
610 int r600_get_lds_unique_index(unsigned semantic_name, unsigned index)
611 {
612 switch (semantic_name) {
613 case TGSI_SEMANTIC_POSITION:
614 return 0;
615 case TGSI_SEMANTIC_PSIZE:
616 return 1;
617 case TGSI_SEMANTIC_CLIPDIST:
618 assert(index <= 1);
619 return 2 + index;
620 case TGSI_SEMANTIC_GENERIC:
621 if (index <= 63-4)
622 return 4 + index - 9;
623 else
624 /* same explanation as in the default statement,
625 * the only user hitting this is st/nine.
626 */
627 return 0;
628
629 /* patch indices are completely separate and thus start from 0 */
630 case TGSI_SEMANTIC_TESSOUTER:
631 return 0;
632 case TGSI_SEMANTIC_TESSINNER:
633 return 1;
634 case TGSI_SEMANTIC_PATCH:
635 return 2 + index;
636
637 default:
638 /* Don't fail here. The result of this function is only used
639 * for LS, TCS, TES, and GS, where legacy GL semantics can't
640 * occur, but this function is called for all vertex shaders
641 * before it's known whether LS will be compiled or not.
642 */
643 return 0;
644 }
645 }
646
647 /* turn input into interpolate on EG */
648 static int evergreen_interp_input(struct r600_shader_ctx *ctx, int index)
649 {
650 int r = 0;
651
652 if (ctx->shader->input[index].spi_sid) {
653 ctx->shader->input[index].lds_pos = ctx->shader->nlds++;
654 if (ctx->shader->input[index].interpolate > 0) {
655 evergreen_interp_assign_ij_index(ctx, index);
656 r = evergreen_interp_alu(ctx, index);
657 } else {
658 r = evergreen_interp_flat(ctx, index);
659 }
660 }
661 return r;
662 }
663
664 static int select_twoside_color(struct r600_shader_ctx *ctx, int front, int back)
665 {
666 struct r600_bytecode_alu alu;
667 int i, r;
668 int gpr_front = ctx->shader->input[front].gpr;
669 int gpr_back = ctx->shader->input[back].gpr;
670
671 for (i = 0; i < 4; i++) {
672 memset(&alu, 0, sizeof(alu));
673 alu.op = ALU_OP3_CNDGT;
674 alu.is_op3 = 1;
675 alu.dst.write = 1;
676 alu.dst.sel = gpr_front;
677 alu.src[0].sel = ctx->face_gpr;
678 alu.src[1].sel = gpr_front;
679 alu.src[2].sel = gpr_back;
680
681 alu.dst.chan = i;
682 alu.src[1].chan = i;
683 alu.src[2].chan = i;
684 alu.last = (i==3);
685
686 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
687 return r;
688 }
689
690 return 0;
691 }
692
693 /* execute a single slot ALU calculation */
694 static int single_alu_op2(struct r600_shader_ctx *ctx, int op,
695 int dst_sel, int dst_chan,
696 int src0_sel, unsigned src0_chan_val,
697 int src1_sel, unsigned src1_chan_val)
698 {
699 struct r600_bytecode_alu alu;
700 int r, i;
701
702 if (ctx->bc->chip_class == CAYMAN && op == ALU_OP2_MULLO_INT) {
703 for (i = 0; i < 4; i++) {
704 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
705 alu.op = op;
706 alu.src[0].sel = src0_sel;
707 if (src0_sel == V_SQ_ALU_SRC_LITERAL)
708 alu.src[0].value = src0_chan_val;
709 else
710 alu.src[0].chan = src0_chan_val;
711 alu.src[1].sel = src1_sel;
712 if (src1_sel == V_SQ_ALU_SRC_LITERAL)
713 alu.src[1].value = src1_chan_val;
714 else
715 alu.src[1].chan = src1_chan_val;
716 alu.dst.sel = dst_sel;
717 alu.dst.chan = i;
718 alu.dst.write = i == dst_chan;
719 alu.last = (i == 3);
720 r = r600_bytecode_add_alu(ctx->bc, &alu);
721 if (r)
722 return r;
723 }
724 return 0;
725 }
726
727 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
728 alu.op = op;
729 alu.src[0].sel = src0_sel;
730 if (src0_sel == V_SQ_ALU_SRC_LITERAL)
731 alu.src[0].value = src0_chan_val;
732 else
733 alu.src[0].chan = src0_chan_val;
734 alu.src[1].sel = src1_sel;
735 if (src1_sel == V_SQ_ALU_SRC_LITERAL)
736 alu.src[1].value = src1_chan_val;
737 else
738 alu.src[1].chan = src1_chan_val;
739 alu.dst.sel = dst_sel;
740 alu.dst.chan = dst_chan;
741 alu.dst.write = 1;
742 alu.last = 1;
743 r = r600_bytecode_add_alu(ctx->bc, &alu);
744 if (r)
745 return r;
746 return 0;
747 }
748
749 /* execute a single slot ALU calculation */
750 static int single_alu_op3(struct r600_shader_ctx *ctx, int op,
751 int dst_sel, int dst_chan,
752 int src0_sel, unsigned src0_chan_val,
753 int src1_sel, unsigned src1_chan_val,
754 int src2_sel, unsigned src2_chan_val)
755 {
756 struct r600_bytecode_alu alu;
757 int r;
758
759 /* validate this for other ops */
760 assert(op == ALU_OP3_MULADD_UINT24);
761 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
762 alu.op = op;
763 alu.src[0].sel = src0_sel;
764 if (src0_sel == V_SQ_ALU_SRC_LITERAL)
765 alu.src[0].value = src0_chan_val;
766 else
767 alu.src[0].chan = src0_chan_val;
768 alu.src[1].sel = src1_sel;
769 if (src1_sel == V_SQ_ALU_SRC_LITERAL)
770 alu.src[1].value = src1_chan_val;
771 else
772 alu.src[1].chan = src1_chan_val;
773 alu.src[2].sel = src2_sel;
774 if (src2_sel == V_SQ_ALU_SRC_LITERAL)
775 alu.src[2].value = src2_chan_val;
776 else
777 alu.src[2].chan = src2_chan_val;
778 alu.dst.sel = dst_sel;
779 alu.dst.chan = dst_chan;
780 alu.is_op3 = 1;
781 alu.last = 1;
782 r = r600_bytecode_add_alu(ctx->bc, &alu);
783 if (r)
784 return r;
785 return 0;
786 }
787
788 /* put it in temp_reg.x */
789 static int get_lds_offset0(struct r600_shader_ctx *ctx,
790 int rel_patch_chan,
791 int temp_reg, bool is_patch_var)
792 {
793 int r;
794
795 /* MUL temp.x, patch_stride (input_vals.x), rel_patch_id (r0.y (tcs)) */
796 /* ADD
797 Dimension - patch0_offset (input_vals.z),
798 Non-dim - patch0_data_offset (input_vals.w)
799 */
800 r = single_alu_op3(ctx, ALU_OP3_MULADD_UINT24,
801 temp_reg, 0,
802 ctx->tess_output_info, 0,
803 0, rel_patch_chan,
804 ctx->tess_output_info, is_patch_var ? 3 : 2);
805 if (r)
806 return r;
807 return 0;
808 }
809
810 static inline int get_address_file_reg(struct r600_shader_ctx *ctx, int index)
811 {
812 return index > 0 ? ctx->bc->index_reg[index - 1] : ctx->bc->ar_reg;
813 }
814
815 static int r600_get_temp(struct r600_shader_ctx *ctx)
816 {
817 return ctx->temp_reg + ctx->max_driver_temp_used++;
818 }
819
820 static int vs_add_primid_output(struct r600_shader_ctx *ctx, int prim_id_sid)
821 {
822 int i;
823 i = ctx->shader->noutput++;
824 ctx->shader->output[i].name = TGSI_SEMANTIC_PRIMID;
825 ctx->shader->output[i].sid = 0;
826 ctx->shader->output[i].gpr = 0;
827 ctx->shader->output[i].interpolate = TGSI_INTERPOLATE_CONSTANT;
828 ctx->shader->output[i].write_mask = 0x4;
829 ctx->shader->output[i].spi_sid = prim_id_sid;
830
831 return 0;
832 }
833
834 static int tgsi_barrier(struct r600_shader_ctx *ctx)
835 {
836 struct r600_bytecode_alu alu;
837 int r;
838
839 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
840 alu.op = ctx->inst_info->op;
841 alu.last = 1;
842
843 r = r600_bytecode_add_alu(ctx->bc, &alu);
844 if (r)
845 return r;
846 return 0;
847 }
848
849 static int tgsi_declaration(struct r600_shader_ctx *ctx)
850 {
851 struct tgsi_full_declaration *d = &ctx->parse.FullToken.FullDeclaration;
852 int r, i, j, count = d->Range.Last - d->Range.First + 1;
853
854 switch (d->Declaration.File) {
855 case TGSI_FILE_INPUT:
856 for (j = 0; j < count; j++) {
857 i = ctx->shader->ninput + j;
858 assert(i < ARRAY_SIZE(ctx->shader->input));
859 ctx->shader->input[i].name = d->Semantic.Name;
860 ctx->shader->input[i].sid = d->Semantic.Index + j;
861 ctx->shader->input[i].interpolate = d->Interp.Interpolate;
862 ctx->shader->input[i].interpolate_location = d->Interp.Location;
863 ctx->shader->input[i].gpr = ctx->file_offset[TGSI_FILE_INPUT] + d->Range.First + j;
864 if (ctx->type == PIPE_SHADER_FRAGMENT) {
865 ctx->shader->input[i].spi_sid = r600_spi_sid(&ctx->shader->input[i]);
866 switch (ctx->shader->input[i].name) {
867 case TGSI_SEMANTIC_FACE:
868 if (ctx->face_gpr != -1)
869 ctx->shader->input[i].gpr = ctx->face_gpr; /* already allocated by allocate_system_value_inputs */
870 else
871 ctx->face_gpr = ctx->shader->input[i].gpr;
872 break;
873 case TGSI_SEMANTIC_COLOR:
874 ctx->colors_used++;
875 break;
876 case TGSI_SEMANTIC_POSITION:
877 ctx->fragcoord_input = i;
878 break;
879 case TGSI_SEMANTIC_PRIMID:
880 /* set this for now */
881 ctx->shader->gs_prim_id_input = true;
882 ctx->shader->ps_prim_id_input = i;
883 break;
884 }
885 if (ctx->bc->chip_class >= EVERGREEN) {
886 if ((r = evergreen_interp_input(ctx, i)))
887 return r;
888 }
889 } else if (ctx->type == PIPE_SHADER_GEOMETRY) {
890 /* FIXME probably skip inputs if they aren't passed in the ring */
891 ctx->shader->input[i].ring_offset = ctx->next_ring_offset;
892 ctx->next_ring_offset += 16;
893 if (ctx->shader->input[i].name == TGSI_SEMANTIC_PRIMID)
894 ctx->shader->gs_prim_id_input = true;
895 }
896 }
897 ctx->shader->ninput += count;
898 break;
899 case TGSI_FILE_OUTPUT:
900 for (j = 0; j < count; j++) {
901 i = ctx->shader->noutput + j;
902 assert(i < ARRAY_SIZE(ctx->shader->output));
903 ctx->shader->output[i].name = d->Semantic.Name;
904 ctx->shader->output[i].sid = d->Semantic.Index + j;
905 ctx->shader->output[i].gpr = ctx->file_offset[TGSI_FILE_OUTPUT] + d->Range.First + j;
906 ctx->shader->output[i].interpolate = d->Interp.Interpolate;
907 ctx->shader->output[i].write_mask = d->Declaration.UsageMask;
908 if (ctx->type == PIPE_SHADER_VERTEX ||
909 ctx->type == PIPE_SHADER_GEOMETRY ||
910 ctx->type == PIPE_SHADER_TESS_EVAL) {
911 ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);
912 switch (d->Semantic.Name) {
913 case TGSI_SEMANTIC_CLIPDIST:
914 ctx->shader->clip_dist_write |= d->Declaration.UsageMask <<
915 ((d->Semantic.Index + j) << 2);
916 break;
917 case TGSI_SEMANTIC_PSIZE:
918 ctx->shader->vs_out_misc_write = 1;
919 ctx->shader->vs_out_point_size = 1;
920 break;
921 case TGSI_SEMANTIC_EDGEFLAG:
922 ctx->shader->vs_out_misc_write = 1;
923 ctx->shader->vs_out_edgeflag = 1;
924 ctx->edgeflag_output = i;
925 break;
926 case TGSI_SEMANTIC_VIEWPORT_INDEX:
927 ctx->shader->vs_out_misc_write = 1;
928 ctx->shader->vs_out_viewport = 1;
929 break;
930 case TGSI_SEMANTIC_LAYER:
931 ctx->shader->vs_out_misc_write = 1;
932 ctx->shader->vs_out_layer = 1;
933 break;
934 case TGSI_SEMANTIC_CLIPVERTEX:
935 ctx->clip_vertex_write = TRUE;
936 ctx->cv_output = i;
937 break;
938 }
939 if (ctx->type == PIPE_SHADER_GEOMETRY) {
940 ctx->gs_out_ring_offset += 16;
941 }
942 } else if (ctx->type == PIPE_SHADER_FRAGMENT) {
943 switch (d->Semantic.Name) {
944 case TGSI_SEMANTIC_COLOR:
945 ctx->shader->nr_ps_max_color_exports++;
946 break;
947 }
948 }
949 }
950 ctx->shader->noutput += count;
951 break;
952 case TGSI_FILE_TEMPORARY:
953 if (ctx->info.indirect_files & (1 << TGSI_FILE_TEMPORARY)) {
954 if (d->Array.ArrayID) {
955 r600_add_gpr_array(ctx->shader,
956 ctx->file_offset[TGSI_FILE_TEMPORARY] +
957 d->Range.First,
958 d->Range.Last - d->Range.First + 1, 0x0F);
959 }
960 }
961 break;
962
963 case TGSI_FILE_CONSTANT:
964 case TGSI_FILE_SAMPLER:
965 case TGSI_FILE_SAMPLER_VIEW:
966 case TGSI_FILE_ADDRESS:
967 break;
968
969 case TGSI_FILE_SYSTEM_VALUE:
970 if (d->Semantic.Name == TGSI_SEMANTIC_SAMPLEMASK ||
971 d->Semantic.Name == TGSI_SEMANTIC_SAMPLEID ||
972 d->Semantic.Name == TGSI_SEMANTIC_SAMPLEPOS) {
973 break; /* Already handled from allocate_system_value_inputs */
974 } else if (d->Semantic.Name == TGSI_SEMANTIC_INSTANCEID) {
975 if (!ctx->native_integers) {
976 struct r600_bytecode_alu alu;
977 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
978
979 alu.op = ALU_OP1_INT_TO_FLT;
980 alu.src[0].sel = 0;
981 alu.src[0].chan = 3;
982
983 alu.dst.sel = 0;
984 alu.dst.chan = 3;
985 alu.dst.write = 1;
986 alu.last = 1;
987
988 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
989 return r;
990 }
991 break;
992 } else if (d->Semantic.Name == TGSI_SEMANTIC_VERTEXID)
993 break;
994 else if (d->Semantic.Name == TGSI_SEMANTIC_INVOCATIONID)
995 break;
996 else if (d->Semantic.Name == TGSI_SEMANTIC_TESSINNER ||
997 d->Semantic.Name == TGSI_SEMANTIC_TESSOUTER) {
998 int param = r600_get_lds_unique_index(d->Semantic.Name, 0);
999 int dreg = d->Semantic.Name == TGSI_SEMANTIC_TESSINNER ? 3 : 2;
1000 unsigned temp_reg = r600_get_temp(ctx);
1001
1002 r = get_lds_offset0(ctx, 2, temp_reg, true);
1003 if (r)
1004 return r;
1005
1006 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
1007 temp_reg, 0,
1008 temp_reg, 0,
1009 V_SQ_ALU_SRC_LITERAL, param * 16);
1010 if (r)
1011 return r;
1012
1013 do_lds_fetch_values(ctx, temp_reg, dreg);
1014 }
1015 else if (d->Semantic.Name == TGSI_SEMANTIC_TESSCOORD) {
1016 /* MOV r1.x, r0.x;
1017 MOV r1.y, r0.y;
1018 */
1019 for (i = 0; i < 2; i++) {
1020 struct r600_bytecode_alu alu;
1021 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1022 alu.op = ALU_OP1_MOV;
1023 alu.src[0].sel = 0;
1024 alu.src[0].chan = 0 + i;
1025 alu.dst.sel = 1;
1026 alu.dst.chan = 0 + i;
1027 alu.dst.write = 1;
1028 alu.last = (i == 1) ? 1 : 0;
1029 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
1030 return r;
1031 }
1032 /* ADD r1.z, 1.0f, -r0.x */
1033 struct r600_bytecode_alu alu;
1034 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1035 alu.op = ALU_OP2_ADD;
1036 alu.src[0].sel = V_SQ_ALU_SRC_1;
1037 alu.src[1].sel = 1;
1038 alu.src[1].chan = 0;
1039 alu.src[1].neg = 1;
1040 alu.dst.sel = 1;
1041 alu.dst.chan = 2;
1042 alu.dst.write = 1;
1043 alu.last = 1;
1044 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
1045 return r;
1046
1047 /* ADD r1.z, r1.z, -r1.y */
1048 alu.op = ALU_OP2_ADD;
1049 alu.src[0].sel = 1;
1050 alu.src[0].chan = 2;
1051 alu.src[1].sel = 1;
1052 alu.src[1].chan = 1;
1053 alu.src[1].neg = 1;
1054 alu.dst.sel = 1;
1055 alu.dst.chan = 2;
1056 alu.dst.write = 1;
1057 alu.last = 1;
1058 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
1059 return r;
1060 break;
1061 }
1062 break;
1063 default:
1064 R600_ERR("unsupported file %d declaration\n", d->Declaration.File);
1065 return -EINVAL;
1066 }
1067 return 0;
1068 }
1069
1070 static int allocate_system_value_inputs(struct r600_shader_ctx *ctx, int gpr_offset)
1071 {
1072 struct tgsi_parse_context parse;
1073 struct {
1074 boolean enabled;
1075 int *reg;
1076 unsigned name, alternate_name;
1077 } inputs[2] = {
1078 { false, &ctx->face_gpr, TGSI_SEMANTIC_SAMPLEMASK, ~0u }, /* lives in Front Face GPR.z */
1079
1080 { false, &ctx->fixed_pt_position_gpr, TGSI_SEMANTIC_SAMPLEID, TGSI_SEMANTIC_SAMPLEPOS } /* SAMPLEID is in Fixed Point Position GPR.w */
1081 };
1082 int i, k, num_regs = 0;
1083
1084 if (tgsi_parse_init(&parse, ctx->tokens) != TGSI_PARSE_OK) {
1085 return 0;
1086 }
1087
1088 /* need to scan shader for system values and interpolateAtSample/Offset/Centroid */
1089 while (!tgsi_parse_end_of_tokens(&parse)) {
1090 tgsi_parse_token(&parse);
1091
1092 if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION) {
1093 const struct tgsi_full_instruction *inst = &parse.FullToken.FullInstruction;
1094 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE ||
1095 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
1096 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID)
1097 {
1098 int interpolate, location, k;
1099
1100 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
1101 location = TGSI_INTERPOLATE_LOC_CENTER;
1102 inputs[1].enabled = true; /* needs SAMPLEID */
1103 } else if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET) {
1104 location = TGSI_INTERPOLATE_LOC_CENTER;
1105 /* Needs sample positions, currently those are always available */
1106 } else {
1107 location = TGSI_INTERPOLATE_LOC_CENTROID;
1108 }
1109
1110 interpolate = ctx->info.input_interpolate[inst->Src[0].Register.Index];
1111 k = eg_get_interpolator_index(interpolate, location);
1112 if (k >= 0)
1113 ctx->eg_interpolators[k].enabled = true;
1114 }
1115 } else if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1116 struct tgsi_full_declaration *d = &parse.FullToken.FullDeclaration;
1117 if (d->Declaration.File == TGSI_FILE_SYSTEM_VALUE) {
1118 for (k = 0; k < ARRAY_SIZE(inputs); k++) {
1119 if (d->Semantic.Name == inputs[k].name ||
1120 d->Semantic.Name == inputs[k].alternate_name) {
1121 inputs[k].enabled = true;
1122 }
1123 }
1124 }
1125 }
1126 }
1127
1128 tgsi_parse_free(&parse);
1129
1130 for (i = 0; i < ARRAY_SIZE(inputs); i++) {
1131 boolean enabled = inputs[i].enabled;
1132 int *reg = inputs[i].reg;
1133 unsigned name = inputs[i].name;
1134
1135 if (enabled) {
1136 int gpr = gpr_offset + num_regs++;
1137
1138 // add to inputs, allocate a gpr
1139 k = ctx->shader->ninput ++;
1140 ctx->shader->input[k].name = name;
1141 ctx->shader->input[k].sid = 0;
1142 ctx->shader->input[k].interpolate = TGSI_INTERPOLATE_CONSTANT;
1143 ctx->shader->input[k].interpolate_location = TGSI_INTERPOLATE_LOC_CENTER;
1144 *reg = ctx->shader->input[k].gpr = gpr;
1145 }
1146 }
1147
1148 return gpr_offset + num_regs;
1149 }
1150
1151 /*
1152 * for evergreen we need to scan the shader to find the number of GPRs we need to
1153 * reserve for interpolation and system values
1154 *
1155 * we need to know if we are going to emit
1156 * any sample or centroid inputs
1157 * if perspective and linear are required
1158 */
1159 static int evergreen_gpr_count(struct r600_shader_ctx *ctx)
1160 {
1161 unsigned i;
1162 int num_baryc;
1163 struct tgsi_parse_context parse;
1164
1165 memset(&ctx->eg_interpolators, 0, sizeof(ctx->eg_interpolators));
1166
1167 for (i = 0; i < ctx->info.num_inputs; i++) {
1168 int k;
1169 /* skip position/face/mask/sampleid */
1170 if (ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_POSITION ||
1171 ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_FACE ||
1172 ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_SAMPLEMASK ||
1173 ctx->info.input_semantic_name[i] == TGSI_SEMANTIC_SAMPLEID)
1174 continue;
1175
1176 k = eg_get_interpolator_index(
1177 ctx->info.input_interpolate[i],
1178 ctx->info.input_interpolate_loc[i]);
1179 if (k >= 0)
1180 ctx->eg_interpolators[k].enabled = TRUE;
1181 }
1182
1183 if (tgsi_parse_init(&parse, ctx->tokens) != TGSI_PARSE_OK) {
1184 return 0;
1185 }
1186
1187 /* need to scan shader for system values and interpolateAtSample/Offset/Centroid */
1188 while (!tgsi_parse_end_of_tokens(&parse)) {
1189 tgsi_parse_token(&parse);
1190
1191 if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_INSTRUCTION) {
1192 const struct tgsi_full_instruction *inst = &parse.FullToken.FullInstruction;
1193 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE ||
1194 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
1195 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID)
1196 {
1197 int interpolate, location, k;
1198
1199 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
1200 location = TGSI_INTERPOLATE_LOC_CENTER;
1201 } else if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET) {
1202 location = TGSI_INTERPOLATE_LOC_CENTER;
1203 } else {
1204 location = TGSI_INTERPOLATE_LOC_CENTROID;
1205 }
1206
1207 interpolate = ctx->info.input_interpolate[inst->Src[0].Register.Index];
1208 k = eg_get_interpolator_index(interpolate, location);
1209 if (k >= 0)
1210 ctx->eg_interpolators[k].enabled = true;
1211 }
1212 }
1213 }
1214
1215 tgsi_parse_free(&parse);
1216
1217 /* assign gpr to each interpolator according to priority */
1218 num_baryc = 0;
1219 for (i = 0; i < ARRAY_SIZE(ctx->eg_interpolators); i++) {
1220 if (ctx->eg_interpolators[i].enabled) {
1221 ctx->eg_interpolators[i].ij_index = num_baryc;
1222 num_baryc ++;
1223 }
1224 }
1225
1226 /* XXX PULL MODEL and LINE STIPPLE */
1227
1228 num_baryc = (num_baryc + 1) >> 1;
1229 return allocate_system_value_inputs(ctx, num_baryc);
1230 }
1231
1232 /* sample_id_sel == NULL means fetch for current sample */
1233 static int load_sample_position(struct r600_shader_ctx *ctx, struct r600_shader_src *sample_id, int chan_sel)
1234 {
1235 struct r600_bytecode_vtx vtx;
1236 int r, t1;
1237
1238 assert(ctx->fixed_pt_position_gpr != -1);
1239
1240 t1 = r600_get_temp(ctx);
1241
1242 memset(&vtx, 0, sizeof(struct r600_bytecode_vtx));
1243 vtx.op = FETCH_OP_VFETCH;
1244 vtx.buffer_id = R600_BUFFER_INFO_CONST_BUFFER;
1245 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
1246 if (sample_id == NULL) {
1247 vtx.src_gpr = ctx->fixed_pt_position_gpr; // SAMPLEID is in .w;
1248 vtx.src_sel_x = 3;
1249 }
1250 else {
1251 struct r600_bytecode_alu alu;
1252
1253 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1254 alu.op = ALU_OP1_MOV;
1255 r600_bytecode_src(&alu.src[0], sample_id, chan_sel);
1256 alu.dst.sel = t1;
1257 alu.dst.write = 1;
1258 alu.last = 1;
1259 r = r600_bytecode_add_alu(ctx->bc, &alu);
1260 if (r)
1261 return r;
1262
1263 vtx.src_gpr = t1;
1264 vtx.src_sel_x = 0;
1265 }
1266 vtx.mega_fetch_count = 16;
1267 vtx.dst_gpr = t1;
1268 vtx.dst_sel_x = 0;
1269 vtx.dst_sel_y = 1;
1270 vtx.dst_sel_z = 2;
1271 vtx.dst_sel_w = 3;
1272 vtx.data_format = FMT_32_32_32_32_FLOAT;
1273 vtx.num_format_all = 2;
1274 vtx.format_comp_all = 1;
1275 vtx.use_const_fields = 0;
1276 vtx.offset = 1; // first element is size of buffer
1277 vtx.endian = r600_endian_swap(32);
1278 vtx.srf_mode_all = 1; /* SRF_MODE_NO_ZERO */
1279
1280 r = r600_bytecode_add_vtx(ctx->bc, &vtx);
1281 if (r)
1282 return r;
1283
1284 return t1;
1285 }
1286
1287 static void tgsi_src(struct r600_shader_ctx *ctx,
1288 const struct tgsi_full_src_register *tgsi_src,
1289 struct r600_shader_src *r600_src)
1290 {
1291 memset(r600_src, 0, sizeof(*r600_src));
1292 r600_src->swizzle[0] = tgsi_src->Register.SwizzleX;
1293 r600_src->swizzle[1] = tgsi_src->Register.SwizzleY;
1294 r600_src->swizzle[2] = tgsi_src->Register.SwizzleZ;
1295 r600_src->swizzle[3] = tgsi_src->Register.SwizzleW;
1296 r600_src->neg = tgsi_src->Register.Negate;
1297 r600_src->abs = tgsi_src->Register.Absolute;
1298
1299 if (tgsi_src->Register.File == TGSI_FILE_IMMEDIATE) {
1300 int index;
1301 if ((tgsi_src->Register.SwizzleX == tgsi_src->Register.SwizzleY) &&
1302 (tgsi_src->Register.SwizzleX == tgsi_src->Register.SwizzleZ) &&
1303 (tgsi_src->Register.SwizzleX == tgsi_src->Register.SwizzleW)) {
1304
1305 index = tgsi_src->Register.Index * 4 + tgsi_src->Register.SwizzleX;
1306 r600_bytecode_special_constants(ctx->literals[index], &r600_src->sel, &r600_src->neg, r600_src->abs);
1307 if (r600_src->sel != V_SQ_ALU_SRC_LITERAL)
1308 return;
1309 }
1310 index = tgsi_src->Register.Index;
1311 r600_src->sel = V_SQ_ALU_SRC_LITERAL;
1312 memcpy(r600_src->value, ctx->literals + index * 4, sizeof(r600_src->value));
1313 } else if (tgsi_src->Register.File == TGSI_FILE_SYSTEM_VALUE) {
1314 if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_SAMPLEMASK) {
1315 r600_src->swizzle[0] = 2; // Z value
1316 r600_src->swizzle[1] = 2;
1317 r600_src->swizzle[2] = 2;
1318 r600_src->swizzle[3] = 2;
1319 r600_src->sel = ctx->face_gpr;
1320 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_SAMPLEID) {
1321 r600_src->swizzle[0] = 3; // W value
1322 r600_src->swizzle[1] = 3;
1323 r600_src->swizzle[2] = 3;
1324 r600_src->swizzle[3] = 3;
1325 r600_src->sel = ctx->fixed_pt_position_gpr;
1326 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_SAMPLEPOS) {
1327 r600_src->swizzle[0] = 0;
1328 r600_src->swizzle[1] = 1;
1329 r600_src->swizzle[2] = 4;
1330 r600_src->swizzle[3] = 4;
1331 r600_src->sel = load_sample_position(ctx, NULL, -1);
1332 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_INSTANCEID) {
1333 r600_src->swizzle[0] = 3;
1334 r600_src->swizzle[1] = 3;
1335 r600_src->swizzle[2] = 3;
1336 r600_src->swizzle[3] = 3;
1337 r600_src->sel = 0;
1338 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_VERTEXID) {
1339 r600_src->swizzle[0] = 0;
1340 r600_src->swizzle[1] = 0;
1341 r600_src->swizzle[2] = 0;
1342 r600_src->swizzle[3] = 0;
1343 r600_src->sel = 0;
1344 } else if (ctx->type != PIPE_SHADER_TESS_CTRL && ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_INVOCATIONID) {
1345 r600_src->swizzle[0] = 3;
1346 r600_src->swizzle[1] = 3;
1347 r600_src->swizzle[2] = 3;
1348 r600_src->swizzle[3] = 3;
1349 r600_src->sel = 1;
1350 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_INVOCATIONID) {
1351 r600_src->swizzle[0] = 2;
1352 r600_src->swizzle[1] = 2;
1353 r600_src->swizzle[2] = 2;
1354 r600_src->swizzle[3] = 2;
1355 r600_src->sel = 0;
1356 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_TESSCOORD) {
1357 r600_src->sel = 1;
1358 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_TESSINNER) {
1359 r600_src->sel = 3;
1360 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_TESSOUTER) {
1361 r600_src->sel = 2;
1362 } else if (ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_VERTICESIN) {
1363 if (ctx->type == PIPE_SHADER_TESS_CTRL) {
1364 r600_src->sel = ctx->tess_input_info;
1365 r600_src->swizzle[0] = 2;
1366 r600_src->swizzle[1] = 2;
1367 r600_src->swizzle[2] = 2;
1368 r600_src->swizzle[3] = 2;
1369 } else {
1370 r600_src->sel = ctx->tess_input_info;
1371 r600_src->swizzle[0] = 3;
1372 r600_src->swizzle[1] = 3;
1373 r600_src->swizzle[2] = 3;
1374 r600_src->swizzle[3] = 3;
1375 }
1376 } else if (ctx->type == PIPE_SHADER_TESS_CTRL && ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_PRIMID) {
1377 r600_src->sel = 0;
1378 r600_src->swizzle[0] = 0;
1379 r600_src->swizzle[1] = 0;
1380 r600_src->swizzle[2] = 0;
1381 r600_src->swizzle[3] = 0;
1382 } else if (ctx->type == PIPE_SHADER_TESS_EVAL && ctx->info.system_value_semantic_name[tgsi_src->Register.Index] == TGSI_SEMANTIC_PRIMID) {
1383 r600_src->sel = 0;
1384 r600_src->swizzle[0] = 3;
1385 r600_src->swizzle[1] = 3;
1386 r600_src->swizzle[2] = 3;
1387 r600_src->swizzle[3] = 3;
1388 }
1389 } else {
1390 if (tgsi_src->Register.Indirect)
1391 r600_src->rel = V_SQ_REL_RELATIVE;
1392 r600_src->sel = tgsi_src->Register.Index;
1393 r600_src->sel += ctx->file_offset[tgsi_src->Register.File];
1394 }
1395 if (tgsi_src->Register.File == TGSI_FILE_CONSTANT) {
1396 if (tgsi_src->Register.Dimension) {
1397 r600_src->kc_bank = tgsi_src->Dimension.Index;
1398 if (tgsi_src->Dimension.Indirect) {
1399 r600_src->kc_rel = 1;
1400 }
1401 }
1402 }
1403 }
1404
1405 static int tgsi_fetch_rel_const(struct r600_shader_ctx *ctx,
1406 unsigned int cb_idx, unsigned cb_rel, unsigned int offset, unsigned ar_chan,
1407 unsigned int dst_reg)
1408 {
1409 struct r600_bytecode_vtx vtx;
1410 unsigned int ar_reg;
1411 int r;
1412
1413 if (offset) {
1414 struct r600_bytecode_alu alu;
1415
1416 memset(&alu, 0, sizeof(alu));
1417
1418 alu.op = ALU_OP2_ADD_INT;
1419 alu.src[0].sel = ctx->bc->ar_reg;
1420 alu.src[0].chan = ar_chan;
1421
1422 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
1423 alu.src[1].value = offset;
1424
1425 alu.dst.sel = dst_reg;
1426 alu.dst.chan = ar_chan;
1427 alu.dst.write = 1;
1428 alu.last = 1;
1429
1430 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
1431 return r;
1432
1433 ar_reg = dst_reg;
1434 } else {
1435 ar_reg = ctx->bc->ar_reg;
1436 }
1437
1438 memset(&vtx, 0, sizeof(vtx));
1439 vtx.buffer_id = cb_idx;
1440 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
1441 vtx.src_gpr = ar_reg;
1442 vtx.src_sel_x = ar_chan;
1443 vtx.mega_fetch_count = 16;
1444 vtx.dst_gpr = dst_reg;
1445 vtx.dst_sel_x = 0; /* SEL_X */
1446 vtx.dst_sel_y = 1; /* SEL_Y */
1447 vtx.dst_sel_z = 2; /* SEL_Z */
1448 vtx.dst_sel_w = 3; /* SEL_W */
1449 vtx.data_format = FMT_32_32_32_32_FLOAT;
1450 vtx.num_format_all = 2; /* NUM_FORMAT_SCALED */
1451 vtx.format_comp_all = 1; /* FORMAT_COMP_SIGNED */
1452 vtx.endian = r600_endian_swap(32);
1453 vtx.buffer_index_mode = cb_rel; // cb_rel ? V_SQ_CF_INDEX_0 : V_SQ_CF_INDEX_NONE;
1454
1455 if ((r = r600_bytecode_add_vtx(ctx->bc, &vtx)))
1456 return r;
1457
1458 return 0;
1459 }
1460
1461 static int fetch_gs_input(struct r600_shader_ctx *ctx, struct tgsi_full_src_register *src, unsigned int dst_reg)
1462 {
1463 struct r600_bytecode_vtx vtx;
1464 int r;
1465 unsigned index = src->Register.Index;
1466 unsigned vtx_id = src->Dimension.Index;
1467 int offset_reg = vtx_id / 3;
1468 int offset_chan = vtx_id % 3;
1469 int t2 = 0;
1470
1471 /* offsets of per-vertex data in ESGS ring are passed to GS in R0.x, R0.y,
1472 * R0.w, R1.x, R1.y, R1.z (it seems R0.z is used for PrimitiveID) */
1473
1474 if (offset_reg == 0 && offset_chan == 2)
1475 offset_chan = 3;
1476
1477 if (src->Dimension.Indirect || src->Register.Indirect)
1478 t2 = r600_get_temp(ctx);
1479
1480 if (src->Dimension.Indirect) {
1481 int treg[3];
1482 struct r600_bytecode_alu alu;
1483 int r, i;
1484 unsigned addr_reg;
1485 addr_reg = get_address_file_reg(ctx, src->DimIndirect.Index);
1486 if (src->DimIndirect.Index > 0) {
1487 r = single_alu_op2(ctx, ALU_OP1_MOV,
1488 ctx->bc->ar_reg, 0,
1489 addr_reg, 0,
1490 0, 0);
1491 if (r)
1492 return r;
1493 }
1494 /*
1495 we have to put the R0.x/y/w into Rt.x Rt+1.x Rt+2.x then index reg from Rt.
1496 at least this is what fglrx seems to do. */
1497 for (i = 0; i < 3; i++) {
1498 treg[i] = r600_get_temp(ctx);
1499 }
1500 r600_add_gpr_array(ctx->shader, treg[0], 3, 0x0F);
1501
1502 for (i = 0; i < 3; i++) {
1503 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1504 alu.op = ALU_OP1_MOV;
1505 alu.src[0].sel = 0;
1506 alu.src[0].chan = i == 2 ? 3 : i;
1507 alu.dst.sel = treg[i];
1508 alu.dst.chan = 0;
1509 alu.dst.write = 1;
1510 alu.last = 1;
1511 r = r600_bytecode_add_alu(ctx->bc, &alu);
1512 if (r)
1513 return r;
1514 }
1515 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1516 alu.op = ALU_OP1_MOV;
1517 alu.src[0].sel = treg[0];
1518 alu.src[0].rel = 1;
1519 alu.dst.sel = t2;
1520 alu.dst.write = 1;
1521 alu.last = 1;
1522 r = r600_bytecode_add_alu(ctx->bc, &alu);
1523 if (r)
1524 return r;
1525 offset_reg = t2;
1526 offset_chan = 0;
1527 }
1528
1529 if (src->Register.Indirect) {
1530 int addr_reg;
1531 unsigned first = ctx->info.input_array_first[src->Indirect.ArrayID];
1532
1533 addr_reg = get_address_file_reg(ctx, src->Indirect.Index);
1534
1535 /* pull the value from index_reg */
1536 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
1537 t2, 1,
1538 addr_reg, 0,
1539 V_SQ_ALU_SRC_LITERAL, first);
1540 if (r)
1541 return r;
1542 r = single_alu_op3(ctx, ALU_OP3_MULADD_UINT24,
1543 t2, 0,
1544 t2, 1,
1545 V_SQ_ALU_SRC_LITERAL, 4,
1546 offset_reg, offset_chan);
1547 if (r)
1548 return r;
1549 offset_reg = t2;
1550 offset_chan = 0;
1551 index = src->Register.Index - first;
1552 }
1553
1554 memset(&vtx, 0, sizeof(vtx));
1555 vtx.buffer_id = R600_GS_RING_CONST_BUFFER;
1556 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
1557 vtx.src_gpr = offset_reg;
1558 vtx.src_sel_x = offset_chan;
1559 vtx.offset = index * 16; /*bytes*/
1560 vtx.mega_fetch_count = 16;
1561 vtx.dst_gpr = dst_reg;
1562 vtx.dst_sel_x = 0; /* SEL_X */
1563 vtx.dst_sel_y = 1; /* SEL_Y */
1564 vtx.dst_sel_z = 2; /* SEL_Z */
1565 vtx.dst_sel_w = 3; /* SEL_W */
1566 if (ctx->bc->chip_class >= EVERGREEN) {
1567 vtx.use_const_fields = 1;
1568 } else {
1569 vtx.data_format = FMT_32_32_32_32_FLOAT;
1570 }
1571
1572 if ((r = r600_bytecode_add_vtx(ctx->bc, &vtx)))
1573 return r;
1574
1575 return 0;
1576 }
1577
1578 static int tgsi_split_gs_inputs(struct r600_shader_ctx *ctx)
1579 {
1580 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
1581 unsigned i;
1582
1583 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
1584 struct tgsi_full_src_register *src = &inst->Src[i];
1585
1586 if (src->Register.File == TGSI_FILE_INPUT) {
1587 if (ctx->shader->input[src->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
1588 /* primitive id is in R0.z */
1589 ctx->src[i].sel = 0;
1590 ctx->src[i].swizzle[0] = 2;
1591 }
1592 }
1593 if (src->Register.File == TGSI_FILE_INPUT && src->Register.Dimension) {
1594 int treg = r600_get_temp(ctx);
1595
1596 fetch_gs_input(ctx, src, treg);
1597 ctx->src[i].sel = treg;
1598 ctx->src[i].rel = 0;
1599 }
1600 }
1601 return 0;
1602 }
1603
1604
1605 /* Tessellation shaders pass outputs to the next shader using LDS.
1606 *
1607 * LS outputs = TCS(HS) inputs
1608 * TCS(HS) outputs = TES(DS) inputs
1609 *
1610 * The LDS layout is:
1611 * - TCS inputs for patch 0
1612 * - TCS inputs for patch 1
1613 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
1614 * - ...
1615 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
1616 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
1617 * - TCS outputs for patch 1
1618 * - Per-patch TCS outputs for patch 1
1619 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
1620 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
1621 * - ...
1622 *
1623 * All three shaders VS(LS), TCS, TES share the same LDS space.
1624 */
1625 /* this will return with the dw address in temp_reg.x */
1626 static int r600_get_byte_address(struct r600_shader_ctx *ctx, int temp_reg,
1627 const struct tgsi_full_dst_register *dst,
1628 const struct tgsi_full_src_register *src,
1629 int stride_bytes_reg, int stride_bytes_chan)
1630 {
1631 struct tgsi_full_dst_register reg;
1632 ubyte *name, *index, *array_first;
1633 int r;
1634 int param;
1635 struct tgsi_shader_info *info = &ctx->info;
1636 /* Set the register description. The address computation is the same
1637 * for sources and destinations. */
1638 if (src) {
1639 reg.Register.File = src->Register.File;
1640 reg.Register.Index = src->Register.Index;
1641 reg.Register.Indirect = src->Register.Indirect;
1642 reg.Register.Dimension = src->Register.Dimension;
1643 reg.Indirect = src->Indirect;
1644 reg.Dimension = src->Dimension;
1645 reg.DimIndirect = src->DimIndirect;
1646 } else
1647 reg = *dst;
1648
1649 /* If the register is 2-dimensional (e.g. an array of vertices
1650 * in a primitive), calculate the base address of the vertex. */
1651 if (reg.Register.Dimension) {
1652 int sel, chan;
1653 if (reg.Dimension.Indirect) {
1654 unsigned addr_reg;
1655 assert (reg.DimIndirect.File == TGSI_FILE_ADDRESS);
1656
1657 addr_reg = get_address_file_reg(ctx, reg.DimIndirect.Index);
1658 /* pull the value from index_reg */
1659 sel = addr_reg;
1660 chan = 0;
1661 } else {
1662 sel = V_SQ_ALU_SRC_LITERAL;
1663 chan = reg.Dimension.Index;
1664 }
1665
1666 r = single_alu_op3(ctx, ALU_OP3_MULADD_UINT24,
1667 temp_reg, 0,
1668 stride_bytes_reg, stride_bytes_chan,
1669 sel, chan,
1670 temp_reg, 0);
1671 if (r)
1672 return r;
1673 }
1674
1675 if (reg.Register.File == TGSI_FILE_INPUT) {
1676 name = info->input_semantic_name;
1677 index = info->input_semantic_index;
1678 array_first = info->input_array_first;
1679 } else if (reg.Register.File == TGSI_FILE_OUTPUT) {
1680 name = info->output_semantic_name;
1681 index = info->output_semantic_index;
1682 array_first = info->output_array_first;
1683 } else {
1684 assert(0);
1685 return -1;
1686 }
1687 if (reg.Register.Indirect) {
1688 int addr_reg;
1689 int first;
1690 /* Add the relative address of the element. */
1691 if (reg.Indirect.ArrayID)
1692 first = array_first[reg.Indirect.ArrayID];
1693 else
1694 first = reg.Register.Index;
1695
1696 addr_reg = get_address_file_reg(ctx, reg.Indirect.Index);
1697
1698 /* pull the value from index_reg */
1699 r = single_alu_op3(ctx, ALU_OP3_MULADD_UINT24,
1700 temp_reg, 0,
1701 V_SQ_ALU_SRC_LITERAL, 16,
1702 addr_reg, 0,
1703 temp_reg, 0);
1704 if (r)
1705 return r;
1706
1707 param = r600_get_lds_unique_index(name[first],
1708 index[first]);
1709
1710 } else {
1711 param = r600_get_lds_unique_index(name[reg.Register.Index],
1712 index[reg.Register.Index]);
1713 }
1714
1715 /* add to base_addr - passed in temp_reg.x */
1716 if (param) {
1717 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
1718 temp_reg, 0,
1719 temp_reg, 0,
1720 V_SQ_ALU_SRC_LITERAL, param * 16);
1721 if (r)
1722 return r;
1723
1724 }
1725 return 0;
1726 }
1727
1728 static int do_lds_fetch_values(struct r600_shader_ctx *ctx, unsigned temp_reg,
1729 unsigned dst_reg)
1730 {
1731 struct r600_bytecode_alu alu;
1732 int r, i;
1733
1734 if ((ctx->bc->cf_last->ndw>>1) >= 0x60)
1735 ctx->bc->force_add_cf = 1;
1736 for (i = 1; i < 4; i++) {
1737 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
1738 temp_reg, i,
1739 temp_reg, 0,
1740 V_SQ_ALU_SRC_LITERAL, 4 * i);
1741 if (r)
1742 return r;
1743 }
1744 for (i = 0; i < 4; i++) {
1745 /* emit an LDS_READ_RET */
1746 memset(&alu, 0, sizeof(alu));
1747 alu.op = LDS_OP1_LDS_READ_RET;
1748 alu.src[0].sel = temp_reg;
1749 alu.src[0].chan = i;
1750 alu.src[1].sel = V_SQ_ALU_SRC_0;
1751 alu.src[2].sel = V_SQ_ALU_SRC_0;
1752 alu.dst.chan = 0;
1753 alu.is_lds_idx_op = true;
1754 alu.last = 1;
1755 r = r600_bytecode_add_alu(ctx->bc, &alu);
1756 if (r)
1757 return r;
1758 }
1759 for (i = 0; i < 4; i++) {
1760 /* then read from LDS_OQ_A_POP */
1761 memset(&alu, 0, sizeof(alu));
1762
1763 alu.op = ALU_OP1_MOV;
1764 alu.src[0].sel = EG_V_SQ_ALU_SRC_LDS_OQ_A_POP;
1765 alu.src[0].chan = 0;
1766 alu.dst.sel = dst_reg;
1767 alu.dst.chan = i;
1768 alu.dst.write = 1;
1769 alu.last = 1;
1770 r = r600_bytecode_add_alu(ctx->bc, &alu);
1771 if (r)
1772 return r;
1773 }
1774 return 0;
1775 }
1776
1777 static int fetch_tes_input(struct r600_shader_ctx *ctx, struct tgsi_full_src_register *src, unsigned int dst_reg)
1778 {
1779 int r;
1780 unsigned temp_reg = r600_get_temp(ctx);
1781
1782 r = get_lds_offset0(ctx, 2, temp_reg,
1783 src->Register.Dimension ? false : true);
1784 if (r)
1785 return r;
1786
1787 /* the base address is now in temp.x */
1788 r = r600_get_byte_address(ctx, temp_reg,
1789 NULL, src, ctx->tess_output_info, 1);
1790 if (r)
1791 return r;
1792
1793 r = do_lds_fetch_values(ctx, temp_reg, dst_reg);
1794 if (r)
1795 return r;
1796 return 0;
1797 }
1798
1799 static int fetch_tcs_input(struct r600_shader_ctx *ctx, struct tgsi_full_src_register *src, unsigned int dst_reg)
1800 {
1801 int r;
1802 unsigned temp_reg = r600_get_temp(ctx);
1803
1804 /* t.x = ips * r0.y */
1805 r = single_alu_op2(ctx, ALU_OP2_MUL_UINT24,
1806 temp_reg, 0,
1807 ctx->tess_input_info, 0,
1808 0, 1);
1809
1810 if (r)
1811 return r;
1812
1813 /* the base address is now in temp.x */
1814 r = r600_get_byte_address(ctx, temp_reg,
1815 NULL, src, ctx->tess_input_info, 1);
1816 if (r)
1817 return r;
1818
1819 r = do_lds_fetch_values(ctx, temp_reg, dst_reg);
1820 if (r)
1821 return r;
1822 return 0;
1823 }
1824
1825 static int fetch_tcs_output(struct r600_shader_ctx *ctx, struct tgsi_full_src_register *src, unsigned int dst_reg)
1826 {
1827 int r;
1828 unsigned temp_reg = r600_get_temp(ctx);
1829
1830 r = get_lds_offset0(ctx, 1, temp_reg,
1831 src->Register.Dimension ? false : true);
1832 if (r)
1833 return r;
1834 /* the base address is now in temp.x */
1835 r = r600_get_byte_address(ctx, temp_reg,
1836 NULL, src,
1837 ctx->tess_output_info, 1);
1838 if (r)
1839 return r;
1840
1841 r = do_lds_fetch_values(ctx, temp_reg, dst_reg);
1842 if (r)
1843 return r;
1844 return 0;
1845 }
1846
1847 static int tgsi_split_lds_inputs(struct r600_shader_ctx *ctx)
1848 {
1849 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
1850 unsigned i;
1851
1852 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
1853 struct tgsi_full_src_register *src = &inst->Src[i];
1854
1855 if (ctx->type == PIPE_SHADER_TESS_EVAL && src->Register.File == TGSI_FILE_INPUT) {
1856 int treg = r600_get_temp(ctx);
1857 fetch_tes_input(ctx, src, treg);
1858 ctx->src[i].sel = treg;
1859 ctx->src[i].rel = 0;
1860 }
1861 if (ctx->type == PIPE_SHADER_TESS_CTRL && src->Register.File == TGSI_FILE_INPUT) {
1862 int treg = r600_get_temp(ctx);
1863 fetch_tcs_input(ctx, src, treg);
1864 ctx->src[i].sel = treg;
1865 ctx->src[i].rel = 0;
1866 }
1867 if (ctx->type == PIPE_SHADER_TESS_CTRL && src->Register.File == TGSI_FILE_OUTPUT) {
1868 int treg = r600_get_temp(ctx);
1869 fetch_tcs_output(ctx, src, treg);
1870 ctx->src[i].sel = treg;
1871 ctx->src[i].rel = 0;
1872 }
1873 }
1874 return 0;
1875 }
1876
1877 static int tgsi_split_constant(struct r600_shader_ctx *ctx)
1878 {
1879 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
1880 struct r600_bytecode_alu alu;
1881 int i, j, k, nconst, r;
1882
1883 for (i = 0, nconst = 0; i < inst->Instruction.NumSrcRegs; i++) {
1884 if (inst->Src[i].Register.File == TGSI_FILE_CONSTANT) {
1885 nconst++;
1886 }
1887 tgsi_src(ctx, &inst->Src[i], &ctx->src[i]);
1888 }
1889 for (i = 0, j = nconst - 1; i < inst->Instruction.NumSrcRegs; i++) {
1890 if (inst->Src[i].Register.File != TGSI_FILE_CONSTANT) {
1891 continue;
1892 }
1893
1894 if (ctx->src[i].rel) {
1895 int chan = inst->Src[i].Indirect.Swizzle;
1896 int treg = r600_get_temp(ctx);
1897 if ((r = tgsi_fetch_rel_const(ctx, ctx->src[i].kc_bank, ctx->src[i].kc_rel, ctx->src[i].sel - 512, chan, treg)))
1898 return r;
1899
1900 ctx->src[i].kc_bank = 0;
1901 ctx->src[i].kc_rel = 0;
1902 ctx->src[i].sel = treg;
1903 ctx->src[i].rel = 0;
1904 j--;
1905 } else if (j > 0) {
1906 int treg = r600_get_temp(ctx);
1907 for (k = 0; k < 4; k++) {
1908 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1909 alu.op = ALU_OP1_MOV;
1910 alu.src[0].sel = ctx->src[i].sel;
1911 alu.src[0].chan = k;
1912 alu.src[0].rel = ctx->src[i].rel;
1913 alu.src[0].kc_bank = ctx->src[i].kc_bank;
1914 alu.src[0].kc_rel = ctx->src[i].kc_rel;
1915 alu.dst.sel = treg;
1916 alu.dst.chan = k;
1917 alu.dst.write = 1;
1918 if (k == 3)
1919 alu.last = 1;
1920 r = r600_bytecode_add_alu(ctx->bc, &alu);
1921 if (r)
1922 return r;
1923 }
1924 ctx->src[i].sel = treg;
1925 ctx->src[i].rel =0;
1926 j--;
1927 }
1928 }
1929 return 0;
1930 }
1931
1932 /* need to move any immediate into a temp - for trig functions which use literal for PI stuff */
1933 static int tgsi_split_literal_constant(struct r600_shader_ctx *ctx)
1934 {
1935 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
1936 struct r600_bytecode_alu alu;
1937 int i, j, k, nliteral, r;
1938
1939 for (i = 0, nliteral = 0; i < inst->Instruction.NumSrcRegs; i++) {
1940 if (ctx->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
1941 nliteral++;
1942 }
1943 }
1944 for (i = 0, j = nliteral - 1; i < inst->Instruction.NumSrcRegs; i++) {
1945 if (j > 0 && ctx->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
1946 int treg = r600_get_temp(ctx);
1947 for (k = 0; k < 4; k++) {
1948 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
1949 alu.op = ALU_OP1_MOV;
1950 alu.src[0].sel = ctx->src[i].sel;
1951 alu.src[0].chan = k;
1952 alu.src[0].value = ctx->src[i].value[k];
1953 alu.dst.sel = treg;
1954 alu.dst.chan = k;
1955 alu.dst.write = 1;
1956 if (k == 3)
1957 alu.last = 1;
1958 r = r600_bytecode_add_alu(ctx->bc, &alu);
1959 if (r)
1960 return r;
1961 }
1962 ctx->src[i].sel = treg;
1963 j--;
1964 }
1965 }
1966 return 0;
1967 }
1968
1969 static int process_twoside_color_inputs(struct r600_shader_ctx *ctx)
1970 {
1971 int i, r, count = ctx->shader->ninput;
1972
1973 for (i = 0; i < count; i++) {
1974 if (ctx->shader->input[i].name == TGSI_SEMANTIC_COLOR) {
1975 r = select_twoside_color(ctx, i, ctx->shader->input[i].back_color_input);
1976 if (r)
1977 return r;
1978 }
1979 }
1980 return 0;
1981 }
1982
1983 static int emit_streamout(struct r600_shader_ctx *ctx, struct pipe_stream_output_info *so,
1984 int stream, unsigned *stream_item_size)
1985 {
1986 unsigned so_gpr[PIPE_MAX_SHADER_OUTPUTS];
1987 unsigned start_comp[PIPE_MAX_SHADER_OUTPUTS];
1988 int i, j, r;
1989
1990 /* Sanity checking. */
1991 if (so->num_outputs > PIPE_MAX_SO_OUTPUTS) {
1992 R600_ERR("Too many stream outputs: %d\n", so->num_outputs);
1993 r = -EINVAL;
1994 goto out_err;
1995 }
1996 for (i = 0; i < so->num_outputs; i++) {
1997 if (so->output[i].output_buffer >= 4) {
1998 R600_ERR("Exceeded the max number of stream output buffers, got: %d\n",
1999 so->output[i].output_buffer);
2000 r = -EINVAL;
2001 goto out_err;
2002 }
2003 }
2004
2005 /* Initialize locations where the outputs are stored. */
2006 for (i = 0; i < so->num_outputs; i++) {
2007
2008 so_gpr[i] = ctx->shader->output[so->output[i].register_index].gpr;
2009 start_comp[i] = so->output[i].start_component;
2010 /* Lower outputs with dst_offset < start_component.
2011 *
2012 * We can only output 4D vectors with a write mask, e.g. we can
2013 * only output the W component at offset 3, etc. If we want
2014 * to store Y, Z, or W at buffer offset 0, we need to use MOV
2015 * to move it to X and output X. */
2016 if (so->output[i].dst_offset < so->output[i].start_component) {
2017 unsigned tmp = r600_get_temp(ctx);
2018
2019 for (j = 0; j < so->output[i].num_components; j++) {
2020 struct r600_bytecode_alu alu;
2021 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
2022 alu.op = ALU_OP1_MOV;
2023 alu.src[0].sel = so_gpr[i];
2024 alu.src[0].chan = so->output[i].start_component + j;
2025
2026 alu.dst.sel = tmp;
2027 alu.dst.chan = j;
2028 alu.dst.write = 1;
2029 if (j == so->output[i].num_components - 1)
2030 alu.last = 1;
2031 r = r600_bytecode_add_alu(ctx->bc, &alu);
2032 if (r)
2033 return r;
2034 }
2035 start_comp[i] = 0;
2036 so_gpr[i] = tmp;
2037 }
2038 }
2039
2040 /* Write outputs to buffers. */
2041 for (i = 0; i < so->num_outputs; i++) {
2042 struct r600_bytecode_output output;
2043
2044 if (stream != -1 && stream != so->output[i].output_buffer)
2045 continue;
2046
2047 memset(&output, 0, sizeof(struct r600_bytecode_output));
2048 output.gpr = so_gpr[i];
2049 output.elem_size = so->output[i].num_components - 1;
2050 if (output.elem_size == 2)
2051 output.elem_size = 3; // 3 not supported, write 4 with junk at end
2052 output.array_base = so->output[i].dst_offset - start_comp[i];
2053 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE;
2054 output.burst_count = 1;
2055 /* array_size is an upper limit for the burst_count
2056 * with MEM_STREAM instructions */
2057 output.array_size = 0xFFF;
2058 output.comp_mask = ((1 << so->output[i].num_components) - 1) << start_comp[i];
2059
2060 if (ctx->bc->chip_class >= EVERGREEN) {
2061 switch (so->output[i].output_buffer) {
2062 case 0:
2063 output.op = CF_OP_MEM_STREAM0_BUF0;
2064 break;
2065 case 1:
2066 output.op = CF_OP_MEM_STREAM0_BUF1;
2067 break;
2068 case 2:
2069 output.op = CF_OP_MEM_STREAM0_BUF2;
2070 break;
2071 case 3:
2072 output.op = CF_OP_MEM_STREAM0_BUF3;
2073 break;
2074 }
2075 output.op += so->output[i].stream * 4;
2076 assert(output.op >= CF_OP_MEM_STREAM0_BUF0 && output.op <= CF_OP_MEM_STREAM3_BUF3);
2077 ctx->enabled_stream_buffers_mask |= (1 << so->output[i].output_buffer) << so->output[i].stream * 4;
2078 } else {
2079 switch (so->output[i].output_buffer) {
2080 case 0:
2081 output.op = CF_OP_MEM_STREAM0;
2082 break;
2083 case 1:
2084 output.op = CF_OP_MEM_STREAM1;
2085 break;
2086 case 2:
2087 output.op = CF_OP_MEM_STREAM2;
2088 break;
2089 case 3:
2090 output.op = CF_OP_MEM_STREAM3;
2091 break;
2092 }
2093 ctx->enabled_stream_buffers_mask |= 1 << so->output[i].output_buffer;
2094 }
2095 r = r600_bytecode_add_output(ctx->bc, &output);
2096 if (r)
2097 goto out_err;
2098 }
2099 return 0;
2100 out_err:
2101 return r;
2102 }
2103
2104 static void convert_edgeflag_to_int(struct r600_shader_ctx *ctx)
2105 {
2106 struct r600_bytecode_alu alu;
2107 unsigned reg;
2108
2109 if (!ctx->shader->vs_out_edgeflag)
2110 return;
2111
2112 reg = ctx->shader->output[ctx->edgeflag_output].gpr;
2113
2114 /* clamp(x, 0, 1) */
2115 memset(&alu, 0, sizeof(alu));
2116 alu.op = ALU_OP1_MOV;
2117 alu.src[0].sel = reg;
2118 alu.dst.sel = reg;
2119 alu.dst.write = 1;
2120 alu.dst.clamp = 1;
2121 alu.last = 1;
2122 r600_bytecode_add_alu(ctx->bc, &alu);
2123
2124 memset(&alu, 0, sizeof(alu));
2125 alu.op = ALU_OP1_FLT_TO_INT;
2126 alu.src[0].sel = reg;
2127 alu.dst.sel = reg;
2128 alu.dst.write = 1;
2129 alu.last = 1;
2130 r600_bytecode_add_alu(ctx->bc, &alu);
2131 }
2132
2133 static int generate_gs_copy_shader(struct r600_context *rctx,
2134 struct r600_pipe_shader *gs,
2135 struct pipe_stream_output_info *so)
2136 {
2137 struct r600_shader_ctx ctx = {};
2138 struct r600_shader *gs_shader = &gs->shader;
2139 struct r600_pipe_shader *cshader;
2140 int ocnt = gs_shader->noutput;
2141 struct r600_bytecode_alu alu;
2142 struct r600_bytecode_vtx vtx;
2143 struct r600_bytecode_output output;
2144 struct r600_bytecode_cf *cf_jump, *cf_pop,
2145 *last_exp_pos = NULL, *last_exp_param = NULL;
2146 int i, j, next_clip_pos = 61, next_param = 0;
2147 int ring;
2148 bool only_ring_0 = true;
2149 cshader = calloc(1, sizeof(struct r600_pipe_shader));
2150 if (!cshader)
2151 return 0;
2152
2153 memcpy(cshader->shader.output, gs_shader->output, ocnt *
2154 sizeof(struct r600_shader_io));
2155
2156 cshader->shader.noutput = ocnt;
2157
2158 ctx.shader = &cshader->shader;
2159 ctx.bc = &ctx.shader->bc;
2160 ctx.type = ctx.bc->type = PIPE_SHADER_VERTEX;
2161
2162 r600_bytecode_init(ctx.bc, rctx->b.chip_class, rctx->b.family,
2163 rctx->screen->has_compressed_msaa_texturing);
2164
2165 ctx.bc->isa = rctx->isa;
2166
2167 cf_jump = NULL;
2168 memset(cshader->shader.ring_item_sizes, 0, sizeof(cshader->shader.ring_item_sizes));
2169
2170 /* R0.x = R0.x & 0x3fffffff */
2171 memset(&alu, 0, sizeof(alu));
2172 alu.op = ALU_OP2_AND_INT;
2173 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2174 alu.src[1].value = 0x3fffffff;
2175 alu.dst.write = 1;
2176 r600_bytecode_add_alu(ctx.bc, &alu);
2177
2178 /* R0.y = R0.x >> 30 */
2179 memset(&alu, 0, sizeof(alu));
2180 alu.op = ALU_OP2_LSHR_INT;
2181 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2182 alu.src[1].value = 0x1e;
2183 alu.dst.chan = 1;
2184 alu.dst.write = 1;
2185 alu.last = 1;
2186 r600_bytecode_add_alu(ctx.bc, &alu);
2187
2188 /* fetch vertex data from GSVS ring */
2189 for (i = 0; i < ocnt; ++i) {
2190 struct r600_shader_io *out = &ctx.shader->output[i];
2191
2192 out->gpr = i + 1;
2193 out->ring_offset = i * 16;
2194
2195 memset(&vtx, 0, sizeof(vtx));
2196 vtx.op = FETCH_OP_VFETCH;
2197 vtx.buffer_id = R600_GS_RING_CONST_BUFFER;
2198 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
2199 vtx.mega_fetch_count = 16;
2200 vtx.offset = out->ring_offset;
2201 vtx.dst_gpr = out->gpr;
2202 vtx.src_gpr = 0;
2203 vtx.dst_sel_x = 0;
2204 vtx.dst_sel_y = 1;
2205 vtx.dst_sel_z = 2;
2206 vtx.dst_sel_w = 3;
2207 if (rctx->b.chip_class >= EVERGREEN) {
2208 vtx.use_const_fields = 1;
2209 } else {
2210 vtx.data_format = FMT_32_32_32_32_FLOAT;
2211 }
2212
2213 r600_bytecode_add_vtx(ctx.bc, &vtx);
2214 }
2215 ctx.temp_reg = i + 1;
2216 for (ring = 3; ring >= 0; --ring) {
2217 bool enabled = false;
2218 for (i = 0; i < so->num_outputs; i++) {
2219 if (so->output[i].stream == ring) {
2220 enabled = true;
2221 if (ring > 0)
2222 only_ring_0 = false;
2223 break;
2224 }
2225 }
2226 if (ring != 0 && !enabled) {
2227 cshader->shader.ring_item_sizes[ring] = 0;
2228 continue;
2229 }
2230
2231 if (cf_jump) {
2232 // Patch up jump label
2233 r600_bytecode_add_cfinst(ctx.bc, CF_OP_POP);
2234 cf_pop = ctx.bc->cf_last;
2235
2236 cf_jump->cf_addr = cf_pop->id + 2;
2237 cf_jump->pop_count = 1;
2238 cf_pop->cf_addr = cf_pop->id + 2;
2239 cf_pop->pop_count = 1;
2240 }
2241
2242 /* PRED_SETE_INT __, R0.y, ring */
2243 memset(&alu, 0, sizeof(alu));
2244 alu.op = ALU_OP2_PRED_SETE_INT;
2245 alu.src[0].chan = 1;
2246 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2247 alu.src[1].value = ring;
2248 alu.execute_mask = 1;
2249 alu.update_pred = 1;
2250 alu.last = 1;
2251 r600_bytecode_add_alu_type(ctx.bc, &alu, CF_OP_ALU_PUSH_BEFORE);
2252
2253 r600_bytecode_add_cfinst(ctx.bc, CF_OP_JUMP);
2254 cf_jump = ctx.bc->cf_last;
2255
2256 if (enabled)
2257 emit_streamout(&ctx, so, only_ring_0 ? -1 : ring, &cshader->shader.ring_item_sizes[ring]);
2258 cshader->shader.ring_item_sizes[ring] = ocnt * 16;
2259 }
2260
2261 /* bc adds nops - copy it */
2262 if (ctx.bc->chip_class == R600) {
2263 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
2264 alu.op = ALU_OP0_NOP;
2265 alu.last = 1;
2266 r600_bytecode_add_alu(ctx.bc, &alu);
2267
2268 r600_bytecode_add_cfinst(ctx.bc, CF_OP_NOP);
2269 }
2270
2271 /* export vertex data */
2272 /* XXX factor out common code with r600_shader_from_tgsi ? */
2273 for (i = 0; i < ocnt; ++i) {
2274 struct r600_shader_io *out = &ctx.shader->output[i];
2275 bool instream0 = true;
2276 if (out->name == TGSI_SEMANTIC_CLIPVERTEX)
2277 continue;
2278
2279 for (j = 0; j < so->num_outputs; j++) {
2280 if (so->output[j].register_index == i) {
2281 if (so->output[j].stream == 0)
2282 break;
2283 if (so->output[j].stream > 0)
2284 instream0 = false;
2285 }
2286 }
2287 if (!instream0)
2288 continue;
2289 memset(&output, 0, sizeof(output));
2290 output.gpr = out->gpr;
2291 output.elem_size = 3;
2292 output.swizzle_x = 0;
2293 output.swizzle_y = 1;
2294 output.swizzle_z = 2;
2295 output.swizzle_w = 3;
2296 output.burst_count = 1;
2297 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
2298 output.op = CF_OP_EXPORT;
2299 switch (out->name) {
2300 case TGSI_SEMANTIC_POSITION:
2301 output.array_base = 60;
2302 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
2303 break;
2304
2305 case TGSI_SEMANTIC_PSIZE:
2306 output.array_base = 61;
2307 if (next_clip_pos == 61)
2308 next_clip_pos = 62;
2309 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
2310 output.swizzle_y = 7;
2311 output.swizzle_z = 7;
2312 output.swizzle_w = 7;
2313 ctx.shader->vs_out_misc_write = 1;
2314 ctx.shader->vs_out_point_size = 1;
2315 break;
2316 case TGSI_SEMANTIC_LAYER:
2317 if (out->spi_sid) {
2318 /* duplicate it as PARAM to pass to the pixel shader */
2319 output.array_base = next_param++;
2320 r600_bytecode_add_output(ctx.bc, &output);
2321 last_exp_param = ctx.bc->cf_last;
2322 }
2323 output.array_base = 61;
2324 if (next_clip_pos == 61)
2325 next_clip_pos = 62;
2326 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
2327 output.swizzle_x = 7;
2328 output.swizzle_y = 7;
2329 output.swizzle_z = 0;
2330 output.swizzle_w = 7;
2331 ctx.shader->vs_out_misc_write = 1;
2332 ctx.shader->vs_out_layer = 1;
2333 break;
2334 case TGSI_SEMANTIC_VIEWPORT_INDEX:
2335 if (out->spi_sid) {
2336 /* duplicate it as PARAM to pass to the pixel shader */
2337 output.array_base = next_param++;
2338 r600_bytecode_add_output(ctx.bc, &output);
2339 last_exp_param = ctx.bc->cf_last;
2340 }
2341 output.array_base = 61;
2342 if (next_clip_pos == 61)
2343 next_clip_pos = 62;
2344 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
2345 ctx.shader->vs_out_misc_write = 1;
2346 ctx.shader->vs_out_viewport = 1;
2347 output.swizzle_x = 7;
2348 output.swizzle_y = 7;
2349 output.swizzle_z = 7;
2350 output.swizzle_w = 0;
2351 break;
2352 case TGSI_SEMANTIC_CLIPDIST:
2353 /* spi_sid is 0 for clipdistance outputs that were generated
2354 * for clipvertex - we don't need to pass them to PS */
2355 ctx.shader->clip_dist_write = gs->shader.clip_dist_write;
2356 if (out->spi_sid) {
2357 /* duplicate it as PARAM to pass to the pixel shader */
2358 output.array_base = next_param++;
2359 r600_bytecode_add_output(ctx.bc, &output);
2360 last_exp_param = ctx.bc->cf_last;
2361 }
2362 output.array_base = next_clip_pos++;
2363 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
2364 break;
2365 case TGSI_SEMANTIC_FOG:
2366 output.swizzle_y = 4; /* 0 */
2367 output.swizzle_z = 4; /* 0 */
2368 output.swizzle_w = 5; /* 1 */
2369 break;
2370 default:
2371 output.array_base = next_param++;
2372 break;
2373 }
2374 r600_bytecode_add_output(ctx.bc, &output);
2375 if (output.type == V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM)
2376 last_exp_param = ctx.bc->cf_last;
2377 else
2378 last_exp_pos = ctx.bc->cf_last;
2379 }
2380
2381 if (!last_exp_pos) {
2382 memset(&output, 0, sizeof(output));
2383 output.gpr = 0;
2384 output.elem_size = 3;
2385 output.swizzle_x = 7;
2386 output.swizzle_y = 7;
2387 output.swizzle_z = 7;
2388 output.swizzle_w = 7;
2389 output.burst_count = 1;
2390 output.type = 2;
2391 output.op = CF_OP_EXPORT;
2392 output.array_base = 60;
2393 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
2394 r600_bytecode_add_output(ctx.bc, &output);
2395 last_exp_pos = ctx.bc->cf_last;
2396 }
2397
2398 if (!last_exp_param) {
2399 memset(&output, 0, sizeof(output));
2400 output.gpr = 0;
2401 output.elem_size = 3;
2402 output.swizzle_x = 7;
2403 output.swizzle_y = 7;
2404 output.swizzle_z = 7;
2405 output.swizzle_w = 7;
2406 output.burst_count = 1;
2407 output.type = 2;
2408 output.op = CF_OP_EXPORT;
2409 output.array_base = next_param++;
2410 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
2411 r600_bytecode_add_output(ctx.bc, &output);
2412 last_exp_param = ctx.bc->cf_last;
2413 }
2414
2415 last_exp_pos->op = CF_OP_EXPORT_DONE;
2416 last_exp_param->op = CF_OP_EXPORT_DONE;
2417
2418 r600_bytecode_add_cfinst(ctx.bc, CF_OP_POP);
2419 cf_pop = ctx.bc->cf_last;
2420
2421 cf_jump->cf_addr = cf_pop->id + 2;
2422 cf_jump->pop_count = 1;
2423 cf_pop->cf_addr = cf_pop->id + 2;
2424 cf_pop->pop_count = 1;
2425
2426 if (ctx.bc->chip_class == CAYMAN)
2427 cm_bytecode_add_cf_end(ctx.bc);
2428 else {
2429 r600_bytecode_add_cfinst(ctx.bc, CF_OP_NOP);
2430 ctx.bc->cf_last->end_of_program = 1;
2431 }
2432
2433 gs->gs_copy_shader = cshader;
2434 cshader->enabled_stream_buffers_mask = ctx.enabled_stream_buffers_mask;
2435
2436 ctx.bc->nstack = 1;
2437
2438 return r600_bytecode_build(ctx.bc);
2439 }
2440
2441 static int emit_inc_ring_offset(struct r600_shader_ctx *ctx, int idx, bool ind)
2442 {
2443 if (ind) {
2444 struct r600_bytecode_alu alu;
2445 int r;
2446
2447 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
2448 alu.op = ALU_OP2_ADD_INT;
2449 alu.src[0].sel = ctx->gs_export_gpr_tregs[idx];
2450 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2451 alu.src[1].value = ctx->gs_out_ring_offset >> 4;
2452 alu.dst.sel = ctx->gs_export_gpr_tregs[idx];
2453 alu.dst.write = 1;
2454 alu.last = 1;
2455 r = r600_bytecode_add_alu(ctx->bc, &alu);
2456 if (r)
2457 return r;
2458 }
2459 return 0;
2460 }
2461
2462 static int emit_gs_ring_writes(struct r600_shader_ctx *ctx, const struct pipe_stream_output_info *so, int stream, bool ind)
2463 {
2464 struct r600_bytecode_output output;
2465 int i, k, ring_offset;
2466 int effective_stream = stream == -1 ? 0 : stream;
2467 int idx = 0;
2468
2469 for (i = 0; i < ctx->shader->noutput; i++) {
2470 if (ctx->gs_for_vs) {
2471 /* for ES we need to lookup corresponding ring offset expected by GS
2472 * (map this output to GS input by name and sid) */
2473 /* FIXME precompute offsets */
2474 ring_offset = -1;
2475 for(k = 0; k < ctx->gs_for_vs->ninput; ++k) {
2476 struct r600_shader_io *in = &ctx->gs_for_vs->input[k];
2477 struct r600_shader_io *out = &ctx->shader->output[i];
2478 if (in->name == out->name && in->sid == out->sid)
2479 ring_offset = in->ring_offset;
2480 }
2481
2482 if (ring_offset == -1)
2483 continue;
2484 } else {
2485 ring_offset = idx * 16;
2486 idx++;
2487 }
2488
2489 if (stream > 0 && ctx->shader->output[i].name == TGSI_SEMANTIC_POSITION)
2490 continue;
2491 /* next_ring_offset after parsing input decls contains total size of
2492 * single vertex data, gs_next_vertex - current vertex index */
2493 if (!ind)
2494 ring_offset += ctx->gs_out_ring_offset * ctx->gs_next_vertex;
2495
2496 memset(&output, 0, sizeof(struct r600_bytecode_output));
2497 output.gpr = ctx->shader->output[i].gpr;
2498 output.elem_size = 3;
2499 output.comp_mask = 0xF;
2500 output.burst_count = 1;
2501
2502 if (ind)
2503 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE_IND;
2504 else
2505 output.type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE;
2506
2507 switch (stream) {
2508 default:
2509 case 0:
2510 output.op = CF_OP_MEM_RING; break;
2511 case 1:
2512 output.op = CF_OP_MEM_RING1; break;
2513 case 2:
2514 output.op = CF_OP_MEM_RING2; break;
2515 case 3:
2516 output.op = CF_OP_MEM_RING3; break;
2517 }
2518
2519 if (ind) {
2520 output.array_base = ring_offset >> 2; /* in dwords */
2521 output.array_size = 0xfff;
2522 output.index_gpr = ctx->gs_export_gpr_tregs[effective_stream];
2523 } else
2524 output.array_base = ring_offset >> 2; /* in dwords */
2525 r600_bytecode_add_output(ctx->bc, &output);
2526 }
2527
2528 ++ctx->gs_next_vertex;
2529 return 0;
2530 }
2531
2532
2533 static int r600_fetch_tess_io_info(struct r600_shader_ctx *ctx)
2534 {
2535 int r;
2536 struct r600_bytecode_vtx vtx;
2537 int temp_val = ctx->temp_reg;
2538 /* need to store the TCS output somewhere */
2539 r = single_alu_op2(ctx, ALU_OP1_MOV,
2540 temp_val, 0,
2541 V_SQ_ALU_SRC_LITERAL, 0,
2542 0, 0);
2543 if (r)
2544 return r;
2545
2546 /* used by VS/TCS */
2547 if (ctx->tess_input_info) {
2548 /* fetch tcs input values into resv space */
2549 memset(&vtx, 0, sizeof(struct r600_bytecode_vtx));
2550 vtx.op = FETCH_OP_VFETCH;
2551 vtx.buffer_id = R600_LDS_INFO_CONST_BUFFER;
2552 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
2553 vtx.mega_fetch_count = 16;
2554 vtx.data_format = FMT_32_32_32_32;
2555 vtx.num_format_all = 2;
2556 vtx.format_comp_all = 1;
2557 vtx.use_const_fields = 0;
2558 vtx.endian = r600_endian_swap(32);
2559 vtx.srf_mode_all = 1;
2560 vtx.offset = 0;
2561 vtx.dst_gpr = ctx->tess_input_info;
2562 vtx.dst_sel_x = 0;
2563 vtx.dst_sel_y = 1;
2564 vtx.dst_sel_z = 2;
2565 vtx.dst_sel_w = 3;
2566 vtx.src_gpr = temp_val;
2567 vtx.src_sel_x = 0;
2568
2569 r = r600_bytecode_add_vtx(ctx->bc, &vtx);
2570 if (r)
2571 return r;
2572 }
2573
2574 /* used by TCS/TES */
2575 if (ctx->tess_output_info) {
2576 /* fetch tcs output values into resv space */
2577 memset(&vtx, 0, sizeof(struct r600_bytecode_vtx));
2578 vtx.op = FETCH_OP_VFETCH;
2579 vtx.buffer_id = R600_LDS_INFO_CONST_BUFFER;
2580 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
2581 vtx.mega_fetch_count = 16;
2582 vtx.data_format = FMT_32_32_32_32;
2583 vtx.num_format_all = 2;
2584 vtx.format_comp_all = 1;
2585 vtx.use_const_fields = 0;
2586 vtx.endian = r600_endian_swap(32);
2587 vtx.srf_mode_all = 1;
2588 vtx.offset = 16;
2589 vtx.dst_gpr = ctx->tess_output_info;
2590 vtx.dst_sel_x = 0;
2591 vtx.dst_sel_y = 1;
2592 vtx.dst_sel_z = 2;
2593 vtx.dst_sel_w = 3;
2594 vtx.src_gpr = temp_val;
2595 vtx.src_sel_x = 0;
2596
2597 r = r600_bytecode_add_vtx(ctx->bc, &vtx);
2598 if (r)
2599 return r;
2600 }
2601 return 0;
2602 }
2603
2604 static int emit_lds_vs_writes(struct r600_shader_ctx *ctx)
2605 {
2606 int i, j, r;
2607 int temp_reg;
2608
2609 /* fetch tcs input values into input_vals */
2610 ctx->tess_input_info = r600_get_temp(ctx);
2611 ctx->tess_output_info = 0;
2612 r = r600_fetch_tess_io_info(ctx);
2613 if (r)
2614 return r;
2615
2616 temp_reg = r600_get_temp(ctx);
2617 /* dst reg contains LDS address stride * idx */
2618 /* MUL vertexID, vertex_dw_stride */
2619 r = single_alu_op2(ctx, ALU_OP2_MUL_UINT24,
2620 temp_reg, 0,
2621 ctx->tess_input_info, 1,
2622 0, 1); /* rel id in r0.y? */
2623 if (r)
2624 return r;
2625
2626 for (i = 0; i < ctx->shader->noutput; i++) {
2627 struct r600_bytecode_alu alu;
2628 int param = r600_get_lds_unique_index(ctx->shader->output[i].name, ctx->shader->output[i].sid);
2629
2630 if (param) {
2631 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
2632 temp_reg, 1,
2633 temp_reg, 0,
2634 V_SQ_ALU_SRC_LITERAL, param * 16);
2635 if (r)
2636 return r;
2637 }
2638
2639 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
2640 temp_reg, 2,
2641 temp_reg, param ? 1 : 0,
2642 V_SQ_ALU_SRC_LITERAL, 8);
2643 if (r)
2644 return r;
2645
2646
2647 for (j = 0; j < 2; j++) {
2648 int chan = (j == 1) ? 2 : (param ? 1 : 0);
2649 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
2650 alu.op = LDS_OP3_LDS_WRITE_REL;
2651 alu.src[0].sel = temp_reg;
2652 alu.src[0].chan = chan;
2653 alu.src[1].sel = ctx->shader->output[i].gpr;
2654 alu.src[1].chan = j * 2;
2655 alu.src[2].sel = ctx->shader->output[i].gpr;
2656 alu.src[2].chan = (j * 2) + 1;
2657 alu.last = 1;
2658 alu.dst.chan = 0;
2659 alu.lds_idx = 1;
2660 alu.is_lds_idx_op = true;
2661 r = r600_bytecode_add_alu(ctx->bc, &alu);
2662 if (r)
2663 return r;
2664 }
2665 }
2666 return 0;
2667 }
2668
2669 static int r600_store_tcs_output(struct r600_shader_ctx *ctx)
2670 {
2671 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
2672 const struct tgsi_full_dst_register *dst = &inst->Dst[0];
2673 int i, r, lasti;
2674 int temp_reg = r600_get_temp(ctx);
2675 struct r600_bytecode_alu alu;
2676 unsigned write_mask = dst->Register.WriteMask;
2677
2678 if (inst->Dst[0].Register.File != TGSI_FILE_OUTPUT)
2679 return 0;
2680
2681 r = get_lds_offset0(ctx, 1, temp_reg, dst->Register.Dimension ? false : true);
2682 if (r)
2683 return r;
2684
2685 /* the base address is now in temp.x */
2686 r = r600_get_byte_address(ctx, temp_reg,
2687 &inst->Dst[0], NULL, ctx->tess_output_info, 1);
2688 if (r)
2689 return r;
2690
2691 /* LDS write */
2692 lasti = tgsi_last_instruction(write_mask);
2693 for (i = 1; i <= lasti; i++) {
2694
2695 if (!(write_mask & (1 << i)))
2696 continue;
2697 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
2698 temp_reg, i,
2699 temp_reg, 0,
2700 V_SQ_ALU_SRC_LITERAL, 4 * i);
2701 if (r)
2702 return r;
2703 }
2704
2705 for (i = 0; i <= lasti; i++) {
2706 if (!(write_mask & (1 << i)))
2707 continue;
2708
2709 if ((i == 0 && ((write_mask & 3) == 3)) ||
2710 (i == 2 && ((write_mask & 0xc) == 0xc))) {
2711 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
2712 alu.op = LDS_OP3_LDS_WRITE_REL;
2713 alu.src[0].sel = temp_reg;
2714 alu.src[0].chan = i;
2715
2716 alu.src[1].sel = dst->Register.Index;
2717 alu.src[1].sel += ctx->file_offset[dst->Register.File];
2718 alu.src[1].chan = i;
2719
2720 alu.src[2].sel = dst->Register.Index;
2721 alu.src[2].sel += ctx->file_offset[dst->Register.File];
2722 alu.src[2].chan = i + 1;
2723 alu.lds_idx = 1;
2724 alu.dst.chan = 0;
2725 alu.last = 1;
2726 alu.is_lds_idx_op = true;
2727 r = r600_bytecode_add_alu(ctx->bc, &alu);
2728 if (r)
2729 return r;
2730 i += 1;
2731 continue;
2732 }
2733 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
2734 alu.op = LDS_OP2_LDS_WRITE;
2735 alu.src[0].sel = temp_reg;
2736 alu.src[0].chan = i;
2737
2738 alu.src[1].sel = dst->Register.Index;
2739 alu.src[1].sel += ctx->file_offset[dst->Register.File];
2740 alu.src[1].chan = i;
2741
2742 alu.src[2].sel = V_SQ_ALU_SRC_0;
2743 alu.dst.chan = 0;
2744 alu.last = 1;
2745 alu.is_lds_idx_op = true;
2746 r = r600_bytecode_add_alu(ctx->bc, &alu);
2747 if (r)
2748 return r;
2749 }
2750 return 0;
2751 }
2752
2753 static int r600_tess_factor_read(struct r600_shader_ctx *ctx,
2754 int output_idx)
2755 {
2756 int param;
2757 unsigned temp_reg = r600_get_temp(ctx);
2758 unsigned name = ctx->shader->output[output_idx].name;
2759 int dreg = ctx->shader->output[output_idx].gpr;
2760 int r;
2761
2762 param = r600_get_lds_unique_index(name, 0);
2763 r = get_lds_offset0(ctx, 1, temp_reg, true);
2764 if (r)
2765 return r;
2766
2767 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
2768 temp_reg, 0,
2769 temp_reg, 0,
2770 V_SQ_ALU_SRC_LITERAL, param * 16);
2771 if (r)
2772 return r;
2773
2774 do_lds_fetch_values(ctx, temp_reg, dreg);
2775 return 0;
2776 }
2777
2778 static int r600_emit_tess_factor(struct r600_shader_ctx *ctx)
2779 {
2780 unsigned i;
2781 int stride, outer_comps, inner_comps;
2782 int tessinner_idx = -1, tessouter_idx = -1;
2783 int r;
2784 int temp_reg = r600_get_temp(ctx);
2785 int treg[3] = {-1, -1, -1};
2786 struct r600_bytecode_alu alu;
2787 struct r600_bytecode_cf *cf_jump, *cf_pop;
2788
2789 /* only execute factor emission for invocation 0 */
2790 /* PRED_SETE_INT __, R0.x, 0 */
2791 memset(&alu, 0, sizeof(alu));
2792 alu.op = ALU_OP2_PRED_SETE_INT;
2793 alu.src[0].chan = 2;
2794 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2795 alu.execute_mask = 1;
2796 alu.update_pred = 1;
2797 alu.last = 1;
2798 r600_bytecode_add_alu_type(ctx->bc, &alu, CF_OP_ALU_PUSH_BEFORE);
2799
2800 r600_bytecode_add_cfinst(ctx->bc, CF_OP_JUMP);
2801 cf_jump = ctx->bc->cf_last;
2802
2803 treg[0] = r600_get_temp(ctx);
2804 switch (ctx->shader->tcs_prim_mode) {
2805 case PIPE_PRIM_LINES:
2806 stride = 8; /* 2 dwords, 1 vec2 store */
2807 outer_comps = 2;
2808 inner_comps = 0;
2809 break;
2810 case PIPE_PRIM_TRIANGLES:
2811 stride = 16; /* 4 dwords, 1 vec4 store */
2812 outer_comps = 3;
2813 inner_comps = 1;
2814 treg[1] = r600_get_temp(ctx);
2815 break;
2816 case PIPE_PRIM_QUADS:
2817 stride = 24; /* 6 dwords, 2 stores (vec4 + vec2) */
2818 outer_comps = 4;
2819 inner_comps = 2;
2820 treg[1] = r600_get_temp(ctx);
2821 treg[2] = r600_get_temp(ctx);
2822 break;
2823 default:
2824 assert(0);
2825 return -1;
2826 }
2827
2828 /* R0 is InvocationID, RelPatchID, PatchID, tf_base */
2829 /* TF_WRITE takes index in R.x, value in R.y */
2830 for (i = 0; i < ctx->shader->noutput; i++) {
2831 if (ctx->shader->output[i].name == TGSI_SEMANTIC_TESSINNER)
2832 tessinner_idx = i;
2833 if (ctx->shader->output[i].name == TGSI_SEMANTIC_TESSOUTER)
2834 tessouter_idx = i;
2835 }
2836
2837 if (tessouter_idx == -1)
2838 return -1;
2839
2840 if (tessinner_idx == -1 && inner_comps)
2841 return -1;
2842
2843 if (tessouter_idx != -1) {
2844 r = r600_tess_factor_read(ctx, tessouter_idx);
2845 if (r)
2846 return r;
2847 }
2848
2849 if (tessinner_idx != -1) {
2850 r = r600_tess_factor_read(ctx, tessinner_idx);
2851 if (r)
2852 return r;
2853 }
2854
2855 /* r.x = tf_base(r0.w) + relpatchid(r0.y) * tf_stride */
2856 /* r.x = relpatchid(r0.y) * tf_stride */
2857
2858 /* multiply incoming r0.y * stride - t.x = r0.y * stride */
2859 /* add incoming r0.w to it: t.x = t.x + r0.w */
2860 r = single_alu_op3(ctx, ALU_OP3_MULADD_UINT24,
2861 temp_reg, 0,
2862 0, 1,
2863 V_SQ_ALU_SRC_LITERAL, stride,
2864 0, 3);
2865 if (r)
2866 return r;
2867
2868 for (i = 0; i < outer_comps + inner_comps; i++) {
2869 int out_idx = i >= outer_comps ? tessinner_idx : tessouter_idx;
2870 int out_comp = i >= outer_comps ? i - outer_comps : i;
2871
2872 r = single_alu_op2(ctx, ALU_OP2_ADD_INT,
2873 treg[i / 2], (2 * (i % 2)),
2874 temp_reg, 0,
2875 V_SQ_ALU_SRC_LITERAL, 4 * i);
2876 if (r)
2877 return r;
2878 r = single_alu_op2(ctx, ALU_OP1_MOV,
2879 treg[i / 2], 1 + (2 * (i%2)),
2880 ctx->shader->output[out_idx].gpr, out_comp,
2881 0, 0);
2882 if (r)
2883 return r;
2884 }
2885 for (i = 0; i < outer_comps + inner_comps; i++) {
2886 struct r600_bytecode_gds gds;
2887
2888 memset(&gds, 0, sizeof(struct r600_bytecode_gds));
2889 gds.src_gpr = treg[i / 2];
2890 gds.src_sel_x = 2 * (i % 2);
2891 gds.src_sel_y = 1 + (2 * (i % 2));
2892 gds.src_sel_z = 4;
2893 gds.dst_sel_x = 7;
2894 gds.dst_sel_y = 7;
2895 gds.dst_sel_z = 7;
2896 gds.dst_sel_w = 7;
2897 gds.op = FETCH_OP_TF_WRITE;
2898 r = r600_bytecode_add_gds(ctx->bc, &gds);
2899 if (r)
2900 return r;
2901 }
2902
2903 // Patch up jump label
2904 r600_bytecode_add_cfinst(ctx->bc, CF_OP_POP);
2905 cf_pop = ctx->bc->cf_last;
2906
2907 cf_jump->cf_addr = cf_pop->id + 2;
2908 cf_jump->pop_count = 1;
2909 cf_pop->cf_addr = cf_pop->id + 2;
2910 cf_pop->pop_count = 1;
2911
2912 return 0;
2913 }
2914
2915 static int r600_shader_from_tgsi(struct r600_context *rctx,
2916 struct r600_pipe_shader *pipeshader,
2917 union r600_shader_key key)
2918 {
2919 struct r600_screen *rscreen = rctx->screen;
2920 struct r600_shader *shader = &pipeshader->shader;
2921 struct tgsi_token *tokens = pipeshader->selector->tokens;
2922 struct pipe_stream_output_info so = pipeshader->selector->so;
2923 struct tgsi_full_immediate *immediate;
2924 struct r600_shader_ctx ctx;
2925 struct r600_bytecode_output output[ARRAY_SIZE(shader->output)];
2926 unsigned output_done, noutput;
2927 unsigned opcode;
2928 int i, j, k, r = 0;
2929 int next_param_base = 0, next_clip_base;
2930 int max_color_exports = MAX2(key.ps.nr_cbufs, 1);
2931 bool indirect_gprs;
2932 bool ring_outputs = false;
2933 bool lds_outputs = false;
2934 bool lds_inputs = false;
2935 bool pos_emitted = false;
2936
2937 ctx.bc = &shader->bc;
2938 ctx.shader = shader;
2939 ctx.native_integers = true;
2940
2941 r600_bytecode_init(ctx.bc, rscreen->b.chip_class, rscreen->b.family,
2942 rscreen->has_compressed_msaa_texturing);
2943 ctx.tokens = tokens;
2944 tgsi_scan_shader(tokens, &ctx.info);
2945 shader->indirect_files = ctx.info.indirect_files;
2946
2947 shader->uses_doubles = ctx.info.uses_doubles;
2948
2949 indirect_gprs = ctx.info.indirect_files & ~((1 << TGSI_FILE_CONSTANT) | (1 << TGSI_FILE_SAMPLER));
2950 tgsi_parse_init(&ctx.parse, tokens);
2951 ctx.type = ctx.info.processor;
2952 shader->processor_type = ctx.type;
2953 ctx.bc->type = shader->processor_type;
2954
2955 switch (ctx.type) {
2956 case PIPE_SHADER_VERTEX:
2957 shader->vs_as_gs_a = key.vs.as_gs_a;
2958 shader->vs_as_es = key.vs.as_es;
2959 shader->vs_as_ls = key.vs.as_ls;
2960 if (shader->vs_as_es)
2961 ring_outputs = true;
2962 if (shader->vs_as_ls)
2963 lds_outputs = true;
2964 break;
2965 case PIPE_SHADER_GEOMETRY:
2966 ring_outputs = true;
2967 break;
2968 case PIPE_SHADER_TESS_CTRL:
2969 shader->tcs_prim_mode = key.tcs.prim_mode;
2970 lds_outputs = true;
2971 lds_inputs = true;
2972 break;
2973 case PIPE_SHADER_TESS_EVAL:
2974 shader->tes_as_es = key.tes.as_es;
2975 lds_inputs = true;
2976 if (shader->tes_as_es)
2977 ring_outputs = true;
2978 break;
2979 case PIPE_SHADER_FRAGMENT:
2980 shader->two_side = key.ps.color_two_side;
2981 break;
2982 default:
2983 break;
2984 }
2985
2986 if (shader->vs_as_es || shader->tes_as_es) {
2987 ctx.gs_for_vs = &rctx->gs_shader->current->shader;
2988 } else {
2989 ctx.gs_for_vs = NULL;
2990 }
2991
2992 ctx.next_ring_offset = 0;
2993 ctx.gs_out_ring_offset = 0;
2994 ctx.gs_next_vertex = 0;
2995 ctx.gs_stream_output_info = &so;
2996
2997 ctx.face_gpr = -1;
2998 ctx.fixed_pt_position_gpr = -1;
2999 ctx.fragcoord_input = -1;
3000 ctx.colors_used = 0;
3001 ctx.clip_vertex_write = 0;
3002
3003 shader->nr_ps_color_exports = 0;
3004 shader->nr_ps_max_color_exports = 0;
3005
3006
3007 /* register allocations */
3008 /* Values [0,127] correspond to GPR[0..127].
3009 * Values [128,159] correspond to constant buffer bank 0
3010 * Values [160,191] correspond to constant buffer bank 1
3011 * Values [256,511] correspond to cfile constants c[0..255]. (Gone on EG)
3012 * Values [256,287] correspond to constant buffer bank 2 (EG)
3013 * Values [288,319] correspond to constant buffer bank 3 (EG)
3014 * Other special values are shown in the list below.
3015 * 244 ALU_SRC_1_DBL_L: special constant 1.0 double-float, LSW. (RV670+)
3016 * 245 ALU_SRC_1_DBL_M: special constant 1.0 double-float, MSW. (RV670+)
3017 * 246 ALU_SRC_0_5_DBL_L: special constant 0.5 double-float, LSW. (RV670+)
3018 * 247 ALU_SRC_0_5_DBL_M: special constant 0.5 double-float, MSW. (RV670+)
3019 * 248 SQ_ALU_SRC_0: special constant 0.0.
3020 * 249 SQ_ALU_SRC_1: special constant 1.0 float.
3021 * 250 SQ_ALU_SRC_1_INT: special constant 1 integer.
3022 * 251 SQ_ALU_SRC_M_1_INT: special constant -1 integer.
3023 * 252 SQ_ALU_SRC_0_5: special constant 0.5 float.
3024 * 253 SQ_ALU_SRC_LITERAL: literal constant.
3025 * 254 SQ_ALU_SRC_PV: previous vector result.
3026 * 255 SQ_ALU_SRC_PS: previous scalar result.
3027 */
3028 for (i = 0; i < TGSI_FILE_COUNT; i++) {
3029 ctx.file_offset[i] = 0;
3030 }
3031
3032 if (ctx.type == PIPE_SHADER_VERTEX) {
3033 ctx.file_offset[TGSI_FILE_INPUT] = 1;
3034 r600_bytecode_add_cfinst(ctx.bc, CF_OP_CALL_FS);
3035 }
3036 if (ctx.type == PIPE_SHADER_FRAGMENT) {
3037 if (ctx.bc->chip_class >= EVERGREEN)
3038 ctx.file_offset[TGSI_FILE_INPUT] = evergreen_gpr_count(&ctx);
3039 else
3040 ctx.file_offset[TGSI_FILE_INPUT] = allocate_system_value_inputs(&ctx, ctx.file_offset[TGSI_FILE_INPUT]);
3041 }
3042 if (ctx.type == PIPE_SHADER_GEOMETRY) {
3043 /* FIXME 1 would be enough in some cases (3 or less input vertices) */
3044 ctx.file_offset[TGSI_FILE_INPUT] = 2;
3045 }
3046 if (ctx.type == PIPE_SHADER_TESS_CTRL)
3047 ctx.file_offset[TGSI_FILE_INPUT] = 1;
3048 if (ctx.type == PIPE_SHADER_TESS_EVAL) {
3049 bool add_tesscoord = false, add_tess_inout = false;
3050 ctx.file_offset[TGSI_FILE_INPUT] = 1;
3051 for (i = 0; i < PIPE_MAX_SHADER_INPUTS; i++) {
3052 /* if we have tesscoord save one reg */
3053 if (ctx.info.system_value_semantic_name[i] == TGSI_SEMANTIC_TESSCOORD)
3054 add_tesscoord = true;
3055 if (ctx.info.system_value_semantic_name[i] == TGSI_SEMANTIC_TESSINNER ||
3056 ctx.info.system_value_semantic_name[i] == TGSI_SEMANTIC_TESSOUTER)
3057 add_tess_inout = true;
3058 }
3059 if (add_tesscoord || add_tess_inout)
3060 ctx.file_offset[TGSI_FILE_INPUT]++;
3061 if (add_tess_inout)
3062 ctx.file_offset[TGSI_FILE_INPUT]+=2;
3063 }
3064
3065 ctx.file_offset[TGSI_FILE_OUTPUT] =
3066 ctx.file_offset[TGSI_FILE_INPUT] +
3067 ctx.info.file_max[TGSI_FILE_INPUT] + 1;
3068 ctx.file_offset[TGSI_FILE_TEMPORARY] = ctx.file_offset[TGSI_FILE_OUTPUT] +
3069 ctx.info.file_max[TGSI_FILE_OUTPUT] + 1;
3070
3071 /* Outside the GPR range. This will be translated to one of the
3072 * kcache banks later. */
3073 ctx.file_offset[TGSI_FILE_CONSTANT] = 512;
3074
3075 ctx.file_offset[TGSI_FILE_IMMEDIATE] = V_SQ_ALU_SRC_LITERAL;
3076 ctx.bc->ar_reg = ctx.file_offset[TGSI_FILE_TEMPORARY] +
3077 ctx.info.file_max[TGSI_FILE_TEMPORARY] + 1;
3078 ctx.bc->index_reg[0] = ctx.bc->ar_reg + 1;
3079 ctx.bc->index_reg[1] = ctx.bc->ar_reg + 2;
3080
3081 if (ctx.type == PIPE_SHADER_TESS_CTRL) {
3082 ctx.tess_input_info = ctx.bc->ar_reg + 3;
3083 ctx.tess_output_info = ctx.bc->ar_reg + 4;
3084 ctx.temp_reg = ctx.bc->ar_reg + 5;
3085 } else if (ctx.type == PIPE_SHADER_TESS_EVAL) {
3086 ctx.tess_input_info = 0;
3087 ctx.tess_output_info = ctx.bc->ar_reg + 3;
3088 ctx.temp_reg = ctx.bc->ar_reg + 4;
3089 } else if (ctx.type == PIPE_SHADER_GEOMETRY) {
3090 ctx.gs_export_gpr_tregs[0] = ctx.bc->ar_reg + 3;
3091 ctx.gs_export_gpr_tregs[1] = ctx.bc->ar_reg + 4;
3092 ctx.gs_export_gpr_tregs[2] = ctx.bc->ar_reg + 5;
3093 ctx.gs_export_gpr_tregs[3] = ctx.bc->ar_reg + 6;
3094 ctx.temp_reg = ctx.bc->ar_reg + 7;
3095 } else {
3096 ctx.temp_reg = ctx.bc->ar_reg + 3;
3097 }
3098
3099 shader->max_arrays = 0;
3100 shader->num_arrays = 0;
3101 if (indirect_gprs) {
3102
3103 if (ctx.info.indirect_files & (1 << TGSI_FILE_INPUT)) {
3104 r600_add_gpr_array(shader, ctx.file_offset[TGSI_FILE_INPUT],
3105 ctx.file_offset[TGSI_FILE_OUTPUT] -
3106 ctx.file_offset[TGSI_FILE_INPUT],
3107 0x0F);
3108 }
3109 if (ctx.info.indirect_files & (1 << TGSI_FILE_OUTPUT)) {
3110 r600_add_gpr_array(shader, ctx.file_offset[TGSI_FILE_OUTPUT],
3111 ctx.file_offset[TGSI_FILE_TEMPORARY] -
3112 ctx.file_offset[TGSI_FILE_OUTPUT],
3113 0x0F);
3114 }
3115 }
3116
3117 ctx.nliterals = 0;
3118 ctx.literals = NULL;
3119
3120 shader->fs_write_all = ctx.info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
3121 ctx.info.colors_written == 1;
3122 shader->vs_position_window_space = ctx.info.properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
3123 shader->ps_conservative_z = (uint8_t)ctx.info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT];
3124
3125 if (shader->vs_as_gs_a)
3126 vs_add_primid_output(&ctx, key.vs.prim_id_out);
3127
3128 if (ctx.type == PIPE_SHADER_TESS_EVAL)
3129 r600_fetch_tess_io_info(&ctx);
3130
3131 while (!tgsi_parse_end_of_tokens(&ctx.parse)) {
3132 tgsi_parse_token(&ctx.parse);
3133 switch (ctx.parse.FullToken.Token.Type) {
3134 case TGSI_TOKEN_TYPE_IMMEDIATE:
3135 immediate = &ctx.parse.FullToken.FullImmediate;
3136 ctx.literals = realloc(ctx.literals, (ctx.nliterals + 1) * 16);
3137 if(ctx.literals == NULL) {
3138 r = -ENOMEM;
3139 goto out_err;
3140 }
3141 ctx.literals[ctx.nliterals * 4 + 0] = immediate->u[0].Uint;
3142 ctx.literals[ctx.nliterals * 4 + 1] = immediate->u[1].Uint;
3143 ctx.literals[ctx.nliterals * 4 + 2] = immediate->u[2].Uint;
3144 ctx.literals[ctx.nliterals * 4 + 3] = immediate->u[3].Uint;
3145 ctx.nliterals++;
3146 break;
3147 case TGSI_TOKEN_TYPE_DECLARATION:
3148 r = tgsi_declaration(&ctx);
3149 if (r)
3150 goto out_err;
3151 break;
3152 case TGSI_TOKEN_TYPE_INSTRUCTION:
3153 case TGSI_TOKEN_TYPE_PROPERTY:
3154 break;
3155 default:
3156 R600_ERR("unsupported token type %d\n", ctx.parse.FullToken.Token.Type);
3157 r = -EINVAL;
3158 goto out_err;
3159 }
3160 }
3161
3162 shader->ring_item_sizes[0] = ctx.next_ring_offset;
3163 shader->ring_item_sizes[1] = 0;
3164 shader->ring_item_sizes[2] = 0;
3165 shader->ring_item_sizes[3] = 0;
3166
3167 /* Process two side if needed */
3168 if (shader->two_side && ctx.colors_used) {
3169 int i, count = ctx.shader->ninput;
3170 unsigned next_lds_loc = ctx.shader->nlds;
3171
3172 /* additional inputs will be allocated right after the existing inputs,
3173 * we won't need them after the color selection, so we don't need to
3174 * reserve these gprs for the rest of the shader code and to adjust
3175 * output offsets etc. */
3176 int gpr = ctx.file_offset[TGSI_FILE_INPUT] +
3177 ctx.info.file_max[TGSI_FILE_INPUT] + 1;
3178
3179 /* if two sided and neither face or sample mask is used by shader, ensure face_gpr is emitted */
3180 if (ctx.face_gpr == -1) {
3181 i = ctx.shader->ninput++;
3182 ctx.shader->input[i].name = TGSI_SEMANTIC_FACE;
3183 ctx.shader->input[i].spi_sid = 0;
3184 ctx.shader->input[i].gpr = gpr++;
3185 ctx.face_gpr = ctx.shader->input[i].gpr;
3186 }
3187
3188 for (i = 0; i < count; i++) {
3189 if (ctx.shader->input[i].name == TGSI_SEMANTIC_COLOR) {
3190 int ni = ctx.shader->ninput++;
3191 memcpy(&ctx.shader->input[ni],&ctx.shader->input[i], sizeof(struct r600_shader_io));
3192 ctx.shader->input[ni].name = TGSI_SEMANTIC_BCOLOR;
3193 ctx.shader->input[ni].spi_sid = r600_spi_sid(&ctx.shader->input[ni]);
3194 ctx.shader->input[ni].gpr = gpr++;
3195 // TGSI to LLVM needs to know the lds position of inputs.
3196 // Non LLVM path computes it later (in process_twoside_color)
3197 ctx.shader->input[ni].lds_pos = next_lds_loc++;
3198 ctx.shader->input[i].back_color_input = ni;
3199 if (ctx.bc->chip_class >= EVERGREEN) {
3200 if ((r = evergreen_interp_input(&ctx, ni)))
3201 return r;
3202 }
3203 }
3204 }
3205 }
3206
3207 if (shader->fs_write_all && rscreen->b.chip_class >= EVERGREEN)
3208 shader->nr_ps_max_color_exports = 8;
3209
3210 if (ctx.fragcoord_input >= 0) {
3211 if (ctx.bc->chip_class == CAYMAN) {
3212 for (j = 0 ; j < 4; j++) {
3213 struct r600_bytecode_alu alu;
3214 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3215 alu.op = ALU_OP1_RECIP_IEEE;
3216 alu.src[0].sel = shader->input[ctx.fragcoord_input].gpr;
3217 alu.src[0].chan = 3;
3218
3219 alu.dst.sel = shader->input[ctx.fragcoord_input].gpr;
3220 alu.dst.chan = j;
3221 alu.dst.write = (j == 3);
3222 alu.last = 1;
3223 if ((r = r600_bytecode_add_alu(ctx.bc, &alu)))
3224 return r;
3225 }
3226 } else {
3227 struct r600_bytecode_alu alu;
3228 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3229 alu.op = ALU_OP1_RECIP_IEEE;
3230 alu.src[0].sel = shader->input[ctx.fragcoord_input].gpr;
3231 alu.src[0].chan = 3;
3232
3233 alu.dst.sel = shader->input[ctx.fragcoord_input].gpr;
3234 alu.dst.chan = 3;
3235 alu.dst.write = 1;
3236 alu.last = 1;
3237 if ((r = r600_bytecode_add_alu(ctx.bc, &alu)))
3238 return r;
3239 }
3240 }
3241
3242 if (ctx.type == PIPE_SHADER_GEOMETRY) {
3243 struct r600_bytecode_alu alu;
3244 int r;
3245
3246 /* GS thread with no output workaround - emit a cut at start of GS */
3247 if (ctx.bc->chip_class == R600)
3248 r600_bytecode_add_cfinst(ctx.bc, CF_OP_CUT_VERTEX);
3249
3250 for (j = 0; j < 4; j++) {
3251 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3252 alu.op = ALU_OP1_MOV;
3253 alu.src[0].sel = V_SQ_ALU_SRC_LITERAL;
3254 alu.src[0].value = 0;
3255 alu.dst.sel = ctx.gs_export_gpr_tregs[j];
3256 alu.dst.write = 1;
3257 alu.last = 1;
3258 r = r600_bytecode_add_alu(ctx.bc, &alu);
3259 if (r)
3260 return r;
3261 }
3262 }
3263
3264 if (ctx.type == PIPE_SHADER_TESS_CTRL)
3265 r600_fetch_tess_io_info(&ctx);
3266
3267 if (shader->two_side && ctx.colors_used) {
3268 if ((r = process_twoside_color_inputs(&ctx)))
3269 return r;
3270 }
3271
3272 tgsi_parse_init(&ctx.parse, tokens);
3273 while (!tgsi_parse_end_of_tokens(&ctx.parse)) {
3274 tgsi_parse_token(&ctx.parse);
3275 switch (ctx.parse.FullToken.Token.Type) {
3276 case TGSI_TOKEN_TYPE_INSTRUCTION:
3277 r = tgsi_is_supported(&ctx);
3278 if (r)
3279 goto out_err;
3280 ctx.max_driver_temp_used = 0;
3281 /* reserve first tmp for everyone */
3282 r600_get_temp(&ctx);
3283
3284 opcode = ctx.parse.FullToken.FullInstruction.Instruction.Opcode;
3285 if ((r = tgsi_split_constant(&ctx)))
3286 goto out_err;
3287 if ((r = tgsi_split_literal_constant(&ctx)))
3288 goto out_err;
3289 if (ctx.type == PIPE_SHADER_GEOMETRY) {
3290 if ((r = tgsi_split_gs_inputs(&ctx)))
3291 goto out_err;
3292 } else if (lds_inputs) {
3293 if ((r = tgsi_split_lds_inputs(&ctx)))
3294 goto out_err;
3295 }
3296 if (ctx.bc->chip_class == CAYMAN)
3297 ctx.inst_info = &cm_shader_tgsi_instruction[opcode];
3298 else if (ctx.bc->chip_class >= EVERGREEN)
3299 ctx.inst_info = &eg_shader_tgsi_instruction[opcode];
3300 else
3301 ctx.inst_info = &r600_shader_tgsi_instruction[opcode];
3302 r = ctx.inst_info->process(&ctx);
3303 if (r)
3304 goto out_err;
3305
3306 if (ctx.type == PIPE_SHADER_TESS_CTRL) {
3307 r = r600_store_tcs_output(&ctx);
3308 if (r)
3309 goto out_err;
3310 }
3311 break;
3312 default:
3313 break;
3314 }
3315 }
3316
3317 /* Reset the temporary register counter. */
3318 ctx.max_driver_temp_used = 0;
3319
3320 noutput = shader->noutput;
3321
3322 if (!ring_outputs && ctx.clip_vertex_write) {
3323 unsigned clipdist_temp[2];
3324
3325 clipdist_temp[0] = r600_get_temp(&ctx);
3326 clipdist_temp[1] = r600_get_temp(&ctx);
3327
3328 /* need to convert a clipvertex write into clipdistance writes and not export
3329 the clip vertex anymore */
3330
3331 memset(&shader->output[noutput], 0, 2*sizeof(struct r600_shader_io));
3332 shader->output[noutput].name = TGSI_SEMANTIC_CLIPDIST;
3333 shader->output[noutput].gpr = clipdist_temp[0];
3334 noutput++;
3335 shader->output[noutput].name = TGSI_SEMANTIC_CLIPDIST;
3336 shader->output[noutput].gpr = clipdist_temp[1];
3337 noutput++;
3338
3339 /* reset spi_sid for clipvertex output to avoid confusing spi */
3340 shader->output[ctx.cv_output].spi_sid = 0;
3341
3342 shader->clip_dist_write = 0xFF;
3343
3344 for (i = 0; i < 8; i++) {
3345 int oreg = i >> 2;
3346 int ochan = i & 3;
3347
3348 for (j = 0; j < 4; j++) {
3349 struct r600_bytecode_alu alu;
3350 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3351 alu.op = ALU_OP2_DOT4;
3352 alu.src[0].sel = shader->output[ctx.cv_output].gpr;
3353 alu.src[0].chan = j;
3354
3355 alu.src[1].sel = 512 + i;
3356 alu.src[1].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
3357 alu.src[1].chan = j;
3358
3359 alu.dst.sel = clipdist_temp[oreg];
3360 alu.dst.chan = j;
3361 alu.dst.write = (j == ochan);
3362 if (j == 3)
3363 alu.last = 1;
3364 r = r600_bytecode_add_alu(ctx.bc, &alu);
3365 if (r)
3366 return r;
3367 }
3368 }
3369 }
3370
3371 /* Add stream outputs. */
3372 if (so.num_outputs) {
3373 bool emit = false;
3374 if (!lds_outputs && !ring_outputs && ctx.type == PIPE_SHADER_VERTEX)
3375 emit = true;
3376 if (!ring_outputs && ctx.type == PIPE_SHADER_TESS_EVAL)
3377 emit = true;
3378 if (emit)
3379 emit_streamout(&ctx, &so, -1, NULL);
3380 }
3381 pipeshader->enabled_stream_buffers_mask = ctx.enabled_stream_buffers_mask;
3382 convert_edgeflag_to_int(&ctx);
3383
3384 if (ctx.type == PIPE_SHADER_TESS_CTRL)
3385 r600_emit_tess_factor(&ctx);
3386
3387 if (lds_outputs) {
3388 if (ctx.type == PIPE_SHADER_VERTEX) {
3389 if (ctx.shader->noutput)
3390 emit_lds_vs_writes(&ctx);
3391 }
3392 } else if (ring_outputs) {
3393 if (shader->vs_as_es || shader->tes_as_es) {
3394 ctx.gs_export_gpr_tregs[0] = r600_get_temp(&ctx);
3395 ctx.gs_export_gpr_tregs[1] = -1;
3396 ctx.gs_export_gpr_tregs[2] = -1;
3397 ctx.gs_export_gpr_tregs[3] = -1;
3398
3399 emit_gs_ring_writes(&ctx, &so, -1, FALSE);
3400 }
3401 } else {
3402 /* Export output */
3403 next_clip_base = shader->vs_out_misc_write ? 62 : 61;
3404
3405 for (i = 0, j = 0; i < noutput; i++, j++) {
3406 memset(&output[j], 0, sizeof(struct r600_bytecode_output));
3407 output[j].gpr = shader->output[i].gpr;
3408 output[j].elem_size = 3;
3409 output[j].swizzle_x = 0;
3410 output[j].swizzle_y = 1;
3411 output[j].swizzle_z = 2;
3412 output[j].swizzle_w = 3;
3413 output[j].burst_count = 1;
3414 output[j].type = -1;
3415 output[j].op = CF_OP_EXPORT;
3416 switch (ctx.type) {
3417 case PIPE_SHADER_VERTEX:
3418 case PIPE_SHADER_TESS_EVAL:
3419 switch (shader->output[i].name) {
3420 case TGSI_SEMANTIC_POSITION:
3421 output[j].array_base = 60;
3422 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3423 pos_emitted = true;
3424 break;
3425
3426 case TGSI_SEMANTIC_PSIZE:
3427 output[j].array_base = 61;
3428 output[j].swizzle_y = 7;
3429 output[j].swizzle_z = 7;
3430 output[j].swizzle_w = 7;
3431 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3432 pos_emitted = true;
3433 break;
3434 case TGSI_SEMANTIC_EDGEFLAG:
3435 output[j].array_base = 61;
3436 output[j].swizzle_x = 7;
3437 output[j].swizzle_y = 0;
3438 output[j].swizzle_z = 7;
3439 output[j].swizzle_w = 7;
3440 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3441 pos_emitted = true;
3442 break;
3443 case TGSI_SEMANTIC_LAYER:
3444 /* spi_sid is 0 for outputs that are
3445 * not consumed by PS */
3446 if (shader->output[i].spi_sid) {
3447 output[j].array_base = next_param_base++;
3448 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
3449 j++;
3450 memcpy(&output[j], &output[j-1], sizeof(struct r600_bytecode_output));
3451 }
3452 output[j].array_base = 61;
3453 output[j].swizzle_x = 7;
3454 output[j].swizzle_y = 7;
3455 output[j].swizzle_z = 0;
3456 output[j].swizzle_w = 7;
3457 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3458 pos_emitted = true;
3459 break;
3460 case TGSI_SEMANTIC_VIEWPORT_INDEX:
3461 /* spi_sid is 0 for outputs that are
3462 * not consumed by PS */
3463 if (shader->output[i].spi_sid) {
3464 output[j].array_base = next_param_base++;
3465 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
3466 j++;
3467 memcpy(&output[j], &output[j-1], sizeof(struct r600_bytecode_output));
3468 }
3469 output[j].array_base = 61;
3470 output[j].swizzle_x = 7;
3471 output[j].swizzle_y = 7;
3472 output[j].swizzle_z = 7;
3473 output[j].swizzle_w = 0;
3474 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3475 pos_emitted = true;
3476 break;
3477 case TGSI_SEMANTIC_CLIPVERTEX:
3478 j--;
3479 break;
3480 case TGSI_SEMANTIC_CLIPDIST:
3481 output[j].array_base = next_clip_base++;
3482 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3483 pos_emitted = true;
3484 /* spi_sid is 0 for clipdistance outputs that were generated
3485 * for clipvertex - we don't need to pass them to PS */
3486 if (shader->output[i].spi_sid) {
3487 j++;
3488 /* duplicate it as PARAM to pass to the pixel shader */
3489 memcpy(&output[j], &output[j-1], sizeof(struct r600_bytecode_output));
3490 output[j].array_base = next_param_base++;
3491 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
3492 }
3493 break;
3494 case TGSI_SEMANTIC_FOG:
3495 output[j].swizzle_y = 4; /* 0 */
3496 output[j].swizzle_z = 4; /* 0 */
3497 output[j].swizzle_w = 5; /* 1 */
3498 break;
3499 case TGSI_SEMANTIC_PRIMID:
3500 output[j].swizzle_x = 2;
3501 output[j].swizzle_y = 4; /* 0 */
3502 output[j].swizzle_z = 4; /* 0 */
3503 output[j].swizzle_w = 4; /* 0 */
3504 break;
3505 }
3506
3507 break;
3508 case PIPE_SHADER_FRAGMENT:
3509 if (shader->output[i].name == TGSI_SEMANTIC_COLOR) {
3510 /* never export more colors than the number of CBs */
3511 if (shader->output[i].sid >= max_color_exports) {
3512 /* skip export */
3513 j--;
3514 continue;
3515 }
3516 output[j].swizzle_w = key.ps.alpha_to_one ? 5 : 3;
3517 output[j].array_base = shader->output[i].sid;
3518 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
3519 shader->nr_ps_color_exports++;
3520 if (shader->fs_write_all && (rscreen->b.chip_class >= EVERGREEN)) {
3521 for (k = 1; k < max_color_exports; k++) {
3522 j++;
3523 memset(&output[j], 0, sizeof(struct r600_bytecode_output));
3524 output[j].gpr = shader->output[i].gpr;
3525 output[j].elem_size = 3;
3526 output[j].swizzle_x = 0;
3527 output[j].swizzle_y = 1;
3528 output[j].swizzle_z = 2;
3529 output[j].swizzle_w = key.ps.alpha_to_one ? 5 : 3;
3530 output[j].burst_count = 1;
3531 output[j].array_base = k;
3532 output[j].op = CF_OP_EXPORT;
3533 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
3534 shader->nr_ps_color_exports++;
3535 }
3536 }
3537 } else if (shader->output[i].name == TGSI_SEMANTIC_POSITION) {
3538 output[j].array_base = 61;
3539 output[j].swizzle_x = 2;
3540 output[j].swizzle_y = 7;
3541 output[j].swizzle_z = output[j].swizzle_w = 7;
3542 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
3543 } else if (shader->output[i].name == TGSI_SEMANTIC_STENCIL) {
3544 output[j].array_base = 61;
3545 output[j].swizzle_x = 7;
3546 output[j].swizzle_y = 1;
3547 output[j].swizzle_z = output[j].swizzle_w = 7;
3548 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
3549 } else if (shader->output[i].name == TGSI_SEMANTIC_SAMPLEMASK) {
3550 output[j].array_base = 61;
3551 output[j].swizzle_x = 7;
3552 output[j].swizzle_y = 7;
3553 output[j].swizzle_z = 0;
3554 output[j].swizzle_w = 7;
3555 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
3556 } else {
3557 R600_ERR("unsupported fragment output name %d\n", shader->output[i].name);
3558 r = -EINVAL;
3559 goto out_err;
3560 }
3561 break;
3562 case PIPE_SHADER_TESS_CTRL:
3563 break;
3564 default:
3565 R600_ERR("unsupported processor type %d\n", ctx.type);
3566 r = -EINVAL;
3567 goto out_err;
3568 }
3569
3570 if (output[j].type==-1) {
3571 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
3572 output[j].array_base = next_param_base++;
3573 }
3574 }
3575
3576 /* add fake position export */
3577 if ((ctx.type == PIPE_SHADER_VERTEX || ctx.type == PIPE_SHADER_TESS_EVAL) && pos_emitted == false) {
3578 memset(&output[j], 0, sizeof(struct r600_bytecode_output));
3579 output[j].gpr = 0;
3580 output[j].elem_size = 3;
3581 output[j].swizzle_x = 7;
3582 output[j].swizzle_y = 7;
3583 output[j].swizzle_z = 7;
3584 output[j].swizzle_w = 7;
3585 output[j].burst_count = 1;
3586 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_POS;
3587 output[j].array_base = 60;
3588 output[j].op = CF_OP_EXPORT;
3589 j++;
3590 }
3591
3592 /* add fake param output for vertex shader if no param is exported */
3593 if ((ctx.type == PIPE_SHADER_VERTEX || ctx.type == PIPE_SHADER_TESS_EVAL) && next_param_base == 0) {
3594 memset(&output[j], 0, sizeof(struct r600_bytecode_output));
3595 output[j].gpr = 0;
3596 output[j].elem_size = 3;
3597 output[j].swizzle_x = 7;
3598 output[j].swizzle_y = 7;
3599 output[j].swizzle_z = 7;
3600 output[j].swizzle_w = 7;
3601 output[j].burst_count = 1;
3602 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PARAM;
3603 output[j].array_base = 0;
3604 output[j].op = CF_OP_EXPORT;
3605 j++;
3606 }
3607
3608 /* add fake pixel export */
3609 if (ctx.type == PIPE_SHADER_FRAGMENT && shader->nr_ps_color_exports == 0) {
3610 memset(&output[j], 0, sizeof(struct r600_bytecode_output));
3611 output[j].gpr = 0;
3612 output[j].elem_size = 3;
3613 output[j].swizzle_x = 7;
3614 output[j].swizzle_y = 7;
3615 output[j].swizzle_z = 7;
3616 output[j].swizzle_w = 7;
3617 output[j].burst_count = 1;
3618 output[j].type = V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_PIXEL;
3619 output[j].array_base = 0;
3620 output[j].op = CF_OP_EXPORT;
3621 j++;
3622 shader->nr_ps_color_exports++;
3623 }
3624
3625 noutput = j;
3626
3627 /* set export done on last export of each type */
3628 for (i = noutput - 1, output_done = 0; i >= 0; i--) {
3629 if (!(output_done & (1 << output[i].type))) {
3630 output_done |= (1 << output[i].type);
3631 output[i].op = CF_OP_EXPORT_DONE;
3632 }
3633 }
3634 /* add output to bytecode */
3635 for (i = 0; i < noutput; i++) {
3636 r = r600_bytecode_add_output(ctx.bc, &output[i]);
3637 if (r)
3638 goto out_err;
3639 }
3640 }
3641
3642 /* add program end */
3643 if (ctx.bc->chip_class == CAYMAN)
3644 cm_bytecode_add_cf_end(ctx.bc);
3645 else {
3646 const struct cf_op_info *last = NULL;
3647
3648 if (ctx.bc->cf_last)
3649 last = r600_isa_cf(ctx.bc->cf_last->op);
3650
3651 /* alu clause instructions don't have EOP bit, so add NOP */
3652 if (!last || last->flags & CF_ALU || ctx.bc->cf_last->op == CF_OP_LOOP_END || ctx.bc->cf_last->op == CF_OP_CALL_FS || ctx.bc->cf_last->op == CF_OP_POP || ctx.bc->cf_last->op == CF_OP_GDS)
3653 r600_bytecode_add_cfinst(ctx.bc, CF_OP_NOP);
3654
3655 ctx.bc->cf_last->end_of_program = 1;
3656 }
3657
3658 /* check GPR limit - we have 124 = 128 - 4
3659 * (4 are reserved as alu clause temporary registers) */
3660 if (ctx.bc->ngpr > 124) {
3661 R600_ERR("GPR limit exceeded - shader requires %d registers\n", ctx.bc->ngpr);
3662 r = -ENOMEM;
3663 goto out_err;
3664 }
3665
3666 if (ctx.type == PIPE_SHADER_GEOMETRY) {
3667 if ((r = generate_gs_copy_shader(rctx, pipeshader, &so)))
3668 return r;
3669 }
3670
3671 free(ctx.literals);
3672 tgsi_parse_free(&ctx.parse);
3673 return 0;
3674 out_err:
3675 free(ctx.literals);
3676 tgsi_parse_free(&ctx.parse);
3677 return r;
3678 }
3679
3680 static int tgsi_unsupported(struct r600_shader_ctx *ctx)
3681 {
3682 const unsigned tgsi_opcode =
3683 ctx->parse.FullToken.FullInstruction.Instruction.Opcode;
3684 R600_ERR("%s tgsi opcode unsupported\n",
3685 tgsi_get_opcode_name(tgsi_opcode));
3686 return -EINVAL;
3687 }
3688
3689 static int tgsi_end(struct r600_shader_ctx *ctx)
3690 {
3691 return 0;
3692 }
3693
3694 static void r600_bytecode_src(struct r600_bytecode_alu_src *bc_src,
3695 const struct r600_shader_src *shader_src,
3696 unsigned chan)
3697 {
3698 bc_src->sel = shader_src->sel;
3699 bc_src->chan = shader_src->swizzle[chan];
3700 bc_src->neg = shader_src->neg;
3701 bc_src->abs = shader_src->abs;
3702 bc_src->rel = shader_src->rel;
3703 bc_src->value = shader_src->value[bc_src->chan];
3704 bc_src->kc_bank = shader_src->kc_bank;
3705 bc_src->kc_rel = shader_src->kc_rel;
3706 }
3707
3708 static void r600_bytecode_src_set_abs(struct r600_bytecode_alu_src *bc_src)
3709 {
3710 bc_src->abs = 1;
3711 bc_src->neg = 0;
3712 }
3713
3714 static void r600_bytecode_src_toggle_neg(struct r600_bytecode_alu_src *bc_src)
3715 {
3716 bc_src->neg = !bc_src->neg;
3717 }
3718
3719 static void tgsi_dst(struct r600_shader_ctx *ctx,
3720 const struct tgsi_full_dst_register *tgsi_dst,
3721 unsigned swizzle,
3722 struct r600_bytecode_alu_dst *r600_dst)
3723 {
3724 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
3725
3726 r600_dst->sel = tgsi_dst->Register.Index;
3727 r600_dst->sel += ctx->file_offset[tgsi_dst->Register.File];
3728 r600_dst->chan = swizzle;
3729 r600_dst->write = 1;
3730 if (inst->Instruction.Saturate) {
3731 r600_dst->clamp = 1;
3732 }
3733 if (ctx->type == PIPE_SHADER_TESS_CTRL) {
3734 if (tgsi_dst->Register.File == TGSI_FILE_OUTPUT) {
3735 return;
3736 }
3737 }
3738 if (tgsi_dst->Register.Indirect)
3739 r600_dst->rel = V_SQ_REL_RELATIVE;
3740
3741 }
3742
3743 static int tgsi_op2_64_params(struct r600_shader_ctx *ctx, bool singledest, bool swap)
3744 {
3745 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
3746 unsigned write_mask = inst->Dst[0].Register.WriteMask;
3747 struct r600_bytecode_alu alu;
3748 int i, j, r, lasti = tgsi_last_instruction(write_mask);
3749 int use_tmp = 0;
3750
3751 if (singledest) {
3752 switch (write_mask) {
3753 case 0x1:
3754 write_mask = 0x3;
3755 break;
3756 case 0x2:
3757 use_tmp = 1;
3758 write_mask = 0x3;
3759 break;
3760 case 0x4:
3761 write_mask = 0xc;
3762 break;
3763 case 0x8:
3764 write_mask = 0xc;
3765 use_tmp = 3;
3766 break;
3767 }
3768 }
3769
3770 lasti = tgsi_last_instruction(write_mask);
3771 for (i = 0; i <= lasti; i++) {
3772
3773 if (!(write_mask & (1 << i)))
3774 continue;
3775
3776 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3777
3778 if (singledest) {
3779 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3780 if (use_tmp) {
3781 alu.dst.sel = ctx->temp_reg;
3782 alu.dst.chan = i;
3783 alu.dst.write = 1;
3784 }
3785 if (i == 1 || i == 3)
3786 alu.dst.write = 0;
3787 } else
3788 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3789
3790 alu.op = ctx->inst_info->op;
3791 if (ctx->parse.FullToken.FullInstruction.Instruction.Opcode == TGSI_OPCODE_DABS) {
3792 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
3793 } else if (!swap) {
3794 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
3795 r600_bytecode_src(&alu.src[j], &ctx->src[j], fp64_switch(i));
3796 }
3797 } else {
3798 r600_bytecode_src(&alu.src[0], &ctx->src[1], fp64_switch(i));
3799 r600_bytecode_src(&alu.src[1], &ctx->src[0], fp64_switch(i));
3800 }
3801
3802 /* handle some special cases */
3803 if (i == 1 || i == 3) {
3804 switch (ctx->parse.FullToken.FullInstruction.Instruction.Opcode) {
3805 case TGSI_OPCODE_DABS:
3806 r600_bytecode_src_set_abs(&alu.src[0]);
3807 break;
3808 default:
3809 break;
3810 }
3811 }
3812 if (i == lasti) {
3813 alu.last = 1;
3814 }
3815 r = r600_bytecode_add_alu(ctx->bc, &alu);
3816 if (r)
3817 return r;
3818 }
3819
3820 if (use_tmp) {
3821 write_mask = inst->Dst[0].Register.WriteMask;
3822
3823 /* move result from temp to dst */
3824 for (i = 0; i <= lasti; i++) {
3825 if (!(write_mask & (1 << i)))
3826 continue;
3827
3828 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3829 alu.op = ALU_OP1_MOV;
3830 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3831 alu.src[0].sel = ctx->temp_reg;
3832 alu.src[0].chan = use_tmp - 1;
3833 alu.last = (i == lasti);
3834
3835 r = r600_bytecode_add_alu(ctx->bc, &alu);
3836 if (r)
3837 return r;
3838 }
3839 }
3840 return 0;
3841 }
3842
3843 static int tgsi_op2_64(struct r600_shader_ctx *ctx)
3844 {
3845 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
3846 unsigned write_mask = inst->Dst[0].Register.WriteMask;
3847 /* confirm writemasking */
3848 if ((write_mask & 0x3) != 0x3 &&
3849 (write_mask & 0xc) != 0xc) {
3850 fprintf(stderr, "illegal writemask for 64-bit: 0x%x\n", write_mask);
3851 return -1;
3852 }
3853 return tgsi_op2_64_params(ctx, false, false);
3854 }
3855
3856 static int tgsi_op2_64_single_dest(struct r600_shader_ctx *ctx)
3857 {
3858 return tgsi_op2_64_params(ctx, true, false);
3859 }
3860
3861 static int tgsi_op2_64_single_dest_s(struct r600_shader_ctx *ctx)
3862 {
3863 return tgsi_op2_64_params(ctx, true, true);
3864 }
3865
3866 static int tgsi_op3_64(struct r600_shader_ctx *ctx)
3867 {
3868 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
3869 struct r600_bytecode_alu alu;
3870 int i, j, r;
3871 int lasti = 3;
3872 int tmp = r600_get_temp(ctx);
3873
3874 for (i = 0; i < lasti + 1; i++) {
3875
3876 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3877 alu.op = ctx->inst_info->op;
3878 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
3879 r600_bytecode_src(&alu.src[j], &ctx->src[j], i == 3 ? 0 : 1);
3880 }
3881
3882 if (inst->Dst[0].Register.WriteMask & (1 << i))
3883 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3884 else
3885 alu.dst.sel = tmp;
3886
3887 alu.dst.chan = i;
3888 alu.is_op3 = 1;
3889 if (i == lasti) {
3890 alu.last = 1;
3891 }
3892 r = r600_bytecode_add_alu(ctx->bc, &alu);
3893 if (r)
3894 return r;
3895 }
3896 return 0;
3897 }
3898
3899 static int tgsi_op2_s(struct r600_shader_ctx *ctx, int swap, int trans_only)
3900 {
3901 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
3902 struct r600_bytecode_alu alu;
3903 unsigned write_mask = inst->Dst[0].Register.WriteMask;
3904 int i, j, r, lasti = tgsi_last_instruction(write_mask);
3905 /* use temp register if trans_only and more than one dst component */
3906 int use_tmp = trans_only && (write_mask ^ (1 << lasti));
3907 unsigned op = ctx->inst_info->op;
3908
3909 if (op == ALU_OP2_MUL_IEEE &&
3910 ctx->info.properties[TGSI_PROPERTY_MUL_ZERO_WINS])
3911 op = ALU_OP2_MUL;
3912
3913 for (i = 0; i <= lasti; i++) {
3914 if (!(write_mask & (1 << i)))
3915 continue;
3916
3917 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3918 if (use_tmp) {
3919 alu.dst.sel = ctx->temp_reg;
3920 alu.dst.chan = i;
3921 alu.dst.write = 1;
3922 } else
3923 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3924
3925 alu.op = op;
3926 if (!swap) {
3927 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
3928 r600_bytecode_src(&alu.src[j], &ctx->src[j], i);
3929 }
3930 } else {
3931 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
3932 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
3933 }
3934 if (i == lasti || trans_only) {
3935 alu.last = 1;
3936 }
3937 r = r600_bytecode_add_alu(ctx->bc, &alu);
3938 if (r)
3939 return r;
3940 }
3941
3942 if (use_tmp) {
3943 /* move result from temp to dst */
3944 for (i = 0; i <= lasti; i++) {
3945 if (!(write_mask & (1 << i)))
3946 continue;
3947
3948 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3949 alu.op = ALU_OP1_MOV;
3950 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3951 alu.src[0].sel = ctx->temp_reg;
3952 alu.src[0].chan = i;
3953 alu.last = (i == lasti);
3954
3955 r = r600_bytecode_add_alu(ctx->bc, &alu);
3956 if (r)
3957 return r;
3958 }
3959 }
3960 return 0;
3961 }
3962
3963 static int tgsi_op2(struct r600_shader_ctx *ctx)
3964 {
3965 return tgsi_op2_s(ctx, 0, 0);
3966 }
3967
3968 static int tgsi_op2_swap(struct r600_shader_ctx *ctx)
3969 {
3970 return tgsi_op2_s(ctx, 1, 0);
3971 }
3972
3973 static int tgsi_op2_trans(struct r600_shader_ctx *ctx)
3974 {
3975 return tgsi_op2_s(ctx, 0, 1);
3976 }
3977
3978 static int tgsi_ineg(struct r600_shader_ctx *ctx)
3979 {
3980 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
3981 struct r600_bytecode_alu alu;
3982 int i, r;
3983 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
3984
3985 for (i = 0; i < lasti + 1; i++) {
3986
3987 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
3988 continue;
3989 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
3990 alu.op = ctx->inst_info->op;
3991
3992 alu.src[0].sel = V_SQ_ALU_SRC_0;
3993
3994 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
3995
3996 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
3997
3998 if (i == lasti) {
3999 alu.last = 1;
4000 }
4001 r = r600_bytecode_add_alu(ctx->bc, &alu);
4002 if (r)
4003 return r;
4004 }
4005 return 0;
4006
4007 }
4008
4009 static int tgsi_dneg(struct r600_shader_ctx *ctx)
4010 {
4011 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4012 struct r600_bytecode_alu alu;
4013 int i, r;
4014 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4015
4016 for (i = 0; i < lasti + 1; i++) {
4017
4018 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
4019 continue;
4020 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4021 alu.op = ALU_OP1_MOV;
4022
4023 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
4024
4025 if (i == 1 || i == 3)
4026 r600_bytecode_src_toggle_neg(&alu.src[0]);
4027 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4028
4029 if (i == lasti) {
4030 alu.last = 1;
4031 }
4032 r = r600_bytecode_add_alu(ctx->bc, &alu);
4033 if (r)
4034 return r;
4035 }
4036 return 0;
4037
4038 }
4039
4040 static int tgsi_dfracexp(struct r600_shader_ctx *ctx)
4041 {
4042 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4043 struct r600_bytecode_alu alu;
4044 unsigned write_mask = inst->Dst[0].Register.WriteMask;
4045 int i, j, r;
4046 int firsti = write_mask == 0xc ? 2 : 0;
4047
4048 for (i = 0; i <= 3; i++) {
4049 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4050 alu.op = ctx->inst_info->op;
4051
4052 alu.dst.sel = ctx->temp_reg;
4053 alu.dst.chan = i;
4054 alu.dst.write = 1;
4055 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
4056 r600_bytecode_src(&alu.src[j], &ctx->src[j], fp64_switch(i));
4057 }
4058
4059 if (i == 3)
4060 alu.last = 1;
4061
4062 r = r600_bytecode_add_alu(ctx->bc, &alu);
4063 if (r)
4064 return r;
4065 }
4066
4067 /* MOV first two channels to writemask dst0 */
4068 for (i = 0; i <= 1; i++) {
4069 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4070 alu.op = ALU_OP1_MOV;
4071 alu.src[0].chan = i + 2;
4072 alu.src[0].sel = ctx->temp_reg;
4073
4074 tgsi_dst(ctx, &inst->Dst[0], firsti + i, &alu.dst);
4075 alu.dst.write = (inst->Dst[0].Register.WriteMask >> (firsti + i)) & 1;
4076 alu.last = 1;
4077 r = r600_bytecode_add_alu(ctx->bc, &alu);
4078 if (r)
4079 return r;
4080 }
4081
4082 for (i = 0; i <= 3; i++) {
4083 if (inst->Dst[1].Register.WriteMask & (1 << i)) {
4084 /* MOV third channels to writemask dst1 */
4085 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4086 alu.op = ALU_OP1_MOV;
4087 alu.src[0].chan = 1;
4088 alu.src[0].sel = ctx->temp_reg;
4089
4090 tgsi_dst(ctx, &inst->Dst[1], i, &alu.dst);
4091 alu.last = 1;
4092 r = r600_bytecode_add_alu(ctx->bc, &alu);
4093 if (r)
4094 return r;
4095 break;
4096 }
4097 }
4098 return 0;
4099 }
4100
4101
4102 static int egcm_int_to_double(struct r600_shader_ctx *ctx)
4103 {
4104 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4105 struct r600_bytecode_alu alu;
4106 int i, r;
4107 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4108
4109 assert(inst->Instruction.Opcode == TGSI_OPCODE_I2D ||
4110 inst->Instruction.Opcode == TGSI_OPCODE_U2D);
4111
4112 for (i = 0; i <= (lasti+1)/2; i++) {
4113 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4114 alu.op = ctx->inst_info->op;
4115
4116 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
4117 alu.dst.sel = ctx->temp_reg;
4118 alu.dst.chan = i;
4119 alu.dst.write = 1;
4120 alu.last = 1;
4121
4122 r = r600_bytecode_add_alu(ctx->bc, &alu);
4123 if (r)
4124 return r;
4125 }
4126
4127 for (i = 0; i <= lasti; i++) {
4128 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4129 alu.op = ALU_OP1_FLT32_TO_FLT64;
4130
4131 alu.src[0].chan = i/2;
4132 if (i%2 == 0)
4133 alu.src[0].sel = ctx->temp_reg;
4134 else {
4135 alu.src[0].sel = V_SQ_ALU_SRC_LITERAL;
4136 alu.src[0].value = 0x0;
4137 }
4138 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4139 alu.last = i == lasti;
4140
4141 r = r600_bytecode_add_alu(ctx->bc, &alu);
4142 if (r)
4143 return r;
4144 }
4145
4146 return 0;
4147 }
4148
4149 static int egcm_double_to_int(struct r600_shader_ctx *ctx)
4150 {
4151 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4152 struct r600_bytecode_alu alu;
4153 int i, r;
4154 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4155
4156 assert(inst->Instruction.Opcode == TGSI_OPCODE_D2I ||
4157 inst->Instruction.Opcode == TGSI_OPCODE_D2U);
4158
4159 for (i = 0; i <= lasti; i++) {
4160 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4161 alu.op = ALU_OP1_FLT64_TO_FLT32;
4162
4163 r600_bytecode_src(&alu.src[0], &ctx->src[0], fp64_switch(i));
4164 alu.dst.chan = i;
4165 alu.dst.sel = ctx->temp_reg;
4166 alu.dst.write = i%2 == 0;
4167 alu.last = i == lasti;
4168
4169 r = r600_bytecode_add_alu(ctx->bc, &alu);
4170 if (r)
4171 return r;
4172 }
4173
4174 for (i = 0; i <= (lasti+1)/2; i++) {
4175 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4176 alu.op = ctx->inst_info->op;
4177
4178 alu.src[0].chan = i*2;
4179 alu.src[0].sel = ctx->temp_reg;
4180 tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
4181 alu.last = 1;
4182
4183 r = r600_bytecode_add_alu(ctx->bc, &alu);
4184 if (r)
4185 return r;
4186 }
4187
4188 return 0;
4189 }
4190
4191 static int cayman_emit_unary_double_raw(struct r600_bytecode *bc,
4192 unsigned op,
4193 int dst_reg,
4194 struct r600_shader_src *src,
4195 bool abs)
4196 {
4197 struct r600_bytecode_alu alu;
4198 const int last_slot = 3;
4199 int r;
4200
4201 /* these have to write the result to X/Y by the looks of it */
4202 for (int i = 0 ; i < last_slot; i++) {
4203 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4204 alu.op = op;
4205
4206 r600_bytecode_src(&alu.src[0], src, 1);
4207 r600_bytecode_src(&alu.src[1], src, 0);
4208
4209 if (abs)
4210 r600_bytecode_src_set_abs(&alu.src[1]);
4211
4212 alu.dst.sel = dst_reg;
4213 alu.dst.chan = i;
4214 alu.dst.write = (i == 0 || i == 1);
4215
4216 if (bc->chip_class != CAYMAN || i == last_slot - 1)
4217 alu.last = 1;
4218 r = r600_bytecode_add_alu(bc, &alu);
4219 if (r)
4220 return r;
4221 }
4222
4223 return 0;
4224 }
4225
4226 static int cayman_emit_double_instr(struct r600_shader_ctx *ctx)
4227 {
4228 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4229 int i, r;
4230 struct r600_bytecode_alu alu;
4231 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4232 int t1 = ctx->temp_reg;
4233
4234 /* should only be one src regs */
4235 assert(inst->Instruction.NumSrcRegs == 1);
4236
4237 /* only support one double at a time */
4238 assert(inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_XY ||
4239 inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_ZW);
4240
4241 r = cayman_emit_unary_double_raw(
4242 ctx->bc, ctx->inst_info->op, t1,
4243 &ctx->src[0],
4244 ctx->parse.FullToken.FullInstruction.Instruction.Opcode == TGSI_OPCODE_DRSQ ||
4245 ctx->parse.FullToken.FullInstruction.Instruction.Opcode == TGSI_OPCODE_DSQRT);
4246 if (r)
4247 return r;
4248
4249 for (i = 0 ; i <= lasti; i++) {
4250 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
4251 continue;
4252 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4253 alu.op = ALU_OP1_MOV;
4254 alu.src[0].sel = t1;
4255 alu.src[0].chan = (i == 0 || i == 2) ? 0 : 1;
4256 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4257 alu.dst.write = 1;
4258 if (i == lasti)
4259 alu.last = 1;
4260 r = r600_bytecode_add_alu(ctx->bc, &alu);
4261 if (r)
4262 return r;
4263 }
4264 return 0;
4265 }
4266
4267 static int cayman_emit_float_instr(struct r600_shader_ctx *ctx)
4268 {
4269 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4270 int i, j, r;
4271 struct r600_bytecode_alu alu;
4272 int last_slot = (inst->Dst[0].Register.WriteMask & 0x8) ? 4 : 3;
4273
4274 for (i = 0 ; i < last_slot; i++) {
4275 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4276 alu.op = ctx->inst_info->op;
4277 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
4278 r600_bytecode_src(&alu.src[j], &ctx->src[j], 0);
4279
4280 /* RSQ should take the absolute value of src */
4281 if (inst->Instruction.Opcode == TGSI_OPCODE_RSQ) {
4282 r600_bytecode_src_set_abs(&alu.src[j]);
4283 }
4284 }
4285 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4286 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
4287
4288 if (i == last_slot - 1)
4289 alu.last = 1;
4290 r = r600_bytecode_add_alu(ctx->bc, &alu);
4291 if (r)
4292 return r;
4293 }
4294 return 0;
4295 }
4296
4297 static int cayman_mul_int_instr(struct r600_shader_ctx *ctx)
4298 {
4299 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4300 int i, j, k, r;
4301 struct r600_bytecode_alu alu;
4302 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4303 int t1 = ctx->temp_reg;
4304
4305 for (k = 0; k <= lasti; k++) {
4306 if (!(inst->Dst[0].Register.WriteMask & (1 << k)))
4307 continue;
4308
4309 for (i = 0 ; i < 4; i++) {
4310 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4311 alu.op = ctx->inst_info->op;
4312 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
4313 r600_bytecode_src(&alu.src[j], &ctx->src[j], k);
4314 }
4315 alu.dst.sel = t1;
4316 alu.dst.chan = i;
4317 alu.dst.write = (i == k);
4318 if (i == 3)
4319 alu.last = 1;
4320 r = r600_bytecode_add_alu(ctx->bc, &alu);
4321 if (r)
4322 return r;
4323 }
4324 }
4325
4326 for (i = 0 ; i <= lasti; i++) {
4327 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
4328 continue;
4329 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4330 alu.op = ALU_OP1_MOV;
4331 alu.src[0].sel = t1;
4332 alu.src[0].chan = i;
4333 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4334 alu.dst.write = 1;
4335 if (i == lasti)
4336 alu.last = 1;
4337 r = r600_bytecode_add_alu(ctx->bc, &alu);
4338 if (r)
4339 return r;
4340 }
4341
4342 return 0;
4343 }
4344
4345
4346 static int cayman_mul_double_instr(struct r600_shader_ctx *ctx)
4347 {
4348 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4349 int i, j, k, r;
4350 struct r600_bytecode_alu alu;
4351 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4352 int t1 = ctx->temp_reg;
4353
4354 /* t1 would get overwritten below if we actually tried to
4355 * multiply two pairs of doubles at a time. */
4356 assert(inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_XY ||
4357 inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_ZW);
4358
4359 k = inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_XY ? 0 : 1;
4360
4361 for (i = 0; i < 4; i++) {
4362 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4363 alu.op = ctx->inst_info->op;
4364 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
4365 r600_bytecode_src(&alu.src[j], &ctx->src[j], k * 2 + ((i == 3) ? 0 : 1));
4366 }
4367 alu.dst.sel = t1;
4368 alu.dst.chan = i;
4369 alu.dst.write = 1;
4370 if (i == 3)
4371 alu.last = 1;
4372 r = r600_bytecode_add_alu(ctx->bc, &alu);
4373 if (r)
4374 return r;
4375 }
4376
4377 for (i = 0; i <= lasti; i++) {
4378 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
4379 continue;
4380 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4381 alu.op = ALU_OP1_MOV;
4382 alu.src[0].sel = t1;
4383 alu.src[0].chan = i;
4384 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4385 alu.dst.write = 1;
4386 if (i == lasti)
4387 alu.last = 1;
4388 r = r600_bytecode_add_alu(ctx->bc, &alu);
4389 if (r)
4390 return r;
4391 }
4392
4393 return 0;
4394 }
4395
4396 /*
4397 * Emit RECIP_64 + MUL_64 to implement division.
4398 */
4399 static int cayman_ddiv_instr(struct r600_shader_ctx *ctx)
4400 {
4401 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4402 int r;
4403 struct r600_bytecode_alu alu;
4404 int t1 = ctx->temp_reg;
4405 int k;
4406
4407 /* Only support one double at a time. This is the same constraint as
4408 * in DMUL lowering. */
4409 assert(inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_XY ||
4410 inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_ZW);
4411
4412 k = inst->Dst[0].Register.WriteMask == TGSI_WRITEMASK_XY ? 0 : 1;
4413
4414 r = cayman_emit_unary_double_raw(ctx->bc, ALU_OP2_RECIP_64, t1, &ctx->src[1], false);
4415 if (r)
4416 return r;
4417
4418 for (int i = 0; i < 4; i++) {
4419 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4420 alu.op = ALU_OP2_MUL_64;
4421
4422 r600_bytecode_src(&alu.src[0], &ctx->src[0], k * 2 + ((i == 3) ? 0 : 1));
4423
4424 alu.src[1].sel = t1;
4425 alu.src[1].chan = (i == 3) ? 0 : 1;
4426
4427 alu.dst.sel = t1;
4428 alu.dst.chan = i;
4429 alu.dst.write = 1;
4430 if (i == 3)
4431 alu.last = 1;
4432 r = r600_bytecode_add_alu(ctx->bc, &alu);
4433 if (r)
4434 return r;
4435 }
4436
4437 for (int i = 0; i < 2; i++) {
4438 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4439 alu.op = ALU_OP1_MOV;
4440 alu.src[0].sel = t1;
4441 alu.src[0].chan = i;
4442 tgsi_dst(ctx, &inst->Dst[0], k * 2 + i, &alu.dst);
4443 alu.dst.write = 1;
4444 if (i == 1)
4445 alu.last = 1;
4446 r = r600_bytecode_add_alu(ctx->bc, &alu);
4447 if (r)
4448 return r;
4449 }
4450 return 0;
4451 }
4452
4453 /*
4454 * r600 - trunc to -PI..PI range
4455 * r700 - normalize by dividing by 2PI
4456 * see fdo bug 27901
4457 */
4458 static int tgsi_setup_trig(struct r600_shader_ctx *ctx)
4459 {
4460 int r;
4461 struct r600_bytecode_alu alu;
4462
4463 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4464 alu.op = ALU_OP3_MULADD;
4465 alu.is_op3 = 1;
4466
4467 alu.dst.chan = 0;
4468 alu.dst.sel = ctx->temp_reg;
4469 alu.dst.write = 1;
4470
4471 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
4472
4473 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
4474 alu.src[1].chan = 0;
4475 alu.src[1].value = u_bitcast_f2u(0.5f * M_1_PI);
4476 alu.src[2].sel = V_SQ_ALU_SRC_0_5;
4477 alu.src[2].chan = 0;
4478 alu.last = 1;
4479 r = r600_bytecode_add_alu(ctx->bc, &alu);
4480 if (r)
4481 return r;
4482
4483 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4484 alu.op = ALU_OP1_FRACT;
4485
4486 alu.dst.chan = 0;
4487 alu.dst.sel = ctx->temp_reg;
4488 alu.dst.write = 1;
4489
4490 alu.src[0].sel = ctx->temp_reg;
4491 alu.src[0].chan = 0;
4492 alu.last = 1;
4493 r = r600_bytecode_add_alu(ctx->bc, &alu);
4494 if (r)
4495 return r;
4496
4497 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4498 alu.op = ALU_OP3_MULADD;
4499 alu.is_op3 = 1;
4500
4501 alu.dst.chan = 0;
4502 alu.dst.sel = ctx->temp_reg;
4503 alu.dst.write = 1;
4504
4505 alu.src[0].sel = ctx->temp_reg;
4506 alu.src[0].chan = 0;
4507
4508 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
4509 alu.src[1].chan = 0;
4510 alu.src[2].sel = V_SQ_ALU_SRC_LITERAL;
4511 alu.src[2].chan = 0;
4512
4513 if (ctx->bc->chip_class == R600) {
4514 alu.src[1].value = u_bitcast_f2u(2.0f * M_PI);
4515 alu.src[2].value = u_bitcast_f2u(-M_PI);
4516 } else {
4517 alu.src[1].sel = V_SQ_ALU_SRC_1;
4518 alu.src[2].sel = V_SQ_ALU_SRC_0_5;
4519 alu.src[2].neg = 1;
4520 }
4521
4522 alu.last = 1;
4523 r = r600_bytecode_add_alu(ctx->bc, &alu);
4524 if (r)
4525 return r;
4526 return 0;
4527 }
4528
4529 static int cayman_trig(struct r600_shader_ctx *ctx)
4530 {
4531 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4532 struct r600_bytecode_alu alu;
4533 int last_slot = (inst->Dst[0].Register.WriteMask & 0x8) ? 4 : 3;
4534 int i, r;
4535
4536 r = tgsi_setup_trig(ctx);
4537 if (r)
4538 return r;
4539
4540
4541 for (i = 0; i < last_slot; i++) {
4542 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4543 alu.op = ctx->inst_info->op;
4544 alu.dst.chan = i;
4545
4546 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4547 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
4548
4549 alu.src[0].sel = ctx->temp_reg;
4550 alu.src[0].chan = 0;
4551 if (i == last_slot - 1)
4552 alu.last = 1;
4553 r = r600_bytecode_add_alu(ctx->bc, &alu);
4554 if (r)
4555 return r;
4556 }
4557 return 0;
4558 }
4559
4560 static int tgsi_trig(struct r600_shader_ctx *ctx)
4561 {
4562 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4563 struct r600_bytecode_alu alu;
4564 int i, r;
4565 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
4566
4567 r = tgsi_setup_trig(ctx);
4568 if (r)
4569 return r;
4570
4571 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4572 alu.op = ctx->inst_info->op;
4573 alu.dst.chan = 0;
4574 alu.dst.sel = ctx->temp_reg;
4575 alu.dst.write = 1;
4576
4577 alu.src[0].sel = ctx->temp_reg;
4578 alu.src[0].chan = 0;
4579 alu.last = 1;
4580 r = r600_bytecode_add_alu(ctx->bc, &alu);
4581 if (r)
4582 return r;
4583
4584 /* replicate result */
4585 for (i = 0; i < lasti + 1; i++) {
4586 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
4587 continue;
4588
4589 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4590 alu.op = ALU_OP1_MOV;
4591
4592 alu.src[0].sel = ctx->temp_reg;
4593 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4594 if (i == lasti)
4595 alu.last = 1;
4596 r = r600_bytecode_add_alu(ctx->bc, &alu);
4597 if (r)
4598 return r;
4599 }
4600 return 0;
4601 }
4602
4603 static int tgsi_scs(struct r600_shader_ctx *ctx)
4604 {
4605 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4606 struct r600_bytecode_alu alu;
4607 int i, r;
4608
4609 /* We'll only need the trig stuff if we are going to write to the
4610 * X or Y components of the destination vector.
4611 */
4612 if (likely(inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_XY)) {
4613 r = tgsi_setup_trig(ctx);
4614 if (r)
4615 return r;
4616 }
4617
4618 /* dst.x = COS */
4619 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_X) {
4620 if (ctx->bc->chip_class == CAYMAN) {
4621 for (i = 0 ; i < 3; i++) {
4622 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4623 alu.op = ALU_OP1_COS;
4624 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4625
4626 if (i == 0)
4627 alu.dst.write = 1;
4628 else
4629 alu.dst.write = 0;
4630 alu.src[0].sel = ctx->temp_reg;
4631 alu.src[0].chan = 0;
4632 if (i == 2)
4633 alu.last = 1;
4634 r = r600_bytecode_add_alu(ctx->bc, &alu);
4635 if (r)
4636 return r;
4637 }
4638 } else {
4639 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4640 alu.op = ALU_OP1_COS;
4641 tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
4642
4643 alu.src[0].sel = ctx->temp_reg;
4644 alu.src[0].chan = 0;
4645 alu.last = 1;
4646 r = r600_bytecode_add_alu(ctx->bc, &alu);
4647 if (r)
4648 return r;
4649 }
4650 }
4651
4652 /* dst.y = SIN */
4653 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Y) {
4654 if (ctx->bc->chip_class == CAYMAN) {
4655 for (i = 0 ; i < 3; i++) {
4656 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4657 alu.op = ALU_OP1_SIN;
4658 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4659 if (i == 1)
4660 alu.dst.write = 1;
4661 else
4662 alu.dst.write = 0;
4663 alu.src[0].sel = ctx->temp_reg;
4664 alu.src[0].chan = 0;
4665 if (i == 2)
4666 alu.last = 1;
4667 r = r600_bytecode_add_alu(ctx->bc, &alu);
4668 if (r)
4669 return r;
4670 }
4671 } else {
4672 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4673 alu.op = ALU_OP1_SIN;
4674 tgsi_dst(ctx, &inst->Dst[0], 1, &alu.dst);
4675
4676 alu.src[0].sel = ctx->temp_reg;
4677 alu.src[0].chan = 0;
4678 alu.last = 1;
4679 r = r600_bytecode_add_alu(ctx->bc, &alu);
4680 if (r)
4681 return r;
4682 }
4683 }
4684
4685 /* dst.z = 0.0; */
4686 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_Z) {
4687 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4688
4689 alu.op = ALU_OP1_MOV;
4690
4691 tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
4692
4693 alu.src[0].sel = V_SQ_ALU_SRC_0;
4694 alu.src[0].chan = 0;
4695
4696 alu.last = 1;
4697
4698 r = r600_bytecode_add_alu(ctx->bc, &alu);
4699 if (r)
4700 return r;
4701 }
4702
4703 /* dst.w = 1.0; */
4704 if (inst->Dst[0].Register.WriteMask & TGSI_WRITEMASK_W) {
4705 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4706
4707 alu.op = ALU_OP1_MOV;
4708
4709 tgsi_dst(ctx, &inst->Dst[0], 3, &alu.dst);
4710
4711 alu.src[0].sel = V_SQ_ALU_SRC_1;
4712 alu.src[0].chan = 0;
4713
4714 alu.last = 1;
4715
4716 r = r600_bytecode_add_alu(ctx->bc, &alu);
4717 if (r)
4718 return r;
4719 }
4720
4721 return 0;
4722 }
4723
4724 static int tgsi_kill(struct r600_shader_ctx *ctx)
4725 {
4726 const struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4727 struct r600_bytecode_alu alu;
4728 int i, r;
4729
4730 for (i = 0; i < 4; i++) {
4731 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4732 alu.op = ctx->inst_info->op;
4733
4734 alu.dst.chan = i;
4735
4736 alu.src[0].sel = V_SQ_ALU_SRC_0;
4737
4738 if (inst->Instruction.Opcode == TGSI_OPCODE_KILL) {
4739 alu.src[1].sel = V_SQ_ALU_SRC_1;
4740 alu.src[1].neg = 1;
4741 } else {
4742 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
4743 }
4744 if (i == 3) {
4745 alu.last = 1;
4746 }
4747 r = r600_bytecode_add_alu(ctx->bc, &alu);
4748 if (r)
4749 return r;
4750 }
4751
4752 /* kill must be last in ALU */
4753 ctx->bc->force_add_cf = 1;
4754 ctx->shader->uses_kill = TRUE;
4755 return 0;
4756 }
4757
4758 static int tgsi_lit(struct r600_shader_ctx *ctx)
4759 {
4760 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4761 struct r600_bytecode_alu alu;
4762 int r;
4763
4764 /* tmp.x = max(src.y, 0.0) */
4765 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4766 alu.op = ALU_OP2_MAX;
4767 r600_bytecode_src(&alu.src[0], &ctx->src[0], 1);
4768 alu.src[1].sel = V_SQ_ALU_SRC_0; /*0.0*/
4769 alu.src[1].chan = 1;
4770
4771 alu.dst.sel = ctx->temp_reg;
4772 alu.dst.chan = 0;
4773 alu.dst.write = 1;
4774
4775 alu.last = 1;
4776 r = r600_bytecode_add_alu(ctx->bc, &alu);
4777 if (r)
4778 return r;
4779
4780 if (inst->Dst[0].Register.WriteMask & (1 << 2))
4781 {
4782 int chan;
4783 int sel;
4784 unsigned i;
4785
4786 if (ctx->bc->chip_class == CAYMAN) {
4787 for (i = 0; i < 3; i++) {
4788 /* tmp.z = log(tmp.x) */
4789 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4790 alu.op = ALU_OP1_LOG_CLAMPED;
4791 alu.src[0].sel = ctx->temp_reg;
4792 alu.src[0].chan = 0;
4793 alu.dst.sel = ctx->temp_reg;
4794 alu.dst.chan = i;
4795 if (i == 2) {
4796 alu.dst.write = 1;
4797 alu.last = 1;
4798 } else
4799 alu.dst.write = 0;
4800
4801 r = r600_bytecode_add_alu(ctx->bc, &alu);
4802 if (r)
4803 return r;
4804 }
4805 } else {
4806 /* tmp.z = log(tmp.x) */
4807 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4808 alu.op = ALU_OP1_LOG_CLAMPED;
4809 alu.src[0].sel = ctx->temp_reg;
4810 alu.src[0].chan = 0;
4811 alu.dst.sel = ctx->temp_reg;
4812 alu.dst.chan = 2;
4813 alu.dst.write = 1;
4814 alu.last = 1;
4815 r = r600_bytecode_add_alu(ctx->bc, &alu);
4816 if (r)
4817 return r;
4818 }
4819
4820 chan = alu.dst.chan;
4821 sel = alu.dst.sel;
4822
4823 /* tmp.x = amd MUL_LIT(tmp.z, src.w, src.x ) */
4824 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4825 alu.op = ALU_OP3_MUL_LIT;
4826 alu.src[0].sel = sel;
4827 alu.src[0].chan = chan;
4828 r600_bytecode_src(&alu.src[1], &ctx->src[0], 3);
4829 r600_bytecode_src(&alu.src[2], &ctx->src[0], 0);
4830 alu.dst.sel = ctx->temp_reg;
4831 alu.dst.chan = 0;
4832 alu.dst.write = 1;
4833 alu.is_op3 = 1;
4834 alu.last = 1;
4835 r = r600_bytecode_add_alu(ctx->bc, &alu);
4836 if (r)
4837 return r;
4838
4839 if (ctx->bc->chip_class == CAYMAN) {
4840 for (i = 0; i < 3; i++) {
4841 /* dst.z = exp(tmp.x) */
4842 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4843 alu.op = ALU_OP1_EXP_IEEE;
4844 alu.src[0].sel = ctx->temp_reg;
4845 alu.src[0].chan = 0;
4846 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4847 if (i == 2) {
4848 alu.dst.write = 1;
4849 alu.last = 1;
4850 } else
4851 alu.dst.write = 0;
4852 r = r600_bytecode_add_alu(ctx->bc, &alu);
4853 if (r)
4854 return r;
4855 }
4856 } else {
4857 /* dst.z = exp(tmp.x) */
4858 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4859 alu.op = ALU_OP1_EXP_IEEE;
4860 alu.src[0].sel = ctx->temp_reg;
4861 alu.src[0].chan = 0;
4862 tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
4863 alu.last = 1;
4864 r = r600_bytecode_add_alu(ctx->bc, &alu);
4865 if (r)
4866 return r;
4867 }
4868 }
4869
4870 /* dst.x, <- 1.0 */
4871 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4872 alu.op = ALU_OP1_MOV;
4873 alu.src[0].sel = V_SQ_ALU_SRC_1; /*1.0*/
4874 alu.src[0].chan = 0;
4875 tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
4876 alu.dst.write = (inst->Dst[0].Register.WriteMask >> 0) & 1;
4877 r = r600_bytecode_add_alu(ctx->bc, &alu);
4878 if (r)
4879 return r;
4880
4881 /* dst.y = max(src.x, 0.0) */
4882 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4883 alu.op = ALU_OP2_MAX;
4884 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
4885 alu.src[1].sel = V_SQ_ALU_SRC_0; /*0.0*/
4886 alu.src[1].chan = 0;
4887 tgsi_dst(ctx, &inst->Dst[0], 1, &alu.dst);
4888 alu.dst.write = (inst->Dst[0].Register.WriteMask >> 1) & 1;
4889 r = r600_bytecode_add_alu(ctx->bc, &alu);
4890 if (r)
4891 return r;
4892
4893 /* dst.w, <- 1.0 */
4894 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4895 alu.op = ALU_OP1_MOV;
4896 alu.src[0].sel = V_SQ_ALU_SRC_1;
4897 alu.src[0].chan = 0;
4898 tgsi_dst(ctx, &inst->Dst[0], 3, &alu.dst);
4899 alu.dst.write = (inst->Dst[0].Register.WriteMask >> 3) & 1;
4900 alu.last = 1;
4901 r = r600_bytecode_add_alu(ctx->bc, &alu);
4902 if (r)
4903 return r;
4904
4905 return 0;
4906 }
4907
4908 static int tgsi_rsq(struct r600_shader_ctx *ctx)
4909 {
4910 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4911 struct r600_bytecode_alu alu;
4912 int i, r;
4913
4914 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4915
4916 /* XXX:
4917 * For state trackers other than OpenGL, we'll want to use
4918 * _RECIPSQRT_IEEE instead.
4919 */
4920 alu.op = ALU_OP1_RECIPSQRT_CLAMPED;
4921
4922 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
4923 r600_bytecode_src(&alu.src[i], &ctx->src[i], 0);
4924 r600_bytecode_src_set_abs(&alu.src[i]);
4925 }
4926 alu.dst.sel = ctx->temp_reg;
4927 alu.dst.write = 1;
4928 alu.last = 1;
4929 r = r600_bytecode_add_alu(ctx->bc, &alu);
4930 if (r)
4931 return r;
4932 /* replicate result */
4933 return tgsi_helper_tempx_replicate(ctx);
4934 }
4935
4936 static int tgsi_helper_tempx_replicate(struct r600_shader_ctx *ctx)
4937 {
4938 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4939 struct r600_bytecode_alu alu;
4940 int i, r;
4941
4942 for (i = 0; i < 4; i++) {
4943 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4944 alu.src[0].sel = ctx->temp_reg;
4945 alu.op = ALU_OP1_MOV;
4946 alu.dst.chan = i;
4947 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
4948 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
4949 if (i == 3)
4950 alu.last = 1;
4951 r = r600_bytecode_add_alu(ctx->bc, &alu);
4952 if (r)
4953 return r;
4954 }
4955 return 0;
4956 }
4957
4958 static int tgsi_trans_srcx_replicate(struct r600_shader_ctx *ctx)
4959 {
4960 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4961 struct r600_bytecode_alu alu;
4962 int i, r;
4963
4964 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4965 alu.op = ctx->inst_info->op;
4966 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
4967 r600_bytecode_src(&alu.src[i], &ctx->src[i], 0);
4968 }
4969 alu.dst.sel = ctx->temp_reg;
4970 alu.dst.write = 1;
4971 alu.last = 1;
4972 r = r600_bytecode_add_alu(ctx->bc, &alu);
4973 if (r)
4974 return r;
4975 /* replicate result */
4976 return tgsi_helper_tempx_replicate(ctx);
4977 }
4978
4979 static int cayman_pow(struct r600_shader_ctx *ctx)
4980 {
4981 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
4982 int i, r;
4983 struct r600_bytecode_alu alu;
4984 int last_slot = (inst->Dst[0].Register.WriteMask & 0x8) ? 4 : 3;
4985
4986 for (i = 0; i < 3; i++) {
4987 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
4988 alu.op = ALU_OP1_LOG_IEEE;
4989 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
4990 alu.dst.sel = ctx->temp_reg;
4991 alu.dst.chan = i;
4992 alu.dst.write = 1;
4993 if (i == 2)
4994 alu.last = 1;
4995 r = r600_bytecode_add_alu(ctx->bc, &alu);
4996 if (r)
4997 return r;
4998 }
4999
5000 /* b * LOG2(a) */
5001 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5002 alu.op = ALU_OP2_MUL;
5003 r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
5004 alu.src[1].sel = ctx->temp_reg;
5005 alu.dst.sel = ctx->temp_reg;
5006 alu.dst.write = 1;
5007 alu.last = 1;
5008 r = r600_bytecode_add_alu(ctx->bc, &alu);
5009 if (r)
5010 return r;
5011
5012 for (i = 0; i < last_slot; i++) {
5013 /* POW(a,b) = EXP2(b * LOG2(a))*/
5014 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5015 alu.op = ALU_OP1_EXP_IEEE;
5016 alu.src[0].sel = ctx->temp_reg;
5017
5018 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
5019 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
5020 if (i == last_slot - 1)
5021 alu.last = 1;
5022 r = r600_bytecode_add_alu(ctx->bc, &alu);
5023 if (r)
5024 return r;
5025 }
5026 return 0;
5027 }
5028
5029 static int tgsi_pow(struct r600_shader_ctx *ctx)
5030 {
5031 struct r600_bytecode_alu alu;
5032 int r;
5033
5034 /* LOG2(a) */
5035 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5036 alu.op = ALU_OP1_LOG_IEEE;
5037 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
5038 alu.dst.sel = ctx->temp_reg;
5039 alu.dst.write = 1;
5040 alu.last = 1;
5041 r = r600_bytecode_add_alu(ctx->bc, &alu);
5042 if (r)
5043 return r;
5044 /* b * LOG2(a) */
5045 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5046 alu.op = ALU_OP2_MUL;
5047 r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
5048 alu.src[1].sel = ctx->temp_reg;
5049 alu.dst.sel = ctx->temp_reg;
5050 alu.dst.write = 1;
5051 alu.last = 1;
5052 r = r600_bytecode_add_alu(ctx->bc, &alu);
5053 if (r)
5054 return r;
5055 /* POW(a,b) = EXP2(b * LOG2(a))*/
5056 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5057 alu.op = ALU_OP1_EXP_IEEE;
5058 alu.src[0].sel = ctx->temp_reg;
5059 alu.dst.sel = ctx->temp_reg;
5060 alu.dst.write = 1;
5061 alu.last = 1;
5062 r = r600_bytecode_add_alu(ctx->bc, &alu);
5063 if (r)
5064 return r;
5065 return tgsi_helper_tempx_replicate(ctx);
5066 }
5067
5068 static int tgsi_divmod(struct r600_shader_ctx *ctx, int mod, int signed_op)
5069 {
5070 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
5071 struct r600_bytecode_alu alu;
5072 int i, r, j;
5073 unsigned write_mask = inst->Dst[0].Register.WriteMask;
5074 int tmp0 = ctx->temp_reg;
5075 int tmp1 = r600_get_temp(ctx);
5076 int tmp2 = r600_get_temp(ctx);
5077 int tmp3 = r600_get_temp(ctx);
5078 /* Unsigned path:
5079 *
5080 * we need to represent src1 as src2*q + r, where q - quotient, r - remainder
5081 *
5082 * 1. tmp0.x = rcp (src2) = 2^32/src2 + e, where e is rounding error
5083 * 2. tmp0.z = lo (tmp0.x * src2)
5084 * 3. tmp0.w = -tmp0.z
5085 * 4. tmp0.y = hi (tmp0.x * src2)
5086 * 5. tmp0.z = (tmp0.y == 0 ? tmp0.w : tmp0.z) = abs(lo(rcp*src2))
5087 * 6. tmp0.w = hi (tmp0.z * tmp0.x) = e, rounding error
5088 * 7. tmp1.x = tmp0.x - tmp0.w
5089 * 8. tmp1.y = tmp0.x + tmp0.w
5090 * 9. tmp0.x = (tmp0.y == 0 ? tmp1.y : tmp1.x)
5091 * 10. tmp0.z = hi(tmp0.x * src1) = q
5092 * 11. tmp0.y = lo (tmp0.z * src2) = src2*q = src1 - r
5093 *
5094 * 12. tmp0.w = src1 - tmp0.y = r
5095 * 13. tmp1.x = tmp0.w >= src2 = r >= src2 (uint comparison)
5096 * 14. tmp1.y = src1 >= tmp0.y = r >= 0 (uint comparison)
5097 *
5098 * if DIV
5099 *
5100 * 15. tmp1.z = tmp0.z + 1 = q + 1
5101 * 16. tmp1.w = tmp0.z - 1 = q - 1
5102 *
5103 * else MOD
5104 *
5105 * 15. tmp1.z = tmp0.w - src2 = r - src2
5106 * 16. tmp1.w = tmp0.w + src2 = r + src2
5107 *
5108 * endif
5109 *
5110 * 17. tmp1.x = tmp1.x & tmp1.y
5111 *
5112 * DIV: 18. tmp0.z = tmp1.x==0 ? tmp0.z : tmp1.z
5113 * MOD: 18. tmp0.z = tmp1.x==0 ? tmp0.w : tmp1.z
5114 *
5115 * 19. tmp0.z = tmp1.y==0 ? tmp1.w : tmp0.z
5116 * 20. dst = src2==0 ? MAX_UINT : tmp0.z
5117 *
5118 * Signed path:
5119 *
5120 * Same as unsigned, using abs values of the operands,
5121 * and fixing the sign of the result in the end.
5122 */
5123
5124 for (i = 0; i < 4; i++) {
5125 if (!(write_mask & (1<<i)))
5126 continue;
5127
5128 if (signed_op) {
5129
5130 /* tmp2.x = -src0 */
5131 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5132 alu.op = ALU_OP2_SUB_INT;
5133
5134 alu.dst.sel = tmp2;
5135 alu.dst.chan = 0;
5136 alu.dst.write = 1;
5137
5138 alu.src[0].sel = V_SQ_ALU_SRC_0;
5139
5140 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
5141
5142 alu.last = 1;
5143 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5144 return r;
5145
5146 /* tmp2.y = -src1 */
5147 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5148 alu.op = ALU_OP2_SUB_INT;
5149
5150 alu.dst.sel = tmp2;
5151 alu.dst.chan = 1;
5152 alu.dst.write = 1;
5153
5154 alu.src[0].sel = V_SQ_ALU_SRC_0;
5155
5156 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5157
5158 alu.last = 1;
5159 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5160 return r;
5161
5162 /* tmp2.z sign bit is set if src0 and src2 signs are different */
5163 /* it will be a sign of the quotient */
5164 if (!mod) {
5165
5166 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5167 alu.op = ALU_OP2_XOR_INT;
5168
5169 alu.dst.sel = tmp2;
5170 alu.dst.chan = 2;
5171 alu.dst.write = 1;
5172
5173 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
5174 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5175
5176 alu.last = 1;
5177 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5178 return r;
5179 }
5180
5181 /* tmp2.x = |src0| */
5182 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5183 alu.op = ALU_OP3_CNDGE_INT;
5184 alu.is_op3 = 1;
5185
5186 alu.dst.sel = tmp2;
5187 alu.dst.chan = 0;
5188 alu.dst.write = 1;
5189
5190 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
5191 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
5192 alu.src[2].sel = tmp2;
5193 alu.src[2].chan = 0;
5194
5195 alu.last = 1;
5196 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5197 return r;
5198
5199 /* tmp2.y = |src1| */
5200 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5201 alu.op = ALU_OP3_CNDGE_INT;
5202 alu.is_op3 = 1;
5203
5204 alu.dst.sel = tmp2;
5205 alu.dst.chan = 1;
5206 alu.dst.write = 1;
5207
5208 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
5209 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5210 alu.src[2].sel = tmp2;
5211 alu.src[2].chan = 1;
5212
5213 alu.last = 1;
5214 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5215 return r;
5216
5217 }
5218
5219 /* 1. tmp0.x = rcp_u (src2) = 2^32/src2 + e, where e is rounding error */
5220 if (ctx->bc->chip_class == CAYMAN) {
5221 /* tmp3.x = u2f(src2) */
5222 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5223 alu.op = ALU_OP1_UINT_TO_FLT;
5224
5225 alu.dst.sel = tmp3;
5226 alu.dst.chan = 0;
5227 alu.dst.write = 1;
5228
5229 if (signed_op) {
5230 alu.src[0].sel = tmp2;
5231 alu.src[0].chan = 1;
5232 } else {
5233 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
5234 }
5235
5236 alu.last = 1;
5237 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5238 return r;
5239
5240 /* tmp0.x = recip(tmp3.x) */
5241 for (j = 0 ; j < 3; j++) {
5242 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5243 alu.op = ALU_OP1_RECIP_IEEE;
5244
5245 alu.dst.sel = tmp0;
5246 alu.dst.chan = j;
5247 alu.dst.write = (j == 0);
5248
5249 alu.src[0].sel = tmp3;
5250 alu.src[0].chan = 0;
5251
5252 if (j == 2)
5253 alu.last = 1;
5254 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5255 return r;
5256 }
5257
5258 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5259 alu.op = ALU_OP2_MUL;
5260
5261 alu.src[0].sel = tmp0;
5262 alu.src[0].chan = 0;
5263
5264 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
5265 alu.src[1].value = 0x4f800000;
5266
5267 alu.dst.sel = tmp3;
5268 alu.dst.write = 1;
5269 alu.last = 1;
5270 r = r600_bytecode_add_alu(ctx->bc, &alu);
5271 if (r)
5272 return r;
5273
5274 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5275 alu.op = ALU_OP1_FLT_TO_UINT;
5276
5277 alu.dst.sel = tmp0;
5278 alu.dst.chan = 0;
5279 alu.dst.write = 1;
5280
5281 alu.src[0].sel = tmp3;
5282 alu.src[0].chan = 0;
5283
5284 alu.last = 1;
5285 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5286 return r;
5287
5288 } else {
5289 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5290 alu.op = ALU_OP1_RECIP_UINT;
5291
5292 alu.dst.sel = tmp0;
5293 alu.dst.chan = 0;
5294 alu.dst.write = 1;
5295
5296 if (signed_op) {
5297 alu.src[0].sel = tmp2;
5298 alu.src[0].chan = 1;
5299 } else {
5300 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
5301 }
5302
5303 alu.last = 1;
5304 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5305 return r;
5306 }
5307
5308 /* 2. tmp0.z = lo (tmp0.x * src2) */
5309 if (ctx->bc->chip_class == CAYMAN) {
5310 for (j = 0 ; j < 4; j++) {
5311 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5312 alu.op = ALU_OP2_MULLO_UINT;
5313
5314 alu.dst.sel = tmp0;
5315 alu.dst.chan = j;
5316 alu.dst.write = (j == 2);
5317
5318 alu.src[0].sel = tmp0;
5319 alu.src[0].chan = 0;
5320 if (signed_op) {
5321 alu.src[1].sel = tmp2;
5322 alu.src[1].chan = 1;
5323 } else {
5324 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5325 }
5326
5327 alu.last = (j == 3);
5328 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5329 return r;
5330 }
5331 } else {
5332 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5333 alu.op = ALU_OP2_MULLO_UINT;
5334
5335 alu.dst.sel = tmp0;
5336 alu.dst.chan = 2;
5337 alu.dst.write = 1;
5338
5339 alu.src[0].sel = tmp0;
5340 alu.src[0].chan = 0;
5341 if (signed_op) {
5342 alu.src[1].sel = tmp2;
5343 alu.src[1].chan = 1;
5344 } else {
5345 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5346 }
5347
5348 alu.last = 1;
5349 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5350 return r;
5351 }
5352
5353 /* 3. tmp0.w = -tmp0.z */
5354 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5355 alu.op = ALU_OP2_SUB_INT;
5356
5357 alu.dst.sel = tmp0;
5358 alu.dst.chan = 3;
5359 alu.dst.write = 1;
5360
5361 alu.src[0].sel = V_SQ_ALU_SRC_0;
5362 alu.src[1].sel = tmp0;
5363 alu.src[1].chan = 2;
5364
5365 alu.last = 1;
5366 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5367 return r;
5368
5369 /* 4. tmp0.y = hi (tmp0.x * src2) */
5370 if (ctx->bc->chip_class == CAYMAN) {
5371 for (j = 0 ; j < 4; j++) {
5372 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5373 alu.op = ALU_OP2_MULHI_UINT;
5374
5375 alu.dst.sel = tmp0;
5376 alu.dst.chan = j;
5377 alu.dst.write = (j == 1);
5378
5379 alu.src[0].sel = tmp0;
5380 alu.src[0].chan = 0;
5381
5382 if (signed_op) {
5383 alu.src[1].sel = tmp2;
5384 alu.src[1].chan = 1;
5385 } else {
5386 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5387 }
5388 alu.last = (j == 3);
5389 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5390 return r;
5391 }
5392 } else {
5393 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5394 alu.op = ALU_OP2_MULHI_UINT;
5395
5396 alu.dst.sel = tmp0;
5397 alu.dst.chan = 1;
5398 alu.dst.write = 1;
5399
5400 alu.src[0].sel = tmp0;
5401 alu.src[0].chan = 0;
5402
5403 if (signed_op) {
5404 alu.src[1].sel = tmp2;
5405 alu.src[1].chan = 1;
5406 } else {
5407 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5408 }
5409
5410 alu.last = 1;
5411 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5412 return r;
5413 }
5414
5415 /* 5. tmp0.z = (tmp0.y == 0 ? tmp0.w : tmp0.z) = abs(lo(rcp*src)) */
5416 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5417 alu.op = ALU_OP3_CNDE_INT;
5418 alu.is_op3 = 1;
5419
5420 alu.dst.sel = tmp0;
5421 alu.dst.chan = 2;
5422 alu.dst.write = 1;
5423
5424 alu.src[0].sel = tmp0;
5425 alu.src[0].chan = 1;
5426 alu.src[1].sel = tmp0;
5427 alu.src[1].chan = 3;
5428 alu.src[2].sel = tmp0;
5429 alu.src[2].chan = 2;
5430
5431 alu.last = 1;
5432 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5433 return r;
5434
5435 /* 6. tmp0.w = hi (tmp0.z * tmp0.x) = e, rounding error */
5436 if (ctx->bc->chip_class == CAYMAN) {
5437 for (j = 0 ; j < 4; j++) {
5438 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5439 alu.op = ALU_OP2_MULHI_UINT;
5440
5441 alu.dst.sel = tmp0;
5442 alu.dst.chan = j;
5443 alu.dst.write = (j == 3);
5444
5445 alu.src[0].sel = tmp0;
5446 alu.src[0].chan = 2;
5447
5448 alu.src[1].sel = tmp0;
5449 alu.src[1].chan = 0;
5450
5451 alu.last = (j == 3);
5452 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5453 return r;
5454 }
5455 } else {
5456 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5457 alu.op = ALU_OP2_MULHI_UINT;
5458
5459 alu.dst.sel = tmp0;
5460 alu.dst.chan = 3;
5461 alu.dst.write = 1;
5462
5463 alu.src[0].sel = tmp0;
5464 alu.src[0].chan = 2;
5465
5466 alu.src[1].sel = tmp0;
5467 alu.src[1].chan = 0;
5468
5469 alu.last = 1;
5470 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5471 return r;
5472 }
5473
5474 /* 7. tmp1.x = tmp0.x - tmp0.w */
5475 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5476 alu.op = ALU_OP2_SUB_INT;
5477
5478 alu.dst.sel = tmp1;
5479 alu.dst.chan = 0;
5480 alu.dst.write = 1;
5481
5482 alu.src[0].sel = tmp0;
5483 alu.src[0].chan = 0;
5484 alu.src[1].sel = tmp0;
5485 alu.src[1].chan = 3;
5486
5487 alu.last = 1;
5488 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5489 return r;
5490
5491 /* 8. tmp1.y = tmp0.x + tmp0.w */
5492 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5493 alu.op = ALU_OP2_ADD_INT;
5494
5495 alu.dst.sel = tmp1;
5496 alu.dst.chan = 1;
5497 alu.dst.write = 1;
5498
5499 alu.src[0].sel = tmp0;
5500 alu.src[0].chan = 0;
5501 alu.src[1].sel = tmp0;
5502 alu.src[1].chan = 3;
5503
5504 alu.last = 1;
5505 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5506 return r;
5507
5508 /* 9. tmp0.x = (tmp0.y == 0 ? tmp1.y : tmp1.x) */
5509 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5510 alu.op = ALU_OP3_CNDE_INT;
5511 alu.is_op3 = 1;
5512
5513 alu.dst.sel = tmp0;
5514 alu.dst.chan = 0;
5515 alu.dst.write = 1;
5516
5517 alu.src[0].sel = tmp0;
5518 alu.src[0].chan = 1;
5519 alu.src[1].sel = tmp1;
5520 alu.src[1].chan = 1;
5521 alu.src[2].sel = tmp1;
5522 alu.src[2].chan = 0;
5523
5524 alu.last = 1;
5525 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5526 return r;
5527
5528 /* 10. tmp0.z = hi(tmp0.x * src1) = q */
5529 if (ctx->bc->chip_class == CAYMAN) {
5530 for (j = 0 ; j < 4; j++) {
5531 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5532 alu.op = ALU_OP2_MULHI_UINT;
5533
5534 alu.dst.sel = tmp0;
5535 alu.dst.chan = j;
5536 alu.dst.write = (j == 2);
5537
5538 alu.src[0].sel = tmp0;
5539 alu.src[0].chan = 0;
5540
5541 if (signed_op) {
5542 alu.src[1].sel = tmp2;
5543 alu.src[1].chan = 0;
5544 } else {
5545 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
5546 }
5547
5548 alu.last = (j == 3);
5549 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5550 return r;
5551 }
5552 } else {
5553 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5554 alu.op = ALU_OP2_MULHI_UINT;
5555
5556 alu.dst.sel = tmp0;
5557 alu.dst.chan = 2;
5558 alu.dst.write = 1;
5559
5560 alu.src[0].sel = tmp0;
5561 alu.src[0].chan = 0;
5562
5563 if (signed_op) {
5564 alu.src[1].sel = tmp2;
5565 alu.src[1].chan = 0;
5566 } else {
5567 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
5568 }
5569
5570 alu.last = 1;
5571 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5572 return r;
5573 }
5574
5575 /* 11. tmp0.y = lo (src2 * tmp0.z) = src2*q = src1 - r */
5576 if (ctx->bc->chip_class == CAYMAN) {
5577 for (j = 0 ; j < 4; j++) {
5578 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5579 alu.op = ALU_OP2_MULLO_UINT;
5580
5581 alu.dst.sel = tmp0;
5582 alu.dst.chan = j;
5583 alu.dst.write = (j == 1);
5584
5585 if (signed_op) {
5586 alu.src[0].sel = tmp2;
5587 alu.src[0].chan = 1;
5588 } else {
5589 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
5590 }
5591
5592 alu.src[1].sel = tmp0;
5593 alu.src[1].chan = 2;
5594
5595 alu.last = (j == 3);
5596 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5597 return r;
5598 }
5599 } else {
5600 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5601 alu.op = ALU_OP2_MULLO_UINT;
5602
5603 alu.dst.sel = tmp0;
5604 alu.dst.chan = 1;
5605 alu.dst.write = 1;
5606
5607 if (signed_op) {
5608 alu.src[0].sel = tmp2;
5609 alu.src[0].chan = 1;
5610 } else {
5611 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
5612 }
5613
5614 alu.src[1].sel = tmp0;
5615 alu.src[1].chan = 2;
5616
5617 alu.last = 1;
5618 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5619 return r;
5620 }
5621
5622 /* 12. tmp0.w = src1 - tmp0.y = r */
5623 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5624 alu.op = ALU_OP2_SUB_INT;
5625
5626 alu.dst.sel = tmp0;
5627 alu.dst.chan = 3;
5628 alu.dst.write = 1;
5629
5630 if (signed_op) {
5631 alu.src[0].sel = tmp2;
5632 alu.src[0].chan = 0;
5633 } else {
5634 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
5635 }
5636
5637 alu.src[1].sel = tmp0;
5638 alu.src[1].chan = 1;
5639
5640 alu.last = 1;
5641 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5642 return r;
5643
5644 /* 13. tmp1.x = tmp0.w >= src2 = r >= src2 */
5645 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5646 alu.op = ALU_OP2_SETGE_UINT;
5647
5648 alu.dst.sel = tmp1;
5649 alu.dst.chan = 0;
5650 alu.dst.write = 1;
5651
5652 alu.src[0].sel = tmp0;
5653 alu.src[0].chan = 3;
5654 if (signed_op) {
5655 alu.src[1].sel = tmp2;
5656 alu.src[1].chan = 1;
5657 } else {
5658 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5659 }
5660
5661 alu.last = 1;
5662 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5663 return r;
5664
5665 /* 14. tmp1.y = src1 >= tmp0.y = r >= 0 */
5666 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5667 alu.op = ALU_OP2_SETGE_UINT;
5668
5669 alu.dst.sel = tmp1;
5670 alu.dst.chan = 1;
5671 alu.dst.write = 1;
5672
5673 if (signed_op) {
5674 alu.src[0].sel = tmp2;
5675 alu.src[0].chan = 0;
5676 } else {
5677 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
5678 }
5679
5680 alu.src[1].sel = tmp0;
5681 alu.src[1].chan = 1;
5682
5683 alu.last = 1;
5684 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5685 return r;
5686
5687 if (mod) { /* UMOD */
5688
5689 /* 15. tmp1.z = tmp0.w - src2 = r - src2 */
5690 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5691 alu.op = ALU_OP2_SUB_INT;
5692
5693 alu.dst.sel = tmp1;
5694 alu.dst.chan = 2;
5695 alu.dst.write = 1;
5696
5697 alu.src[0].sel = tmp0;
5698 alu.src[0].chan = 3;
5699
5700 if (signed_op) {
5701 alu.src[1].sel = tmp2;
5702 alu.src[1].chan = 1;
5703 } else {
5704 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5705 }
5706
5707 alu.last = 1;
5708 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5709 return r;
5710
5711 /* 16. tmp1.w = tmp0.w + src2 = r + src2 */
5712 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5713 alu.op = ALU_OP2_ADD_INT;
5714
5715 alu.dst.sel = tmp1;
5716 alu.dst.chan = 3;
5717 alu.dst.write = 1;
5718
5719 alu.src[0].sel = tmp0;
5720 alu.src[0].chan = 3;
5721 if (signed_op) {
5722 alu.src[1].sel = tmp2;
5723 alu.src[1].chan = 1;
5724 } else {
5725 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
5726 }
5727
5728 alu.last = 1;
5729 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5730 return r;
5731
5732 } else { /* UDIV */
5733
5734 /* 15. tmp1.z = tmp0.z + 1 = q + 1 DIV */
5735 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5736 alu.op = ALU_OP2_ADD_INT;
5737
5738 alu.dst.sel = tmp1;
5739 alu.dst.chan = 2;
5740 alu.dst.write = 1;
5741
5742 alu.src[0].sel = tmp0;
5743 alu.src[0].chan = 2;
5744 alu.src[1].sel = V_SQ_ALU_SRC_1_INT;
5745
5746 alu.last = 1;
5747 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5748 return r;
5749
5750 /* 16. tmp1.w = tmp0.z - 1 = q - 1 */
5751 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5752 alu.op = ALU_OP2_ADD_INT;
5753
5754 alu.dst.sel = tmp1;
5755 alu.dst.chan = 3;
5756 alu.dst.write = 1;
5757
5758 alu.src[0].sel = tmp0;
5759 alu.src[0].chan = 2;
5760 alu.src[1].sel = V_SQ_ALU_SRC_M_1_INT;
5761
5762 alu.last = 1;
5763 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5764 return r;
5765
5766 }
5767
5768 /* 17. tmp1.x = tmp1.x & tmp1.y */
5769 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5770 alu.op = ALU_OP2_AND_INT;
5771
5772 alu.dst.sel = tmp1;
5773 alu.dst.chan = 0;
5774 alu.dst.write = 1;
5775
5776 alu.src[0].sel = tmp1;
5777 alu.src[0].chan = 0;
5778 alu.src[1].sel = tmp1;
5779 alu.src[1].chan = 1;
5780
5781 alu.last = 1;
5782 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5783 return r;
5784
5785 /* 18. tmp0.z = tmp1.x==0 ? tmp0.z : tmp1.z DIV */
5786 /* 18. tmp0.z = tmp1.x==0 ? tmp0.w : tmp1.z MOD */
5787 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5788 alu.op = ALU_OP3_CNDE_INT;
5789 alu.is_op3 = 1;
5790
5791 alu.dst.sel = tmp0;
5792 alu.dst.chan = 2;
5793 alu.dst.write = 1;
5794
5795 alu.src[0].sel = tmp1;
5796 alu.src[0].chan = 0;
5797 alu.src[1].sel = tmp0;
5798 alu.src[1].chan = mod ? 3 : 2;
5799 alu.src[2].sel = tmp1;
5800 alu.src[2].chan = 2;
5801
5802 alu.last = 1;
5803 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5804 return r;
5805
5806 /* 19. tmp0.z = tmp1.y==0 ? tmp1.w : tmp0.z */
5807 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5808 alu.op = ALU_OP3_CNDE_INT;
5809 alu.is_op3 = 1;
5810
5811 if (signed_op) {
5812 alu.dst.sel = tmp0;
5813 alu.dst.chan = 2;
5814 alu.dst.write = 1;
5815 } else {
5816 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
5817 }
5818
5819 alu.src[0].sel = tmp1;
5820 alu.src[0].chan = 1;
5821 alu.src[1].sel = tmp1;
5822 alu.src[1].chan = 3;
5823 alu.src[2].sel = tmp0;
5824 alu.src[2].chan = 2;
5825
5826 alu.last = 1;
5827 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5828 return r;
5829
5830 if (signed_op) {
5831
5832 /* fix the sign of the result */
5833
5834 if (mod) {
5835
5836 /* tmp0.x = -tmp0.z */
5837 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5838 alu.op = ALU_OP2_SUB_INT;
5839
5840 alu.dst.sel = tmp0;
5841 alu.dst.chan = 0;
5842 alu.dst.write = 1;
5843
5844 alu.src[0].sel = V_SQ_ALU_SRC_0;
5845 alu.src[1].sel = tmp0;
5846 alu.src[1].chan = 2;
5847
5848 alu.last = 1;
5849 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5850 return r;
5851
5852 /* sign of the remainder is the same as the sign of src0 */
5853 /* tmp0.x = src0>=0 ? tmp0.z : tmp0.x */
5854 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5855 alu.op = ALU_OP3_CNDGE_INT;
5856 alu.is_op3 = 1;
5857
5858 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
5859
5860 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
5861 alu.src[1].sel = tmp0;
5862 alu.src[1].chan = 2;
5863 alu.src[2].sel = tmp0;
5864 alu.src[2].chan = 0;
5865
5866 alu.last = 1;
5867 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5868 return r;
5869
5870 } else {
5871
5872 /* tmp0.x = -tmp0.z */
5873 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5874 alu.op = ALU_OP2_SUB_INT;
5875
5876 alu.dst.sel = tmp0;
5877 alu.dst.chan = 0;
5878 alu.dst.write = 1;
5879
5880 alu.src[0].sel = V_SQ_ALU_SRC_0;
5881 alu.src[1].sel = tmp0;
5882 alu.src[1].chan = 2;
5883
5884 alu.last = 1;
5885 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5886 return r;
5887
5888 /* fix the quotient sign (same as the sign of src0*src1) */
5889 /* tmp0.x = tmp2.z>=0 ? tmp0.z : tmp0.x */
5890 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5891 alu.op = ALU_OP3_CNDGE_INT;
5892 alu.is_op3 = 1;
5893
5894 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
5895
5896 alu.src[0].sel = tmp2;
5897 alu.src[0].chan = 2;
5898 alu.src[1].sel = tmp0;
5899 alu.src[1].chan = 2;
5900 alu.src[2].sel = tmp0;
5901 alu.src[2].chan = 0;
5902
5903 alu.last = 1;
5904 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
5905 return r;
5906 }
5907 }
5908 }
5909 return 0;
5910 }
5911
5912 static int tgsi_udiv(struct r600_shader_ctx *ctx)
5913 {
5914 return tgsi_divmod(ctx, 0, 0);
5915 }
5916
5917 static int tgsi_umod(struct r600_shader_ctx *ctx)
5918 {
5919 return tgsi_divmod(ctx, 1, 0);
5920 }
5921
5922 static int tgsi_idiv(struct r600_shader_ctx *ctx)
5923 {
5924 return tgsi_divmod(ctx, 0, 1);
5925 }
5926
5927 static int tgsi_imod(struct r600_shader_ctx *ctx)
5928 {
5929 return tgsi_divmod(ctx, 1, 1);
5930 }
5931
5932
5933 static int tgsi_f2i(struct r600_shader_ctx *ctx)
5934 {
5935 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
5936 struct r600_bytecode_alu alu;
5937 int i, r;
5938 unsigned write_mask = inst->Dst[0].Register.WriteMask;
5939 int last_inst = tgsi_last_instruction(write_mask);
5940
5941 for (i = 0; i < 4; i++) {
5942 if (!(write_mask & (1<<i)))
5943 continue;
5944
5945 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5946 alu.op = ALU_OP1_TRUNC;
5947
5948 alu.dst.sel = ctx->temp_reg;
5949 alu.dst.chan = i;
5950 alu.dst.write = 1;
5951
5952 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
5953 if (i == last_inst)
5954 alu.last = 1;
5955 r = r600_bytecode_add_alu(ctx->bc, &alu);
5956 if (r)
5957 return r;
5958 }
5959
5960 for (i = 0; i < 4; i++) {
5961 if (!(write_mask & (1<<i)))
5962 continue;
5963
5964 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5965 alu.op = ctx->inst_info->op;
5966
5967 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
5968
5969 alu.src[0].sel = ctx->temp_reg;
5970 alu.src[0].chan = i;
5971
5972 if (i == last_inst || alu.op == ALU_OP1_FLT_TO_UINT)
5973 alu.last = 1;
5974 r = r600_bytecode_add_alu(ctx->bc, &alu);
5975 if (r)
5976 return r;
5977 }
5978
5979 return 0;
5980 }
5981
5982 static int tgsi_iabs(struct r600_shader_ctx *ctx)
5983 {
5984 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
5985 struct r600_bytecode_alu alu;
5986 int i, r;
5987 unsigned write_mask = inst->Dst[0].Register.WriteMask;
5988 int last_inst = tgsi_last_instruction(write_mask);
5989
5990 /* tmp = -src */
5991 for (i = 0; i < 4; i++) {
5992 if (!(write_mask & (1<<i)))
5993 continue;
5994
5995 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
5996 alu.op = ALU_OP2_SUB_INT;
5997
5998 alu.dst.sel = ctx->temp_reg;
5999 alu.dst.chan = i;
6000 alu.dst.write = 1;
6001
6002 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
6003 alu.src[0].sel = V_SQ_ALU_SRC_0;
6004
6005 if (i == last_inst)
6006 alu.last = 1;
6007 r = r600_bytecode_add_alu(ctx->bc, &alu);
6008 if (r)
6009 return r;
6010 }
6011
6012 /* dst = (src >= 0 ? src : tmp) */
6013 for (i = 0; i < 4; i++) {
6014 if (!(write_mask & (1<<i)))
6015 continue;
6016
6017 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6018 alu.op = ALU_OP3_CNDGE_INT;
6019 alu.is_op3 = 1;
6020 alu.dst.write = 1;
6021
6022 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6023
6024 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
6025 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
6026 alu.src[2].sel = ctx->temp_reg;
6027 alu.src[2].chan = i;
6028
6029 if (i == last_inst)
6030 alu.last = 1;
6031 r = r600_bytecode_add_alu(ctx->bc, &alu);
6032 if (r)
6033 return r;
6034 }
6035 return 0;
6036 }
6037
6038 static int tgsi_issg(struct r600_shader_ctx *ctx)
6039 {
6040 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6041 struct r600_bytecode_alu alu;
6042 int i, r;
6043 unsigned write_mask = inst->Dst[0].Register.WriteMask;
6044 int last_inst = tgsi_last_instruction(write_mask);
6045
6046 /* tmp = (src >= 0 ? src : -1) */
6047 for (i = 0; i < 4; i++) {
6048 if (!(write_mask & (1<<i)))
6049 continue;
6050
6051 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6052 alu.op = ALU_OP3_CNDGE_INT;
6053 alu.is_op3 = 1;
6054
6055 alu.dst.sel = ctx->temp_reg;
6056 alu.dst.chan = i;
6057 alu.dst.write = 1;
6058
6059 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
6060 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
6061 alu.src[2].sel = V_SQ_ALU_SRC_M_1_INT;
6062
6063 if (i == last_inst)
6064 alu.last = 1;
6065 r = r600_bytecode_add_alu(ctx->bc, &alu);
6066 if (r)
6067 return r;
6068 }
6069
6070 /* dst = (tmp > 0 ? 1 : tmp) */
6071 for (i = 0; i < 4; i++) {
6072 if (!(write_mask & (1<<i)))
6073 continue;
6074
6075 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6076 alu.op = ALU_OP3_CNDGT_INT;
6077 alu.is_op3 = 1;
6078 alu.dst.write = 1;
6079
6080 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6081
6082 alu.src[0].sel = ctx->temp_reg;
6083 alu.src[0].chan = i;
6084
6085 alu.src[1].sel = V_SQ_ALU_SRC_1_INT;
6086
6087 alu.src[2].sel = ctx->temp_reg;
6088 alu.src[2].chan = i;
6089
6090 if (i == last_inst)
6091 alu.last = 1;
6092 r = r600_bytecode_add_alu(ctx->bc, &alu);
6093 if (r)
6094 return r;
6095 }
6096 return 0;
6097 }
6098
6099
6100
6101 static int tgsi_ssg(struct r600_shader_ctx *ctx)
6102 {
6103 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6104 struct r600_bytecode_alu alu;
6105 int i, r;
6106
6107 /* tmp = (src > 0 ? 1 : src) */
6108 for (i = 0; i < 4; i++) {
6109 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6110 alu.op = ALU_OP3_CNDGT;
6111 alu.is_op3 = 1;
6112
6113 alu.dst.sel = ctx->temp_reg;
6114 alu.dst.chan = i;
6115
6116 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
6117 alu.src[1].sel = V_SQ_ALU_SRC_1;
6118 r600_bytecode_src(&alu.src[2], &ctx->src[0], i);
6119
6120 if (i == 3)
6121 alu.last = 1;
6122 r = r600_bytecode_add_alu(ctx->bc, &alu);
6123 if (r)
6124 return r;
6125 }
6126
6127 /* dst = (-tmp > 0 ? -1 : tmp) */
6128 for (i = 0; i < 4; i++) {
6129 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6130 alu.op = ALU_OP3_CNDGT;
6131 alu.is_op3 = 1;
6132 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6133
6134 alu.src[0].sel = ctx->temp_reg;
6135 alu.src[0].chan = i;
6136 alu.src[0].neg = 1;
6137
6138 alu.src[1].sel = V_SQ_ALU_SRC_1;
6139 alu.src[1].neg = 1;
6140
6141 alu.src[2].sel = ctx->temp_reg;
6142 alu.src[2].chan = i;
6143
6144 if (i == 3)
6145 alu.last = 1;
6146 r = r600_bytecode_add_alu(ctx->bc, &alu);
6147 if (r)
6148 return r;
6149 }
6150 return 0;
6151 }
6152
6153 static int tgsi_bfi(struct r600_shader_ctx *ctx)
6154 {
6155 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6156 struct r600_bytecode_alu alu;
6157 int i, r, t1, t2;
6158
6159 unsigned write_mask = inst->Dst[0].Register.WriteMask;
6160 int last_inst = tgsi_last_instruction(write_mask);
6161
6162 t1 = ctx->temp_reg;
6163
6164 for (i = 0; i < 4; i++) {
6165 if (!(write_mask & (1<<i)))
6166 continue;
6167
6168 /* create mask tmp */
6169 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6170 alu.op = ALU_OP2_BFM_INT;
6171 alu.dst.sel = t1;
6172 alu.dst.chan = i;
6173 alu.dst.write = 1;
6174 alu.last = i == last_inst;
6175
6176 r600_bytecode_src(&alu.src[0], &ctx->src[3], i);
6177 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
6178
6179 r = r600_bytecode_add_alu(ctx->bc, &alu);
6180 if (r)
6181 return r;
6182 }
6183
6184 t2 = r600_get_temp(ctx);
6185
6186 for (i = 0; i < 4; i++) {
6187 if (!(write_mask & (1<<i)))
6188 continue;
6189
6190 /* shift insert left */
6191 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6192 alu.op = ALU_OP2_LSHL_INT;
6193 alu.dst.sel = t2;
6194 alu.dst.chan = i;
6195 alu.dst.write = 1;
6196 alu.last = i == last_inst;
6197
6198 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
6199 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
6200
6201 r = r600_bytecode_add_alu(ctx->bc, &alu);
6202 if (r)
6203 return r;
6204 }
6205
6206 for (i = 0; i < 4; i++) {
6207 if (!(write_mask & (1<<i)))
6208 continue;
6209
6210 /* actual bitfield insert */
6211 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6212 alu.op = ALU_OP3_BFI_INT;
6213 alu.is_op3 = 1;
6214 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6215 alu.dst.chan = i;
6216 alu.dst.write = 1;
6217 alu.last = i == last_inst;
6218
6219 alu.src[0].sel = t1;
6220 alu.src[0].chan = i;
6221 alu.src[1].sel = t2;
6222 alu.src[1].chan = i;
6223 r600_bytecode_src(&alu.src[2], &ctx->src[0], i);
6224
6225 r = r600_bytecode_add_alu(ctx->bc, &alu);
6226 if (r)
6227 return r;
6228 }
6229
6230 return 0;
6231 }
6232
6233 static int tgsi_msb(struct r600_shader_ctx *ctx)
6234 {
6235 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6236 struct r600_bytecode_alu alu;
6237 int i, r, t1, t2;
6238
6239 unsigned write_mask = inst->Dst[0].Register.WriteMask;
6240 int last_inst = tgsi_last_instruction(write_mask);
6241
6242 assert(ctx->inst_info->op == ALU_OP1_FFBH_INT ||
6243 ctx->inst_info->op == ALU_OP1_FFBH_UINT);
6244
6245 t1 = ctx->temp_reg;
6246
6247 /* bit position is indexed from lsb by TGSI, and from msb by the hardware */
6248 for (i = 0; i < 4; i++) {
6249 if (!(write_mask & (1<<i)))
6250 continue;
6251
6252 /* t1 = FFBH_INT / FFBH_UINT */
6253 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6254 alu.op = ctx->inst_info->op;
6255 alu.dst.sel = t1;
6256 alu.dst.chan = i;
6257 alu.dst.write = 1;
6258 alu.last = i == last_inst;
6259
6260 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
6261
6262 r = r600_bytecode_add_alu(ctx->bc, &alu);
6263 if (r)
6264 return r;
6265 }
6266
6267 t2 = r600_get_temp(ctx);
6268
6269 for (i = 0; i < 4; i++) {
6270 if (!(write_mask & (1<<i)))
6271 continue;
6272
6273 /* t2 = 31 - t1 */
6274 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6275 alu.op = ALU_OP2_SUB_INT;
6276 alu.dst.sel = t2;
6277 alu.dst.chan = i;
6278 alu.dst.write = 1;
6279 alu.last = i == last_inst;
6280
6281 alu.src[0].sel = V_SQ_ALU_SRC_LITERAL;
6282 alu.src[0].value = 31;
6283 alu.src[1].sel = t1;
6284 alu.src[1].chan = i;
6285
6286 r = r600_bytecode_add_alu(ctx->bc, &alu);
6287 if (r)
6288 return r;
6289 }
6290
6291 for (i = 0; i < 4; i++) {
6292 if (!(write_mask & (1<<i)))
6293 continue;
6294
6295 /* result = t1 >= 0 ? t2 : t1 */
6296 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6297 alu.op = ALU_OP3_CNDGE_INT;
6298 alu.is_op3 = 1;
6299 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6300 alu.dst.chan = i;
6301 alu.dst.write = 1;
6302 alu.last = i == last_inst;
6303
6304 alu.src[0].sel = t1;
6305 alu.src[0].chan = i;
6306 alu.src[1].sel = t2;
6307 alu.src[1].chan = i;
6308 alu.src[2].sel = t1;
6309 alu.src[2].chan = i;
6310
6311 r = r600_bytecode_add_alu(ctx->bc, &alu);
6312 if (r)
6313 return r;
6314 }
6315
6316 return 0;
6317 }
6318
6319 static int tgsi_interp_egcm(struct r600_shader_ctx *ctx)
6320 {
6321 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6322 struct r600_bytecode_alu alu;
6323 int r, i = 0, k, interp_gpr, interp_base_chan, tmp, lasti;
6324 unsigned location;
6325 int input;
6326
6327 assert(inst->Src[0].Register.File == TGSI_FILE_INPUT);
6328
6329 input = inst->Src[0].Register.Index;
6330
6331 /* Interpolators have been marked for use already by allocate_system_value_inputs */
6332 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
6333 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
6334 location = TGSI_INTERPOLATE_LOC_CENTER; /* sample offset will be added explicitly */
6335 }
6336 else {
6337 location = TGSI_INTERPOLATE_LOC_CENTROID;
6338 }
6339
6340 k = eg_get_interpolator_index(ctx->shader->input[input].interpolate, location);
6341 if (k < 0)
6342 k = 0;
6343 interp_gpr = ctx->eg_interpolators[k].ij_index / 2;
6344 interp_base_chan = 2 * (ctx->eg_interpolators[k].ij_index % 2);
6345
6346 /* NOTE: currently offset is not perspective correct */
6347 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
6348 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
6349 int sample_gpr = -1;
6350 int gradientsH, gradientsV;
6351 struct r600_bytecode_tex tex;
6352
6353 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
6354 sample_gpr = load_sample_position(ctx, &ctx->src[1], ctx->src[1].swizzle[0]);
6355 }
6356
6357 gradientsH = r600_get_temp(ctx);
6358 gradientsV = r600_get_temp(ctx);
6359 for (i = 0; i < 2; i++) {
6360 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
6361 tex.op = i == 0 ? FETCH_OP_GET_GRADIENTS_H : FETCH_OP_GET_GRADIENTS_V;
6362 tex.src_gpr = interp_gpr;
6363 tex.src_sel_x = interp_base_chan + 0;
6364 tex.src_sel_y = interp_base_chan + 1;
6365 tex.src_sel_z = 0;
6366 tex.src_sel_w = 0;
6367 tex.dst_gpr = i == 0 ? gradientsH : gradientsV;
6368 tex.dst_sel_x = 0;
6369 tex.dst_sel_y = 1;
6370 tex.dst_sel_z = 7;
6371 tex.dst_sel_w = 7;
6372 tex.inst_mod = 1; // Use per pixel gradient calculation
6373 tex.sampler_id = 0;
6374 tex.resource_id = tex.sampler_id;
6375 r = r600_bytecode_add_tex(ctx->bc, &tex);
6376 if (r)
6377 return r;
6378 }
6379
6380 for (i = 0; i < 2; i++) {
6381 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6382 alu.op = ALU_OP3_MULADD;
6383 alu.is_op3 = 1;
6384 alu.src[0].sel = gradientsH;
6385 alu.src[0].chan = i;
6386 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
6387 alu.src[1].sel = sample_gpr;
6388 alu.src[1].chan = 2;
6389 }
6390 else {
6391 r600_bytecode_src(&alu.src[1], &ctx->src[1], 0);
6392 }
6393 alu.src[2].sel = interp_gpr;
6394 alu.src[2].chan = interp_base_chan + i;
6395 alu.dst.sel = ctx->temp_reg;
6396 alu.dst.chan = i;
6397 alu.last = i == 1;
6398
6399 r = r600_bytecode_add_alu(ctx->bc, &alu);
6400 if (r)
6401 return r;
6402 }
6403
6404 for (i = 0; i < 2; i++) {
6405 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6406 alu.op = ALU_OP3_MULADD;
6407 alu.is_op3 = 1;
6408 alu.src[0].sel = gradientsV;
6409 alu.src[0].chan = i;
6410 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
6411 alu.src[1].sel = sample_gpr;
6412 alu.src[1].chan = 3;
6413 }
6414 else {
6415 r600_bytecode_src(&alu.src[1], &ctx->src[1], 1);
6416 }
6417 alu.src[2].sel = ctx->temp_reg;
6418 alu.src[2].chan = i;
6419 alu.dst.sel = ctx->temp_reg;
6420 alu.dst.chan = i;
6421 alu.last = i == 1;
6422
6423 r = r600_bytecode_add_alu(ctx->bc, &alu);
6424 if (r)
6425 return r;
6426 }
6427 }
6428
6429 tmp = r600_get_temp(ctx);
6430 for (i = 0; i < 8; i++) {
6431 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6432 alu.op = i < 4 ? ALU_OP2_INTERP_ZW : ALU_OP2_INTERP_XY;
6433
6434 alu.dst.sel = tmp;
6435 if ((i > 1 && i < 6)) {
6436 alu.dst.write = 1;
6437 }
6438 else {
6439 alu.dst.write = 0;
6440 }
6441 alu.dst.chan = i % 4;
6442
6443 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
6444 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
6445 alu.src[0].sel = ctx->temp_reg;
6446 alu.src[0].chan = 1 - (i % 2);
6447 } else {
6448 alu.src[0].sel = interp_gpr;
6449 alu.src[0].chan = interp_base_chan + 1 - (i % 2);
6450 }
6451 alu.src[1].sel = V_SQ_ALU_SRC_PARAM_BASE + ctx->shader->input[input].lds_pos;
6452 alu.src[1].chan = 0;
6453
6454 alu.last = i % 4 == 3;
6455 alu.bank_swizzle_force = SQ_ALU_VEC_210;
6456
6457 r = r600_bytecode_add_alu(ctx->bc, &alu);
6458 if (r)
6459 return r;
6460 }
6461
6462 // INTERP can't swizzle dst
6463 lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
6464 for (i = 0; i <= lasti; i++) {
6465 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
6466 continue;
6467
6468 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6469 alu.op = ALU_OP1_MOV;
6470 alu.src[0].sel = tmp;
6471 alu.src[0].chan = ctx->src[0].swizzle[i];
6472 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6473 alu.dst.write = 1;
6474 alu.last = i == lasti;
6475 r = r600_bytecode_add_alu(ctx->bc, &alu);
6476 if (r)
6477 return r;
6478 }
6479
6480 return 0;
6481 }
6482
6483
6484 static int tgsi_helper_copy(struct r600_shader_ctx *ctx, struct tgsi_full_instruction *inst)
6485 {
6486 struct r600_bytecode_alu alu;
6487 int i, r;
6488
6489 for (i = 0; i < 4; i++) {
6490 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6491 if (!(inst->Dst[0].Register.WriteMask & (1 << i))) {
6492 alu.op = ALU_OP0_NOP;
6493 alu.dst.chan = i;
6494 } else {
6495 alu.op = ALU_OP1_MOV;
6496 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6497 alu.src[0].sel = ctx->temp_reg;
6498 alu.src[0].chan = i;
6499 }
6500 if (i == 3) {
6501 alu.last = 1;
6502 }
6503 r = r600_bytecode_add_alu(ctx->bc, &alu);
6504 if (r)
6505 return r;
6506 }
6507 return 0;
6508 }
6509
6510 static int tgsi_make_src_for_op3(struct r600_shader_ctx *ctx,
6511 unsigned temp, int chan,
6512 struct r600_bytecode_alu_src *bc_src,
6513 const struct r600_shader_src *shader_src)
6514 {
6515 struct r600_bytecode_alu alu;
6516 int r;
6517
6518 r600_bytecode_src(bc_src, shader_src, chan);
6519
6520 /* op3 operands don't support abs modifier */
6521 if (bc_src->abs) {
6522 assert(temp!=0); /* we actually need the extra register, make sure it is allocated. */
6523 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6524 alu.op = ALU_OP1_MOV;
6525 alu.dst.sel = temp;
6526 alu.dst.chan = chan;
6527 alu.dst.write = 1;
6528
6529 alu.src[0] = *bc_src;
6530 alu.last = true; // sufficient?
6531 r = r600_bytecode_add_alu(ctx->bc, &alu);
6532 if (r)
6533 return r;
6534
6535 memset(bc_src, 0, sizeof(*bc_src));
6536 bc_src->sel = temp;
6537 bc_src->chan = chan;
6538 }
6539 return 0;
6540 }
6541
6542 static int tgsi_op3(struct r600_shader_ctx *ctx)
6543 {
6544 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6545 struct r600_bytecode_alu alu;
6546 int i, j, r;
6547 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
6548 int temp_regs[4];
6549 unsigned op = ctx->inst_info->op;
6550
6551 if (op == ALU_OP3_MULADD_IEEE &&
6552 ctx->info.properties[TGSI_PROPERTY_MUL_ZERO_WINS])
6553 op = ALU_OP3_MULADD;
6554
6555 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
6556 temp_regs[j] = 0;
6557 if (ctx->src[j].abs)
6558 temp_regs[j] = r600_get_temp(ctx);
6559 }
6560 for (i = 0; i < lasti + 1; i++) {
6561 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
6562 continue;
6563
6564 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6565 alu.op = op;
6566 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
6567 r = tgsi_make_src_for_op3(ctx, temp_regs[j], i, &alu.src[j], &ctx->src[j]);
6568 if (r)
6569 return r;
6570 }
6571
6572 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6573 alu.dst.chan = i;
6574 alu.dst.write = 1;
6575 alu.is_op3 = 1;
6576 if (i == lasti) {
6577 alu.last = 1;
6578 }
6579 r = r600_bytecode_add_alu(ctx->bc, &alu);
6580 if (r)
6581 return r;
6582 }
6583 return 0;
6584 }
6585
6586 static int tgsi_dp(struct r600_shader_ctx *ctx)
6587 {
6588 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6589 struct r600_bytecode_alu alu;
6590 int i, j, r;
6591 unsigned op = ctx->inst_info->op;
6592 if (op == ALU_OP2_DOT4_IEEE &&
6593 ctx->info.properties[TGSI_PROPERTY_MUL_ZERO_WINS])
6594 op = ALU_OP2_DOT4;
6595
6596 for (i = 0; i < 4; i++) {
6597 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6598 alu.op = op;
6599 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
6600 r600_bytecode_src(&alu.src[j], &ctx->src[j], i);
6601 }
6602
6603 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
6604 alu.dst.chan = i;
6605 alu.dst.write = (inst->Dst[0].Register.WriteMask >> i) & 1;
6606 /* handle some special cases */
6607 switch (inst->Instruction.Opcode) {
6608 case TGSI_OPCODE_DP2:
6609 if (i > 1) {
6610 alu.src[0].sel = alu.src[1].sel = V_SQ_ALU_SRC_0;
6611 alu.src[0].chan = alu.src[1].chan = 0;
6612 }
6613 break;
6614 case TGSI_OPCODE_DP3:
6615 if (i > 2) {
6616 alu.src[0].sel = alu.src[1].sel = V_SQ_ALU_SRC_0;
6617 alu.src[0].chan = alu.src[1].chan = 0;
6618 }
6619 break;
6620 case TGSI_OPCODE_DPH:
6621 if (i == 3) {
6622 alu.src[0].sel = V_SQ_ALU_SRC_1;
6623 alu.src[0].chan = 0;
6624 alu.src[0].neg = 0;
6625 }
6626 break;
6627 default:
6628 break;
6629 }
6630 if (i == 3) {
6631 alu.last = 1;
6632 }
6633 r = r600_bytecode_add_alu(ctx->bc, &alu);
6634 if (r)
6635 return r;
6636 }
6637 return 0;
6638 }
6639
6640 static inline boolean tgsi_tex_src_requires_loading(struct r600_shader_ctx *ctx,
6641 unsigned index)
6642 {
6643 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6644 return (inst->Src[index].Register.File != TGSI_FILE_TEMPORARY &&
6645 inst->Src[index].Register.File != TGSI_FILE_INPUT &&
6646 inst->Src[index].Register.File != TGSI_FILE_OUTPUT) ||
6647 ctx->src[index].neg || ctx->src[index].abs ||
6648 (inst->Src[index].Register.File == TGSI_FILE_INPUT && ctx->type == PIPE_SHADER_GEOMETRY);
6649 }
6650
6651 static inline unsigned tgsi_tex_get_src_gpr(struct r600_shader_ctx *ctx,
6652 unsigned index)
6653 {
6654 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6655 return ctx->file_offset[inst->Src[index].Register.File] + inst->Src[index].Register.Index;
6656 }
6657
6658 static int do_vtx_fetch_inst(struct r600_shader_ctx *ctx, boolean src_requires_loading)
6659 {
6660 struct r600_bytecode_vtx vtx;
6661 struct r600_bytecode_alu alu;
6662 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6663 int src_gpr, r, i;
6664 int id = tgsi_tex_get_src_gpr(ctx, 1);
6665
6666 src_gpr = tgsi_tex_get_src_gpr(ctx, 0);
6667 if (src_requires_loading) {
6668 for (i = 0; i < 4; i++) {
6669 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6670 alu.op = ALU_OP1_MOV;
6671 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
6672 alu.dst.sel = ctx->temp_reg;
6673 alu.dst.chan = i;
6674 if (i == 3)
6675 alu.last = 1;
6676 alu.dst.write = 1;
6677 r = r600_bytecode_add_alu(ctx->bc, &alu);
6678 if (r)
6679 return r;
6680 }
6681 src_gpr = ctx->temp_reg;
6682 }
6683
6684 memset(&vtx, 0, sizeof(vtx));
6685 vtx.op = FETCH_OP_VFETCH;
6686 vtx.buffer_id = id + R600_MAX_CONST_BUFFERS;
6687 vtx.fetch_type = SQ_VTX_FETCH_NO_INDEX_OFFSET;
6688 vtx.src_gpr = src_gpr;
6689 vtx.mega_fetch_count = 16;
6690 vtx.dst_gpr = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
6691 vtx.dst_sel_x = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7; /* SEL_X */
6692 vtx.dst_sel_y = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7; /* SEL_Y */
6693 vtx.dst_sel_z = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7; /* SEL_Z */
6694 vtx.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7; /* SEL_W */
6695 vtx.use_const_fields = 1;
6696
6697 if ((r = r600_bytecode_add_vtx(ctx->bc, &vtx)))
6698 return r;
6699
6700 if (ctx->bc->chip_class >= EVERGREEN)
6701 return 0;
6702
6703 for (i = 0; i < 4; i++) {
6704 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
6705 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
6706 continue;
6707
6708 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6709 alu.op = ALU_OP2_AND_INT;
6710
6711 alu.dst.chan = i;
6712 alu.dst.sel = vtx.dst_gpr;
6713 alu.dst.write = 1;
6714
6715 alu.src[0].sel = vtx.dst_gpr;
6716 alu.src[0].chan = i;
6717
6718 alu.src[1].sel = R600_SHADER_BUFFER_INFO_SEL;
6719 alu.src[1].sel += (id * 2);
6720 alu.src[1].chan = i % 4;
6721 alu.src[1].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
6722
6723 if (i == lasti)
6724 alu.last = 1;
6725 r = r600_bytecode_add_alu(ctx->bc, &alu);
6726 if (r)
6727 return r;
6728 }
6729
6730 if (inst->Dst[0].Register.WriteMask & 3) {
6731 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6732 alu.op = ALU_OP2_OR_INT;
6733
6734 alu.dst.chan = 3;
6735 alu.dst.sel = vtx.dst_gpr;
6736 alu.dst.write = 1;
6737
6738 alu.src[0].sel = vtx.dst_gpr;
6739 alu.src[0].chan = 3;
6740
6741 alu.src[1].sel = R600_SHADER_BUFFER_INFO_SEL + (id * 2) + 1;
6742 alu.src[1].chan = 0;
6743 alu.src[1].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
6744
6745 alu.last = 1;
6746 r = r600_bytecode_add_alu(ctx->bc, &alu);
6747 if (r)
6748 return r;
6749 }
6750 return 0;
6751 }
6752
6753 static int r600_do_buffer_txq(struct r600_shader_ctx *ctx)
6754 {
6755 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6756 struct r600_bytecode_alu alu;
6757 int r;
6758 int id = tgsi_tex_get_src_gpr(ctx, 1);
6759
6760 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6761 alu.op = ALU_OP1_MOV;
6762 alu.src[0].sel = R600_SHADER_BUFFER_INFO_SEL;
6763 if (ctx->bc->chip_class >= EVERGREEN) {
6764 /* channel 0 or 2 of each word */
6765 alu.src[0].sel += (id / 2);
6766 alu.src[0].chan = (id % 2) * 2;
6767 } else {
6768 /* r600 we have them at channel 2 of the second dword */
6769 alu.src[0].sel += (id * 2) + 1;
6770 alu.src[0].chan = 1;
6771 }
6772 alu.src[0].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
6773 tgsi_dst(ctx, &inst->Dst[0], 0, &alu.dst);
6774 alu.last = 1;
6775 r = r600_bytecode_add_alu(ctx->bc, &alu);
6776 if (r)
6777 return r;
6778 return 0;
6779 }
6780
6781 static int tgsi_tex(struct r600_shader_ctx *ctx)
6782 {
6783 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
6784 struct r600_bytecode_tex tex;
6785 struct r600_bytecode_alu alu;
6786 unsigned src_gpr;
6787 int r, i, j;
6788 int opcode;
6789 bool read_compressed_msaa = ctx->bc->has_compressed_msaa_texturing &&
6790 inst->Instruction.Opcode == TGSI_OPCODE_TXF &&
6791 (inst->Texture.Texture == TGSI_TEXTURE_2D_MSAA ||
6792 inst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY_MSAA);
6793
6794 bool txf_add_offsets = inst->Texture.NumOffsets &&
6795 inst->Instruction.Opcode == TGSI_OPCODE_TXF &&
6796 inst->Texture.Texture != TGSI_TEXTURE_BUFFER;
6797
6798 /* Texture fetch instructions can only use gprs as source.
6799 * Also they cannot negate the source or take the absolute value */
6800 const boolean src_requires_loading = (inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ &&
6801 inst->Instruction.Opcode != TGSI_OPCODE_TXQS &&
6802 tgsi_tex_src_requires_loading(ctx, 0)) ||
6803 read_compressed_msaa || txf_add_offsets;
6804
6805 boolean src_loaded = FALSE;
6806 unsigned sampler_src_reg = inst->Instruction.Opcode == TGSI_OPCODE_TXQ_LZ ? 0 : 1;
6807 int8_t offset_x = 0, offset_y = 0, offset_z = 0;
6808 boolean has_txq_cube_array_z = false;
6809 unsigned sampler_index_mode;
6810
6811 if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ &&
6812 ((inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
6813 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY)))
6814 if (inst->Dst[0].Register.WriteMask & 4) {
6815 ctx->shader->has_txq_cube_array_z_comp = true;
6816 has_txq_cube_array_z = true;
6817 }
6818
6819 if (inst->Instruction.Opcode == TGSI_OPCODE_TEX2 ||
6820 inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
6821 inst->Instruction.Opcode == TGSI_OPCODE_TXL2 ||
6822 inst->Instruction.Opcode == TGSI_OPCODE_TG4)
6823 sampler_src_reg = 2;
6824
6825 /* TGSI moves the sampler to src reg 3 for TXD */
6826 if (inst->Instruction.Opcode == TGSI_OPCODE_TXD)
6827 sampler_src_reg = 3;
6828
6829 sampler_index_mode = inst->Src[sampler_src_reg].Indirect.Index == 2 ? 2 : 0; // CF_INDEX_1 : CF_INDEX_NONE
6830
6831 src_gpr = tgsi_tex_get_src_gpr(ctx, 0);
6832
6833 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
6834 if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ) {
6835 ctx->shader->uses_tex_buffers = true;
6836 return r600_do_buffer_txq(ctx);
6837 }
6838 else if (inst->Instruction.Opcode == TGSI_OPCODE_TXF) {
6839 if (ctx->bc->chip_class < EVERGREEN)
6840 ctx->shader->uses_tex_buffers = true;
6841 return do_vtx_fetch_inst(ctx, src_requires_loading);
6842 }
6843 }
6844
6845 if (inst->Instruction.Opcode == TGSI_OPCODE_TXP) {
6846 int out_chan;
6847 /* Add perspective divide */
6848 if (ctx->bc->chip_class == CAYMAN) {
6849 out_chan = 2;
6850 for (i = 0; i < 3; i++) {
6851 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6852 alu.op = ALU_OP1_RECIP_IEEE;
6853 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
6854
6855 alu.dst.sel = ctx->temp_reg;
6856 alu.dst.chan = i;
6857 if (i == 2)
6858 alu.last = 1;
6859 if (out_chan == i)
6860 alu.dst.write = 1;
6861 r = r600_bytecode_add_alu(ctx->bc, &alu);
6862 if (r)
6863 return r;
6864 }
6865
6866 } else {
6867 out_chan = 3;
6868 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6869 alu.op = ALU_OP1_RECIP_IEEE;
6870 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
6871
6872 alu.dst.sel = ctx->temp_reg;
6873 alu.dst.chan = out_chan;
6874 alu.last = 1;
6875 alu.dst.write = 1;
6876 r = r600_bytecode_add_alu(ctx->bc, &alu);
6877 if (r)
6878 return r;
6879 }
6880
6881 for (i = 0; i < 3; i++) {
6882 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6883 alu.op = ALU_OP2_MUL;
6884 alu.src[0].sel = ctx->temp_reg;
6885 alu.src[0].chan = out_chan;
6886 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
6887 alu.dst.sel = ctx->temp_reg;
6888 alu.dst.chan = i;
6889 alu.dst.write = 1;
6890 r = r600_bytecode_add_alu(ctx->bc, &alu);
6891 if (r)
6892 return r;
6893 }
6894 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6895 alu.op = ALU_OP1_MOV;
6896 alu.src[0].sel = V_SQ_ALU_SRC_1;
6897 alu.src[0].chan = 0;
6898 alu.dst.sel = ctx->temp_reg;
6899 alu.dst.chan = 3;
6900 alu.last = 1;
6901 alu.dst.write = 1;
6902 r = r600_bytecode_add_alu(ctx->bc, &alu);
6903 if (r)
6904 return r;
6905 src_loaded = TRUE;
6906 src_gpr = ctx->temp_reg;
6907 }
6908
6909
6910 if ((inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
6911 inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
6912 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
6913 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
6914 inst->Instruction.Opcode != TGSI_OPCODE_TXQ &&
6915 inst->Instruction.Opcode != TGSI_OPCODE_TXQ_LZ) {
6916
6917 static const unsigned src0_swizzle[] = {2, 2, 0, 1};
6918 static const unsigned src1_swizzle[] = {1, 0, 2, 2};
6919
6920 /* tmp1.xyzw = CUBE(R0.zzxy, R0.yxzz) */
6921 for (i = 0; i < 4; i++) {
6922 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6923 alu.op = ALU_OP2_CUBE;
6924 r600_bytecode_src(&alu.src[0], &ctx->src[0], src0_swizzle[i]);
6925 r600_bytecode_src(&alu.src[1], &ctx->src[0], src1_swizzle[i]);
6926 alu.dst.sel = ctx->temp_reg;
6927 alu.dst.chan = i;
6928 if (i == 3)
6929 alu.last = 1;
6930 alu.dst.write = 1;
6931 r = r600_bytecode_add_alu(ctx->bc, &alu);
6932 if (r)
6933 return r;
6934 }
6935
6936 /* tmp1.z = RCP_e(|tmp1.z|) */
6937 if (ctx->bc->chip_class == CAYMAN) {
6938 for (i = 0; i < 3; i++) {
6939 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6940 alu.op = ALU_OP1_RECIP_IEEE;
6941 alu.src[0].sel = ctx->temp_reg;
6942 alu.src[0].chan = 2;
6943 alu.src[0].abs = 1;
6944 alu.dst.sel = ctx->temp_reg;
6945 alu.dst.chan = i;
6946 if (i == 2)
6947 alu.dst.write = 1;
6948 if (i == 2)
6949 alu.last = 1;
6950 r = r600_bytecode_add_alu(ctx->bc, &alu);
6951 if (r)
6952 return r;
6953 }
6954 } else {
6955 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6956 alu.op = ALU_OP1_RECIP_IEEE;
6957 alu.src[0].sel = ctx->temp_reg;
6958 alu.src[0].chan = 2;
6959 alu.src[0].abs = 1;
6960 alu.dst.sel = ctx->temp_reg;
6961 alu.dst.chan = 2;
6962 alu.dst.write = 1;
6963 alu.last = 1;
6964 r = r600_bytecode_add_alu(ctx->bc, &alu);
6965 if (r)
6966 return r;
6967 }
6968
6969 /* MULADD R0.x, R0.x, PS1, (0x3FC00000, 1.5f).x
6970 * MULADD R0.y, R0.y, PS1, (0x3FC00000, 1.5f).x
6971 * muladd has no writemask, have to use another temp
6972 */
6973 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6974 alu.op = ALU_OP3_MULADD;
6975 alu.is_op3 = 1;
6976
6977 alu.src[0].sel = ctx->temp_reg;
6978 alu.src[0].chan = 0;
6979 alu.src[1].sel = ctx->temp_reg;
6980 alu.src[1].chan = 2;
6981
6982 alu.src[2].sel = V_SQ_ALU_SRC_LITERAL;
6983 alu.src[2].chan = 0;
6984 alu.src[2].value = u_bitcast_f2u(1.5f);
6985
6986 alu.dst.sel = ctx->temp_reg;
6987 alu.dst.chan = 0;
6988 alu.dst.write = 1;
6989
6990 r = r600_bytecode_add_alu(ctx->bc, &alu);
6991 if (r)
6992 return r;
6993
6994 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
6995 alu.op = ALU_OP3_MULADD;
6996 alu.is_op3 = 1;
6997
6998 alu.src[0].sel = ctx->temp_reg;
6999 alu.src[0].chan = 1;
7000 alu.src[1].sel = ctx->temp_reg;
7001 alu.src[1].chan = 2;
7002
7003 alu.src[2].sel = V_SQ_ALU_SRC_LITERAL;
7004 alu.src[2].chan = 0;
7005 alu.src[2].value = u_bitcast_f2u(1.5f);
7006
7007 alu.dst.sel = ctx->temp_reg;
7008 alu.dst.chan = 1;
7009 alu.dst.write = 1;
7010
7011 alu.last = 1;
7012 r = r600_bytecode_add_alu(ctx->bc, &alu);
7013 if (r)
7014 return r;
7015 /* write initial compare value into Z component
7016 - W src 0 for shadow cube
7017 - X src 1 for shadow cube array */
7018 if (inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
7019 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
7020 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7021 alu.op = ALU_OP1_MOV;
7022 if (inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
7023 r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
7024 else
7025 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
7026 alu.dst.sel = ctx->temp_reg;
7027 alu.dst.chan = 2;
7028 alu.dst.write = 1;
7029 alu.last = 1;
7030 r = r600_bytecode_add_alu(ctx->bc, &alu);
7031 if (r)
7032 return r;
7033 }
7034
7035 if (inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
7036 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
7037 if (ctx->bc->chip_class >= EVERGREEN) {
7038 int mytmp = r600_get_temp(ctx);
7039 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7040 alu.op = ALU_OP1_MOV;
7041 alu.src[0].sel = ctx->temp_reg;
7042 alu.src[0].chan = 3;
7043 alu.dst.sel = mytmp;
7044 alu.dst.chan = 0;
7045 alu.dst.write = 1;
7046 alu.last = 1;
7047 r = r600_bytecode_add_alu(ctx->bc, &alu);
7048 if (r)
7049 return r;
7050
7051 /* have to multiply original layer by 8 and add to face id (temp.w) in Z */
7052 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7053 alu.op = ALU_OP3_MULADD;
7054 alu.is_op3 = 1;
7055 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
7056 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7057 alu.src[1].chan = 0;
7058 alu.src[1].value = u_bitcast_f2u(8.0f);
7059 alu.src[2].sel = mytmp;
7060 alu.src[2].chan = 0;
7061 alu.dst.sel = ctx->temp_reg;
7062 alu.dst.chan = 3;
7063 alu.dst.write = 1;
7064 alu.last = 1;
7065 r = r600_bytecode_add_alu(ctx->bc, &alu);
7066 if (r)
7067 return r;
7068 } else if (ctx->bc->chip_class < EVERGREEN) {
7069 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
7070 tex.op = FETCH_OP_SET_CUBEMAP_INDEX;
7071 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
7072 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
7073 tex.src_gpr = r600_get_temp(ctx);
7074 tex.src_sel_x = 0;
7075 tex.src_sel_y = 0;
7076 tex.src_sel_z = 0;
7077 tex.src_sel_w = 0;
7078 tex.dst_sel_x = tex.dst_sel_y = tex.dst_sel_z = tex.dst_sel_w = 7;
7079 tex.coord_type_x = 1;
7080 tex.coord_type_y = 1;
7081 tex.coord_type_z = 1;
7082 tex.coord_type_w = 1;
7083 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7084 alu.op = ALU_OP1_MOV;
7085 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
7086 alu.dst.sel = tex.src_gpr;
7087 alu.dst.chan = 0;
7088 alu.last = 1;
7089 alu.dst.write = 1;
7090 r = r600_bytecode_add_alu(ctx->bc, &alu);
7091 if (r)
7092 return r;
7093
7094 r = r600_bytecode_add_tex(ctx->bc, &tex);
7095 if (r)
7096 return r;
7097 }
7098
7099 }
7100
7101 /* for cube forms of lod and bias we need to route things */
7102 if (inst->Instruction.Opcode == TGSI_OPCODE_TXB ||
7103 inst->Instruction.Opcode == TGSI_OPCODE_TXL ||
7104 inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
7105 inst->Instruction.Opcode == TGSI_OPCODE_TXL2) {
7106 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7107 alu.op = ALU_OP1_MOV;
7108 if (inst->Instruction.Opcode == TGSI_OPCODE_TXB2 ||
7109 inst->Instruction.Opcode == TGSI_OPCODE_TXL2)
7110 r600_bytecode_src(&alu.src[0], &ctx->src[1], 0);
7111 else
7112 r600_bytecode_src(&alu.src[0], &ctx->src[0], 3);
7113 alu.dst.sel = ctx->temp_reg;
7114 alu.dst.chan = 2;
7115 alu.last = 1;
7116 alu.dst.write = 1;
7117 r = r600_bytecode_add_alu(ctx->bc, &alu);
7118 if (r)
7119 return r;
7120 }
7121
7122 src_loaded = TRUE;
7123 src_gpr = ctx->temp_reg;
7124 }
7125
7126 if (inst->Instruction.Opcode == TGSI_OPCODE_TXD) {
7127 int temp_h = 0, temp_v = 0;
7128 int start_val = 0;
7129
7130 /* if we've already loaded the src (i.e. CUBE don't reload it). */
7131 if (src_loaded == TRUE)
7132 start_val = 1;
7133 else
7134 src_loaded = TRUE;
7135 for (i = start_val; i < 3; i++) {
7136 int treg = r600_get_temp(ctx);
7137
7138 if (i == 0)
7139 src_gpr = treg;
7140 else if (i == 1)
7141 temp_h = treg;
7142 else
7143 temp_v = treg;
7144
7145 for (j = 0; j < 4; j++) {
7146 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7147 alu.op = ALU_OP1_MOV;
7148 r600_bytecode_src(&alu.src[0], &ctx->src[i], j);
7149 alu.dst.sel = treg;
7150 alu.dst.chan = j;
7151 if (j == 3)
7152 alu.last = 1;
7153 alu.dst.write = 1;
7154 r = r600_bytecode_add_alu(ctx->bc, &alu);
7155 if (r)
7156 return r;
7157 }
7158 }
7159 for (i = 1; i < 3; i++) {
7160 /* set gradients h/v */
7161 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
7162 tex.op = (i == 1) ? FETCH_OP_SET_GRADIENTS_H :
7163 FETCH_OP_SET_GRADIENTS_V;
7164 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
7165 tex.sampler_index_mode = sampler_index_mode;
7166 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
7167 tex.resource_index_mode = sampler_index_mode;
7168
7169 tex.src_gpr = (i == 1) ? temp_h : temp_v;
7170 tex.src_sel_x = 0;
7171 tex.src_sel_y = 1;
7172 tex.src_sel_z = 2;
7173 tex.src_sel_w = 3;
7174
7175 tex.dst_gpr = r600_get_temp(ctx); /* just to avoid confusing the asm scheduler */
7176 tex.dst_sel_x = tex.dst_sel_y = tex.dst_sel_z = tex.dst_sel_w = 7;
7177 if (inst->Texture.Texture != TGSI_TEXTURE_RECT) {
7178 tex.coord_type_x = 1;
7179 tex.coord_type_y = 1;
7180 tex.coord_type_z = 1;
7181 tex.coord_type_w = 1;
7182 }
7183 r = r600_bytecode_add_tex(ctx->bc, &tex);
7184 if (r)
7185 return r;
7186 }
7187 }
7188
7189 if (src_requires_loading && !src_loaded) {
7190 for (i = 0; i < 4; i++) {
7191 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7192 alu.op = ALU_OP1_MOV;
7193 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
7194 alu.dst.sel = ctx->temp_reg;
7195 alu.dst.chan = i;
7196 if (i == 3)
7197 alu.last = 1;
7198 alu.dst.write = 1;
7199 r = r600_bytecode_add_alu(ctx->bc, &alu);
7200 if (r)
7201 return r;
7202 }
7203 src_loaded = TRUE;
7204 src_gpr = ctx->temp_reg;
7205 }
7206
7207 /* get offset values */
7208 if (inst->Texture.NumOffsets) {
7209 assert(inst->Texture.NumOffsets == 1);
7210
7211 /* The texture offset feature doesn't work with the TXF instruction
7212 * and must be emulated by adding the offset to the texture coordinates. */
7213 if (txf_add_offsets) {
7214 const struct tgsi_texture_offset *off = inst->TexOffsets;
7215
7216 switch (inst->Texture.Texture) {
7217 case TGSI_TEXTURE_3D:
7218 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7219 alu.op = ALU_OP2_ADD_INT;
7220 alu.src[0].sel = src_gpr;
7221 alu.src[0].chan = 2;
7222 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7223 alu.src[1].value = ctx->literals[4 * off[0].Index + off[0].SwizzleZ];
7224 alu.dst.sel = src_gpr;
7225 alu.dst.chan = 2;
7226 alu.dst.write = 1;
7227 alu.last = 1;
7228 r = r600_bytecode_add_alu(ctx->bc, &alu);
7229 if (r)
7230 return r;
7231 /* fall through */
7232
7233 case TGSI_TEXTURE_2D:
7234 case TGSI_TEXTURE_SHADOW2D:
7235 case TGSI_TEXTURE_RECT:
7236 case TGSI_TEXTURE_SHADOWRECT:
7237 case TGSI_TEXTURE_2D_ARRAY:
7238 case TGSI_TEXTURE_SHADOW2D_ARRAY:
7239 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7240 alu.op = ALU_OP2_ADD_INT;
7241 alu.src[0].sel = src_gpr;
7242 alu.src[0].chan = 1;
7243 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7244 alu.src[1].value = ctx->literals[4 * off[0].Index + off[0].SwizzleY];
7245 alu.dst.sel = src_gpr;
7246 alu.dst.chan = 1;
7247 alu.dst.write = 1;
7248 alu.last = 1;
7249 r = r600_bytecode_add_alu(ctx->bc, &alu);
7250 if (r)
7251 return r;
7252 /* fall through */
7253
7254 case TGSI_TEXTURE_1D:
7255 case TGSI_TEXTURE_SHADOW1D:
7256 case TGSI_TEXTURE_1D_ARRAY:
7257 case TGSI_TEXTURE_SHADOW1D_ARRAY:
7258 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7259 alu.op = ALU_OP2_ADD_INT;
7260 alu.src[0].sel = src_gpr;
7261 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7262 alu.src[1].value = ctx->literals[4 * off[0].Index + off[0].SwizzleX];
7263 alu.dst.sel = src_gpr;
7264 alu.dst.write = 1;
7265 alu.last = 1;
7266 r = r600_bytecode_add_alu(ctx->bc, &alu);
7267 if (r)
7268 return r;
7269 break;
7270 /* texture offsets do not apply to other texture targets */
7271 }
7272 } else {
7273 switch (inst->Texture.Texture) {
7274 case TGSI_TEXTURE_3D:
7275 offset_z = ctx->literals[4 * inst->TexOffsets[0].Index + inst->TexOffsets[0].SwizzleZ] << 1;
7276 /* fallthrough */
7277 case TGSI_TEXTURE_2D:
7278 case TGSI_TEXTURE_SHADOW2D:
7279 case TGSI_TEXTURE_RECT:
7280 case TGSI_TEXTURE_SHADOWRECT:
7281 case TGSI_TEXTURE_2D_ARRAY:
7282 case TGSI_TEXTURE_SHADOW2D_ARRAY:
7283 offset_y = ctx->literals[4 * inst->TexOffsets[0].Index + inst->TexOffsets[0].SwizzleY] << 1;
7284 /* fallthrough */
7285 case TGSI_TEXTURE_1D:
7286 case TGSI_TEXTURE_SHADOW1D:
7287 case TGSI_TEXTURE_1D_ARRAY:
7288 case TGSI_TEXTURE_SHADOW1D_ARRAY:
7289 offset_x = ctx->literals[4 * inst->TexOffsets[0].Index + inst->TexOffsets[0].SwizzleX] << 1;
7290 }
7291 }
7292 }
7293
7294 /* Obtain the sample index for reading a compressed MSAA color texture.
7295 * To read the FMASK, we use the ldfptr instruction, which tells us
7296 * where the samples are stored.
7297 * For uncompressed 8x MSAA surfaces, ldfptr should return 0x76543210,
7298 * which is the identity mapping. Each nibble says which physical sample
7299 * should be fetched to get that sample.
7300 *
7301 * Assume src.z contains the sample index. It should be modified like this:
7302 * src.z = (ldfptr() >> (src.z * 4)) & 0xF;
7303 * Then fetch the texel with src.
7304 */
7305 if (read_compressed_msaa) {
7306 unsigned sample_chan = 3;
7307 unsigned temp = r600_get_temp(ctx);
7308 assert(src_loaded);
7309
7310 /* temp.w = ldfptr() */
7311 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
7312 tex.op = FETCH_OP_LD;
7313 tex.inst_mod = 1; /* to indicate this is ldfptr */
7314 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
7315 tex.sampler_index_mode = sampler_index_mode;
7316 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
7317 tex.resource_index_mode = sampler_index_mode;
7318 tex.src_gpr = src_gpr;
7319 tex.dst_gpr = temp;
7320 tex.dst_sel_x = 7; /* mask out these components */
7321 tex.dst_sel_y = 7;
7322 tex.dst_sel_z = 7;
7323 tex.dst_sel_w = 0; /* store X */
7324 tex.src_sel_x = 0;
7325 tex.src_sel_y = 1;
7326 tex.src_sel_z = 2;
7327 tex.src_sel_w = 3;
7328 tex.offset_x = offset_x;
7329 tex.offset_y = offset_y;
7330 tex.offset_z = offset_z;
7331 r = r600_bytecode_add_tex(ctx->bc, &tex);
7332 if (r)
7333 return r;
7334
7335 /* temp.x = sample_index*4 */
7336 if (ctx->bc->chip_class == CAYMAN) {
7337 for (i = 0 ; i < 4; i++) {
7338 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7339 alu.op = ALU_OP2_MULLO_INT;
7340 alu.src[0].sel = src_gpr;
7341 alu.src[0].chan = sample_chan;
7342 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7343 alu.src[1].value = 4;
7344 alu.dst.sel = temp;
7345 alu.dst.chan = i;
7346 alu.dst.write = i == 0;
7347 if (i == 3)
7348 alu.last = 1;
7349 r = r600_bytecode_add_alu(ctx->bc, &alu);
7350 if (r)
7351 return r;
7352 }
7353 } else {
7354 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7355 alu.op = ALU_OP2_MULLO_INT;
7356 alu.src[0].sel = src_gpr;
7357 alu.src[0].chan = sample_chan;
7358 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7359 alu.src[1].value = 4;
7360 alu.dst.sel = temp;
7361 alu.dst.chan = 0;
7362 alu.dst.write = 1;
7363 alu.last = 1;
7364 r = r600_bytecode_add_alu(ctx->bc, &alu);
7365 if (r)
7366 return r;
7367 }
7368
7369 /* sample_index = temp.w >> temp.x */
7370 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7371 alu.op = ALU_OP2_LSHR_INT;
7372 alu.src[0].sel = temp;
7373 alu.src[0].chan = 3;
7374 alu.src[1].sel = temp;
7375 alu.src[1].chan = 0;
7376 alu.dst.sel = src_gpr;
7377 alu.dst.chan = sample_chan;
7378 alu.dst.write = 1;
7379 alu.last = 1;
7380 r = r600_bytecode_add_alu(ctx->bc, &alu);
7381 if (r)
7382 return r;
7383
7384 /* sample_index & 0xF */
7385 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7386 alu.op = ALU_OP2_AND_INT;
7387 alu.src[0].sel = src_gpr;
7388 alu.src[0].chan = sample_chan;
7389 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
7390 alu.src[1].value = 0xF;
7391 alu.dst.sel = src_gpr;
7392 alu.dst.chan = sample_chan;
7393 alu.dst.write = 1;
7394 alu.last = 1;
7395 r = r600_bytecode_add_alu(ctx->bc, &alu);
7396 if (r)
7397 return r;
7398 #if 0
7399 /* visualize the FMASK */
7400 for (i = 0; i < 4; i++) {
7401 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7402 alu.op = ALU_OP1_INT_TO_FLT;
7403 alu.src[0].sel = src_gpr;
7404 alu.src[0].chan = sample_chan;
7405 alu.dst.sel = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
7406 alu.dst.chan = i;
7407 alu.dst.write = 1;
7408 alu.last = 1;
7409 r = r600_bytecode_add_alu(ctx->bc, &alu);
7410 if (r)
7411 return r;
7412 }
7413 return 0;
7414 #endif
7415 }
7416
7417 /* does this shader want a num layers from TXQ for a cube array? */
7418 if (has_txq_cube_array_z) {
7419 int id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
7420
7421 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7422 alu.op = ALU_OP1_MOV;
7423
7424 alu.src[0].sel = R600_SHADER_BUFFER_INFO_SEL;
7425 if (ctx->bc->chip_class >= EVERGREEN) {
7426 /* channel 1 or 3 of each word */
7427 alu.src[0].sel += (id / 2);
7428 alu.src[0].chan = ((id % 2) * 2) + 1;
7429 } else {
7430 /* r600 we have them at channel 2 of the second dword */
7431 alu.src[0].sel += (id * 2) + 1;
7432 alu.src[0].chan = 2;
7433 }
7434 alu.src[0].kc_bank = R600_BUFFER_INFO_CONST_BUFFER;
7435 tgsi_dst(ctx, &inst->Dst[0], 2, &alu.dst);
7436 alu.last = 1;
7437 r = r600_bytecode_add_alu(ctx->bc, &alu);
7438 if (r)
7439 return r;
7440 /* disable writemask from texture instruction */
7441 inst->Dst[0].Register.WriteMask &= ~4;
7442 }
7443
7444 opcode = ctx->inst_info->op;
7445 if (opcode == FETCH_OP_GATHER4 &&
7446 inst->TexOffsets[0].File != TGSI_FILE_NULL &&
7447 inst->TexOffsets[0].File != TGSI_FILE_IMMEDIATE) {
7448 opcode = FETCH_OP_GATHER4_O;
7449
7450 /* GATHER4_O/GATHER4_C_O use offset values loaded by
7451 SET_TEXTURE_OFFSETS instruction. The immediate offset values
7452 encoded in the instruction are ignored. */
7453 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
7454 tex.op = FETCH_OP_SET_TEXTURE_OFFSETS;
7455 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
7456 tex.sampler_index_mode = sampler_index_mode;
7457 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
7458 tex.resource_index_mode = sampler_index_mode;
7459
7460 tex.src_gpr = ctx->file_offset[inst->TexOffsets[0].File] + inst->TexOffsets[0].Index;
7461 tex.src_sel_x = inst->TexOffsets[0].SwizzleX;
7462 tex.src_sel_y = inst->TexOffsets[0].SwizzleY;
7463 tex.src_sel_z = inst->TexOffsets[0].SwizzleZ;
7464 tex.src_sel_w = 4;
7465
7466 tex.dst_sel_x = 7;
7467 tex.dst_sel_y = 7;
7468 tex.dst_sel_z = 7;
7469 tex.dst_sel_w = 7;
7470
7471 r = r600_bytecode_add_tex(ctx->bc, &tex);
7472 if (r)
7473 return r;
7474 }
7475
7476 if (inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D ||
7477 inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D ||
7478 inst->Texture.Texture == TGSI_TEXTURE_SHADOWRECT ||
7479 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
7480 inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY ||
7481 inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY ||
7482 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
7483 switch (opcode) {
7484 case FETCH_OP_SAMPLE:
7485 opcode = FETCH_OP_SAMPLE_C;
7486 break;
7487 case FETCH_OP_SAMPLE_L:
7488 opcode = FETCH_OP_SAMPLE_C_L;
7489 break;
7490 case FETCH_OP_SAMPLE_LB:
7491 opcode = FETCH_OP_SAMPLE_C_LB;
7492 break;
7493 case FETCH_OP_SAMPLE_G:
7494 opcode = FETCH_OP_SAMPLE_C_G;
7495 break;
7496 /* Texture gather variants */
7497 case FETCH_OP_GATHER4:
7498 opcode = FETCH_OP_GATHER4_C;
7499 break;
7500 case FETCH_OP_GATHER4_O:
7501 opcode = FETCH_OP_GATHER4_C_O;
7502 break;
7503 }
7504 }
7505
7506 memset(&tex, 0, sizeof(struct r600_bytecode_tex));
7507 tex.op = opcode;
7508
7509 tex.sampler_id = tgsi_tex_get_src_gpr(ctx, sampler_src_reg);
7510 tex.sampler_index_mode = sampler_index_mode;
7511 tex.resource_id = tex.sampler_id + R600_MAX_CONST_BUFFERS;
7512 tex.resource_index_mode = sampler_index_mode;
7513 tex.src_gpr = src_gpr;
7514 tex.dst_gpr = ctx->file_offset[inst->Dst[0].Register.File] + inst->Dst[0].Register.Index;
7515
7516 if (inst->Instruction.Opcode == TGSI_OPCODE_DDX_FINE ||
7517 inst->Instruction.Opcode == TGSI_OPCODE_DDY_FINE) {
7518 tex.inst_mod = 1; /* per pixel gradient calculation instead of per 2x2 quad */
7519 }
7520
7521 if (inst->Instruction.Opcode == TGSI_OPCODE_TG4) {
7522 int8_t texture_component_select = ctx->literals[4 * inst->Src[1].Register.Index + inst->Src[1].Register.SwizzleX];
7523 tex.inst_mod = texture_component_select;
7524
7525 if (ctx->bc->chip_class == CAYMAN) {
7526 /* GATHER4 result order is different from TGSI TG4 */
7527 tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 2) ? 0 : 7;
7528 tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 4) ? 1 : 7;
7529 tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 1) ? 2 : 7;
7530 tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
7531 } else {
7532 tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
7533 tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;
7534 tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
7535 tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
7536 }
7537 }
7538 else if (inst->Instruction.Opcode == TGSI_OPCODE_LODQ) {
7539 tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
7540 tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
7541 tex.dst_sel_z = 7;
7542 tex.dst_sel_w = 7;
7543 }
7544 else if (inst->Instruction.Opcode == TGSI_OPCODE_TXQS) {
7545 tex.dst_sel_x = 3;
7546 tex.dst_sel_y = 7;
7547 tex.dst_sel_z = 7;
7548 tex.dst_sel_w = 7;
7549 }
7550 else {
7551 tex.dst_sel_x = (inst->Dst[0].Register.WriteMask & 1) ? 0 : 7;
7552 tex.dst_sel_y = (inst->Dst[0].Register.WriteMask & 2) ? 1 : 7;
7553 tex.dst_sel_z = (inst->Dst[0].Register.WriteMask & 4) ? 2 : 7;
7554 tex.dst_sel_w = (inst->Dst[0].Register.WriteMask & 8) ? 3 : 7;
7555 }
7556
7557
7558 if (inst->Instruction.Opcode == TGSI_OPCODE_TXQ_LZ ||
7559 inst->Instruction.Opcode == TGSI_OPCODE_TXQS) {
7560 tex.src_sel_x = 4;
7561 tex.src_sel_y = 4;
7562 tex.src_sel_z = 4;
7563 tex.src_sel_w = 4;
7564 } else if (src_loaded) {
7565 tex.src_sel_x = 0;
7566 tex.src_sel_y = 1;
7567 tex.src_sel_z = 2;
7568 tex.src_sel_w = 3;
7569 } else {
7570 tex.src_sel_x = ctx->src[0].swizzle[0];
7571 tex.src_sel_y = ctx->src[0].swizzle[1];
7572 tex.src_sel_z = ctx->src[0].swizzle[2];
7573 tex.src_sel_w = ctx->src[0].swizzle[3];
7574 tex.src_rel = ctx->src[0].rel;
7575 }
7576
7577 if (inst->Texture.Texture == TGSI_TEXTURE_CUBE ||
7578 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
7579 inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
7580 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
7581 tex.src_sel_x = 1;
7582 tex.src_sel_y = 0;
7583 tex.src_sel_z = 3;
7584 tex.src_sel_w = 2; /* route Z compare or Lod value into W */
7585 }
7586
7587 if (inst->Texture.Texture != TGSI_TEXTURE_RECT &&
7588 inst->Texture.Texture != TGSI_TEXTURE_SHADOWRECT) {
7589 tex.coord_type_x = 1;
7590 tex.coord_type_y = 1;
7591 }
7592 tex.coord_type_z = 1;
7593 tex.coord_type_w = 1;
7594
7595 tex.offset_x = offset_x;
7596 tex.offset_y = offset_y;
7597 if (inst->Instruction.Opcode == TGSI_OPCODE_TG4 &&
7598 (inst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY ||
7599 inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY)) {
7600 tex.offset_z = 0;
7601 }
7602 else {
7603 tex.offset_z = offset_z;
7604 }
7605
7606 /* Put the depth for comparison in W.
7607 * TGSI_TEXTURE_SHADOW2D_ARRAY already has the depth in W.
7608 * Some instructions expect the depth in Z. */
7609 if ((inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D ||
7610 inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D ||
7611 inst->Texture.Texture == TGSI_TEXTURE_SHADOWRECT ||
7612 inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY) &&
7613 opcode != FETCH_OP_SAMPLE_C_L &&
7614 opcode != FETCH_OP_SAMPLE_C_LB) {
7615 tex.src_sel_w = tex.src_sel_z;
7616 }
7617
7618 if (inst->Texture.Texture == TGSI_TEXTURE_1D_ARRAY ||
7619 inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY) {
7620 if (opcode == FETCH_OP_SAMPLE_C_L ||
7621 opcode == FETCH_OP_SAMPLE_C_LB) {
7622 /* the array index is read from Y */
7623 tex.coord_type_y = 0;
7624 } else {
7625 /* the array index is read from Z */
7626 tex.coord_type_z = 0;
7627 tex.src_sel_z = tex.src_sel_y;
7628 }
7629 } else if (inst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY ||
7630 inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY ||
7631 ((inst->Texture.Texture == TGSI_TEXTURE_CUBE_ARRAY ||
7632 inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) &&
7633 (ctx->bc->chip_class >= EVERGREEN)))
7634 /* the array index is read from Z */
7635 tex.coord_type_z = 0;
7636
7637 /* mask unused source components */
7638 if (opcode == FETCH_OP_SAMPLE || opcode == FETCH_OP_GATHER4) {
7639 switch (inst->Texture.Texture) {
7640 case TGSI_TEXTURE_2D:
7641 case TGSI_TEXTURE_RECT:
7642 tex.src_sel_z = 7;
7643 tex.src_sel_w = 7;
7644 break;
7645 case TGSI_TEXTURE_1D_ARRAY:
7646 tex.src_sel_y = 7;
7647 tex.src_sel_w = 7;
7648 break;
7649 case TGSI_TEXTURE_1D:
7650 tex.src_sel_y = 7;
7651 tex.src_sel_z = 7;
7652 tex.src_sel_w = 7;
7653 break;
7654 }
7655 }
7656
7657 r = r600_bytecode_add_tex(ctx->bc, &tex);
7658 if (r)
7659 return r;
7660
7661 /* add shadow ambient support - gallium doesn't do it yet */
7662 return 0;
7663 }
7664
7665 static int tgsi_lrp(struct r600_shader_ctx *ctx)
7666 {
7667 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
7668 struct r600_bytecode_alu alu;
7669 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
7670 unsigned i, temp_regs[2];
7671 int r;
7672
7673 /* optimize if it's just an equal balance */
7674 if (ctx->src[0].sel == V_SQ_ALU_SRC_0_5) {
7675 for (i = 0; i < lasti + 1; i++) {
7676 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
7677 continue;
7678
7679 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7680 alu.op = ALU_OP2_ADD;
7681 r600_bytecode_src(&alu.src[0], &ctx->src[1], i);
7682 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
7683 alu.omod = 3;
7684 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
7685 alu.dst.chan = i;
7686 if (i == lasti) {
7687 alu.last = 1;
7688 }
7689 r = r600_bytecode_add_alu(ctx->bc, &alu);
7690 if (r)
7691 return r;
7692 }
7693 return 0;
7694 }
7695
7696 /* 1 - src0 */
7697 for (i = 0; i < lasti + 1; i++) {
7698 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
7699 continue;
7700
7701 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7702 alu.op = ALU_OP2_ADD;
7703 alu.src[0].sel = V_SQ_ALU_SRC_1;
7704 alu.src[0].chan = 0;
7705 r600_bytecode_src(&alu.src[1], &ctx->src[0], i);
7706 r600_bytecode_src_toggle_neg(&alu.src[1]);
7707 alu.dst.sel = ctx->temp_reg;
7708 alu.dst.chan = i;
7709 if (i == lasti) {
7710 alu.last = 1;
7711 }
7712 alu.dst.write = 1;
7713 r = r600_bytecode_add_alu(ctx->bc, &alu);
7714 if (r)
7715 return r;
7716 }
7717
7718 /* (1 - src0) * src2 */
7719 for (i = 0; i < lasti + 1; i++) {
7720 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
7721 continue;
7722
7723 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7724 alu.op = ALU_OP2_MUL;
7725 alu.src[0].sel = ctx->temp_reg;
7726 alu.src[0].chan = i;
7727 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
7728 alu.dst.sel = ctx->temp_reg;
7729 alu.dst.chan = i;
7730 if (i == lasti) {
7731 alu.last = 1;
7732 }
7733 alu.dst.write = 1;
7734 r = r600_bytecode_add_alu(ctx->bc, &alu);
7735 if (r)
7736 return r;
7737 }
7738
7739 /* src0 * src1 + (1 - src0) * src2 */
7740 if (ctx->src[0].abs)
7741 temp_regs[0] = r600_get_temp(ctx);
7742 else
7743 temp_regs[0] = 0;
7744 if (ctx->src[1].abs)
7745 temp_regs[1] = r600_get_temp(ctx);
7746 else
7747 temp_regs[1] = 0;
7748
7749 for (i = 0; i < lasti + 1; i++) {
7750 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
7751 continue;
7752
7753 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7754 alu.op = ALU_OP3_MULADD;
7755 alu.is_op3 = 1;
7756 r = tgsi_make_src_for_op3(ctx, temp_regs[0], i, &alu.src[0], &ctx->src[0]);
7757 if (r)
7758 return r;
7759 r = tgsi_make_src_for_op3(ctx, temp_regs[1], i, &alu.src[1], &ctx->src[1]);
7760 if (r)
7761 return r;
7762 alu.src[2].sel = ctx->temp_reg;
7763 alu.src[2].chan = i;
7764
7765 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
7766 alu.dst.chan = i;
7767 if (i == lasti) {
7768 alu.last = 1;
7769 }
7770 r = r600_bytecode_add_alu(ctx->bc, &alu);
7771 if (r)
7772 return r;
7773 }
7774 return 0;
7775 }
7776
7777 static int tgsi_cmp(struct r600_shader_ctx *ctx)
7778 {
7779 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
7780 struct r600_bytecode_alu alu;
7781 int i, r, j;
7782 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
7783 int temp_regs[3];
7784 unsigned op;
7785
7786 if (ctx->src[0].abs && ctx->src[0].neg) {
7787 op = ALU_OP3_CNDE;
7788 ctx->src[0].abs = 0;
7789 ctx->src[0].neg = 0;
7790 } else {
7791 op = ALU_OP3_CNDGE;
7792 }
7793
7794 for (j = 0; j < inst->Instruction.NumSrcRegs; j++) {
7795 temp_regs[j] = 0;
7796 if (ctx->src[j].abs)
7797 temp_regs[j] = r600_get_temp(ctx);
7798 }
7799
7800 for (i = 0; i < lasti + 1; i++) {
7801 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
7802 continue;
7803
7804 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7805 alu.op = op;
7806 r = tgsi_make_src_for_op3(ctx, temp_regs[0], i, &alu.src[0], &ctx->src[0]);
7807 if (r)
7808 return r;
7809 r = tgsi_make_src_for_op3(ctx, temp_regs[2], i, &alu.src[1], &ctx->src[2]);
7810 if (r)
7811 return r;
7812 r = tgsi_make_src_for_op3(ctx, temp_regs[1], i, &alu.src[2], &ctx->src[1]);
7813 if (r)
7814 return r;
7815 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
7816 alu.dst.chan = i;
7817 alu.dst.write = 1;
7818 alu.is_op3 = 1;
7819 if (i == lasti)
7820 alu.last = 1;
7821 r = r600_bytecode_add_alu(ctx->bc, &alu);
7822 if (r)
7823 return r;
7824 }
7825 return 0;
7826 }
7827
7828 static int tgsi_ucmp(struct r600_shader_ctx *ctx)
7829 {
7830 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
7831 struct r600_bytecode_alu alu;
7832 int i, r;
7833 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
7834
7835 for (i = 0; i < lasti + 1; i++) {
7836 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
7837 continue;
7838
7839 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7840 alu.op = ALU_OP3_CNDE_INT;
7841 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
7842 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
7843 r600_bytecode_src(&alu.src[2], &ctx->src[1], i);
7844 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
7845 alu.dst.chan = i;
7846 alu.dst.write = 1;
7847 alu.is_op3 = 1;
7848 if (i == lasti)
7849 alu.last = 1;
7850 r = r600_bytecode_add_alu(ctx->bc, &alu);
7851 if (r)
7852 return r;
7853 }
7854 return 0;
7855 }
7856
7857 static int tgsi_xpd(struct r600_shader_ctx *ctx)
7858 {
7859 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
7860 static const unsigned int src0_swizzle[] = {2, 0, 1};
7861 static const unsigned int src1_swizzle[] = {1, 2, 0};
7862 struct r600_bytecode_alu alu;
7863 uint32_t use_temp = 0;
7864 int i, r;
7865
7866 if (inst->Dst[0].Register.WriteMask != 0xf)
7867 use_temp = 1;
7868
7869 for (i = 0; i < 4; i++) {
7870 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7871 alu.op = ALU_OP2_MUL;
7872 if (i < 3) {
7873 r600_bytecode_src(&alu.src[0], &ctx->src[0], src0_swizzle[i]);
7874 r600_bytecode_src(&alu.src[1], &ctx->src[1], src1_swizzle[i]);
7875 } else {
7876 alu.src[0].sel = V_SQ_ALU_SRC_0;
7877 alu.src[0].chan = i;
7878 alu.src[1].sel = V_SQ_ALU_SRC_0;
7879 alu.src[1].chan = i;
7880 }
7881
7882 alu.dst.sel = ctx->temp_reg;
7883 alu.dst.chan = i;
7884 alu.dst.write = 1;
7885
7886 if (i == 3)
7887 alu.last = 1;
7888 r = r600_bytecode_add_alu(ctx->bc, &alu);
7889 if (r)
7890 return r;
7891 }
7892
7893 for (i = 0; i < 4; i++) {
7894 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7895 alu.op = ALU_OP3_MULADD;
7896
7897 if (i < 3) {
7898 r600_bytecode_src(&alu.src[0], &ctx->src[0], src1_swizzle[i]);
7899 r600_bytecode_src(&alu.src[1], &ctx->src[1], src0_swizzle[i]);
7900 } else {
7901 alu.src[0].sel = V_SQ_ALU_SRC_0;
7902 alu.src[0].chan = i;
7903 alu.src[1].sel = V_SQ_ALU_SRC_0;
7904 alu.src[1].chan = i;
7905 }
7906
7907 alu.src[2].sel = ctx->temp_reg;
7908 alu.src[2].neg = 1;
7909 alu.src[2].chan = i;
7910
7911 if (use_temp)
7912 alu.dst.sel = ctx->temp_reg;
7913 else
7914 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
7915 alu.dst.chan = i;
7916 alu.dst.write = 1;
7917 alu.is_op3 = 1;
7918 if (i == 3)
7919 alu.last = 1;
7920 r = r600_bytecode_add_alu(ctx->bc, &alu);
7921 if (r)
7922 return r;
7923 }
7924 if (use_temp)
7925 return tgsi_helper_copy(ctx, inst);
7926 return 0;
7927 }
7928
7929 static int tgsi_exp(struct r600_shader_ctx *ctx)
7930 {
7931 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
7932 struct r600_bytecode_alu alu;
7933 int r;
7934 unsigned i;
7935
7936 /* result.x = 2^floor(src); */
7937 if (inst->Dst[0].Register.WriteMask & 1) {
7938 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7939
7940 alu.op = ALU_OP1_FLOOR;
7941 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
7942
7943 alu.dst.sel = ctx->temp_reg;
7944 alu.dst.chan = 0;
7945 alu.dst.write = 1;
7946 alu.last = 1;
7947 r = r600_bytecode_add_alu(ctx->bc, &alu);
7948 if (r)
7949 return r;
7950
7951 if (ctx->bc->chip_class == CAYMAN) {
7952 for (i = 0; i < 3; i++) {
7953 alu.op = ALU_OP1_EXP_IEEE;
7954 alu.src[0].sel = ctx->temp_reg;
7955 alu.src[0].chan = 0;
7956
7957 alu.dst.sel = ctx->temp_reg;
7958 alu.dst.chan = i;
7959 alu.dst.write = i == 0;
7960 alu.last = i == 2;
7961 r = r600_bytecode_add_alu(ctx->bc, &alu);
7962 if (r)
7963 return r;
7964 }
7965 } else {
7966 alu.op = ALU_OP1_EXP_IEEE;
7967 alu.src[0].sel = ctx->temp_reg;
7968 alu.src[0].chan = 0;
7969
7970 alu.dst.sel = ctx->temp_reg;
7971 alu.dst.chan = 0;
7972 alu.dst.write = 1;
7973 alu.last = 1;
7974 r = r600_bytecode_add_alu(ctx->bc, &alu);
7975 if (r)
7976 return r;
7977 }
7978 }
7979
7980 /* result.y = tmp - floor(tmp); */
7981 if ((inst->Dst[0].Register.WriteMask >> 1) & 1) {
7982 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
7983
7984 alu.op = ALU_OP1_FRACT;
7985 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
7986
7987 alu.dst.sel = ctx->temp_reg;
7988 #if 0
7989 r = tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
7990 if (r)
7991 return r;
7992 #endif
7993 alu.dst.write = 1;
7994 alu.dst.chan = 1;
7995
7996 alu.last = 1;
7997
7998 r = r600_bytecode_add_alu(ctx->bc, &alu);
7999 if (r)
8000 return r;
8001 }
8002
8003 /* result.z = RoughApprox2ToX(tmp);*/
8004 if ((inst->Dst[0].Register.WriteMask >> 2) & 0x1) {
8005 if (ctx->bc->chip_class == CAYMAN) {
8006 for (i = 0; i < 3; i++) {
8007 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8008 alu.op = ALU_OP1_EXP_IEEE;
8009 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8010
8011 alu.dst.sel = ctx->temp_reg;
8012 alu.dst.chan = i;
8013 if (i == 2) {
8014 alu.dst.write = 1;
8015 alu.last = 1;
8016 }
8017
8018 r = r600_bytecode_add_alu(ctx->bc, &alu);
8019 if (r)
8020 return r;
8021 }
8022 } else {
8023 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8024 alu.op = ALU_OP1_EXP_IEEE;
8025 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8026
8027 alu.dst.sel = ctx->temp_reg;
8028 alu.dst.write = 1;
8029 alu.dst.chan = 2;
8030
8031 alu.last = 1;
8032
8033 r = r600_bytecode_add_alu(ctx->bc, &alu);
8034 if (r)
8035 return r;
8036 }
8037 }
8038
8039 /* result.w = 1.0;*/
8040 if ((inst->Dst[0].Register.WriteMask >> 3) & 0x1) {
8041 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8042
8043 alu.op = ALU_OP1_MOV;
8044 alu.src[0].sel = V_SQ_ALU_SRC_1;
8045 alu.src[0].chan = 0;
8046
8047 alu.dst.sel = ctx->temp_reg;
8048 alu.dst.chan = 3;
8049 alu.dst.write = 1;
8050 alu.last = 1;
8051 r = r600_bytecode_add_alu(ctx->bc, &alu);
8052 if (r)
8053 return r;
8054 }
8055 return tgsi_helper_copy(ctx, inst);
8056 }
8057
8058 static int tgsi_log(struct r600_shader_ctx *ctx)
8059 {
8060 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8061 struct r600_bytecode_alu alu;
8062 int r;
8063 unsigned i;
8064
8065 /* result.x = floor(log2(|src|)); */
8066 if (inst->Dst[0].Register.WriteMask & 1) {
8067 if (ctx->bc->chip_class == CAYMAN) {
8068 for (i = 0; i < 3; i++) {
8069 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8070
8071 alu.op = ALU_OP1_LOG_IEEE;
8072 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8073 r600_bytecode_src_set_abs(&alu.src[0]);
8074
8075 alu.dst.sel = ctx->temp_reg;
8076 alu.dst.chan = i;
8077 if (i == 0)
8078 alu.dst.write = 1;
8079 if (i == 2)
8080 alu.last = 1;
8081 r = r600_bytecode_add_alu(ctx->bc, &alu);
8082 if (r)
8083 return r;
8084 }
8085
8086 } else {
8087 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8088
8089 alu.op = ALU_OP1_LOG_IEEE;
8090 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8091 r600_bytecode_src_set_abs(&alu.src[0]);
8092
8093 alu.dst.sel = ctx->temp_reg;
8094 alu.dst.chan = 0;
8095 alu.dst.write = 1;
8096 alu.last = 1;
8097 r = r600_bytecode_add_alu(ctx->bc, &alu);
8098 if (r)
8099 return r;
8100 }
8101
8102 alu.op = ALU_OP1_FLOOR;
8103 alu.src[0].sel = ctx->temp_reg;
8104 alu.src[0].chan = 0;
8105
8106 alu.dst.sel = ctx->temp_reg;
8107 alu.dst.chan = 0;
8108 alu.dst.write = 1;
8109 alu.last = 1;
8110
8111 r = r600_bytecode_add_alu(ctx->bc, &alu);
8112 if (r)
8113 return r;
8114 }
8115
8116 /* result.y = |src.x| / (2 ^ floor(log2(|src.x|))); */
8117 if ((inst->Dst[0].Register.WriteMask >> 1) & 1) {
8118
8119 if (ctx->bc->chip_class == CAYMAN) {
8120 for (i = 0; i < 3; i++) {
8121 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8122
8123 alu.op = ALU_OP1_LOG_IEEE;
8124 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8125 r600_bytecode_src_set_abs(&alu.src[0]);
8126
8127 alu.dst.sel = ctx->temp_reg;
8128 alu.dst.chan = i;
8129 if (i == 1)
8130 alu.dst.write = 1;
8131 if (i == 2)
8132 alu.last = 1;
8133
8134 r = r600_bytecode_add_alu(ctx->bc, &alu);
8135 if (r)
8136 return r;
8137 }
8138 } else {
8139 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8140
8141 alu.op = ALU_OP1_LOG_IEEE;
8142 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8143 r600_bytecode_src_set_abs(&alu.src[0]);
8144
8145 alu.dst.sel = ctx->temp_reg;
8146 alu.dst.chan = 1;
8147 alu.dst.write = 1;
8148 alu.last = 1;
8149
8150 r = r600_bytecode_add_alu(ctx->bc, &alu);
8151 if (r)
8152 return r;
8153 }
8154
8155 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8156
8157 alu.op = ALU_OP1_FLOOR;
8158 alu.src[0].sel = ctx->temp_reg;
8159 alu.src[0].chan = 1;
8160
8161 alu.dst.sel = ctx->temp_reg;
8162 alu.dst.chan = 1;
8163 alu.dst.write = 1;
8164 alu.last = 1;
8165
8166 r = r600_bytecode_add_alu(ctx->bc, &alu);
8167 if (r)
8168 return r;
8169
8170 if (ctx->bc->chip_class == CAYMAN) {
8171 for (i = 0; i < 3; i++) {
8172 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8173 alu.op = ALU_OP1_EXP_IEEE;
8174 alu.src[0].sel = ctx->temp_reg;
8175 alu.src[0].chan = 1;
8176
8177 alu.dst.sel = ctx->temp_reg;
8178 alu.dst.chan = i;
8179 if (i == 1)
8180 alu.dst.write = 1;
8181 if (i == 2)
8182 alu.last = 1;
8183
8184 r = r600_bytecode_add_alu(ctx->bc, &alu);
8185 if (r)
8186 return r;
8187 }
8188 } else {
8189 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8190 alu.op = ALU_OP1_EXP_IEEE;
8191 alu.src[0].sel = ctx->temp_reg;
8192 alu.src[0].chan = 1;
8193
8194 alu.dst.sel = ctx->temp_reg;
8195 alu.dst.chan = 1;
8196 alu.dst.write = 1;
8197 alu.last = 1;
8198
8199 r = r600_bytecode_add_alu(ctx->bc, &alu);
8200 if (r)
8201 return r;
8202 }
8203
8204 if (ctx->bc->chip_class == CAYMAN) {
8205 for (i = 0; i < 3; i++) {
8206 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8207 alu.op = ALU_OP1_RECIP_IEEE;
8208 alu.src[0].sel = ctx->temp_reg;
8209 alu.src[0].chan = 1;
8210
8211 alu.dst.sel = ctx->temp_reg;
8212 alu.dst.chan = i;
8213 if (i == 1)
8214 alu.dst.write = 1;
8215 if (i == 2)
8216 alu.last = 1;
8217
8218 r = r600_bytecode_add_alu(ctx->bc, &alu);
8219 if (r)
8220 return r;
8221 }
8222 } else {
8223 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8224 alu.op = ALU_OP1_RECIP_IEEE;
8225 alu.src[0].sel = ctx->temp_reg;
8226 alu.src[0].chan = 1;
8227
8228 alu.dst.sel = ctx->temp_reg;
8229 alu.dst.chan = 1;
8230 alu.dst.write = 1;
8231 alu.last = 1;
8232
8233 r = r600_bytecode_add_alu(ctx->bc, &alu);
8234 if (r)
8235 return r;
8236 }
8237
8238 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8239
8240 alu.op = ALU_OP2_MUL;
8241
8242 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8243 r600_bytecode_src_set_abs(&alu.src[0]);
8244
8245 alu.src[1].sel = ctx->temp_reg;
8246 alu.src[1].chan = 1;
8247
8248 alu.dst.sel = ctx->temp_reg;
8249 alu.dst.chan = 1;
8250 alu.dst.write = 1;
8251 alu.last = 1;
8252
8253 r = r600_bytecode_add_alu(ctx->bc, &alu);
8254 if (r)
8255 return r;
8256 }
8257
8258 /* result.z = log2(|src|);*/
8259 if ((inst->Dst[0].Register.WriteMask >> 2) & 1) {
8260 if (ctx->bc->chip_class == CAYMAN) {
8261 for (i = 0; i < 3; i++) {
8262 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8263
8264 alu.op = ALU_OP1_LOG_IEEE;
8265 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8266 r600_bytecode_src_set_abs(&alu.src[0]);
8267
8268 alu.dst.sel = ctx->temp_reg;
8269 if (i == 2)
8270 alu.dst.write = 1;
8271 alu.dst.chan = i;
8272 if (i == 2)
8273 alu.last = 1;
8274
8275 r = r600_bytecode_add_alu(ctx->bc, &alu);
8276 if (r)
8277 return r;
8278 }
8279 } else {
8280 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8281
8282 alu.op = ALU_OP1_LOG_IEEE;
8283 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8284 r600_bytecode_src_set_abs(&alu.src[0]);
8285
8286 alu.dst.sel = ctx->temp_reg;
8287 alu.dst.write = 1;
8288 alu.dst.chan = 2;
8289 alu.last = 1;
8290
8291 r = r600_bytecode_add_alu(ctx->bc, &alu);
8292 if (r)
8293 return r;
8294 }
8295 }
8296
8297 /* result.w = 1.0; */
8298 if ((inst->Dst[0].Register.WriteMask >> 3) & 1) {
8299 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8300
8301 alu.op = ALU_OP1_MOV;
8302 alu.src[0].sel = V_SQ_ALU_SRC_1;
8303 alu.src[0].chan = 0;
8304
8305 alu.dst.sel = ctx->temp_reg;
8306 alu.dst.chan = 3;
8307 alu.dst.write = 1;
8308 alu.last = 1;
8309
8310 r = r600_bytecode_add_alu(ctx->bc, &alu);
8311 if (r)
8312 return r;
8313 }
8314
8315 return tgsi_helper_copy(ctx, inst);
8316 }
8317
8318 static int tgsi_eg_arl(struct r600_shader_ctx *ctx)
8319 {
8320 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8321 struct r600_bytecode_alu alu;
8322 int r;
8323 int i, lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
8324 unsigned reg = get_address_file_reg(ctx, inst->Dst[0].Register.Index);
8325
8326 assert(inst->Dst[0].Register.Index < 3);
8327 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8328
8329 switch (inst->Instruction.Opcode) {
8330 case TGSI_OPCODE_ARL:
8331 alu.op = ALU_OP1_FLT_TO_INT_FLOOR;
8332 break;
8333 case TGSI_OPCODE_ARR:
8334 alu.op = ALU_OP1_FLT_TO_INT;
8335 break;
8336 case TGSI_OPCODE_UARL:
8337 alu.op = ALU_OP1_MOV;
8338 break;
8339 default:
8340 assert(0);
8341 return -1;
8342 }
8343
8344 for (i = 0; i <= lasti; ++i) {
8345 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
8346 continue;
8347 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
8348 alu.last = i == lasti;
8349 alu.dst.sel = reg;
8350 alu.dst.chan = i;
8351 alu.dst.write = 1;
8352 r = r600_bytecode_add_alu(ctx->bc, &alu);
8353 if (r)
8354 return r;
8355 }
8356
8357 if (inst->Dst[0].Register.Index > 0)
8358 ctx->bc->index_loaded[inst->Dst[0].Register.Index - 1] = 0;
8359 else
8360 ctx->bc->ar_loaded = 0;
8361
8362 return 0;
8363 }
8364 static int tgsi_r600_arl(struct r600_shader_ctx *ctx)
8365 {
8366 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8367 struct r600_bytecode_alu alu;
8368 int r;
8369 int i, lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
8370
8371 switch (inst->Instruction.Opcode) {
8372 case TGSI_OPCODE_ARL:
8373 memset(&alu, 0, sizeof(alu));
8374 alu.op = ALU_OP1_FLOOR;
8375 alu.dst.sel = ctx->bc->ar_reg;
8376 alu.dst.write = 1;
8377 for (i = 0; i <= lasti; ++i) {
8378 if (inst->Dst[0].Register.WriteMask & (1 << i)) {
8379 alu.dst.chan = i;
8380 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
8381 alu.last = i == lasti;
8382 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
8383 return r;
8384 }
8385 }
8386
8387 memset(&alu, 0, sizeof(alu));
8388 alu.op = ALU_OP1_FLT_TO_INT;
8389 alu.src[0].sel = ctx->bc->ar_reg;
8390 alu.dst.sel = ctx->bc->ar_reg;
8391 alu.dst.write = 1;
8392 /* FLT_TO_INT is trans-only on r600/r700 */
8393 alu.last = TRUE;
8394 for (i = 0; i <= lasti; ++i) {
8395 alu.dst.chan = i;
8396 alu.src[0].chan = i;
8397 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
8398 return r;
8399 }
8400 break;
8401 case TGSI_OPCODE_ARR:
8402 memset(&alu, 0, sizeof(alu));
8403 alu.op = ALU_OP1_FLT_TO_INT;
8404 alu.dst.sel = ctx->bc->ar_reg;
8405 alu.dst.write = 1;
8406 /* FLT_TO_INT is trans-only on r600/r700 */
8407 alu.last = TRUE;
8408 for (i = 0; i <= lasti; ++i) {
8409 if (inst->Dst[0].Register.WriteMask & (1 << i)) {
8410 alu.dst.chan = i;
8411 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
8412 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
8413 return r;
8414 }
8415 }
8416 break;
8417 case TGSI_OPCODE_UARL:
8418 memset(&alu, 0, sizeof(alu));
8419 alu.op = ALU_OP1_MOV;
8420 alu.dst.sel = ctx->bc->ar_reg;
8421 alu.dst.write = 1;
8422 for (i = 0; i <= lasti; ++i) {
8423 if (inst->Dst[0].Register.WriteMask & (1 << i)) {
8424 alu.dst.chan = i;
8425 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
8426 alu.last = i == lasti;
8427 if ((r = r600_bytecode_add_alu(ctx->bc, &alu)))
8428 return r;
8429 }
8430 }
8431 break;
8432 default:
8433 assert(0);
8434 return -1;
8435 }
8436
8437 ctx->bc->ar_loaded = 0;
8438 return 0;
8439 }
8440
8441 static int tgsi_opdst(struct r600_shader_ctx *ctx)
8442 {
8443 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8444 struct r600_bytecode_alu alu;
8445 int i, r = 0;
8446
8447 for (i = 0; i < 4; i++) {
8448 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8449
8450 alu.op = ALU_OP2_MUL;
8451 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
8452
8453 if (i == 0 || i == 3) {
8454 alu.src[0].sel = V_SQ_ALU_SRC_1;
8455 } else {
8456 r600_bytecode_src(&alu.src[0], &ctx->src[0], i);
8457 }
8458
8459 if (i == 0 || i == 2) {
8460 alu.src[1].sel = V_SQ_ALU_SRC_1;
8461 } else {
8462 r600_bytecode_src(&alu.src[1], &ctx->src[1], i);
8463 }
8464 if (i == 3)
8465 alu.last = 1;
8466 r = r600_bytecode_add_alu(ctx->bc, &alu);
8467 if (r)
8468 return r;
8469 }
8470 return 0;
8471 }
8472
8473 static int emit_logic_pred(struct r600_shader_ctx *ctx, int opcode, int alu_type)
8474 {
8475 struct r600_bytecode_alu alu;
8476 int r;
8477
8478 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8479 alu.op = opcode;
8480 alu.execute_mask = 1;
8481 alu.update_pred = 1;
8482
8483 alu.dst.sel = ctx->temp_reg;
8484 alu.dst.write = 1;
8485 alu.dst.chan = 0;
8486
8487 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8488 alu.src[1].sel = V_SQ_ALU_SRC_0;
8489 alu.src[1].chan = 0;
8490
8491 alu.last = 1;
8492
8493 r = r600_bytecode_add_alu_type(ctx->bc, &alu, alu_type);
8494 if (r)
8495 return r;
8496 return 0;
8497 }
8498
8499 static int pops(struct r600_shader_ctx *ctx, int pops)
8500 {
8501 unsigned force_pop = ctx->bc->force_add_cf;
8502
8503 if (!force_pop) {
8504 int alu_pop = 3;
8505 if (ctx->bc->cf_last) {
8506 if (ctx->bc->cf_last->op == CF_OP_ALU)
8507 alu_pop = 0;
8508 else if (ctx->bc->cf_last->op == CF_OP_ALU_POP_AFTER)
8509 alu_pop = 1;
8510 }
8511 alu_pop += pops;
8512 if (alu_pop == 1) {
8513 ctx->bc->cf_last->op = CF_OP_ALU_POP_AFTER;
8514 ctx->bc->force_add_cf = 1;
8515 } else if (alu_pop == 2) {
8516 ctx->bc->cf_last->op = CF_OP_ALU_POP2_AFTER;
8517 ctx->bc->force_add_cf = 1;
8518 } else {
8519 force_pop = 1;
8520 }
8521 }
8522
8523 if (force_pop) {
8524 r600_bytecode_add_cfinst(ctx->bc, CF_OP_POP);
8525 ctx->bc->cf_last->pop_count = pops;
8526 ctx->bc->cf_last->cf_addr = ctx->bc->cf_last->id + 2;
8527 }
8528
8529 return 0;
8530 }
8531
8532 static inline void callstack_update_max_depth(struct r600_shader_ctx *ctx,
8533 unsigned reason)
8534 {
8535 struct r600_stack_info *stack = &ctx->bc->stack;
8536 unsigned elements, entries;
8537
8538 unsigned entry_size = stack->entry_size;
8539
8540 elements = (stack->loop + stack->push_wqm ) * entry_size;
8541 elements += stack->push;
8542
8543 switch (ctx->bc->chip_class) {
8544 case R600:
8545 case R700:
8546 /* pre-r8xx: if any non-WQM PUSH instruction is invoked, 2 elements on
8547 * the stack must be reserved to hold the current active/continue
8548 * masks */
8549 if (reason == FC_PUSH_VPM) {
8550 elements += 2;
8551 }
8552 break;
8553
8554 case CAYMAN:
8555 /* r9xx: any stack operation on empty stack consumes 2 additional
8556 * elements */
8557 elements += 2;
8558
8559 /* fallthrough */
8560 /* FIXME: do the two elements added above cover the cases for the
8561 * r8xx+ below? */
8562
8563 case EVERGREEN:
8564 /* r8xx+: 2 extra elements are not always required, but one extra
8565 * element must be added for each of the following cases:
8566 * 1. There is an ALU_ELSE_AFTER instruction at the point of greatest
8567 * stack usage.
8568 * (Currently we don't use ALU_ELSE_AFTER.)
8569 * 2. There are LOOP/WQM frames on the stack when any flavor of non-WQM
8570 * PUSH instruction executed.
8571 *
8572 * NOTE: it seems we also need to reserve additional element in some
8573 * other cases, e.g. when we have 4 levels of PUSH_VPM in the shader,
8574 * then STACK_SIZE should be 2 instead of 1 */
8575 if (reason == FC_PUSH_VPM) {
8576 elements += 1;
8577 }
8578 break;
8579
8580 default:
8581 assert(0);
8582 break;
8583 }
8584
8585 /* NOTE: it seems STACK_SIZE is interpreted by hw as if entry_size is 4
8586 * for all chips, so we use 4 in the final formula, not the real entry_size
8587 * for the chip */
8588 entry_size = 4;
8589
8590 entries = (elements + (entry_size - 1)) / entry_size;
8591
8592 if (entries > stack->max_entries)
8593 stack->max_entries = entries;
8594 }
8595
8596 static inline void callstack_pop(struct r600_shader_ctx *ctx, unsigned reason)
8597 {
8598 switch(reason) {
8599 case FC_PUSH_VPM:
8600 --ctx->bc->stack.push;
8601 assert(ctx->bc->stack.push >= 0);
8602 break;
8603 case FC_PUSH_WQM:
8604 --ctx->bc->stack.push_wqm;
8605 assert(ctx->bc->stack.push_wqm >= 0);
8606 break;
8607 case FC_LOOP:
8608 --ctx->bc->stack.loop;
8609 assert(ctx->bc->stack.loop >= 0);
8610 break;
8611 default:
8612 assert(0);
8613 break;
8614 }
8615 }
8616
8617 static inline void callstack_push(struct r600_shader_ctx *ctx, unsigned reason)
8618 {
8619 switch (reason) {
8620 case FC_PUSH_VPM:
8621 ++ctx->bc->stack.push;
8622 break;
8623 case FC_PUSH_WQM:
8624 ++ctx->bc->stack.push_wqm;
8625 case FC_LOOP:
8626 ++ctx->bc->stack.loop;
8627 break;
8628 default:
8629 assert(0);
8630 }
8631
8632 callstack_update_max_depth(ctx, reason);
8633 }
8634
8635 static void fc_set_mid(struct r600_shader_ctx *ctx, int fc_sp)
8636 {
8637 struct r600_cf_stack_entry *sp = &ctx->bc->fc_stack[fc_sp];
8638
8639 sp->mid = realloc((void *)sp->mid,
8640 sizeof(struct r600_bytecode_cf *) * (sp->num_mid + 1));
8641 sp->mid[sp->num_mid] = ctx->bc->cf_last;
8642 sp->num_mid++;
8643 }
8644
8645 static void fc_pushlevel(struct r600_shader_ctx *ctx, int type)
8646 {
8647 assert(ctx->bc->fc_sp < ARRAY_SIZE(ctx->bc->fc_stack));
8648 ctx->bc->fc_stack[ctx->bc->fc_sp].type = type;
8649 ctx->bc->fc_stack[ctx->bc->fc_sp].start = ctx->bc->cf_last;
8650 ctx->bc->fc_sp++;
8651 }
8652
8653 static void fc_poplevel(struct r600_shader_ctx *ctx)
8654 {
8655 struct r600_cf_stack_entry *sp = &ctx->bc->fc_stack[ctx->bc->fc_sp - 1];
8656 free(sp->mid);
8657 sp->mid = NULL;
8658 sp->num_mid = 0;
8659 sp->start = NULL;
8660 sp->type = 0;
8661 ctx->bc->fc_sp--;
8662 }
8663
8664 #if 0
8665 static int emit_return(struct r600_shader_ctx *ctx)
8666 {
8667 r600_bytecode_add_cfinst(ctx->bc, CF_OP_RETURN));
8668 return 0;
8669 }
8670
8671 static int emit_jump_to_offset(struct r600_shader_ctx *ctx, int pops, int offset)
8672 {
8673
8674 r600_bytecode_add_cfinst(ctx->bc, CF_OP_JUMP));
8675 ctx->bc->cf_last->pop_count = pops;
8676 /* XXX work out offset */
8677 return 0;
8678 }
8679
8680 static int emit_setret_in_loop_flag(struct r600_shader_ctx *ctx, unsigned flag_value)
8681 {
8682 return 0;
8683 }
8684
8685 static void emit_testflag(struct r600_shader_ctx *ctx)
8686 {
8687
8688 }
8689
8690 static void emit_return_on_flag(struct r600_shader_ctx *ctx, unsigned ifidx)
8691 {
8692 emit_testflag(ctx);
8693 emit_jump_to_offset(ctx, 1, 4);
8694 emit_setret_in_loop_flag(ctx, V_SQ_ALU_SRC_0);
8695 pops(ctx, ifidx + 1);
8696 emit_return(ctx);
8697 }
8698
8699 static void break_loop_on_flag(struct r600_shader_ctx *ctx, unsigned fc_sp)
8700 {
8701 emit_testflag(ctx);
8702
8703 r600_bytecode_add_cfinst(ctx->bc, ctx->inst_info->op);
8704 ctx->bc->cf_last->pop_count = 1;
8705
8706 fc_set_mid(ctx, fc_sp);
8707
8708 pops(ctx, 1);
8709 }
8710 #endif
8711
8712 static int emit_if(struct r600_shader_ctx *ctx, int opcode)
8713 {
8714 int alu_type = CF_OP_ALU_PUSH_BEFORE;
8715
8716 /* There is a hardware bug on Cayman where a BREAK/CONTINUE followed by
8717 * LOOP_STARTxxx for nested loops may put the branch stack into a state
8718 * such that ALU_PUSH_BEFORE doesn't work as expected. Workaround this
8719 * by replacing the ALU_PUSH_BEFORE with a PUSH + ALU */
8720 if (ctx->bc->chip_class == CAYMAN && ctx->bc->stack.loop > 1) {
8721 r600_bytecode_add_cfinst(ctx->bc, CF_OP_PUSH);
8722 ctx->bc->cf_last->cf_addr = ctx->bc->cf_last->id + 2;
8723 alu_type = CF_OP_ALU;
8724 }
8725
8726 emit_logic_pred(ctx, opcode, alu_type);
8727
8728 r600_bytecode_add_cfinst(ctx->bc, CF_OP_JUMP);
8729
8730 fc_pushlevel(ctx, FC_IF);
8731
8732 callstack_push(ctx, FC_PUSH_VPM);
8733 return 0;
8734 }
8735
8736 static int tgsi_if(struct r600_shader_ctx *ctx)
8737 {
8738 return emit_if(ctx, ALU_OP2_PRED_SETNE);
8739 }
8740
8741 static int tgsi_uif(struct r600_shader_ctx *ctx)
8742 {
8743 return emit_if(ctx, ALU_OP2_PRED_SETNE_INT);
8744 }
8745
8746 static int tgsi_else(struct r600_shader_ctx *ctx)
8747 {
8748 r600_bytecode_add_cfinst(ctx->bc, CF_OP_ELSE);
8749 ctx->bc->cf_last->pop_count = 1;
8750
8751 fc_set_mid(ctx, ctx->bc->fc_sp - 1);
8752 ctx->bc->fc_stack[ctx->bc->fc_sp - 1].start->cf_addr = ctx->bc->cf_last->id;
8753 return 0;
8754 }
8755
8756 static int tgsi_endif(struct r600_shader_ctx *ctx)
8757 {
8758 pops(ctx, 1);
8759 if (ctx->bc->fc_stack[ctx->bc->fc_sp - 1].type != FC_IF) {
8760 R600_ERR("if/endif unbalanced in shader\n");
8761 return -1;
8762 }
8763
8764 if (ctx->bc->fc_stack[ctx->bc->fc_sp - 1].mid == NULL) {
8765 ctx->bc->fc_stack[ctx->bc->fc_sp - 1].start->cf_addr = ctx->bc->cf_last->id + 2;
8766 ctx->bc->fc_stack[ctx->bc->fc_sp - 1].start->pop_count = 1;
8767 } else {
8768 ctx->bc->fc_stack[ctx->bc->fc_sp - 1].mid[0]->cf_addr = ctx->bc->cf_last->id + 2;
8769 }
8770 fc_poplevel(ctx);
8771
8772 callstack_pop(ctx, FC_PUSH_VPM);
8773 return 0;
8774 }
8775
8776 static int tgsi_bgnloop(struct r600_shader_ctx *ctx)
8777 {
8778 /* LOOP_START_DX10 ignores the LOOP_CONFIG* registers, so it is not
8779 * limited to 4096 iterations, like the other LOOP_* instructions. */
8780 r600_bytecode_add_cfinst(ctx->bc, CF_OP_LOOP_START_DX10);
8781
8782 fc_pushlevel(ctx, FC_LOOP);
8783
8784 /* check stack depth */
8785 callstack_push(ctx, FC_LOOP);
8786 return 0;
8787 }
8788
8789 static int tgsi_endloop(struct r600_shader_ctx *ctx)
8790 {
8791 unsigned i;
8792
8793 r600_bytecode_add_cfinst(ctx->bc, CF_OP_LOOP_END);
8794
8795 if (ctx->bc->fc_stack[ctx->bc->fc_sp - 1].type != FC_LOOP) {
8796 R600_ERR("loop/endloop in shader code are not paired.\n");
8797 return -EINVAL;
8798 }
8799
8800 /* fixup loop pointers - from r600isa
8801 LOOP END points to CF after LOOP START,
8802 LOOP START point to CF after LOOP END
8803 BRK/CONT point to LOOP END CF
8804 */
8805 ctx->bc->cf_last->cf_addr = ctx->bc->fc_stack[ctx->bc->fc_sp - 1].start->id + 2;
8806
8807 ctx->bc->fc_stack[ctx->bc->fc_sp - 1].start->cf_addr = ctx->bc->cf_last->id + 2;
8808
8809 for (i = 0; i < ctx->bc->fc_stack[ctx->bc->fc_sp - 1].num_mid; i++) {
8810 ctx->bc->fc_stack[ctx->bc->fc_sp - 1].mid[i]->cf_addr = ctx->bc->cf_last->id;
8811 }
8812 /* XXX add LOOPRET support */
8813 fc_poplevel(ctx);
8814 callstack_pop(ctx, FC_LOOP);
8815 return 0;
8816 }
8817
8818 static int tgsi_loop_breakc(struct r600_shader_ctx *ctx)
8819 {
8820 int r;
8821 unsigned int fscp;
8822
8823 for (fscp = ctx->bc->fc_sp; fscp > 0; fscp--)
8824 {
8825 if (FC_LOOP == ctx->bc->fc_stack[fscp - 1].type)
8826 break;
8827 }
8828 if (fscp == 0) {
8829 R600_ERR("BREAKC not inside loop/endloop pair\n");
8830 return -EINVAL;
8831 }
8832
8833 if (ctx->bc->chip_class == EVERGREEN &&
8834 ctx->bc->family != CHIP_CYPRESS &&
8835 ctx->bc->family != CHIP_JUNIPER) {
8836 /* HW bug: ALU_BREAK does not save the active mask correctly */
8837 r = tgsi_uif(ctx);
8838 if (r)
8839 return r;
8840
8841 r = r600_bytecode_add_cfinst(ctx->bc, CF_OP_LOOP_BREAK);
8842 if (r)
8843 return r;
8844 fc_set_mid(ctx, fscp - 1);
8845
8846 return tgsi_endif(ctx);
8847 } else {
8848 r = emit_logic_pred(ctx, ALU_OP2_PRED_SETE_INT, CF_OP_ALU_BREAK);
8849 if (r)
8850 return r;
8851 fc_set_mid(ctx, fscp - 1);
8852 }
8853
8854 return 0;
8855 }
8856
8857 static int tgsi_loop_brk_cont(struct r600_shader_ctx *ctx)
8858 {
8859 unsigned int fscp;
8860
8861 for (fscp = ctx->bc->fc_sp; fscp > 0; fscp--)
8862 {
8863 if (FC_LOOP == ctx->bc->fc_stack[fscp - 1].type)
8864 break;
8865 }
8866
8867 if (fscp == 0) {
8868 R600_ERR("Break not inside loop/endloop pair\n");
8869 return -EINVAL;
8870 }
8871
8872 r600_bytecode_add_cfinst(ctx->bc, ctx->inst_info->op);
8873
8874 fc_set_mid(ctx, fscp - 1);
8875
8876 return 0;
8877 }
8878
8879 static int tgsi_gs_emit(struct r600_shader_ctx *ctx)
8880 {
8881 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8882 int stream = ctx->literals[inst->Src[0].Register.Index * 4 + inst->Src[0].Register.SwizzleX];
8883 int r;
8884
8885 if (ctx->inst_info->op == CF_OP_EMIT_VERTEX)
8886 emit_gs_ring_writes(ctx, ctx->gs_stream_output_info, stream, TRUE);
8887
8888 r = r600_bytecode_add_cfinst(ctx->bc, ctx->inst_info->op);
8889 if (!r) {
8890 ctx->bc->cf_last->count = stream; // Count field for CUT/EMIT_VERTEX indicates which stream
8891 if (ctx->inst_info->op == CF_OP_EMIT_VERTEX)
8892 return emit_inc_ring_offset(ctx, stream, TRUE);
8893 }
8894 return r;
8895 }
8896
8897 static int tgsi_umad(struct r600_shader_ctx *ctx)
8898 {
8899 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8900 struct r600_bytecode_alu alu;
8901 int i, j, k, r;
8902 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
8903
8904 /* src0 * src1 */
8905 for (i = 0; i < lasti + 1; i++) {
8906 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
8907 continue;
8908
8909 if (ctx->bc->chip_class == CAYMAN) {
8910 for (j = 0 ; j < 4; j++) {
8911 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8912
8913 alu.op = ALU_OP2_MULLO_UINT;
8914 for (k = 0; k < inst->Instruction.NumSrcRegs; k++) {
8915 r600_bytecode_src(&alu.src[k], &ctx->src[k], i);
8916 }
8917 alu.dst.chan = j;
8918 alu.dst.sel = ctx->temp_reg;
8919 alu.dst.write = (j == i);
8920 if (j == 3)
8921 alu.last = 1;
8922 r = r600_bytecode_add_alu(ctx->bc, &alu);
8923 if (r)
8924 return r;
8925 }
8926 } else {
8927 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8928
8929 alu.dst.chan = i;
8930 alu.dst.sel = ctx->temp_reg;
8931 alu.dst.write = 1;
8932
8933 alu.op = ALU_OP2_MULLO_UINT;
8934 for (j = 0; j < 2; j++) {
8935 r600_bytecode_src(&alu.src[j], &ctx->src[j], i);
8936 }
8937
8938 alu.last = 1;
8939 r = r600_bytecode_add_alu(ctx->bc, &alu);
8940 if (r)
8941 return r;
8942 }
8943 }
8944
8945
8946 for (i = 0; i < lasti + 1; i++) {
8947 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
8948 continue;
8949
8950 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8951 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
8952
8953 alu.op = ALU_OP2_ADD_INT;
8954
8955 alu.src[0].sel = ctx->temp_reg;
8956 alu.src[0].chan = i;
8957
8958 r600_bytecode_src(&alu.src[1], &ctx->src[2], i);
8959 if (i == lasti) {
8960 alu.last = 1;
8961 }
8962 r = r600_bytecode_add_alu(ctx->bc, &alu);
8963 if (r)
8964 return r;
8965 }
8966 return 0;
8967 }
8968
8969 static int tgsi_pk2h(struct r600_shader_ctx *ctx)
8970 {
8971 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
8972 struct r600_bytecode_alu alu;
8973 int r, i;
8974 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
8975
8976 /* temp.xy = f32_to_f16(src) */
8977 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8978 alu.op = ALU_OP1_FLT32_TO_FLT16;
8979 alu.dst.chan = 0;
8980 alu.dst.sel = ctx->temp_reg;
8981 alu.dst.write = 1;
8982 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
8983 r = r600_bytecode_add_alu(ctx->bc, &alu);
8984 if (r)
8985 return r;
8986 alu.dst.chan = 1;
8987 r600_bytecode_src(&alu.src[0], &ctx->src[0], 1);
8988 alu.last = 1;
8989 r = r600_bytecode_add_alu(ctx->bc, &alu);
8990 if (r)
8991 return r;
8992
8993 /* dst.x = temp.y * 0x10000 + temp.x */
8994 for (i = 0; i < lasti + 1; i++) {
8995 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
8996 continue;
8997
8998 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
8999 alu.op = ALU_OP3_MULADD_UINT24;
9000 alu.is_op3 = 1;
9001 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
9002 alu.last = i == lasti;
9003 alu.src[0].sel = ctx->temp_reg;
9004 alu.src[0].chan = 1;
9005 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
9006 alu.src[1].value = 0x10000;
9007 alu.src[2].sel = ctx->temp_reg;
9008 alu.src[2].chan = 0;
9009 r = r600_bytecode_add_alu(ctx->bc, &alu);
9010 if (r)
9011 return r;
9012 }
9013
9014 return 0;
9015 }
9016
9017 static int tgsi_up2h(struct r600_shader_ctx *ctx)
9018 {
9019 struct tgsi_full_instruction *inst = &ctx->parse.FullToken.FullInstruction;
9020 struct r600_bytecode_alu alu;
9021 int r, i;
9022 int lasti = tgsi_last_instruction(inst->Dst[0].Register.WriteMask);
9023
9024 /* temp.x = src.x */
9025 /* note: no need to mask out the high bits */
9026 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
9027 alu.op = ALU_OP1_MOV;
9028 alu.dst.chan = 0;
9029 alu.dst.sel = ctx->temp_reg;
9030 alu.dst.write = 1;
9031 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
9032 r = r600_bytecode_add_alu(ctx->bc, &alu);
9033 if (r)
9034 return r;
9035
9036 /* temp.y = src.x >> 16 */
9037 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
9038 alu.op = ALU_OP2_LSHR_INT;
9039 alu.dst.chan = 1;
9040 alu.dst.sel = ctx->temp_reg;
9041 alu.dst.write = 1;
9042 r600_bytecode_src(&alu.src[0], &ctx->src[0], 0);
9043 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
9044 alu.src[1].value = 16;
9045 alu.last = 1;
9046 r = r600_bytecode_add_alu(ctx->bc, &alu);
9047 if (r)
9048 return r;
9049
9050 /* dst.wz = dst.xy = f16_to_f32(temp.xy) */
9051 for (i = 0; i < lasti + 1; i++) {
9052 if (!(inst->Dst[0].Register.WriteMask & (1 << i)))
9053 continue;
9054 memset(&alu, 0, sizeof(struct r600_bytecode_alu));
9055 tgsi_dst(ctx, &inst->Dst[0], i, &alu.dst);
9056 alu.op = ALU_OP1_FLT16_TO_FLT32;
9057 alu.src[0].sel = ctx->temp_reg;
9058 alu.src[0].chan = i % 2;
9059 alu.last = i == lasti;
9060 r = r600_bytecode_add_alu(ctx->bc, &alu);
9061 if (r)
9062 return r;
9063 }
9064
9065 return 0;
9066 }
9067
9068 static const struct r600_shader_tgsi_instruction r600_shader_tgsi_instruction[] = {
9069 [TGSI_OPCODE_ARL] = { ALU_OP0_NOP, tgsi_r600_arl},
9070 [TGSI_OPCODE_MOV] = { ALU_OP1_MOV, tgsi_op2},
9071 [TGSI_OPCODE_LIT] = { ALU_OP0_NOP, tgsi_lit},
9072
9073 /* XXX:
9074 * For state trackers other than OpenGL, we'll want to use
9075 * _RECIP_IEEE instead.
9076 */
9077 [TGSI_OPCODE_RCP] = { ALU_OP1_RECIP_CLAMPED, tgsi_trans_srcx_replicate},
9078
9079 [TGSI_OPCODE_RSQ] = { ALU_OP0_NOP, tgsi_rsq},
9080 [TGSI_OPCODE_EXP] = { ALU_OP0_NOP, tgsi_exp},
9081 [TGSI_OPCODE_LOG] = { ALU_OP0_NOP, tgsi_log},
9082 [TGSI_OPCODE_MUL] = { ALU_OP2_MUL_IEEE, tgsi_op2},
9083 [TGSI_OPCODE_ADD] = { ALU_OP2_ADD, tgsi_op2},
9084 [TGSI_OPCODE_DP3] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9085 [TGSI_OPCODE_DP4] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9086 [TGSI_OPCODE_DST] = { ALU_OP0_NOP, tgsi_opdst},
9087 [TGSI_OPCODE_MIN] = { ALU_OP2_MIN, tgsi_op2},
9088 [TGSI_OPCODE_MAX] = { ALU_OP2_MAX, tgsi_op2},
9089 [TGSI_OPCODE_SLT] = { ALU_OP2_SETGT, tgsi_op2_swap},
9090 [TGSI_OPCODE_SGE] = { ALU_OP2_SETGE, tgsi_op2},
9091 [TGSI_OPCODE_MAD] = { ALU_OP3_MULADD_IEEE, tgsi_op3},
9092 [TGSI_OPCODE_LRP] = { ALU_OP0_NOP, tgsi_lrp},
9093 [TGSI_OPCODE_FMA] = { ALU_OP0_NOP, tgsi_unsupported},
9094 [TGSI_OPCODE_SQRT] = { ALU_OP1_SQRT_IEEE, tgsi_trans_srcx_replicate},
9095 [TGSI_OPCODE_DP2A] = { ALU_OP0_NOP, tgsi_unsupported},
9096 [22] = { ALU_OP0_NOP, tgsi_unsupported},
9097 [23] = { ALU_OP0_NOP, tgsi_unsupported},
9098 [TGSI_OPCODE_FRC] = { ALU_OP1_FRACT, tgsi_op2},
9099 [25] = { ALU_OP0_NOP, tgsi_unsupported},
9100 [TGSI_OPCODE_FLR] = { ALU_OP1_FLOOR, tgsi_op2},
9101 [TGSI_OPCODE_ROUND] = { ALU_OP1_RNDNE, tgsi_op2},
9102 [TGSI_OPCODE_EX2] = { ALU_OP1_EXP_IEEE, tgsi_trans_srcx_replicate},
9103 [TGSI_OPCODE_LG2] = { ALU_OP1_LOG_IEEE, tgsi_trans_srcx_replicate},
9104 [TGSI_OPCODE_POW] = { ALU_OP0_NOP, tgsi_pow},
9105 [TGSI_OPCODE_XPD] = { ALU_OP0_NOP, tgsi_xpd},
9106 [32] = { ALU_OP0_NOP, tgsi_unsupported},
9107 [33] = { ALU_OP0_NOP, tgsi_unsupported},
9108 [34] = { ALU_OP0_NOP, tgsi_unsupported},
9109 [TGSI_OPCODE_DPH] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9110 [TGSI_OPCODE_COS] = { ALU_OP1_COS, tgsi_trig},
9111 [TGSI_OPCODE_DDX] = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
9112 [TGSI_OPCODE_DDY] = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
9113 [TGSI_OPCODE_KILL] = { ALU_OP2_KILLGT, tgsi_kill}, /* unconditional kill */
9114 [TGSI_OPCODE_PK2H] = { ALU_OP0_NOP, tgsi_unsupported},
9115 [TGSI_OPCODE_PK2US] = { ALU_OP0_NOP, tgsi_unsupported},
9116 [TGSI_OPCODE_PK4B] = { ALU_OP0_NOP, tgsi_unsupported},
9117 [TGSI_OPCODE_PK4UB] = { ALU_OP0_NOP, tgsi_unsupported},
9118 [44] = { ALU_OP0_NOP, tgsi_unsupported},
9119 [TGSI_OPCODE_SEQ] = { ALU_OP2_SETE, tgsi_op2},
9120 [46] = { ALU_OP0_NOP, tgsi_unsupported},
9121 [TGSI_OPCODE_SGT] = { ALU_OP2_SETGT, tgsi_op2},
9122 [TGSI_OPCODE_SIN] = { ALU_OP1_SIN, tgsi_trig},
9123 [TGSI_OPCODE_SLE] = { ALU_OP2_SETGE, tgsi_op2_swap},
9124 [TGSI_OPCODE_SNE] = { ALU_OP2_SETNE, tgsi_op2},
9125 [51] = { ALU_OP0_NOP, tgsi_unsupported},
9126 [TGSI_OPCODE_TEX] = { FETCH_OP_SAMPLE, tgsi_tex},
9127 [TGSI_OPCODE_TXD] = { FETCH_OP_SAMPLE_G, tgsi_tex},
9128 [TGSI_OPCODE_TXP] = { FETCH_OP_SAMPLE, tgsi_tex},
9129 [TGSI_OPCODE_UP2H] = { ALU_OP0_NOP, tgsi_unsupported},
9130 [TGSI_OPCODE_UP2US] = { ALU_OP0_NOP, tgsi_unsupported},
9131 [TGSI_OPCODE_UP4B] = { ALU_OP0_NOP, tgsi_unsupported},
9132 [TGSI_OPCODE_UP4UB] = { ALU_OP0_NOP, tgsi_unsupported},
9133 [59] = { ALU_OP0_NOP, tgsi_unsupported},
9134 [60] = { ALU_OP0_NOP, tgsi_unsupported},
9135 [TGSI_OPCODE_ARR] = { ALU_OP0_NOP, tgsi_r600_arl},
9136 [62] = { ALU_OP0_NOP, tgsi_unsupported},
9137 [TGSI_OPCODE_CAL] = { ALU_OP0_NOP, tgsi_unsupported},
9138 [TGSI_OPCODE_RET] = { ALU_OP0_NOP, tgsi_unsupported},
9139 [TGSI_OPCODE_SSG] = { ALU_OP0_NOP, tgsi_ssg},
9140 [TGSI_OPCODE_CMP] = { ALU_OP0_NOP, tgsi_cmp},
9141 [TGSI_OPCODE_SCS] = { ALU_OP0_NOP, tgsi_scs},
9142 [TGSI_OPCODE_TXB] = { FETCH_OP_SAMPLE_LB, tgsi_tex},
9143 [69] = { ALU_OP0_NOP, tgsi_unsupported},
9144 [TGSI_OPCODE_DIV] = { ALU_OP0_NOP, tgsi_unsupported},
9145 [TGSI_OPCODE_DP2] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9146 [TGSI_OPCODE_TXL] = { FETCH_OP_SAMPLE_L, tgsi_tex},
9147 [TGSI_OPCODE_BRK] = { CF_OP_LOOP_BREAK, tgsi_loop_brk_cont},
9148 [TGSI_OPCODE_IF] = { ALU_OP0_NOP, tgsi_if},
9149 [TGSI_OPCODE_UIF] = { ALU_OP0_NOP, tgsi_uif},
9150 [76] = { ALU_OP0_NOP, tgsi_unsupported},
9151 [TGSI_OPCODE_ELSE] = { ALU_OP0_NOP, tgsi_else},
9152 [TGSI_OPCODE_ENDIF] = { ALU_OP0_NOP, tgsi_endif},
9153 [TGSI_OPCODE_DDX_FINE] = { ALU_OP0_NOP, tgsi_unsupported},
9154 [TGSI_OPCODE_DDY_FINE] = { ALU_OP0_NOP, tgsi_unsupported},
9155 [TGSI_OPCODE_PUSHA] = { ALU_OP0_NOP, tgsi_unsupported},
9156 [TGSI_OPCODE_POPA] = { ALU_OP0_NOP, tgsi_unsupported},
9157 [TGSI_OPCODE_CEIL] = { ALU_OP1_CEIL, tgsi_op2},
9158 [TGSI_OPCODE_I2F] = { ALU_OP1_INT_TO_FLT, tgsi_op2_trans},
9159 [TGSI_OPCODE_NOT] = { ALU_OP1_NOT_INT, tgsi_op2},
9160 [TGSI_OPCODE_TRUNC] = { ALU_OP1_TRUNC, tgsi_op2},
9161 [TGSI_OPCODE_SHL] = { ALU_OP2_LSHL_INT, tgsi_op2_trans},
9162 [88] = { ALU_OP0_NOP, tgsi_unsupported},
9163 [TGSI_OPCODE_AND] = { ALU_OP2_AND_INT, tgsi_op2},
9164 [TGSI_OPCODE_OR] = { ALU_OP2_OR_INT, tgsi_op2},
9165 [TGSI_OPCODE_MOD] = { ALU_OP0_NOP, tgsi_imod},
9166 [TGSI_OPCODE_XOR] = { ALU_OP2_XOR_INT, tgsi_op2},
9167 [TGSI_OPCODE_SAD] = { ALU_OP0_NOP, tgsi_unsupported},
9168 [TGSI_OPCODE_TXF] = { FETCH_OP_LD, tgsi_tex},
9169 [TGSI_OPCODE_TXQ] = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
9170 [TGSI_OPCODE_CONT] = { CF_OP_LOOP_CONTINUE, tgsi_loop_brk_cont},
9171 [TGSI_OPCODE_EMIT] = { CF_OP_EMIT_VERTEX, tgsi_gs_emit},
9172 [TGSI_OPCODE_ENDPRIM] = { CF_OP_CUT_VERTEX, tgsi_gs_emit},
9173 [TGSI_OPCODE_BGNLOOP] = { ALU_OP0_NOP, tgsi_bgnloop},
9174 [TGSI_OPCODE_BGNSUB] = { ALU_OP0_NOP, tgsi_unsupported},
9175 [TGSI_OPCODE_ENDLOOP] = { ALU_OP0_NOP, tgsi_endloop},
9176 [TGSI_OPCODE_ENDSUB] = { ALU_OP0_NOP, tgsi_unsupported},
9177 [TGSI_OPCODE_TXQ_LZ] = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
9178 [TGSI_OPCODE_TXQS] = { FETCH_OP_GET_NUMBER_OF_SAMPLES, tgsi_tex},
9179 [TGSI_OPCODE_RESQ] = { ALU_OP0_NOP, tgsi_unsupported},
9180 [106] = { ALU_OP0_NOP, tgsi_unsupported},
9181 [TGSI_OPCODE_NOP] = { ALU_OP0_NOP, tgsi_unsupported},
9182 [TGSI_OPCODE_FSEQ] = { ALU_OP2_SETE_DX10, tgsi_op2},
9183 [TGSI_OPCODE_FSGE] = { ALU_OP2_SETGE_DX10, tgsi_op2},
9184 [TGSI_OPCODE_FSLT] = { ALU_OP2_SETGT_DX10, tgsi_op2_swap},
9185 [TGSI_OPCODE_FSNE] = { ALU_OP2_SETNE_DX10, tgsi_op2_swap},
9186 [TGSI_OPCODE_MEMBAR] = { ALU_OP0_NOP, tgsi_unsupported},
9187 [TGSI_OPCODE_CALLNZ] = { ALU_OP0_NOP, tgsi_unsupported},
9188 [114] = { ALU_OP0_NOP, tgsi_unsupported},
9189 [TGSI_OPCODE_BREAKC] = { ALU_OP0_NOP, tgsi_loop_breakc},
9190 [TGSI_OPCODE_KILL_IF] = { ALU_OP2_KILLGT, tgsi_kill}, /* conditional kill */
9191 [TGSI_OPCODE_END] = { ALU_OP0_NOP, tgsi_end}, /* aka HALT */
9192 [TGSI_OPCODE_DFMA] = { ALU_OP0_NOP, tgsi_unsupported},
9193 [TGSI_OPCODE_F2I] = { ALU_OP1_FLT_TO_INT, tgsi_op2_trans},
9194 [TGSI_OPCODE_IDIV] = { ALU_OP0_NOP, tgsi_idiv},
9195 [TGSI_OPCODE_IMAX] = { ALU_OP2_MAX_INT, tgsi_op2},
9196 [TGSI_OPCODE_IMIN] = { ALU_OP2_MIN_INT, tgsi_op2},
9197 [TGSI_OPCODE_INEG] = { ALU_OP2_SUB_INT, tgsi_ineg},
9198 [TGSI_OPCODE_ISGE] = { ALU_OP2_SETGE_INT, tgsi_op2},
9199 [TGSI_OPCODE_ISHR] = { ALU_OP2_ASHR_INT, tgsi_op2_trans},
9200 [TGSI_OPCODE_ISLT] = { ALU_OP2_SETGT_INT, tgsi_op2_swap},
9201 [TGSI_OPCODE_F2U] = { ALU_OP1_FLT_TO_UINT, tgsi_op2_trans},
9202 [TGSI_OPCODE_U2F] = { ALU_OP1_UINT_TO_FLT, tgsi_op2_trans},
9203 [TGSI_OPCODE_UADD] = { ALU_OP2_ADD_INT, tgsi_op2},
9204 [TGSI_OPCODE_UDIV] = { ALU_OP0_NOP, tgsi_udiv},
9205 [TGSI_OPCODE_UMAD] = { ALU_OP0_NOP, tgsi_umad},
9206 [TGSI_OPCODE_UMAX] = { ALU_OP2_MAX_UINT, tgsi_op2},
9207 [TGSI_OPCODE_UMIN] = { ALU_OP2_MIN_UINT, tgsi_op2},
9208 [TGSI_OPCODE_UMOD] = { ALU_OP0_NOP, tgsi_umod},
9209 [TGSI_OPCODE_UMUL] = { ALU_OP2_MULLO_UINT, tgsi_op2_trans},
9210 [TGSI_OPCODE_USEQ] = { ALU_OP2_SETE_INT, tgsi_op2},
9211 [TGSI_OPCODE_USGE] = { ALU_OP2_SETGE_UINT, tgsi_op2},
9212 [TGSI_OPCODE_USHR] = { ALU_OP2_LSHR_INT, tgsi_op2_trans},
9213 [TGSI_OPCODE_USLT] = { ALU_OP2_SETGT_UINT, tgsi_op2_swap},
9214 [TGSI_OPCODE_USNE] = { ALU_OP2_SETNE_INT, tgsi_op2_swap},
9215 [TGSI_OPCODE_SWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
9216 [TGSI_OPCODE_CASE] = { ALU_OP0_NOP, tgsi_unsupported},
9217 [TGSI_OPCODE_DEFAULT] = { ALU_OP0_NOP, tgsi_unsupported},
9218 [TGSI_OPCODE_ENDSWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
9219 [TGSI_OPCODE_SAMPLE] = { 0, tgsi_unsupported},
9220 [TGSI_OPCODE_SAMPLE_I] = { 0, tgsi_unsupported},
9221 [TGSI_OPCODE_SAMPLE_I_MS] = { 0, tgsi_unsupported},
9222 [TGSI_OPCODE_SAMPLE_B] = { 0, tgsi_unsupported},
9223 [TGSI_OPCODE_SAMPLE_C] = { 0, tgsi_unsupported},
9224 [TGSI_OPCODE_SAMPLE_C_LZ] = { 0, tgsi_unsupported},
9225 [TGSI_OPCODE_SAMPLE_D] = { 0, tgsi_unsupported},
9226 [TGSI_OPCODE_SAMPLE_L] = { 0, tgsi_unsupported},
9227 [TGSI_OPCODE_GATHER4] = { 0, tgsi_unsupported},
9228 [TGSI_OPCODE_SVIEWINFO] = { 0, tgsi_unsupported},
9229 [TGSI_OPCODE_SAMPLE_POS] = { 0, tgsi_unsupported},
9230 [TGSI_OPCODE_SAMPLE_INFO] = { 0, tgsi_unsupported},
9231 [TGSI_OPCODE_UARL] = { ALU_OP1_MOVA_INT, tgsi_r600_arl},
9232 [TGSI_OPCODE_UCMP] = { ALU_OP0_NOP, tgsi_ucmp},
9233 [TGSI_OPCODE_IABS] = { 0, tgsi_iabs},
9234 [TGSI_OPCODE_ISSG] = { 0, tgsi_issg},
9235 [TGSI_OPCODE_LOAD] = { ALU_OP0_NOP, tgsi_unsupported},
9236 [TGSI_OPCODE_STORE] = { ALU_OP0_NOP, tgsi_unsupported},
9237 [TGSI_OPCODE_MFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9238 [TGSI_OPCODE_LFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9239 [TGSI_OPCODE_SFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9240 [TGSI_OPCODE_BARRIER] = { ALU_OP0_NOP, tgsi_unsupported},
9241 [TGSI_OPCODE_ATOMUADD] = { ALU_OP0_NOP, tgsi_unsupported},
9242 [TGSI_OPCODE_ATOMXCHG] = { ALU_OP0_NOP, tgsi_unsupported},
9243 [TGSI_OPCODE_ATOMCAS] = { ALU_OP0_NOP, tgsi_unsupported},
9244 [TGSI_OPCODE_ATOMAND] = { ALU_OP0_NOP, tgsi_unsupported},
9245 [TGSI_OPCODE_ATOMOR] = { ALU_OP0_NOP, tgsi_unsupported},
9246 [TGSI_OPCODE_ATOMXOR] = { ALU_OP0_NOP, tgsi_unsupported},
9247 [TGSI_OPCODE_ATOMUMIN] = { ALU_OP0_NOP, tgsi_unsupported},
9248 [TGSI_OPCODE_ATOMUMAX] = { ALU_OP0_NOP, tgsi_unsupported},
9249 [TGSI_OPCODE_ATOMIMIN] = { ALU_OP0_NOP, tgsi_unsupported},
9250 [TGSI_OPCODE_ATOMIMAX] = { ALU_OP0_NOP, tgsi_unsupported},
9251 [TGSI_OPCODE_TEX2] = { FETCH_OP_SAMPLE, tgsi_tex},
9252 [TGSI_OPCODE_TXB2] = { FETCH_OP_SAMPLE_LB, tgsi_tex},
9253 [TGSI_OPCODE_TXL2] = { FETCH_OP_SAMPLE_L, tgsi_tex},
9254 [TGSI_OPCODE_IMUL_HI] = { ALU_OP2_MULHI_INT, tgsi_op2_trans},
9255 [TGSI_OPCODE_UMUL_HI] = { ALU_OP2_MULHI_UINT, tgsi_op2_trans},
9256 [TGSI_OPCODE_TG4] = { FETCH_OP_GATHER4, tgsi_unsupported},
9257 [TGSI_OPCODE_LODQ] = { FETCH_OP_GET_LOD, tgsi_unsupported},
9258 [TGSI_OPCODE_IBFE] = { ALU_OP3_BFE_INT, tgsi_unsupported},
9259 [TGSI_OPCODE_UBFE] = { ALU_OP3_BFE_UINT, tgsi_unsupported},
9260 [TGSI_OPCODE_BFI] = { ALU_OP0_NOP, tgsi_unsupported},
9261 [TGSI_OPCODE_BREV] = { ALU_OP1_BFREV_INT, tgsi_unsupported},
9262 [TGSI_OPCODE_POPC] = { ALU_OP1_BCNT_INT, tgsi_unsupported},
9263 [TGSI_OPCODE_LSB] = { ALU_OP1_FFBL_INT, tgsi_unsupported},
9264 [TGSI_OPCODE_IMSB] = { ALU_OP1_FFBH_INT, tgsi_unsupported},
9265 [TGSI_OPCODE_UMSB] = { ALU_OP1_FFBH_UINT, tgsi_unsupported},
9266 [TGSI_OPCODE_INTERP_CENTROID] = { ALU_OP0_NOP, tgsi_unsupported},
9267 [TGSI_OPCODE_INTERP_SAMPLE] = { ALU_OP0_NOP, tgsi_unsupported},
9268 [TGSI_OPCODE_INTERP_OFFSET] = { ALU_OP0_NOP, tgsi_unsupported},
9269 [TGSI_OPCODE_LAST] = { ALU_OP0_NOP, tgsi_unsupported},
9270 };
9271
9272 static const struct r600_shader_tgsi_instruction eg_shader_tgsi_instruction[] = {
9273 [TGSI_OPCODE_ARL] = { ALU_OP0_NOP, tgsi_eg_arl},
9274 [TGSI_OPCODE_MOV] = { ALU_OP1_MOV, tgsi_op2},
9275 [TGSI_OPCODE_LIT] = { ALU_OP0_NOP, tgsi_lit},
9276 [TGSI_OPCODE_RCP] = { ALU_OP1_RECIP_IEEE, tgsi_trans_srcx_replicate},
9277 [TGSI_OPCODE_RSQ] = { ALU_OP1_RECIPSQRT_IEEE, tgsi_rsq},
9278 [TGSI_OPCODE_EXP] = { ALU_OP0_NOP, tgsi_exp},
9279 [TGSI_OPCODE_LOG] = { ALU_OP0_NOP, tgsi_log},
9280 [TGSI_OPCODE_MUL] = { ALU_OP2_MUL_IEEE, tgsi_op2},
9281 [TGSI_OPCODE_ADD] = { ALU_OP2_ADD, tgsi_op2},
9282 [TGSI_OPCODE_DP3] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9283 [TGSI_OPCODE_DP4] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9284 [TGSI_OPCODE_DST] = { ALU_OP0_NOP, tgsi_opdst},
9285 [TGSI_OPCODE_MIN] = { ALU_OP2_MIN, tgsi_op2},
9286 [TGSI_OPCODE_MAX] = { ALU_OP2_MAX, tgsi_op2},
9287 [TGSI_OPCODE_SLT] = { ALU_OP2_SETGT, tgsi_op2_swap},
9288 [TGSI_OPCODE_SGE] = { ALU_OP2_SETGE, tgsi_op2},
9289 [TGSI_OPCODE_MAD] = { ALU_OP3_MULADD_IEEE, tgsi_op3},
9290 [TGSI_OPCODE_LRP] = { ALU_OP0_NOP, tgsi_lrp},
9291 [TGSI_OPCODE_FMA] = { ALU_OP3_FMA, tgsi_op3},
9292 [TGSI_OPCODE_SQRT] = { ALU_OP1_SQRT_IEEE, tgsi_trans_srcx_replicate},
9293 [TGSI_OPCODE_DP2A] = { ALU_OP0_NOP, tgsi_unsupported},
9294 [22] = { ALU_OP0_NOP, tgsi_unsupported},
9295 [23] = { ALU_OP0_NOP, tgsi_unsupported},
9296 [TGSI_OPCODE_FRC] = { ALU_OP1_FRACT, tgsi_op2},
9297 [25] = { ALU_OP0_NOP, tgsi_unsupported},
9298 [TGSI_OPCODE_FLR] = { ALU_OP1_FLOOR, tgsi_op2},
9299 [TGSI_OPCODE_ROUND] = { ALU_OP1_RNDNE, tgsi_op2},
9300 [TGSI_OPCODE_EX2] = { ALU_OP1_EXP_IEEE, tgsi_trans_srcx_replicate},
9301 [TGSI_OPCODE_LG2] = { ALU_OP1_LOG_IEEE, tgsi_trans_srcx_replicate},
9302 [TGSI_OPCODE_POW] = { ALU_OP0_NOP, tgsi_pow},
9303 [TGSI_OPCODE_XPD] = { ALU_OP0_NOP, tgsi_xpd},
9304 [32] = { ALU_OP0_NOP, tgsi_unsupported},
9305 [33] = { ALU_OP0_NOP, tgsi_unsupported},
9306 [34] = { ALU_OP0_NOP, tgsi_unsupported},
9307 [TGSI_OPCODE_DPH] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9308 [TGSI_OPCODE_COS] = { ALU_OP1_COS, tgsi_trig},
9309 [TGSI_OPCODE_DDX] = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
9310 [TGSI_OPCODE_DDY] = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
9311 [TGSI_OPCODE_KILL] = { ALU_OP2_KILLGT, tgsi_kill}, /* unconditional kill */
9312 [TGSI_OPCODE_PK2H] = { ALU_OP0_NOP, tgsi_pk2h},
9313 [TGSI_OPCODE_PK2US] = { ALU_OP0_NOP, tgsi_unsupported},
9314 [TGSI_OPCODE_PK4B] = { ALU_OP0_NOP, tgsi_unsupported},
9315 [TGSI_OPCODE_PK4UB] = { ALU_OP0_NOP, tgsi_unsupported},
9316 [44] = { ALU_OP0_NOP, tgsi_unsupported},
9317 [TGSI_OPCODE_SEQ] = { ALU_OP2_SETE, tgsi_op2},
9318 [46] = { ALU_OP0_NOP, tgsi_unsupported},
9319 [TGSI_OPCODE_SGT] = { ALU_OP2_SETGT, tgsi_op2},
9320 [TGSI_OPCODE_SIN] = { ALU_OP1_SIN, tgsi_trig},
9321 [TGSI_OPCODE_SLE] = { ALU_OP2_SETGE, tgsi_op2_swap},
9322 [TGSI_OPCODE_SNE] = { ALU_OP2_SETNE, tgsi_op2},
9323 [51] = { ALU_OP0_NOP, tgsi_unsupported},
9324 [TGSI_OPCODE_TEX] = { FETCH_OP_SAMPLE, tgsi_tex},
9325 [TGSI_OPCODE_TXD] = { FETCH_OP_SAMPLE_G, tgsi_tex},
9326 [TGSI_OPCODE_TXP] = { FETCH_OP_SAMPLE, tgsi_tex},
9327 [TGSI_OPCODE_UP2H] = { ALU_OP0_NOP, tgsi_up2h},
9328 [TGSI_OPCODE_UP2US] = { ALU_OP0_NOP, tgsi_unsupported},
9329 [TGSI_OPCODE_UP4B] = { ALU_OP0_NOP, tgsi_unsupported},
9330 [TGSI_OPCODE_UP4UB] = { ALU_OP0_NOP, tgsi_unsupported},
9331 [59] = { ALU_OP0_NOP, tgsi_unsupported},
9332 [60] = { ALU_OP0_NOP, tgsi_unsupported},
9333 [TGSI_OPCODE_ARR] = { ALU_OP0_NOP, tgsi_eg_arl},
9334 [62] = { ALU_OP0_NOP, tgsi_unsupported},
9335 [TGSI_OPCODE_CAL] = { ALU_OP0_NOP, tgsi_unsupported},
9336 [TGSI_OPCODE_RET] = { ALU_OP0_NOP, tgsi_unsupported},
9337 [TGSI_OPCODE_SSG] = { ALU_OP0_NOP, tgsi_ssg},
9338 [TGSI_OPCODE_CMP] = { ALU_OP0_NOP, tgsi_cmp},
9339 [TGSI_OPCODE_SCS] = { ALU_OP0_NOP, tgsi_scs},
9340 [TGSI_OPCODE_TXB] = { FETCH_OP_SAMPLE_LB, tgsi_tex},
9341 [69] = { ALU_OP0_NOP, tgsi_unsupported},
9342 [TGSI_OPCODE_DIV] = { ALU_OP0_NOP, tgsi_unsupported},
9343 [TGSI_OPCODE_DP2] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9344 [TGSI_OPCODE_TXL] = { FETCH_OP_SAMPLE_L, tgsi_tex},
9345 [TGSI_OPCODE_BRK] = { CF_OP_LOOP_BREAK, tgsi_loop_brk_cont},
9346 [TGSI_OPCODE_IF] = { ALU_OP0_NOP, tgsi_if},
9347 [TGSI_OPCODE_UIF] = { ALU_OP0_NOP, tgsi_uif},
9348 [76] = { ALU_OP0_NOP, tgsi_unsupported},
9349 [TGSI_OPCODE_ELSE] = { ALU_OP0_NOP, tgsi_else},
9350 [TGSI_OPCODE_ENDIF] = { ALU_OP0_NOP, tgsi_endif},
9351 [TGSI_OPCODE_DDX_FINE] = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
9352 [TGSI_OPCODE_DDY_FINE] = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
9353 [TGSI_OPCODE_PUSHA] = { ALU_OP0_NOP, tgsi_unsupported},
9354 [TGSI_OPCODE_POPA] = { ALU_OP0_NOP, tgsi_unsupported},
9355 [TGSI_OPCODE_CEIL] = { ALU_OP1_CEIL, tgsi_op2},
9356 [TGSI_OPCODE_I2F] = { ALU_OP1_INT_TO_FLT, tgsi_op2_trans},
9357 [TGSI_OPCODE_NOT] = { ALU_OP1_NOT_INT, tgsi_op2},
9358 [TGSI_OPCODE_TRUNC] = { ALU_OP1_TRUNC, tgsi_op2},
9359 [TGSI_OPCODE_SHL] = { ALU_OP2_LSHL_INT, tgsi_op2},
9360 [88] = { ALU_OP0_NOP, tgsi_unsupported},
9361 [TGSI_OPCODE_AND] = { ALU_OP2_AND_INT, tgsi_op2},
9362 [TGSI_OPCODE_OR] = { ALU_OP2_OR_INT, tgsi_op2},
9363 [TGSI_OPCODE_MOD] = { ALU_OP0_NOP, tgsi_imod},
9364 [TGSI_OPCODE_XOR] = { ALU_OP2_XOR_INT, tgsi_op2},
9365 [TGSI_OPCODE_SAD] = { ALU_OP0_NOP, tgsi_unsupported},
9366 [TGSI_OPCODE_TXF] = { FETCH_OP_LD, tgsi_tex},
9367 [TGSI_OPCODE_TXQ] = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
9368 [TGSI_OPCODE_CONT] = { CF_OP_LOOP_CONTINUE, tgsi_loop_brk_cont},
9369 [TGSI_OPCODE_EMIT] = { CF_OP_EMIT_VERTEX, tgsi_gs_emit},
9370 [TGSI_OPCODE_ENDPRIM] = { CF_OP_CUT_VERTEX, tgsi_gs_emit},
9371 [TGSI_OPCODE_BGNLOOP] = { ALU_OP0_NOP, tgsi_bgnloop},
9372 [TGSI_OPCODE_BGNSUB] = { ALU_OP0_NOP, tgsi_unsupported},
9373 [TGSI_OPCODE_ENDLOOP] = { ALU_OP0_NOP, tgsi_endloop},
9374 [TGSI_OPCODE_ENDSUB] = { ALU_OP0_NOP, tgsi_unsupported},
9375 [TGSI_OPCODE_TXQ_LZ] = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
9376 [TGSI_OPCODE_TXQS] = { FETCH_OP_GET_NUMBER_OF_SAMPLES, tgsi_tex},
9377 [TGSI_OPCODE_RESQ] = { ALU_OP0_NOP, tgsi_unsupported},
9378 [106] = { ALU_OP0_NOP, tgsi_unsupported},
9379 [TGSI_OPCODE_NOP] = { ALU_OP0_NOP, tgsi_unsupported},
9380 [TGSI_OPCODE_FSEQ] = { ALU_OP2_SETE_DX10, tgsi_op2},
9381 [TGSI_OPCODE_FSGE] = { ALU_OP2_SETGE_DX10, tgsi_op2},
9382 [TGSI_OPCODE_FSLT] = { ALU_OP2_SETGT_DX10, tgsi_op2_swap},
9383 [TGSI_OPCODE_FSNE] = { ALU_OP2_SETNE_DX10, tgsi_op2_swap},
9384 [TGSI_OPCODE_MEMBAR] = { ALU_OP0_NOP, tgsi_unsupported},
9385 [TGSI_OPCODE_CALLNZ] = { ALU_OP0_NOP, tgsi_unsupported},
9386 [114] = { ALU_OP0_NOP, tgsi_unsupported},
9387 [TGSI_OPCODE_BREAKC] = { ALU_OP0_NOP, tgsi_unsupported},
9388 [TGSI_OPCODE_KILL_IF] = { ALU_OP2_KILLGT, tgsi_kill}, /* conditional kill */
9389 [TGSI_OPCODE_END] = { ALU_OP0_NOP, tgsi_end}, /* aka HALT */
9390 /* Refer below for TGSI_OPCODE_DFMA */
9391 [TGSI_OPCODE_F2I] = { ALU_OP1_FLT_TO_INT, tgsi_f2i},
9392 [TGSI_OPCODE_IDIV] = { ALU_OP0_NOP, tgsi_idiv},
9393 [TGSI_OPCODE_IMAX] = { ALU_OP2_MAX_INT, tgsi_op2},
9394 [TGSI_OPCODE_IMIN] = { ALU_OP2_MIN_INT, tgsi_op2},
9395 [TGSI_OPCODE_INEG] = { ALU_OP2_SUB_INT, tgsi_ineg},
9396 [TGSI_OPCODE_ISGE] = { ALU_OP2_SETGE_INT, tgsi_op2},
9397 [TGSI_OPCODE_ISHR] = { ALU_OP2_ASHR_INT, tgsi_op2},
9398 [TGSI_OPCODE_ISLT] = { ALU_OP2_SETGT_INT, tgsi_op2_swap},
9399 [TGSI_OPCODE_F2U] = { ALU_OP1_FLT_TO_UINT, tgsi_f2i},
9400 [TGSI_OPCODE_U2F] = { ALU_OP1_UINT_TO_FLT, tgsi_op2_trans},
9401 [TGSI_OPCODE_UADD] = { ALU_OP2_ADD_INT, tgsi_op2},
9402 [TGSI_OPCODE_UDIV] = { ALU_OP0_NOP, tgsi_udiv},
9403 [TGSI_OPCODE_UMAD] = { ALU_OP0_NOP, tgsi_umad},
9404 [TGSI_OPCODE_UMAX] = { ALU_OP2_MAX_UINT, tgsi_op2},
9405 [TGSI_OPCODE_UMIN] = { ALU_OP2_MIN_UINT, tgsi_op2},
9406 [TGSI_OPCODE_UMOD] = { ALU_OP0_NOP, tgsi_umod},
9407 [TGSI_OPCODE_UMUL] = { ALU_OP2_MULLO_UINT, tgsi_op2_trans},
9408 [TGSI_OPCODE_USEQ] = { ALU_OP2_SETE_INT, tgsi_op2},
9409 [TGSI_OPCODE_USGE] = { ALU_OP2_SETGE_UINT, tgsi_op2},
9410 [TGSI_OPCODE_USHR] = { ALU_OP2_LSHR_INT, tgsi_op2},
9411 [TGSI_OPCODE_USLT] = { ALU_OP2_SETGT_UINT, tgsi_op2_swap},
9412 [TGSI_OPCODE_USNE] = { ALU_OP2_SETNE_INT, tgsi_op2},
9413 [TGSI_OPCODE_SWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
9414 [TGSI_OPCODE_CASE] = { ALU_OP0_NOP, tgsi_unsupported},
9415 [TGSI_OPCODE_DEFAULT] = { ALU_OP0_NOP, tgsi_unsupported},
9416 [TGSI_OPCODE_ENDSWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
9417 [TGSI_OPCODE_SAMPLE] = { 0, tgsi_unsupported},
9418 [TGSI_OPCODE_SAMPLE_I] = { 0, tgsi_unsupported},
9419 [TGSI_OPCODE_SAMPLE_I_MS] = { 0, tgsi_unsupported},
9420 [TGSI_OPCODE_SAMPLE_B] = { 0, tgsi_unsupported},
9421 [TGSI_OPCODE_SAMPLE_C] = { 0, tgsi_unsupported},
9422 [TGSI_OPCODE_SAMPLE_C_LZ] = { 0, tgsi_unsupported},
9423 [TGSI_OPCODE_SAMPLE_D] = { 0, tgsi_unsupported},
9424 [TGSI_OPCODE_SAMPLE_L] = { 0, tgsi_unsupported},
9425 [TGSI_OPCODE_GATHER4] = { 0, tgsi_unsupported},
9426 [TGSI_OPCODE_SVIEWINFO] = { 0, tgsi_unsupported},
9427 [TGSI_OPCODE_SAMPLE_POS] = { 0, tgsi_unsupported},
9428 [TGSI_OPCODE_SAMPLE_INFO] = { 0, tgsi_unsupported},
9429 [TGSI_OPCODE_UARL] = { ALU_OP1_MOVA_INT, tgsi_eg_arl},
9430 [TGSI_OPCODE_UCMP] = { ALU_OP0_NOP, tgsi_ucmp},
9431 [TGSI_OPCODE_IABS] = { 0, tgsi_iabs},
9432 [TGSI_OPCODE_ISSG] = { 0, tgsi_issg},
9433 [TGSI_OPCODE_LOAD] = { ALU_OP0_NOP, tgsi_unsupported},
9434 [TGSI_OPCODE_STORE] = { ALU_OP0_NOP, tgsi_unsupported},
9435 [TGSI_OPCODE_MFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9436 [TGSI_OPCODE_LFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9437 [TGSI_OPCODE_SFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9438 [TGSI_OPCODE_BARRIER] = { ALU_OP0_GROUP_BARRIER, tgsi_barrier},
9439 [TGSI_OPCODE_ATOMUADD] = { ALU_OP0_NOP, tgsi_unsupported},
9440 [TGSI_OPCODE_ATOMXCHG] = { ALU_OP0_NOP, tgsi_unsupported},
9441 [TGSI_OPCODE_ATOMCAS] = { ALU_OP0_NOP, tgsi_unsupported},
9442 [TGSI_OPCODE_ATOMAND] = { ALU_OP0_NOP, tgsi_unsupported},
9443 [TGSI_OPCODE_ATOMOR] = { ALU_OP0_NOP, tgsi_unsupported},
9444 [TGSI_OPCODE_ATOMXOR] = { ALU_OP0_NOP, tgsi_unsupported},
9445 [TGSI_OPCODE_ATOMUMIN] = { ALU_OP0_NOP, tgsi_unsupported},
9446 [TGSI_OPCODE_ATOMUMAX] = { ALU_OP0_NOP, tgsi_unsupported},
9447 [TGSI_OPCODE_ATOMIMIN] = { ALU_OP0_NOP, tgsi_unsupported},
9448 [TGSI_OPCODE_ATOMIMAX] = { ALU_OP0_NOP, tgsi_unsupported},
9449 [TGSI_OPCODE_TEX2] = { FETCH_OP_SAMPLE, tgsi_tex},
9450 [TGSI_OPCODE_TXB2] = { FETCH_OP_SAMPLE_LB, tgsi_tex},
9451 [TGSI_OPCODE_TXL2] = { FETCH_OP_SAMPLE_L, tgsi_tex},
9452 [TGSI_OPCODE_IMUL_HI] = { ALU_OP2_MULHI_INT, tgsi_op2_trans},
9453 [TGSI_OPCODE_UMUL_HI] = { ALU_OP2_MULHI_UINT, tgsi_op2_trans},
9454 [TGSI_OPCODE_TG4] = { FETCH_OP_GATHER4, tgsi_tex},
9455 [TGSI_OPCODE_LODQ] = { FETCH_OP_GET_LOD, tgsi_tex},
9456 [TGSI_OPCODE_IBFE] = { ALU_OP3_BFE_INT, tgsi_op3},
9457 [TGSI_OPCODE_UBFE] = { ALU_OP3_BFE_UINT, tgsi_op3},
9458 [TGSI_OPCODE_BFI] = { ALU_OP0_NOP, tgsi_bfi},
9459 [TGSI_OPCODE_BREV] = { ALU_OP1_BFREV_INT, tgsi_op2},
9460 [TGSI_OPCODE_POPC] = { ALU_OP1_BCNT_INT, tgsi_op2},
9461 [TGSI_OPCODE_LSB] = { ALU_OP1_FFBL_INT, tgsi_op2},
9462 [TGSI_OPCODE_IMSB] = { ALU_OP1_FFBH_INT, tgsi_msb},
9463 [TGSI_OPCODE_UMSB] = { ALU_OP1_FFBH_UINT, tgsi_msb},
9464 [TGSI_OPCODE_INTERP_CENTROID] = { ALU_OP0_NOP, tgsi_interp_egcm},
9465 [TGSI_OPCODE_INTERP_SAMPLE] = { ALU_OP0_NOP, tgsi_interp_egcm},
9466 [TGSI_OPCODE_INTERP_OFFSET] = { ALU_OP0_NOP, tgsi_interp_egcm},
9467 [TGSI_OPCODE_F2D] = { ALU_OP1_FLT32_TO_FLT64, tgsi_op2_64},
9468 [TGSI_OPCODE_D2F] = { ALU_OP1_FLT64_TO_FLT32, tgsi_op2_64_single_dest},
9469 [TGSI_OPCODE_DABS] = { ALU_OP1_MOV, tgsi_op2_64},
9470 [TGSI_OPCODE_DNEG] = { ALU_OP2_ADD_64, tgsi_dneg},
9471 [TGSI_OPCODE_DADD] = { ALU_OP2_ADD_64, tgsi_op2_64},
9472 [TGSI_OPCODE_DMUL] = { ALU_OP2_MUL_64, cayman_mul_double_instr},
9473 [TGSI_OPCODE_DDIV] = { 0, cayman_ddiv_instr },
9474 [TGSI_OPCODE_DMAX] = { ALU_OP2_MAX_64, tgsi_op2_64},
9475 [TGSI_OPCODE_DMIN] = { ALU_OP2_MIN_64, tgsi_op2_64},
9476 [TGSI_OPCODE_DSLT] = { ALU_OP2_SETGT_64, tgsi_op2_64_single_dest_s},
9477 [TGSI_OPCODE_DSGE] = { ALU_OP2_SETGE_64, tgsi_op2_64_single_dest},
9478 [TGSI_OPCODE_DSEQ] = { ALU_OP2_SETE_64, tgsi_op2_64_single_dest},
9479 [TGSI_OPCODE_DSNE] = { ALU_OP2_SETNE_64, tgsi_op2_64_single_dest},
9480 [TGSI_OPCODE_DRCP] = { ALU_OP2_RECIP_64, cayman_emit_double_instr},
9481 [TGSI_OPCODE_DSQRT] = { ALU_OP2_SQRT_64, cayman_emit_double_instr},
9482 [TGSI_OPCODE_DMAD] = { ALU_OP3_FMA_64, tgsi_op3_64},
9483 [TGSI_OPCODE_DFMA] = { ALU_OP3_FMA_64, tgsi_op3_64},
9484 [TGSI_OPCODE_DFRAC] = { ALU_OP1_FRACT_64, tgsi_op2_64},
9485 [TGSI_OPCODE_DLDEXP] = { ALU_OP2_LDEXP_64, tgsi_op2_64},
9486 [TGSI_OPCODE_DFRACEXP] = { ALU_OP1_FREXP_64, tgsi_dfracexp},
9487 [TGSI_OPCODE_D2I] = { ALU_OP1_FLT_TO_INT, egcm_double_to_int},
9488 [TGSI_OPCODE_I2D] = { ALU_OP1_INT_TO_FLT, egcm_int_to_double},
9489 [TGSI_OPCODE_D2U] = { ALU_OP1_FLT_TO_UINT, egcm_double_to_int},
9490 [TGSI_OPCODE_U2D] = { ALU_OP1_UINT_TO_FLT, egcm_int_to_double},
9491 [TGSI_OPCODE_DRSQ] = { ALU_OP2_RECIPSQRT_64, cayman_emit_double_instr},
9492 [TGSI_OPCODE_LAST] = { ALU_OP0_NOP, tgsi_unsupported},
9493 };
9494
9495 static const struct r600_shader_tgsi_instruction cm_shader_tgsi_instruction[] = {
9496 [TGSI_OPCODE_ARL] = { ALU_OP0_NOP, tgsi_eg_arl},
9497 [TGSI_OPCODE_MOV] = { ALU_OP1_MOV, tgsi_op2},
9498 [TGSI_OPCODE_LIT] = { ALU_OP0_NOP, tgsi_lit},
9499 [TGSI_OPCODE_RCP] = { ALU_OP1_RECIP_IEEE, cayman_emit_float_instr},
9500 [TGSI_OPCODE_RSQ] = { ALU_OP1_RECIPSQRT_IEEE, cayman_emit_float_instr},
9501 [TGSI_OPCODE_EXP] = { ALU_OP0_NOP, tgsi_exp},
9502 [TGSI_OPCODE_LOG] = { ALU_OP0_NOP, tgsi_log},
9503 [TGSI_OPCODE_MUL] = { ALU_OP2_MUL_IEEE, tgsi_op2},
9504 [TGSI_OPCODE_ADD] = { ALU_OP2_ADD, tgsi_op2},
9505 [TGSI_OPCODE_DP3] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9506 [TGSI_OPCODE_DP4] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9507 [TGSI_OPCODE_DST] = { ALU_OP0_NOP, tgsi_opdst},
9508 [TGSI_OPCODE_MIN] = { ALU_OP2_MIN, tgsi_op2},
9509 [TGSI_OPCODE_MAX] = { ALU_OP2_MAX, tgsi_op2},
9510 [TGSI_OPCODE_SLT] = { ALU_OP2_SETGT, tgsi_op2_swap},
9511 [TGSI_OPCODE_SGE] = { ALU_OP2_SETGE, tgsi_op2},
9512 [TGSI_OPCODE_MAD] = { ALU_OP3_MULADD_IEEE, tgsi_op3},
9513 [TGSI_OPCODE_LRP] = { ALU_OP0_NOP, tgsi_lrp},
9514 [TGSI_OPCODE_FMA] = { ALU_OP3_FMA, tgsi_op3},
9515 [TGSI_OPCODE_SQRT] = { ALU_OP1_SQRT_IEEE, cayman_emit_float_instr},
9516 [TGSI_OPCODE_DP2A] = { ALU_OP0_NOP, tgsi_unsupported},
9517 [22] = { ALU_OP0_NOP, tgsi_unsupported},
9518 [23] = { ALU_OP0_NOP, tgsi_unsupported},
9519 [TGSI_OPCODE_FRC] = { ALU_OP1_FRACT, tgsi_op2},
9520 [25] = { ALU_OP0_NOP, tgsi_unsupported},
9521 [TGSI_OPCODE_FLR] = { ALU_OP1_FLOOR, tgsi_op2},
9522 [TGSI_OPCODE_ROUND] = { ALU_OP1_RNDNE, tgsi_op2},
9523 [TGSI_OPCODE_EX2] = { ALU_OP1_EXP_IEEE, cayman_emit_float_instr},
9524 [TGSI_OPCODE_LG2] = { ALU_OP1_LOG_IEEE, cayman_emit_float_instr},
9525 [TGSI_OPCODE_POW] = { ALU_OP0_NOP, cayman_pow},
9526 [TGSI_OPCODE_XPD] = { ALU_OP0_NOP, tgsi_xpd},
9527 [32] = { ALU_OP0_NOP, tgsi_unsupported},
9528 [33] = { ALU_OP0_NOP, tgsi_unsupported},
9529 [34] = { ALU_OP0_NOP, tgsi_unsupported},
9530 [TGSI_OPCODE_DPH] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9531 [TGSI_OPCODE_COS] = { ALU_OP1_COS, cayman_trig},
9532 [TGSI_OPCODE_DDX] = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
9533 [TGSI_OPCODE_DDY] = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
9534 [TGSI_OPCODE_KILL] = { ALU_OP2_KILLGT, tgsi_kill}, /* unconditional kill */
9535 [TGSI_OPCODE_PK2H] = { ALU_OP0_NOP, tgsi_pk2h},
9536 [TGSI_OPCODE_PK2US] = { ALU_OP0_NOP, tgsi_unsupported},
9537 [TGSI_OPCODE_PK4B] = { ALU_OP0_NOP, tgsi_unsupported},
9538 [TGSI_OPCODE_PK4UB] = { ALU_OP0_NOP, tgsi_unsupported},
9539 [44] = { ALU_OP0_NOP, tgsi_unsupported},
9540 [TGSI_OPCODE_SEQ] = { ALU_OP2_SETE, tgsi_op2},
9541 [46] = { ALU_OP0_NOP, tgsi_unsupported},
9542 [TGSI_OPCODE_SGT] = { ALU_OP2_SETGT, tgsi_op2},
9543 [TGSI_OPCODE_SIN] = { ALU_OP1_SIN, cayman_trig},
9544 [TGSI_OPCODE_SLE] = { ALU_OP2_SETGE, tgsi_op2_swap},
9545 [TGSI_OPCODE_SNE] = { ALU_OP2_SETNE, tgsi_op2},
9546 [51] = { ALU_OP0_NOP, tgsi_unsupported},
9547 [TGSI_OPCODE_TEX] = { FETCH_OP_SAMPLE, tgsi_tex},
9548 [TGSI_OPCODE_TXD] = { FETCH_OP_SAMPLE_G, tgsi_tex},
9549 [TGSI_OPCODE_TXP] = { FETCH_OP_SAMPLE, tgsi_tex},
9550 [TGSI_OPCODE_UP2H] = { ALU_OP0_NOP, tgsi_up2h},
9551 [TGSI_OPCODE_UP2US] = { ALU_OP0_NOP, tgsi_unsupported},
9552 [TGSI_OPCODE_UP4B] = { ALU_OP0_NOP, tgsi_unsupported},
9553 [TGSI_OPCODE_UP4UB] = { ALU_OP0_NOP, tgsi_unsupported},
9554 [59] = { ALU_OP0_NOP, tgsi_unsupported},
9555 [60] = { ALU_OP0_NOP, tgsi_unsupported},
9556 [TGSI_OPCODE_ARR] = { ALU_OP0_NOP, tgsi_eg_arl},
9557 [62] = { ALU_OP0_NOP, tgsi_unsupported},
9558 [TGSI_OPCODE_CAL] = { ALU_OP0_NOP, tgsi_unsupported},
9559 [TGSI_OPCODE_RET] = { ALU_OP0_NOP, tgsi_unsupported},
9560 [TGSI_OPCODE_SSG] = { ALU_OP0_NOP, tgsi_ssg},
9561 [TGSI_OPCODE_CMP] = { ALU_OP0_NOP, tgsi_cmp},
9562 [TGSI_OPCODE_SCS] = { ALU_OP0_NOP, tgsi_scs},
9563 [TGSI_OPCODE_TXB] = { FETCH_OP_SAMPLE_LB, tgsi_tex},
9564 [69] = { ALU_OP0_NOP, tgsi_unsupported},
9565 [TGSI_OPCODE_DIV] = { ALU_OP0_NOP, tgsi_unsupported},
9566 [TGSI_OPCODE_DP2] = { ALU_OP2_DOT4_IEEE, tgsi_dp},
9567 [TGSI_OPCODE_TXL] = { FETCH_OP_SAMPLE_L, tgsi_tex},
9568 [TGSI_OPCODE_BRK] = { CF_OP_LOOP_BREAK, tgsi_loop_brk_cont},
9569 [TGSI_OPCODE_IF] = { ALU_OP0_NOP, tgsi_if},
9570 [TGSI_OPCODE_UIF] = { ALU_OP0_NOP, tgsi_uif},
9571 [76] = { ALU_OP0_NOP, tgsi_unsupported},
9572 [TGSI_OPCODE_ELSE] = { ALU_OP0_NOP, tgsi_else},
9573 [TGSI_OPCODE_ENDIF] = { ALU_OP0_NOP, tgsi_endif},
9574 [TGSI_OPCODE_DDX_FINE] = { FETCH_OP_GET_GRADIENTS_H, tgsi_tex},
9575 [TGSI_OPCODE_DDY_FINE] = { FETCH_OP_GET_GRADIENTS_V, tgsi_tex},
9576 [TGSI_OPCODE_PUSHA] = { ALU_OP0_NOP, tgsi_unsupported},
9577 [TGSI_OPCODE_POPA] = { ALU_OP0_NOP, tgsi_unsupported},
9578 [TGSI_OPCODE_CEIL] = { ALU_OP1_CEIL, tgsi_op2},
9579 [TGSI_OPCODE_I2F] = { ALU_OP1_INT_TO_FLT, tgsi_op2},
9580 [TGSI_OPCODE_NOT] = { ALU_OP1_NOT_INT, tgsi_op2},
9581 [TGSI_OPCODE_TRUNC] = { ALU_OP1_TRUNC, tgsi_op2},
9582 [TGSI_OPCODE_SHL] = { ALU_OP2_LSHL_INT, tgsi_op2},
9583 [88] = { ALU_OP0_NOP, tgsi_unsupported},
9584 [TGSI_OPCODE_AND] = { ALU_OP2_AND_INT, tgsi_op2},
9585 [TGSI_OPCODE_OR] = { ALU_OP2_OR_INT, tgsi_op2},
9586 [TGSI_OPCODE_MOD] = { ALU_OP0_NOP, tgsi_imod},
9587 [TGSI_OPCODE_XOR] = { ALU_OP2_XOR_INT, tgsi_op2},
9588 [TGSI_OPCODE_SAD] = { ALU_OP0_NOP, tgsi_unsupported},
9589 [TGSI_OPCODE_TXF] = { FETCH_OP_LD, tgsi_tex},
9590 [TGSI_OPCODE_TXQ] = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
9591 [TGSI_OPCODE_CONT] = { CF_OP_LOOP_CONTINUE, tgsi_loop_brk_cont},
9592 [TGSI_OPCODE_EMIT] = { CF_OP_EMIT_VERTEX, tgsi_gs_emit},
9593 [TGSI_OPCODE_ENDPRIM] = { CF_OP_CUT_VERTEX, tgsi_gs_emit},
9594 [TGSI_OPCODE_BGNLOOP] = { ALU_OP0_NOP, tgsi_bgnloop},
9595 [TGSI_OPCODE_BGNSUB] = { ALU_OP0_NOP, tgsi_unsupported},
9596 [TGSI_OPCODE_ENDLOOP] = { ALU_OP0_NOP, tgsi_endloop},
9597 [TGSI_OPCODE_ENDSUB] = { ALU_OP0_NOP, tgsi_unsupported},
9598 [TGSI_OPCODE_TXQ_LZ] = { FETCH_OP_GET_TEXTURE_RESINFO, tgsi_tex},
9599 [TGSI_OPCODE_TXQS] = { FETCH_OP_GET_NUMBER_OF_SAMPLES, tgsi_tex},
9600 [TGSI_OPCODE_RESQ] = { ALU_OP0_NOP, tgsi_unsupported},
9601 [106] = { ALU_OP0_NOP, tgsi_unsupported},
9602 [TGSI_OPCODE_NOP] = { ALU_OP0_NOP, tgsi_unsupported},
9603 [TGSI_OPCODE_FSEQ] = { ALU_OP2_SETE_DX10, tgsi_op2},
9604 [TGSI_OPCODE_FSGE] = { ALU_OP2_SETGE_DX10, tgsi_op2},
9605 [TGSI_OPCODE_FSLT] = { ALU_OP2_SETGT_DX10, tgsi_op2_swap},
9606 [TGSI_OPCODE_FSNE] = { ALU_OP2_SETNE_DX10, tgsi_op2_swap},
9607 [TGSI_OPCODE_MEMBAR] = { ALU_OP0_NOP, tgsi_unsupported},
9608 [TGSI_OPCODE_CALLNZ] = { ALU_OP0_NOP, tgsi_unsupported},
9609 [114] = { ALU_OP0_NOP, tgsi_unsupported},
9610 [TGSI_OPCODE_BREAKC] = { ALU_OP0_NOP, tgsi_unsupported},
9611 [TGSI_OPCODE_KILL_IF] = { ALU_OP2_KILLGT, tgsi_kill}, /* conditional kill */
9612 [TGSI_OPCODE_END] = { ALU_OP0_NOP, tgsi_end}, /* aka HALT */
9613 /* Refer below for TGSI_OPCODE_DFMA */
9614 [TGSI_OPCODE_F2I] = { ALU_OP1_FLT_TO_INT, tgsi_op2},
9615 [TGSI_OPCODE_IDIV] = { ALU_OP0_NOP, tgsi_idiv},
9616 [TGSI_OPCODE_IMAX] = { ALU_OP2_MAX_INT, tgsi_op2},
9617 [TGSI_OPCODE_IMIN] = { ALU_OP2_MIN_INT, tgsi_op2},
9618 [TGSI_OPCODE_INEG] = { ALU_OP2_SUB_INT, tgsi_ineg},
9619 [TGSI_OPCODE_ISGE] = { ALU_OP2_SETGE_INT, tgsi_op2},
9620 [TGSI_OPCODE_ISHR] = { ALU_OP2_ASHR_INT, tgsi_op2},
9621 [TGSI_OPCODE_ISLT] = { ALU_OP2_SETGT_INT, tgsi_op2_swap},
9622 [TGSI_OPCODE_F2U] = { ALU_OP1_FLT_TO_UINT, tgsi_op2},
9623 [TGSI_OPCODE_U2F] = { ALU_OP1_UINT_TO_FLT, tgsi_op2},
9624 [TGSI_OPCODE_UADD] = { ALU_OP2_ADD_INT, tgsi_op2},
9625 [TGSI_OPCODE_UDIV] = { ALU_OP0_NOP, tgsi_udiv},
9626 [TGSI_OPCODE_UMAD] = { ALU_OP0_NOP, tgsi_umad},
9627 [TGSI_OPCODE_UMAX] = { ALU_OP2_MAX_UINT, tgsi_op2},
9628 [TGSI_OPCODE_UMIN] = { ALU_OP2_MIN_UINT, tgsi_op2},
9629 [TGSI_OPCODE_UMOD] = { ALU_OP0_NOP, tgsi_umod},
9630 [TGSI_OPCODE_UMUL] = { ALU_OP2_MULLO_INT, cayman_mul_int_instr},
9631 [TGSI_OPCODE_USEQ] = { ALU_OP2_SETE_INT, tgsi_op2},
9632 [TGSI_OPCODE_USGE] = { ALU_OP2_SETGE_UINT, tgsi_op2},
9633 [TGSI_OPCODE_USHR] = { ALU_OP2_LSHR_INT, tgsi_op2},
9634 [TGSI_OPCODE_USLT] = { ALU_OP2_SETGT_UINT, tgsi_op2_swap},
9635 [TGSI_OPCODE_USNE] = { ALU_OP2_SETNE_INT, tgsi_op2},
9636 [TGSI_OPCODE_SWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
9637 [TGSI_OPCODE_CASE] = { ALU_OP0_NOP, tgsi_unsupported},
9638 [TGSI_OPCODE_DEFAULT] = { ALU_OP0_NOP, tgsi_unsupported},
9639 [TGSI_OPCODE_ENDSWITCH] = { ALU_OP0_NOP, tgsi_unsupported},
9640 [TGSI_OPCODE_SAMPLE] = { 0, tgsi_unsupported},
9641 [TGSI_OPCODE_SAMPLE_I] = { 0, tgsi_unsupported},
9642 [TGSI_OPCODE_SAMPLE_I_MS] = { 0, tgsi_unsupported},
9643 [TGSI_OPCODE_SAMPLE_B] = { 0, tgsi_unsupported},
9644 [TGSI_OPCODE_SAMPLE_C] = { 0, tgsi_unsupported},
9645 [TGSI_OPCODE_SAMPLE_C_LZ] = { 0, tgsi_unsupported},
9646 [TGSI_OPCODE_SAMPLE_D] = { 0, tgsi_unsupported},
9647 [TGSI_OPCODE_SAMPLE_L] = { 0, tgsi_unsupported},
9648 [TGSI_OPCODE_GATHER4] = { 0, tgsi_unsupported},
9649 [TGSI_OPCODE_SVIEWINFO] = { 0, tgsi_unsupported},
9650 [TGSI_OPCODE_SAMPLE_POS] = { 0, tgsi_unsupported},
9651 [TGSI_OPCODE_SAMPLE_INFO] = { 0, tgsi_unsupported},
9652 [TGSI_OPCODE_UARL] = { ALU_OP1_MOVA_INT, tgsi_eg_arl},
9653 [TGSI_OPCODE_UCMP] = { ALU_OP0_NOP, tgsi_ucmp},
9654 [TGSI_OPCODE_IABS] = { 0, tgsi_iabs},
9655 [TGSI_OPCODE_ISSG] = { 0, tgsi_issg},
9656 [TGSI_OPCODE_LOAD] = { ALU_OP0_NOP, tgsi_unsupported},
9657 [TGSI_OPCODE_STORE] = { ALU_OP0_NOP, tgsi_unsupported},
9658 [TGSI_OPCODE_MFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9659 [TGSI_OPCODE_LFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9660 [TGSI_OPCODE_SFENCE] = { ALU_OP0_NOP, tgsi_unsupported},
9661 [TGSI_OPCODE_BARRIER] = { ALU_OP0_GROUP_BARRIER, tgsi_barrier},
9662 [TGSI_OPCODE_ATOMUADD] = { ALU_OP0_NOP, tgsi_unsupported},
9663 [TGSI_OPCODE_ATOMXCHG] = { ALU_OP0_NOP, tgsi_unsupported},
9664 [TGSI_OPCODE_ATOMCAS] = { ALU_OP0_NOP, tgsi_unsupported},
9665 [TGSI_OPCODE_ATOMAND] = { ALU_OP0_NOP, tgsi_unsupported},
9666 [TGSI_OPCODE_ATOMOR] = { ALU_OP0_NOP, tgsi_unsupported},
9667 [TGSI_OPCODE_ATOMXOR] = { ALU_OP0_NOP, tgsi_unsupported},
9668 [TGSI_OPCODE_ATOMUMIN] = { ALU_OP0_NOP, tgsi_unsupported},
9669 [TGSI_OPCODE_ATOMUMAX] = { ALU_OP0_NOP, tgsi_unsupported},
9670 [TGSI_OPCODE_ATOMIMIN] = { ALU_OP0_NOP, tgsi_unsupported},
9671 [TGSI_OPCODE_ATOMIMAX] = { ALU_OP0_NOP, tgsi_unsupported},
9672 [TGSI_OPCODE_TEX2] = { FETCH_OP_SAMPLE, tgsi_tex},
9673 [TGSI_OPCODE_TXB2] = { FETCH_OP_SAMPLE_LB, tgsi_tex},
9674 [TGSI_OPCODE_TXL2] = { FETCH_OP_SAMPLE_L, tgsi_tex},
9675 [TGSI_OPCODE_IMUL_HI] = { ALU_OP2_MULHI_INT, cayman_mul_int_instr},
9676 [TGSI_OPCODE_UMUL_HI] = { ALU_OP2_MULHI_UINT, cayman_mul_int_instr},
9677 [TGSI_OPCODE_TG4] = { FETCH_OP_GATHER4, tgsi_tex},
9678 [TGSI_OPCODE_LODQ] = { FETCH_OP_GET_LOD, tgsi_tex},
9679 [TGSI_OPCODE_IBFE] = { ALU_OP3_BFE_INT, tgsi_op3},
9680 [TGSI_OPCODE_UBFE] = { ALU_OP3_BFE_UINT, tgsi_op3},
9681 [TGSI_OPCODE_BFI] = { ALU_OP0_NOP, tgsi_bfi},
9682 [TGSI_OPCODE_BREV] = { ALU_OP1_BFREV_INT, tgsi_op2},
9683 [TGSI_OPCODE_POPC] = { ALU_OP1_BCNT_INT, tgsi_op2},
9684 [TGSI_OPCODE_LSB] = { ALU_OP1_FFBL_INT, tgsi_op2},
9685 [TGSI_OPCODE_IMSB] = { ALU_OP1_FFBH_INT, tgsi_msb},
9686 [TGSI_OPCODE_UMSB] = { ALU_OP1_FFBH_UINT, tgsi_msb},
9687 [TGSI_OPCODE_INTERP_CENTROID] = { ALU_OP0_NOP, tgsi_interp_egcm},
9688 [TGSI_OPCODE_INTERP_SAMPLE] = { ALU_OP0_NOP, tgsi_interp_egcm},
9689 [TGSI_OPCODE_INTERP_OFFSET] = { ALU_OP0_NOP, tgsi_interp_egcm},
9690 [TGSI_OPCODE_F2D] = { ALU_OP1_FLT32_TO_FLT64, tgsi_op2_64},
9691 [TGSI_OPCODE_D2F] = { ALU_OP1_FLT64_TO_FLT32, tgsi_op2_64_single_dest},
9692 [TGSI_OPCODE_DABS] = { ALU_OP1_MOV, tgsi_op2_64},
9693 [TGSI_OPCODE_DNEG] = { ALU_OP2_ADD_64, tgsi_dneg},
9694 [TGSI_OPCODE_DADD] = { ALU_OP2_ADD_64, tgsi_op2_64},
9695 [TGSI_OPCODE_DMUL] = { ALU_OP2_MUL_64, cayman_mul_double_instr},
9696 [TGSI_OPCODE_DDIV] = { 0, cayman_ddiv_instr },
9697 [TGSI_OPCODE_DMAX] = { ALU_OP2_MAX_64, tgsi_op2_64},
9698 [TGSI_OPCODE_DMIN] = { ALU_OP2_MIN_64, tgsi_op2_64},
9699 [TGSI_OPCODE_DSLT] = { ALU_OP2_SETGT_64, tgsi_op2_64_single_dest_s},
9700 [TGSI_OPCODE_DSGE] = { ALU_OP2_SETGE_64, tgsi_op2_64_single_dest},
9701 [TGSI_OPCODE_DSEQ] = { ALU_OP2_SETE_64, tgsi_op2_64_single_dest},
9702 [TGSI_OPCODE_DSNE] = { ALU_OP2_SETNE_64, tgsi_op2_64_single_dest},
9703 [TGSI_OPCODE_DRCP] = { ALU_OP2_RECIP_64, cayman_emit_double_instr},
9704 [TGSI_OPCODE_DSQRT] = { ALU_OP2_SQRT_64, cayman_emit_double_instr},
9705 [TGSI_OPCODE_DMAD] = { ALU_OP3_FMA_64, tgsi_op3_64},
9706 [TGSI_OPCODE_DFMA] = { ALU_OP3_FMA_64, tgsi_op3_64},
9707 [TGSI_OPCODE_DFRAC] = { ALU_OP1_FRACT_64, tgsi_op2_64},
9708 [TGSI_OPCODE_DLDEXP] = { ALU_OP2_LDEXP_64, tgsi_op2_64},
9709 [TGSI_OPCODE_DFRACEXP] = { ALU_OP1_FREXP_64, tgsi_dfracexp},
9710 [TGSI_OPCODE_D2I] = { ALU_OP1_FLT_TO_INT, egcm_double_to_int},
9711 [TGSI_OPCODE_I2D] = { ALU_OP1_INT_TO_FLT, egcm_int_to_double},
9712 [TGSI_OPCODE_D2U] = { ALU_OP1_FLT_TO_UINT, egcm_double_to_int},
9713 [TGSI_OPCODE_U2D] = { ALU_OP1_UINT_TO_FLT, egcm_int_to_double},
9714 [TGSI_OPCODE_DRSQ] = { ALU_OP2_RECIPSQRT_64, cayman_emit_double_instr},
9715 [TGSI_OPCODE_LAST] = { ALU_OP0_NOP, tgsi_unsupported},
9716 };