Game Development Reference
In-Depth Information
Cg/HLSL programming
This secion presents a brief descripion of how to access the shader
Properties
in Cg/
HLSL programming, and the data types and common methods used in Cg/HLSL programming.
Accessing shader properties in Cg/HLSL
Shader can be declared with properies in a
Properties
block. If you want to access some
of those properies in a Cg/HLSL shader program, you need to declare a Cg/HLSL variable
with the same name and a matching type.
Example:
Properties {
_MainTex ("Texture", 2D) = "white" {}
}
SubShader {
……
CGPROGRAM
sampler2D _MainTex;
…
Property types to Cg/HLSL variable types are as follows:
F
Color
and
Vector
properies map to
float4
variables.
F
Range
and
Float
properies map to
float
variables.
F
Texture
properies map to
sampler2D
variables for regular (2D) textures.
CUBE
and
RECT
textures map to
samplerCUBE
and
samplerRECT
variables, respecively.
Data type
Cg/HLSL has six basic data types. Some of them are the same as in C, while others are
especially added for GPU programming. These types are:
Date type
Descripion
float
A 32-bit loaing point number (high precision loaing point.
Generally 32 bits, just like float type in regular programming languages).
half
A 16-bit loaing point number (medium-precision loaing point. Generally 16 bits,
with a range of -60000 to +60000 and 3.3 decimal digits of precision)
int
A 32-bit integer.