   Fast Matrix Library
  Does such a thing exist?For transparency sorting, I need to multiply a fair amount of matrices each frame... 6 for each triangle (3 verts, 3 normals).Is there any library has fast matrix functions (all I really need is multiply I suppose...)? The one I'm using currently is eating up 50% CPU with a 420-triangle model (of course, I highly doubt 420 triangles will have to be sorted each frame but still).ThanksEDIT: And I'll also need to use more matrices for skeletal animation.[Edited by - Gumgo on September 10, 2007 9:43:56 AM]
  Well, you won't get any faster than the hardware-accelerated matrix multiplication on your graphics card. Is this an option, or must you work in software? If you are restricted to software, don't expect any magical matrix libraries to come to your rescue. Matrix multiplication is a pretty basic operation and it's hard to code it badly. Nevertheless, D3DX does a good job of taking advantage of SIMD processor operations to speed up vector maths. While this is the best out there, don't expect wonders.Where did you get that 50% figure from? I hope it's a CPU profiler and not Windows's Task Manager [rolleyes].Anyway, are you sure that the code is already macro-optimised? there aren't too many situations where you'd need to transform every vertex in a model for the purposes of depth sorting. Typically, the designer would group the 3D model into logical convex units (often according to their bones) and treat them each as a self-contained object. Back-face culling will take care of the rest. By working with centres-of-mass in this way, you can typically cut your working set down from thousands of vertices to a dozen or two.As for the skeletal animation, we tend to rely on vertex-shaders to do this for us (or even better, geometry shaders if you're on SM4).Give us some more details of what you're really trying to do. No offence intended, but I suspect that you're solving the wrong problem in the wrong way [razz].Admiral   ;Quote:Well, you won't get any faster than the hardware-accelerated matrix multiplication on your graphics card. Is this an option, or must you work in software?Well I'd love to use hardware accelerated matrices but I don't know how to access them other than using OpenGL or DirectX functions that have to do with rendering. I didn't know you could.Quote:Where did you get that 50% figure from? I hope it's a CPU profiler and not Windows's Task Manager.The exact amount doesn't matter. Even if I just listen to my computer's fan I can tell that my computer is working much harder when sorting the triangles then when not. I commented out sections of code and discovered that the matrices were the cause.Quote:Anyway, are you sure that the code is already macro-optimised?No I'm not sure of anything. I just downloaded a matrix library to test out whether it would work and it seemed to but now I need a faster solution.Quote:aren't too many situations where you'd need to transform every vertex in a model for the purposes of depth sorting.Quote:Give us some more details of what you're really trying to do.I'm simply finding the distance to the camera and sorting triangles like that but in order to find the distance to the camera I must transform the vertices to their proper rendering locations.Today in school though I just had another idea... the only thing I need to get for each triangle is the distance to the camera... So would it work if, rather than rotate/scale/translate each vertex to find the distance to the camera, I temporarily rotated/translated/scaled the camera oppositely around the object? That would be only 1 (well, 2, 1 for vertices and 1 for normals) matrix-multiplication per frame rather than 1 (2) per vertex.Quote:As for the skeletal animation, we tend to rely on vertex-shaders to do this for us (or even better, geometry shaders if you're on SM4).For skeletal animation-I don't know how to use shaders at all. But since I will probably be doing a lot of matrix multiplication, that hardware-accelerated matrix thing sounds pretty useful right now. I'm using OpenGL, where can I get more info on that?EDIT: Actually... If the hardware accelerated matrices are in the GPU then I can't use them except for rendering it would seem... so never mind about those.   ;  Sorry for not answering your questions, but I think TheAdmiral has raised an important point. Transforming and sorting only 420 triangles should only take a few microseconds. If something is causing a frame-rate hit, it is probably not that.In contrast, I have a particle system with 20,000 particles transformed and sorted in software. I don't do anything special. It runs about 30 fps. Transforming and sorting the particles are a big part of the CPU cost, but compare my 20,000 triangles to your 420.   ;  That's what I'm confused about as well. But when I comment out the matrix multiplication code it goes back down to 0-2% CPU.And just to make you happy TheAdmiral I'll download a CPU profiler and test it.I'd assume that it is just a really slow matrix library coded poorly or something because I can't think of any reason why it would use up so much CPU. I think if I translate the camera once rather than translate each vertex, it will speed up quite a bit though.To do that, I'd simply have to use (-vx,-vy,-vz) and angle for the camera rotation and (-x,-y,-z) for the camera's position offset, right? (where vx, vy, vz, and angle determine the axis of rotation for the object and x, y, and z is the translation)   ;  Does your *frame rate* drop?*any* code, even an *empty* loop, will eat all the CPU if you don't interrupt it (i.e. yield the processor to other processes). Is there any conceivable way that the code with the matrix multiplication installed somehow misses any thread sleep/yield calls?   ;Quote:*any* code, even an *empty* loop, will eat all the CPU if you don't interrupt it (i.e. yield the processor to other processes). Is there any conceivable way that the code with the matrix multiplication installed somehow misses any thread sleep/yield calls?Well... yes. What I think is that it just wasn't programmed well.This is the one I am using.I downloaded it just to test matrices out and to see if I could get it working, even though it does say it wasn't built for speed/efficiency in the readme. So I wouldn't be surprised if there is problems with it.I'm not quite sure what you mean by missing sleep/yield calls... but whatever the case is with it, I'm not going to stick with this matrix library if possible because it was just a temporary test.Anyways that's why I made this thread, to see if anyone knows of any matrix libraries that are maybe a little better, optimized, efficient, etc.EDIT: WHOA, yeah, there's definately something screwy going on with this library... something very weird. I made it so the camera is translated rather than the vertices and that helped a TON. But still, this is so strange. This code:tform = !tform;campos *= tform;Which makes the transform matrix inverse and then multiplies it with the camera position matrix, running ONCE per game loop, makes the CPU jump from 2-7 percent to... 25 percent! There's obviously something very strange with this library which is why I need another quite badly.[Edited by - Gumgo on September 11, 2007 1:10:09 AM];  Have you checked CML ?http://cmldev.net;  Thanks for the link! This looks like it will work much better.   ;Quote:Original post by GumgoThanks for the link! This looks like it will work much better.Glad to hear you're considering the CML, and I do hope you give it a try. Meanwhile though, a couple words of caution...First of all, determining exactly why a program is performing poorly (and then fixing it) takes some experience and expertise - you'll often find that your first guess at the source of the problem is wrong. In other words, I wouldn't assume that plugging in a different math library will solve the problem that you're having (although it might).Actually, just go back and read TheAdmiral's first post :-) The point is that it's usually possible (and preferable) to avoid performing extensive linear algebra computations in software. Use your math library to construct and manipulate transforms and so on, but let the hardware do the heavy lifting.As for the sorting you mention, I'm still not clear as to why you need to sort on a per-triangle basis; that could end up being slow no matter what library you use.Still, if you do need a math library, I hope you'll take a look at the CML (and feel free to contact me if you have any questions about it).   