| 1 |
/* |
| 2 |
* Copyright © 2018 Jonathan Marek <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
* |
| 5 |
* Authors: |
| 6 |
* Jonathan Marek <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "ir2_private.h" |
| 10 |
|
| 11 |
#include "fd2_program.h" |
| 12 |
#include "freedreno_util.h" |
| 13 |
#include "nir_legacy.h" |
| 14 |
|
| 15 |
static const nir_shader_compiler_options options = { |
| 16 |
.compact_arrays = true, |
| 17 |
.lower_fpow = true, |
| 18 |
.lower_flrp32 = true, |
| 19 |
.lower_fmod = true, |
| 20 |
.lower_fdiv = true, |
| 21 |
.lower_fceil = true, |
| 22 |
.fuse_ffma16 = true, |
| 23 |
.fuse_ffma32 = true, |
| 24 |
.fuse_ffma64 = true, |
| 25 |
/* .fdot_replicates = true, it is replicated, but it makes things worse */ |
| 26 |
.vertex_id_zero_based = true, /* its not implemented anyway */ |
| 27 |
.lower_bitops = true, |
| 28 |
.lower_fdph = true, |
| 29 |
.has_fsub = true, |
| 30 |
.has_isub = true, |
| 31 |
.no_integers = true, |
| 32 |
.lower_insert_byte = true, |
| 33 |
.lower_insert_word = true, |
| 34 |
.force_indirect_unrolling = nir_var_all, |
| 35 |
.force_indirect_unrolling_sampler = true, |
| 36 |
.max_unroll_iterations = 32, |
| 37 |
}; |
| 38 |
|
| 39 |
const nir_shader_compiler_options * |
| 40 |
ir2_get_compiler_options(void) |
| 41 |
{ |
| 42 |
return &options; |
| 43 |
} |
| 44 |
|
| 45 |
#define OPT(nir, pass, ...) \ |
| 46 |
({ \ |
| 47 |
bool this_progress = false; \ |
| 48 |
NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \ |
| 49 |
this_progress; \ |
| 50 |
}) |
| 51 |
#define OPT_V(nir, pass, ...) NIR_PASS(_, nir, pass, ##__VA_ARGS__) |
| 52 |
|
| 53 |
static void |
| 54 |
ir2_optimize_loop(nir_shader *s) |
| 55 |
{ |
| 56 |
bool progress; |
| 57 |
do { |
| 58 |
progress = false; |
| 59 |
|
| 60 |
OPT_V(s, nir_lower_vars_to_ssa); |
| 61 |
progress |= OPT(s, nir_opt_copy_prop_vars); |
| 62 |
progress |= OPT(s, nir_opt_copy_prop); |
| 63 |
progress |= OPT(s, nir_opt_dce); |
| 64 |
progress |= OPT(s, nir_opt_cse); |
| 65 |
/* progress |= OPT(s, nir_opt_gcm, true); */ |
| 66 |
nir_opt_peephole_select_options peephole_select_options = { |
| 67 |
.limit = UINT_MAX, |
| 68 |
.indirect_load_ok = true, |
| 69 |
.expensive_alu_ok = true, |
| 70 |
}; |
| 71 |
progress |= OPT(s, nir_opt_peephole_select, &peephole_select_options); |
| 72 |
progress |= OPT(s, nir_opt_intrinsics); |
| 73 |
progress |= OPT(s, nir_opt_algebraic); |
| 74 |
progress |= OPT(s, nir_opt_constant_folding); |
| 75 |
progress |= OPT(s, nir_opt_dead_cf); |
| 76 |
if (OPT(s, nir_opt_loop)) { |
| 77 |
progress |= true; |
| 78 |
/* If nir_opt_loop makes progress, then we need to clean |
| 79 |
* things up if we want any hope of nir_opt_if or nir_opt_loop_unroll |
| 80 |
* to make progress. |
| 81 |
*/ |
| 82 |
OPT(s, nir_opt_copy_prop); |
| 83 |
OPT(s, nir_opt_dce); |
| 84 |
} |
| 85 |
progress |= OPT(s, nir_opt_loop_unroll); |
| 86 |
progress |= OPT(s, nir_opt_if, nir_opt_if_optimize_phi_true_false); |
| 87 |
progress |= OPT(s, nir_opt_remove_phis); |
| 88 |
progress |= OPT(s, nir_opt_undef); |
| 89 |
|
| 90 |
} while (progress); |
| 91 |
} |
| 92 |
|
| 93 |
/* trig workarounds is the same as ir3.. but we don't want to include ir3 */ |
| 94 |
bool ir3_nir_apply_trig_workarounds(nir_shader *shader); |
| 95 |
|
| 96 |
int |
| 97 |
ir2_optimize_nir(nir_shader *s, bool lower) |
| 98 |
{ |
| 99 |
struct nir_lower_tex_options tex_options = { |
| 100 |
.lower_txp = ~0u, |
| 101 |
.lower_rect = 0, |
| 102 |
.lower_invalid_implicit_lod = true, |
| 103 |
}; |
| 104 |
|
| 105 |
if (FD_DBG(DISASM)) { |
| 106 |
debug_printf("----------------------\n"); |
| 107 |
nir_print_shader(s, stdout); |
| 108 |
debug_printf("----------------------\n"); |
| 109 |
} |
| 110 |
|
| 111 |
OPT_V(s, nir_lower_vars_to_ssa); |
| 112 |
OPT_V(s, nir_lower_indirect_derefs_to_if_else_trees, |
| 113 |
nir_var_shader_in | nir_var_shader_out, UINT32_MAX); |
| 114 |
|
| 115 |
if (lower) { |
| 116 |
OPT_V(s, ir3_nir_apply_trig_workarounds); |
| 117 |
OPT_V(s, nir_lower_tex, &tex_options); |
| 118 |
} |
| 119 |
|
| 120 |
ir2_optimize_loop(s); |
| 121 |
|
| 122 |
OPT_V(s, nir_remove_dead_variables, nir_var_function_temp, NULL); |
| 123 |
OPT_V(s, nir_opt_sink, nir_move_const_undef); |
| 124 |
|
| 125 |
/* TODO we dont want to get shaders writing to depth for depth textures */ |
| 126 |
if (s->info.stage == MESA_SHADER_FRAGMENT) { |
| 127 |
nir_foreach_shader_out_variable (var, s) { |
| 128 |
if (var->data.location == FRAG_RESULT_DEPTH) |
| 129 |
return -1; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
static struct ir2_src |
| 137 |
load_const(struct ir2_context *ctx, float *value_f, unsigned ncomp) |
| 138 |
{ |
| 139 |
struct fd2_shader_stateobj *so = ctx->so; |
| 140 |
unsigned idx, i, j; |
| 141 |
unsigned imm_ncomp = 0; |
| 142 |
unsigned swiz = 0; |
| 143 |
uint32_t *value = (uint32_t *)value_f; |
| 144 |
|
| 145 |
/* try to merge with existing immediate (TODO: try with neg) */ |
| 146 |
for (idx = 0; idx < so->num_immediates; idx++) { |
| 147 |
swiz = 0; |
| 148 |
imm_ncomp = so->immediates[idx].ncomp; |
| 149 |
for (i = 0; i < ncomp; i++) { |
| 150 |
for (j = 0; j < imm_ncomp; j++) { |
| 151 |
if (value[i] == so->immediates[idx].val[j]) |
| 152 |
break; |
| 153 |
} |
| 154 |
if (j == imm_ncomp) { |
| 155 |
if (j == 4) |
| 156 |
break; |
| 157 |
so->immediates[idx].val[imm_ncomp++] = value[i]; |
| 158 |
} |
| 159 |
swiz |= swiz_set(j, i); |
| 160 |
} |
| 161 |
/* matched all components */ |
| 162 |
if (i == ncomp) |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
/* need to allocate new immediate */ |
| 167 |
if (idx == so->num_immediates) { |
| 168 |
swiz = 0; |
| 169 |
imm_ncomp = 0; |
| 170 |
for (i = 0; i < ncomp; i++) { |
| 171 |
for (j = 0; j < imm_ncomp; j++) { |
| 172 |
if (value[i] == ctx->so->immediates[idx].val[j]) |
| 173 |
break; |
| 174 |
} |
| 175 |
if (j == imm_ncomp) { |
| 176 |
so->immediates[idx].val[imm_ncomp++] = value[i]; |
| 177 |
} |
| 178 |
swiz |= swiz_set(j, i); |
| 179 |
} |
| 180 |
so->num_immediates++; |
| 181 |
} |
| 182 |
so->immediates[idx].ncomp = imm_ncomp; |
| 183 |
|
| 184 |
if (ncomp == 1) |
| 185 |
swiz = swiz_merge(swiz, IR2_SWIZZLE_XXXX); |
| 186 |
|
| 187 |
return ir2_src(so->first_immediate + idx, swiz, IR2_SRC_CONST); |
| 188 |
} |
| 189 |
|
| 190 |
struct ir2_src |
| 191 |
ir2_zero(struct ir2_context *ctx) |
| 192 |
{ |
| 193 |
return load_const(ctx, (float[]){0.0f}, 1); |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
update_range(struct ir2_context *ctx, struct ir2_reg *reg) |
| 198 |
{ |
| 199 |
if (!reg->initialized) { |
| 200 |
reg->initialized = true; |
| 201 |
reg->loop_depth = ctx->loop_depth; |
| 202 |
} |
| 203 |
|
| 204 |
if (ctx->loop_depth > reg->loop_depth) { |
| 205 |
reg->block_idx_free = ctx->loop_last_block[reg->loop_depth + 1]; |
| 206 |
} else { |
| 207 |
reg->loop_depth = ctx->loop_depth; |
| 208 |
reg->block_idx_free = -1; |
| 209 |
} |
| 210 |
|
| 211 |
/* for regs we want to free at the end of the loop in any case |
| 212 |
* XXX dont do this for ssa |
| 213 |
*/ |
| 214 |
if (reg->loop_depth) |
| 215 |
reg->block_idx_free = ctx->loop_last_block[reg->loop_depth]; |
| 216 |
} |
| 217 |
|
| 218 |
static struct ir2_src |
| 219 |
make_legacy_src(struct ir2_context *ctx, nir_legacy_src src) |
| 220 |
{ |
| 221 |
struct ir2_src res = {}; |
| 222 |
struct ir2_reg *reg; |
| 223 |
|
| 224 |
/* Handle constants specially */ |
| 225 |
if (src.is_ssa) { |
| 226 |
nir_const_value *const_value = |
| 227 |
nir_src_as_const_value(nir_src_for_ssa(src.ssa)); |
| 228 |
|
| 229 |
if (const_value) { |
| 230 |
float c[src.ssa->num_components]; |
| 231 |
nir_const_value_to_array(c, const_value, src.ssa->num_components, f32); |
| 232 |
return load_const(ctx, c, src.ssa->num_components); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/* Otherwise translate the SSA def or register */ |
| 237 |
if (!src.is_ssa) { |
| 238 |
res.num = src.reg.handle->index; |
| 239 |
res.type = IR2_SRC_REG; |
| 240 |
reg = &ctx->reg[res.num]; |
| 241 |
} else { |
| 242 |
assert(ctx->ssa_map[src.ssa->index] >= 0); |
| 243 |
res.num = ctx->ssa_map[src.ssa->index]; |
| 244 |
res.type = IR2_SRC_SSA; |
| 245 |
reg = &ctx->instr[res.num].ssa; |
| 246 |
} |
| 247 |
|
| 248 |
update_range(ctx, reg); |
| 249 |
return res; |
| 250 |
} |
| 251 |
|
| 252 |
static struct ir2_src |
| 253 |
make_src(struct ir2_context *ctx, nir_src src) |
| 254 |
{ |
| 255 |
return make_legacy_src(ctx, nir_legacy_chase_src(&src)); |
| 256 |
} |
| 257 |
|
| 258 |
static void |
| 259 |
set_legacy_index(struct ir2_context *ctx, nir_legacy_dest dst, |
| 260 |
struct ir2_instr *instr) |
| 261 |
{ |
| 262 |
struct ir2_reg *reg = &instr->ssa; |
| 263 |
|
| 264 |
if (dst.is_ssa) { |
| 265 |
ctx->ssa_map[dst.ssa->index] = instr->idx; |
| 266 |
} else { |
| 267 |
reg = &ctx->reg[dst.reg.handle->index]; |
| 268 |
|
| 269 |
instr->is_ssa = false; |
| 270 |
instr->reg = reg; |
| 271 |
} |
| 272 |
update_range(ctx, reg); |
| 273 |
} |
| 274 |
|
| 275 |
static void |
| 276 |
set_index(struct ir2_context *ctx, nir_def *def, struct ir2_instr *instr) |
| 277 |
{ |
| 278 |
set_legacy_index(ctx, nir_legacy_chase_dest(def), instr); |
| 279 |
} |
| 280 |
|
| 281 |
static struct ir2_instr * |
| 282 |
ir2_instr_create(struct ir2_context *ctx, int type) |
| 283 |
{ |
| 284 |
struct ir2_instr *instr; |
| 285 |
|
| 286 |
instr = &ctx->instr[ctx->instr_count++]; |
| 287 |
instr->idx = ctx->instr_count - 1; |
| 288 |
instr->type = type; |
| 289 |
instr->block_idx = ctx->block_idx; |
| 290 |
instr->pred = ctx->pred; |
| 291 |
instr->is_ssa = true; |
| 292 |
return instr; |
| 293 |
} |
| 294 |
|
| 295 |
static struct ir2_instr * |
| 296 |
instr_create_alu(struct ir2_context *ctx, nir_op opcode, unsigned ncomp) |
| 297 |
{ |
| 298 |
/* emit_alu will fixup instrs that don't map directly */ |
| 299 |
static const struct ir2_opc { |
| 300 |
int8_t scalar, vector; |
| 301 |
} nir_ir2_opc[nir_num_opcodes + 1] = { |
| 302 |
[0 ... nir_num_opcodes - 1] = {-1, -1}, |
| 303 |
|
| 304 |
[nir_op_mov] = {MAXs, MAXv}, |
| 305 |
[nir_op_fneg] = {MAXs, MAXv}, |
| 306 |
[nir_op_fabs] = {MAXs, MAXv}, |
| 307 |
[nir_op_fsat] = {MAXs, MAXv}, |
| 308 |
[nir_op_fsign] = {-1, CNDGTEv}, |
| 309 |
[nir_op_fadd] = {ADDs, ADDv}, |
| 310 |
[nir_op_fsub] = {ADDs, ADDv}, |
| 311 |
[nir_op_fmul] = {MULs, MULv}, |
| 312 |
[nir_op_ffma] = {-1, MULADDv}, |
| 313 |
[nir_op_fmax] = {MAXs, MAXv}, |
| 314 |
[nir_op_fmin] = {MINs, MINv}, |
| 315 |
[nir_op_ffloor] = {FLOORs, FLOORv}, |
| 316 |
[nir_op_ffract] = {FRACs, FRACv}, |
| 317 |
[nir_op_ftrunc] = {TRUNCs, TRUNCv}, |
| 318 |
[nir_op_fdot2] = {-1, DOT2ADDv}, |
| 319 |
[nir_op_fdot3] = {-1, DOT3v}, |
| 320 |
[nir_op_fdot4] = {-1, DOT4v}, |
| 321 |
[nir_op_sge] = {-1, SETGTEv}, |
| 322 |
[nir_op_slt] = {-1, SETGTv}, |
| 323 |
[nir_op_sne] = {-1, SETNEv}, |
| 324 |
[nir_op_seq] = {-1, SETEv}, |
| 325 |
[nir_op_fcsel] = {-1, CNDEv}, |
| 326 |
[nir_op_frsq] = {RECIPSQ_IEEE, -1}, |
| 327 |
[nir_op_frcp] = {RECIP_IEEE, -1}, |
| 328 |
[nir_op_flog2] = {LOG_IEEE, -1}, |
| 329 |
[nir_op_fexp2] = {EXP_IEEE, -1}, |
| 330 |
[nir_op_fsqrt] = {SQRT_IEEE, -1}, |
| 331 |
[nir_op_fcos] = {COS, -1}, |
| 332 |
[nir_op_fsin] = {SIN, -1}, |
| 333 |
/* no fsat, fneg, fabs since source mods deal with those */ |
| 334 |
|
| 335 |
/* so we can use this function with non-nir op */ |
| 336 |
#define ir2_op_cube nir_num_opcodes |
| 337 |
[ir2_op_cube] = {-1, CUBEv}, |
| 338 |
}; |
| 339 |
|
| 340 |
struct ir2_opc op = nir_ir2_opc[opcode]; |
| 341 |
assert(op.vector >= 0 || op.scalar >= 0); |
| 342 |
|
| 343 |
struct ir2_instr *instr = ir2_instr_create(ctx, IR2_ALU); |
| 344 |
instr->alu.vector_opc = op.vector; |
| 345 |
instr->alu.scalar_opc = op.scalar; |
| 346 |
instr->alu.export = -1; |
| 347 |
instr->alu.write_mask = (1 << ncomp) - 1; |
| 348 |
instr->src_count = |
| 349 |
opcode == ir2_op_cube ? 2 : nir_op_infos[opcode].num_inputs; |
| 350 |
instr->ssa.ncomp = ncomp; |
| 351 |
return instr; |
| 352 |
} |
| 353 |
|
| 354 |
static struct ir2_instr * |
| 355 |
instr_create_alu_reg(struct ir2_context *ctx, nir_op opcode, uint8_t write_mask, |
| 356 |
struct ir2_instr *share_reg) |
| 357 |
{ |
| 358 |
struct ir2_instr *instr; |
| 359 |
struct ir2_reg *reg; |
| 360 |
|
| 361 |
reg = share_reg ? share_reg->reg : &ctx->reg[ctx->reg_count++]; |
| 362 |
reg->ncomp = MAX2(reg->ncomp, util_logbase2(write_mask) + 1); |
| 363 |
|
| 364 |
instr = instr_create_alu(ctx, opcode, util_bitcount(write_mask)); |
| 365 |
instr->alu.write_mask = write_mask; |
| 366 |
instr->reg = reg; |
| 367 |
instr->is_ssa = false; |
| 368 |
return instr; |
| 369 |
} |
| 370 |
|
| 371 |
static struct ir2_instr * |
| 372 |
instr_create_alu_dest(struct ir2_context *ctx, nir_op opcode, nir_def *def) |
| 373 |
{ |
| 374 |
struct ir2_instr *instr; |
| 375 |
instr = instr_create_alu(ctx, opcode, def->num_components); |
| 376 |
set_index(ctx, def, instr); |
| 377 |
return instr; |
| 378 |
} |
| 379 |
|
| 380 |
static struct ir2_instr * |
| 381 |
ir2_instr_create_fetch(struct ir2_context *ctx, nir_def *def, |
| 382 |
instr_fetch_opc_t opc) |
| 383 |
{ |
| 384 |
struct ir2_instr *instr = ir2_instr_create(ctx, IR2_FETCH); |
| 385 |
instr->fetch.opc = opc; |
| 386 |
instr->src_count = 1; |
| 387 |
instr->ssa.ncomp = def->num_components; |
| 388 |
set_index(ctx, def, instr); |
| 389 |
return instr; |
| 390 |
} |
| 391 |
|
| 392 |
static struct ir2_src |
| 393 |
make_src_noconst(struct ir2_context *ctx, nir_src src) |
| 394 |
{ |
| 395 |
struct ir2_instr *instr; |
| 396 |
|
| 397 |
if (nir_src_as_const_value(src)) { |
| 398 |
instr = instr_create_alu(ctx, nir_op_mov, src.ssa->num_components); |
| 399 |
instr->src[0] = make_src(ctx, src); |
| 400 |
return ir2_src(instr->idx, 0, IR2_SRC_SSA); |
| 401 |
} |
| 402 |
|
| 403 |
return make_src(ctx, src); |
| 404 |
} |
| 405 |
|
| 406 |
static void |
| 407 |
emit_alu(struct ir2_context *ctx, nir_alu_instr *alu) |
| 408 |
{ |
| 409 |
const nir_op_info *info = &nir_op_infos[alu->op]; |
| 410 |
nir_def *def = &alu->def; |
| 411 |
struct ir2_instr *instr; |
| 412 |
struct ir2_src tmp; |
| 413 |
unsigned ncomp; |
| 414 |
|
| 415 |
/* Don't emit modifiers that are totally folded */ |
| 416 |
if (((alu->op == nir_op_fneg) || (alu->op == nir_op_fabs)) && |
| 417 |
nir_legacy_float_mod_folds(alu)) |
| 418 |
return; |
| 419 |
|
| 420 |
if ((alu->op == nir_op_fsat) && nir_legacy_fsat_folds(alu)) |
| 421 |
return; |
| 422 |
|
| 423 |
/* get the number of dst components */ |
| 424 |
ncomp = def->num_components; |
| 425 |
|
| 426 |
instr = instr_create_alu(ctx, alu->op, ncomp); |
| 427 |
|
| 428 |
nir_legacy_alu_dest legacy_dest = |
| 429 |
nir_legacy_chase_alu_dest(&alu->def); |
| 430 |
set_legacy_index(ctx, legacy_dest.dest, instr); |
| 431 |
instr->alu.saturate = legacy_dest.fsat; |
| 432 |
instr->alu.write_mask = legacy_dest.write_mask; |
| 433 |
|
| 434 |
for (int i = 0; i < info->num_inputs; i++) { |
| 435 |
nir_alu_src *src = &alu->src[i]; |
| 436 |
|
| 437 |
/* compress swizzle with writemask when applicable */ |
| 438 |
unsigned swiz = 0, j = 0; |
| 439 |
for (int i = 0; i < 4; i++) { |
| 440 |
if (!(legacy_dest.write_mask & 1 << i) && !info->output_size) |
| 441 |
continue; |
| 442 |
swiz |= swiz_set(src->swizzle[i], j++); |
| 443 |
} |
| 444 |
|
| 445 |
nir_legacy_alu_src legacy_src = |
| 446 |
nir_legacy_chase_alu_src(src, true /* fuse_abs */); |
| 447 |
|
| 448 |
instr->src[i] = make_legacy_src(ctx, legacy_src.src); |
| 449 |
instr->src[i].swizzle = swiz_merge(instr->src[i].swizzle, swiz); |
| 450 |
instr->src[i].negate = legacy_src.fneg; |
| 451 |
instr->src[i].abs = legacy_src.fabs; |
| 452 |
} |
| 453 |
|
| 454 |
/* workarounds for NIR ops that don't map directly to a2xx ops */ |
| 455 |
switch (alu->op) { |
| 456 |
case nir_op_fneg: |
| 457 |
instr->src[0].negate = 1; |
| 458 |
break; |
| 459 |
case nir_op_fabs: |
| 460 |
instr->src[0].abs = 1; |
| 461 |
break; |
| 462 |
case nir_op_fsat: |
| 463 |
instr->alu.saturate = 1; |
| 464 |
break; |
| 465 |
case nir_op_slt: |
| 466 |
tmp = instr->src[0]; |
| 467 |
instr->src[0] = instr->src[1]; |
| 468 |
instr->src[1] = tmp; |
| 469 |
break; |
| 470 |
case nir_op_fcsel: |
| 471 |
tmp = instr->src[1]; |
| 472 |
instr->src[1] = instr->src[2]; |
| 473 |
instr->src[2] = tmp; |
| 474 |
break; |
| 475 |
case nir_op_fsub: |
| 476 |
instr->src[1].negate = !instr->src[1].negate; |
| 477 |
break; |
| 478 |
case nir_op_fdot2: |
| 479 |
instr->src_count = 3; |
| 480 |
instr->src[2] = ir2_zero(ctx); |
| 481 |
break; |
| 482 |
case nir_op_fsign: { |
| 483 |
/* we need an extra instruction to deal with the zero case */ |
| 484 |
struct ir2_instr *tmp; |
| 485 |
|
| 486 |
/* tmp = x == 0 ? 0 : 1 */ |
| 487 |
tmp = instr_create_alu(ctx, nir_op_fcsel, ncomp); |
| 488 |
tmp->src[0] = instr->src[0]; |
| 489 |
tmp->src[1] = ir2_zero(ctx); |
| 490 |
tmp->src[2] = load_const(ctx, (float[]){1.0f}, 1); |
| 491 |
|
| 492 |
/* result = x >= 0 ? tmp : -tmp */ |
| 493 |
instr->src[1] = ir2_src(tmp->idx, 0, IR2_SRC_SSA); |
| 494 |
instr->src[2] = instr->src[1]; |
| 495 |
instr->src[2].negate = true; |
| 496 |
instr->src_count = 3; |
| 497 |
} break; |
| 498 |
default: |
| 499 |
break; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
static void |
| 504 |
load_input(struct ir2_context *ctx, nir_def *def, unsigned idx) |
| 505 |
{ |
| 506 |
struct ir2_instr *instr; |
| 507 |
int slot = -1; |
| 508 |
|
| 509 |
if (ctx->so->type == MESA_SHADER_VERTEX) { |
| 510 |
instr = ir2_instr_create_fetch(ctx, def, 0); |
| 511 |
instr->src[0] = ir2_src(0, 0, IR2_SRC_INPUT); |
| 512 |
instr->fetch.vtx.const_idx = 20 + (idx / 3); |
| 513 |
instr->fetch.vtx.const_idx_sel = idx % 3; |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
/* get slot from idx */ |
| 518 |
nir_foreach_shader_in_variable (var, ctx->nir) { |
| 519 |
if (var->data.driver_location == idx) { |
| 520 |
slot = var->data.location; |
| 521 |
break; |
| 522 |
} |
| 523 |
} |
| 524 |
assert(slot >= 0); |
| 525 |
|
| 526 |
switch (slot) { |
| 527 |
case VARYING_SLOT_POS: |
| 528 |
/* need to extract xy with abs and add tile offset on a20x |
| 529 |
* zw from fragcoord input (w inverted in fragment shader) |
| 530 |
* TODO: only components that are required by fragment shader |
| 531 |
*/ |
| 532 |
instr = instr_create_alu_reg( |
| 533 |
ctx, ctx->so->is_a20x ? nir_op_fadd : nir_op_mov, 3, NULL); |
| 534 |
instr->src[0] = ir2_src(ctx->f->inputs_count, 0, IR2_SRC_INPUT); |
| 535 |
instr->src[0].abs = true; |
| 536 |
/* on a20x, C64 contains the tile offset */ |
| 537 |
instr->src[1] = ir2_src(64, 0, IR2_SRC_CONST); |
| 538 |
|
| 539 |
instr = instr_create_alu_reg(ctx, nir_op_mov, 4, instr); |
| 540 |
instr->src[0] = ir2_src(ctx->f->fragcoord, 0, IR2_SRC_INPUT); |
| 541 |
|
| 542 |
instr = instr_create_alu_reg(ctx, nir_op_frcp, 8, instr); |
| 543 |
instr->src[0] = ir2_src(ctx->f->fragcoord, IR2_SWIZZLE_Y, IR2_SRC_INPUT); |
| 544 |
|
| 545 |
unsigned reg_idx = instr->reg - ctx->reg; /* XXX */ |
| 546 |
instr = instr_create_alu_dest(ctx, nir_op_mov, def); |
| 547 |
instr->src[0] = ir2_src(reg_idx, 0, IR2_SRC_REG); |
| 548 |
break; |
| 549 |
default: |
| 550 |
instr = instr_create_alu_dest(ctx, nir_op_mov, def); |
| 551 |
instr->src[0] = ir2_src(idx, 0, IR2_SRC_INPUT); |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
static unsigned |
| 557 |
output_slot(struct ir2_context *ctx, nir_intrinsic_instr *intr) |
| 558 |
{ |
| 559 |
int slot = -1; |
| 560 |
unsigned idx = nir_intrinsic_base(intr); |
| 561 |
nir_foreach_shader_out_variable (var, ctx->nir) { |
| 562 |
if (var->data.driver_location == idx) { |
| 563 |
slot = var->data.location; |
| 564 |
break; |
| 565 |
} |
| 566 |
} |
| 567 |
assert(slot != -1); |
| 568 |
return slot; |
| 569 |
} |
| 570 |
|
| 571 |
static void |
| 572 |
store_output(struct ir2_context *ctx, nir_src src, unsigned slot, |
| 573 |
unsigned ncomp) |
| 574 |
{ |
| 575 |
struct ir2_instr *instr; |
| 576 |
unsigned idx = 0; |
| 577 |
|
| 578 |
if (ctx->so->type == MESA_SHADER_VERTEX) { |
| 579 |
switch (slot) { |
| 580 |
case VARYING_SLOT_POS: |
| 581 |
ctx->position = make_src(ctx, src); |
| 582 |
idx = 62; |
| 583 |
break; |
| 584 |
case VARYING_SLOT_PSIZ: |
| 585 |
ctx->so->writes_psize = true; |
| 586 |
idx = 63; |
| 587 |
break; |
| 588 |
default: |
| 589 |
/* find matching slot from fragment shader input */ |
| 590 |
for (idx = 0; idx < ctx->f->inputs_count; idx++) |
| 591 |
if (ctx->f->inputs[idx].slot == slot) |
| 592 |
break; |
| 593 |
if (idx == ctx->f->inputs_count) |
| 594 |
return; |
| 595 |
} |
| 596 |
} else if (slot != FRAG_RESULT_COLOR && slot != FRAG_RESULT_DATA0) { |
| 597 |
/* only color output is implemented */ |
| 598 |
return; |
| 599 |
} |
| 600 |
|
| 601 |
instr = instr_create_alu(ctx, nir_op_mov, ncomp); |
| 602 |
instr->src[0] = make_src(ctx, src); |
| 603 |
instr->alu.export = idx; |
| 604 |
} |
| 605 |
|
| 606 |
static void |
| 607 |
emit_intrinsic(struct ir2_context *ctx, nir_intrinsic_instr *intr) |
| 608 |
{ |
| 609 |
struct ir2_instr *instr; |
| 610 |
ASSERTED nir_const_value *const_offset; |
| 611 |
unsigned idx; |
| 612 |
|
| 613 |
switch (intr->intrinsic) { |
| 614 |
case nir_intrinsic_decl_reg: |
| 615 |
case nir_intrinsic_load_reg: |
| 616 |
case nir_intrinsic_store_reg: |
| 617 |
/* Nothing to do for these */ |
| 618 |
break; |
| 619 |
|
| 620 |
case nir_intrinsic_load_input: |
| 621 |
load_input(ctx, &intr->def, nir_intrinsic_base(intr)); |
| 622 |
break; |
| 623 |
case nir_intrinsic_store_output: |
| 624 |
store_output(ctx, intr->src[0], output_slot(ctx, intr), |
| 625 |
intr->num_components); |
| 626 |
break; |
| 627 |
case nir_intrinsic_load_uniform: |
| 628 |
const_offset = nir_src_as_const_value(intr->src[0]); |
| 629 |
assert(const_offset); /* TODO can be false in ES2? */ |
| 630 |
idx = nir_intrinsic_base(intr); |
| 631 |
idx += (uint32_t)const_offset[0].f32; |
| 632 |
instr = instr_create_alu_dest(ctx, nir_op_mov, &intr->def); |
| 633 |
instr->src[0] = ir2_src(idx, 0, IR2_SRC_CONST); |
| 634 |
break; |
| 635 |
case nir_intrinsic_terminate: |
| 636 |
case nir_intrinsic_terminate_if: |
| 637 |
instr = ir2_instr_create(ctx, IR2_ALU); |
| 638 |
instr->alu.vector_opc = VECTOR_NONE; |
| 639 |
if (intr->intrinsic == nir_intrinsic_terminate_if) { |
| 640 |
instr->alu.scalar_opc = KILLNEs; |
| 641 |
instr->src[0] = make_src(ctx, intr->src[0]); |
| 642 |
} else { |
| 643 |
instr->alu.scalar_opc = KILLEs; |
| 644 |
instr->src[0] = ir2_zero(ctx); |
| 645 |
} |
| 646 |
instr->alu.export = -1; |
| 647 |
instr->src_count = 1; |
| 648 |
ctx->so->has_kill = true; |
| 649 |
break; |
| 650 |
case nir_intrinsic_load_front_face: |
| 651 |
/* gl_FrontFacing is in the sign of param.x |
| 652 |
* rcp required because otherwise we can't differentiate -0.0 and +0.0 |
| 653 |
*/ |
| 654 |
ctx->so->need_param = true; |
| 655 |
|
| 656 |
struct ir2_instr *tmp = instr_create_alu(ctx, nir_op_frcp, 1); |
| 657 |
tmp->src[0] = ir2_src(ctx->f->inputs_count, 0, IR2_SRC_INPUT); |
| 658 |
|
| 659 |
instr = instr_create_alu_dest(ctx, nir_op_sge, &intr->def); |
| 660 |
instr->src[0] = ir2_src(tmp->idx, 0, IR2_SRC_SSA); |
| 661 |
instr->src[1] = ir2_zero(ctx); |
| 662 |
break; |
| 663 |
case nir_intrinsic_load_point_coord: |
| 664 |
/* param.zw (note: abs might be needed like fragcoord in param.xy?) */ |
| 665 |
ctx->so->need_param = true; |
| 666 |
|
| 667 |
instr = instr_create_alu_dest(ctx, nir_op_mov, &intr->def); |
| 668 |
instr->src[0] = |
| 669 |
ir2_src(ctx->f->inputs_count, IR2_SWIZZLE_ZW, IR2_SRC_INPUT); |
| 670 |
break; |
| 671 |
default: |
| 672 |
compile_error(ctx, "unimplemented intr %s\n", nir_intrinsic_infos[intr->intrinsic].name); |
| 673 |
break; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
static void |
| 678 |
emit_tex(struct ir2_context *ctx, nir_tex_instr *tex) |
| 679 |
{ |
| 680 |
bool is_rect = false, is_cube = false; |
| 681 |
struct ir2_instr *instr; |
| 682 |
nir_src *coord, *lod_bias; |
| 683 |
|
| 684 |
coord = lod_bias = NULL; |
| 685 |
|
| 686 |
for (unsigned i = 0; i < tex->num_srcs; i++) { |
| 687 |
switch (tex->src[i].src_type) { |
| 688 |
case nir_tex_src_coord: |
| 689 |
coord = &tex->src[i].src; |
| 690 |
break; |
| 691 |
case nir_tex_src_bias: |
| 692 |
case nir_tex_src_lod: |
| 693 |
assert(!lod_bias); |
| 694 |
lod_bias = &tex->src[i].src; |
| 695 |
break; |
| 696 |
default: |
| 697 |
compile_error(ctx, "Unhandled NIR tex src type: %d\n", |
| 698 |
tex->src[i].src_type); |
| 699 |
return; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
switch (tex->op) { |
| 704 |
case nir_texop_tex: |
| 705 |
case nir_texop_txb: |
| 706 |
case nir_texop_txl: |
| 707 |
break; |
| 708 |
default: |
| 709 |
compile_error(ctx, "unimplemented texop %d\n", tex->op); |
| 710 |
return; |
| 711 |
} |
| 712 |
|
| 713 |
switch (tex->sampler_dim) { |
| 714 |
case GLSL_SAMPLER_DIM_2D: |
| 715 |
case GLSL_SAMPLER_DIM_EXTERNAL: |
| 716 |
break; |
| 717 |
case GLSL_SAMPLER_DIM_RECT: |
| 718 |
is_rect = true; |
| 719 |
break; |
| 720 |
case GLSL_SAMPLER_DIM_CUBE: |
| 721 |
is_cube = true; |
| 722 |
break; |
| 723 |
default: |
| 724 |
compile_error(ctx, "unimplemented sampler %d\n", tex->sampler_dim); |
| 725 |
return; |
| 726 |
} |
| 727 |
|
| 728 |
struct ir2_src src_coord = make_src_noconst(ctx, *coord); |
| 729 |
|
| 730 |
/* for cube maps |
| 731 |
* tmp = cube(coord) |
| 732 |
* tmp.xy = tmp.xy / |tmp.z| + 1.5 |
| 733 |
* coord = tmp.xyw |
| 734 |
*/ |
| 735 |
if (is_cube) { |
| 736 |
struct ir2_instr *rcp, *coord_xy; |
| 737 |
unsigned reg_idx; |
| 738 |
|
| 739 |
instr = instr_create_alu_reg(ctx, ir2_op_cube, 15, NULL); |
| 740 |
instr->src[0] = src_coord; |
| 741 |
instr->src[0].swizzle = IR2_SWIZZLE_ZZXY; |
| 742 |
instr->src[1] = src_coord; |
| 743 |
instr->src[1].swizzle = IR2_SWIZZLE_YXZZ; |
| 744 |
|
| 745 |
reg_idx = instr->reg - ctx->reg; /* hacky */ |
| 746 |
|
| 747 |
rcp = instr_create_alu(ctx, nir_op_frcp, 1); |
| 748 |
rcp->src[0] = ir2_src(reg_idx, IR2_SWIZZLE_Z, IR2_SRC_REG); |
| 749 |
rcp->src[0].abs = true; |
| 750 |
|
| 751 |
coord_xy = instr_create_alu_reg(ctx, nir_op_ffma, 3, instr); |
| 752 |
coord_xy->src[0] = ir2_src(reg_idx, 0, IR2_SRC_REG); |
| 753 |
coord_xy->src[1] = ir2_src(rcp->idx, IR2_SWIZZLE_XXXX, IR2_SRC_SSA); |
| 754 |
coord_xy->src[2] = load_const(ctx, (float[]){1.5f}, 1); |
| 755 |
|
| 756 |
src_coord = ir2_src(reg_idx, 0, IR2_SRC_REG); |
| 757 |
/* TODO: lod/bias transformed by src_coord.z ? */ |
| 758 |
} |
| 759 |
|
| 760 |
instr = ir2_instr_create_fetch(ctx, &tex->def, TEX_FETCH); |
| 761 |
instr->src[0] = src_coord; |
| 762 |
instr->src[0].swizzle = is_cube ? IR2_SWIZZLE_YXW : 0; |
| 763 |
instr->fetch.tex.is_cube = is_cube; |
| 764 |
instr->fetch.tex.is_rect = is_rect; |
| 765 |
instr->fetch.tex.samp_id = tex->sampler_index; |
| 766 |
|
| 767 |
/* for lod/bias, we insert an extra src for the backend to deal with */ |
| 768 |
if (lod_bias) { |
| 769 |
instr->src[1] = make_src_noconst(ctx, *lod_bias); |
| 770 |
/* backend will use 2-3 components so apply swizzle */ |
| 771 |
swiz_merge_p(&instr->src[1].swizzle, IR2_SWIZZLE_XXXX); |
| 772 |
instr->src_count = 2; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
static void |
| 777 |
setup_input(struct ir2_context *ctx, nir_variable *in) |
| 778 |
{ |
| 779 |
struct fd2_shader_stateobj *so = ctx->so; |
| 780 |
unsigned n = in->data.driver_location; |
| 781 |
unsigned slot = in->data.location; |
| 782 |
|
| 783 |
assert(glsl_type_is_vector_or_scalar(in->type) || |
| 784 |
glsl_type_is_unsized_array(in->type)); |
| 785 |
|
| 786 |
/* handle later */ |
| 787 |
if (ctx->so->type == MESA_SHADER_VERTEX) |
| 788 |
return; |
| 789 |
|
| 790 |
if (ctx->so->type != MESA_SHADER_FRAGMENT) |
| 791 |
compile_error(ctx, "unknown shader type: %d\n", ctx->so->type); |
| 792 |
|
| 793 |
n = ctx->f->inputs_count++; |
| 794 |
|
| 795 |
/* half of fragcoord from param reg, half from a varying */ |
| 796 |
if (slot == VARYING_SLOT_POS) { |
| 797 |
ctx->f->fragcoord = n; |
| 798 |
so->need_param = true; |
| 799 |
} |
| 800 |
|
| 801 |
ctx->f->inputs[n].slot = slot; |
| 802 |
ctx->f->inputs[n].ncomp = glsl_get_components(in->type); |
| 803 |
|
| 804 |
/* in->data.interpolation? |
| 805 |
* opengl ES 2.0 can't do flat mode, but we still get it from GALLIUM_HUD |
| 806 |
*/ |
| 807 |
} |
| 808 |
|
| 809 |
static void |
| 810 |
emit_undef(struct ir2_context *ctx, nir_undef_instr *undef) |
| 811 |
{ |
| 812 |
/* TODO we don't want to emit anything for undefs */ |
| 813 |
|
| 814 |
struct ir2_instr *instr; |
| 815 |
|
| 816 |
instr = instr_create_alu_dest(ctx, nir_op_mov, &undef->def); |
| 817 |
instr->src[0] = ir2_src(0, 0, IR2_SRC_CONST); |
| 818 |
} |
| 819 |
|
| 820 |
static void |
| 821 |
emit_instr(struct ir2_context *ctx, nir_instr *instr) |
| 822 |
{ |
| 823 |
switch (instr->type) { |
| 824 |
case nir_instr_type_alu: |
| 825 |
emit_alu(ctx, nir_instr_as_alu(instr)); |
| 826 |
break; |
| 827 |
case nir_instr_type_deref: |
| 828 |
/* ignored, handled as part of the intrinsic they are src to */ |
| 829 |
break; |
| 830 |
case nir_instr_type_intrinsic: |
| 831 |
emit_intrinsic(ctx, nir_instr_as_intrinsic(instr)); |
| 832 |
break; |
| 833 |
case nir_instr_type_load_const: |
| 834 |
/* dealt with when using nir_src */ |
| 835 |
break; |
| 836 |
case nir_instr_type_tex: |
| 837 |
emit_tex(ctx, nir_instr_as_tex(instr)); |
| 838 |
break; |
| 839 |
case nir_instr_type_jump: |
| 840 |
ctx->block_has_jump[ctx->block_idx] = true; |
| 841 |
break; |
| 842 |
case nir_instr_type_undef: |
| 843 |
emit_undef(ctx, nir_instr_as_undef(instr)); |
| 844 |
break; |
| 845 |
default: |
| 846 |
break; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
/* fragcoord.zw and a20x hw binning outputs */ |
| 851 |
static void |
| 852 |
extra_position_exports(struct ir2_context *ctx, bool binning) |
| 853 |
{ |
| 854 |
struct ir2_instr *instr, *rcp, *sc, *wincoord, *off; |
| 855 |
|
| 856 |
if (ctx->f->fragcoord < 0 && !binning) |
| 857 |
return; |
| 858 |
|
| 859 |
instr = instr_create_alu(ctx, nir_op_fmax, 1); |
| 860 |
instr->src[0] = ctx->position; |
| 861 |
instr->src[0].swizzle = IR2_SWIZZLE_W; |
| 862 |
instr->src[1] = ir2_zero(ctx); |
| 863 |
|
| 864 |
rcp = instr_create_alu(ctx, nir_op_frcp, 1); |
| 865 |
rcp->src[0] = ir2_src(instr->idx, 0, IR2_SRC_SSA); |
| 866 |
|
| 867 |
sc = instr_create_alu(ctx, nir_op_fmul, 4); |
| 868 |
sc->src[0] = ctx->position; |
| 869 |
sc->src[1] = ir2_src(rcp->idx, IR2_SWIZZLE_XXXX, IR2_SRC_SSA); |
| 870 |
|
| 871 |
wincoord = instr_create_alu(ctx, nir_op_ffma, 4); |
| 872 |
wincoord->src[0] = ir2_src(66, 0, IR2_SRC_CONST); |
| 873 |
wincoord->src[1] = ir2_src(sc->idx, 0, IR2_SRC_SSA); |
| 874 |
wincoord->src[2] = ir2_src(65, 0, IR2_SRC_CONST); |
| 875 |
|
| 876 |
/* fragcoord z/w */ |
| 877 |
if (ctx->f->fragcoord >= 0 && !binning) { |
| 878 |
instr = instr_create_alu(ctx, nir_op_mov, 1); |
| 879 |
instr->src[0] = ir2_src(wincoord->idx, IR2_SWIZZLE_Z, IR2_SRC_SSA); |
| 880 |
instr->alu.export = ctx->f->fragcoord; |
| 881 |
|
| 882 |
instr = instr_create_alu(ctx, nir_op_mov, 1); |
| 883 |
instr->src[0] = ctx->position; |
| 884 |
instr->src[0].swizzle = IR2_SWIZZLE_W; |
| 885 |
instr->alu.export = ctx->f->fragcoord; |
| 886 |
instr->alu.write_mask = 2; |
| 887 |
} |
| 888 |
|
| 889 |
if (!binning) |
| 890 |
return; |
| 891 |
|
| 892 |
off = instr_create_alu(ctx, nir_op_fadd, 1); |
| 893 |
off->src[0] = ir2_src(64, 0, IR2_SRC_CONST); |
| 894 |
off->src[1] = ir2_src(2, 0, IR2_SRC_INPUT); |
| 895 |
|
| 896 |
/* 8 max set in freedreno_screen.. unneeded instrs patched out */ |
| 897 |
for (int i = 0; i < 8; i++) { |
| 898 |
instr = instr_create_alu(ctx, nir_op_ffma, 4); |
| 899 |
instr->src[0] = ir2_src(1, IR2_SWIZZLE_WYWW, IR2_SRC_CONST); |
| 900 |
instr->src[1] = ir2_src(off->idx, IR2_SWIZZLE_XXXX, IR2_SRC_SSA); |
| 901 |
instr->src[2] = ir2_src(3 + i, 0, IR2_SRC_CONST); |
| 902 |
instr->alu.export = 32; |
| 903 |
|
| 904 |
instr = instr_create_alu(ctx, nir_op_ffma, 4); |
| 905 |
instr->src[0] = ir2_src(68 + i * 2, 0, IR2_SRC_CONST); |
| 906 |
instr->src[1] = ir2_src(wincoord->idx, 0, IR2_SRC_SSA); |
| 907 |
instr->src[2] = ir2_src(67 + i * 2, 0, IR2_SRC_CONST); |
| 908 |
instr->alu.export = 33; |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
static bool emit_cf_list(struct ir2_context *ctx, struct exec_list *list); |
| 913 |
|
| 914 |
static bool |
| 915 |
emit_block(struct ir2_context *ctx, nir_block *block) |
| 916 |
{ |
| 917 |
struct ir2_instr *instr; |
| 918 |
nir_block *succs = block->successors[0]; |
| 919 |
|
| 920 |
ctx->block_idx = block->index; |
| 921 |
|
| 922 |
nir_foreach_instr (instr, block) |
| 923 |
emit_instr(ctx, instr); |
| 924 |
|
| 925 |
if (!succs || !succs->index) |
| 926 |
return false; |
| 927 |
|
| 928 |
/* we want to be smart and always jump and have the backend cleanup |
| 929 |
* but we are not, so there are two cases where jump is needed: |
| 930 |
* loops (succs index lower) |
| 931 |
* jumps (jump instruction seen in block) |
| 932 |
*/ |
| 933 |
if (succs->index > block->index && !ctx->block_has_jump[block->index]) |
| 934 |
return false; |
| 935 |
|
| 936 |
assert(block->successors[1] == NULL); |
| 937 |
|
| 938 |
instr = ir2_instr_create(ctx, IR2_CF); |
| 939 |
instr->cf.block_idx = succs->index; |
| 940 |
/* XXX can't jump to a block with different predicate */ |
| 941 |
return true; |
| 942 |
} |
| 943 |
|
| 944 |
static void |
| 945 |
emit_if(struct ir2_context *ctx, nir_if *nif) |
| 946 |
{ |
| 947 |
unsigned pred = ctx->pred, pred_idx = ctx->pred_idx; |
| 948 |
struct ir2_instr *instr; |
| 949 |
|
| 950 |
/* XXX: blob seems to always use same register for condition */ |
| 951 |
|
| 952 |
instr = ir2_instr_create(ctx, IR2_ALU); |
| 953 |
instr->src[0] = make_src(ctx, nif->condition); |
| 954 |
instr->src_count = 1; |
| 955 |
instr->ssa.ncomp = 1; |
| 956 |
instr->alu.vector_opc = VECTOR_NONE; |
| 957 |
instr->alu.scalar_opc = SCALAR_NONE; |
| 958 |
instr->alu.export = -1; |
| 959 |
instr->alu.write_mask = 1; |
| 960 |
instr->pred = 0; |
| 961 |
|
| 962 |
/* if nested, use PRED_SETNE_PUSHv */ |
| 963 |
if (pred) { |
| 964 |
instr->alu.vector_opc = PRED_SETNE_PUSHv; |
| 965 |
instr->src[1] = instr->src[0]; |
| 966 |
instr->src[0] = ir2_src(pred_idx, 0, IR2_SRC_SSA); |
| 967 |
instr->src[0].swizzle = IR2_SWIZZLE_XXXX; |
| 968 |
instr->src[1].swizzle = IR2_SWIZZLE_XXXX; |
| 969 |
instr->src_count = 2; |
| 970 |
} else { |
| 971 |
instr->alu.scalar_opc = PRED_SETNEs; |
| 972 |
} |
| 973 |
|
| 974 |
ctx->pred_idx = instr->idx; |
| 975 |
ctx->pred = 3; |
| 976 |
|
| 977 |
emit_cf_list(ctx, &nif->then_list); |
| 978 |
|
| 979 |
/* TODO: if these is no else branch we don't need this |
| 980 |
* and if the else branch is simple, can just flip ctx->pred instead |
| 981 |
*/ |
| 982 |
instr = ir2_instr_create(ctx, IR2_ALU); |
| 983 |
instr->src[0] = ir2_src(ctx->pred_idx, 0, IR2_SRC_SSA); |
| 984 |
instr->src_count = 1; |
| 985 |
instr->ssa.ncomp = 1; |
| 986 |
instr->alu.vector_opc = VECTOR_NONE; |
| 987 |
instr->alu.scalar_opc = PRED_SET_INVs; |
| 988 |
instr->alu.export = -1; |
| 989 |
instr->alu.write_mask = 1; |
| 990 |
instr->pred = 0; |
| 991 |
ctx->pred_idx = instr->idx; |
| 992 |
|
| 993 |
emit_cf_list(ctx, &nif->else_list); |
| 994 |
|
| 995 |
/* restore predicate for nested predicates */ |
| 996 |
if (pred) { |
| 997 |
instr = ir2_instr_create(ctx, IR2_ALU); |
| 998 |
instr->src[0] = ir2_src(ctx->pred_idx, 0, IR2_SRC_SSA); |
| 999 |
instr->src_count = 1; |
| 1000 |
instr->ssa.ncomp = 1; |
| 1001 |
instr->alu.vector_opc = VECTOR_NONE; |
| 1002 |
instr->alu.scalar_opc = PRED_SET_POPs; |
| 1003 |
instr->alu.export = -1; |
| 1004 |
instr->alu.write_mask = 1; |
| 1005 |
instr->pred = 0; |
| 1006 |
ctx->pred_idx = instr->idx; |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* restore ctx->pred */ |
| 1010 |
ctx->pred = pred; |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* get the highest block idx in the loop, so we know when |
| 1014 |
* we can free registers that are allocated outside the loop |
| 1015 |
*/ |
| 1016 |
static unsigned |
| 1017 |
loop_last_block(struct exec_list *list) |
| 1018 |
{ |
| 1019 |
nir_cf_node *node = |
| 1020 |
exec_node_data(nir_cf_node, exec_list_get_tail(list), node); |
| 1021 |
switch (node->type) { |
| 1022 |
case nir_cf_node_block: |
| 1023 |
return nir_cf_node_as_block(node)->index; |
| 1024 |
case nir_cf_node_if: |
| 1025 |
assert(0); /* XXX could this ever happen? */ |
| 1026 |
return 0; |
| 1027 |
case nir_cf_node_loop: |
| 1028 |
return loop_last_block(&nir_cf_node_as_loop(node)->body); |
| 1029 |
default: |
| 1030 |
compile_error(ctx, "Not supported\n"); |
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
static void |
| 1036 |
emit_loop(struct ir2_context *ctx, nir_loop *nloop) |
| 1037 |
{ |
| 1038 |
assert(!nir_loop_has_continue_construct(nloop)); |
| 1039 |
ctx->loop_last_block[++ctx->loop_depth] = loop_last_block(&nloop->body); |
| 1040 |
emit_cf_list(ctx, &nloop->body); |
| 1041 |
ctx->loop_depth--; |
| 1042 |
} |
| 1043 |
|
| 1044 |
static bool |
| 1045 |
emit_cf_list(struct ir2_context *ctx, struct exec_list *list) |
| 1046 |
{ |
| 1047 |
bool ret = false; |
| 1048 |
foreach_list_typed (nir_cf_node, node, node, list) { |
| 1049 |
ret = false; |
| 1050 |
switch (node->type) { |
| 1051 |
case nir_cf_node_block: |
| 1052 |
ret = emit_block(ctx, nir_cf_node_as_block(node)); |
| 1053 |
break; |
| 1054 |
case nir_cf_node_if: |
| 1055 |
emit_if(ctx, nir_cf_node_as_if(node)); |
| 1056 |
break; |
| 1057 |
case nir_cf_node_loop: |
| 1058 |
emit_loop(ctx, nir_cf_node_as_loop(node)); |
| 1059 |
break; |
| 1060 |
case nir_cf_node_function: |
| 1061 |
compile_error(ctx, "Not supported\n"); |
| 1062 |
break; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
return ret; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static void |
| 1069 |
cleanup_binning(struct ir2_context *ctx) |
| 1070 |
{ |
| 1071 |
assert(ctx->so->type == MESA_SHADER_VERTEX); |
| 1072 |
|
| 1073 |
/* kill non-position outputs for binning variant */ |
| 1074 |
nir_foreach_block (block, nir_shader_get_entrypoint(ctx->nir)) { |
| 1075 |
nir_foreach_instr_safe (instr, block) { |
| 1076 |
if (instr->type != nir_instr_type_intrinsic) |
| 1077 |
continue; |
| 1078 |
|
| 1079 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 1080 |
if (intr->intrinsic != nir_intrinsic_store_output) |
| 1081 |
continue; |
| 1082 |
|
| 1083 |
if (output_slot(ctx, intr) != VARYING_SLOT_POS) |
| 1084 |
nir_instr_remove(instr); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
ir2_optimize_nir(ctx->nir, false); |
| 1089 |
} |
| 1090 |
|
| 1091 |
static bool |
| 1092 |
ir2_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data) |
| 1093 |
{ |
| 1094 |
if (instr->type != nir_instr_type_alu) |
| 1095 |
return false; |
| 1096 |
|
| 1097 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 1098 |
switch (alu->op) { |
| 1099 |
case nir_op_ball_fequal2: |
| 1100 |
case nir_op_ball_fequal3: |
| 1101 |
case nir_op_ball_fequal4: |
| 1102 |
case nir_op_bany_fnequal2: |
| 1103 |
case nir_op_bany_fnequal3: |
| 1104 |
case nir_op_bany_fnequal4: |
| 1105 |
case nir_op_ball_iequal2: |
| 1106 |
case nir_op_ball_iequal3: |
| 1107 |
case nir_op_ball_iequal4: |
| 1108 |
case nir_op_bany_inequal2: |
| 1109 |
case nir_op_bany_inequal3: |
| 1110 |
case nir_op_bany_inequal4: |
| 1111 |
case nir_op_frsq: |
| 1112 |
case nir_op_frcp: |
| 1113 |
case nir_op_flog2: |
| 1114 |
case nir_op_fexp2: |
| 1115 |
case nir_op_fsqrt: |
| 1116 |
case nir_op_fcos: |
| 1117 |
case nir_op_fsin: |
| 1118 |
return true; |
| 1119 |
default: |
| 1120 |
break; |
| 1121 |
} |
| 1122 |
|
| 1123 |
return false; |
| 1124 |
} |
| 1125 |
|
| 1126 |
void |
| 1127 |
ir2_nir_compile(struct ir2_context *ctx, bool binning) |
| 1128 |
{ |
| 1129 |
struct fd2_shader_stateobj *so = ctx->so; |
| 1130 |
|
| 1131 |
memset(ctx->ssa_map, 0xff, sizeof(ctx->ssa_map)); |
| 1132 |
|
| 1133 |
ctx->nir = nir_shader_clone(NULL, so->nir); |
| 1134 |
|
| 1135 |
if (binning) |
| 1136 |
cleanup_binning(ctx); |
| 1137 |
|
| 1138 |
OPT_V(ctx->nir, nir_opt_copy_prop); |
| 1139 |
OPT_V(ctx->nir, nir_opt_dce); |
| 1140 |
OPT_V(ctx->nir, nir_opt_move, nir_move_comparisons); |
| 1141 |
|
| 1142 |
OPT_V(ctx->nir, nir_lower_int_to_float); |
| 1143 |
OPT_V(ctx->nir, nir_lower_alu_to_scalar, ir2_alu_to_scalar_filter_cb, NULL); |
| 1144 |
OPT_V(ctx->nir, nir_lower_bool_to_float, true); |
| 1145 |
while (OPT(ctx->nir, nir_opt_algebraic)) |
| 1146 |
; |
| 1147 |
OPT_V(ctx->nir, nir_opt_algebraic_late); |
| 1148 |
|
| 1149 |
OPT_V(ctx->nir, nir_convert_from_ssa, true, false); |
| 1150 |
|
| 1151 |
OPT_V(ctx->nir, nir_move_vec_src_uses_to_dest, false); |
| 1152 |
OPT_V(ctx->nir, nir_lower_vec_to_regs, NULL, NULL); |
| 1153 |
|
| 1154 |
OPT_V(ctx->nir, nir_legacy_trivialize, true); |
| 1155 |
|
| 1156 |
OPT_V(ctx->nir, nir_opt_dce); |
| 1157 |
|
| 1158 |
nir_sweep(ctx->nir); |
| 1159 |
|
| 1160 |
if (FD_DBG(DISASM)) { |
| 1161 |
debug_printf("----------------------\n"); |
| 1162 |
nir_print_shader(ctx->nir, stdout); |
| 1163 |
debug_printf("----------------------\n"); |
| 1164 |
} |
| 1165 |
|
| 1166 |
/* fd2_shader_stateobj init */ |
| 1167 |
if (so->type == MESA_SHADER_FRAGMENT) { |
| 1168 |
ctx->f->fragcoord = -1; |
| 1169 |
ctx->f->inputs_count = 0; |
| 1170 |
memset(ctx->f->inputs, 0, sizeof(ctx->f->inputs)); |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* Setup inputs: */ |
| 1174 |
nir_foreach_shader_in_variable (in, ctx->nir) |
| 1175 |
setup_input(ctx, in); |
| 1176 |
|
| 1177 |
if (so->type == MESA_SHADER_FRAGMENT) { |
| 1178 |
unsigned idx; |
| 1179 |
for (idx = 0; idx < ctx->f->inputs_count; idx++) { |
| 1180 |
ctx->input[idx].ncomp = ctx->f->inputs[idx].ncomp; |
| 1181 |
update_range(ctx, &ctx->input[idx]); |
| 1182 |
} |
| 1183 |
/* assume we have param input and kill it later if not */ |
| 1184 |
ctx->input[idx].ncomp = 4; |
| 1185 |
update_range(ctx, &ctx->input[idx]); |
| 1186 |
} else { |
| 1187 |
ctx->input[0].ncomp = 1; |
| 1188 |
ctx->input[2].ncomp = 1; |
| 1189 |
update_range(ctx, &ctx->input[0]); |
| 1190 |
update_range(ctx, &ctx->input[2]); |
| 1191 |
} |
| 1192 |
|
| 1193 |
/* And emit the body: */ |
| 1194 |
nir_function_impl *fxn = nir_shader_get_entrypoint(ctx->nir); |
| 1195 |
|
| 1196 |
nir_foreach_reg_decl (decl, fxn) { |
| 1197 |
assert(decl->def.index < ARRAY_SIZE(ctx->reg)); |
| 1198 |
ctx->reg[decl->def.index].ncomp = nir_intrinsic_num_components(decl); |
| 1199 |
ctx->reg_count = MAX2(ctx->reg_count, decl->def.index + 1); |
| 1200 |
} |
| 1201 |
|
| 1202 |
nir_metadata_require(fxn, nir_metadata_block_index); |
| 1203 |
emit_cf_list(ctx, &fxn->body); |
| 1204 |
/* TODO emit_block(ctx, fxn->end_block); */ |
| 1205 |
|
| 1206 |
if (so->type == MESA_SHADER_VERTEX) |
| 1207 |
extra_position_exports(ctx, binning); |
| 1208 |
|
| 1209 |
ralloc_free(ctx->nir); |
| 1210 |
|
| 1211 |
/* kill unused param input */ |
| 1212 |
if (so->type == MESA_SHADER_FRAGMENT && !so->need_param) |
| 1213 |
ctx->input[ctx->f->inputs_count].initialized = false; |
| 1214 |
} |