【发布时间】: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;
gpuAddress 是 UInt64。请注意他们将其转换为(float *) 是多么容易,以便可以在float* 的结构字段上设置它。这在 Swift 中似乎并不容易。我想出的最好的就是这个丑陋的位:
#ifdef __METAL_VERSION__
#define BUFFER constant CustomStruct*
#else
#define BUFFER uint64_t
#endif
typedef struct {
BUFFER structs;
} TestArgBuffer;
这允许我使用 Swift 在共享结构定义上使用 gpuAddress 设置该地址。
这是预期的方式吗?我无法想象它是。
【问题讨论】: