Question

I know little to nothing about the actual writing of shaders although I have a basic understanding of their place in the rendering pipeline. I have a CG shader that I have aquired from here and here. I am attempting to convert these shaders to GLSL. I have gone through the reference docs and found the languages to be very similar and converted all the bits that were obvious based on the methods and syntax available. I am having some issues with default values on parameters however. Based on this webpage the line:

float4 iPos     : POSITION,

, means it is setting the default value via a "Semantic". This is where I am starting to get lost. The same thing doesn't appear to be available in GLSL unless I am missing something. I don't understand what the closest parallel is in GLSL or what it means for the conversion effort. How do I properly account for this when converting the shader?

Was it helpful?

Solution

In Cg semantics are a way to alias attributes and varyings with certain built-in fixed-function attributes and varyings. A Cg attribute is first and foremost just a generic vertex attribute without any semantic. But in OpenGL (well, only in old and deprecated OpenGL) you have certain fixed-function attributes that carry, well, a semantic. E.g. the vertex position is set with glVertex or glVertexPointer, while a vertex's normal is set with glNormal or glNormalPointer. By giving a previously plain generic attribute the semantic POSITION or NORMAL in Cg, you can make it source it's values from such a fixed-function attribute channel.

In GLSL there are in turn built-in vertex attribute variables that contain the values of the deprecated fixed-function attributes, e.g. gl_Vertex, gl_Normal, ... for the position, normal and other semantics. But like said those fixed-function attribute channels are actually deprecated and their use in modern OpenGL is discouraged (or even prohibited in a core profile). What you should instead use is the generic vertex attributes, set with glVertexAttrib and glVertexAttribPointer. Those don't carry an actual semantic and only get their semantic by the shader using them in a particular way. But if you need to port an older application over from Cg to GLSL, then using the deprecated builtin attributes like gl_Vertex or gl_Normal and the like is the best subsitute for Cg's attribute semantics like POSITION or NORMAL.

In the same way there are certain varyings that carry a special meaning and need to be treated specially, like the clip-space position (semantic POSITION?). Those correspond to special varying variables you can write to in the vertex shader, like gl_Position in this case. And in deprecated OpenGL there are also other varying variables that can be written to and that correspond to certain fixed-function varyings, like gl_TexCoord[i] and the TEXCOORDi semantic. When using those you can easily interface shaders with fixed-function stages and e.g. use a vertex shader but fixed-function fragment processing.

So to sum up those Cg semantics actually correspond to special pre-defined attribute and varying variables in GLSL. But in modern OpenGL all of those special vertex attributes and most of those special varying variables don't exist anymore. But I advise you to first delve a little deeper into how GLSL and Cg works in practice, since as you see porting between those two is not as simple as subsitituting some keywords.

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