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
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 8th Oct 2009 15:57 Edited at: 8th Oct 2009 16:01
Quote: "your query is prompting me to have another look"

Sometimes even older problems can be solved if you look at things from a different matrix ...oops... i mean angle!

Quote: "initialize the colour in the shader to something other than black"

I did, and it still is not drawn. Another prove for this is that I can still see the light-sphere even if I position the camera right under my ground object.

Quote: "Just put the camera where the light is"

I did this when initiating the light. Well, I think there should be a command like this: SetViewMatrixToCamera ( CamID ) or something Then I could use that between the two passes...

In the meantime I tried something like this:



I tried to make my own view matrix for the light and passed that to the shader.
Then in the vertex shader of the second technique I did this:



I multiplied all the 3 matrices (including my custom view matrix) together and use that as my 'LightWorldViewProjection' matrix.
But as a result all my objects are grey now! Don't know exactly what went wrong...
(Screenshot attached.)

How can I calculate my own WorldViewProjection matrix for a given position and rotation? (either in the shader or in my code)

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: 8th Oct 2009 16:24
Quote: "I did, and it still is not drawn. Another prove for this is that I can still see the light-sphere even if I position the camera right under my ground object."


Sounds like the camera is facing the wrong way.

Quote: "But as a result all my objects are grey now! Don't know exactly what went wrong..."


That has to be progress - at least you can see them now.

Quote: "How can I calculate my own WorldViewProjection matrix for a given position and rotation? (either in the shader or in my code)"


I think that is precisely the point I got stuck on before and didn't have time to look into it. For a start, what is the "world" matrix? Perhaps the world matrix is irrelevant? I'm sure the answer is very simple - it just seems to be escaping me for the moment. How does the MS DX9 SDK C++ shadow mapping demo do it?
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 8th Oct 2009 16:45
Quote: "what is the "world" matrix?"

As far as I know the World matrix is used to position/rotate/scale the object in the world. Before that the object is just stored in what is called 'Model Space' (like in your model editor, all vertices are relative to the models own origin).

Quote: "Perhaps the world matrix is irrelevant?"

Yes, I think the world matrix should not be the problem, since the lights camera is looking at the same world as the eye camera.

I think that either the view matrix or the projection matrix has to be altered somehow.
In the DirectX-SDK sample there is this constant:



So I guess they are transforming the Projection matrix!

I also tried this:



But every object remains grey...

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: 8th Oct 2009 16:59
Quote: "As far as I know the World matrix is used to position/rotate/scale the object in the world"


Yes, I know. What I meant was what should we be using as the world matrix? Anyway, we seem to be agreed that it isn't needed.

Looks like I'd better tidy up my demo because it does work up to a point - but it's very similar to EVOLVED's and you have that already.

Quote: "But every object remains grey..."


Where is the grey coming from? I suggest you work through the code carefully to see what colour you expect.

Must do something else now, but I'll return to this later.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Oct 2009 21:40
An excellent discussion of shadow mapping can be found here:

shadow mapping
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 8th Oct 2009 22:51
Yes I know that, it's a great article!
Really helped me understand the whole thing...

This tutorial series is very useful, too: (It's the one I tried to follow)

http://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series3/Shadow_mapping.php

But my main problem remains:
How to create two individual WorldViewProjection matrices?

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: 8th Oct 2009 23:49
Thanks for the link I'll have a look later.

Quote: "But my main problem remains:
How to create two individual WorldViewProjection matrices?"


I'm fairly sure EVOLVED's demo shows you how to do it.

I'll also try to find time to dig into my shadow mapping demo, fix the bugs ( ), clean up the code ( ), and post it with explanations ( ). Over the weekend if we're lucky.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 9th Oct 2009 01:07 Edited at: 9th Oct 2009 01:11
GOOD NEWS!!!
I finally got it to work!

I found out how to pass my custom WorldViewProjection matrix of the light to the shader!

Here is how:

- You first have to create a new LookAtLH Matrix



- Pass that to the shader



- Then you calculate the light's WorldViewProjection matrix like this:
( The World & Projection matrices are both passed through semantics as usual! )



It's not very accurate yet but fast as hell!
So I'm on my way to finish up my own Shadow Mapping shader!
Very satisfying.
I attached a screen shot that shows the shadow map projected onto the scene.
And thanks for your help Green Gandalf!

(Edit: typos...)

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: 9th Oct 2009 02:01
Well done and the screenshot looks good too.

Shadow mapping is one of the more difficult things to get right so you deserve to be pleased with yourself.

Are you going to post the complete code? I'm sure people will find it useful.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 9th Oct 2009 02:09
Thanks!
Making huge progress now that I've sorted this out.
Check my new screenshot of the almost final shader running at a constant 200 FPS on my old crappy laptop.

And yes, I will post it here! ( I owe this forum too much to keep this secret )
But I'll finish it up first...

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: 9th Oct 2009 02:12
Nice. I look forward to the finished demo.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 9th Oct 2009 13:00
You don't need to create a lookAt matrix, you can retrieve the view matrix of the current camera using View Matrix4 (there is also Projection Matrix4).

Now, it seems that all you need is a PCF filter to get smooth shadows.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Oct 2009 13:36
Quote: "you can retrieve the view matrix of the current camera using View Matrix4 (there is also Projection Matrix4)."


Yes, see my post several posts back. But you still need to fiddle about to make sure the camera is the right way up so you might as well construct the view matrix manually.

Quote: "Now, it seems that all you need is a PCF filter to get smooth shadows."


I don't think I've seen an example that completely removes the jagged edges of the shadows. Can it be done?
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 9th Oct 2009 13:53
Yes you can remove them, most common technique is just to simply blur them. PCF to me is basically a fancy name for blur. You see things like PCF4x4 or what not but thats just a 4x4 image kernel. The larger the kernel the smoother they looks, but also the slower it runs.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 9th Oct 2009 14:06
Quote: "But you still need to fiddle about to make sure the camera is the right way up so you might as well construct the view matrix manually."

Since the shadow map is made from a camera, I think it's more logical to retrieve the matrix directly from that camera. It's the only way to make sure that the shadow will be cast correctly (making a matrix manually will introduce some inaccuracies).

Quote: "I don't think I've seen an example that completely removes the jagged edges of the shadows. Can it be done?"

If you have a Shader 3 graphics card, you can use more samples and make it quite fast. Just start with a simple 2x2 PCF, and then, if some samples are different, do an other larger PCF. That way, only the penumbra area will be slow to compute.
But if you want some extra smooth shadows, take a look at variance shadow maps, apparently, they can be filtered just like any texture. Since I have never implemented them, I have no idea how good and efficient they are.
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 9th Oct 2009 14:10
Hi everyone. I have gotten to the point in my project where I would like to start making a particle system, but since I am using Evolved's deferred shading I have run into a few problems.

The deferred shading system uses a screen quad to render the final image, so I had to come up with a way to make sure the particles could be obstructed by the scenery objects. My solution was to re-write Math89's soft particle shader to use the World Position render which is already used by the deferred shading system (since I didn't want to add yet another camera).

Now that I have that out of the way I am running onto some Z-sorting issues, where the particle objects in the back are being rendered on top of objects in front of it.

I have figured out that the objects which are rendering on top of the others are the particles with the lowest object IDs, so I wrote a system that repositions the particles with the lowest object ID's in front of the others. That works alright, but now I can see a "popping" effect when the objects are rearranged.

Does anyone have any ideas how I could combat this issue? I have attached a demo which displays the problem, but the object sorting code has been commented out in case someone comes up with a more elegant solution.

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 Oct 2009 15:50
Math89

Quote: "Since the shadow map is made from a camera, I think it's more logical to retrieve the matrix directly from that camera."


Good point. I posted before engaging brain.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 9th Oct 2009 16:14
Hmm, ok might be true, didn't really think about that yet.
Anyway, using my previously stated method I wrote a demo with my new Shadow Mapping shader.
It's not the most awesome shader out there but good enough for me
The demo is located in this Thread.

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: 10th Oct 2009 01:44
Here's my offering.

The shadows have jagged edges - that part of the shader still needs to be sorted out properly and I think that something like PCF ought to be fairly easy to implement (which means it'll take me a month to figure out ).

Attachments

Login to view attachments
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Oct 2009 02:09
Would you be so kind and compile it for me please?
I don't have DBPro anymore...

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: 10th Oct 2009 02:28
Quote: "I don't have DBPro anymore..."




Here you are. I don't usually post DBPro exe files since they are so large. If you move the camera around with the mouse and up/down keys you'll see a small white object representing the light. If you move the camera inside the white object and look at the scene you'll see that the shadows disappear (as they should).

This is work in progress because I want to implement something like PCF. The shader includes some crude (and expensive )filtering code which I've commented out.

Attachments

Login to view attachments
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 10th Oct 2009 03:56 Edited at: 10th Oct 2009 05:34
I want to help on this shadow project. This is by far the best example of shadowing I've seen in DBP period. I'm downloading it and am going to have a look over it.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 10th Oct 2009 10:59
The filtering code you have is actually a 3x3 PCF. If you want to make it slightly better, you can add some randomness in the texture projection using a full screen noise texture (that's what games like crysis do) and then slightly blur the whole screen (which means that you need a separate shadow mapping pass).

In the screenshot, I use the a slightly random 2x2 PCF filter. The shadowmap texels can no longer be seen, but the render is extremely noisy.

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: 10th Oct 2009 13:32
Math89

Quote: "The filtering code you have is actually a 3x3 PCF."


It seems to be - according to the following article:

ATI Shadow Mapping Article

However, what I had in mind was something more like the Edge Tap Smoothing described on page 12 of that article. The "PCF" described in that article (and what I have commented out in my shader) is a simple linear filter with differential weights which means you still get the jagged edges but just in several stages of intensity as you can see easily if you reinstate those lines in the shader. I don't know why several authors refer to that as PCF. A properly constructed filter, whatever it's called (), will give you smooth transitions for the shadows - and that is my next project. I think I can do something using just three or four samples from the shadow map. Watch this space.

Quote: "In the screenshot, I use the a slightly random 2x2 PCF filter."


I'd like to see the code for that.

david w

Were you referring to mine or _Pauli_'s? Either way, I'm sure any suggestions would be appreciated. One thing you could do would be to translate my demo into DarkGDK and/or C++. I'd find that useful even if no-one else does.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Oct 2009 13:48
@Green Gandalf: Your demo is nice and runs very fast here!
Also it's a bit easier to include in DBP/DGDK than mine. I will have to work on that

I'm trying to get proper Shadow Mapping done for 3 lights, where you store different depht data in the 3 color channels (as you can see in my demo). But it's a little buggy right now...

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: 10th Oct 2009 14:27
Quote: "Also it's a bit easier to include in DBP/DGDK than mine."


That's good to know - but it's completely accidental.

Quote: "I'm trying to get proper Shadow Mapping done for 3 lights, where you store different depht data in the 3 color channels"


Hmm? I think it would be simpler (essential?) to use 3 shadow maps, one for each light (with its own camera). You can then edit my demo making the "obvious" changes. The final render would need all three maps and three light projection matrices, etc.

Quote: "But it's a little buggy right now"


I know the feeling. I'm glad you've raised this topic because it's forced me to tidy up my code and I now know exactly which part of my shader I need to work on.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Oct 2009 14:50
Quote: "but it's completely accidental"




Quote: "I think it would be simpler (essential?) to use 3 shadow maps"


Well, I was just following a tutorial where they did it with the 3 color channels. I was basically just trying this method, nothing final...

Quote: "I now know exactly which part of my shader I need to work on"


Yes this whole topic here helped me a lot! I learned a few essential things!

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: 10th Oct 2009 16:15
Quote: "Well, I was just following a tutorial where they did it with the 3 color channels. I was basically just trying this method, nothing final..."


I'd be very interested to see that tutorial.

What I don't quite see at the moment is what a specific pixel in the shadow map corresponds to. A given pixel must correspond to different pixels in the scene because the lights are in different positions. Perhaps the shadow map is being built in three successive renders? The first render initialises the map and fills the red channel, the next stage reads the previous stages shadow map and changes the green channel, and similarly for the next stage and the blue channel. I can't see how it can be done with a single render - but I can see how you might be able to use a single map.

The final render then picks out different parts of the map depending on which light is being processed. But I'm guessing ...
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 10th Oct 2009 17:26
I got the 3 color method from this Tutorial.

I think what is very important about it, is this:



Each light (in its own pass) can only render to its own color defined by colorwriteenable.
The actual depth is stored in the specific color like this:
(in the shadow map pixel shader)



Then each light needs to sample its corresponding color in the shadowed scene pixel shader:



In the shadowed scene technique I set additive blending so each pixels intensity will be increased when lit:



Well, it's not easy for me to explain, but I understand a bit and hope it makes sense...
You can see the three color depthmap by pressing '1' in my demo.

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: 10th Oct 2009 17:29 Edited at: 10th Oct 2009 18:10
Math89

To clarify what I was saying about PCF shadow mapping, here's the shader "shadowmap.fx" used in the MS DX9 SDK:



The shader uses a 2x2 grid of samples from the shadow map then uses PCF to smooth the result. The relevant bit of code is this:



I'm using projective texture lookup (tex2Dproj) with simple filtering and haven't yet got it clear in my mind how to get the fractional parts required for true PCF which I believe the MS DX9 SDK is using. The odd thing is though, if you run the SDK demo the shadow edges don't seem very smooth so something doesn't seem quite right there.

Of course if MS can swallow the enormous typo on the 4th line of their shader:



it's anyone's guess what other coding horrors have slipped through.

Of course, no-one checks their commented lines carefully, including me - see my shader for example.

Edit

Just found the following discussion of the tex2Dproj function and shadowmaps. Note in particular the comments about PCF in Reedbeta's second post. There's some useful stuff out there.

shadow maps, PCF and tex2Dproj
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 10th Oct 2009 22:23
@GG Yes I can do a C++ version for you. Not a problem. I don't have GDK, but the code would be 90% the same as darkbasic. I will get to work on it after a birthday party I have to go to.

Note: I was talking about your shadow shader. Very simple, very effective, solid starting point. It has much potential. I'm going to be using it alongside my C17 project. (well once I modify it to "look" a bit better. (smoother shadows,normal and parallax mapping, Plus, MRT'S.) You know all the goodies.

What I really want to try out is a "technique LOD system". Parallax up close, then a bit farther out normal, and finally in the distance just Per Pixel. I've always wondered if that would actually produce any substantial speed savings.

Thanks.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Oct 2009 00:14 Edited at: 11th Oct 2009 02:32
Quote: "@GG Yes I can do a C++ version for you. Not a problem."


Thanks. I look forward to it.

I'm stuck at the moment trying to get the PCF ("Percentage Closer Filtering") idea working correctly. I'm using a 2x2 sample of the shadow map and trying to use PCF to give smooth edges to the shadows.

The method is almost working - I'm using the PCF method given in the MS DX9 SDK shadow map shader. I'm getting nice smooth blocks around the edges - but they are oriented the wrong way and I just can't see why.

Here's a typical close-up of a shadow edge (I'm using a white texture for the plain to make the shadow edges clearer):



and here's the corresponding shader code:



The PCF part of the code is this:



I've tried all sorts of variations e.g. swapping lerps.x and lerps.y, array elements 0 and 2 with 1 and 3, etc, to no avail - I just keep getting different wrongly oriented versions. I'm probably doing something silly but just can't see it.

Edit

You might need the dba code as well:

Attachments

Login to view attachments
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 11th Oct 2009 12:10 Edited at: 11th Oct 2009 13:08
I've been working all night trying to get it to run in DirectX but I'm starting to have some issues. DBP does not work the same, I'm guessing on alot of things. I'm close.

Can you please explain to me step-by-step the process that you are trying to do in the original example.

few questions also.

1. Why do you set the camera FOV to 120? What does this do?
2. Are you just using D3DFMT_A8R8G8B8. which would be what dbp defaults to. If I use D3DFMT_R32F, I get the same results.
3. I the "scene" pass you are just rendering geometry as normal. in "shadow" pass, are you just rendering the geometry from the cameras perspective, without textures. I've looked at the shader and it would seem so.


I've never understood how DBP can magically set the world and worldviewprojection matrix's. This really bothers me. I would really like to know how thats possible.

And while I'm on it how can they set the VertexFormat magically also? It just doesn't make sense.

I have to explicitly tell it these things, and every example I've ever seen also does it explicitly.

Here is a simple test program so you can see what I'm talking about.

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 Oct 2009 13:20 Edited at: 11th Oct 2009 13:24
Quote: "Can you please explain to me step-by-step the process that you are trying to do in the original example."


That could take a while. Might be simpler to answer specific questions such as those you raise.

Quote: "few questions also.

1. Why do you set the camera FOV to 120? What does this do?
2. Are you just using D3DFMT_A8R8G8B8. which would be what dbp defaults to. If I use D3DFMT_R32F, I get the same results.
3. I the "scene" pass you are just rendering geometry as normal. in "shadow" pass, are you just rendering the geometry from the cameras perspective, without textures. I've looked at the shader and it would seem so."


1. That's a fairly arbitrary choice. I've chosen a wide camera view so that a large part of the scene has a shadow map pixel associated with it. Ideally everything in the scene that can be seen from the main camera (camera 0) can also be seen from the light's camera (camera 1). This obviously can't be guaranteed if the viewer and light move around - and it's one of the awkward details you need to think about when using shadow mapping. It helps if you draw the two viewing frustums for your scene. The image below illustrates the problem.

The pink circle represents the viewer (camera 0) and the black lines represent the viewing frustum. The yellow circle represents the light (camera 1) and the blue lines its viewing frustum. The rectangles represent various objects in the scene. The red objects are within the viewing frustum of both cameras and will be included in both the shadow map and the final scene. The green objects will appear in the shadow map but can't be seen by the viewer, whereas the blue objects can be seen by the viewer but won't appear in the shadow map. You can include more blue objects by widening the light's camera FOV - but that means a loss of precision in the shadow map since you are trying to pack more information into the same size image. It's a trade off.



2. Yes. At the moment the GBA channels are wasted (not used) and you will get better precision using D3DFMT_R32F. I think C++ will allow you to use the depth buffer directly and there may be other GFX card specific optimisations available too but I can't advise you on those.

3. Yes.

Quote: "I've never understood how DBP can magically set the world and worldviewprojection matrix's. This really bothers me. I would really like to know how thats possible."


My understanding is that the world matrix is unique to each object whereas the view and projection matrices are specific to each camera (which is why I had to switch the current camera when calculating those two matrices using native DBPro commands). Actually I'm not quite sure what your difficulty is - but then DBPro does everything for me behind the scenes.

It certainly possible because DBPro has all the information it needs, i.e. object positions, scales and rotations, camera positions and orientations, etc. There's no magic involved.

Quote: "And while I'm on it how can they set the VertexFormat magically also? It just doesn't make sense."


I don't know - and it seems to be bugged in some rare situations. Again, no magic is involved - all the necessary information is available to DBPro behind the scenes. Whether it uses it correctly in all situations is another matter. )

Quote: "I have to explicitly tell it these things, and every example I've ever seen also does it explicitly."


That's one of the joys of using C++ - and one of many reasons I stick with DBPro. I guess I'm just lazy.

One question you didn't ask is: why do I calculate the view and projection matrices outside the main look? Because the light isn't moving in the demo. With a few changes that code could be put in the main loop if the light is allowed to move.

Attachments

Login to view attachments
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 11th Oct 2009 13:36
I think that your main problem with the fractional thing is that your values of IN.lightproj are floats between 0 and 1 (or -1 and 1, don't remember), so you actually need to multiply them by the shadowmap size. Also, you're using filtering on the shadowmap which will give you some slightly wrong values at the edges. Set the filtering to 'None'.
Anyway, I must admit that I have never seen this smoothing system, with the lerps.

And here is the shadowing code I use:

projection is the light projection vector, and randProjection is the camera projection vector (i.e. the screen-space position of the current pixel). There are a lot of things which aren't really necessary (such as changing the blur according to the distance), but I think they can be interesting for those who want to improve their shadows.
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 11th Oct 2009 13:38
This is all directional light, this wouldnt work for point lights. This method is probably much faster.

I've always wondered something about when you do you vextex inputs. You seem to like using Float4 -- when Float3 would work fine. Why do you do this? Is there some technical reason?

Also, I dont mean to sound like I'm picking you apart. I'm trying to learn still, thats why I ask these things. Also I have a shader sematic question. I notice you do this alot -- float4x4 wvp : WorldViewProjection; -- Why put WorldViewProjection in? Its clearly not needed, is it some kinda of a comment or does it serve some purpose?

Your new version with 2x2 PCF will not compile with the new fx compiler. The message is "global variables are implicitly constant line 92,collum 22.

Good thing dbp uses an outdated version. Good thing DBP does alot under the hood for us.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 11th Oct 2009 14:32
GreenGandalf, I've managed to fix your code. What it actually does, is a manual bilinear filtering by sampling 4 neighbouring texels and interpolating between their values according to the position of the current sample inside those texels. The result looks like a normal texture projected on the scene (no more jagged edges), but it doesn't make the shadows blurry.



And the dba:
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Oct 2009 14:33
Math89

Quote: "I think that your main problem with the fractional thing is that your values of IN.lightproj are floats between 0 and 1 (or -1 and 1, don't remember), so you actually need to multiply them by the shadowmap size."


Could be - but I thought I'd tried that. I'll have another look just in case it's that simple.

Quote: "Also, you're using filtering on the shadowmap which will give you some slightly wrong values at the edges. Set the filtering to 'None'."


I tried that - makes no discernible difference. But it should make a difference, I'll check that again. I could have done something silly like not saving the edited shader.

Quote: "Anyway, I must admit that I have never seen this smoothing system, with the lerps."


Strange. It seemed the obvious thing to do - and is what the MS DX9 SDK demo uses and is described in some of the links I posted earlier. By the way, I checked various sources for the PCF idea and you were right. The 3x3 filter that I was originally using (but commented out) is what the original authors of the PCF method used. The extra smoothing step I'm trying to use seems to be a later variation and is called something else by some authors.

Quote: "And here is the shadowing code I use:"


Thanks. I'll study that and see if it's faster. I was going to try fading the shadow according to distance (which your snippet seems to be doing) since that is more realistic. For example, "ambient" light is not constant - if an object is very close to the ground not much ambient light will reach under it, whereas if the object is a long way away then the shadow might be almost invisible.

I'll post back when I've tested your suggestions (again ).



david w

Quote: "This is all directional light, this wouldnt work for point lights."


Actually it's for a positional light - but only uses the lighting in one viewing frustum so it's more like a spotlight. In fact the code could be improved somewhat to make the spotlight connection more obvious, so I'm glad you've raised this, something else for me to work on . Dealing with a full positional light is more complicated - I guess you'd need up to 6 (?) shadow maps or a cube shadow map for that and I haven't seen a full working demo for that. A true directional light would need an orthogonal (?) projection and the present code doesn't allow for that.

Quote: "I've always wondered something about when you do you vextex inputs. You seem to like using Float4 -- when Float3 would work fine. Why do you do this? Is there some technical reason?"


If you're referring to things like this in the dba file:



then the answer is "yes, there's a technical reason" because DBPro doesn't allow float3's etc to be passed to a shader.

If you mean this in the FX file:



then the final entry, i.e. the alpha, isn't used in this demo but simplifies the colour calculations. Sometimes I use float4's in a shader when it isn't really necessary and pick out the bits I need using modifiers, e.g. lightCol.rgb or lightPos.xyz.

Quote: "Also, I dont mean to sound like I'm picking you apart. I'm trying to learn still, thats why I ask these things."


This is the "Learning To Write Shaders" thread after all. Feel free to ask questions. Questions often help me to understand something as well, so don't worry.

Quote: "Also I have a shader sematic question. I notice you do this alot -- float4x4 wvp : WorldViewProjection; -- Why put WorldViewProjection in?"


Yes it is needed - otherwise you would have to manually calculate it yourself in DBPro and explicitly pass it as a matrix to the shader. Semantics are intended to be spotted by the application so it can fill in the values for you - and there are lists of "Standard Annotations and Semantics" which application authors can use. There is a , hard/impossible to find , of semantics which are recognized by DBPro. I don't know what you have to do in C++ - you might have to do all the "behind the scenes" stuff yourself.

Quote: "Your new version with 2x2 PCF will not compile with the new fx compiler. The message is "global variables are implicitly constant line 92,collum 22."


Is that error message or a warning message? If it's a warning message you could try ignoring it. [As an aside, DarkShader won't compile shaders if it throws a warning message - but DBPro will. Just the sort of irritating detail that makes like awkward for application writers.]

Did you set the compile target correctly? The shader compiles fine using DBPro, Dark Shader and FX Composer 1.8. I don't know what the problem is with the new FX compiler. Try using the old one?

Could you send me a very simple C++ project which does nothing except load and compile the shader and print out the error messages so I can see how it works here?

Quote: "Good thing dbp uses an outdated version. Good thing DBP does alot under the hood for us."


Yes. Progress has it's downside.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Oct 2009 14:35
Quote: "GreenGandalf, I've managed to fix your code. What it actually does, is a manual bilinear filtering by sampling 4 neighbouring texels and interpolating between their values according to the position of the current sample inside those texels. The result looks like a normal texture projected on the scene (no more jagged edges), but it doesn't make the shadows blurry."


Our posts crossed. Thanks. I'll look at this first.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Oct 2009 15:10 Edited at: 11th Oct 2009 15:46
Math89

Thanks. You've certainly fixed the main problem.

The crucial lines seem to be these :



I obviously got in a muddle with the various scales.

Quote: "What it actually does, is a manual bilinear filtering by sampling 4 neighbouring texels and interpolating between their values according to the position of the current sample inside those texels."


That's what I was doing but had the "frac" part wrong.

Quote: "but it doesn't make the shadows blurry"


Yes it does as your code shows clearly.

I was hoping for a better removal of the jagged edges with just a 2x2 filter - but the main thing is you've fixed the bug in my code so I can move on and improve it.

I really appreciate your fix. Thanks.

[Edited punctuation. ]
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 11th Oct 2009 15:58
Quote: "Yes it does as your code shows clearly."

It makes them rather soft, but there is no way to have a large penumbra.
Oh and also, you should change the render type to float16 or float32 (by setting the D3D format to 111 or 114) because storing the depth in rgb isn't accurate enough in most cases.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Oct 2009 16:15
Quote: "It makes them rather soft, but there is no way to have a large penumbra. "


Increase the number of samples perhaps? A 4x4 grid with bilinear filtering should help.

Quote: "Oh and also, you should change the render type to float16 or float32"


Yes I know, but that was the least of my problems - see also david w's comment a few posts back.

I'll also be using your method of packing the samples into a single float since that saves some instructions, especially if it's inside a loop.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 11th Oct 2009 16:30
The packing method isn't mine, I saw it in an Evolved's shader. But yes, it's great since the calculations will then be done simultaneously on different threads.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Oct 2009 00:25
david w

I've just realised why you thought the light was directional in my shadow mapping demo - I've calculated the shadow map assuming a positional light but I've assumed a directional light when calculating the diffuse lighting. This is obviously a silly error on my part - the diffuse lighting should be calculated assuming a positional light as well.

I'll post a fixed version as soon as I can - probably tomorrow or Tuesday.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 12th Oct 2009 16:51
How about doing the actual shadow mapping for a directional light, maybe to achieve a sun-like effect?
How would that be done? I guess just setting the light camera to orthogonal won't do it, right?

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: 12th Oct 2009 20:13
Quote: " I guess just setting the light camera to orthogonal won't do it, right?"


But how would you do that? That seems to be an essential first step. When we've done that we can worry about the rest.
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 12th Oct 2009 20:32 Edited at: 12th Oct 2009 20:36
I once discovered this in a very old thread.
You can set a camera to orthogonal by changing its FOV to the exact value of 361.45 .
To set it back to perspective you use the value 61.962 .
The DarkGDK command is:


I don't know why it's like this, but it works (at least in DGDK).
I used it once in an editor I made...

Edit:
I just changed my light cam to orthogonal, but it doesn't work properly. The depth map looks strange, almost completly shaded in one tone of gray.

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 12th Oct 2009 22:00
Setting the fov to 361.45 will set the fov to 1.45, which isn't orthogonal.

This function applies an orthogonal projection to a specified camera. (IanM's dll required)
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 12th Oct 2009 22:31 Edited at: 12th Oct 2009 22:32
Quote: "Setting the fov to 361.45 will set the fov to 1.45, which isn't orthogonal."


I know, I thought they may have used this value like a placeholder to switch it on or something.
But as I said, I used this method before and it seemed to be orthogonal! (See attached screenshot)
Or is 361.45 (or 1.45) just something close to orthogonal?
I know what a FOV is, but maybe I'm misunderstanding here, please enlighten me

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: 12th Oct 2009 22:48
Quote: "Or is 361.45 (or 1.45) just something close to orthogonal?"


Yes. An FOV of 1.45 means that the sides of the view frustum are nearly parallel - but you'll only be looking at the centre of the usual scene unless you move the camera back a long way. Simple trigonometry or maths will tell you how far back you have to go - and I expect you'll have to change the camera near and far distances as well. Just draw a 2D picture similar to my diagram a few posts back and you'll see what's required.

It should give a good approximation to true orthogonal projection. I haven't tried it though.

Quote: "This function applies an orthogonal projection to a specified camera. (IanM's dll required)"


I really ought to check his DLLs first. I have no idea how to use the resulting image in a shader though. Looks like another learning experience coming up.

Login to post a reply

Server time is: 2024-04-27 02:39:04
Your offset time is: 2024-04-27 02:39:04