Thanks for your reply. Strangely I didn't get an email notification of your reply.
Here are some links to other forum posts:
https://developer.apple.com/forums/thread/650405
https://developer.apple.com/forums/thread/125431
There is also a discussion here: https://stackoverflow.com/questions/57751660/metal-shading-language-for-core-image-color-kernel-how-to-pass-an-array-of-floa
On the Metal side I am using the extern "C" method.
I've tried multiple setups using both an array of structs and a simple array of floats:
struct MyStruct {
float value1;
float value2;
float value3;
float value4;
float value5;
float value6;
};
extern "C" float4 myKernel(coreimage::sample_t pixel, uint count, constant MyStruct *myArray) {
for (uint i = 0; i < count; ++i) {
MyStruct myStruct = myArray[I];
float valueToProcess = myStruct.value1;
Or
extern "C" float4 myKernel(coreimage::sample_t pixel, uint count, constant float myValues[]) {
On the Swift side the data is sent into the CIColorKernel like:
struct MyStruct {
let value1: Float
let value2: Float
let value3: Float
let value4: Float
let value5: Float
let value6: Float
}
let myArray: [MyStruct] = ...
let data = Data(bytes: myArray, count: MemoryLayout<MyStruct>.stride * myArray.count)
or
let data = myArray.withUnsafeBufferPointer { Data(buffer: $0) }
kernel.apply(
extent: inputImage.extent,
roiCallback: { _, rect in rect },
arguments: [
inputImage,
UInt(myArray.count),
data
]
)
Or
let myArray: [Float] = ...
let data = myArray.withUnsafeBufferPointer { Data(buffer: $0) }
kernel.apply(
extent: inputImage.extent,
roiCallback: { _, rect in rect },
arguments: [
inputImage,
UInt(myArray.count),
data
]
)
I tried multiple combinations including using SIMD types but couldn’t get any to work reliably, so I’ve reverted back to a fixed set of CIVectors for now. Ideally I need an arbitrary array size that works similarly to a MTLArgumentEncoder.
As I already mentioned, I get the expected behavior up to a certain data size (1-2 structs or around 4 floats in the array), afterwards excess data is discarded. As the size grows beyond the threshold the instability randomly increases with flickering and other unexpected results.
Thanks!
Michael