iosswiftmetalmetal-performance-shaders

Slicing a MTLTexture of type - 2DArray in Swift environment


Consider a MTLTexture of type 2DArray having some number of slices. To calculate histogram of a specific slice of this texture, how to pass only the reference of a single slice of texture to MPSImageHistogram shader?

In general, How to slice a texture in swift environment? Texture.makeTextureview() is not the preferred way because it creates a new texture consuming more memory and time.


Solution

  • makeTextureView doesn't create a new texture, even though it returns MTLTexture. It creates a texture view. Let me explain.

    You can think of a texture as a header and one or more blobs of data. Header tells API and GPU how to interpret the blobs of data. Header is pretty small, blobs can be large.

    Now, think of a texture view like a new header, but it references blobs of another texture and doesn't have its own blobs, meaning it doesn't copy blobs of a texture you are "viewing" into. This is a cheap way to reinterpret data of an existing texture. For example, you would use texture view when you only need one slice of a texture array or just one face of a cubemap, or if you want to view sRGB texture as linear or vice versa.