Skip to content

Commit

Permalink
libhb: add a Metal utility function to append multiple pipelines to a…
Browse files Browse the repository at this point in the history
…n existing Metal context struct.
  • Loading branch information
galad87 committed Nov 24, 2023
1 parent 28d5e68 commit ad20fe4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 31 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
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,10 +57,11 @@
goto fail;
}

ctx->function = [ctx->library newFunctionWithName:@(function_name)];
if (!ctx->function)
ctx->params_buffer = [ctx->device newBufferWithLength:params_buffer_len
options:MTLResourceStorageModeShared];
if (!ctx->params_buffer)
{
hb_error("metal: failed to create Metal function");
hb_error("metal: failed to create Metal buffer for parameters");
goto fail;
}

Expand All @@ -71,18 +72,9 @@
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)
if (hb_metal_add_pipeline(ctx, function_name, 0))
{
hb_error("metal: failed to create Metal buffer for parameters");
hb_error("metal: failed to add Metal function");
goto fail;
}

Expand Down Expand Up @@ -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 ad20fe4

Please sign in to comment.