我正在尝试将缓冲区转换为PVRTC纹理。我之所以这样做,是因为要保持私有存储的质感。
这里是文档的引用。
如果纹理的像素格式是压缩格式,则sourceSize 必须是像素格式的块大小的倍数,或者被限制为 如果块延伸到a的边界之外,则纹理的边缘 质地。对于压缩格式,sourceBytesPerRow是 从一排块的开始到下一行的开始的字节 块。
我的代码出了点问题,因为纹理在此之后看起来坏了。
MTLBlitOption options = MTLBlitOptionNone;
if (_pixelFormat == MTLPixelFormatPVRTC_RGB_4BPP || _pixelFormat == MTLPixelFormatPVRTC_RGBA_4BPP) {
uint32_t blockWidth = 4;
uint32_t blockHeight = 4;
uint32_t bitsPerPixel = 4;
uint32_t blockSize = blockWidth * blockHeight;
uint32_t widthInBlocks = width / blockWidth;
uint32_t heightInBlocks = height / blockHeight;
options = MTLBlitOptionRowLinearPVRTC;
levelBytesPerRow = widthInBlocks * ((blockSize * bitsPerPixel) / 8);
}
id <MTLBuffer> buffer = [device newBufferWithBytes:[data bytes] length:[data length] options:0];
[blitEncoder copyFromBuffer:buffer
sourceOffset:0
sourceBytesPerRow:levelBytesPerRow
sourceBytesPerImage:[buffer length]
sourceSize:MTLSizeMake(width, height, 1)
toTexture:self.textureMetal
destinationSlice:0
destinationLevel:i
destinationOrigin:MTLOriginMake(0, 0, 0)
options:options];