文章

shader入门基础知识五

shader入门基础知识五
  • content

Cg 入门二 C 语言和 Cg 语言的区别联系

前言

这里主要是我个人的理解,大家想要准确的解释,还是建议大家上 nvdia 官网读官方文档

Similar Operations That Must be Expressed Differently

There are several changes that force the same operation to be expressed differently in Cg than in C:
//cg 和 c 都有,但是表达却不同的地方

  • A Boolean type, bool, is introduced, with corresponding implications for operators and control constructs.//c 语言中的 bool 类型是非 0 为真,cg 中有 true false
  • Arrays are first‐class types because Cg does not support pointers. //数组是第一等类型,因为 cg 不支持指针 ,这里强调数组相当于基础数据类型,不支持引用传递而是值传递
  • Functions pass values by value/result, and thus use an out or inout modifier in the formal parameter list to return a parameter. By default, formal parameters are in, but it is acceptable to specify this explicitly. Parameters can also be specified as in out, which is semantically the same as inout. //函数传参通过值传递,因此通过 inout 修饰符返回形参列表中的一个修饰符

Differences from ANSI C

Cg was developed based on the ANSI‐C language with the following major additions, deletions, and changes. (This is a summary—more detail is provided later in this document)://基于 c 语言的添加删除更改

  • Language profiles (described in “Profiles” on page 225) may subset language capabilities in a variety of ways. In particular, language profiles may restrict the use of for and while loops. For example, some profiles may only support loops that can be fully unrolled at compile time.
  • A binding semantic may be associated with a structure tag, a variable, or a structure element to denote that object’s mapping to a specific hardware or API resource. See “Binding Semantics” on page 242. ‰
  • Reserved keywords goto, break, and continue are not supported.
  • Reserved keywords switch, case, and default are not supported.
  • Labels are not supported either.//保留的关键词,goto,break,continue,switch,case,default 不被支持,Labels 也是不被支持的
  • Pointers and pointer‐related capabilities (such as the & and -> operators) are not supported.//不支持指针和指针相关,例如 & 和 ->
  • Arrays are supported, but with some limitations on size and dimensionality. Restrictions on the use of computed subscripts are also permitted. Arrays may be designated as packed. The operations allowed on packed arrays may be different from those allowed on unpacked arrays. Predefined packed types are provided for vectors and matrices. It is strongly recommended these predefined types be used.//支持数组,但是有指针和维度的限制
  • Unsized arrays can be created by declaring an array’s dimension as []. The array’s actual dimension can be set at runtime before a final compilation step.//可以通过将数组的维度声明为[]来创建无大小的数组。数组的实际维度可以在运行时在 final 之前设置
  • There is a built‐in swizzle operator: .xyzw or .rgba for vectors. This operator allows the components of a vector to be rearranged and also replicated. It also allows the creation of a vector from a scalar. //内置的 swizzle 操作符:.xyzw 或.rgba 用于向量。swizzle 运算符允许向量的分量也可以重新排列 复制。它还允许从标量创建向量
  • For an lvalue, the swizzle operator allows components of a vector or matrix to be selectively written.
  • There is a similar built‐in swizzle operator for matrices: This operator allows access to individual matrix components and allows the creation of a vector from elements of a matrix. For compatibility with DirectX 8 notation, there is a second form of matrix swizzle, which is described later.
  • Numeric data types are different. Cg’s primary numeric data types are float, half, and fixed. Fragment profiles are required to support all three data types, but may choose to implement half and fixed at float precision. Vertex profiles are required to support half and float, but may choose to implement half at float precision. Vertex profiles may omit support for fixed operations, but must still support definition of fixed variables. Cg allows profiles to omit run‐time support for int. Cg allows profiles to treat double as float. ‰ Many operators support per‐element vector operations.
  • The ?:, ||, &&, !, and comparison operators can be used with bool four‐ vectors to perform four conditional operations simultaneously. The side effects of all operands to the ?:, ||, and && operators are always executed.//Cg 主要数据类型 fixed,half,float,支持?:(三目运算符)||(或),&&(且),!(非)等布尔操作符
  • Non‐static global variables and parameters to top‐level functions—such as main()—may be designated as uniform. A uniform variable may be read and written within a program, just like any other variable. However, the uniform modifier indicates that the initial value of the variable or parameter is expected to be constant across a large number of invocations of the program.
  • A new set of sampler* types represents handles to texture objects. //sampler 处理采样对象
  • Functions may have default values for their parameters, as in C++. These defaults are expressed using assignment syntax.//函数可能会有默认参数,这些默认的参数使用赋值语义表示
  • Function overloading is supported.//支持方法重写
  • There is no enum or union. //不支持枚举或者联合体 ‰
  • Bit‐field declarations in structures are not allowed. ‰ There are no bit‐field declarations in structures.//不支持位域
  • Variables may be defined anywhere before they are used, rather than just at the beginning of a scope as in C. (That is, we adopt the C++ rules that govern where variable declarations are allowed.) Variables may not be redeclared within the same scope.// 变量可以声明在使用前的任何地方,不可以重复声明在相同的代码块
  • Vector constructors, such as the form float4(1,2,3,4), may be used anywhere in an expression.//向量构造器可以在任何表达的地方使用
  • A struct definition automatically performs a corresponding typedef, as in C++.//结构体自动执行位相应的类型
  • An interface can be specified to define a set of methods that comprises an abstract interface.//接口可以指定定义一些方法来组成抽象接口
  • A struct type can be declared as implementing an interface by adding a colon “:” and the name of the interface after the name of the struct.//一个结构体可以通过:实现一个接口 ‰
  • Methods can be defined in the body of a struct definition.//方法可以定义在结构体内部
  • C++‐style // comments are allowed in addition to C‐style // comments.//注释风格支持 // 和//
本文由作者按照 CC BY 4.0 进行授权