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.

Dark GDK / Need to know something about shaders

Author
Message
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 17th Feb 2010 16:42 Edited at: 17th Feb 2010 16:44
Hello guys!

i've been looking around for info about shaders lately ( thanks for aldur )

i have a few questions about them which i cant understand:

1-i wrote a simple shader in a .fx file, it didnt work in my game, copied code to a .txt file and it worked fine, what is the difference?

2-vertex/pixel shaders can move and color object's vertices, right? how is that? or generally, how do a shader modify an object's behavior? like, an object has 300 vertices, and you want to give each one a dirfferent color, how to do it? if it's possible..

3-i have


why did we multiply the WVP by the position of the input? what is the position of the input anyway? position of a vertex? which one?

thats it for now, im really interested in learning shaders, but i cant get how the work exactly.

thanks in advance

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Feb 2010 19:27 Edited at: 17th Feb 2010 19:30
Quote: "1-i wrote a simple shader in a .fx file, it didnt work in my game, copied code to a .txt file and it worked fine, what is the difference?"


There shouldn't be one.


Quote: "2-vertex/pixel shaders can move and color object's vertices, right? how is that? or generally, how do a shader modify an object's behavior? like, an object has 300 vertices, and you want to give each one a dirfferent color, how to do it? if it's possible.."


A shader is just a function that takes various inputs and generates various outputs, a vertex shader will output a vertex position from an input position(optional) as well as other things such a the vertex colour, UV data etc, this is then sent to the pixel shader.

The pixel shader then takes various inputs and generates an output such as colour and z-depth, the pixel shader itself doesn't move anything and merely serves to handle the output for a single pixel which is found after the vertex shader is run.

As such, to give each vertex a different colour, you need to make sure the input is different for each vertex. This can be the position, the UV, the normals, whatever, you can alter this using the Vertex Data function set, or using memblocks.


Quote: "why did we multiply the WVP by the position of the input?"


You didn't, you multiplied the input position by the WVP, the 'mul' function multiplies two matrices together, in this instance the other matrix is vertically 1D, so it's multiplying a matrix by a vector. The WVP matrix transforms things into World-View-Projection space, the World matrix is your object's scale, rotation and position, the View matrix is the camera's rotation and position, the Projection matrix is the camera's view shape, like the fov and near/far clipping distances. Together they can transform vertices into the camera view, but there are probably loads of articles out there that go far more in-depth about this.

Quote: "what is the position of the input anyway?"


That's the raw vertex position and won't change if you move your object, because movement is stored in the World matrix as mentioned above.

Quote: "position of a vertex? which one?"


Any, the function is called for every vertex in the object you apply the shader too.

Also: 'VertexShader = compile vs_1_1 VS();' 'PixelShader = compile ps_1_1 PS();', GDK uses a newer version of DirectX than DBPro, one that only supports shader model 2.0 and up, so you must specify vs_2_0 and ps_2_0 as minimum for it to work in GDK.

Plus your pixel shader takes the wrong input: 'float4 PS ( VS_INPUT input ) : COLOR0', I assume you meant to write VS_OUTPUT, but in this shader it won't matter because the members are the same.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 17th Feb 2010 22:15 Edited at: 17th Feb 2010 22:17
alright, thanks for the info, pretty much helpful.

so, the vertex shader is compiled n times ( n = number of object's vertices ) and pixel shader is compiled n timer ( n = number of pixels the object is covering ) so if the object is covering the whole screen, n in the PS should be screen width * screen height ?, so if i use the built-in function noise(), i can make random colored sphere for example?

so if i want to make a random-coloring shader, the shader should be


is it possible to make the first element of the Color random in
?

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 18th Feb 2010 03:24
Quote: "so, the vertex shader is compiled n times"


No, it's compiled once, the same for the pixel shader.


Quote: "so if the object is covering the whole screen, n in the PS should be screen width * screen height ?"


No, if an object fully covers the screen then the amount of visible pixels will be that many, but the amount drawn has a lower bound of screenWidth*screenHeight, if your object is a cube within a cube then it would be twice that number.


Quote: "so if i use the built-in function noise(), i can make random colored sphere for example?"


No, you cannot have anything random because the shaders can't alter its own state for other vertex/pixel shader calls, at least not without using a far higher shader model(I haven't looked into it). If a shader has the same input then its output will always be the same, notice noise() requires you pass in a scalar, if this is always identical then it will always return the same value.


Quote: "so if i want to make a random-coloring shader, the shader should be"


You're just outputting red, how is that random?


Quote: "is it possible to make the first element of the Color random in"


No. Why do you even want it to be random? It'd look like an incredibly crazy static display that you had no control over. If you want a static display then you should create a texture that is random noise and alter the shader values(such as UV shift) every frame to give the illusion of it changing all the time.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 18th Feb 2010 07:06
alright, that makes sense, thanks alot for the info

Login to post a reply

Server time is: 2024-10-02 01:20:13
Your offset time is: 2024-10-02 01:20:13