How to get the Number of Drawn Triangles reliably?

I am desperately looking for a way to reliably get the average number of triangles drawn during a selected time window. When I use command line tools for that I get weird numbers. For example, for a scene with a single object that has 220 triangles, RHI Triangles Drawn gives an average of 1607.5 over a 10 second window. I think Unreal includes the number of triangles to render the text it prints on screen (the text on upper left in the image below) which appears when I use command Stat StartFile

I tried Unreal Insights but I couldn’t find a way to get an average count of triangles drawn over a period.

Any ideas to reliably get it?

any constructive feedback is much appreciated
ps. I hope self-upping like this is not illegal here)

What is the goal of this ? You could use a logger but if you have too many pouring in the log it may get your unreal editor unresponsive. Triangles are usually int, so you would need to log an int variable where the data is pouring in, or per hap an int TArray where the triangles reside. To get variables in an int var you would need a loop for the variable to change each time and unload the triangles inside of the int var from the source on each count, on each count the variable changes and you get an average per time when you stop. But it’s not easy to set up the loop in this case, you have to figure out how to make a loop that stores 3 ints since a triangle is made out of 3 ints , you would have to get 3 ints per one count and then mark those 3 ints as one triangle count. So it’s 1 X 3, you could also build a normal simplistic loop and count each int and 3 ints counted represents a triangle.

For example how you log an int variable :

UE_LOG(LogClass, Log, TEXT(“int value %d”), Variable_name_here);

As the loop go’s


 for (int triaglecount = 0; triaglecount < 300; triaglecount ++) 
 {
 int tris = triangle_array[trianglecount];
UE_LOG(LogClass, Log, TEXT("int value %d"), tris);
 }

This will count all the ints inside the tri array and log it for you each count, providing a number.

The loop can count 300 here for example, but you can use num() for your array instead of 300 that is just an example number. Each 3 numbers should be a triangle. It’s going to produce one number per count.

You could also design an elaborate loop to count 3 elements from the array with one count of the loop. The yourtrianglearray.Num(); will get all of it and when you hit escape it will stop before finishing countring and you will get an average per time.

ok thanks for the logging suggestion but how am I getting the number of drawn triangles by the engine in this scenario? Because the momentary count of drawn triangles by the engine is something quite different from the total triangle count of all the objects in the scene, after many rendering operations starting with frustum culling and occlusion culling and so on…

Yes, if you want to just read an area where you are looking then it’s more dificult, and get an instant reaction in some log. I have no idea how, I would have to read on things just to get somewhere for that, just to point the camera somewhere and read how many there are without counting the ones who have been removed (culled) Difficult task but it all starts with the logger, logging some variables.

yes but what I mean is one can’t simply count them through programming etc.
You need to use a number or a statistic reported by the engine itself or a tracer

You can, you just need to know how, to create a system for that that reacts instant, maybe a sphere trace but you would have to know how to set off things from the trace to count. Like in that instance fill some container and then unload the container as it counts in some loop and count the total amount inside the temporary container you setup as you iliterate thru it with a capture possition.

yes but the problem is I don’t know how and I don’t see anybody being able to tell how. Unless one can point me to some solid info on what to use, it is simply not possible for me to realize. Because my google searches or unreal documentation searched produced nothing

Ray traces can’t give accurate info about drawn triangles. Like I said before, it is a vastly different thing than simply counting existing triangles. It is even much more complex when Nanite comes into play. So one needs to rely on a number/stat reported by the engine

Because no one knows how, they would have to spend time figuring it out, this is not a usual question, if ray trace can’t give you that then you would have to find something else that can see triangles like the frustrum maybe, I don’t know. Unreal is greedy with that too as documentation go’s, the problems is tutorials for this kind of stuff are not around and you are going to spend like me a lot of time figuring things out that are of interest to you like I did, I spent lots of time to figure some things out, it’s really like a puzzle, you get some things from somewhere, some things from other places and then you know you can do this and that with whatever and next you have to use some other thing to complete what you have. So you have to first search what can see triangles when going over an area and then work out from there , how you can get and transfer the count from the method you found that sees triangles. Who knows may be some C++ , it may be some blue print. You are not going to find it today, and I have a bad feeling just like me , you asked and will get no answer. So you are off to google and ghithub searching.

well like I said I already did google, so I’m still keeping the hope alive that someone here will come along with some solid helpful info :joy:
or some idea why the RHI Drawn Triangles count reported by Unreal itself is not usable
and what is a better alternative among the many many stats reported by Unreal

Maybe you should try render to target and get the pixels off the camera somehow and work it out from there. You would have to look with the camera and record to render to target, then from there wire it to some system where it counts, it’s easy to set up render to target with a camera, with render to target you can read the actual pixels and maybe from there triangles. It’s just an idea since render to target can work live but it’s very slow. They do it all the time with the “Security Camera tutorial” so you can get first an idea how that works from one of those tutorials and then dig deeper how to read each pixel and store them else where. Each pixel is a vertex point in most cases. Can you imagine, read render target and create a texture, read the texture pixels and figure out how many triangles are there off the texture, need some calculation for each pixel. A potential heightmap can give off this many triangles from this many pixels.

Learn how to render to target to get some portion of the screen with camera. Learn how to store to texture, Learn to read the texture by counting the pixels in it and deducting how many triangles are per a sum of pixels. It’s hard stuff dude, even if anyone knows, they are not going to spill the beans here, and some allready have it figured out :joy: but are you know holding it. You could get the number of pixels for sure and then figure out how many triangles an area has, you have to learn how to automate all this stuff also I think after you figure out each thing.

This could be the first step.

Then find a way to save render to target to a texture, from there it’s just image processing getting the pixels, but render to target I hear can also do this directly without converting to texture by reading the pixels off of the render to target. Pixels for heightmap repersent 1 per 1 as in 1 pixel = 1 vertex point. 3 vertex points = 1 triangle. You can get I think pretty easy the amount of pixels per one area. I think looking with the camera is pretty good as the culled don;t show up into the render to target and what you see is what you get.

There is a way to do this live.

But it’s slow…

There is also a way to do it live on a texture that you save but it requires image processing code in an extensive manner.

In the end you can draw triangles if you have the pixels as vertex data, you have to build an instant heightmap system when you click some button after you view an area draw a mesh, the triangle system is automated at this point, it just counts the vertex points in a procedural way, alocates indices for it and then draws the triangles from the vertexes that were initial pixels.

If that is what you want then this could be one way of doing it.

are you sure about this? how do I count drawn triangles by examining pixels?

you count the pixels and then for each 3 pixels you have a triangle, if you have the total number of pixels in your camera captured inside the frustrum you know how many triangles you have. You have to run a loop on the pixels and count them as you store them to some container, the total number stored inside the container in this case will be the total number of pixels as ints that you have captured on the camera.

You can take it further and convert the pixels captured inside the camera view to vertex points and then draw triangles out of the vertices with indexes on them and then you will have triangles , but that invovles procedural work . You can also use all sort of stuff by blue print for your render target, draw to canvas, draw to texture, draw to many things.

What do you need this for anyway ? you want to do what with the counted triangles ?

Anyway it’s pretty simple at first.
…Get it on the render target

…From the render target you decide if you want to store to texture or read the pixels directly, there are nodes and stuff inside blue prints to do this from render target if you decide to read them directly.

…If you saved it to a texture you can get the pixels off of that and then fast have the pixel counted each time you press a button to call a function from blue print that is derived from your C++ class maybe.

That would be cool, point the camera somewhere at any angle and get total number of pixels displayed by a hood, on screen, each time you press the button after aiming the camera, then you can work your self from there if you have the pixies you can draw triangles from them, maybe there is some silly draw to canvas option for them to draw geometry from pixels in blue print.

I did this with texture from render target, each time I press a button I store a new texture from the render target, what ever render target has in it at current time.

umm well thanks but this is so wrong on so many levels starting with “for each 3 pixels you have a triangle”

It’s what it is, out of each vertex you create an indice index , then you draw on the indices , you need 3 indices to create a triangle, this is everywhere, unreal’s vector logic and procedural systems, opengl and so on. Those pixels are potential vertex points each, you can just read them after you get a texture and transcribe them from Fcolor that is an int variant to float values. Once you have floats you do vector logic with vectors etc and so on.

You are right that it is kind of wrong, but only because there may be a simpler solution, while this will have you digging how to create your own system and takes a long time.

I created render targets that instantly gives me textures saved from materials and the camera capture frustrum from capture 2d camera.

Created a system where I read the pixels, created a system where I draw triangles out of the pixels.

I would lie to say I created a system where texture fluctuates all the time is live and the pixels turn to triangles instantly, that would be a chalange but a good one.

I think it’s possible if the camera frustrum is small and only involves refreshing a small resolution like 100 x 100 to create a system where it instantly reacts and reads the pixels and then you have access to the pixel stream in C++ some how from the read pixel function that is in blue print that is attached to the render target.