I see. It looks interesting, since it looks like I will be able to do things like write and read from a secondary buffer, which I've wanted to do for a while.
I was confused though, since looking in the directory I didn't see any actual shaders, just JSON files. And yet it still ran, leading me to believe the shaders were built into mame somehow.
Is there any documentation or tutorial on how to use your own shader with bgfx?
I don't know about any tutorial. But if you look in:
you'll find the shader source.
You really need to write three sets of files:
Shader Source files: In a subfolder of /mame/src/osd/modules/render/bgfx/shaders/chains -- This is where you write your shader source for each shader.
Effect files: In a subfolder of /mame/bgfx/effects -- These are *.json files that define the uniforms for each shader effect as well as the name of the compiled shader.
Shader Chain file: In a subfolder of /mame/bgfx/chains -- This the the user visible "Shader Chain". It's another json file that defines all of your render targets and shader passes. In addition, there are a bunch of fancy things you can do here. You can set up sliders so users can change parameters from the user interface. You can have an effect take input from any previous effect if you haven't overwritten the target. You have access to the current and previous emulated frames. There's really a ton of stuff you can do with this system that you could never do before.
So what you have to do looks something like:
- You write the vertex and fragment shaders in the subfolder of "/mame/src/osd/modules/render/bgfx/shaders/chains" that you create. There are plenty of simple shaders to look at to see how things are done. Like the color, defocus, etc in the "hlsl" shader chain folder. Don't forget the "varying.def.sc".
- Then you need to compile your shader using "make shaders" from the root of the mame source. note: I can't currently compile shaders in windows and I still haven't figured out what I broke... Hopefully you'll do better.
- Then you need to drop the compiled shaders (they'll be ".bin" extension) to the mame/bgfx/shaders/TYPE/chains/yourshader folder. Where "TYPE" is dx9, dx11, gles, glsl, metal.
- Then for each individual shader effect you need a *.json file in mame/bgfx/effects/yourshader. You can copy/paste most of it. You mostly need to include the uniforms you're using and the correct names of the vertex and fragment shaders. The shader names need to match the *.bin files from the previous step.
- Finally, you can create a "Shader Chain" file. This is another *.json file but in the folde /mame/bgfx/chains/yourshader. This is the file that will define up your render targets and set the inputs/outputs/uniforms for all your shader passes.
It's a lot more work than writing the glsl shaders. But you can do a lot
more with it.