我试图通过MTLTexture
这种方式获得像素:
let pixelCount = compareTexture.width * compareTexture.height let region = MTLRegionMake2D(0, 0, compareTexture.width, compareTexture.height) var textureComponentsArray = Array(repeating: float4(0), count: pixelCount) textureComponentsArray.withUnsafeMutableBytes { compareTexture.getBytes($0.baseAddress!, bytesPerRow: (MemoryLayout .size * compareTexture.width), from: region, mipmapLevel: 0) } print(textureComponentsArray.first!)
不幸的是,大多数元素要么是NaN,要么等于我初始化textureComponentsArray的值.例如,此代码打印:
float4(nan, nan, nan, nan)
我在macOS上,我的MTLTexture有这些属性:
let textureDescriptor = MTLTextureDescriptor() textureDescriptor.width = imageTexture.width textureDescriptor.height = imageTexture.height textureDescriptor.pixelFormat = imageTexture.pixelFormat textureDescriptor.resourceOptions = .storageModeManaged textureDescriptor.storageMode = .managed textureDescriptor.usage = [.renderTarget, .shaderRead, .shaderWrite]
我确实使用托管存储模式,因此数据应该可供CPU使用,我不明白为什么它不起作用.
谢谢.
编辑:
我正在尝试使用:
blitCommandEncoder.synchronize(resource: compareTexture)
我确实等待命令缓冲区完成,但仍然存在问题.