Sign in to follow this  

Automate setting effect parameters

Recommended Posts

I know there have been quite a few topics about this subject, I read a lot of them, but I still have a few issues I can't resolve with my 'effect system' design. I've pretty much automated all drawing in my game framework. Each frame I pass all meshes that should be drawn to the renderer, renderer does all the drawing automatically. This was very easy until effects (.fx files) kicked in. I am writing a number of shader effects like per pixel lighting, shadow mapping, texture splatting, bump/parallax mapping, etc. Now I'm having issues automating this in code. These are the problems I foresee: In a map editor, the end-user will be able to select geometry and choose an effect from a list (e.g. diffuse lighting, bump&diffuse, etc). Probably they can also toggle options like "Casts shadows" on renderable objects. This means I do not know in advance which effect is desired, and a combination of effects may be necessary (e.g. bump¶llax mapping effect with "Casts shadows" enabled is basically 2 or 3 effects). In addition, for flexibility I might need to allow users to define their own effects (even if it is as simple as allowing them to write and load their own .fx files). When the application starts, the user's computer will be checked to see what support for shaders it has. On older hardware certain effects may not be available, and need to be replaced by effects that are supported, and resemble the desired effect as much as possible. All and all there are a lot of factors that make it kind of impossible for the renderer to know in advance which shader parameters need to be set (setvalue... blabla). I might need light information, textures, matrices, etc., depending on the effect(s) that are chosen to render the object. The way I see it, retrieval should be fairly simple. When rendering a renderable object, the renderer should probably have access to something like ObjectState (with object-specific settings like textures, world transformation, etc.) and WorldState (light information, view/projection matrices, etc. etc.). These states should hold the information required in the SetValue calls. However, since it is not known in advance what information is actually required, a 'frame problem' (for the AI people here) occurs, I basically need to store all information available so that the required info is always present. Ouch, memory wastage? But even if that is sorted out, how does the renderer know which values to set, and which info from the object/game state it should be set to? It heavily depends on the effect(s) used, and these effects might not even be known when compiling the framework, as users may add new ones. Another problem I have is, how do you combine effects? E.g. the mentioned example where you can toggle shadow casting, it would mean the object required a 2-pass effect just for shadows, plus the effect it is actually rendered with. Now suppose we are drawing terrain, this could mean the 'effect it is actually rendered with' is also a combination of effects, e.g. some lighting effect and texture splatting. How would I combine all this? Last question, which is more of a general question, is there a downside to putting all effects in 1 fx file? (meaning, is it slower and such) It seems logical to me to put it all in one big file because of the way you define techniques. There is the disadvantage of the file being a bit messy, but that shouldn't have to be a problem imo. Who can help me with the design of this system?

Share this post

Link to post
Share on other sites
I'll try to answer what I can here.

The renderer can find out what values a shader wants. You can query a shader either for parameter names, or parameter annotations and figure out on the fly what its requesting. If it requests parameters that you're engine doesn't support or understand, then, you just simply don't allow that effect.

Personally, I like keeping all of my shaders as seperate .fx files, instead of using techniques. It basically amounts to the same thing, but, I find it easier to manage that way. Plus that has the advantage of, when you query for parameters as mentioned above, you only get the parameters for the specific shader you are requesting and not all of them.

As for multi-pass effects, you can do this either by:
Using the actual multi-pass abilities of the .fx format, you can have multiple passes in a technique.
Setting up something in your editor, where you build a list of effects to apply either manually or based on the properties that the user has set. This starts getting rather specific to your engine/game.

Share this post

Link to post
Share on other sites
For my engine I'm using a different approach.

I have different effects (like .fx files) and every effect has pointer to the specific parameters that needs to pass to the shaders.
Tipical parameters are world/view/projection matrixes, light properties, material properties, textures...
Every cycle the effect itself take the actual values from the pointers it has, and pass it to the renderer to set these values.

For the different properties of effects, I'm implementing a "shader library" in which I create functions for every property.
An example is the function that performs per-pixel light from a spotlight, one for texture splatting, one for bump of parallax occlusion...then you define shader using these functions.
If you have also an editor, it can be easy (I hope!) to implement a system that can link together these properties and build a custom shader.

Multi-pass shaders are always slower than single pass one, so it is better to implement many things in a single pass. Consider that every pass means render all the visible geometries again, using the vertex/index buffers, textures and shader switching...
Shader library is definitely a better approach (for me).

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