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
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 24th Jun 2009 13:52
I would like to request a shader:

diffuse map
normal map
bump map
specularity based on a given specular map
8 lights
fog (better if it supports linear and layered both together and if its possible to set it through dbpro application)

vs2.0 and ps2.0 is maximum.

is this possible?

There is always one more imbecile than you counted on.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 24th Jun 2009 20:23
Quote: "is this possible?"

I don't think so, the shader model 2 has a very low instruction limit, I don't think you can have parallax and specular and fog in the same shader.

Also, I don't really recommend using 8 lights because the shader would need to do 8 passes (and that's slow). Most of the time, you can have a good result with only 3 or 4 lights. The key is to put a different shader on each part of your world (like each room has its own shader, and thus allows only 4 lights)
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 25th Jun 2009 09:54
Well, okay then id like to request the same shader with maximum lights possible for vs2 ps2, if its possible?
Plus with optional reflection technique added seperately?

There is always one more imbecile than you counted on.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 25th Jun 2009 12:17
The problem isn't the amount of lights, just all the features you want. If I remember correctly, I tried to do a simple normal mapping + lightmap and I was almost hitting the maximum instruction count.
But a separate reflection pass should be possible.
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 25th Jun 2009 13:01 Edited at: 25th Jun 2009 13:04
allright then, is it possible to combine different fragments into 1 shader? If so, do we have to make different passes for every part included into the shader?

I see samples of the shader i want with even more additions like glow map, transparency map..etc.

how do they do that?

EDIT: one more unrelated question.
Does DBPro compile hlsl code into assembly itself using the directx compiler?

May i have a very basic vertex shader file(*.vsh) written with the hlsl language which dbpro can "create vertex shader from file" using the same command?

There is always one more imbecile than you counted on.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 25th Jun 2009 14:55
Adding a glowmap and transparency is not a problem. The glowmap is a simple additional pass rendering the texture (which is then used for the glow itself in a fullscreen shader), and the transparency is stored in the diffuse texture.

Since I have a bit of free time, I'll try to make a basic shader that you could then enhance yourself.
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 25th Jun 2009 15:09 Edited at: 25th Jun 2009 15:33
Here you go, this is one of evolved's shaders that I modified to have 8 lights (instead of 6), and use a Specular map.

Use the arrow keys, and mouse to navigate. Use keys 1 - 8 to position
the lights (the scene will be black until you do so).

The shader doesn't currently have fog or reflection, but this should get you started.

Attachments

Login to view attachments
CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 25th Jun 2009 16:08
Thanks revenant chaos, it looks like it does most of what i want.
However, according to the sample shaders i see around on the web, its possible to achive same effect with less code, 1 vertex shader output, less passes..etc.
I mean, this shader might be optimized i suppose but how.

Im looking forward for your example math89.

There is always one more imbecile than you counted on.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 25th Jun 2009 19:27 Edited at: 26th Jun 2009 18:34
Here is my shader, it supports parallax, glow and specular. I have implemented only 2 lights, but you can add some other ones with a lot of boring copy-pasting.

Edit: shader fixed and commented, and added an interface for the parameters.

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 25th Jun 2009 23:57
hey folks! just say honestly - you all are ignoring me or i have asked some forbidden questions?...*sad*

Lee Bamber is so~o swett'n'cool desu nya ^^
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Jun 2009 00:26
Quote: "hey folks! just say honestly - you all are ignoring me or i have asked some forbidden questions?...*sad*"


"No" to both questions.

Yes, it would be nice to be able to create cube maps and volume textures correctly using built-in DBPro commands - and I think the time HAS come for this feature. There was some discussion of saving volume textures, for example, at the last TGC Convention - but I believe no definite decision was made. I was keen on this - but it is hard for TGC to commit time on things like this for just one or two users. On the plus side, a useful step forward was made at the convention - we can now load and apply volume textures in DBPro as well as standard images and cube maps.

I'm not sure what you mean by "compiled" cube maps - the only formats we need to get are the various .dds formats for cube maps.

However, you might be able to mimic this using the texture atlas idea that was discussed on this thread a few months ago - I think this is what you mean by a strip image ( ). If you could get that working then you wouldn't need the more conventional cubemaps.

revenant chaos and Math89

Great to see you both contributing excellent demos. Keep up the good work.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 26th Jun 2009 00:47
I think that using a strip image would be very difficult and would involve a lot of if statements. It would be a complete waste of instructions and would be really slow.

If you are interested in making your own cubemap sampler, here is the method:
- Find the largest component of your vector3. This gives the face you need to sample: if the largest value is a negative x, then you have to sample the left side of the cube.
- Then, you need to find the uv coordinates on the face. This is done by dividing the 2 other components by the one found before. Unfortunately, those raw uv values aren't enough: depending on the face, you will probably need to swap or invert them.
- Finally, use the standard tex2D command with the uv coords you found.

As you see, there is no general way to do it, you need to have all the 6 cases in some if statements...


Green Gandalf:
My shader is based on Evolved's one, I just added and changed a few things.
By the way, does any of you have problems with comments in shaders? Since the last update, it seems that using the double slash isn't recognized as a comment any more and makes the program to crash or give some silly errors.
Talking about errors, it also seems that most of the shader compilation errors aren't reported by the Perform Checklist For Effect Errors command. This is extremely annoying, because blind debugging isn't a real pleasure... (especially when you find out after a whole day that it comes from the comments )
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Jun 2009 01:36
Quote: "I think that using a strip image would be very difficult and would involve a lot of if statements."


Agreed.

Quote: "My shader is based on Evolved's one, I just added and changed a few things."


I know - but it's still a nice demo.

Quote: "By the way, does any of you have problems with comments in shaders? Since the last update, it seems that using the double slash isn't recognized as a comment any more and makes the program to crash or give some silly errors."


I haven't experienced that problem - either in shaders or DBPro and my shaders usually have several lines of double slashed comments. Are you using the right slash?

Quote: "Talking about errors, it also seems that most of the shader compilation errors aren't reported by the Perform Checklist For Effect Errors command. This is extremely annoying, because blind debugging isn't a real pleasure... (especially when you find out after a whole day that it comes from the comments )"


Can't comment on that - I never use that feature. I test mine using Dark Shader (usually) and FX Composer (sometimes). Both give the error messages in full.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 26th Jun 2009 10:23
@Math89
Thanks, man! I'll try to make it and post here results.

Lee Bamber is so~o swett'n'cool desu nya ^^
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 26th Jun 2009 12:12
Quote: "I haven't experienced that problem - either in shaders or DBPro and my shaders usually have several lines of double slashed comments. Are you using the right slash?"

If I add " // this is a test " anywhere on the shader I posted, it says that the compilation failed because there isn't any technique or because I'm truncating a vector.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Jun 2009 14:49
Quote: "If I add " // this is a test " anywhere on the shader I posted, it says that the compilation failed because there isn't any technique or because I'm truncating a vector."


That's because the file doesn't seem to contain any linebreaks so the double slash tells the shader compiler to ignore everything after the inserted double slash.

I couldn't view the shader correctly in any standard application (e.g. Notepad, FX Composer, Dark Shader) until I opened it in MS Word and saved it again as a txt document (using the original filename of course). Then it opens fine in the other apps and your problem disappears.

What application are you using?
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 26th Jun 2009 17:07
I'm using notepad++, and after trying it in the standard notepad, I must admit that you are 100% right. It seems that the problem comes from the fact that notepad++ thought I would like to write a Unix file which needs only the carriage return character at the end of a line (Windows needs another one).
I will upload the corrected version with some extra comments.

Thank you, I don't think I would have figured it out by myself.
aki
15
Years of Service
User Offline
Joined: 8th Nov 2008
Location:
Posted: 2nd Jul 2009 07:25 Edited at: 2nd Jul 2009 07:33
I've been working on a shader which takes colors from an overhead view of a lightmapped plain and projects the light onto objects based on their world position. After working on it on and off for a month, it finally works - but whenever I stretch or rotate objects it affects, it breaks. Is there any way to fix this without completely rewriting it? It looks to me like I would have to multiply my TexLight UV by something, but I'm not quite sure what.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Jul 2009 11:35
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. ]
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 2nd Jul 2009 11:58
This may seem like a newbie question but how many textures can a shader have? 8 I think?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Jul 2009 13:33
Quote: "This may seem like a newbie question but how many textures can a shader have? 8 I think?"


Yes for SM2 - but I'm not so sure about SM3 or SM4.

One solution if you really need more is to use a "texture atlas" - there was some discussion on this thread with AtomR around January 2009 time.

Another solution is to use limbs which can be textured separately (and have different copies of the shader applied to them) - but I realise this isn't always an option.
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 2nd Jul 2009 14:11
Well, the main reason I was asking was for the good old terrain shader. I took your "GG Blended Bump Mapping Shader.fx" and made it into a deferred shader. Here it is for anyone who wishes to use it. If you could have a look over it and see if I did anything that was needless please let me know. Its working fine on my project as demonstrated by this screenshot.



Thanks for the help.

Attachments

Login to view attachments
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 2nd Jul 2009 14:16
Your shader is interesting, does that mean that DBPro can retrieve multiple colours from a shader? If so, how would you define the data type used for each colour?
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 2nd Jul 2009 14:26
Well actually I'm not using DBP on that project.(sorry for the confusion) -- I'm sure that DarkGDK could make use of that shader though. Though the way it was explained to me was that the cameras are just a render target. I believe that the cameras are D3DFMT_A8R8G8B8 format.

Though you could use a different approach and make it work with DBP. I'd have to rewrite the shader but it could be made into a DBP compatable defered one.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 2nd Jul 2009 14:32
OK, does that mean that rendering only one camera will fill the other renders? If it works in DGDK, it should work in DBPro (and that would be a great news!).
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 2nd Jul 2009 14:43
If you have yahoo or msn messenger my id is dwestfall10070@hotmail.com and dwestfall10070 for yahoo. I can answer better on there.
Alfa x
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 3rd Jul 2009 22:58
Hi,
I have a question for the experts in this board.

When you use shader you can apply multiple textures to an object with multiple transforms to make some nice effects in the object.

Is there a way to include various transform ( Lightening, Normalmapping), into a single transform to optimize resources from a shader so only a unique texture stage is used (or multiple, preferring only one)? (And is more efficient). (Perhaps with stored information? ). This of course will not work for dynamic environments but is often needed in a project with a large scale of objects (like an RTS ) with a need of cheaper good graphics.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Jul 2009 01:14
Yes.

One method is to put lots of techniques into the same shader with each technique referring to its own subset of the textures applied to an object.

For example, you might have the following:

stage 0 - base texture
stage 1 - normal map
stage 2 - cube map (for reflections from the environment)

Then you might have three techniques:

standard lighting - just uses stage 0 texture
standard normalmapping - uses stages 0 and 1
bump reflections - uses stages 1 and 2 (see a recent thread today )

Many other combinations are possible and each technique could share some code. There is a limit of 8 textures in total though.
Alfa x
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 4th Jul 2009 01:51 Edited at: 4th Jul 2009 01:53
hi,
i'm a bit confused (im a noob..., maybe im saying something very incoherent ). I will say it in other words

When I use for example specular lighting i have one set of equations to calculate the final form that is sampled.
When I used normal mapping i do something similar.

It's is possible to make both effects in one set of instructions?.

something like taking the visual effect from a shader that was a precalculated before, so i can save instructions in the shader?. Like saving the calculations of a shader that has a complex instruction set and then use them in a static way in a with less instructions saving resources obtaining the same result that in the end will not change. And merging the results of both visual effects (normalmapping and lightening) in one instruction set only.
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 4th Jul 2009 06:48
@alpha x -- do you mean that you would bump map the object and then save out the shaded version and just re-load that without the shader?
Alfa x
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 4th Jul 2009 08:05
Something like that.
WIth another shader or without shader (what actually works). All to save space of processing time.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2009 14:13
Quote: "All to save space of processing time"


Which processing time are you thinking of saving?
Alfa x
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 5th Jul 2009 16:12
The time you save by applying less instructions in a shader to obtain the same effect. (or by not applying the shader at all)

Maybe I'm confused and making strange questions? (I'm fearing im too noob in this )
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2009 18:12
Quote: "The time you save by applying less instructions in a shader to obtain the same effect"


For example?

You were earlier talking about normal mapping. How would you save instructions? In a standard 3D scene, lights and objects move around. This means that things like diffuse and specular lighting need to be recalculated for each object. It is usual to do this each "sync" - in fact it's hard to do it effectively in any other way. One way of saving instructions is to replace a series of instructions by a texture lookup - this is, in effect, what a "light map" is for.

One of my terrain blending shaders is a good example where some saving could be made, in theory at least. A large part of the processing is simply, and wastefully, repeated each sync. Since the instructions in question simply produce a blend of four textures why not do this once at the beginning and store the resulting image and use that? One reason is that it's easier not to bother. Another reason is that the resulting image for the whole terrain would be far too big. You could split it into smaller images, one for each of the terrain's limbs, but the details rapidly get messy. It could be done though.

Otherwise the only way of saving instructions is to code more efficiently ...
Alfa x
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 5th Jul 2009 22:43 Edited at: 5th Jul 2009 22:44
Thanks!.
Thats is what i was talking about. I read in a book that each texture that is used in the blending consumes resources that can be optimized.

Also the lightmap is a good start for me to optimize resources.

I thank you so much. It's hard to express yourself when you know so little about a subject.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2009 22:50 Edited at: 5th Jul 2009 22:58
You're welcome.

[Edit: added this edit to get past the forum "duplicate error" dragon. ]
Alfa x
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 7th Jul 2009 01:34
Hi,
I have a question.

I have been reading about shaders and i have read that in Pixel shaders you can do the following:

"The pixel shader can now have four color outputs, so we can update four independent render targets at one time"

Can anyone point me to an example shader (or make an example) so i can understand how to update four render targets at a time and explain what advantages has?.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 7th Jul 2009 13:09
You can't do that in DBPro, but a good example of use would be deferred shading, when you need to get a few images of scene: one with the texture colour only, one with the normals, one with the depth and one with the specular information.
Irradic
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 7th Jul 2009 18:48 Edited at: 7th Jul 2009 19:39
Hello, I'm in need of some help. I'm currently trying to implement Evolved's cubic shadow mapping into my own normal map shader.
But I'm having a problem with the depth map, instead of being black and white it is red and black. I have no idea why that is, I would be very thankful if someone could take a look and point me in the right direction. I attached an image and the shader + source for the example. Thanks in advance !



Attachments

Login to view attachments
Irradic
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 7th Jul 2009 19:08
Here is the source of the shader and example

Attachments

Login to view attachments
Irradic
17
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 8th Jul 2009 01:47
Solved it, didn't have the techniques set correctly. Sorry all

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Jul 2009 02:32
I like those kind of questions.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Jul 2009 19:56
I have the following textures that I'm using as a test from my artist. He's asked me to use this as a test for displaying objects in my game with and without shaders. I can do a fair job without shaders using blend mapping.

Diffuse


Normal


Specular


Problem is I can't find a shader that uses a full size specular texture like this. The only ones I've been able to find use a thin gradient line. If you know of one that uses a full size specular texture, please point me in the right direction.

Thanks.

revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 9th Jul 2009 02:50
Here is one.

Attachments

Login to view attachments
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 9th Jul 2009 17:18
Excellent, thanks.

Now I'm looking for an example of how to use it. I'm just getting started with shaders, and don't quite know how to read them yet.

revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 9th Jul 2009 17:58
Oh, sorry. Here is a demo that shows the basics.

Attachments

Login to view attachments
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 9th Jul 2009 18:05 Edited at: 9th Jul 2009 19:17
Most excellent. Thank you!!

EDIT:

Now another question.. I also have an EMISSIVE texture for some of my game models. Is there a way to integrate that into the use of this shader? I've tried a few different things, but the shader is overriding any effect the emissive texture might have.

revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 9th Jul 2009 20:41 Edited at: 9th Jul 2009 20:42
I am not quite sure what you mean by an emissive texture, so I am going to assume you mean a texture that makes surfaces look as if they were emitting light. Look at the demo and tell me what you think.

Attachments

Login to view attachments
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 10th Jul 2009 00:36 Edited at: 10th Jul 2009 00:39
That's exactly right. Thanks again.

..and in looking at the shader code I actually learned something..

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Jul 2009 00:50
revenant chaos

Excellent demo - again.

Login to post a reply

Server time is: 2024-05-04 04:36:06
Your offset time is: 2024-05-04 04:36:06