在 C++ 中嵌入 cg 着色器GPGPU库

发布于 2024-08-25 13:01:33 字数 521 浏览 6 评论 0原文

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

我发现必须在我自己的项目和测试中指定这一点非常烦人,因此我希望将着色器内容与其余内容链接起来。

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

为了让事情变得更烦人,我有一个“utils”着色器文件,其中包含在事物之间共享的函数(例如将 3d 纹理坐标转换为等效的 2d 图集)。

如果可能的话,我想要一个跨平台的解决方案,但这没什么大不了的,因为它目前仅适用于 Windows。我的搜索只真正找到了适用于 Linux 的 objcopy,但在 Windows 上使用它并不理想。

如果有帮助,请访问 http://code.google.com/p/fluidic

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-09-01 13:01:34

您的意思是您希望将着色器作为字符串嵌入到您的二进制文件中?我不知道有任何跨平台工具/库可以做到这一点,这并不奇怪,因为二进制文件的格式不同。

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

一个特别hacky但跨平台的解决方案可能是编写一个脚本将着色器转换为标头中的字符串常量。然后你可以在你的代码中#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;"
"}";

然后添加 #将“myshader.shader.h”包含到您的代码中。

非常老套,但我看不出它为什么不起作用(除了字符串文字的长度/空间限制)。

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

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

不过我还没有测试过。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文