Chapter 15 Create Heap with Cube Map

I’m trying to move the sky box texture in the MTL heap too.

To achieved it I had to a change the code:

zip(textures, heapTextures).forEach { (texture, heapTexture) in
            var region = MTLRegionMake2D(0, 0, texture.width,
                                         texture.height)
            
            for level in 0..<texture.mipmapLevelCount {
                for slice in 0..<texture.arrayLength {
                    blitEncoder.copy(from: texture,
                                     sourceSlice: slice,
                                     sourceLevel: level,
                                     sourceOrigin: region.origin,
                                     sourceSize: region.size,
                                     to: heapTexture,
                                     destinationSlice: slice,
                                     destinationLevel: level,
                                     destinationOrigin: region.origin)
                }
                
                region.size.width /= 2
                region.size.height /= 2
                if(region.size.width == 0) {region.size.width = 1}
                if(region.size.height == 0) {region.size.height = 1}
            }
        }
}

To

  zip(textures, heapTextures).forEach { (texture, heapTexture) in
            var region = MTLRegionMake2D(0, 0, texture.width,
                                         texture.height)
            let sliceCount = (texture.textureType == .typeCube) ? 6 : texture.arrayLength
            
            for level in 0..<texture.mipmapLevelCount {
                for slice in 0..<sliceCount {
                    blitEncoder.copy(from: texture,
                                     sourceSlice: slice,
                                     sourceLevel: level,
                                     sourceOrigin: region.origin,
                                     sourceSize: region.size,
                                     to: heapTexture,
                                     destinationSlice: slice,
                                     destinationLevel: level,
                                     destinationOrigin: region.origin)
                }
                
                region.size.width /= 2
                region.size.height /= 2
                if(region.size.width == 0) {region.size.width = 1}
                if(region.size.height == 0) {region.size.height = 1}
            }
        }

do you know if there is other textureType needing this type of adaptation.

Hi @hougoul and welcome to the forums :smiley:

That’s a good change to make. The documentation for MTLTextureType provides a bewildering array of types to cater for:

https://developer.apple.com/documentation/metal/mtltexturetype

For example, typeCubeArray holds an array of cube images, which would need catering for if you use that type.