Embedding cg shaders in C++ GPGPU library

笑着哭i 提交于 2019-12-11 09:52:35

问题


I'm writing a GPGPU Fluid simulation, which runs using C++/OpenGL/Cg. At the moment, the library requires that the user specify a path to the shaders, which is will then read it from.

I'm finding it extremely annoying to have to specify that in my own projects and testing, so I want to make the shader contents linked in with the rest.

Ideally, my .cg files would still be browsable seperately, but a post-build step or pre-processor directive would include it in the source when required.

To make things slightly more annoying, I have a "utils" shader file, which contains functions that are shared among things (like converting 3d texture coords to the 2d atlas equivalent).

I'd like a solution that's cross platform if possible, but it's not so big a deal, as it is currently windows-only. My searches have only really turned up objcopy for linux, but using that for windows is less than ideal.

If it helps, the project is available at http://code.google.com/p/fluidic


回答1:


You mean you want the shaders embedded as strings in your binary? I'm not aware of any cross-platform tools/libraries to do that, which isn't that surprising because the binaries will be different formats.

For Windows it sounds like you want to store them as a string resource. You can then read the string using LoadString(). Here's how to add them, but it doesn't look like you can link them to a file.

A particularly hacky but cross-platform solution might be to write a script to convert your shaders into a string constant in a header. Then you can just #include it in your code.

I.e. you have the file myshader.shader which contains:

int aFunction(int i, int j)
{
   return i/j;
}

And you have a build step that creates the file myshader.shader.h which looks like:

const char[] MYSHADER_SHADER =
"int aFunction(int i, int j)"
"{"
"   return i/j;"
"}";

Then add #include "myshader.shader.h" to your code.

Very hacky, but I see no reason why it wouldn't work (except for maybe length/space limits on string literals).

Update: With the release of G++ 4.5 it supports C++0x raw string literals. These can contain new lines 4. I haven't tested it but you should be able to do something like this:

const char[] MY_SHADER = R"qazwsx[
#include "my_shader.c"
]qazwsx";

I haven't tested it though.



来源:https://stackoverflow.com/questions/2485503/embedding-cg-shaders-in-c-gpgpu-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!