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