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

.cso in .lib

Started by matt77hias Mar 6, 2017 at 7:35 PM 7 replies 3.4k views
Original Post
matt77hias
matt77hias

Is it possible to include .cso files (i.e. compiled shaders) in a static C++ library?

🧙
Ryan_001
Ryan_001

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.

matt77hias
matt77hias

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?

🧙
vstrakh
vstrakh

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.

Zaoshi Kaba
Zaoshi Kaba

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]

Ryan_001
Ryan_001

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?

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.

matt77hias
matt77hias



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")?
🧙
Zaoshi Kaba
Zaoshi Kaba
  • 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]
Fredericvo
Fredericvo
How about the resource compiler instead? I use that to embed binary data that I don't want as separate files. Simple and flexible without extra plugins or build tools.

Topic Locked

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

Sign in to reply to this topic.