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
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 18th Apr 2010 18:35
Quote: "just rename your current dbpro directory, intall new version, and if you don't like it, rename back your old dbpro directory "

That's genius. Ta.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 18th Apr 2010 23:00 Edited at: 18th Apr 2010 23:02
Hi!

I can't find any info on how to set mipmap levels manually:
I know that needed command is tex2Dlod(texture,float4(IN.coords.xy,A,B)).
but i dont know how to use A,B - anything like 1,2 or 0,3 doesn't work.
For example, depth value of pixel is 5 meters and I want 1 mipmap level to start from this point.

Maybe i need tex2Dbias? %)

Any help is good)

Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 19th Apr 2010 05:24
A quick bit of googling for "HLSL set mipmap" threw up stuff including this page:

http://www.catalinzima.com/?page_id=75

I think you want tex2Dlod(). Not sure because i've not used that yet.


I updated DBP to U74. Good news - my projects still work, although the lighting (or blendmapping) is different and transparencies are rendering wierdly, but basically i'll likely be able to fix all that so will stick with u74.

Cubemap demo still didn't work as was (it requires u75), but i managed to get it working simply by changing the texture flag on load effect from 0 to 1. Now works fine!

Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 20th Apr 2010 04:05
Quote: "I know that needed command is tex2Dlod(texture,float4(IN.coords.xy,A,B))."


http://developer.nvidia.com/forums/index.php?showtopic=2461&pid=7010&mode=threaded&start=]check this out.

apparently you can set the mipmap level using the w component of the 4d texture co-ord in tex2Dlod:

tex2Dlod(textureMap, float4(texCoord.xy, 0, lod))

I made that portal shader. It works but it's kind of slow. Seems I had to do the thing where you load in another texture first to get dynamic cubemapping working. Is there any way i can do it without that? It's wierd. Even when i use the dummy texture, if i don't let it effect the final output of the pixel shader, it won't work! No big deal though.

Here's a link to the thread. Shader version is attached to last post.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Apr 2010 16:27
Quote: "Seems I had to do the thing where you load in another texture first to get dynamic cubemapping working."


That's a new one on me. What exactly are you doing?
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 20th Apr 2010 17:14 Edited at: 20th Apr 2010 17:32
I'm using a shader with a cube map, and am getting that cube map from DBP with the "set cube mapping on" command, so i can send it images rendered from extra cameras. If i declare just the cubemap texture in the shader, and load effect, set object effect, then set cubemapping on in DBP, the shader is just black.

If I declare a dummy texture in the shader before the cube map texture, then in DBP assign a dummy image to texture stage 0 before applying the effect and setting cube mapping on, it works, but it's quite fussy in that the shader seems to have to sample from the dummy texture 1st, and it has to have some effect on the pixel shader output. I attached my project file to the thread I linked if you want to have a play.

bit of DBP code:

Actually you can even set texture stage 0 to image 0, or other image that dones't exist!

whole shader:

Full project is in other thread.

However, it may be that if you're running the U75 beta you won't have the same problem I had. Lee said in the Sept 09 newsletter that there had been some unspecified tweaking in U75 that enabled it to work. He also says that getting dynamic cubemaps to work previously was somewhat of a black art IIRC. I got Lee's demo to work on U74 by changing the texture flag on "load effect" to 1.

The dummy texture thing is mysterious. I got hints from this: http://forum.thegamecreators.com/?m=forum_view&t=157246&b=22

I hear that you (GG) managed to make a dynamic cube prior to U75. Can't find a link to it. Would be interested to see.

Failing that i'll try using the U75 Beta.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Apr 2010 21:25 Edited at: 20th Apr 2010 21:39
Quote: "was somewhat of a black art IIRC"


In my opinion the "black arts", i.e. the Dark Shader commands, are much simpler.

Quote: "I hear that you (GG) managed to make a dynamic cube prior to U75. Can't find a link to it. Would be interested to see."


I'll see if I can find it. IIRC I merely followed the instructions in Dark Shader ...

At the moment I have no idea why you are having difficulties since I don't recall any problems like that before.

Edit Do you mean this one? It uses either the Dark Shader method (with a shader) or Lee's method (without a shader). If you're having problems using a shader with Lee's method (it seems I haven't tried that yet) try the Dark Shader method.

Attachments

Login to view attachments
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 20th Apr 2010 23:17 Edited at: 20th Apr 2010 23:18
Thanks. When I use the shader method for that, i have to change the texture flag of the following line to 1:

from
load effect "shader.fx",3,0
to
load effect "shader.fx",3,1

and it works. However, i can't seem to have a shader without the bump map.

Didn't know about the Darkshader commands. I thought Darkshader was a plugin people had to buy. DBP recognises the commands but doesn't have the relevant help files! That method works too, albeit a tiny bit slower.

I shall see if i can use these Darkshader commands in conjunction with my shader. I don't need 1 of the 6 images, but an extra camera on top of the 6 i had already isn't that big of a deal! Ta.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Apr 2010 04:10
Quote: "That method works too, albeit a tiny bit slower."


Yes. I've noticed that. I intend to ask Lee about it one day.

Quote: "However, i can't seem to have a shader without the bump map."


Not sure I understand. If you mean the method which doesn't use the Dark Shader commands then I guess DBPro is using texture stage 1 for the cube map so the shader needs to have two textures declared (because of the way DBPro assigns texture stages to the textures declared in a shader). But why you need an actual texture I've no idea. I might look into this a bit closer when I get time.

Quote: "I thought Darkshader was a plugin people had to buy."


No. It's a stand-alone application for developing shaders - but some additional DBPro commands were introduced with it and it's those extra commands that are now part of DBPro whether or not you have Dark Shader.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 21st Apr 2010 05:34 Edited at: 21st Apr 2010 05:38
Thanks dude. I've managed to do a bit better. Was wondering how the shader decides what DBP texture to map to each texture in the shader, so I consulted my friend Google. You can specify which, up to a point!

the relevant shader code:


No stage 0 texture is required in DBP, but it automatically puts the cube map to stage 1 i gather.

Anyhow i'm happier because now my pixel shader is much shorter now. The whole shader:


Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Apr 2010 13:22
The texture stages specified in DBPro normally correspond to the ones used in the shader in order of declaration in the shader. Hence DBPro texture stage 0 corresponds to the first texture declaration in the shader, stage 1 corresponds to the second and so on. I'm not quite sure, without doing some simple experimenting (always worth doing yourself ), what happens if certain stages haven't been assigned textures in DBPro.

I believe the situation in C++ is more flexible - you can also match the textures by the name used in the shader which would have avoided your problem.

Just tested your shader. Nice. [Although the image seemed to be upside down. But I was guessing suitable values for the constants of course. ]

Puzzled by one thing. I didn't know you could do this on its own:



and expected a compiler error. The shader does compile with that line included but it seems the shader compiler simply ignores it as you can easily check by looking at the asm code listings when you compile with and without that line. Was it supposed to be doing something?
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 21st Apr 2010 17:42
Thanks. It's supposed to be upside down because of the way the portal works.

Interesting about that normalize thing. I was trying to normalise the x,y,z, components. Now i switched to

Out.Pos.xyz=normalize(Out.Pos.xyz);

but it doesn't seem to work any different. That's pretty bizarre!

I'm currently writing my shaders in Wordpad. Perhaps i should get one of those programs that you can test shaders with and look at the assembler code etc. Which one's best?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Apr 2010 18:15 Edited at: 21st Apr 2010 18:29
Quote: "Perhaps i should get one of those programs that you can test shaders with and look at the assembler code etc."


Definitely.

I use Dark Shader most of the time but I also have FX Composer 2.5. Dark Shader is better for compatibility with DBPro whereas FX Composer is free and has access to nVidia's large shader library. Dark Shader is very easy to use but, by the same token, is somewhat limited in scope. I find it very easy to go wrong in FX Composer and find the only solution is to exit and start again - but then I've only had it a week. By comparison I was able to use Dark Shader straightaway without getting stuck.

There used to be another free program called RenderMonkey but I gave up on that a long time ago as it seemed to be overcomplicated. It did have some nice shader demos though.

Edit

Quote: "Interesting about that normalize thing. I was trying to normalise the x,y,z, components. Now i switched to

Out.Pos.xyz=normalize(Out.Pos.xyz);

but it doesn't seem to work any different. That's pretty bizarre!
"


It makes a big difference for me. I guess it depends on the values you happen to use for the various constants.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 21st Apr 2010 19:11
Quote: "It makes a big difference for me. I guess it depends on the values you happen to use for the various constants. "

Yeah you're right. With the constants i use, there isn't a big difference, but there still is a difference.

It appears that

normalize(Out.Pos);

does nothing. It's odd that the shader didn't give an error.

I'll get that FX Composer thing. Fed up of portals now. Going to try some shadows. If I can get shadows and normal mapping working i can give one of my old games an awesome graphical update.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Apr 2010 22:17
Quote: "Going to try some shadows"


Have you seen the demo I posted a page or two back?
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 21st Apr 2010 23:01
Quote: "Have you seen the demo I posted a page or two back? "

Not yet. I'll check it out.

Today i've been playing with a shadow shader off evolved's site. Was initially way too slow, but basically i've cut out the multisampling and some other bits and pieces, and it seems it will be plenty fast enough to use in my game.

Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 22nd Apr 2010 23:50 Edited at: 23rd Apr 2010 00:39
I got my shadow shader working OK. Basically it's the one off evolved's site, with normal mapping and gloss, but without the multisampling, and i added a kind of trick to try and reduce some of the artifacts. Not sure how successful that was.

Also I was determined to use orthographic projection for the shadow - ie a true directional shadow. Not much point, because i could just move the projection point a long way away, and was a complete pig to do, but it's done. Used some code posted by Dmitry to make the DBP camera orthographic, then extracted the projection matrix from the camera (since apparently you can't set matrix elements). Anyway it means i don't need to use projective texture mapping, so the pixel shader is a little simpler. Couldn't see any difference in speed though, presumably because it only loses a couple of divisions! Tonight maybe i'll sort my sock draw in order of tog rating.

Runs at about 75fps on my machine. Will tidy up and post here shortly. Ortho (directional) and Perspective (point) camera versions.

Hey GG I checked out your shadow shader. Pretty nice. 100fps when i uncapped the sync rate. Faster than mine, but maybe that's because it doesn't have normal mapping. I am intruiged by the use of the question mark on the line comparing the two depths, used by both you and evolved.

float shadow = tex2Dproj(depthSample, In.lightProj).r < depth ? 0.0 : 1.0;

Mine is more like

float shadow = tex2Dproj(depthSample, In.lightProj).r < depth;

What does the extra part do?

There's loads of cool shadow stuff in this thread. Time for me to get reading.

edit: it's because my vsync was on! Now i get like 130fps, but then i only have 3 objects in my scene. Pauli's demo is hella quick for projecting 3 shadows!! But then i'm not sure what resolution he's using.

Here's an idea for multisampling - take the first sample, then instead of just comparing the two depths, look at the difference. Then have your extra samples moved away from the intial one by a random amount proportional to that difference. You could do the "randomness" using an extra texture map. That way you'd get soft shadow edges with width proportional to the distance from the shadow casting object, although they would be a bit noisy.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Apr 2010 02:49
Quote: "I am intruiged by the use of the question mark on the line comparing the two depths, used by both you and evolved.

float shadow = tex2Dproj(depthSample, In.lightProj).r < depth ? 0.0 : 1.0;

Mine is more like

float shadow = tex2Dproj(depthSample, In.lightProj).r < depth;

What does the extra part do?"


If the condition is true the expression returns 0, or 1 if false. Try the following very simple shader to see the difference.

Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 23rd Apr 2010 05:27 Edited at: 23rd Apr 2010 05:30
Thanks! That's actually pretty useful.

A demo of my directional lighting thing is attached to this 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: 23rd Apr 2010 12:44 Edited at: 23rd Apr 2010 12:49
Nice demo - 1090 fps uncapped.

One thing seems to need attention though. When I pressed shift to change the light's direction the shadows were inconsistent - it was as if the direction was calculated per satellite object according to the satellite's direction from the main sphere. Was that intentional or a bug?

Edit Not sure about that last comment having thought about the geometry a bit more (I was probably thinking of a positional light placed at the camera) - but I still seem to see a lot of shadow. Perhaps a side view would tell us whether it was OK?
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 23rd Apr 2010 16:41 Edited at: 23rd Apr 2010 17:28
Nice! Wow 1090fps. That's a lot. Bodes well for my game running OK on other people's computers.

I think that inconsistency may be an illusion. The camera used to view the scene is not orthographic. Actually the fov is fairly large. If you zoom out with the mousewheel, you can see the scene from a distance. If you want a side view, just move the mouse. It's an undocumented command.

Next up on my to do list with shaders:

Check out FX composer and see what ASM is generated for my code. Will be fun because I don't know assembler (yet). Another cool thing to go on my CV. Maybe can make some optimisations to the directional shadow shader.

Particles. I currently use explosion, smoke etc billboards. Work fine, but if the plain (plane) object intersects a solid object, there's an obvious discontinuity. My idea is to use the camera's depth mapth (for all solid stuff, that's rendered before the particles), and compare with the depth of the particle. Then from the difference i can work out how between the camera and the nearest solid surface.

Can I access the camera's z-buffer from within a shader that's rendering to that camera? A quick googling says no. I reckon i could use 2 cameras in the same place. Render all solid objects to the first as a texture with depth map. Then use this texture on a plane at the back of view of the second camera (basically resulting in copying the image across) and render all my special particles on top. Bit of a pig. If anyone has better ideas, i'm all ears.

I also think i can use Dmitry K's camera projection matrix code to arbitrarily skew the camera frustrum and so make flat mirrors, portals etc. Not a shader thing but kind of related to what's gone before. Anyway, lots to do!

BTW does anyone know if there's a way access object world matrices from DBP maybe using code similar to Dmitry's? Also, is there any way to set matrix4 elements? Isn't DBP open source now? I wonder if I can just look up the command "get matrix4 element" and see what adresses the data is located at. So much to do!

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Apr 2010 18:22
Quote: "I think that inconsistency may be an illusion."


So do I. I think you can safely ignore my earlier comment.

Quote: "Wow 1090fps."


Goes up to 1160 if I move the camera closer. Not sure why your demo is so much faster than mine - I'll have to study yours very carefully.

I liked your fix to remove the "weirdness" on polygons which are almost perpendicular to the camera. I couldn't see those artefacts on the spheres in my demo although I remember they were a problem at one point. Can't recall now whether I included code to remove them. Hope you don't mind if I borrow your fix if I encounter the problem again.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 23rd Apr 2010 20:06 Edited at: 23rd Apr 2010 20:08
Quote: "Goes up to 1160 if I move the camera closer. Not sure why your demo is so much faster than mine - I'll have to study yours very carefully."

Excellent! Good to know. Some of the optimisations are specific to having a directional light rather than a point one, but not all. Will be interesting to hear how you get on.

Quote: "I liked your fix to remove the "weirdness" on polygons which are almost perpendicular to the camera. I couldn't see those artefacts on the spheres in my demo although I remember they were a problem at one point. Can't recall now whether I included code to remove them. Hope you don't mind if I borrow your fix if I encounter the problem again. "

I think the wierdness happens in areas that should be in shadow but aren't, because of the pixellation of the depth buffer, and the z bias. If you have your depth range set up well so can set a small bias, and the resolution of your shadow image when projected over your scene is high enough, then there's not that much to fix.

Please feel free to use any of my code. All of my shaders thus far have been stuff i've ripped from either you or evolved and tinkered with, so it would be super wierd if i started "patenting" code. It's all probably been done a million times before anyway.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Apr 2010 12:19
Dr Tank

(Off-topic)

Are you the same person as Mr Tank. If so, does the change in title mean that congratulations are in order?

Also off-topic, I notice that the link in your signature is dead.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 24th Apr 2010 14:49
Same guy. Lost my old password. And now I have a doctorate in Physics - ho ho ho.

Geocities got closed ages ago. I should update my sig.

Got work now, but hopefully will get to screw around with shaders again in a couple of days.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Apr 2010 13:36
Quote: "And now I have a doctorate in Physics - ho ho ho."


Congratulations. Sounds like high-level stuff.

Quote: "Got work now, but hopefully will get to screw around with shaders again in a couple of days."


Good. Sounds like you won't be phased by the maths involved at times.
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 29th Apr 2010 14:21
Dr Tank your shader looks AMAZING in my opinion great work!

Green Gandalf
Thousand times thank you for your first shader explanations, thanks to you I just wrote my first shader today and it works. Yeah!
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 30th Apr 2010 04:46 Edited at: 30th Apr 2010 04:53
Quote: "Congratulations. Sounds like high-level stuff."

Thanks. My PhD was mostly experimental physics, but the grounding in maths and science and stuff does help out when i'm coding.

Quote: "Dr Tank your shader looks AMAZING in my opinion great work!"

Thanks very much.
With the directional shadow shader I overlooked an obvious optimisation in the shadow map vertex shader. Basically I calculate the exact same thing twice. No big deal because it's only the vertex shader, but i'll update it when I get around to it.

Quote: "I just wrote my first shader today and it works. Yeah! "

Well done dude! I was in that boat a few weeks ago, and assure you that fun times are ahead. Shaders are totally sweet.

Been toying with volumetric fog particles. Basically got it working, but it's kind of slow and looks a bit balls, and have been a bit confused about in what order DBP renders transparent things and that. So i've binned that.

Today i did this atmosphere/fog skybox thingy.



A cool thing about this is that the ground is on the shader too. Lots of ideas and stuff to play around with this. It may be highly inefficient/rubbish/physically incorrect. For example, would be better to have separate shaders for up and down halves of skybox. A work in progress but I attach it as is so you can have a play around.

I'm chuffed with this because back in the day when i was trying to make a flight sim in DBP, making a skybox work with fogging was a big problem. Maybe i'd like to throw together a little flight sim thingy.

After improving this, i'll look to writing a shader that does the same thing (but without the infinite ground tiling), but for regular objects in the world. Also i'll try out advanced terrain and see if i can get it to work seamlessly with this infinite tiled ground skybox.

Quick reference:

DBP code:


Shader code:

Attachments

Login to view attachments
zstarsales04
User Banned
Posted: 30th Apr 2010 06:34
Zstar Electronic Co.Ltd, Sell fire cards for DS/NDSL/NDSi, also have Wii, DSiLL, NDSi, NDSL, PSP2000,PSP3000, PS2, PS3, PSP go, PSP, Xbox360 accessories, all kinds of phones are available
www.zstar.hk
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 30th Apr 2010 15:37
Hmm I have a question, I have a little shader...

in PSOutput part I have few lines that use the STEP float, like here:

float4 col2 = tex2D(baseSample, float2(In.UV.x-step.x,In.UV.y-step.y) );

if I define the float in the same part, the PSOutput part, it works perfect

but if I move this line

float4 step = {0.01,0.01,0,0};

to the tweakables... it stops working And the object stops being rendered at all. I can't figure out way this happens.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 30th Apr 2010 15:59 Edited at: 30th Apr 2010 16:12
Try putting 0.0 instead of 0.

Failing that, usually my problem is forgetting to put semicolons in.
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 30th Apr 2010 16:27 Edited at: 30th Apr 2010 16:29
Hmm no it doesn't change anything, both 0.0 and 0 work in the PSOutput section but they don't when moved to untweakables section

true, I often forget about the semicolons... but this time they're there
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Apr 2010 22:39
It should work - despite the fact that "step" is an intrinsic function and shouldn't be used this way.

Without seeing the shader, I'd guess there's a coding error somewhere. Does the shader compile? You'll need DarkShader, FX Composer or similar for that.
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 30th Apr 2010 23:54
I'll download FX composer and try... here's the shader, I didn't want to show it cause it's very un-optimised and badly-written but I hope you'll forgive me

Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 1st May 2010 00:22 Edited at: 1st May 2010 00:22
If step is an intrinsic function, try replacing it with mystep or something.
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 1st May 2010 01:00 Edited at: 1st May 2010 01:06
I tried replacing it entirely with the transx and transy floats but still doesn't work...

AH!

I get it... I used it in FX composer and it seems I used "too many arithmetic slots"... seems it's so un-optimised it can't even run properly, bah!

Now that I know what's wrong I can just reduce the arithmetic operations...

Thanks for help guys!

edit:
wait, that's weird. When I change it back, it runs in DBP but doesn't run in FX Composer with the same error (too much instructions)

the shader that works in DBP only:



Sigh, this is weird (but I fallen in love with shaders anyway already!)
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st May 2010 01:33 Edited at: 1st May 2010 01:41
Yep. That's your problem. Welcome to the land of shaders. I've encountered the same problem from time to time.

A good place to start in your code is to replace things like



with



With those changes both versions of your code compile.

I'm not sure why one version of your original code compiles while the other doesn't. I suspect the problem is that your original instruction count was on the limit of the pixel shader instruction count so that if the built-in optimising rules of the shader compiler get applied in slightly different ways (because the two versions are slightly different) then one case might exceed the limit while the other doesn't. This is a guess though - it's very hard to find any details of what the shader compiler optimiser does precisely.

Another thing to check is whether any pixel shader computations can be moved into the vertex shader. The pixel shader is the usual bottle-neck and any pixel shader calculation that can be computed per vertex and then linearly interpolated rather than computed per pixel should be put in the vertex shader. Most shaders have loads of unused instruction slots in the vertex shader.

Edit I wrote the above before seeing your edit.

I suspect the FX Composer vs DBPro issue is to do with either the shader compiler version or the precise options used when they're called. The new version of FX Composer probably uses FXC10.exe whereas I guess DBPro uses the DX9 version FXC.exe. Again, the problem is probably made worse by the fact that your shader uses 64 arithmetic instruction slots which is the standard limit for PS2.
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 1st May 2010 11:45 Edited at: 1st May 2010 11:59
I get it Thank very much for help!

By the way, does that mean that vertex shaders are generally faster than pixel?

And does this 64 limit is counted within the PSOutput part or in whole shader?

One more question if I'm not being too tiresome, if I used if {64 instructions} else {64 other instructions} would it work? Or the overall count is calculated?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st May 2010 13:42
Quote: "By the way, does that mean that vertex shaders are generally faster than pixel?"


No. The real issue is the total number of instructions executed per sync - and this will depend on things such as number of vertices on screen, texture resolution and screen resolution. For Shader Model 2 (i.e. VS2 for vertex shaders and PS2 for pixel shaders) the texture and screen resolutions affect only the pixel shader, whereas the number of vertices affects only the vertex shader. To take an extreme example, if you have a simple plain (two triangles, 6 vertices) filling the whole screen then the vertex shader part has only 6 vertices to process, whereas the pixel shader has to process all the "texels" of the texture used and all the pixels in the screen. Hence the pixel shader is doing an enormous amount of work compared to the vertex shader, i.e. something in excess of (no. of pixel shader instructions)x(no. of screen pixels).

At the other extreme, suppose you are looking down at a high poly terrain from a great height and you are using a low screen resolution. You might be looking at, say, 1M polys (e.g. 6M vertices) but only 31K pixels. In such a case the vertex shader is doing more work. In practice you'd use some sort of level of detail procedure to reduce the poly count. The professionals will be watching these issues to keep their games running smoothly without loss of visual detail.

In practice it is the pixel shader that usually slows things up - but high poly models viewed from a great distance can slow things up unnecessarily because of the work the vertex shader is having to do.

Quote: "And does this 64 limit is counted within the PSOutput part or in whole shader?"


The pixel shader - for VS2 the limit for the vertex shader is 256. Here are the standard limits for Shader Models 2, 3 and 4 taken from the MS DX SDK:

Shader Model 2 (SM2)
vertex shader - 256
pixel shader - 32 texture + 64 arithmetic
Shader Model 3 (SM3)
vertex shader - 4 texture + 512 arithmetic
pixel shader - unlimited texture + 512 arithmetic
Shader Model 4 (SM4)
vertex shader - no restriction
pixel shader - no restriction

These are the minimum standard for compatibility with DirectX. Different graphics cards may support larger numbers. The SDK doesn't seem to mention limits for texture reads for SM4.

DBPro supports only SM1, SM2 and SM3 not SM4 - that will have to wait for DBProDX10 .

Quote: "One more question if I'm not being too tiresome, if I used if {64 instructions} else {64 other instructions} would it work? Or the overall count is calculated? "


No you're not being tiresome and no it wouldn't work .

You'd probably find that you've got 128 instructions plus the extra "if" instruction(s). Similarly with "for" loops - they get "unrolled" by the compiler. I'm fairly sure that's the case for SM2 and SM3. I don't know about PS4 yet . Once you've got a few shaders of your own working it's probably worth doing some experimenting and taking a peek at the asm listing produced by the shader compiler. This listing is easily obtained using Dark Shader and with somewhat greater difficulty in FX Composer (i.e. it took me some time to find out how to get it ).

I've often noticed that my attempts at "optimising" a shader have come to nothing because the shader compiler has already optimised the shader for me - but the compiler can't always notice that certain optimisations are possible. This is particularly true about moving instructions from the pixel shader into the vertex shader or vice versa.
Garion
16
Years of Service
User Offline
Joined: 7th Dec 2007
Location: Poland
Posted: 1st May 2010 15:09
I see. Thank you very much for the explanations It seems it will be a bit harder than I expected... By the way, what I'm trying to achieve is track screen position of few vertexes of my FPP hero's model (top of the weapon, hilt, arm, hand...), pass their direction of movement and velocity as vectors to the shader, render the model to camera, use the shader to perform limited directional blur within the areas between old vertex position and new... and get 'simple' motion blur effect without using 150 cameras or rendering the object multiple times.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 5th May 2010 23:50 Edited at: 5th May 2010 23:56
I'm still going with this. Having a bash with "Advanced Terrain". I managed to find a help file and have made some progress.



Basically there's atmospheric fogging, where the density decreases exponentially with altitude. Works for any objects, and is pretty quick. You can go into space and it gets dark and stuff. Works seamlessly with that ground/skybox shader thing i posted last time. That puts way too much in the pixel shader and I should work on it at some point.

Next task is to write a bespoke terrain lighting function, and to write vertex normals to the terrain so they can be accessed by the shader. I might just cheat and use normal and diffuse texture maps, although may be slower.

Things are looking up. Just found GG's "Shaders for Advanced Terrain" thread. Seems to have the info I need. Happy coding.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th May 2010 01:43
Looks like you're making excellent progress with this.

Quote: "That puts way too much in the pixel shader and I should work on it at some point."


Don't forget it sometimes pays to use pre-prepared textures to store the results of complicated calculations.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 6th May 2010 05:07
Thanks dude. I was pondering doing that, but making the texture maps I want would be hard work, since the calculations involve the 3d position of the pixel in space.

Maybe I could adapt the shader to prerender texture maps, using the UV co-ords for the output position. Might not be too hard.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th May 2010 12:57
Quote: "Maybe I could adapt the shader to prerender texture maps, using the UV co-ords for the output position. Might not be too hard."


Image resolution is likely to be the main problem.
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 6th May 2010 16:02 Edited at: 6th May 2010 16:05
Quote: "Image resolution is likely to be the main problem. "

The size on screen could be scaled arbitrarily via camera fov, zposition, or just multiplying the UV by a scalar. View could be rendered to arbitrary resolution image. I reckon it's quite doable. Quite busy, but will try to make a proof of concept in the coming week.

Edit: thinking you may have meant texture res of the final object. For sure I can't practically multiply landscape detail and large scale maps and hope to keep the same overall resolution.

So much could be done like that i expect- such as painting on objects from the camera view, using volume textures to prerender rocky stuff, making simple light maps with shadowing, normal mapping and stuff.

I made a program a while back using DBP's drawing commands to render rock textures to the UV map using a 3D perlin noise texture. Worked, but was hella slow. I expect a shader version would be much faster.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th May 2010 17:48
Quote: "Edit: thinking you may have meant texture res of the final object."


I think that was what I meant.

If you have a terrain textured with, for example, a single base texture plus a 12x12 tiled detail texture then if you are going to pre-render the whole terrain texture either you have lots of textures (assuming the terrain has lots of limbs) or a single enormous texture for the whole terrain. Perhaps I misunderstand what you mean?

Quote: "Quite busy, but will try to make a proof of concept in the coming week."


No rush on my account - but I'm looking forward to seeing what you come up with.

Glad to see you're considering using volume textures - a greatly underused feature. Sounds like it's a good time for me to tidy up my old volume texture creation code.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 7th May 2010 12:40
Hey shader people

First of all,
wouldn't it be time to create a 'Shaders'-board or something?
It's a pain to read through this looong (yet very useful) thread everytime...

So my question is:
Is it possible to draw a certain object over another object using shaders (like it was ghosted)?
Think about a lightmap object over a map object (with the exact same geometry, but different texture).

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
chafari
Valued Member
17
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 7th May 2010 14:00
Quote: "wouldn't it be time to create a 'Shaders'-board or something?"


You are reading something !!

cheers.

I'm not a grumpy grandpa
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th May 2010 16:22
Quote: "Is it possible to draw a certain object over another object using shaders (like it was ghosted)? "


Yes.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 8th May 2010 00:45 Edited at: 8th May 2010 00:47
Now these were two very constructive replies, thanks

I guess I'll have to go a bit into detail...
Like I said, I have two objects with the same geometry, but one is textured with the lightmap (from Cartography Shop 4).
So I have this very basic shader (for testing purposes):



This is applied to the first map object, that is textured.
Then there is the second map object, which is textured with the lightmap and ghosted (mode 1).
But then I get strange results with clipping errors all over the scene! You can see this in the attached screenshot.
When I don't apply any shader to the first object, it works fine.

How should I change the shader or the object setup?

Any help is greatly appreciated

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-04-20 01:50:52
Your offset time is: 2024-04-20 01:50:52