Question

I'm trying to write a shader using cg (for ogre3d). I can't seem to parse a working shader that I'd like to use as a starting point for my own code.

Here's the declaration for the shader:

void main
(
  float2 iTexCoord0 : TEXCOORD0,

  out float4 oColor : COLOR,

  uniform sampler2D covMap1,
  uniform sampler2D covMap2,
  uniform sampler2D splat1,
  uniform sampler2D splat2,
  uniform sampler2D splat3,
  uniform sampler2D splat4,
  uniform sampler2D splat5,
  uniform sampler2D splat6,

  uniform float splatScaleX,
  uniform float splatScaleZ
) 
{...}

My questions:

iTexCoord0 is obviously an input parameter. Why is it not declared uniform?

(oColor is obviously an output parameter. No question)

covMap1 - splat6 are textures. Are these parameters or something loaded into the graphics card memory (like globals)? The ogre definition for the shader program doesn't list them as parameters.

Are splatScaleX and splatScaleZ also parameters? The ogre definition for the shader program also doesn't list these as parameters.

Does the order of declaration mean anything when sending values from an external program?

I'd like to pass in an array of floats (the height map). I assume that would be

  uniform float splatScaleZ,
  uniform float heightmap[1024]
) 
{...}

If I don't pass one of the parameters will the shader just not be executed (and my object will be invisible because it has no texture)?

Is there a better way to debug these than just hit/miss and guess?

Was it helpful?

Solution

iTexCoord0 is obviously an input parameter. Why is it not declared uniform?

Uniforms are not the same things as input parameters. iTexCoord is a varying input, which is to say that for every vertex, it can have a unique value. This is set with commands like glVertexAttribPointer. Things like vertex coordinates, normals, texcoords, vertex colors, are examples of what you might use a varying input for.

Uniforms are the other hand are intended to be static for an entire draw call, or potentially for the entire frame or life of the program. They are set with glUniform* commands. A uniform might be something like the modelview matrix for an object, or the position of the sun for a lighting calculation. They don't change very often.

[edit] These specific commands I think actually work with GLSL, but the theory should be the same for CG. Lookup a cg specific tutorial to figure out the exact commands to set varyings and uniforms.

covMap1 - splat6 are textures. Are these parameters or something loaded into the graphics card memory (like globals)? The ogre definition for the shader program doesn't list them as parameters.

My CG is a little rusty, but if its the same as GLSL then the sampler2d is a uniform that takes an index that represent which sampler you want to sample from. When you do something like glActiveTexture(GL_TEXTURE3), glBindTexture(n), then you set the sampler uniform to "3" you can sample from that texture.

Does the order of declaration mean anything when sending values from an external program?

No, the variables are referred to in the external program by their string variable names.

If I don't pass one of the parameters will the shader just not be executed (and my object will be invisible because it has no texture)?

Unknown. They will likely have "some" initial value, though whether that makes any sense and will generate anything visible is hard to guess.

Is there a better way to debug these than just hit/miss and guess?

http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_b.html See section B.3.8

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top