logo资料库

Metal_Shading_Language_Guide翻译.pdf

第1页 / 共79页
第2页 / 共79页
第3页 / 共79页
第4页 / 共79页
第5页 / 共79页
第6页 / 共79页
第7页 / 共79页
第8页 / 共79页
资料共79页,剩余部分请下载后查看
Metal Shading Language Guide Metal着色语言指南 Introduction The Metal shading language is a unified programming language for writing both graphics and compute kernel functions that are used by apps written with the Metal framework. The Metal shading language is designed to work together with the Metal framework, which manages the execution, and optionally the compilation, of the Metal shading language code. The Metal shading language uses clang and LLVM so developers get a compiler that delivers close to the metal performance for code executing on the GPU. Metal着色语言是一个用来编写3d图形渲染逻辑 和 并行计算核心逻辑的编程语言,编写Metal框架的app需要使用 Metal着色语言程序。 Metal着色语言和Metal框架配合使用,Metal框架管理Metal着色语言的运行和可选编译选项。Metal着色语言使用clang 和 LLVM,编译器对于在GPU上的代码执行效率有更好的控制。 At a Glance This document describes the Metal unified graphics and compute shading language. The Metal shading language is a C++ based programming language that developers can use to write code that is executed on the GPU for graphics and general-purpose data-parallel computations. Since the Metal shading language is based on C++, developers will find it familiar and easy to use. With the Metal shading language, both graphics and compute programs can be written with a single, unified language, which allows tighter integration between the two. 本文描述了Metal着色语言的使用(图形渲染和并行计算)。Metal着色语言基于C++设计,开发者可以用它来编写在 GPU上执行的图形渲染逻辑代码和通用并行计算逻辑代码。因为是基于C++设计,开发者会发现它熟悉便于使用。使 用Metal着色语言,图形渲染和并行计算可以使用同一种语言,这就允许把两种任务结合在一起完成。 How to Use This Document Developers who are writing code with the Metal framework will want to read this document, because they will need to use the Metal shading language to write graphics and compute programs to be executed on the GPU. This document is organized into the following chapters: ! Metal and C++11 (page 8) covers the similarities and differences between the Metal shading language and C++11. ! Metal Data Types (page 10) lists the Metal shading language data types, including types that represent vectors, matrices, buffers, textures, and samplers. It also discusses type alignment and type conversion. ! Operators (page 30) lists the Metal shading language operators. ! Functions, Variables, and Qualifiers (page 36) details how functions and variables are declared, sometimes with qualifiers that restrict how they are used. Metal Standard Library (page 62) defines a collection of built-in Metal shading language functions. ! ! Compiler Options (page 93) details the options for the Metal shading language compiler, including pre-processor directives, options for math intrinsics, and options that control optimization. ! Numerical Compliance (page 95) describes requirements for representing floating-point numbers, including accuracy in mathematical operations. 使用Metal框架的开发者应该阅读本文,因为他们需要使用Metal着色语言编写运行在GPU上的图形渲染和并行计算逻 辑。本文包含如下的章节: ! Metal and C++11, 描述Metal着色语言和它的基础C++11的异同。
! Metal数据类型, 列举Metal着色语言的数据类型,包括表示向量,矩阵,缓存,纹理,采样器等等,还描 述了类型对齐和类型转换。 列举Metal着色语言操作运算符。 ! 运算符, ! 方法、变量、修饰符, 详细描述了方法变量如何声明,已经如何使用修饰符修饰,表示使用时的限制。 ! Metal标准库, ! 编译选项 优化控制选项。 描述了一些Metal着色语言内建的方法如何使用。 描述了Metal着色语言编译器的可选项,包括预编译指令,数学内联计算函数选项和其他 描述了表示浮点数的要求,包括数学运算的精度。 ! 数值规则, See Also C++11 Stroustrup, Bjarne. The C++ Programming Language. Harlow: Addison-Wesley, 2013. Metal Framework  The 《Metal Programming Guide》 provides a detailed introduction to writing apps with the Metal framework. 详细介 绍如何使用Metal框架编写app The 《Metal Framework Reference》 details individual classes in the Metal framework. 详细描述每一个Metal框架中 的类。
Metal and C++11 The Metal shading language is based on the C++11 Specification (a.k.a., the ISO/IEC JTC1/SC22/WG21 N3290 Language Specification) with specific extensions and restrictions. Please refer to the C++11 Specification for a detailed description of the language grammar. This section and its subsections describe modifications and restrictions to the C++11 language supported in the Metal shading language. For more information about Metal shading language pre-processing directives and compiler options, see Compiler Options (page 93) of this document. Metal着色语言是基于C++ 11标准设计的,它在C++基础上多了一些扩展和限制。下面的内容就是在描述Metal着色语 言相比C++11的修改和限制。Metal着色语言的预编指令和编译器选项后面的章节有描述。 Overloading 重载 The Metal shading language supports overloading as defined by section 13 of the C++11 Specification . The function overloading rules are extended to include the address space qualifier of an argument. The Metal shading language graphics and kernel functions cannot be overloaded. (For definition of graphics and kernel functions, see Function Qualifiers (page 36).) Metal着色语言支持的重载如同C++11标准的section 13所述,方法重载规则有扩展,可以包括参数的地址空间描述符。 Metal着色语言中的标识为图形渲染入口函数或是并行计算入口函数的不可以被重载。(如何标识函数为图形渲染入 口函数 或 并行计算入口函数,参见后面的章节)。 Templates 模板 The Metal shading language supports templates as defined by section 14 of the C++11 Specification . Metal着色语言支持的模板如同C++11标准的section 14所述 Preprocessing Directives 预编译指令 The Metal shading language supports the pre-processing directives defined by section 16 of the C++11 Specification . Metal着色语言支持的预编译指令如同C++11标准的section 16所述 Restrictions 限制 The following C++11 features are not available in the Metal shading language (section numbers in this list refer to the C++11 Specification ): 如下的C++11特性在Metal着色语言中不支持: ! ! ! dynamic_cast operator (section 5.2.7) ! ! new and delete operators (sections 5.3.4 and 5.3.5) 对象创建(new)和销毁(delete)操作符 ! noexcept operator (section 5.3.7) ! ! ! virtual function qualifier (section 7.1.2) ! derived classes (sections 10 and 11) ! exception handling (section 15) The C++ standard library must not be used in the Metal shading language code. Instead of the C++ standard library, Metal has its own standard library that is described in Metal Standard Library (page 62). C++标准库不可以在Metal着色语言中使用,Metal着色语言使用自己的标准库,后面专门有一个章节描述。 The Metal shading language restricts the use of pointers: Metal着色语言中对于指针的使用的限制 goto statement (section 6.6) register, thread_local storage qualifiers (section 7.1.1) 变量存储修饰符register 和 thread_local lambda表达式 递归函数调用 动态转换操作符 类型识别 lambda expressions (section 5.1.2) recursive function calls (section 5.2.2, item 9) type identification (section 5.2.8) 虚函数修饰符 派生类 异常处理 操作符 noexcept goto跳转
! Arguments to Metal graphics and kernel functions that are pointers declared in a program must be declared with the Metal device, threadgroup, or constant address space qualifier. (See Address Space Qualifiers for Variables and Arguments (page 37) for more about address space qualifiers.) Metal图形和并行计算函数用到的入参如果是指针必须使用地址空间修饰符(device, threadgroup, constant)。 不支持函数指针。 ! Function pointers are not supported. A Metal function cannot be called main. Metal函数名不能叫main。 Metal Pixel Coordinate System Metal像素坐标系统 In Metal, the origin of the pixel coordinate system of a texture or a framebuffer attachment is defined at the top left corner. 在Metal中,纹理 或是 帧缓存attachment 中的像素 使用的坐标系统的原点定义在左上角。
Metal Data Types Metal数据类型 This chapter details the Metal shading language data types, including types that represent vectors and matrices. Atomic data types, buffers, textures, samplers, arrays, and user-defined structs are also discussed. Type alignment and type conversion are also described. 本章详述Metal着色语言的数据类型,包括表示向量和矩阵的类型,原子数据类型,缓存,纹理,采样器,数组,已 经用户自定义结构体。还会描述类型对齐和类型转换。 Scalar Data Types 标量数据类型 Metal supports the scalar types listed in Table 2-1 (page 10). Metal does not support the double, long, unsigned long, long long, unsigned long long, and long double data types. Metal支持如表2-1列举的标量数据类型,Metal不支持这些数据类型:double, long, unsigned long, long long, unsigned long long, long double。 Table 2-1 Metal scalar data types Type bool char unsigned char uchar short unsigned short ushort int unsigned int uint half float size_t Description A conditional data type that has the value of either true or false. The value true expands to the integer constant 1, and the value false expands to the integer constant 0. 布尔数据类型,取值有true或false,true可以扩展为整数常量1,false可以扩展为整数常量0 A signed two’s complement 8-bit integer. 有符号8-bit整数 An unsigned 8-bit integer. 无符号8-bit整数 A signed two’s complement 16-bit integer. 有符号16-bit整数 An unsigned 16-bit integer. 无符号16-bit整数 A signed two’s complement 32-bit integer. 有符号32-bit整数 An unsigned 32-bit integer. 无符号32-bit整数 A 16-bit floating-point. The half data type must conform to the IEEE 754 binary16 storage format. 一个16-bit浮点数,符合IEEE754的16位二进制浮点数存储格式规范 A 32-bit floating-point. The float data type must conform to the IEEE 754 single precision storage format. 一个32-bit浮点数,符合IEEE754的单精度浮点数存储格式规范 An unsigned integer type of the result of the sizeof operator. This is a 64-bit unsigned integer. 64-bit无符号整数,表示sizeof操作符的结果,
ptrdiff_t void A signed integer type that is the result of subtracting two pointers. This is a 64-bit signed integer. 64-bit有符号整数,表示两个指针的差。 The void type comprises an empty set of values; it is an incomplete type that cannot be completed. 该类型表示一个空的值集合, Note: Metal supports the following suffixes that specify the type for a literal: the standard f or F suffix to specify a single precision floating-point literal value (e.g., 0.5f or 0.5F). the h or H suffix to specify a half precision floating-point literal value (e.g., 0.5h or 0.5H). the u or U suffix for unsigned integer literals. 注意:Metal支持使用下面的后缀用来表示字面量类型 f或是F用来表示单精度浮点型字面量(比如 0.5f 或是 0.5F) h或是H用来表示半单精度浮点型字面量(比如0.5h 或是0.5H) u或是U用来表示无符号整形字面量。 Vector and Matrix Data Types 向量和矩阵数据类型 The Metal shading language supports a subset of the vector and matrix data types implemented by the system vector math library. The vector type names supported are: booln! charn, shortn, intn, ucharn, ushortn, uintn halfn and floatn n is 2, 3, or 4 representing a 2-, 3- or 4- component vector type. Metal着色语言通过系统向量数学库支持一系列的向量和矩阵数据类型。 Metal支持的向量类型名如下:booln, charn, shortn, intn, ucharn, ushortn, uintn, halfn, floatn n的取值为2,3或是4,分别表示一个2维,3维或是4维向量类型。 The matrix type names supported are: halfnxm and floatnxm where n and m are number of columns and rows. n and m can be 2, 3, or 4. A matrix is composed of several vectors. For example, a floatnx3 matrix is composed of n float3 vectors. Similarly, a halfnx4 matrix is composed of n half4 vectors. Metal支持的矩阵类型名如下:halfnxm, floatnxm n和m分别表示矩阵列数和行数,n和m可以是2,3或是4.一个矩阵被看做是由几个向量构成,比如一个floatnx3的矩阵由 n个float3类型向量构成。类似的一个halfnx4矩阵由n个half4类型向量构成。 Accessing Vector Components 访问向量的分量 Vector components can be accessed using an array index. Array index 0 refers to the first component of the vector, index 1 to the second component, and so on. The following examples show various ways to access array components: 向量的分量可以使用数组下标进行访问存取。数组下标0引用了向量的第一个分量,数组下标1引用了向量的第二个 分量,以此类推。下面的例子展示了多种访问向量分量:
pos = float4(1.0f, 2.0f, 3.0f, 4.0f); float x = pos[0]; // x = 1.0 float z = pos[2]; // z = 3.0 float4 vA = float4(1.0f, 2.0f, 3.0f, 4.0f); float4 vB; for (int i=0; i<4; i++) vB[i] = vA[i] * 2.0f // vB = (2.0, 4.0, 6.0, 8.0); Metal supports using the period ( . ) as a selection operator to access vector components, using letters that may indicate coordinate or color data: .xyzw or .rgba. In the following code, the vector test is initialized, and then components are accessed using the .xyzw or .rgba selection syntax: Metal支持使用(.)作为选择向量分量进行访问的操作符,可以使用表示坐标分量或是颜色分量的字母来存取向量: 向量名.xyzw 或是 向量名.rgba。 下面的代码,向量test被初始化,然后使用.xyzw 和 .rgba 选择语法来访问它的各个分量: int4 test = int4(0, 1, 2, 3); int a = test.x; // a=0 int b = test.y; // b=1 int c = test.z; // c=2 int d = test.w; // d=3 int e = test.r; // e=0 int f = test.g; // f=1 int g = test.b; // g=2 int h = test.a; // h=3 The component selection syntax allows multiple components to be selected. 分量选择语法允许多个分量同时被选择访问。如下面的例子所示: float4 c; c.xyzw = float4(1.0f, 2.0f, 3.0f, 4.0f); c.z = 1.0f; c.xy = float2(3.0f, 4.0f); c.xyz = float3(3.0f, 4.0f, 5.0f); The component selection syntax also allows components to be permuted or replicated. 分量选择语法允许多个分量乱序或是重复出现。如下面的例子所示: float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f); float4 swiz = pos.wzyx; // swiz = (4.0f, 3.0f, 2.0f, 1.0f) float4 dup = pos.xxyy; // dup = (1.0f, 1.0f, 2.0f, 2.0f) The component group notation can occur on the left hand side of an expression. To form the lvalue, swizzling may be applied. The resulting lvalue may be either the scalar or vector type, depending on number of components specified. Each component must be a supported scalar or vector type. The resulting lvalue of vector type must not contain duplicate components. 向量的分量组标记可以出现在表达式的左边,表示左值,乱序的分量也是支持的。左值可以是标量或是向量,这取 决于被指定的分量的个数。每一个分量必须是一个被支持的标量或是向量类型。作为左值,不可以包含重复分量。 以上描述的规则如下面的例子所示: float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f); // pos = (5.0, 2.0, 3.0, 6.0) pos.xw = float2(5.0f, 6.0f); // pos = (8.0, 2.0, 3.0, 7.0) pos.wx = float2(7.0f, 8.0f); // pos = (3.0, 5.0, 9.0, 7.0) pos.xyz = float3(3.0f, 5.0f, 9.0f); The following methods of vector component access are not permitted and result in a compile-time error: 如下的向量分量访问方法是不被允许会导致编译错误: ! Accessing components beyond those declared for the vector type is an error. 2-component vector data types can only access .xy or .rg elements. 3-component vector data types can only access .xyz or .rgb elements. For instance:
访问向量分量时,如果超过了向量声明的维度数会产生错误。一个2维向量可以通过 .xy 或 .rg 访问其分量。一 个3维的向量可以通过 .xyz 或 .rgb 访问其分量,如下面的例子所示: float2 pos; pos.x = 1.0f; // is legal; so is y pos.z = 1.0f; // is illegal; so is w float3 pos; pos.z = 1.0f; // is legal pos.w = 1.0f; // is illegal ! Accessing the same component twice on the left-hand side is ambiguous; for instance, 如果作为左值,同一个分量出现多于一次是错误的,如下例所示: // illegal - 'x' used twice pos.xx = float2(3.0f, 4.0f); // illegal - mismatch between float2 and float4 pos.xy = float4(1.0f, 2.0f, 3.0f, 4.0f); ! The .rgba and .xyzw qualifiers cannot be intermixed in a single access; for instance, .rbga 和 .xyzw 不能在同一次访问中混用,如下例所示: float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f); pos.x = 1.0f; // OK pos.g = 2.0f; // OK pos.xg = float2(3.0f, 4.0f); // illegal - mixed qualifiers used float3 coord = pos.ryz; // illegal - mixed qualifiers used ! A pointer or reference to a vector with swizzles; for instance 指向向量分量的指针或是引用也是不合法的。如下例所示: float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f); my_func(&pos.xy); // illegal The sizeof operator on a vector type returns the size of the vector, which is given as the number of components * size of each component. For example, sizeof(float4) returns 16 and sizeof(half4) returns 8. sizeof 操作符号返回向量的尺寸,这个尺寸等于向量分量的数量 * 每个分量的尺寸。比如sizeof(float4)返回16,而 sizeof(half4) 返回8。 Accessing Matrix Components 访问矩阵分量 The floatnxm and halfnxm matrices can be accessed as an array of n floatm or n halfm entries. The components of a matrix can be accessed using the array subscripting syntax. Applying a single subscript to a matrix treats the matrix as an array of column vectors. The first column is column 0. A second subscript would then operate on the resulting vector, as defined earlier for vectors. Hence, two subscripts select a column and then a row. floatnxm可以被当做一个有n个floatm向量构成的数组来访问,halfnxm可以被当做一个有n个halfm向量构成的数组来访 问。 矩阵的各分量可以使用数组下标语法来访问。如果只使用一个数组下标访问矩阵,那么数组就是被当做列向量数组 被访问,第一列的下标是0。如果有第二个下标,那么表示访问列向量内的某个分量,也就是说两个下标先选择了列 后选择行。下面就是使用数组下标访问矩阵分量的示例: float4x4 m; // sets the 2nd column to all 2.0 m[1] = float4(2.0f); // sets the 1st element of the 1st column to 1.0 m[0][0] = 1.0f; // sets the 4th element of the 3rd column to 3.0 m[2][3] = 3.0f; Accessing a component outside the bounds of a matrix with a non-constant expression results in undefined behavior. Accessing a matrix component that is outside the bounds of the matrix with a constant expression generates a compile-time error.
分享到:
收藏