Is it possible to include .cso files (i.e. compiled shaders) in a static C++ library?
.cso in .lib
I wrote a small Visual Studio plugin that compiles HLSL and GLSL files into their binary form, and then writes that out to a COFF file which is linked into the executable. I love the result, but it took some time as the MSBuild system is a little strange and its documentation terrible.
I wrote a small Visual Studio plugin that compiles HLSL and GLSL files into their binary form, and then writes that out to a COFF file which is linked into the executable. I love the result, but it took some time as the MSBuild system is a little strange and its documentation terrible.
And you can then read the blobs from the .lib in the .exe?
You don't need special plugins.
I'm using objcopy in linux, and objcopy from mingw-w64 package on windows.
It reads your binary data, generates object file to be included in the build or in the static lib (many supported formats and platforms), and defines two symbols, designating binary blob beginning address and its length. Accessing that data is no different than constant string.
I wrote a small Visual Studio plugin that compiles HLSL and GLSL files into their binary form, and then writes that out to a COFF file which is linked into the executable. I love the result, but it took some time as the MSBuild system is a little strange and its documentation terrible.
And you can then read the blobs from the .lib in the .exe?
Alternatively, HLSL compiler can output .h file (see picture) which contains array with compiled shader. It's just .cso file in inlined into array, so you load it exactly same way and if you need to access it outside .lib you'll have to write some function to return array + size.
[attachment=35208:Capture.PNG]
And you can then read the blobs from the .lib in the .exe?I wrote a small Visual Studio plugin that compiles HLSL and GLSL files into their binary form, and then writes that out to a COFF file which is linked into the executable. I love the result, but it took some time as the MSBuild system is a little strange and its documentation terrible.
Yup. They become just a global variable, so I define them at file scope, something like:
namespace SomeNamespace { extern const ShaderByteCode vertex_shader; }
As Zaoshi Kaba pointer out, you can output to a header file which you can then link in, its probably alot easier than rolling your own.
Alternatively, HLSL compiler can output .h file (see picture) which contains array with compiled shader. It's just .cso file in inlined into array, so you load it exactly same way and if you need to access it outside .lib you'll have to write some function to return array + size.
This works like a charm!
I still have 2 minor issues with the basic approach:
- Is it possible to let the header begin automatically with "pragma once"?
- Is it possible to have no textual references to my directory structure (i.e. #line 48 "C:\Users\Matthias\Documents\Visual Studio 2015\Projects\...\....fx")?
- HLSL compiler doesn't seem to provide any way to add extra code so not really. Since it's const and global you could include all shaders in a single .cpp file and create GetShader(...) method to retrieve data.
- It appears only in debug build for debugging reasons, so if you aren't interested in debugging shaders you might as well turn off debug stuff for shaders:
[attachment=35337:Capture.PNG]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.