diff --git a/src/imagination/g9p/g9p_a9_compute.h b/src/imagination/g9p/g9p_a9_compute.h index 56f282e758a..6d9feb1431a 100644 --- a/src/imagination/g9p/g9p_a9_compute.h +++ b/src/imagination/g9p/g9p_a9_compute.h @@ -303,7 +303,7 @@ bool g9p_a9_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_c memset(out, 0, sizeof(*out)); out->a9 = true; a9_last_refusal[0] = 0; - lower_front(nir); + lower_front(nir, false); NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_lower_subgroups_size1, nir_metadata_control_flow, NULL); NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_pack_image_store, diff --git a/src/imagination/g9p/g9p_cdm.h b/src/imagination/g9p/g9p_cdm.h index 97d2d6b7c88..ed57ba0b7bd 100644 --- a/src/imagination/g9p/g9p_cdm.h +++ b/src/imagination/g9p/g9p_cdm.h @@ -130,13 +130,18 @@ static inline struct g9p_cdm_record g9p_cdm_task(uint32_t k0, uint32_t data, uin /* * A DMA-only task loading constants into the USC common store (the buffer * binding; iPadOS's constants record). Word 6 = 0x40000001 as observed on - * every captured DMA task (low bit: Rogue's common-store allocation, open). + * every captured DMA task. Its low bits are the common store the kernels + * after it have, in blocks of 16 dwords (decoded + tested on the A10: with + * one block, kernels read other values from dword 16 on, and buffer + * addresses there lost the GPU; 16 blocks, 256 dwords, read right). + * @dwords is the store the kernels read. */ -static inline struct g9p_cdm_record g9p_cdm_dma_task(uint32_t k0, uint32_t data, uint32_t code) +static inline struct g9p_cdm_record g9p_cdm_dma_task(uint32_t k0, uint32_t data, uint32_t code, + uint32_t dwords) { struct g9p_cdm_record r = g9p_cdm_task(k0, data, G9P_CDM_K1_DMA, code); - r.w[6] |= 1; + r.w[6] |= dwords ? (dwords + 15) / 16 : 1; return r; } @@ -152,11 +157,39 @@ static inline struct g9p_cdm_record g9p_cdm_kernel(uint32_t k0, uint32_t data, u uint32_t threads, uint32_t tg_alloc) { struct g9p_cdm_record r = { { k0, data, code, gx - 1, gy - 1, gz - 1, - (threads % 32) << 27 | tg_alloc, (threads - 1) << 20 } }; + (threads < 32 ? threads : 0) << 27 | tg_alloc, (threads - 1) << 20 } }; return r; } +/* + * The threads a kernel record launches for a workgroup of @threads: past one + * task, whole tasks of 32, as the A9 launches them (g5p_cdm_workgroup_threads()). + * The program's prologue turns off the lanes past the workgroup (g9p_prologue() + * compares the index with the count in the common store). Measured on the A10 + * with 64 workgroups: launched as they were, groups past 128 invocations and 8 + * past a multiple of 16 (136, 152 ... 248) hung the GPU, and with registers + * above r31 so did 40, 56 and 72. As whole tasks every size tested ran. + */ +static inline uint32_t g9p_compute_hw_threads(uint32_t threads) +{ + return threads > 32 ? (threads + 31) & ~31u : threads; +} + +/* + * The most tasks of 32 a workgroup of programs of @regs registers may have. + * Measured on the A10 with 64 workgroups of whole tasks, registers in eights: + * 16 ran at 512 invocations, 24 at 256, 40 at 160 (192 hung one run in two), + * 48 and 64 at 128 (48 hung at 160), 72 to 96 at 64 (72 hung at 96). 32 is + * not measured and keeps the 192 invocations the earlier rule allowed. + */ +static inline unsigned g9p_compute_max_tasks(unsigned regs) +{ + const unsigned r = (regs + 7) & ~7u; + + return r <= 16 ? 16 : r <= 24 ? 8 : r <= 32 ? 6 : r <= 40 ? 5 : r <= 64 ? 4 : r <= 96 ? 2 : 1; +} + /* --- Internal programs and the end of a compute pass --------------------- */ /* diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 37a05f35858..6b2d6226301 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -28,6 +28,7 @@ #include "compiler/spirv/nir_spirv.h" #include "util/bitset.h" #include "util/ralloc.h" +#include "pvr_limits.h" #include "util/u_math.h" #include @@ -349,7 +350,8 @@ struct g9p_ctx { bool repeat_loads; /* an attempt after running out of registers */ bool point_size; /* a point pipeline's vertex shader: the size is the last output */ int psize_reg; - int vertex_reg, instance_reg; /* vertex shaders: r0-r1, or copies of them */ + /* vertex shaders: r0-r1, or copies of them; compute: the local index's copy, or r0 */ + int vertex_reg, instance_reg; bool reserve_st_low; /* the mask stack is r15, reserved (compile_back()) */ unsigned reserve_low; /* r11-r14 for address work, after running out (compile_back()) */ unsigned ip; @@ -1371,7 +1373,7 @@ only_tex_uses(nir_def *def) const nir_tex_instr *tex = nir_instr_as_tex(nir_src_use_instr(src)); if (tex->is_array || tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE || - tex->sampler_dim == GLSL_SAMPLER_DIM_3D || + tex->sampler_dim == GLSL_SAMPLER_DIM_3D || tex->op == nir_texop_txf_ms || tex->op == nir_texop_txl || tex->op == nir_texop_txb) return false; } @@ -1445,6 +1447,8 @@ emit_vec(struct g9p_ctx *ctx, nir_alu_instr *alu) return true; } #include "g9p_a9_alu.h" +static int shift_amount(struct g9p_ctx *ctx, nir_alu_instr *alu, int amount); + static bool emit_alu(struct g9p_ctx *ctx, nir_alu_instr *alu) { @@ -1585,6 +1589,11 @@ emit_alu(struct g9p_ctx *ctx, nir_alu_instr *alu) if (r[0] < 0) return false; } + if (alu->op == nir_op_ishl || alu->op == nir_op_ushr || alu->op == nir_op_ishr) { + r[1] = shift_amount(ctx, alu, r[1]); + if (r[1] < 0) + return false; + } release(ctx, &alu->instr); dst = alloc_reg(ctx); if (dst < 0) @@ -1623,6 +1632,27 @@ fs_dword(struct g9p_ctx *ctx, unsigned slot, unsigned dword) : fs_buffer_base(ctx, slot) + dword; } +/* + * A shift's amount, masked to 0-31 as NIR's shifts are: the hardware's shifts + * by 32 or more give 0 (measured: 1 << 0xfffffffe read 0, not 1 << 30). + * Called before the sources are released: its temporaries must not land on + * the value being shifted (textureSize's size >> lod read 1 for 40 >> 0). + */ +static int +shift_amount(struct g9p_ctx *ctx, nir_alu_instr *alu, int amount) +{ + int mask, t; + + if (nir_src_is_const(alu->src[1].src)) + return amount; + mask = const_reg(ctx, 31); + t = alloc_temp(ctx); + if (mask < 0 || t < 0) + return -1; + g9p_alu2(ctx->c, G9P_AND, t, amount, mask, false); + return t; +} + /* The op itself, sources in @r, destination @dst */ static bool emit_alu_op(struct g9p_ctx *ctx, nir_alu_instr *alu, const int *r, int dst) @@ -1655,10 +1685,11 @@ emit_alu_op(struct g9p_ctx *ctx, nir_alu_instr *alu, const int *r, int dst) if (a < 0) return false; /* the A8: a constant amount in Apple's literal form (g9p_shift_imm()) */ - if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) + if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) { g9p_shift_imm(ctx->c, dst, a, nir_alu_src_as_uint(alu->src[1]) & 31, false); - else + } else { g9p_shift(ctx->c, dst, a, r[1], false); + } return true; } case nir_op_ushr: { @@ -1667,10 +1698,11 @@ emit_alu_op(struct g9p_ctx *ctx, nir_alu_instr *alu, const int *r, int dst) if (a < 0) return false; /* the A8: a constant amount in Apple's literal form (g9p_shift_imm()) */ - if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) + if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) { g9p_shift_imm(ctx->c, dst, a, nir_alu_src_as_uint(alu->src[1]) & 31, true); - else + } else { g9p_shift(ctx->c, dst, a, r[1], true); + } return true; } case nir_op_ishr: @@ -2105,7 +2137,12 @@ emit_attrib_load(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) offset = (a->offset & ~3u) + 4 * nir_intrinsic_component(intr) + 16 * nir_src_as_uint(*offset_src); - /* the stride from its slot: the address form with 0 added (rs:rs+1) */ + /* + * The stride from its slot, the address form with 0 added: the stride in + * rs and the binding's last whole element in rs + 1 (buffer_addrs()). The + * element is clamped to that, since robust buffer access is advertised + * and a fetch far past the buffer faults the GPU. + */ zero = const_reg(ctx, 0); zero = zero < 0 ? zero : low_reg(ctx, zero); rs = alloc_pair(ctx); @@ -2114,12 +2151,16 @@ emit_attrib_load(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) return false; g9p_addr(ctx->c, rs, 2 * stride_slot, zero, 0, false); { - const int element = divided ? src_reg(ctx, nir_get_scalar(intr->src[0].ssa, 0)) - : a->per_instance ? ctx->instance_reg : ctx->vertex_reg; + int element = divided ? src_reg(ctx, nir_get_scalar(intr->src[0].ssa, 0)) + : a->per_instance ? ctx->instance_reg : ctx->vertex_reg; + int clamped; - if (element < 0) + element = element < 0 ? element : low_reg(ctx, element); + clamped = element < 0 ? element : alloc_low_temp(ctx); + if (clamped < 0) return false; - g9p_alu2(ctx->c, G9P_IMUL, t, element, rs, false); + emit_select(ctx, G9P_ULT, false, element, rs + 1, element, rs + 1, clamped); + g9p_alu2(ctx->c, G9P_IMUL, t, clamped, rs, false); } ctx->busy &= ~BITFIELD64_RANGE(rs, 2); if (offset) { @@ -2185,6 +2226,7 @@ emit_varying_load(struct g9p_ctx *ctx, nir_intrinsic_instr *intr, bool persp) gl_varying_slot_name_for_stage(sem.location, MESA_SHADER_FRAGMENT)); return false; } + persp = persp && !ctx->out->varying_flat[n]; ctx->out->num_varyings = MAX2(ctx->out->num_varyings, n + 1); ctx->out->varying_perspective[n] = persp; @@ -2686,17 +2728,25 @@ count_textures(nir_function_impl *impl) return n; } +/* Where the textures' words start in the common store (g9p_tex_state()) */ +static unsigned +tex_base(const struct g9p_ctx *ctx) +{ + return ctx->stage == MESA_SHADER_VERTEX ? G9P_VS_TEX_BASE + : ctx->stage == MESA_SHADER_COMPUTE ? G9P_CS_TEX_BASE + : 0; +} + static unsigned tex_state_dword(const struct g9p_ctx *ctx, unsigned slot) { - return g9p_tex_state(ctx->stage == MESA_SHADER_VERTEX ? G9P_VS_TEX_BASE : 0, slot); + return g9p_tex_state(tex_base(ctx), slot); } static unsigned tex_sampler_dword(const struct g9p_ctx *ctx, unsigned slot) { - return g9p_tex_sampler(ctx->stage == MESA_SHADER_VERTEX ? G9P_VS_TEX_BASE : 0, - ctx->tex_total, slot); + return g9p_tex_sampler(tex_base(ctx), ctx->tex_total, slot); } /* The varying component an SSA scalar is a plain load of, or -1 */ @@ -2870,16 +2920,20 @@ sample_lod(struct g9p_ctx *ctx, nir_tex_instr *tex, unsigned at, int lod) } /* - * The fragment shader samples a 2D array or a cube, whose coordinates take - * r0-r3 (emit_tex_layered()) + * The fragment shader samples a 2D array or a cube, or fetches from a + * multisampled texture, whose coordinates take r0-r3 (emit_tex_layered(), + * emit_txf_ms()) */ static bool uses_layered_tex(nir_function_impl *impl) { + const bool fragment = impl->function->shader->info.stage == MESA_SHADER_FRAGMENT; + nir_foreach_block(block, impl) { nir_foreach_instr(instr, block) { if (instr->type == nir_instr_type_tex && (nir_instr_as_tex(instr)->is_array || + (fragment && nir_instr_as_tex(instr)->op == nir_texop_txf_ms) || nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_CUBE || nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_3D)) return true; @@ -3113,6 +3167,96 @@ emit_tex_layered(struct g9p_ctx *ctx, nir_tex_instr *tex) return true; } +/* + * texelFetch of a multisampled texture: the texture unit's multisample form + * (g9p_pack_image_view()), dimension 4 in W0 and integer coordinates (W1 bit + * 6), the pixel in r0-r1 and the sample index in r2, as the background reads + * a multisampled target. r0-r3 are reserved in such shaders (generate()). + */ +static bool +emit_txf_ms(struct g9p_ctx *ctx, nir_tex_instr *tex) +{ + const int ci = nir_tex_instr_src_index(tex, nir_tex_src_coord); + const int si = nir_tex_instr_src_index(tex, nir_tex_src_ms_index); + const int slot = texture_slot(ctx, tex); + unsigned at; + int dst; + + if (ci < 0 || slot < 0 || tex->is_array) { + fprintf(stderr, "g9p: multisample fetches of 2D textures only\n"); + return false; + } + /* + * Vertex and compute shaders: the vertex form (g9p_sample_vertex()) at level 0, with + * dimension 4 and integer coordinates as above, the pixel in r[coord]-r[coord + 1] and + * the sample index in r[coord + 2]. Measured: the level operand is not read for these, + * a level of the sample index read the same values. + */ + if (ctx->stage != MESA_SHADER_FRAGMENT) { + int coord, lod; + + coord = alloc_low_range(ctx, 4); + if (coord < 0) + return false; + ctx->temps |= BITFIELD64_RANGE(coord, 4); + for (unsigned c = 0; c < 3; c++) { + const int r = c < 2 ? src_reg(ctx, nir_get_scalar(tex->src[ci].src.ssa, c)) + : si >= 0 ? src_reg(ctx, nir_get_scalar(tex->src[si].src.ssa, 0)) + : const_reg(ctx, 0); + + if (r < 0) + return false; + g9p_alu2(ctx->c, G9P_OR, coord + c, r, r, false); + } + lod = const_reg(ctx, 0); + lod = lod < 0 ? lod : low_reg(ctx, lod); + if (lod < 0) + return false; + dst = alloc_range(ctx, 4); + if (dst < 0) + return false; + at = ctx->c->n; + g9p_sample_vertex(ctx->c, dst, tex_state_dword(ctx, slot), tex_sampler_dword(ctx, slot), + coord, lod); + (*g9p_code_word(ctx->c, at)) = ((*g9p_code_word(ctx->c, at)) & ~0x0f00) | G9P_TEX_2D_MS << 8; + (*g9p_code_word(ctx->c, at + 1)) |= 0x40; + g9p_wait(ctx->c); + release(ctx, &tex->instr); + ctx->temps &= ~BITFIELD64_RANGE(coord, 4); + ctx->busy &= ~BITFIELD64_RANGE(coord, 4); + for (unsigned c = 0; c < 4; c++) { + ctx->reg[key(&tex->def, c)] = dst + c; + if (c >= tex->def.num_components || !ctx->last_use[key(&tex->def, c)]) + ctx->busy &= ~BITFIELD64_BIT(dst + c); + } + return true; + } + for (unsigned c = 0; c < 3; c++) { + const int r = c < 2 ? src_reg(ctx, nir_get_scalar(tex->src[ci].src.ssa, c)) + : si >= 0 ? src_reg(ctx, nir_get_scalar(tex->src[si].src.ssa, 0)) + : const_reg(ctx, 0); + + if (r < 0) + return false; + g9p_alu2(ctx->c, G9P_OR, c, r, r, false); + } + release(ctx, &tex->instr); + dst = alloc_range(ctx, 4); + if (dst < 0) + return false; + at = ctx->c->n; + G9P_EMIT(ctx->c, 0xf91a, g9p_sample_w1(dst, tex_state_dword(ctx, slot)) | 0x40, + 0x8100 | tex_sampler_dword(ctx, slot), 0x0700, 0x0200); + note_sample(ctx, tex, at); + g9p_wait(ctx->c); + for (unsigned c = 0; c < 4; c++) { + ctx->reg[key(&tex->def, c)] = dst + c; + if (c >= tex->def.num_components || !ctx->last_use[key(&tex->def, c)]) + ctx->busy &= ~BITFIELD64_BIT(dst + c); + } + return true; +} + /* * A vertex shader's sample: explicit level (level 0 without one), the * coordinates in registers of their own, as r0-r1 hold the vertex and instance @@ -3228,7 +3372,10 @@ emit_tex(struct g9p_ctx *ctx, nir_tex_instr *tex) if (tex->op == nir_texop_txs) return emit_txs(ctx, tex); - if (ctx->stage == MESA_SHADER_VERTEX) + if (tex->op == nir_texop_txf_ms) + return emit_txf_ms(ctx, tex); + /* compute samples as vertex shaders do: explicit levels, no derivatives */ + if (ctx->stage == MESA_SHADER_VERTEX || ctx->stage == MESA_SHADER_COMPUTE) return emit_tex_vertex(ctx, tex); if (tex->is_array || tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE || tex->sampler_dim == GLSL_SAMPLER_DIM_3D) @@ -3309,8 +3456,8 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) G9P_A9_INTRINSICS switch (intr->intrinsic) { case nir_intrinsic_load_local_invocation_index: - /* r0 at entry; reserved from the start when used */ - ctx->reg[key(&intr->def, 0)] = 0; + /* r0 at entry; reserved from the start when used, or its copy (generate()) */ + ctx->reg[key(&intr->def, 0)] = ctx->stage == MESA_SHADER_COMPUTE ? ctx->vertex_reg : 0; return true; case nir_intrinsic_load_workgroup_id: @@ -3393,9 +3540,44 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) return true; } - case nir_intrinsic_get_ssbo_size: { - /* compute: the size the binding DMA copies from the descriptor (pvr_arch_g9p.c) */ - const int slot = ctx->stage == MESA_SHADER_COMPUTE + case nir_intrinsic_ssbo_atomic_swap: { + /* + * Compare-and-swap, which has no device atomic of its own: the A9's + * loop (g5p_device_cas()), each lane in turn loading the word, + * comparing it and storing the new value + */ + if (ctx->stage != MESA_SHADER_COMPUTE || intr->def.bit_size != 32 || + nir_intrinsic_atomic_op(intr) != nir_atomic_op_cmpxchg) { + fprintf(stderr, "g9p: ssbo compare-and-swap in stage %d not supported yet\n", ctx->stage); + return false; + } + const int slot = buffer_slot(ctx, intr->src[0]); + const int off = src_reg(ctx, nir_get_scalar(intr->src[1].ssa, 0)); + const int cmp = src_reg(ctx, nir_get_scalar(intr->src[2].ssa, 0)); + const int val = src_reg(ctx, nir_get_scalar(intr->src[3].ssa, 0)); + const int pair = alloc_pair(ctx), i = alloc_temp(ctx), dst = alloc_reg(ctx); + + if (slot < 0 || off < 0 || cmp < 0 || val < 0 || pair < 0 || i < 0 || dst < 0 || + pair > 30 || dst > 31 || i > 31) + return false; + ctx->temps |= BITFIELD64_RANGE(pair, 2); + g9p_addr(ctx->c, pair, 2 * slot, off, 0, false); + g5p_device_cas(ctx->c, pair, dst, cmp, val, i, ctx->st); + release(ctx, &intr->instr); + ctx->reg[key(&intr->def, 0)] = dst; + ctx->busy &= ~ctx->temps; + ctx->temps = 0; + return true; + } + + case nir_intrinsic_get_ssbo_size: + case nir_intrinsic_get_ubo_size: { + /* + * The size from the descriptor: compute's binding DMA copies it + * (pvr_arch_g9p.c), and a draw fills the vertex shader's slot + * (buffer_addrs()) + */ + const int slot = ctx->stage == MESA_SHADER_COMPUTE || ctx->stage == MESA_SHADER_VERTEX ? buffer_slot_flags(ctx, intr->src[0], G9P_BINDING_SIZE) : -1; int zero = const_reg(ctx, 0), r; @@ -3621,7 +3803,11 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) case nir_intrinsic_load_input: if (ctx->stage == MESA_SHADER_VERTEX) return emit_attrib_load(ctx, intr); - return emit_varying_load(ctx, intr, true); + { + const unsigned n = nir_intrinsic_io_semantics(intr).location - VARYING_SLOT_VAR0; + + return emit_varying_load(ctx, intr, n >= G9P_MAX_VARYINGS || !ctx->out->varying_linear[n]); + } case nir_intrinsic_load_frag_coord: return emit_frag_coord(ctx, intr); case nir_intrinsic_load_point_coord: @@ -4157,6 +4343,28 @@ generate(struct g9p_ctx *ctx, nir_function_impl *impl, unsigned st) g9p_alu2(ctx->c, G9P_OR, ctx->vertex_reg, 0, 0, false); g9p_alu2(ctx->c, G9P_OR, ctx->instance_reg, 1, 1, false); } + /* + * A10 compute takes the vertex form's samples (emit_tex_vertex()), whose cubes use r0-r3 + * too: the local index, r0 at entry, then in a copy + */ + if (ctx->stage == MESA_SHADER_COMPUTE && !ctx->out->a9 && uses_layered_tex(impl)) { + bool local_index = false; + + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) + local_index |= instr->type == nir_instr_type_intrinsic && + nir_instr_as_intrinsic(instr)->intrinsic == + nir_intrinsic_load_local_invocation_index; + } + ctx->busy |= BITFIELD64_RANGE(0, 4), ctx->pinned |= BITFIELD64_RANGE(0, 4); + if (local_index) { + ctx->vertex_reg = alloc_reg(ctx); + if (ctx->vertex_reg < 0) + return false; + ctx->pinned |= BITFIELD64_BIT(ctx->vertex_reg); + g9p_alu2(ctx->c, G9P_OR, ctx->vertex_reg, 0, 0, false); + } + } /* compute: r0 holds the local index from the start when it is used. Pinned: every * load_local_invocation_index reads r0 without code, so r0 must not be reallocated after @@ -4603,14 +4811,83 @@ lower_fb_fetch_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data) comps[i] = nir_bcsel(b, low, nir_imm_int(b, 0), nir_bcsel(b, nir_uge_imm(b, h, 0x3c00), nir_imm_int(b, 0x3f800000), bits)); + /* R5G6B5: the level lower_rgb565_output() wrote, as the stored value */ + if (out->targets[read_target].rgb565 && k < 3) { + const unsigned scale = k == 1 ? 64 : 32; + + comps[i] = nir_fmul_imm(b, nir_fmin(b, nir_ffloor(b, nir_fmul_imm(b, comps[i], scale)), + nir_imm_float(b, scale - 1)), + 1.0 / (scale - 1)); + } } nir_def_replace(&intr->def, nir_vec(b, comps, intr->def.num_components)); return true; } -/* One function, globals as locals; buffers and push constants lowered */ +/* + * R5G6B5 targets: the PBE stores floor(v * 2^bits) of each channel (measured: a + * 0-255 ramp blitted from RGBA8 took the top bits, a level low for 52 of 256 + * values in 5 bits and 42 in 6). The shader writes the middle of the level + * nearest to v instead, (round(v * m) + 0.5) / 2^bits, which that keeps. + */ +static bool +lower_rgb565_output(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + const struct g9p_target *targets = data; + + if (intr->intrinsic != nir_intrinsic_store_output) + return false; + const nir_io_semantics sem = nir_intrinsic_io_semantics(intr); + const unsigned t = sem.location == FRAG_RESULT_COLOR ? 0 : sem.location - FRAG_RESULT_DATA0; + + if ((sem.location != FRAG_RESULT_COLOR && sem.location < FRAG_RESULT_DATA0) || + t >= G9P_MAX_TARGETS || !targets[t].rgb565 || intr->src[0].ssa->bit_size != 32) + return false; + b->cursor = nir_before_instr(&intr->instr); + nir_def *v = intr->src[0].ssa; + nir_def *comps[4]; + + for (unsigned i = 0; i < v->num_components; i++) { + const unsigned k = nir_intrinsic_component(intr) + i; + const unsigned scale = k == 1 ? 64 : 32; + + comps[i] = nir_channel(b, v, i); + if (k < 3) { + nir_def *level = nir_ffloor(b, nir_fadd_imm(b, nir_fmul_imm(b, nir_fsat(b, comps[i]), scale - 1), 0.5)); + + comps[i] = nir_fmul_imm(b, nir_fadd_imm(b, level, 0.5), 1.0 / scale); + } + } + nir_src_rewrite(&intr->src[0], nir_vec(b, comps, v->num_components)); + return true; +} + +/* + * The buffer accesses robust access bounds: every storage buffer access, + * and the uniform buffer loads at an offset only known when the shader runs + */ +static bool +robust_access_filter(const nir_intrinsic_instr *intr, const void *data) +{ + switch (intr->intrinsic) { + case nir_intrinsic_load_ssbo: + case nir_intrinsic_store_ssbo: + case nir_intrinsic_ssbo_atomic: + case nir_intrinsic_ssbo_atomic_swap: + return true; + case nir_intrinsic_load_ubo: + return !nir_src_is_const(intr->src[1]); + default: + return false; + } +} + +/* + * One function, globals as locals; buffers and push constants lowered, + * bounded with @robust + */ static void -lower_front(nir_shader *nir) +lower_front(nir_shader *nir, bool robust) { NIR_PASS(_, nir, nir_lower_variable_initializers, nir_var_function_temp); NIR_PASS(_, nir, nir_lower_returns); @@ -4641,6 +4918,17 @@ lower_front(nir_shader *nir) nir_metadata_control_flow, NULL); NIR_PASS(_, nir, nir_shader_intrinsics_pass, zero_descriptor_offset, nir_metadata_control_flow, NULL); + /* + * Robust buffer access: the memory accesses of A10 compute and vertex + * shaders stay inside their buffers (loads past the end read its start, + * stores and atomics there are dropped), since far past a buffer they + * fault the GPU. Fragment shaders read their uniform buffers from the + * common store. + */ + if (robust) { + NIR_PASS(_, nir, nir_opt_constant_folding); + NIR_PASS(_, nir, nir_lower_robust_access, robust_access_filter, NULL); + } } /* @@ -4677,6 +4965,53 @@ lower_io(nir_shader *nir) NIR_PASS(_, nir, nir_shader_intrinsics_pass, clamp_frag_depth, nir_metadata_control_flow, NULL); } +/* + * A discard at four samples keeps only sample 0 of the pixels it lets through + * (measured: a conditional discard never taken left a quarter of every pixel, + * at either sample rate, also with the full mask written). So a multisampled + * shader's discards become its coverage: a mask of 0 where one is taken, and + * the shader runs on. Not a shader that writes memory, whose stores and + * atomics a discarded pixel must not make. + */ +bool +g9p_folds_discards(const nir_shader *nir) +{ + bool discards = false; + + nir_foreach_function_impl (impl, nir) { + nir_foreach_block (block, impl) { + nir_foreach_instr (instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + switch (nir_instr_as_intrinsic(instr)->intrinsic) { + case nir_intrinsic_terminate: + case nir_intrinsic_terminate_if: + case nir_intrinsic_demote: + case nir_intrinsic_demote_if: + discards = true; + break; + case nir_intrinsic_store_ssbo: + case nir_intrinsic_ssbo_atomic: + case nir_intrinsic_ssbo_atomic_swap: + case nir_intrinsic_store_global: + case nir_intrinsic_global_atomic: + case nir_intrinsic_global_atomic_swap: + case nir_intrinsic_image_deref_store: + case nir_intrinsic_image_deref_atomic: + case nir_intrinsic_image_deref_atomic_swap: + case nir_intrinsic_image_store: + case nir_intrinsic_image_atomic: + case nir_intrinsic_image_atomic_swap: + return false; + default: + break; + } + } + } + } + return discards; +} + /* * Sample coverage and alpha to coverage: what the fragment writes as its mask, the word * `6104 half` reads (metal-tt: Apple's alpha to coverage is this mask computed in the @@ -4686,9 +5021,37 @@ static void lower_coverage(nir_shader *nir, const struct g9p_coverage *coverage) { nir_function_impl *impl = nir_shader_get_entrypoint(nir); - nir_builder b = nir_builder_at(nir_after_impl(impl)); + nir_builder b = nir_builder_at(nir_before_impl(impl)); nir_def *alpha = NULL; + nir_variable *kept = NULL; + if (g9p_folds_discards(nir)) { + kept = nir_local_variable_create(impl, glsl_bool_type(), "kept"); + nir_store_var(&b, kept, nir_imm_true(&b), 1); + nir_foreach_block_safe (block, impl) { + nir_foreach_instr_safe (instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + + b.cursor = nir_before_instr(instr); + switch (intr->intrinsic) { + case nir_intrinsic_demote: + case nir_intrinsic_terminate: + nir_store_var(&b, kept, nir_imm_false(&b), 1); + break; + case nir_intrinsic_demote_if: + case nir_intrinsic_terminate_if: + nir_store_var(&b, kept, nir_iand(&b, nir_load_var(&b, kept), nir_inot(&b, intr->src[0].ssa)), 1); + break; + default: + continue; + } + nir_instr_remove(instr); + } + } + } + b.cursor = nir_after_impl(impl); nir_foreach_block (block, impl) { nir_foreach_instr (instr, block) { if (instr->type != nir_instr_type_intrinsic) @@ -4714,11 +5077,28 @@ lower_coverage(nir_shader *nir, const struct g9p_coverage *coverage) if (alpha) { nir_def *enabled = nir_load_ubo(&b, 1, 32, index, nir_imm_int(&b, 4), .align_mul = 4, .range = 8); - nir_def *n = nir_f2u32(&b, nir_fadd_imm(&b, nir_fmul_imm(&b, nir_fsat(&b, alpha), 4.0f), 0.5f)); - nir_def *by_alpha = nir_iadd_imm(&b, nir_ishl(&b, nir_imm_int(&b, 1), n), -1); + const float count = coverage->samples == 2 || coverage->samples == 8 ? coverage->samples : 4; + nir_def *n = nir_f2u32(&b, nir_fadd_imm(&b, nir_fmul_imm(&b, nir_fsat(&b, alpha), count), 0.5f)); + /* + * a nested set by the count kept. 4x: samples 0, 3, 1 and 2 in turn, so two + * kept are a diagonal pair, as the GLES driver kept them. 8x: the centre + * ones first, samples 0, 1, 5, 2, 3, 6, 7 and 4 (Vulkan's positions). 2x: + * sample 0, then 1. + */ + static const uint32_t nested2[] = { 0x0, 0x1, 0x3 }; + static const uint32_t nested4[] = { 0x0, 0x1, 0x9, 0xb, 0xf }; + static const uint32_t nested8[] = { 0x00, 0x01, 0x03, 0x23, 0x27, 0x2f, 0x6f, 0xef, 0xff }; + const unsigned samples = coverage->samples == 2 || coverage->samples == 8 ? coverage->samples : 4; + const uint32_t *nested = samples == 2 ? nested2 : samples == 8 ? nested8 : nested4; + nir_def *by_alpha = nir_imm_int(&b, nested[samples]); + + for (int k = samples - 1; k >= 0; k--) + by_alpha = nir_bcsel(&b, nir_ieq_imm(&b, n, k), nir_imm_int(&b, nested[k]), by_alpha); mask = nir_iand(&b, mask, nir_bcsel(&b, nir_ine_imm(&b, enabled, 0), by_alpha, nir_imm_int(&b, ~0))); } + if (kept) + mask = nir_bcsel(&b, nir_load_var(&b, kept), mask, nir_imm_int(&b, 0)); nir_store_output(&b, mask, nir_imm_int(&b, 0), .base = FRAG_RESULT_SAMPLE_MASK, .src_type = nir_type_uint32, .io_semantics.location = FRAG_RESULT_SAMPLE_MASK, .io_semantics.num_slots = 1); @@ -4733,6 +5113,12 @@ static thread_local bool g9p_no_schedule; * 100 registers with homes on the first attempt, 4 instances, geometry 10x slower) */ static thread_local bool g9p_no_homes; +/* + * A10 compute whose registers are too many for its workgroup in bank 1: values in bank 1 + * below this many (the mask stack after them), the rest parked in bank 0 as A9 compute + * does (g9p_compile_compute_bank0()); 0: everything in bank 1 + */ +static thread_local unsigned g9p_bank1_cap; /* vertex shaders: attribute loads moved next to their uses (g9p_compile_vertex() tries both) */ static thread_local bool g9p_move_inputs; @@ -4825,6 +5211,9 @@ rematerialize_loads(nir_shader *nir, bool every_use) intr->intrinsic != nir_intrinsic_load_interpolated_input && intr->intrinsic != nir_intrinsic_load_frag_coord && intr->intrinsic != nir_intrinsic_load_point_coord && + /* compute: the dispatch's words, read again where used (lower_task_barrier()) */ + intr->intrinsic != nir_intrinsic_load_workgroup_id && + intr->intrinsic != nir_intrinsic_load_num_workgroups && !(intr->intrinsic == nir_intrinsic_load_input && (nir->info.stage == MESA_SHADER_FRAGMENT || every_use))) continue; @@ -4861,27 +5250,68 @@ rematerialize_loads(nir_shader *nir, bool every_use) * operands with a fixed index: a load at a computed offset becomes a chain of * selects over the offsets it can have (GL's uniform arrays indexed in a loop) */ +/* A dword of a fragment shader's buffer at a constant offset (the common store) */ +static nir_def * +ubo_dword(nir_builder *b, nir_intrinsic_instr *intr, unsigned at) +{ + return nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, at), .align_mul = 4, + .align_offset = 0, .range_base = nir_intrinsic_range_base(intr), + .range = nir_intrinsic_range(intr)); +} + +/* + * Entries @lo .. @lo + @size - 1 of @count, at @first + @step x entry, + * chosen by the bits of @j below @size: a binary tree built depth first, so + * a few partial results are live at once. Entries past @count read entry 0. + */ +static nir_def * +ubo_select_tree(nir_builder *b, nir_intrinsic_instr *intr, nir_def *j, unsigned lo, + unsigned size, unsigned count, unsigned first, unsigned step) +{ + if (size == 1) + return ubo_dword(b, intr, first + step * (lo < count ? lo : 0)); + + nir_def *low = ubo_select_tree(b, intr, j, lo, size / 2, count, first, step); + nir_def *high = ubo_select_tree(b, intr, j, lo + size / 2, size / 2, count, first, step); + + return nir_bcsel(b, nir_ine_imm(b, nir_iand_imm(b, j, size / 2), 0), high, low); +} + +/* + * A fragment shader reads its buffers from the common store, as uniform + * operands with a fixed index: a load at a computed offset becomes a + * selection among the dwords it can reach, those its alignment allows + * (one component of a vec4 array: every fourth), by the bits of its entry + * number. An offset past them reads the same component of the buffer's + * first vec4, as robust access gives the vector loads of other stages. + */ static bool lower_dynamic_ubo_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data) { - nir_def *value; - unsigned n; - if (intr->intrinsic != nir_intrinsic_load_ubo || nir_src_is_const(intr->src[1]) || intr->def.num_components != 1 || intr->def.bit_size != 32) return false; - n = MIN2(nir_intrinsic_range(intr) / 4, G9P_FS_BUF_DWORDS); + + const unsigned end = nir_intrinsic_range_base(intr) + + 4 * MIN2(nir_intrinsic_range(intr) / 4, G9P_FS_BUF_DWORDS); + const unsigned step = MAX2(nir_intrinsic_align_mul(intr), 4); + const unsigned phase = nir_intrinsic_align_offset(intr) % step; + /* the first reachable dword at or after the range's start */ + const unsigned first = phase + ALIGN_POT(MAX2(nir_intrinsic_range_base(intr), phase) - phase, step); + const unsigned count = end > first ? DIV_ROUND_UP(end - first, step) : 1; + b->cursor = nir_before_instr(&intr->instr); - value = nir_imm_int(b, 0); - for (unsigned k = 0; k < n; k++) { - const unsigned at = nir_intrinsic_range_base(intr) + 4 * k; - nir_def *load = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, at), - .align_mul = 4, .align_offset = 0, - .range_base = nir_intrinsic_range_base(intr), - .range = nir_intrinsic_range(intr)); + /* the entry: an offset below the first wraps past count */ + nir_def *j = nir_udiv_imm(b, nir_iadd_imm(b, intr->src[1].ssa, -(int)first), step); + nir_def *value = ubo_select_tree(b, intr, j, 0, util_next_power_of_two(count), count, first, step); - value = nir_bcsel(b, nir_ieq_imm(b, intr->src[1].ssa, at), load, value); - } + /* past them: the same component of the first vec4, whatever the alignment says */ + nir_def *oob = ubo_dword(b, intr, nir_intrinsic_range_base(intr)); + + for (unsigned c = 1; c < 4; c++) + oob = nir_bcsel(b, nir_ieq_imm(b, nir_iand_imm(b, intr->src[1].ssa, 12), 4 * c), + ubo_dword(b, intr, nir_intrinsic_range_base(intr) + 4 * c), oob); + value = nir_bcsel(b, nir_ult_imm(b, j, count), value, oob); nir_def_replace(&intr->def, value); return true; } @@ -5115,37 +5545,115 @@ lower_image_packed_load(nir_builder *b, nir_intrinsic_instr *in, void *data) return true; } +/* + * Framebuffer fetch through input attachments (VK_KHR_dynamic_rendering_local_read, + * Zink's fbfetch): a subpass load of input attachment i reads colour attachment + * i from the tile, as blending does (lower_fb_fetch_instr()); @data: the outputs' + * bases by colour attachment + */ static bool -lower_txf(nir_builder *b, nir_tex_instr *tex, void *data) +lower_subpass_load(nir_builder *b, nir_intrinsic_instr *intr, void *data) { - /* - * A four-sample texture reads as a 2D one of twice the size, each pixel's samples - * its 2x2 block with the index in Morton order, y in bit 0 (g9p_pack_image_view()) - */ - if (tex->op == nir_texop_txs && tex->sampler_dim == GLSL_SAMPLER_DIM_MS) { - b->cursor = nir_after_instr(&tex->instr); - nir_def *half = nir_ushr_imm(b, &tex->def, 1); + const int *base = data; - nir_def_rewrite_uses_after(&tex->def, half); - tex->sampler_dim = GLSL_SAMPLER_DIM_2D; + if (intr->intrinsic != nir_intrinsic_image_deref_input_attachment_load || + nir_intrinsic_image_dim(intr) != GLSL_SAMPLER_DIM_SUBPASS) + return false; + /* the input attachment index; the identity map to colour attachments */ + const unsigned i = nir_intrinsic_base(intr); + + b->cursor = nir_before_instr(&intr->instr); + if (i >= G9P_MAX_TARGETS || base[i] < 0) { + nir_def_replace(&intr->def, nir_imm_zero(b, intr->def.num_components, intr->def.bit_size)); return true; } - if (tex->op == nir_texop_txf_ms) { - const int coord_i = nir_tex_instr_src_index(tex, nir_tex_src_coord); - const int ms_i = nir_tex_instr_src_index(tex, nir_tex_src_ms_index); + nir_def *v = nir_load_output(b, 4, 32, nir_imm_int(b, 0), .base = base[i], .component = 0, + .dest_type = nir_intrinsic_dest_type(intr), + .io_semantics.location = FRAG_RESULT_DATA0 + i, + .io_semantics.num_slots = 1); - if (coord_i < 0 || ms_i < 0) - return false; - b->cursor = nir_before_instr(&tex->instr); - nir_def *c = tex->src[coord_i].src.ssa, *s = tex->src[ms_i].src.ssa; - nir_def *x = nir_iadd(b, nir_ishl_imm(b, nir_channel(b, c, 0), 1), nir_ushr_imm(b, s, 1)); - nir_def *y = nir_iadd(b, nir_ishl_imm(b, nir_channel(b, c, 1), 1), nir_iand_imm(b, s, 1)); + nir_def_replace(&intr->def, nir_trim_vector(b, v, intr->def.num_components)); + b->shader->info.fs.uses_fbfetch_output = true; + return true; +} - nir_tex_instr_remove_src(tex, ms_i); - nir_src_rewrite(&tex->src[nir_tex_instr_src_index(tex, nir_tex_src_coord)].src, nir_vec2(b, x, y)); - tex->op = nir_texop_txf; - tex->sampler_dim = GLSL_SAMPLER_DIM_2D; +/* + * textureGather (tg4) of 2D textures and arrays: the four texels of the + * bilinear footprint, each sampled at its centre at level 0, in the order + * (i0, j1), (i1, j1), (i1, j0), (i0, j0). The sampler's wrap applies to + * texels outside the level, a linear filter at a texel's centre reads that + * texel alone, and a shadow sampler compares each one. + */ +static bool +lower_tg4(nir_builder *b, nir_tex_instr *tex, void *data) +{ + static const int corner[4][2] = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 0, 0 } }; + const int ci = nir_tex_instr_src_index(tex, nir_tex_src_coord); + const int oi = nir_tex_instr_src_index(tex, nir_tex_src_offset); + + if (tex->op != nir_texop_tg4 || tex->sampler_dim != GLSL_SAMPLER_DIM_2D || ci < 0) + return false; + b->cursor = nir_before_instr(&tex->instr); + + nir_def *coord = tex->src[ci].src.ssa; + nir_def *size = nir_i2f32(b, nir_get_texture_size(b, tex)); + nir_def *x = nir_fadd_imm(b, nir_fmul(b, nir_channel(b, coord, 0), nir_channel(b, size, 0)), -0.5); + nir_def *y = nir_fadd_imm(b, nir_fmul(b, nir_channel(b, coord, 1), nir_channel(b, size, 1)), -0.5); + nir_def *i0 = nir_ffloor(b, x), *j0 = nir_ffloor(b, y); + nir_def *texel[4]; + + if (oi >= 0) { + nir_def *off = nir_i2f32(b, tex->src[oi].src.ssa); + + i0 = nir_fadd(b, i0, nir_channel(b, off, 0)); + j0 = nir_fadd(b, j0, nir_channel(b, off, 1)); } + for (unsigned k = 0; k < 4; k++) { + int di = corner[k][0], dj = corner[k][1]; + unsigned n = 0; + + if (nir_tex_instr_has_explicit_tg4_offsets(tex)) { + /* textureGatherOffsets: each texel's own footprint, its (i0, j0) */ + di = tex->tg4_offsets[k][0]; + dj = tex->tg4_offsets[k][1]; + } + + nir_def *cx = nir_fdiv(b, nir_fadd_imm(b, i0, di + 0.5), nir_channel(b, size, 0)); + nir_def *cy = nir_fdiv(b, nir_fadd_imm(b, j0, dj + 0.5), nir_channel(b, size, 1)); + nir_def *comps[3] = { cx, cy, tex->is_array ? nir_channel(b, coord, 2) : NULL }; + nir_tex_instr *s = nir_tex_instr_create(b->shader, tex->num_srcs + 1); + + for (unsigned i = 0; i < tex->num_srcs; i++) { + if (tex->src[i].src_type == nir_tex_src_coord || tex->src[i].src_type == nir_tex_src_offset) + continue; + s->src[n].src_type = tex->src[i].src_type; + s->src[n++].src = nir_src_for_ssa(tex->src[i].src.ssa); + } + s->src[n].src_type = nir_tex_src_coord; + s->src[n++].src = nir_src_for_ssa(nir_vec(b, comps, tex->is_array ? 3 : 2)); + s->src[n].src_type = nir_tex_src_lod; + s->src[n++].src = nir_src_for_ssa(nir_imm_float(b, 0.0f)); + s->num_srcs = n; + s->op = nir_texop_txl; + s->sampler_dim = tex->sampler_dim; + s->is_array = tex->is_array; + s->is_shadow = tex->is_shadow; + s->coord_components = tex->is_array ? 3 : 2; + s->dest_type = tex->dest_type; + s->texture_index = tex->texture_index; + s->sampler_index = tex->sampler_index; + nir_def_init(&s->instr, &s->def, 4, tex->def.bit_size); + nir_builder_instr_insert(b, &s->instr); + texel[k] = nir_channel(b, &s->def, tex->is_shadow ? 0 : tex->component); + } + nir_def_replace(&tex->def, nir_vec(b, texel, 4)); + return true; +} + +static bool +lower_txf(nir_builder *b, nir_tex_instr *tex, void *data) +{ + /* a multisampled texture's fetch and size: emit_txf_ms() and its state's own size */ const int ci = nir_tex_instr_src_index(tex, nir_tex_src_coord); const int li = nir_tex_instr_src_index(tex, nir_tex_src_lod); const unsigned n = tex->coord_components - tex->is_array; @@ -5156,21 +5664,33 @@ lower_txf(nir_builder *b, nir_tex_instr *tex, void *data) b->cursor = nir_before_instr(&tex->instr); lod = li >= 0 ? tex->src[li].src.ssa : nir_imm_int(b, 0); size = nir_get_texture_size(b, tex); - coord = nir_i2f32(b, tex->src[ci].src.ssa); + /* + * Robust image access: a texel outside the level reads 0, where the + * sample would wrap or clamp to one inside (Zink bounds the level itself) + */ + nir_def *icoord = tex->src[ci].src.ssa, *inside = nir_imm_true(b); + coord = nir_i2f32(b, icoord); for (unsigned c = 0; c < n; c++) { nir_def *level_size = nir_imax_imm(b, nir_ishr(b, nir_channel(b, size, c), lod), 1); + inside = nir_iand(b, inside, nir_ult(b, nir_channel(b, icoord, c), level_size)); comps[c] = nir_fdiv(b, nir_fadd_imm(b, nir_channel(b, coord, c), 0.5), nir_i2f32(b, level_size)); } - if (tex->is_array) + if (tex->is_array) { + inside = nir_iand(b, inside, nir_ult(b, nir_channel(b, icoord, n), nir_channel(b, size, n))); comps[n] = nir_channel(b, coord, n); + } nir_src_rewrite(&tex->src[ci].src, nir_vec(b, comps, tex->coord_components)); if (li >= 0) nir_src_rewrite(&tex->src[li].src, nir_i2f32(b, lod)); else nir_tex_instr_add_src(tex, nir_tex_src_lod, nir_imm_float(b, 0.0f)); tex->op = nir_texop_txl; + b->cursor = nir_after_instr(&tex->instr); + nir_def *robust = nir_bcsel(b, inside, &tex->def, nir_imm_zero(b, tex->def.num_components, + tex->def.bit_size)); + nir_def_rewrite_uses_after(&tex->def, robust); return true; } @@ -5265,10 +5785,12 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, /* texture2DProj */ { + if (!out->a9) + NIR_PASS(_, nir, nir_shader_tex_pass, lower_tg4, nir_metadata_control_flow, NULL); const nir_lower_tex_options tex = { .lower_txp = ~0u, .lower_txs_lod = true, - .lower_txd = stage == MESA_SHADER_VERTEX, + .lower_txd = stage == MESA_SHADER_VERTEX || (stage == MESA_SHADER_COMPUTE && !out->a9), .lower_txd_cb = lower_txd_to_level, /* A9: 1D textures are 2D ones of height 1 (lower_image_1d()) */ .lower_1d = out->a9, @@ -5277,7 +5799,7 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, 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) { + if (stage == MESA_SHADER_VERTEX || (stage == MESA_SHADER_COMPUTE && !out->a9)) { /* after the gradients became levels above */ const nir_lower_tex_options offsets = { .lower_offset_filter = vertex_compare_offset, @@ -5298,6 +5820,12 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, NIR_PASS(_, nir, nir_shader_instructions_pass, lower_half_packing, nir_metadata_control_flow, (void *)(uintptr_t)(out->a9 && stage == MESA_SHADER_COMPUTE)); } + /* + * gl_PointSize in the advertised range: the hardware's size field is 9 + * bits (measured: 512 drew nothing and 1000 drew 488 pixels wide) + */ + if (stage == MESA_SHADER_VERTEX) + NIR_PASS(_, nir, nir_lower_point_size, PVR_POINT_SIZE_RANGE_MIN, PVR_POINT_SIZE_RANGE_MAX); /* integer division through floats: no such instruction (GLSL ES 1.00 int and ivec /) */ { const nir_lower_idiv_options idiv = { .allow_fp16 = false }; @@ -5321,6 +5849,8 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, optimize(nir); } if (stage == MESA_SHADER_FRAGMENT) { + /* the alignments the offsets prove: the dwords a computed offset can reach */ + NIR_PASS(_, nir, nir_opt_load_store_update_alignments); NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_dynamic_ubo_instr, nir_metadata_control_flow, NULL); optimize(nir); @@ -5384,13 +5914,15 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, ctx.c->w = ctx.c->initial_w; ctx.c->capacity = G9P_INITIAL_CODE_WORDS; ctx.c->fragment = stage == MESA_SHADER_FRAGMENT; - ctx.c->high_bank0 = stage == MESA_SHADER_COMPUTE && out->a9; + ctx.c->high_bank0 = stage == MESA_SHADER_COMPUTE && (out->a9 || g9p_bank1_cap); ctx.c->a9_stack = stage == MESA_SHADER_COMPUTE && out->a9; /* A9 compute: values in bank 1 up to the workgroup's limit, the mask stack after them; the rest in * bank-0 homes (g9p_a9_bank1_limit()) */ if (stage == MESA_SHADER_COMPUTE && out->a9) ctx.value_cap = g9p_a9_bank1_limit(MAX2(out->local_size[0], 1) * MAX2(out->local_size[1], 1) * MAX2(out->local_size[2], 1)) - 1; + else if (stage == MESA_SHADER_COMPUTE && g9p_bank1_cap) + ctx.value_cap = g9p_bank1_cap - 1; ctx.reg = ralloc_array(mem_ctx, int8_t, impl->ssa_alloc * 4); ctx.uni = ralloc_array(mem_ctx, int16_t, impl->ssa_alloc * 4); memset(ctx.uni, -1, impl->ssa_alloc * 4 * sizeof(*ctx.uni)); @@ -5522,9 +6054,13 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, ctx.reserve_st_low = true; continue; } - /* A9 compute parks values above its bank-1 limit in bank 0. */ - if (!ctx.oom || (stage == MESA_SHADER_COMPUTE && !(out->a9)) || - g9p_no_homes) + /* + * A9 compute parks values above its bank-1 limit in bank 0. A10 compute parks them + * above r31 as graphics code does: measured up to r95 (93 registers) at 64 + * invocations. The pipeline refuses workgroups too large for the registers + * (g9p_compute_max_tasks()). + */ + if (!ctx.oom || g9p_no_homes) return backend_failure(out, &ctx); stuck = ctx.oom_ip == last_oom_ip ? stuck + 1 : 0; last_oom_ip = ctx.oom_ip; @@ -5790,6 +6326,403 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, return true; } +static nir_def *f32_to_half(nir_builder *b, nir_def *f); + +/* + * A storage image format whose channels are R, G, B, A in order, each + * 8, 16 or 32 bits wide: what the texel words of lower_storage_image() hold + */ +static const struct util_format_description * +storage_format(enum pipe_format fmt) +{ + const struct util_format_description *desc = util_format_description(fmt); + const unsigned bits = desc ? util_format_get_blocksizebits(fmt) : 0; + + if (!desc || desc->layout != UTIL_FORMAT_LAYOUT_PLAIN || (bits != 32 && bits != 64 && bits != 128)) + return NULL; + for (unsigned i = 0; i < desc->nr_channels; i++) { + const struct util_format_channel_description *ch = &desc->channel[i]; + + if (desc->swizzle[i] != i || (ch->size != 8 && ch->size != 16 && ch->size != 32) || + (ch->type == UTIL_FORMAT_TYPE_FLOAT && ch->size == 8)) + return NULL; + } + return desc; +} + +/* + * A texel's words from the shader's value, in 32-bit integer code (the + * backend has no 16-bit values, which nir_format_pack_rgba() would give): + * normalized channels rounded, integer channels clamped to their range + */ +static nir_def * +storage_pack(nir_builder *b, const struct util_format_description *desc, nir_def *value) +{ + nir_def *word[4] = { NULL }; + + for (unsigned i = 0; i < desc->nr_channels; i++) { + const struct util_format_channel_description *ch = &desc->channel[i]; + nir_def *v = nir_channel(b, value, i), *bits; + const unsigned n = ch->size; + + if (ch->type == UTIL_FORMAT_TYPE_FLOAT) { + bits = n == 32 ? v : f32_to_half(b, v); + } else if (ch->normalized && ch->type == UTIL_FORMAT_TYPE_UNSIGNED) { + const float m = (float)BITFIELD_MASK(n); + + bits = nir_f2u32(b, nir_ffloor(b, nir_fadd_imm(b, nir_fmul_imm(b, nir_fsat(b, v), m), 0.5))); + } else if (ch->normalized) { + const float m = (float)BITFIELD_MASK(n - 1); + nir_def *c = nir_fmin(b, nir_fmax(b, v, nir_imm_float(b, -1.0f)), nir_imm_float(b, 1.0f)); + + bits = nir_f2i32(b, nir_fround_even(b, nir_fmul_imm(b, c, m))); + } else if (ch->type == UTIL_FORMAT_TYPE_UNSIGNED) { + bits = n == 32 ? v : nir_umin(b, v, nir_imm_int(b, BITFIELD_MASK(n))); + } else { + bits = n == 32 ? v + : nir_imin(b, nir_imax(b, v, nir_imm_int(b, -(1 << (n - 1)))), + nir_imm_int(b, BITFIELD_MASK(n - 1))); + } + if (n < 32) + bits = nir_ishl_imm(b, nir_iand_imm(b, bits, BITFIELD_MASK(n)), ch->shift % 32); + word[ch->shift / 32] = word[ch->shift / 32] ? nir_ior(b, word[ch->shift / 32], bits) : bits; + } + for (unsigned i = 0; i < util_format_get_blocksizebits(desc->format) / 32; i++) { + if (!word[i]) + word[i] = nir_imm_int(b, 0); + } + return nir_vec(b, word, util_format_get_blocksizebits(desc->format) / 32); +} + +/* The shader's value of a texel's words: storage_pack() backwards, 0, 0, 0, 1 where missing */ +static nir_def * +storage_unpack(nir_builder *b, const struct util_format_description *desc, nir_def *words) +{ + const bool is_int = desc->channel[0].type != UTIL_FORMAT_TYPE_FLOAT && !desc->channel[0].normalized; + nir_def *rgba[4]; + + for (unsigned i = 0; i < 4; i++) { + const struct util_format_channel_description *ch = &desc->channel[i]; + + if (i >= desc->nr_channels) { + rgba[i] = i == 3 ? (is_int ? nir_imm_int(b, 1) : nir_imm_float(b, 1.0f)) : nir_imm_int(b, 0); + continue; + } + + const unsigned n = ch->size; + nir_def *w = nir_channel(b, words, ch->shift / 32); + nir_def *u = n == 32 ? w : nir_ubitfield_extract_imm(b, w, ch->shift % 32, n); + nir_def *sx = n == 32 ? w : nir_ibitfield_extract_imm(b, w, ch->shift % 32, n); + + if (ch->type == UTIL_FORMAT_TYPE_FLOAT) + rgba[i] = n == 32 ? w : half_to_f32(b, u); + else if (ch->normalized && ch->type == UTIL_FORMAT_TYPE_UNSIGNED) + rgba[i] = nir_fmul_imm(b, nir_u2f32(b, u), 1.0f / BITFIELD_MASK(n)); + else if (ch->normalized) + rgba[i] = nir_fmax(b, nir_fmul_imm(b, nir_i2f32(b, sx), 1.0f / BITFIELD_MASK(n - 1)), + nir_imm_float(b, -1.0f)); + else + rgba[i] = ch->type == UTIL_FORMAT_TYPE_UNSIGNED ? u : sx; + } + return nir_vec(b, rgba, 4); +} + +/* The low 16 bits of @v spread to the even bits */ +static nir_def * +spread_bits(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); +} + +/* + * A10 compute storage images, as memory: each access reads the view's + * record from its descriptor (struct g9p_storage_image, at the slot of + * G9P_BINDING_DESCRIPTOR), then loads, stores or an atomic on the texel's + * raw words in the format the shader declares (storage_pack()). + * Outside the view a store or atomic is dropped and a load reads 0, as + * robust image access asks. @data: set when an access cannot be lowered. + */ +static bool +lower_storage_image(nir_builder *b, nir_intrinsic_instr *in, void *data) +{ + bool *failed = data; + + switch (in->intrinsic) { + case nir_intrinsic_image_deref_load: + case nir_intrinsic_image_deref_store: + case nir_intrinsic_image_deref_size: + case nir_intrinsic_image_deref_samples: + case nir_intrinsic_image_deref_atomic: + case nir_intrinsic_image_deref_atomic_swap: + break; + default: + return false; + } + nir_deref_instr *deref = nir_src_as_deref(in->src[0]); + unsigned element = 0; + + if (deref && deref->deref_type == nir_deref_type_array) { + if (!nir_src_is_const(deref->arr.index)) { + *failed = true; + return false; + } + element = nir_src_as_uint(deref->arr.index); + deref = nir_deref_instr_parent(deref); + } + nir_variable *var = deref && deref->deref_type == nir_deref_type_var ? deref->var : NULL; + + if (!var || var->data.binding >= BITFIELD_BIT(G9P_BINDING_ELEMENT_SHIFT)) { + *failed = true; + return false; + } + b->cursor = nir_before_instr(&in->instr); + if (in->intrinsic == nir_intrinsic_image_deref_samples) { + nir_def_replace(&in->def, nir_imm_int(b, 1)); + return true; + } + + const enum glsl_sampler_dim dim = nir_intrinsic_image_dim(in); + const bool array = nir_intrinsic_image_array(in); + nir_def *rec = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, element), + .desc_set = var->data.descriptor_set, + .binding = var->data.binding | G9P_BINDING_DESCRIPTOR, + .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); + nir_def *word[8]; + + /* as uniform loads, which rematerialize_loads() repeats where they are used */ + for (unsigned i = 2; i < 8; i++) + word[i] = nir_load_ubo(b, 1, 32, rec, nir_imm_int(b, 4 * i), .align_mul = 4, + .align_offset = 0, .range_base = 0, + .range = sizeof(struct g9p_storage_image)); + + nir_def *tbits = word[2], *row_bytes = word[3], *stride = word[4]; + nir_def *w = word[5], *h = word[6], *n = word[7]; + + if (in->intrinsic == nir_intrinsic_image_deref_size) { + nir_def *size[3]; + unsigned k = 0; + + size[k++] = w; + if (dim != GLSL_SAMPLER_DIM_1D) + size[k++] = h; + if (dim == GLSL_SAMPLER_DIM_3D || array) + size[k++] = dim == GLSL_SAMPLER_DIM_CUBE ? nir_udiv_imm(b, n, 6) : n; + while (k < in->def.num_components) + size[k++] = nir_imm_int(b, 1); + nir_def_replace(&in->def, nir_u2uN(b, nir_vec(b, size, in->def.num_components), + in->def.bit_size)); + return true; + } + + enum pipe_format fmt = nir_intrinsic_format(in); + + if (fmt == PIPE_FORMAT_NONE) + fmt = var->data.image.format; + const struct util_format_description *desc = + fmt == PIPE_FORMAT_NONE ? NULL : storage_format(fmt); + + if (!desc) { + *failed = true; + return false; + } + const unsigned bits = util_format_get_blocksizebits(fmt), words = bits / 32; + + nir_def *c = in->src[1].ssa; + nir_def *x = nir_channel(b, c, 0); + nir_def *y = dim == GLSL_SAMPLER_DIM_1D ? nir_imm_int(b, 0) : nir_channel(b, c, 1); + /* layers (3D slices, cube faces): only when there are any, for fewer live values */ + const bool layered = array || dim == GLSL_SAMPLER_DIM_3D || dim == GLSL_SAMPLER_DIM_CUBE; + nir_def *z = dim == GLSL_SAMPLER_DIM_1D && array ? nir_channel(b, c, 1) : nir_channel(b, c, 2); + nir_def *inside = nir_iand(b, nir_ult(b, x, w), nir_ult(b, y, h)); + + if (layered) + inside = nir_iand(b, inside, nir_ult(b, z, n)); + nir_def *mask = nir_iadd_imm(b, nir_ishl(b, nir_imm_int(b, 1), tbits), -1); + nir_def *col = nir_ior(b, nir_ishl_imm(b, spread_bits(b, nir_iand(b, x, mask)), 1), + nir_ishl(b, nir_iand(b, x, nir_inot(b, mask)), tbits)); + nir_def *row = nir_ior(b, spread_bits(b, nir_iand(b, y, mask)), + nir_ishl(b, nir_iand(b, y, nir_inot(b, mask)), tbits)); + nir_def *off = nir_iadd(b, nir_imul_imm(b, col, bits / 8), nir_imul(b, row, row_bytes)); + nir_def *texels = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, element), + .desc_set = var->data.descriptor_set, + .binding = var->data.binding, + .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); + + if (layered) + off = nir_iadd(b, off, nir_imul(b, z, stride)); + /* before the ifs: a phi must come first in the block after one */ + nir_def *zero = nir_imm_int(b, 0); + + if (in->intrinsic == nir_intrinsic_image_deref_atomic || + in->intrinsic == nir_intrinsic_image_deref_atomic_swap) { + nir_def *r; + + if (bits != 32 || in->def.bit_size != 32) { + *failed = true; + return false; + } + /* + * Outside the view, the first texel with an operand that changes + * nothing (the result is undefined there): no branch, whose phi would + * hold a register for the whole program. Exchange has none. + */ + const nir_atomic_op op = nir_intrinsic_atomic_op(in); + nir_def *data = in->src[3].ssa, *keep; + + switch (op) { + case nir_atomic_op_iadd: case nir_atomic_op_ior: case nir_atomic_op_ixor: + case nir_atomic_op_umax: + keep = nir_imm_int(b, 0); + break; + case nir_atomic_op_iand: case nir_atomic_op_umin: + keep = nir_imm_int(b, ~0); + break; + case nir_atomic_op_imin: + keep = nir_imm_int(b, INT32_MAX); + break; + case nir_atomic_op_imax: + keep = nir_imm_int(b, INT32_MIN); + break; + default: + keep = NULL; + break; + } + if (in->intrinsic == nir_intrinsic_image_deref_atomic_swap) { + /* storing the compared value back changes nothing */ + r = nir_ssbo_atomic_swap(b, 32, texels, nir_bcsel(b, inside, off, zero), data, + nir_bcsel(b, inside, in->src[4].ssa, data), .atomic_op = op); + } else if (keep) { + r = nir_ssbo_atomic(b, 32, texels, nir_bcsel(b, inside, off, zero), + nir_bcsel(b, inside, data, keep), .atomic_op = op); + } else { + nir_push_if(b, inside); + r = nir_ssbo_atomic(b, 32, texels, off, data, .atomic_op = op); + nir_pop_if(b, NULL); + r = nir_if_phi(b, r, zero); + } + nir_def_replace(&in->def, r); + return true; + } + if (in->intrinsic == nir_intrinsic_image_deref_store) { + nir_def *raw = storage_pack(b, desc, in->src[3].ssa); + + nir_push_if(b, inside); + for (unsigned i = 0; i < words; i++) + nir_store_ssbo(b, nir_channel(b, raw, i), texels, nir_iadd_imm(b, off, 4 * i), + .write_mask = 1, .align_mul = 4, .align_offset = 0); + nir_pop_if(b, NULL); + nir_instr_remove(&in->instr); + return true; + } + + /* + * A load outside reads the first texel and gives 0: no branch, whose phis + * would hold registers for the whole program + */ + nir_def *raw[4]; + + off = nir_bcsel(b, inside, off, zero); + for (unsigned i = 0; i < words; i++) + raw[i] = nir_bcsel(b, inside, + nir_load_ssbo(b, 1, 32, texels, nir_iadd_imm(b, off, 4 * i), + .align_mul = 4, .align_offset = 0), + zero); + + nir_def *rgba = storage_unpack(b, desc, nir_vec(b, raw, words)); + + if (rgba->bit_size != in->def.bit_size) + rgba = nir_u2uN(b, rgba, in->def.bit_size); + nir_def_replace(&in->def, nir_trim_vector(b, rgba, in->def.num_components)); + return true; +} + +static bool a9_spill_shared(nir_builder *b, nir_intrinsic_instr *in, void *data); + +/* + * A workgroup barrier of a group of more than one task. The tasks of a group + * run apart, so the barrier the hardware does (a wait for memory) left + * eglcompute's sums through shared memory wrong from 128 invocations up. As + * the GLES driver did it, with coherent atomics on a word of the device's + * counters (G9P_BARRIER_COUNTERS): lane 0 of each task adds 1, whose low byte + * counts the tasks in. The last one adds 256 - tasks, which sets the count + * back to 0 and advances the generation above it, and the others wait for the + * generation to change. A task's lanes run in step, so they wait with their + * lane 0. One word, so that the barrier keeps few registers. Words by + * workgroup modulo G9P_BARRIER_SLOTS: so many groups are never resident at + * once. + */ +static bool +lower_task_barrier(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + const unsigned *tasks = data; + + if (intr->intrinsic != nir_intrinsic_barrier || + nir_intrinsic_execution_scope(intr) != SCOPE_WORKGROUP) + return false; + b->cursor = nir_after_instr(&intr->instr); + nir_def *index = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, 0), + .desc_set = G9P_BARRIER_COUNTERS, .binding = 0, + .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); + nir_def *lane0 = nir_ieq_imm(b, nir_iand_imm(b, nir_load_local_invocation_index(b), 31), 0); + + nir_push_if(b, lane0); + { + nir_def *id = nir_load_workgroup_id(b); + nir_def *groups = nir_load_num_workgroups(b); + nir_def *flat = nir_iadd(b, nir_channel(b, id, 0), + nir_imul(b, nir_channel(b, groups, 0), + nir_iadd(b, nir_channel(b, id, 1), + nir_imul(b, nir_channel(b, groups, 1), + nir_channel(b, id, 2))))); + nir_def *word = nir_ishl_imm(b, nir_iand_imm(b, flat, G9P_BARRIER_SLOTS - 1), 2); + nir_def *old = nir_ssbo_atomic(b, 32, index, word, nir_imm_int(b, 1), + .atomic_op = nir_atomic_op_iadd); + + nir_push_if(b, nir_ieq_imm(b, nir_iand_imm(b, old, 0xff), *tasks - 1)); + { + nir_ssbo_atomic(b, 32, index, word, nir_imm_int(b, 256 - *tasks), + .atomic_op = nir_atomic_op_iadd); + } + nir_push_else(b, NULL); + { + nir_def *generation = nir_ushr_imm(b, old, 8); + nir_loop *loop = nir_push_loop(b); + { + nir_def *now = nir_ssbo_atomic(b, 32, index, word, nir_imm_int(b, 0), + .atomic_op = nir_atomic_op_iadd); + + nir_break_if(b, nir_ine(b, nir_ushr_imm(b, now, 8), generation)); + } + nir_pop_loop(b, loop); + } + nir_pop_if(b, NULL); + } + nir_pop_if(b, NULL); + /* the memory the other tasks wrote before the barrier, after it */ + nir_barrier(b, .execution_scope = SCOPE_NONE, .memory_scope = SCOPE_WORKGROUP, + .memory_semantics = NIR_MEMORY_ACQ_REL, .memory_modes = nir_intrinsic_memory_modes(intr)); + return true; +} + +/* + * A10: the same with at most @bank1 registers in bank 1, the kernel record's register field; + * the values past them in bank 0, which the kick's DOUTU temps (out->num_regs) allocate. + * out->a9_bank1_regs: the bank-1 count. + */ +bool +g9p_compile_compute_bank0(nir_shader *nir, struct g9p_shader *out, void *mem_ctx, unsigned bank1) +{ + bool ok; + + g9p_bank1_cap = bank1; + ok = g9p_compile_compute(nir, out, mem_ctx); + g9p_bank1_cap = 0; + return ok; +} + bool g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) { @@ -5799,7 +6732,48 @@ g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) }; memset(out, 0, sizeof(*out)); - lower_front(nir); + lower_front(nir, true); + { + bool failed = false; + + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_storage_image, nir_metadata_none, + &failed); + if (failed) + return g9p_fail(out, "a storage image access without a format of RGBA channels of 8, 16 " + "or 32 bits, or of an image array by a computed index"); + NIR_PASS(_, nir, nir_opt_dce); + NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_image, NULL); + } + /* + * Atomics on shared variables: the A10 has none for its shared memory, so such a + * shader keeps its shared variables in memory, a region per workgroup, where the + * storage-buffer atomics work (a9_spill_shared(), as the A9's go past its size) + */ + { + bool shared_atomics = false; + + nir_foreach_function_impl (impl, nir) + nir_foreach_block (block, impl) + nir_foreach_instr (instr, block) + if (instr->type == nir_instr_type_intrinsic && + (nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_shared_atomic || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_shared_atomic_swap)) + shared_atomics = true; + if (shared_atomics && nir->info.shared_size) { + out->shared_spill_stride = ALIGN_POT(nir->info.shared_size, 64); + NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_spill_shared, nir_metadata_control_flow, + &out->shared_spill_stride); + nir->info.shared_size = 0; + } + } + { + const unsigned threads = nir->info.workgroup_size[0] * nir->info.workgroup_size[1] * + nir->info.workgroup_size[2]; + unsigned tasks = DIV_ROUND_UP(threads, 32); + + if (tasks > 1 && !getenv("G9P_NO_TASK_BARRIER")) + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_task_barrier, nir_metadata_none, &tasks); + } NIR_PASS(_, nir, nir_lower_compute_system_values, &csv); return compile_back(nir, out, MESA_SHADER_COMPUTE, NULL, 0, false, false, mem_ctx, false, false); } @@ -6133,7 +7107,7 @@ g9p_compile_vertex(nir_shader *nir, const struct g9p_vertex_attrib *attribs, NIR_PASS(_, nir, nir_fixup_deref_modes); NIR_PASS(_, nir, nir_lower_vars_to_ssa); NIR_PASS(_, nir, nir_opt_dce); - lower_front(nir); + lower_front(nir, true); if (frag_coord_slot >= 0) add_frag_coord_output(nir, frag_coord_slot, point_coord); lower_io(nir); @@ -6245,17 +7219,33 @@ discards_to_if(nir_shader *nir) * Discards left inside control flow (nested, or with more around them): * each sets a flag instead, and one discard_if on it ends the shader. The * shader runs on for such pixels, which only matters for side effects - * (none in the shaders seen so far). + * (none in the shaders seen so far). A shader with more than one discard + * folds every one of them into the flag: two hardware discards in a shader + * kill pixels where neither condition holds (measured: an opaque, + * depth-tested draw whose shader had a uniform's discard and a texture's, + * neither taken, lost about 90% of its pixels in random tiles; either one + * alone drew every pixel). */ static bool discards_to_flag(nir_shader *nir) { nir_function_impl *impl = nir_shader_get_entrypoint(nir); nir_variable *flag = NULL; + unsigned num_discards = 0; nir_builder b; + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_intrinsic && + (nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_demote || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_terminate || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_demote_if || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_terminate_if)) + num_discards++; + } + } nir_foreach_block_safe(block, impl) { - if (block->cf_node.parent->type == nir_cf_node_function) + if (block->cf_node.parent->type == nir_cf_node_function && num_discards < 2) continue; nir_foreach_instr_safe(instr, block) { nir_intrinsic_instr *intr; @@ -6306,8 +7296,8 @@ static bool discards_to_select(nir_shader *nir, bool zero) { nir_function_impl *impl = nir_shader_get_entrypoint(nir); - nir_intrinsic_instr *discards[16], *stores[8]; - unsigned num_discards = 0, num_stores = 0; + nir_intrinsic_instr *discards[16], *stores[32], *writes[32]; + unsigned num_discards = 0, num_stores = 0, num_writes = 0; nir_def *cond = NULL; nir_builder b; @@ -6336,6 +7326,12 @@ discards_to_select(nir_shader *nir, bool zero) return nir_no_progress(impl); stores[num_stores++] = intr; break; + case nir_intrinsic_store_ssbo: + /* a target past what a pixel holds (lower_overflow_target()) */ + if (!top || num_writes == ARRAY_SIZE(writes)) + return nir_no_progress(impl); + writes[num_writes++] = intr; + break; default: break; } @@ -6376,6 +7372,15 @@ discards_to_select(nir_shader *nir, bool zero) .io_semantics = nir_intrinsic_io_semantics(st)); nir_instr_remove(&st->instr); } + /* a discarded pixel writes no memory either: those stores at the end, where it is kept */ + if (num_writes) { + nir_push_if(&b, nir_inot(&b, cond)); + for (unsigned i = 0; i < num_writes; i++) { + nir_instr_remove(&writes[i]->instr); + nir_builder_instr_insert(&b, &writes[i]->instr); + } + nir_pop_if(&b, NULL); + } return nir_progress(true, impl, nir_metadata_none); } @@ -6461,18 +7466,27 @@ lower_centroid(nir_builder *b, nir_intrinsic_instr *intr, void *data) return true; } -/* Flat inputs, by varying: NIR's loads do not say */ +/* + * Flat and noperspective inputs, by varying: NIR's loads do not say (they are + * load_input, without a barycentric whose mode perspective() could read) + */ static void find_flat_varyings(nir_shader *nir, struct g9p_shader *out) { nir_foreach_variable_with_modes(var, nir, nir_var_shader_in) { - if (var->data.interpolation != INTERP_MODE_FLAT || var->data.location < VARYING_SLOT_VAR0) + if ((var->data.interpolation != INTERP_MODE_FLAT && + var->data.interpolation != INTERP_MODE_NOPERSPECTIVE) || + var->data.location < VARYING_SLOT_VAR0) continue; const unsigned first = var->data.location - VARYING_SLOT_VAR0; const unsigned slots = glsl_count_attribute_slots(var->type, false); - for (unsigned i = first; i < MIN2(first + slots, G9P_MAX_VARYINGS); i++) - out->varying_flat[i] = true; + for (unsigned i = first; i < MIN2(first + slots, G9P_MAX_VARYINGS); i++) { + if (var->data.interpolation == INTERP_MODE_FLAT) + out->varying_flat[i] = true; + else + out->varying_linear[i] = true; + } } } @@ -6489,8 +7503,8 @@ layer_id_zero(nir_builder *b, nir_intrinsic_instr *intr, void *data) bool g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, - const struct g9p_target targets[G9P_MAX_TARGETS], struct g9p_shader *out, - void *mem_ctx) + const struct g9p_target targets[G9P_MAX_TARGETS], bool zs_attachment, + struct g9p_shader *out, void *mem_ctx) { /* Targets past what a pixel holds are no part of its output */ struct g9p_target live[G9P_MAX_TARGETS]; @@ -6518,12 +7532,24 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, out->output = targets[0].output; out->output_int = targets[0].integer; out->output_float = targets[0].is_float; - lower_front(nir); + lower_front(nir, false); NIR_PASS(_, nir, discards_to_if); NIR_PASS(_, nir, discards_to_flag); NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_centroid, nir_metadata_control_flow, NULL); find_flat_varyings(nir, out); - lower_io(nir); + { + /* the colour outputs present: lower_io() makes a location the base */ + int base[G9P_MAX_TARGETS] = { -1, -1, -1, -1 }; + + nir_foreach_shader_out_variable(var, nir) { + if (var->data.location >= FRAG_RESULT_DATA0 && + var->data.location < FRAG_RESULT_DATA0 + G9P_MAX_TARGETS) + base[var->data.location - FRAG_RESULT_DATA0] = var->data.location; + } + lower_io(nir); + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_subpass_load, nir_metadata_control_flow, + base); + } NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_overflow_target, nir_metadata_control_flow, (void *)targets); if (coverage) lower_coverage(nir, coverage); @@ -6533,7 +7559,7 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, out->native_blend = out->native_blend_kind != G9P_BLEND_NONE && out->output == G9P_OUTPUT_HALF2 && !out->output_float && !targets[1].present && !targets[2].present && - !targets[3].present && !getenv("G9P_NO_NATIVE_BLEND"); + !targets[3].present && !targets[0].rgb565 && !getenv("G9P_NO_NATIVE_BLEND"); if (blend && !out->native_blend && getenv("G9P_PASSLOG")) { const nir_lower_blend_rt *rt = &blend->rt[0]; @@ -6548,9 +7574,19 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_blend_constant, nir_metadata_control_flow, NULL); } + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_rgb565_output, nir_metadata_control_flow, + (void *)targets); out->uses_fbfetch = nir->info.fs.uses_fbfetch_output || out->native_blend; - if (out->uses_fbfetch) + /* + * With a depth/stencil attachment the discards stay: a select keeps the + * tile's colour, but the pixel would still write its depth and stencil + * (our GLES 3 multi-target test: a blended draw's discarded pixels hid + * the later draws behind them). Such a shader runs as punch-through + * (punch_fbfetch), where the hardware discard works with tile reads. + */ + if (out->uses_fbfetch && !zs_attachment) NIR_PASS(_, nir, discards_to_select, out->native_blend); + out->punch_fbfetch = out->uses_fbfetch && zs_attachment; NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_fb_fetch_instr, nir_metadata_control_flow, out); nir_shader *copy = nir_shader_clone(mem_ctx, nir); diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index a027a3c4a35..70e7f9328d2 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -26,6 +26,31 @@ #define G9P_BINDING_ELEMENT_SHIFT 8 /* On a slot's binding: the buffer's size (the descriptor's dword 2), not its address */ #define G9P_BINDING_SIZE (1u << 30) +/* + * On a slot's binding: the address of the descriptor itself, for a storage + * image's record (struct g9p_storage_image) + */ +#define G9P_BINDING_DESCRIPTOR (1u << 29) +#define G9P_BINDING_FLAGS (G9P_BINDING_SIZE | G9P_BINDING_DESCRIPTOR) + +/* + * A10 compute storage images are read and written as memory + * (lower_storage_image()): their descriptor holds this record in place of + * texture state. The texels of the view's level and first layer, as the + * sampler lays them out. Twiddled, the low @bits bits of x and y are + * interleaved (y in bit 0) and the rest of the longer side follows, so a + * texel's index is col(x) + row(y) with disjoint bits. A linear image is the + * same with no interleaved bits and @row_bytes per row: texel + * col(x) * cpp + row(y) * row_bytes, with @row_bytes the texel size when + * twiddled. + */ +struct g9p_storage_image { + uint64_t base; + uint32_t bits; + uint32_t row_bytes; + uint32_t layer_stride; + uint32_t width, height, layers; +}; #define G9P_MAX_BUFFERS 40 /* 16 vertex bindings x address and stride, 4 capture buffers, uniforms */ #define G9P_MAX_ATTRIBS 16 /* @@ -44,6 +69,12 @@ * fragment code does). */ #define G9P_VS_TEX_BASE (2 * G9P_MAX_BUFFERS) +/* + * Compute shaders: the textures' words from dword 96, past the buffers' address slots and the + * thread count after them (2 * G9P_MAX_BUFFERS + 1 dwords), so that 16 textures end at 255, + * the last dword a sample's state and sampler fields reach + */ +#define G9P_CS_TEX_BASE 96 /* * Vertex shaders: the first dwords of each buffer they read at constant offsets, copied into * the common store per draw from g9p_vs_buf_base() (the buffers in slot order, @@ -103,6 +134,12 @@ g9p_fs_buf_base(unsigned n) #define G9P_BLEND_CONSTANTS (~5u) /* The set of target t's memory (binding t) when it overflows: address, pitch (3 dwords) */ #define G9P_OVERFLOW_TARGETS (~20u) +/* + * The set of the device's barrier counters (binding 0), for workgroups of more + * than one task: G9P_BARRIER_SLOTS slots of a count and a generation + */ +#define G9P_BARRIER_COUNTERS (~27u) +#define G9P_BARRIER_SLOTS 4096u /* The set of a draw's coverage state (binding 0): the sample mask, alpha to coverage */ #define G9P_COVERAGE (~21u) /* The set of the front face state: one word, ~0 for clockwise */ @@ -161,6 +198,8 @@ struct g9p_target { uint8_t write_mask; /* past what a pixel holds: the program writes it to memory */ bool overflow; + /* R5G6B5: the PBE keeps floor(v * 2^bits), the shader rounds for it (lower_rgb565_output()) */ + bool rgb565; }; struct g9p_shader { @@ -208,6 +247,8 @@ struct g9p_shader { unsigned num_varyings; bool varying_perspective[G9P_MAX_VARYINGS]; bool varying_flat[G9P_MAX_VARYINGS]; + /* Fragment: noperspective inputs, by varying (find_flat_varyings()) */ + bool varying_linear[G9P_MAX_VARYINGS]; /* Fragment: what the first render target takes; integers its low 16 bits */ enum g9p_output output; bool output_int; @@ -230,6 +271,11 @@ struct g9p_shader { /* writes its coverage (sample mask, alpha to coverage) */ bool writes_coverage; bool uses_fbfetch; + /* + * Fragment: reads the tile and may discard, with a depth/stencil attachment: + * the punch-through pass type, so a discarded pixel writes no depth or stencil + */ + bool punch_fbfetch; /* * Fragment: premultiplied source-over blended at the end with Apple's * half-precision sequence (g9p_blend_src_over()) instead of @@ -264,6 +310,7 @@ int g9p_buffer_slot(struct g9p_shader *out, unsigned set, unsigned binding); * stderr for anything the backend does not handle yet. */ bool g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx); +bool g9p_compile_compute_bank0(nir_shader *nir, struct g9p_shader *out, void *mem_ctx, unsigned bank1); /* The varying slot after the vertex shader's own: gl_FragCoord's */ unsigned g9p_frag_coord_slot(const nir_shader *vs); @@ -288,17 +335,20 @@ bool g9p_compile_vertex(nir_shader *nir, const struct g9p_vertex_attrib *attribs * pipeline to mark its draws translucent. */ /* - * A four-sample pipeline: the fragment program writes its coverage from the draw's + * A multisampled pipeline: the fragment program writes its coverage from the draw's * sample mask and alpha to coverage, the block of G9P_COVERAGE (binding 0: mask, enable) */ struct g9p_coverage { bool dynamic; + uint8_t samples; /* 2, 4 or 8 */ }; +bool g9p_folds_discards(const nir_shader *nir); + bool g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, - const struct g9p_target targets[G9P_MAX_TARGETS], struct g9p_shader *out, - void *mem_ctx); + const struct g9p_target targets[G9P_MAX_TARGETS], bool zs_attachment, + struct g9p_shader *out, void *mem_ctx); /* * A shader the backend refused, with what its compile was given, for replaying it offline @@ -312,6 +362,7 @@ struct g9p_capture_args { nir_lower_blend_options blend; struct g9p_coverage coverage; struct g9p_target targets[G9P_MAX_TARGETS]; + uint8_t zs_attachment; }; #define G9P_CAPTURE_MAGIC 0x43503947u /* "G9PC" */ diff --git a/src/imagination/g9p/g9p_formats.h b/src/imagination/g9p/g9p_formats.h index 43216da1dce..0bcba1360fc 100644 --- a/src/imagination/g9p/g9p_formats.h +++ b/src/imagination/g9p/g9p_formats.h @@ -202,6 +202,37 @@ g9p_output_words(enum g9p_output output) /* A pixel holds eight output words (A10: 32 bytes) */ #define G9P_PIXEL_WORDS 8 +/* Groups of targets, at most one a target (G9P_MAX_TARGETS) */ +#define G9P_MAX_GROUPS 4 + +/* + * More words than a pixel holds: the targets in groups of at most G9P_PIXEL_WORDS + * words, in order, each group rendered by itself (the draws of the render again for + * each). Returns how many groups, 1 when the targets fit; @masks gets each one's + * targets. Every caller with the same outputs gets the same groups. + */ +static inline unsigned +g9p_target_groups(const enum g9p_output *outputs, unsigned present, unsigned masks[G9P_MAX_GROUPS]) +{ + unsigned n = 0, words = 0; + + for (unsigned g = 0; g < G9P_MAX_GROUPS; g++) + masks[g] = 0; + for (unsigned t = 0; t < G9P_MAX_GROUPS; t++) { + const unsigned w = g9p_output_words(outputs[t]); + + if (!(present >> t & 1)) + continue; + if (words + w > G9P_PIXEL_WORDS && masks[n]) { + n++; + words = 0; + } + masks[n] |= 1u << t; + words += w; + } + return n + 1; +} + /* * The targets past what a pixel holds: the fragment program writes these to memory itself. * Only 32-bit channels, which need no packing and don't blend; four targets of the others diff --git a/src/imagination/g9p/g9p_isa.h b/src/imagination/g9p/g9p_isa.h index 9c39840acc1..d6f01da6bd1 100644 --- a/src/imagination/g9p/g9p_isa.h +++ b/src/imagination/g9p/g9p_isa.h @@ -976,8 +976,10 @@ g9p_end_fragment(struct g9p_code *c, unsigned words) /* * Texture dimensions of the sample instruction: W0 bits 8-11 (2D 5, as - * g9p_sample_regs()'s f51a; 2D array 7, cube d). From the A9 (PR #5). + * g9p_sample_regs()'s f51a; 2D array 7, cube d). From the A9 (PR #5). A 2D + * multisample fetch takes 9 (the f91a of emit_txf_ms()). */ +#define G9P_TEX_2D_MS 0x9 #define G9P_TEX_2D_ARRAY 0x7 #define G9P_TEX_3D 0xb #define G9P_TEX_CUBE 0xd diff --git a/src/imagination/g9p/g9p_replay.c b/src/imagination/g9p/g9p_replay.c index 6e94caabc76..acdbeb34f53 100644 --- a/src/imagination/g9p/g9p_replay.c +++ b/src/imagination/g9p/g9p_replay.c @@ -49,8 +49,8 @@ replay(const char *path) break; case MESA_SHADER_FRAGMENT: ok = g9p_compile_fragment(nir, args.frag_coord_slot, args.has_blend ? &args.blend : NULL, - args.has_coverage ? &args.coverage : NULL, args.targets, &out, - mem_ctx); + args.has_coverage ? &args.coverage : NULL, args.targets, args.zs_attachment, + &out, mem_ctx); break; default: ok = g9p_compile_compute(nir, &out, mem_ctx); diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index 7a6b163519b..bba32ea2570 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -89,6 +89,7 @@ #include "vk_pipeline_layout.h" #include "vk_synchronization.h" #include "pvr_g9p.h" +#include "g9p_compile.h" #include "vk_util.h" #define G5P_UPLOAD_BLOCK_SIZE (64u * 1024u) @@ -219,8 +220,10 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, util_dynarray_fini(&sub_cmd->gfx.sec_query_indices); util_dynarray_fini(&sub_cmd->gfx.g9p_realigns); + util_dynarray_fini(&sub_cmd->gfx.g9p_indirects); util_dynarray_fini(&sub_cmd->gfx.g9p_fills); util_dynarray_fini(&sub_cmd->gfx.g9p_view_kicks); + util_dynarray_fini(&sub_cmd->gfx.g9p_group_ppps); util_dynarray_foreach (&sub_cmd->gfx.g9p_view_bos, struct pvr_bo *, bo) pvr_bo_free(cmd_buffer->device, *bo); util_dynarray_fini(&sub_cmd->gfx.g9p_view_bos); @@ -240,6 +243,7 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, case PVR_SUB_CMD_TYPE_COMPUTE: case PVR_SUB_CMD_TYPE_QUERY: + util_dynarray_fini(&sub_cmd->compute.g9p_indirects); vk_free(&cmd_buffer->vk.pool->alloc, sub_cmd->compute.g9p_query); for (struct pvr_g5p_dispatch_log *l = sub_cmd->compute.g5p_log_first, *next; l; l = next) { next = l->next; @@ -1479,6 +1483,14 @@ static bool pvr_sub_cmd_gfx_requires_ds_subtile_alignment( rogue_get_zls_tile_size_xy(dev_info, &zls_tile_size_x, &zls_tile_size_y); PVR_G5P_HOOK(ds_subtile_alignment, dev_info) + /* + * G9P: the ZLS's surface is the attachment's level and layer, sized for + * the ZLS (g9p_ds_addr(), g9p_zls_pixels(), pvr_image.c). The + * tile-rounded buffer would be copied by CPU transfers, which cannot + * reach the general heap. + */ + if (PVR_HAS_FEATURE(dev_info, apple_g9p)) + return false; if (ds_plane->physical_extent.width >= zls_tile_size_x && ds_plane->physical_extent.height >= zls_tile_size_y) { return false; @@ -1503,6 +1515,124 @@ static bool pvr_sub_cmd_gfx_requires_ds_subtile_alignment( return true; } +/* + * G9P: a D32F_S8 level of a chain with a side under 32 pixels. Its stencil + * plane takes ZLS_PIXELS aligned to 32 (g9p_zls_pixels()), which would + * store whole tiles past the level, so the render loads and stores a + * scratch plane of whole tiles instead (as iOS), copied in before it when + * it loads and back after it when it stores, by CPU transfers serialized + * with the fragment jobs, so renders take the scratch plane one at a time. + */ +static VkResult +g9p_ds_scratch(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd) +{ + struct pvr_sub_cmd *const prev_sub_cmd = container_of(gfx_sub_cmd, struct pvr_sub_cmd, gfx); + struct pvr_ds_attachment *const ds = &gfx_sub_cmd->job.ds; + const struct pvr_image *const image = pvr_image_view_get_image(ds->iview); + const struct pvr_image_plane *const plane = pvr_single_plane_const(image); + const uint32_t level = ds->iview->vk.base_mip_level; + const uint32_t layer = ds->iview->vk.base_array_layer; + const uint32_t pw = u_minify(plane->physical_extent.width, level); + const uint32_t ph = u_minify(plane->physical_extent.height, level); + const uint32_t sbw = MAX2(pw, 32), sbh = MAX2(ph, 32); + const uint64_t stencil = (uint64_t)sbw * sbh * 4; + VkResult result; + + ds->g9p_scratch = 0; + if (image->vk.format != VK_FORMAT_D32_SFLOAT_S8_UINT || image->vk.mip_levels == 1 || + (pw >= 32 && ph >= 32) || !pvr_ds_attachment_requires_zls(ds)) + return VK_SUCCESS; + + const uint64_t scratch = PVR_PER_ARCH(g9p_zls_scratch)(cmd_buffer->device); + + if (!scratch || stencil + (uint64_t)sbw * sbh > G9P_ZLS_SCRATCH_SIZE) + return g9p_feature_error(cmd_buffer, "G9P: no scratch plane for a %ux%u stencil level", pw, ph); + + if (ds->load.d || ds->load.s) { + struct pvr_sub_cmd *new_sub_cmd; + + cmd_buffer->state.current_sub_cmd = NULL; + result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER); + if (result != VK_SUCCESS) + return result; + new_sub_cmd = cmd_buffer->state.current_sub_cmd; + result = pvr_g9p_ds_scratch_transfer(cmd_buffer, image, level, layer, scratch, sbw, sbh, + stencil, true); + if (result != VK_SUCCESS) + return result; + new_sub_cmd->transfer.serialize_with_frag = true; + result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + if (result != VK_SUCCESS) + return result; + /* before the render (list_move_to() places it after the entry given) */ + list_move_to(&new_sub_cmd->link, prev_sub_cmd->link.prev); + cmd_buffer->state.current_sub_cmd = prev_sub_cmd; + } + if (ds->store.d || ds->store.s) { + cmd_buffer->state.current_sub_cmd = NULL; + result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER); + if (result != VK_SUCCESS) + return result; + result = pvr_g9p_ds_scratch_transfer(cmd_buffer, image, level, layer, scratch, sbw, sbh, + stencil, false); + if (result != VK_SUCCESS) + return result; + /* after the render's fragment job */ + cmd_buffer->state.current_sub_cmd->transfer.serialize_with_frag = true; + result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + if (result != VK_SUCCESS) + return result; + cmd_buffer->state.current_sub_cmd = prev_sub_cmd; + } + ds->g9p_scratch = scratch; + ds->g9p_scratch_stencil = stencil; + return VK_SUCCESS; +} + +/* + * G9P: the colour targets that store to a scratch slot (g9p_eot_addr()): the + * level copied in before the render, so that its tiles left unrendered keep + * it, and back after it, by CPU transfers serialized with the fragment jobs + * as g9p_ds_scratch()'s are. + */ +static VkResult +g9p_colour_scratch(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd, + const struct pvr_render_pass_info *info, + const struct pvr_renderpass_hwsetup_render *hw_render) +{ + struct pvr_sub_cmd *const prev_sub_cmd = container_of(gfx_sub_cmd, struct pvr_sub_cmd, gfx); + const uint64_t scratch = PVR_PER_ARCH(g9p_zls_scratch)(cmd_buffer->device); + VkResult result; + + for (unsigned before = 0; before < 2; before++) { + cmd_buffer->state.current_sub_cmd = NULL; + result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER); + if (result != VK_SUCCESS) + return result; + struct pvr_sub_cmd *const new_sub_cmd = cmd_buffer->state.current_sub_cmd; + + u_foreach_bit (t, gfx_sub_cmd->job.g9p.colour_scratch) { + const struct pvr_image_view *iview = info->attachments[hw_render->eot_surfaces[t].attachment_idx]; + + result = pvr_g9p_colour_scratch_transfer(cmd_buffer, pvr_image_view_get_image(iview), + iview->vk.base_mip_level, + iview->vk.base_array_layer, + g9p_colour_scratch_slot(scratch, t), before); + if (result != VK_SUCCESS) + return result; + } + new_sub_cmd->transfer.serialize_with_frag = true; + result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + if (result != VK_SUCCESS) + return result; + /* the copy in before the render (list_move_to() places it after the entry given) */ + if (before) + list_move_to(&new_sub_cmd->link, prev_sub_cmd->link.prev); + } + cmd_buffer->state.current_sub_cmd = prev_sub_cmd; + return VK_SUCCESS; +} + static VkResult pvr_sub_cmd_gfx_align_ds_subtiles(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd) @@ -2461,12 +2591,22 @@ static VkResult pvr_sub_cmd_gfx_job_init(const struct pvr_device_info *dev_info, if (result != VK_SUCCESS) return result; } + if (PVR_HAS_FEATURE(dev_info, apple_g9p)) { + result = g9p_ds_scratch(cmd_buffer, sub_cmd); + if (result != VK_SUCCESS) + return result; + } } } else { job->has_depth_attachment = false; job->has_stencil_attachment = false; job->ds_clear_value = default_ds_clear_value; } + if (PVR_HAS_FEATURE(dev_info, apple_g9p) && job->g9p.colour_scratch) { + result = g9p_colour_scratch(cmd_buffer, sub_cmd, render_pass_info, hw_render); + if (result != VK_SUCCESS) + return result; + } if (hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED) { struct pvr_image_view *iview = @@ -3075,6 +3215,8 @@ VkResult pvr_arch_cmd_buffer_end_sub_cmd(struct pvr_cmd_buffer *cmd_buffer) /* G9P: per-view copies of the stream instead (Rogue's PDS program is not G9P's) */ if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p)) { result = pvr_arch_g9p_build_view_streams(cmd_buffer, gfx_sub_cmd); + if (result == VK_SUCCESS) + result = pvr_arch_g9p_build_group_streams(cmd_buffer, gfx_sub_cmd); if (result != VK_SUCCESS) return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result); } else if (gfx_sub_cmd->view_index_wanted) { @@ -3433,8 +3575,11 @@ VkResult pvr_arch_cmd_buffer_start_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, sub_cmd->gfx.sec_query_indices = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_realigns = UTIL_DYNARRAY_INIT; + sub_cmd->gfx.g9p_indirects = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_fills = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_view_kicks = UTIL_DYNARRAY_INIT; + sub_cmd->gfx.g9p_group_ppps = UTIL_DYNARRAY_INIT; + memset(sub_cmd->gfx.g9p_group_stream, 0, sizeof(sub_cmd->gfx.g9p_group_stream)); sub_cmd->gfx.g9p_view_bos = UTIL_DYNARRAY_INIT; memset(sub_cmd->gfx.g9p_view_stream, 0, sizeof(sub_cmd->gfx.g9p_view_stream)); sub_cmd->gfx.unbound_deferred_clears = UTIL_DYNARRAY_INIT; @@ -3445,6 +3590,7 @@ VkResult pvr_arch_cmd_buffer_start_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, pvr_csb_init(device, PVR_CMD_STREAM_TYPE_COMPUTE, &sub_cmd->compute.control_stream); + sub_cmd->compute.g9p_indirects = UTIL_DYNARRAY_INIT; break; case PVR_SUB_CMD_TYPE_TRANSFER: @@ -4905,6 +5051,25 @@ static VkResult pvr_dynamic_rendering_output_attachments_setup( dr_info->color_attachment_count = pRenderingInfo->colorAttachmentCount; + /* + * G9P: an end of tile stores at most four surfaces (G9P_MAX_TARGETS, the image state + * blocks its emits reach). Targets and resolves past that: the resolves by transfer, + * after the render. + */ + bool g9p_transfer_resolves = false; + + if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p)) { + unsigned surfaces = 0; + + for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) { + const VkRenderingAttachmentInfo *a = &pRenderingInfo->pColorAttachments[i]; + + if (a->imageView != VK_NULL_HANDLE) + surfaces += 1 + (a->resolveMode && a->resolveImageView != VK_NULL_HANDLE); + } + g9p_transfer_resolves = surfaces > G9P_MAX_TARGETS; + } + if (dr_info->color_attachment_count) { const uint32_t size = sizeof(*dr_info->color_attachments) * dr_info->color_attachment_count; @@ -4981,6 +5146,8 @@ static VkResult pvr_dynamic_rendering_output_attachments_setup( image->vk.samples, attachment_info->resolveMode); PVR_G5P_HOOK(msaa_pbe_resolve, &cmd_buffer->device->pdevice->dev_info, attachment, iview->vk.view_format, image->vk.samples, attachment_info->resolveMode) + if (g9p_transfer_resolves) + attachment->is_pbe_downscalable = false; dr_info->color_attachments[i].index_color = cur_attach_idx; dr_info->hw_render.sample_count = MAX2(dr_info->hw_render.sample_count, attachment->sample_count); @@ -5882,6 +6049,21 @@ static bool g9p_merge_compatible(const struct pvr_cmd_buffer *cmd_buffer, const info->layerCount != state->g9p_merge.layers || info->viewMask != state->g9p_merge.view_mask || info->colorAttachmentCount > state->g9p_merge.color_count) return false; + /* + * Every colour attachment of the open render in the new one too: draws of a + * render without it ran in the open render and wrote it (eglcoverage's draw + * into a framebuffer with the depth buffer alone, after a clear of colour + * and depth: the clear's render took the draw, which coloured the target) + */ + for (uint32_t i = 0; i < state->g9p_merge.color_count; i++) { + if (!state->g9p_merge.color[i].image) + continue; + if (i >= info->colorAttachmentCount) + return false; + g9p_merge_attachment(&info->pColorAttachments[i], &image, &level, &layer); + if (!image) + return false; + } for (uint32_t i = 0; i < info->colorAttachmentCount; i++) { const VkRenderingAttachmentInfo *a = &info->pColorAttachments[i]; @@ -7657,8 +7839,8 @@ static void pvr_cmd_dispatch( return; PVR_G5P_HOOK(compute_dispatch) if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { - /* Indirect dispatches and base groups are not supported yet */ - result = pvr_arch_g9p_cmd_dispatch(cmd_buffer, sub_cmd, workgroup_size); + /* base groups are not supported yet */ + result = pvr_arch_g9p_cmd_dispatch(cmd_buffer, sub_cmd, workgroup_size, indirect_addr.addr); if (result != VK_SUCCESS) pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result); goto end_cmd_dispatch; @@ -10360,16 +10542,27 @@ void PVR_PER_ARCH(CmdDrawIndexed)(VkCommandBuffer commandBuffer, return; const unsigned size = vk_index_type_to_bytes(state->index_buffer_binding.type); + /* + * Robust buffer access: only the indices inside the buffer are drawn. + * The VDM reads every index it is given, and far past the buffer that + * faults the GPU. + */ + const uint64_t in_buffer = (state->index_buffer_binding.buffer->vk.size - + state->index_buffer_binding.offset) / size; + const uint32_t count = firstIndex < in_buffer ? MIN2(indexCount, in_buffer - firstIndex) : 0; const struct pvr_g9p_draw draw = { .indexed = true, .index_addr = state->index_buffer_binding.buffer->dev_addr.addr + state->index_buffer_binding.offset + (uint64_t)firstIndex * size, .index_size = size, - .count = indexCount, + .count = count, .instances = instanceCount, .offset = vertexOffset, }; + if (!count) + goto end_cmd_draw_indexed; + /* the vertex shader's G9P_DRAW_PARAMS slot */ state->draw_state.base_instance = firstInstance; result = pvr_flush_push_descriptors(cmd_buffer, &state->gfx_desc_state); @@ -10409,6 +10602,56 @@ void PVR_PER_ARCH(CmdDrawIndexed)(VkCommandBuffer commandBuffer, PVR_TRACE_END_DRAW(cmd_buffer); } +/* + * G9P: indirect draws, each recorded with its words filled from its + * arguments when the job is submitted (g9p_patch_indirect_draws()): the + * arguments are read by the CPU, which waits first for a GPU job that may + * have written them + */ +static void +g9p_draw_indirect(struct pvr_cmd_buffer *cmd_buffer, struct pvr_buffer *buffer, + VkDeviceSize offset, uint32_t count, uint32_t stride, bool indexed) +{ + struct pvr_cmd_buffer_state *state = &cmd_buffer->state; + const unsigned size = indexed ? sizeof(VkDrawIndexedIndirectCommand) + : sizeof(VkDrawIndirectCommand); + VkResult result; + + if (!state->gfx_pipeline || !count) + return; + result = pvr_flush_push_descriptors(cmd_buffer, &state->gfx_desc_state); + for (uint32_t i = 0; i < count && result == VK_SUCCESS; i++) { + const uint64_t at = offset + (uint64_t)i * stride; + const uint8_t *args = + at + size <= buffer->vk.size + ? PVR_PER_ARCH(g9p_cpu_addr)(cmd_buffer->device, buffer->dev_addr.addr + at) + : NULL; + struct pvr_g9p_draw draw = { + .indexed = indexed, + .count = 1, + .instances = 1, + .indirect = args, + .indirect_buffer = buffer, + }; + + if (!args) { + result = g9p_feature_error(cmd_buffer, "G9P: indirect draw arguments the CPU cannot read"); + break; + } + if (indexed) { + const struct pvr_buffer *ib = state->index_buffer_binding.buffer; + + draw.index_size = vk_index_type_to_bytes(state->index_buffer_binding.type); + draw.index_addr = ib->dev_addr.addr + state->index_buffer_binding.offset; + draw.index_end = ib->dev_addr.addr + ib->vk.size; + } + state->draw_state.base_instance = 0; + result = pvr_arch_g9p_cmd_draw(cmd_buffer, &draw); + } + if (result != VK_SUCCESS) + vk_command_buffer_set_error(&cmd_buffer->vk, result); +} + void PVR_PER_ARCH(CmdDrawIndexedIndirect)(VkCommandBuffer commandBuffer, VkBuffer _buffer, VkDeviceSize offset, @@ -10434,6 +10677,10 @@ void PVR_PER_ARCH(CmdDrawIndexedIndirect)(VkCommandBuffer commandBuffer, PVR_TRACE_BEGIN_DRAW(cmd_buffer, PVR_DRAW_INDEXED_INDIRECT); PVR_G5P_HOOK(g5p_indexed_indirect) + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { + g9p_draw_indirect(cmd_buffer, buffer, offset, drawCount, stride, true); + goto end_cmd_draw_indexed_indirect; + } pvr_update_draw_state(state, &draw_state); result = pvr_validate_draw_state(cmd_buffer); @@ -10481,6 +10728,10 @@ void PVR_PER_ARCH(CmdDrawIndirect)(VkCommandBuffer commandBuffer, PVR_TRACE_BEGIN_DRAW(cmd_buffer, PVR_DRAW_INDIRECT); PVR_G5P_HOOK(g5p_indirect) + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { + g9p_draw_indirect(cmd_buffer, buffer, offset, drawCount, stride, false); + goto end_cmd_draw_indirect; + } pvr_update_draw_state(state, &draw_state); result = pvr_validate_draw_state(cmd_buffer); @@ -11633,6 +11884,14 @@ void PVR_PER_ARCH(CmdPipelineBarrier2)(VkCommandBuffer commandBuffer, if (!render_pass) break; + /* + * G9P: a fragment shader's tile reads see the draws before it in + * order (lower_subpass_load()); Rogue's transparent object is not an + * A10 draw + */ + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) + break; + assert(current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS); /* Flush all fragment work up to this point. */ @@ -11657,6 +11916,18 @@ void PVR_PER_ARCH(CmdPipelineBarrier2)(VkCommandBuffer commandBuffer, * anything new. */ PVR_G5P_HOOK(compute_barrier) + /* + * G9P: Rogue's data fence and fence kernel crash the A10's firmware + * (two dispatches with a barrier between them in one job): the job + * ends here, and the next dispatch starts one after it + */ + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { + VkResult result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + + if (result != VK_SUCCESS) + pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result); + break; + } /* Issue Data Fence, Wait for Data Fence (IDFWDF) makes the PDS wait * for data. */ diff --git a/src/imagination/vulkan/pvr_arch_descriptor_set.c b/src/imagination/vulkan/pvr_arch_descriptor_set.c index c49abb22da9..ac11ac74899 100644 --- a/src/imagination/vulkan/pvr_arch_descriptor_set.c +++ b/src/imagination/vulkan/pvr_arch_descriptor_set.c @@ -23,6 +23,8 @@ #include "pvr_physical_device.h" #include "pvr_sampler.h" #include "pvr_g5p_compute_state.h" +#include "pvr_g9p.h" +#include "g9p_compile.h" static void write_buffer(const struct pvr_descriptor_set *set, const VkDescriptorBufferInfo *buffer_info, @@ -300,6 +302,17 @@ write_storage_image(const struct pvr_descriptor_set *set, VK_FROM_HANDLE(pvr_image_view, image_view, image_info->imageView); + /* G9P: the record its compute shaders address the texels with */ + if (PVR_HAS_FEATURE(dev_info, apple_g9p)) { + struct g9p_storage_image rec; + + static_assert(sizeof(rec) <= sizeof(struct pvr_image_descriptor), + "a storage image's record fits its descriptor"); + PVR_PER_ARCH(g9p_storage_image_record)(image_view, &rec); + memcpy(desc_mapping, &rec, sizeof(rec)); + return; + } + bool is_cube = image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE || image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY; diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index dd9f04ea3da..ed10b49500d 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -6,6 +6,7 @@ * of the pco/Rogue paths. */ +#include #include #include "vk_pipeline.h" @@ -25,6 +26,7 @@ #include "pvr_entrypoints.h" #include "pvr_free_list.h" #include "pvr_g9p.h" +#include "pvr_image.h" #include "pvr_physical_device.h" #include "pvr_pipeline.h" @@ -50,6 +52,8 @@ PVR_PER_ARCH(g9p_device_init)(struct pvr_device *device) VkResult result; simple_mtx_init(&device->g9p_vmas.lock, mtx_plain); + simple_mtx_init(&device->g9p.zls_scratch.lock, mtx_plain); + simple_mtx_init(&device->g9p.barriers.lock, mtx_plain); util_dynarray_init(&device->g9p_vmas.vmas, NULL); for (unsigned i = 0; i < ARRAY_SIZE(empty); i++) @@ -110,10 +114,70 @@ PVR_PER_ARCH(g9p_device_finish)(struct pvr_device *device) pvr_bo_suballoc_free(device->g9p.fence.pvr_bo); pvr_bo_suballoc_free(device->g9p.kick.pvr_bo); pvr_bo_suballoc_free(device->g9p.empty_kernel); + if (device->g9p.zls_scratch.bo) { + util_dynarray_delete_unordered(&device->g9p_vmas.vmas, struct pvr_winsys_vma *, + device->g9p.zls_scratch.bo->vma); + pvr_bo_free(device, device->g9p.zls_scratch.bo); + } + simple_mtx_destroy(&device->g9p.zls_scratch.lock); + if (device->g9p.barriers.bo) + pvr_bo_free(device, device->g9p.barriers.bo); + simple_mtx_destroy(&device->g9p.barriers.lock); util_dynarray_fini(&device->g9p_vmas.vmas); simple_mtx_destroy(&device->g9p_vmas.lock); } +/* + * The ZLS scratch plane (device->g9p.zls_scratch): its device address, 0 if + * it cannot be had. Whole tiles of a level with a side under 32 pixels: 32 + * on that side, at most 16384 on the other, 4 bytes of depth and 1 of + * stencil a pixel. + */ +uint64_t +PVR_PER_ARCH(g9p_zls_scratch)(struct pvr_device *device) +{ + uint64_t addr = 0; + + simple_mtx_lock(&device->g9p.zls_scratch.lock); + if (!device->g9p.zls_scratch.bo) { + struct pvr_bo *bo; + + if (pvr_bo_alloc(device, device->heaps.general_heap, G9P_ZLS_SCRATCH_SIZE, 16384, + PVR_BO_ALLOC_FLAG_CPU_MAPPED, &bo) == VK_SUCCESS) { + device->g9p.zls_scratch.bo = bo; + simple_mtx_lock(&device->g9p_vmas.lock); + util_dynarray_append_typed(&device->g9p_vmas.vmas, struct pvr_winsys_vma *, bo->vma); + simple_mtx_unlock(&device->g9p_vmas.lock); + } + } + if (device->g9p.zls_scratch.bo) + addr = device->g9p.zls_scratch.bo->vma->dev_addr.addr; + simple_mtx_unlock(&device->g9p.zls_scratch.lock); + return addr; +} + +/* The barrier counters (device->g9p.barriers, G9P_BARRIER_COUNTERS): their device address, 0 if none */ +static uint64_t +g9p_barrier_counters(struct pvr_device *device) +{ + uint64_t addr = 0; + + simple_mtx_lock(&device->g9p.barriers.lock); + if (!device->g9p.barriers.bo) { + struct pvr_bo *bo; + + if (pvr_bo_alloc(device, device->heaps.general_heap, 8 * G9P_BARRIER_SLOTS, 64, + PVR_BO_ALLOC_FLAG_CPU_MAPPED, &bo) == VK_SUCCESS) { + memset(bo->bo->map, 0, 8 * G9P_BARRIER_SLOTS); + device->g9p.barriers.bo = bo; + } + } + if (device->g9p.barriers.bo) + addr = device->g9p.barriers.bo->vma->dev_addr.addr; + simple_mtx_unlock(&device->g9p.barriers.lock); + return addr; +} + VkResult PVR_PER_ARCH(g9p_compute_pipeline_compile)( struct pvr_device *device, @@ -135,8 +199,10 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( goto out; nir_shader *capture = getenv("G9P_CAPTURE") ? nir_shader_clone(mem_ctx, nir) : NULL; + nir_shader *again = nir_shader_clone(mem_ctx, nir); if (!g9p_compile_compute(nir, &shader, mem_ctx)) { + mesa_loge("G9P: compute shader not supported by the backend yet"); if (capture) g9p_capture_failure(capture, &(struct g9p_capture_args){ .stage = MESA_SHADER_COMPUTE }); result = vk_errorf(device, VK_ERROR_UNKNOWN, @@ -144,6 +210,40 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( goto out; } + /* + * A workgroup's tasks are resident together, and too many registers for + * them hang the GPU (g9p_compute_max_tasks()). 512 invocations, the device + * limit, take at most 16 in bank 1. + */ + const uint32_t threads = + shader.local_size[0] * shader.local_size[1] * shader.local_size[2]; + /* + * Too many for bank 1: again with the values past what the workgroup's tasks may hold + * there in bank 0, as A9 compute keeps them (g9p_compile_compute_bank0()); the kick's + * DOUTU temps allocate bank 0 + */ + state->bank1_regs = 0; + if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(shader.num_regs) && !getenv("G9P_NO_BANK0")) { + const unsigned tasks = DIV_ROUND_UP(threads, 32); + const unsigned bank1 = tasks <= 2 ? 96 : tasks <= 4 ? 64 : tasks <= 5 ? 40 : tasks <= 6 ? 32 : + tasks <= 8 ? 24 : 16; + struct g9p_shader capped; + + if (g9p_compile_compute_bank0(again, &capped, mem_ctx, bank1) && + DIV_ROUND_UP(threads, 32) <= g9p_compute_max_tasks(capped.a9_bank1_regs)) { + shader = capped; + state->bank1_regs = capped.a9_bank1_regs; + } + } + if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(state->bank1_regs ? state->bank1_regs : shader.num_regs)) { + mesa_loge("G9P: %u invocations of %u registers each do not fit the USC", threads, + shader.num_regs); + result = vk_errorf(device, VK_ERROR_UNKNOWN, + "G9P: %u invocations of %u registers each do not fit the USC", + threads, shader.num_regs); + goto out; + } + result = pvr_gpu_upload_usc(device, shader.code, shader.code_size, 64, &state->shader_bo); if (result != VK_SUCCESS) @@ -158,6 +258,16 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( state->uses_workgroup_id = shader.uses_workgroup_id; memcpy(state->local_size, shader.local_size, sizeof(state->local_size)); state->shared_size = shader.shared_size; + state->shared_spill_stride = shader.shared_spill_stride; + state->num_textures = shader.num_textures; + for (unsigned i = 0; i < shader.num_textures; i++) { + state->textures[i].set = shader.textures[i].set; + state->textures[i].binding = shader.textures[i].binding; + state->textures[i].index = shader.textures[i].index; + state->textures[i].sampler_set = shader.textures[i].sampler_set; + state->textures[i].sampler_binding = shader.textures[i].sampler_binding; + state->textures[i].sampler_index = shader.textures[i].sampler_index; + } out: ralloc_free(mem_ctx); @@ -179,10 +289,46 @@ emit_record(struct pvr_csb *csb, const struct g9p_cdm_record *rec) * kernel, whose PDS program writes the workgroup ID into threadgroup words * 0-1 and kicks the USC. */ +/* + * A storage image view's record (struct g9p_storage_image): its level's + * texels from its first layer, the layer stride (a 3D image's slices are + * layers, pvr_image.c), and the twiddle box's shorter side, which the + * level's power-of-two physical extent gives (as the sampler and the + * transfers lay the texels out) + */ +void +PVR_PER_ARCH(g9p_storage_image_record)(const struct pvr_image_view *iview, + struct g9p_storage_image *rec) +{ + const struct pvr_image *image = pvr_image_view_get_image(iview); + const struct pvr_image_plane *plane = pvr_single_plane_const(image); + const unsigned level = iview->vk.base_mip_level; + const bool is3d = iview->vk.view_type == VK_IMAGE_VIEW_TYPE_3D; + + memset(rec, 0, sizeof(*rec)); + rec->base = image->dev_addr.addr + plane->offset + plane->mip_levels[level].offset + + (is3d ? 0 : iview->vk.base_array_layer * plane->layer_size); + rec->layer_stride = plane->layer_size; + rec->width = iview->vk.extent.width; + rec->height = iview->vk.extent.height; + rec->layers = is3d ? iview->vk.extent.depth : iview->vk.layer_count; + if (image->memlayout == PVR_MEMLAYOUT_TWIDDLED || + image->memlayout == PVR_MEMLAYOUT_3DTWIDDLED) { + const uint32_t pw = u_minify(plane->physical_extent.width, level); + const uint32_t ph = u_minify(plane->physical_extent.height, level); + + rec->bits = util_logbase2(util_next_power_of_two(MAX2(MIN2(pw, ph), 1))); + rec->row_bytes = vk_format_get_blocksize(iview->vk.format); + } else { + rec->row_bytes = plane->mip_levels[level].pitch; + } +} + VkResult PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd, - const uint32_t group_count[static 3]) + const uint32_t group_count[static 3], + uint64_t indirect) { struct pvr_device *device = cmd_buffer->device; const struct pvr_compute_pipeline *pipeline = cmd_buffer->state.compute_pipeline; @@ -194,24 +340,74 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const uint32_t count = threads | threads << 16; struct pvr_suballoc_bo *uniforms_bo; uint32_t uniforms[2 * G9P_MAX_BUFFERS + 1] = { 0 }; - uint32_t bind_data[4 * (G9P_MAX_BUFFERS + 1) + 4] = { 0 }; - uint32_t bind_code[G9P_MAX_BUFFERS + 2]; + uint32_t bind_data[4 * (G9P_MAX_BUFFERS + 2) + 4] = { 0 }; + uint32_t bind_code[G9P_MAX_BUFFERS + 3]; uint32_t kernel_data[G9P_KERNEL_DATA_DWORDS] = { 0 }, kernel_code[8]; bool from_uniforms[G9P_MAX_BUFFERS] = { false }; struct pvr_pds_upload bind, kernel; unsigned bind_dwords, ctrl0, nc = 0; VkResult result; + /* + * Shared variables in memory (a9_spill_shared(): a shader with atomics on them): + * a region of the pipeline's stride per workgroup, for this dispatch + */ + struct pvr_bo *shared_spill = NULL; + + if (state->shared_spill_stride && indirect) + return vk_errorf(cmd_buffer, VK_ERROR_FEATURE_NOT_PRESENT, + "G9P: an indirect dispatch of shared variables in memory"); + if (state->shared_spill_stride) { + const uint64_t groups = (uint64_t)group_count[0] * group_count[1] * group_count[2]; + struct pvr_suballoc_bo *owner; + + if (groups > (64u << 20) / state->shared_spill_stride) + return vk_errorf(cmd_buffer, VK_ERROR_OUT_OF_DEVICE_MEMORY, + "G9P: %" PRIu64 " workgroups of %u bytes of shared variables in memory", + groups, state->shared_spill_stride); + owner = vk_zalloc(&cmd_buffer->device->vk.alloc, sizeof(*owner), 8, + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!owner) + return vk_error(cmd_buffer, VK_ERROR_OUT_OF_HOST_MEMORY); + result = pvr_bo_alloc(cmd_buffer->device, cmd_buffer->device->heaps.general_heap, + groups * state->shared_spill_stride, 64, PVR_BO_ALLOC_FLAG_CPU_MAPPED, + &shared_spill); + if (result != VK_SUCCESS) { + vk_free(&cmd_buffer->device->vk.alloc, owner); + return result; + } + /* freed with the command buffer's uploads (pvr_arch_g5p_compute.c does the same) */ + owner->allocator = pvr_cmd_suballocator(cmd_buffer->device, &cmd_buffer->device->suballoc_general); + owner->bo = shared_spill; + owner->dev_addr = shared_spill->vma->dev_addr; + owner->size = groups * state->shared_spill_stride; + list_add(&owner->link, &cmd_buffer->bo_list); + } + /* * Values the DMA copies that are not in descriptor memory: the * push-constant buffer's address and the thread count. */ for (unsigned k = 0; k < n; k++) { + if (state->buffers[k].set == G9P_SHARED_SPILL) { + uniforms[2 * k] = shared_spill ? shared_spill->vma->dev_addr.addr : 0; + uniforms[2 * k + 1] = shared_spill ? shared_spill->vma->dev_addr.addr >> 32 : 0; + from_uniforms[k] = true; + } if (state->buffers[k].set == G9P_NUM_WORKGROUPS) { /* gl_NumWorkGroups: x and y in binding 0's slot, z in binding 1's */ uniforms[2 * k] = group_count[state->buffers[k].binding ? 2 : 0]; uniforms[2 * k + 1] = state->buffers[k].binding ? 0 : group_count[1]; } + if (state->buffers[k].set == G9P_BARRIER_COUNTERS) { + const uint64_t addr = g9p_barrier_counters(cmd_buffer->device); + + if (!addr) + return vk_error(cmd_buffer, VK_ERROR_OUT_OF_DEVICE_MEMORY); + uniforms[2 * k] = addr; + uniforms[2 * k + 1] = addr >> 32; + from_uniforms[k] = true; + } if (state->buffers[k].set == G9P_PUSH_CONSTANTS) { const struct pvr_push_constants *pc = &cmd_buffer->state.push_consts[PVR_STAGE_ALLOCATION_COMPUTE]; @@ -219,10 +415,26 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, uniforms[2 * k] = pc->dev_addr.addr; uniforms[2 * k + 1] = pc->dev_addr.addr >> 32; } - if (state->buffers[k].set < PVR_MAX_DESCRIPTOR_SETS) { + if (state->buffers[k].set < PVR_MAX_DESCRIPTOR_SETS && + (state->buffers[k].binding & G9P_BINDING_DESCRIPTOR)) { + /* a storage image's record: the descriptor's own address */ + const struct pvr_descriptor_set *set = desc->sets[state->buffers[k].set]; + const unsigned element = + (state->buffers[k].binding & ~G9P_BINDING_FLAGS) >> G9P_BINDING_ELEMENT_SHIFT; + const unsigned b = state->buffers[k].binding & BITFIELD_MASK(G9P_BINDING_ELEMENT_SHIFT); + uint64_t addr; + + if (!set || !set->layout || b >= set->layout->binding_count) + return vk_error(cmd_buffer, VK_ERROR_UNKNOWN); + addr = set->dev_addr.addr + set->layout->bindings[b].offset + + element * set->layout->bindings[b].stride; + uniforms[2 * k] = addr; + uniforms[2 * k + 1] = addr >> 32; + from_uniforms[k] = true; + } else if (state->buffers[k].set < PVR_MAX_DESCRIPTOR_SETS) { /* a dynamic buffer has no descriptor in set memory (binding offset ~0) */ const unsigned element = - (state->buffers[k].binding & ~G9P_BINDING_SIZE) >> G9P_BINDING_ELEMENT_SHIFT; + (state->buffers[k].binding & ~G9P_BINDING_FLAGS) >> G9P_BINDING_ELEMENT_SHIFT; const unsigned b = state->buffers[k].binding & BITFIELD_MASK(G9P_BINDING_ELEMENT_SHIFT); if (!pvr_descriptor_set_dynamic_slot(desc->sets[state->buffers[k].set], b, element, @@ -238,13 +450,39 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, if (result != VK_SUCCESS) return result; + /* + * The textures' states and sampler words, as the shader finds them from + * G9P_CS_TEX_BASE (g9p_tex_state(), g9p_tex_sampler()) + */ + const unsigned nt = state->num_textures; + uint64_t tex_src = 0; + + if (nt) { + uint32_t block[10 * G9P_MAX_TEXTURES] = { 0 }; + struct pvr_suballoc_bo *tex_bo; + + for (unsigned t = 0; t < nt; t++) + PVR_PER_ARCH(g9p_texture_words)(desc, state->textures[t].set, state->textures[t].binding, + state->textures[t].index, state->textures[t].sampler_set, + state->textures[t].sampler_binding, + state->textures[t].sampler_index, + &block[g9p_tex_state(0, t)], &block[g9p_tex_sampler(0, nt, t)]); + result = pvr_arch_cmd_buffer_upload_general(cmd_buffer, block, 40 * nt, &tex_bo); + if (result != VK_SUCCESS) + return result; + tex_src = tex_bo->dev_addr.addr; + } + /* * Binding task data: the source addresses (64-bit each), then one DOUTD * control word per transfer: Rogue DOUTD src1, last (bit 31), common - * store (bit 28), destination dword << 13, dwords. + * store (bit 28), destination dword << 13, dwords. The textures' block, + * if any, is the last transfer. */ - ctrl0 = 2 * (n + 1); - for (unsigned k = 0; k <= n; k++) { + const unsigned transfers = n + 1 + !!nt; + + ctrl0 = 2 * transfers; + for (unsigned k = 0; k < transfers; k++) { uint64_t src; uint32_t dest = 2 * k, dwords = 2; @@ -253,7 +491,7 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_descriptor_set *set = desc->sets[state->buffers[k].set]; /* an array's element (Zink keeps all storage buffers in one array) */ const unsigned element = - (state->buffers[k].binding & ~G9P_BINDING_SIZE) >> G9P_BINDING_ELEMENT_SHIFT; + (state->buffers[k].binding & ~G9P_BINDING_FLAGS) >> G9P_BINDING_ELEMENT_SHIFT; const struct pvr_descriptor_set_layout_binding *binding; if (!set) @@ -263,6 +501,13 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, /* pvr_buffer_descriptor: the address comes first, the size at byte 8 */ src = set->dev_addr.addr + binding->offset + element * binding->stride + (state->buffers[k].binding & G9P_BINDING_SIZE ? 8 : 0); + } else if (k > n) { + src = tex_src; + dest = G9P_CS_TEX_BASE; + dwords = 10 * nt; + } else if (k < n && indirect && state->buffers[k].set == G9P_NUM_WORKGROUPS) { + /* an indirect dispatch's x and y, or z, straight from its arguments */ + src = indirect + (state->buffers[k].binding ? 8 : 0); } else { src = uniforms_bo->dev_addr.addr + 8 * k; if (k == n) @@ -271,12 +516,12 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, bind_data[2 * k] = src; bind_data[2 * k + 1] = src >> 32; bind_data[ctrl0 + k] = - (k == n ? BITFIELD_BIT(31) : 0) | BITFIELD_BIT(28) | dest << 13 | dwords; + (k == transfers - 1 ? BITFIELD_BIT(31) : 0) | BITFIELD_BIT(28) | dest << 13 | dwords; bind_code[nc++] = g9p_pds_dout(G9P_DOUTD, k, G9P_PDS_DATA, ctrl0 + k, - k == n ? G9P_PDS_END : 0); + k == transfers - 1 ? G9P_PDS_END : 0); } bind_code[nc++] = g9p_pds_halt(); - bind_dwords = ALIGN_POT(ctrl0 + n + 1, 4); + bind_dwords = ALIGN_POT(ctrl0 + transfers, 4); result = pvr_arch_cmd_buffer_upload_pds(cmd_buffer, bind_data, bind_dwords, ROGUE_CDMCTRL_KERNEL1_DATA_ADDR_ALIGNMENT, @@ -294,6 +539,9 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, * -> word 2 (z; COMPUTE.md "y and z", Apple's EncodeComputePDSProgram) */ g9p_pds_kick_data(kernel_data, usc_offset(device, state->shader_bo)); + /* values in bank 0 too: the DOUTU temps hold them all (A9 compute's form) */ + if (state->bank1_regs) + kernel_data[1] = DIV_ROUND_UP(MAX2(state->num_regs, 8), 4) << 3; kernel_data[2] = 0x80000000; kernel_data[4] = 0x10000002; kernel_data[5] = 0x90004001; @@ -325,19 +573,39 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const uint32_t tg_alloc = MAX2(DIV_ROUND_UP(G9P_SHARED_BASE + state->shared_size, 16), 1); { struct pvr_csb *csb = &sub_cmd->control_stream; - const struct g9p_cdm_record bind_rec = - g9p_cdm_dma_task(G9P_CDM_DATA_BLOCKS(bind_dwords / 4), bind.data_offset, - bind.code_offset); + /* the buffers' addresses, the thread count after them and the textures' words */ + const struct g9p_cdm_record bind_rec = g9p_cdm_dma_task( + G9P_CDM_DATA_BLOCKS(bind_dwords / 4), bind.data_offset, bind.code_offset, + nt ? g9p_tex_sampler(G9P_CS_TEX_BASE, nt, nt) : 2 * n + 1); const struct g9p_cdm_record kernel_rec = g9p_cdm_kernel( - G9P_CDM_KERNEL_REGS(state->num_regs) | G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | + G9P_CDM_KERNEL_REGS(state->bank1_regs ? state->bank1_regs : state->num_regs) | + G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | G9P_CDM_DATA_BLOCKS(G9P_KERNEL_DATA_DWORDS / 4), kernel.data_offset, kernel.code_offset, group_count[0], group_count[1], - group_count[2], threads, tg_alloc); + group_count[2], g9p_compute_hw_threads(threads), tg_alloc); pvr_csb_set_relocation_mark(csb); emit_record(csb, &bind_rec); - emit_record(csb, &kernel_rec); + uint32_t *const kernel_words = pvr_arch_csb_alloc_dwords(csb, 8); + + if (kernel_words) + memcpy(kernel_words, kernel_rec.w, sizeof(kernel_rec.w)); pvr_csb_clear_relocation_mark(csb); + if (indirect) { + /* the arguments may come from a job before: always waited for */ + const struct g9p_dispatch_indirect patch = { + .groups = kernel_words ? kernel_words + 3 : NULL, + .kick = pvr_bo_suballoc_get_map_addr(kernel.pvr_bo), + .empty_kernel = usc_offset(device, device->g9p.empty_kernel), + .args = (const uint32_t *)PVR_PER_ARCH(g9p_cpu_addr)(device, indirect), + .gpu_written = true, + }; + + if (!patch.groups || !patch.args) + return vk_errorf(cmd_buffer, VK_ERROR_FEATURE_NOT_PRESENT, + "G9P: an indirect dispatch's arguments without a CPU mapping"); + util_dynarray_append_typed(&sub_cmd->g9p_indirects, struct g9p_dispatch_indirect, patch); + } } return VK_SUCCESS; @@ -369,3 +637,36 @@ PVR_PER_ARCH(g9p_end_pass)(struct pvr_cmd_buffer *cmd_buffer, memcpy(w, words, 32 * sizeof(uint32_t)); pvr_csb_clear_relocation_mark(csb); } + +/* + * An indirect dispatch's group counts from its arguments, when the job is + * submitted: the kernel record's words 3-5. A dispatch of no groups runs the + * empty kernel once instead (its record has no count of 0). + */ +void +PVR_PER_ARCH(g9p_patch_indirect_dispatches)(struct pvr_sub_cmd_compute *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_dispatch_indirect, p) { + const uint32_t x = p->args[0], y = p->args[1], z = p->args[2]; + + if (!x || !y || !z) { + p->groups[0] = p->groups[1] = p->groups[2] = 0; + p->kick[0] = p->empty_kernel; + } else { + p->groups[0] = x - 1; + p->groups[1] = y - 1; + p->groups[2] = z - 1; + } + } +} + +/* Arguments a GPU job before may have written */ +bool +PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait)(const struct pvr_sub_cmd_compute *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_dispatch_indirect, p) { + if (p->gpu_written) + return true; + } + return false; +} diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 9ba4f7d41f2..c847e40d535 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -121,8 +121,24 @@ usc_temps(unsigned regs) return DIV_ROUND_UP(MAX2(regs, 8), 4) << 3; } -/* Vulkan's standard 4x positions, x | y << 4 per sample in 1/16 pixel (iOS msaa4, PR #6) */ -#define G9P_POSITIONS_4X 0xeaa26e26u +/* + * Vulkan's standard sample positions, x | y << 4 per sample in 1/16 pixel (4x: iOS + * msaa4, PR #6), the same words the kernel gives the TA (its multisample control) + */ +static uint64_t +g9p_positions(unsigned samples) +{ + switch (samples) { + case 2: + return 0x44ccull; + case 4: + return 0xeaa26e26ull; + case 8: + return 0x1ffb71d3359db759ull; + default: + return 0x88ull; + } +} /* DOUTD control word: last, common store, destination dword, dwords */ static uint32_t @@ -188,11 +204,11 @@ g9p_pack_clear(VkFormat format, const float colour[4], uint32_t *words) } /* - * A fragment-side kick at four samples: DOUTU's sample rate 2, data word 1 bits 1-2 + * A fragment-side kick at several samples: DOUTU's sample rate 2, data word 1 bits 1-2 * (src0[34:33]; iOS 0x10 -> 0x14, PR #6), for the same program. Only the kick differs. */ static VkResult -g9p_kick_4x(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_pds_upload *kick, struct pvr_pds_upload *out) +g9p_kick_ms(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_pds_upload *kick, struct pvr_pds_upload *out) { const uint32_t *old = pvr_bo_suballoc_get_map_addr(kick->pvr_bo); const uint32_t data[2] = { old[0], old[1] | 2 << 1 }; @@ -204,6 +220,72 @@ g9p_kick_4x(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_pds_upload *kick return pvr_arch_cmd_buffer_upload_pds(cmd_buffer, data, ARRAY_SIZE(data), 16, code, ARRAY_SIZE(code), 32, 64, out); } +/* dst = a << n or a >> n (logical) in fragment code: the amount in register @tmp */ +static void +g9p_shift_by(struct g9p_code *c, unsigned dst, unsigned a, unsigned n, bool right, unsigned tmp) +{ + g9p_mov_imm(c, tmp, n); + g9p_shift(c, dst, a, tmp, right); +} + +/* + * A load at several samples, in its task per sample (g9p_kick_ms()): the pixel in + * r0-r1 and the sample's index in r2, for g9p_ms_sample(). 111c 0006 is the + * sample's y (measured: the samples of a pixel differ, where 0002 is the + * centre's), and 211c 6800 the pixel centre, both in 1/16 pixel. Each sample + * of a pixel has a y of its own (g9p_positions()), which gives the index: 4x + * (y >> 2) & 3, 2x (y 12 and 4) ((y >> 3) & 1) ^ 1, and 8x (y 5, 11, 9, 3, 13, + * 7, 15 and 1) a table of nibbles by y >> 1. Uses r6 and r8. + */ +static void +g9p_ms_load_coords(struct g9p_code *c, unsigned samples) +{ + G9P_EMIT(c, 0x111c, 0x6900 | 2 * 6, 0x0006); + g9p_wait(c); + G9P_EMIT(c, 0x211c, 0x6800); + g9p_wait(c); + g9p_shift_by(c, 0, 0, 4, true, 8); + g9p_shift_by(c, 1, 1, 4, true, 8); + switch (samples) { + case 2: + g9p_shift_by(c, 2, 6, 3, true, 8); + g9p_mov_imm(c, 8, 1); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + g9p_alu2(c, G9P_XOR, 2, 2, 8, false); + break; + case 8: + g9p_shift_by(c, 2, 6, 1, true, 8); + g9p_mov_imm(c, 8, 7); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + g9p_shift_by(c, 2, 2, 2, false, 8); + g9p_mov_imm(c, 8, 0x64125037); + g9p_shift(c, 2, 8, 2, true); + g9p_mov_imm(c, 8, 7); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + break; + default: + g9p_shift_by(c, 2, 6, 2, true, 8); + g9p_mov_imm(c, 8, 3); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + break; + } +} + +/* + * A sample of a multisampled texture into @dst: dimension 4 (W0 bits 9-11), + * integer coordinates (W1 bit 6) in r0-r1 and the sample index in r2, as the + * GLES driver read them, with the state at common-store dword @state and the + * sampler at @sampler. Its state is g9p_pack_image_view()'s. Read as a 2D texture + * of twice the size instead, one sample of each task of 32 lanes in a block of + * 8 x 4 pixels came from another (measured over whole renders). + */ +static void +g9p_ms_sample(struct g9p_code *c, bool half, unsigned dst, unsigned state, unsigned sampler) +{ + G9P_EMIT(c, half ? 0xf918 : 0xf91a, g9p_sample_w1(dst, state) | 0x40, 0x8100 | sampler, 0x0700, + 0x0200); +} + /* --- Device programs ------------------------------------------------------ */ VkResult @@ -245,7 +327,7 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) * RGBA32F, 051c for RGBA16F and RGBA8, iOS capture). */ for (unsigned raw = 0; raw < 2; raw++) { - /* A store of four samples: the emit's sample field 2 (iOS msaa4-store, PR #6) */ + /* A store of every sample: the emit's sample field 2 (iOS msaa4-store, PR #6) */ for (unsigned ms = 0; ms < 2; ms++) { c->n = 0; g9p_mov_imm(c, 0, 0); @@ -360,6 +442,18 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) result = upload_usc(device, c, &p->load_usc[o]); if (result != VK_SUCCESS) goto out; + + /* at 2, 4 and 8 samples: each sample its own, the state in the multisample form */ + for (unsigned m = 0; m < 3; m++) { + c->n = 0; + g9p_ms_load_coords(c, 2u << m); + g9p_ms_sample(c, half, 0, 0, 8); + g9p_wait(c); + g9p_end_fragment(c, g9p_output_words(o)); + result = upload_usc(device, c, &p->load_ms_usc[m][o]); + if (result != VK_SUCCESS) + goto out; + } } /* @@ -399,6 +493,8 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) result = upload_kick(device, p->keep_usc[o], 8, 0, &p->keep_kick[o]); if (result == VK_SUCCESS) result = upload_kick(device, p->load_usc[o], 8, 0, &p->load_kick[o]); + for (unsigned m = 0; m < 3 && result == VK_SUCCESS; m++) + result = upload_kick(device, p->load_ms_usc[m][o], 9, 0, &p->load_ms_kick[m][o]); if (result == VK_SUCCESS) result = upload_kick(device, p->clear_raw_usc[o], 8, 0, &p->clear_raw_kick[o]); } @@ -468,9 +564,13 @@ PVR_PER_ARCH(g9p_gfx_device_finish)(struct pvr_device *device) simple_mtx_destroy(&p->cached_lock); for (unsigned o = 0; o < G9P_OUTPUT_COUNT; o++) { pvr_bo_suballoc_free(p->load_usc[o]); + for (unsigned m = 0; m < 3; m++) + pvr_bo_suballoc_free(p->load_ms_usc[m][o]); pvr_bo_suballoc_free(p->keep_usc[o]); pvr_bo_suballoc_free(p->clear_raw_usc[o]); pvr_bo_suballoc_free(p->load_kick[o].pvr_bo); + for (unsigned m = 0; m < 3; m++) + pvr_bo_suballoc_free(p->load_ms_kick[m][o].pvr_bo); pvr_bo_suballoc_free(p->keep_kick[o].pvr_bo); pvr_bo_suballoc_free(p->clear_raw_kick[o].pvr_bo); } @@ -538,16 +638,26 @@ g9p_keep_kick(struct pvr_device *device, unsigned words, struct pvr_pds_upload * static const struct pvr_renderpass_hwsetup_render * g9p_current_hw_render(const struct pvr_render_pass_info *info); -/* The targets of a render the fragment program writes to memory (g9p_overflow_targets()) */ +static unsigned g9p_render_groups(const struct pvr_render_pass_info *info, + const struct pvr_renderpass_hwsetup_render *hw, + unsigned masks[G9P_MAX_GROUPS]); + +/* + * The targets of a render the fragment program writes to memory (g9p_overflow_targets()), + * none when the render runs once per group of them + */ static unsigned g9p_hw_overflow(const struct pvr_render_pass_info *info, const struct pvr_renderpass_hwsetup_render *hw) { enum g9p_output outputs[8] = { 0 }; - unsigned present = 0; + unsigned present = 0, masks[G9P_MAX_GROUPS]; + + if (g9p_render_groups(info, hw, masks) > 1) + return 0; for (unsigned t = 0; hw && t < MIN2(hw->eot_surface_count, 8); t++) { outputs[t] = g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format); - present |= BITFIELD_BIT(t); + present |= hw->eot_surfaces[t].need_resolve ? 0 : BITFIELD_BIT(t); } return g9p_overflow_targets(outputs, present); } @@ -600,7 +710,79 @@ g9p_render_words(const struct pvr_render_pass_info *info, unsigned words = 0; for (unsigned t = 0; hw && t < MIN2(hw->eot_surface_count, first); t++) { - if (!(overflow >> t & 1)) + if (!(overflow >> t & 1) && !hw->eot_surfaces[t].need_resolve) + words += g9p_output_words(g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format)); + } + return words; +} + +/* + * A resolve's end-of-tile surface (a multisampled target stored once more, + * resolved): no words of its own in the tile, its source's. The surface whose + * words surface @t stores. + */ +static unsigned +g9p_words_surface(const struct pvr_renderpass_hwsetup_render *hw, unsigned t) +{ + if (!hw->eot_surfaces[t].need_resolve) + return t; + for (unsigned s = 0; s < hw->eot_surface_count; s++) { + if (!hw->eot_surfaces[s].need_resolve && + hw->eot_surfaces[s].attachment_idx == hw->eot_surfaces[t].src_attachment_idx) + return s; + } + return t; +} + +/* Colour attachment @j's end-of-tile surface: the j-th that is not a resolve, or -1 */ +static int +g9p_colour_surface(const struct pvr_renderpass_hwsetup_render *hw, unsigned j) +{ + for (unsigned t = 0; hw && t < hw->eot_surface_count; t++) { + if (!hw->eot_surfaces[t].need_resolve && j-- == 0) + return t; + } + return -1; +} + +/* + * A render's target groups (g9p_target_groups()) over its colour targets, numbered as a + * pipeline's: with more words than a pixel holds, the draws run once per group. Not + * with multiview; G9P_NO_GROUPS: never (the targets past a pixel go to memory then). + */ +static unsigned +g9p_render_groups(const struct pvr_render_pass_info *info, const struct pvr_renderpass_hwsetup_render *hw, + unsigned masks[G9P_MAX_GROUPS]) +{ + enum g9p_output outputs[8] = { 0 }; + unsigned present = 0; + + for (unsigned j = 0; hw && j < G9P_MAX_TARGETS; j++) { + const int t = g9p_colour_surface(hw, j); + + if (t < 0) + break; + outputs[j] = g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format); + present |= BITFIELD_BIT(j); + } + const unsigned n = g9p_target_groups(outputs, present, masks); + + return n > 1 && util_bitcount(hw->view_mask) <= 1 && !getenv("G9P_NO_GROUPS") ? n : 1; +} + +/* The words of colour targets @mask before colour target @upto */ +static unsigned +g9p_group_words(const struct pvr_render_pass_info *info, const struct pvr_renderpass_hwsetup_render *hw, + unsigned mask, unsigned upto) +{ + unsigned words = 0; + + for (unsigned j = 0; hw && j < MIN2(upto, G9P_MAX_TARGETS); j++) { + const int t = g9p_colour_surface(hw, j); + + if (t < 0) + break; + if (mask >> j & 1) words += g9p_output_words(g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format)); } return words; @@ -764,7 +946,8 @@ compile_stage(struct pvr_device *device, nir_shader *nir, const VkPipelineShaderStageCreateInfo *stage, const struct g9p_vertex_attrib *attribs, int frag_coord_slot, bool point_coord, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, - const struct g9p_target *targets, struct g9p_shader *out, bool point_size, void *mem_ctx) + const struct g9p_target *targets, bool zs_attachment, struct g9p_shader *out, + bool point_size, void *mem_ctx) { bool ok, capture_ok = false; /* G9P_CAPTURE: the shader as the backend got it, kept for a refusal (g9p_replay) */ @@ -774,7 +957,8 @@ compile_stage(struct pvr_device *device, nir_shader *nir, ok = g9p_compile_vertex(nir, attribs, frag_coord_slot, point_coord, out, point_size, mem_ctx); else - ok = g9p_compile_fragment(nir, frag_coord_slot, blend, coverage, targets, out, mem_ctx); + ok = g9p_compile_fragment(nir, frag_coord_slot, blend, coverage, targets, zs_attachment, out, + mem_ctx); /* G9P_CAPTURE_REGS=N: also the shaders that compiled to N registers or more */ if (ok && capture && getenv("G9P_CAPTURE_REGS") && out->num_regs >= (unsigned)atoi(getenv("G9P_CAPTURE_REGS"))) @@ -787,6 +971,7 @@ compile_stage(struct pvr_device *device, nir_shader *nir, .point_size = point_size, .has_blend = blend != NULL, .has_coverage = coverage != NULL, + .zs_attachment = zs_attachment, }; if (attribs) @@ -808,19 +993,31 @@ compile_stage(struct pvr_device *device, nir_shader *nir, } /* + * The varyings by the TA's classes (TA_STATE_VARYING0: perspective, flat and + * screen-linear dwords, in that order in the vertex): perspective ones first + * (and those only the vertex shader writes), then flat ones (flat or + * integer), then noperspective ones, gl_FragCoord's after them all. With one + * count for all of them the TA took every varying as perspective, which it + * is not where it clips: a clipped triangle's flat values came from a new + * vertex, and noperspective ones were wrong (our flat varying clipping test). + * Sets the slots of the first two classes. Not with transform feedback, + * whose layout names the locations: everything is perspective then. + * * A varying without W (flat or noperspective) right after one with W loses that one's last * component: its first iterate lands there (dEQP fragment_out). An unused slot between - * them, in both stages' locations. Returns whether the last varying has W, which wants - * the same gap before gl_FragCoord's. Not with transform feedback, whose layout names - * the locations. + * them, in both stages' locations, counted with the perspective ones. Returns whether the + * last varying has W, which wants the same gap before gl_FragCoord's. */ static bool -g9p_gap_varyings(nir_shader *vs, nir_shader *fs) +g9p_gap_varyings(nir_shader *vs, nir_shader *fs, unsigned *persp_slots, unsigned *flat_slots) { bool with_w[G9P_MAX_VARYINGS], used[G9P_MAX_VARYINGS] = { 0 }; - unsigned shift[G9P_MAX_VARYINGS]; + uint8_t cls[G9P_MAX_VARYINGS] = { 0 }; /* 0 perspective, 1 flat, 2 screen-linear */ + unsigned shift[G9P_MAX_VARYINGS], order[G9P_MAX_VARYINGS], num = 0; bool prev = false; + *persp_slots = G9P_MAX_VARYINGS; + *flat_slots = 0; for (unsigned s = 0; s < G9P_MAX_VARYINGS; s++) with_w[s] = true; nir_foreach_variable_with_modes(var, fs, nir_var_shader_in) { @@ -828,13 +1025,14 @@ g9p_gap_varyings(nir_shader *vs, nir_shader *fs) continue; const unsigned first = var->data.location - VARYING_SLOT_VAR0; const unsigned n = glsl_count_attribute_slots(var->type, false); - const bool w = var->data.interpolation != INTERP_MODE_FLAT && - var->data.interpolation != INTERP_MODE_NOPERSPECTIVE && - !glsl_type_is_integer(glsl_without_array(var->type)); + const bool flat = var->data.interpolation == INTERP_MODE_FLAT || + glsl_type_is_integer(glsl_without_array(var->type)); + const bool w = !flat && var->data.interpolation != INTERP_MODE_NOPERSPECTIVE; for (unsigned s = first; s < MIN2(first + n, G9P_MAX_VARYINGS); s++) { with_w[s] = w; used[s] = true; + cls[s] = w ? 0 : flat ? 1 : 2; } } nir_foreach_variable_with_modes(var, vs, nir_var_shader_out) { @@ -846,24 +1044,47 @@ g9p_gap_varyings(nir_shader *vs, nir_shader *fs) for (unsigned s = first; s < MIN2(first + n, G9P_MAX_VARYINGS); s++) used[s] = true; } - unsigned gap = 0; - - for (unsigned s = 0; s < G9P_MAX_VARYINGS; s++) { - if (used[s]) { - gap += prev && !with_w[s]; - prev = with_w[s]; + if (vs->xfb_info) + return false; + /* the used slots by class, each class in its slots' order */ + for (unsigned c = 0; c < 3; c++) { + for (unsigned s = 0; s < G9P_MAX_VARYINGS; s++) { + if (used[s] && cls[s] == c) + order[num++] = s; } - shift[s] = gap; } - if (vs->xfb_info) + /* new locations: in that order, with the gap where W ends */ + unsigned next = 0; + + for (unsigned i = 0; i < num; i++) { + const unsigned s = order[i]; + + if (prev && !with_w[s]) + next++; + if (cls[s] == 1 && *persp_slots == G9P_MAX_VARYINGS) + *persp_slots = next; + if (cls[s] == 2 && *persp_slots == G9P_MAX_VARYINGS) + *persp_slots = next; + if (cls[s] == 1) + (*flat_slots)++; + prev = with_w[s]; + shift[s] = next++; + } + if (*persp_slots == G9P_MAX_VARYINGS) + *persp_slots = next; + /* past the last slot: no room for the gap, every varying stays where it is, as perspective */ + if (next > G9P_MAX_VARYINGS) { + *persp_slots = G9P_MAX_VARYINGS; + *flat_slots = 0; return false; + } nir_foreach_variable_with_modes(var, vs, nir_var_shader_out) { if (var->data.location >= VARYING_SLOT_VAR0 && var->data.location - VARYING_SLOT_VAR0 < G9P_MAX_VARYINGS) - var->data.location += shift[var->data.location - VARYING_SLOT_VAR0]; + var->data.location = VARYING_SLOT_VAR0 + shift[var->data.location - VARYING_SLOT_VAR0]; } nir_foreach_variable_with_modes(var, fs, nir_var_shader_in) { if (var->data.location >= VARYING_SLOT_VAR0 && var->data.location - VARYING_SLOT_VAR0 < G9P_MAX_VARYINGS) - var->data.location += shift[var->data.location - VARYING_SLOT_VAR0]; + var->data.location = VARYING_SLOT_VAR0 + shift[var->data.location - VARYING_SLOT_VAR0]; } return prev; } @@ -929,6 +1150,75 @@ upload_varyings(struct pvr_device *device, struct pvr_g9p_graphics_state *state, return VK_SUCCESS; } +/* The fragment shader stores to memory: run once per group of targets, it would store again */ +static bool +g9p_writes_memory(const nir_shader *nir) +{ + nir_foreach_function_impl (impl, nir) { + nir_foreach_block (block, impl) { + nir_foreach_instr (instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + switch (nir_instr_as_intrinsic(instr)->intrinsic) { + case nir_intrinsic_store_ssbo: + case nir_intrinsic_ssbo_atomic: + case nir_intrinsic_ssbo_atomic_swap: + case nir_intrinsic_store_global: + case nir_intrinsic_global_atomic: + case nir_intrinsic_global_atomic_swap: + case nir_intrinsic_image_deref_store: + case nir_intrinsic_image_deref_atomic: + case nir_intrinsic_image_deref_atomic_swap: + case nir_intrinsic_image_store: + case nir_intrinsic_image_atomic: + case nir_intrinsic_image_atomic_swap: + return true; + default: + break; + } + } + } + } + return false; +} + +/* Group @g's targets of @all: the others absent, their outputs dropped (into @buf) */ +static const struct g9p_target * +g9p_group_of(const struct g9p_target *all, const struct pvr_g9p_graphics_state *state, unsigned g, + struct g9p_target *buf) +{ + if (state->num_groups <= 1) + return all; + for (unsigned t = 0; t < G9P_MAX_TARGETS; t++) { + buf[t] = all[t]; + buf[t].present = all[t].present && (state->group_targets[g] >> t & 1); + } + return buf; +} + +/* + * Two groups' fragment programs take the same uniforms, textures and varyings and run + * as the same pass type: a draw's state words serve both but for the kick + */ +static bool +g9p_same_interface(const struct g9p_shader *a, const struct g9p_shader *b) +{ + if (a->num_buffers != b->num_buffers || a->num_fs_runs != b->num_fs_runs || + a->num_varyings != b->num_varyings || a->num_textures != b->num_textures || + a->uses_frag_coord != b->uses_frag_coord || a->uses_point_coord != b->uses_point_coord || + a->point_coord_slot != b->point_coord_slot || a->uses_discard != b->uses_discard || + a->writes_depth != b->writes_depth || a->uses_derivatives != b->uses_derivatives || + a->straight_samples != b->straight_samples || a->writes_coverage != b->writes_coverage || + a->uses_fbfetch != b->uses_fbfetch || a->punch_fbfetch != b->punch_fbfetch) + return false; + return !memcmp(a->buffers, b->buffers, a->num_buffers * sizeof(a->buffers[0])) && + !memcmp(a->fs_runs, b->fs_runs, a->num_fs_runs * sizeof(a->fs_runs[0])) && + !memcmp(a->varying_perspective, b->varying_perspective, sizeof(a->varying_perspective)) && + !memcmp(a->varying_flat, b->varying_flat, sizeof(a->varying_flat)) && + !memcmp(a->varying_linear, b->varying_linear, sizeof(a->varying_linear)) && + !memcmp(a->textures, b->textures, a->num_textures * sizeof(a->textures[0])); +} + static VkResult g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo *create_info, struct pvr_graphics_pipeline *pipeline, @@ -978,6 +1268,9 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo } const struct vk_vertex_binding_state *b = &vk_state->vi->bindings[a->binding]; + state->vertex_binding_end[a->binding] = + MAX2(state->vertex_binding_end[a->binding], + a->offset + vk_format_get_blocksize(a->format)); attribs[loc] = (struct g9p_vertex_attrib){ .present = true, .per_instance = b->input_rate == VK_VERTEX_INPUT_RATE_INSTANCE, @@ -1021,7 +1314,13 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_CUBE; } { - const bool last_with_w = g9p_gap_varyings(vs_nir, fs_nir) && !vs_nir->xfb_info; + unsigned persp_slots, flat_slots; + const bool last_with_w = g9p_gap_varyings(vs_nir, fs_nir, &persp_slots, &flat_slots) && + !vs_nir->xfb_info; + + state->varying_classes = persp_slots < G9P_MAX_VARYINGS; + state->varying_persp_slots = persp_slots; + state->varying_flat_slots = flat_slots; slot = MIN2(g9p_frag_coord_slot(vs_nir) + last_with_w, G9P_MAX_VARYINGS - 1); } @@ -1041,22 +1340,27 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo nir_metadata_control_flow, &zero); } } - /* Four samples with a coverage: the fragment program writes it (Zink: static state) */ + /* Several samples with a coverage: the fragment program writes it (Zink: static state) */ /* only a pipeline that drops samples writes its coverage: that takes punch-through */ - const struct g9p_coverage coverage = { true }; + /* so does one that discards (g9p_folds_discards()) */ + const unsigned samples = vk_state->ms ? vk_state->ms->rasterization_samples : 1; + const uint32_t all = BITFIELD_MASK(samples); + const struct g9p_coverage coverage = { true, samples }; const struct g9p_coverage *coverage_ptr = - vk_state->ms && vk_state->ms->rasterization_samples == VK_SAMPLE_COUNT_4_BIT && - ((vk_state->ms->sample_mask & 0xf) != 0xf || vk_state->ms->alpha_to_coverage_enable) + samples > 1 && + ((vk_state->ms->sample_mask & all) != all || vk_state->ms->alpha_to_coverage_enable || + g9p_folds_discards(fs_nir)) ? &coverage : NULL; - struct g9p_target targets[G9P_MAX_TARGETS] = { 0 }; + struct g9p_target targets[G9P_MAX_TARGETS] = { 0 }, group_targets[G9P_MAX_TARGETS]; for (unsigned t = 0; vk_state->rp && t < MIN2(vk_state->rp->color_attachment_count, G9P_MAX_TARGETS); t++) { const VkFormat f = vk_state->rp->color_attachment_formats[t]; if (f != VK_FORMAT_UNDEFINED) targets[t] = (struct g9p_target){ true, g9p_format_output(f), vk_format_is_int(f), - vk_format_is_float(f), .write_mask = 0xf }; + vk_format_is_float(f), .write_mask = 0xf, + .rgb565 = f == VK_FORMAT_R5G6B5_UNORM_PACK16 }; } for (unsigned t = 0; vk_state->cb && t < MIN2(vk_state->cb->attachment_count, G9P_MAX_TARGETS); t++) { @@ -1067,17 +1371,80 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo /* no attachment: as for BGRA8, the first target's programs stay valid */ if (!targets[0].present && !targets[1].present && !targets[2].present && !targets[3].present) targets[0] = (struct g9p_target){ true, G9P_OUTPUT_HALF2, false, false }; + const bool zs_attachment = + vk_state->rp && (vk_state->rp->depth_attachment_format != VK_FORMAT_UNDEFINED || + vk_state->rp->stencil_attachment_format != VK_FORMAT_UNDEFINED); + + /* + * More words than a pixel holds: a fragment program per group of targets + * (g9p_target_groups()), each writing only its own, when the draws can run once + * per group: no memory writes, transform feedback or multiview + */ + { + enum g9p_output outputs[8] = { 0 }; + unsigned present = 0; + + for (unsigned t = 0; t < G9P_MAX_TARGETS; t++) { + outputs[t] = targets[t].output; + present |= targets[t].present ? BITFIELD_BIT(t) : 0; + } + state->num_groups = g9p_target_groups(outputs, present, state->group_targets); + if (state->num_groups > 1 && + (g9p_writes_memory(fs_nir) || vs_xfb || vs_views[1] || getenv("G9P_NO_GROUPS"))) + state->num_groups = 1; + } + nir_shader *fs_all = state->num_groups > 1 ? nir_shader_clone(mem_ctx, fs_nir) : NULL; + result = compile_stage(device, fs_nir, fs_info, NULL, slot, false, - blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, targets, &fs, false, - mem_ctx); + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, + g9p_group_of(targets, state, 0, group_targets), + zs_attachment, &fs, false, mem_ctx); + for (unsigned g = 1; result == VK_SUCCESS && g < state->num_groups; g++) { + struct g9p_shader *gs = rzalloc(mem_ctx, struct g9p_shader); + + result = compile_stage(device, nir_shader_clone(mem_ctx, fs_all), fs_info, NULL, slot, false, + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, + g9p_group_of(targets, state, g, group_targets), + zs_attachment, gs, false, mem_ctx); + if (result != VK_SUCCESS) { + state->num_groups = 1; + result = VK_SUCCESS; + break; + } + if (!g9p_same_interface(&fs, gs)) { + if (getenv("G9P_PASSLOG")) + fprintf(stderr, "g9p: target group %u's program differs: no groups\n", g); + state->num_groups = 1; + break; + } + if (result == VK_SUCCESS) + result = pvr_gpu_upload_usc(device, gs->code, gs->code_size, 64, &state->fs_group_bo[g]); + if (result == VK_SUCCESS) + result = upload_kick(device, state->fs_group_bo[g], gs->num_regs, 0, &state->fs_group_kick[g]); + } + /* a group's program differs or failed: all targets in one, those past a pixel to memory */ + if (fs_all && state->num_groups == 1) { + for (unsigned g = 1; g < ARRAY_SIZE(state->fs_group_bo); g++) { + if (state->fs_group_kick[g].pvr_bo) + pvr_bo_suballoc_free(state->fs_group_kick[g].pvr_bo); + if (state->fs_group_bo[g]) + pvr_bo_suballoc_free(state->fs_group_bo[g]); + } + memset(state->fs_group_bo, 0, sizeof(state->fs_group_bo)); + memset(state->fs_group_kick, 0, sizeof(state->fs_group_kick)); + memset(&fs, 0, sizeof(fs)); + result = compile_stage(device, fs_all, fs_info, NULL, slot, false, + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, targets, + zs_attachment, &fs, false, mem_ctx); + } if (result == VK_SUCCESS) result = compile_stage(device, vs_nir, vs_info, attribs, - fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, &vs, + fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, false, &vs, point_size, mem_ctx); for (unsigned v = 1; result == VK_SUCCESS && vs_views[1] && v < PVR_MAX_MULTIVIEW; v++) { result = compile_stage(device, vs_views[v], vs_info, attribs, fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, - &vs_view[v], point_size, mem_ctx); + false, &vs_view[v], point_size, mem_ctx); if (result == VK_SUCCESS) vs.num_regs = MAX2(vs.num_regs, vs_view[v].num_regs); } @@ -1193,6 +1560,7 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo state->straight_samples = fs.straight_samples; state->writes_coverage = fs.writes_coverage; state->uses_fbfetch = fs.uses_fbfetch; + state->punch_fbfetch = fs.punch_fbfetch; /* * Whole blocks of 4: 17 and 18 registers drew wrong values with the field * in VDM_STATE5 covering them, 20 did not (dEQP vertex shaders). @@ -1311,6 +1679,12 @@ g9p_state_destroy(struct pvr_g9p_graphics_state *state) pvr_bo_suballoc_free(state->vs_view_bo[v]); } pvr_bo_suballoc_free(state->fs_bo); + for (unsigned g = 1; g < ARRAY_SIZE(state->fs_group_bo); g++) { + if (state->fs_group_kick[g].pvr_bo) + pvr_bo_suballoc_free(state->fs_group_kick[g].pvr_bo); + if (state->fs_group_bo[g]) + pvr_bo_suballoc_free(state->fs_group_bo[g]); + } pvr_bo_suballoc_free(state->vs_bo); } @@ -1427,6 +1801,34 @@ tex_sampler_at(const struct pvr_descriptor_state *desc, unsigned set_idx, unsign return set; } +/* + * A texture slot's words for a common store: the image's state and its sampler word, from the + * bound descriptor sets (as the vertex shader's block takes them); zero when unbound. Compute + * dispatches copy them to G9P_CS_TEX_BASE. + */ +void +PVR_PER_ARCH(g9p_texture_words)(const struct pvr_descriptor_state *desc, unsigned set_idx, + unsigned binding, unsigned index, int sampler_set, + int sampler_binding, unsigned sampler_index, uint32_t state[4], + uint32_t sampler[2]) +{ + const struct pvr_descriptor_set *set = desc->sets[set_idx]; + const struct pvr_descriptor_set *ss; + unsigned so = 0; + + memset(state, 0, 16); + memset(sampler, 0, 8); + if (set && set->mapping) { + const struct pvr_descriptor_set_layout_binding *b = &set->layout->bindings[binding]; + + memcpy(state, (const uint8_t *)set->mapping + b->offset + index * b->stride, 16); + } + ss = tex_sampler_at(desc, set_idx, binding, index, sampler_set, sampler_binding, sampler_index, + &so); + if (ss && ss->mapping) + memcpy(sampler, (const uint8_t *)ss->mapping + so, 8); +} + #define TEX_SAMPLER_AT(desc, tex, offset) \ tex_sampler_at(desc, (tex).set, (tex).binding, (tex).index, (tex).sampler_set, \ (tex).sampler_binding, (tex).sampler_index, offset) @@ -1667,6 +2069,24 @@ descriptor_buffer_addr(const struct pvr_descriptor_set *ds, unsigned slot_bindin ->addr; } +/* The size of the buffer behind a descriptor (get_ssbo_size, get_ubo_size) */ +static uint32_t +descriptor_buffer_size(const struct pvr_descriptor_set *ds, unsigned slot_binding) +{ + const unsigned element = slot_binding >> G9P_BINDING_ELEMENT_SHIFT; + const unsigned index = slot_binding & BITFIELD_MASK(G9P_BINDING_ELEMENT_SHIFT); + const struct pvr_descriptor_set_layout_binding *b = &ds->layout->bindings[index]; + + if (index >= ds->layout->binding_count) + return 0; + if (b->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC || + b->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) + return ds->dynamic_buffers[b->dynamic_buffer_idx + element].size; + return ((const struct pvr_buffer_descriptor *)((const uint8_t *)ds->mapping + b->offset + + element * b->stride)) + ->size; +} + /* * Vertex fetch reads words from the buffer's address + index x stride + the * attribute's offset, and only the offset's bytes are handled in the shader. A @@ -1693,6 +2113,72 @@ binding_needs_copy(const struct pvr_vertex_binding *vb, uint32_t stride) */ /* struct g9p_realign: pvr_g9p.h (G5P's draws queue them too) */ +/* + * An indirect draw's words from its arguments: the index list's address + * from firstIndex, the counts (indices past the index buffer not drawn, as + * a direct draw), the first vertex or vertex offset, and the first + * instance. A draw of no instances gets a count of 0, which draws nothing + * (measured, points too). + */ +static void +patch_indirect(const struct g9p_indirect *p) +{ + uint32_t count, instances, offset, first_instance; + uint32_t *w = p->words; + + if (!w) + return; + if (p->indexed) { + VkDrawIndexedIndirectCommand c; + + memcpy(&c, p->args, sizeof(c)); + const uint64_t in_buffer = (p->index_end - p->index_addr) / p->index_size; + const uint64_t addr = p->index_addr + (uint64_t)c.firstIndex * p->index_size; + + count = c.firstIndex < in_buffer ? MIN2(c.indexCount, in_buffer - c.firstIndex) : 0; + instances = c.instanceCount; + offset = c.vertexOffset; + first_instance = c.firstInstance; + w[0] = (w[0] & ~0xffu) | (uint32_t)(addr >> 32); + w[1] = (uint32_t)addr; + w += 2; + } else { + VkDrawIndirectCommand c; + + memcpy(&c, p->args, sizeof(c)); + count = c.vertexCount; + instances = c.instanceCount; + offset = c.firstVertex; + first_instance = c.firstInstance; + w += 1; + } + if (!instances) + count = 0; + w[0] = count; + w[1] = MAX2(instances, 1) - 1; + w[2] = offset; + if (p->first_instance) + *p->first_instance = first_instance; +} + +void +PVR_PER_ARCH(g9p_patch_indirect_draws)(struct pvr_sub_cmd_gfx *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_indirect, p) + patch_indirect(p); +} + +/* As g9p_realign_needs_wait(): arguments a GPU job before may have written */ +bool +PVR_PER_ARCH(g9p_indirect_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_indirect, p) { + if (p->buffer && p->buffer->g9p_gpu_written) + return true; + } + return false; +} + static void realign_copy(const struct g9p_realign *r) { @@ -1779,6 +2265,27 @@ realign_vertex_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_verte return VK_SUCCESS; } +/* + * The last element of vertex binding @binding whose attributes all lie in + * the bound range, which the vertex shader clamps its element to (0 when + * not even the first does: the fetch stays at the binding's start) + */ +static uint32_t +vertex_binding_last(const struct pvr_cmd_buffer *cmd_buffer, unsigned binding, uint32_t stride) +{ + const struct pvr_vertex_binding *vb = &cmd_buffer->state.vertex_bindings[binding]; + const struct pvr_graphics_pipeline *pipeline = cmd_buffer->state.gfx_pipeline; + const uint64_t end = pipeline ? pipeline->g9p.vertex_binding_end[binding] : 0; + uint64_t size; + + if (!vb->buffer || !stride) + return 0; + size = vb->size == VK_WHOLE_SIZE ? vb->buffer->vk.size - vb->offset : vb->size; + if (size < end) + return 0; + return MIN2((size - end) / stride, UINT32_MAX); +} + /* * The addresses of a stage's buffers by common-store slot (vertex buffers * and strides, push constants, the viewport, descriptor set buffers) @@ -1814,9 +2321,12 @@ buffer_addrs(struct pvr_cmd_buffer *cmd_buffer, unsigned n, const unsigned (*set dump32("vertex buffer", addrs[k], v, 16); } } else if (set == G9P_VERTEX_STRIDES) { - addrs[k] = cmd_buffer->vk.dynamic_graphics_state.vi_binding_strides[binding]; - if (binding_needs_copy(&cs->vertex_bindings[binding], addrs[k])) - addrs[k] = realigned_stride(addrs[k]); + const uint32_t stride = cmd_buffer->vk.dynamic_graphics_state.vi_binding_strides[binding]; + + addrs[k] = binding_needs_copy(&cs->vertex_bindings[binding], stride) + ? realigned_stride(stride) + : stride; + addrs[k] |= (uint64_t)vertex_binding_last(cmd_buffer, binding, stride) << 32; } else if (set == G9P_PUSH_CONSTANTS) { struct pvr_push_constants *pc = &cs->push_consts[stage]; @@ -1843,7 +2353,8 @@ buffer_addrs(struct pvr_cmd_buffer *cmd_buffer, unsigned n, const unsigned (*set } else if (set == G9P_OVERFLOW_TARGETS) { const struct pvr_render_pass_info *info = &cs->render_pass_info; const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); - const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[binding].attachment_idx]; + const int surface = g9p_colour_surface(hw, binding); + const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[surface >= 0 ? surface : binding].attachment_idx]; const struct pvr_image *im = pvr_image_view_get_image(v); const struct pvr_image_plane *pl = pvr_single_plane_const(im); const uint64_t addr = im->dev_addr.addr + pl->offset + pl->mip_levels[v->vk.base_mip_level].offset + @@ -1897,7 +2408,9 @@ buffer_addrs(struct pvr_cmd_buffer *cmd_buffer, unsigned n, const unsigned (*set } else { const struct pvr_descriptor_set *ds = cs->gfx_desc_state.sets[set]; - if (ds && ds->layout) + if (ds && ds->layout && (binding & G9P_BINDING_SIZE)) + addrs[k] = descriptor_buffer_size(ds, binding & ~G9P_BINDING_SIZE); + else if (ds && ds->layout) addrs[k] = descriptor_buffer_addr(ds, binding); if (addrs[k] && dumping()) { const uint32_t *v = (const uint32_t *)PVR_PER_ARCH(g9p_cpu_addr)(cmd_buffer->device, @@ -2100,6 +2613,8 @@ struct g9p_draw_programs { /* the depth test (a depth attachment and the test enabled), its write and compare op */ bool depth_test, depth_write; VkCompareOp depth_compare; + /* an indirect draw: the vertex shader's G9P_DRAW_PARAMS slot, filled at submission */ + uint32_t *first_instance; }; /* @@ -2276,7 +2791,7 @@ g9p_ds_draw_isp(const struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_gr { const struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info; const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); - const unsigned pass = state->uses_fbfetch && !state->writes_coverage ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; + const unsigned pass = state->uses_fbfetch && !state->writes_coverage && !(state->punch_fbfetch && state->uses_discard) ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; if (!hw || hw->ds_attach_idx == VK_ATTACHMENT_UNUSED || !vk_format_has_stencil(info->attachments[hw->ds_attach_idx]->vk.format)) @@ -2471,6 +2986,11 @@ g9p_cmd_draw_now(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_draw *d result = upload_vs_uniforms(cmd_buffer, state, &dma, &vs_block); if (result != VK_SUCCESS) return result; + /* the addresses come first in the uploaded block, two dwords each */ + for (unsigned k = 0; draw->indirect && vs_block && k < state->num_buffers; k++) { + if (state->buffers[k].set == G9P_DRAW_PARAMS) + dp.first_instance = (uint32_t *)vs_block + 2 * k; + } if (state->has_xfb) { result = xfb_draw(cmd_buffer, state, draw, vs_block, false); if (result != VK_SUCCESS) @@ -2634,8 +3154,19 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state at += ALIGN_POT(ARRAY_SIZE(_w), 4); \ } while (0) - /* 0: varying words (f32_linear = the varyings' components) */ - PPP_BLOCK(0x001c0000, 4 * state->num_varyings, 0, 0); + /* + * 0: varying words by class (g9p_gap_varyings()): f32_linear (perspective), + * f32_flat, f32_npc (screen-linear) + */ + { + const unsigned persp = state->varying_classes ? MIN2(state->varying_persp_slots, state->num_varyings) + : state->num_varyings; + const unsigned flat = state->varying_classes ? MIN2(state->varying_flat_slots, state->num_varyings - persp) + : 0; + + PPP_BLOCK(0x001c0000, 4 * persp | 4 * flat << 8 | 4 * (state->num_varyings - persp - flat) << 16, + 0, 0); + } /* * 1: PDS state pointers 0-1: shader base, texture/uniform program, * SIZEINFO1 (uniform data in 16-byte units, varying sizes), SIZEINFO2 @@ -2643,11 +3174,14 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state */ struct pvr_pds_upload fs_kick = state->fs_kick; - if (g9p_current_samples(info) == 4) { - result = g9p_kick_4x(cmd_buffer, &state->fs_kick, &fs_kick); + if (g9p_current_samples(info) > 1) { + result = g9p_kick_ms(cmd_buffer, &state->fs_kick, &fs_kick); if (result != VK_SUCCESS) return result; } + /* the fragment kick's word, for the copies of a render of target groups */ + const unsigned fs_kick_at = at + 1; + PPP_BLOCK(0xc0, fs_kick.data_offset, tex ? tex->code_offset : p->halt.code_offset, (tex ? MAX2(tex->data_size / 4, 1) : 1) << 23 | (state->num_varyings @@ -2686,7 +3220,7 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state */ /* a shader's depth (iOS `fragdepth`): pass 5, and bit 20 of the ISPA words off */ /* a written coverage with tile reads: punch-through, or the samples it drops are lost */ - pass = state->uses_fbfetch && !state->writes_coverage ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; + pass = state->uses_fbfetch && !state->writes_coverage && !(state->punch_fbfetch && state->uses_discard) ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; const uint32_t obj = g9p_isp_objtype(&cmd_buffer->vk.dynamic_graphics_state) << 28 | g9p_isp_line_width(&cmd_buffer->vk.dynamic_graphics_state) << 8; @@ -2720,14 +3254,17 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state memcpy(&ppp[blocks[nb - 1][0] + 1], dp->isp, sizeof(dp->isp)); else g9p_ds_draw_isp(cmd_buffer, state, dp->scissor >= 0, &ppp[blocks[nb - 1][0]]); - /* Four samples: user pass 7 for every draw that shades */ - if (g9p_current_samples(info) == 4) { + /* Several samples: user pass 7 for every draw that shades */ + if (g9p_current_samples(info) > 1) { uint32_t *isp0 = &ppp[blocks[nb - 1][0] + 1]; if (((*isp0 >> 22) & 0xf) == 0xf) *isp0 = (*isp0 & ~(0xfu << 22)) | 7u << 22; } - /* An occlusion query counts draws; never the clears (isp_set) */ + /* An occlusion query counts draws; never the clears (isp_set). Target groups: in group 0. */ + const unsigned isp_at = blocks[nb - 1][0] + 1; + const uint32_t isp_uncounted = ppp[isp_at]; + if (cmd_buffer->state.vis_test_enabled && !dp->isp_set && !dp->discard) { ppp[blocks[nb - 1][0] + 1] |= BITFIELD_BIT(15) | cmd_buffer->state.vis_reg; } @@ -2747,8 +3284,19 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state fui(dp->vp[0]), fui(dp->vp[1]), fui(dp->vp[2]), fui(dp->vp[3]), fui(dp->vp[4]), fui(dp->vp[5])); } - /* 5: w clamp (1e-20) and PPP control */ - PPP_BLOCK(0x00210000, 0x1e3ce508, 0x4a0 | dp->cull); + /* + * 5: w clamp (1e-20) and PPP control: bits 7-8 the provoking vertex + 1, whose flat + * values the triangles of a clipped one keep (0x4a0 kept the first: GL's last lost + * them where the TA clipped, in our flat varying clipping test) + */ + { + const struct vk_dynamic_graphics_state *dyn = &cmd_buffer->vk.dynamic_graphics_state; + const uint32_t pv = dyn->rs.provoking_vertex == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT ? 2 + : dyn->ia.primitive_topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN ? 1 + : 0; + + PPP_BLOCK(0x00210000, 0x1e3ce508, 0x420 | (pv + 1) << 7 | dp->cull); + } /* 6: depth bias / scissor index (ISPCTL bit 16 enables the scissor) */ PPP_BLOCK(0x20, (dp->dbias >= 0 ? (uint32_t)dp->dbias << 16 : 0) | (dp->scissor >= 0 ? (uint32_t)dp->scissor : 0)); @@ -2759,6 +3307,42 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state result = pvr_arch_cmd_buffer_upload_general(cmd_buffer, ppp, 4 * at, &ppp_bo); if (result != VK_SUCCESS) return result; + /* + * A render of target groups: per group past the first the same words with that group's + * fragment kick, the occlusion query left out (g9p_build_group_streams() names them) + */ + unsigned group_masks[G9P_MAX_GROUPS]; + const unsigned groups = g9p_render_groups(info, g9p_current_hw_render(info), group_masks); + uint64_t group_addr[G9P_MAX_GROUPS] = { ppp_bo->dev_addr.addr }; + + if (groups <= 1 && state->num_groups > 1) + mesa_logw_once("G9P: a program of target groups in a render without them (multiview)"); + if (groups > 1) { + const bool own = state->num_groups == groups && + !memcmp(state->group_targets, group_masks, groups * sizeof(group_masks[0])); + + if (!own) + mesa_logw_once("G9P: a draw whose program writes all targets in a render of target groups"); + for (unsigned g = 1; g < groups; g++) { + const struct pvr_pds_upload *kick = own ? &state->fs_group_kick[g] : &state->fs_kick; + struct pvr_pds_upload ms_kick = *kick; + struct pvr_suballoc_bo *bo; + uint32_t words[ARRAY_SIZE(ppp)]; + + if (g9p_current_samples(info) > 1) { + result = g9p_kick_ms(cmd_buffer, kick, &ms_kick); + if (result != VK_SUCCESS) + return result; + } + memcpy(words, ppp, 4 * at); + words[fs_kick_at] = ms_kick.data_offset; + words[isp_at] = isp_uncounted; + result = pvr_arch_cmd_buffer_upload_general(cmd_buffer, words, 4 * at, &bo); + if (result != VK_SUCCESS) + return result; + group_addr[g] = bo->dev_addr.addr; + } + } for (unsigned i = 0; i < nb; i++) dump32("ppp", ppp_bo->dev_addr.addr + 4 * blocks[i][0], &ppp[blocks[i][0]], blocks[i][1]); dump_pds("draw dma", dma); @@ -2862,11 +3446,37 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state (draw->indexed ? util_logbase2(draw->index_size) : 2u) << 17; if (draw->indexed) w[n++] = (uint32_t)draw->index_addr; + const unsigned list = n; + w[n++] = draw->count; w[n++] = MAX2(draw->instances, 1) - 1; w[n++] = draw->offset; w[n++] = 0xaa; - emit_words(csb, w, n); + if (draw->indirect) { + uint32_t *dst = pvr_arch_csb_alloc_dwords(csb, n); + const struct g9p_indirect ind = { + .words = dst ? dst + list - (draw->indexed ? 2 : 1) : NULL, + .first_instance = dp->first_instance, + .args = draw->indirect, + .buffer = draw->indirect_buffer, + .index_addr = draw->index_addr, + .index_end = draw->index_end, + .index_size = draw->index_size, + .indexed = draw->indexed, + }; + + if (!dst) + return csb->status; + memcpy(dst, w, 4 * n); + /* a secondary's render joins its primary's job: filled now */ + if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) + patch_indirect(&ind); + else + util_dynarray_append_typed(&cs->current_sub_cmd->gfx.g9p_indirects, + struct g9p_indirect, ind); + } else { + emit_words(csb, w, n); + } } /* * Multiview: where this draw's kick word ended up (the whole draw sits in the current @@ -2892,10 +3502,146 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state util_dynarray_append_typed(&cs->current_sub_cmd->gfx.g9p_view_kicks, struct g9p_view_kick, k); } } + /* Target groups: where each of the draw's state-word pointers ended up */ + if (groups > 1 && csb->status == VK_SUCCESS) { + struct g9p_group_ppp r = { .bo = csb->pvr_bo, .n = nb }; + const uint32_t *start = csb->start; + + memcpy(r.addr, group_addr, sizeof(r.addr)); + for (unsigned i = 0; i < nb; i++) { + const uint64_t a = ppp_bo->dev_addr.addr + 4 * blocks[i][0]; + const uint32_t hi = blocks[i][1] << 8 | (uint32_t)(a >> 32), lo = (uint32_t)a; + const uint32_t *q = csb->next; + + while (--q > start && !(q[0] == lo && q[-1] == hi)) + ; + if (q <= start) { + mesa_logw_once("G9P: a draw's state words not found for its target groups"); + r.n = 0; + break; + } + r.dword[i] = q - start; + r.offset[i] = 4 * blocks[i][0]; + r.words[i] = blocks[i][1]; + } + if (r.n) + util_dynarray_append_typed(&cs->current_sub_cmd->gfx.g9p_group_ppps, struct g9p_group_ppp, r); + } pvr_csb_clear_relocation_mark(csb); return csb->status; } +/* + * A copy of a sub-command's control stream buffers @orig into @copy (kept with the + * sub-command), each one's link to the next naming the next copy + */ +static VkResult +g9p_copy_stream(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_gfx *sub_cmd, + struct pvr_bo *const *orig, unsigned nbo, struct pvr_bo **copy) +{ + struct pvr_device *device = cmd_buffer->device; + unsigned i; + + for (i = 0; i < nbo; i++) { + VkResult result = pvr_bo_alloc(device, device->heaps.general_heap, orig[i]->bo->size, + pvr_get_slc_cache_line_size(&device->pdevice->dev_info), + PVR_BO_ALLOC_FLAG_CPU_MAPPED, ©[i]); + + if (result != VK_SUCCESS) + return result; + util_dynarray_append_typed(&sub_cmd->g9p_view_bos, struct pvr_bo *, copy[i]); + memcpy(copy[i]->bo->map, orig[i]->bo->map, orig[i]->bo->size); + } + /* each buffer's link to the next, packed as pvr_csb_emit_link_unmarked() does */ + for (i = 0; i + 1 < nbo; i++) { + uint32_t from[2], to[2], *w = copy[i]->bo->map; + const unsigned n = orig[i]->bo->size / 4; + unsigned j; + + pvr_csb_pack (&from[0], VDMCTRL_STREAM_LINK0, link) + link.link_addrmsb = orig[i + 1]->vma->dev_addr; + pvr_csb_pack (&from[1], VDMCTRL_STREAM_LINK1, link) + link.link_addrlsb = orig[i + 1]->vma->dev_addr; + pvr_csb_pack (&to[0], VDMCTRL_STREAM_LINK0, link) + link.link_addrmsb = copy[i + 1]->vma->dev_addr; + pvr_csb_pack (&to[1], VDMCTRL_STREAM_LINK1, link) + link.link_addrlsb = copy[i + 1]->vma->dev_addr; + for (j = 0; j + 1 < n && !(w[j] == from[0] && w[j + 1] == from[1]); j++) + ; + if (j + 1 == n) + return vk_errorf(cmd_buffer, VK_ERROR_UNKNOWN, "G9P: control stream link not found"); + w[j] = to[0]; + w[j + 1] = to[1]; + } + /* indirect draws: their words in the copy are filled at submission too (patch_indirect()) */ + const unsigned num_indirects = util_dynarray_num_elements(&sub_cmd->g9p_indirects, struct g9p_indirect); + + for (unsigned k = 0; k < num_indirects; k++) { + struct g9p_indirect ind = + *util_dynarray_element(&sub_cmd->g9p_indirects, struct g9p_indirect, k); + + for (i = 0; i < nbo; i++) { + const uint32_t *map = orig[i]->bo->map; + + if (ind.words >= map && ind.words < map + orig[i]->bo->size / 4) { + ind.words = (uint32_t *)copy[i]->bo->map + (ind.words - map); + util_dynarray_append_typed(&sub_cmd->g9p_indirects, struct g9p_indirect, ind); + break; + } + } + } + return VK_SUCCESS; +} + +/* + * A render of target groups: per group past the first a copy of the sub-command's control + * stream whose draws name that group's state words (struct g9p_group_ppp) + */ +VkResult +PVR_PER_ARCH(g9p_build_group_streams)(struct pvr_cmd_buffer *cmd_buffer, + struct pvr_sub_cmd_gfx *sub_cmd) +{ + struct pvr_csb *csb = &sub_cmd->control_stream; + const unsigned nbo = list_length(&csb->pvr_bo_list); + struct pvr_bo *orig[64], *copy[64]; + unsigned i = 0, groups = 1; + + util_dynarray_foreach (&sub_cmd->g9p_group_ppps, struct g9p_group_ppp, r) { + for (unsigned g = 1; g < G9P_MAX_GROUPS; g++) + groups = r->addr[g] ? MAX2(groups, g + 1) : groups; + } + if (groups <= 1) + return VK_SUCCESS; + if (csb->stream_type != PVR_CMD_STREAM_TYPE_GRAPHICS || nbo > ARRAY_SIZE(orig)) { + mesa_logw_once("G9P: target groups not rendered for this command buffer"); + return VK_SUCCESS; + } + list_for_each_entry (struct pvr_bo, bo, &csb->pvr_bo_list, link) + orig[i++] = bo; + for (unsigned g = 1; g < groups; g++) { + VkResult result = g9p_copy_stream(cmd_buffer, sub_cmd, orig, nbo, copy); + + if (result != VK_SUCCESS) + return result; + util_dynarray_foreach (&sub_cmd->g9p_group_ppps, struct g9p_group_ppp, r) { + for (i = 0; i < nbo && orig[i] != r->bo; i++) + ; + if (i == nbo || !r->addr[g]) + continue; + uint32_t *w = copy[i]->bo->map; + + for (unsigned b = 0; b < r->n; b++) { + const uint64_t a = r->addr[g] + r->offset[b]; + + w[r->dword[b] - 1] = r->words[b] << 8 | (uint32_t)(a >> 32); + w[r->dword[b]] = (uint32_t)a; + } + } + sub_cmd->g9p_group_stream[g] = copy[0]->vma->dev_addr.addr; + } + return VK_SUCCESS; +} + /* * Multiview with the view index in the vertex shader: per view a copy of the sub-command's * control stream, its links between buffers and its draws' kick words naming that view's @@ -2905,7 +3651,6 @@ VkResult PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_gfx *sub_cmd) { - struct pvr_device *device = cmd_buffer->device; struct pvr_csb *csb = &sub_cmd->control_stream; const unsigned nbo = list_length(&csb->pvr_bo_list); struct pvr_bo *orig[64], *copy[64]; @@ -2921,37 +3666,10 @@ PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, orig[i++] = bo; u_foreach_bit (view, sub_cmd->view_mask) { - for (i = 0; i < nbo; i++) { - VkResult result = pvr_bo_alloc(device, device->heaps.general_heap, orig[i]->bo->size, - pvr_get_slc_cache_line_size(&device->pdevice->dev_info), - PVR_BO_ALLOC_FLAG_CPU_MAPPED, ©[i]); + VkResult result = g9p_copy_stream(cmd_buffer, sub_cmd, orig, nbo, copy); - if (result != VK_SUCCESS) - return result; - util_dynarray_append_typed(&sub_cmd->g9p_view_bos, struct pvr_bo *, copy[i]); - memcpy(copy[i]->bo->map, orig[i]->bo->map, orig[i]->bo->size); - } - /* each buffer's link to the next, packed as pvr_csb_emit_link_unmarked() does */ - for (i = 0; i + 1 < nbo; i++) { - uint32_t from[2], to[2], *w = copy[i]->bo->map; - const unsigned n = orig[i]->bo->size / 4; - unsigned j; - - pvr_csb_pack (&from[0], VDMCTRL_STREAM_LINK0, link) - link.link_addrmsb = orig[i + 1]->vma->dev_addr; - pvr_csb_pack (&from[1], VDMCTRL_STREAM_LINK1, link) - link.link_addrlsb = orig[i + 1]->vma->dev_addr; - pvr_csb_pack (&to[0], VDMCTRL_STREAM_LINK0, link) - link.link_addrmsb = copy[i + 1]->vma->dev_addr; - pvr_csb_pack (&to[1], VDMCTRL_STREAM_LINK1, link) - link.link_addrlsb = copy[i + 1]->vma->dev_addr; - for (j = 0; j + 1 < n && !(w[j] == from[0] && w[j + 1] == from[1]); j++) - ; - if (j + 1 == n) - return vk_errorf(cmd_buffer, VK_ERROR_UNKNOWN, "G9P: control stream link not found"); - w[j] = to[0]; - w[j + 1] = to[1]; - } + if (result != VK_SUCCESS) + return result; util_dynarray_foreach (&sub_cmd->g9p_view_kicks, struct g9p_view_kick, k) { for (i = 0; i < nbo && orig[i] != k->bo; i++) ; @@ -2969,6 +3687,39 @@ PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, return VK_SUCCESS; } +/* + * A clear draw in a render of target groups: per group a program that keeps the tile's + * words, or clears colour target @colour's from the common store in its group (-1: none) + */ +static VkResult +g9p_clear_group_kicks(struct pvr_cmd_buffer *cmd_buffer, int colour, struct pvr_g9p_graphics_state *state) +{ + const struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info; + const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); + unsigned masks[G9P_MAX_GROUPS]; + const unsigned n = g9p_render_groups(info, hw, masks); + + if (n <= 1) + return VK_SUCCESS; + for (unsigned g = 0; g < n; g++) { + const unsigned words = g9p_group_words(info, hw, masks[g], G9P_MAX_TARGETS); + VkResult result; + + if (colour >= 0 && (masks[g] >> colour & 1)) + result = g9p_clear_one_kick(cmd_buffer->device, words, g9p_group_words(info, hw, masks[g], colour), + g9p_group_words(info, hw, BITFIELD_BIT(colour), G9P_MAX_TARGETS), + &state->fs_group_kick[g]); + else + result = g9p_keep_kick(cmd_buffer->device, words, &state->fs_group_kick[g]); + if (result != VK_SUCCESS) + return result; + state->group_targets[g] = masks[g]; + } + state->num_groups = n; + state->fs_kick = state->fs_group_kick[0]; + return VK_SUCCESS; +} + /* * vkCmdClearAttachments of depth and/or stencil: a draw of the rectangles * with depth ALWAYS (written when cleared) and stencil REPLACE by the @@ -3010,6 +3761,8 @@ g9p_ds_clear(struct pvr_cmd_buffer *cmd_buffer, const VkClearAttachment *att, if (targets > 1) { VkResult keep = g9p_keep_kick(cmd_buffer->device, g9p_render_words(info, hw, targets), &state.fs_kick); + if (keep == VK_SUCCESS) + keep = g9p_clear_group_kicks(cmd_buffer, -1, &state); if (keep != VK_SUCCESS) return keep; } @@ -3187,14 +3940,15 @@ PVR_PER_ARCH(g9p_clear_attachments)(struct pvr_cmd_buffer *cmd_buffer, uint32_t { const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); - if (hw && colour->colorAttachment < hw->eot_surface_count && - (g9p_hw_overflow(info, hw) >> colour->colorAttachment & 1)) { - g9p_fill_target(cmd_buffer, info->attachments[hw->eot_surfaces[colour->colorAttachment].attachment_idx], + const int surface = g9p_colour_surface(hw, colour->colorAttachment); + + if (surface >= 0 && (g9p_hw_overflow(info, hw) >> surface & 1)) { + g9p_fill_target(cmd_buffer, info->attachments[hw->eot_surfaces[surface].attachment_idx], colour->clearValue.color.uint32); return VK_SUCCESS; } - if (hw && colour->colorAttachment < hw->eot_surface_count) { - const unsigned t = colour->colorAttachment; + if (surface >= 0) { + const unsigned t = surface; const VkFormat format = info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format; if (hw->eot_surface_count > 1) { @@ -3204,6 +3958,8 @@ PVR_PER_ARCH(g9p_clear_attachments)(struct pvr_cmd_buffer *cmd_buffer, uint32_t g9p_render_words(info, hw, hw->eot_surface_count), g9p_render_words(info, hw, t), g9p_output_words(g9p_format_output(format)), &state.fs_kick); + if (result == VK_SUCCESS) + result = g9p_clear_group_kicks(cmd_buffer, colour->colorAttachment, &state); if (result != VK_SUCCESS) return result; state.uses_fbfetch = true; @@ -3296,15 +4052,20 @@ PVR_PER_ARCH(g9p_pack_image_view)(const struct pvr_image_view *iview, uint64_t w */ const uint64_t base = image->dev_addr.addr + plane->offset + plane_offset + iview->vk.base_array_layer * plane->layer_size; - /* - * Four samples, sampled: a twiddled texture of twice the size, each pixel's samples - * its 2x2 block (each pixel 4 samples in Morton order, y in bit 0: iOS msaa4-store) - */ - const uint32_t ms = image->vk.samples == VK_SAMPLE_COUNT_4_BIT ? 2 : 1; - const uint32_t w0 = image->vk.extent.width * ms, h0 = image->vk.extent.height * ms; + const uint32_t w0 = image->vk.extent.width, h0 = image->vk.extent.height; d[0] = (d[0] & ~(0x1fu << 27)) | (w0 - 1) << 27; d[1] = (w0 - 1) >> 5 | (h0 - 1) << 9 | iview->vk.base_mip_level << 25; + /* + * Several samples: the texture unit's multisample form (g9p_ms_sample()), + * dimension 4 and log2(samples) - 1 at bit 55, the pixels' size. The + * surface is twiddled by pixel, each pixel's samples one after another + * (iOS msaa4-store at four samples). + */ + if (image->vk.samples > 1) { + d[0] = (d[0] & ~7u) | 4; + d[1] |= (util_logbase2(image->vk.samples) - 1) << 23; + } d[2] = (uint32_t)(base >> 1) | (iview->vk.base_mip_level + iview->vk.level_count - 1) | (image->vk.mip_levels > 1 ? 0x10 : 0); d[3] = (uint32_t)(base >> 33); @@ -3416,6 +4177,26 @@ PVR_PER_ARCH(g9p_pack_sampler)(const VkSamplerCreateInfo *info) * dword 5 0xc0000000, as iPadOS writes them. */ +/* + * The format an end of tile stores a target as. The emit of every sample takes the + * tile in 16-bit channels: R32F stored as itself came out wrong. A target of one or two + * raw words goes out as 16-bit integers then, bit for bit (RG16 and RGBA16 uint). + */ +static VkFormat +g9p_eot_format(VkFormat format, unsigned samples) +{ + if (samples <= 1) + return format; + switch (g9p_format_output(format)) { + case G9P_OUTPUT_RAW1: + return VK_FORMAT_R16G16_UINT; + case G9P_OUTPUT_RAW2: + return VK_FORMAT_R16G16B16A16_UINT; + default: + return format; + } +} + static void pack_image_state(uint32_t *w, uint64_t addr, uint32_t width, uint32_t height, bool linear, uint32_t pitch, VkFormat format) @@ -3459,13 +4240,22 @@ g9p_ds_check(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwse if (!g9p_ds_format(iview->vk.format)) return g9p_feature_error(cmd_buffer, "G9P: depth/stencil format %s", vk_Format_to_str(iview->vk.format)); - /* Sides under 32 pixels: the image holds the ZLS's whole tiles (pvr_image.c) */ - if (iview->vk.base_mip_level || iview->vk.base_array_layer) + /* + * A stencil plane takes ZLS_PIXELS aligned to 32 (g9p_zls_pixels()): in a + * chain of levels one under 32 pixels would be stored past its end. D32F_S8 + * renders such a level in a scratch plane (g9p_ds_scratch()). + */ + const struct pvr_image *image = pvr_image_view_get_image(iview); + const unsigned level = iview->vk.base_mip_level; + + if (vk_format_has_stencil(iview->vk.format) && iview->vk.format != VK_FORMAT_D32_SFLOAT_S8_UINT && + image->vk.mip_levels > 1 && + (u_minify(plane->physical_extent.width, level) < 32 || + u_minify(plane->physical_extent.height, level) < 32)) return g9p_feature_error(cmd_buffer, - "G9P: a depth/stencil attachment of %ux%u (level %u, layer %u): " - "level 0, layer 0 only", - plane->physical_extent.width, plane->physical_extent.height, - iview->vk.base_mip_level, iview->vk.base_array_layer); + "G9P: level %u of a %ux%u depth/stencil chain: " + "stencil levels of 32 pixels and more only", + level, plane->physical_extent.width, plane->physical_extent.height); return VK_SUCCESS; } @@ -3498,6 +4288,9 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa for (unsigned t = 0; t < n; t++) { const uint32_t idx = hw->eot_surfaces[t].attachment_idx; + /* a resolve has no words of its own (g9p_words_surface()) */ + if (hw->eot_surfaces[t].need_resolve) + continue; for (unsigned j = 0; j < hw->color_init_count; j++) loads |= (hw->color_init[j].index == idx && hw->color_init[j].op == VK_ATTACHMENT_LOAD_OP_LOAD) << t; } @@ -3505,14 +4298,20 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[0].attachment_idx]; key |= (uint64_t)v->vk.extent.width << 40 | (uint64_t)v->vk.extent.height << 26; - G9P_EMIT(c, 0x211c, 0x6800); - g9p_wait(c); - g9p_convert(c, 2, 0, G9P_U2F); - g9p_convert(c, 3, 1, G9P_U2F); - g9p_mov_imm(c, 4, fui(1.0f / (16 * v->vk.extent.width))); - g9p_mov_imm(c, 5, fui(1.0f / (16 * v->vk.extent.height))); - g9p_alu2(c, G9P_FMUL, 0, 2, 4, false); - g9p_alu2(c, G9P_FMUL, 1, 3, 5, false); + if (job->g9p.samples > 1) { + /* each sample its own (g9p_ms_load_coords()), the multisampled targets so read */ + g9p_ms_load_coords(c, job->g9p.samples); + key |= (uint64_t)util_logbase2(job->g9p.samples) << 56; + } else { + G9P_EMIT(c, 0x211c, 0x6800); + g9p_wait(c); + g9p_convert(c, 2, 0, G9P_U2F); + g9p_convert(c, 3, 1, G9P_U2F); + g9p_mov_imm(c, 4, fui(1.0f / (16 * v->vk.extent.width))); + g9p_mov_imm(c, 5, fui(1.0f / (16 * v->vk.extent.height))); + g9p_alu2(c, G9P_FMUL, 0, 2, 4, false); + g9p_alu2(c, G9P_FMUL, 1, 3, 5, false); + } } for (unsigned t = 0; t < n; t++) { const uint32_t idx = hw->eot_surfaces[t].attachment_idx; @@ -3520,15 +4319,24 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa const enum g9p_output o = g9p_format_output(v->vk.format); const unsigned w = g9p_output_words(o); + if (hw->eot_surfaces[t].need_resolve) { + key |= (uint64_t)1 << (20 + t); + continue; + } + key |= (uint64_t)(o | (loads >> t & 1) << 3) << (4 * t); at[t] = tmp; if (loads >> t & 1) { uint64_t tex[2]; PVR_PER_ARCH(g9p_pack_image_view)(v, tex); + if (job->g9p.samples > 1) { + g9p_ms_sample(c, o < G9P_OUTPUT_RAW1, tmp, 8 * t, 8 * n); + } else { + G9P_EMIT(c, o >= G9P_OUTPUT_RAW1 ? 0xf51a : 0xf518, g9p_sample_w1(tmp, 8 * t), + 0x8100 | 8 * n, 0x0700, 0x0200); + } memcpy(&data[8 * t], tex, sizeof(tex)); - G9P_EMIT(c, o >= G9P_OUTPUT_RAW1 ? 0xf51a : 0xf518, g9p_sample_w1(tmp, 8 * t), - 0x8100 | 8 * n, 0x0700, 0x0200); tmp += 4; } else { float clear[4] = { 0 }; @@ -3555,6 +4363,9 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa for (unsigned t = 0; t < n; t++) { const unsigned w = g9p_output_words(g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format)); + if (hw->eot_surfaces[t].need_resolve) + continue; + for (unsigned k = 0; k < w; k++) g9p_alu2(c, G9P_OR, base + k, at[t] + k, at[t] + k, false); base += w; @@ -3578,8 +4389,8 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa } const uint64_t units = DIV_ROUND_UP(8 * n + 2, 16); - if (job->g9p.samples == 4) { - result = g9p_kick_4x(cmd_buffer, &kick, &kick); + if (job->g9p.samples > 1) { + result = g9p_kick_ms(cmd_buffer, &kick, &kick); if (result != VK_SUCCESS) return result; } @@ -3590,10 +4401,102 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa return VK_SUCCESS; } +/* + * Where the end of tile stores colour target @t, at @addr in its level. Its + * emit takes only a 64-byte aligned address: a level at 0x1550 in a 64x16 + * RGBA8 chain (2x1) or at 0xa0 in a 16x2 one (4x1) kept none of a render's + * pixels, where every level at a multiple of 64 bytes was exact, and the + * sampler reads them all where they are. Such levels (a chain's tail, past + * its first level under 64 bytes) store to a scratch slot instead, which + * g9p_colour_scratch() fills from the level before the render and copies + * back after it. Not with multiview: the views share the slot. + */ +static uint64_t +g9p_eot_addr(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwsetup_render *hw, + struct pvr_render_job *job, unsigned t, uint64_t addr) +{ + uint64_t scratch; + + if (!(addr & 63)) + return addr; + if (util_bitcount(hw->view_mask) > 1) { + mesa_logw_once("G9P: multiview render into a level not 64-byte aligned"); + return addr; + } + scratch = PVR_PER_ARCH(g9p_zls_scratch)(cmd_buffer->device); + if (!scratch) + return addr; + job->g9p.colour_scratch |= BITFIELD_BIT(t); + return g9p_colour_scratch_slot(scratch, t); +} + +static VkResult +g9p_job_init_targets(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_renderpass_hwsetup_render *hw_render, + struct pvr_render_job *job); + +/* + * A render's job. With target groups (g9p_render_groups()) the same for each group's + * surfaces alone, a render each (pvr_process_graphics_cmd()): only the last stores depth + * and stencil, the others load what the render started with. + */ VkResult PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwsetup_render *hw_render, struct pvr_render_job *job) +{ + const struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info; + unsigned masks[G9P_MAX_GROUPS]; + const unsigned n = g9p_render_groups(info, hw_render, masks); + + job->g9p_groups = 1; + if (n <= 1) + return g9p_job_init_targets(cmd_buffer, hw_render, job); + for (unsigned g = n; g-- > 0;) { + struct pvr_renderpass_hwsetup_render part = *hw_render; + struct pvr_renderpass_hwsetup_eot_surface eot[2 * G9P_MAX_TARGETS]; + struct pvr_renderpass_colorinit init[2 * G9P_MAX_TARGETS]; + VkResult result; + + part.eot_surfaces = eot; + part.color_init = init; + part.eot_surface_count = 0; + part.color_init_count = 0; + for (unsigned t = 0; t < hw_render->eot_surface_count && part.eot_surface_count < ARRAY_SIZE(eot); t++) { + /* a surface's colour target, a resolve's its source's */ + const unsigned s = g9p_words_surface(hw_render, t); + unsigned j = 0; + + for (unsigned k = 0; k < s; k++) + j += !hw_render->eot_surfaces[k].need_resolve; + if (masks[g] >> j & 1) + eot[part.eot_surface_count++] = hw_render->eot_surfaces[t]; + } + for (unsigned i = 0; i < hw_render->color_init_count && part.color_init_count < ARRAY_SIZE(init); i++) { + for (unsigned t = 0; t < part.eot_surface_count; t++) { + if (!eot[t].need_resolve && eot[t].attachment_idx == hw_render->color_init[i].index) { + init[part.color_init_count++] = hw_render->color_init[i]; + break; + } + } + } + result = g9p_job_init_targets(cmd_buffer, &part, job); + if (result != VK_SUCCESS) + return result; + if (job->g9p.colour_scratch) + mesa_logw_once("G9P: target groups with a level the end of tile can't store to"); + job->g9p.zs_keep = g + 1 < n; + job->g9p_group[g] = job->g9p; + job->g9p_group_eot_data[g] = job->view_state.view[0].pds_pixel_event_data_offset; + } + job->g9p_groups = n; + return VK_SUCCESS; +} + +static VkResult +g9p_job_init_targets(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_renderpass_hwsetup_render *hw_render, + struct pvr_render_job *job) { struct pvr_device *device = cmd_buffer->device; const struct pvr_g9p_gfx_programs *p = &device->g9p.gfx; @@ -3616,14 +4519,21 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_renderpass_hwsetup_eot_surface live_eot[G9P_MAX_TARGETS]; struct pvr_renderpass_colorinit live_init[G9P_MAX_TARGETS]; const unsigned overflow = g9p_hw_overflow(info, hw_render); + /* resolves by transfer (more surfaces than an end of tile stores): no part of it either */ + unsigned by_transfer = 0; - if (overflow) { + for (unsigned t = 0; t < hw_render->eot_surface_count; t++) { + if (hw_render->eot_surfaces[t].need_resolve && + hw_render->eot_surfaces[t].resolve_type == PVR_RESOLVE_TYPE_TRANSFER) + by_transfer |= BITFIELD_BIT(t); + } + if (overflow || by_transfer) { live_render.eot_surfaces = live_eot; live_render.color_init = live_init; live_render.eot_surface_count = 0; live_render.color_init_count = 0; for (unsigned t = 0; t < hw_render->eot_surface_count; t++) { - if (!(overflow >> t & 1)) + if (!((overflow | by_transfer) >> t & 1) && live_render.eot_surface_count < G9P_MAX_TARGETS) live_eot[live_render.eot_surface_count++] = hw_render->eot_surfaces[t]; } for (unsigned i = 0; i < hw_render->color_init_count; i++) { @@ -3648,7 +4558,7 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, memset(&job->g9p, 0, sizeof(job->g9p)); job->g9p.enabled = true; job->g9p.samples = hw_render->sample_count; - if (hw_render->sample_count != 1 && hw_render->sample_count != 4) + if (!util_is_power_of_two_nonzero(hw_render->sample_count) || hw_render->sample_count > 8) return g9p_feature_error(cmd_buffer, "G9P: %u samples", hw_render->sample_count); /* multiview: each view's job stores to its own layer */ @@ -3673,9 +4583,10 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, job->g9p.target_addr = addr; job->g9p.target_pitch = pitch; job->g9p.target_linear = linear; - pack_image_state(image, addr, iview->vk.extent.width, iview->vk.extent.height, linear, - pitch, iview->vk.format); - if (image_obj->vk.samples == 4) + pack_image_state(image, g9p_eot_addr(cmd_buffer, hw_render, job, 0, addr), + iview->vk.extent.width, iview->vk.extent.height, linear, pitch, + g9p_eot_format(iview->vk.format, image_obj->vk.samples)); + if (image_obj->vk.samples > 1) image[0] = (image[0] & ~0xfu) | 0x0c; /* 2D multisample, twiddled (iOS msaa4-store) */ /* the other targets' blocks: their state, their output-half offset (PR #6) */ for (unsigned t = 1; t < eot_targets; t++) { @@ -3684,14 +4595,17 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_image_plane *pl = pvr_single_plane_const(im); pack_image_state(&image[8 * t], - im->dev_addr.addr + pl->offset + pl->mip_levels[v->vk.base_mip_level].offset + - (v->vk.base_array_layer + view) * pl->layer_size, + g9p_eot_addr(cmd_buffer, hw_render, job, t, + im->dev_addr.addr + pl->offset + + pl->mip_levels[v->vk.base_mip_level].offset + + (v->vk.base_array_layer + view) * pl->layer_size), v->vk.extent.width, v->vk.extent.height, im->memlayout == PVR_MEMLAYOUT_LINEAR, - pl->mip_levels[v->vk.base_mip_level].pitch, v->vk.format); - if (im->vk.samples == 4) + pl->mip_levels[v->vk.base_mip_level].pitch, + g9p_eot_format(v->vk.format, im->vk.samples)); + if (im->vk.samples > 1) image[8 * t] = (image[8 * t] & ~0xfu) | 0x0c; - image[8 * t + 6] = 2 * g9p_render_words(info, hw_render, t); + image[8 * t + 6] = 2 * g9p_render_words(info, hw_render, g9p_words_surface(hw_render, t)); image[8 * t - 1] = 0xffffffff; } job->g9p.width = iview->vk.extent.width; @@ -3720,16 +4634,17 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const VkFormat f = info->attachments[hw_render->eot_surfaces[t].attachment_idx]->vk.format; - if (g9p_format_output(f) >= G9P_OUTPUT_RAW1) + /* several samples: the emit of 16-bit channels (g9p_eot_format()) */ + if (g9p_format_output(f) >= G9P_OUTPUT_RAW1 && job->g9p.samples == 1) raw |= BITFIELD_BIT(t); } const uint32_t data[8] = { bo->dev_addr.addr, bo->dev_addr.addr >> 32, doutd_ctrl(0, eot_dwords, true), 0, usc_offset(device, eot_targets > 1 - ? (job->g9p.samples == 4 ? p->eot_mrt_ms_usc[eot_targets][raw] + ? (job->g9p.samples > 1 ? p->eot_mrt_ms_usc[eot_targets][raw] : p->eot_mrt_usc[eot_targets][raw]) - : (job->g9p.samples == 4 ? p->eot_ms_usc[raw & 1] + : (job->g9p.samples > 1 ? p->eot_ms_usc[raw & 1] : p->eot_usc[raw & 1])), usc_temps(8), 0, 0 }; @@ -3890,12 +4805,13 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, if (result != VK_SUCCESS) return result; } else { - struct pvr_pds_upload bg_kick = load ? p->load_kick[bg_output] + struct pvr_pds_upload bg_kick = load && job->g9p.samples > 1 ? p->load_ms_kick[util_logbase2(job->g9p.samples) - 1][bg_output] + : load ? p->load_kick[bg_output] : bg_raw ? p->clear_raw_kick[bg_output] : p->clear_kick; - if (job->g9p.samples == 4) { - result = g9p_kick_4x(cmd_buffer, &bg_kick, &bg_kick); + if (job->g9p.samples > 1) { + result = g9p_kick_ms(cmd_buffer, &bg_kick, &bg_kick); if (result != VK_SUCCESS) return result; } @@ -3984,7 +4900,7 @@ PVR_PER_ARCH(g9p_geom_stream)(const struct pvr_device *device, put64(&s, 0); for (unsigned i = 0; i < 6; i++) put64(&s, 0); /* cluster */ - put32(&s, job->g9p.samples == 4 ? G9P_POSITIONS_4X : 0x88); /* 0xc78 */ + put32(&s, (uint32_t)g9p_positions(job->g9p.samples)); /* 0xc78 */ dump32("geom stream", 0, (const uint32_t *)buf, s.len / 4); return s.len; } @@ -4014,10 +4930,32 @@ PVR_PER_ARCH(g9p_geom_stream)(const struct pvr_device *device, * Zink renders GL's DEPTH24_STENCIL8 with D32F_S8. * Sampled as the matching colour format. * Sides under 32 pixels: the ZLS writes whole 32-pixel tiles, so such an - * image is allocated with them (pvr_image.c). Refused by name: mip levels - * and layers other than the first. + * image is allocated with them (pvr_image.c). In a chain of levels, a depth + * plane alone takes its level's exact size instead, and stencil levels under + * 32 pixels are refused by name. */ +/* + * The ZLS's surface: the attachment's level and layer, at the offsets the + * sampler reads them from (g9p_pack_image_view()) + */ +static uint64_t +g9p_ds_addr(const struct pvr_render_job *job) +{ + const struct pvr_image_view *iview = job->ds.iview; + const struct pvr_image *img; + const struct pvr_image_plane *pl; + + if (job->ds.g9p_scratch) + return job->ds.g9p_scratch; + if (!iview) + return job->ds.addr.addr; + img = pvr_image_view_get_image(iview); + pl = pvr_single_plane_const(img); + return img->dev_addr.addr + pl->offset + pl->mip_levels[iview->vk.base_mip_level].offset + + iview->vk.base_array_layer * pl->layer_size; +} + /* The stencil plane's offset in a D32F_S8 image: the second half, after the depth's whole chain */ static uint64_t g9p_ds_stencil_offset(const struct pvr_image *img) @@ -4051,9 +4989,9 @@ g9p_ds_zls(const struct pvr_render_job *job, uint64_t *zlsctl, uint64_t *stencil ? ROGUE_CR_ZLS_FORMAT_TYPE_16BITINT : ROGUE_CR_ZLS_FORMAT_TYPE_F32Z; z.zloaden = job->has_depth_attachment && job->ds.load.d; - z.zstoreen = job->has_depth_attachment && job->ds.store.d; + z.zstoreen = job->has_depth_attachment && job->ds.store.d && !job->g9p.zs_keep; z.sloaden = job->has_stencil_attachment && job->ds.load.s; - z.sstoreen = job->has_stencil_attachment && job->ds.store.s; + z.sstoreen = job->has_stencil_attachment && job->ds.store.s && !job->g9p.zs_keep; z.forcezload = z.zloaden || z.sloaden; z.forcezstore = z.zstoreen || z.sstoreen; z.loadtwiddled = z.storetwiddled = true; @@ -4061,7 +4999,8 @@ g9p_ds_zls(const struct pvr_render_job *job, uint64_t *zlsctl, uint64_t *stencil memcpy(zlsctl, ctl, sizeof(ctl)); if (job->has_stencil_attachment) { pvr_csb_pack (&base, CR_ISP_STENCIL_LOAD_BASE, s) { - s.addr = PVR_DEV_ADDR_OFFSET(job->ds.addr, g9p_ds_stencil_offset(img)); + s.addr = PVR_DEV_ADDR(g9p_ds_addr(job) + (job->ds.g9p_scratch ? job->ds.g9p_scratch_stencil + : g9p_ds_stencil_offset(img))); s.enable = true; } } @@ -4091,6 +5030,14 @@ g9p_zls_pixels(const struct pvr_render_job *job) const bool stencil = job->ds.iview && vk_format_has_stencil(job->ds.iview->vk.format); uint32_t w = job->ds.physical_extent.width, h = job->ds.physical_extent.height; + /* + * A depth plane alone in a chain of levels: its level's exact size, the + * ZLS stores a level smaller than a tile then (measured down to 1x1), + * where the whole tiles would reach into the next level + */ + if (!stencil && job->ds.iview && + pvr_image_view_get_image(job->ds.iview)->vk.mip_levels > 1) + return (w - 1) | (uint64_t)(h - 1) << 15; if (stencil || w < 32 || h < 32) { w = ALIGN_POT(w, 32); h = ALIGN_POT(h, 32); @@ -4130,7 +5077,7 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, g9p_ds_zls(job, &zlsctl, &stencil_base, &bgobjdepth, &bgobjvals); put64(&s, zlsctl); - put64(&s, job->has_depth_attachment ? job->ds.addr.addr : 0); + put64(&s, job->has_depth_attachment ? g9p_ds_addr(job) : 0); /* * The stencil base. The kernel's Apple record takes the depth base for * 0xf50/0xf58 (Z load and store) and this one for 0xf60/0xf68; a stencil @@ -4148,8 +5095,8 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, put32(&s, 0); put32(&s, bgobjdepth); /* ISP_BGOBJDEPTH: the depth clear */ put32(&s, bgobjvals); /* ISP_BGOBJVALS */ - /* ISP_AA: no multisampling */ - put32(&s, job->g9p.samples == 4 ? 2 : 0); + /* ISP_AA: log2 of the samples */ + put32(&s, util_logbase2(job->g9p.samples)); /* * ISP_CTL: bit 17 (Rogue's process_empty_tiles) runs the background object * on tiles without geometry too; without it a render's clear reaches @@ -4162,8 +5109,8 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, * small damaged part. G9P_ALL_TILES=1 keeps it on. */ { - /* Four samples: the low bits 7, and the partition mode of the output below */ - const bool ms = job->g9p.samples == 4; + /* Several samples: the low bits 7, and the partition mode of the output below */ + const bool ms = job->g9p.samples > 1; const uint32_t ctl = (job->g9p.load || job->g9p.load_op == G9P_LOAD_OP_NONE) && !getenv("G9P_ALL_TILES") ? 0x21000f : 0x23000f; @@ -4178,7 +5125,7 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, *(uint32_t *)buf = s.len; put32(&s, G9P_EXTHDR_APPLE); - put64(&s, job->g9p.samples == 4 ? G9P_POSITIONS_4X : 0x88); /* 0xfa8 sample positions */ + put64(&s, g9p_positions(job->g9p.samples)); /* 0xfa8 sample positions */ for (unsigned i = 0; i < 4; i++) { const uint32_t view_data = job->g9p.bgnd_data_view[job->view_state.view_index]; @@ -4191,7 +5138,8 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, * a depth buffer), 0x4070 and each core's word (PR #74's g5p_ru_fill_3d_width(), * Apple's table by width; iOS record +0x6ec): 0x1d3 is width 4 */ - const unsigned output_count = 0x4000 / (job->g9p.pixel_width << (job->g9p.samples == 4 ? 11 : 10)); + /* tiles of 2048 samples when multisampled: 32 x 32 pixels at 2x, 32 x 16 at 4x, 16 x 16 at 8x */ + const unsigned output_count = 0x4000 / (job->g9p.pixel_width << (job->g9p.samples > 1 ? 11 : 10)); const unsigned output_code = job->g9p.pixel_width == 8 ? 0x20 : job->g9p.pixel_width == 4 ? 0x10 : 0; for (unsigned i = 0; i < 6; i++) @@ -4208,8 +5156,11 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, const uint32_t mx = ALIGN_POT(DIV_ROUND_UP(DIV_ROUND_UP(w, 32), 4), 4); const uint32_t my = ALIGN_POT(DIV_ROUND_UP(DIV_ROUND_UP(h, 32), 4), 4); - /* Four samples are 2x2 tiles per tile (PR #6's g5p_ru_rt_derive_samples()) */ - put32(&s, DIV_ROUND_UP(mx * my * (job->g9p.samples == 4 ? 4 : 1) * + /* + * Several samples are tiles of tiles, 1x2 at 2x, 2x2 at 4x (PR #6's + * g5p_ru_rt_derive_samples()) and 2x4 at 8x, as the kernel's TE_AA has them + */ + put32(&s, DIV_ROUND_UP(mx * my * job->g9p.samples * rogue_get_region_header_size(&device->pdevice->dev_info), 4)); } diff --git a/src/imagination/vulkan/pvr_arch_g9p_transfer.c b/src/imagination/vulkan/pvr_arch_g9p_transfer.c index b02f3c65e84..6c5da8b7bad 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_transfer.c +++ b/src/imagination/vulkan/pvr_arch_g9p_transfer.c @@ -72,13 +72,32 @@ struct surface { uint8_t *base; enum pipe_format format; unsigned bpp, stride, width, height; - /* 1 or 4; four samples are a twiddled surface of twice the size (2x2 per pixel) */ - unsigned samples; + /* + * 1, 2, 4 or 8. A multisampled surface is twiddled by pixel, each pixel's + * samples one after another; its texels here are the samples, sx x sy of + * them a pixel (ms_shape()), and width and height count them. + */ + unsigned samples, sx, sy; + /* the pixels' width and height */ + unsigned pw, ph; /* D32F_S8: from the depth to the stencil, 0 when unknown */ uint64_t stencil_offset; bool twiddled; }; +/* A surface's samples, with its width and height in pixels: then counted in samples */ +static void +surface_samples(struct surface *out, unsigned samples) +{ + out->samples = samples == 2 || samples == 4 || samples == 8 ? samples : 1; + out->sx = out->samples >= 4 ? 2 : 1; + out->sy = out->samples / out->sx; + out->pw = out->width; + out->ph = out->height; + out->width *= out->sx; + out->height *= out->sy; +} + static bool surface_init(struct pvr_device *device, const struct pvr_transfer_cmd_surface *s, struct surface *out) @@ -90,12 +109,8 @@ surface_init(struct pvr_device *device, const struct pvr_transfer_cmd_surface *s out->width = s->width; out->height = s->height; out->twiddled = s->mem_layout == PVR_MEMLAYOUT_TWIDDLED; - out->samples = s->sample_count == 4 ? 4 : 1; out->stencil_offset = s->g9p_stencil_offset; - if (out->samples == 4) { - out->width *= 2; - out->height *= 2; - } + surface_samples(out, s->sample_count); if (!out->base) mesa_loge("G9P transfer: no CPU mapping for %#" PRIx64, s->dev_addr.addr); return out->base != NULL; @@ -117,13 +132,35 @@ twiddle(unsigned x, unsigned y, unsigned w, unsigned h) return m; } +/* + * Sample k's place in its pixel's block of sx x sy texels, and back: 2x + * one above the other, 4x 2x2 with y in bit 0 (the twiddled order of a + * surface of twice the size), 8x 2x4 with y in bits 0 and 2 + */ +static void +ms_sample_xy(const struct surface *s, unsigned k, unsigned *dx, unsigned *dy) +{ + *dx = s->samples >= 4 ? (k >> 1) & 1 : 0; + *dy = s->samples == 2 ? k : s->samples == 8 ? (k & 1) | ((k >> 2) & 1) << 1 : k & 1; +} + +static unsigned +ms_sample_bits(const struct surface *s, unsigned dx, unsigned dy) +{ + return (s->samples >= 4 ? dx << 1 : 0) | (s->samples == 8 ? (dy & 1) | (dy >> 1) << 2 : dy); +} + /* * A texel's byte offset is a column part plus a row part: in twiddled - * surfaces the x and y bits of the index are interleaved, disjoint + * surfaces the x and y bits of the index are interleaved, disjoint, and + * a multisampled one's pixel index (times the samples) and sample index too */ static size_t column_offset(const struct surface *s, unsigned x) { + if (s->twiddled && s->samples > 1) + return ((size_t)twiddle(x / s->sx, 0, s->pw, s->ph) * s->samples + + ms_sample_bits(s, x % s->sx, 0)) * s->bpp; if (s->twiddled) return (size_t)twiddle(x, 0, s->width, s->height) * s->bpp; return (size_t)x * s->bpp; @@ -132,6 +169,9 @@ column_offset(const struct surface *s, unsigned x) static size_t row_offset(const struct surface *s, unsigned y) { + if (s->twiddled && s->samples > 1) + return ((size_t)twiddle(0, y / s->sy, s->pw, s->ph) * s->samples + + ms_sample_bits(s, 0, y % s->sy)) * s->bpp; if (s->twiddled) return (size_t)twiddle(0, y, s->width, s->height) * s->bpp; return (size_t)y * s->stride * s->bpp; @@ -140,9 +180,8 @@ row_offset(const struct surface *s, unsigned y) static void fill(const struct pvr_transfer_cmd *cmd, const struct surface *dst) { - const unsigned ms = dst->samples == 4 ? 2 : 1; - const VkRect2D scaled = { { cmd->scissor.offset.x * ms, cmd->scissor.offset.y * ms }, - { cmd->scissor.extent.width * ms, cmd->scissor.extent.height * ms } }; + const VkRect2D scaled = { { cmd->scissor.offset.x * dst->sx, cmd->scissor.offset.y * dst->sy }, + { cmd->scissor.extent.width * dst->sx, cmd->scissor.extent.height * dst->sy } }; const VkRect2D *r = &scaled; uint8_t packed[16] = { 0 }; @@ -513,15 +552,16 @@ copy_linear(const struct pvr_rect_mapping *m, const struct surface *src, const s free(src_cols); } -/* A rectangle in a four-sample surface's texels (2x2 per pixel) */ +/* A rectangle in a multisampled surface's texels (sx x sy per pixel) */ static VkRect2D -ms_rect(const VkRect2D *r) +ms_rect(const VkRect2D *r, const struct surface *s) { - return (VkRect2D){ { r->offset.x * 2, r->offset.y * 2 }, { r->extent.width * 2, r->extent.height * 2 } }; + return (VkRect2D){ { r->offset.x * s->sx, r->offset.y * s->sy }, + { r->extent.width * s->sx, r->extent.height * s->sy } }; } /* - * Four samples into one: the average, or sample 0 for integers and depth/stencil. Nearest + * Several samples into one: the average, or sample 0 for integers and depth/stencil. Nearest * for a scaled mapping, as a resolve has none in Vulkan. */ static void @@ -538,23 +578,30 @@ resolve(const struct pvr_rect_mapping *m, const struct surface *src, const struc uint8_t *d = row + column_offset(dst, m->dst_rect.offset.x + x); if (first) { - memcpy(d, src->base + row_offset(src, 2 * sy) + column_offset(src, 2 * sx), dst->bpp); + memcpy(d, src->base + row_offset(src, src->sy * sy) + column_offset(src, src->sx * sx), + dst->bpp); continue; } - float sum[4] = { 0 }; + /* in double, divided once: eight float sums of a value / 8 lost its last bit */ + double sum[4] = { 0 }; + float mean[4]; - for (unsigned s = 0; s < 4; s++) { + for (unsigned s = 0; s < src->samples; s++) { float texel[4]; + unsigned dx, dy; + ms_sample_xy(src, s, &dx, &dy); util_format_unpack_rgba(src->format, texel, - src->base + row_offset(src, 2 * sy + (s & 1)) + - column_offset(src, 2 * sx + (s >> 1)), + src->base + row_offset(src, src->sy * sy + dy) + + column_offset(src, src->sx * sx + dx), 1); for (unsigned c = 0; c < 4; c++) - sum[c] += texel[c] / 4; + sum[c] += texel[c]; } - util_format_pack_rgba(dst->format, d, sum, 1); + for (unsigned c = 0; c < 4; c++) + mean[c] = sum[c] / src->samples; + util_format_pack_rgba(dst->format, d, mean, 1); } } } @@ -563,19 +610,19 @@ resolve(const struct pvr_rect_mapping *m, const struct surface *src, const struc static void copy_samples(const struct pvr_rect_mapping *m, const struct surface *src, const struct surface *dst) { - if (src->samples == 4 && dst->samples == 1) { + if (src->samples > 1 && dst->samples == 1) { resolve(m, src, dst); - } else if (src->samples == dst->samples && src->samples == 4) { + } else if (src->samples == dst->samples && src->samples > 1) { struct pvr_rect_mapping scaled = *m; - scaled.src_rect = ms_rect(&m->src_rect); - scaled.dst_rect = ms_rect(&m->dst_rect); + scaled.src_rect = ms_rect(&m->src_rect, src); + scaled.dst_rect = ms_rect(&m->dst_rect, dst); copy(&scaled, src, dst); - } else if (dst->samples == 4) { - /* one sample into all four */ + } else if (dst->samples > 1) { + /* one sample into all of them */ struct pvr_rect_mapping scaled = *m; - scaled.dst_rect = ms_rect(&m->dst_rect); + scaled.dst_rect = ms_rect(&m->dst_rect, dst); copy(&scaled, src, dst); } else { copy(m, src, dst); @@ -706,6 +753,8 @@ host_image_surface(struct pvr_device *device, const struct pvr_image *image, out->width = MAX2(image->vk.extent.width >> sub->mipLevel, 1); out->height = MAX2(image->vk.extent.height >> sub->mipLevel, 1); out->twiddled = image->memlayout == PVR_MEMLAYOUT_TWIDDLED; + out->stencil_offset = 0; + surface_samples(out, image->vk.samples); return out->base != NULL; } @@ -721,6 +770,8 @@ host_memory_surface(const struct pvr_image *image, const void *ptr, uint32_t row out->width = out->stride; out->height = image_height ? image_height : extent->height; out->twiddled = false; + out->stencil_offset = 0; + surface_samples(out, 1); } static VkResult @@ -738,8 +789,8 @@ host_copy(struct pvr_device *device, const struct pvr_image *image, bool to_imag return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED); if (flags & VK_HOST_IMAGE_COPY_MEMCPY_BIT) { /* the subresource as it is laid out, back to back per layer */ - const size_t size = (size_t)img.stride * (img.twiddled ? util_next_power_of_two(img.height) - : img.height) * img.bpp; + const size_t size = (size_t)img.stride * (img.twiddled ? util_next_power_of_two(img.ph) + : img.ph) * img.bpp * img.samples; uint8_t *host = (uint8_t *)ptr + l * size; if (to_image) @@ -781,7 +832,7 @@ host_copy(struct pvr_device *device, const struct pvr_image *image, bool to_imag const struct pvr_rect_mapping m = { .src_rect = to_image ? at_memory : at_image, .dst_rect = to_image ? at_image : at_memory }; - copy(&m, to_image ? &mem : &img, to_image ? &img : &mem); + copy_samples(&m, to_image ? &mem : &img, to_image ? &img : &mem); } return VK_SUCCESS; } @@ -845,7 +896,7 @@ PVR_PER_ARCH(CopyImageToImage)(VkDevice _device, const VkCopyImageToImageInfo *i .dst_rect = { { r->dstOffset.x, r->dstOffset.y }, { r->extent.width, r->extent.height } }, }; - copy(&m, &src, &dst); + copy_samples(&m, &src, &dst); } } return VK_SUCCESS; diff --git a/src/imagination/vulkan/pvr_arch_queue.c b/src/imagination/vulkan/pvr_arch_queue.c index 99a4d04435e..76870d7af57 100644 --- a/src/imagination/vulkan/pvr_arch_queue.c +++ b/src/imagination/vulkan/pvr_arch_queue.c @@ -731,6 +731,18 @@ static VkResult pvr_process_graphics_cmd(struct pvr_device *device, } PVR_G5P_HOOK(xfb_before_job, device, cmd_buffer, sub_cmd) + if (util_dynarray_num_elements(&sub_cmd->g9p_indirects, char)) { + struct vk_sync *wait = queue->next_job_wait_sync[PVR_JOB_TYPE_GEOM]; + + if (wait && PVR_PER_ARCH(g9p_indirect_needs_wait)(sub_cmd)) { + VkResult result = vk_sync_wait(&device->vk, wait, 0, VK_SYNC_WAIT_COMPLETE, UINT64_MAX); + + if (result != VK_SUCCESS) + return result; + } + PVR_PER_ARCH(g9p_patch_indirect_draws)(sub_cmd); + } + if (util_dynarray_num_elements(&sub_cmd->g9p_realigns, char)) { struct vk_sync *wait = queue->next_job_wait_sync[PVR_JOB_TYPE_GEOM]; @@ -779,6 +791,40 @@ static VkResult pvr_process_graphics_cmd(struct pvr_device *device, /* PVR_DEBUG=pass_log: the winsys' submit lines name this render (pvr_pass_log.h) */ if (pvr_pass_log_enabled()) pvr_pass_log_set_current(sub_cmd->pass_log_serial); + /* + * G9P target groups (more colour words than a pixel holds): the render once per + * group, each with its own end of tile and background and its copy of the stream + */ + if (sub_cmd->job.g9p.enabled && sub_cmd->job.g9p_groups > 1) { + const uint64_t oclqry = sub_cmd->job.g9p.oclqry_base; + const pvr_dev_addr_t stream = sub_cmd->job.ctrl_stream_addr; + const uint32_t eot_data = sub_cmd->job.view_state.view[view_idx].pds_pixel_event_data_offset; + + for (unsigned g = 0; g < sub_cmd->job.g9p_groups; g++) { + if (g && !sub_cmd->g9p_group_stream[g]) + break; + sub_cmd->job.g9p = sub_cmd->job.g9p_group[g]; + sub_cmd->job.g9p.oclqry_base = oclqry; + sub_cmd->job.view_state.view[view_idx].pds_pixel_event_data_offset = + sub_cmd->job.g9p_group_eot_data[g]; + if (g) + sub_cmd->job.ctrl_stream_addr.addr = sub_cmd->g9p_group_stream[g]; + result = pvr_process_graphics_cmd_for_view(device, queue, cmd_buffer, sub_cmd, view_idx); + sub_cmd->job.ds.addr = ds_addr; + sub_cmd->job.ctrl_stream_addr = stream; + if (result != VK_SUCCESS) { + if (pvr_pass_log_enabled()) + pvr_pass_log_set_current(0); + return result; + } + } + sub_cmd->job.g9p = sub_cmd->job.g9p_group[0]; + sub_cmd->job.g9p.oclqry_base = oclqry; + sub_cmd->job.view_state.view[view_idx].pds_pixel_event_data_offset = eot_data; + if (pvr_pass_log_enabled()) + pvr_pass_log_set_current(0); + continue; + } result = pvr_process_graphics_cmd_for_view(device, queue, cmd_buffer, @@ -807,6 +853,36 @@ static VkResult pvr_process_compute_cmd(struct pvr_device *device, if (result != VK_SUCCESS) return result; + /* + * G9P: a compute job queued while another runs hangs the A10's firmware + * (two or three dispatches with barriers between them in one submission: + * "hang: compute job 1"), though the job waits for the one before it. + * The CPU waits for the previous compute job first. + */ + if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p) && + queue->last_job_signal_sync[PVR_JOB_TYPE_COMPUTE]) { + result = vk_sync_wait(&device->vk, queue->last_job_signal_sync[PVR_JOB_TYPE_COMPUTE], 0, + VK_SYNC_WAIT_COMPLETE, UINT64_MAX); + if (result != VK_SUCCESS) { + pvr_g5p_sync_put(device, sync); + return result; + } + } + /* G9P: indirect dispatches' group counts, once what writes their arguments is done */ + if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p) && + util_dynarray_num_elements(&sub_cmd->g9p_indirects, char)) { + struct vk_sync *wait = queue->next_job_wait_sync[PVR_JOB_TYPE_COMPUTE]; + + if (wait && PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait)(sub_cmd)) { + result = vk_sync_wait(&device->vk, wait, 0, VK_SYNC_WAIT_COMPLETE, UINT64_MAX); + if (result != VK_SUCCESS) { + pvr_g5p_sync_put(device, sync); + return result; + } + } + PVR_PER_ARCH(g9p_patch_indirect_dispatches)(sub_cmd); + } + sub_cmd->g5p_stamp = pvr_g5p_queue_stamp_next(queue); result = pvr_arch_compute_job_submit( queue->compute_ctx, diff --git a/src/imagination/vulkan/pvr_blit.h b/src/imagination/vulkan/pvr_blit.h index 323724a74fe..ac1a7cd03a1 100644 --- a/src/imagination/vulkan/pvr_blit.h +++ b/src/imagination/vulkan/pvr_blit.h @@ -53,6 +53,23 @@ VkResult pvr_copy_or_resolve_depth_stencil_region( const VkClearDepthStencilValue *ds_clear_values, const VkImageCopy2 *region); +/* + * G9P: both planes of a depth/stencil level to or from the ZLS scratch plane + * (@sbw x @sbh, twiddled, its stencil plane @stencil bytes in), a CPU transfer + */ +VkResult pvr_g9p_ds_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_image *image, uint32_t level, + uint32_t layer, uint64_t scratch, uint32_t sbw, + uint32_t sbh, uint64_t stencil, bool to_scratch); + +/* + * G9P: a colour level to or from its scratch slot (g9p_colour_scratch()), the + * same layout at another address, a CPU transfer + */ +VkResult pvr_g9p_colour_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_image *image, uint32_t level, + uint32_t layer, uint64_t scratch, bool to_scratch); + VkResult pvr_copy_buffer_to_image_region_format(struct pvr_cmd_buffer *cmd_buffer, pvr_dev_addr_t buffer_dev_addr, diff --git a/src/imagination/vulkan/pvr_cmd_buffer.h b/src/imagination/vulkan/pvr_cmd_buffer.h index 9ad041645a3..0c9def1a3ed 100644 --- a/src/imagination/vulkan/pvr_cmd_buffer.h +++ b/src/imagination/vulkan/pvr_cmd_buffer.h @@ -195,6 +195,8 @@ struct pvr_sub_cmd_gfx { /* G9P: vertex bindings to copy when the job is submitted */ struct util_dynarray g9p_realigns; + /* G9P: indirect draws to fill when the job is submitted (struct g9p_indirect) */ + struct util_dynarray g9p_indirects; /* G9P: clears of targets the fragment program writes to memory (struct g9p_fill) */ struct util_dynarray g9p_fills; /* @@ -205,6 +207,14 @@ struct pvr_sub_cmd_gfx { struct util_dynarray g9p_view_kicks; uint64_t g9p_view_stream[PVR_MAX_MULTIVIEW]; struct util_dynarray g9p_view_bos; + /* + * G9P: a render of target groups (more colour words than a pixel holds): each + * draw's state words per group (struct g9p_group_ppp), and per group past the first + * a copy of the control stream naming them (pvr_arch_g9p_build_group_streams()), its + * buffers in g9p_view_bos + */ + struct util_dynarray g9p_group_ppps; + uint64_t g9p_group_stream[4]; /* Control stream builder object */ struct pvr_csb control_stream; @@ -289,6 +299,24 @@ struct pvr_sub_cmd_compute { bool g5p_kernel_since_end; struct pvr_g5p_dispatch_log *g5p_log_first, *g5p_log_last; unsigned g5p_log_count; + /* + * G9P: indirect dispatches, their kernel records' group counts filled from + * the arguments when the job is submitted (struct g9p_dispatch_indirect) + */ + struct util_dynarray g9p_indirects; +}; + +/* An indirect dispatch's words to fill at submission (g9p_patch_indirect_dispatches()) */ +struct g9p_dispatch_indirect { + /* the kernel record's words 3-5: groups - 1 */ + uint32_t *groups; + /* the kernel's PDS data: its USC program, the empty kernel for no groups */ + uint32_t *kick; + uint32_t empty_kernel; + /* VkDispatchIndirectCommand, CPU-mapped */ + const uint32_t *args; + /* the arguments' buffer can be written by the GPU: wait for the jobs before */ + bool gpu_written; }; struct pvr_sub_cmd_transfer { diff --git a/src/imagination/vulkan/pvr_device.h b/src/imagination/vulkan/pvr_device.h index 5742750a6e8..b42cd0bb783 100644 --- a/src/imagination/vulkan/pvr_device.h +++ b/src/imagination/vulkan/pvr_device.h @@ -119,6 +119,9 @@ struct pvr_device { /* load op: the old target sampled at the pixel position, and its kick */ struct pvr_suballoc_bo *load_usc[G9P_OUTPUT_COUNT]; struct pvr_pds_upload load_kick[G9P_OUTPUT_COUNT]; + /* the same at 2, 4 and 8 samples, each sample its own (g9p_ms_load_coords()) */ + struct pvr_suballoc_bo *load_ms_usc[3][G9P_OUTPUT_COUNT]; + struct pvr_pds_upload load_ms_kick[3][G9P_OUTPUT_COUNT]; /* depth/stencil clear draws: the tile's colour unchanged, and its kick */ struct pvr_suballoc_bo *keep_usc[G9P_OUTPUT_COUNT]; struct pvr_pds_upload keep_kick[G9P_OUTPUT_COUNT]; @@ -155,6 +158,24 @@ struct pvr_device { * free list (pvr_arch_render_target_dataset_create()) */ struct pvr_free_list *free_list; + /* + * A stencil level under a tile's 32 pixels renders here, in whole tiles + * (g9p_ds_scratch()): allocated at its first use, CPU-mapped, its vma in + * g9p_vmas for the CPU transfers in and out + */ + struct { + simple_mtx_t lock; + struct pvr_bo *bo; + } zls_scratch; + /* + * The counters of compute barriers across tasks (G9P_BARRIER_COUNTERS): + * allocated zeroed at the first such pipeline, each count back at 0 + * after its barrier + */ + struct { + simple_mtx_t lock; + struct pvr_bo *bo; + } barriers; } g9p; /* Compute shaders for queries. */ diff --git a/src/imagination/vulkan/pvr_formats.c b/src/imagination/vulkan/pvr_formats.c index 1cb1eadd1bc..553b1e7b344 100644 --- a/src/imagination/vulkan/pvr_formats.c +++ b/src/imagination/vulkan/pvr_formats.c @@ -391,8 +391,13 @@ pvr_get_image_format_features2(struct pvr_physical_device *pdevice, else flags &= ~(VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT); - /* blends read the tile as f16: not integers or 32-bit channels */ - if (g9p_format_renderable(vk_format) && g9p_format_filterable(vk_format)) + /* + * blends read the tile as f16, or a 32-bit float target's own words + * (lower_fb_fetch_instr(): raw targets keep their bits): not integers + */ + if (g9p_format_renderable(vk_format) && + (g9p_format_filterable(vk_format) || vk_format == VK_FORMAT_R32_SFLOAT || + vk_format == VK_FORMAT_R32G32_SFLOAT || vk_format == VK_FORMAT_R32G32B32A32_SFLOAT)) flags |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT; else flags &= ~VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT; @@ -856,11 +861,11 @@ pvr_get_image_format_properties(struct pvr_physical_device *pdevice, pImageFormatProperties->maxArrayLayers = max_array_layers; pImageFormatProperties->sampleCounts = max_sample_bits; /* - * G9P: four samples of 32-bit channels: RGBA hangs the fragment job and RG and R - * come out wrong (dEQP fbo.msaa); GL ES needs no samples of these formats + * G9P: several samples of four raw words (RGBA32) hang the fragment job (dEQP + * fbo.msaa). One and two words are stored in 16-bit channels (g9p_eot_format()). */ if (PVR_HAS_FEATURE(&pdevice->dev_info, apple_g9p) && vk_format_is_color(info->format) && - g9p_format_output(info->format) >= G9P_OUTPUT_RAW1) + g9p_format_output(info->format) == G9P_OUTPUT_RAW4) pImageFormatProperties->sampleCounts = VK_SAMPLE_COUNT_1_BIT; } diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 8423d0a9375..fcf945f3223 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -56,6 +56,25 @@ struct pvr_g9p_draw { uint64_t index_addr; unsigned index_size; uint32_t count, instances, offset; + /* an indirect draw: its arguments (CPU), from @indirect_buffer; the index buffer's end */ + const uint8_t *indirect; + struct pvr_buffer *indirect_buffer; + uint64_t index_end; +}; + +/* + * An indirect draw's INDEX_LIST, filled from its arguments when its job is + * submitted (pvr_arch_g9p_gfx.c): the A10's VDM is given the counts in its + * words + */ +struct g9p_indirect { + uint32_t *words; /* INDEX_LIST's first word */ + uint32_t *first_instance; /* the vertex shader's G9P_DRAW_PARAMS slot, or NULL */ + const uint8_t *args; + struct pvr_buffer *buffer; /* where the arguments are, if the GPU may write them */ + uint64_t index_addr, index_end; + unsigned index_size; + bool indexed; }; #ifdef PVR_PER_ARCH @@ -67,12 +86,32 @@ VkResult PVR_PER_ARCH(g9p_clear_attachments)(struct pvr_cmd_buffer *cmd_buffer, VkResult PVR_PER_ARCH(g9p_device_init)(struct pvr_device *device); +/* The ZLS scratch plane's device address (pvr_arch_g9p.c), 0 if none */ +#define G9P_ZLS_SCRATCH_SIZE (16384u * 32u * 5u) +uint64_t PVR_PER_ARCH(g9p_zls_scratch)(struct pvr_device *device); + +/* + * The end of the scratch plane: a slot per colour target, for a level the end + * of tile can't store to (g9p_job_init()). The levels past a chain's first + * level under 64 bytes, each under 64 bytes (four samples: 256). + */ +#define G9P_COLOUR_SCRATCH_SLOT 256u +static inline uint64_t +g9p_colour_scratch_slot(uint64_t scratch, unsigned t) +{ + return scratch + G9P_ZLS_SCRATCH_SIZE - G9P_COLOUR_SCRATCH_SLOT * (t + 1); +} + /* The CPU address of a device address in a bound buffer or image, or NULL */ uint8_t *PVR_PER_ARCH(g9p_cpu_addr)(struct pvr_device *device, uint64_t addr); struct pvr_sub_cmd_gfx; void PVR_PER_ARCH(g9p_run_fills)(struct pvr_device *device, struct pvr_sub_cmd_gfx *sub_cmd); void PVR_PER_ARCH(g9p_device_finish)(struct pvr_device *device); +/* Fill a render's indirect draws from their arguments; before its job is submitted */ +void PVR_PER_ARCH(g9p_patch_indirect_draws)(struct pvr_sub_cmd_gfx *sub_cmd); +bool PVR_PER_ARCH(g9p_indirect_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd); + /* Copy a render's unaligned vertex bindings; before its job is submitted */ void PVR_PER_ARCH(g9p_realign_vertex_bindings)(struct pvr_sub_cmd_gfx *sub_cmd); bool PVR_PER_ARCH(g9p_realign_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd); @@ -84,7 +123,11 @@ VkResult PVR_PER_ARCH(g9p_compute_pipeline_compile)( VkResult PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd, - const uint32_t group_count[static 3]); + const uint32_t group_count[static 3], + uint64_t indirect); +/* The group counts of a compute job's indirect dispatches, from their arguments */ +void PVR_PER_ARCH(g9p_patch_indirect_dispatches)(struct pvr_sub_cmd_compute *sub_cmd); +bool PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait)(const struct pvr_sub_cmd_compute *sub_cmd); void PVR_PER_ARCH(g9p_end_pass)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd); @@ -117,6 +160,21 @@ struct pvr_sub_cmd_gfx; VkResult PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_gfx *sub_cmd); +/* + * A draw in a render of target groups: where its state-word pointers are in the + * control stream (each PPP_STATE's address word), and per group its state words + */ +#define G9P_DRAW_PPP_BLOCKS 12 +struct g9p_group_ppp { + struct pvr_bo *bo; + unsigned n; + uint32_t dword[G9P_DRAW_PPP_BLOCKS], offset[G9P_DRAW_PPP_BLOCKS], words[G9P_DRAW_PPP_BLOCKS]; + uint64_t addr[4]; +}; + +VkResult PVR_PER_ARCH(g9p_build_group_streams)(struct pvr_cmd_buffer *cmd_buffer, + struct pvr_sub_cmd_gfx *sub_cmd); + /* The job streams with the Apple register block; return their length */ uint32_t PVR_PER_ARCH(g9p_geom_stream)(const struct pvr_device *device, const struct pvr_render_job *job, uint8_t *buf, @@ -168,7 +226,15 @@ void PVR_PER_ARCH(g9p_xfb_free)(struct pvr_cmd_buffer *cmd_buffer); struct pvr_image_view; void PVR_PER_ARCH(g9p_pack_image_view)(const struct pvr_image_view *iview, uint64_t words[2]); +struct g9p_storage_image; +void PVR_PER_ARCH(g9p_storage_image_record)(const struct pvr_image_view *iview, + struct g9p_storage_image *rec); uint64_t PVR_PER_ARCH(g9p_pack_sampler)(const VkSamplerCreateInfo *info); +struct pvr_descriptor_state; +void PVR_PER_ARCH(g9p_texture_words)(const struct pvr_descriptor_state *desc, unsigned set_idx, + unsigned binding, unsigned index, int sampler_set, + int sampler_binding, unsigned sampler_index, + uint32_t state[4], uint32_t sampler[2]); struct pvr_sub_cmd_transfer; struct vk_sync; @@ -205,6 +271,7 @@ bool PVR_PER_ARCH(g9p_meta_upload)(struct pvr_cmd_buffer *cmd_buffer, const VkCo # define pvr_arch_g9p_cmd_draw PVR_PER_ARCH(g9p_cmd_draw) # define pvr_arch_g9p_job_init PVR_PER_ARCH(g9p_job_init) # define pvr_arch_g9p_build_view_streams PVR_PER_ARCH(g9p_build_view_streams) +# define pvr_arch_g9p_build_group_streams PVR_PER_ARCH(g9p_build_group_streams) # define pvr_arch_g9p_geom_stream PVR_PER_ARCH(g9p_geom_stream) # define pvr_arch_g9p_frag_stream PVR_PER_ARCH(g9p_frag_stream) # define pvr_arch_g9p_device_init PVR_PER_ARCH(g9p_device_init) @@ -212,6 +279,8 @@ bool PVR_PER_ARCH(g9p_meta_upload)(struct pvr_cmd_buffer *cmd_buffer, const VkCo # define pvr_arch_g9p_compute_pipeline_compile \ PVR_PER_ARCH(g9p_compute_pipeline_compile) # define pvr_arch_g9p_cmd_dispatch PVR_PER_ARCH(g9p_cmd_dispatch) +# define pvr_arch_g9p_patch_indirect_dispatches PVR_PER_ARCH(g9p_patch_indirect_dispatches) +# define pvr_arch_g9p_dispatch_indirect_needs_wait PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait) # define pvr_arch_g9p_end_pass PVR_PER_ARCH(g9p_end_pass) #endif /* PVR_PER_ARCH */ diff --git a/src/imagination/vulkan/pvr_image.c b/src/imagination/vulkan/pvr_image.c index 3c65d2dc4ff..5b7ed717c9d 100644 --- a/src/imagination/vulkan/pvr_image.c +++ b/src/imagination/vulkan/pvr_image.c @@ -311,12 +311,17 @@ static void pvr_image_setup_mip_levels(struct pvr_image *image) } image->total_size = offset; - /* G9P: the stencil plane of D32F_S8 (pvr_image_plane_setup_mip_levels()) */ + /* + * G9P: the stencil plane of D32F_S8 (pvr_image_plane_setup_mip_levels()), + * at an aligned offset: the ZLS's stencil base is 16-byte aligned, and a + * chain of levels ends at a 4-byte one (its stencil stored over the depth + * plane's 1x1 level) + */ if (PVR_HAS_FEATURE(&container_of(image->vk.base.device, struct pvr_device, vk) ->pdevice->dev_info, apple_g9p) && image->vk.format == VK_FORMAT_D32_SFLOAT_S8_UINT) - image->total_size *= 2; + image->total_size = align64(image->total_size, 64) * 2; } static unsigned get_pbe_stride_align(const struct pvr_device_info *dev_info); diff --git a/src/imagination/vulkan/pvr_job_render.h b/src/imagination/vulkan/pvr_job_render.h index 7ea26811b1c..9dc275dbf03 100644 --- a/src/imagination/vulkan/pvr_job_render.h +++ b/src/imagination/vulkan/pvr_job_render.h @@ -122,6 +122,13 @@ struct pvr_render_job { const struct pvr_image_view *iview; bool has_alignment_transfers; + + /* + * G9P: the ZLS's surface instead of the level, a scratch plane of whole + * 32-pixel tiles (g9p_ds_scratch()), and its stencil plane's offset; 0 + * for none + */ + uint64_t g9p_scratch, g9p_scratch_stencil; } ds; VkClearDepthStencilValue ds_clear_value; @@ -145,7 +152,7 @@ struct pvr_render_job { #define G9P_LOAD_OP_NONE (-1) /* Apple G9P (pvr_arch_g9p_gfx.c): what the kernel's Apple register block takes */ - struct { + struct pvr_g9p_job { bool enabled; /* end-of-tile PDS code; its data is the view's pds_pixel_event_data_offset */ uint32_t eot_code; @@ -167,13 +174,30 @@ struct pvr_render_job { bool target_linear; /* the background loads the target (load op load) */ bool load; + /* + * the colour targets whose level the end of tile can't store to (an + * address not 64-byte aligned): they store to a scratch slot + * (g9p_colour_scratch_slot()) + */ + uint32_t colour_scratch; /* * the first colour attachment's load op, G9P_LOAD_OP_NONE without one * (don't care: the render pass setup initializes nothing) */ int load_op; + /* a target group's render before the last: depth and stencil loaded, not stored */ + bool zs_keep; } g9p; + /* + * More colour words than a pixel holds: the render once per group of targets + * (g9p_target_groups()), each with its own background and end of tile and its + * own copy of the control stream (but group 0); group 0's are in g9p + */ + unsigned g9p_groups; + struct pvr_g9p_job g9p_group[4]; + uint32_t g9p_group_eot_data[4]; + /* * G9P_SYNC: the pipelines of the job's first draws, reported on a timeout; * G9P_TRACE: also the render area and the 32-pixel tiles the draws' region diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index d63073999f5..e4310018354 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -636,6 +636,10 @@ static bool pvr_physical_device_get_properties( UNUSED const uint32_t max_sample_bits = ((max_multisample << 1) - 1); + /* the A10 renders 2, 4 and 8 samples a pixel (its kernel's TE_AA 1, 5, 7), the others 4 */ + const VkSampleCountFlags sample_counts = + PVR_HAS_FEATURE(dev_info, apple_g9p) ? max_sample_bits : VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT; + UNUSED const uint32_t max_user_vertex_components = pvr_get_max_user_vertex_output_components(dev_info); @@ -760,11 +764,16 @@ static bool pvr_physical_device_get_properties( /* G5P: 256 (AGXMetalA9: 512 per threadgroup, maxThreadsPerThreadgroup; Rogue's CDM: 32 lanes x 16 * tasks per USC). Barriers above 32 lanes are Apple's software barrier (g5p_barrier.h); a pipeline whose * temporaries cannot keep every task of a barriered workgroup resident is refused at creation - * (pvr_arch_g5p_compute.c). */ - .maxComputeWorkGroupInvocations = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, + * (pvr_arch_g5p_compute.c). G9P: 512, 16 tasks with 16 registers each in bank 1 and the rest + * in bank 0 (pvr_arch_g9p.c). */ + .maxComputeWorkGroupInvocations = + PVR_HAS_FEATURE(dev_info, apple_g9p) ? 512U : + PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, .maxComputeWorkGroupSize = { - [0] = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, - [1] = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, + [0] = PVR_HAS_FEATURE(dev_info, apple_g9p) ? 512U : + PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, + [1] = PVR_HAS_FEATURE(dev_info, apple_g9p) ? 512U : + PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, [2] = 64U, }, @@ -808,15 +817,15 @@ static bool pvr_physical_device_get_properties( .maxFramebufferLayers = 256U, /* Note: update nir_shader_compiler_options.max_samples when changing this. */ - .framebufferColorSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .framebufferDepthSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .framebufferStencilSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .framebufferNoAttachmentsSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, + .framebufferColorSampleCounts = sample_counts, + .framebufferDepthSampleCounts = sample_counts, + .framebufferStencilSampleCounts = sample_counts, + .framebufferNoAttachmentsSampleCounts = sample_counts, .maxColorAttachments = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 8U : 4U, /* G5P: 5..8 compact UNORM8 targets (pvr_g5p_mrt.h) */ - .sampledImageColorSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .sampledImageDepthSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .sampledImageStencilSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, + .sampledImageColorSampleCounts = sample_counts, + .sampledImageIntegerSampleCounts = sample_counts, + .sampledImageDepthSampleCounts = sample_counts, + .sampledImageStencilSampleCounts = sample_counts, .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT, /* Requires shaderStorageImageMultisample */ .maxSampleMaskWords = 1U, .timestampComputeAndGraphics = false, @@ -883,8 +892,7 @@ static bool pvr_physical_device_get_properties( .maxDescriptorSetUpdateAfterBindInputAttachments = 0, .filterMinmaxSingleComponentFormats = false, .filterMinmaxImageComponentMapping = false, - .framebufferIntegerColorSampleCounts = - VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, + .framebufferIntegerColorSampleCounts = sample_counts, /* Vulkan 1.0 / VK_KHR_maintenance2 */ .pointClippingBehavior = @@ -1318,6 +1326,23 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, supported_extensions.EXT_color_write_enable = false; pvr_physical_device_get_supported_features(&pdevice->dev_info, &supported_features); + /* + * G9P: a texel fetch outside its level reads 0 (lower_txf()), and a + * storage image access outside the view reads 0 or is dropped + * (lower_storage_image()), so image accesses are robust. With robust + * buffer access, Zink then offers GL_KHR_robustness. + */ + if (PVR_HAS_FEATURE(&pdevice->dev_info, apple_g9p)) { + supported_extensions.EXT_image_robustness = true; + supported_features.robustImageAccess = true; + /* + * G9P: a subpass load reads the colour attachment from the tile, as + * blending does (lower_subpass_load()), in the order the draws were + * given, so Zink offers framebuffer fetch and advanced blending + */ + supported_extensions.KHR_dynamic_rendering_local_read = true; + supported_features.dynamicRenderingLocalRead = true; + } if (!pvr_physical_device_get_properties(pdevice, &supported_properties)) { result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED, diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index 4dbd2a5b14f..70d73b00e81 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -128,9 +128,18 @@ struct pvr_g9p_compute_state { unsigned set, binding; } buffers[40]; /* G9P_MAX_BUFFERS */ unsigned num_regs; + /* registers in bank 1 (the record's field) when the rest are in bank 0; 0: all in bank 1 */ + unsigned bank1_regs; bool uses_workgroup_id; unsigned local_size[3]; PVR_G5P_COMPUTE_STATE_FIELDS unsigned shared_size; + /* combined image samplers, their words in the common store from G9P_CS_TEX_BASE */ + unsigned num_textures; + struct { + unsigned set, binding, index; + int sampler_set, sampler_binding; /* a separate sampler, binding -1 if none */ + unsigned sampler_index; + } textures[16]; /* G9P_MAX_TEXTURES */ }; /* Apple G9P (has_apple_g9p): a graphics pipeline's programs (pvr_arch_g9p_gfx.c) */ @@ -148,6 +157,16 @@ struct pvr_g9p_graphics_state { struct pvr_suballoc_bo *vs_bo, *fs_bo; /* vertex kick (code and data), fragment kick bundle, DOUTI program */ struct pvr_pds_upload vs_kick, fs_kick; + /* + * More colour words than a pixel holds: the targets in groups, a render each + * (g9p_target_groups()), and per group the fragment program that writes only its + * targets, all with the same uniforms, textures and varyings; group 0's are fs_bo + * and fs_kick + */ + unsigned num_groups; + unsigned group_targets[4]; + struct pvr_suballoc_bo *fs_group_bo[4]; + struct pvr_pds_upload fs_group_kick[4]; /* multiview: a vertex kick per view and its program (the view index a constant in it) */ struct pvr_pds_upload vs_kick_views[PVR_MAX_MULTIVIEW]; struct pvr_suballoc_bo *vs_view_bo[PVR_MAX_MULTIVIEW]; @@ -157,6 +176,11 @@ struct pvr_g9p_graphics_state { /* the DOUTI data in 16-byte units */ unsigned varying_units, varying_coeffs; unsigned num_varyings; + /* the varying slots by the TA's classes (g9p_gap_varyings()), or all perspective */ + bool varying_classes; + unsigned varying_persp_slots, varying_flat_slots; + /* per vertex binding: the end of its furthest attribute (the fetch's clamp) */ + uint32_t vertex_binding_end[PVR_MAX_VERTEX_INPUT_BINDINGS]; /* vertex output dwords */ unsigned vs_outputs; /* vertex shader registers (VDM_STATE5 sets their allocation) */ @@ -164,6 +188,7 @@ struct pvr_g9p_graphics_state { /* the fragment shader may discard: punch-through pass type */ bool uses_discard; bool uses_fbfetch; + bool punch_fbfetch; /* the vertex shader's common-store slots: vertex buffers, push constants, buffers */ unsigned num_buffers; struct { diff --git a/src/imagination/vulkan/rogue/pvr_blit.c b/src/imagination/vulkan/rogue/pvr_blit.c index 2a74894708b..518b39a88e7 100644 --- a/src/imagination/vulkan/rogue/pvr_blit.c +++ b/src/imagination/vulkan/rogue/pvr_blit.c @@ -1168,6 +1168,74 @@ pvr_copy_buffer_to_image_region(struct pvr_cmd_buffer *const cmd_buffer, flags); } +VkResult +pvr_g9p_ds_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_image *image, + uint32_t level, uint32_t layer, uint64_t scratch, uint32_t sbw, + uint32_t sbh, uint64_t stencil, bool to_scratch) +{ + struct pvr_transfer_cmd *transfer_cmd = pvr_transfer_cmd_alloc(cmd_buffer); + const VkExtent3D extent = { u_minify(image->vk.extent.width, level), + u_minify(image->vk.extent.height, level), 1 }; + const VkOffset3D origin = { 0 }; + struct pvr_transfer_cmd_surface level_surface, scratch_surface; + VkRect2D rect; + VkResult result; + + if (!transfer_cmd) + return VK_ERROR_OUT_OF_HOST_MEMORY; + pvr_setup_surface_for_image(cmd_buffer->device, &level_surface, &rect, image, layer, 0, level, + &origin, &extent, 0, image->vk.format, + VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT); + scratch_surface = level_surface; + scratch_surface.dev_addr = PVR_DEV_ADDR(scratch); + scratch_surface.width = scratch_surface.stride = sbw; + scratch_surface.height = sbh; + scratch_surface.mem_layout = PVR_MEMLAYOUT_TWIDDLED; + scratch_surface.g9p_stencil_offset = stencil; + transfer_cmd->sources[0].surface = to_scratch ? level_surface : scratch_surface; + transfer_cmd->dst = to_scratch ? scratch_surface : level_surface; + transfer_cmd->scissor = rect; + transfer_cmd->sources[0].mappings[0].src_rect = rect; + transfer_cmd->sources[0].mappings[0].dst_rect = rect; + transfer_cmd->sources[0].mapping_count = 1; + transfer_cmd->source_count = 1; + result = pvr_arch_cmd_buffer_add_transfer_cmd(cmd_buffer, transfer_cmd); + if (result != VK_SUCCESS) + vk_free(&cmd_buffer->vk.pool->alloc, transfer_cmd); + return result; +} + +VkResult +pvr_g9p_colour_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_image *image, + uint32_t level, uint32_t layer, uint64_t scratch, bool to_scratch) +{ + struct pvr_transfer_cmd *transfer_cmd = pvr_transfer_cmd_alloc(cmd_buffer); + const VkExtent3D extent = { u_minify(image->vk.extent.width, level), + u_minify(image->vk.extent.height, level), 1 }; + const VkOffset3D origin = { 0 }; + struct pvr_transfer_cmd_surface level_surface, scratch_surface; + VkRect2D rect; + VkResult result; + + if (!transfer_cmd) + return VK_ERROR_OUT_OF_HOST_MEMORY; + pvr_setup_surface_for_image(cmd_buffer->device, &level_surface, &rect, image, layer, 0, level, + &origin, &extent, 0, image->vk.format, VK_IMAGE_ASPECT_COLOR_BIT); + scratch_surface = level_surface; + scratch_surface.dev_addr = PVR_DEV_ADDR(scratch); + transfer_cmd->sources[0].surface = to_scratch ? level_surface : scratch_surface; + transfer_cmd->dst = to_scratch ? scratch_surface : level_surface; + transfer_cmd->scissor = rect; + transfer_cmd->sources[0].mappings[0].src_rect = rect; + transfer_cmd->sources[0].mappings[0].dst_rect = rect; + transfer_cmd->sources[0].mapping_count = 1; + transfer_cmd->source_count = 1; + result = pvr_arch_cmd_buffer_add_transfer_cmd(cmd_buffer, transfer_cmd); + if (result != VK_SUCCESS) + vk_free(&cmd_buffer->vk.pool->alloc, transfer_cmd); + return result; +} + void pvr_rogue_CmdCopyBufferToImage2( VkCommandBuffer commandBuffer, const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo)