Sign in to follow this  
weewoo

Shader Engines

Recommended Posts

Hello All, Sorry to bring this up again if its already been discussed but I ive searched the forums multiple times to find a discussion on it and have failed. I have a few questions regarding shader engines and how they deal with lighting scenarios. I'm guessing most games use some sort of Light Manager that will grab all the lights in the scene that affect the object being rendered and in some way sort them based on how much they affect the object. It would then set the appropriate constants on the shader with the lights affecting it. Now my question is this... How do you deal with a scenario where the lights you are setting on the shader are all different. For example a shader that is setup for three directional lights. The LightManager returns 2 directional and a spot light and two point lights that barely affect it. In this scenario, the two point lights are ignored since the shader can only handle three lights. It would then set the constants in the shader for the first three light slots however since this shader is designed for 3 directiona lights, the spot light will be applied as a directional. It seems one would have to design every shader with every different combo possible and assign the appropriate one to the object based on the light setup. Is there a better way to automate this process? It seems you could pass over all the light information to the shader so it can tell what lights are of what type however since all the lighting code is different for each type of light, then I can see this being an issue since most shader models do not support Branching Statements. Also, when doing outdoor lighting and the player moves indoor, im guessing each light would have to be marked as outdoor and indoor and the Light Manager would need to know if it should return indoor or outdoor lights. Because directional lights in the outdoor scene should not be applied to the objects indoors.(unless of course its near a window...yet another problem) Now, when dealing with PostProcessing effects. I understand that you have to render the scene to a texture and than apply that texture to a full screen quad and render it with the effect. Now in a game where these post process effects get added at various times during gameplay, It seems the whole pipeline has to be configured from the start of rendering to render to a texture rather than the back buffer. I know you can copy the backbuffer to a texture but i'd imagine this would be a slow and bad approach. Does anyone have any ideas on maybe designing an engine to handle such events? I guess I am just having problems wrapping my head around the above situations and coming up with a good class design to address those issues. Any information would be greatful! Thanks in advanced.

Share this post


Link to post
Share on other sites

Quote:

In this scenario, the two point lights are ignored since the shader can only handle three lights. It would then set the constants in the shader for the first three light slots however since this shader is designed for 3 directiona lights, the spot light will be applied as a directional. It seems one would have to design every shader with every different combo possible and assign the appropriate one to the object based on the light setup. Is there a better way to automate this process? It seems you could pass over all the light information to the shader so it can tell what lights are of what type however since all the lighting code is different for each type of light, then I can see this being an issue since most shader models do not support Branching Statements.


I believe that this is generally done by automatically generating shaders for each specific light type. In the shader, you could have a function that computes all of the lighting, and then automatically generate different versions of the shader with different implementations. You can use dynamic branching to make this easier if your target hardware supports it.



Quote:

Now, when dealing with PostProcessing effects. I understand that you have to render the scene to a texture and than apply that texture to a full screen quad and render it with the effect. Now in a game where these post process effects get added at various times during gameplay, It seems the whole pipeline has to be configured from the start of rendering to render to a texture rather than the back buffer. I know you can copy the backbuffer to a texture but i'd imagine this would be a slow and bad approach. Does anyone have any ideas on maybe designing an engine to handle such events?


I'm confused. How is rendering to a texture that much different from rendering to the backbuffer? In DX at least, all you need to do is set a different render target and then call the exact same rendering routines.

JB

Share this post


Link to post
Share on other sites
Setting the RenderTarget is not the confusing part to me. I guess the question is more of a design question. If i understand correctly the engine just needs to know before hand on whether or not it will be doing post processing on that frame and if so, switch the render target to a texture instead of the back buffer.

Now as far as your reponse goes to the light scenario, your response is kinda what ive read in other posts. Dynamic Branching is only supported in Shader Model 3 correct? Not a very good option since i'd like to atleast support 2.0.

From what you described it seems that you are doing one pass per light which from what i understand is not very effecient. Is this how most engines are doing it?

I think one thing i never fully understood was how people were "automatically generating shaders". I've heard some people doing it with include files, etc.
Is the way of doing this by creating the effect file in memory. This effect file would contain the proper includes and techniques/passes to link it all up? Then the shader is created at runtime from this memory?

Share this post


Link to post
Share on other sites

Dynamic branching is only supported for pixel shaders in PS3.0

For PS2.0, you'll want to do as many lights per pass as you can afford. In order to make this work, you need to have as many distinct shaders as you have light combinations. The easiest way to do that is to build a basic shader framework and write code that automatically fills it in. This could be done either when your app loads, or as a pre-process.


JB

Share this post


Link to post
Share on other sites
Does anyone know of any technical articles/write-ups on this dynamic shader creation based on different possible light combinations?

All i seem to ever find/hear are just the concepts...

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