HLSL - Lights...is there any point?

Started by
6 comments, last by MooMansun 18 years, 6 months ago
I have been working on HLSL and creating vertex shading effects. From what I have done so far, I cannot see the point in having a light. This may sound strange, however, let me explain. The actual effect of shading is computed using the Light Vector, not the light itself. Regardless of lights being enabled or disabled, the real-time shading is identical. The same is true for every aspect of the light's impact on the shading, color, etc. Why use a light? Is it a redundant overhead in HLSL?
Advertisement
Do you mean using lights as in calling IDirect3DDevice9::SetLight? If so, then you're right. The functions SetLight, SetTransform, SetMaterial, are all used for the fixed function pipeline. Using shaders completely turns off the fixed function pipeline. So calling SetLight when using shaders will have no effect.

All of your lighting computations will happen in your shaders. You set the lights parameters by setting constants in your shader that the shader will then use to compute the actual light in the scene.

neneboricua
I understand that the fixed function pipeline is disabled, however, I have seen HSLS files which create lights (LightEnable[0] = true) and wondered why bother?

I don't see any change unless I use the parameters of the created light, which I can do without the light.
Is there a way to use shaders WITHOUT disabling this fixed function pipeline?

Because there is a big discomfort when creating light dependant shading in the HLSL shader itself. You must know how many lights there are and how they're called when you're writing the shader - and you must give the light variables from the api (or something like that - dont know ^^) to the shader.

So - every time you want to change your scene by adding some lights you must rewrite all your HLSL shaders...


That's why it would be cool to create the constant output color for the material by the shader, and then use the regular function pipeline to add the light dependant shading...


Is ther no way to achieve that?
Quote:Original post by genesys
That's why it would be cool to create the constant output color for the material by the shader, and then use the regular function pipeline to add the light dependant shading...
Is ther no way to achieve that?

You could do this by first rendering unlit diffuse colors with shaders (for example, multiple blended terrain layers or just plain textures) and then doing additional pass using fixed-func pipeline with lighting enabled. Blend this pass modulatively (D3DBLEND_DESTCOLOR, D3DBLEND_ZERO) to framebuffer.

Yes, you would need to set your system to use a mixedvertex processing mode. Again, there is no need to create an actual light, unless you want to use the fixed function.

In pure hardware vp, you just output the light vectors to the HLSL file using SetEffect(). A single class with all the necessary values, nothing major.
Moomansun:

I think you have slightly misunderstood that there is a difference between HLSL, and effect (.FX) files. Effect files can set fixed function states such as lights because they are meant to be usable for fixed function processing, as well as supporting shader-based techniques as well. So it's possible to write an effect that has a technique using a vertex shader with light vectors obtained from code on cards with shader support, and another technique using the fixed function lighting set up. Does that make sense.

genesys:

You can use a for loop inside your vertex shader to decide how many lights to process - so you don't need to rewrite them every time. On the earlier shader models the number of lights can be included as a technique parameter as uniforms, and the compiler will generate the right shader code for you. In later shader models it can be dynamic (if you want) and you could set it as a parameter, and it will loop properly. Effects are very flexible like this, and can be coaxed into doing a lot of things :)

-Mezz
Oh, right...that explains it.

Nice one.

This topic is closed to new replies.

Advertisement