| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "lp_bld_nir.h" |
| 27 |
#include "lp_bld_arit.h" |
| 28 |
#include "lp_bld_conv.h" |
| 29 |
#include "lp_bld_init.h" |
| 30 |
#include "lp_bld_const.h" |
| 31 |
#include "lp_bld_flow.h" |
| 32 |
#include "lp_bld_quad.h" |
| 33 |
#include "lp_bld_sample.h" |
| 34 |
#include "lp_bld_swizzle.h" |
| 35 |
#include "lp_bld_tgsi.h" |
| 36 |
#include "lp_bld_debug.h" |
| 37 |
#include "util/u_math.h" |
| 38 |
|
| 39 |
|
| 40 |
struct lp_build_nir_aos_context |
| 41 |
{ |
| 42 |
struct lp_build_context base; |
| 43 |
struct lp_build_context uint_bld; |
| 44 |
struct lp_build_context int_bld; |
| 45 |
|
| 46 |
LLVMValueRef *ssa_defs; |
| 47 |
struct hash_table *regs; |
| 48 |
|
| 49 |
nir_shader *shader; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
unsigned char swizzles[4]; |
| 59 |
unsigned char inv_swizzles[4]; |
| 60 |
|
| 61 |
LLVMValueRef consts_ptr; |
| 62 |
const LLVMValueRef *inputs; |
| 63 |
LLVMValueRef *outputs; |
| 64 |
|
| 65 |
const struct lp_build_sampler_aos *sampler; |
| 66 |
}; |
| 67 |
|
| 68 |
static inline struct lp_build_context * |
| 69 |
get_flt_bld(struct lp_build_nir_aos_context *bld, |
| 70 |
unsigned op_bit_size) |
| 71 |
{ |
| 72 |
switch (op_bit_size) { |
| 73 |
default: |
| 74 |
case 32: |
| 75 |
return &bld->base; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static inline struct lp_build_context * |
| 80 |
get_int_bld(struct lp_build_nir_aos_context *bld, |
| 81 |
bool is_unsigned, |
| 82 |
unsigned op_bit_size) |
| 83 |
{ |
| 84 |
if (is_unsigned) { |
| 85 |
switch (op_bit_size) { |
| 86 |
case 32: |
| 87 |
default: |
| 88 |
return &bld->uint_bld; |
| 89 |
} |
| 90 |
} else { |
| 91 |
switch (op_bit_size) { |
| 92 |
default: |
| 93 |
case 32: |
| 94 |
return &bld->int_bld; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static LLVMValueRef |
| 100 |
swizzle_aos(struct lp_build_nir_aos_context *bld, |
| 101 |
LLVMValueRef a, |
| 102 |
unsigned swizzle_x, |
| 103 |
unsigned swizzle_y, |
| 104 |
unsigned swizzle_z, |
| 105 |
unsigned swizzle_w) |
| 106 |
{ |
| 107 |
unsigned char swizzles[4]; |
| 108 |
assert(swizzle_x < 4); |
| 109 |
assert(swizzle_y < 4); |
| 110 |
assert(swizzle_z < 4); |
| 111 |
assert(swizzle_w < 4); |
| 112 |
|
| 113 |
swizzles[bld->inv_swizzles[0]] = bld->swizzles[swizzle_x]; |
| 114 |
swizzles[bld->inv_swizzles[1]] = bld->swizzles[swizzle_y]; |
| 115 |
swizzles[bld->inv_swizzles[2]] = bld->swizzles[swizzle_z]; |
| 116 |
swizzles[bld->inv_swizzles[3]] = bld->swizzles[swizzle_w]; |
| 117 |
|
| 118 |
return lp_build_swizzle_aos(&bld->base, a, swizzles); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
emit_var_decl(struct lp_build_nir_aos_context *bld, |
| 123 |
nir_variable *var) |
| 124 |
{ |
| 125 |
if (!bld->outputs || var->data.mode != nir_var_shader_out) |
| 126 |
return; |
| 127 |
|
| 128 |
unsigned this_loc = var->data.driver_location; |
| 129 |
bld->outputs[this_loc] = lp_build_alloca(bld->base.gallivm, |
| 130 |
bld->base.vec_type, |
| 131 |
"output"); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
static void |
| 136 |
emit_load_var(struct lp_build_nir_aos_context *bld, |
| 137 |
nir_variable_mode deref_mode, |
| 138 |
nir_variable *var, |
| 139 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]) |
| 140 |
{ |
| 141 |
unsigned location = var->data.driver_location; |
| 142 |
|
| 143 |
if (deref_mode == nir_var_shader_in) { |
| 144 |
result[0] = bld->inputs[location]; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
static void |
| 150 |
emit_store_var(struct lp_build_nir_aos_context *bld, |
| 151 |
nir_variable_mode deref_mode, |
| 152 |
nir_variable *var, |
| 153 |
LLVMValueRef vals) |
| 154 |
{ |
| 155 |
struct gallivm_state *gallivm = bld->base.gallivm; |
| 156 |
unsigned location = var->data.driver_location; |
| 157 |
|
| 158 |
if (deref_mode == nir_var_shader_out) { |
| 159 |
LLVMBuildStore(gallivm->builder, vals, bld->outputs[location]); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static LLVMValueRef |
| 165 |
emit_load_reg(struct lp_build_nir_aos_context *bld, |
| 166 |
struct lp_build_context *reg_bld, |
| 167 |
LLVMValueRef reg_storage) |
| 168 |
{ |
| 169 |
struct gallivm_state *gallivm = bld->base.gallivm; |
| 170 |
return LLVMBuildLoad2(gallivm->builder, reg_bld->vec_type, reg_storage, ""); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static unsigned |
| 179 |
swizzle_writemask(struct lp_build_nir_aos_context *bld, |
| 180 |
unsigned writemask) |
| 181 |
{ |
| 182 |
assert(writemask != 0x0); |
| 183 |
assert(writemask != 0xf); |
| 184 |
|
| 185 |
|
| 186 |
unsigned new_writemask = 0; |
| 187 |
for (unsigned chan = 0; chan < 4; chan++) { |
| 188 |
if (writemask & (1 << chan)) { |
| 189 |
new_writemask |= 1 << bld->swizzles[chan]; |
| 190 |
} |
| 191 |
} |
| 192 |
return new_writemask; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
static void |
| 197 |
emit_store_reg(struct lp_build_nir_aos_context *bld, |
| 198 |
struct lp_build_context *reg_bld, |
| 199 |
unsigned writemask, |
| 200 |
LLVMValueRef reg_storage, |
| 201 |
LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS]) |
| 202 |
{ |
| 203 |
struct gallivm_state *gallivm = bld->base.gallivm; |
| 204 |
|
| 205 |
if (writemask == 0xf) { |
| 206 |
LLVMBuildStore(gallivm->builder, vals[0], reg_storage); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
writemask = swizzle_writemask(bld, writemask); |
| 211 |
|
| 212 |
LLVMValueRef cur = LLVMBuildLoad2(gallivm->builder, reg_bld->vec_type, |
| 213 |
reg_storage, ""); |
| 214 |
LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); |
| 215 |
LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH]; |
| 216 |
for (unsigned j = 0; j < 16; j++) { |
| 217 |
unsigned comp = j % 4; |
| 218 |
if (writemask & (1 << comp)) { |
| 219 |
shuffles[j] = LLVMConstInt(i32t, 16 + j, 0); |
| 220 |
} else { |
| 221 |
shuffles[j] = LLVMConstInt(i32t, j, 0); |
| 222 |
} |
| 223 |
} |
| 224 |
cur = LLVMBuildShuffleVector(gallivm->builder, cur, vals[0], |
| 225 |
LLVMConstVector(shuffles, 16), ""); |
| 226 |
|
| 227 |
LLVMBuildStore(gallivm->builder, cur, reg_storage); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
static void |
| 232 |
emit_load_ubo(struct lp_build_nir_aos_context *bld, |
| 233 |
unsigned nc, |
| 234 |
LLVMValueRef offset, |
| 235 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]) |
| 236 |
{ |
| 237 |
LLVMBuilderRef builder = bld->base.gallivm->builder; |
| 238 |
struct gallivm_state *gallivm = bld->base.gallivm; |
| 239 |
struct lp_type type = bld->base.type; |
| 240 |
LLVMValueRef res; |
| 241 |
|
| 242 |
res = bld->base.undef; |
| 243 |
offset = LLVMBuildExtractElement(builder, offset, |
| 244 |
lp_build_const_int32(gallivm, 0), ""); |
| 245 |
assert(LLVMIsConstant(offset)); |
| 246 |
unsigned offset_val = LLVMConstIntGetZExtValue(offset) >> 2; |
| 247 |
for (unsigned chan = 0; chan < nc; ++chan) { |
| 248 |
LLVMValueRef this_offset = lp_build_const_int32(gallivm, |
| 249 |
offset_val + chan); |
| 250 |
|
| 251 |
LLVMTypeRef scalar_type = LLVMInt8TypeInContext(gallivm->context); |
| 252 |
LLVMValueRef scalar_ptr = LLVMBuildGEP2(builder, scalar_type, bld->consts_ptr, &this_offset, 1, ""); |
| 253 |
LLVMValueRef scalar = LLVMBuildLoad2(builder, scalar_type, scalar_ptr, ""); |
| 254 |
|
| 255 |
lp_build_name(scalar, "const[%u].%c", offset_val, "xyzw"[chan]); |
| 256 |
|
| 257 |
LLVMValueRef swizzle = lp_build_const_int32(bld->base.gallivm, |
| 258 |
nc == 1 ? 0 : bld->swizzles[chan]); |
| 259 |
|
| 260 |
res = LLVMBuildInsertElement(builder, res, scalar, swizzle, ""); |
| 261 |
} |
| 262 |
|
| 263 |
if (type.length > 4) { |
| 264 |
LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH]; |
| 265 |
|
| 266 |
for (unsigned chan = 0; chan < nc; ++chan) { |
| 267 |
shuffles[chan] = |
| 268 |
lp_build_const_int32(bld->base.gallivm, chan); |
| 269 |
} |
| 270 |
|
| 271 |
for (unsigned i = nc; i < type.length; ++i) { |
| 272 |
shuffles[i] = shuffles[i % nc]; |
| 273 |
} |
| 274 |
|
| 275 |
res = LLVMBuildShuffleVector(builder, res, bld->base.undef, |
| 276 |
LLVMConstVector(shuffles, type.length), |
| 277 |
""); |
| 278 |
} |
| 279 |
|
| 280 |
if (nc == 4) |
| 281 |
swizzle_aos(bld, res, 0, 1, 2, 3); |
| 282 |
|
| 283 |
result[0] = res; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
static void |
| 288 |
emit_tex(struct lp_build_nir_aos_context *bld, |
| 289 |
struct lp_sampler_params *params) |
| 290 |
{ |
| 291 |
static const struct lp_derivatives derivs = { 0 }; |
| 292 |
params->type = bld->base.type; |
| 293 |
params->texel[0] = bld->sampler->emit_fetch_texel(bld->sampler, |
| 294 |
&bld->base, |
| 295 |
TGSI_TEXTURE_2D, |
| 296 |
params->texture_index, |
| 297 |
params->coords[0], |
| 298 |
params->derivs ? params->derivs[0] : derivs, |
| 299 |
LP_BLD_TEX_MODIFIER_NONE); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
static void |
| 304 |
emit_load_const(struct lp_build_nir_aos_context *bld, |
| 305 |
const nir_load_const_instr *instr, |
| 306 |
LLVMValueRef outval[NIR_MAX_VEC_COMPONENTS]) |
| 307 |
{ |
| 308 |
LLVMValueRef elems[16]; |
| 309 |
const int nc = instr->def.num_components; |
| 310 |
bool do_swizzle = false; |
| 311 |
|
| 312 |
if (nc == 4) |
| 313 |
do_swizzle = true; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
assert(bld->base.type.length <= ARRAY_SIZE(elems)); |
| 324 |
for (unsigned i = 0; i < bld->base.type.length; i++) { |
| 325 |
const unsigned j = do_swizzle ? bld->swizzles[i % nc] : i % nc; |
| 326 |
assert(instr->value[j].f32 >= 0.0f); |
| 327 |
assert(instr->value[j].f32 <= 1.0f); |
| 328 |
const unsigned u8val = float_to_ubyte(instr->value[j].f32); |
| 329 |
elems[i] = LLVMConstInt(bld->uint_bld.int_elem_type, u8val, 0); |
| 330 |
} |
| 331 |
outval[0] = LLVMConstVector(elems, bld->base.type.length); |
| 332 |
outval[1] = outval[2] = outval[3] = NULL; |
| 333 |
} |
| 334 |
|
| 335 |
static LLVMValueRef |
| 336 |
cast_type(struct lp_build_nir_aos_context *bld, LLVMValueRef val, |
| 337 |
nir_alu_type alu_type, unsigned bit_size) |
| 338 |
{ |
| 339 |
LLVMBuilderRef builder = bld->base.gallivm->builder; |
| 340 |
switch (alu_type) { |
| 341 |
case nir_type_float: |
| 342 |
switch (bit_size) { |
| 343 |
case 32: |
| 344 |
return LLVMBuildBitCast(builder, val, bld->base.vec_type, ""); |
| 345 |
default: |
| 346 |
assert(0); |
| 347 |
break; |
| 348 |
} |
| 349 |
break; |
| 350 |
case nir_type_int: |
| 351 |
switch (bit_size) { |
| 352 |
case 32: |
| 353 |
return LLVMBuildBitCast(builder, val, bld->int_bld.vec_type, ""); |
| 354 |
default: |
| 355 |
assert(0); |
| 356 |
break; |
| 357 |
} |
| 358 |
break; |
| 359 |
case nir_type_uint: |
| 360 |
switch (bit_size) { |
| 361 |
case 1: |
| 362 |
case 32: |
| 363 |
return LLVMBuildBitCast(builder, val, bld->uint_bld.vec_type, ""); |
| 364 |
default: |
| 365 |
assert(0); |
| 366 |
break; |
| 367 |
} |
| 368 |
break; |
| 369 |
case nir_type_uint32: |
| 370 |
return LLVMBuildBitCast(builder, val, bld->uint_bld.vec_type, ""); |
| 371 |
default: |
| 372 |
return val; |
| 373 |
} |
| 374 |
return NULL; |
| 375 |
} |
| 376 |
|
| 377 |
static LLVMValueRef |
| 378 |
get_src(struct lp_build_nir_aos_context *bld, nir_src src) |
| 379 |
{ |
| 380 |
return bld->ssa_defs[src.ssa->index]; |
| 381 |
} |
| 382 |
|
| 383 |
static void |
| 384 |
assign_ssa(struct lp_build_nir_aos_context *bld, int idx, LLVMValueRef ptr) |
| 385 |
{ |
| 386 |
bld->ssa_defs[idx] = ptr; |
| 387 |
} |
| 388 |
|
| 389 |
static void |
| 390 |
assign_ssa_dest(struct lp_build_nir_aos_context *bld, const nir_def *ssa, |
| 391 |
LLVMValueRef *vals) |
| 392 |
{ |
| 393 |
assign_ssa(bld, ssa->index, vals[0]); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static LLVMValueRef |
| 404 |
get_alu_src(struct lp_build_nir_aos_context *bld, |
| 405 |
nir_alu_src src, |
| 406 |
unsigned num_components) |
| 407 |
{ |
| 408 |
assert(num_components >= 1); |
| 409 |
assert(num_components <= 4); |
| 410 |
|
| 411 |
struct gallivm_state *gallivm = bld->base.gallivm; |
| 412 |
LLVMBuilderRef builder = gallivm->builder; |
| 413 |
const unsigned src_components = nir_src_num_components(src.src); |
| 414 |
assert(src_components > 0); |
| 415 |
LLVMValueRef value = get_src(bld, src.src); |
| 416 |
assert(value); |
| 417 |
|
| 418 |
|
| 419 |
bool need_swizzle = false; |
| 420 |
for (unsigned i = 0; i < src_components; ++i) { |
| 421 |
if (src.swizzle[i] != i) { |
| 422 |
need_swizzle = true; |
| 423 |
break; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
if (!need_swizzle) |
| 428 |
return value; |
| 429 |
|
| 430 |
if (need_swizzle || num_components != src_components) { |
| 431 |
if (need_swizzle) { |
| 432 |
|
| 433 |
assert(LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind); |
| 434 |
|
| 435 |
|
| 436 |
assert(bld->base.type.width == 8); |
| 437 |
assert(bld->base.type.length == 16); |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
LLVMValueRef shuffles[LP_MAX_VECTOR_WIDTH]; |
| 446 |
for (unsigned i = 0; i < 16; i++) { |
| 447 |
unsigned chan = i % 4; |
| 448 |
|
| 449 |
if (chan < num_components) { |
| 450 |
chan = src.swizzle[chan]; |
| 451 |
} else { |
| 452 |
chan = src.swizzle[0]; |
| 453 |
} |
| 454 |
|
| 455 |
chan = bld->swizzles[chan]; |
| 456 |
shuffles[i] = lp_build_const_int32(gallivm, (i & ~3) + chan); |
| 457 |
} |
| 458 |
value = LLVMBuildShuffleVector(builder, value, |
| 459 |
LLVMGetUndef(LLVMTypeOf(value)), |
| 460 |
LLVMConstVector(shuffles, 16), ""); |
| 461 |
} else if (src_components > 1 && num_components == 1) { |
| 462 |
value = LLVMBuildExtractValue(gallivm->builder, value, |
| 463 |
src.swizzle[0], ""); |
| 464 |
} else if (src_components == 1 && num_components > 1) { |
| 465 |
LLVMValueRef values[] = {value, value, value, value, |
| 466 |
value, value, value, value, |
| 467 |
value, value, value, value, |
| 468 |
value, value, value, value}; |
| 469 |
value = lp_nir_array_build_gather_values(builder, values, num_components); |
| 470 |
} else { |
| 471 |
LLVMValueRef arr = LLVMGetUndef(LLVMArrayType(LLVMTypeOf(LLVMBuildExtractValue(builder, value, 0, "")), num_components)); |
| 472 |
for (unsigned i = 0; i < num_components; i++) |
| 473 |
arr = LLVMBuildInsertValue(builder, arr, LLVMBuildExtractValue(builder, value, src.swizzle[i], ""), i, ""); |
| 474 |
value = arr; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
return value; |
| 479 |
} |
| 480 |
|
| 481 |
static LLVMValueRef |
| 482 |
do_alu_action(struct lp_build_nir_aos_context *bld, |
| 483 |
const nir_alu_instr *instr, |
| 484 |
unsigned src_bit_size[NIR_MAX_VEC_COMPONENTS], |
| 485 |
LLVMValueRef src[NIR_MAX_VEC_COMPONENTS]) |
| 486 |
{ |
| 487 |
LLVMValueRef result = NULL; |
| 488 |
|
| 489 |
switch (instr->op) { |
| 490 |
case nir_op_fmul: |
| 491 |
result = lp_build_mul(get_flt_bld(bld, src_bit_size[0]), |
| 492 |
src[0], src[1]); |
| 493 |
break; |
| 494 |
case nir_op_mov: |
| 495 |
result = src[0]; |
| 496 |
break; |
| 497 |
default: |
| 498 |
assert(0); |
| 499 |
break; |
| 500 |
} |
| 501 |
return result; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
static void |
| 506 |
visit_alu(struct lp_build_nir_aos_context *bld, |
| 507 |
const nir_alu_instr *instr) |
| 508 |
{ |
| 509 |
LLVMValueRef src[NIR_MAX_VEC_COMPONENTS]; |
| 510 |
unsigned src_bit_size[NIR_MAX_VEC_COMPONENTS]; |
| 511 |
const unsigned num_components = instr->def.num_components; |
| 512 |
unsigned src_components; |
| 513 |
|
| 514 |
switch (instr->op) { |
| 515 |
case nir_op_vec2: |
| 516 |
case nir_op_vec4: |
| 517 |
src_components = 1; |
| 518 |
break; |
| 519 |
default: |
| 520 |
src_components = num_components; |
| 521 |
break; |
| 522 |
} |
| 523 |
|
| 524 |
for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) { |
| 525 |
src[i] = get_alu_src(bld, instr->src[i], src_components); |
| 526 |
src_bit_size[i] = nir_src_bit_size(instr->src[i].src); |
| 527 |
} |
| 528 |
|
| 529 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS] = { NULL }; |
| 530 |
if (instr->op == nir_op_vec4 || |
| 531 |
instr->op == nir_op_vec2) { |
| 532 |
for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) { |
| 533 |
result[i] = cast_type(bld, src[i], |
| 534 |
nir_op_infos[instr->op].input_types[i], |
| 535 |
src_bit_size[i]); |
| 536 |
} |
| 537 |
} else { |
| 538 |
result[0] = do_alu_action(bld, instr, src_bit_size, src); |
| 539 |
} |
| 540 |
|
| 541 |
assign_ssa_dest(bld, &instr->def, result); |
| 542 |
} |
| 543 |
|
| 544 |
static void |
| 545 |
visit_load_const(struct lp_build_nir_aos_context *bld, |
| 546 |
const nir_load_const_instr *instr) |
| 547 |
{ |
| 548 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS] = { NULL }; |
| 549 |
emit_load_const(bld, instr, result); |
| 550 |
assign_ssa_dest(bld, &instr->def, result); |
| 551 |
} |
| 552 |
|
| 553 |
static void |
| 554 |
visit_load_input(struct lp_build_nir_aos_context *bld, |
| 555 |
nir_intrinsic_instr *instr, |
| 556 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]) |
| 557 |
{ |
| 558 |
nir_variable var = {0}; |
| 559 |
var.data.location = nir_intrinsic_io_semantics(instr).location; |
| 560 |
var.data.driver_location = nir_intrinsic_base(instr); |
| 561 |
var.data.location_frac = nir_intrinsic_component(instr); |
| 562 |
|
| 563 |
emit_load_var(bld, nir_var_shader_in, &var, result); |
| 564 |
} |
| 565 |
|
| 566 |
static void |
| 567 |
visit_store_output(struct lp_build_nir_aos_context *bld, |
| 568 |
nir_intrinsic_instr *instr) |
| 569 |
{ |
| 570 |
nir_variable var = {0}; |
| 571 |
var.data.location = nir_intrinsic_io_semantics(instr).location; |
| 572 |
var.data.driver_location = nir_intrinsic_base(instr); |
| 573 |
var.data.location_frac = nir_intrinsic_component(instr); |
| 574 |
|
| 575 |
unsigned mask = nir_intrinsic_write_mask(instr); |
| 576 |
|
| 577 |
LLVMValueRef src = get_src(bld, instr->src[0]); |
| 578 |
|
| 579 |
if (mask == 0x1 && LLVMGetTypeKind(LLVMTypeOf(src)) == LLVMArrayTypeKind) { |
| 580 |
src = LLVMBuildExtractValue(bld->base.gallivm->builder, |
| 581 |
src, 0, ""); |
| 582 |
} |
| 583 |
|
| 584 |
emit_store_var(bld, nir_var_shader_out, &var, src); |
| 585 |
} |
| 586 |
|
| 587 |
static void |
| 588 |
visit_load_reg(struct lp_build_nir_aos_context *bld, |
| 589 |
nir_intrinsic_instr *instr, |
| 590 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]) |
| 591 |
{ |
| 592 |
nir_intrinsic_instr *decl = nir_reg_get_decl(instr->src[0].ssa); |
| 593 |
|
| 594 |
struct hash_entry *entry = _mesa_hash_table_search(bld->regs, decl); |
| 595 |
LLVMValueRef reg_storage = (LLVMValueRef)entry->data; |
| 596 |
|
| 597 |
unsigned bit_size = nir_intrinsic_bit_size(decl); |
| 598 |
struct lp_build_context *reg_bld = get_int_bld(bld, true, bit_size); |
| 599 |
|
| 600 |
LLVMValueRef val = emit_load_reg(bld, reg_bld, reg_storage); |
| 601 |
|
| 602 |
result[0] = val; |
| 603 |
} |
| 604 |
|
| 605 |
static void |
| 606 |
visit_store_reg(struct lp_build_nir_aos_context *bld, |
| 607 |
nir_intrinsic_instr *instr) |
| 608 |
{ |
| 609 |
nir_intrinsic_instr *decl = nir_reg_get_decl(instr->src[1].ssa); |
| 610 |
unsigned write_mask = nir_intrinsic_write_mask(instr); |
| 611 |
assert(write_mask != 0x0); |
| 612 |
|
| 613 |
LLVMValueRef val = get_src(bld, instr->src[0]); |
| 614 |
LLVMValueRef vals[NIR_MAX_VEC_COMPONENTS] = { NULL }; |
| 615 |
vals[0] = val; |
| 616 |
|
| 617 |
struct hash_entry *entry = _mesa_hash_table_search(bld->regs, decl); |
| 618 |
LLVMValueRef reg_storage = (LLVMValueRef)entry->data; |
| 619 |
|
| 620 |
unsigned bit_size = nir_intrinsic_bit_size(decl); |
| 621 |
struct lp_build_context *reg_bld = get_int_bld(bld, true, bit_size); |
| 622 |
|
| 623 |
emit_store_reg(bld, reg_bld, write_mask, reg_storage, vals); |
| 624 |
} |
| 625 |
|
| 626 |
static void |
| 627 |
visit_load_var(struct lp_build_nir_aos_context *bld, |
| 628 |
nir_intrinsic_instr *instr, |
| 629 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]) |
| 630 |
{ |
| 631 |
nir_deref_instr *deref = nir_def_as_deref(instr->src[0].ssa); |
| 632 |
nir_variable *var = nir_deref_instr_get_variable(deref); |
| 633 |
assert(util_bitcount(deref->modes) == 1); |
| 634 |
nir_variable_mode mode = deref->modes; |
| 635 |
|
| 636 |
emit_load_var(bld, mode, var, result); |
| 637 |
} |
| 638 |
|
| 639 |
static void |
| 640 |
visit_store_var(struct lp_build_nir_aos_context *bld, |
| 641 |
nir_intrinsic_instr *instr) |
| 642 |
{ |
| 643 |
nir_deref_instr *deref = nir_def_as_deref(instr->src[0].ssa); |
| 644 |
nir_variable *var = nir_deref_instr_get_variable(deref); |
| 645 |
assert(util_bitcount(deref->modes) == 1); |
| 646 |
nir_variable_mode mode = deref->modes; |
| 647 |
LLVMValueRef src = get_src(bld, instr->src[1]); |
| 648 |
|
| 649 |
emit_store_var(bld, mode, var, src); |
| 650 |
} |
| 651 |
|
| 652 |
static void |
| 653 |
visit_load_ubo(struct lp_build_nir_aos_context *bld, |
| 654 |
nir_intrinsic_instr *instr, |
| 655 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]) |
| 656 |
{ |
| 657 |
LLVMValueRef offset = get_src(bld, instr->src[1]); |
| 658 |
emit_load_ubo(bld, instr->def.num_components, offset, result); |
| 659 |
} |
| 660 |
|
| 661 |
static void |
| 662 |
visit_intrinsic(struct lp_build_nir_aos_context *bld, |
| 663 |
nir_intrinsic_instr *instr) |
| 664 |
{ |
| 665 |
LLVMValueRef result[NIR_MAX_VEC_COMPONENTS] = { NULL }; |
| 666 |
switch (instr->intrinsic) { |
| 667 |
case nir_intrinsic_decl_reg: |
| 668 |
|
| 669 |
break; |
| 670 |
case nir_intrinsic_load_reg: |
| 671 |
case nir_intrinsic_load_reg_indirect: |
| 672 |
visit_load_reg(bld, instr, result); |
| 673 |
break; |
| 674 |
case nir_intrinsic_store_reg: |
| 675 |
case nir_intrinsic_store_reg_indirect: |
| 676 |
visit_store_reg(bld, instr); |
| 677 |
break; |
| 678 |
case nir_intrinsic_load_input: |
| 679 |
case nir_intrinsic_load_per_primitive_input: |
| 680 |
visit_load_input(bld, instr, result); |
| 681 |
break; |
| 682 |
case nir_intrinsic_store_output: |
| 683 |
visit_store_output(bld, instr); |
| 684 |
break; |
| 685 |
case nir_intrinsic_load_deref: |
| 686 |
visit_load_var(bld, instr, result); |
| 687 |
break; |
| 688 |
case nir_intrinsic_store_deref: |
| 689 |
visit_store_var(bld, instr); |
| 690 |
break; |
| 691 |
case nir_intrinsic_load_ubo: |
| 692 |
visit_load_ubo(bld, instr, result); |
| 693 |
break; |
| 694 |
default: |
| 695 |
fprintf(stderr, "Unsupported intrinsic: "); |
| 696 |
nir_print_instr(&instr->instr, stderr); |
| 697 |
fprintf(stderr, "\n"); |
| 698 |
assert(0); |
| 699 |
break; |
| 700 |
} |
| 701 |
if (result[0]) { |
| 702 |
assign_ssa_dest(bld, &instr->def, result); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
static void |
| 707 |
visit_tex(struct lp_build_nir_aos_context *bld, nir_tex_instr *instr) |
| 708 |
{ |
| 709 |
struct gallivm_state *gallivm = bld->base.gallivm; |
| 710 |
LLVMBuilderRef builder = gallivm->builder; |
| 711 |
|
| 712 |
nir_deref_instr *texture_deref_instr = NULL; |
| 713 |
nir_deref_instr *sampler_deref_instr = NULL; |
| 714 |
|
| 715 |
LLVMValueRef coords[5] = { NULL }; |
| 716 |
struct lp_sampler_params params = { 0 }; |
| 717 |
LLVMValueRef texel[NIR_MAX_VEC_COMPONENTS] = { NULL }; |
| 718 |
LLVMValueRef texture_unit_offset = NULL; |
| 719 |
LLVMValueRef offsets[3] = { NULL }; |
| 720 |
LLVMValueRef explicit_lod = NULL, ms_index = NULL; |
| 721 |
LLVMValueRef coord_undef = LLVMGetUndef(bld->base.vec_type); |
| 722 |
|
| 723 |
for (unsigned i = 0; i < instr->num_srcs; i++) { |
| 724 |
switch (instr->src[i].src_type) { |
| 725 |
case nir_tex_src_coord: { |
| 726 |
LLVMValueRef coord = get_src(bld, instr->src[i].src); |
| 727 |
coords[0] = coord; |
| 728 |
for (unsigned chan = 1; chan < 5; chan++) { |
| 729 |
coords[chan] = coord_undef; |
| 730 |
} |
| 731 |
break; |
| 732 |
} |
| 733 |
case nir_tex_src_texture_deref: |
| 734 |
texture_deref_instr = nir_src_as_deref(instr->src[i].src); |
| 735 |
break; |
| 736 |
case nir_tex_src_sampler_deref: |
| 737 |
sampler_deref_instr = nir_src_as_deref(instr->src[i].src); |
| 738 |
break; |
| 739 |
case nir_tex_src_comparator: |
| 740 |
coords[4] = get_src(bld, instr->src[i].src); |
| 741 |
coords[4] = cast_type(bld, coords[4], nir_type_float, 32); |
| 742 |
break; |
| 743 |
case nir_tex_src_bias: |
| 744 |
explicit_lod = cast_type(bld, get_src(bld, instr->src[i].src), nir_type_float, 32); |
| 745 |
break; |
| 746 |
case nir_tex_src_lod: |
| 747 |
if (instr->op == nir_texop_txf) |
| 748 |
explicit_lod = cast_type(bld, get_src(bld, instr->src[i].src), nir_type_int, 32); |
| 749 |
else |
| 750 |
explicit_lod = cast_type(bld, get_src(bld, instr->src[i].src), nir_type_float, 32); |
| 751 |
break; |
| 752 |
case nir_tex_src_offset: { |
| 753 |
int offset_cnt = instr->coord_components; |
| 754 |
if (instr->is_array) |
| 755 |
offset_cnt--; |
| 756 |
LLVMValueRef offset_val = get_src(bld, instr->src[i].src); |
| 757 |
if (offset_cnt == 1) |
| 758 |
offsets[0] = cast_type(bld, offset_val, nir_type_int, 32); |
| 759 |
else { |
| 760 |
for (unsigned chan = 0; chan < offset_cnt; ++chan) { |
| 761 |
offsets[chan] = LLVMBuildExtractValue(builder, offset_val, |
| 762 |
chan, ""); |
| 763 |
offsets[chan] = cast_type(bld, offsets[chan], nir_type_int, 32); |
| 764 |
} |
| 765 |
} |
| 766 |
break; |
| 767 |
} |
| 768 |
case nir_tex_src_ms_index: |
| 769 |
ms_index = cast_type(bld, get_src(bld, instr->src[i].src), nir_type_int, 32); |
| 770 |
break; |
| 771 |
|
| 772 |
case nir_tex_src_texture_offset: |
| 773 |
texture_unit_offset = get_src(bld, instr->src[i].src); |
| 774 |
break; |
| 775 |
case nir_tex_src_sampler_offset: |
| 776 |
break; |
| 777 |
case nir_tex_src_plane: |
| 778 |
assert(nir_src_is_const(instr->src[i].src) && !nir_src_as_uint(instr->src[i].src)); |
| 779 |
break; |
| 780 |
default: |
| 781 |
assert(0); |
| 782 |
break; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
if (!sampler_deref_instr) |
| 787 |
sampler_deref_instr = texture_deref_instr; |
| 788 |
|
| 789 |
coords[0] = cast_type(bld, coords[0], nir_type_float, 32); |
| 790 |
|
| 791 |
uint32_t samp_base_index = 0, tex_base_index = 0; |
| 792 |
if (!sampler_deref_instr) { |
| 793 |
int samp_src_index = nir_tex_instr_src_index(instr, nir_tex_src_sampler_handle); |
| 794 |
if (samp_src_index == -1) { |
| 795 |
samp_base_index = instr->sampler_index; |
| 796 |
} |
| 797 |
} |
| 798 |
if (!texture_deref_instr) { |
| 799 |
int tex_src_index = nir_tex_instr_src_index(instr, nir_tex_src_texture_handle); |
| 800 |
if (tex_src_index == -1) { |
| 801 |
tex_base_index = instr->texture_index; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
params.sample_key = lp_build_nir_sample_key(bld->shader->info.stage, instr); |
| 806 |
params.offsets = offsets; |
| 807 |
params.texture_index = tex_base_index; |
| 808 |
params.texture_index_offset = texture_unit_offset; |
| 809 |
params.sampler_index = samp_base_index; |
| 810 |
params.coords = coords; |
| 811 |
params.texel = texel; |
| 812 |
params.lod = explicit_lod; |
| 813 |
params.ms_index = ms_index; |
| 814 |
emit_tex(bld, ¶ms); |
| 815 |
|
| 816 |
assign_ssa_dest(bld, &instr->def, texel); |
| 817 |
} |
| 818 |
|
| 819 |
static void |
| 820 |
visit_block(struct lp_build_nir_aos_context *bld, nir_block *block) |
| 821 |
{ |
| 822 |
nir_foreach_instr(instr, block) |
| 823 |
{ |
| 824 |
switch (instr->type) { |
| 825 |
case nir_instr_type_alu: |
| 826 |
visit_alu(bld, nir_instr_as_alu(instr)); |
| 827 |
break; |
| 828 |
case nir_instr_type_load_const: |
| 829 |
visit_load_const(bld, nir_instr_as_load_const(instr)); |
| 830 |
break; |
| 831 |
case nir_instr_type_intrinsic: |
| 832 |
visit_intrinsic(bld, nir_instr_as_intrinsic(instr)); |
| 833 |
break; |
| 834 |
case nir_instr_type_tex: |
| 835 |
visit_tex(bld, nir_instr_as_tex(instr)); |
| 836 |
break; |
| 837 |
case nir_instr_type_deref: |
| 838 |
break; |
| 839 |
default: |
| 840 |
fprintf(stderr, "Unknown NIR instr type: "); |
| 841 |
nir_print_instr(instr, stderr); |
| 842 |
fprintf(stderr, "\n"); |
| 843 |
abort(); |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
static void |
| 849 |
visit_cf_list(struct lp_build_nir_aos_context *bld, |
| 850 |
struct exec_list *list) |
| 851 |
{ |
| 852 |
foreach_list_typed(nir_cf_node, node, node, list) |
| 853 |
{ |
| 854 |
switch (node->type) { |
| 855 |
case nir_cf_node_block: |
| 856 |
visit_block(bld, nir_cf_node_as_block(node)); |
| 857 |
break; |
| 858 |
case nir_cf_node_if: |
| 859 |
case nir_cf_node_loop: |
| 860 |
default: |
| 861 |
assert(0); |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
void |
| 867 |
lp_build_nir_aos(struct gallivm_state *gallivm, |
| 868 |
struct nir_shader *shader, |
| 869 |
struct lp_type type, |
| 870 |
const unsigned char swizzles[4], |
| 871 |
LLVMValueRef consts_ptr, |
| 872 |
const LLVMValueRef *inputs, |
| 873 |
LLVMValueRef *outputs, |
| 874 |
const struct lp_build_sampler_aos *sampler) |
| 875 |
{ |
| 876 |
struct lp_build_nir_aos_context bld; |
| 877 |
|
| 878 |
memset(&bld, 0, sizeof bld); |
| 879 |
lp_build_context_init(&bld.base, gallivm, type); |
| 880 |
lp_build_context_init(&bld.uint_bld, gallivm, lp_uint_type(type)); |
| 881 |
lp_build_context_init(&bld.int_bld, gallivm, lp_int_type(type)); |
| 882 |
|
| 883 |
for (unsigned chan = 0; chan < 4; ++chan) { |
| 884 |
bld.swizzles[chan] = swizzles[chan]; |
| 885 |
bld.inv_swizzles[swizzles[chan]] = chan; |
| 886 |
} |
| 887 |
bld.sampler = sampler; |
| 888 |
|
| 889 |
bld.inputs = inputs; |
| 890 |
bld.outputs = outputs; |
| 891 |
bld.consts_ptr = consts_ptr; |
| 892 |
|
| 893 |
NIR_PASS(_, shader, nir_convert_to_lcssa, true, true); |
| 894 |
NIR_PASS(_, shader, nir_convert_from_ssa, true, false); |
| 895 |
NIR_PASS(_, shader, nir_lower_locals_to_regs, 32); |
| 896 |
NIR_PASS(_, shader, nir_remove_dead_derefs); |
| 897 |
NIR_PASS(_, shader, nir_remove_dead_variables, nir_var_function_temp, NULL); |
| 898 |
NIR_PASS(_, shader, nir_move_vec_src_uses_to_dest, false); |
| 899 |
NIR_PASS(_, shader, nir_lower_vec_to_regs, NULL, NULL); |
| 900 |
|
| 901 |
nir_foreach_shader_out_variable(variable, shader) |
| 902 |
emit_var_decl(&bld, variable); |
| 903 |
|
| 904 |
if (shader->info.io_lowered) { |
| 905 |
uint64_t outputs_written = shader->info.outputs_written; |
| 906 |
|
| 907 |
while (outputs_written) { |
| 908 |
unsigned location = u_bit_scan64(&outputs_written); |
| 909 |
nir_variable var = {0}; |
| 910 |
|
| 911 |
var.type = glsl_vec4_type(); |
| 912 |
var.data.mode = nir_var_shader_out; |
| 913 |
var.data.location = location; |
| 914 |
var.data.driver_location = util_bitcount64(shader->info.outputs_written & |
| 915 |
BITFIELD64_MASK(location)); |
| 916 |
emit_var_decl(&bld, &var); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
bld.regs = _mesa_hash_table_create(NULL, _mesa_hash_pointer, |
| 921 |
_mesa_key_pointer_equal); |
| 922 |
|
| 923 |
bld.shader = shader; |
| 924 |
|
| 925 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 926 |
nir_foreach_reg_decl(reg, impl) { |
| 927 |
LLVMTypeRef type = bld.base.int_vec_type; |
| 928 |
LLVMValueRef reg_alloc = lp_build_alloca(gallivm, type, "reg"); |
| 929 |
_mesa_hash_table_insert(bld.regs, reg, reg_alloc); |
| 930 |
} |
| 931 |
nir_index_ssa_defs(impl); |
| 932 |
bld.ssa_defs = calloc(impl->ssa_alloc, sizeof(LLVMValueRef)); |
| 933 |
visit_cf_list(&bld, &impl->body); |
| 934 |
|
| 935 |
free(bld.ssa_defs); |
| 936 |
ralloc_free(bld.regs); |
| 937 |
} |