如何从数组文字正确创建 MTLTexture?
How do I properly create an MTLTexture from an array literal?
这里的想法是创建两个黑色和两个灰色像素的纹理
let TextureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: View.colorPixelFormat, width: 2, height: 2, mipmapped: Settings.Mipmaps > 0)
Texture = Device.makeBuffer(bytes: [UInt8](arrayLiteral: 127, 127, 127, 255, 0, 0, 0, 255, 127, 127, 127, 255, 0, 0, 0, 255), length: 16)?.makeTexture(descriptor: TextureDescriptor, offset: 0, bytesPerRow: 8)
但是,首先,它抱怨 bytesPerRow 未对齐到 256,如果我更改它和缓冲区,它会出现其他问题,例如 resourceOptions (0x10) must match backing buffer resource options (0x0)
,那么我如何初始化 MTL 纹理来自代表红色、绿色、蓝色、alpha 的数字数组,并给定纹理宽度和高度?
我不得不使用 MTLTexture.replace()
,但说真的,你不想像这样存储纹理!
Texture = Device.makeTexture(descriptor: TextureDescriptor)
Texture?.replace(region: MTLRegionMake2D(0, 0, 16, 16), mipmapLevel: 0, slice: 0, withBytes: [UInt8]([127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 127, 127, 127, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 251, 206, 119, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 254, 231, 199, 255, 251, 206, 119, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 104, 104, 104, 255, 127, 127, 127, 255, 254, 231, 199, 255, 245, 236, 93, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 116, 116, 116, 255, 127, 127, 127, 255, 104, 104, 104, 255, 255, 255, 255, 255, 254, 231, 199, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 251, 206, 119, 255, 251, 206, 119, 255, 245, 236, 93, 255, 127, 127, 127, 255, 127, 127, 127, 255, 251, 206, 119, 255, 245, 236, 93, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 254, 231, 199, 255, 251, 206, 119, 255, 104, 104, 104, 255, 143, 143, 143, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 254, 231, 199, 255, 245, 236, 93, 255, 245, 236, 93, 255, 245, 236, 93, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 116, 116, 116, 255, 251, 206, 119, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 104, 104, 104, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 254, 231, 199, 255, 245, 236, 93, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 254, 231, 199, 255, 255, 255, 255, 255, 254, 231, 199, 255, 251, 206, 119, 255, 251, 206, 119, 255, 245, 236, 93, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 116, 116, 116, 255, 254, 231, 199, 255, 245, 236, 93, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 245, 236, 93, 255, 245, 236, 93, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255]), bytesPerRow: 16*4, bytesPerImage: 1024)
这里的想法是创建两个黑色和两个灰色像素的纹理
let TextureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: View.colorPixelFormat, width: 2, height: 2, mipmapped: Settings.Mipmaps > 0)
Texture = Device.makeBuffer(bytes: [UInt8](arrayLiteral: 127, 127, 127, 255, 0, 0, 0, 255, 127, 127, 127, 255, 0, 0, 0, 255), length: 16)?.makeTexture(descriptor: TextureDescriptor, offset: 0, bytesPerRow: 8)
但是,首先,它抱怨 bytesPerRow 未对齐到 256,如果我更改它和缓冲区,它会出现其他问题,例如 resourceOptions (0x10) must match backing buffer resource options (0x0)
,那么我如何初始化 MTL 纹理来自代表红色、绿色、蓝色、alpha 的数字数组,并给定纹理宽度和高度?
我不得不使用 MTLTexture.replace()
,但说真的,你不想像这样存储纹理!
Texture = Device.makeTexture(descriptor: TextureDescriptor)
Texture?.replace(region: MTLRegionMake2D(0, 0, 16, 16), mipmapLevel: 0, slice: 0, withBytes: [UInt8]([127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 127, 127, 127, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 251, 206, 119, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 254, 231, 199, 255, 251, 206, 119, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 104, 104, 104, 255, 127, 127, 127, 255, 254, 231, 199, 255, 245, 236, 93, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 116, 116, 116, 255, 127, 127, 127, 255, 104, 104, 104, 255, 255, 255, 255, 255, 254, 231, 199, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 251, 206, 119, 255, 251, 206, 119, 255, 245, 236, 93, 255, 127, 127, 127, 255, 127, 127, 127, 255, 251, 206, 119, 255, 245, 236, 93, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 254, 231, 199, 255, 251, 206, 119, 255, 104, 104, 104, 255, 143, 143, 143, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 254, 231, 199, 255, 245, 236, 93, 255, 245, 236, 93, 255, 245, 236, 93, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 116, 116, 116, 255, 251, 206, 119, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 104, 104, 104, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 254, 231, 199, 255, 245, 236, 93, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 254, 231, 199, 255, 255, 255, 255, 255, 254, 231, 199, 255, 251, 206, 119, 255, 251, 206, 119, 255, 245, 236, 93, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 116, 116, 116, 255, 254, 231, 199, 255, 245, 236, 93, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 245, 236, 93, 255, 245, 236, 93, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255]), bytesPerRow: 16*4, bytesPerImage: 1024)