Skip navigation

APPARENTLY HLSL FORCES you to use global variables.

In Cg, you have to get a “handle” to each uniform parameter that you simply call

void vertexShaderFunction (
            float3 incomingPosition : POSITION,
            float3 incomingColor    : COLOR,

    uniform float  mParam,             // this is a UNIFORM PARAMETER
                                       // Notice that it has NO SEMANTIC

        out float4 outgoingPosition : POSITION,
        out float4 outgoingColor    : COLOR
    outgoingPosition = float4( incomingPosition, 1);

    float3 transformedColor = incomingColor * mParam;
    outgoingColor = float4( transformedColor, 1 );


// C code:
  handleToParameter = cgGetNamedParameter( myCgVertexProgram,
                                           "mParam" );
  cgSetParameter1f( handleToParameter, 20.0f );  // set uniform parameter from C code
  // used "uniformly" across series of vertices.

In Cg GLOBALS aren’t supported.

In HLSL however, I’m finding that I CAN’T seem to get a reference to the uniform parameters. Instead, it seems as though HLSL WON’T LET YOU get the reference to those parameters. They aren’t in Effect.Parameters. The only items that show up in Effect.Parameters are the GLOBAL variables of the shader.

I can’t find it anywhere. MSDN says:

Uniform Shader Inputs

Vertex and pixel shaders accept two kinds of input data: varying and uniform. The varying input is the data that is unique to each execution of the shader. For a vertex shader, the varying data (for example: position, normal, etc.) comes from the vertex streams. The uniform data (for example: material color, world transform, etc.) is constant for multiple executions of a shader. For those familiar with the assembly shader models, uniform data is specified by constant registers and varying data by the v and t registers.

Uniform data can be specified by two methods. The most common method is to declare global variables and use them within a shader. Any use of global variables within a shader will result in adding that variable to the list of uniform variables required by that shader. The second method is to mark an input parameter of the top-level shader function as uniform. This marking specifies that the given variable should be added to the list of uniform variables.

Uniform variables used by a shader are communicated back to the application via the constant table. The constant table is the name for the symbol table that defines how the uniform variables used by a shader fit into the constant registers. The uniform function parameters appear in the constant table prepended with a dollar sign ($), unlike the global variables. The dollar sign is required to avoid name collisions between local uniform inputs and global variables of the same name.

The constant table contains the constant register locations of all uniform variables used by the shader. The table also includes the type information and the default value, if specified.

AH! I see. Because I’m working in XNA, I just don’t think those parameters are exposed. Apparently you need a ID3DXConstantTable interface and the uniform would have a prepended $to avoid name collisions between local uniform inputs and global variables of the same name.

OK. I forgive you, MSDN.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: