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
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 20th Feb 2011 02:48
Haha... that looks totally awesome Duke E...

Is it just me though, or are the little guys invincible?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Feb 2011 22:09
Quote: "Haha... that looks totally awesome Duke E... "


Add my vote to that.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 22nd Feb 2011 15:09 Edited at: 22nd Feb 2011 15:10
Hi,
I need some help with a shader I'm working on. Basically I want to do screen space ambient occlusion, but I'm already having problems with some basic things.
The steps for the shader should be like this:

1) Render the scene, output position in view space to a render target as color.
2) Render the scene, output normals in view space to a render target as color.
3) Render full screen quad, use previously created render targets for post processing.

I think my major misunderstanding is in how to setup the render targets. Maybe someone can guide me a bit...

Here is how I create the textures and samplers in the shader:



Here is the (only) technique:



I then applied the shader effect to all scene objects AND the screen quad, which is textured with a camera image, correct?
I thought using the new RenderColorTarget semantic could save me all the camera masking/syncing stuff...
I also thought about taking the quad pass out of the technique to a second single pass technique, which is used for a second sync.

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: 22nd Feb 2011 17:55
I suspect that in DBPro the rendertargets work per object. This means, I think, you can only use your method for an object if the final pass needs only the pixels previously rendered for that object. However, if you're doing occlusion I guess you need to look up different parts of the scene. I'm not very sure about this though - in fact a simple test suggests I might be wrong .

If I've understood you correctly you need two intermediate rendertargets for the whole scene, one for the position map and one for the normal map. I'm not convinced that the separate object renders will render to the same rendertarget. This can be tested by a bit of experimentation though.

I'm also puzzled by your use of the screen quad. Why do you need it if the other objects already use the same passes?
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 23rd Feb 2011 01:31 Edited at: 23rd Feb 2011 01:36
Quote: "you need two intermediate rendertargets for the whole scene, one for the position map and one for the normal map"


That's right, so because what I'm trying to do will happen in screen space, I need the same view of the scene three times: one which shows every scene objects positions in view space, one with the normals in view space, and the final should be just the quad. The quad then has a technique to combine the to views (and the data they show) to the final rendering.

I hope that makes sense to you. To illustrate my problem better, I've writen a little test application and attached it to this post.
It contains the scene objects shader and the post processing shader, which is applied to the quad (the quad is textured with its own camera image as usual). If you switch the quad shaders technique (return key), you'll see that the output is just corrupted mess. This technique should display the contents of the render target sampler!
So think I'm doing something wrong with feeding the render target image with content. What could I be doing wrong?

Edit:
Here is the program code (C++ & DarkGDK):



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: 23rd Feb 2011 01:41
I'm away for the next few days but If I get a minute I'll have a quick look. For now though I'm a bit confused since your previous post said:

Quote: "Here is the (only) technique"


Quote: "I then applied the shader effect to all scene objects AND the screen quad"


(my emphases).

Quote: "What could I be doing wrong?"


Don't know whether you are for now - there is a recognised bug in the DBPro rendertarget implementation which causes garbage if different techniques use different numbers of passes. Don't yet know whether that is your problem.

I'll look into this as soon as I can - Sunday?
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 23rd Feb 2011 01:47
Yeah, no problem at all! Take your time!

Quote: "since your previous post said"


When I wrote my previous post I thought it would be managable with only one technique for the scene and the quad - that was dumb
Now I have separate shaders (and techniques) for the scene and the quad.

But the above example application focuses on the render target issue. I have to clear that one first.

Quote: "there is a recognised bug in the DBPro rendertarget implementation which causes garbage if different techniques use different numbers of passes"


Thanks for the hint! I'll have to take a look at it, because the current ouput looks a lot like there's something wrong with the render target support.

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 23rd Feb 2011 01:57 Edited at: 23rd Feb 2011 01:58
Wow, your above hint helped a lot already
I've had a look at your bug report.
I then just removed a dummy technique, so there's now just the one technique that uses the render target stuff, and that seems to work, no more corrupted output!
Thanks again!

Now I'll have to take it a few steps further...

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: 23rd Feb 2011 11:46
Great! Sounds like you're making excellent progress on your own. I can relax while I'm away now.

The dummy technique (or was it a dummy pass?) is the workaround that Lee suggested to me till he remembers to fix it properly.
nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 23rd Feb 2011 16:31
here is one awsome shader that i found with searching it supports normal specular and emissive maps, but can someone modify this shader so it also uses height maps?

revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 23rd Feb 2011 19:14
Yep, Here ya go:

The heightmap is on stage2 like most other parallax mapping shaders.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 8th Mar 2011 22:56 Edited at: 8th Mar 2011 23:25
Hey, I have another issue with the RENDERCOLORTARGET semantic.
I had the idea to write a shadow mapping shader, that doesn't need all the light camera and sync mask stuff. I thought it should be possible to do it like this in one technique:

- first do the depth pass and output depth as color to a render target
- do the final scene pass that outputs to the screen and sample the depth from the render target

Ok, so I thought that this technique should be working, but it doesn't. Instead I get a strange output that looks like the scene is rendered twice but with wrong transformations and the undesired copy of the scene is just colored like the backdrop (or just transparent thus showing the camera backdrop).
To me it looks like there is some kind of bug in the RENDERCOLORTARGET thing but maybe I'm just handling it correctly.
I have attached a screen shot and here is some temporary shader code (no shadow mapping done here, I first need to sort out the render color target stuff).



Edit:
I just found out that the dimensions of the render color target determine how much of the screen is covered by the false image of the scene. It looks like the render color target is pasted into the final render.

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: 8th Mar 2011 23:37
I've wondered about trying this myself but wasn't convinced it would work so didn't actually try it. It would be useful to have a definitive answer.

Have you tried just outputting the depth map? That might give you a clue. Use a sphere between the light and a high poly plane. Position the objects so that the main camera can see both.

How does your shader know that the camera position is different for the two passes? I have a feeling that camera clipping could be the problem since both passes use the same camera. In other words it might not be enough to use the two separate view matrices. Not sure till I try it myself though.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Mar 2011 23:45
Quote: "I just found out that the dimensions of the render color target determine how much of the screen is covered by the false image of the scene. It looks like the render color target is pasted into the final render."


Just seen your edit (I take too long to compose my posts ). I experienced something similar at one point. Can't recall whether it was related to the render target bug though. If it was there should be a fix. I'll try to find time to look into this over the next few days - unlikely unless it rains though.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 8th Mar 2011 23:49 Edited at: 9th Mar 2011 12:41
Quote: "Have you tried just outputting the depth map?"


Yes, I have changed the final pass pixel shader, so it would texture the objects with just the depth image (the render target). The depth image seems to be okay, but still the strange duplicate of the scene is shown.

Quote: "How does your shader know that the camera position is different for the two passes?"


In the final version of the shader I will pass two matrices to construct the lights view of the scene in the shader. One is a projection matrix and the other is the view matrix, which is a left handed look-at matrix constructed from the light position and target.

Quote: "I have a feeling that camera clipping could be the problem since both passes use the same camera."


I plan to pass an orthographic projection matrix for the light for directional lighting. You can set the clipping range for such matrices when creating them.
But anyway right now I'm just using the users view and projection matrices to create the depth image for testing purposes. Don't know if that affects the render color target issue.

Edit:
Ok, I've made a little progress. When I add a ZEnable switch to the technique the duplicated render doesn't appear (I guess it's still there but drawn behind the final pass). But unfortunatly I can't get the correct depth from the first pass, because Z depth is disabled! Any ideas how I could solve that?

Here is the changed technique:



Edit 2:
After some testing, I think it has something to do with the projection matrix. I need to create an orthographic projection matrix like this:



That was all working correct when I was using a separate camera for the light. It is at least constructing the matrix correct, because if I display the lights view with this projection matrix to the screen it looks as expected. But when using it with the two pass technique there is the duplicate render as mentioned before. If I change how I construct the projection matrix to this:



Then the false render duplicate does not appear! I have no clue why but I guess this shows that it has something to do with the projection matrix in combination with the render color target.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Mar 2011 18:48
Does your shader have more than one technique? If so, do the techniques have the same number of passes? The render target bug arises when a shader has different numbers of passes in its techniques.

Quote: "Quote: "How does your shader know that the camera position is different for the two passes?"

In the final version of the shader I will pass two matrices to construct the lights view of the scene in the shader. One is a projection matrix and the other is the view matrix, which is a left handed look-at matrix constructed from the light position and target"


I was referring to the culling that takes place before the shader sees the scene. But it seems your problem is a different issue entirely. The annoying thing is that I've seen the same symptoms with one of my demos and I can't remember which it was or what the cause and solution were. It's just possible it was machine specific, e.g. desktop versus laptop but I suspect it was a coding error.

Quote: "Then the false render duplicate does not appear! I have no clue why"


Neither have I. Unfortunately I don't usually document my mistakes - this is one of those occasions when I wish I did. (But then I'd never have time to do anything. )

Could you prepare a simple DBPro demo of the problem so I can play with it? In the meantime I'll delve into my files to see if I still have a copy of the demo that had similar symptoms.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 9th Mar 2011 21:27 Edited at: 9th Mar 2011 21:31
Quote: "Unfortunately I don't usually document my mistakes"


So that makes two of us

Quote: "Does your shader have more than one technique?"


I'am already aware of that bug so I have only one technique with two passes.

Quote: "It's just possible it was machine specific"


I hope not because I only have one machine!

Quote: "Could you prepare a simple DBPro demo of the problem so I can play with it?"


Unfortunatly I don't have DBPro anymore, I'm coding with DarkGDK in C++. But I put together the most simple demo I could think of that produces the error. Since the commands of DarkGDK are almost the same as in DBPro I think it won't be a big problem for you to understand (and translate).

Here is the program code:



And here is the shader code:



The scene consists of just a flat box and a sphere above it. You can move around using the arrow keys. In case your wondering why I put a texture on the objects which is not even used in the shader - that's because I get an error if the render color target is the first texture / sampler in the shader!
The shader should draw the objects red in the first pass, store the result in the render target and then just draw the objects green in the second pass. When I run the program I see the objects painted in green, but there is always this strange blue version of the scene drawn into it. To me it looks very buggy...
I have attached the compiled demo to this post so you can see for yourself.

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: 9th Mar 2011 23:48
Thanks. I'll take a look tomorrow.

The exe runs fine and the symptoms you describe are clear.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Mar 2011 23:56
Sorry for the double post but couldn't resist a quick peek. Does the following change to your shader fix the problem?

Change this line



to



I'll have a closer look tomorrow but that change seems familiar. I think the deleted part was telling the shader to use the screen image as its render target, therefore causing your problem. I'm not convinced though. It seems to fix your demo at least.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Mar 2011 00:04 Edited at: 10th Mar 2011 00:22
Ok, I've removed the ResourceName bit. Now the duplicate of the scene is gone! But...
I then tried to read out the render target by using the target sampler as texture for the objects like this (with correct uv coords of course):



But the color is black all over the sampler! Looks like the render target didn't become filled correctly.

Edit:
I think DarkGDK / DBPro can't handle textures of any kind without the ResourceName string! I've removed it from a usual diffuse texture and that is then completly black too. And I think it doesn't even really matter what you type in as content for this string. I wonder what it is good for...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Mar 2011 00:24 Edited at: 10th Mar 2011 00:28
OK, sounds like I was wrong about that. Looks like I'd better have a closer look when I'm awake tomorrow.

Edit

Quote: "I think DarkGDK / DBPro can't handle textures of any kind without the ResourceName string!"


Yes, I guess you're right. I'm just annoyed with myself because I've encountered all this before and just can't remember the fix.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Mar 2011 00:52 Edited at: 10th Mar 2011 12:48
Ok, no problem. Maybe we'll find a solution tomorrow. I'm hanging in front of the screen for too long now staring at pages full of RENDERCOLORTARGET google results...
Most people out there working with other engines (e.g. pure DirectX, FX Composer) use something which is called a shader script along with render targets, and I guess that's not supported by DarkGDK / DBPro.
But well, that doesn't really explain the "buggy" nature of this issue, don't know...
Let's have a look at it tomorrow. Good night

Edit:
Ok, back again. I was just messing around with the shader again.
I found out that the strange scene duplicate render only appears when you set the render target texture dimension to square values (same width and height)! For example when you set the width to 320 and the height to 200 there is no double render.
You can also define the texture size by making it depend on the view port ratio like this:



Now on for some more testing...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Mar 2011 19:25
Quote: " found out that the strange scene duplicate render only appears when you set the render target texture dimension to square values (same width and height)! "


That is weird - unless the render target hasn't been setup as in my earlier daft "fix".

I've just looked at my render target demos and I think the fix may be simple. My passes uses the zenable state as in the following snippet:



I can't recall what led me to that but I think the point is that the rendercolortarget seems to share the same space as the final screen render (I have no idea why or whether that is actually true, something I must ask Lee about one day). I suspect the problem which you are now having (and which I had some time back but couldn't recall yesterday) is that the first pass writes to the z buffer and, depending on the values written, the second pass may or may not pass the z test. This probably explains why changing your projection matrix had an effect in one of your tests.

Let me know if that fix in the snippet works.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Mar 2011 23:38 Edited at: 10th Mar 2011 23:39
I already had the Zenable set to false in the first pass, and set to true again in the second, but the problem is that I can't get the depth information from the first pass, when there is no z depth! It fixed the duplicate render though...

About the target texture dimensions: It did only fix it in this one situation where both passes share the same view and projection matrices. When I tried to fix the real light pass (with different matrices for both passes) it didn't fix it.

It's all realy wierd and makes me wonder if it's even possible to achieve shadow mapping without a second camera. In other engines that have a better shader compiler it would be easily possible (in some it's even possible to create dedicated render depth targets in a shader!), but I'm sceptical about DarkGDK/DBPro.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Mar 2011 01:13
Quote: "I already had the Zenable set to false in the first pass, and set to true again in the second,"


No you haven't. Here is your technique copied straight from your zip file.



Quote: "but the problem is that I can't get the depth information from the first pass, when there is no z depth!"


I don't understand the problem - you've coded depth in the colour channels. Taken from your first post on this topic:

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 11th Mar 2011 02:06
In the zipped demo I wasn't doing anything with depth data, that was just to show the render target issue. I could fix that by using a ViewPortRatio of { 1.0, 1.0 } on the render color target texture.
Setting Zenable to false in the first pass and setting it to true in the second pass would also fix that problem in the demo.
But, if I want to do shadow mapping, I need to compare the depth of a "screen" pixel from the lights view with a projected pixel from the lights view in the second pass. Now when Z depth is disabled in the first pass (Zenable = false) each pixel has the same Z value so it's impossible to determine if one pixel is behind another.
One main issue I have with all this is that there seem to be two causes for the same effect. One is using a render color target that has another ratio then the viewport. The other is using a view/projection matrix without having a dedicated camera for it.
Both result in this strange duplicated render appearance. Very confusing...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Mar 2011 02:35
Quote: "But, if I want to do shadow mapping, I need to compare the depth of a "screen" pixel from the lights view with a projected pixel from the lights view in the second pass."


True, but your shader uses depth coded as colour not Z depth. All you have to do is read the colour, and re-scale it to the correct Z value if necessary for your comparison. Works fine for me, why shouldn't it work for you?

I really don't see this particular problem.

Quote: "Both result in this strange duplicated render appearance. Very confusing..."


I am as well. I've just removed the zenable lines from my render target shader demo and I don't get the duplicates at all - which makes me wonder why I put them there at all. I also don't get them when I change the render target dimensions. Both these tests were done on my desktop.

I've just looked back at Lee's article on this (Tip of the month, TGC Newsletter 87) and found this:

Quote: "The new addition was the support of the RENDERCOLORTARGET semantic in the FX shader effect. Previously this instruction was ignored to keep the shader system simple, but the benefit of an elegant coding solution was too important to ignore. Thanks to this semantic, you can now declare a shader texture like this:



What this means is that you are creating a texture inside the shader which will be dynamically created for the purpose of rendering something to it, and it will be destroyed when no longer needed. This allows you to redirect the output render of a render pass as follows:
"


Note the absence of ResourceName from the texture declaration. The plot thickens.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 11th Mar 2011 12:47
Quote: "your shader uses depth coded as colour not Z depth. All you have to do is read the colour, and re-scale it to the correct Z value if necessary for your comparison."


Well, that's what I thought, too. Honestly I don't know why it's not working. Have a look at the attached image. The sphere up right represents the light source, for which I created a left handed look-at matrix as view matrix. As you can see the big sphere doesn't have any effect on the depth beneath it (on the ground box). That's why I think that I can't get the correct depth with Z disabled.

Here is the shader I used for that:



Quote: "I've just removed the zenable lines from my render target shader demo and I don't get the duplicates at all - which makes me wonder why I put them there at all. I also don't get them when I change the render target dimensions."


Strange! For me it happens as I described! Maybe it is a hardware problem (I'm at a notebook with ATI Radeon Mobility).

Quote: "Note the absence of ResourceName from the texture declaration."


For me any texture without the ResourceName string just becomes black, render color target or not!

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Mar 2011 16:36 Edited at: 12th Mar 2011 12:50
Quote: "That's why I think that I can't get the correct depth with Z disabled."


I don't think that is relevant. The Z buffer isn't being used for the shadows. My guess is that the problem is elsewhere in the code - although I still suspect possible hardware problems. Unfortunately I'm very busy with other things at the moment so haven't had the time I would like to delve into this properly. But if I can I'll see if I can test your shader myself. Or adapt my own shadow mapping shader? Debatable which will be easier.

Quote: "Strange! For me it happens as I described! Maybe it is a hardware problem (I'm at a notebook with ATI Radeon Mobility)."


I think I'm getting different results on my laptop too - but haven't compared the two systematically (too many slightly different versions of the demo on different machines - just need time to do the checks carefully. )

Quote: "For me any texture without the ResourceName string just becomes black, render color target or not!"


Same here. Lee's Tip also says the rendercolortarget is destroyed when finished with. That doesn't rest easily with the duplicate image problem which I've stumbled across a few times now. I think Lee's tip left out some rather important details. Of course there could be more bugs in the DBPro implementation ...

Edit Regarding the duplicate image yet again (), I suspect what's happening is that the first pass was writing to the Z buffer so that for any pixel in the second pass where the new z value failed the Z test nothing was being rendered. I think that explains why the red didn't appear. All we were seeing was the camera backdrop showing in unexpected places depending on the relevant z values - I don't think we were seeing the rendercolortarget at all. Disabling zwrite or zenable cures this as we've noted.

Edit2 Returning to the main issue - shadow mapping - I don't see how this method can work unless different objects can share the same render target. I doubt DBPro is set up that way. I'll see if I can prepare a simple test along the following lines: place two objects side by side and ttexture one with a green image and the other with a blue one. Then apply a shader to them both. In the first shader pass swap the object's positions and colour them both red and send the output to a rendercolortarget. In the second pass position the objects in the usual way and read the colour from the rendercolortarget. Then add that colour to the objects' texture colours. I would expect the outcome to be one of the following:

A) If the rendercolortarget is being shared, which I doubt, then one object will be coloured yellow and the other purple if render order is unimportant.

B) If the target is not shared then both objects ought to be their original texture colours, green and blue (assuming the rendercolortarget is initialised to black).

C) If render order is important, but the target is being shared, then there are two possibilities: (1) one will be yellow and the other blue; (2) one will be green and the other purple.

Outcome B) is what I would guess.

It ought to be possible to initialise the rendercolortarget but I haven't succeeded yet.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 13th Mar 2011 20:56 Edited at: 13th Mar 2011 20:59
Quote: "I suspect what's happening is that the first pass was writing to the Z buffer so that for any pixel in the second pass where the new z value failed the Z test nothing was being rendered. I think that explains why the red didn't appear. All we were seeing was the camera backdrop showing in unexpected places depending on the relevant z values"


That makes sense! I was thinking something along these lines but couldn't put it in words

Quote: "Returning to the main issue - shadow mapping - I don't see how this method can work unless different objects can share the same render target. I doubt DBPro is set up that way."


Ok, so the problem is that DBPro's shader effects work on an object basis right? I think it would be possible in other engines.
There you can use things like RenderDepthTargets and the like.
But right now I'm good with the classic light-camera method, I just thought doing it using render targets would be a clean and simple way...
But then if your test proves that it would be possible, that would be awesome!

By the way, do you know how to set the individual values of a matrix4 using the built-in 3D math functions? Couldn't find anything in the help docs.
In HLSL I would do this:



Now I wonder if it's possible using DBPro code (and so DarkGDK too)?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Mar 2011 22:00 Edited at: 13th Mar 2011 22:03
Quote: "By the way, do you know how to set the individual values of a matrix4 using the built-in 3D math functions? Couldn't find anything in the help docs."


I can't find it in the Help files either - but the editor recognises set matrix4 which would be the obvious choice of course.

Looks like one of those rare Help file glitches.

Oops! No it isn't!! It's one of IanM's essential additions to the language from his Matrix1 plug-in. If you haven't got that do so now.

I haven't had time to do the test yet as rather busy at the moment but I hope to get to it soon if you haven't beaten me to it that is.

Quote: "I was thinking something along these lines but couldn't put it in words "


It took me an embarrassingly long time to come up with that explanation.

Several odd results in various trial demos are now explained.

Edit

Quote: "I think it would be possible in other engines.
There you can use things like RenderDepthTargets and the like."


Yes, I'm sure you're right as I've seen nVidia documentation, etc, suggesting that approach. Probably more efficient too. But at least the DBPro method works.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 15th Mar 2011 22:31
Ok, here is another one...

Let's say I want to scroll a texture along each face of an object downwards, with speed depending on the angle of the face. Any ideas about that?

Ok, I can determine the steepness of the face by taking the Y component of the world vertex normal. A value of 1.0 means it's pointing straight upwards, so no movement at all. And a value of -1.0 means it's pointing straight downwards, so there you have the maximum scroll speed.
But how would I calculate the uv coordinates to make them allways scroll downwards?
If I multiply the above scroll factor (from the world normal.y) with the texture coordinates over time the texture doesn't allways scroll downwards. As one might expect it scrolls in the 0.0-1.0 uv direction. No surprise. So how could I calculate the new uv coordinates to make it scroll downwards no matter what the angle of the face is?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Mar 2011 23:04
Quote: "Ok, here is another one..."


This is turning into that long awaited "Shader Challenges" thread.

You might be able to use some maths using tangent space coordinates (as used in a normal mapping shader for example). I think you need to find the "down" direction in terms of the tangent and binormal vectors and use that to modify your UV coordinates.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 15th Mar 2011 23:12
Quote: "This is turning into that long awaited "Shader Challenges" thread."


Yeah, really mind provoking stuff going on here

Quote: "you need to find the "down" direction in terms of the tangent and binormal vectors"


So you mean I have to get the cross product of the normal and the tanget? (or is that the binormal?)
And how would I then change the UV coordinates?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Mar 2011 23:44
Quote: "So you mean I have to get the cross product of the normal and the tanget?"


No.

Quote: "or is that the binormal?"


Yes.

Going back to your previous post, you said

Quote: "Ok, I can determine the steepness of the face by taking the Y component of the world vertex normal. A value of 1.0 means it's pointing straight upwards, so no movement at all. And a value of -1.0 means it's pointing straight downwards, so there you have the maximum scroll speed."


Is that quite what you want? Don't you want the max speed to occur when N.y is zero?

Quote: "And how would I then change the UV coordinates?"


Good question. I'm not quite sure of the details yet - but I am fairly sure that tangent space is the key somehow.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 16th Mar 2011 00:22
Quote: "Don't you want the max speed to occur when N.y is zero?"


Oh, yes that's right. Wrong explanation by me in the previous post...

Ok, so the tanget is perpendicular to the normal in U direction, while the binormal is the same but in V direction.
The problem is how to find out what's down on the surface, no matter how it is rotated.
Tangent space is like a plane with the tangent as X axis and the binormal as Y axis. How would that help to determine what direction to scroll? I mean the tangent/binormal could be facing anywhere.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Mar 2011 00:50
As I said, do it the way standard bumpmapping does it.

I've had a quick go at this (by adapting one of my bumpmapping shaders) but with mixed success. It seems to work for cubes but not for spheres. Not sure why yet but my guess is that the amount of scrolling varies from one poly to the next so you get a mess. See attached demo (the "down" direction can be set to what you like, it's set to negative X in the demo).

You might find it easier to use world space texturing rather than UV coordinates. What sort of objects are you working with?

Attachments

Login to view attachments
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 16th Mar 2011 01:23 Edited at: 16th Mar 2011 01:58
Quote: "it's set to negative X in the demo"


You mean negative Y?!

Ok, I've put that shader into a project. I can't compile your demo because I don't have DBPro installed. Could you maybe compile an exe for me?
Anyway, it was quite what I need. I'll have a deeper look into the FX file.

Quote: "You might find it easier to use world space texturing rather than UV coordinates."


I'm not quite sure what this means or how it would look like!

Quote: "What sort of objects are you working with?"


Well, the resulting shader should work with a mesh that's something like a strip of quads that are facing upwards (but not straight, kind of like one row of a terrain). But it would be nice if it would work with any shape

Edit:
By the way, what does transpose actually do/mean?

Edit:
Ok, I've adapted the down scrolling part of your shader. It works with cubes, boxes and planes but not with spheres. Then I tried three models: a human character, the mentioned quad strip and a cube model. The cube model worked (like the code-created cube) but the others didn't!

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Mar 2011 02:03
Quote: "You mean negative Y?!"


No. From the DBA code:



I adapted an earlier demo which had the camera pointing straight down - it was easier to allow the shader to use any given direction rather than to re-write the demo. As I said, you can define the "down" vector to be any fixed direction. I chose (-1, 0, 0), i.e. negative X. The 4th component of the vector4 in the DBPro code isn't used by the shader.

Quote: "Could you maybe compile an exe for me?"


Attached.

Quote: "By the way, what does transpose actually do/mean?"


It interchanges the roles of rows and columns in a matrix. For example, the transpose of the matrix



would be

Attachments

Login to view attachments
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 16th Mar 2011 02:14 Edited at: 16th Mar 2011 02:39
Ok, thanks for the EXE and the explanation!
As I edited in my last post, it works with cubes, boxes and planes, but not with spheres (unlike the spheres in your demo, mine don't even show any scrolling!) and most models (except a simple cube). I have no idea why...
I tried reconstructing the binormal manually in the vertex shader by doing b = cross( n, t ), as I thought some models might be missing that data, but it didn't fix the spheres/models either...

Edit:
I think it must have something to do with the tangent space thing.
Because when I change



to



It seems to work, except that it doesn't scroll in the right direction (because the face normal/tangent/binormal aren't used of course).

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Mar 2011 12:42 Edited at: 16th Mar 2011 12:49
Interesting. I'll have another look later today. I did do that in a bit of a rush so I could easily have got my matrix multiplications muddled.

I'll also post a demo of the world space idea - but I'm not convinced it will look OK. What sort of textures are you talking about? Things like this would look very odd if scrolled:

Attachments

Login to view attachments
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 16th Mar 2011 13:27
It should work with standart seamless square textures, nothing like this character skin texture you posted.

Quote: "I'll also post a demo of the world space idea"


That would be nice, as I still have no idea what that would look like

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Mar 2011 17:35
Quote: "as I still have no idea what that would look like"


Neither have I till I try it.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 17th Mar 2011 22:47 Edited at: 17th Mar 2011 22:50
I have had a little bit of success with fixing the scrolling issues.
It seems like the tangents and binormals are somehow messed up.
When I scale them up in the vertex shader by a value like 100 it seems to work with spheres!



With the test quad-strip model I mentioned (attached), this fix only works on certain faces. Some still look very strange. Sometimes if you come very close to one of these faces (with the camera) you can see very tiny movement on them, but it looks very distorted.

Could it be that DBPro/DarkGDK sends the wrong tangent/binormal information to the shader?

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2011 01:53 Edited at: 18th Mar 2011 02:20
Quote: "Could it be that DBPro/DarkGDK sends the wrong tangent/binormal information to the shader?"


Possibly, but I doubt it. The tangents don't change with time whereas the problem gets worse with time which makes me suspect a logic error in my method and I just can't see it yet. The key thing which is baffling me is that apart from minor effects of curvature I would expect the UV coords to scroll at a constant rate and the texture to have the same amount of tiling as time passes - but when you look at the spheres with my original version the texture seems to be tiled at an increasing rate as well as being scrolled. I can't see the reason for this tiling effect. I must be overlooking something simple.

I'll try a low poly faceted version of the spheres next because that should rule out curvature effects (which might not be small after all).

Unfortunately I'm pressed for time this week so might not have time to resolve this - but I'm sure it's my logic at fault this time. I could be wrong though.

Edit Done a quick test using faceted spheres and the problem is still there. It's worse after rotating the spheres and is also worse using my technique with "fixed" tangents and binormals. I'm doing something wrong somewhere.

Here's the code for the faceted spheres:



I need time to think about this one.

Edit2 Just had one final thought before I close for the night. I think my method relies on the UV coordinates of all points on a face being scrolled by the same amount. That will happen if all points on a given face have the same normals, tangents and binormals. But they don't, especially if the model isn't faceted. The faceted version may go some way towards this (because the normals are fixed across a face) but not completely if some sort of smoothing happens to the tangents and binormals. So you might be right after all.

This is going to keep me awake.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 18th Mar 2011 02:26
No problem, take your time.

It's really strange. It's like one set of the UVs stays fixed so the whole texture is also being scaled while it should just being scrolled. The texture seems to be crunched towards the (1,1) uv position (?)

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2011 13:00
Still not sure about the tangent space problem. Decided to look at my other idea instead.

Have a look at this demo. It uses normal triplanar texturing based on the world space coordinates of the objects. It completely ignores the objects' UV coordinates.

I've included the exe file as well plus an animated object in the demo. Have fun.

Attachments

Login to view attachments
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 18th Mar 2011 13:22
Oh, that looks great!
I'll try to understand it and adapt parts of the shader to fit my needs.
Good Work as always,
thanks a lot!

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 18th Mar 2011 14:37 Edited at: 18th Mar 2011 14:41
I've had a go at it.
It works so far, but there is one problem I have with it.
On top of the sphere there is no noticable scroll at all. This may be because the normals on these faces are almost (0,1,0).
This may not be a problem at first glance, but as I mentioned earlier I'm aiming at applying this to a bunch of quads that are all almost facing upwards, so there won't be much scrolling going on.
How could I adjust this?

Edit:
Ok, one simple fix for this was to just scale the normal:



Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2011 15:36
Yes, that would be one way to do it. Another way would to be ignore the XZ part of the texture read with consequential changes to the code (might give odd results on horizontal surfaces though). That would probably reduce the instruction count - but your solution may be better for nearly horizontal surfaces.

If it works, use it.

Login to post a reply

Server time is: 2024-11-23 21:52:43
Your offset time is: 2024-11-23 21:52:43