我使用一个代码来生成金属着色器的声明,有时我不知道传递给着色器的对象的确切类型。
。我会让下面的声明自动生成:
vertex VertexOut vertexShader(constant Element *array [[buffer(0)]])
当我试图获得数组元素的类型时,我从编译器得到错误:
using T = metal::remove_reference_t<decltype( *array )>;
T test;// <-- ERROR! "Automatic variable qualified with an address space"
是否可以"擦除"?地址空间从类型?
在Metal中获得数组元素类型的最佳方法是什么(如果可能的话)?
正如我在评论中所说的,我认为问题在于remove_reference
确实做到了它所说的:它删除了引用,同时仍然保留了类型限定。您不能在device
或constant
空间中声明变量,因此还需要删除地址空间限定符,类似于remove_cv_t
的工作方式。我已经编写了几个模板来向您展示我的意思:
template <typename T>
struct remove_address_space
{
typedef T type;
};
template <typename T>
struct remove_address_space<device T>
{
typedef T type;
};
template <typename T>
struct remove_address_space<constant T>
{
typedef T type;
};
然后写成
using T = remove_address_space<metal::remove_reference_t<decltype( *array )>>::type;
请记住,金属有很多地址空间,但为了编写函数的入口点,我认为只有device
和constant
是相关的。