Is it possible to reduce the CPU consumption in irrlicht?
Is it possible to reduce the CPU consumption in irrlicht?
It seems to me that irrlicht is very powerful and fast, but I find it difficult that it uses 100% of the CPU all the time, giving it a Sleep(1) would seem to be a solution since the results seem to imply that the consumption is lower, but it is not so, if you measure it in even shorter times the consumption is not reduced in each iteration, What happens is that there are programs that simply take an average in an estimated time, but if you calculate it without considering the time, but the consumption in each iteration independent of other processes, it seems to consume as much as possible at that time.
How could this consumption be reduced?
How could this consumption be reduced?
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
The problem isn't so much Irrlicht, but that games basically tend to be an endless loop. Meaning it's your code - not Irrlicht. Although Irrlicht examples obviously have the same problem. Endless loops keep on using up one CPU (or core).
Not quite sure what you mean that sleep doesn't reduce it. That's pretty much the very thing it does. Thought obviously it's not reducing it per loop, but you will run less loops in the same time. Thought it comes with it's own downsides (like restricting yourself to 65 FPS on Windows unless you increase the timer resolution). I've talked about this already in other threads recently (viewtopic.php?p=308256 ... and you are mentioning vsync there yourself, so I guess I didn't tell you anything new below ...)
There are other options. One of the easiest ones is to enable VSync. If your FPS is way beyond screen refresh rate, then that basically restricts it to that. Irrlicht examples on my PC will go below 1% if I do that. To try that you can modify an Irrlicht example like this (replacing the usual createDevice):
Not quite sure what you mean that sleep doesn't reduce it. That's pretty much the very thing it does. Thought obviously it's not reducing it per loop, but you will run less loops in the same time. Thought it comes with it's own downsides (like restricting yourself to 65 FPS on Windows unless you increase the timer resolution). I've talked about this already in other threads recently (viewtopic.php?p=308256 ... and you are mentioning vsync there yourself, so I guess I didn't tell you anything new below ...)
There are other options. One of the easiest ones is to enable VSync. If your FPS is way beyond screen refresh rate, then that basically restricts it to that. Irrlicht examples on my PC will go below 1% if I do that. To try that you can modify an Irrlicht example like this (replacing the usual createDevice):
Code: Select all
irr::SIrrlichtCreationParameters params;
params.DriverType=driverType;
params.WindowSize=core::dimension2d<u32>(640, 480);
params.Vsync = true; // the important line
IrrlichtDevice* device = createDeviceEx(params);
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Is it possible to reduce the CPU consumption in irrlicht?
A few weeks ago I found that Sleep does not reduce the CPU load as such so to speak, I mean it does if you get to analyze the CPU consumption in a short period, but not so short, I mean, directly in Sleep the CPU consumption is almost almost 0.0, but what I mean is that the consumption once Sleep leaves its state, is exactly the same as without Sleep in that moment, however, these iterations are difficult to measure because they are very fast, for example, if you limit it to 60 FPS, equally each FPS rendered does it in a period difficult to measure.
I will make it simpler, you have 1000 iterations per second, and 60 of them are the frames, the % of consumption will not be exact if it is measured with the CPU consumption of the other iterations, for example, of the operating system, the minimum interval to be 16 ms what you will obtain is an average of consumption in those 16 ms, it can that the rendered frame has been to half of those 16 ms consuming 90%, you will obtain an average and the result will be that it consumed 25% or 50% for example, now well, if it is separated by a single process, you will obtain a consumption similar like this:
10%
4%
90%
4%
12%
90%
And an average will be obtained, since when rendering a frame, not all the time is consuming 90% of the CPU, for example, 4% may have been consumed when doing a minor calculation that does not consume much, but all this will be taken and an average will be obtained.
There are ways to obtain the consumption of each iteration consumed in the CPU, not by time, but by recording the actual consumption of a task.
As I say, if you combine everything in a short time it seems to reduce, although Sleep is useful to reduce the total consumption, what I am looking for is not this, I want to reduce the individual consumption of an iteration, for example, the operating system consumes GPU constantly to render the graphical interface, however the CPU consumption is minimal, what I want to know is what consumes so much in irrlicht even in an empty application? I mean, isn't there a way to reduce the CPU consumption by editing a section of the engine? I would like it to consume a maximum of 5% or 10%, I just want it for a 2d game where the FPS at most reach 4 per second (a visual novel).
Mmm... irrlicht is a powerful engine, but I think it is not possible to reduce CPU consumption in a traditional way, if that is the case it doesn't matter, but I still wanted to know
I will make it simpler, you have 1000 iterations per second, and 60 of them are the frames, the % of consumption will not be exact if it is measured with the CPU consumption of the other iterations, for example, of the operating system, the minimum interval to be 16 ms what you will obtain is an average of consumption in those 16 ms, it can that the rendered frame has been to half of those 16 ms consuming 90%, you will obtain an average and the result will be that it consumed 25% or 50% for example, now well, if it is separated by a single process, you will obtain a consumption similar like this:
10%
4%
90%
4%
12%
90%
And an average will be obtained, since when rendering a frame, not all the time is consuming 90% of the CPU, for example, 4% may have been consumed when doing a minor calculation that does not consume much, but all this will be taken and an average will be obtained.
There are ways to obtain the consumption of each iteration consumed in the CPU, not by time, but by recording the actual consumption of a task.
As I say, if you combine everything in a short time it seems to reduce, although Sleep is useful to reduce the total consumption, what I am looking for is not this, I want to reduce the individual consumption of an iteration, for example, the operating system consumes GPU constantly to render the graphical interface, however the CPU consumption is minimal, what I want to know is what consumes so much in irrlicht even in an empty application? I mean, isn't there a way to reduce the CPU consumption by editing a section of the engine? I would like it to consume a maximum of 5% or 10%, I just want it for a 2d game where the FPS at most reach 4 per second (a visual novel).
Mmm... irrlicht is a powerful engine, but I think it is not possible to reduce CPU consumption in a traditional way, if that is the case it doesn't matter, but I still wanted to know
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
Sorry, I simply don't get it. There is no simple or complicated calculation. As long as the CPU has work it works. So this code will use up one processor core completely:
And that is basically a single threaded game loop (without the game). It doesn't matter how much stuff you add inside this loop. The processor will not work more or less.
You can optimize Irrlicht - so it can do more stuff in one frame. And then you end up with more frames (aka the loop runs more often). But as long as you have no sleep in your game loop you still end up basically with an infinite while loop and the processor is used up.
So you need either sleep or yield or use a similar function where your processor gives up processing time or the process will use up one core. VSync is another one for example. Or WaitForSingleObject can also do that (in some cases I think). In other Windows applications which don't need to update frames constantly you will have a GetMessage which will do that. There might be more (not sure, not a topic I investigated enough so far).
Anyway - unless you give the CPU once in a while your game (independent of the engine) will use up the CPU because all games have this game loop they need to render. The only thing where a game engine can help is do less - so you can sleep longer (or whatever you use to give up cpu for some time). But you still have to ensure yourself you give up the CPU.
Check that link for a bit more info (warning, going beyond sleep and yield is a bit complicated): https://randomascii.wordpress.com/2012/ ... f-idleness
Code: Select all
while(1);
You can optimize Irrlicht - so it can do more stuff in one frame. And then you end up with more frames (aka the loop runs more often). But as long as you have no sleep in your game loop you still end up basically with an infinite while loop and the processor is used up.
So you need either sleep or yield or use a similar function where your processor gives up processing time or the process will use up one core. VSync is another one for example. Or WaitForSingleObject can also do that (in some cases I think). In other Windows applications which don't need to update frames constantly you will have a GetMessage which will do that. There might be more (not sure, not a topic I investigated enough so far).
Anyway - unless you give the CPU once in a while your game (independent of the engine) will use up the CPU because all games have this game loop they need to render. The only thing where a game engine can help is do less - so you can sleep longer (or whatever you use to give up cpu for some time). But you still have to ensure yourself you give up the CPU.
Check that link for a bit more info (warning, going beyond sleep and yield is a bit complicated): https://randomascii.wordpress.com/2012/ ... f-idleness
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Is it possible to reduce the CPU consumption in irrlicht?
Wow, thank you dear mr CuteAlien, you simplified the cause of the problem very well, well, now I understand the situation better, certainly Irrlicht is not the culprit, mmm.... now I will need to do a CPU consumption test without while and with while, maybe the result will be the same or maybe not, thank you again



**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
Investigating a little, I think that the high CPU problem is more prone in older computers, this is called "branch mispredictions" and depending on how the CPU works, it can consume a lot of CPU, this can occur in a WHILE for example, although it is executed only once without any internal task running, at the time it is executed it uses 90% or more, interesting, however the internal tasks within the while consume less, 5% at most.
Mmm... I'll test using goto, maybe it's more effective... or I don't know, but it's interesting, new computers seem to suffer less from this as they have better prediction implementations.
Mmm... I'll test using goto, maybe it's more effective... or I don't know, but it's interesting, new computers seem to suffer less from this as they have better prediction implementations.
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
Note: What I wrote was a bit of a simplification. The OS has for example also a lot of influence how much CPU a process gets. Irrlicht also has driver calls (which the simple loop doesn't have) so the graphic card driver might for example cause another core to get involved. And I'm a bit out of my field when it comes to multi-core stuff. On very old PC's with single core you simple got 100%. These days I get maybe 8% or so in the taskmanager display (which I assume depends on the amount of cores involved).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Is it possible to reduce the CPU consumption in irrlicht?
it is very strange, the while in a windows application does not overload my CPU, maybe it is more predictable for the CPU, well so, I changed the way of programming irrlicht, try in irrlicht 1.3 to make a quick test (just open devc++ and write the code), and certainly the CPU consumption now stays at 0%, incredible, in the traditional way it uses about 50% of the CPU core, now it stays at 0%, the difference is too much:
Although now I have another event even stranger than the previous one, it stays at 0% usage but this is when it creates two windows, the windows and irrlicht own, when using only one the consumption goes up again, damn because it is so complicated to keep a low consumption hahaha, for example if I uncomment this section the consumption goes up to 25%:
But it stays at 25% only if I have the window active, when I switch windows the consumption is reduced to 0-3%(still higher than with two windows most of the time), however it still runs the same amount of frames, I think it has to do with that the operating system is determining how much CPU it can use.... mmm.... it is very strange, I don't know how to get a valid solution, I like that it consumes little and maintains the same performance, but now I don't know how to make it only 1 window...
Thank you mr CuteAlien as well

Code: Select all
#include <irrlicht.h>
#include <windows.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
#define TIMER_ID 1
#define TIMER_INTERVAL 4 // Approximately 250 FPS
IrrlichtDevice* device = NULL;
IVideoDriver* driver = NULL;
ISceneManager* smgr = NULL;
IGUIEnvironment* guienv = NULL;
IAnimatedMeshSceneNode* node = NULL;
// Windows event callback
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
{
PostQuitMessage(0);
return 0;
}
break;
case WM_TIMER:
if (device && device->run())
{
driver->beginScene(true, true, SColor(255, 100, 101, 140));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
else
{
PostQuitMessage(0);
}
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSA wc = { 0 }; // Use `WNDCLASSA` for ANSI strings
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = "IrrlichtWinClass";
RegisterClassA(&wc);
HWND hWnd = CreateWindowA("IrrlichtWinClass", "Irrlicht Engine - Event Based",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
256, 256, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Irrlicht setup
SIrrlichtCreationParameters params;
params.DriverType = EDT_SOFTWARE;
params.WindowSize = dimension2d<s32>(256, 256);
params.Bits = 16;
params.Fullscreen = false;
params.Stencilbuffer = false;
params.Vsync = false;
params.AntiAlias = false;
// params.WindowId = reinterpret_cast<s32>(hWnd); // DO NOT USE IN 64-BIT SYSTEMS
device = createDeviceEx(params);
if (!device) return -1;
driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
rect<int>(10, 10, 400, 22), true);
IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
node = smgr->addAnimatedMeshSceneNode(mesh);
if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setFrameLoop(0, 310);
node->setMaterialTexture(0, driver->getTexture("../../media/sydney.bmp"));
}
smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0));
SetTimer(hWnd, TIMER_ID, TIMER_INTERVAL, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(hWnd, TIMER_ID);
device->drop();
return msg.wParam;
}
Code: Select all
params.WindowId = reinterpret_cast<s32>(hWnd);
Thank you mr CuteAlien as well


Last edited by Noiecity on Wed Mar 19, 2025 5:48 pm, edited 1 time in total.
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
Okay I tried in irrlicht 1.9 and I get exactly the same wtf, although to compile the code I have to Project -> Properties -> Configuration Properties -> Configuration Properties -> Linker -> System -> change Console to Windows, and to run a single window I have to params.WindowId = hWnd;


This is as close as I can get for low CPU consumption, now I don't know how to remove the second window without increasing the consumption damn it.
I plan to render at 256x256 and then scale the resulting image to a higher resolution if necessary, but still render at 256x256, thus keeping CPU consumption low with a touch of pixel art so to speak.
By the way, in irrlicht 1.9 it sometimes consumes 2% instead of 0%, while in 1.3 it stays at 0% no matter what, I have yet to analyze that information (it may simply be rendering much less fps).


This is as close as I can get for low CPU consumption, now I don't know how to remove the second window without increasing the consumption damn it.
I plan to render at 256x256 and then scale the resulting image to a higher resolution if necessary, but still render at 256x256, thus keeping CPU consumption low with a touch of pixel art so to speak.
By the way, in irrlicht 1.9 it sometimes consumes 2% instead of 0%, while in 1.3 it stays at 0% no matter what, I have yet to analyze that information (it may simply be rendering much less fps).
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
Re: Is it possible to reduce the CPU consumption in irrlicht?
That's the reason. The code you posted uses GetMessage + OnTimer to replace the endless game loop. If you are fine with max 65fps that is one solution (the "Approximately 250 FPS" comment is lying - the timer has the same restrictions as sleep).CuteAlien wrote: Wed Mar 19, 2025 12:06 pmIn other Windows applications which don't need to update frames constantly you will have a GetMessage which will do that.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Is it possible to reduce the CPU consumption in irrlicht?
About your other comments - I can't reproduce this on my system. Using one or two windows makes no speed difference here. Also using sleep instead of your GetMessage+OnTimer gives me the same speed.
As you use software driver in your tests the window size makes a big difference (this matters for software driver, but not for OpenGL). So with your tiny window I get around 1% in all software cases - increasing window sizes slows it down. With OpenGL I stay below 1%.
Note I might have different behavior on an older PC, but too lazy right now to test (every time I boot my old PC Windows first spends an hour updating it ...)
As you use software driver in your tests the window size makes a big difference (this matters for software driver, but not for OpenGL). So with your tiny window I get around 1% in all software cases - increasing window sizes slows it down. With OpenGL I stay below 1%.
Note I might have different behavior on an older PC, but too lazy right now to test (every time I boot my old PC Windows first spends an hour updating it ...)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Is it possible to reduce the CPU consumption in irrlicht?
Yes, it's very strange this event, I want to use software renderer in a small resolution so, I just intend to reduce the CPU consumption while keeping a low framerate, 8 fps per second or something like that, I like to play with irrlicht anyway. Mmm... actually I wanted to test some things, I think software renderer has problems rendering 3d models only if they are not closed, like a plane, while a cube renders well, probably because it's closed, mmm.... I'm also too lazy to confirm this, but I'll do it, I still want to confirm if shadow volume works.CuteAlien wrote:...
Burning Video is great, but it consumes me much more than software renderer.
I'm thinking about prerendering everything except for the shadows that is more difficult to precalculate in so many conditions, use a mask or something similar to get only the shadows that irrlicht processes in a low poly model, use a bmp image and make transparent the white color for example, so I keepl the black color in a 2d texture and put it on top of another image and I would get it...
mmm... I've already explained too much, it's fun to play with irrlicht, that's all


I was doing a tutorial on how to texurize a model and get hyper realistic results in irrlicht, I saved the beginning but I think it will be too long to read... however they are interesting techniques to play with this...maybe people prefer PS1 graphics instead of realistic ones, then maybe it is something useless to learn.
And... I'm still finishing the models to upload them to the forum someday... sorry, I'm too much of a perfectionist, so to speak, and at the same time, lazy
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

**