Jump to content

[HELP] Use 2 shaders together


85242

Recommended Posts

local blackWhiteShader = dxCreateShader("fx/blackwhite.fx")
local screenX, screenY = guiGetScreenSize()
local screenSource = dxCreateScreenSource(screenX, screenY)

local blurStrength = 6
local blurShader = dxCreateShader("fx/BlurShader.fx")


function doBlackAndWhite()
    if (blackWhiteShader) then
        dxUpdateScreenSource(screenSource)     
        dxSetShaderValue(blackWhiteShader, "screenSource", screenSource)
        dxDrawImage(0, 0, screenX, screenY, blackWhiteShader)
    end
end


function blur()
    if (blurShader) then
        dxSetShaderValue(blurShader, "ScreenSource", screenSource);
        dxSetShaderValue(blurShader, "BlurStrength", blurStrength);
        dxSetShaderValue(blurShader, "UVSize", screenWidth, screenHeight);
        dxDrawImage(0, 0, screenX, screenY, blurShader)
    end
end

how to run these two shaders at once

addEventHandler("onClientPreRender", getRootElement(), doBlackAndWhite)
addEventHandler("onClientPreRender", getRootElement(), blur)

it doesn't work. Only 1 shader load

Edited by 85242
Link to comment
2 minutes ago, Patrick said:

I think the only way if you merge them to one.

Can you upload both of them, with shader files?

//
// blackwhite.fx
//

texture screenSource;

sampler TextureSampler = sampler_state
{
    Texture = <screenSource>;
};

float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
    float4 color = tex2D(TextureSampler, TextureCoordinate);
 
    float value = (color.r + color.g + color.b) / 11; 
    color.r = value;
    color.g = value;
    color.b = value;

    return color;
}
 
technique BlackAndWhite
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
//blurshader.fx

texture ScreenSource;
float BlurStrength;
float2 UVSize;

sampler TextureSampler = sampler_state
{
    Texture = <ScreenSource>;
	MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};

static const float2 poisson[16] = 
{
        float2(-0.326212f, -0.40581f),
        float2(-0.840144f, -0.07358f),
        float2(-0.695914f, 0.457137f),
        float2(-0.203345f, 0.620716f),
        float2(0.96234f, -0.194983f),
        float2(0.473434f, -0.480026f),
        float2(0.519456f, 0.767022f),
        float2(0.185461f, -0.893124f),
        float2(0.507431f, 0.064425f),
        float2(0.89642f, 0.412458f),
        float2(-0.32194f, -0.932615f),
        float2(-0.65432f, -0.87421f),
		float2(-0.456899f, -0.633247f),
		float2(-0.123456f, -0.865433f),
		float2(-0.664332f, -0.25680f),
		float2(-0.791559f, -0.59771f)
};

float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{	
    float4 color = tex2D(TextureSampler, TextureCoordinate);
	
    for(int i = 0; i < 16; i++)
    {
        float2 coord= TextureCoordinate.xy + (poisson[i] / UVSize * BlurStrength);
        color += tex2D(TextureSampler, coord);
    }

    return(color/17);
}
 
technique BlurShader
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

 

Edited by 85242
Link to comment
  • Moderators

@85242 Something like this, but I can't test it.

 

Shader

Spoiler

texture ScreenSource;
float BlurStrength;
float2 UVSize;

sampler TextureSampler = sampler_state
{
    Texture = <ScreenSource>;
	MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};

static const float2 poisson[16] = 
{
        float2(-0.326212f, -0.40581f),
        float2(-0.840144f, -0.07358f),
        float2(-0.695914f, 0.457137f),
        float2(-0.203345f, 0.620716f),
        float2(0.96234f, -0.194983f),
        float2(0.473434f, -0.480026f),
        float2(0.519456f, 0.767022f),
        float2(0.185461f, -0.893124f),
        float2(0.507431f, 0.064425f),
        float2(0.89642f, 0.412458f),
        float2(-0.32194f, -0.932615f),
        float2(-0.65432f, -0.87421f),
		float2(-0.456899f, -0.633247f),
		float2(-0.123456f, -0.865433f),
		float2(-0.664332f, -0.25680f),
		float2(-0.791559f, -0.59771f)
};

float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{	
    float4 color = tex2D(TextureSampler, TextureCoordinate);
	
    float value = (color.r + color.g + color.b) / 11; 
    color.r = value;
    color.g = value;
    color.b = value;

    for(int i = 0; i < 16; i++)
    {
        float2 coord= TextureCoordinate.xy + (poisson[i] / UVSize * BlurStrength);
        color += tex2D(TextureSampler, coord);
    }

    return(color/17);
}
 
technique BlurShader
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

 

 

local screenX, screenY = guiGetScreenSize()
local screenSource = dxCreateScreenSource(screenX, screenY)

local blurStrength = 6
local blurShader   = dxCreateShader("fx/BlackAndWhiteBlurShader.fx")


dxSetShaderValue(blurShader, "BlurStrength", blurStrength);
dxSetShaderValue(blurShader, "UVSize", screenWidth, screenHeight);

function blur()
	if (blurShader) then
		dxUpdateScreenSource(screenSource)
		dxSetShaderValue(blurShader, "ScreenSource", screenSource);

        dxDrawImage(0, 0, screenX, screenY, blurShader)
    end
end
addEventHandler("onClientPreRender", getRootElement(), blur)

 

  • Like 1
Link to comment
  • Moderators

As alternative: It would be possible to use a render-target in between.

Draw the first shader inside of the rendertarget, then apply the render-target on to the second shader and draw that one on to the screen.

 

https://wiki.multitheftauto.com/wiki/DxCreateRenderTarget

 

I am not sure what the impact on the quality as well as the performance will be. Probably not so good for toasters... so stick with 1 shader if you possible can...

 

 

 

  • Like 1
Link to comment
  • Moderators
1 minute ago, 85242 said:

not working, shows only the blur

 

Then try with this shader file please:

Spoiler

texture ScreenSource;
float BlurStrength;
float2 UVSize;

sampler TextureSampler = sampler_state
{
    Texture = <ScreenSource>;
	MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};

static const float2 poisson[16] = 
{
        float2(-0.326212f, -0.40581f),
        float2(-0.840144f, -0.07358f),
        float2(-0.695914f, 0.457137f),
        float2(-0.203345f, 0.620716f),
        float2(0.96234f, -0.194983f),
        float2(0.473434f, -0.480026f),
        float2(0.519456f, 0.767022f),
        float2(0.185461f, -0.893124f),
        float2(0.507431f, 0.064425f),
        float2(0.89642f, 0.412458f),
        float2(-0.32194f, -0.932615f),
        float2(-0.65432f, -0.87421f),
		float2(-0.456899f, -0.633247f),
		float2(-0.123456f, -0.865433f),
		float2(-0.664332f, -0.25680f),
		float2(-0.791559f, -0.59771f)
};

float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{	
    float4 color = tex2D(TextureSampler, TextureCoordinate);
	
    for(int i = 0; i < 16; i++)
    {
        float2 coord= TextureCoordinate.xy + (poisson[i] / UVSize * BlurStrength);
        color += tex2D(TextureSampler, coord);
    }
  
    float value = (color.r + color.g + color.b) / 11; 
    color.r = value;
    color.g = value;
    color.b = value;

    return(color/17);
}
 
technique BlurShader
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

 

 

  • Thanks 1
Link to comment
  • Patrick changed the title to [HELP] Use 2 shaders together
  • Scripting Moderators

@85242 you could try aswell changing 2nd argument of dxUpdateScreenSource.

Quote

resampleNow: A bool to indicate if the screen should be captured immediately. The default is false which means the screen from the end of the previous frame is used (better for performance and consistency). Use true for layering fullscreen effects.

As far i remember that's how i solved it.

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...