   DX11 Rendering an Object the fastest way
Hi guys,my engine has come to a stage where instancing will be required, so what i do right now is somewhat like this:void RenderFunc(..){ renderAllLights RenderDiffuse If instancing: // This one knows if I'm rendering an instance   RenderFunc(instance);}  Now the thing is that I'm using one render function to render all meshes, is that a bad idea? My full render function looks like this: void C3DEngineObject::RenderMesh(UMesh &mesh, bool recursive){#define Passes mesh.passes  Passes is just a class containing information for the passesObjectBuffer.AmbientColor = Passes.GlobalMaterial.AmbientColor;/////////PREPARE ALPHA SETTINGS////////////ObjectBuffer.Alpha = 1.0f;ObjectBuffer.Alpha = Passes.getAlpha();if (Passes.Lighting){ObjectBuffer.Alpha += Lights.Directional.size();BlendStateManager.AlphaPass(devcon);}float alphaUp = 1.0f/ObjectBuffer.Alpha;ObjectBuffer.Alpha = 1.0f/ObjectBuffer.Alpha;#define AlphaStepUp ObjectBuffer.Alpha += alphaUpif (!recursive) // Don't send the buffers if recursivemesh.SendBuffers(devcon, dev, Camera, ObjectBuffer, Bufferphase);/////////RENDER LIGHTING/////////////////if (Passes.Lighting){////////////////////////////////////////////////////// For each directional light//////////////////////////////////////////////////////FOREACH (Lights.Directional.safe_size()){Shaders[7].ApplyShader(dev, devcon);// INJECT!Lights.Directional[i].Inject(FrameBuffer);Shaders[7].UpdateObject(ObjectBuffer, devcon);Shaders[7].UpdateWorld(FrameBuffer, devcon);//Shaders[7].SendRegBuffer(FrameBuffer, 1, 0, devcon);Shaders[7].Render(devcon, Bufferphase);// Step up one alphaAlphaStepUp;}}/////////RENDER THE MESH/////////////////for (int p = 1; p <= Passes.options; p++) // Get all the passes{if (Passes.getAt(p) == true) // Is this pass enabled?{int relative = Passes.getRelative(p); // If so, find the mat id.Material* active = getMaterial(relative, Passes); // Find the mat of the idactive->ApplyMaterial(dev, devcon);active->ShaderRef->UpdateObject(ObjectBuffer, devcon); // Send the object and world buffersactive->ShaderRef->UpdateWorld(FrameBuffer, devcon);if (relative == BUMPMAP_SHADER) // Is this bump mapping?{MatrixBuffer.world = ObjectBuffer.Final;active->ShaderRef->UpdateMatrix(MatrixBuffer, devcon);active->ShaderRef->SendTexture(Passes.base->pTexture, 0, devcon);active->ShaderRef->SendTexture(Passes.bumpmap->pTexture, 1, devcon);}if (relative == TEXTURE_SHADER) // Texturing?{active->ShaderRef->SendTexture(Passes.base->pTexture, 0, devcon);//active->ShaderRef->SendTexture(Passes.opacityMap->pTexture, 1, devcon);}if (mesh.Instanced && !recursive) // Is this mesh instanced{FOREACH (mesh.InstancedData.size()) // For all instances{mesh.SendInstancedData(ObjectBuffer, Camera, i, Bufferphase); // Send the location, rotation and scaling of this instance.RenderMesh(mesh, true); // Call this function, with recursive true so the origional matrixes wont be re-sent (removing the instance loc,rot and scaling).}}// Render the mesh in diffuse modeactive->Render(devcon, Bufferphase);// Step up one alphaAlphaStepUp;}}if (recursive)return;BlendStateManager.RestoreBlendState(devcon);}  PS. The mesh.passes is just a class with contains the passes(e.g. Texture, shadows, Lighting).Now if you look at my code, in each pass it checks if it is bump mapping or texturing, but is this a slow method, how could it be improved? Or even better, how can it be improved in the concept of instancing? Also if you have time, it could be nice if you could post a good way of handling mesh rendering, or an efficient method, as I'm just following my minds advice, and not a professional persons advice. Also, how much time does it take to check if a bool is true?Thank You
It seems to me like you are looking for improvements without a strong need in mind, which will make it very hard to find a solution that you are looking for.  Ultimately, you will find that normally the CPU side manipulations account for a fairly small amount of work in the grand scheme of things unless you are using lots and lots of small objects.  However, even in that case, the GPU work being done can often take up more time than the CPU side - meaning that any optimization on the CPU side won't make any difference to the end result framerate. For your specific question about testing a bool - it is pretty much one of the fastest operations that exist, so you don't need to worry about changing that.  You should invest some time in learning how to use profiling tools (free or otherwise) and also tools like PIX/Graphics Debugger to learn more about the performance of your engine/app.  That will allow you to pinpoint the bottlenecks and make effective use of your optimization time.;Thanks Jason Z, though I already knew of the profiling tools, I never really put any effort into it, but they(tools) actually impress me!;Well most of the lost performance is from the direct draw calls, is there any way to make them faster? ;Most of the loss of performance is /not/ the draw calls, but a Game Engine's utilization of them...  From an architectural point of view, the code hint that you may not be implementing standard "Separation of Concerns" patterns, (ensuring that methods/classes don't do more than one thing, but rather responsible for doing a specialized purpose).  In other words, at this stage, I believe everyone would agree that getting your engine do to what you want is more important than the performance. For example, you are using a "renderfunc"  and "rendermesh" function names: Okay, I know this sounds a little out of left field for being, nitpicky, but bear with me ... Rather than, RenderScene, -> RenderEachLayout  -> RenderBufferSet -> RenderBufferPartitions ->  or some other rendering step in the context of your Game Engine's OWN Pipeline. In a game engine, there is /massive/ importance on the efficient management of the associations between Modeling entities, specifically:1. Scenes to Layouts2. Layouts To Buffers3. Buffers to Buffer Partitions, Shaders, etc4. Model to: (Mesh, Behaviors, Textures, Shaders, etc)5. Many Textures to Many Models and Meshes6. Many Meshes to Many Models7. and the list goes on and on and on and on. The GPU operations are /fast/ ....  Managing memory, garbage collection, managing collections in memory that are hundreds of megabytes, etc, is one of the biggest game performance hits.  This is where a lot of your optimizations will need to occur.  As horrible as it sounds, use place holder shaders, textures, etc at first, (greyscale gradients, etc), to ensure your Game Engine's Framework is working right. In other words, pick a game engine feature, set of features that you want, program towards them, and find out if those features are running slow or not, then performance tune them. Stage 1 Features:1. Rendering Multiple Instances of the Same Model definition.  (sidewalk squares)2. Rendering Multiple Instances of the Same Model, but with different Shaders,  (stars, the same car but different color, etc)3. Rendering Multiple Types of Models, (Tree, Sidewalk Square, etc)4. Pre-Defined animations for a subset of models, (rotations, movement/translation in a circle pattern, movement along a Bezier, whatever).5. User Driven Movement,6. etc. There are a /lot/ of design patterns that can be used to organize the complexity of the code, so that the application will scale appropriately with the features you want. You may think that implementing facades, multiton, or factory design patterns are a /lot/ of overhead for a small program, but in actuality this is not the case as compilers optimize and inline what needs to be.  Still, the point that I am trying to make, the use of Design Patterns, and the avoidance of Anti-Patterns in a game engine is what is going to always be what will determine your game engine's performance story. Then, there are fancy shmancy GPU render operations... See, the funny thing about fancy shmancy GPU operations is that /only if/ your architecture is "sound" in the first place, can you implement GPU operations on a priority basis, (maybe ensure that that that fancy shmancy model never attempts to get rendered more than once a frame, or maybe once every two frames, (there is a LOT of overrendering in games). Architectural techniques and optimizations are probably where you want to start off first when facing performance problems, and regardless, you will be able to do more with your engine anyway.