|
There are risks associated with releasing a program that uses what I call an "open shader". An open shader is a disclosed shader file that is used by a program. Because your clients can read and change the file content, your program must handle whatever change is made to the file. It must also handle the case when the shader file is delete or misplaced. If your program fails when your clients make an error in the shader file (and they will), then you have a serious problem. Your program security and reliability is compromised. It doesn't matter that your client did a mistake when she tried to assign a vector4 to a vector3 in the shader code! If you have ever written a shader then you knew this was bound to happen. I do this type of mistake many times in the course of developing a shader. But it just feels wrong when your program fails because it couldn't handle an incorrect input.
How do you release a program with open shader files? Simple! Implement a fallback shader! When your program fail to compile a shader then redirect it to use another shader that you know "can never fail" (in theory!). That shader should never be open to your clients. It should remain undisclosed so no one can easily temper with it. And the best way to do that is to embed it in your program executable.
In my programs, I have a shader embedded with the source code. It looks like this.
TCHAR gPinkShader[] = TEXT( "[Vertex Shader] \n\ #version 110 \n\ \n\ uniform mat4 ObjectMatrix; \n\ uniform mat4 CameraMatrix; \n\ uniform mat4 ProjectionMatrix; \n\ \n\ void main() \n\ { \n\ gl_Position = ProjectionMatrix * CameraMatrix * ObjectMatrix * gl_Vertex; \n\ } \n\ \n\ [Fragment Shader] \n\ #version 110 \n\ \n\ void main() \n\ { \n\ gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0); \n\ }");
What this shader does is, it render every fragment with a solid pink color. Truth is, the color is more like magenta but I believe "Pink Shader" is more telling. When I see this color in my rendering, chances are, there is something wrong with the shader (in addition, it doesn't hurt to display a message telling you the shader is wrong... something I don't do yet). So, be careful or you too might see pink when you modify the shader file of a program from inalogic!
 |