【问题标题】:How to encode the Metal 3 argument buffer gpuAddress using only Swift如何仅使用 Swift 对 Metal 3 参数缓冲区 gpuAddress 进行编码
【发布时间】:2022-11-11 22:01:25
【问题描述】:

所有Apple's documentation 和参数缓冲区的示例项目都在Objective-C 中,所以也许他们不希望任何人使用Swift 和新的Metal 3 参数缓冲区语法。

问题在于 Apple 示例中使用的方便的新 gpuAddress 属性:

FragmentShaderArguments *argumentStructure = (FragmentShaderArguments *)_fragmentShaderArgumentBuffer.contents;


argumentStructure->exampleTexture = _texture.gpuResourceID;
argumentStructure->exampleBuffer = (float*) _indirectBuffer.gpuAddress;
argumentStructure->exampleSampler = _sampler.gpuResourceID;
argumentStructure->exampleConstant = bufferElements;

gpuAddressUInt64。请注意他们将其转换为(float *) 是多么容易,以便可以在float* 的结构字段上设置它。这在 Swift 中似乎并不容易。我想出的最好的就是这个丑陋的位:

#ifdef __METAL_VERSION__
#define BUFFER constant CustomStruct*
#else
#define BUFFER uint64_t
#endif

typedef struct {
    BUFFER structs;
} TestArgBuffer;

这允许我使用 Swift 在共享结构定义上使用 gpuAddress 设置该地址。

这是预期的方式吗?我无法想象它是。

【问题讨论】:

    标签: swift metal


    【解决方案1】:

    我认为您可以使用以下实现:

    struct FragmentShaderArguments {
        var exampleTexture: MTLResourceID
        var exampleSampler: MTLResourceID
        var exampleBuffer: UnsafeRawPointer
        var exampleConstant: UInt32
    }
    
    let argumentStructure = fragmentShaderArgumentBuffer.contents().bindMemory(to: FragmentShaderArguments.self, capacity: 1)
    argumentStructure.pointee.exampleTexture = texture.gpuResourceID
    argumentStructure.pointee.exampleBuffer = UnsafeRawPointer(bitPattern: UInt(indirectBuffer.gpuAddress))!
    argumentStructure.pointee.exampleSampler = sampler.gpuResourceID
    argumentStructure.pointee.exampleConstant = UInt32(bufferElements)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-20
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 2023-03-20
      • 2017-01-22
      • 2017-09-27
      相关资源
      最近更新 更多