gpt4 book ai didi

swift - 如何正确创建自定义空纹理?

转载 作者:行者123 更新时间:2023-12-04 07:15:59 25 4
gpt4 key购买 nike

我需要创建一个 MTLTexture使用我的自定义数据(当前填充为 0),为了做到这一点,我使用了这样的实现

  private func createTexture(frame: UnsafeMutableRawPointer) -> MTLTexture? {
let width = 2048
let height = 2048

let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(
pixelFormat: MTLPixelFormat.rgba8Unorm,
width: width,
height: height,
mipmapped: false)

textureDescriptor.usage = [.shaderWrite, .shaderRead]

guard let texture: MTLTexture = device?.makeTexture(descriptor: textureDescriptor) else
{
logger?.log(severity: .error, msg: "create texture FAILED.")
return nil
}

let region = MTLRegion.init(origin: MTLOrigin.init(x: 0, y: 0, z: 0), size: MTLSize.init(width: texture.width, height: texture.height, depth: 4));

//MARK: >>> JUST FOR TEST
let count = width * height * 4
let stride = MemoryLayout<CChar>.stride
let alignment = MemoryLayout<CChar>.alignment
let byteCount = stride * count

let p = UnsafeMutableRawPointer.allocate(byteCount: byteCount, alignment: alignment)
let data = p.initializeMemory(as: CChar.self, repeating: 0, count: count)
//MARK: <<<

texture.replace(region: region, mipmapLevel: 0, withBytes: data, bytesPerRow: width * 4)

return texture
}
所以,在这里我创建了一个有 4 个 channel 的描述符,然后我创建了一个区域 depth: 4 ,然后创建 UnsafeMutableRawPointer填充数据 stride * count大小,但我在这一行出现错误
texture.replace(region: region, mipmapLevel: 0, withBytes: data, bytesPerRow: width * 4)

_validateReplaceRegion:155: failed assertion `(origin.z + size.depth)(4) must be <= depth(1).'


我究竟做错了什么?

最佳答案

以下行中的 depth 属性不正确:

let region = MTLRegion.init(origin: MTLOrigin.init(x: 0, y: 0, z: 0), size: MTLSize.init(width: texture.width, height: texture.height, depth: 4));
depth 属性描述了 z 维度中的元素数量。对于 2D 纹理,它应该是 1。

关于swift - 如何正确创建自定义空纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68758885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com