Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to ask if there is a best practice for setting Effect parameters in XNA. Or in other words, what exactly happens when I call pass.Apply(). I can imagine multiple scenarios:

  1. Each time Apply is called, all effect parameters are transferred to the GPU and therefor it has no real influence how often I set a parameter.
  2. Each time Apply is called, only the parameters that got reset are transferred. So caching Set-operations that don't actually set a new value should be avoided.
  3. Each time Apply is called, only the parameters that got changed are transferred. So caching Set-operations is useless.
  4. This whole questions is bootless because no one of the mentions ways has any noteworthy impact on game performance.

So the final question: Is it useful to implement some caching of set operation like:

private Matrix _world;
public Matrix World
{
    get{ return _world; }
    set 
    {
        if (value == world) return;
        _effect.Parameters["xWorld"].SetValue(value);
        _world = value;
    }
}

Thanking you in anticipation.

share|improve this question
I added the DirectX tag, on the basis that this is lower-level functionality than XNA. – Andrew Russell Oct 15 '12 at 13:20

1 Answer

up vote 8 down vote accepted

This all happens on the CPU side, so if caching were a useful feature, then I would speculate that the graphics driver would implement it itself. Adding your own caching layer is unnecessary.

My understanding is that whenever you set a parameter, and whenever you call Apply, these calls are passed to DirectX largely as-is, and in turn passed to the user-mode GPU driver as-is. The user-mode driver can then do whatever the hell it wants. All three of your scenarios are possible.

(Because scenario #2 is a possibility, it's probably best not to run around deliberately re-setting parameters that don't change.)

To be honest, I'm not really sure what a typical driver does. Mostly because it's never really come up as an issue. I've never heard of anyone having effect-parameter setting as a bottleneck. Maybe it could be, in theory. But there are so many more common things to worry about.

Certainly don't start implementing optimisations like this without measuring your performance and understanding what's going on.

Also, comparing a Matrix with == is bad voodoo. A Matrix is made up of floats, and floating-point equality comparisons are prone to failure in many cases.

And, generally speaking, the pattern if(x != y) x = y; is slower than simply x = y.

share|improve this answer
Interesting point that driver should care about this. Thanks for the link (and re-links). – hichaeretaqua Oct 15 '12 at 14:36
I recently came across the geometry instancing example from msdn. Resetting the same renderstates (with same values) multiple times per frame significantly slows down the rendering process by two or three times. So state batching is deferentially useful. Unfortunately I'm not sure weather this situation applies to setting effect parameters, too. But I would like to share my information. – hichaeretaqua Nov 21 '12 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.