| 1 |
/* |
| 2 |
* Copyright (C) 2020 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Implements framebuffer format conversions in software for Midgard/Bifrost |
| 8 |
* blend shaders. This pass is designed for a single render target; Midgard |
| 9 |
* duplicates blend shaders for MRT to simplify everything. A particular |
| 10 |
* framebuffer format may be categorized as 1) typed load available, 2) typed |
| 11 |
* unpack available, or 3) software unpack only, and likewise for stores. The |
| 12 |
* first two types are handled in the compiler backend directly, so this module |
| 13 |
* is responsible for identifying type 3 formats (hardware dependent) and |
| 14 |
* inserting appropriate ALU code to perform the conversion from the packed |
| 15 |
* type to a designated unpacked type, and vice versa. |
| 16 |
* |
| 17 |
* The unpacked type depends on the format: |
| 18 |
* |
| 19 |
* - For 32-bit float formats or >8-bit UNORM, 32-bit floats. |
| 20 |
* - For other floats, 16-bit floats. |
| 21 |
* - For 32-bit ints, 32-bit ints. |
| 22 |
* - For 8-bit ints, 8-bit ints. |
| 23 |
* - For other ints, 16-bit ints. |
| 24 |
* |
| 25 |
* The rationale is to optimize blending and logic op instructions by using the |
| 26 |
* smallest precision necessary to store the pixel losslessly. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "pan_nir.h" |
| 30 |
#include "compiler/nir/nir.h" |
| 31 |
#include "compiler/nir/nir_builder.h" |
| 32 |
#include "compiler/nir/nir_format_convert.h" |
| 33 |
#include "util/format/u_format.h" |
| 34 |
|
| 35 |
/* Determines the unpacked type best suiting a given format, so the rest of the |
| 36 |
* pipeline may be adjusted accordingly */ |
| 37 |
|
| 38 |
nir_alu_type |
| 39 |
pan_unpacked_type_for_format(const struct util_format_description *desc) |
| 40 |
{ |
| 41 |
int c = util_format_get_first_non_void_channel(desc->format); |
| 42 |
|
| 43 |
if (c == -1) |
| 44 |
UNREACHABLE("Void format not renderable"); |
| 45 |
|
| 46 |
bool large = (desc->channel[c].size > 16); |
| 47 |
bool large_norm = (desc->channel[c].size > 8); |
| 48 |
bool bit8 = (desc->channel[c].size == 8); |
| 49 |
assert(desc->channel[c].size <= 32); |
| 50 |
|
| 51 |
if (desc->channel[c].normalized) |
| 52 |
return large_norm ? nir_type_float32 : nir_type_float16; |
| 53 |
|
| 54 |
switch (desc->channel[c].type) { |
| 55 |
case UTIL_FORMAT_TYPE_UNSIGNED: |
| 56 |
return bit8 ? nir_type_uint8 : large ? nir_type_uint32 : nir_type_uint16; |
| 57 |
case UTIL_FORMAT_TYPE_SIGNED: |
| 58 |
return bit8 ? nir_type_int8 : large ? nir_type_int32 : nir_type_int16; |
| 59 |
case UTIL_FORMAT_TYPE_FLOAT: |
| 60 |
return large ? nir_type_float32 : nir_type_float16; |
| 61 |
default: |
| 62 |
UNREACHABLE("Format not renderable"); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static bool |
| 67 |
pan_is_format_native(const struct util_format_description *desc, |
| 68 |
bool broken_ld_special, bool is_store) |
| 69 |
{ |
| 70 |
if (is_store || broken_ld_special) |
| 71 |
return false; |
| 72 |
|
| 73 |
if (util_format_is_pure_integer(desc->format) || |
| 74 |
util_format_is_float(desc->format)) |
| 75 |
return false; |
| 76 |
|
| 77 |
/* Some formats are missing as typed but have unpacks */ |
| 78 |
if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT) |
| 79 |
return false; |
| 80 |
|
| 81 |
if (desc->is_array) { |
| 82 |
int c = util_format_get_first_non_void_channel(desc->format); |
| 83 |
assert(c >= 0); |
| 84 |
if (desc->channel[c].size > 8) |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
/* Software packs/unpacks, by format class. Packs take in the pixel value typed |
| 92 |
* as `pan_unpacked_type_for_format` of the format and return an i32vec4 |
| 93 |
* suitable for storing (with components replicated to fill). Unpacks do the |
| 94 |
* reverse but cannot rely on replication. */ |
| 95 |
|
| 96 |
static nir_def * |
| 97 |
pan_replicate(nir_builder *b, nir_def *v, unsigned num_components) |
| 98 |
{ |
| 99 |
nir_def *replicated[4]; |
| 100 |
|
| 101 |
for (unsigned i = 0; i < 4; ++i) |
| 102 |
replicated[i] = nir_channel(b, v, i % num_components); |
| 103 |
|
| 104 |
return nir_vec(b, replicated, 4); |
| 105 |
} |
| 106 |
|
| 107 |
/* Pure x16 formats are x16 unpacked, so it's similar, but we need to pack |
| 108 |
* upper/lower halves of course */ |
| 109 |
|
| 110 |
static nir_def * |
| 111 |
pan_pack_pure_16(nir_builder *b, nir_def *v, unsigned num_components) |
| 112 |
{ |
| 113 |
nir_def *v4 = pan_replicate(b, v, num_components); |
| 114 |
|
| 115 |
nir_def *lo = nir_pack_32_2x16(b, nir_channels(b, v4, 0x3 << 0)); |
| 116 |
nir_def *hi = nir_pack_32_2x16(b, nir_channels(b, v4, 0x3 << 2)); |
| 117 |
|
| 118 |
return nir_vec4(b, lo, hi, lo, hi); |
| 119 |
} |
| 120 |
|
| 121 |
static nir_def * |
| 122 |
pan_unpack_pure_16(nir_builder *b, nir_def *pack, unsigned num_components) |
| 123 |
{ |
| 124 |
nir_def *unpacked[4]; |
| 125 |
|
| 126 |
assert(num_components <= 4); |
| 127 |
|
| 128 |
for (unsigned i = 0; i < num_components; i += 2) { |
| 129 |
nir_def *halves = nir_unpack_32_2x16(b, nir_channel(b, pack, i >> 1)); |
| 130 |
|
| 131 |
unpacked[i + 0] = nir_channel(b, halves, 0); |
| 132 |
unpacked[i + 1] = nir_channel(b, halves, 1); |
| 133 |
} |
| 134 |
|
| 135 |
return nir_pad_vec4(b, nir_vec(b, unpacked, num_components)); |
| 136 |
} |
| 137 |
|
| 138 |
static nir_def * |
| 139 |
pan_pack_reorder(nir_builder *b, const struct util_format_description *desc, |
| 140 |
nir_def *v) |
| 141 |
{ |
| 142 |
unsigned swizzle[4] = {0, 1, 2, 3}; |
| 143 |
|
| 144 |
for (unsigned i = 0; i < v->num_components; i++) { |
| 145 |
if (desc->swizzle[i] <= PIPE_SWIZZLE_W) |
| 146 |
swizzle[i] = desc->swizzle[i]; |
| 147 |
} |
| 148 |
|
| 149 |
return nir_swizzle(b, v, swizzle, v->num_components); |
| 150 |
} |
| 151 |
|
| 152 |
static nir_def * |
| 153 |
pan_unpack_reorder(nir_builder *b, const struct util_format_description *desc, |
| 154 |
nir_def *v) |
| 155 |
{ |
| 156 |
unsigned swizzle[4] = {0, 1, 2, 3}; |
| 157 |
|
| 158 |
for (unsigned i = 0; i < v->num_components; i++) { |
| 159 |
if (desc->swizzle[i] <= PIPE_SWIZZLE_W) |
| 160 |
swizzle[desc->swizzle[i]] = i; |
| 161 |
} |
| 162 |
|
| 163 |
return nir_swizzle(b, v, swizzle, v->num_components); |
| 164 |
} |
| 165 |
|
| 166 |
static nir_def * |
| 167 |
pan_pack_pure_8(nir_builder *b, nir_def *v, unsigned num_components) |
| 168 |
{ |
| 169 |
return nir_replicate( |
| 170 |
b, nir_pack_32_4x8(b, pan_replicate(b, v, num_components)), 4); |
| 171 |
} |
| 172 |
|
| 173 |
static nir_def * |
| 174 |
pan_unpack_pure_8(nir_builder *b, nir_def *pack, unsigned num_components) |
| 175 |
{ |
| 176 |
nir_def *unpacked = nir_unpack_32_4x8(b, nir_channel(b, pack, 0)); |
| 177 |
return nir_trim_vector(b, unpacked, num_components); |
| 178 |
} |
| 179 |
|
| 180 |
static nir_def * |
| 181 |
pan_fsat(nir_builder *b, nir_def *v, bool is_signed) |
| 182 |
{ |
| 183 |
if (is_signed) |
| 184 |
return nir_fsat_signed(b, v); |
| 185 |
else |
| 186 |
return nir_fsat(b, v); |
| 187 |
} |
| 188 |
|
| 189 |
static float |
| 190 |
norm_scale(bool snorm, unsigned bits) |
| 191 |
{ |
| 192 |
if (snorm) |
| 193 |
return (1 << (bits - 1)) - 1; |
| 194 |
else |
| 195 |
return (1 << bits) - 1; |
| 196 |
} |
| 197 |
|
| 198 |
/* For <= 8-bits per channel, [U,S]NORM formats are packed like [U,S]NORM 8, |
| 199 |
* with zeroes spacing out each component as needed */ |
| 200 |
|
| 201 |
static nir_def * |
| 202 |
pan_pack_norm(nir_builder *b, nir_def *v, unsigned x, unsigned y, unsigned z, |
| 203 |
unsigned w, bool is_signed) |
| 204 |
{ |
| 205 |
/* If a channel has N bits, 1.0 is encoded as 2^N - 1 for UNORMs and |
| 206 |
* 2^(N-1) - 1 for SNORMs */ |
| 207 |
nir_def *scales = |
| 208 |
is_signed ? nir_imm_vec4_16(b, (1 << (x - 1)) - 1, (1 << (y - 1)) - 1, |
| 209 |
(1 << (z - 1)) - 1, (1 << (w - 1)) - 1) |
| 210 |
: nir_imm_vec4_16(b, (1 << x) - 1, (1 << y) - 1, (1 << z) - 1, |
| 211 |
(1 << w) - 1); |
| 212 |
|
| 213 |
/* If a channel has N bits, we pad out to the byte by (8 - N) bits */ |
| 214 |
nir_def *shifts = nir_imm_ivec4(b, 8 - x, 8 - y, 8 - z, 8 - w); |
| 215 |
nir_def *clamped = pan_fsat(b, nir_pad_vec4(b, v), is_signed); |
| 216 |
|
| 217 |
nir_def *f = nir_fmul(b, clamped, scales); |
| 218 |
nir_def *u8 = nir_f2u8(b, nir_fround_even(b, f)); |
| 219 |
nir_def *s = nir_ishl(b, u8, shifts); |
| 220 |
nir_def *repl = nir_pack_32_4x8(b, s); |
| 221 |
|
| 222 |
return nir_replicate(b, repl, 4); |
| 223 |
} |
| 224 |
|
| 225 |
static nir_def * |
| 226 |
pan_pack_unorm(nir_builder *b, nir_def *v, unsigned x, unsigned y, unsigned z, |
| 227 |
unsigned w) |
| 228 |
{ |
| 229 |
return pan_pack_norm(b, v, x, y, z, w, false); |
| 230 |
} |
| 231 |
|
| 232 |
/* RGB10_A2 is packed in the tilebuffer as the bottom 3 bytes being the top |
| 233 |
* 8-bits of RGB and the top byte being RGBA as 2-bits packed. As imirkin |
| 234 |
* pointed out, this means free conversion to RGBX8 */ |
| 235 |
|
| 236 |
static nir_def * |
| 237 |
pan_pack_unorm_1010102(nir_builder *b, nir_def *v) |
| 238 |
{ |
| 239 |
nir_def *scale = nir_imm_vec4(b, 1023.0, 1023.0, 1023.0, 3.0); |
| 240 |
nir_def *s = |
| 241 |
nir_f2u32(b, nir_fround_even(b, nir_fmul(b, nir_fsat(b, v), scale))); |
| 242 |
|
| 243 |
nir_def *top8 = nir_ushr(b, s, nir_imm_ivec4(b, 0x2, 0x2, 0x2, 0x2)); |
| 244 |
nir_def *top8_rgb = nir_pack_32_4x8(b, nir_u2u8(b, top8)); |
| 245 |
|
| 246 |
nir_def *bottom2 = nir_iand(b, s, nir_imm_ivec4(b, 0x3, 0x3, 0x3, 0x3)); |
| 247 |
|
| 248 |
nir_def *top = |
| 249 |
nir_ior(b, |
| 250 |
nir_ior(b, nir_ishl_imm(b, nir_channel(b, bottom2, 0), 24 + 0), |
| 251 |
nir_ishl_imm(b, nir_channel(b, bottom2, 1), 24 + 2)), |
| 252 |
nir_ior(b, nir_ishl_imm(b, nir_channel(b, bottom2, 2), 24 + 4), |
| 253 |
nir_ishl_imm(b, nir_channel(b, bottom2, 3), 24 + 6))); |
| 254 |
|
| 255 |
nir_def *p = nir_ior(b, top, top8_rgb); |
| 256 |
return nir_replicate(b, p, 4); |
| 257 |
} |
| 258 |
|
| 259 |
/* On the other hand, the pure int RGB10_A2 is identical to the spec */ |
| 260 |
|
| 261 |
static nir_def * |
| 262 |
pan_pack_int_1010102(nir_builder *b, nir_def *v, bool is_signed) |
| 263 |
{ |
| 264 |
v = nir_u2u32(b, v); |
| 265 |
|
| 266 |
/* Clamp the values */ |
| 267 |
if (is_signed) { |
| 268 |
v = nir_imin(b, v, nir_imm_ivec4(b, 511, 511, 511, 1)); |
| 269 |
v = nir_imax(b, v, nir_imm_ivec4(b, -512, -512, -512, -2)); |
| 270 |
} else { |
| 271 |
v = nir_umin(b, v, nir_imm_ivec4(b, 1023, 1023, 1023, 3)); |
| 272 |
} |
| 273 |
|
| 274 |
v = nir_ishl(b, v, nir_imm_ivec4(b, 0, 10, 20, 30)); |
| 275 |
v = nir_ior(b, nir_ior(b, nir_channel(b, v, 0), nir_channel(b, v, 1)), |
| 276 |
nir_ior(b, nir_channel(b, v, 2), nir_channel(b, v, 3))); |
| 277 |
|
| 278 |
return nir_replicate(b, v, 4); |
| 279 |
} |
| 280 |
|
| 281 |
static nir_def * |
| 282 |
pan_unpack_int_1010102(nir_builder *b, nir_def *packed, bool is_signed) |
| 283 |
{ |
| 284 |
nir_def *v = nir_replicate(b, nir_channel(b, packed, 0), 4); |
| 285 |
|
| 286 |
/* Left shift all components so the sign bit is on the MSB, and |
| 287 |
* can be extended by ishr(). The ishl()+[u,i]shr() combination |
| 288 |
* sets all unused bits to 0 without requiring a mask. |
| 289 |
*/ |
| 290 |
v = nir_ishl(b, v, nir_imm_ivec4(b, 22, 12, 2, 0)); |
| 291 |
|
| 292 |
if (is_signed) |
| 293 |
v = nir_ishr(b, v, nir_imm_ivec4(b, 22, 22, 22, 30)); |
| 294 |
else |
| 295 |
v = nir_ushr(b, v, nir_imm_ivec4(b, 22, 22, 22, 30)); |
| 296 |
|
| 297 |
return nir_i2i16(b, v); |
| 298 |
} |
| 299 |
|
| 300 |
/* NIR means we can *finally* catch a break */ |
| 301 |
|
| 302 |
static nir_def * |
| 303 |
pan_pack_r11g11b10(nir_builder *b, nir_def *v) |
| 304 |
{ |
| 305 |
return nir_replicate(b, nir_format_pack_11f11f10f(b, nir_f2f32(b, v)), 4); |
| 306 |
} |
| 307 |
|
| 308 |
static nir_def * |
| 309 |
pan_unpack_r11g11b10(nir_builder *b, nir_def *v) |
| 310 |
{ |
| 311 |
nir_def *f32 = nir_format_unpack_11f11f10f(b, nir_channel(b, v, 0)); |
| 312 |
nir_def *f16 = nir_f2fmp(b, f32); |
| 313 |
|
| 314 |
/* Extend to vec4 with alpha */ |
| 315 |
nir_def *components[4] = {nir_channel(b, f16, 0), nir_channel(b, f16, 1), |
| 316 |
nir_channel(b, f16, 2), nir_imm_float16(b, 1.0)}; |
| 317 |
|
| 318 |
return nir_vec(b, components, 4); |
| 319 |
} |
| 320 |
|
| 321 |
/* Wrapper around sRGB conversion */ |
| 322 |
|
| 323 |
static nir_def * |
| 324 |
pan_linear_to_srgb(nir_builder *b, nir_def *linear) |
| 325 |
{ |
| 326 |
nir_def *rgb = nir_trim_vector(b, linear, 3); |
| 327 |
|
| 328 |
/* TODO: fp16 native conversion */ |
| 329 |
nir_def *srgb = |
| 330 |
nir_f2fmp(b, nir_format_linear_to_srgb(b, nir_f2f32(b, rgb))); |
| 331 |
|
| 332 |
nir_def *comp[4] = { |
| 333 |
nir_channel(b, srgb, 0), |
| 334 |
nir_channel(b, srgb, 1), |
| 335 |
nir_channel(b, srgb, 2), |
| 336 |
nir_channel(b, linear, 3), |
| 337 |
}; |
| 338 |
|
| 339 |
return nir_vec(b, comp, 4); |
| 340 |
} |
| 341 |
|
| 342 |
static nir_def * |
| 343 |
pan_unpack_pure(nir_builder *b, nir_def *packed, unsigned size, unsigned nr) |
| 344 |
{ |
| 345 |
switch (size) { |
| 346 |
case 32: |
| 347 |
return nir_trim_vector(b, packed, nr); |
| 348 |
case 16: |
| 349 |
return pan_unpack_pure_16(b, packed, nr); |
| 350 |
case 8: |
| 351 |
return pan_unpack_pure_8(b, packed, nr); |
| 352 |
default: |
| 353 |
UNREACHABLE("Unrenderable size"); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/* Generic dispatches for un/pack regardless of format */ |
| 358 |
|
| 359 |
static nir_def * |
| 360 |
pan_unpack(nir_builder *b, const struct util_format_description *desc, |
| 361 |
nir_def *packed) |
| 362 |
{ |
| 363 |
if (desc->is_array) { |
| 364 |
int c = util_format_get_first_non_void_channel(desc->format); |
| 365 |
assert(c >= 0); |
| 366 |
struct util_format_channel_description d = desc->channel[c]; |
| 367 |
nir_def *unpacked = pan_unpack_pure(b, packed, d.size, desc->nr_channels); |
| 368 |
|
| 369 |
/* Normalized formats are unpacked as integers. We need to |
| 370 |
* convert to float for the final result. |
| 371 |
*/ |
| 372 |
if (d.normalized) { |
| 373 |
bool snorm = desc->is_snorm; |
| 374 |
unsigned float_sz = (d.size <= 8 ? 16 : 32); |
| 375 |
float multiplier = norm_scale(snorm, d.size); |
| 376 |
|
| 377 |
nir_def *as_float = snorm ? nir_i2fN(b, unpacked, float_sz) |
| 378 |
: nir_u2fN(b, unpacked, float_sz); |
| 379 |
|
| 380 |
return nir_fmul_imm(b, as_float, 1.0 / multiplier); |
| 381 |
} else { |
| 382 |
return unpacked; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
switch (desc->format) { |
| 387 |
case PIPE_FORMAT_R10G10B10A2_UINT: |
| 388 |
case PIPE_FORMAT_B10G10R10A2_UINT: |
| 389 |
return pan_unpack_int_1010102(b, packed, false); |
| 390 |
case PIPE_FORMAT_R10G10B10A2_SINT: |
| 391 |
case PIPE_FORMAT_B10G10R10A2_SINT: |
| 392 |
return pan_unpack_int_1010102(b, packed, true); |
| 393 |
case PIPE_FORMAT_R11G11B10_FLOAT: |
| 394 |
return pan_unpack_r11g11b10(b, packed); |
| 395 |
default: |
| 396 |
break; |
| 397 |
} |
| 398 |
|
| 399 |
mesa_loge("%s\n", desc->name); |
| 400 |
UNREACHABLE("Unknown format"); |
| 401 |
} |
| 402 |
|
| 403 |
static nir_def *pan_pack(nir_builder *b, |
| 404 |
const struct util_format_description *desc, |
| 405 |
nir_def * unpacked) |
| 406 |
{ |
| 407 |
if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) |
| 408 |
unpacked = pan_linear_to_srgb(b, unpacked); |
| 409 |
|
| 410 |
if (desc->is_array) { |
| 411 |
int c = util_format_get_first_non_void_channel(desc->format); |
| 412 |
assert(c >= 0); |
| 413 |
struct util_format_channel_description d = desc->channel[c]; |
| 414 |
|
| 415 |
/* Pure formats are packed as-is */ |
| 416 |
nir_def *raw = unpacked; |
| 417 |
|
| 418 |
/* Normalized formats get normalized first */ |
| 419 |
if (d.normalized) { |
| 420 |
bool snorm = desc->is_snorm; |
| 421 |
float multiplier = norm_scale(snorm, d.size); |
| 422 |
nir_def *clamped = pan_fsat(b, unpacked, snorm); |
| 423 |
nir_def *normed = nir_fmul_imm(b, clamped, multiplier); |
| 424 |
|
| 425 |
raw = nir_f2uN(b, normed, d.size); |
| 426 |
} |
| 427 |
|
| 428 |
/* Pack the raw format */ |
| 429 |
switch (d.size) { |
| 430 |
case 32: |
| 431 |
return pan_replicate(b, raw, desc->nr_channels); |
| 432 |
case 16: |
| 433 |
return pan_pack_pure_16(b, raw, desc->nr_channels); |
| 434 |
case 8: |
| 435 |
return pan_pack_pure_8(b, raw, desc->nr_channels); |
| 436 |
default: |
| 437 |
UNREACHABLE("Unrenderable size"); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
switch (desc->format) { |
| 442 |
case PIPE_FORMAT_B4G4R4A4_UNORM: |
| 443 |
case PIPE_FORMAT_B4G4R4X4_UNORM: |
| 444 |
case PIPE_FORMAT_A4R4_UNORM: |
| 445 |
case PIPE_FORMAT_R4A4_UNORM: |
| 446 |
case PIPE_FORMAT_A4B4G4R4_UNORM: |
| 447 |
case PIPE_FORMAT_R4G4B4A4_UNORM: |
| 448 |
return pan_pack_unorm(b, unpacked, 4, 4, 4, 4); |
| 449 |
case PIPE_FORMAT_B5G5R5A1_UNORM: |
| 450 |
case PIPE_FORMAT_R5G5B5A1_UNORM: |
| 451 |
return pan_pack_unorm(b, unpacked, 5, 6, 5, 1); |
| 452 |
case PIPE_FORMAT_R5G6B5_UNORM: |
| 453 |
case PIPE_FORMAT_B5G6R5_UNORM: |
| 454 |
return pan_pack_unorm(b, unpacked, 5, 6, 5, 0); |
| 455 |
case PIPE_FORMAT_R10G10B10A2_UNORM: |
| 456 |
case PIPE_FORMAT_R10G10B10X2_UNORM: |
| 457 |
case PIPE_FORMAT_B10G10R10A2_UNORM: |
| 458 |
return pan_pack_unorm_1010102(b, unpacked); |
| 459 |
case PIPE_FORMAT_R10G10B10A2_UINT: |
| 460 |
case PIPE_FORMAT_B10G10R10A2_UINT: |
| 461 |
return pan_pack_int_1010102(b, unpacked, false); |
| 462 |
case PIPE_FORMAT_R10G10B10A2_SINT: |
| 463 |
case PIPE_FORMAT_R10G10B10X2_SINT: |
| 464 |
case PIPE_FORMAT_B10G10R10A2_SINT: |
| 465 |
return pan_pack_int_1010102(b, unpacked, true); |
| 466 |
case PIPE_FORMAT_R11G11B10_FLOAT: |
| 467 |
return pan_pack_r11g11b10(b, unpacked); |
| 468 |
default: |
| 469 |
break; |
| 470 |
} |
| 471 |
|
| 472 |
mesa_loge("%s\n", desc->name); |
| 473 |
UNREACHABLE("Unknown format"); |
| 474 |
} |
| 475 |
|
| 476 |
static void |
| 477 |
pan_lower_fb_store(nir_builder *b, nir_intrinsic_instr *intr, |
| 478 |
const struct util_format_description *desc, |
| 479 |
bool reorder_comps, unsigned nr_samples) |
| 480 |
{ |
| 481 |
/* For stores, add conversion before */ |
| 482 |
nir_def *unpacked = intr->src[0].ssa; |
| 483 |
unpacked = nir_pad_vec4(b, unpacked); |
| 484 |
|
| 485 |
/* Re-order the components */ |
| 486 |
if (reorder_comps) |
| 487 |
unpacked = pan_pack_reorder(b, desc, unpacked); |
| 488 |
|
| 489 |
nir_def *packed = pan_pack(b, desc, unpacked); |
| 490 |
|
| 491 |
/* We have to split writeout in 128 bit chunks */ |
| 492 |
unsigned iterations = DIV_ROUND_UP(desc->block.bits * nr_samples, 128); |
| 493 |
|
| 494 |
for (unsigned s = 0; s < iterations; ++s) { |
| 495 |
nir_store_raw_output_pan(b, packed, |
| 496 |
.io_semantics = nir_intrinsic_io_semantics(intr), |
| 497 |
.base = s); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
static nir_def * |
| 502 |
pan_sample_id(nir_builder *b, int sample) |
| 503 |
{ |
| 504 |
return (sample >= 0) ? nir_imm_int(b, sample) : nir_load_sample_id(b); |
| 505 |
} |
| 506 |
|
| 507 |
static void |
| 508 |
pan_lower_fb_load(nir_builder *b, nir_intrinsic_instr *intr, |
| 509 |
const struct util_format_description *desc, |
| 510 |
bool reorder_comps, int sample) |
| 511 |
{ |
| 512 |
nir_def *packed = |
| 513 |
nir_load_raw_output_pan(b, 4, 32, pan_sample_id(b, sample), |
| 514 |
.io_semantics = nir_intrinsic_io_semantics(intr)); |
| 515 |
|
| 516 |
/* Convert the raw value */ |
| 517 |
nir_def *unpacked = pan_unpack(b, desc, packed); |
| 518 |
|
| 519 |
/* Convert to the size of the load intrinsic. |
| 520 |
* |
| 521 |
* We can assume that the type will match with the framebuffer format: |
| 522 |
* |
| 523 |
* Page 170 of the PDF of the OpenGL ES 3.0.6 spec says: |
| 524 |
* |
| 525 |
* If [UNORM or SNORM, convert to fixed-point]; otherwise no type |
| 526 |
* conversion is applied. If the values written by the fragment shader |
| 527 |
* do not match the format(s) of the corresponding color buffer(s), |
| 528 |
* the result is undefined. |
| 529 |
*/ |
| 530 |
|
| 531 |
unsigned bits = intr->def.bit_size; |
| 532 |
|
| 533 |
nir_alu_type src_type = |
| 534 |
nir_alu_type_get_base_type(pan_unpacked_type_for_format(desc)); |
| 535 |
|
| 536 |
unpacked = nir_convert_to_bit_size(b, unpacked, src_type, bits); |
| 537 |
unpacked = nir_resize_vector(b, unpacked, intr->def.num_components); |
| 538 |
|
| 539 |
/* Reorder the components */ |
| 540 |
if (reorder_comps) |
| 541 |
unpacked = pan_unpack_reorder(b, desc, unpacked); |
| 542 |
|
| 543 |
nir_def_rewrite_uses_after_instr(&intr->def, unpacked, &intr->instr); |
| 544 |
} |
| 545 |
|
| 546 |
struct inputs { |
| 547 |
const enum pipe_format *rt_fmts; |
| 548 |
uint8_t raw_fmt_mask; |
| 549 |
bool is_blend; |
| 550 |
bool broken_ld_special; |
| 551 |
unsigned nr_samples; |
| 552 |
}; |
| 553 |
|
| 554 |
static bool |
| 555 |
lower(nir_builder *b, nir_instr *instr, void *data) |
| 556 |
{ |
| 557 |
struct inputs *inputs = data; |
| 558 |
if (instr->type != nir_instr_type_intrinsic) |
| 559 |
return false; |
| 560 |
|
| 561 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 562 |
bool is_load = intr->intrinsic == nir_intrinsic_load_output; |
| 563 |
bool is_store = intr->intrinsic == nir_intrinsic_store_output; |
| 564 |
|
| 565 |
if (!(is_load || (is_store && inputs->is_blend))) |
| 566 |
return false; |
| 567 |
|
| 568 |
nir_io_semantics sem = nir_intrinsic_io_semantics(intr); |
| 569 |
if (sem.location < FRAG_RESULT_DATA0) |
| 570 |
return false; |
| 571 |
|
| 572 |
unsigned rt = sem.location - FRAG_RESULT_DATA0; |
| 573 |
if (inputs->rt_fmts[rt] == PIPE_FORMAT_NONE) |
| 574 |
return false; |
| 575 |
|
| 576 |
const struct util_format_description *desc = |
| 577 |
util_format_description(inputs->rt_fmts[rt]); |
| 578 |
|
| 579 |
/* Don't lower */ |
| 580 |
if (pan_is_format_native(desc, inputs->broken_ld_special, is_store)) |
| 581 |
return false; |
| 582 |
|
| 583 |
/* EXT_shader_framebuffer_fetch requires per-sample loads. MSAA blend |
| 584 |
* shaders are not yet handled, so for now always load sample 0. |
| 585 |
*/ |
| 586 |
int sample = inputs->is_blend ? 0 : -1; |
| 587 |
bool reorder_comps = inputs->raw_fmt_mask & BITFIELD_BIT(rt); |
| 588 |
|
| 589 |
if (is_store) { |
| 590 |
b->cursor = nir_before_instr(instr); |
| 591 |
pan_lower_fb_store(b, intr, desc, reorder_comps, inputs->nr_samples); |
| 592 |
} else { |
| 593 |
b->cursor = nir_after_instr(instr); |
| 594 |
pan_lower_fb_load(b, intr, desc, reorder_comps, sample); |
| 595 |
} |
| 596 |
|
| 597 |
nir_instr_remove(instr); |
| 598 |
return true; |
| 599 |
} |
| 600 |
|
| 601 |
bool |
| 602 |
pan_nir_lower_framebuffer(nir_shader *shader, |
| 603 |
const enum pipe_format *rt_fmts, |
| 604 |
uint8_t raw_fmt_mask, |
| 605 |
unsigned blend_shader_nr_samples, |
| 606 |
bool broken_ld_special) |
| 607 |
{ |
| 608 |
assert(shader->info.stage == MESA_SHADER_FRAGMENT); |
| 609 |
|
| 610 |
return nir_shader_instructions_pass( |
| 611 |
shader, lower, nir_metadata_control_flow, |
| 612 |
&(struct inputs){ |
| 613 |
.rt_fmts = rt_fmts, |
| 614 |
.raw_fmt_mask = raw_fmt_mask, |
| 615 |
.nr_samples = blend_shader_nr_samples, |
| 616 |
.is_blend = blend_shader_nr_samples > 0, |
| 617 |
.broken_ld_special = broken_ld_special, |
| 618 |
}); |
| 619 |
} |