Tuesday, August 27, 2013

Inconsistency naming convention

And another more technical post :] Today I had occasion to fix some functions in my rendering code. It was look somehow like this: 


struct GPUBuffer
{
    uint32 openGLBufferId;
    uint32 offset;
    uint32 size;
};

...

class CGraphicsManager
{
    ...
    /** @brief Create geometry buffer. */
    bool createBuffers( GPUBuffer* a_buffer,  uint32 a_sizeInBytes,  uint8* a_ptr,  bool a_dynamic);

    /** @brief Flush memory of geometry buffer. */
   void flushBuffer( const GPUBuffer& a_buffer,  uint32 a_offsetInBytes,  uint32 a_sizeInBytes, const uint8* a_ptr);
        
    /** @brief Destroy geometry buffer. */
    void destroyBuffers( GPUBuffer* a_buffer);
    ...
};



So nothing special but I seen here two inconsistency in code convention. First is that once I use pointers and in other occasions references I didn't thought a lot about this and changed everything on pointer. This was something natural to do for me but here came second problem : const .

Function flushBuffer don't change a_buffer in any way. It only use information that it contain to flush OpenGl constructions. So following most of articles and books that I remember, use of const is correct. But looking on purpose of this function it's somehow unnatural. Because even if a_buffer don't change inside function, object that is represented by it change. So in the end I removed const because I think that this way code is a more intuitive.

It's funny like such'a simple thing can force to so much thinking about code convention.

Monday, August 26, 2013

Multithreaded rendering

This time more technical but I think it's really interesting stuff for some of you :]

More than week ago I created new branch on my git repository containing code for "multithreaded rendering". Problem like this are never easy and in my case I needed to deal with OpenGL. And for this who don't know OpenGL is state machine and because of this isn't parallel friendly. All creation of resources, uploads of data, rendering and releasing of it need to be done on the same thread where context were created.

So you see a lot of restriction which complicate already not easy problem. Personaly I thought about solution of it from some time already but couldn't found any that would fit my needs. In most cases there were problems how nicely fit it into my architecture, how to deal with synchronization and few other stuff. But not so long ago I found at last way to deal with it and of course didn't started to code it

In last few years I learned that if we have any idea and we want to realize it, then there is need to give it some time. Start doing something else, talk with people that can give some tips or comments, search for weakness of solution and if we have time wait even week or two. If after everything this idea is still there there is big possibility that it's really worth work that we will put in it. In other way we would lost time reverting changes or dealing with incomplete creation that will need a lot of tweaks later .

So after month of thinking I decided to begin work. I split task from my main thread on few others and right now my threads look like that:
  • Main Thread :
    • Upload loaded data to GPU
    • Rendering using OpenGL api.
  • IO thread :
    • Process inputs from devices  (mouse, keyboard, pads, oculus rift).
  • Game Thread :
    • Physics update
    • Process game logic
    • Prepare scene to rendering
    • Loading data to memory (from i.e. disk)
  • Working threads
    • Execution of jobs
Everything proceed with replacing step by step elements of current pipeline with new one. I done it that way so I could check if any change don't brake anything. In meantime I needed to deal with some problems about which I though a lot ( i.e. how exchange information about what to render between threads ) and this which I didn't thought so much ( i.e. synchronizations between loading of data to OpenGL and using it in rendering ). 

Right now it's really hard to tell how big performance gain will be from this changes but I can say for sure that there is a lot of problems like crashes and bugs that I still need to deal with. But as John Carmack wrote on his twitter: 
"Many types of performance optimization screw up your codebase, but making functions pure for parallel execute actually improves it."
I can say that after all this changes my code starting to be a lot cleaner, stable and I could even say a lot easier to understand.

Till the next post.


Saturday, August 17, 2013

A little break

As title say a little brief what was happening in this last few weeks. First part was really nice I took two week of break for vacation and was traveling a lot. Thanks to that I meet interesting peoples and I believe that in future there will be a loot of occasion to meet again. Of course this was only begin because after that I started to finalizing some big changes that will happen for me in near future. I will write about them them at some other occasion.

After this short and boring part lets move on some more interesting topics: what changed  :] 

***

I started work on some few bigger elements. Not all of them were directly connected with game but I would call them "made for fun" i.e: 

LBA Remake on Oculus Rift
It was funny experience but in the same time it pushed my tech a little forward. Mostly because it need this specific way of rendering and also oculus is optional device of multiple purpose (it control parameters of 3D rendering and in the same time is some kind of controller). So multiple system needed to acquire data from it and because of that I needed to change a little bit architecture of engine. On end I get pretty clear and straightforward way of supporting such a devices. 

Of course on oculus rift my work didn't ended. I was doing a lot of cleanup in my old code. It wasn't easy to start this task but it was needed for future growth of project. So I done cleanup, moved everything on unified writing convention, added comments and descriptions how each of cleaned system work. Simple task but took me a lot of time.

When you do a lot of technical stuff you cannot forgot that you do game. Following this believe I added some new character movements. Some were nice and some were put away because I don't have any idea how to create them in the way they wouldn't look stupid i.e. animation after picking important item when twinsen waving his hand up and down. I will probably return to it on some point but if it won't look nice I will probably need to give up on implementing it.

On end I left standard element : bug tracking. In this occasion I done more gameplay test than normal and my list todo grown long in few hours. Some of points are already done but there is still a lot before me. 

Till the next post.