Silence-GitHub / BBMetalImage

A high performance Swift library for GPU-accelerated image/video processing based on Metal.
MIT License
989 stars 126 forks source link

How to implement a custom filter? #9

Closed hoangdado closed 5 years ago

hoangdado commented 5 years ago

Hi @Silence-GitHub, thanks for your great project, i integrated BBMetalImage to my project and tested build-in filter succesfully. But when I tried to create a custom filter, it failed. Bewlow is the flow i did:

  1. Create a metal file MyProject.metal
  2. Copy all content of file BBMetalContrastFilter.metal to MyProject.metal and rename "contrastKernel" to "testKernel"
  3. Create a copy of BBMetalContrastFilter.swift with just renamed to TestFilter.swift then change the line super.init(kernelFunctionName: "contrastKernel") to super.init(kernelFunctionName: "testKernel")
  4. Create TestFilter and run

The program crashed with error Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value at line 232: encoder.setComputePipelineState(computePipeline) of file BBMetalBaseFilter.swift

How could I solve this problem. Thanks in advanced!

Silence-GitHub commented 5 years ago

By default, filter searches kernel shader in BBMetalImage project bundle, not in main bundle. From version 1.0.3, BBMetalImage can search kernel shader in main bundle. When you init a custom filter, pass true to useMainBundleKernel parameter.

super.init(kernelFunctionName: "testKernel", useMainBundleKernel: true)
hoangdado commented 5 years ago

Thanks @Silence-GitHub, now it is working :)