Archived

This topic is now archived and is closed to further replies.

Draigan

Vertex Shaders, Again

Recommended Posts

Previously with Direct3D, if I wanted a directional light and say a spotlight, I just enabled the two lights, send the geometry and voila! Now that I''ve started my move to vertex shaders, how exactly do I accomplish the same sort of thing? Do I have to write a new vertex shader for every combination of lights that I want to apply. Say I have an object that receives a directional light, is spherically mapped and a spotlight may or may not be on the object. Do I have to make 2 vertex shaders - one for the situation without the spotlight and one for with?

Share this post


Link to post
Share on other sites
quote:

Do I have to write a new vertex shader for every combination of lights that I want to apply.



In DX8.x, yes!

Although in many situations you could write the shader so that it always applied both lights, then simply modulate the output of each light by a constant - that way you have a sort of dimmer switch for each light. Depends how many vertices you''re passing and how much you''d save by removing a light.

nVidia and ATI also have solutions to this which link "fragments" of shaders together (sort of like a macro programming language) - so you write a bit of code to do a transform, another bit to do a point light, and another bit to do a directional light. Their solutions then join the fragments together on demand when you need a new shader. Take a look on their websites for: ATI ATILLA and nVidia NVLink

In a future version of D3D its been proposed that there will be subroutine and branch support for vertex shaders...

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
I have a question.

My GeForce 2 handles the hardware TnL, but, if I use vertex shaders the code will run in the CPU, isn''t it?

Is there any method to use vertex shader and perform the TnL in hardware?

Thanks in advance

Share this post


Link to post
Share on other sites
IDirect3DDevice8::SetVertexShader

uses built in TnL pipeline instead of your own shader if you pass it FVF flags instead of a shader handle. see the documentation

Share this post


Link to post
Share on other sites
But if I want to do a simple vertex shader for vertex tweening, which method should I use?

Locking a vertex buffer and interpolate the vertex, or use a vertex shader?. The first method can be better in a GeForce 2 card, that use Tnl. But the other can be better in the other cards, that support hardware vertex shaders, or don''t support it (D3D optimizes the code for the CPU)

I''m correct?

Thanks

Share this post


Link to post
Share on other sites
You need different rendering paths for different video cards Adso. Do it one way for a GF2 and another way for GF3, GF4.

Share this post


Link to post
Share on other sites
Why is the VS instruction set missing condition and branch instuctions. It makes the code very inflexible (ie, dynamic number of lights).

Share this post


Link to post
Share on other sites
If I use the D3DX Effects for the vertex shaders and I choose to implement the fixed-function vertex shader in there to automatically transform, does it use hardware?

Share this post


Link to post
Share on other sites
quote:
Original post by JonnyBoy
Why is the VS instruction set missing condition and branch instuctions. It makes the code very inflexible (ie, dynamic number of lights).


1. By imposing this limitation, an implementation is able to perform parallel vertex processing. For example if you have a CPU with SSE and use D3D software vertex processing, it will transform and light *4* vertices at a time (SIMD with the data in the buffer swizzled into SoA format). The same goes for hardware. Allowing arbitrary *conditional* branches would break this ability!

2. You can do conditional code in a vertex shader with the min, max, sge, slt instructions. The idea is to nullify the results of any parts you don''t want such as set the output of any light you don''t want to black. Obviously it means redundant instructions are executed. An alternative is to make a new shader for each permutation - if you''re transforming a lot of vertices with varied numbers of lights, this could in fact be faster than any conditionals.

3. Another current solution to the permutation problem is to use NVLink from nVidia or ATILLA from ATI which are both linkers for fragments of vertex shader code which can be used at runtime.

4. In the provisional 2.0 shader spec which is coming in DX9 (and was previewed at Meltdown), there are some nice things on the way including: SUBROUTINES and LOOPS which should solve almost all cases where you''d like a conditional branch. For example for a variable number of lights you''d simply make the loop counter use a constant which specified the number of active lights.
However to use 2.0 shaders in hardware you''ll need hardware capable of doing them (i.e. nothing currently publically available will ever do them).


--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Where can I find the specs for the 1.4 and 2.0 vertex shaders? In the DirectX 8.1 SDK covers only up to the 1.1 version.

And, anybody knows a fast method of normalizing a vector in vertex shader?

Thanks

Share this post


Link to post
Share on other sites
Adso:

1. There isn''t a vertex shader version 1.4 - the only 1.4 shaders are 1.4 *pixel* shaders, which are fully documented in the 8.1 docs.

2. The 2.0 vertex shader spec isn''t set in stone yet, and there is no hardware available which supports it yet. It''s a DX9 thing - the *proposed* spec was shown at Meltdown last year. The slides from Meltdown are available online, IIRC the URL is something like http://www.microsoft.com/hwdev/meltdown - there should also be a link from the DirectX developers page (http://msdn.microsoft.com/directx).

3. Umm - you have a reciprocal square root instruction - that should be enough to do a proper implementation. Don''t be scared off by CPU performance of square root instructions either - current hardware implementations of vertex shaders tend to have 1 instruction=1 clock cycle timing.
To see smart code using shaders take a look at the ATI and nVidia websites where you''ll find all sorts of cool shaders - including a very smart vector normalisation in a *pixel* shader using *2* instructions!

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
After ten minutes of posting the message, I viewed that the 1.4 is pixel shader and no vertex '':-)

I know there aren''t hardware with vertex shader 2.0, but I wanted to know how they will be.

Now, I have written a simple code to normalize a vector, is only 3 instructions, for testing is sufficient.

I am downloading the nVidia Browser & Effects, but it''s sloooow, no the nVidia site, my 56K connection :-) It''s a 56MB file.


Thanks

Share this post


Link to post
Share on other sites
Anyone know if:
If I use the D3DX Effects for the vertex shaders and I choose to implement the fixed-function vertex shader in there to automatically transform, does it use hardware for this?

Share this post


Link to post
Share on other sites