0
\$\begingroup\$

Right now for my engine, I rely on pixel and vertex shader script text files that I load and compile when I need them.

What I am wondering is, how can I store the shader scripts so that they are not visible to the user?

Maybe I could hard code them inside the executable, then dump them to .ps and .vs files, compile them, and then delete those files?

Or I could place them in binary files along, say, level or model data, and then generated/compiled in that same way?

Or would I pack them as text files inside a proprietary compressed file format with all the other resources, that is uncompressed and loaded as needed?

I'd like to avoid storing them in a way that's hackable or easy to reverse engineer.

\$\endgroup\$
2
  • \$\begingroup\$ The blobs generated by D3DCompile are hardware-independent and may be shipped. Create*Shader is all that's then needed. I.e. With D3D shaders one doesn't need to shop the HLSL source. \$\endgroup\$ Sep 8, 2018 at 20:56
  • \$\begingroup\$ Hi Hermetix. Generally we can't answer questions of the form "what does Game X do?" (Since most of us didn't work on Game X, and those who did may be under NDAs) or "what do games of class Y do?" (For all the same reasons, plus the fact that different games might do it in many different ways). But if you want to know what you can do in your game, that's absolutely something we can help with. I've edited your question to ask about solving the problem in your game so it's on-topic here. \$\endgroup\$
    – DMGregory
    Sep 8, 2018 at 21:01

1 Answer 1

1
\$\begingroup\$

There are multiple ways the shader scripts can be hidden from the user - the graphics api used doesn't matter much.

The shader files could

  • be added in some obscure subfolder as plaintext files. Most non-power users won't bother searching around. Most power users won't be stopped by this and most other techniques I'm going to explain.
  • just be renamed with another extension so they're not identified as textfiles by the operating system. Anyone could still open them in a text editor though.
  • be included in a header/source file as a string/literal/character array. The shader gets compiled into the program and is hidden in the executable file (can still be seen in a hex editor). The shaders would then get loaded from the variable in program memory instead of from a file.
  • compressed into an archive file. Some games use compressed files (e.g. *.zip, or the *.pak files of doom/quake) to collect multiple resources into one file. The compressed files are opened and uncompressed by the executable and get used as a virtual file system, from which the shaders are loaded.
  • added into resource files, e.g. some engines store the shader source as part of the asset metadata.
  • transformed into an intermediate format. Many graphics apis provide a way to compile shaders into binary files, e.g. using fxc.exe on directx or SPIR V on opengl/vulkan. Since the binary formats are standardized, reverse engineering is still possible.

Of course, any of the above can be combined with cryptographic methods. A simple scheme like ROT13 or BASE64 can be used to encrypt the shader source, which will make the shaders harder to find and keep out the script kiddies, but won't deter anyone really serious about finding out how the rendering works. Harder encryption schemes would be getting into cryptography.stackexchange.com territory though. Due to the time needed to decrypt the files, this approach isn't used often.

Also something to consider: Making it harder to modify the games asset and shader files also makes it harder to mod the game. Which can be a good or bad thing.

Most rendering techniques are available in research papers and/or other sources one way or another, so many games just throw shaders into an asset file or folder and focus on providing a good gameplay experience.

\$\endgroup\$
1
  • \$\begingroup\$ Just pointing out that Microsoft requires all shaders to be in a compiled form before being deployed with an application on the Microsoft Store so leaving them in plain-text is not preferable even if the game is designed to be modded. (I've never seen games allow you to mod a shader anyway, kinda pointless to do so) \$\endgroup\$
    – Casey
    Sep 9, 2018 at 5:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .