CGP/GLSL

As we have lately worked on adding our shader manager to RGUI, and refactored a lot of the shader systems, the gap between XML/GLSL shaders and CGP is now complete with very little code. This format should be treated as a compatibility format for GLES platforms, and I don’t expect it to be used much.

Cg does not run on all platforms, i.e. when EGL context is used, and GLES. In these cases, only GLSL (ES) works, and to avoid a lot of mess in the shader manager later on, CGP now has a sister format, GLSLP, which is exactly the same as CGP, except that source files are GLSL.

Since the GLSL source files generally need to be split into two, #ifdefs need to be employed. By sticking both vertex and fragment into one, duplicating “varying” declarations is not needed. Note that only the “modern” GLSL style is supported in this format.

We still recommend that shaders are targeted against Cg, as it is much simpler to convert Cg -> GLSL (see cg2xml script) than the opposite direction. The original Cg spec is also more widely supported across platforms, APIs and different projects.

I will probably add functionality to convert .cg to .glsl as well in the near future.

E.g. stock.shader


<?xml version="1.0" encoding="UTF-8"?>
<shader language="GLSL" style="GLES2">
   <vertex><![CDATA[
      attribute vec2 TexCoord;
      attribute vec2 VertexCoord;
      uniform mat4 MVPMatrix;
      varying vec2 tex;
      void main()
      {
         gl_Position = MVPMatrix * vec4(VertexCoord, 0.0, 1.0);
         tex = TexCoord;
      }
   ]]></vertex>
   <fragment><![CDATA[
      varying vec2 tex;
      uniform sampler2D Texture;
      void main()
      {
         gl_FragColor = texture2D(Texture, tex);
      }
   ]]></fragment>
</shader>

becomes

stock.glsl


      varying vec2 tex;
#if defined(VERTEX)
      attribute vec2 TexCoord;
      attribute vec2 VertexCoord;
      uniform mat4 MVPMatrix;

      void main()
      {
         gl_Position = MVPMatrix * vec4(VertexCoord, 0.0, 1.0);
         tex = TexCoord;
      }
#elif defined(FRAGMENT)
      uniform sampler2D Texture;
      void main()
      {
         gl_FragColor = texture2D(Texture, tex);
      }
#endif

2 Likes