Extensible 3D (X3D)
Part 1: Architecture and base components
Annex X
(normative)
nVidia Cg Shading Language
This annex defines how a browser implementing the Shader component maps the concepts of the nVidia Cg Shading Language (Cg) to the Shading Component. It applies to the ProgramShader, ShaderProgram and PackagedShader nodes with the language field starting with "CG".
Table 1 provides links to the major topics in this annex.
The Cg language is a diverse set of shading capabilities that aim to support programmable shaders for both OpenGL and Microsoft's Direct3D APIs. The two programming APIs express the same concepts in very distinctly different ways. The result of this is that a Cg shader program written to work on OpenGL will not work on Direct3D and vice versa.
Cg handles the incompatible code by defining profiles, which is a set of available shading language functionality. At the time the browser downloads the file, it can use the profile information to guide how to compile the code to the appropriate target. This Annex thus needs to define it's behaviour based on the Cg profile selected by the user.
The Cg profile information is encoded as part of the language string of the ProgramShader node. All strings starting with "CG-" define behaviour defined in this annex. The part after the prefix defines the programming API and profile that the Cg code shall be compiled with. This allows for a browser to quickly distinguish which nodes to ignore and which to investigate further. The language string is defined in the ProgramShader node, while the source files are referenced in the ShaderProgram nodes. This specification requires that the same profile is used for both the vertex and fragment programs.
Table 2 defines the mappings between the language string and the appropriate Cg profile. It is not an exhaustive list as Cg is in constant, rapid development. Newer profiles shall follow a similar naming convention.
Table 2 — Language String to Cg Profile mapping
| Language String | Cg Vertex Shader Profile | Cg Pixel Shader Profile |
|---|---|---|
| CG_OPENGL_ARB | CG_PROFILE_ARBVP1 | CG_PROFILE_ARBFP1 |
| CG_OPENGL_NV30 | CG_PROFILE_VP30 | CG_PROFILE_VP30 |
| CG_OPENGL_NV20 | CG_PROFILE_VP20 | CG_PROFILE_VP20 |
| CG_D3D_SHADER_2.0 | CG_PROFILE_VS_2_0 | CG_PROFILE_PS_20 |
| CG_D3D_SHADER_3.0 | CG_PROFILE_VS_3_0 | CG_PROFILE_PS_1_3 |
| CG_D3D_SHADER_1.3 | CG_PROFILE_VS_1_3 | CG_PROFILE_PS_1_3 |
The vertex shader replaces the fixed functionality of the vertex processor. The OpenGL specification states that the following functionality is disabled if a vertex shader is supplied:
In Cg language Direct3D profiles, the vertex shader replaces the vertex processing done by the Microsoft Direct3D graphics pipeline. While using a vertex shader, state information regarding transformation and lighting operations is ignored by the fixed function pipeline. The Direct3D specification states that the following functionality is disabled if a vertex shader is supplied:
The fragment shader replaces the fixed functionality of the fragment processor. The OpenGL specification states that the following functionality is disabled if a fragment shader is supplied:
In Cg language Direct3D profiles, the fragment shader, also know as a pixel shader in Cg, replaces the fixed functionality of the Direct3D fragment processor. The Direct3D specification states that the following functionality is disabled if a fragment shader is supplied:
The LoadSensor node (See 9.4.3 LoadSensor) has two output fields isActive and isLoaded. The isLoaded field behaviour is unchanged.
The isActive field is defined to issue a TRUE event when the all the following conditions have been satisifed:
Each vertex attribute node directly maps the name field to the uniform variable of the same name. If the name is not available as a uniform variable in the provided shader source, the values of the node shall be ignored.
The browser implementation shall automatically assign appropriate internal index values for each attribute
In Cg language Direct3D profiles, each vertex attribute node directly maps the name field to a Direct3D usage type for use within a Direct3D vertex declaration (with the prefix "D3DDECLUSAGE_" prepended to the name), as well as a Cg binding semantic of the same name defined on the varying inputs to a shader program. This language binding allows the use of the predefined Direct3D vertex declaration usage types and Cg binding semantics listed in Table 2. If the name is can not be interpreted as a valid Direct3D usage type or Cg binding semantic, the values of the node shall be ignored.
Table 3 — Supported Direct3D Vertex Declaration Usage Types
| Direct3D Usage Type |
|---|
| POSITION |
| NORMAL |
| TEXCOORD |
| TANGENT |
| BINORMAL |
| COLOR |
| FOG |
The browser implementation shall automatically assign appropriate internal index values for each attribute in the case where multiple nodes a define having the same value in the name field.
Fields that are of type SFNode/MFNode are ignored unless the value is of type X3DTextureNode, or in Direct3D profiles, X3DMaterialNode, or X3DLightNode. Field instances of type X3DTextureNode are mapped according to the appropriate Direct3D or OpenGL sampler data type. The mappings from texture nodes to built-in sampler types is defined in Table 3
.Table 4 — Mapping of X3D texture node types to OpenGL or Direct3D Sampler Types
| X3D Texture type | OpenGL Variable Type | Direct3D Variable Type |
|---|---|---|
| X3DTexture2DNode | sampler2D | sampler2D |
| X3DTexture3DNode | sampler3D | sampler3D |
| X3DEnvironmentTextureNode | samplerCube | samplerCube |
X3D does not define mappings to the OpenGL types sampler1D, sampler1DShadow and sampler2DShadow or the Direct3D types sampler1D, sampler1DShadow and sampler2DShadow.
In Cg language OpenGL profiles, the current geometry and pipeline state is exposed through the builtin variable glstate
In Cg language Direct3D profiles, field instances of type X3DMaterialNode and X3DLightNode are mapped to structures which must be declared in the shader program as defined in Table 4.
Table 5 — Mapping of X3D material and light node types to Cg Shading Language Structure Declarations (Direct3D profiles only)
| X3D Node Type | Cg Structure Declaration | Additional Information |
|---|---|---|
| X3DMaterialNode |
struct X3DMaterial {
float4 diffuseColor;
float4 ambientColor;
float4 specularColor;
float4 emissiveColor;
float power;
};
|
All color values are 4-component with alpha value = 1.0 |
| X3DLightNode |
struct X3DLight {
int type;
float4 diffuseColor;
float4 specularColor;
float4 ambientColor;
point3 position;
point3 direction;
float range;
float falloff;
float attenuation0;
float attenuation1;
float attenuation2;
float theta;
float phi;
bool on;
};
|
Valid type member values are 1 for Point light, 2 for Spot light and
3 for Direction light. All color values are 4-component with alpha value = 1.0 All position, direction and scalar values are assumed to be in world space The on member specifies whether the light is enabled |
Table 5 indicates how the X3D field types shall be mapped to data types used in the Cg Language.
Table 6 — Mapping of X3D Field Types to Cg Data Types
| X3D Field type | Cg Data Type |
|---|---|
| SFBool | bool |
| MFBool | bool[] |
| MFInt32 | float[] |
| SFInt32 | float |
| SFFloat | float |
| MFFloat | float[] |
| SFDouble | double |
| MFDouble | double[] |
| SFTime | double |
| MFTime | double[] |
| SFNode | See 5.1 Node Fields |
| MFNode | See 5.1 Node Fields |
| SFVec2f | float2 |
| MFVec2f | float2[] |
| SFVec3f | float3 |
| MFVec3f | float3[] |
| SFVec4f | float4 |
| MFVec4f | float4[] |
| SFVec3d | float3 |
| MFVec3d | float3[] |
| SFVec4d | float4 |
| MFVec4d | float4[] |
| SFRotation | float4 |
| MFRotation | float4[] |
| MFColor | float4[] |
| SFColor | float4 |
| SFImage | int[] |
| MFImage | int[] |
| SFString | Not supported |
| MFString | Not supported |
| SFMatrix3f | float3x3 |
| MFMatrix3f | float3x3[] |
| SFMatrix4f | float4x4 |
| MFMatrix4f | float4x4[] |
Cg defines maximum supported lengths of each array data type, which may conflict with the minimum support requirements for X3D.
In Cg language Direct3D profiles, certain internal states of the X3D world, such as transformation matrices, or the viewer's position in world space, are neither readily available via the Cg shader program or directly accessible from the X3D scene graph. Thus, they must be explicitly passed in to the shader program as named parameters. This binding defines an automatic mapping of these states to predefined shader program parameter names. Table 6 indicates how internal states of the X3D world are mapped to parameter names used in the Cg programs.
Table 7 — Mapping of X3D World State to Cg Parameter Names (Direct3D profiles only)
| Parameter name | Description |
|---|---|
| model | The matrix transforming from local to global coordinates. Transforms vertices from their model position to their position in world space (i.e. after effect of all Transform nodes has been applied). |
| view | The viewing matrix transforming from world to view relative coordinates. |
| projection | The projection matrix transforming from viewing relative coordinates to clip space, including the projective part. |
| modelView | Concatenation of model and view transforms vertices from their model position to their position in view space (i.e. after effect of all Transform nodes and current viewpoint has been applied). |
| modelViewProjection | Concatenation of model, view and projection transforms vertices from their model position to their final position in clip space. |
| viewPosition | The current viewer position in world space coordinates. |
The following suffixes can be applied to the matrix builtin values. A suffix of I signifies the inverse of the matrix. T signifies the transpose of the matrix. IT signifies the inverse transpose of the matrix.
When the url receives an event changing the value, the browser shall immediately attempt to download the new source. Upon successful download, the browser shall attempt to compile the new source and issue the appropriate LoadSensor events. It shall not automatically activate the shader program, nor disable the currently running shader.
Values defined at load time of the file, do not require an explicit request to activate the shader program. It shall be assumed to automatically activate the program once all the objects have successfully downloaded. If some of the shader source files are not downloaded or compiled (eg due to errors) then no activation will occur for the shader program.
Per-vertex attributes may be defined as one of the fields of X3DComposedGeometryNode. These may be changed at runtime by adding or removing node instances. Adding new node instances to the field shall require that the user request an explicit activate in order to make them visible to the shader.
The user may, at any time, request that the browser activate the composing shader objects by sending a TRUE value to the activate inputOnly field of the ProgramShader or PackagedShader node. Users may need to force a re-activation of the node under various circumstances, such as changing the url field of one or more ShaderProgram or PackagedShader nodes, or adding or removing ShaderProgram nodes from the programsfield of the ProgramShader node. Reactivating the shader shall replace the existing shader with the new compiled shader for subsequent rendering.