Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Is it possible to store shaders in a binary format?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 MarkS   Prime Members   -  Reputation: 1077

Like
0Likes
Like

Posted 15 December 2011 - 06:32 PM

I would like my shaders to be non human-readable. I'm not concerned about IP issues, but I simply don't want the user messing with the code. The only options I can think of is to either hard code the shaders into the executable or encrypt the files. Neither option looks all that appealing.

Is this possible and how would I go about it? Can shaders be precompiled?

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 36314

Like
0Likes
Like

Posted 15 December 2011 - 06:48 PM

This is a long running flaw in GLSL... The newer versions do allow for binary shaders, but it's more of a caching mechanism than a pre-compilation mechanism -- they're only compiled for the current GPU/driver combo, and can't be reused on another PC.

I'd personally just put them in a ZIP-like file like you do for all your other assets Posted Image. I assume you don't want people editing your geometry files either, so you can use the same protection mechanism on all asset types to detect if they've been modified from their shipping versions.

#3 MarkS   Prime Members   -  Reputation: 1077

Like
0Likes
Like

Posted 15 December 2011 - 07:03 PM

Posted Image

I was afraid of that.

#4 dpadam450   Members   -  Reputation: 1149

Like
1Likes
Like

Posted 16 December 2011 - 12:11 AM

Since shaders are not a security issue, why not just do something simple like subtract 1 from each character in your file? Nobody is going to open that and try to crack into a shader, so something that simple should be no problem. I doubt they would fiddle with a file that has random characters in it. Then come load time you just add 1.

Even then, if you just put them into a data folder and give them an extension like .gamedata, then most people are going to leave that alone since its not a ".txt" or any other file they know of. I don't know the last time I tried to open any .data files for any game that I bought so you shouldn't be worried. Anyone that goes that deep to mess up their game will probably save a copy of the file so it does not corrupt their game and can revert back to the original file.

#5 samoth   Crossbones+   -  Reputation: 5369

Like
0Likes
Like

Posted 16 December 2011 - 03:33 AM

You could use Shader Minifier which as the name suggests minimizes your shaders, making their size smaller and at the same time making them pretty much unreadable without a lot of work. Add to that some basic encryption, and you're as good as you can go. The encryption is a bit futile insofar as the shader source is plaintext when being sent to OpenGL, so tools like gDebugger can trivially record it. It's not intended any different either, sadly.

Do note that Shader Minifier raises a Trojan/Rootkit warning with my AV software and gets a score of 4/42 on Virustotal (I don't know if these are false positives or not (it looks like a false positive, but better be on the safe side and run inside a virtual machine).

OpenCL is a bit better, at least in theory, as saving compiled kernels in some kind of meta format is required core functionality. So much for the theory. In practice it seems you're not even guaranteed that it works with every implementor, or that there is a meta-format at all. AMD announced "supported now" in May -- which means no more and no less than it did not work before. Plus, there are about 3 screen pages of instructions, which seems a lot of work just to save some binary data. nVidia doesn't even mention, so who knows what works and how well.

#6 samoth   Crossbones+   -  Reputation: 5369

Like
0Likes
Like

Posted 16 December 2011 - 03:40 AM

Since shaders are not a security issue


Shaders are certainly a security issue, in the same way as for example textures. This is not so much about "someone stealing your valuable intellectual property". It's not as valuable as most people think, and most of it is freely available on the net anyway. It's a different issue.
If you can read and modify a shader, you can for example implement a wall hack or something similar. This could be a serious issue in a competitive game where 2% of the players head-shoot 98% of the players every time they sneak around a corner, even when nobody could possibly have seen them.

I'm not even going as far as coming up with crazy ideas like malicious shaders/kernels that might cause deadlocks in the graphics hardware hardware (though surely that's possible too).

#7 PolyVox   Members   -  Reputation: 708

Like
0Likes
Like

Posted 16 December 2011 - 07:07 AM

OpenCL is a bit better, at least in theory, as saving compiled kernels in some kind of meta format is required core functionality. So much for the theory. In practice it seems you're not even guaranteed that it works with every implementor, or that there is a meta-format at all. AMD announced "supported now" in May -- which means no more and no less than it did not work before. Plus, there are about 3 screen pages of instructions, which seems a lot of work just to save some binary data. nVidia doesn't even mention, so who knows what works and how well.


Can you provide any links to this? What's the name of the functionality? I'm just getting into OpenCL and din't realise it was any different to OpenGL in this regard. I'm going to be writing some auto-parallelization code and having a byte-code like format could be very useful.

#8 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 17 December 2011 - 07:34 AM

I guess he is talking about clCreateProgramWithBinary()
http://forums.nvidia.com/index.php?showtopic=206598
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#9 Katie   Members   -  Reputation: 1423

Like
0Likes
Like

Posted 17 December 2011 - 10:52 AM

"If you can read and modify a shader, you can for example implement a wall hack or something similar."

How will compiled ones prevent this? They must be able to be decompiled (ie; the format must be well known) because drivers must be able to translate them into their own machine-code format exactly by decompiling and translating them. Hence they are readable.

And since the tools to compile them will be available, then people can write new ones. Hence they are modifiable. Binary format shaders will merely slow down a hack like this by possibly as long as minutes.

**Signing** shaders might help prevent some of this stuff, but then you're into a whole world of horridity.

#10 MarkS   Prime Members   -  Reputation: 1077

Like
0Likes
Like

Posted 17 December 2011 - 11:08 AM

Since shaders are not a security issue, why not just do something simple like subtract 1 from each character in your file? Nobody is going to open that and try to crack into a shader, so something that simple should be no problem. I doubt they would fiddle with a file that has random characters in it. Then come load time you just add 1.


That actually sounds like the best idea yet. Simple, effective and quick to implement.

#11 zmippie   Members   -  Reputation: 100

Like
0Likes
Like

Posted 20 December 2011 - 04:53 PM

That actually sounds like the best idea yet. Simple, effective and quick to implement.


Search for ROT13 mangling. All but a safe encryption scheme, but it sounds as if your audience just needs a little discouragement.

#12 mhagain   Crossbones+   -  Reputation: 8986

Like
2Likes
Like

Posted 20 December 2011 - 05:21 PM

Alternatively, stuff them into TGAs. Construct a TGA header with the correct properties, and each byte in the shader source code becomes a byte in the TGA data. To the casual meddler it will look like some kind of noise texture you're using for something (and if they try to change that the program will break on them, so they'll shy away from it after the first attempt).

Edit to add: it occurs to me that you may want to semi-obfuscate the error message your program gives if a shader fails to compile, of course.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#13 FXACE   Members   -  Reputation: 182

Like
0Likes
Like

Posted 21 December 2011 - 02:29 AM

Nice discussion: "How to hide codes of shaders from user's eyes". Also, things like to hold them encrypted in files or something like that. But you forgot about OpenGL sniffers (GLIntercept, for example). Libraries like this replacing functions ("glShaderSource") and as how you would encrypt your code you would always need to decode it for OpenGL. And as result this library would give to user all your GLSL code as how it is (from anywhere).




If you want to protect your idea construct its algorithm which would be misunderstanding for others (for example: without any comments; no accurate code; "int jhfgdjfgjhg; vec4 __04129adasuh8hfasf215;"). Of course, do this when your code completing works correct.




+ clCreateProgramWithBinary():

Do not think that code (which is generated by OpenCL compler) is really "binary"... It just becomes from C++ style language to GPU Asm style language (which can be read by human too).




Best wishes, FXACE.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS