Tool [tML/Standalone/...] FNAGLSL: GLSL shaders in FNA

PoroCYon

Terrarian
Introduction

Got enough of the limitations imposed by Shader Model 2.0? Today I present to you the solution: GLSL shaders! GLSL shaders have no annoying instruction limit, and are less picky about syntax.

As FNA uses OpenGL under the hood (and uses MojoShader to translate HLSL shaders into GLSL), it's technically possible to use GLSL shaders in Terraria. So I made this possibility into reality.

2020-06-27-212720_1902x1024_scrot.png


Here is a video, it's too large to upload here.

Details (explanation, download, usage, etc.)

It works by directly calling the necessary OpenGL functions, bypassing FNA. Because of that, it's not very stable, and I haven't had the chance to test it much. (But it kinda seems to work?)

You can get the source code here, it comes with a full example. If you want to use this in tModLoader, include the source code in your mod, or compile the DLL and use it as a dllReference. Basic usage boils down to the following code, though:

Code:
var glsl = new GLSLEffect(GraphicsDevice, new Dictionary<GLSLPurpose, string>()
{
    { GLSLPurpose.  VertexShader, "vertex shader here"   },
    { GLSLPurpose.FragmentShader, "fragment shader here" },
});

// ...

glsl.Parameters["paramname"].SetValue(xyz);

using (var bound = glsl.Bind())
{
    GraphicsDevice.DrawPrimitives(/*...*/);
}

FAQ

How can I use this to make fancy graphics?

You can check out the example code included in the repository (testmod-tML, and src/test.cs for standalone FNA projects), but I'm not going to provide step-by-step tutorials: shaders are a powerful but advanced concept that you better learn the necessary required skills first, and secondly, this codebase isn't too stable, so I don't want to encourage its use too much. So if you don't know how to use HLSL shaders, don't try this before grasping those first.

It doesn't work!

That's very well possible. I've only tested it on my own machines (one oldish laptop, and one OLD laptop), both running Linux with Mesa drivers, so I have no idea how it performs on Windows or OS X. Secondly, the default tModLoader build for Windows uses XNA (which uses DirectX9 and not OpenGL) instead of FNA, so if you run Windows, you must use the 64-bit tModLoader build in order to use this code! You may also need to set some FNA-related environment variables in order to make it choose an appropriate GL context, but hopefully the default should be fine (it is for me, at least), although it may have issues with the FORCE_CORE_PROFILE variable, depending on the drivers.

Honesty, if you want to use this in a "real" mod, I'd first experiement or wait for other people to experiment to see how it works for multiple people, before using it for anything serious.

How do GLSL shaders work? Can you please help me debugging an issue in my shader?

If things aren't displaying, or you have a syntax error in your shader, or you don't know how to begin writing one: please don't contact me for this, you're on your own. GLSL is well documented on the official OpenGL website, and there are many tutorials available: here, here, on ShaderToy, etc., and you can use ShaderToy or this tool called Bonzomatic to prototype shaders. So please look elsewhere instead of bugging me.

That's it, you reached the end.

P.S. I'm back.
 
Last edited:
Back
Top Bottom