无法转换类型的值' int'对于预期参数类型' unsaferawpointer

发布于 2025-02-13 08:09:04 字数 757 浏览 0 评论 0原文

我要做的

是初始化的金属缓冲区,其中包含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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2025-02-20 08:09:04

原因是Swift具有一些编译器魔术,可以将阵列传递给不安全的指针。这种魔术不仅仅是常规类型。

您可以做类似的事情与魔术编译器类似的事情是使用标准库中的这种独立函数func withunsafebytes(of:&amp; t,body :( unsaferawbufferpointer) - ) - &gt;结果

例如:

  3> var someInt: Int = 3
someInt: Int = 3
  4> withUnsafeBytes(of: &someInt) { print($0) }
UnsafeRawBufferPointer(start: 0x00000001000ec4d0, count: 8)

但是,您可以

let buffer = device?.makeBuffer(length: MemoryLayout<Int>.size,
                   options: .storageModeShared)!

使用cottents() pointer:

buffer.contents().storeBytes(of: dist, as: Int.self)

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:

  3> var someInt: Int = 3
someInt: Int = 3
  4> withUnsafeBytes(of: &someInt) { print($0) }
UnsafeRawBufferPointer(start: 0x00000001000ec4d0, count: 8)

But instead you can just make a regular buffer with

let buffer = device?.makeBuffer(length: MemoryLayout<Int>.size,
                   options: .storageModeShared)!

and then put anything you want in it using contents() pointer:

buffer.contents().storeBytes(of: dist, as: Int.self)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文