Sunday, April 24, 2016

Plans, real life and monkey job.

Funny thing about planning is that sometimes its not working out the way you want. This was case this week I planned to work on animations and ended doing small upgrade for resource system. The best thing is that I don't even feel bad about stuff ending this way :D 

How I ended doing that is other story. Because I have policy of moving everything what I can to resource system which I have (this make stuff more consistent) I moved my rendering script to it. Everything would be good if not the fact that render script is initialized on main thread and my resource system never was prepared for request from other thread than game thread. So it was time to do improvement to allow for this :D

This was even more important because there were sometimes crashes at startup of game. I split my task on 3 sub task:

  1. Access to resource data require locking of resource (i.e. meshRes->lock()->getMaterial();)
  2. Separation of resource from data so you could have resource handle and it was still loading 
  3. Moving prepared stuff on multithreading.
First two task were robust :/ required iterating over all my resource types and changing it's interface. This was boring like hell but well programming is not only about interesting stuff.

My mind was blank when I was doing this so I had time to think and came to conclusion:
Some people saying that programming is about creating stuff. But reality is different. We as programmer spend most of our time improving existing stuff, changing them, fixing and redesigning. When we do all of this we make small piece of code which do something new and we once again starting whole thing again. This is also true in personal projects.
Few hour later changes were ready and I could start preparing functions which worked on one thread but were already separated in a way part of it could be moved to other thread. This was more interesting task which resulted in changes for some of resources. Generally my resource system look like this:

class ResourceXYZ
{
     ResourceId  m_id;
     DataType*  m_privateData;
};

Till now private data was part which control whole resource but now in some cases I want to hold some data in resource so even when private data change it will still be the same. I.e. LevelID (Yes level is also resource).

Another hour later I was ready. I know that my game worked because I were changes step by step with running game to be sure it at least work. Next step was mutli-threaded code which went smoothly.

Thanks to lock system I would add simple wait for loading of resource if it was still loading. There were small issue with dependencies of compiling GPU programs (they expected other resources to be already loaded) but well thanks to my split on soft and hard references fixing it was also easy.

And so my code working now and my previous 700 ms loading on debug configuration resulted in 600 ms loading now :) There are still few things I want to make:

  • File asynchronous loading with forward loading for better performance.
  • Reloading of resources
  • Cleanup iteration over whole system
But well you cannot have everything in life but some day I will make this all work :) For now I will return to animation topic to push everything forward :)

Greg

No comments:

Post a Comment