gallium/ttn: add TXQ support (v2)
[mesa.git] / src / gallium / auxiliary / nir / tgsi_to_nir.c
1 /*
2 * Copyright © 2014-2015 Broadcom
3 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "util/ralloc.h"
26 #include "glsl/nir/nir.h"
27 #include "glsl/nir/nir_builder.h"
28 #include "glsl/list.h"
29 #include "glsl/shader_enums.h"
30
31 #include "nir/tgsi_to_nir.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "tgsi/tgsi_dump.h"
34 #include "tgsi/tgsi_info.h"
35 #include "tgsi/tgsi_scan.h"
36
37 #define SWIZ(X, Y, Z, W) (unsigned[4]){ \
38 TGSI_SWIZZLE_##X, \
39 TGSI_SWIZZLE_##Y, \
40 TGSI_SWIZZLE_##Z, \
41 TGSI_SWIZZLE_##W, \
42 }
43
44 struct ttn_reg_info {
45 /** nir register containing this TGSI index. */
46 nir_register *reg;
47 nir_variable *var;
48 /** Offset (in vec4s) from the start of var for this TGSI index. */
49 int offset;
50 };
51
52 struct ttn_compile {
53 union tgsi_full_token *token;
54 nir_builder build;
55 struct nir_shader *s;
56 struct tgsi_shader_info *scan;
57
58 struct ttn_reg_info *output_regs;
59 struct ttn_reg_info *temp_regs;
60 nir_ssa_def **imm_defs;
61
62 nir_register *addr_reg;
63
64 /**
65 * Stack of cf_node_lists where instructions should be pushed as we pop
66 * back out of the control flow stack.
67 *
68 * For each IF/ELSE/ENDIF block, if_stack[if_stack_pos] has where the else
69 * instructions should be placed, and if_stack[if_stack_pos - 1] has where
70 * the next instructions outside of the if/then/else block go.
71 */
72 struct exec_list **if_stack;
73 unsigned if_stack_pos;
74
75 /**
76 * Stack of cf_node_lists where instructions should be pushed as we pop
77 * back out of the control flow stack.
78 *
79 * loop_stack[loop_stack_pos - 1] contains the cf_node_list for the outside
80 * of the loop.
81 */
82 struct exec_list **loop_stack;
83 unsigned loop_stack_pos;
84
85 /* How many TGSI_FILE_IMMEDIATE vec4s have been parsed so far. */
86 unsigned next_imm;
87 };
88
89 #define ttn_swizzle(b, src, x, y, z, w) \
90 nir_swizzle(b, src, SWIZ(x, y, z, w), 4, false)
91 #define ttn_channel(b, src, swiz) \
92 nir_swizzle(b, src, SWIZ(swiz, swiz, swiz, swiz), 1, false)
93
94 static nir_ssa_def *
95 ttn_src_for_dest(nir_builder *b, nir_alu_dest *dest)
96 {
97 nir_alu_src src;
98 memset(&src, 0, sizeof(src));
99
100 if (dest->dest.is_ssa)
101 src.src = nir_src_for_ssa(&dest->dest.ssa);
102 else {
103 assert(!dest->dest.reg.indirect);
104 src.src = nir_src_for_reg(dest->dest.reg.reg);
105 src.src.reg.base_offset = dest->dest.reg.base_offset;
106 }
107
108 for (int i = 0; i < 4; i++)
109 src.swizzle[i] = i;
110
111 return nir_fmov_alu(b, src, 4);
112 }
113
114 static void
115 ttn_emit_declaration(struct ttn_compile *c)
116 {
117 nir_builder *b = &c->build;
118 struct tgsi_full_declaration *decl = &c->token->FullDeclaration;
119 unsigned array_size = decl->Range.Last - decl->Range.First + 1;
120 unsigned file = decl->Declaration.File;
121 unsigned i;
122
123 if (file == TGSI_FILE_TEMPORARY) {
124 if (decl->Declaration.Array) {
125 /* for arrays, we create variables instead of registers: */
126 nir_variable *var = rzalloc(b->shader, nir_variable);
127
128 var->type = glsl_array_type(glsl_vec4_type(), array_size);
129 var->data.mode = nir_var_global;
130 var->name = ralloc_asprintf(var, "arr_%d", decl->Array.ArrayID);
131
132 exec_list_push_tail(&b->shader->globals, &var->node);
133
134 for (i = 0; i < array_size; i++) {
135 /* point all the matching slots to the same var,
136 * with appropriate offset set, mostly just so
137 * we know what to do when tgsi does a non-indirect
138 * access
139 */
140 c->temp_regs[decl->Range.First + i].reg = NULL;
141 c->temp_regs[decl->Range.First + i].var = var;
142 c->temp_regs[decl->Range.First + i].offset = i;
143 }
144 } else {
145 for (i = 0; i < array_size; i++) {
146 nir_register *reg = nir_local_reg_create(b->impl);
147 reg->num_components = 4;
148 c->temp_regs[decl->Range.First + i].reg = reg;
149 c->temp_regs[decl->Range.First + i].var = NULL;
150 c->temp_regs[decl->Range.First + i].offset = 0;
151 }
152 }
153 } else if (file == TGSI_FILE_ADDRESS) {
154 c->addr_reg = nir_local_reg_create(b->impl);
155 c->addr_reg->num_components = 4;
156 } else if (file == TGSI_FILE_SAMPLER) {
157 /* Nothing to record for samplers. */
158 } else {
159 nir_variable *var;
160 assert(file == TGSI_FILE_INPUT ||
161 file == TGSI_FILE_OUTPUT ||
162 file == TGSI_FILE_CONSTANT);
163
164 var = rzalloc(b->shader, nir_variable);
165 var->data.driver_location = decl->Range.First;
166
167 var->type = glsl_vec4_type();
168 if (array_size > 1)
169 var->type = glsl_array_type(var->type, array_size);
170
171 switch (file) {
172 case TGSI_FILE_INPUT:
173 var->data.read_only = true;
174 var->data.mode = nir_var_shader_in;
175 var->name = ralloc_asprintf(var, "in_%d", decl->Range.First);
176
177 /* We should probably translate to a VERT_ATTRIB_* or VARYING_SLOT_*
178 * instead, but nothing in NIR core is looking at the value
179 * currently, and this is less change to drivers.
180 */
181 var->data.location = decl->Semantic.Name;
182 var->data.index = decl->Semantic.Index;
183
184 /* We definitely need to translate the interpolation field, because
185 * nir_print will decode it.
186 */
187 switch (decl->Interp.Interpolate) {
188 case TGSI_INTERPOLATE_CONSTANT:
189 var->data.interpolation = INTERP_QUALIFIER_FLAT;
190 break;
191 case TGSI_INTERPOLATE_LINEAR:
192 var->data.interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
193 break;
194 case TGSI_INTERPOLATE_PERSPECTIVE:
195 var->data.interpolation = INTERP_QUALIFIER_SMOOTH;
196 break;
197 }
198
199 exec_list_push_tail(&b->shader->inputs, &var->node);
200 break;
201 case TGSI_FILE_OUTPUT: {
202 /* Since we can't load from outputs in the IR, we make temporaries
203 * for the outputs and emit stores to the real outputs at the end of
204 * the shader.
205 */
206 nir_register *reg = nir_local_reg_create(b->impl);
207 reg->num_components = 4;
208 if (array_size > 1)
209 reg->num_array_elems = array_size;
210
211 var->data.mode = nir_var_shader_out;
212 var->name = ralloc_asprintf(var, "out_%d", decl->Range.First);
213
214 var->data.location = decl->Semantic.Name;
215 var->data.index = decl->Semantic.Index;
216
217 for (i = 0; i < array_size; i++) {
218 c->output_regs[decl->Range.First + i].offset = i;
219 c->output_regs[decl->Range.First + i].reg = reg;
220 }
221
222 exec_list_push_tail(&b->shader->outputs, &var->node);
223 }
224 break;
225 case TGSI_FILE_CONSTANT:
226 var->data.mode = nir_var_uniform;
227 var->name = ralloc_asprintf(var, "uniform_%d", decl->Range.First);
228
229 exec_list_push_tail(&b->shader->uniforms, &var->node);
230 break;
231 default:
232 unreachable("bad declaration file");
233 return;
234 }
235
236 }
237 }
238
239 static void
240 ttn_emit_immediate(struct ttn_compile *c)
241 {
242 nir_builder *b = &c->build;
243 struct tgsi_full_immediate *tgsi_imm = &c->token->FullImmediate;
244 nir_load_const_instr *load_const;
245 int i;
246
247 load_const = nir_load_const_instr_create(b->shader, 4);
248 c->imm_defs[c->next_imm] = &load_const->def;
249 c->next_imm++;
250
251 for (i = 0; i < 4; i++)
252 load_const->value.u[i] = tgsi_imm->u[i].Uint;
253
254 nir_instr_insert_after_cf_list(b->cf_node_list, &load_const->instr);
255 }
256
257 static nir_src *
258 ttn_src_for_indirect(struct ttn_compile *c, struct tgsi_ind_register *indirect);
259
260 /* generate either a constant or indirect deref chain for accessing an
261 * array variable.
262 */
263 static nir_deref_var *
264 ttn_array_deref(struct ttn_compile *c, nir_intrinsic_instr *instr,
265 nir_variable *var, unsigned offset,
266 struct tgsi_ind_register *indirect)
267 {
268 nir_deref_var *deref = nir_deref_var_create(instr, var);
269 nir_deref_array *arr = nir_deref_array_create(deref);
270
271 arr->base_offset = offset;
272 arr->deref.type = glsl_get_array_element(var->type);
273
274 if (indirect) {
275 arr->deref_array_type = nir_deref_array_type_indirect;
276 arr->indirect = *ttn_src_for_indirect(c, indirect);
277 } else {
278 arr->deref_array_type = nir_deref_array_type_direct;
279 }
280
281 deref->deref.child = &arr->deref;
282
283 return deref;
284 }
285
286 static nir_src
287 ttn_src_for_file_and_index(struct ttn_compile *c, unsigned file, unsigned index,
288 struct tgsi_ind_register *indirect)
289 {
290 nir_builder *b = &c->build;
291 nir_src src;
292
293 memset(&src, 0, sizeof(src));
294
295 switch (file) {
296 case TGSI_FILE_TEMPORARY:
297 if (c->temp_regs[index].var) {
298 unsigned offset = c->temp_regs[index].offset;
299 nir_variable *var = c->temp_regs[index].var;
300 nir_intrinsic_instr *load;
301
302 load = nir_intrinsic_instr_create(b->shader,
303 nir_intrinsic_load_var);
304 load->num_components = 4;
305 load->variables[0] = ttn_array_deref(c, load, var, offset, indirect);
306
307 nir_ssa_dest_init(&load->instr, &load->dest, 4, NULL);
308 nir_instr_insert_after_cf_list(b->cf_node_list, &load->instr);
309
310 src = nir_src_for_ssa(&load->dest.ssa);
311
312 } else {
313 assert(!indirect);
314 src.reg.reg = c->temp_regs[index].reg;
315 }
316 break;
317
318 case TGSI_FILE_ADDRESS:
319 src.reg.reg = c->addr_reg;
320 break;
321
322 case TGSI_FILE_IMMEDIATE:
323 src = nir_src_for_ssa(c->imm_defs[index]);
324 assert(!indirect);
325 break;
326
327 case TGSI_FILE_INPUT:
328 case TGSI_FILE_CONSTANT: {
329 nir_intrinsic_instr *load;
330
331 switch (file) {
332 case TGSI_FILE_INPUT:
333 load = nir_intrinsic_instr_create(b->shader,
334 indirect ?
335 nir_intrinsic_load_input_indirect :
336 nir_intrinsic_load_input);
337 break;
338 case TGSI_FILE_CONSTANT:
339 load = nir_intrinsic_instr_create(b->shader,
340 indirect ?
341 nir_intrinsic_load_uniform_indirect :
342 nir_intrinsic_load_uniform);
343 break;
344 default:
345 unreachable("No other load files supported");
346 break;
347 }
348
349 load->num_components = 4;
350 load->const_index[0] = index;
351 load->const_index[1] = 1;
352 if (indirect) {
353 nir_alu_src indirect_address;
354 memset(&indirect_address, 0, sizeof(indirect_address));
355 indirect_address.src = nir_src_for_reg(c->addr_reg);
356 for (int i = 0; i < 4; i++)
357 indirect_address.swizzle[i] = indirect->Swizzle;
358 load->src[0] = nir_src_for_ssa(nir_imov_alu(b, indirect_address, 1));
359 }
360 nir_ssa_dest_init(&load->instr, &load->dest, 4, NULL);
361 nir_instr_insert_after_cf_list(b->cf_node_list, &load->instr);
362
363 src = nir_src_for_ssa(&load->dest.ssa);
364 break;
365 }
366
367 default:
368 unreachable("bad src file");
369 }
370
371
372 return src;
373 }
374
375 static nir_src *
376 ttn_src_for_indirect(struct ttn_compile *c, struct tgsi_ind_register *indirect)
377 {
378 nir_builder *b = &c->build;
379 nir_alu_src src;
380 memset(&src, 0, sizeof(src));
381 for (int i = 0; i < 4; i++)
382 src.swizzle[i] = indirect->Swizzle;
383 src.src = ttn_src_for_file_and_index(c,
384 indirect->File,
385 indirect->Index, NULL);
386 nir_src *result = ralloc(b->shader, nir_src);
387 *result = nir_src_for_ssa(nir_imov_alu(b, src, 1));
388 return result;
389 }
390
391 static nir_alu_dest
392 ttn_get_dest(struct ttn_compile *c, struct tgsi_full_dst_register *tgsi_fdst)
393 {
394 struct tgsi_dst_register *tgsi_dst = &tgsi_fdst->Register;
395 nir_alu_dest dest;
396 unsigned index = tgsi_dst->Index;
397
398 memset(&dest, 0, sizeof(dest));
399
400 if (tgsi_dst->File == TGSI_FILE_TEMPORARY) {
401 if (c->temp_regs[index].var) {
402 nir_builder *b = &c->build;
403 nir_intrinsic_instr *load;
404 struct tgsi_ind_register *indirect =
405 tgsi_dst->Indirect ? &tgsi_fdst->Indirect : NULL;
406 nir_register *reg;
407
408 /* this works, because TGSI will give us a base offset
409 * (in case of indirect index) that points back into
410 * the array. Access can be direct or indirect, we
411 * don't really care. Just create a one-shot dst reg
412 * that will get store_var'd back into the array var
413 * at the end of ttn_emit_instruction()
414 */
415 reg = nir_local_reg_create(c->build.impl);
416 reg->num_components = 4;
417 dest.dest.reg.reg = reg;
418 dest.dest.reg.base_offset = 0;
419
420 /* since the alu op might not write to all components
421 * of the temporary, we must first do a load_var to
422 * get the previous array elements into the register.
423 * This is one area that NIR could use a bit of
424 * improvement (or opt pass to clean up the mess
425 * once things are scalarized)
426 */
427
428 load = nir_intrinsic_instr_create(c->build.shader,
429 nir_intrinsic_load_var);
430 load->num_components = 4;
431 load->variables[0] =
432 ttn_array_deref(c, load, c->temp_regs[index].var,
433 c->temp_regs[index].offset,
434 indirect);
435
436 load->dest = nir_dest_for_reg(reg);
437
438 nir_instr_insert_after_cf_list(b->cf_node_list, &load->instr);
439 } else {
440 assert(!tgsi_dst->Indirect);
441 dest.dest.reg.reg = c->temp_regs[index].reg;
442 dest.dest.reg.base_offset = c->temp_regs[index].offset;
443 }
444 } else if (tgsi_dst->File == TGSI_FILE_OUTPUT) {
445 dest.dest.reg.reg = c->output_regs[index].reg;
446 dest.dest.reg.base_offset = c->output_regs[index].offset;
447 } else if (tgsi_dst->File == TGSI_FILE_ADDRESS) {
448 assert(index == 0);
449 dest.dest.reg.reg = c->addr_reg;
450 }
451
452 dest.write_mask = tgsi_dst->WriteMask;
453 dest.saturate = false;
454
455 if (tgsi_dst->Indirect && (tgsi_dst->File != TGSI_FILE_TEMPORARY))
456 dest.dest.reg.indirect = ttn_src_for_indirect(c, &tgsi_fdst->Indirect);
457
458 return dest;
459 }
460
461 static nir_variable *
462 ttn_get_var(struct ttn_compile *c, struct tgsi_full_dst_register *tgsi_fdst)
463 {
464 struct tgsi_dst_register *tgsi_dst = &tgsi_fdst->Register;
465 unsigned index = tgsi_dst->Index;
466
467 if (tgsi_dst->File == TGSI_FILE_TEMPORARY) {
468 /* we should not have an indirect when there is no var! */
469 if (!c->temp_regs[index].var)
470 assert(!tgsi_dst->Indirect);
471 return c->temp_regs[index].var;
472 }
473
474 return NULL;
475 }
476
477 static nir_ssa_def *
478 ttn_get_src(struct ttn_compile *c, struct tgsi_full_src_register *tgsi_fsrc)
479 {
480 nir_builder *b = &c->build;
481 struct tgsi_src_register *tgsi_src = &tgsi_fsrc->Register;
482 unsigned tgsi_opcode = c->token->FullInstruction.Instruction.Opcode;
483 unsigned tgsi_src_type = tgsi_opcode_infer_src_type(tgsi_opcode);
484 bool src_is_float = !(tgsi_src_type == TGSI_TYPE_SIGNED ||
485 tgsi_src_type == TGSI_TYPE_UNSIGNED);
486 nir_alu_src src;
487
488 memset(&src, 0, sizeof(src));
489
490 if (tgsi_src->File == TGSI_FILE_NULL) {
491 return nir_imm_float(b, 0.0);
492 } else if (tgsi_src->File == TGSI_FILE_SAMPLER) {
493 /* Only the index of the sampler gets used in texturing, and it will
494 * handle looking that up on its own instead of using the nir_alu_src.
495 */
496 assert(!tgsi_src->Indirect);
497 return NULL;
498 } else {
499 src.src = ttn_src_for_file_and_index(c,
500 tgsi_src->File,
501 tgsi_src->Index,
502 (tgsi_src->Indirect ?
503 &tgsi_fsrc->Indirect : NULL));
504 }
505
506 src.swizzle[0] = tgsi_src->SwizzleX;
507 src.swizzle[1] = tgsi_src->SwizzleY;
508 src.swizzle[2] = tgsi_src->SwizzleZ;
509 src.swizzle[3] = tgsi_src->SwizzleW;
510
511 nir_ssa_def *def = nir_fmov_alu(b, src, 4);
512
513 if (tgsi_src->Absolute) {
514 if (src_is_float)
515 def = nir_fabs(b, def);
516 else
517 def = nir_iabs(b, def);
518 }
519
520 if (tgsi_src->Negate) {
521 if (src_is_float)
522 def = nir_fneg(b, def);
523 else
524 def = nir_ineg(b, def);
525 }
526
527 return def;
528 }
529
530 static void
531 ttn_alu(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
532 {
533 unsigned num_srcs = nir_op_infos[op].num_inputs;
534 nir_alu_instr *instr = nir_alu_instr_create(b->shader, op);
535 unsigned i;
536
537 for (i = 0; i < num_srcs; i++)
538 instr->src[i].src = nir_src_for_ssa(src[i]);
539
540 instr->dest = dest;
541 nir_instr_insert_after_cf_list(b->cf_node_list, &instr->instr);
542 }
543
544 static void
545 ttn_move_dest_masked(nir_builder *b, nir_alu_dest dest,
546 nir_ssa_def *def, unsigned write_mask)
547 {
548 if (!(dest.write_mask & write_mask))
549 return;
550
551 nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_imov);
552 mov->dest = dest;
553 mov->dest.write_mask &= write_mask;
554 mov->src[0].src = nir_src_for_ssa(def);
555 for (unsigned i = def->num_components; i < 4; i++)
556 mov->src[0].swizzle[i] = def->num_components - 1;
557 nir_instr_insert_after_cf_list(b->cf_node_list, &mov->instr);
558 }
559
560 static void
561 ttn_move_dest(nir_builder *b, nir_alu_dest dest, nir_ssa_def *def)
562 {
563 ttn_move_dest_masked(b, dest, def, TGSI_WRITEMASK_XYZW);
564 }
565
566 static void
567 ttn_arl(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
568 {
569 ttn_move_dest(b, dest, nir_f2i(b, nir_ffloor(b, src[0])));
570 }
571
572 /* EXP - Approximate Exponential Base 2
573 * dst.x = 2^{\lfloor src.x\rfloor}
574 * dst.y = src.x - \lfloor src.x\rfloor
575 * dst.z = 2^{src.x}
576 * dst.w = 1.0
577 */
578 static void
579 ttn_exp(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
580 {
581 nir_ssa_def *srcx = ttn_channel(b, src[0], X);
582
583 ttn_move_dest_masked(b, dest, nir_fexp2(b, nir_ffloor(b, srcx)),
584 TGSI_WRITEMASK_X);
585 ttn_move_dest_masked(b, dest, nir_fsub(b, srcx, nir_ffloor(b, srcx)),
586 TGSI_WRITEMASK_Y);
587 ttn_move_dest_masked(b, dest, nir_fexp2(b, srcx), TGSI_WRITEMASK_Z);
588 ttn_move_dest_masked(b, dest, nir_imm_float(b, 1.0), TGSI_WRITEMASK_W);
589 }
590
591 /* LOG - Approximate Logarithm Base 2
592 * dst.x = \lfloor\log_2{|src.x|}\rfloor
593 * dst.y = \frac{|src.x|}{2^{\lfloor\log_2{|src.x|}\rfloor}}
594 * dst.z = \log_2{|src.x|}
595 * dst.w = 1.0
596 */
597 static void
598 ttn_log(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
599 {
600 nir_ssa_def *abs_srcx = nir_fabs(b, ttn_channel(b, src[0], X));
601 nir_ssa_def *log2 = nir_flog2(b, abs_srcx);
602
603 ttn_move_dest_masked(b, dest, nir_ffloor(b, log2), TGSI_WRITEMASK_X);
604 ttn_move_dest_masked(b, dest,
605 nir_fdiv(b, abs_srcx, nir_fexp2(b, nir_ffloor(b, log2))),
606 TGSI_WRITEMASK_Y);
607 ttn_move_dest_masked(b, dest, nir_flog2(b, abs_srcx), TGSI_WRITEMASK_Z);
608 ttn_move_dest_masked(b, dest, nir_imm_float(b, 1.0), TGSI_WRITEMASK_W);
609 }
610
611 /* DST - Distance Vector
612 * dst.x = 1.0
613 * dst.y = src0.y \times src1.y
614 * dst.z = src0.z
615 * dst.w = src1.w
616 */
617 static void
618 ttn_dst(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
619 {
620 ttn_move_dest_masked(b, dest, nir_imm_float(b, 1.0), TGSI_WRITEMASK_X);
621 ttn_move_dest_masked(b, dest, nir_fmul(b, src[0], src[1]), TGSI_WRITEMASK_Y);
622 ttn_move_dest_masked(b, dest, nir_fmov(b, src[0]), TGSI_WRITEMASK_Z);
623 ttn_move_dest_masked(b, dest, nir_fmov(b, src[1]), TGSI_WRITEMASK_W);
624 }
625
626 /* LIT - Light Coefficients
627 * dst.x = 1.0
628 * dst.y = max(src.x, 0.0)
629 * dst.z = (src.x > 0.0) ? max(src.y, 0.0)^{clamp(src.w, -128.0, 128.0))} : 0
630 * dst.w = 1.0
631 */
632 static void
633 ttn_lit(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
634 {
635 ttn_move_dest_masked(b, dest, nir_imm_float(b, 1.0), TGSI_WRITEMASK_XW);
636
637 ttn_move_dest_masked(b, dest, nir_fmax(b, ttn_channel(b, src[0], X),
638 nir_imm_float(b, 0.0)), TGSI_WRITEMASK_Y);
639
640 if (dest.write_mask & TGSI_WRITEMASK_Z) {
641 nir_ssa_def *src0_y = ttn_channel(b, src[0], Y);
642 nir_ssa_def *wclamp = nir_fmax(b, nir_fmin(b, ttn_channel(b, src[0], W),
643 nir_imm_float(b, 128.0)),
644 nir_imm_float(b, -128.0));
645 nir_ssa_def *pow = nir_fpow(b, nir_fmax(b, src0_y, nir_imm_float(b, 0.0)),
646 wclamp);
647
648 ttn_move_dest_masked(b, dest,
649 nir_bcsel(b,
650 nir_fge(b,
651 nir_imm_float(b, 0.0),
652 ttn_channel(b, src[0], X)),
653 nir_imm_float(b, 0.0),
654 pow),
655 TGSI_WRITEMASK_Z);
656 }
657 }
658
659 /* SCS - Sine Cosine
660 * dst.x = \cos{src.x}
661 * dst.y = \sin{src.x}
662 * dst.z = 0.0
663 * dst.w = 1.0
664 */
665 static void
666 ttn_scs(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
667 {
668 ttn_move_dest_masked(b, dest, nir_fcos(b, ttn_channel(b, src[0], X)),
669 TGSI_WRITEMASK_X);
670 ttn_move_dest_masked(b, dest, nir_fsin(b, ttn_channel(b, src[0], X)),
671 TGSI_WRITEMASK_Y);
672 ttn_move_dest_masked(b, dest, nir_imm_float(b, 0.0), TGSI_WRITEMASK_Z);
673 ttn_move_dest_masked(b, dest, nir_imm_float(b, 1.0), TGSI_WRITEMASK_W);
674 }
675
676 static void
677 ttn_sle(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
678 {
679 ttn_move_dest(b, dest, nir_sge(b, src[1], src[0]));
680 }
681
682 static void
683 ttn_sgt(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
684 {
685 ttn_move_dest(b, dest, nir_slt(b, src[1], src[0]));
686 }
687
688 static void
689 ttn_clamp(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
690 {
691 ttn_move_dest(b, dest, nir_fmin(b, nir_fmax(b, src[0], src[1]), src[2]));
692 }
693
694 static void
695 ttn_xpd(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
696 {
697 ttn_move_dest_masked(b, dest,
698 nir_fsub(b,
699 nir_fmul(b,
700 ttn_swizzle(b, src[0], Y, Z, X, X),
701 ttn_swizzle(b, src[1], Z, X, Y, X)),
702 nir_fmul(b,
703 ttn_swizzle(b, src[1], Y, Z, X, X),
704 ttn_swizzle(b, src[0], Z, X, Y, X))),
705 TGSI_WRITEMASK_XYZ);
706 ttn_move_dest_masked(b, dest, nir_imm_float(b, 1.0), TGSI_WRITEMASK_W);
707 }
708
709 static void
710 ttn_dp2a(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
711 {
712 ttn_move_dest(b, dest,
713 ttn_channel(b, nir_fadd(b, nir_fdot2(b, src[0], src[1]),
714 src[2]),
715 X));
716 }
717
718 static void
719 ttn_dp2(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
720 {
721 ttn_move_dest(b, dest, nir_fdot2(b, src[0], src[1]));
722 }
723
724 static void
725 ttn_dp3(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
726 {
727 ttn_move_dest(b, dest, nir_fdot3(b, src[0], src[1]));
728 }
729
730 static void
731 ttn_dp4(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
732 {
733 ttn_move_dest(b, dest, nir_fdot4(b, src[0], src[1]));
734 }
735
736 static void
737 ttn_dph(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
738 {
739 ttn_move_dest(b, dest, nir_fadd(b, nir_fdot3(b, src[0], src[1]),
740 ttn_channel(b, src[1], W)));
741 }
742
743 static void
744 ttn_umad(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
745 {
746 ttn_move_dest(b, dest, nir_iadd(b, nir_imul(b, src[0], src[1]), src[2]));
747 }
748
749 static void
750 ttn_arr(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
751 {
752 ttn_move_dest(b, dest, nir_ffloor(b, nir_fadd(b, src[0], nir_imm_float(b, 0.5))));
753 }
754
755 static void
756 ttn_cmp(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
757 {
758 ttn_move_dest(b, dest, nir_bcsel(b,
759 nir_flt(b, src[0], nir_imm_float(b, 0.0)),
760 src[1], src[2]));
761 }
762
763 static void
764 ttn_ucmp(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
765 {
766 ttn_move_dest(b, dest, nir_bcsel(b,
767 nir_ine(b, src[0], nir_imm_int(b, 0)),
768 src[1], src[2]));
769 }
770
771 static void
772 ttn_kill(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
773 {
774 nir_intrinsic_instr *discard =
775 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
776 nir_instr_insert_after_cf_list(b->cf_node_list, &discard->instr);
777 }
778
779 static void
780 ttn_kill_if(nir_builder *b, nir_op op, nir_alu_dest dest, nir_ssa_def **src)
781 {
782 nir_ssa_def *cmp = nir_bany4(b, nir_flt(b, src[0], nir_imm_float(b, 0.0)));
783 nir_intrinsic_instr *discard =
784 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if);
785 discard->src[0] = nir_src_for_ssa(cmp);
786 nir_instr_insert_after_cf_list(b->cf_node_list, &discard->instr);
787 }
788
789 static void
790 ttn_if(struct ttn_compile *c, nir_ssa_def *src, bool is_uint)
791 {
792 nir_builder *b = &c->build;
793
794 /* Save the outside-of-the-if-statement node list. */
795 c->if_stack[c->if_stack_pos] = b->cf_node_list;
796 c->if_stack_pos++;
797
798 src = ttn_channel(b, src, X);
799
800 nir_if *if_stmt = nir_if_create(b->shader);
801 if (is_uint) {
802 if_stmt->condition = nir_src_for_ssa(nir_ine(b, src, nir_imm_int(b, 0)));
803 } else {
804 if_stmt->condition = nir_src_for_ssa(nir_fne(b, src, nir_imm_int(b, 0)));
805 }
806 nir_cf_node_insert_end(b->cf_node_list, &if_stmt->cf_node);
807
808 nir_builder_insert_after_cf_list(b, &if_stmt->then_list);
809
810 c->if_stack[c->if_stack_pos] = &if_stmt->else_list;
811 c->if_stack_pos++;
812 }
813
814 static void
815 ttn_else(struct ttn_compile *c)
816 {
817 nir_builder *b = &c->build;
818
819 nir_builder_insert_after_cf_list(b, c->if_stack[c->if_stack_pos - 1]);
820 }
821
822 static void
823 ttn_endif(struct ttn_compile *c)
824 {
825 nir_builder *b = &c->build;
826
827 c->if_stack_pos -= 2;
828 nir_builder_insert_after_cf_list(b, c->if_stack[c->if_stack_pos]);
829 }
830
831 static void
832 ttn_bgnloop(struct ttn_compile *c)
833 {
834 nir_builder *b = &c->build;
835
836 /* Save the outside-of-the-loop node list. */
837 c->loop_stack[c->loop_stack_pos] = b->cf_node_list;
838 c->loop_stack_pos++;
839
840 nir_loop *loop = nir_loop_create(b->shader);
841 nir_cf_node_insert_end(b->cf_node_list, &loop->cf_node);
842
843 nir_builder_insert_after_cf_list(b, &loop->body);
844 }
845
846 static void
847 ttn_cont(nir_builder *b)
848 {
849 nir_jump_instr *instr = nir_jump_instr_create(b->shader, nir_jump_continue);
850 nir_instr_insert_after_cf_list(b->cf_node_list, &instr->instr);
851 }
852
853 static void
854 ttn_brk(nir_builder *b)
855 {
856 nir_jump_instr *instr = nir_jump_instr_create(b->shader, nir_jump_break);
857 nir_instr_insert_after_cf_list(b->cf_node_list, &instr->instr);
858 }
859
860 static void
861 ttn_endloop(struct ttn_compile *c)
862 {
863 nir_builder *b = &c->build;
864
865 c->loop_stack_pos--;
866 nir_builder_insert_after_cf_list(b, c->loop_stack[c->loop_stack_pos]);
867 }
868
869 static void
870 setup_texture_info(nir_tex_instr *instr, unsigned texture)
871 {
872 switch (texture) {
873 case TGSI_TEXTURE_1D:
874 instr->sampler_dim = GLSL_SAMPLER_DIM_1D;
875 break;
876 case TGSI_TEXTURE_1D_ARRAY:
877 instr->sampler_dim = GLSL_SAMPLER_DIM_1D;
878 instr->is_array = true;
879 break;
880 case TGSI_TEXTURE_SHADOW1D:
881 instr->sampler_dim = GLSL_SAMPLER_DIM_1D;
882 instr->is_shadow = true;
883 break;
884 case TGSI_TEXTURE_SHADOW1D_ARRAY:
885 instr->sampler_dim = GLSL_SAMPLER_DIM_1D;
886 instr->is_shadow = true;
887 instr->is_array = true;
888 break;
889 case TGSI_TEXTURE_2D:
890 instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
891 break;
892 case TGSI_TEXTURE_2D_ARRAY:
893 instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
894 instr->is_array = true;
895 break;
896 case TGSI_TEXTURE_2D_MSAA:
897 instr->sampler_dim = GLSL_SAMPLER_DIM_MS;
898 break;
899 case TGSI_TEXTURE_2D_ARRAY_MSAA:
900 instr->sampler_dim = GLSL_SAMPLER_DIM_MS;
901 instr->is_array = true;
902 break;
903 case TGSI_TEXTURE_SHADOW2D:
904 instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
905 instr->is_shadow = true;
906 break;
907 case TGSI_TEXTURE_SHADOW2D_ARRAY:
908 instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
909 instr->is_shadow = true;
910 instr->is_array = true;
911 break;
912 case TGSI_TEXTURE_3D:
913 instr->sampler_dim = GLSL_SAMPLER_DIM_3D;
914 break;
915 case TGSI_TEXTURE_CUBE:
916 instr->sampler_dim = GLSL_SAMPLER_DIM_CUBE;
917 break;
918 case TGSI_TEXTURE_CUBE_ARRAY:
919 instr->sampler_dim = GLSL_SAMPLER_DIM_CUBE;
920 instr->is_array = true;
921 break;
922 case TGSI_TEXTURE_SHADOWCUBE:
923 instr->sampler_dim = GLSL_SAMPLER_DIM_CUBE;
924 instr->is_shadow = true;
925 break;
926 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
927 instr->sampler_dim = GLSL_SAMPLER_DIM_CUBE;
928 instr->is_shadow = true;
929 instr->is_array = true;
930 break;
931 case TGSI_TEXTURE_RECT:
932 instr->sampler_dim = GLSL_SAMPLER_DIM_RECT;
933 break;
934 case TGSI_TEXTURE_SHADOWRECT:
935 instr->sampler_dim = GLSL_SAMPLER_DIM_RECT;
936 instr->is_shadow = true;
937 break;
938 default:
939 fprintf(stderr, "Unknown TGSI texture target %d\n", texture);
940 abort();
941 }
942 }
943
944 static void
945 ttn_tex(struct ttn_compile *c, nir_alu_dest dest, nir_ssa_def **src)
946 {
947 nir_builder *b = &c->build;
948 struct tgsi_full_instruction *tgsi_inst = &c->token->FullInstruction;
949 nir_tex_instr *instr;
950 nir_texop op;
951 unsigned num_srcs;
952
953 switch (tgsi_inst->Instruction.Opcode) {
954 case TGSI_OPCODE_TEX:
955 op = nir_texop_tex;
956 num_srcs = 1;
957 break;
958 case TGSI_OPCODE_TXP:
959 op = nir_texop_tex;
960 num_srcs = 2;
961 break;
962 case TGSI_OPCODE_TXB:
963 op = nir_texop_txb;
964 num_srcs = 2;
965 break;
966 case TGSI_OPCODE_TXL:
967 op = nir_texop_txl;
968 num_srcs = 2;
969 break;
970 case TGSI_OPCODE_TXF:
971 op = nir_texop_txf;
972 num_srcs = 1;
973 break;
974 case TGSI_OPCODE_TXD:
975 op = nir_texop_txd;
976 num_srcs = 3;
977 break;
978
979 default:
980 fprintf(stderr, "unknown TGSI tex op %d\n", tgsi_inst->Instruction.Opcode);
981 abort();
982 }
983
984 if (tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D ||
985 tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOW1D_ARRAY ||
986 tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D ||
987 tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOW2D_ARRAY ||
988 tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOWRECT ||
989 tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE ||
990 tgsi_inst->Texture.Texture == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
991 num_srcs++;
992 }
993
994 instr = nir_tex_instr_create(b->shader, num_srcs);
995 instr->op = op;
996
997 setup_texture_info(instr, tgsi_inst->Texture.Texture);
998
999 switch (instr->sampler_dim) {
1000 case GLSL_SAMPLER_DIM_1D:
1001 case GLSL_SAMPLER_DIM_BUF:
1002 instr->coord_components = 1;
1003 break;
1004 case GLSL_SAMPLER_DIM_2D:
1005 case GLSL_SAMPLER_DIM_RECT:
1006 case GLSL_SAMPLER_DIM_EXTERNAL:
1007 case GLSL_SAMPLER_DIM_MS:
1008 instr->coord_components = 2;
1009 break;
1010 case GLSL_SAMPLER_DIM_3D:
1011 case GLSL_SAMPLER_DIM_CUBE:
1012 instr->coord_components = 3;
1013 break;
1014 }
1015
1016 if (instr->is_array)
1017 instr->coord_components++;
1018
1019 assert(tgsi_inst->Src[1].Register.File == TGSI_FILE_SAMPLER);
1020 instr->sampler_index = tgsi_inst->Src[1].Register.Index;
1021
1022 unsigned src_number = 0;
1023
1024 instr->src[src_number].src =
1025 nir_src_for_ssa(nir_swizzle(b, src[0], SWIZ(X, Y, Z, W),
1026 instr->coord_components, false));
1027 instr->src[src_number].src_type = nir_tex_src_coord;
1028 src_number++;
1029
1030 if (tgsi_inst->Instruction.Opcode == TGSI_OPCODE_TXP) {
1031 instr->src[src_number].src = nir_src_for_ssa(ttn_channel(b, src[0], W));
1032 instr->src[src_number].src_type = nir_tex_src_projector;
1033 src_number++;
1034 }
1035
1036 if (tgsi_inst->Instruction.Opcode == TGSI_OPCODE_TXB) {
1037 instr->src[src_number].src = nir_src_for_ssa(ttn_channel(b, src[0], W));
1038 instr->src[src_number].src_type = nir_tex_src_bias;
1039 src_number++;
1040 }
1041
1042 if (tgsi_inst->Instruction.Opcode == TGSI_OPCODE_TXL) {
1043 instr->src[src_number].src = nir_src_for_ssa(ttn_channel(b, src[0], W));
1044 instr->src[src_number].src_type = nir_tex_src_lod;
1045 src_number++;
1046 }
1047
1048 if (instr->is_shadow) {
1049 if (instr->coord_components < 3)
1050 instr->src[src_number].src = nir_src_for_ssa(ttn_channel(b, src[0], Z));
1051 else
1052 instr->src[src_number].src = nir_src_for_ssa(ttn_channel(b, src[0], W));
1053
1054 instr->src[src_number].src_type = nir_tex_src_comparitor;
1055 src_number++;
1056 }
1057
1058 assert(src_number == num_srcs);
1059
1060 nir_ssa_dest_init(&instr->instr, &instr->dest, 4, NULL);
1061 nir_instr_insert_after_cf_list(b->cf_node_list, &instr->instr);
1062
1063 /* Resolve the writemask on the texture op. */
1064 ttn_move_dest(b, dest, &instr->dest.ssa);
1065 }
1066
1067 /* TGSI_OPCODE_TXQ is actually two distinct operations:
1068 *
1069 * dst.x = texture\_width(unit, lod)
1070 * dst.y = texture\_height(unit, lod)
1071 * dst.z = texture\_depth(unit, lod)
1072 * dst.w = texture\_levels(unit)
1073 *
1074 * dst.xyz map to NIR txs opcode, and dst.w maps to query_levels
1075 */
1076 static void
1077 ttn_txq(struct ttn_compile *c, nir_alu_dest dest, nir_ssa_def **src)
1078 {
1079 nir_builder *b = &c->build;
1080 struct tgsi_full_instruction *tgsi_inst = &c->token->FullInstruction;
1081 nir_tex_instr *txs, *qlv;
1082
1083 txs = nir_tex_instr_create(b->shader, 1);
1084 txs->op = nir_texop_txs;
1085 setup_texture_info(txs, tgsi_inst->Texture.Texture);
1086
1087 qlv = nir_tex_instr_create(b->shader, 0);
1088 qlv->op = nir_texop_query_levels;
1089 setup_texture_info(qlv, tgsi_inst->Texture.Texture);
1090
1091 assert(tgsi_inst->Src[1].Register.File == TGSI_FILE_SAMPLER);
1092 txs->sampler_index = tgsi_inst->Src[1].Register.Index;
1093 qlv->sampler_index = tgsi_inst->Src[1].Register.Index;
1094
1095 /* only single src, the lod: */
1096 txs->src[0].src = nir_src_for_ssa(ttn_channel(b, src[0], X));
1097 txs->src[0].src_type = nir_tex_src_lod;
1098
1099 nir_ssa_dest_init(&txs->instr, &txs->dest, 3, NULL);
1100 nir_instr_insert_after_cf_list(b->cf_node_list, &txs->instr);
1101
1102 nir_ssa_dest_init(&qlv->instr, &qlv->dest, 1, NULL);
1103 nir_instr_insert_after_cf_list(b->cf_node_list, &qlv->instr);
1104
1105 ttn_move_dest_masked(b, dest, &txs->dest.ssa, TGSI_WRITEMASK_XYZ);
1106 ttn_move_dest_masked(b, dest, &qlv->dest.ssa, TGSI_WRITEMASK_W);
1107 }
1108
1109 static const nir_op op_trans[TGSI_OPCODE_LAST] = {
1110 [TGSI_OPCODE_ARL] = 0,
1111 [TGSI_OPCODE_MOV] = nir_op_fmov,
1112 [TGSI_OPCODE_LIT] = 0,
1113 [TGSI_OPCODE_RCP] = nir_op_frcp,
1114 [TGSI_OPCODE_RSQ] = nir_op_frsq,
1115 [TGSI_OPCODE_EXP] = 0,
1116 [TGSI_OPCODE_LOG] = 0,
1117 [TGSI_OPCODE_MUL] = nir_op_fmul,
1118 [TGSI_OPCODE_ADD] = nir_op_fadd,
1119 [TGSI_OPCODE_DP3] = 0,
1120 [TGSI_OPCODE_DP4] = 0,
1121 [TGSI_OPCODE_DST] = 0,
1122 [TGSI_OPCODE_MIN] = nir_op_fmin,
1123 [TGSI_OPCODE_MAX] = nir_op_fmax,
1124 [TGSI_OPCODE_SLT] = nir_op_slt,
1125 [TGSI_OPCODE_SGE] = nir_op_sge,
1126 [TGSI_OPCODE_MAD] = nir_op_ffma,
1127 [TGSI_OPCODE_SUB] = nir_op_fsub,
1128 [TGSI_OPCODE_LRP] = 0,
1129 [TGSI_OPCODE_SQRT] = nir_op_fsqrt,
1130 [TGSI_OPCODE_DP2A] = 0,
1131 [TGSI_OPCODE_FRC] = nir_op_ffract,
1132 [TGSI_OPCODE_CLAMP] = 0,
1133 [TGSI_OPCODE_FLR] = nir_op_ffloor,
1134 [TGSI_OPCODE_ROUND] = nir_op_fround_even,
1135 [TGSI_OPCODE_EX2] = nir_op_fexp2,
1136 [TGSI_OPCODE_LG2] = nir_op_flog2,
1137 [TGSI_OPCODE_POW] = nir_op_fpow,
1138 [TGSI_OPCODE_XPD] = 0,
1139 [TGSI_OPCODE_ABS] = nir_op_fabs,
1140 [TGSI_OPCODE_DPH] = 0,
1141 [TGSI_OPCODE_COS] = nir_op_fcos,
1142 [TGSI_OPCODE_DDX] = nir_op_fddx,
1143 [TGSI_OPCODE_DDY] = nir_op_fddy,
1144 [TGSI_OPCODE_KILL] = 0,
1145 [TGSI_OPCODE_PK2H] = 0, /* XXX */
1146 [TGSI_OPCODE_PK2US] = 0, /* XXX */
1147 [TGSI_OPCODE_PK4B] = 0, /* XXX */
1148 [TGSI_OPCODE_PK4UB] = 0, /* XXX */
1149 [TGSI_OPCODE_SEQ] = nir_op_seq,
1150 [TGSI_OPCODE_SGT] = 0,
1151 [TGSI_OPCODE_SIN] = nir_op_fsin,
1152 [TGSI_OPCODE_SLE] = 0,
1153 [TGSI_OPCODE_TEX] = 0,
1154 [TGSI_OPCODE_TXD] = 0,
1155 [TGSI_OPCODE_TXP] = 0,
1156 [TGSI_OPCODE_UP2H] = 0, /* XXX */
1157 [TGSI_OPCODE_UP2US] = 0, /* XXX */
1158 [TGSI_OPCODE_UP4B] = 0, /* XXX */
1159 [TGSI_OPCODE_UP4UB] = 0, /* XXX */
1160 [TGSI_OPCODE_ARR] = 0,
1161
1162 /* No function calls, yet. */
1163 [TGSI_OPCODE_CAL] = 0, /* XXX */
1164 [TGSI_OPCODE_RET] = 0, /* XXX */
1165
1166 [TGSI_OPCODE_SSG] = nir_op_fsign,
1167 [TGSI_OPCODE_CMP] = 0,
1168 [TGSI_OPCODE_SCS] = 0,
1169 [TGSI_OPCODE_TXB] = 0,
1170 [TGSI_OPCODE_DIV] = nir_op_fdiv,
1171 [TGSI_OPCODE_DP2] = 0,
1172 [TGSI_OPCODE_DP2A] = 0,
1173 [TGSI_OPCODE_TXL] = 0,
1174
1175 [TGSI_OPCODE_BRK] = 0,
1176 [TGSI_OPCODE_IF] = 0,
1177 [TGSI_OPCODE_UIF] = 0,
1178 [TGSI_OPCODE_ELSE] = 0,
1179 [TGSI_OPCODE_ENDIF] = 0,
1180
1181 [TGSI_OPCODE_DDX_FINE] = nir_op_fddx_fine,
1182 [TGSI_OPCODE_DDY_FINE] = nir_op_fddy_fine,
1183
1184 [TGSI_OPCODE_PUSHA] = 0, /* XXX */
1185 [TGSI_OPCODE_POPA] = 0, /* XXX */
1186
1187 [TGSI_OPCODE_CEIL] = nir_op_fceil,
1188 [TGSI_OPCODE_I2F] = nir_op_i2f,
1189 [TGSI_OPCODE_NOT] = nir_op_inot,
1190 [TGSI_OPCODE_TRUNC] = nir_op_ftrunc,
1191 [TGSI_OPCODE_SHL] = nir_op_ishl,
1192 [TGSI_OPCODE_AND] = nir_op_iand,
1193 [TGSI_OPCODE_OR] = nir_op_ior,
1194 [TGSI_OPCODE_MOD] = nir_op_umod,
1195 [TGSI_OPCODE_XOR] = nir_op_ixor,
1196 [TGSI_OPCODE_SAD] = 0, /* XXX */
1197 [TGSI_OPCODE_TXF] = 0,
1198 [TGSI_OPCODE_TXQ] = 0,
1199
1200 [TGSI_OPCODE_CONT] = 0,
1201
1202 [TGSI_OPCODE_EMIT] = 0, /* XXX */
1203 [TGSI_OPCODE_ENDPRIM] = 0, /* XXX */
1204
1205 [TGSI_OPCODE_BGNLOOP] = 0,
1206 [TGSI_OPCODE_BGNSUB] = 0, /* XXX: no function calls */
1207 [TGSI_OPCODE_ENDLOOP] = 0,
1208 [TGSI_OPCODE_ENDSUB] = 0, /* XXX: no function calls */
1209
1210 [TGSI_OPCODE_TXQ_LZ] = 0,
1211 [TGSI_OPCODE_NOP] = 0,
1212 [TGSI_OPCODE_FSEQ] = nir_op_feq,
1213 [TGSI_OPCODE_FSGE] = nir_op_fge,
1214 [TGSI_OPCODE_FSLT] = nir_op_flt,
1215 [TGSI_OPCODE_FSNE] = nir_op_fne,
1216
1217 /* No control flow yet */
1218 [TGSI_OPCODE_CALLNZ] = 0, /* XXX */
1219 [TGSI_OPCODE_BREAKC] = 0, /* not emitted by glsl_to_tgsi.cpp */
1220
1221 [TGSI_OPCODE_KILL_IF] = 0,
1222
1223 [TGSI_OPCODE_END] = 0,
1224
1225 [TGSI_OPCODE_F2I] = nir_op_f2i,
1226 [TGSI_OPCODE_IDIV] = nir_op_idiv,
1227 [TGSI_OPCODE_IMAX] = nir_op_imax,
1228 [TGSI_OPCODE_IMIN] = nir_op_imin,
1229 [TGSI_OPCODE_INEG] = nir_op_ineg,
1230 [TGSI_OPCODE_ISGE] = nir_op_ige,
1231 [TGSI_OPCODE_ISHR] = nir_op_ishr,
1232 [TGSI_OPCODE_ISLT] = nir_op_ilt,
1233 [TGSI_OPCODE_F2U] = nir_op_f2u,
1234 [TGSI_OPCODE_U2F] = nir_op_u2f,
1235 [TGSI_OPCODE_UADD] = nir_op_iadd,
1236 [TGSI_OPCODE_UDIV] = nir_op_udiv,
1237 [TGSI_OPCODE_UMAD] = 0,
1238 [TGSI_OPCODE_UMAX] = nir_op_umax,
1239 [TGSI_OPCODE_UMIN] = nir_op_umin,
1240 [TGSI_OPCODE_UMOD] = nir_op_umod,
1241 [TGSI_OPCODE_UMUL] = nir_op_imul,
1242 [TGSI_OPCODE_USEQ] = nir_op_ieq,
1243 [TGSI_OPCODE_USGE] = nir_op_uge,
1244 [TGSI_OPCODE_USHR] = nir_op_ushr,
1245 [TGSI_OPCODE_USLT] = nir_op_ult,
1246 [TGSI_OPCODE_USNE] = nir_op_ine,
1247
1248 [TGSI_OPCODE_SWITCH] = 0, /* not emitted by glsl_to_tgsi.cpp */
1249 [TGSI_OPCODE_CASE] = 0, /* not emitted by glsl_to_tgsi.cpp */
1250 [TGSI_OPCODE_DEFAULT] = 0, /* not emitted by glsl_to_tgsi.cpp */
1251 [TGSI_OPCODE_ENDSWITCH] = 0, /* not emitted by glsl_to_tgsi.cpp */
1252
1253 /* XXX: SAMPLE opcodes */
1254
1255 [TGSI_OPCODE_UARL] = nir_op_imov,
1256 [TGSI_OPCODE_UCMP] = 0,
1257 [TGSI_OPCODE_IABS] = nir_op_iabs,
1258 [TGSI_OPCODE_ISSG] = nir_op_isign,
1259
1260 /* XXX: atomics */
1261
1262 [TGSI_OPCODE_TEX2] = 0,
1263 [TGSI_OPCODE_TXB2] = 0,
1264 [TGSI_OPCODE_TXL2] = 0,
1265
1266 [TGSI_OPCODE_IMUL_HI] = nir_op_imul_high,
1267 [TGSI_OPCODE_UMUL_HI] = nir_op_umul_high,
1268
1269 [TGSI_OPCODE_TG4] = 0,
1270 [TGSI_OPCODE_LODQ] = 0, /* XXX */
1271
1272 [TGSI_OPCODE_IBFE] = nir_op_ibitfield_extract,
1273 [TGSI_OPCODE_UBFE] = nir_op_ubitfield_extract,
1274 [TGSI_OPCODE_BFI] = nir_op_bitfield_insert,
1275 [TGSI_OPCODE_BREV] = nir_op_bitfield_reverse,
1276 [TGSI_OPCODE_POPC] = nir_op_bit_count,
1277 [TGSI_OPCODE_LSB] = nir_op_find_lsb,
1278 [TGSI_OPCODE_IMSB] = nir_op_ifind_msb,
1279 [TGSI_OPCODE_UMSB] = nir_op_ifind_msb, /* XXX: signed vs unsigned */
1280
1281 [TGSI_OPCODE_INTERP_CENTROID] = 0, /* XXX */
1282 [TGSI_OPCODE_INTERP_SAMPLE] = 0, /* XXX */
1283 [TGSI_OPCODE_INTERP_OFFSET] = 0, /* XXX */
1284 };
1285
1286 static void
1287 ttn_emit_instruction(struct ttn_compile *c)
1288 {
1289 nir_builder *b = &c->build;
1290 struct tgsi_full_instruction *tgsi_inst = &c->token->FullInstruction;
1291 unsigned i;
1292 unsigned tgsi_op = tgsi_inst->Instruction.Opcode;
1293 struct tgsi_full_dst_register *tgsi_dst = &tgsi_inst->Dst[0];
1294
1295 if (tgsi_op == TGSI_OPCODE_END)
1296 return;
1297
1298 nir_ssa_def *src[TGSI_FULL_MAX_SRC_REGISTERS];
1299 for (i = 0; i < TGSI_FULL_MAX_SRC_REGISTERS; i++) {
1300 src[i] = ttn_get_src(c, &tgsi_inst->Src[i]);
1301 }
1302 nir_alu_dest dest = ttn_get_dest(c, tgsi_dst);
1303
1304 switch (tgsi_op) {
1305 case TGSI_OPCODE_RSQ:
1306 ttn_move_dest(b, dest, nir_frsq(b, ttn_channel(b, src[0], X)));
1307 break;
1308
1309 case TGSI_OPCODE_SQRT:
1310 ttn_move_dest(b, dest, nir_fsqrt(b, ttn_channel(b, src[0], X)));
1311 break;
1312
1313 case TGSI_OPCODE_RCP:
1314 ttn_move_dest(b, dest, nir_frcp(b, ttn_channel(b, src[0], X)));
1315 break;
1316
1317 case TGSI_OPCODE_EX2:
1318 ttn_move_dest(b, dest, nir_fexp2(b, ttn_channel(b, src[0], X)));
1319 break;
1320
1321 case TGSI_OPCODE_LG2:
1322 ttn_move_dest(b, dest, nir_flog2(b, ttn_channel(b, src[0], X)));
1323 break;
1324
1325 case TGSI_OPCODE_POW:
1326 ttn_move_dest(b, dest, nir_fpow(b,
1327 ttn_channel(b, src[0], X),
1328 ttn_channel(b, src[1], X)));
1329 break;
1330
1331 case TGSI_OPCODE_COS:
1332 ttn_move_dest(b, dest, nir_fcos(b, ttn_channel(b, src[0], X)));
1333 break;
1334
1335 case TGSI_OPCODE_SIN:
1336 ttn_move_dest(b, dest, nir_fsin(b, ttn_channel(b, src[0], X)));
1337 break;
1338
1339 case TGSI_OPCODE_ARL:
1340 ttn_arl(b, op_trans[tgsi_op], dest, src);
1341 break;
1342
1343 case TGSI_OPCODE_EXP:
1344 ttn_exp(b, op_trans[tgsi_op], dest, src);
1345 break;
1346
1347 case TGSI_OPCODE_LOG:
1348 ttn_log(b, op_trans[tgsi_op], dest, src);
1349 break;
1350
1351 case TGSI_OPCODE_DST:
1352 ttn_dst(b, op_trans[tgsi_op], dest, src);
1353 break;
1354
1355 case TGSI_OPCODE_LIT:
1356 ttn_lit(b, op_trans[tgsi_op], dest, src);
1357 break;
1358
1359 case TGSI_OPCODE_CLAMP:
1360 ttn_clamp(b, op_trans[tgsi_op], dest, src);
1361 break;
1362
1363 case TGSI_OPCODE_XPD:
1364 ttn_xpd(b, op_trans[tgsi_op], dest, src);
1365 break;
1366
1367 case TGSI_OPCODE_DP2:
1368 ttn_dp2(b, op_trans[tgsi_op], dest, src);
1369 break;
1370
1371 case TGSI_OPCODE_DP3:
1372 ttn_dp3(b, op_trans[tgsi_op], dest, src);
1373 break;
1374
1375 case TGSI_OPCODE_DP4:
1376 ttn_dp4(b, op_trans[tgsi_op], dest, src);
1377 break;
1378
1379 case TGSI_OPCODE_DP2A:
1380 ttn_dp2a(b, op_trans[tgsi_op], dest, src);
1381 break;
1382
1383 case TGSI_OPCODE_DPH:
1384 ttn_dph(b, op_trans[tgsi_op], dest, src);
1385 break;
1386
1387 case TGSI_OPCODE_UMAD:
1388 ttn_umad(b, op_trans[tgsi_op], dest, src);
1389 break;
1390
1391 case TGSI_OPCODE_LRP:
1392 ttn_move_dest(b, dest, nir_flrp(b, src[2], src[1], src[0]));
1393 break;
1394
1395 case TGSI_OPCODE_KILL:
1396 ttn_kill(b, op_trans[tgsi_op], dest, src);
1397 break;
1398
1399 case TGSI_OPCODE_ARR:
1400 ttn_arr(b, op_trans[tgsi_op], dest, src);
1401 break;
1402
1403 case TGSI_OPCODE_CMP:
1404 ttn_cmp(b, op_trans[tgsi_op], dest, src);
1405 break;
1406
1407 case TGSI_OPCODE_UCMP:
1408 ttn_ucmp(b, op_trans[tgsi_op], dest, src);
1409 break;
1410
1411 case TGSI_OPCODE_SCS:
1412 ttn_scs(b, op_trans[tgsi_op], dest, src);
1413 break;
1414
1415 case TGSI_OPCODE_SGT:
1416 ttn_sgt(b, op_trans[tgsi_op], dest, src);
1417 break;
1418
1419 case TGSI_OPCODE_SLE:
1420 ttn_sle(b, op_trans[tgsi_op], dest, src);
1421 break;
1422
1423 case TGSI_OPCODE_KILL_IF:
1424 ttn_kill_if(b, op_trans[tgsi_op], dest, src);
1425 break;
1426
1427 case TGSI_OPCODE_TEX:
1428 case TGSI_OPCODE_TXP:
1429 case TGSI_OPCODE_TXL:
1430 case TGSI_OPCODE_TXB:
1431 case TGSI_OPCODE_TXD:
1432 case TGSI_OPCODE_TXL2:
1433 case TGSI_OPCODE_TXB2:
1434 case TGSI_OPCODE_TXQ_LZ:
1435 case TGSI_OPCODE_TXF:
1436 case TGSI_OPCODE_TG4:
1437 ttn_tex(c, dest, src);
1438 break;
1439
1440 case TGSI_OPCODE_TXQ:
1441 ttn_txq(c, dest, src);
1442 break;
1443
1444 case TGSI_OPCODE_NOP:
1445 break;
1446
1447 case TGSI_OPCODE_IF:
1448 ttn_if(c, src[0], false);
1449 break;
1450
1451 case TGSI_OPCODE_UIF:
1452 ttn_if(c, src[0], true);
1453 break;
1454
1455 case TGSI_OPCODE_ELSE:
1456 ttn_else(c);
1457 break;
1458
1459 case TGSI_OPCODE_ENDIF:
1460 ttn_endif(c);
1461 break;
1462
1463 case TGSI_OPCODE_BGNLOOP:
1464 ttn_bgnloop(c);
1465 break;
1466
1467 case TGSI_OPCODE_BRK:
1468 ttn_brk(b);
1469 break;
1470
1471 case TGSI_OPCODE_CONT:
1472 ttn_cont(b);
1473 break;
1474
1475 case TGSI_OPCODE_ENDLOOP:
1476 ttn_endloop(c);
1477 break;
1478
1479 default:
1480 if (op_trans[tgsi_op] != 0 || tgsi_op == TGSI_OPCODE_MOV) {
1481 ttn_alu(b, op_trans[tgsi_op], dest, src);
1482 } else {
1483 fprintf(stderr, "unknown TGSI opcode: %s\n",
1484 tgsi_get_opcode_name(tgsi_op));
1485 abort();
1486 }
1487 break;
1488 }
1489
1490 if (tgsi_inst->Instruction.Saturate) {
1491 assert(tgsi_inst->Instruction.Saturate == TGSI_SAT_ZERO_ONE);
1492 assert(!dest.dest.is_ssa);
1493 ttn_move_dest(b, dest, nir_fsat(b, ttn_src_for_dest(b, &dest)));
1494 }
1495
1496 /* if the dst has a matching var, append store_global to move
1497 * output from reg to var
1498 */
1499 nir_variable *var = ttn_get_var(c, tgsi_dst);
1500 if (var) {
1501 unsigned index = tgsi_dst->Register.Index;
1502 unsigned offset = c->temp_regs[index].offset;
1503 nir_intrinsic_instr *store =
1504 nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
1505 struct tgsi_ind_register *indirect = tgsi_dst->Register.Indirect ?
1506 &tgsi_dst->Indirect : NULL;
1507
1508 store->num_components = 4;
1509 store->variables[0] = ttn_array_deref(c, store, var, offset, indirect);
1510 store->src[0] = nir_src_for_reg(dest.dest.reg.reg);
1511
1512 nir_instr_insert_after_cf_list(b->cf_node_list, &store->instr);
1513 }
1514 }
1515
1516 /**
1517 * Puts a NIR intrinsic to store of each TGSI_FILE_OUTPUT value to the output
1518 * variables at the end of the shader.
1519 *
1520 * We don't generate these incrementally as the TGSI_FILE_OUTPUT values are
1521 * written, because there's no output load intrinsic, which means we couldn't
1522 * handle writemasks.
1523 */
1524 static void
1525 ttn_add_output_stores(struct ttn_compile *c)
1526 {
1527 nir_builder *b = &c->build;
1528
1529 foreach_list_typed(nir_variable, var, node, &b->shader->outputs) {
1530 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1531 unsigned i;
1532
1533 for (i = 0; i < array_len; i++) {
1534 nir_intrinsic_instr *store =
1535 nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_output);
1536 store->num_components = 4;
1537 store->const_index[0] = var->data.driver_location + i;
1538 store->const_index[1] = 1;
1539 store->src[0].reg.reg = c->output_regs[var->data.driver_location].reg;
1540 nir_instr_insert_after_cf_list(b->cf_node_list, &store->instr);
1541 }
1542 }
1543 }
1544
1545 struct nir_shader *
1546 tgsi_to_nir(const void *tgsi_tokens,
1547 const nir_shader_compiler_options *options)
1548 {
1549 struct tgsi_parse_context parser;
1550 struct tgsi_shader_info scan;
1551 struct ttn_compile *c;
1552 struct nir_shader *s;
1553 int ret;
1554
1555 c = rzalloc(NULL, struct ttn_compile);
1556 s = nir_shader_create(NULL, options);
1557
1558 nir_function *func = nir_function_create(s, "main");
1559 nir_function_overload *overload = nir_function_overload_create(func);
1560 nir_function_impl *impl = nir_function_impl_create(overload);
1561
1562 nir_builder_init(&c->build, impl);
1563 nir_builder_insert_after_cf_list(&c->build, &impl->body);
1564
1565 tgsi_scan_shader(tgsi_tokens, &scan);
1566 c->scan = &scan;
1567
1568 s->num_inputs = scan.file_max[TGSI_FILE_INPUT] + 1;
1569 s->num_uniforms = scan.file_max[TGSI_FILE_CONSTANT] + 1;
1570 s->num_outputs = scan.file_max[TGSI_FILE_OUTPUT] + 1;
1571
1572 c->output_regs = rzalloc_array(c, struct ttn_reg_info,
1573 scan.file_max[TGSI_FILE_OUTPUT] + 1);
1574 c->temp_regs = rzalloc_array(c, struct ttn_reg_info,
1575 scan.file_max[TGSI_FILE_TEMPORARY] + 1);
1576 c->imm_defs = rzalloc_array(c, nir_ssa_def *,
1577 scan.file_max[TGSI_FILE_IMMEDIATE] + 1);
1578
1579 c->if_stack = rzalloc_array(c, struct exec_list *,
1580 (scan.opcode_count[TGSI_OPCODE_IF] +
1581 scan.opcode_count[TGSI_OPCODE_UIF]) * 2);
1582 c->loop_stack = rzalloc_array(c, struct exec_list *,
1583 scan.opcode_count[TGSI_OPCODE_BGNLOOP]);
1584
1585 ret = tgsi_parse_init(&parser, tgsi_tokens);
1586 assert(ret == TGSI_PARSE_OK);
1587
1588 while (!tgsi_parse_end_of_tokens(&parser)) {
1589 tgsi_parse_token(&parser);
1590 c->token = &parser.FullToken;
1591
1592 switch (parser.FullToken.Token.Type) {
1593 case TGSI_TOKEN_TYPE_DECLARATION:
1594 ttn_emit_declaration(c);
1595 break;
1596
1597 case TGSI_TOKEN_TYPE_INSTRUCTION:
1598 ttn_emit_instruction(c);
1599 break;
1600
1601 case TGSI_TOKEN_TYPE_IMMEDIATE:
1602 ttn_emit_immediate(c);
1603 break;
1604 }
1605 }
1606
1607 tgsi_parse_free(&parser);
1608
1609 ttn_add_output_stores(c);
1610
1611 ralloc_free(c);
1612 return s;
1613 }