Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: How to include GLSL code in a release product

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    14

    How to include GLSL code in a release product

    I'm looking for a good way to include GLSL code in our C/C++ application. In the examples I've seen, the code is included in separate .glsl files. It would be inappropriate to include glsl files with our application. I've seen other examples where the code is encoded directly in C strings like this:

    Code :
    const char *pVertexProgram110 =
      "varying vec2 vTexCoord;\n"
      " \n"
      "void main() {\n"
      "  vTexCoord   = gl_MultiTexCoord0.st; \n"
      "  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
      "}\n";

    This doesn't seem very elegant. I was hoping to have each GLSL program in its own file. Does anyone know of a good way to include GLSL code in a C/C++ application without releasing the .glsl files to the end user?

  2. #2
    Member Regular Contributor malexander's Avatar
    Join Date
    Aug 2009
    Location
    Ontario
    Posts
    302
    Use separate files and encrypt them with a text encryption library, perhaps? You could also roll your own encryption/decryption scheme using a key, depending on how secure you want the files to be. Even if you were to embed the shaders in the application itself, a tool as simple as unix "strings" can be used to extract them.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,101
    Yes encryption is the way to go - if you have a lot of shaders have a look at glsw - OpenGl Shader Wrangler - at the Little Grasshopper

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    You can do any of the following

    • include the strings as you yourself posted above but which is possible in multiple fashions
      • define a const char* the way you already showed above
      • use the pre-processor's stringification functionality
      • if you're gonna go to C++11 any time soon, use raw string literals, which will let you write the source code like you were writing a normal text

    • include the source code as text
    • map ASCII characters to some other value so you can't read the text without knowing the mapping and some function to unscramble the text - this way you can also produce seemingly obfuscated binary files
    • go full blown encryption like malexander and tonyo suggested


    The first option is common at least for small projects and even for larger applications this is a viable solution for instance for fallback shaders of some kind. Also, you can guarantee that basic functionality expressed with shaders, like a tone-mapping algorithm or something, is always available with your application. This is cool if you have a stock set of features your application/library (think 3D engine etc.) is supposed to provide but may be overridden by the user/developer. The major drawbacks with pre-C++11 techniques is the tedious authoring and stupid compiler-dependent quirks and, first and foremost, increased executable size. It will also increase start-up time due to increased amounts of data having to be pushed into memory before actual execution.

    The second option, which is basically open-source GLSL, is fine for most cases, even for larger projects if you keep your stuff together and don't have users stupid enough to temper with your distributed files - or mean users trying to steal your stuff and sell it as your own. Side-effects include accidentally missing files, encoding or line-ending issues and some people may steal your work and claim it as their own - but that's a risk to take when doing open-source stuff.

    The third option is cool for simple obfuscation but also increases your implementation's complexity and shader loading times. And it also still has the problems with files having to be consistent between the distributed files and the files on your development machine. Also, since this is just a simple mapping, it's not too hard to "decrypt". However, it sufficiently scare most people away so they don't mess with the code.

    The fourth option is far superior in terms of securing your code but it will probably introduce a third party dependency to en- and decrypt your code. This will also increase loading times and may lead to consistency problems.

    Personally I'd go with


    • fallback and minimally guaranteed shaders included in the executable - this will get more sweet when using C++11
    • the rest in human readable text files

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Apr 2009
    Posts
    578
    There is 0 point in encrypting one's shaders: this is because one sends the shaders as plain text to the GL implementation, and there are GL interceptors (and in truth it is not rocket science to make one either). As a side note there are GOOD end-user uses for GL interceptors.

    On the other hand you can obsuficate the shaders and ship those.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •