[SOLVED] Shader isnt working as expected

Hey there ! Im trying to make a neon shooter.

So i googled a bit and found this tutorial site :

I took some of the source code modified it for monogame, enabled the shader, compiled it and vola :

But it dont work as expected … normaly it should look like the picture from the tutorial, it should be brighter more neon like… so what did i do wrong ?

Theres a lot of stuff to post sooo i created a gist where is stored all the stuff :

I would be glad for some help … i really could need it thanks !

You commented the bloom.BeginDraw(); in your Main.cs, try uncommenting it and let us know about the results. :slight_smile:

Ohhhh … sorry i havent added that into the github code. In my project im calling it :slight_smile: But thanks, im going to change that later in github

I would be very glad about any kind of help :slight_smile:

It’s really hard to tell what the exact problem is and it’s probably not the shader. Does it look the same with the bloom enabled and disabled?

The image looks really dark, so there is probably nothing the bloom can pick up, since it triggers only above a certain brightness/luma threshold. Try lowering the threshold value, or set it to 0, does it change anything?

You can try to step through the bloom process, check out the horizontal or vertical blur rendertargets only, see if they are not completely black.

Sounds to me as favorite XNA sample bloom issue.

Hey there !

tried it only with bloom.beginDraw(); and i get a damn black screen… When i call bloom.draw(); i can see the ship and it looks like the picture i posted . When i change some stats like threshold nothing changes … Here the bloom settings :

` public static BloomSettings[] PresetSettings =
{
// Name Thresh Blur Bloom Base BloomSat BaseSat
new BloomSettings(“Default”, 0.25f, 4, 1.25f, 1, 1, 1),
new BloomSettings(“Soft”, 0, 3, 1, 1, 1, 1),
new BloomSettings(“Desaturated”, 0.5f, 8, 2, 1, 0, 1),
new BloomSettings(“Saturated”, 0.25f, 4, 2, 1, 2, 0),
new BloomSettings(“Blurry”, 0, 2, 1, 0.1f, 1, 1),
new BloomSettings(“Subtle”, 0.5f, 2, 1, 1, 1, 1),
};

`

Nothing changes, i can set bloom to 200 and it looks like i set it to 2 … Wheres the iusse ? Im going to look at the link :slight_smile:

Thats really weierd, i played a bit around with the shader … Now i am using a bright player ship, which looks now like it should. But even if i set the shader parameters BlurAmount to 1000f :

The rendered texture looks like :

Why the hell ? With blurAmount 1000f it looks like 1f … what i am doing wrong ? :confused:

Can you please go through thread I´ve linked?

I already readed it but it couldnt help me :confused: I still dont know why nothing changes at changing the parameters

Why this isnt working ? I just noticed that the shader wasnt working at all … even when i thought it would work lol … Those shader methods are called, i just tested it … but they arent changing anything :

`
public void BeginDraw()
{
if (Visible)
{
GraphicsDevice.SetRenderTarget(sceneRenderTarget);
}
}

    public override void Draw(GameTime gameTime)
    {
    
        GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;

        // Pass 1: draw the scene into rendertarget 1, using a
        // shader that extracts only the brightest parts of the image.
        bloomExtractEffect.Parameters["BloomThreshold"].SetValue(
            Settings.BloomThreshold);

        DrawFullscreenQuad(sceneRenderTarget, renderTarget1,
                           bloomExtractEffect,
                           IntermediateBuffer.PreBloom);

        // Pass 2: draw from rendertarget 1 into rendertarget 2,
        // using a shader to apply a horizontal gaussian blur filter.
        SetBlurEffectParameters(1.0f / (float)renderTarget1.Width, 0);

        DrawFullscreenQuad(renderTarget1, renderTarget2,
                           gaussianBlurEffect,
                           IntermediateBuffer.BlurredHorizontally);


        // Pass 3: draw from rendertarget 2 back into rendertarget 1,
        // using a shader to apply a vertical gaussian blur filter.
        SetBlurEffectParameters(0, 1.0f / (float)renderTarget1.Height);

        DrawFullscreenQuad(renderTarget2, renderTarget1,
                           gaussianBlurEffect,
                           IntermediateBuffer.BlurredBothWays);

        // Pass 4: draw both rendertarget 1 and the original scene
        // image back into the main backbuffer, using a shader that
        // combines them to produce the final bloomed result.
        GraphicsDevice.SetRenderTarget(null);

        EffectParameterCollection parameters = bloomCombineEffect.Parameters;

        parameters["BloomIntensity"].SetValue(Settings.BloomIntensity);
        parameters["BaseIntensity"].SetValue(Settings.BaseIntensity);
        parameters["BloomSaturation"].SetValue(Settings.BloomSaturation);
        parameters["BaseSaturation"].SetValue(Settings.BaseSaturation);

        GraphicsDevice.Textures[1] = sceneRenderTarget;

        Viewport viewport = GraphicsDevice.Viewport;

        DrawFullscreenQuad(renderTarget1,
                           viewport.Width, viewport.Height,
                           bloomCombineEffect,
                           IntermediateBuffer.FinalResult);
    }


    /// <summary>
    /// Helper for drawing a texture into a rendertarget, using
    /// a custom shader to apply postprocessing effects.
    /// </summary>
    public void DrawFullscreenQuad(Texture2D texture, RenderTarget2D renderTarget,
                            Effect effect, IntermediateBuffer currentBuffer)
    {
        GraphicsDevice.SetRenderTarget(renderTarget);

        DrawFullscreenQuad(texture,
                           renderTarget.Width, renderTarget.Height,
                           effect, currentBuffer);
    }


    /// <summary>
    /// Helper for drawing a texture into the current rendertarget,
    /// using a custom shader to apply postprocessing effects.
    /// </summary>
    void DrawFullscreenQuad(Texture2D texture, int width, int height,
                            Effect effect, IntermediateBuffer currentBuffer)
    {
        // If the user has selected one of the show intermediate buffer options,
        // we still draw the quad to make sure the image will end up on the screen,
        // but might need to skip applying the custom pixel shader.
        if (showBuffer < currentBuffer)
        {
            effect = null;
        }

        spriteBatch.Begin(0, BlendState.Opaque, null, null, null, effect);
        spriteBatch.Draw(texture, new Rectangle(0, 0, width, height), Color.White);
        spriteBatch.End();
    }


    /// <summary>
    /// Computes sample weightings and texture coordinate offsets
    /// for one pass of a separable gaussian blur filter.
    /// </summary>
    void SetBlurEffectParameters(float dx, float dy)
    {
        // Look up the sample weight and offset effect parameters.
        EffectParameter weightsParameter, offsetsParameter;

        weightsParameter = gaussianBlurEffect.Parameters["SampleWeights"];
        offsetsParameter = gaussianBlurEffect.Parameters["SampleOffsets"];

        // Look up how many samples our gaussian blur effect supports.
        int sampleCount = weightsParameter.Elements.Count;

        // Create temporary arrays for computing our filter settings.
        float[] sampleWeights = new float[sampleCount];
        Vector2[] sampleOffsets = new Vector2[sampleCount];

        // The first sample always has a zero offset.
        sampleWeights[0] = ComputeGaussian(0);
        sampleOffsets[0] = new Vector2(0);

        // Maintain a sum of all the weighting values.
        float totalWeights = sampleWeights[0];

        // Add pairs of additional sample taps, positioned
        // along a line in both directions from the center.
        for (int i = 0; i < sampleCount / 2; i++)
        {
            // Store weights for the positive and negative taps.
            float weight = ComputeGaussian(i + 1);

            sampleWeights[i * 2 + 1] = weight;
            sampleWeights[i * 2 + 2] = weight;

            totalWeights += weight * 2;

            // To get the maximum amount of blurring from a limited number of
            // pixel shader samples, we take advantage of the bilinear filtering
            // hardware inside the texture fetch unit. If we position our texture
            // coordinates exactly halfway between two texels, the filtering unit
            // will average them for us, giving two samples for the price of one.
            // This allows us to step in units of two texels per sample, rather
            // than just one at a time. The 1.5 offset kicks things off by
            // positioning us nicely in between two texels.
            float sampleOffset = i * 2 + 1.5f;

            Vector2 delta = new Vector2(dx, dy) * sampleOffset;

            // Store texture coordinate offsets for the positive and negative taps.
            sampleOffsets[i * 2 + 1] = delta;
            sampleOffsets[i * 2 + 2] = -delta;
        }

        // Normalize the list of sample weightings, so they will always sum to one.
        for (int i = 0; i < sampleWeights.Length; i++)
        {
            sampleWeights[i] /= totalWeights;
        }

        // Tell the effect about our new filter settings.
        weightsParameter.SetValue(sampleWeights);
        offsetsParameter.SetValue(sampleOffsets);
    }


    /// <summary>
    /// Evaluates a single point on the gaussian falloff curve.
    /// Used for setting up the blur filter weightings.
    /// </summary>
    float ComputeGaussian(float n)
    {
        float theta = Settings.BlurAmount;

        return (float)((1.0 / Math.Sqrt(2 * Math.PI * theta)) *
                       Math.Exp(-(n * n) / (2 * theta * theta)));
    }`

I also read the thread again and changed the PixelShaderFunction in all fx files from :

float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0 ->> float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0

That didnt changed anything … Any ideas ?

because this: GraphicsDevice.Textures[1] = sceneRenderTarget;

as far as my experience goes, doesn´t work.

Instead do this in shader

sampler BaseSampler : register(s1)
{ 
    Texture = (BaseTexture);  
    Filter = Linear;  
    AddressU = clamp;
    AddressV = clamp;
};

and this to assign texture

bloomCombineEffect.Parameters["BaseTexture"].SetParameter(RenderTargetWithBaseTexture);

@Ravendarke Thanks for helping me ! :slight_smile: That sounds logical. But after i set this into my shader :

`   
sampler BaseSampler : register(s1)
{ 
Texture = (BaseTexture);  
Filter = Linear;  
AddressU = clamp;
AddressV = clamp;
};

`

And changed

GraphicsDevice.Textures[1] = sceneRenderTarget;

to

bloomCombineEffect.Parameters["BaseTexture"].SetValue(sceneRenderTarget); (because there isnt SetParameter(); )

It dont changes anything … Its still the same as before … any ideas ? :confused: But is that right that i need to pass sceneRenderTarget into the BaseTexture ?

Hey there ! Just tested something. I didnt changed the shader or the class to call the shader anymore. Without calling bloom.draw(gameTime) i just see a black screen.

Without using the shader (Without calling bloom.beginDraw(); and bloom.draw(gameTime)) I can see the normal unchanged textures)

And with using the Shader (With callingbloom.beginDraw(); and bloom.draw(gameTime) ) nothing changes and i see the normal textures as without using the shader :confused:

Until yet i did everything as @Ravendarke told me but it didnt changed anything … Im a total noob to Shaders what am i doing wrong ?

------UPDATE------

I updated my gisthub / github code :

------UPDATE------

I also tried to convert my fx files to mgfx. It worked but when i put them into the pipeline and build the project it tells me “Bad Token Cpp.Net

It works now ! I found the iusse !

Dont call the shader and your sprite like that :

bloomShader.Begin(); bloomShader.Draw(gameTime); spriteBatch.Begin(); spriteBatch.draw(); spriteBatch.end()

instad call it like that :

bloomShader.Begin(); spriteBatch.Begin(); spriteBatch.draw(); spriteBatch.end() bloomShader.Draw(gameTime);

Than it works ! Because :

Your sprite batch rendering should be between bloom.BeginDraw and bloom.Draw.

bloom.BeginDraw binds the sceneRenderTarget you’re supposed to render you scene to. This way the rendered data is accessible for further processing done in bloom.Draw.

pics or it didn’t happen! :slight_smile: