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 / .fx shaders and HLSL

Author
Message
Bmad6
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: Virginia, United States
Posted: 7th Sep 2006 05:32 Edited at: 7th Sep 2006 06:00
I want to learn HLSL, so I've been looking at some tutorials on shaders online. I was trying this tutorial:

http://www.neatware.com/lbstudio/web/hlsl.html

but I couldn't seem to get it working - the object would just act as if hidden when I applied the shader.

Then, I took a look at some of the shaders in the free shader pack (in particular, I looked at one of Evolved's shaders) and it seemed like the structure of the .fx is nothing like what is described here. So, are there different forms of HLSL? And, what's the difference? Also, does DBPro suppport all of them?

And, while I'm at it, I would like to ask all of the people here who can write shaders how they learned - can you suggest a book or web page?

UPDATE:
I just tried this:
http://www.cfxweb.net/modules.php?name=News&file=article&sid=1305
tutorial, and again, it just hid my object. I've got an nvidia geforce 5200, brand new, so I don't think it's my hardware... also, all of the examples from the free shader pack work for me, so shouldn't these work too?

EDIT:
Oh, yea, here's the code I'm using (press space to apply test.fx):



Harvard's Law, as Applied to Computers:
Under the most rigorously controlled conditions of pressure, temperature, volume, humidity and other variables, the computer will do as it d*mn well pleases.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2006 13:05 Edited at: 7th Sep 2006 13:07
Yes, I've found the available material on shaders very confusing. Some are written in HLSL and some (like Evolved's) are written in asm. There are also several ways of passing parameters to the functions within shaders which can be confusing at first. Best to examine several shaders which work and adopt whichever style suits you. Practice with very simple ones first.

A very good way to start is to download FX Composer from nVidia (it's free) and try the shaders that come with it. Most of them work with DBP quite nicely once you've set up the constants correctly in DBP. You can learn a lot just by experimenting with the examples and seeing what effects various code changes have. After that try RenderMonkey from ATI (but I've found the shaders there need alteration before they work properly in DBP - AND the output FX files look awful with very long variable names like My_First_Effect_Technique_Two_Pass_One_Pixel_Shader ...).

However, I found I couldn't make much progress till I got a book. I've tried:

"Shaders for Game Programmers and Artists" by Sebastien St-Laurent

and

"Programming Vertex and Pixel Shaders" by Wolfgang Engel.

Neither book is completely satisfactory - but both were very helpful. Engel's book gives more accurate explanations, but is rather heavy reading. Both books, especially St-Laurent's book, are littered with typos which make the tutorials hard to follow in places - and you need to "read between the lines" to correct the typos in the list of intrinsic functions and their arguments!

There may be better books around but one of those should help a lot - but be prepared to be left figuring some things out for yourself.

Have fun - and post questions here. Someone can probably help.

Edit: corrected a typo!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2006 13:13 Edited at: 7th Sep 2006 13:15
Quote: "Oh, yea, here's the code I'm using (press space to apply test.fx):"


Could you give us the "test.fx" file as well? The problem could be there rather than your DBP file. Will try it for you if you post it.

Edit: And, of course, any texture (image) files you are using...
Pulsar Coder
20
Years of Service
User Offline
Joined: 3rd Jan 2004
Location:
Posted: 7th Sep 2006 20:41
Engel's a very good book.

There's another one that can be useful: "The COMPLETE Effect and HLSL Guide"

~Pulsar Coder
Bmad6
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: Virginia, United States
Posted: 8th Sep 2006 00:39 Edited at: 8th Sep 2006 00:53
Quote: "Could you give us the "test.fx" file as well? The problem could be there rather than your DBP file. Will try it for you if you post it.

Edit: And, of course, any texture (image) files you are using..."


I keep deleting the test.fx file and creating a new one with each tutorial, and none of them seem to work.

I'll post a couple of pieces of test code I tried in the .fx files, though...

And, the textures are 1-pixel images generated at runtime using bitmaps...

UPDATE:

Here are a couple of the test.fx files I used:



That's supposed to fill the screen with red, c0 stores red, and r0 is the current pixel...



Very simple - supposed to just render the object normally, I think...
This is the HLSL version of that same code:



All of these just hid the object... also, if you can point out an error in any of these (they're copied almost directly from their respective tutorials) I would appreciate it, some of the tutorials look terribly old and probably out of date...

EDIT: Oh, btw, run the program, then press the spacebar to apply the shader... escape to exit...

Harvard's Law, as Applied to Computers:
Under the most rigorously controlled conditions of pressure, temperature, volume, humidity and other variables, the computer will do as it d*mn well pleases.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Sep 2006 01:20
The snippets you've just given are only fragments of a complete shader. If you just want a shader to output red (purely as an example, of course) I'll write one now and post it in a few minutes.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Sep 2006 01:40
Here you go. First the DBP program:



And now the FX file (which you should call "GGSimplyRed.fx"):



I used Vs and PS2.0.0 but it should work with most earlier versions as well.

Any questions, just let me know.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Sep 2006 02:09 Edited at: 8th Sep 2006 02:10
Just looking at your code fragments more closely.

The first one should output something. I think it needs something like the following as the FX file:



[Edit The above works when called by the following DBP program:



No more time to look at your other fragments now. Will look again tomorrow. Good luck.
Bmad6
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: Virginia, United States
Posted: 8th Sep 2006 06:56
Wow... that's really simple, but about 1000 times more helpful than any of the tutorials I saw... thanks!!

Now, a quick question about vertex shaders versus pixel shaders:

In your example, the vertex shader takes a position and returns a position and the pixel shader just returns a color (i understand that it could also take various values); my question is: what are all the possible values you can have as input and output from these 2 different types of shaders?

And, I've already got NVidia's FX Composer (but don't know how to use it ) and ps and vs 2.0 work well for my gpu...

And, another thing: I see a lot of places in these tutorials that talk about 4x4 matrices, which aren't that complicated by themselves, but what are they used for and what values do they hold? For example, in your example, you include the WorldViewProjection matrix and call it wvp. I understand that this is the world matrix multiplied by the view and projection matrices (right?), but what are the world, view, and projection matrices?

Harvard's Law, as Applied to Computers:
Under the most rigorously controlled conditions of pressure, temperature, volume, humidity and other variables, the computer will do as it d*mn well pleases.
coolgames
18
Years of Service
User Offline
Joined: 26th Sep 2005
Location: Oregon, USA
Posted: 8th Sep 2006 07:31
I believe when you multiply the objects position by the matrix it returns the objects position on the screen. But I don't really know for sure.

VR2
19
Years of Service
User Offline
Joined: 14th Mar 2005
Location:
Posted: 8th Sep 2006 11:32
The world matrix is the matrix that translates the 3D object into World space.

The view matrix is the matrix that describes the camera position (as an inverse of a world matrix)

The projection matrix describes how the vertices are transformed from 3D world coordinates to 2D screen coordinates.

Bearing in mind that the view matrix is the inverse of the world matrix you can see that

World * View = How an object would appear to the viewer (camera).

thus World * View * Projection = How an object would appear to the viewer (camera), projected onto a 2d screen.
Bmad6
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: Virginia, United States
Posted: 8th Sep 2006 14:25
I'm not sure I understand it entirely, but that gives me a good enough idea of what's going on that I think I can work with it...

Now, how do you add textures to a shader and how do you use pixels or coordinates from the in your output?

In one of Evolved's shaders I see the input TEXCOORD and the outputs TEXCOORD0 and TEXCOORD1, but I assume these store uv data only, not rgba data? And, I assume that these different outputs are different textures? Also, why do the outputs have a number after them, but not the input? Could you change the input to TEXCOORD0 with the same result?

Harvard's Law, as Applied to Computers:
Under the most rigorously controlled conditions of pressure, temperature, volume, humidity and other variables, the computer will do as it d*mn well pleases.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Sep 2006 17:42 Edited at: 8th Sep 2006 17:48
Have a look at the project and fx file in the attached zip file. The FX file uses two textures, one is used as a "noise" file ("magic clouds.png") and the other ("lava colours.png") is used as a simple colour lookup table depending on the value found in the noise lookup. Look at the code and see if you can work out what is going on. You can also lookup 3D textures to get interesting effects with a bit more work.

The output from the vertex shader has two variables labelled as TEXCOORDs, i.e. TEXCOORD0 and TEXCOORD1 - and there are two input variables similarly labelled for the pixel shader. These labels tell the compiler two things: first, that the variables are unrestricted in value (i.e. not COLORs which are restricted to the range 0 to 1 for each component), and second, how the output variables from the vertex shader link to the input variables of the pixel shader (i.e. the variable labelled as TEXCOORD1 from the vertex shader will be matched to the variable labelled TEXCOORD1 in the pixel shader). You can see what goes wrong if you swap the 1 and 0 in the names in the pixel shader and re-run the program.

[Edited silly error in zip file.]

Attachments

Login to view attachments
Pulsar Coder
20
Years of Service
User Offline
Joined: 3rd Jan 2004
Location:
Posted: 8th Sep 2006 23:18
Good example. Easy to understand ...

~Pulsar Coder
Bmad6
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: Virginia, United States
Posted: 9th Sep 2006 06:34 Edited at: 9th Sep 2006 06:41
The example looks great, Green Gandalf. It's very easy to understand, but I have a couple of questions about the code:

1 - What do the UIMin and UIMax values for scrollspeed x and y do? Do they somehow limit the possible values?

2 - At what rate does Time increase? By milliseconds?

3 - I assume tex1D returns the position (1,n) from a texture given a sampler and n, and tex2d returns the position (x,y) from a texture given a sampler and a float2 with the tex coords?

4 - When you define a sampler, you define a number of values. ADDRESSU=WRAP and ADDRESSV=WRAP are self-explanatory, but what about the rest (ADDRESSW=WRAP, MAGFILTER=LINEAR, MINFILTER=LINEAR, MIPFILTER=LINEAR)?

EDIT:
Also, why does it have all those little lines when it renders? It looks like some sort of wierd noise pattern.

Harvard's Law, as Applied to Computers:
Under the most rigorously controlled conditions of pressure, temperature, volume, humidity and other variables, the computer will do as it d*mn well pleases.
Bmad6
18
Years of Service
User Offline
Joined: 25th Aug 2005
Location: Virginia, United States
Posted: 9th Sep 2006 17:10
And, one more thing - if I change the pixel shader so that it says "Out.color = In.color;" it just renders the cube all black. Is this because of the way darkbasic professional's texture object command works?

Harvard's Law, as Applied to Computers:
Under the most rigorously controlled conditions of pressure, temperature, volume, humidity and other variables, the computer will do as it d*mn well pleases.

Login to post a reply

Server time is: 2024-05-04 01:17:21
Your offset time is: 2024-05-04 01:17:21