Shader to work with Texture or DrawString

To preface this, I am new to writing shaders and using HLSL. I have written a couple of shaders in the past for Gamemaker projects but this is a different shader language IIRC.

I am trying to write a grayscale shader that I can use with both spriteBatch.Draw and spriteBatch.DrawString. I am close to a solution but I don’t fully understand what I am doing so I need some help:

Here is the shader code:

sampler inputTexture;
bool isFont;

float4 MainPS(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 textureCoordinates : TEXCOORD0) : COLOR0
{
	float4 color = tex2D(inputTexture, textureCoordinates);
	
		// this works correctly for Textures
		color.rgb = color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722;
		return color;

		// this works correctly for DrawString
		color.rgb = color1.r * 0.2126 + color1.g * 0.7152 + color1.b * 0.0722;
		return color;
}

technique Technique1
{
	pass Pass1
	{
		AlphaBlendEnable = TRUE;
		DestBlend = INVSRCALPHA;
		SrcBlend = SRCALPHA;
		PixelShader = compile ps_3_0 MainPS();
	}
};

And here is how I am using it:

 if (uncastableCard)
{
	_spriteBatch.End();
	_spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
	grayscaleShader.CurrentTechnique.Passes[0].Apply();
	
}

// draw an outline around the card to indicate it can be played if release
if (inPlayArea && displayType == CardDisplayType.battle)
	_spriteBatch.Draw(cardHover, currentPosition + new Vector2(-1, -1), null, Color.White, rotation, spriteOrigin, 1.0f, SpriteEffects.None, 0);

_spriteBatch.Draw(imgCard, currentPosition, null, Color.White, rotation, spriteOrigin, 1.0f, SpriteEffects.None, 0);
_spriteBatch.Draw(imgRarity, textOrigin + new Vector2(57, 88), Color.White);
_spriteBatch.DrawString(fntNes, cost.ToString(), textOrigin, Color.Black);

// draw the card name
var wrappedCardName = cardName.WrapText(fntCGpixel, 48);
if (wrappedCardName.Contains("\n"))
{
	_spriteBatch.DrawStringWrappedAndCentered(fntCGpixel, cardName, 6, 48, textOrigin + new Vector2(37, -2), Color.Black);
}
else{
	_spriteBatch.DrawStringCentered(fntCGpixel, cardName, textOrigin + new Vector2(37, 1), Color.Black);
}

// draw the card text
_spriteBatch.DrawStringWrappedAndFormatted(fntCGpixel, cardText, textOrigin + new Vector2(1, 54), 6, 60, Color.Black, this, displayType);

// draw the card type
_spriteBatch.DrawStringCentered(fntCGpixel, TypeAsString(), textOrigin + new Vector2(32, 87), Color.Black);            

// now turn off the shader
if (uncastableCard)
{
	_spriteBatch.End();
	_spriteBatch.Begin();
}

Is there a way to tell within the shader if I am operating on a texture or not? Any help or guidance is appreciated!

With the SpriteBatch it’s all a texture.

With the SpriteBatch it’s all a texture.

Thank you for your help. I’m not sure what you mean though. If that were true wouldn’t operating on the “inputTexture” sampler input give me the correct output for the DrawString operation as well?

I wrote this as an introduction to shaders with the SpriteBatch: First shader - Learn MonoGame

I didn’t look too closely at your shader but I believe you can modify the first line to:

sampler inputTexture : register(s0);