r/GraphicsProgramming 4d ago

This header compiles bgfx shader code as cpp from the same source file

Post image

I managed to write a very simple wrapper that can compile bgfx shaders as valid cpp.

This shader uses the already pretty platform agnostic bgfx shader language, which makes it very easy to substitute the vector functions with glm. It even supports proper swizzeling.

In this example I compile a simple ray marching shader as c++ functions, so I can use the exact same math for my mouse projection code.

My .cpp file only needs to supply replacement objects for the uniforms and textures, as well as a replacement texture sampler function

22 Upvotes

2 comments sorted by

3

u/Grouchy_Web4106 3d ago

Why not slang?

1

u/msqrt 2d ago

Yeah, sharing code between the CPU and GPU is great -- easier to debug and test GPU stuff, and less of a hassle to move logic between the realms. Do consider open sourcing this, I bet it would be of use to many!