解释Metal和SIMD中的不同类型 [英] Explaining the different types in Metal and SIMD

查看:185
本文介绍了解释Metal和SIMD中的不同类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Metal一起使用时,我发现类型令人困惑,而且我并不总是清楚在哪种情况下应该使用哪种类型.

When working with Metal, I find there's a bewildering number of types and it's not always clear to me which type I should be using in which context.

在Apple的Metal Shading语言规范中,Metal着色器文件中有一个非常明确的表,说明了支持哪些类型.但是,有许多示例代码似乎使用了SIMD一部分中的其他类型.在macOS(Objective-C)方面,Metal类型不可用,但SIMD类型可用,我不确定应该使用哪种类型.

In Apple's Metal Shading Language Specification, there's a pretty clear table of which types are supported within a Metal shader file. However, there's plenty of sample code available that seems to use additional types that are part of SIMD. On the macOS (Objective-C) side of things, the Metal types are not available but the SIMD ones are and I'm not sure which ones I'm supposed to be used.

例如:

在《金属规格》中,有float2被描述为代表两个浮动成分的向量"数据类型.

In the Metal Spec, there's float2 that is described as a "vector" data type representing two floating components.

在应用程序方面,以下所有功能似乎都得到了使用或表示:

On the app side, the following all seem to be used or represented in some capacity:

  • float2 ,在vector_types.h

  • float2, which is typedef ::simd_float2 float2 in vector_types.h

注意:在C或Objective-C中,此类型可以作为simd_float2使用."

  • vector_float2 ,即typedef simd_float2 vector_float2

注意:不建议使用此类型;应改用simd_float2或simd :: float2"

  • simd_float2 ,即typedef __attribute__((__ext_vector_type__(2))) float simd_float2
  • ::simd_float2 simd::float2 吗?

矩阵类型也存在类似情况:

A similar situation exists for matrix types:

  • matrix_float4x4 simd_float4x4 ::simd_float4x4 float4x4

请问有人可以解释为什么这么多的typedef具有看似重叠的功能吗?如果您今天(2018年)使用Objective-C/Objective-C ++编写新应用程序,则应使用哪种类型来表示两个浮点值(x/y),以及可以在应用程序代码和Metal之间共享的矩阵转换的类型?

Could someone please shed some light on why there are so many typedefs with seemingly overlapping functionality? If you were writing a new application today (2018) in Objective-C / Objective-C++, which type should you use to represent two floating values (x/y) and which type for matrix transforms that can be shared between app code and Metal?

推荐答案

不推荐使用带有vector_matrix_前缀的类型,而推荐使用带有simd_前缀的类型,因此一般指导(使用例如)

The types with vector_ and matrix_ prefixes have been deprecated in favor of those with the simd_ prefix, so the general guidance (using float4 as an example) would be:

  • 在C代码中,使用simd_float4类型. (除非您提供自己的typedef,否则您必须包括前缀,因为C没有命名空间.)
  • 与Objective-C相同.
  • 在C ++代码中,使用simd::float4类型,您可以通过using namespace simd;将其缩短为float4.
  • 与Objective-C ++相同.
  • 在金属代码中,请使用float4类型,因为float4是金属着色语言[1]中的基本类型.
  • 在Swift代码中,使用float4类型,因为simd_类型被类型化为短名称.
  • 更新:在Swift 5中,已弃用float4和相关类型,而推荐使用SIMD4<Float>和相关类型.
  • In C code, use the simd_float4 type. (You have to include the prefix unless you provide your own typedef, since C doesn't have namespaces.)
  • Same for Objective-C.
  • In C++ code, use the simd::float4 type, which you can shorten to float4 by using namespace simd;.
  • Same for Objective-C++.
  • In Metal code, use the float4 type, since float4 is a fundamental type in the Metal Shading Language [1].
  • In Swift code, use the float4 type, since the simd_ types are typealiased to shorter names.
  • Update: In Swift 5, float4 and related types have been deprecated in favor of SIMD4<Float> and related types.

这些类型在根本上都是等效的,并且都具有相同的大小和对齐特征,因此您可以跨语言使用它们.实际上,这就是simd框架的设计目标之一.

These types are all fundamentally equivalent, and all have the same size and alignment characteristics so you can use them across languages. That is, in fact, one of the design goals of the simd framework.

由于您没有询问,我将把打包类型的讨论推迟到另一天.

I'll leave a discussion of packed types to another day, since you didn't ask.

[1] Metal是一种不寻常的情况,因为它在全局名称空间中定义了float4,然后将其导入到metal名称空间中,该名称空间也作为simd名称空间导出.它还将float4别名为vector_float4.因此,可以将以上任何名称用于此向量类型(simd_float4除外).更喜欢float4.

[1] Metal is an unusual case since it defines float4 in the global namespace, then imports it into the metal namespace, which is also exported as the simd namespace. It additionally aliases float4 as vector_float4. So, you can use any of the above names for this vector type (except simd_float4). Prefer float4.

这篇关于解释Metal和SIMD中的不同类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆