Hi,
I'm checking out the new MetalPerformanceShaders which were presented in WWDC 607. 😎
Are there some examples showning how to use them? I couldn't find any and the code documentation isn't (to me) exaustive enough.
I've been playing with MPSImageHistogram on some test images (100x100px with just a few color stripes) and there are a few things that just don't look right.
The docs don't mention exactly how data is written to the output buffer, but from what I could deduce the bin count are stored as UInt32. So far so good.
The initializer of MPSHistogramInfo states that the numberOfHistogramEntries are the number of histogram bins for each channel. However it seems you need to multiply this number by 256 to actually get the desidered bin number. This unecessarily increases the size of the histogram buffer. Has anyone noticed the same?
Allocated size a part this works pretty well from 2 to 128 actual bins. 256 bins don't work, with numbers coming out gibberish from the result buffer.
Could anyone review the following code for errors? Thanks!
// colorPixelFormat = .BGRA8Unorm
let commandBuffer = commandQueue.commandBuffer()
let binsPerChannel = 128 // ok from 2-128, ko 256
let hSize = 256 * 128
let channels = 4
let bytesPerBin = 4
var histogramInfo = MPSHistogramInfo(numberOfHistogramEntries: 256 * binsPerChannel,
histogramForAlpha: ObjCBool(channels == 4),
minPixelValue: vector_float4(0.0, 0.0, 0.0, 0.0),
maxPixelValue: vector_float4(255.0, 255.0, 255.0, 255.0))
let histFilter = MPSImageHistogram(device: view.device!,
histogramInfo: &histogramInfo)
let histogramBuffer = view.device?.newBufferWithLength(hSize * bytesPerBin * channels, options: MTLResourceOptions.OptionCPUCacheModeDefault)
histFilter.encodeToCommandBuffer(commandBuffer, sourceTexture: sourceTexture, histogram: histogramBuffer!, histogramOffset: 0)
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
var binData = UnsafePointer<UInt32>(histogramBuffer!.contents())
for i in 0..<hSize * channels {
if binData[i] != 0 {
print("\(binData[i]) \(i)")
}
}
I'm using Xcode7, iOS9 beta 3 on an iPad Air2.