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
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 29th Sep 2010 23:28
Wonderful, perfect

The only issue is the edge shading switching off when the object shares the frame with any object that does not have the shader appplied, like the light ball (object 2) when zoomed out to maximum.

This wouldn't normally be a problem as I will export all my models with shaders enabled, but I do sometimes generate DBpro objects (planes and spheres mainly) procedurally at runtime.

Thanks once again Green Gandalf, if you ever find yourself in Frankfurt let me know and I'll give you a tour of Crytek
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 23:51
Quote: "The only issue is the edge shading switching off when the object shares the frame with any object that does not have the shader appplied, like the light ball (object 2) when zoomed out to maximum."


I'll look into that but offhand I've no idea what's causing that.

Quote: "if you ever find yourself in Frankfurt let me know and I'll give you a tour of Crytek "


Noted!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Sep 2010 01:11
Quote: "I'll look into that but offhand I've no idea what's causing that."


I've still no idea what's causing it but see if changing this line in the shader



to this



improves matters.

It looks like a problem related to transparency, possibly a DBPro bug, but I'm really not sure.
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 30th Sep 2010 11:10
That did the trick. You sir are a gentleman and a scholar.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Sep 2010 14:39
You're welcome. It was a good opportunity for me to learn some new tricks.

I've been working on a simple demo to reproduce the "edge disappearing" problem but haven't succeeded yet, in other words I haven't spotted the key step in your code which triggers it. I'm glad my fix solves it for the time being, but I'd be happier knowing the true source of the problem. I suspect a pipeline state is being changed behind the scenes somewhere and the shader uses that by default.

It's just as well my leg is still in plaster so I've got plenty of time to play with this.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 1st Oct 2010 15:11
Hi! I need some help with "set vertexdata diffuse" command.

My code is like this:

...and nothing changes! What Iam doin wrong!?

And second part of question:
How can i use vertex diffuse data in a shader? Is it possible?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Oct 2010 16:02
Quote: "What Iam doin wrong!?"


You forgot to add diffuse vertex data to your object - the default vertx format is 274, i.e. FVF_XYZ + FVF_NORMAL + FVF_TEX1=0x100.

The following works fine:



Quote: "How can i use vertex diffuse data in a shader? Is it possible?"


In reverse order:

Yes.
Depends on the shader. As a minimum you need to use the final DoNotGenerateExtraData flag in the load effect command. Unfortunately that interferes with things like bumpmapping which require tangents and binormals - but otherwise it should be OK.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 1st Oct 2010 16:30
Quote: "Unfortunately that interferes with things like bumpmapping which require tangents and binormals - but otherwise it should be OK."

So in that case I can't use normal map or other bump shaders from UltimateShadersPack?

Quote: "convert object fvf 1, 274 + 0x040"

Wow...
Okay, that works for sphere, but not for loaded .x

Also...does this conversion to fvf change object data like uv coords, stages or anything else? AFAK converting object to mesh will loose second uv stage...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Oct 2010 17:08
Quote: "Okay, that works for sphere, but not for loaded .x "


It works if used correctly. What's the FVF format of your .x object?

Quote: "Also...does this conversion to fvf change object data like uv coords, stages or anything else?"


No - unless you remove one of the components of course.

Quote: "AFAK converting object to mesh will loose second uv stage"


I don't know offhand. Have you tried a simple demo using a DBPro plain?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Oct 2010 22:27 Edited at: 1st Oct 2010 22:52
It's time for a new demo.

Well, more of a WIP really - plus an imminent DBPro bug report .

Sixty Squares recently asked the following:

Quote: "I was looking at Lee's Tip of the Month in April's Newsletter and noticed the new RENDERCOLOR semantic. Supposedly this would make creating effects such as bloom much easier, because the glow and blur passes could all be done with 1 camera.

Does anyone know how to use this semantic? I'd like to see what it can do"


and I responded:

Quote: "Actually I don't know what Lee meant by that Newsletter remark - I thought the RENDERTARGET semantic was already supported because the extra stuff that came with DarkShader was included in DBPro a long time ago."


Well I know now. He extended the functionality to simple object shaders - and here's a simple demo. The demo is rather crude but should illustrate the basic idea. It's motivated by Wilf's use of a cartoon shader discussed earlier. The aim is to give dark edges to a low poly facted model such as cubes, low poly spheres, etc.

The shader uses two passes: the first pass renders each face in a different colour according to the "faceted" normals. The result of this pass goes to an intermediate render target as Lee described in the Newsletter. The second pass then reads this texture and uses a Sobel filter to detect the edges, i.e. the lines where the colours change. This "edge" is then applied to the object's main texture to give the final result.

The demo allows you to see the objects with the intermediate colours - and this is where the DBPro bug appears. The shader has two techniques but with intermediate render targets it seems that DBPro will only use the first technique and fails if you try to use the second. I've defeated this obscure limitation by reversing the order of the techniques in a second copy of the shader.

I'll add a couple of screenshots later.

Edit Here they are. As you can see, the edges need cleaning up. Any suggestions?

"A" shows technique t0 with the edges, "B" shows technique t1 with the intermediate render target's colours, i.e. t1 is effectively just the first pass of technique t0.

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: 1st Oct 2010 22:46
Screenshot showing the two techniques for previous post.

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 4th Oct 2010 12:24
Thanks for help, Green Gandalf!

Quote: "It works if used correctly. What's the FVF format of your .x object?"

I've soved the problem - it was wrong frame export settings.
Now I can load x without special flags, and apply vertex paint on it. So the last question - how can I access to vertex color data in shader?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Oct 2010 12:37
Quote: "So the last question"




Quote: "how can I access to vertex color data in shader?"


I believe you need to add something like



to the vertex input structure. It might be "diffuse" instead of "color0" - not sure right now but too busy to check. If you still have problems with those let us know and I'll put together a simple demo.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 19th Oct 2010 02:51 Edited at: 19th Oct 2010 02:53
Hello,

I'm having trouble implementing a simple screen quad shader!
Here is the shader code:



Then I apply this effect to a plane object. The object also has a camera rendered to an image as texture in stage 0. When I paste the camera-image to the screen it looks fine, but the quad only shows the backdrop! I know the camera is pointing at something visible.
Also when I quickly hide and show the quad using a key state, the quad shows the correct image for a split second (one frame I guess) but then switches back to the pure backdrop color! I should mention that I use sync masking, but I'm sure the camera that renders the quad texture and camera 0 are both sync masked (the quad camera has a different backdrop color then all others).
The problem is a little hard to describe, I hope someone gets what I'm talking about
Is the HLSL code correct at least?

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Oct 2010 14:00
Nothing wrong with your shader - and since you haven't posted any code it's hard to know what to suggest.

However, you might find the following demo code helpful using your shader (with one trivial change):



Your shader (with trivial change so we know it's working):



You should see red objects against a black background.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 19th Oct 2010 15:01 Edited at: 19th Oct 2010 15:03
Ok, thanks.
I just had a quick translation of your code to DGDK to see if it works and it does. Here is the code:



I see some red spheres on a black backdrop.
I already had the screen quad shader working in a simple test environment. But I want to use it in a very complex setup in a project of mine, where I was already syncing two cameras. Now I wanted to add this screen quad for post processing effects. That's why I couldn't post code, it just would have been to much. I'll try to filter out the relevant commands and post something more detailed.
Is it necessary to hide/show the screen quad/scene objects?

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Oct 2010 16:26
Quote: "Is it necessary to hide/show the screen quad/scene objects?"


Yes - otherwise the scene objects might appear in front of your screen quad or vice versa. You can achieve the same result using set object mask although I haven't used that myself.

Quote: "I'll try to filter out the relevant commands and post something more detailed."


That might identify the problem for you of course.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 19th Oct 2010 22:21 Edited at: 19th Oct 2010 22:22
Ok, I just had something in my camera masking order messed up. Now it works! Post processing - here I come

Quote: "using set object mask "


Unfortunatly that command (aswell as the RENDERCOLORTARGET thing) aren't available since DarkGDK is stuck on version 7.4. But I was told that's gonna change soon...

Thanks for your help GG, I think there should definitly be a thread called "Ask Green Gandalf", that would save many people lots of trouble

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Oct 2010 22:30
I'd be tempted to post questions on that thread too.
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 20th Oct 2010 12:43 Edited at: 20th Oct 2010 12:45
Quote: "Unfortunatly that command (aswell as the RENDERCOLORTARGET thing) aren't available since DarkGDK is stuck on version 7.4. But I was told that's gonna change soon..."


Don't know about RENDERCOLORTARGET, but you can use Set Object Mask in DGDK:



Use it like this:



I think it was Mista Wilson the one that published that function, as it is true that it is currently missing from the DGDK libraries

_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 20th Oct 2010 13:39 Edited at: 20th Oct 2010 13:43
Quote: "but you can use Set Object Mask in DGDK"


Oh thanks Morcilla! I thought it was dbSetObjectMask() ! Still doesn't make sense why the "db" is missing though...

I have another question:

How do I construct an orthogonal matrix? I know it's not directly HLSL related, but I need it for Shadow Mapping with a directional light.
So far I've done this: (sorry for posting DarkGDK code again, it's almost the same in DBPro)



As you can see I pass the constructed orthogonal matrix as the view matrix to the shader to multiply a vertex position:



And this matrix is used in the final scene vertex shader to get the sample pos (to get the stored depth from the shadow map produced by the depth technique):



Don't know about this one though, but the main issue is to get the proper depth information (from the depth technique) first. I'm pasting the resulting image from the depth technique to the screen for testing. As you can see in the first code snippet I'm only rotating the orthogonal matrix, because I don't know how to position (translate?) this matrix! And that's how it looks right now: the depth view is at (0,0,0) as you would expect. Somehow the rotating is messed up too, as only the Y rotation is working (although negated (-), don't know why!) and the X rotation has no effect!
I should note that my shadow mapping shader is already working when I pass a dbViewMatrix4() as the lights view matrix (mLightV). I think it's all about constructing the proper orthogonal matrix at this point.

Complicated stuff, I know, but any advice is greatly appreciated!

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Oct 2010 17:18
Quote: "Somehow the rotating is messed up too, as only the Y rotation is working (although negated (-), don't know why!) and the X rotation has no effect"


I'm not that familiar with DGDK but generally its commands mimic the corresponding DBPro commands and I suspect you're a victim of another DBPro Help file obscurity. I believe the commands create the rotation matrix corresponding to the angle you've given - NOT applying that rotation to the existing matrix. The result is that your Y rotation matrix overwrites the X rotation matrix.

As for the negation, it's just a matter of experimenting till you get it right since the Help files give no clue as to the direction in which the angle is measured.
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 20th Oct 2010 17:35 Edited at: 20th Oct 2010 17:37
Quote: "I thought it was dbSetObjectMask() ! Still doesn't make sense why the "db" is missing though..."

It is because it doesn't belong to DGDK natively. That is, you still have to include the body of the function (posted above) in your program, it is up to you to name it as you like, although I'd reserve the db prefix to the DGDK official functions.

Not sure about the orthogonal matrix. It goes beyond my knowledge if it needs to be rotated, translated and/or scaled.
Apart from that, if you feel lucky you can try this way of rotation, based on the XYZ axis and angles:



I'm afraid I cannot help any further about it. I'd recommend you to specifically write another thread for this. Also you'll get the best answers if the code is written in DBPro. Good luck

_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 20th Oct 2010 18:39
Quote: "I'm not that familiar with DGDK but generally its commands mimic the corresponding DBPro commands"


Exactly. dbMakeObjectCube() is MAKE OBJECT CUBE and so on... (with the same set of parameters in general)

Quote: "I believe the commands create the rotation matrix corresponding to the angle you've given"

Quote: "you can try this way of rotation, based on the XYZ axis and angles"


This makes sense, didn't think about that. Would have been nice if they had written a single word about that in the documentary
Your code looks interesting Mocilla, I'm going to try this.
No idea of how to apply position (translation) to a matrix? I know there is dbTranslateMatrix4() (= TRANSLATE MATRIX4 I guess), but the help file says "This command will produce a translation matrix". Do I have to multiply my orthogonal matrix with the produced translation matrix?

Quote: "I'd recommend you to specifically write another thread for this"


I'm gonna try a few things out and if nothing will do the trick I will create a thread. Would you advise me to create it on the DBPro or the DarkGDK board? I think more people will recognize it on DBPro and the commands are the same...

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 20th Oct 2010 22:28 Edited at: 20th Oct 2010 22:30
Ok, first of all I've got it solved!
It's as easy as building an orthographic matrix and use it as projection matrix, not view matrix! D'OH
Well, at least I've gathered some knowledge about matrices
(besides having directional shadow mapping of course ).

Second, here is how to properly rotate a matrix:



I hope that's correct DBPro code, as I just translated it "freestyle"

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 00:30 Edited at: 12th Feb 2011 14:08
Quote: "Ok, first of all I've got it solved!"


Hope you're going to post the final shader for us.

How does your "YPR" method differ from using separate X and Y rotations? Also, can you do it in one step using "YPR"?

What are "yaw", "pitch" and "roll" anyway? (I can guess but I don't know.)

[Edit: fixed tags.]
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 21st Oct 2010 01:06 Edited at: 21st Oct 2010 01:08
Quote: "Hope you're going to post the final shader for us."


I can't really do that, because it's part of a large shader that I've been developing for quite some time, and there is lots of program code involved. But here are the essential things I did to get it working (I think there is an older shadow mapping demo I once made on this forum, which can be changed):

Building the matrices:



Transform vertex and get depth in vertex shader:



Get sample position on the shadow map in the scene vertex shader:



The rest is standard Shadow Mapping. There isn't really much to change.

Quote: "How does your "YPR" method differ from using separate X and Y rotations?"


The rotate YPR command is basically what Mocilla suggested above, but without all the matrix operations. The important thing to note in my method is that I separated the axis rotations (and matrix multiplications) to get the same way of rotation like the normal camera would rotate.

Quote: "What are "yaw", "pitch" and "roll" anyway?"


I think these words come from aircrafts or something.
Yaw is the rotation on the Y axis, pitch is X rotation and roll is Z. But these depent on each other, like a rotating airplane rolls always along its length axis (from front to back).

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 13:06
Thanks. I'll see if I can get that working. It would be interesting to compare that with the approximation of a distant positional light and narrow FOV which has been suggested.

Quote: "because it's part of a large shader that I've been developing for quite some time"


Sounds like a potential performance eater.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 21st Oct 2010 13:50
Quote: "It would be interesting to compare that with the approximation of a distant positional light and narrow FOV"


I think a directional light works better, because the shadow doesn't "move" with the camera. After some tweaking you can get quite nice results.

Quote: "Sounds like a potential performance eater."


Well, I'm developing my engine with low spec. machines in mind. So far I have the Shadow Mapping, Rim Lighting, Refraction and the usual stuff like Normal Mapping and Specular Lighting running along ODE physics, a GUI, Post Processing and a particle system. And that with about 30-60 fps (depending on the settings) on a 1,8 Ghz laptop with 512 Mb RAM! Plus I'm allowing lots of customization to optimize performance and quality.
I've attached a screenshot showing some features, a Work In Progress thread will follow soon.

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

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: 21st Oct 2010 21:22
I'm impressed. I can't get anything like that on my laptop (1.8GHz, 894MB RAM).

What screen resolution are you using?
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 21st Oct 2010 22:02 Edited at: 21st Oct 2010 22:08
The resolution of the screenshot is 800 x 600.

With every feature turned on, a shadow map resolution of 1024 x 1024 and full screen Gaussian blur (9 samples in each direction) it drops to about 20 fps on my laptop.
With shadow mapping switched off, it's running at about 55+ fps almost all the time without the blur. If a medium blur (5 samples) is turned on then it's at about 30 fps.
But I've designed it with lots of customization options right from the start (Shadows On/Off, Refraction On/Off, Blur On/Off, Shadow Quality, Blur Quality, etc). That's one of the reasons why the shaders are so big, but you can adjust the engine to just do what you need and still run on older machines (or netbooks).
Here is a screenshot showing refractive material.

Edit: I just found out that it's running at fluid 30 fps when at a resolution of 640 x 480 (windowed or fullscreen) with high resolution shadows, medium blur and refraction, so that's quite nice! Keep in mind that there's also real-time physics running and GUI, then that should be enough for some cool netbook games

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

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: 21st Oct 2010 22:14
Looks good - and rather weird.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 22nd Oct 2010 02:29
Quote: "Looks good - and rather weird"


Have you never seen a wall of red glass on a giant chess-board?

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 1st Feb 2011 22:27
is there any shaders that can make shadows with pixel shader 1.4 or less? i don't have pixel shader 2.0, but i need shadows in my game. I can't achive any shadow mapping even with set shadow mapping commands in dbp, because of pixel shader 1.4.
nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 2nd Feb 2011 19:30
can someone help me out with this i want to make shadow mapping shader that will work with pixel shader 1.4

i have tried to replicate evolved's shadow mapping shader but i get same result i get gray screen with all shaders that use pixel shader 2.0

here is what i did

but it doesn't work anyway, shaders are hard stuff for me some parts with shaders are very confusing.
nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 2nd Feb 2011 19:30
can someone help me out with this i want to make shadow mapping shader that will work with pixel shader 1.4

i have tried to replicate evolved's shadow mapping shader but i get same result i get gray screen with all shaders that use pixel shader 2.0

here is what i did

but it doesn't work anyway, shaders are hard stuff for me some parts with shaders are very confusing.
enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 11th Feb 2011 14:11
Dunno much about this, but...



... is it just me or are you multiplying by an empty value?

enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 11th Feb 2011 14:22 Edited at: 11th Feb 2011 14:25
I just started playing with shaders, so I might be wrong in my previous comment.

Here's a short video of my first shader attempt...

I looked a bit at GG's worm shader while making it, so thanks to you GG.

I have attached the project to this message, including the shader, example dbpro program, and the texture...

EDIT: BTW GG, both this shader and your worm shader seems to work fine even if compiled with VS_1_1, is there any specific reason you set it to compile with VS_2_0?

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: 12th Feb 2011 14:07
Quote: "Here's a short video of my first shader attempt"


That's an impressive first attempt. The demo runs at up to 4000 fps on my machine - it makes the GPU scream at me.

Quote: "BTW GG, both this shader and your worm shader seems to work fine even if compiled with VS_1_1, is there any specific reason you set it to compile with VS_2_0?"


Habit mainly - and it saves me worrying about the occasional obscure limitations of the various versions of SM1.

There are situations where the earlier shader models are more efficient than the later ones - but I suspect those tend to be GPU dependent and are not usually worth worrying about. I tend to stick to SM2 (or occasionally SM3) for simplicity.

I vaguely recall that some GPUs don't support some versions of SM1 - but I'm not now convinced the problems people reported weren't caused by something else.

In short, if SM1 works for you then fine.

Glad to see you getting into this. Let's hope you produce more like this.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Feb 2011 14:12
Quote: "... is it just me or are you multiplying by an empty value?"


Should be fine if he's passed the value from his DBPro code, i.e. using set effect constant matrix.
enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 12th Feb 2011 14:58
Is there any way in a shader to find out how big the object is, without having to pass in object width()/height() from DBPRO?

Ie. is there a Semantic or something for max/min coords of the object?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Feb 2011 01:43
Not as far as I know.

But it is quite easy to pass that info yourself as a vector4 containing { SizeX, SizeY, SizeZ, any}. You could replace the arbitrary 4th element with something useful if you're lucky.
enderleit
16
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 13th Feb 2011 12:35
Well... what if you're using an effect on different sized objects?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Feb 2011 16:12
There are at least three ways of dealing with that.

The simplest is to use a separate copy of the effect for each object. Then you can pass object specific information to the shader. That solution becomes difficult if too many copies are needed though.

A second method is to use a different small texture, e.g. 4x4, for each object. This texture would contain coded values of the desired information. Then do a texture lookup in the shader to decode and extract the necessary information.

A third method which I'm currently using in a terrain system I'm working on is to reserve some of the vertex data for this purpose. That's a bit of a "sledgehammer to crack a nut" when each vertex has the same value but is a workable option.

I've used all three methods in different situations so a lot depends on what exactly you are trying to do.

I suspect these workarounds are needed because of DBPro limitations. I doubt C++ would have these particular limitations.
Duke E
15
Years of Service
User Offline
Joined: 10th Mar 2009
Location:
Posted: 14th Feb 2011 09:37
Quote: "The second method..."

So a 4x4 texture is the most you can use as the float conversions in extracting data from more pixel rows/columns will be hard to overcome? To clarify my question, a 4x4 texture can be used to get 4x4 values (rgba) in the range of 0-255, using the coordinates 0.0, 1.0, 0.1 and 1.1 right?

Quote: "A third method which I'm currently using in a terrain system I'm working on is to reserve some of the vertex data for this purpose. That's a bit of a "sledgehammer to crack a nut" when each vertex has the same value but is a workable option."


My pointsprite particle system shader uses this method pretty much to the max. Cant use a setup with several copys of the shader for every emitter (200 shader copys eek), mostly due to compile times and memory use. Also i want all the vertex transformations in the shader code in a fire and forget type setup. Only time data changes in the shader is when a new emitter is assigned.
I make a "vertex stream" that is a object with a FVF containing all 8 UV sets and i pack the data x2 in every UV position so i can pass 16 parameters for every vertex/particle (range 0-0x8000) to the shader trough the object.
(then there is the tracking of the Object IDs inside the shader, i wont go in to that here as it would be confusing)


DBPro pack data that is sent to the shader:



Shader unpack:


Regards
Duke
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 14th Feb 2011 09:52
I haven't tested, but maybe using Kaedroho's plugin for instant drawing (http://forum.thegamecreators.com/?m=forum_view&t=176912&b=5) might help. If you set your constant and then call the drawing command, it might work just like in native Direct3D. If so, I think it could be a real breakthrough for shaders in DBPro.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Feb 2011 11:57
Quote: "So a 4x4 texture is the most you can use as the float conversions in extracting data from more pixel rows/columns will be hard to overcome?"


Why do you say that? There's no restriction to 4x4. You can use any size that your hardware supports.

You can avoid the problems with float conversions by turning off the filtering in the shader texture sampler using something like this:



You then just need to make sure you access the middle of the pixel when you look it up.

Quote: "I make a "vertex stream" that is a object with a FVF containing all 8 UV sets and i pack the data x2 in every UV position so i can pass 16 parameters for every vertex/particle (range 0-0x8000) to the shader trough the object.
(then there is the tracking of the Object IDs inside the shader, i wont go in to that here as it would be confusing)"


Sounds interesting. Any chance of letting us know the "confusing" details?

Quote: "If you set your constant and then call the drawing command, it might work just like in native Direct3D. If so, I think it could be a real breakthrough for shaders in DBPro."


I hope you look into that - it sounds potentially very useful.
Duke E
15
Years of Service
User Offline
Joined: 10th Mar 2009
Location:
Posted: 14th Feb 2011 13:16
Quote: "You then just need to make sure you access the middle of the pixel when you look it up."


Yeah that would work, setting the filtering to point. Thanks, I did not think of that

Been thinking of a way to optimize the parameters. My VS is struggling to keep in the instruction limits of shader model 2, I might save some instructions and gain features with the texture stage method

Quote: "Any chance of letting us know the "confusing" details?"

Absolutely, however the shader is not much use without the DBPro code that creates the emitter objects and handles the parameter injection. That code is not for public consumption just yet =)

Regards
Duke
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Feb 2011 18:42
Quote: "My VS is struggling to keep in the instruction limits of shader model 2"


You must be making it work hard. Perhaps some optimisations are possible? I usually find the PS is the bottle-neck.

Quote: "I might save some instructions and gain features with the texture stage method"


The downside is that means more work for the PS - unless you move to SM3 which allows some texture lookups in the vertex shader. SM3 could be useful for this particular problem.

Quote: "That code is not for public consumption just yet"


I know the feeling. I'll try to be patient.
Duke E
15
Years of Service
User Offline
Joined: 10th Mar 2009
Location:
Posted: 15th Feb 2011 00:10 Edited at: 15th Feb 2011 00:11
Here is the shader code, i'll clean up the DBPro code and post a demo asap

The code i hinted about for the objects use arrays in the shader (float4 EmitterIDInstance[50] and other arrays to keep emitter lifetime, random seeds) that gives me 50x4 unique emitters to work with.

Thats the first part, then i have to keep track of the object and what vertice of that object the shader is working with as i use the same shader for all the emitters.

The DBPro code only sets parameters on emitter creation, some are set when an emitterobject is reused. The emitter object is created in a memblock and all the UV stages possible are added with FVF.
When a new emitter is assigned i set the object number of the emitter as one of the UV coordinate values along with the other parameters for that emitter. 8 UV stages gives 16 parameters but i needed more so some are packed.

The DBPro code have to set the shader array that tracks the object number, this also happens when an object is reused. Then set the Vector4 and pass it to the shaders object ID tracking array (EmitterIDInstance):

null = Get Object Effect(ActiveObject ,-1)


Set Vector4 EmitterPointerVector,Emitter(IndexToChange).ControllerID,Emitter(IndexToChange+1).ControllerID,Emitter(IndexToChange+2).ControllerID,Emitter(IndexToChange+3).ControllerID

Set Effect Constant Vector Element "EmitterIDInstance", floor(IndexPos) ,EmitterPointerVector


(have to repopulate all four floats in the float4 it just reassigns the previous values for the other floats).

The "for" loop in the shader then compares the vertex being processed so it corresponds with the correct object/emitter (Stored in the vertex UV data) and then transform the particle/vertex according to lifetime, movement and so on.

Shader code, love to see if there are some optimizations that can be done :



Quote: "The downside is that means more work for the PS - unless you move to SM3 which allows some texture lookups in the vertex shader. SM3 could be useful for this particular problem"


Yeah i feel it's going to be hard to use texture lookups for the parameters as VS model 3.0 is needed, maybe my obsession with keeping it VS 2.0 is misplaced.

Quote: "I usually find the PS is the bottle-neck."


I try to use as little of the PS as possible cause of the bottleneck . I would like to have all of the rotation matrix transformation in the VS (did so in my first version, was based on your Vertex Particles demo ), however with Point Sprites it is not possible, have to do half and half in the VS/PS.

I made a video for now, it's my Small gnomes game that i have been working on for two years, dunno if it ever will be completed as i keep working on the details too much and change things .
Anyways it's got a lot of particles and explosions, every explosion is made up of seven separate emitters: Blast concussion, fireball, flash, fire sparks, smoke trails, spawling cloud and residual smoke. Oh and a dirt cloud if it hits the ground.
Think i saw it top almost 4k particles at one point . My old CPU only particle system would never have been able to run this, especially as i converted it from Dark Physics to use Newton physics that takes a lot of CPU in itself.



Regards
Duke

Login to post a reply

Server time is: 2024-04-18 23:44:27
Your offset time is: 2024-04-18 23:44:27