| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "kk_nir_lower_vbo.h" |
| 9 |
|
| 10 |
#include "kk_cmd_buffer.h" |
| 11 |
|
| 12 |
#include "compiler/nir/nir_builder.h" |
| 13 |
#include "compiler/nir/nir_format_convert.h" |
| 14 |
#include "util/bitset.h" |
| 15 |
#include "util/u_math.h" |
| 16 |
#include "shader_enums.h" |
| 17 |
|
| 18 |
struct ctx { |
| 19 |
struct kk_attribute *attribs; |
| 20 |
bool requires_vertex_id; |
| 21 |
bool requires_instance_id; |
| 22 |
bool requires_base_instance; |
| 23 |
}; |
| 24 |
|
| 25 |
static bool |
| 26 |
is_rgb10_a2(const struct util_format_description *desc) |
| 27 |
{ |
| 28 |
return desc->channel[0].shift == 0 && desc->channel[0].size == 10 && |
| 29 |
desc->channel[1].shift == 10 && desc->channel[1].size == 10 && |
| 30 |
desc->channel[2].shift == 20 && desc->channel[2].size == 10 && |
| 31 |
desc->channel[3].shift == 30 && desc->channel[3].size == 2; |
| 32 |
} |
| 33 |
|
| 34 |
static bool |
| 35 |
is_rg11_b10(const struct util_format_description *desc) |
| 36 |
{ |
| 37 |
return desc->channel[0].shift == 0 && desc->channel[0].size == 11 && |
| 38 |
desc->channel[1].shift == 11 && desc->channel[1].size == 11 && |
| 39 |
desc->channel[2].shift == 22 && desc->channel[2].size == 10; |
| 40 |
} |
| 41 |
|
| 42 |
static enum pipe_format |
| 43 |
kk_vbo_internal_format(enum pipe_format format) |
| 44 |
{ |
| 45 |
const struct util_format_description *desc = util_format_description(format); |
| 46 |
|
| 47 |
|
| 48 |
if (is_rgb10_a2(desc) || is_rg11_b10(desc)) |
| 49 |
return PIPE_FORMAT_R32_UINT; |
| 50 |
|
| 51 |
|
| 52 |
if (format == PIPE_FORMAT_R11G11B10_FLOAT) |
| 53 |
return format; |
| 54 |
|
| 55 |
|
| 56 |
if (!desc->is_array) |
| 57 |
return PIPE_FORMAT_NONE; |
| 58 |
|
| 59 |
|
| 60 |
int idx = util_format_get_first_non_void_channel(format); |
| 61 |
if (idx < 0) |
| 62 |
return PIPE_FORMAT_NONE; |
| 63 |
|
| 64 |
|
| 65 |
if ((desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB) || |
| 66 |
(desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)) |
| 67 |
return PIPE_FORMAT_NONE; |
| 68 |
|
| 69 |
|
| 70 |
struct util_format_channel_description chan = desc->channel[idx]; |
| 71 |
|
| 72 |
|
| 73 |
switch (chan.size) { |
| 74 |
case 32: |
| 75 |
return PIPE_FORMAT_R32_UINT; |
| 76 |
case 16: |
| 77 |
return PIPE_FORMAT_R16_UINT; |
| 78 |
case 8: |
| 79 |
return PIPE_FORMAT_R8_UINT; |
| 80 |
default: |
| 81 |
return PIPE_FORMAT_NONE; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
bool |
| 86 |
kk_vbo_supports_format(enum pipe_format format) |
| 87 |
{ |
| 88 |
return kk_vbo_internal_format(format) != PIPE_FORMAT_NONE; |
| 89 |
} |
| 90 |
|
| 91 |
static nir_def * |
| 92 |
apply_swizzle_channel(nir_builder *b, nir_def *vec, unsigned swizzle, |
| 93 |
bool is_int) |
| 94 |
{ |
| 95 |
switch (swizzle) { |
| 96 |
case PIPE_SWIZZLE_X: |
| 97 |
return nir_channel(b, vec, 0); |
| 98 |
case PIPE_SWIZZLE_Y: |
| 99 |
return nir_channel(b, vec, 1); |
| 100 |
case PIPE_SWIZZLE_Z: |
| 101 |
return nir_channel(b, vec, 2); |
| 102 |
case PIPE_SWIZZLE_W: |
| 103 |
return nir_channel(b, vec, 3); |
| 104 |
case PIPE_SWIZZLE_0: |
| 105 |
return nir_imm_intN_t(b, 0, vec->bit_size); |
| 106 |
case PIPE_SWIZZLE_1: |
| 107 |
return is_int ? nir_imm_intN_t(b, 1, vec->bit_size) |
| 108 |
: nir_imm_floatN_t(b, 1.0, vec->bit_size); |
| 109 |
default: |
| 110 |
UNREACHABLE("Invalid swizzle channel"); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
static bool |
| 115 |
pass(struct nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 116 |
{ |
| 117 |
if (intr->intrinsic != nir_intrinsic_load_input) |
| 118 |
return false; |
| 119 |
|
| 120 |
struct ctx *ctx = data; |
| 121 |
struct kk_attribute *attribs = ctx->attribs; |
| 122 |
b->cursor = nir_instr_remove(&intr->instr); |
| 123 |
|
| 124 |
nir_src *offset_src = nir_get_io_offset_src(intr); |
| 125 |
assert(nir_src_is_const(*offset_src) && "no attribute indirects"); |
| 126 |
unsigned index = nir_intrinsic_base(intr) + nir_src_as_uint(*offset_src); |
| 127 |
|
| 128 |
struct kk_attribute attrib = attribs[index]; |
| 129 |
|
| 130 |
const struct util_format_description *desc = |
| 131 |
util_format_description(attrib.format); |
| 132 |
int chan = util_format_get_first_non_void_channel(attrib.format); |
| 133 |
assert(chan >= 0); |
| 134 |
|
| 135 |
bool is_float = desc->channel[chan].type == UTIL_FORMAT_TYPE_FLOAT; |
| 136 |
bool is_unsigned = desc->channel[chan].type == UTIL_FORMAT_TYPE_UNSIGNED; |
| 137 |
bool is_signed = desc->channel[chan].type == UTIL_FORMAT_TYPE_SIGNED; |
| 138 |
bool is_fixed = desc->channel[chan].type == UTIL_FORMAT_TYPE_FIXED; |
| 139 |
bool is_int = util_format_is_pure_integer(attrib.format); |
| 140 |
|
| 141 |
assert((is_float ^ is_unsigned ^ is_signed ^ is_fixed) && "Invalid format"); |
| 142 |
|
| 143 |
enum pipe_format interchange_format = kk_vbo_internal_format(attrib.format); |
| 144 |
assert(interchange_format != PIPE_FORMAT_NONE); |
| 145 |
|
| 146 |
unsigned interchange_align = util_format_get_blocksize(interchange_format); |
| 147 |
unsigned interchange_comps = util_format_get_nr_components(attrib.format); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
unsigned interchange_register_size = |
| 154 |
util_format_is_pure_uint(interchange_format) ? (interchange_align * 8) |
| 155 |
: intr->def.bit_size; |
| 156 |
|
| 157 |
|
| 158 |
if (interchange_format == PIPE_FORMAT_R32_UINT && !desc->is_array) |
| 159 |
interchange_comps = 1; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
nir_def *el; |
| 165 |
if (attrib.instanced) { |
| 166 |
if (attrib.divisor > 0) { |
| 167 |
|
| 168 |
nir_def *instance_id = |
| 169 |
nir_isub(b, nir_load_instance_id(b), nir_load_base_instance(b)); |
| 170 |
el = nir_udiv_imm(b, instance_id, attrib.divisor); |
| 171 |
ctx->requires_instance_id = true; |
| 172 |
} else |
| 173 |
el = nir_imm_int(b, 0); |
| 174 |
|
| 175 |
el = nir_iadd(b, el, nir_load_base_instance(b)); |
| 176 |
ctx->requires_base_instance = true; |
| 177 |
|
| 178 |
BITSET_SET(b->shader->info.system_values_read, |
| 179 |
SYSTEM_VALUE_BASE_INSTANCE); |
| 180 |
} else { |
| 181 |
el = nir_load_vertex_id(b); |
| 182 |
ctx->requires_vertex_id = true; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
nir_def *argbuf = nir_load_buffer_ptr_kk(b, 1, 64, .binding = 0); |
| 187 |
uint64_t attrib_base_offset = |
| 188 |
offsetof(struct kk_root_descriptor_table, draw.attrib_base[index]); |
| 189 |
nir_def *base = nir_load_global_constant( |
| 190 |
b, 1, 64, nir_iadd_imm(b, argbuf, attrib_base_offset)); |
| 191 |
|
| 192 |
uint64_t buffer_stride_offset = offsetof( |
| 193 |
struct kk_root_descriptor_table, draw.buffer_strides[attrib.binding]); |
| 194 |
nir_def *stride = nir_load_global_constant( |
| 195 |
b, 1, 32, nir_iadd_imm(b, argbuf, buffer_stride_offset)); |
| 196 |
nir_def *stride_offset_el = |
| 197 |
nir_imul(b, el, nir_udiv_imm(b, stride, interchange_align)); |
| 198 |
|
| 199 |
|
| 200 |
nir_def *memory = nir_load_constant_agx( |
| 201 |
b, interchange_comps, interchange_register_size, base, stride_offset_el, |
| 202 |
.format = interchange_format, .base = 0u); |
| 203 |
|
| 204 |
unsigned dest_size = intr->def.bit_size; |
| 205 |
unsigned bits[] = {desc->channel[chan].size, desc->channel[chan].size, |
| 206 |
desc->channel[chan].size, desc->channel[chan].size}; |
| 207 |
|
| 208 |
|
| 209 |
if (is_rg11_b10(desc)) { |
| 210 |
memory = nir_format_unpack_11f11f10f(b, memory); |
| 211 |
} else if (is_rgb10_a2(desc)) { |
| 212 |
bits[0] = 10; |
| 213 |
bits[1] = 10; |
| 214 |
bits[2] = 10; |
| 215 |
bits[3] = 2; |
| 216 |
if (is_signed) |
| 217 |
memory = nir_format_unpack_sint(b, memory, bits, 4); |
| 218 |
else |
| 219 |
memory = nir_format_unpack_uint(b, memory, bits, 4); |
| 220 |
} |
| 221 |
|
| 222 |
if (desc->channel[chan].normalized) { |
| 223 |
if (is_signed) |
| 224 |
memory = nir_format_snorm_to_float(b, memory, bits); |
| 225 |
else |
| 226 |
memory = nir_format_unorm_to_float(b, memory, bits); |
| 227 |
} else if (desc->channel[chan].pure_integer) { |
| 228 |
if (is_signed) |
| 229 |
memory = nir_i2iN(b, memory, dest_size); |
| 230 |
else |
| 231 |
memory = nir_u2uN(b, memory, dest_size); |
| 232 |
} else { |
| 233 |
if (is_unsigned) |
| 234 |
memory = nir_u2fN(b, memory, dest_size); |
| 235 |
else if (is_signed || is_fixed) |
| 236 |
memory = nir_i2fN(b, memory, dest_size); |
| 237 |
else |
| 238 |
memory = nir_f2fN(b, memory, dest_size); |
| 239 |
|
| 240 |
|
| 241 |
if (is_fixed) { |
| 242 |
assert(desc->is_array && desc->channel[chan].size == 32); |
| 243 |
assert(dest_size == 32 && "overflow if smaller"); |
| 244 |
memory = nir_fmul_imm(b, memory, 1.0 / 65536.0); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
nir_def *channels[4] = {NULL}; |
| 252 |
|
| 253 |
for (unsigned i = 0; i < intr->num_components; ++i) { |
| 254 |
unsigned c = nir_intrinsic_component(intr) + i; |
| 255 |
channels[i] = apply_swizzle_channel(b, memory, desc->swizzle[c], is_int); |
| 256 |
} |
| 257 |
|
| 258 |
nir_def *logical = nir_vec(b, channels, intr->num_components); |
| 259 |
nir_def_rewrite_uses(&intr->def, logical); |
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
bool |
| 264 |
kk_nir_lower_vbo(nir_shader *nir, struct kk_attribute *attribs) |
| 265 |
{ |
| 266 |
assert(nir->info.stage == MESA_SHADER_VERTEX); |
| 267 |
|
| 268 |
struct ctx ctx = {.attribs = attribs}; |
| 269 |
bool progress = |
| 270 |
nir_shader_intrinsics_pass(nir, pass, nir_metadata_control_flow, &ctx); |
| 271 |
|
| 272 |
if (ctx.requires_instance_id) |
| 273 |
BITSET_SET(nir->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID); |
| 274 |
if (ctx.requires_base_instance) |
| 275 |
BITSET_SET(nir->info.system_values_read, SYSTEM_VALUE_BASE_INSTANCE); |
| 276 |
if (ctx.requires_vertex_id) |
| 277 |
BITSET_SET(nir->info.system_values_read, SYSTEM_VALUE_VERTEX_ID); |
| 278 |
return progress; |
| 279 |
} |