Followers 0

DX11 DX11 Rendering an Object the fastest way

4 posts in this topic

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 passes

ObjectBuffer.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 += alphaUp

if (!recursive) // Don't send the buffers if recursive
mesh.SendBuffers(devcon, dev, Camera, ObjectBuffer, Bufferphase);

/////////RENDER LIGHTING/////////////////

if (Passes.Lighting)
{
//////////////////////////////////////////////////
////      For each directional light          ////
//////////////////////////////////////////////////

FOREACH (Lights.Directional.safe_size())
{

// INJECT!
Lights.Directional[i].Inject(FrameBuffer);

// Step up one alpha
AlphaStepUp;
}
}

/////////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 id

active->ApplyMaterial(dev, devcon);

active->ShaderRef->UpdateObject(ObjectBuffer, devcon); // Send the object and world buffers

if (relative == BUMPMAP_SHADER) // Is this bump mapping?
{
MatrixBuffer.world = ObjectBuffer.Final;
}

if (relative == TEXTURE_SHADER) // Texturing?
{
}

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 mode
active->Render(devcon, Bufferphase);

// Step up one alpha
AlphaStepUp;
}
}

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

Edited by Migi0027
0

Share on other sites

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.

0

Share on other sites

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!

0

Share on other sites

Well most of the lost performance is from the direct draw calls, is there any way to make them faster?

0

Share on other sites

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 Layouts

2. Layouts To Buffers

3. Buffers to Buffer Partitions, Shaders, etc

4. Model to: (Mesh, Behaviors, Textures, Shaders, etc)

5. Many Textures to Many Models and Meshes

6. Many Meshes to Many Models

7. 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.

Edited by e.s.
0

Create an account

Register a new account

Followers 0

• Similar Content

• By YixunLiu
Hi,
I have a surface mesh and I want to use a cone to cut a hole on the surface mesh.
Anybody know a fast method to calculate the intersected boundary of these two geometries?

Thanks.

YL

• By hiya83
Hi, I tried searching for this but either I failed or couldn't find anything. I know there's D11/D12 interop and there are extensions for GL/D11 (though not very efficient). I was wondering if there's any Vulkan/D11 or Vulkan/D12 interop?
Thanks!

• Hi Guys,
I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
Any advice would be greatly appreciated

• bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }
As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device)
• By ProfL
Can anyone recommend a wrapper for Direct3D 11 that is similarly simple to use as SFML? I don't need all the image formats etc. BUT I want a simple way to open a window, allocate a texture, buffer, shader.

• 12
• 28
• 14
• 11
• 36