| 1 |
/* |
| 2 |
* Copyright (C) 2022 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/nir/nir_builder.h" |
| 7 |
#include "pan_nir.h" |
| 8 |
|
| 9 |
static void |
| 10 |
lower_xfb_output(nir_builder *b, nir_intrinsic_instr *intr, |
| 11 |
unsigned start_component, unsigned num_components, |
| 12 |
unsigned buffer, unsigned offset_words) |
| 13 |
{ |
| 14 |
assert(buffer < MAX_XFB_BUFFERS); |
| 15 |
assert(nir_intrinsic_component(intr) == 0); // TODO |
| 16 |
|
| 17 |
/* Transform feedback info in units of words, convert to bytes. */ |
| 18 |
uint16_t stride = b->shader->info.xfb_stride[buffer] * 4; |
| 19 |
assert(stride != 0); |
| 20 |
|
| 21 |
uint16_t offset = offset_words * 4; |
| 22 |
|
| 23 |
nir_def *index = nir_iadd( |
| 24 |
b, nir_imul(b, nir_load_instance_id(b), nir_load_num_vertices(b)), |
| 25 |
nir_load_raw_vertex_id_pan(b)); |
| 26 |
|
| 27 |
BITSET_SET(b->shader->info.system_values_read, |
| 28 |
SYSTEM_VALUE_VERTEX_ID_ZERO_BASE); |
| 29 |
BITSET_SET(b->shader->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID); |
| 30 |
|
| 31 |
nir_def *buf = nir_load_xfb_address(b, 64, .base = buffer); |
| 32 |
nir_def *addr = nir_iadd( |
| 33 |
b, buf, |
| 34 |
nir_u2u64(b, nir_iadd_imm(b, nir_imul_imm(b, index, stride), offset))); |
| 35 |
|
| 36 |
nir_def *src = intr->src[0].ssa; |
| 37 |
nir_component_mask_t mask = nir_component_mask(num_components); |
| 38 |
nir_def *value = nir_channels(b, src, mask << start_component); |
| 39 |
nir_store_global(b, value, addr); |
| 40 |
} |
| 41 |
|
| 42 |
static bool |
| 43 |
lower_xfb(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) |
| 44 |
{ |
| 45 |
/* In transform feedback programs, vertex ID becomes zero-based, so apply |
| 46 |
* that lowering even on Valhall. |
| 47 |
*/ |
| 48 |
if (intr->intrinsic == nir_intrinsic_load_vertex_id) { |
| 49 |
b->cursor = nir_instr_remove(&intr->instr); |
| 50 |
|
| 51 |
nir_def *repl = nir_iadd(b, nir_load_raw_vertex_id_pan(b), |
| 52 |
nir_load_raw_vertex_offset_pan(b)); |
| 53 |
|
| 54 |
nir_def_rewrite_uses(&intr->def, repl); |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
if (intr->intrinsic != nir_intrinsic_store_output) |
| 59 |
return false; |
| 60 |
|
| 61 |
bool progress = false; |
| 62 |
|
| 63 |
b->cursor = nir_before_instr(&intr->instr); |
| 64 |
|
| 65 |
nir_io_xfb xfb = nir_intrinsic_io_xfb(intr); |
| 66 |
for (unsigned i = 0; i < 4; ++i) { |
| 67 |
if (!xfb.out[i].num_components) |
| 68 |
continue; |
| 69 |
|
| 70 |
lower_xfb_output(b, intr, i, xfb.out[i].num_components, |
| 71 |
xfb.out[i].buffer, xfb.out[i].offset); |
| 72 |
progress = true; |
| 73 |
} |
| 74 |
|
| 75 |
nir_instr_remove(&intr->instr); |
| 76 |
return progress; |
| 77 |
} |
| 78 |
|
| 79 |
bool |
| 80 |
pan_nir_lower_xfb(nir_shader *nir) |
| 81 |
{ |
| 82 |
return nir_shader_intrinsics_pass( |
| 83 |
nir, lower_xfb, nir_metadata_control_flow, NULL); |
| 84 |
} |