Search code examples
renderingtexturesmetal

how larger image replace small image in metal


It's a very stupid question. I want to play a video. According to Apple's official guide, I should reuse texture resources, so I choose replaceRegion, but the size of the pictures I choose is not consistent. Is there any way to put a bigger breakthrough into the previous small texture? , or I suppose to use the largest as the standard when creating textures from the beginning?

I am a beginner. I have learned a little bit of vulkan before, and it's my first time to use swift and metal. Is there any good learning path that you can recommend to me? Thanks!

        if(changeRequest){
            changeRequest = false
            let region = MTLRegionMake2D(0, 0, Int(stagingBuffer.size.width), Int(stagingBuffer.size.height))
            textures[currentFrame].replace(region: region, mipmapLevel: 0, withBytes: &stagingBuffer, bytesPerRow: Int(stagingBuffer.size.width * stagingBuffer.size.height) * 4)
            }

Solution

  • If you want to reuse the texture, you will have to create it for the largest you texture plan on using. There is no way to enlarge the extent of a MTLTexture once it has been allocated.

    Therefore it may make more sense to create textures the size needed, or a family of textures (eg 512x512, 768x768, 1024x1024).

    Note that you can scale a MTLTexture using MPSImageLanczosScale, but it creates a new texture, so I don't think that is what you want to do.