3

In Metal we can create samplers like this

      constexpr sampler s(coord::normalized, address::clamp_to_edge, filter::linear);

Then we can sample any texture using this sampler.

I am wondering if something similar is available in Metal Core Image Kernels or not.

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131

1 Answers1

3

Yes, there is! You can use a CISampler that you can pass to your kernel instead of a CIImage:

let sampler = CISampler(image: image, options: [
    kCISamplerWrapMode: kCISamplerWrapClamp,
    kCISamplerFilterMode: kCISamplerFilterLinear
])

You can also set kCISamplerAffineMatrix in the options to define a transform that is applied to the coordinates when sampling (normalized is the default behavior, I think).

Alternatively, you can use these convenience helpers on CIImage to set the wrap and filter modes:

// for linear & clamp-to-edge
let newImage = image.samplingLinear().clampedToExtent()
// for nearest & clamp-to-black (actually transparent, not black)
let newImage = image.samplingNearest().cropped(to: aRect)

Using these methods you can also force built-in filters to use the corresponding modes.

Frank Rupprecht
  • 9,191
  • 31
  • 56
  • Please have a look at [this](https://stackoverflow.com/questions/71005484/metal-core-image-kernel-sampling) question. My doubts regarding samplers are growing! – Deepak Sharma Feb 06 '22 at 08:43
  • Also another [one](https://stackoverflow.com/questions/71005717/metal-core-image-kernel-use-of-dod) related to sampling. Please have a look. – Deepak Sharma Feb 06 '22 at 09:19
  • Do you know how to convert existing Metal fragment shader to Core Image Kernel? If in Metal fragment shader we have interpolating vertex coordinate using which we sample all textures in the fragment shader, what is the equivalent in Core Image Kernel? – Deepak Sharma Feb 08 '22 at 13:35
  • I'm afraid I don't quite understand... CI kernels are compute (not fragment) shaders. Pixel values will be interpolated between coordinates based on the sampler settings (linear or nearest). – Frank Rupprecht Feb 08 '22 at 15:57
  • I understand. What I am stuck in is accurately translating code written in Metal vertex/fragment shader to compute shader (CIKernel). – Deepak Sharma Feb 08 '22 at 16:42
  • Also, CISampler passed to the kernel has infinite extent it seems. It is not the same as extent of the image used to create sampler. – Deepak Sharma Feb 08 '22 at 16:49