CG Learning (2)--CG basic syntax and core functions of object-oriented simple encapsulation

Source: Internet
Author: User
Tags function definition scalar

Abstract: This paper introduces the grammatical features of CG, special semantics and special operations supported, briefly introduces the approximate process of CG work, and focuses on how to encapsulate an object-oriented Cgshader class, which encapsulates the core function of CG, and uses this class to simplify the development work of CG.

1. CG syntax, semantics and special operations
In the same way as C: CG has the syntax of Class C, there are many basic data types and keywords like C, with the same struct definition, you can also use # include files, and C & C + + same annotation.
CG's Special: CG uses the C & C + + No other keywords, CG support semantic binding, semantic (semantic) is actually CG and traditional graphics pipeline communication interface, CG shader program can get some properties from the fixed pipeline, such as vertex position, color, Texture coordinates and so on, then do the processing you want to feedback back, to modify the fixed pipeline, to obtain the property and feedback modification is implemented through the semantic binding. Semantic is divided into input and output semantics, the interface of getting the attribute is input semantics, and modifying the feedback interface is the output semantics. Semantic binding is valid only in the parameter list of the entry function of the CG, and is not valid within the normal intrinsic function. Input and output semantics are different, although some semantics have the same name, such as the input parameters of vertex shader, position refers to the vertex position that the application passes in, and the output parameter uses position semantics to represent the clipping space position to be fed back to the hardware rasterizer. The two semantics are named position, and all represent a location, but each location semantics represents the location of the different stages of the graphics pipeline. The following semantic binding instance (binding for output semantics):
struct Output {
FLOAT4 position:position;
FLOAT4 Color:color;
};
Another special area of CG is the support vector type and vector, matrix operations. Both C & C + + are scalar types, because vector operations are essential for processing vertices and fragments, and the graphics processor has built-in support for vector data types, so CG introduces vector type data, and the vector in CG is not simply a scalar array, but a compressed array (packed arrays) to provide faster vector operation. In addition to vectors, CG also natively supports matrix types. Here are some examples of vectors and matrices:
FLOAT4 data = {0.5,-2, 3, 3.14}; A vector with 4 component, initializing as in C
float4x4 matrix1; Define a 4x4 matrix with elements
Variable (Varying) and uniform (Uniform) variables: Semantics provide a way to initialize CG program parameters using values that vary with vertices or with fragments, so CG does not cancel the Varying, unlike GLSL, which uses varing keywords to qualify variable variables, and CG semantics binding to express. CG in the same way as other shading language also uses the Uniform keyword to represent uniform variables, the uniform variable is the need to be imported from the application CG program.

2. Approximate workflow for applying CG
(1) Create an environment: Cgcontext CTX = Cgcreatecontext ();
(2) Compile a program: Add a CG program to an environment by using Cgcreateprogram, and compile the CG program
Cgprogram program = Cgcreateprogram (CTX, Cg_source, programstring, Profile, "main", args);
(3) Loading a procedure: Cgglloadprogram (program);
(4) Modify the program parameters: first obtain the parameters, Cgparameter myparameter = cggetnamedparameter (Programs, "Myparamter"); Then set the parameters, such as: CGGLSETPARAMETER4FV (Myparameter, value);
(5) Execution procedure: cgglenableprofile (Profile); Cgglbindprogram (program); In OpenGL, to disallow a profile to call Cggldisableprofile (profile);
(6) Releasing resources: Cgdestroyprogram (program); Cgdestroycontext (context);

3. Cgshader Package
This class simplifies and encapsulates the call flow and parameter settings of CG, which is more intuitive and convenient to use. The functions in which the parameters are set are made into template member functions. Because the general compiler to the template function definition and implementation of the separation of support is not good, the template function implementation is also placed in the header file.
The header file (h file) is as follows: #ifndef Cgshader_h
#define Cgshader_h

#include < map >
#include < cg/cg.h >
#include < cg/cggl.h >

Class String;
Class Cgshader
... {
Public
Cgshader (void);
Cgshader (const cgshader& copy);
Cgshader (const char* shadername);
Cgshader (bool ifvertexshader, const char* sourcestrorfilename, const char* shadername, const char* entry = "Main",
BOOL Iffromfile = True, cgenum sourcetype = cg_source, const char** args = NULL);
void operator = (const cgshader& copy);

void Setshadername (const char* shadername);
void Createshader (bool ifvertexshader, const char* sourcestrorfilename, const char* entry = "Main",
BOOL Iffromfile = True, cgenum sourcetype = cg_source, const char** args = NULL);
void AddParam (const char* paramname);
Cgparameter getparambyname (const char* paramname);

/**//*------------Parameters Setting------------*/
void setrowpriormatrixf (const char* paramname, const float* MAT);
void Setrowpriormatrixd (const char* paramname, const double* MAT);
void setcolpriormatrixf (const char* paramname, const float* MAT);
void Setcolpriormatrixd (const char* paramname, const double* MAT);

Template<typename t>
void setParam1 (const char* paramname, T x)
... {
Cgparameter var = getparambyname (paramname);
if (!cgisparameter (Var))
Return

if (typeid (x) = = typeid (int))
Cgsetparameter1i (Var, x);
else if (typeid (x) = = typeID (float))
CGSETPARAMETER1F (Var, x);
else if (typeid (x) = = typeID (double))
CGSETPARAMETER1D (Var, x);

Checkcgerror (String ("setParam1") + typeid (x). name ());
}

Template<typename t>
void setParam2 (const char* paramname, T X, t y)
... {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.