在c ++ GPGPU库中嵌入cg着色器 [英] Embedding cg shaders in C++ GPGPU library

查看:297
本文介绍了在c ++ GPGPU库中嵌入cg着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个GPGPU流体模拟,它使用C ++ / OpenGL / Cg运行。目前,库要求用户指定着色器的路径,然后将从中读取。



我发现它非常讨厌指定在我自己的项目和测试,所以我想让着色器内容链接到其余。



理想情况下,我的.cg文件仍然可以单独浏览,但是后构建步骤或预处理器指令会在需要时将它包含在源代码中。



为了使事情稍微烦人,我有一个utils着色器文件,它包含了在事物之间共享的函数(例如将3d纹理坐标转换为2d地图集等价物)。



我想要一个跨平台的解决方案,它不是那么大的交易,因为它是目前只有Windows。我的搜索只是真的变成了 objcopy 对于linux,但使用它的窗口是不太理想。



如果有助于此项目,请访问 http://code.google.com/p/fluidic

解决方案

你的意思是你想要在二进制文件中嵌入字符串的着色器?我不知道任何跨平台工具/库,这是不奇怪,因为二进制文件将是不同的格式。



对于Windows它听起来例如您希望将其存储为字符串资源。然后,您可以使用 LoadString() 以下是添加的方法,但它不看起来像是可以将它们链接到一个文件。



一个特别复杂但跨平台的解决方案可能是写一个脚本来将你的着色器转换为字符串常量标题。然后,您可以在代码中<$ p $ c> #include 。



您有 myshader.shader 文件,其中包含:

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

您有一个创建文件 myshader.shader.h 其格式如下:

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

然后添加 #includemyshader.shader.h到你的代码。



很奇怪,但我看不到为什么它不工作(除了可能长度/字符串字面量的空间限制)。 / p>

更新:随着G ++ 4.5的发布,它支持C ++ 0x原始字符串文字。它们可以包含新行 4 。我没有测试它,但你应该能够做这样的事情:

  const char [] MY_SHADER = Rqazwsx [
#includemy_shader.c
] qazwsx;

我没有测试过。


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

解决方案

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.

这篇关于在c ++ GPGPU库中嵌入cg着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆