[SOLVED] Shader Coming Back all Black

I’m trying to make the same scanline shader that is talked about in this tutorial: http://joshuasmyth.maglevstudios.com

My code is almost what is in that tutorial with a slight update changing ps_2_0 to ps_4_0_level_9_1. Also the formatting on this site screws up the “Texture = ;” line by removing Texture surrounded by greater than and less than signs.

// Shader:

texture Texture;

sampler TextureSampler = sampler_state
{
Texture = ;
};

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
// Return normal
float4 color = tex2D(TextureSampler, input.TextureCordinate);
return color;
}

int ImageHeight;

float4 ScanLine(VertexShaderOutput input) : COLOR0
{
float4 color = tex2D(TextureSampler, input.TextureCordinate);

int a = saturate((input.Position.y * ImageHeight) % 4);
int b = saturate((input.Position.y * ImageHeight + 1) % 4);
float m = min(a,b);

color.rgb *= m;

return color;
}
.

// Game1.cs

Program.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
Program.m_Effect.Parameters[“ImageHeight”].SetValue(720);
Program.m_Effect.CurrentTechnique = Program.m_Effect.Techniques[1];
Program.m_Effect.CurrentTechnique.Passes[0].Apply();

GraphicsDevice.Clear(Color.Black);
Program.SpriteBatch.Draw(this.gameTexture2, SetResolutionHelper.DestinationRectangle, Color.White);

Program.SpriteBatch.End();

When I run the PixelShaderFunction() shader, it correctly displays the current picture to the screen (this was just for testing purposes).

When I run the ScanLine() shader, it just draws a black screen.

Any ideas? Any tips on how I can debug this, as it doesn’t stop on any breakpoints for me?

Thanks,

Kris

try floats instead of ints

saturate will clamp to a value between 0 and 1. If you cast to an int everything not exactly 1 and up will be zero.
If at least one of a or b is zero your min(a,b) returns 0.

EDIT: Actually no this should work

No dice, still all black. Good idea though.

float4 ScanLine(VertexShaderOutput input) : COLOR0
{
float4 color = tex2D(TextureSampler, input.TextureCordinate);

float a = saturate((input.Position.y * ImageHeight) % 4);
float b = saturate((input.Position.y * ImageHeight + 1) % 4);
float m = min(a,b);

color.rgb *= m;

return color;
}

I think it could be a logic error… it looks like float a may always be zero. I’m not familiar with what the “saturate()” method is doing though.

Doesn’t the % sign return the remainder of that division, which might be zero?

and then that zero is passed onto float ‘m’, which is then multiplied into the color.rgb, making it black?

I didn’t write the shader (nor do I have experience writing shaders) but the idea is that every fourth row I think should be dark (or black) to give it a scanline look and with this, every line is black. Maybe the whole shader logic needs to be redone :frowning:

if ((input.Position.y % 4) == 0)
{
color.rgb *= .1;
}

This seems to ALWAYS be true.

Maybe my issue is that input.Position.y is always the same value? I’m wanting this to occur for each row (and perhaps each pixel in each row).

I’ve looked at the shader, and it seems correct (before it was just a glance)

So I went ahead and plugged it in.

Works as intended

I did change one thing though, instead of

input.Position.y * ImageHeight

I use

input.texCoord.y * ImageHeight

I would suggest making the input “ImageHeight” a float by the way. The results are the same but GPUs like it more.
Most likely your input is not passed correctly.
Try it out with input.texCoord.y * 800 or something, some hard value. if that doesn’t work report back.

2 Likes

hmmm… So Kriswd40 (slick name btw), can you draw your texture NOT shaded, and then only when you shade it, you get all black?

And [quote=“kosmonautgames, post:7, topic:8488”]
I did change one thing though, instead of
[/quote]

So, without that change, you got black too?

Changing to use “input.TextureCordinate.y” did the trick! Thanks!