• Advertisement
Sign in to follow this  

DX11 DX11 Rendering an Object the fastest way

This topic is 1809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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())
		{
			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 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
			active->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 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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By mister345
      Hi, can somebody please tell me in clear simple steps how to debug and step through an hlsl shader file?
      I already did Debug > Start Graphics Debugging > then captured some frames from Visual Studio and
      double clicked on the frame to open it, but no idea where to go from there.
       
      I've been searching for hours and there's no information on this, not even on the Microsoft Website!
      They say "open the  Graphics Pixel History window" but there is no such window!
      Then they say, in the "Pipeline Stages choose Start Debugging"  but the Start Debugging option is nowhere to be found in the whole interface.
      Also, how do I even open the hlsl file that I want to set a break point in from inside the Graphics Debugger?
       
      All I want to do is set a break point in a specific hlsl file, step thru it, and see the data, but this is so unbelievably complicated
      and Microsoft's instructions are horrible! Somebody please, please help.
       
       
       

    • By mister345
      I finally ported Rastertek's tutorial # 42 on soft shadows and blur shading. This tutorial has a ton of really useful effects and there's no working version anywhere online.
      Unfortunately it just draws a black screen. Not sure what's causing it. I'm guessing the camera or ortho matrix transforms are wrong, light directions, or maybe texture resources not being properly initialized.  I didnt change any of the variables though, only upgraded all types and functions DirectX3DVector3 to XMFLOAT3, and used DirectXTK for texture loading. If anyone is willing to take a look at what might be causing the black screen, maybe something pops out to you, let me know, thanks.
      https://github.com/mister51213/DX11Port_SoftShadows
       
      Also, for reference, here's tutorial #40 which has normal shadows but no blur, which I also ported, and it works perfectly.
      https://github.com/mister51213/DX11Port_ShadowMapping
       
    • By xhcao
      Is Direct3D 11 an api function like glMemoryBarrier in OpenGL? For example, if binds a texture to compute shader, compute shader writes some values to texture, then dispatchCompute, after that, read texture content to CPU side. I know, In OpenGL, we could call glMemoryBarrier before reading to assure that texture all content has been updated by compute shader.
      How to handle incoherent memory access in Direct3D 11? Thank you.
    • By _Engine_
      Atum engine is a newcomer in a row of game engines. Most game engines focus on render
      techniques in features list. The main task of Atum is to deliver the best toolset; that’s why,
      as I hope, Atum will be a good light weighted alternative to Unity for indie games. Atum already
      has fully workable editor that has an ability to play test edited scene. All system code has
      simple ideas behind them and focuses on easy to use functionality. That’s why code is minimized
      as much as possible.
      Currently the engine consists from:
      - Scene Editor with ability to play test edited scene;
      - Powerful system for binding properties into the editor;
      - Render system based on DX11 but created as multi API; so, adding support of another GAPI
        is planned;
      - Controls system based on aliases;
      - Font system based on stb_truetype.h;
      - Support of PhysX 3.0, there are samples in repo that use physics;
      - Network code which allows to create server/clinet; there is some code in repo which allows
        to create a simple network game
      I plan to use this engine in multiplayer game - so, I definitely will evolve the engine. Also
      I plan to add support for mobile devices. And of course, the main focus is to create a toolset
      that will ease games creation.
      Link to repo on source code is - https://github.com/ENgineE777/Atum
      Video of work process in track based editor can be at follow link: 
       
       

    • By mister345
      I made a spotlight that
      1. Projects 3d models onto a render target from each light POV to simulate shadows
      2. Cuts a circle out of the square of light that has been projected onto the render target
      as a result of the light frustum, then only lights up the pixels inside that circle 
      (except the shadowed parts of course), so you dont see the square edges of the projected frustum.
       
      After doing an if check to see if the dot product of light direction and light to vertex vector is greater than .95
      to get my initial cutoff, I then multiply the light intensity value inside the resulting circle by the same dot product value,
      which should range between .95 and 1.0.
       
      This should give the light inside that circle a falloff from 100% lit to 0% lit toward the edge of the circle. However,
      there is no falloff. It's just all equally lit inside the circle. Why on earth, I have no idea. If someone could take a gander
      and let me know, please help, thank you so much.
      float CalculateSpotLightIntensity(     float3 LightPos_VertexSpace,      float3 LightDirection_WS,      float3 SurfaceNormal_WS) {     //float3 lightToVertex = normalize(SurfacePosition - LightPos_VertexSpace);     float3 lightToVertex_WS = -LightPos_VertexSpace;          float dotProduct = saturate(dot(normalize(lightToVertex_WS), normalize(LightDirection_WS)));     // METALLIC EFFECT (deactivate for now)     float metalEffect = saturate(dot(SurfaceNormal_WS, normalize(LightPos_VertexSpace)));     if(dotProduct > .95 /*&& metalEffect > .55*/)     {         return saturate(dot(SurfaceNormal_WS, normalize(LightPos_VertexSpace)));         //return saturate(dot(SurfaceNormal_WS, normalize(LightPos_VertexSpace))) * dotProduct;         //return dotProduct;     }     else     {         return 0;     } } float4 LightPixelShader(PixelInputType input) : SV_TARGET {     float2 projectTexCoord;     float depthValue;     float lightDepthValue;     float4 textureColor;     // Set the bias value for fixing the floating point precision issues.     float bias = 0.001f;     // Set the default output color to the ambient light value for all pixels.     float4 lightColor = cb_ambientColor;     /////////////////// NORMAL MAPPING //////////////////     float4 bumpMap = shaderTextures[4].Sample(SampleType, input.tex);     // Expand the range of the normal value from (0, +1) to (-1, +1).     bumpMap = (bumpMap * 2.0f) - 1.0f;     // Change the COORDINATE BASIS of the normal into the space represented by basis vectors tangent, binormal, and normal!     float3 bumpNormal = normalize((bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal));     //////////////// LIGHT LOOP ////////////////     for(int i = 0; i < NUM_LIGHTS; ++i)     {     // Calculate the projected texture coordinates.     projectTexCoord.x =  input.vertex_ProjLightSpace[i].x / input.vertex_ProjLightSpace[i].w / 2.0f + 0.5f;     projectTexCoord.y = -input.vertex_ProjLightSpace[i].y / input.vertex_ProjLightSpace[i].w / 2.0f + 0.5f;     if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))     {         // Sample the shadow map depth value from the depth texture using the sampler at the projected texture coordinate location.         depthValue = shaderTextures[6 + i].Sample(SampleTypeClamp, projectTexCoord).r;         // Calculate the depth of the light.         lightDepthValue = input.vertex_ProjLightSpace[i].z / input.vertex_ProjLightSpace[i].w;         // Subtract the bias from the lightDepthValue.         lightDepthValue = lightDepthValue - bias;         float lightVisibility = shaderTextures[6 + i].SampleCmp(SampleTypeComp, projectTexCoord, lightDepthValue );         // Compare the depth of the shadow map value and the depth of the light to determine whether to shadow or to light this pixel.         // If the light is in front of the object then light the pixel, if not then shadow this pixel since an object (occluder) is casting a shadow on it.             if(lightDepthValue < depthValue)             {                 // Calculate the amount of light on this pixel.                 float lightIntensity = saturate(dot(bumpNormal, normalize(input.lightPos_LS[i])));                 if(lightIntensity > 0.0f)                 {                     // Determine the final diffuse color based on the diffuse color and the amount of light intensity.                     float spotLightIntensity = CalculateSpotLightIntensity(                         input.lightPos_LS[i], // NOTE - this is NOT NORMALIZED!!!                         cb_lights[i].lightDirection,                          bumpNormal/*input.normal*/);                     lightColor += cb_lights[i].diffuseColor*spotLightIntensity* .18f; // spotlight                     //lightColor += cb_lights[i].diffuseColor*lightIntensity* .2f; // square light                 }             }         }     }     // Saturate the final light color.     lightColor = saturate(lightColor);    // lightColor = saturate( CalculateNormalMapIntensity(input, lightColor, cb_lights[0].lightDirection));     // TEXTURE ANIMATION -  Sample pixel color from texture at this texture coordinate location.     input.tex.x += textureTranslation;     // BLENDING     float4 color1 = shaderTextures[0].Sample(SampleTypeWrap, input.tex);     float4 color2 = shaderTextures[1].Sample(SampleTypeWrap, input.tex);     float4 alphaValue = shaderTextures[3].Sample(SampleTypeWrap, input.tex);     textureColor = saturate((alphaValue * color1) + ((1.0f - alphaValue) * color2));     // Combine the light and texture color.     float4 finalColor = lightColor * textureColor;     /////// TRANSPARENCY /////////     //finalColor.a = 0.2f;     return finalColor; }  
      Light_vs.hlsl
      Light_ps.hlsl
  • Advertisement