Skip to main content
GameDev.net gamedev.net
🔒 Locked

HLSL code hiding

Started by johnsss May 2, 2006 at 9:02 AM 7 replies 2.3k views
Original Post
johnsss
johnsss
Hi everyone. First of all, sorry for the unclear subject. Didn't know what title I should choose for my question. My question is that is there any way to hide the effect files (.fx) (embedd it in the application) so that when I give someone the release version, he/she can't see and change the effect file? Currently I use CreateEffectFromFile () which requires the effect file to be in the specified directory, hence, I need to include the effect file with the .exe and dlls. However, for some reason, I do not want to publish the effect file (it's not a ground breaking, outstanding, effect but still... I don't want to release it as a simple text file). I looked around in the net and found CreateEffectFromResourse () which can be the very function I am looking for. But I don't know how to use this function and google is not helping me much with it. Thus I would really appreciate if someone could point me to the right direction or perhaps show me an example on how this can be done. (If you provide an example, please tell me every step). Thank you very much in advance. Johnsss
clb
clb
If you're using Visual studio, you can add resources to your project by right-clicking on the project -> add -> resource.. then select your .fx file. (I think the resource type can be either binary or text)

To get access to the resource linked to your EXE, include the resource.h that has now been created, and use MAKEINTRESOURCE(ID_MYRESOURCE) to get the string handle for the resource, and pass it as the pSrcResource parameter.
DarthJappie
DarthJappie
Or you could create a small tool that will package your resources (it could even encrypt and compress them) into a file, that can be unpacked by the application.
----------------------------- Jan-Jaap Severs Grendel Games[ Grendel Games ]
jpetrie
jpetrie
Additionally, you can use the D3DXEffectCompiler interface to precompile your shader and spit out binary data and ship that instead.
jollyjeffers
jollyjeffers
Quote:
Original post by jpetrie
Additionally, you can use the D3DXEffectCompiler interface to precompile your shader and spit out binary data and ship that instead.
This is the more common (and probably better) alternative.

If using VStudio you can keep your ".fx" files as part of your project/solution and generate a "custom build step" to compile it at the same time as your source code; then just use the compiled/output filename as your input when creating the effect from your code. I found this system to work quite nicely for me...

Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
johnsss
johnsss
Thank you all very much for the quick responses.

I tried the first way mentioned yesterday (making resource inside VStudio and loading it from the resourse) but for some unexplained reasons, this didn't work.

I then tried the D3DXCreateEffectCompilerFromFile () which kind of works but does not inclusd the ->SetTechnique() function. This is bad for me because I'm using several techniques (and passes) inside the effect file.

Here's what I used to do:

        LPD3DXEFFECT g_pEffect;	HRESULT hr = D3DXCreateEffectCompilerFromFile(		                           ".\\shaders\\xyz.fx",							   NULL,							   NULL,							   NULL,							  &g_pEffect,							  &pBufferErrors);


I then tried to change it to:

LPD3DXEFFECTCOMPILER g_pEffect;HRESULT hr = D3DXCreateEffectFromFile( device, 		                           ".\\shaders\\xyz.fx",		                           NULL, 		                           NULL, 		                           0, 		                           NULL, 		                           &g_pEffect, 		                           &pBufferErrors );


The first solution obviously works perfectly. The second one works as well (up to some extent) but as I mentioned, the compiled effect (from type: LPD3DXEFFECTCOMPILER as opposed to: LPD3DXEFFECT) does not include the SetTechnique() funciton.

I also tried other solutions such as D3DXCompileShaderFromFile () which didn't work either.

Please tell me what I'm doing wrong.

Again, thank you all very much for the great help.

p.s. to : if you have time can you please include a tutorial like explanation of that solution you mentioned? It seems like a very good way to go.
jpetrie
jpetrie
I only intended for you to use the effect compiler interface to generate binary files for shipping; you can continue to load those compiled files via the regular effect interface. jollyjeffers solution is better, however, unless perhaps you want to also package the compiled shaders up with a bunch of other binary data and ship one big package file (though you'd still write an external tool for that, presumably).
johnsss
johnsss
Beautiful :-) Finally got it working :)

For those who may want to know how to do this here's the solution:

If you are using the Visual Studio, right-click on the project name -> choose Configuration Properties -> Custom Build Step -> General.

In the right-side window, find "command line" and enter:

fxc.exe /Zi /T fx_2_0 /Fo xyz.fxo xyz.fx

This will compile the original shader code (.fx) into a binary file (.fxo or any extension you prefer).

You can then load this file instead of the ASCII .fx file inside your program and use it.

Don't know how to automatically move it into the release directory though and if you don't know either, just manually copy it to the release dir.

(the reason for this is that I am not too familiar with the Custom Build stuff and thus don't know much about using it).

Thanks alot to everyone who responded in such a fast manner. Really really appreciate it.
CrazyCdn
CrazyCdn
I believe the "custom build step" jollyjeffers is talking about is (in MS Visual Studio 2003, you'll find it in a simular area in 2k5 not sure about VC6 but you shouldn't be using that anyways):

Project -> "Project Name" properties (no quotes) -> Custom Build Step.

Here you can add in a batch file (.bat) that runs the HLSL compiler (never used it so don't know how) to make the text files into a binary format for you.

I'm sure google with a +msdn will help you figure it out if you need more help.

Edit: Well beaten but I'd use a batch file that moves them for you to a data directory (not release/debug directories).
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.