metal performance shader 实现直方图均衡化,直方图计算部分没问题,到了执行 encodeTransformToCommandBuffer 计算累加直方图的时候报错:
-[MTLDebugComputeCommandEncoder setBuffer:offset:atIndex:]:283: failed assertion
offset(4096) must be < buffer length.'`
代码:
// histogram
MPSImageHistogramInfo info;
info.histogramForAlpha = true;
info.numberOfHistogramEntries = 256;
info.minPixelValue = simd_make_float4(0, 0, 0, 0);
info.maxPixelValue = simd_make_float4(1, 1, 1, 1);
MPSImageHistogram *histogram = [[MPSImageHistogram alloc] initWithDevice:_device histogramInfo:&info];
size_t length = [histogram histogramSizeForSourceFormat:sourceTexture.pixelFormat];
id<MTLBuffer> histogramInfoBuffer = [_device newBufferWithLength:length options:MTLResourceStorageModePrivate];
[histogram encodeToCommandBuffer:commandBuffer sourceTexture:sourceTexture histogram:histogramInfoBuffer histogramOffset:0];
MPSImageHistogramEqualization *equalization = [[MPSImageHistogramEqualization alloc] initWithDevice:_device histogramInfo:&info];
// accumulative distribution function of the histogram
[equalization encodeTransformToCommandBuffer:commandBuffer sourceTexture:sourceTexture histogram:histogramInfoBuffer histogramOffset:0];
// apply equalisation
[equalization encodeToCommandBuffer:commandBuffer sourceImage:sourceTexture destinationImage:destTexture];