Cg/HLSL programming

This section presents a brief description 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 properties in a Properties block. If you want to access some of those properties 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:

  • Color and Vector properties map to float4 variables.
  • Range and Float properties map to float variables.
  • Texture properties map to sampler2D variables for regular (2D) textures. CUBE and RECT textures map to samplerCUBE and samplerRECT variables, respectively.

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

Description

float

A 32-bit floating point number (high precision floating point.

Generally 32 bits, just like float type in regular programming languages).

half

A 16-bit floating point number (medium-precision floating point. Generally 16 bits, with a range of -60000 to +60000 and 3.3 decimal digits of precision)

int

A 32-bit integer.

fixed

A 12-bit fixed point number (low-precision fixed point. Generally 11 bits, with a range of -2.0 to +2.0 and 1/256th precision).

bool

A Boolean variable (FALSE = 0, TRUE = 1).

sampler*

Represents a texture object (sampler1D, sampler2D, sampler3D, samplerCUBE, samplerRECT).

Cg/HLSL also features vector and matrix data types that are based on the basic data types, such as float3 and float4x4. Such data types are quite common when dealing with 3D graphics programming. Cg/HLSL also has struct and array data types, which work in a similar way to their C equivalents.

Common methods to create shaders

Method

Description

dot( a, b )

Dot product of two vectors.

cross( A , B )

Cross product of vectors A and B; A and B must be three-component vectors.

max( a, b )

Maximum of a and b.

min( a , b )

Minimum of a and b.

floor( x )

Get largest integer not greater than x.

round( x )

Get closest integer to x.

ceil( x )

Get smallest integer not less than x.

pow( x , y )

Computes x raised to the power y.

normalize( v )

Returns a vector of length 1 that points in the same direction as vector v.

saturate( x )

Clamps x to the [0, 1] range.

tex2D(sampler, x )

2D texture lookup (sampled data at the location indicated by the texture coordinate set in the sampler object).

The preceding methods are the common methods that you can use to create your shader with Cg/HLSL. There are a lot of methods that you can also use in Cg/HLSL.

For more details, you can refer to the following site:

http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html.

Tip

Note that UnpackNormal( x ) is the method that is provided by Unity to unpack the normal or bump texture, which you can find in the UnityCG.cginc file inside Unity {unity install path}/Data/CGIncludes/UnityCG.cginc on Windows, and in /Applications/Unity/Unity.app/Contents/CGIncludes/UnityCG.cginc on Mac.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.57.172