i have another problem
it uses extremely amount of performance and my fps drops
here's my lua code:
--
-- c_bloom.lua
--
local scx, scy = guiGetScreenSize()
-----------------------------------------------------------------------------------
-- Le settings
-----------------------------------------------------------------------------------
Settings = {}
Settings.var = {}
Settings.var.bloom = 1.5
Settings.var.blendR = 255
Settings.var.blendG = 255
Settings.var.blendB = 255
Settings.var.blendA = 100
----------------------------------------------------------------
-- onClientResourceStart
----------------------------------------------------------------
addEventHandler( "onClientResourceStart", resourceRoot,
function()
-- Version check
if getVersion ().sortable < "1.1.0" then
outputChatBox( "Resource is not compatible with this client." )
return
end
-- Create things
myScreenSource = dxCreateScreenSource( scx/2, scy/2 )
blurHShader,tecName = dxCreateShader( "blurH.fx" )
outputDebugString( "blurHShader is using technique " .. tostring(tecName) )
--blurVShader,tecName = dxCreateShader( "blurV.fx" )
--outputDebugString( "blurVShader is using technique " .. tostring(tecName) )
-- Check everything is ok
bAllValid = myScreenSource and blurHShader
if not bAllValid then
outputChatBox( "Could not create some things. Please use debugscript 3" )
end
end
)
-----------------------------------------------------------------------------------
-- onClientHUDRender
-----------------------------------------------------------------------------------
addEventHandler( "onClientHUDRender", root,
function()
if not Settings.var then
return
end
if bAllValid then
-- Reset render target pool
RTPool.frameStart()
-- Update screen
dxUpdateScreenSource( myScreenSource )
-- Start with screen
local current = myScreenSource
-- Apply all the effects, bouncing from one render target to another
current = applyGBlurH( current, Settings.var.bloom )
--current = applyGBlurV( current, Settings.var.bloom )
-- When we're done, turn the render target back to default
dxSetRenderTarget()
-- Mix result onto the screen using 'add' rather than 'alpha blend'
if current then
dxSetShaderValue( blurHShader, "TEX0", current )
local col = tocolor(Settings.var.blendR, Settings.var.blendG, Settings.var.blendB, Settings.var.blendA)
dxDrawImage( 0, 0, scx, scy, blurHShader, 0,0,0, col)
end
end
end
)
-----------------------------------------------------------------------------------
-- Apply the different stages
-----------------------------------------------------------------------------------
function applyGBlurH( Src, bloom )
if not Src then return nil end
local mx,my = dxGetMaterialSize( Src )
local newRT = RTPool.GetUnused(mx,my)
if not newRT then return nil end
dxSetRenderTarget( newRT, true )
dxSetShaderValue( blurHShader, "TEX0", Src )
dxSetShaderValue( blurHShader, "TEX0SIZE", mx,my )
dxSetShaderValue( blurHShader, "BLOOM", bloom )
dxDrawImage( 0, 0, mx, my, blurHShader )
return newRT
end
-----------------------------------------------------------------------------------
-- Pool of render targets
-----------------------------------------------------------------------------------
RTPool = {}
RTPool.list = {}
function RTPool.frameStart()
for rt,info in pairs(RTPool.list) do
info.bInUse = false
end
end
function RTPool.GetUnused( mx, my )
-- Find unused existing
for rt,info in pairs(RTPool.list) do
if not info.bInUse and info.mx == mx and info.my == my then
info.bInUse = true
return rt
end
end
-- Add new
local rt = dxCreateRenderTarget( mx, my )
if rt then
outputDebugString( "creating new RT " .. tostring(mx) .. " x " .. tostring(mx) )
RTPool.list[rt] = { bInUse = true, mx = mx, my = my }
end
return rt
end
and the .fx code:
//
// Example shader - blurH.fx
//
// Horizontal blur
//
//---------------------------------------------------------------------
// blurH settings
//---------------------------------------------------------------------
float sBloom : BLOOM = 1;
texture sTex0 : TEX0;
float2 sTex0Size : TEX0SIZE;
//---------------------------------------------------------------------
// Include some common stuff
//---------------------------------------------------------------------
#include "mta-helper.fx"
//-----------------------------------------------------------------------------
// Static data
//-----------------------------------------------------------------------------
static const float Kernel[13] = {-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6};
static const float Weights[13] = { 0.002216, 0.008764, 0.026995, 0.064759, 0.120985, 0.176033, 0.199471, 0.176033, 0.120985, 0.064759, 0.026995, 0.008764, 0.002216};
//static const float Weights[13] = { 0.2270270270, 0.3162162162, 0.0702702703, 0.064759, 0.120985, 0.176033, 0.199471, 0.176033, 0.120985, 0.064759, 0.026995, 0.008764, 0.002216};
//---------------------------------------------------------------------
// Sampler for the main texture
//---------------------------------------------------------------------
sampler Sampler0 = sampler_state
{
Texture = (sTex0);
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
//---------------------------------------------------------------------
// Structure of data sent to the vertex shader
//---------------------------------------------------------------------
struct VSInput
{
float3 Position : POSITION0;
float4 Diffuse : COLOR0;
float2 TexCoord : TEXCOORD0;
};
//---------------------------------------------------------------------
// Structure of data sent to the pixel shader ( from the vertex shader )
//---------------------------------------------------------------------
struct PSInput
{
float4 Position : POSITION0;
float4 Diffuse : COLOR0;
float2 TexCoord: TEXCOORD0;
};
//------------------------------------------------------------------------------------------
// VertexShaderFunction
// 1. Read from VS structure
// 2. Process
// 3. Write to PS structure
//------------------------------------------------------------------------------------------
PSInput VertexShaderFunction(VSInput VS)
{
PSInput PS = (PSInput)0;
// Calculate screen pos of vertex
PS.Position = MTACalcScreenPosition ( VS.Position );
// Pass through color and tex coord
PS.Diffuse = VS.Diffuse;
PS.TexCoord = VS.TexCoord;
return PS;
}
//------------------------------------------------------------------------------------------
// PixelShaderFunction
// 1. Read from PS structure
// 2. Process
// 3. Return pixel color
//------------------------------------------------------------------------------------------
float4 PixelShaderFunction(PSInput PS) : COLOR0
{
float4 Color = 0;
float2 coord;
coord.y = PS.TexCoord.y;
for(int i = 0; i < 7; ++i)
{
coord.x = PS.TexCoord.x + Kernel[i]/sTex0Size.x;
coord.y = PS.TexCoord.y + Kernel[i]/sTex0Size.y;
Color += tex2D(Sampler0, coord.xy) * Weights[i] * sBloom;
}
Color = Color * PS.Diffuse;
Color.a = 1;
return Color;
}
//------------------------------------------------------------------------------------------
// Techniques
//------------------------------------------------------------------------------------------
technique blurh
{
pass P0
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
// Fallback
technique fallback
{
pass P0
{
// Just draw normally
}
}