Skip to content

One shader with multiple inputs

MWstudios edited this page Sep 10, 2021 · 3 revisions

How to do it

What used to be a painful implementation in SharpDX (speaking from my experience), is now merely a one-liner. Just add multiple CustomEffectInput attributes on top of your shader class. By default, each class has one input attribute, so if you want a shader to have, say, 4 inputs, add three more attributes:

[CustomEffectInput("Source2"), CustomEffectInput("Source3"), CustomEffectInput("Source4")]
public class Shader1 : PVShaderBase
{
	//...
}

Then, specify three more inputs in the HLSL file. This shader blends 4 textures together, each in one corner:

Texture2D InputTexture1 : register(t0); //here
Texture2D InputTexture2 : register(t1);
Texture2D InputTexture3 : register(t2);
Texture2D InputTexture4 : register(t3);

SamplerState InputSampler1 : register(s0); //here
SamplerState InputSampler2 : register(s1);
SamplerState InputSampler3 : register(s2);
SamplerState InputSampler4 : register(s3);

float4 main(float4 pos : SV_POSITION, float4 posScene : SCENE_POSITION,
	float4 uv0 : TEXCOORD0, float4 uv1 : TEXCOORD1, float4 uv2 : TEXCOORD2, float4 uv3 : TEXCOORD3) : SV_Target //and here
{
	//We're using uv0 only so all 4 textures will be lined up. Otherwise, 4 textures with different sizes would be offset.
	return lerp(
		lerp(InputTexture1.Sample(InputSampler1, uv0), InputTexture2.Sample(InputSampler2, uv0), uv0.x),
		lerp(InputTexture3.Sample(InputSampler3, uv0), InputTexture4.Sample(InputSampler4, uv0), uv0.x),
		uv0.y);
}

How does it work?

In reality, setting up an effect to work with multiple effects is more than just adding a few attributes to your class. I had to understand how the nodes in Direct2D work, and Microsoft Docs are making it more complicated than it actually is. But as a result of my work, you really don't have to do anything else.

And why was it so hard? Because nobody on the internet ever talked about multiple texture inputs or explained how to do it, which means I was on my own.

In this wiki you can find out all the information on how to use the Ensoftener library. For more information on how to add Ensoftener to your project, see "Installing and running". The rest is dedicated to the library's features.

Notice: This wiki shows information for Ensoftener 5.0 and is currently outdated.

Clone this wiki locally