[SOLVED] Custom Effect includes

Hi,

I am developing a small terrain engine and I am making a custom effect to handle the multi-texturing of the terrain chunks. (Using a lightmap layer system)

Thus, I have started creating an effect and added it to the MonoGame Content Pipeline. Everything works great so far. But the effect file is starting to be quite long with a lot of parameters and stuff.

I tried to #include an .fxh file but the MonoGame Content Pipeline doesn’t really like it and fails.

There is my hlsl code:
Common.fxh

matrix World;
matrix View;
matrix Projection;
float3 CameraPosition;

matrix ComputeWorldViewProj(matrix world, matrix view, matrix proj)
{
	return mul(mul(world, view), proj);
}

Terrain.fx

#include "Common.fxh"

// ...
VertexShaderOutput MainVS(in VertexShaderInput input)
{
    matrix worldViewProj = ComputeWorldViewProj(World, View, Projection);
    // ...
    return ...;
}
// ...

Do I need to used another tool to build the shaders or is there a way to do it with the content pipeline tool ?

Thanks.

1 Like

Hi @Eastrall Welcome to the Community!

My understanding is, Shaders need to be platform specific, so, it might help to mention if this is a DirectX or OpenGL project or both…

Happy Coding!

Hi, thanks for the quick reply!

I am currently targeting DirectX, but in the feature I might want to use both, DirectX for Windows and OpenGL for a deployment on Linux and mobile.
In my shader I have already specified the VertexShader and PixelShader models:

#if OPENGL
	#define SV_POSITION POSITION
	#define VS_SHADERMODEL vs_3_0
	#define PS_SHADERMODEL ps_3_0
#else
	#define VS_SHADERMODEL vs_4_0_level_9_1
	#define PS_SHADERMODEL ps_4_0_level_9_1
#endif

#include "Common.fxh"

...

Don’t know if this helps.

1 Like

I’ve been able to do shader includes, and it’s pretty simple if feeling a bit janky.
So, my understanding of includes is that when the shader is compiled the includes are basically “pasted” where the include is. Includes are, themselves, not functioning shaders but just reusable code. They will always fail to compile.
I just don’t have shader includes in the .mgcb. I save them as normal .fx shaders and keep them in the same folder, but since they will fail to compile on their own leaving them out will fix the issue while not causing any compile issues for regular shaders.

Hope this helps, if anyone knows of a better way I’d love to know!

1 Like

@GoldenThumbs Thanks for the reply. I have excluded the “included” files from the Content Pipeline Tool and I can now include the files into my shader.

Thank you, problem solved. :slight_smile:

1 Like

Would like to add that you can easily create new .fx files from within Visual Studio, you just create a new .txt plain text file and change the extension to .fx. You can actually do this for pretty much every plain text format (including model formats like .obj and .gltf). Only real downside is you don’t get basic “starting point” code, but for shader includes you really don’t need that.

1 Like

Yes, that is exactly what I do. But instead of creating a .txt I create a .cs one and rename it. Works the same too. :wink:

1 Like