[C++] Postprocessing class and shaders (GLSL and HLSL)

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Slin
Posts: 23
Joined: Sat May 17, 2008 9:31 am
Location: Eutin, Germany
Contact:

[C++] Postprocessing class and shaders (GLSL and HLSL)

Post by Slin »

I am very new to Irrlicht and coding in C++, but worked with GameStudio for the last five years. During that time I learned very little C++, C-Script, Lite-C and HLSL.
At the moment I am really interested in graphics programming and thus nearly the first thing I wanted to get working with Irrlicht was postprocessing.
I found several snippets but none which really was as I wanted it to be. Because of that I decided to create my own class for postprocessing effects which allows chaining of shaders and sending parameters to them.

I also plan to create a collection of several postprocessing shaders. I´ve got Bloom and inverting colors done in GLSL and HLSL.

It is all released under the MIT-license.

You can download it all, together with a compiled version here: http://files.filefront.com/Irrlicht+Pos ... einfo.html
Please have a look at the readme!

If you have any proposals to improve it, please tell me, as well as any postprocessing shader wishes you have got :)


Here are two screenshots showing the bloom:
Image
Image


I hope you like it :)
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

"class for postprocessing effects which allows chaining of shaders and sending parameters to them"

that sounds pretty awesome. i'll look into the code and figure out shader chaining.

thanks a lot, man.
Image
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Yeah that does sound cool, almost like the Ogre system. I will like to have a look at the code too. One thing I have to say is that I would like to see someone do some HDR rendering with Irrlicht.

EDIT:

Well I tried running this demo, and I have to say, I got a crap load of shader compilation errors.
TheQuestion = 2B || !2B
Slin
Posts: 23
Joined: Sat May 17, 2008 9:31 am
Location: Eutin, Germany
Contact:

Post by Slin »

Halifax wrote: EDIT:

Well I tried running this demo, and I have to say, I got a crap load of shader compilation errors.
What kind of errors?
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

like:

ERROR 0:11: 'assign' : cannot convert from 'const int' to '2-component vector of float'

ERROR: 1 compliation errors. No code generated

FBO has one or several image attachments with different dimensions
FBO error
FBO=1, Color=33, Depth=34, Stencil=0

and there freezes
Slin
Posts: 23
Joined: Sat May 17, 2008 9:31 am
Location: Eutin, Germany
Contact:

Post by Slin »

What are the specs of the system you are running it on?

And is there anyone it works for?
(I know of me and at least one other...)
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Errors
Irrlicht Engine version 1.4
Microsoft Windows XP Personal Service Pack 2 (Build 2600)
Using renderer: OpenGL 2.1.7412
ATI MOBILITY RADEON XPRESS 200: ATI Technologies Inc.
OpenGL driver version is 1.2 or better.
GLSL version: 1.3
Loaded shader: scripts/common.shader for scripts/common.shader
Loaded shader: scripts/models.shader for scripts/models.shader
Loaded shader: scripts/e7.shader for textures/e7/e7walldesign01b
Loaded texture: textures/e7/e7walldesign01b.jpg
Loaded texture: textures/e7/e7steptop2.jpg
Loaded texture: textures/e7/e7dimfloor.jpg
Loaded texture: textures/e7/e7brickfloor01.jpg
Loaded texture: textures/e7/e7bmtrim.jpg
Loaded texture: textures/e7/e7sbrickfloor.jpg
Loaded texture: textures/e7/e7brnmetal.jpg
Loaded texture: textures/e7/e7beam02_red.jpg
Loaded texture: textures/e7/e7swindow.jpg
Loaded texture: textures/e7/e7bigwall.jpg
Loaded texture: textures/e7/e7panelwood.jpg
Loaded texture: textures/e7/e7beam01.jpg
Loaded texture: textures/gothic_floor/xstepborder5.jpg
Loaded texture: textures/liquids/lavahell.jpg
Loaded texture: textures/e7/e7steptop.jpg
Loaded texture: textures/gothic_trim/metalblackwave01.jpg
Loaded texture: textures/stone/pjrock1.jpg
Loaded texture: models/mapobjects/timlamp/timlamp.tga
Loaded shader: scripts/sfx.shader for textures/sfx/flame1side
Loaded texture: models/mapobjects/gratelamp/gratetorch2.jpg
Loaded texture: models/mapobjects/gratelamp/gratetorch2b.tga
Loaded mesh: 20kdm2.bsp
Needed 45ms to create OctTree SceneNode.(357 nodes, 10684 polys)
GLSL shader failed to compile
Fragment shader failed to compile with the following errors:
ERROR: 0:37: 'tex2D' : no matching overloaded function found
ERROR: 0:37: '=' : cannot convert from 'const float' to 'undefined interpolatio
n 4-component vector of float'
ERROR: 0:40: 'assign' : cannot convert from 'const int' to '3-component vector
of float'
ERROR: 3 compilation errors. No code generated.

GLSL shader failed to compile
Fragment shader failed to compile with the following errors:
ERROR: 0:38: 'tex2D' : no matching overloaded function found
ERROR: 0:38: '=' : cannot convert from 'const float' to '4-component vector of
float'
ERROR: 0:44: 'tex2D' : no matching overloaded function found
ERROR: 3 compilation errors. No code generated.

GLSL shader failed to compile
Fragment shader failed to compile with the following errors:
ERROR: 0:38: 'tex2D' : no matching overloaded function found
ERROR: 0:38: '=' : cannot convert from 'const float' to '4-component vector of
float'
ERROR: 0:44: 'tex2D' : no matching overloaded function found
ERROR: 3 compilation errors. No code generated.

GLSL shader failed to compile
Fragment shader failed to compile with the following errors:
ERROR: 0:38: 'tex2D' : no matching overloaded function found
ERROR: 0:38: '=' : cannot convert from 'const float' to '4-component vector of
float'
ERROR: 0:39: 'tex2D' : no matching overloaded function found
ERROR: 3 compilation errors. No code generated.
The GLSL language has no such thing as 'tex2D' the name of the function is 'texture2D'.
TheQuestion = 2B || !2B
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

Image
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Yes correct, 'tex2D' belongs to HLSL.

EDIT:
Also, I am curious as to why the horizontal and vertical blurring were not combined into one shader instead of multiple passes.
TheQuestion = 2B || !2B
Slin
Posts: 23
Joined: Sat May 17, 2008 9:31 am
Location: Eutin, Germany
Contact:

Post by Slin »

Halifax wrote: Yes correct, 'tex2D' belongs to HLSL.
I don´t understand why it works fine for me and some others, but that is probably okay. This code was my first contact with GLSL and thus I don´t really know the differences between HLSL and GLSL yet.
I changed it in my files and will upload it together with the next version of my class and hopefully one or two more shaders in the next days.
Halifax wrote: Also, I am curious as to why the horizontal and vertical blurring were not combined into one shader instead of multiple passes.
It is faster than doing it in one pass with a similar quality because of less texture lookups.
http://www.blackpawn.com/texts/blur/default.html[/url]
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

Slin wrote:What are the specs of the system you are running it on?

And is there anyone it works for?
(I know of me and at least one other...)
It's AMD 2500, Windows XP, ATI Radeon 9600 PRO
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Ah, okay, I understand Slin. Here are the shaders I got working with GLSL, but it doesn't really do the bloom effect it just blurs the image. Either way the errors are all erased, and I will try to get the bloom effect working for you:

PP_GL_Bloom1

Code: Select all

/***********************************************************************
Copyright (c) 2008 Nils Daumann
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/

/************************************************************************
GLSL Bloom
Effect 1: Filtering out dark Colors, needs PS 1.4
*************************************************************************/


uniform vec4 vecValues[2];
uniform sampler2D texture1;

void main()
{
	vec4 Color = texture2D(texture1,gl_TexCoord[0].xy);
	if(dot(Color.rgb,vec3(0.299f,0.587f,0.114f)) < vecValues[0].x)
	{
		//Color.rgb = 0;
		Color.rgb = vec3(0);
	}
	
	gl_FragColor = Color;
}
PP_GL_Bloom2

Code: Select all

/***********************************************************************
Copyright (c) 2008 Nils Daumann
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/

/************************************************************************
GLSL Bloom
Effect 2: Horizontal blur, needs PS 2.0
*************************************************************************/


uniform vec4 vecValues[2];
#define blurRadius 5
uniform sampler2D texture1;

void main()
{
	vec4 Color = texture2D(texture1,gl_TexCoord[0].xy);
	vec2 tempTex;
	tempTex.y = gl_TexCoord[0].y;
	for(int i=-blurRadius;i<=blurRadius;i++)
	{
		tempTex.x = gl_TexCoord[0].x+i*vecValues[0].x;
		Color += texture2D(texture1,tempTex.xy);
	}
	gl_FragColor = Color/(blurRadius*2+1);
}
PP_GL_Bloom3

Code: Select all

/***********************************************************************
Copyright (c) 2008 Nils Daumann
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/

/************************************************************************
GLSL Bloom
Effect 3: Vertical blur, needs PS 2.0
*************************************************************************/


uniform vec4 vecValues[2];
#define blurRadius 5
uniform sampler2D texture1;

void main()
{
	vec4 Color = texture2D(texture1,gl_TexCoord[0].xy);
	vec2 tempTex;
	tempTex.x = gl_TexCoord[0].x;
	for(int i=-blurRadius;i<=blurRadius;i++)
	{
		tempTex.y = gl_TexCoord[0].y+i*vecValues[0].x;
		Color += texture2D(texture1,tempTex.xy);
	}
	gl_FragColor = Color/(blurRadius*2+1);
}
PP_GL_Bloom4

Code: Select all

/***********************************************************************
Copyright (c) 2008 Nils Daumann
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/

/************************************************************************
GLSL Bloom
Effect 4: Add the blurred image to the original one, needs PS 2.0
*************************************************************************/


uniform vec4 vecValues[2];
uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
	vec4 Color = texture2D(texture2, gl_TexCoord[0].xy);
	Color += vecValues[0].x * texture2D(texture1, gl_TexCoord[0].xy);
	gl_FragColor = Color;
}
TheQuestion = 2B || !2B
Slin
Posts: 23
Joined: Sat May 17, 2008 9:31 am
Location: Eutin, Germany
Contact:

Post by Slin »

That is exactly what I already did :) (both, the renaming and the define)
It seems to be a problem with ATI cards that it doesn´t work as expected. Is it possible that it does not work because of something with the texture sizes?

Thanks for your help :)
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Yeah, it might be a problem with ATI cards, that is definitely possible. But no, I don't think it has anything to do with texture sizes because I checked your code and they are all POT textures.

Maybe BlindSide will be here sooner or later though and enlighten us on why this doesn't work no ATI cards.

One thing I have to ask, though, is can you make a build of your code for DX9, and post that please?
TheQuestion = 2B || !2B
Slin
Posts: 23
Joined: Sat May 17, 2008 9:31 am
Location: Eutin, Germany
Contact:

Post by Slin »

Here is a an executable of it with directx: http://files.filefront.com/PostProcessi ... einfo.html

The result you get seems as if the second texture isn´t set...
Post Reply