Advanced Shader Inputs

From ETC Public Wiki
Jump to: navigation, search

Using the New Shader Inputs(Available in the current Panda3D CVS)

In the latest CVS version of Panda3D, new and advanced shader inputs are available. The available input types are

- Vec4
- Vec3
- Vec2
- Point4
- Point3
- Point2
- Mat4
- Mat3
- PTALMatrix4f
- PTALMatrix3f
- PTALVecBase4f
- PTALVecBase3f
- PTALVecBase2f
- PTAFloat
- PTADouble

are now supported. For definition let us consider the shader parameter float3. It's type is float and format is Vec3 (meaning it can hold 3 elements) and a float3x3 input is of type float and format Mat3(meaning it can hold 9 elements)

The main concept of the shader inputs is that the Cg input format and type is independent to the Panda3D input. The only condition is that the number of elements passed by the user through the setShaderInput() function of Panda3D and the number of elements expected by the shader input should be the same. For example a parameter uniform float4x4 mat[4] (total of 16*4 elements) could be set with(the below list is just a sample and there are more ways to represent it):


setShaderInput("input_name",PTALMat4f[4])
setShaderInput(PTALVecBase4f[16])
setShaderInput(PTAFloat[16*4])
setShaderInput(PTADouble[16*4])


But for some Cg input types there is no corresponding Panda3D type such as float3x2(Panda3D does not have a corresponding Mat3x2 class) Hence these input types can be initiated row-wise as

1 2 3


4 5 6

This row wise input can be sent to the Cg shader in any of the following ways(Note that the below list is just a sample and there are more ways to represent it)

setShaderInput(PTAFloat[6])
setShaderInput(PTADouble[6])
setShaderInput(PTALVecBase3f[2])
setShaderInput(PTALVecBase2f[3])

Now, the issue of common input types such as float, double, int, long. The GPU registers generally can handle only floats. Hence even if we do send a double it will be automatically type casted into float. Hence for such type of inputs we can use above types.

For example, input types such as

float3 var
bool3 var
half3 var
double3 var
fixed3 var
int3 var

Can be sent to your Cg shader program by(the below list is just a sample and there are more ways to represent it)

setShaderInput(PTAFloat[3])
setShaderInput(PTADouble[3])

Below is a sample code snippet that shows how you can use the new shader inputs.

from pandac.PandaModules import Vec4
vec4 = Vec4(0.0,1.0,0.0,1.0)
myModel.setShaderInput("Inputs.vec4",vec4)

First import the necessary header to use the type of input. In our case it's Vec4. The next statement shows a Vec4 input type. Then set the Vec4 as a shader input to your model.