Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / [STICKY] Learning to write Shaders

Author
Message
lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 11th Jul 2009 05:03
hello all, i love what you are doing here...

now ive been working on a sort of bloom for my game but am having some difficulty..

Firstly i should say that i have been using Evolveds bloom shader rewritten by RUCCUS from here http://forum.thegamecreators.com/?m=forum_view&t=102187&b=1

i took some code out and tryed to make it work as best as it could in my program, i ended up with this



now the bloom works ok for the mosty part, but there are 2 problems

1. the color of the sphere doesnt seem to affect the bloom (using a loaded texture)

2. the bg which was black with white dots is now all white and thats a bit devastating

attached is a shot of the shader at work

Attachments

Login to view attachments
lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 11th Jul 2009 05:42 Edited at: 11th Jul 2009 08:45
also here is a shot without the shader off

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Jul 2009 13:11
Quote: "the bg which was black with white dots is now all white "


Looks blue to me.

Quote: "also here is a shot without the shader off"


With the shader off, perhaps?

Looks like the shader has shrunk the sphere as well.

As you say, something isn't right.

I'm playing with a bloom shader as it happens. I'll see if I can find time to post a demo later today.

Am I right in assuming you just want the colour of the sphere to spread out as a glow - but leave the background as it is? Or do you want the bright background stars to glow as well?
lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 11th Jul 2009 13:27
well the backgound is just 2d so i may leave it, but i will be working on it a bit..

sorry about the bg color being wrong, my eyes didnt like it and told me that it was white

if you could do an example for me that would be great

p.s. the sphere has shrunk but not because of the shader, its because its a generator and it generated a small sphere..
Xlaydos
20
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 14th Jul 2009 23:51 Edited at: 14th Jul 2009 23:55
Do you think you could help me with this shader i have written.



It is supposed to render a mandelbrot fractal, however the object comes out completly red (in the mandelbrot set). Do UV values range from 0.0 to 1.0? Wouldn't this mean that multiplying the UV coords by 10, will cause most of the pixels to be outside the mandelbrot set (any point with x0 or y0 > 1 will definatly by outside the set).

The algorithm works correctly however as can be seen if you put in x0 = 1.0 and y0 = 1.0 instead of the uv coordinates.

Also, as i am not using the color component of PSInput, is it necessary that i include it in the structure? It appears not to matter from experimentation.

Thanks for your help, sorry if i am unclear anywhere.

Edit: It also stops working if i add the line "int maxIter = 1000;" right at the top of the code and replace 1000 by maxIter. Shouldn't this be okay?
aki
15
Years of Service
User Offline
Joined: 8th Nov 2008
Location:
Posted: 15th Jul 2009 14:19 Edited at: 15th Jul 2009 14:22
Quote: "aki

Could you include a simple small working demo (media + .dba file) so we can see what the problem is?

[I'm not too sure I really understand what it's supposed to do. ] "

I gave up on this shader, and replaced the entire system I was working on with a multi-cubic lighting shader that I just stayed up until 7 AM working on. I've attached it and a neat demo, if anybody wants to try it out or use it. It's pretty much EVOLVED's Cubic Lighting Shader, but with support for multiple lights and modified for a bit better performance at marginal quality loss.

Attachments

Login to view attachments
aki
15
Years of Service
User Offline
Joined: 8th Nov 2008
Location:
Posted: 15th Jul 2009 14:21
[Screenshot]

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Jul 2009 23:12 Edited at: 16th Jul 2009 02:18
Xlaydos

Very interesting questions.

I don't know the full explanation yet. However,

1) I'm amazed the shader even compiles - it does without even a warning message. The reason I'm amazed is that the while loop - if it compiles at all - would have to unroll to a potentially enormous number of instructions and in PS_1_1 only a few instructions are allowed in total (you get a similar all red result in PS2).

2) The shader compiles to the following asm code:



which, as you can see, contains no references at all to your while loops etc. In fact all it does is define the register c0 as the colour red:



and this is immediately moved to the output register:



So the question is: why is all the looping ignored without giving an error or warning message?

The next question is: can we fix this, at least approximately?

I don't know the answer to either question ... yet. Back to school for me it seems.

Edit

Your basic algorithm seems to be correct. Here's a PS_2_b version of your code with your while loop unrolled a fixed number of times. While this obviously won't give the Mandelbrot set you can see the right sort of shape beginning to emerge when you run it on a plain:



If you're using an nVidia card try replacing the PS_2_b with PS_2_a.

If you have a suitable card you could try PS_3_0 with your original code - but you might need PS_4_0.

Here's a screenshot from Dark Shader using the tweaks displayed:




2nd Edit

Just tested the above shaders on my new PC - and PS3 just gives the red image with both shaders which is odd. No time to work out why now. Hwever, it's an nVidia card and PS2a allows 512 pixel shader instructions so I've pushed it to the limit with this code ("mandel3.fx"):



This is beginning to work nicely and gives the following image when I run the shader from DBPro (code below) :






aki and others

Been rather busy recently - hope to return to some of your questions soon.

Had my retirement party at work today so it might not be the best time for me to try anything clever.

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Jul 2009 02:14
Second image for previous post.

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Jul 2009 16:18 Edited at: 16th Jul 2009 16:19
Here's a revised version of the shader:



and the calling DBPro code (you may need to change the screen resolutions, etc):



Here's a screenshot:

Attachments

Login to view attachments
Xlaydos
20
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 16th Jul 2009 23:31
Thanks for the swift reply, i am away for a few days tomorrow and still need to pack so will read and reply when i return Thanks again
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 00:39 Edited at: 17th Jul 2009 00:44
sorry if this is sort of off topic, can someone please make a heat haze effect, sort of like this:
so if you applyed it to something then everything behind it would be distorted.
(sorry for the big image, its all i could find)


Now is better than never.
Although never is often better than *right* now.
jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 17th Jul 2009 00:43
that's jet exhaust haze to be specific - note the trees are fairly "clear" while the haze as you call it (distortion) is behind the jets only. This does make coding the shader a bit trickeir I'd think.

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 01:01 Edited at: 17th Jul 2009 01:17
that image is not really the best example, all I want is the area behind a plain to be distorted:
like this:
or is that exactley what you were talking about?

Now is better than never.
Although never is often better than *right* now.

Attachments

Login to view attachments
jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 17th Jul 2009 01:07
Maybe not the best example - but a really cool picture nonetheless ( I love aircraft - all of em)

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 01:17
look at above pic...

Now is better than never.
Although never is often better than *right* now.
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 17th Jul 2009 01:23
That's much easier, thenerd.

Get a camera and set it to image, put it in the same place as your main camera and texture the plain with the image.

Then you just need to change the pixel shader so it reads from so heat-hazey normal map and offset the texel by that amount.

If you scroll two heat-haze textures over each other then it should look good.

---

First of all, see if you can get it to render the plain as invisible; then try and do the offsets.

-= Out here in the fields, I fight for my meals =-
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Jul 2009 01:39
How about a "Predator" style effect where an object is invisible - except for the "heat haze" effect it gives to objects behind it?
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 02:10
thats exactly what I want!

Now is better than never.
Although never is often better than *right* now.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 17th Jul 2009 12:09
Evolved made a refraction shader, it must be on his website. And if it isn't, just take a look at his water shader, it's a good start.
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 16:29
i tryed that, and it works pretty good for distorting an image, but setting up a camera for every flame is to complicated. fire is a big aspect of my game, so I want it to be as efficient as possible. Having multiple cameras drastically reduces frame rate.

Now is better than never.
Although never is often better than *right* now.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 17th Jul 2009 18:04
Only one camera is needed (it has to be a 'clone' of the main camera).
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 17th Jul 2009 18:11
I would just do it with a transparent plane with 2 heat haze textures scrolling. A shader might make it look prettier, but if you're looking for a quick solution that's probably the most efficient.

thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 18:13 Edited at: 17th Jul 2009 18:13
I sort of got it to work, but it is always either upside-down or sideways!
upside down screenie:


Now is better than never.
Although never is often better than *right* now.

Attachments

Login to view attachments
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 18:14 Edited at: 17th Jul 2009 18:14
sideways:


Now is better than never.
Although never is often better than *right* now.

Attachments

Login to view attachments
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Jul 2009 18:15 Edited at: 17th Jul 2009 18:16
Quote: "but setting up a camera for every flame is to complicated."


Who said anything about this? Just draw your scene to an image then draw a 2nd scene where all your normal objects are RGB 128,128,128 and your flames are blended planes with animated UV offsets for textures and you draw this scene to a 2nd image. You finally draw the first image to your main scene(on a quad) and using a shader, offset the sampling by your 2nd image thus giving you heat haze with unlimited heat sources and per-pixel occlusion.

thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 18:18
sorry, im not the best with shaders...
ignore the "multiple camera" comment

Now is better than never.
Although never is often better than *right* now.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Jul 2009 19:05
Quote: "Who said anything about this? Just draw your scene to an image then draw a 2nd scene where all your normal objects are RGB 128,128,128 and your flames are blended planes with animated UV offsets for textures and you draw this scene to a 2nd image. You finally draw the first image to your main scene(on a quad) and using a shader, offset the sampling by your 2nd image thus giving you heat haze with unlimited heat sources and per-pixel occlusion"


That's more or less how I would do it too.
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 22:45
*slaps self for stupidity*
i am stupid.

Now is better than never.
Although never is often better than *right* now.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Jul 2009 23:01
Quote: "*slaps self for stupidity*"


Welcome to the club.

There's nothing stupid about having to learn.

Anyway, have you solved the problem?
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 23:12 Edited at: 17th Jul 2009 23:13
not really. I just realized that the reason evolved's refraction shaders didnt work was i had the wrong version!
i hate shaders

Now is better than never.
Although never is often better than *right* now.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Jul 2009 23:23
Quote: "i hate shaders"


Doesn't sound too promising.
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 17th Jul 2009 23:35
I don't really hate them, they are quite amazing. just im not good at making them.

Quote: "Who said anything about this? Just draw your scene to an image then draw a 2nd scene where all your normal objects are RGB 128,128,128 and your flames are blended planes with animated UV offsets for textures and you draw this scene to a 2nd image. You finally draw the first image to your main scene(on a quad) and using a shader, offset the sampling by your 2nd image thus giving you heat haze with unlimited heat sources and per-pixel occlusion."

can you make an example, because i don't quite get it.

Now is better than never.
Although never is often better than *right* now.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Jul 2009 23:58
aki

Quote: "and replaced the entire system I was working on with a multi-cubic lighting shader that I just stayed up until 7 AM working on. I've attached it and a neat demo, if anybody wants to try it out or use it. It's pretty much EVOLVED's Cubic Lighting Shader, but with support for multiple lights and modified for a bit better performance at marginal quality loss. "


That's a very nice demo - that should prove very useful. Well done.

Looks like you don't help on this any more.
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 18th Jul 2009 00:10 Edited at: 18th Jul 2009 00:13
[edit]
nevermind. youre talking to aki.

Now is better than never.
Although never is often better than *right* now.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Jul 2009 01:32
Quote: "nevermind. youre talking to aki"


True - but I hope to look at your query this weekend.
aki
15
Years of Service
User Offline
Joined: 8th Nov 2008
Location:
Posted: 18th Jul 2009 01:38 Edited at: 18th Jul 2009 01:51
Quote: "
That's a very nice demo - that should prove very useful. Well done.

Looks like you don't help on this any more. "

Nope, thanks. Now that I've spent 2 months learning shaders and draining community resources (IE you) I figure I might be able to be less useless.

Quote: "can you make an example, because i don't quite get it."

To get started, try using that refraction shader mentioned earlier and applying it to a plain positioned directly in front of the screen.
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 18th Jul 2009 02:28 Edited at: 18th Jul 2009 02:30
progress!
i tryed that, and the cow is perfectly distorted:


Now is better than never.
Although never is often better than *right* now.

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Jul 2009 03:10
Quote: "and the cow is perfectly distorted"


It certainly is.

And I hope you realise DBPro is worth paying for.
lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 18th Jul 2009 03:26
DBP is definately worth it, get the studio pack if you can it helps alot in the long run...

Anyway back on topic, GG have you had a chance to look into that bloom shader i needed help with...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Jul 2009 12:36
Quote: "Anyway back on topic, GG have you had a chance to look into that bloom shader i needed help with..."


Oops!

I made a start and had something working but wasn't happy with it (I was trying to "improve" the one that comes with Dark Shader ...) then got distracted by other things. Sorry.

I'll move this back to the top of my "to do" list.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Jul 2009 14:32
Might as well post what I've got so far even though there is plenty of room for improvement. I've included some notes on performance for four different machines.

Attachments

Login to view attachments
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 18th Jul 2009 21:47
DB pro is definitely worth it!
I am getting dark game studio this weekend...
also, the bloom demo is great, though a bit slow on my machine.

Now is better than never.
Although never is often better than *right* now.
lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 19th Jul 2009 08:30
thanks soo much for that GG i will look through it a little later, it looks great tho

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Jul 2009 13:28
I love this thread, it really brings out the best in the community.

Keep up the good work folks.

-= Out here in the fields, I fight for my meals =-
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Jul 2009 13:50
Quote: "Keep up the good work folks."


We'll try.

lucifer 1101

I've just tested that bloom demo of mine using the Dark Shader (DS) version "Bloom.dbs" - it's about four times faster than my shader and looks just as good. I was trying a slightly different method because someone somewhere said there was a problem with the DS version - I can't see anything wrong myself so if you want speed you'd be better getting hold of Dark Shader which includes several other very useful shaders to play with.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 19th Jul 2009 22:31
thenerd, if you don't mind me asking, what did you do?

<-- Spell based team dueling game!
thenerd
15
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 20th Jul 2009 00:33 Edited at: 20th Jul 2009 00:33
with the picture of the cow?
just used evolved's refraction shader on a plain,
used a this texture for the bump map

and scrolled the texture every loop.

Now is better than never.
Although never is often better than *right* now.

Attachments

Login to view attachments
lucifer 1101
15
Years of Service
User Offline
Joined: 26th Jan 2009
Location: Melbourne, Australia
Posted: 20th Jul 2009 06:23
ok thankyou GG, i have looked through you example trying to learn how to us the fullscreen shader, but i dont understand a few things.
Firstly what is set camera effect 1, 1, 3 it doesnt show up in my help files, also why do i need another plain for the final image, if im setting the effect to the camera shouldnt it just be set to the original camera and then it does the rest of the rendering automatically..

P.S. i also just noticed the sync camera command, why are so many commands missing from the help file, if those commands were there i may not be having so much difficulty..

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Jul 2009 13:28 Edited at: 20th Jul 2009 15:01
Quote: "Firstly what is set camera effect 1, 1, 3"


The syntax is:



The syntax for the preceding load camera effect command is



Quote: "it doesnt show up in my help files"


Yes, that is very annoying - same thing applies to the Advanced Terrain commands.

Quote: "also why do i need another plain for the final image"


I guess it is because the camera's output is sent to an image - and you need to apply that to an object so you can see it (note to self: - could we use paste image instead? )

Quote: "if im setting the effect to the camera shouldnt it just be set to the original camera and then it does the rest of the rendering automatically.."


Yes, that would be a helpful option for camera 0. I'll try to remember to ask Paul Johnston about that.

Quote: "why are so many commands missing from the help file"


I have no idea. But it seems to be following the time-honoured tradition of not including Help files for commands which originate with a different piece of software (in this case Dark Shader). No doubt there are exceptions. Anyone know of any?

Quote: "if those commands were there i may not be having so much difficulty"


You are not alone. In this case the commands are documented, albeit somewhat buried, in the Dark Shader Help file. I believe there was a file "ShaderCommands.txt" which summarised the syntax. I seem to have mislaid my copy and have no idea where I got it from in the first place. There may also have been some documentation in the relevant Upgrade*_*.txt file when the commands were first made generally available. In my opinion the commands should have been added to the Help file at the same time.

Edit

Found two relevant files lurking on my old PC (never, ever, throw anything away ):

"ShaderData.ini"



and "ShaderData Commands.txt" :



I think I'll copy them to sensible locations on my other machines.

Login to post a reply

Server time is: 2024-05-01 08:53:57
Your offset time is: 2024-05-01 08:53:57