Advertisement

Factory Classes

Started by April 04, 2001 08:40 PM
2 comments, last by Jx 23 years, 10 months ago
In my engine I want to have maths code for different processors stored in plugin .DLL files that I load at runtime depending on which processor is used. The thing is, this introduces virtual function calls into my classes. This is ok for certain things, but not for things like my vertex class which is going to used thousands of times per frame. I saw a post on here the other day where someone suggested using factories to solve this problem, but now I can''t find it. Can anyone help? If the factory method doesn''t solve this problem can you post it anyway as i''m curious.... cheers jx
Well, using a dll doesn''t make the function call virtual, it just uses function pointers. A virtual call requires an array index, and then a function pointer dereference.

I don''t think dll''s or a factory is a good solution to cpu optimized math ops. You''ll probably lose the performance gain in the overhead, 10x over...
I don''t think making optimized MatrixMult''s, K6, K7, P2, P3, P4, etc... and using a funtion pointer for each call is efficent... You''d need three seperate engines - like the same code compiled to three/four/whatever dlls depending on switches, so that the code is right in there. And GameStart is the dll call, instead of every MatrixMult.

Anyway, an IMathFactory

  class IMath{public:IMath();virtual ~IMath()=0;virtual VECTOR3D Vector3DMult(VECTOR3D, VECTOR3D)=0;virtual MATRIX4D MatrixMult(MATRIX4D, MATRIX4d)=0;};class CAMDMath : public IMath{CAMDMath(){}virtual ~CAMDMath(){}virtual VECTOR3D Vector3DMult(VECTOR3D, VECTOR3D){//3D Now! code to do vector mult}virtual MATRIX4D MatrixMult(MATRIX4D, MATRIX4d){//3D Now! code to do matrix mult};class CPIIIMath : public IMath{CPIIIMath(){}virtual ~CPIIIMath(){}virtual VECTOR3D Vector3DMult(VECTOR3D, VECTOR3D){//PIII code to do vector mult}virtual MATRIX4D MatrixMult(MATRIX4D, MATRIX4d){//PIII code to do matrix mult};//On & on...class CMathFactory{public:static IMath* InstanceIMath(){//Determine CPU typeswitch(CPUType){AMD:return(new CAMDMath);PIII:return(new CPIIIMath);Default:return(new CMath); //normal C code to do the job if we have no special code}}};  


You could make a specific IVectorMath, & an IMatric, IQuaterion, etc... and have the MathFactory make each one for you.

You could put all this stuff in a dll, and then you could add support for new processors by updating the dll (screams COM). Again, I don''t recommend that route for performance reasons, but it could be interesting to see how bad it is.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Additioanlly, function pointers break the branch prediction. The best way I can think of is to use nested if''s..
Chris Brodie
Sorry... in my original post I wasnt very clear.

Magmai Kai Holmlor:

The way you have show there is effectivley how I do it, just with the different classes stored in different .DLLs. All i do when I want to load a news maths class is have a function in the .DLL called getInterface() or something similar which returns a new CAMDMath object or whatever. This is the same as you have there really. What I was wondering is that because CAMDMath and CPIIIMath etc are derived from a base class which has the methods as virtual, doesn''t this make the calls virtual in all the derived classes and negate the optimisation?


The way I do it at the moment is as per this tutorial on GameDev:
http://www.gamedev.net/reference/programming/features/intdll/

My question is basically: Is this method too slow for things like vectors and matrices even with optimisation?

Jx








This topic is closed to new replies.

Advertisement