Skip to content

Commit

Permalink
libhb: add a Metal utility function to add multiple pipelines to an e…
Browse files Browse the repository at this point in the history
…xisting context.
  • Loading branch information
galad87 committed Nov 17, 2023
1 parent e049628 commit 20e5db4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 71 deletions.
6 changes: 3 additions & 3 deletions libhb/platform/macosx/chroma_smooth_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ static void call_kernel(hb_filter_private_t *pv,

if (pv->global)
{
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, dst.width, dst.height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, dst.width, dst.height);
}
else
{
hb_metal_compute_encoder_dispatch_fixed_threadgroup_size(pv->mtl->device, pv->mtl->pipeline, encoder,
dst.width, dst.height, 16, 16);
hb_metal_compute_encoder_dispatch_fixed_threadgroup_size(pv->mtl->device, pv->mtl->pipelines[0],
encoder, dst.width, dst.height, 16, 16);
}
[encoder endEncoding];

Expand Down
53 changes: 13 additions & 40 deletions libhb/platform/macosx/comb_detect_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@

bool force_exaustive_check;

// Pipelines
id<MTLFunction> functions[5];
id<MTLComputePipelineState> pipelines[5];

// Mask textures
id<MTLTexture> mask;
id<MTLTexture> temp;
Expand Down Expand Up @@ -135,24 +131,6 @@ static void store_buf(hb_filter_private_t *pv, hb_buffer_t *in)
pv->ref[NEXT] = in;
}

static int add_pipeline(hb_filter_private_t *pv, const char *function_name, size_t index)
{
NSError *err = nil;
pv->functions[index] = [pv->mtl->library newFunctionWithName:@(function_name)];
if (!pv->functions[index])
{
hb_error("metal: failed to create Metal function");
return -1;
}
pv->pipelines[index] = [pv->mtl->device newComputePipelineStateWithFunction:pv->functions[index] error:&err];
if (!pv->pipelines[index])
{
hb_error("metal: failed to create Metal compute pipeline: %s", err.description.UTF8String);
return -1;
}
return 0;
}

static int comb_detect_vt_init(hb_filter_object_t *filter,
hb_filter_init_t *init)
{
Expand Down Expand Up @@ -250,15 +228,16 @@ static int comb_detect_vt_init(hb_filter_object_t *filter,
.force_exaustive_check = pv->force_exaustive_check
};

if (add_pipeline(pv, pv->filter_mode == FILTER_ERODE_DILATE ? "filter_erode_dilate" : "filter_classic", 0))
if (hb_metal_add_pipeline(pv->mtl, pv->filter_mode == FILTER_ERODE_DILATE ? "filter_erode_dilate" : "filter_classic",
pv->mtl->pipelines_count))
{
return -1;
}
if (add_pipeline(pv, "erode_mask", 1))
if (hb_metal_add_pipeline(pv->mtl, "erode_mask", pv->mtl->pipelines_count))
{
return -1;
}
if (add_pipeline(pv, "dilate_mask", 2))
if (hb_metal_add_pipeline(pv->mtl, "dilate_mask", pv->mtl->pipelines_count))
{
return -1;
}
Expand All @@ -272,11 +251,11 @@ static int comb_detect_vt_init(hb_filter_object_t *filter,
check_combing_name = pv->mode & MODE_FILTER ? "check_filtered_combing_mask_simd" : "check_combing_mask_simd";
}
}
if (add_pipeline(pv,check_combing_name, 3))
if (hb_metal_add_pipeline(pv->mtl,check_combing_name, pv->mtl->pipelines_count))
{
return -1;
}
if (add_pipeline(pv, "apply_mask", 4))
if (hb_metal_add_pipeline(pv->mtl, "apply_mask", pv->mtl->pipelines_count))
{
return -1;
}
Expand Down Expand Up @@ -312,12 +291,6 @@ static void comb_detect_vt_close(hb_filter_object_t *filter)
hb_log("comb detect: heavy %i | light %i | uncombed %i | total %i",
pv->comb_heavy, pv->comb_light, pv->comb_none, pv->frames);

for (int i = 0; i < 5; i++)
{
[pv->functions[i] release];
[pv->pipelines[i] release];
}

[pv->combed release];
[pv->temp release];
[pv->mask release];
Expand Down Expand Up @@ -359,17 +332,17 @@ static void call_kernel(hb_filter_private_t *pv,
[encoder setBuffer:pv->combed offset:0 atIndex:0];
[encoder setBuffer:pv->mtl->params_buffer offset:0 atIndex:1];

hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, width, height);

if (pv->mode & MODE_FILTER)
{
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->pipelines[0], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[1], encoder, width, height);

if (pv->filter_mode == FILTER_ERODE_DILATE)
{
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->pipelines[1], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->pipelines[2], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->pipelines[1], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[2], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[3], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[2], encoder, width, height);
}
}

Expand All @@ -378,12 +351,12 @@ static void call_kernel(hb_filter_private_t *pv,
[encoder setTexture:pv->temp atIndex:3];
}

hb_metal_compute_encoder_dispatch_fixed_threadgroup_size(pv->mtl->device, pv->pipelines[3], encoder,
hb_metal_compute_encoder_dispatch_fixed_threadgroup_size(pv->mtl->device, pv->mtl->pipelines[4], encoder,
width, height, pv->block_width, pv->block_height);

if (pv->mode & MODE_MASK || pv->mode & MODE_COMPOSITE)
{
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->pipelines[4], encoder, width, height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[5], encoder, width, height);
}

[encoder endEncoding];
Expand Down
2 changes: 1 addition & 1 deletion libhb/platform/macosx/deinterlace_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void call_kernel(hb_filter_private_t *pv,
[encoder setTexture:next atIndex:3];
[encoder setBuffer:pv->mtl->params_buffer offset:0 atIndex:0];

hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, dst.width, dst.height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, dst.width, dst.height);

[encoder endEncoding];

Expand Down
2 changes: 1 addition & 1 deletion libhb/platform/macosx/grayscale_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void call_kernel(hb_filter_private_t *pv,
}
[encoder setBuffer:pv->mtl->params_buffer offset:0 atIndex:0];

hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, dst.width, dst.height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, dst.width, dst.height);

[encoder endEncoding];

Expand Down
2 changes: 1 addition & 1 deletion libhb/platform/macosx/lapsharp_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static void call_kernel(hb_filter_private_t *pv,
[encoder setBuffer:pv->mem[plane] offset:0 atIndex:0];
[encoder setBuffer:pv->mtl->params_buffer offset:0 atIndex:1];

hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, dst.width, dst.height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, dst.width, dst.height);
[encoder endEncoding];

[buffer commit];
Expand Down
15 changes: 9 additions & 6 deletions libhb/platform/macosx/metal_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

struct hb_metal_context_s
{
id<MTLDevice> device;
id<MTLLibrary> library;
id<MTLCommandQueue> queue;
id<MTLComputePipelineState> pipeline;
id<MTLFunction> function;
id<MTLBuffer> params_buffer;
id<MTLDevice> device;
id<MTLLibrary> library;
id<MTLCommandQueue> queue;
id<MTLBuffer> params_buffer;
id<MTLComputePipelineState> *pipelines;
id<MTLFunction> *functions;
size_t pipelines_count;

CVMetalTextureCacheRef cache;
CVPixelBufferPoolRef pool;
Expand Down Expand Up @@ -55,4 +56,6 @@ CVMetalTextureRef hb_metal_create_texture_from_pixbuf(CVMetalTextureCacheRef tex
int plane,
MTLPixelFormat format);

int hb_metal_add_pipeline(hb_metal_context_t *ctx, const char *function_name, size_t index);

#endif /* HB_METAL_UTILS_H */
50 changes: 34 additions & 16 deletions libhb/platform/macosx/metal_utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@
goto fail;
}

ctx->function = [ctx->library newFunctionWithName:@(function_name)];
if (!ctx->function)
{
hb_error("metal: failed to create Metal function");
goto fail;
}

ctx->pipeline = [ctx->device newComputePipelineStateWithFunction:ctx->function error:&err];
if (!ctx->pipeline)
{
hb_error("metal: failed to create Metal compute pipeline: %s", err.description.UTF8String);
goto fail;
}

ctx->params_buffer = [ctx->device newBufferWithLength:params_buffer_len
options:MTLResourceStorageModeShared];
if (!ctx->params_buffer)
Expand All @@ -86,6 +72,12 @@
goto fail;
}

if (hb_metal_add_pipeline(ctx, function_name, 0))
{
hb_error("metal: failed to add Metal function");
goto fail;
}

CVReturn ret = CVMetalTextureCacheCreate(NULL, NULL, ctx->device, NULL, &ctx->cache);
if (ret != kCVReturnSuccess)
{
Expand All @@ -110,14 +102,40 @@
return NULL;
}

int hb_metal_add_pipeline(hb_metal_context_t *ctx, const char *function_name, size_t index)
{
if (ctx->pipelines_count < index + 1) {
ctx->pipelines_count = index + 1;
ctx->pipelines = av_realloc(ctx->pipelines, (ctx->pipelines_count) * sizeof(id<MTLComputePipelineState>));
ctx->functions = av_realloc(ctx->functions, (ctx->pipelines_count) * sizeof(id<MTLFunction>));
}
NSError *err = nil;
ctx->functions[index] = [ctx->library newFunctionWithName:@(function_name)];
if (!ctx->functions[index])
{
hb_error("metal: failed to create Metal function");
return -1;
}
ctx->pipelines[index] = [ctx->device newComputePipelineStateWithFunction:ctx->functions[index] error:&err];
if (!ctx->pipelines[index])
{
hb_error("metal: failed to create Metal compute pipeline: %s", err.description.UTF8String);
return -1;
}
return 0;
}

void hb_metal_context_close(hb_metal_context_t **_ctx)
{
hb_metal_context_t *ctx = *_ctx;
if (ctx)
{
for (int i = 0; i < ctx->pipelines_count; i++)
{
[ctx->functions[i] release];
[ctx->pipelines[i] release];
}
[ctx->params_buffer release];
[ctx->function release];
[ctx->pipeline release];
[ctx->queue release];
[ctx->library release];
[ctx->device release];
Expand Down
2 changes: 1 addition & 1 deletion libhb/platform/macosx/pad_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static void call_kernel(hb_filter_private_t *pv,
[encoder setTexture:src atIndex:1];
[encoder setBuffer:pv->mtl->params_buffer offset:0 atIndex:0];

hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, dst.width, dst.height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, dst.width, dst.height);

[encoder endEncoding];

Expand Down
4 changes: 2 additions & 2 deletions libhb/platform/macosx/unsharp_vt.m
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ static void call_kernel(hb_filter_private_t *pv,

if (pv->global)
{
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipeline, encoder, dst.width, dst.height);
hb_metal_compute_encoder_dispatch(pv->mtl->device, pv->mtl->pipelines[0], encoder, dst.width, dst.height);
}
else
{
hb_metal_compute_encoder_dispatch_fixed_threadgroup_size(pv->mtl->device, pv->mtl->pipeline, encoder,
hb_metal_compute_encoder_dispatch_fixed_threadgroup_size(pv->mtl->device, pv->mtl->pipelines[0], encoder,
dst.width, dst.height, 16, 16);
}
[encoder endEncoding];
Expand Down

0 comments on commit 20e5db4

Please sign in to comment.