Question

I made a sprite renderer using DirectX and C++ and I would like to put that into a .lib file.

Is it possible to somehow put the two (compiled) shaders into the .lib so I don't have to include them everywhere? Right now I read them from disk at runtime which of course means that I have to put the two .cso files somewhere so they can be read.

The solution I came up with is to put them into the project as strings and then compile them at runtime but that's not as convenient...

Was it helpful?

Solution

How about creating a C or C++ file with 2 global arrays of unsigned char, and declared to have the size of one shader file each. Then initialize them with something like:

// let it start with a signature string not found anywhere else in the .obj file
// EXTRA is some extra bytes to account for alignment
unsigned char Shader1[SIZE_OF_SHADER_1+EXTRA] = "x2232323$$%**sswe";
unsigned char Shader2[SIZE_OF_SHADER_2+EXTRA] = "x2952378$@%**ssqw";

Then, in your code use these arrays as if they actually contain the code of the shaders. Finally, after compiling them into a lib, use a hex editor to find the starting strings, and paste there the entire shader contents. If you can't find a suitable hex editor, you can easily write a small utility to read a file, find a signature string in another, and then write the contents of the file it read into the location of the signature in the second.

Your library will then contain the shaders inside it. If you wrote this little utility as a console app, then you can call it in a post-build step to perform this 'injection' in the library.

OTHER TIPS

What version of Visual Studio are you using? VS2012 allows you to compile .hlsl files into .cso, at which point you can link it into your executable as data or whatnot:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509633(v=vs.85).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top