Zac Heimgartner
Zac Heimgartner

Reputation: 1

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. I intend to implement collision using the same signed distance field function that was used to render the scene.

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.

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?

Setting up an entire separate rendering pipeline just to call a function from a compute shader sounds very inconvenient. Is there a simpler way to accomplish this?

Upvotes: 0

Views: 204

Answers (2)

mystery
mystery

Reputation: 19523

With heavy use of the C preprocessor (#if, #define) you can write GLSL code which also compiles as C or C++. I'm not sure if this is to be recommended in general, but it is an option.

Upvotes: 0

Michael IV
Michael IV

Reputation: 11496

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. 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++. 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. 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.

Upvotes: 0

Related Questions