Sign in to follow this  
newObjekt

OpenGL Vertex Weight / Skinning Calculations - GPU or CPU?

Recommended Posts

newObjekt    208

Hello, I'm a self taught programmer and I'm currently working on my own game engine built from the ground up using LWJGL. I'm currently having a pretty big performance problem with animations. I have written my own format for skeletal meshes and animations that I export from 3DS Max and then import at load time. The information is stored in a simple array of bones, vertexes and, triangles and rendered by OGL. For animation I use quaternions for the rotation values of each bone.

 

On each update I move and rotate the bones to the next place they need to be in the animation and then calculate the movement and rotation for each vertex based on their weight. 

- I use doubles for almost all my location and rotation values in the engine.

- I am doing the vertex calculations on the CPU with my own Math classes and what not.

- I am currently only using a single thread for the model updates.

- In java I am using the built in Math.sin, Math.cos, Math.tan for a lot of calculations.

 

As it stands the models animate fine but it takes too long to do the calculation for the animations. I get noticeable slow downs after I have about 20,000 vertexes being updated on every game step.

 

Should I try using floats? Should I try using the OpenGL GPU skinning api? Should I try splitting the calculations out to seperate threads? Should I find a faster math Library for java?

 

All advice is welcome. :)

I can post the actual code for my vertex weights if it would help as well.

Share this post


Link to post
Share on other sites
TheChubu    9446

I don't think you'll find a faster lang.Math library implementation. I've even read that Android's lang.Math library, which uses floats instead of doubles, is slower. And the JDK implementation is written in C++ last time I checked.

 

A completely (un) educated guess of mine would be to see if you have GPU to spare. If you have, do the skinning on the GPU, if you don't, profile (VisualVM, free profiler for Java for example) and see if you can optimize the slowest parts of the skinning process.

Share this post


Link to post
Share on other sites
Ashaman73    13715

- I use doubles for almost all my location and rotation values in the engine.

- I am doing the vertex calculations on the CPU with my own Math classes and what not.

- I am currently only using a single thread for the model updates.

- In java I am using the built in Math.sin, Math.cos, Math.tan for a lot of calculations.

This are all more or less all reasons for a slow down. Use the GPU for skinning, though bone calculations can be left on the CPU for now.

Share this post


Link to post
Share on other sites
C0lumbo    4411

Multithreading should be a really big win for you assuming you're targeting machines with multiple cores. If it were C++, I'd say you should always use floats instead of doubles, I don't know about Java. Hardware skinning (using the GPU) is a trade-off - it will speed things up on the CPU and slow things down on the GPU, so it depends on your game's balance.

 

Another thing to consider is lodding. Can you switch to lower detailed models for distant skins? Can you reduce the number of weights per vertex for lower lod models? Can you get away with calculating the normal based on only the largest bone weight instead of all of them?

Share this post


Link to post
Share on other sites
newObjekt    208

GPU usage right now in my engine is very low. I have not put in any kind of post processing or shading so far. Just standard diffuse texture rendering. I have also not implemented lighting in any way yet. 

 

I don't know how heavy the GPU usage will be once I get those things added in and I have no idea what kind of improvement in performance it would grant me. I would love some statistics on that if anyone has done some research on it.

 

I was thinking maybe I could move the vertex weight updates out to there own thread that runs alongside physics and game world updates. Should I put each model skin update on it's own thread or just have a single thread dedicated to handling those?

 

The LOD idea is great. I can't believe I haven't even thought about that yet. I can easily add that though so it's good that you mentioned it :D

Share this post


Link to post
Share on other sites
Schrompf    1035

Put the skinning on the GPU. Todays gamer GPUs have such an incredible amount of computing power specialized for exactly this kind of calculations that you wouldn't be able to measure a difference even when your GPU is fully loaded. 

Share this post


Link to post
Share on other sites
newObjekt    208

Put the skinning on the GPU. Todays gamer GPUs have such an incredible amount of computing power specialized for exactly this kind of calculations that you wouldn't be able to measure a difference even when your GPU is fully loaded. 


Alright, I'm going to branch my CPU skinning and implement a GPU skinning and do some speed comparisons.

Edited by newObjekt

Share this post


Link to post
Share on other sites
mhagain    13430

Yeah, definitely go with GPU.

 

  • As you add more functionality elsewhere the CPU load is also going to increase.
  • Even if you thread it you've still got a max of 8 or so threads on current hardware; Even low-end GPUs have hundreds.  And the threading is free, with no messy code required on your part.
  • Even if you use SSE instructions, GPUs are still better optimized for this kind of calculation.  Also free, with no messy code required on your part.
  • Even if none of the above matter to you, you've still got a heavy upload of the transformed positions to do; using the GPU you only need a much lighter upload of bone matrices.
  • As you add more animated models to the scene that heavy upload is only going to get worse; using the GPU the upload will remain considerably lighter.
  • A GPU method allows for cool things such as different models sharing the same skeleton with much less overhead.

Share this post


Link to post
Share on other sites
newObjekt    208

Alright so I've done some research on GPU skinning and to be honest I am beyond lost.

 

Simply put I have this format in my engine

 

Bone

position(x,y,z) //original position

positionOffset(x,y,z) //distance moved (used in the animation)

rotationOffset(x,y,z,w) //rotation quaternion(used in animation)

 

Vertex

position(x,y,z) //position of vertex

weights[ ](bone, weight) //array of weights, each weight contains a bone and a weight

 

When I need to do skinning I just loop through every vertex in the model and use the vertex.weights to move and rotate the vertex.

 

How the balls do I accomplish this on the GPU using OpenGL?

Edited by newObjekt

Share this post


Link to post
Share on other sites
Schrompf    1035

What keeps you from doing exactly the same in the shader? And don't tell me you're using the fixed function pipeline, I'm out then.

 

To stay in your terms: you usually combine the bone's bind transformation (the original position and rotation) in relation to the mesh, plus the current rotation offset and position offset, all into one transformation matrix. You end up with an array of matrices which you upload to the shader. You enhance your vertex structure to contain a number of bone indices (normally 4, because the fit nicely into an input register) and an equal count of weights that specify how much the vertex is influenced by that bone. So every vertex in your mesh says: I'm affected by bone 13, 25 and 5, with a weight of 0.3, 0.25 and 0.45, respectively. The shader now simply transforms the vertex by each bone matrix, accumulating the result weighted by the corresponding vertex weight, and you're done.

Share this post


Link to post
Share on other sites
newObjekt    208

What keeps you from doing exactly the same in the shader? And don't tell me you're using the fixed function pipeline, I'm out then.

 

To stay in your terms: you usually combine the bone's bind transformation (the original position and rotation) in relation to the mesh, plus the current rotation offset and position offset, all into one transformation matrix. You end up with an array of matrices which you upload to the shader. You enhance your vertex structure to contain a number of bone indices (normally 4, because the fit nicely into an input register) and an equal count of weights that specify how much the vertex is influenced by that bone. So every vertex in your mesh says: I'm affected by bone 13, 25 and 5, with a weight of 0.3, 0.25 and 0.45, respectively. The shader now simply transforms the vertex by each bone matrix, accumulating the result weighted by the corresponding vertex weight, and you're done.

Yeah this is basically what I read about but I could find any explanation on how to do it in code. 

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  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now