
Cg provides a set of built-in functions and predefined structures with binding semantics to simplify GPU programming. These functions are similar in spirit to the C standard library, offering a convenient set of common functions. In many cases, the functions map to a single native GPU instruction, so they are executed very quickly. Of the functions that map to multiple native GPU instructions, you may expect the most useful to become more efficient in the near future.
Although you can write your own versions of specific functions for performance or precision reasons, it is generally wiser to use the Cg Standard Library functions when possible. The Standard Library functions will continue to be optimized for future GPUs; a program written today using these functions will automatically be optimized for the latest architectures at compile time. Additionally, the Standard Library provides a convenient unified interface for both vertex and fragment programs.
This appendix describes the contents of the Cg Standard Library, and is divided into the following five sections:
Where appropriate, functions are overloaded to support scalar and vector variants when the input and output types are the same.
Table E-1 lists the mathematical functions that the Cg Standard Library provides. The table includes functions useful for trigonometry, exponentiation, rounding, and vector and matrix manipulations, among others. All functions work on scalars and vectors of all sizes, except where noted.
Function | Description |
|---|---|
abs(x) | Absolute value ofx. |
acos(x) | Arccosine ofx in range [0, ],x in [–1, 1]. |
all(x) | Returnstrue if every component ofx is not equal to 0. Returnsfalse otherwise. |
any(x) | Returnstrue if any component ofx is not equal to 0. Returnsfalse otherwise. |
asin(x) | Arcsine ofx in range [–/2, /2];x should be in [–1, 1]. |
atan(x) | Arctangent ofx in range [–/2, /2]. |
atan2(y,x) | Arctangent ofy/x in range [–, ]. |
ceil(x) | Smallest integer not less thanx. |
clamp(x,a,b) | x clamped to the range [a,b] as follows:
|
cos(x) | Cosine ofx. |
cosh(x) | Hyperbolic cosine ofx. |
cross(A,B) | Cross product of vectorsA andB; A andB must be three-component vectors. |
degrees(x) | Radian-to-degree conversion. |
determinant(M) | Determinant of matrixM. |
dot(A,B) | Dot product of vectorsA andB. |
exp(x) | Exponential function ex. |
exp2(x) | Exponential function 2x. |
floor(x) | Largest integer not greater thanx. |
fmod(x,y) | Remainder ofx/y, with the same sign asx. Ify is 0, the result is implementation-defined. |
frac(x) | Fractional part ofx. |
frexp(x, outexp) | Splitsx into a normalized fraction in the interval [½, 1), which is returned, and a power of 2, which is stored inexp. Ifx is 0, both parts of the result are 0. |
isfinite(x) | Returnstrue ifx is finite. |
isinf(x) | Returnstrue ifx is infinite. |
isnan(x) | Returnstrue ifx is NaN (Not a Number). |
ldexp(x,n) | x x 2n. |
lerp(a,b,f) | Linear interpolation: (1 –f)*a+b*f wherea andb are matching vector or scalar types.f can be either a scalar or a vector of the same type asa andb. |
lit(NdotL,NdotH,m) | Computes lighting coefficients for ambient, diffuse, and specular light contributions. Expects theNdotL parameter to containN •L and theNdotH parameter to containN •H. Returns a four-component vector as follows:
There is no vectorized version of this function. |
log(x) | Natural logarithmln(x);x must be greater than 0. |
log2(x) | Base 2 logarithm ofx;x must be greater than 0. |
log10(x) | Base 10 logarithm ofx;x must be greater than 0. |
max(a,b) | Maximum ofa andb. |
min(a,b) | Minimum ofa andb. |
modf(x, outip) | Splitsx into integral and fractional parts, each with the same sign asx. Stores the integral part inip and returns the fractional part. |
mul(M,N) | Matrix product of matrixM and matrixN, as shown below:
IfM has sizeAxB, andN has sizeBxC, returns a matrix of sizeAxC. |
mul(M,v) | Product of matrixM and column vectorv, as shown below:
IfM is anAxB matrix andv is aBx1 vector, returns anAx1 vector. |
mul(v,M) | Product of row vectorv and matrixM, as shown below:
Ifv is a1xA vector andM is anAxB matrix, returns a1xB vector. |
noise(x) | Either a one-, two-, or three-dimensional noise function, depending on the type of its argument. The returned value is between 0 and 1, and is always the same for a given input value. |
pow(x,y) | xy. |
radians(x) | Degree-to-radian conversion. |
round(x) | Closest integer tox. |
rsqrt(x) | Reciprocal square root ofx;x must be greater than 0. |
saturate(x) | Clampsx to the [0, 1] range. |
sign(x) | 1 ifx > 0; –1 ifx < 0; 0 otherwise. |
sin(x) | Sine ofx. |
sincos(floatx, outs, outc) | s is set to the sine ofx, andc is set to the cosine ofx. If bothsin(x) andcos(x) are needed, this function is more efficient than calculating each individually. |
sinh(x) | Hyperbolic sine ofx. |
smoothstep(min,max,x) | For values ofx betweenmin andmax, returns a smoothly varying value that ranges from 0 atx=min to 1 atx=max. x is clamped to the range [min,max] and then the interpolation formula is evaluated: –2*((x–min)/(max–min))3 + 3*((x–min)/(max–min))2 |
step(a,x) | 0 ifx <a; 1 ifx >=a. |
sqrt(x) | Square root ofx; x must be greater than 0. |
tan(x) | Tangent ofx. |
tanh(x) | Hyperbolic tangent ofx. |
transpose(M) | Matrix transpose of matrixM. IfM is anAxB matrix, the transpose ofM is aBxA matrix whose first column is the first row ofM, whose second column is the second row ofM, whose third column is the third row ofM, and so on. |
Table E-2 presents the geometric functions that are provided in the Cg Standard Library.
Function | Description |
|---|---|
distance(pt1,pt2) | Euclidean distance between pointspt1 andpt2. |
faceforward(N,I,Ng) | N ifdot(Ng,I) < 0;-N otherwise. |
length(v) | Euclidean length of a vector. |
normalize(v) | Returns a vector of length 1 that points in the same direction as vectorv. |
reflect(I,N) | Computes reflection vector from entering ray directionI and surface normalN. Valid only for three-component vectors. |
refract(I,N,eta) | Given entering ray directionI, surface normalN, and relative index of refractioneta, computes refraction vector. If the angle betweenI andN is too large for a giveneta, returns (0, 0, 0). Valid only for three-component vectors. |
Table E-3 presents the texture map functions that are provided in the Cg Standard Library. Currently, these texture functions are fully supported by theps_2_0,ps_2_x,arbfp1, andfp30 profiles (though only OpenGL profiles support thesamplerRECT functions). They will also be supported by all future advanced fragment profiles with texture-mapping capabilities. All of the functions listed in Table E-3 return afloat4 value.
Function | Description |
|---|---|
tex1D(sampler1Dtex, floats) | 1D nonprojective texture query |
tex1D(sampler1Dtex, floats, floatdsdx, floatdsdy) | 1D nonprojective texture query with derivatives |
tex1D(sampler1Dtex, float2sz) | 1D nonprojective depth compare texture query |
tex1D(sampler1Dtex, float2sz, floatdsdx, floatdsdy) | 1D nonprojective depth compare texture query with derivatives |
tex1Dproj(sampler1Dtex, float2sq) | 1D projective texture query |
tex1Dproj(sampler1Dtex, float3szq) | 1D projective depth compare texture query |
tex2D(sampler2Dtex, float2s) | 2D nonprojective texture query |
tex2D(sampler2Dtex, float2s, float2dsdx, float2dsdy) | 2D nonprojective texture query with derivatives |
tex2D(sampler2Dtex, float3sz) | 2D nonprojective depth compare texture query |
tex2D(sampler2Dtex, float3sz, float2dsdx, float2dsdy) | 2D nonprojective depth compare texture query with derivatives |
tex2Dproj(sampler2Dtex, float3sq) | 2D projective texture query |
tex2Dproj(sampler2Dtex, float4szq) | 2D projective depth compare texture query |
texRECT(samplerRECTtex, float2s) | 2D nonprojective texture rectangle texture query (OpenGL only) |
texRECT(samplerRECTtex, float2s, float2dsdx, float2dsdy) | 2D nonprojective texture rectangle texture query with derivatives (OpenGL only) |
texRECT(samplerRECTtex, float3sz) | 2D nonprojective texture rectangle depth compare texture query (OpenGL only) |
texRECT(samplerRECTtex, float3sz, float2dsdx, float2dsdy) | 2D nonprojective depth compare texture query with derivatives (OpenGL only) |
texRECTproj(samplerRECTtex, float3sq) | 2D texture rectangle projective texture query (OpenGL only) |
texRECTproj(samplerRECTtex, float3szq) | 2D texture rectangle projective depth compare texture query (OpenGL only) |
tex3D(sampler3Dtex, float3s) | 3D nonprojective texture query |
tex3D(sampler3Dtex, float3s, float3dsdx, float3dsdy) | 3D nonprojective texture query with derivatives |
tex3Dproj(sampler3Dtex, float4sq) | 3D projective texture query |
texCUBE(samplerCUBEtex, float3s) | Cube map nonprojective texture query |
texCUBE(samplerCUBEtex, float3s, float3dsdx, float3dsdy) | Cube map nonprojective texture query with derivatives |
texCUBEproj(samplerCUBEtex, float4sq) | Cube map projective texture query (ignoresq) |
Because of the limited pixel programmability of older hardware, theps_1_1,ps_1_2,ps_1_3, andfp20 profiles have restrictions on the use of texture-mapping functions. See the documentation for these profiles for more information.
In the table, the name of the second argument to each function indicates how its values are used when performing the texture lookup:
When you use the texture functions that allow specifying a depth comparison value, the associated texture unit must be configured for depth-compare texturing. Otherwise, no depth comparison will actually be performed.
Table E-4 presents the derivative functions that are supported by the Cg Standard Library. Vertex profiles do not support these functions.
Function | Description |
|---|---|
ddx(a) | Approximate partial derivative ofa with respect to screen-spacex coordinate |
ddy(a) | Approximate partial derivative ofa with respect to screen-spacey coordinate |
Table E-5 presents the debugging function that is supported by the Cg Standard Library. Vertex profiles are not required to support this function.
Function | Description |
|---|---|
void debug(float4x) | If the compiler'sDEBUG option is enabled, calling this function causes the valuex to be copied to theCOLOR output of the program, and execution of the program is terminated. If the compiler'sDEBUG option is not enabled, this function does nothing. |
The intent of thedebug function is to allow a program to be compiled twice—once with theDEBUG option and once without. By executing both programs, it is possible to obtain one frame buffer containing the final output of the program and another frame buffer containing an intermediate value to be examined for debugging purposes.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact:
International Sales
international@pearsontechgroup.com
Visit Addison-Wesley on the Web:www.awprofessional.com
Library of Congress Control Number: 2002117794
Copyright © 2003 by NVIDIA Corporation
Cover image © 2003 by NVIDIA Corporation
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada.
For information on obtaining permission for use of material from this work, please submit a written request to:
Pearson Education, Inc.
Rights and Contracts Department
75 Arlington Street, Suite 300
Boston, MA 02116
Fax: (617) 848-7047
Text printed on recycled paper at RR Donnelley Crawfordsville in Crawfordsville, Indiana.
8 9 10111213 DOC 09 08 07
8th Printing, November 2007



