无法转换类型的值' int'对于预期参数类型' unsaferawpointer
我要做的
是初始化的金属缓冲区,其中包含int
对
问题进行
import MetalKit
...
func doStuff(gra: [Int], dist: Int) {
let graphBuff = device?.makeBuffer(bytes: gra,
length: MemoryLayout<Int>.size * count,
options: .storageModeShared)
let distanceBuff = device?.makeBuffer(bytes: dist, // Cannot convert value of type 'Int' to expected argument type 'UnsafeRawPointer'
length: MemoryLayout<Int>.size,
options: .storageModeShared)
}
了某种原因,因为它不允许使用类型int
inter代码> [int] 很好。 我有点茫然,这是怎么回事。
What I'm trying to do
Initialise a buffer for metal that contains an Int
value
The Problem
import MetalKit
...
func doStuff(gra: [Int], dist: Int) {
let graphBuff = device?.makeBuffer(bytes: gra,
length: MemoryLayout<Int>.size * count,
options: .storageModeShared)
let distanceBuff = device?.makeBuffer(bytes: dist, // Cannot convert value of type 'Int' to expected argument type 'UnsafeRawPointer'
length: MemoryLayout<Int>.size,
options: .storageModeShared)
}
for some reason it does not allow to init a buffer with type Int
, but [Int]
is fine.
I'm kind of at a loss here what's going on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是Swift具有一些编译器魔术,可以将阵列传递给不安全的指针。这种魔术不仅仅是常规类型。
您可以做类似的事情与魔术编译器类似的事情是使用标准库中的这种独立函数
func withunsafebytes(of:&amp; t,body :( unsaferawbufferpointer) - ) - &gt;结果
例如:
但是,您可以
使用
cottents()
pointer:The reason for this is Swift has some compiler magic for passing arrays to unsafe pointers. This magic doesn't extend to just regular types.
You can kinda do similar thing to the magic compiler is doing yourself using this freestanding function from standard library
func withUnsafeBytes(of: &T, body: (UnsafeRawBufferPointer) throws -> Result(UnsafeRawBufferPointer) throws -> Result) -> Result
For example:
But instead you can just make a regular buffer with
and then put anything you want in it using
contents()
pointer: