Sign in to follow this  
jf_wangfeng

What is VertexShader used for?

Recommended Posts

I hope someone would show me some images to explain what is vertexshader used for.Also I want to know what is pixelshader used for. I also confuse the term of "shader". Thanks

Share this post


Link to post
Share on other sites
Hey bud,

A vertex shader is typically the first phase of a rendering shader. It is responsible for transforming the vertices before the interpolated pixels are passed to the pixel shader. Often it is the case that some of the calculations for effects need only be done on a per vertex basis. So they are calculated in the vertex shader and then passed through to the pixel shader. The pixel shader then takes data that has been interpolated between the vertices and applies them to the pixels.

Here is a typical VS for a phong lighting shader:

struct VS_OUTPUT
{
float4 Position : POSITION;
float3 Vert2Light : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 View : TEXCOORD2;
};

VS_OUTPUT vs_main(VS_INPUT input)
{
// Clear the output structure
VS_OUTPUT output = (VS_OUTPUT) 0;
// Transform the model space vertex position
// into clip space position
output.Position = mul( input.Position, matWorldViewProj );
// Transform the normal to world space
output.Normal = mul( input.Normal, matWorld );
// Make the vertex.position into world space
float3 posWorld;
posWorld = normalize( mul( input.Position, matWorld ) );
output.Vert2Light = lightPos - posWorld;
// Eye to vertex vector
output.View = cameraPosition - posWorld;
return output;
}



Hope that helps,

Dave

Share this post


Link to post
Share on other sites

Vertex Shader or Vertex Program is a small piece of code usually running on GPU and it is used to process data. In DX for example the Vertex Shader is executed when calling any primitive drawing functions. A VShader is executed for every incoming vertex that is needed for drawing the requested primitives (points, lines, usually triangles) .

A usual pattern to use VShader is to first fill a vertex buffer with vertex data such as

struct MyVertex
{
Vector Pos;
};

A simple HLSL shader could be for example

struct HLSLVertex
{
float4 Position : POSITION;
};

float4x4 WorldViewProjMatrix;

void main(in HLSLVertex IN, out HLSLVertex OUT)
{
OUT.Position = mul(IN.Position, WorldViewProjMatrix);
}

This shader only transforms a point by WorldViewProjMatrix (which is assumed to contain the requires transformations). The geometry drawn with this shader won't look very interesting though, since no texture coordinates or colors were defined. So this shader doesn't really "shade" anything, but that is the next step :)

Pixel shader is very similar to vertex shader, but it is executed for every drawn pixel. A typical pixel shader fetches data from textures and does some modulation according to colors emitted by the vertex shader. A pixel shader can be used to create effects such as bump mapping, phong lighting etc.

Share this post


Link to post
Share on other sites
The term "shader" is a misnomer. A shader is a program that processes graphics data and is part of producing the image on the screen.

An image on the screen starts out as triangles. Every vertex of every triangle is processed by a vertex shader. The most basic vertex shader simply computes where the vertex goes on the screen.

Once the screen location of the triangle is determined, it needs to be drawn. Every pixel of every triangle is processed by the pixel shader. The most basic pixel shader simply computes the color of the pixel.

Share this post


Link to post
Share on other sites
Thanks guys!
But my GeForce4Mx440 doesn't get the vs1.1(2) and ps1.1(2),it still can draw 3d graphic.
What is the different between the video-card with vs(ps) and the video-card without vs(ps)?

Share this post


Link to post
Share on other sites

A video card without VS and PS support is tied to work with the "fixed function pipeline" or FFP. There is an amount of different options to affect the output of the FFP, such as number of lights (usually 8 at maximum and lighting is done always with the same formula) and many more.

The point is that the functionality of FFP is quite large, but still it is limited to the predefined functions. VS and PS on the other hand gives you plenty of freedom, but implementing all the FFP functionality might be difficult.

A card without hardware VS support can use software vertex shaders. Pixel shaders aren't supported on software (except on reference rasterizer but that's like slow).

Share this post


Link to post
Share on other sites
Sorry for interrupting you, but I must ask this: Is it possible in any way to implement occlusion culling using VS?

Share this post


Link to post
Share on other sites

You could ask a separate question about occlusion to get more and better answers.

Occlusion culling and vertex shaders aren't related. You can perform HW occlusion culling with modern HW using occlusion culling queries.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this