MPSImageHistogramEqualization引发断言偏移必须小于<. [缓冲区长度] [英] MPSImageHistogramEqualization throws assertion that offset must be < [buffer length]

查看:160
本文介绍了MPSImageHistogramEqualization引发断言偏移必须小于<. [缓冲区长度]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iOS上使用MPSImageHistogramEqualization进行直方图均衡,但是最终抛出了一个我不理解的断言.这是我的代码:

I'm trying to do histogram equalization using MPSImageHistogramEqualization on iOS but it ends up throwin an assertion I do not understand. Here is my code:

    // Calculate Histogram
    var histogramInfo = MPSImageHistogramInfo(
        numberOfHistogramEntries: 256,
        histogramForAlpha: false,
        minPixelValue: vector_float4(0,0,0,0),
        maxPixelValue: vector_float4(1,1,1,1))
    let calculation = MPSImageHistogram(device: self.mtlDevice, histogramInfo: &histogramInfo)
    let bufferLength = calculation.histogramSize(forSourceFormat: sourceTexture.pixelFormat)
    let histogramInfoBuffer = self.mtlDevice.makeBuffer(length: bufferLength, options: [.storageModePrivate])!
    calculation.encode(to: commandBuffer,
                       sourceTexture: sourceTexture,
                       histogram: histogramInfoBuffer,
                       histogramOffset: 0)
    let histogramEqualization = MPSImageHistogramEqualization(device: self.mtlDevice, histogramInfo: &histogramInfo)
    histogramEqualization.encodeTransform(to: commandBuffer, sourceTexture: sourceTexture, histogram: histogramInfoBuffer, histogramOffset: 0)

这是在最后一行发生的结果断言:

And here is the resulting assert that happens on that last line:

-[MTLDebugComputeCommandEncoder setBuffer:offset:atIndex:]:283: failed assertion `offset(4096) must be < [buffer length](4096).'

关于这里可能会发生什么的任何建议?

Any suggestions on what might be going on here?

推荐答案

这似乎是MPSImageHistogramEqualization中专用路径中的错误,我鼓励您使用

This appears to be a bug in a specialized path in MPSImageHistogramEqualization, and I encourage you to file feedback on it.

numberOfHistogramEntries大于256时,图像内核会分配一个内部缓冲区,该缓冲区足够大以容纳需要处理的数据(对于N = 512,这是8192字节),外加额外的空间(32个字节).设置内部optimized256BinsUseCase标志时,它将恰好分配4096个字节,而忽略了额外存储的最后一位.我的怀疑是,后续操作依赖于初始数据块之后有更多空间,并且无意中将缓冲区偏移设置为超过内部缓冲区的长度.

When numberOfHistogramEntries is greater than 256, the image kernel allocates an internal buffer large enough to hold the data it needs to work with (for N=512, this is 8192 bytes), plus an extra bit of space (32 bytes). When the internal optimized256BinsUseCase flag is set, it allocates exactly 4096 bytes, omitting that last bit of extra storage. My suspicion is that subsequent operations rely on having more space after the initial data chunk, and inadvertently set the buffer offset past the length of the internal buffer.

您也许可以通过使用不同数量的直方图箱(例如512)来解决此问题.这浪费了一些空间和时间,但是我认为它将产生相同的结果.

You may be able to work around this by using a different number of histogram bins, like 512. This wastes a little space and time, but I assume it will produce the same results.

或者,您可以通过禁用Metal验证层来避免崩溃,但是我强烈不建议这样做,因为您只是掩盖了潜在的问题,直到解决为止.

Alternatively, you might be able to avoid this crash by disabling the Metal validation layer, but I strongly discourage that, since you'll just be masking the underlying issue till it gets fixed.

注意:我在macOS Catalina上对MetalPerformanceShaders框架进行了反向工程.不同的平台和不同的软件版本可能具有不同的代码路径.

Note: I did my reverse-engineering of the MetalPerformanceShaders framework on macOS Catalina. Different platforms and different software versions likely have different code paths.

这篇关于MPSImageHistogramEqualization引发断言偏移必须小于&lt;. [缓冲区长度]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆