简体   繁体   English

在 Shader 之外访问 Function

[英]Access Function Outside of Shader

I'm attempting to develop a game using OpenGL that renders a scene via a raymarching algorithm in a fragment shader.我正在尝试使用 OpenGL 开发一款游戏,该游戏通过片段着色器中的 raymarching 算法渲染场景。 I intend to implement collision using the same signed distance field function that was used to render the scene.我打算使用用于渲染场景的相同有符号距离场 function 来实现碰撞。

What I'm trying to figure out is if there's a way to use the same SDF function for both the collision test and the shader without having to write it twice.我想弄清楚的是,是否有一种方法可以在碰撞测试和着色器中使用相同的 SDF function 而无需编写两次。

Is there some way that I could either access a c++ function inside the frag shader or get the output of a compute shader to use for the collision test?有什么方法可以访问碎片着色器中的 c++ function 或获取计算着色器的 output 以用于碰撞测试?

Setting up an entire separate rendering pipeline just to call a function from a compute shader sounds very inconvenient.设置一个完整的单独渲染管道只是为了从计算着色器调用 function 听起来很不方便。 Is there a simpler way to accomplish this?有没有更简单的方法来实现这一点?

As long as you want to use raw OpenGL and GLSL,or any other low level rendering API, you will have to port your code to the shading language used by that API.只要您想使用原始 OpenGL 和 GLSL,或任何其他低级渲染 API,您就必须将代码移植到该 API 使用的着色语言。 If you want to have that code portable, you should seek for higher level frameworks/engines/cross-compilers.如果您想让该代码具有可移植性,您应该寻求更高级别的框架/引擎/交叉编译器。 Here is a one , for example, that allows writing shaders in C++.例如,这里有一个允许在 C++ 中编写着色器。 Also, keep in mind that the logic of your C++ code may need modification as well, because shaders execution model is massively concurrent: Vertex shader executes per vertex, fragment shader - per pixel, compute - per thread.另外,请记住,C++ 代码的逻辑也可能需要修改,因为着色器执行 model 是大规模并发的:顶点着色器按顶点执行,片段着色器 - 每个像素,计算 - 每个线程。 The executions themselves run in parallel( not all at once but as many as the underlying hardware specs allow, see this article to learn how it work on Nvidia GPUs) Serial algorithms perform extremely bad on GPU.执行本身是并行运行的(不是一次全部运行,而是在底层硬件规格允许的范围内,请参阅这篇文章以了解它如何在 Nvidia GPU 上工作)串行算法在 GPU 上的性能非常差。

With heavy use of the C preprocessor ( #if , #define ) you can write GLSL code which also compiles as C or C++.通过大量使用#define预处理器( #if 、#define),您可以编写 GLSL 代码,该代码也可以编译为 C 或 C++。 I'm not sure if this is to be recommended in general, but it is an option.我不确定这是否是一般推荐的,但它是一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM