diff --git a/CMakeLists.txt b/CMakeLists.txt index 54bc67d..c7747b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -306,7 +306,18 @@ if (DAEDALUS_BUILD_VULKAN) VERBATIM ) - add_custom_target(daedalus_shaders ALL DEPENDS ${NOOP_SPV} ${IDCT8_SPV} ${LPF_SPV} ${MC_SPV} ${LPF8_SPV} ${CDEF_SPV} ${H264DEBLOCK_SPV} ${H264_IDCT4_SPV} ${H264_IDCT8_SPV}) + set(H264_QPEL_MC20_SPV ${CMAKE_BINARY_DIR}/v3d_h264_qpel_mc20.spv) + add_custom_command( + OUTPUT ${H264_QPEL_MC20_SPV} + COMMAND ${GLSLANG_VALIDATOR} -V --target-env vulkan1.3 + -o ${H264_QPEL_MC20_SPV} + ${CMAKE_SOURCE_DIR}/src/v3d_h264_qpel_mc20.comp + DEPENDS ${CMAKE_SOURCE_DIR}/src/v3d_h264_qpel_mc20.comp + COMMENT "glslang: v3d_h264_qpel_mc20.comp -> v3d_h264_qpel_mc20.spv" + VERBATIM + ) + + add_custom_target(daedalus_shaders ALL DEPENDS ${NOOP_SPV} ${IDCT8_SPV} ${LPF_SPV} ${MC_SPV} ${LPF8_SPV} ${CDEF_SPV} ${H264DEBLOCK_SPV} ${H264_IDCT4_SPV} ${H264_IDCT8_SPV} ${H264_QPEL_MC20_SPV}) # v3d_runner — reusable Vulkan plumbing. add_library(v3d_runner STATIC src/v3d_runner.c) @@ -436,6 +447,7 @@ if (DAEDALUS_BUILD_VULKAN) ${H264DEBLOCK_SPV} ${H264_IDCT4_SPV} ${H264_IDCT8_SPV} + ${H264_QPEL_MC20_SPV} DESTINATION ${CMAKE_INSTALL_DATADIR}/daedalus-fourier/shaders ) endif() diff --git a/src/daedalus_core.c b/src/daedalus_core.c index 1348981..a7a184e 100644 --- a/src/daedalus_core.c +++ b/src/daedalus_core.c @@ -44,6 +44,8 @@ struct daedalus_ctx { v3d_pipeline h264_idct4_pipe; int h264_idct8_pipe_ready; v3d_pipeline h264_idct8_pipe; + int h264_qpel_mc20_pipe_ready; + v3d_pipeline h264_qpel_mc20_pipe; }; daedalus_ctx *daedalus_ctx_create(void) @@ -100,6 +102,7 @@ void daedalus_ctx_destroy(daedalus_ctx *ctx) 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); + if (ctx->h264_qpel_mc20_pipe_ready) v3d_runner_destroy_pipeline(ctx->runner, &ctx->h264_qpel_mc20_pipe); v3d_runner_destroy(ctx->runner); } free(ctx); @@ -127,7 +130,7 @@ daedalus_substrate daedalus_recipe_substrate_for(daedalus_kernel k) 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 */ + case DAEDALUS_KERNEL_H264_QPEL_MC20: return DAEDALUS_SUBSTRATE_QPU; /* v3d_h264_qpel_mc20.spv */ } return DAEDALUS_SUBSTRATE_CPU; } @@ -930,6 +933,107 @@ fail: return -1; } +/* -------------------- H.264 qpel mc20 QPU dispatch (cycle 9) --- */ + +typedef struct { + uint32_t n_blocks; + uint32_t stride_u8; + uint32_t _pad0; + uint32_t _pad1; +} h264_qpel_mc20_pc; + +static int dispatch_h264_qpel_mc20_qpu(daedalus_ctx *ctx, + uint8_t *dst, const uint8_t *src, size_t stride, + size_t n_blocks, const daedalus_h264_qpel_meta *meta) +{ + if (!ctx->h264_qpel_mc20_pipe_ready) { + if (v3d_runner_create_pipeline(ctx->runner, "v3d_h264_qpel_mc20.spv", + 3, sizeof(h264_qpel_mc20_pc), + &ctx->h264_qpel_mc20_pipe) != 0) + return -1; + ctx->h264_qpel_mc20_pipe_ready = 1; + } + + /* Compute the smallest contiguous src/dst window that covers + * every block's read/write footprint. + * + * src: filter reads cols (c-2)..(c+3) for c=0..7 across rows 0..7. + * Highest read = src_off + 7*stride + (7 + 3) = src_off + 7*stride + 10. + * Plus 1 for the byte-count semantic of memcpy (length=N copies + * indices 0..N-1) → src_max = src_off + 7*stride + 11. + * + * dst: writes cols 0..7 across rows 0..7. + * Highest write = dst_off + 7*stride + 7; +1 → dst_off + 7*stride + 8. */ + size_t meta_bytes = n_blocks * 4 * sizeof(uint32_t); + size_t src_max = 0, dst_max = 0; + for (size_t i = 0; i < n_blocks; i++) { + size_t s_end = meta[i].src_off + (size_t) 7 * stride + 11; + size_t d_end = meta[i].dst_off + (size_t) 7 * stride + 8; + if (s_end > src_max) src_max = s_end; + if (d_end > dst_max) dst_max = d_end; + } + + v3d_buffer bs = {0}, bd = {0}, bm = {0}; + if (v3d_runner_create_buffer(ctx->runner, src_max, &bs)) return -1; + if (v3d_runner_create_buffer(ctx->runner, dst_max, &bd)) { + v3d_runner_destroy_buffer(ctx->runner, &bs); 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, &bs); return -1; + } + + /* Copy src window (filter needs cols -2..+3, captured by src_max + * upper bound above; the lower bound is implicit in src_off >= 2 + * which the caller guarantees per the public API contract). */ + memcpy(bs.mapped, src, src_max); + 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] = meta[i].src_off; + m[4*i+2] = 0; + m[4*i+3] = 0; + } + + v3d_buffer binds[3] = { bs, bd, bm }; + if (v3d_runner_bind_buffers(ctx->runner, &ctx->h264_qpel_mc20_pipe, binds, 3)) + goto fail; + + uint32_t wg_count = (uint32_t) n_blocks; /* 1 block per WG */ + h264_qpel_mc20_pc pc = { + .n_blocks = (uint32_t) n_blocks, + .stride_u8 = (uint32_t) 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_qpel_mc20_pipe.pipeline); + vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, + ctx->h264_qpel_mc20_pipe.layout, 0, 1, + &ctx->h264_qpel_mc20_pipe.desc_set, 0, NULL); + vkCmdPushConstants(cb, ctx->h264_qpel_mc20_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); + + v3d_runner_destroy_buffer(ctx->runner, &bm); + v3d_runner_destroy_buffer(ctx->runner, &bd); + v3d_runner_destroy_buffer(ctx->runner, &bs); + return 0; +fail: + v3d_runner_destroy_buffer(ctx->runner, &bm); + v3d_runner_destroy_buffer(ctx->runner, &bd); + v3d_runner_destroy_buffer(ctx->runner, &bs); + return -1; +} + /* -------------------- Public dispatch entry points -------------- */ #define ROUTE_CPU_ONLY(_kernel, _cpu_fn, ...) \ @@ -1065,8 +1169,16 @@ int daedalus_dispatch_h264_qpel_mc20(daedalus_ctx *ctx, daedalus_substrate sub, uint8_t *dst, const uint8_t *src, size_t stride, size_t n_blocks, const daedalus_h264_qpel_meta *meta) { - ROUTE_CPU_ONLY(DAEDALUS_KERNEL_H264_QPEL_MC20, dispatch_h264_qpel_mc20_cpu, - dst, src, stride, n_blocks, meta); + daedalus_substrate eff = sub; + if (eff == DAEDALUS_SUBSTRATE_AUTO) + eff = daedalus_recipe_substrate_for(DAEDALUS_KERNEL_H264_QPEL_MC20); + if (eff == DAEDALUS_SUBSTRATE_QPU && !daedalus_ctx_has_qpu(ctx)) + eff = DAEDALUS_SUBSTRATE_CPU; + if (eff == DAEDALUS_SUBSTRATE_CPU) + return dispatch_h264_qpel_mc20_cpu(ctx, dst, src, stride, + n_blocks, meta); + return dispatch_h264_qpel_mc20_qpu(ctx, dst, src, stride, + n_blocks, meta); } /* -------------------- Recipe convenience wrappers --------------- */ diff --git a/src/v3d_h264_qpel_mc20.comp b/src/v3d_h264_qpel_mc20.comp new file mode 100644 index 0000000..51001f9 --- /dev/null +++ b/src/v3d_h264_qpel_mc20.comp @@ -0,0 +1,83 @@ +// daedalus-fourier — H.264 luma qpel mc20 (8x8, horizontal half-pel), V3D 7.1. +// +// H.264 spec §8.4.2.2.1 horizontal 6-tap luma interpolation: +// +// dst[r,c] = clip255( +// ( s[r,c-2] +// - 5 * s[r,c-1] +// + 20 * s[r,c] +// + 20 * s[r,c+1] +// - 5 * s[r,c+2] +// + s[r,c+3] +// + 16 +// ) >> 5) +// +// Single-stride: dst and src share `stride` (H264QpelContext +// convention). src+src_off already points at the leftmost output +// column (col 0); the filter reads cols -2..+3. Caller guarantees +// edge-padding context per the public API docstring. +// +// Workgroup layout: 64 invocations = 1 lane per output pixel. +// 1 block per WG; n_blocks WGs total. This is the simplest layout +// that avoids any inter-lane communication — each lane independently +// reads its 6 src samples and writes its 1 dst sample. V3D's L2 +// cache handles the redundant reads from adjacent lanes. +// +// License: BSD-2-Clause. + +#version 450 +#extension GL_EXT_shader_8bit_storage : require +#extension GL_EXT_shader_explicit_arithmetic_types : require + +layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; + +layout(binding = 0) readonly buffer Src { + uint8_t src[]; +} u_src; + +layout(binding = 1) buffer Dst { + uint8_t dst[]; +} u_dst; + +layout(binding = 2) readonly buffer Meta { + uvec4 meta[]; // .x = dst_off, .y = src_off +} u_meta; + +layout(push_constant) uniform PC { + uint n_blocks; + uint stride_u8; + uint _pad0, _pad1; +} pc; + +void main() +{ + // 1 block per WG, 64 lanes covering the 8x8 output block. + uint wg_id = gl_WorkGroupID.x; + uint block_idx = wg_id; + if (block_idx >= pc.n_blocks) return; + + uint lane = gl_LocalInvocationID.x; + uint r = lane >> 3; // 0..7 (row) + uint c = lane & 7u; // 0..7 (column) + + uint dst_off = u_meta.meta[block_idx].x; + uint src_off = u_meta.meta[block_idx].y; + uint stride = pc.stride_u8; + + // src points at output col 0 of the block; filter reads cols -2..+3 + // of the current row. Negative col arithmetic is unsigned-safe + // because src_off >= 2 (caller-guaranteed left context). + uint row_base = src_off + r * stride + c; + + int s_m2 = int(u_src.src[row_base - 2u]); + int s_m1 = int(u_src.src[row_base - 1u]); + int s_0 = int(u_src.src[row_base + 0u]); + int s_p1 = int(u_src.src[row_base + 1u]); + int s_p2 = int(u_src.src[row_base + 2u]); + int s_p3 = int(u_src.src[row_base + 3u]); + + int v = s_m2 - 5 * s_m1 + 20 * s_0 + 20 * s_p1 - 5 * s_p2 + s_p3 + 16; + int p = clamp(v >> 5, 0, 255); + + u_dst.dst[dst_off + r * stride + c] = uint8_t(p); +}