Skip to content
76 changes: 76 additions & 0 deletions src/imagination/g9p/g9p_a9_compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ a9_pack_image_store(nir_builder *b, nir_intrinsic_instr *in, void *data)
lo = nir_pack_half_2x16(b, nir_vec2(b, nir_channel(b, value, 0), nir_imm_float(b, 0)));
value = nir_vec2(b, lo, hi);
break;
/* integer channels of 8 and 16 bits: four 16-bit lanes in the two words, as the f16 formats above (the
* store converts a lane to the format's channel) */
case PIPE_FORMAT_R8G8B8A8_UINT: case PIPE_FORMAT_R8G8B8A8_SINT:
case PIPE_FORMAT_R16G16B16A16_UINT: case PIPE_FORMAT_R16G16B16A16_SINT:
hi = nir_ior(b, nir_iand_imm(b, nir_channel(b, value, 2), 0xffff),
nir_ishl_imm(b, nir_channel(b, value, 3), 16));
FALLTHROUGH;
case PIPE_FORMAT_R8G8_UINT: case PIPE_FORMAT_R8G8_SINT:
case PIPE_FORMAT_R16G16_UINT: case PIPE_FORMAT_R16G16_SINT:
lo = nir_ior(b, nir_iand_imm(b, nir_channel(b, value, 0), 0xffff),
nir_ishl_imm(b, nir_channel(b, value, 1), 16));
value = nir_vec2(b, lo, hi);
break;
case PIPE_FORMAT_R8_UINT: case PIPE_FORMAT_R8_SINT:
case PIPE_FORMAT_R16_UINT: case PIPE_FORMAT_R16_SINT:
lo = nir_iand_imm(b, nir_channel(b, value, 0), 0xffff);
value = nir_vec2(b, lo, hi);
break;
default:
return false; /* a9_image() records the format by name */
}
Expand Down Expand Up @@ -219,6 +237,63 @@ a9_spill_shared(nir_builder *b, nir_intrinsic_instr *in, void *data)
}


/* spread the low 16 bits of v to the even bit positions (pvr_arch_g5p_layout.c g5p_spread()) */
static nir_def *
a9_spread(nir_builder *b, nir_def *v)
{
v = nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 8)), 0x00ff00ff);
v = nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 4)), 0x0f0f0f0f);
v = nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 2)), 0x33333333);
return nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 1)), 0x55555555);
}

/* An image atomic on a 32-bit 2D, 2D-array or cube storage image: no A9 image atomic is known, so it is a
* device atomic on the texel's address, the image's base (G9P_IMAGE_BASE) + layer * layer stride + its
* rectangular Morton index * 4 (pvr_arch_g5p_layout.c g5p_twiddle2d(): y in bit 0 for the shorter side's
* bits, the rest of the longer side above), the Morton bits and the stride from G9P_IMAGE_INFO. */
static bool
a9_lower_image_atomic(nir_builder *b, nir_intrinsic_instr *in, void *data)
{
if (in->intrinsic != nir_intrinsic_image_deref_atomic && in->intrinsic != nir_intrinsic_image_deref_atomic_swap)
return false;
if (getenv("G5P_NO_IMAGE_ATOMIC"))
return false;
nir_deref_instr *deref = nir_src_as_deref(in->src[0]);
nir_variable *v = deref ? nir_deref_instr_get_variable(deref) : NULL;
const enum glsl_sampler_dim dim = nir_intrinsic_image_dim(in);
if (!v || deref->deref_type != nir_deref_type_var || v->data.binding >= 256 || v->data.descriptor_set >= 4 ||
in->def.bit_size != 32 || (dim != GLSL_SAMPLER_DIM_2D && dim != GLSL_SAMPLER_DIM_CUBE))
return false;
const enum pipe_format f = v->data.image.format;
if (f != PIPE_FORMAT_R32_UINT && f != PIPE_FORMAT_R32_SINT && f != PIPE_FORMAT_NONE)
return false;
b->cursor = nir_before_instr(&in->instr);
const unsigned binding = v->data.descriptor_set << 16 | v->data.binding;
nir_def *c = in->src[1].ssa;
nir_def *x = nir_channel(b, c, 0), *y = nir_channel(b, c, 1);
nir_def *layer = (nir_intrinsic_image_array(in) || dim == GLSL_SAMPLER_DIM_CUBE) ? nir_channel(b, c, 2)
: nir_imm_int(b, 0);
nir_def *info_idx = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, 0), .desc_set = G9P_IMAGE_INFO,
.binding = binding, .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
nir_def *info = nir_load_ssbo(b, 2, 32, info_idx, nir_imm_int(b, 0), .align_mul = 8, .align_offset = 0);
nir_def *bits = nir_channel(b, info, 0), *stride = nir_channel(b, info, 1);
nir_def *mask = nir_iadd_imm(b, nir_ishl(b, nir_imm_int(b, 1), bits), -1);
nir_def *morton = nir_ior(b, nir_ior(b, nir_ishl_imm(b, a9_spread(b, nir_iand(b, x, mask)), 1),
a9_spread(b, nir_iand(b, y, mask))),
nir_ishl(b, nir_iand(b, nir_ior(b, x, y), nir_inot(b, mask)), bits));
nir_def *offset = nir_iadd(b, nir_imul(b, layer, stride), nir_ishl_imm(b, morton, 2));
nir_def *base_idx = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, 0), .desc_set = G9P_IMAGE_BASE,
.binding = binding, .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
nir_def *r;
if (in->intrinsic == nir_intrinsic_image_deref_atomic_swap)
r = nir_ssbo_atomic_swap(b, 32, base_idx, offset, in->src[3].ssa, in->src[4].ssa,
.atomic_op = nir_intrinsic_atomic_op(in));
else
r = nir_ssbo_atomic(b, 32, base_idx, offset, in->src[3].ssa, .atomic_op = nir_intrinsic_atomic_op(in));
nir_def_replace(&in->def, r);
return true;
}

bool g9p_a9_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx)
{
nir->options = &nir_options_a9;
Expand All @@ -233,6 +308,7 @@ bool g9p_a9_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_c
nir_metadata_control_flow, NULL);
NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_pack_image_store,
nir_metadata_control_flow, NULL);
NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_lower_image_atomic, nir_metadata_none, NULL);
NIR_PASS(_, nir, nir_lower_alu);
/* lower_front() already allocated and lowered shared variables. Doing
* it again double-counts the allocation after addresses became explicit. */
Expand Down
55 changes: 44 additions & 11 deletions src/imagination/g9p/g9p_a9_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ static bool a9_image(struct g9p_ctx *ctx, nir_intrinsic_instr *in)
bool array = nir_intrinsic_image_array(in);
if (!ctx->out->a9 || ctx->stage != MESA_SHADER_COMPUTE)
return false;
if ((dim != GLSL_SAMPLER_DIM_2D && !(store && dim == GLSL_SAMPLER_DIM_CUBE)) ||
(!store && array)) {
/* a load takes the third coordinate (array layer, cube face) as a store does: the same image access with
* three coordinates (GLES3.1 image_load_store 2d_array / cube load_store and format_reinterpret pass) */
if (dim != GLSL_SAMPLER_DIM_2D && dim != GLSL_SAMPLER_DIM_CUBE) {
char why[128];
snprintf(why, sizeof(why), "image %s dimension %s is_array=%u unsupported",
store ? "store" : "load", (dim == GLSL_SAMPLER_DIM_CUBE ? "cube" : dim == GLSL_SAMPLER_DIM_3D ? "3D" :
Expand All @@ -249,7 +250,23 @@ static bool a9_image(struct g9p_ctx *ctx, nir_intrinsic_instr *in)
return false;
}
enum pipe_format format = v->data.image.format;
bool packed = false, supported = format == PIPE_FORMAT_R32_UINT;
/* a load reads the texel's 32-bit words as they are (four raw dwords): the 32-bit-channel formats */
bool packed = false, supported = format == PIPE_FORMAT_R32_UINT || format == PIPE_FORMAT_R32_SINT ||
format == PIPE_FORMAT_R32_FLOAT || format == PIPE_FORMAT_R32G32B32A32_UINT ||
format == PIPE_FORMAT_R32G32B32A32_SINT ||
format == PIPE_FORMAT_R32G32B32A32_FLOAT;
/* 8/16-bit channels: through the narrow class, two dwords of f16 (lower_image_packed_load() unpacks them) */
if (!store) switch (format) {
/* R8G8B8A8_UNORM reads back wrong this way (every channel off by up to 1.0): still refused */
case PIPE_FORMAT_R16G16B16A16_FLOAT: case PIPE_FORMAT_R16G16_FLOAT: case PIPE_FORMAT_R16_FLOAT:
supported = packed = in->def.num_components == 2; break;
case PIPE_FORMAT_R8G8B8A8_UINT: case PIPE_FORMAT_R8G8B8A8_SINT: case PIPE_FORMAT_R8G8_UINT:
case PIPE_FORMAT_R8G8_SINT: case PIPE_FORMAT_R8_UINT: case PIPE_FORMAT_R8_SINT:
case PIPE_FORMAT_R16G16B16A16_UINT: case PIPE_FORMAT_R16G16B16A16_SINT: case PIPE_FORMAT_R16G16_UINT:
case PIPE_FORMAT_R16G16_SINT: case PIPE_FORMAT_R16_UINT: case PIPE_FORMAT_R16_SINT:
supported = packed = in->def.num_components == 2 && !getenv("G5P_NO_INT_PACK"); break;
default: break;
}
if (store) switch (format) {
case PIPE_FORMAT_R32_UINT: case PIPE_FORMAT_R32_SINT: case PIPE_FORMAT_R32_FLOAT:
case PIPE_FORMAT_R32G32B32A32_UINT: case PIPE_FORMAT_R32G32B32A32_SINT:
Expand All @@ -258,6 +275,11 @@ static bool a9_image(struct g9p_ctx *ctx, nir_intrinsic_instr *in)
case PIPE_FORMAT_R8G8B8A8_UNORM: case PIPE_FORMAT_R16G16B16A16_FLOAT:
case PIPE_FORMAT_R16G16_FLOAT: case PIPE_FORMAT_R16_FLOAT:
supported = packed = true; break;
case PIPE_FORMAT_R8G8B8A8_UINT: case PIPE_FORMAT_R8G8B8A8_SINT: case PIPE_FORMAT_R8G8_UINT:
case PIPE_FORMAT_R8G8_SINT: case PIPE_FORMAT_R8_UINT: case PIPE_FORMAT_R8_SINT:
case PIPE_FORMAT_R16G16B16A16_UINT: case PIPE_FORMAT_R16G16B16A16_SINT: case PIPE_FORMAT_R16G16_UINT:
case PIPE_FORMAT_R16G16_SINT: case PIPE_FORMAT_R16_UINT: case PIPE_FORMAT_R16_SINT:
supported = packed = !getenv("G5P_NO_INT_PACK"); break;
default: break;
}
if (!supported) {
Expand All @@ -272,14 +294,25 @@ static bool a9_image(struct g9p_ctx *ctx, nir_intrinsic_instr *in)
a9_tex_refuse(ctx, "image access at a nonzero or dynamic level");
return false;
}
if (ctx->out->num_textures && (ctx->out->textures[0].set != v->data.descriptor_set ||
ctx->out->textures[0].binding != v->data.binding)) {
a9_tex_refuse(ctx, "more than one storage image binding");
return false;
/* storage image i's words are DMAed to common 40 + 8 * i (pvr_arch_g5p_compute.c): two fit below the
* device fence at 56; the second shares 48-55 with a second sampled texture (a9_tex_image_dw) */
unsigned image = 0;
while (image < ctx->out->num_textures && (ctx->out->textures[image].set != v->data.descriptor_set ||
ctx->out->textures[image].binding != v->data.binding))
image++;
if (image == ctx->out->num_textures) {
unsigned sampled = 0;
for (unsigned i = 0; i < ctx->out->a9_num_uploads; i++)
sampled += ctx->out->a9_uploads[i].kind == G9P_A9_UP_IMAGE;
if (image >= 2 || (image == 1 && sampled >= 2)) {
a9_tex_refuse(ctx, image >= 2 ? "more than two storage image bindings"
: "a second storage image with two sampled textures (both at common 48)");
return false;
}
ctx->out->textures[image].set = v->data.descriptor_set;
ctx->out->textures[image].binding = v->data.binding;
ctx->out->num_textures = image + 1;
}
ctx->out->num_textures = 1;
ctx->out->textures[0].set = v->data.descriptor_set;
ctx->out->textures[0].binding = v->data.binding;
unsigned coords = array || dim == GLSL_SAMPLER_DIM_CUBE ? 3 : 2;
int co = alloc_range(ctx, coords);
if (co < 0 || co + coords > 32) return false;
Expand All @@ -302,7 +335,7 @@ static bool a9_image(struct g9p_ctx *ctx, nir_intrinsic_instr *in)
g9p_alu2(ctx->c, G9P_OR, data + j, r, r, false);
}
} else if (in->def.bit_size != 32 || in->def.num_components > 4) return false;
g5p_image_access(ctx->c, store, packed, coords == 3, data, co, 40, 31);
g5p_image_access(ctx->c, store, packed, coords == 3, data, co, 40 + 8 * image, 31);
release(ctx, &in->instr);
if (!store) for (unsigned j = 0; j < 4; j++) {
ctx->reg[key(&in->def, j)] = data + j;
Expand Down
4 changes: 4 additions & 0 deletions src/imagination/g9p/g9p_a9_tex.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ a9_tex_upload(struct g9p_ctx *ctx, unsigned kind, unsigned set, unsigned binding
return u->common;
used += same_class;
}
if (kind == G9P_A9_UP_IMAGE && slots[used < nslots ? used : 0] == 48 && out->num_textures >= 2) {
a9_tex_refuse(ctx, "a second sampled texture with two storage images (both at common 48)");
return -1;
}
if (used == nslots || out->a9_num_uploads == G9P_A9_MAX_UPLOADS || set > 255 || binding > 255 ||
element > 255 || level > 255) {
a9_tex_refuse(ctx, kind == G9P_A9_UP_IMAGE ? "more than 2 sampled textures in a compute shader" :
Expand Down
94 changes: 94 additions & 0 deletions src/imagination/g9p/g9p_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5027,6 +5027,92 @@ g9p_lower_cubes_seamful(nir_shader *nir, bool mag_at_half)
* texelFetch as a sample of the texel's centre at its level (in float
* coordinates; the layer of an array stays a layer index)
*/
/*
* A9: a 1D image is a 2D one of height 1 (pvr_arch_g5p_tex.c: a 1D view is a
* 2D view, a 1D array view a 2D array view), so a 1D load or store takes
* y = 0, and a 1D array's layer moves from y to z.
*/
static bool
lower_image_1d(nir_builder *b, nir_intrinsic_instr *in, void *data)
{
switch (in->intrinsic) {
case nir_intrinsic_image_deref_load:
case nir_intrinsic_image_deref_store:
break;
default:
return false;
}
if (nir_intrinsic_image_dim(in) != GLSL_SAMPLER_DIM_1D)
return false;
b->cursor = nir_before_instr(&in->instr);
nir_def *c = in->src[1].ssa;
nir_def *zero = nir_imm_intN_t(b, 0, c->bit_size);
nir_def *comps[4] = { nir_channel(b, c, 0), zero, zero, zero };

if (nir_intrinsic_image_array(in))
comps[2] = nir_channel(b, c, 1);
nir_src_rewrite(&in->src[1], nir_vec(b, comps, c->num_components));
nir_intrinsic_set_image_dim(in, GLSL_SAMPLER_DIM_2D);
return true;
}

static nir_def *half_to_f32(nir_builder *b, nir_def *h);

/*
* A9 compute: a storage image load of a format with 16-bit float channels reads through the narrow sample
* class, which returns the four channels as f16 in two dwords (g5p_image_access(), packed): load those two
* dwords, then unpack each half (missing channels 0, 0, 0, 1 as a load gives).
*/
static bool
lower_image_packed_load(nir_builder *b, nir_intrinsic_instr *in, void *data)
{
if (in->intrinsic != nir_intrinsic_image_deref_load)
return false;
nir_variable *v = nir_deref_instr_get_variable(nir_src_as_deref(in->src[0]));
if (!v)
return false;
unsigned comps;
int integer = 0; /* 1: unsigned lanes, -1: signed lanes (the integer formats' packed load) */
switch (v->data.image.format) {
case PIPE_FORMAT_R16G16B16A16_FLOAT: comps = 4; break;
case PIPE_FORMAT_R16G16_FLOAT: comps = 2; break;
case PIPE_FORMAT_R16_FLOAT: comps = 1; break;
case PIPE_FORMAT_R16G16B16A16_UINT: case PIPE_FORMAT_R8G8B8A8_UINT: comps = 4; integer = 1; break;
case PIPE_FORMAT_R16G16_UINT: case PIPE_FORMAT_R8G8_UINT: comps = 2; integer = 1; break;
case PIPE_FORMAT_R16_UINT: case PIPE_FORMAT_R8_UINT: comps = 1; integer = 1; break;
case PIPE_FORMAT_R16G16B16A16_SINT: case PIPE_FORMAT_R8G8B8A8_SINT: comps = 4; integer = -1; break;
case PIPE_FORMAT_R16G16_SINT: case PIPE_FORMAT_R8G8_SINT: comps = 2; integer = -1; break;
case PIPE_FORMAT_R16_SINT: case PIPE_FORMAT_R8_SINT: comps = 1; integer = -1; break;
default: return false;
}
if (integer && getenv("G5P_NO_INT_PACK"))
return false;
if (in->def.bit_size != 32 || in->def.num_components == 2 && (in->instr.pass_flags & 1))
return false;
b->cursor = nir_after_instr(&in->instr);
const unsigned n = in->def.num_components;
in->def.num_components = 2;
in->num_components = 2;
in->instr.pass_flags |= 1; /* lowered: two raw dwords */
nir_intrinsic_set_dest_type(in, nir_type_uint32);
nir_def *lane[4] = {
nir_iand_imm(b, nir_channel(b, &in->def, 0), 0xffff), nir_ushr_imm(b, nir_channel(b, &in->def, 0), 16),
nir_iand_imm(b, nir_channel(b, &in->def, 1), 0xffff), nir_ushr_imm(b, nir_channel(b, &in->def, 1), 16),
};
nir_def *rgba[4];
for (unsigned i = 0; i < 4; i++) {
if (!integer)
rgba[i] = i < comps ? half_to_f32(b, lane[i]) : nir_imm_float(b, i == 3 ? 1.0f : 0.0f);
else if (i >= comps)
rgba[i] = nir_imm_int(b, i == 3 ? 1 : 0);
else
rgba[i] = integer < 0 ? nir_ishr_imm(b, nir_ishl_imm(b, lane[i], 16), 16) : lane[i];
}
nir_def *res = nir_vec(b, rgba, n);
nir_def_rewrite_uses_after(&in->def, res);
return true;
}

static bool
lower_txf(nir_builder *b, nir_tex_instr *tex, void *data)
{
Expand Down Expand Up @@ -5182,9 +5268,13 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage,
.lower_txs_lod = true,
.lower_txd = stage == MESA_SHADER_VERTEX,
.lower_txd_cb = lower_txd_to_level,
/* A9: 1D textures are 2D ones of height 1 (lower_image_1d()) */
.lower_1d = out->a9,
};

NIR_PASS(_, nir, nir_lower_tex, &tex);
if (out->a9)
NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_image_1d, nir_metadata_control_flow, NULL);
if (stage == MESA_SHADER_VERTEX) {
/* after the gradients became levels above */
const nir_lower_tex_options offsets = {
Expand All @@ -5199,6 +5289,10 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage,
/* A9 compute reads texels with the sampler's read form and the fetch sampler (g9p_a9_tex.h) */
if (!(out->a9 && stage == MESA_SHADER_COMPUTE))
NIR_PASS(_, nir, nir_shader_tex_pass, lower_txf, nir_metadata_control_flow, NULL);
if (out->a9 && stage == MESA_SHADER_COMPUTE) {
nir_shader_clear_pass_flags(nir);
NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_image_packed_load, nir_metadata_control_flow, NULL);
}
NIR_PASS(_, nir, nir_shader_instructions_pass, lower_half_packing, nir_metadata_control_flow,
(void *)(uintptr_t)(out->a9 && stage == MESA_SHADER_COMPUTE));
}
Expand Down
4 changes: 4 additions & 0 deletions src/imagination/g9p/g9p_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ g9p_fs_buf_base(unsigned n)
#define G9P_NUM_WORKGROUPS (~22u)
/* Device-backed workgroup memory, binding 0. */
#define G9P_SHARED_SPILL (~24u)
/* A9 image atomics (g9p_a9_compute.h): a storage image's base address, and the address of its
* {Morton bits, layer stride} (filled per dispatch); binding: set << 16 | binding (< 256) */
#define G9P_IMAGE_BASE (~25u)
#define G9P_IMAGE_INFO (~26u)
/* A draw's parameters: binding 0's slot holds firstInstance (filled per draw) */
#define G9P_DRAW_PARAMS (~23u)
/* The set of the push-constant slot (its address is loaded like a buffer's) */
Expand Down
Loading
Loading