Cg語言

本页使用了标题或全文手工转换
?基百科,自由的百科全?

Cg?言 (C for Graphics)是? GPU ?程??的 高級著色器語言 ,由 NVIDIA 公司??。Cg?力保留 C?言 的大部分??,????者?硬件??中解?出?,Cg同?也有一?高??言的其他好?,如代?的易重用性,可?性得到提高,??器代??化。

Cg?言主要?照 ANSI C 建模,但也? C++ Java 以及早期的?制?言如RenderMan and the Stanford shading language中吸取了一些思想。?些使得?容易?程序然后由??器?行?化,提高了可?性。而且Cg的??考?了GPU的?系??,如可?程多?理器?元(?点?理器,像素?理器,外加不可?程?元)。?些部分和?用都是通??据流?接起?。Cg?言允?分?? ?点 和像素?程序。Cg API引入了profiles的?念以?理?点和像素?程所缺乏的通用性。一?Cg profile就定?了一套整?Cg?言的子集以适?不同的硬件平台和API。Cg程序可以根据?行?的需要或者事先??成GPU??代?。??可以?容易地?一?Cg像素程序和手?的?点程序?合起?,或者甚至采用不可?程的OpenGL或者DirectX?点流水?,反之亦然。

由于Nvidia和微?合作?密,Cg?言和 HLSL 的共通之??多。 [1] 在早期的官方文?里,??者被解?成?了??牌的同一??言。 [2]

細節 [ ?? ]

資料型別 [ ?? ]

Cg有6種?据型別(data types),?C語言相當類似,有一些是?GPU特別定義的,型別如下:

  • float - 32位元浮點數
  • half - 16位元浮點數
  • int - 32位元整數
  • fixed - 12位元定點數
  • bool - 布??
  • sampler* - 代表紋理物件(texture object)

範例 [ ?? ]

  • A sample Cg vertex shader
 // input vertex

 struct
 VertIn
 {

     float4
 pos
   :
 POSITION
;

     float4
 color
 :
 COLOR0
;

 };

 
 // output vertex

 struct
 VertOut
 {

     float4
 pos
   :
 POSITION
;

     float4
 color
 :
 COLOR0
;

 };

 
 // vertex shader main entry

 VertOut
 main
(
VertIn
 IN
,
 uniform
 float4x4
 modelViewProj
)
 {

     VertOut
 OUT
;

     OUT
.
pos
     =
 mul
(
modelViewProj
,
 IN
.
pos
);
 // calculate output coords

     OUT
.
color
   =
 IN
.
color
;
 // copy input color to output

     OUT
.
color
.
z
 =
 1.0f
;
 // blue component of color = 1.0f

     return
 OUT
;

 }

參照 [ ?? ]

外部連結 [ ?? ]

  1. ^ Fusion Industries :: Cg and HLSL FAQ :: . 2012-08-24 [ 2020-04-25 ] . ( 原始?容 存?于2012-08-24).  
  2. ^ The Cg Tutorial - Chapter 1. Introduction . developer.download.nvidia.cn. [ 2020-04-25 ] . ( 原始?容 存?于2022-04-15).