|
Re: Gaussian Blur PP Pixel Shader
[Re: Hummel]
#350519
12/16/10 18:49
12/16/10 18:49
|
Joined: Sep 2003
Posts: 5,900 Bielefeld, Germany
Pappenheimer
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
|
Thanks. Any hints, why "#ifdef" is faster than "if()"? (Sorry, TSG_Torsten for hijacking your thread.)
EDIT: Thanks, Joozey. I missed the "every pixel" in Hummel's explanation.
Last edited by Pappenheimer; 12/16/10 19:19.
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Pappenheimer]
#350520
12/16/10 19:06
12/16/10 19:06
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Because the compiler evaluates the #ifdef only once, whereas the if() gets executed for every pixel every frame as Hummel says. So the #ifdef is here faster by definition . TSG_Torsen: No, no performance issues. My laptop is really crappy at processing stuff like shaders, but this goes fine. Must be said that it isn't exactly a large area of pp shading here . My game is 352x512 pixels .
Last edited by Joozey; 12/16/10 19:09.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Hummel]
#350521
12/16/10 19:30
12/16/10 19:30
|
Joined: Mar 2005
Posts: 564 /www/europe/ germany/index.php
TSG_Torsten
OP
User
|
OP
User
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
|
Thanks for the hint with ifdef, haven't thought about it before, and this is quite a good idea! Anyway, I've re-written the whole shader... So, now it is really direction-independent... voila: You can set a lot more options now in the shader-file. By default, PS 3.0 is needed, but take a look into the file if you need a PS 2.0:
// Gaussian Blur PixelShader 3.0
// By Torsten Simon - © 2010
// Accepts incomming:
// skill1 - strength (0 ... 10)
// mat_name.skill1=floatv(0...10);
#define USE_STATIC_VALUES true
// Remove/Comment this line to use material.Skill2...Skill5 for the values below
#define WIDTH 8
// WIDTH in Pixels to distribute the blur, lower values -> faster rendering
#define WIDTH_SKIP 2
// "Jump" Pixels when blurring, higher values -> faster rendering
#define RADIUS 7
// Radius when blurring, higher values -> faster rendering
#define GLOW 0
// Min: 0, Max: 100, Applying a glowing blur effect
// Please Read:
// This shader uses PS_3_0, but if you use the fast, simple blur-parameters
// you can also use PS_2_0. Therefore, change line 123 to
// PixelShader = compile ps_2_0 postprocessing_gaussian();
/* Some Examples to try
// Default (Small, Smooth)
#define WIDTH 8
#define WIDTH_SKIP 2
#define RADIUS 7
// Fast, Simple Blur (Works with PS_2_0 as well!)
#define WIDTH 8
#define WIDTH_SKIP 4
#define RADIUS 45
// Wide Range
#define WIDTH 30
#define WIDTH_SKIP 8
#define RADIUS 10
// Medium Range
#define WIDTH 20
#define WIDTH_SKIP 8
#define RADIUS 15
*/
#include <define>
float4 vecSkill1;
// Min: 0, Max:10
float4 vecSkill5;
Texture TargetMap;
sampler2D g_samSrcColor = sampler_state { texture = <TargetMap>; MipFilter = Linear; };
float4 vecViewPort;
#define RENDER vecSkill1.x>0
float4 postprocessing_gaussian(float2 Tex : TEXCOORD0) : COLOR0
{
float4 ret=tex2D(g_samSrcColor, Tex.xy);
#ifdef RENDER
int width,width_skip,radius,glow;
#ifdef USE_STATIC_VALUES
width=WIDTH;
width_skip=WIDTH_SKIP;
radius=RADIUS;
glow=GLOW;
#else
width=vecSkill1.y;
width_skip=max(vecSkill1.z,1);
if(width<width_skip) width=width_skip;
else width+=width%width_skip;
radius=max(vecSkill1.w,1);
glow=vecSkill5.x;
#endif
// Real sample
float4 sampleM = tex2D(g_samSrcColor, Tex.xy);
// Brightness calculation script
float fac=(vecSkill1.x)*float(float(((float)width_skip/(float)width)/float(360/radius))/10)*((glow+50)/50.f);
float s=(10-vecSkill1.x)*.1+fac;
ret=s*sampleM; // return variable
int i=0;
int dist=width_skip;
while(dist<=width)
{
float4 pos1;
float round=radians(i);
pos1.x=Tex.x+sin(round)*vecViewPort.z*dist;
pos1.y=Tex.y+cos(round)*vecViewPort.w*dist;
pos1.z=0;
pos1.w=0;
float4 S1=tex2Dlod(g_samSrcColor,pos1);
ret+=fac*(S1);
i+=radius;
if(i>=360) {
dist+=width_skip;
i=0;
}
}
#endif
return ret;
}
technique PostProcess
{
pass p1
{
AlphaBlendEnable = false;
VertexShader = null;
PixelShader = compile ps_3_0 postprocessing_gaussian();
}
}
As always, C & C are welcome [Edit:] And I'm again curious if you run into any performance issue... no problems for me so far @ATI HD 5870 Have fun! Regards TSGames
Last edited by TSG_Torsten; 12/16/10 21:41.
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Joey]
#350524
12/16/10 19:59
12/16/10 19:59
|
Joined: Mar 2005
Posts: 564 /www/europe/ germany/index.php
TSG_Torsten
OP
User
|
OP
User
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
|
I'm new to shaders, so if there are ways to design it better (and, for sure, there are ), feel free to do and re-post it I'm checking the activity of my GPU, and I've got less than 10% @1980x1080 and 30 FPS, so there are no performance problems for me so far... Regards TSGames
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Joey]
#350525
12/16/10 19:59
12/16/10 19:59
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Can you make the width, glow and radius also in vecSkill variables? I'd like to change those by events in the game. I tried, but I keep getting errors at the WIDTH in the while loop. Converted the vecskill2 to an integer, but same problem. I do get a performance issue now. Framerate drops considerably when I use your wide range sample. EDIT: I say vecSkill2, but that one is still part of the float4 of skill1. skill5 is better .
Last edited by Joozey; 12/16/10 21:32.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: TSG_Torsten]
#350531
12/16/10 20:25
12/16/10 20:25
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
I briefly tried the shader in joey's link, but that thing doesn't do a very good job. On high range it becomes a cross rather than a complete blur. It requires a vertical blur for each horizontal sample I think. So that's 9*9=81 samples anyway. not 9+9. Ill ponder a bit more with the vecskills, really want it to work . And yeah I can have a nice setting that gives me a good FPS, luckily.
Last edited by Joozey; 12/16/10 20:26.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Joozey]
#350536
12/16/10 20:59
12/16/10 20:59
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
the tex2d in your loop causes the gradient error to be triggered. If you use tex2dlod, and fill in float4(pos1.x,pos1.y,0,0) for the texture argument, it will work . Found the solution here, also the only thread about it on the whole internet. In English at least. Shaders are SO completely undocumented. http://www.gamedev.net/community/forums/topic.asp?topic_id=466629the [loop] keyword as suggested there didnt work for me.
Last edited by Joozey; 12/16/10 20:59.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Joozey]
#350540
12/16/10 21:43
12/16/10 21:43
|
Joined: Mar 2005
Posts: 564 /www/europe/ germany/index.php
TSG_Torsten
OP
User
|
OP
User
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
|
Thank you for this great tip! But i really don't understand why it doesn't work for the other function... Confusing I've updated the code above and you can remove the USE_STATIC_VALUES define if you want to use dynamic ones... (if you didn't already have rewritten the shader ) Regards TSGames
Last edited by TSG_Torsten; 12/16/10 21:51.
|
|
|
|