首页 > 解决方案 > 你可以使用纹理视图只为 MTLTexture 生成 mipmap 的子集吗?

问题描述

我正在尝试使用纹理视图仅生成纹理的 mipmap 子集,但我的所有尝试都导致 Metal 引发以下错误:

由于执行期间出现错误,命令缓冲区的执行被中止。忽略(用于导致先前/过多的 GPU 错误)(IOAF 代码 4)

伪代码:

// This is a texture that is 1024 x 1024 with 11 mipmap levels.
id<MTLTexture> destinationTexture = ...

// Extract all mipmap levels from 256 and smaller
NSRange levelsRange = NSMakeRange(2, 9);

// Create a new texture view for that includes only those levels from the destination texture.
id<MTLTexture> destinationView = [destinationTexture newTextureViewWithPixelFormat:destinationTexture.pixelFormat
                                                                       textureType:destinationTexture.textureType
                                                                            levels:levelsRange
                                                                            slices:NSMakeRange(0, 1)];

// Copy a region from a source texture into the destination view, 
// starting at level 0 of the view which, I think, should represent
// level 2 of its parent texture.
[blitEncoder copyFromTexture:sourceTexture
                 sourceSlice:0
                 sourceLevel:0
                sourceOrigin:sourceOrigin
                  sourceSize:sourceSize
                   toTexture:destinationView
            destinationSlice:0
            destinationLevel:0
           destinationOrigin:destinationOrigin];

// Generate mipmaps on the destination view. This causes the crash. 
[blitEncoder generateMipmapsForTexture:destinationView];

我的目的是快速渲染一些较低分辨率的内容并将其复制到纹理,然后逐步渲染更高分辨率的内容。我可以显式渲染每个 mipmap 级别的内容,但我希望只选择几个“截止”级别,然后让 Metal 为截止以下的所有级别生成 mipmap。

就目前而言,blit 正确执行并更新了纹理,但如果我尝试生成 mipmap,它会崩溃。如果我不生成 mipmap,那么我需要使用 显式地从该级别进行采样lod(),而我希望使用该bias()功能来限制范围。

堆栈跟踪是:

堆栈跟踪

标签: objective-cmacosmetalmetalkit

解决方案


推荐阅读