Merge pull request 'QPU is default substrate: recipe table + ctx env-var override' (#7) from noether/qpu-default-recipe-cycles-5-8 into main
Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
+241
-10
@@ -40,6 +40,10 @@ struct daedalus_ctx {
|
||||
v3d_pipeline cdef_pipe;
|
||||
int h264deblock_pipe_ready;
|
||||
v3d_pipeline h264deblock_pipe;
|
||||
int h264_idct4_pipe_ready;
|
||||
v3d_pipeline h264_idct4_pipe;
|
||||
int h264_idct8_pipe_ready;
|
||||
v3d_pipeline h264_idct8_pipe;
|
||||
};
|
||||
|
||||
daedalus_ctx *daedalus_ctx_create(void)
|
||||
@@ -53,6 +57,25 @@ daedalus_ctx *daedalus_ctx_create(void)
|
||||
|
||||
daedalus_ctx *daedalus_ctx_create_no_qpu(void)
|
||||
{
|
||||
/*
|
||||
* Per the "QPU is default substrate" decree 2026-05-23:
|
||||
* setting DAEDALUS_FORCE_QPU=1 in the process env escalates this
|
||||
* function to a full daedalus_ctx_create(), letting the libavcodec
|
||||
* substitution shims (which call create_no_qpu via pthread_once)
|
||||
* fire the V3D shaders that exist for cycles 1/2/4/5/8. Without
|
||||
* this hook each consumer process (firefox, mpv, daemon) would
|
||||
* need its own shim build to opt into QPU.
|
||||
*
|
||||
* Default behaviour (env var unset / not "1") is unchanged: pure
|
||||
* NEON ctx, no implicit Vulkan init. Firefox / mpv consumers
|
||||
* that dlopen libavcodec without opting in stay on the
|
||||
* Vulkan-free path; the daemon explicitly sets
|
||||
* DAEDALUS_FORCE_QPU=1 before loading libavcodec.
|
||||
*/
|
||||
const char *force = getenv("DAEDALUS_FORCE_QPU");
|
||||
if (force && force[0] == '1' && force[1] == 0)
|
||||
return daedalus_ctx_create();
|
||||
|
||||
daedalus_ctx *ctx = calloc(1, sizeof(*ctx));
|
||||
if (!ctx) return NULL;
|
||||
ctx->has_qpu = 0;
|
||||
@@ -75,6 +98,8 @@ void daedalus_ctx_destroy(daedalus_ctx *ctx)
|
||||
if (ctx->mc8h_pipe_ready) v3d_runner_destroy_pipeline(ctx->runner, &ctx->mc8h_pipe);
|
||||
if (ctx->cdef_pipe_ready) v3d_runner_destroy_pipeline(ctx->runner, &ctx->cdef_pipe);
|
||||
if (ctx->h264deblock_pipe_ready) v3d_runner_destroy_pipeline(ctx->runner, &ctx->h264deblock_pipe);
|
||||
if (ctx->h264_idct4_pipe_ready) v3d_runner_destroy_pipeline(ctx->runner, &ctx->h264_idct4_pipe);
|
||||
if (ctx->h264_idct8_pipe_ready) v3d_runner_destroy_pipeline(ctx->runner, &ctx->h264_idct8_pipe);
|
||||
v3d_runner_destroy(ctx->runner);
|
||||
}
|
||||
free(ctx);
|
||||
@@ -84,16 +109,25 @@ void daedalus_ctx_destroy(daedalus_ctx *ctx)
|
||||
|
||||
daedalus_substrate daedalus_recipe_substrate_for(daedalus_kernel k)
|
||||
{
|
||||
/*
|
||||
* Recipe table per the "QPU is default substrate" decree
|
||||
* 2026-05-23. Any kernel that has a V3D compute shader returns
|
||||
* SUBSTRATE_QPU; CPU is the fallback for kernels without a
|
||||
* shader (still the case for H.264 IDCT 4x4 / IDCT 8x8 / qpel
|
||||
* mc20 — covered by follow-on task 165). The dispatch
|
||||
* wrappers already fall back to CPU automatically when the
|
||||
* ctx doesn't have QPU available (daedalus_ctx_has_qpu == 0).
|
||||
*/
|
||||
switch (k) {
|
||||
case DAEDALUS_KERNEL_VP9_IDCT8: return DAEDALUS_SUBSTRATE_QPU;
|
||||
case DAEDALUS_KERNEL_VP9_LPF4_INNER: return DAEDALUS_SUBSTRATE_QPU;
|
||||
case DAEDALUS_KERNEL_VP9_MC_8H: return DAEDALUS_SUBSTRATE_CPU;
|
||||
case DAEDALUS_KERNEL_VP9_MC_8H: return DAEDALUS_SUBSTRATE_QPU; /* v3d_mc_8h.spv */
|
||||
case DAEDALUS_KERNEL_VP9_LPF8_INNER: return DAEDALUS_SUBSTRATE_QPU;
|
||||
case DAEDALUS_KERNEL_AV1_CDEF_8X8: return DAEDALUS_SUBSTRATE_CPU;
|
||||
case DAEDALUS_KERNEL_H264_IDCT4: return DAEDALUS_SUBSTRATE_CPU;
|
||||
case DAEDALUS_KERNEL_H264_IDCT8: return DAEDALUS_SUBSTRATE_CPU;
|
||||
case DAEDALUS_KERNEL_H264_DEBLOCK_LV: return DAEDALUS_SUBSTRATE_CPU;
|
||||
case DAEDALUS_KERNEL_H264_QPEL_MC20: return DAEDALUS_SUBSTRATE_CPU;
|
||||
case DAEDALUS_KERNEL_AV1_CDEF_8X8: return DAEDALUS_SUBSTRATE_QPU; /* v3d_cdef.spv */
|
||||
case DAEDALUS_KERNEL_H264_IDCT4: return DAEDALUS_SUBSTRATE_QPU; /* v3d_h264_idct4.spv */
|
||||
case DAEDALUS_KERNEL_H264_IDCT8: return DAEDALUS_SUBSTRATE_QPU; /* v3d_h264_idct8.spv */
|
||||
case DAEDALUS_KERNEL_H264_DEBLOCK_LV: return DAEDALUS_SUBSTRATE_QPU; /* v3d_h264deblock.spv */
|
||||
case DAEDALUS_KERNEL_H264_QPEL_MC20: return DAEDALUS_SUBSTRATE_CPU; /* TODO task #165 */
|
||||
}
|
||||
return DAEDALUS_SUBSTRATE_CPU;
|
||||
}
|
||||
@@ -715,6 +749,187 @@ fail:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -------------------- H.264 IDCT 4x4 QPU dispatch (cycle 6) ----- */
|
||||
|
||||
typedef struct {
|
||||
uint32_t n_blocks;
|
||||
uint32_t dst_stride_u8;
|
||||
uint32_t _pad0;
|
||||
uint32_t _pad1;
|
||||
} h264_idct4_pc;
|
||||
|
||||
static int dispatch_h264_idct4_qpu(daedalus_ctx *ctx,
|
||||
uint8_t *dst, size_t dst_stride,
|
||||
int16_t *coeffs, size_t n_blocks,
|
||||
const daedalus_h264_block_meta *meta)
|
||||
{
|
||||
if (!ctx->h264_idct4_pipe_ready) {
|
||||
if (v3d_runner_create_pipeline(ctx->runner, "v3d_h264_idct4.spv",
|
||||
3, sizeof(h264_idct4_pc),
|
||||
&ctx->h264_idct4_pipe) != 0)
|
||||
return -1;
|
||||
ctx->h264_idct4_pipe_ready = 1;
|
||||
}
|
||||
|
||||
size_t coeff_bytes = n_blocks * 16 * sizeof(int16_t);
|
||||
size_t meta_bytes = n_blocks * 4 * sizeof(uint32_t); /* uvec4 per block */
|
||||
size_t dst_max = 0;
|
||||
for (size_t i = 0; i < n_blocks; i++) {
|
||||
size_t e = meta[i].dst_off + (size_t) 3 * dst_stride + 4;
|
||||
if (e > dst_max) dst_max = e;
|
||||
}
|
||||
|
||||
v3d_buffer bc = {0}, bd = {0}, bm = {0};
|
||||
if (v3d_runner_create_buffer(ctx->runner, coeff_bytes, &bc)) return -1;
|
||||
if (v3d_runner_create_buffer(ctx->runner, dst_max, &bd)) {
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc); return -1;
|
||||
}
|
||||
if (v3d_runner_create_buffer(ctx->runner, meta_bytes, &bm)) {
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bd);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc); return -1;
|
||||
}
|
||||
|
||||
memcpy(bc.mapped, coeffs, coeff_bytes);
|
||||
memcpy(bd.mapped, dst, dst_max);
|
||||
uint32_t *m = bm.mapped;
|
||||
for (size_t i = 0; i < n_blocks; i++) {
|
||||
m[4*i+0] = meta[i].dst_off;
|
||||
m[4*i+1] = 0;
|
||||
m[4*i+2] = 0;
|
||||
m[4*i+3] = 0;
|
||||
}
|
||||
|
||||
v3d_buffer binds[3] = { bc, bd, bm };
|
||||
if (v3d_runner_bind_buffers(ctx->runner, &ctx->h264_idct4_pipe, binds, 3))
|
||||
goto fail;
|
||||
|
||||
uint32_t wg_count = (uint32_t)((n_blocks + 15) / 16); /* 16 blocks/WG */
|
||||
h264_idct4_pc pc = {
|
||||
.n_blocks = (uint32_t) n_blocks,
|
||||
.dst_stride_u8 = (uint32_t) dst_stride,
|
||||
};
|
||||
|
||||
VkCommandBuffer cb = v3d_runner_alloc_cmdbuf(ctx->runner);
|
||||
if (cb == VK_NULL_HANDLE) goto fail;
|
||||
VkCommandBufferBeginInfo cbbi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
||||
vkBeginCommandBuffer(cb, &cbbi);
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
ctx->h264_idct4_pipe.pipeline);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
ctx->h264_idct4_pipe.layout, 0, 1,
|
||||
&ctx->h264_idct4_pipe.desc_set, 0, NULL);
|
||||
vkCmdPushConstants(cb, ctx->h264_idct4_pipe.layout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
|
||||
vkCmdDispatch(cb, wg_count, 1, 1);
|
||||
vkEndCommandBuffer(cb);
|
||||
if (v3d_runner_submit_wait(ctx->runner, cb)) goto fail;
|
||||
|
||||
memcpy(dst, bd.mapped, dst_max);
|
||||
|
||||
/* H.264/FFmpeg convention: zero the coeffs block after the
|
||||
* transform (matches the C ref + NEON .S behaviour). */
|
||||
memset(coeffs, 0, coeff_bytes);
|
||||
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bm);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bd);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc);
|
||||
return 0;
|
||||
fail:
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bm);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bd);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -------------------- H.264 IDCT 8x8 QPU dispatch (cycle 7) ----- */
|
||||
|
||||
typedef struct {
|
||||
uint32_t n_blocks;
|
||||
uint32_t dst_stride_u8;
|
||||
uint32_t _pad0;
|
||||
uint32_t _pad1;
|
||||
} h264_idct8_pc;
|
||||
|
||||
static int dispatch_h264_idct8_qpu(daedalus_ctx *ctx,
|
||||
uint8_t *dst, size_t dst_stride,
|
||||
int16_t *coeffs, size_t n_blocks,
|
||||
const daedalus_h264_block_meta *meta)
|
||||
{
|
||||
if (!ctx->h264_idct8_pipe_ready) {
|
||||
if (v3d_runner_create_pipeline(ctx->runner, "v3d_h264_idct8.spv",
|
||||
3, sizeof(h264_idct8_pc),
|
||||
&ctx->h264_idct8_pipe) != 0)
|
||||
return -1;
|
||||
ctx->h264_idct8_pipe_ready = 1;
|
||||
}
|
||||
|
||||
size_t coeff_bytes = n_blocks * 64 * sizeof(int16_t);
|
||||
size_t meta_bytes = n_blocks * 4 * sizeof(uint32_t);
|
||||
size_t dst_max = 0;
|
||||
for (size_t i = 0; i < n_blocks; i++) {
|
||||
size_t e = meta[i].dst_off + (size_t) 7 * dst_stride + 8;
|
||||
if (e > dst_max) dst_max = e;
|
||||
}
|
||||
|
||||
v3d_buffer bc = {0}, bd = {0}, bm = {0};
|
||||
if (v3d_runner_create_buffer(ctx->runner, coeff_bytes, &bc)) return -1;
|
||||
if (v3d_runner_create_buffer(ctx->runner, dst_max, &bd)) {
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc); return -1;
|
||||
}
|
||||
if (v3d_runner_create_buffer(ctx->runner, meta_bytes, &bm)) {
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bd);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc); return -1;
|
||||
}
|
||||
|
||||
memcpy(bc.mapped, coeffs, coeff_bytes);
|
||||
memcpy(bd.mapped, dst, dst_max);
|
||||
uint32_t *m = bm.mapped;
|
||||
for (size_t i = 0; i < n_blocks; i++) {
|
||||
m[4*i+0] = meta[i].dst_off;
|
||||
m[4*i+1] = 0;
|
||||
m[4*i+2] = 0;
|
||||
m[4*i+3] = 0;
|
||||
}
|
||||
|
||||
v3d_buffer binds[3] = { bc, bd, bm };
|
||||
if (v3d_runner_bind_buffers(ctx->runner, &ctx->h264_idct8_pipe, binds, 3))
|
||||
goto fail;
|
||||
|
||||
uint32_t wg_count = (uint32_t)((n_blocks + 7) / 8); /* 8 blocks/WG */
|
||||
h264_idct8_pc pc = {
|
||||
.n_blocks = (uint32_t) n_blocks,
|
||||
.dst_stride_u8 = (uint32_t) dst_stride,
|
||||
};
|
||||
|
||||
VkCommandBuffer cb = v3d_runner_alloc_cmdbuf(ctx->runner);
|
||||
if (cb == VK_NULL_HANDLE) goto fail;
|
||||
VkCommandBufferBeginInfo cbbi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
||||
vkBeginCommandBuffer(cb, &cbbi);
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
ctx->h264_idct8_pipe.pipeline);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
ctx->h264_idct8_pipe.layout, 0, 1,
|
||||
&ctx->h264_idct8_pipe.desc_set, 0, NULL);
|
||||
vkCmdPushConstants(cb, ctx->h264_idct8_pipe.layout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
|
||||
vkCmdDispatch(cb, wg_count, 1, 1);
|
||||
vkEndCommandBuffer(cb);
|
||||
if (v3d_runner_submit_wait(ctx->runner, cb)) goto fail;
|
||||
|
||||
memcpy(dst, bd.mapped, dst_max);
|
||||
memset(coeffs, 0, coeff_bytes);
|
||||
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bm);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bd);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc);
|
||||
return 0;
|
||||
fail:
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bm);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bd);
|
||||
v3d_runner_destroy_buffer(ctx->runner, &bc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -------------------- Public dispatch entry points -------------- */
|
||||
|
||||
#define ROUTE_CPU_ONLY(_kernel, _cpu_fn, ...) \
|
||||
@@ -803,8 +1018,16 @@ int daedalus_dispatch_h264_idct4(daedalus_ctx *ctx, daedalus_substrate sub,
|
||||
int16_t *coeffs, size_t n_blocks,
|
||||
const daedalus_h264_block_meta *meta)
|
||||
{
|
||||
ROUTE_CPU_ONLY(DAEDALUS_KERNEL_H264_IDCT4, dispatch_h264_idct4_cpu,
|
||||
dst, dst_stride, coeffs, n_blocks, meta);
|
||||
daedalus_substrate eff = sub;
|
||||
if (eff == DAEDALUS_SUBSTRATE_AUTO)
|
||||
eff = daedalus_recipe_substrate_for(DAEDALUS_KERNEL_H264_IDCT4);
|
||||
if (eff == DAEDALUS_SUBSTRATE_QPU && !daedalus_ctx_has_qpu(ctx))
|
||||
eff = DAEDALUS_SUBSTRATE_CPU;
|
||||
if (eff == DAEDALUS_SUBSTRATE_CPU)
|
||||
return dispatch_h264_idct4_cpu(ctx, dst, dst_stride,
|
||||
coeffs, n_blocks, meta);
|
||||
return dispatch_h264_idct4_qpu(ctx, dst, dst_stride,
|
||||
coeffs, n_blocks, meta);
|
||||
}
|
||||
|
||||
int daedalus_dispatch_h264_idct8(daedalus_ctx *ctx, daedalus_substrate sub,
|
||||
@@ -812,8 +1035,16 @@ int daedalus_dispatch_h264_idct8(daedalus_ctx *ctx, daedalus_substrate sub,
|
||||
int16_t *coeffs, size_t n_blocks,
|
||||
const daedalus_h264_block_meta *meta)
|
||||
{
|
||||
ROUTE_CPU_ONLY(DAEDALUS_KERNEL_H264_IDCT8, dispatch_h264_idct8_cpu,
|
||||
dst, dst_stride, coeffs, n_blocks, meta);
|
||||
daedalus_substrate eff = sub;
|
||||
if (eff == DAEDALUS_SUBSTRATE_AUTO)
|
||||
eff = daedalus_recipe_substrate_for(DAEDALUS_KERNEL_H264_IDCT8);
|
||||
if (eff == DAEDALUS_SUBSTRATE_QPU && !daedalus_ctx_has_qpu(ctx))
|
||||
eff = DAEDALUS_SUBSTRATE_CPU;
|
||||
if (eff == DAEDALUS_SUBSTRATE_CPU)
|
||||
return dispatch_h264_idct8_cpu(ctx, dst, dst_stride,
|
||||
coeffs, n_blocks, meta);
|
||||
return dispatch_h264_idct8_qpu(ctx, dst, dst_stride,
|
||||
coeffs, n_blocks, meta);
|
||||
}
|
||||
|
||||
int daedalus_dispatch_h264_deblock_luma_v(daedalus_ctx *ctx, daedalus_substrate sub,
|
||||
|
||||
Reference in New Issue
Block a user