Nov 16

C++: A singleton base class

C++: A singleton base class

Hi everyone, here is an example I used in a project of mine in order to generate singleton in a easier way.

The main goal was to remove much of the boiler plate from the class itself and try to wrap it inside a base class.

By googling around  I found many interesting approaches, and also on game programming gem 1 there is a nice template based  example, but the main problem with those approaches was that all of them were based on the assumption that your class need to be instantiated otherwise you return a null pointer, I don’t like that idea, I don’t want to clutter the code all over the place checking for a null pointer.

Of course since I am working on some sort of game engine I can just create all the singleton at boot up and be done with it, but I wish to reuse singleton in other project and I wanted a general way to implement that. The singleton I usually write just does a check if the pointer is null when you request it, if it is, it makes an instance and returns it. Pretty simple, so I tried to merge both workflows and here is the result.

#pragma once

template < typename T>
class Singleton
	static inline T* get_instance()
		if (!m_instance)
			m_instance = new T;
		return m_instance;

	static inline T& get_reference()
		if (!m_instance)
			m_instance = new T;
		return *m_instance;

	static inline bool exists()
		return m_instance != nullptr;

	static inline void clean_up()
		if (m_instance)
			delete m_instance;
                m_instance = nullptr;

	virtual ~Singleton(void)
		m_instance = nullptr;

	static T* m_instance;

template < typename T>
T* Singleton< T>::m_instance = nullptr;

How it works is pretty simple, we created a template class, passing the class we wish to be able to make a singleton, all this is just to be able to auto generate boilerplate code
making the singleton being a singleton.
So we declare a couple of methods returning the correct pointer type and reference ,also handling the creation of the class and the creation of the static pointer for our variable.
Finally we also initialize the static pointer to nullptr.

Fairly straight forward but how does it work? fairly simple here is an example:

#include <core/singleton.h>

class Constants : public Singleton<Constants>

 friend Singleton<Constants>;


     void set_path(const char * exec_path);
     void set_window(GLFWwindow * window);

     Constants(); //private constructor
     Constants( Constants const&)=delete; //private copy constructor
 Constants& operator=(Constants const &)=delete; //private assignemnt operator

Here is a part of a class I use to hold constants for my engine, the only thing you need to do is to inherit from Singleton passing in the current class type of the child class and you are done (make all constructors private as-well).
I know it twisted my brain a little to think what the compiler does, but in the end template resolution is not so different from a macro, if you think a little you will see everything unroll niceley.
There is one gotcha you need to be aware of, you might have noticed that we need a friend declaration to the base class. This is needed because in a proper singleton you should not be able to instantiate the singleton by its constructor, so should be private, but if it is private the base class cannot access it (trough the template resolution), this mean we need the friend declaration. I am willing to live with that, doesn’t look ugly and I think overall is an elegant solution to the problem.
Let me know what you think about it!


PS: Special thanks to Alan Stanzione for helping me figure out I was using friend declaration the other way around 😀

Sep 20

GAME-ENGINE: getting started

GAME-ENGINE: getting started

Hi there,
as learning project I decided to create a simple game engine. Let me explain, I am not crazy or mad, what I am trying to do is, hopefully, a subset of a game engine, starting as small as possible and then build on top of that.

The first goal I have is to have a 2D sprite jumping around the screen in a Mario style. Which is not that hard if you hardcode your way out of it putting everything in a rigid loop etc, doing it by trying to have a basic pipeline running is a hole another story. Here I will try to explain what I designed and trying to build. Most likely a game developer will see this and have an hart attack, but I will try to have not a uber terrible bunch of code.

Here is how I see a basic game loop happening:



Let me try to explain it.
We have the event loop which has a standard series of call. First we process the inputs, to know what has been pressed. Next step is to update the game state, right now I am only updating the position of the game objects.
The game object is assembled with components, one components is in charge to update it’s position, by swapping the component I can change the wanted behaviour.  For example a component can read the user input and update accordingly, another type of component might be controlled from AI or just replay older user inputs etc. The input class is also quite useful to abstract away the windowing system, in this way hopefully if  the windowing library needs to be changed wont be super painful. Last step would be resolving collisions but that part is being designed right now, probably I will write a post about it in the future.

Next is the rendering step, before this step (or even before world update) we can build a subset of entity to render, again there are different rendering components in charge of different type of rendering, more about it later.

Here a scheme more focused on rendering:

Rendering pipeline

This is an old picture of the design (pre components) but should still give an idea.
The renderer is nothing more then a container grouping together a glsl_program with the shaders, a camera pointer and a series of resources to render. You register to a renderer a resource you wish to render, this can be for example built on the fly after a camera frustum culling pass.

A resource is called GameEntity,  which again is noting more then a container of different other resources, in the latest iteration it is filled with components,  a move , collision and render component. Now the buffers and render data is encapsulated in the render components. Some resources are at entity level like the position or velocity, I will need to find the right balance between encapsulation and shared resources. All the resources should be allocated with custom allocators to avoid memory fragmentation and ensure memory locality.

This was a super quick run trough of the engine structure, surely will change a lot while working on it. It has been super exciting so far!

Here is what I got:



By no means this is a tutorial on how a game engine should be structured, this is just a report of a programmer trying to get into game development. For this reason comment or critique are more then welcome, anything that can make the “engine wanna be” better.


Feb 22

PC BUILD: let’s go cool!

Hi everyone so I am back with part two of my pc build! It s time to go water-cooling!


So as you might know from my previous post I built my first custom pc from scratch, but as my dad always told me:”If you do something, do it right!”.

This means I could not stop there, I had to push further and watercooling and overclocking was the next natural step.

Finally last weekend I received the second batch of stuff:


Right there we got the second gtx 960, green fans (you will see the result later) , water cooling kit, green colour for the fluid and LED strips.

The first think I did was put in the second card and start seeing as it performed. (PS if you get a flashing green screen in SLI mode, just swap the direction of the SLI bridge).

IMG_20150214_133135 IMG_20150214_133125


As soon as I got started  with the watercooling kit problems showed up,  the instruction manual  said there was the need for an oring between the pump and the reservoir, of course that o-ring was missing.

As you might understand being the first time I was building a watercooling kit I did not want to take any chances to flood all that precious hardware. I had to contact the retailer, the kit manufacturer and they all gave me everytime different answers, in the end I found out they upgraded they watercooling kit and that oring is now integrated in the pump and colored



With that out of the way let the fun begins!

IMG_20150219_213830 IMG_20150219_213819 IMG_20150219_081333



The process was fairly straight forward even though I was freaking scared of leaks! ( that’s also the reason why there is toilet paper everywhere).

I let the system run for quite a while (4~ hours running and a whole day standing). In the kit there was an handy plug to jump start the PSU so I could just run the pump without need of anything else running.

After all that was done time for final touches and that means light!

IMG_20150219_213437 IMG_20150219_194344 IMG_20150219_194337 IMG_20150219_194328


That was it for the build, it took me quite a bit of trial and error but it was an amazing experience and I learned so much!

Here you can see a video of the lighting effect:

PC BUILD: NV FURY final result from Marco Giordano on Vimeo.

Finally here just a slide shows of picture with music of the whole building process!

NV FURY: making of from Marco Giordano on Vimeo.

That’s it guys! I did not talk about temperatures yet because I want to keep that for next post where overclocking will come ! Trust me when I say that little pentium holds some serious horse power!

Feb 15

My first pc build!

My first pc build!


Hi everyone! Yes I finally decided to build my first pc!

The reason for that is simple, since I started programming I got interested more and more about how actually a pc works, that led me to follow many channel on youtube that talk about hardware etc, I love following tech news about new cpus  and gpus. When I started doing parallel computing on both CPU and GPU I could hold it no more, I had to do my own build.

This build was born with the goal of being an experiment, cheap material just to get some experience. I also wanted to be able to play with overclocking, that led me to more expensive components like the motherboard.

In the end I decided what the heck lets make a nice build that will stand nicely for a year or two meanwhile I wait for new pascal architecture to come out.

This is what I ended up with:

CPU : Intel Pentium G 3258 K Anniversary edition (modest processor dual core but its multiplier is unlocked, and this bad boy overclocks like a beast!)

MOBO: Gigabyte Z97X-SLI

RAM : 2×8 GB Corsair vengeance


PSU : Corsair AX 750W

It is funny you know, years ago a friend of mine, Alberto Vazzola (AKA Wolf), asked me: “Aren’t you interested at all in hardware?”, I answered him that I was a programmer, and I did not give a s**t about hardware, kindle he answered me : “wait for it…”. He was damm right.

Here is a nice pic of the initial hardware:

IMG_20150207_112022 IMG_20150207_114355

I have to say that 960 is a sexy piece of hardware come on, you cannot deny it!

So let s move forward, before putting it into the case I did the most basic build possible just to see if the system post (and as you will see it was a good choice).

Getting the system to post was a pain in the neck, not for hardware fault but for my newbie mistakes.

For example took me ages to see why the PSU and motherboard wouldn’t get started and the why I got no screen output at all. After a lot of head banging on the wall and the help of friends and dad, we figured out that

I did not connect a second plug of the main 24 pin connector:



The reason why I did not plug the second one (on PSU side) was because a little force was needed to plug both and I had to bend the cable at the base, that combined with the fear of breaking stuff (you always have that fear with new stuff) led me to think only one of the two was needed. After my mistake was fixed I could finally scream: “IT ALIVE!”.

IMG_20150208_174250After that I could finally put everything in the case:



I did not pay too much attention for cable management since this is just the beginning 😀 Stay tuned if you wanna know more!

Anyway so It took me almost a weekend to put everything together but I am quite satisfied because I learned a huge bunch of stuff!

So that is it for now guys!


PS huge thanks to Alberto Vazzole and Alessandro Boschian Pest and best of all my Dad for all the advice.

Jan 18

LINUX: Fixing screen position running NVidia drivers

LINUX: Fixing screen position running NVidia drivers

Hello folks!

So lately I have been playing a lot with CentOS, and looks like I finally am on a stable setup, where I can compile all my stuff, opengl, cuda and TBB etc.

The problem that bothered me for a while was the setup of the screens, I had swapped monitor, meaning I had to go to the right  side in order to go in the left monitor, and swapping the monitors was not a solution due to a comples setup running with a shared monitor with two computers etc.

I was able to fix that with the nvidia-settings panel, unluckily the fix was not permanent, I tried a shitload of guides in order to fix the  problem, everyone suggested to use this command at runtime:

nvidia-settings --load-confing-only

But that was completely ignored for some weird reason I don;t know, so I had to do that with a script, I tried to mess around with the nvidia-settings command but no luck there, luckily I remebered a command I used ages ago on Ubuntu 10 to fix this very same problem, the command is xrandr, after a little googling on how to use the command I come out with few lines of code to fix my problem:

xrandr --auto --output DVI-D-0 --right-of HDMI-0
xrandr --auto --output HDMI-0 --left-of DVI-D-0
xrandr --auto --output HDMI-0 --primary
echo ">>>> screens setupped properly ... I hope!"

The code first sets my second monitor on the right of my main and then the opposite, at the end I also set the HDMI monitor as primary.
Now you might wonder where I came out whit those names HDMI-0, DVI-D-0 etc. To get the names of your monitors is quite simple you just need to
run xrandr without arguments in the terminal and it will spit out the current plugged monitors.

<pre>$ xrandr
Screen 0: minimum 8 x 8, current 3840 x 1080, maximum 16384 x 16384
VGA-0 disconnected (normal left inverted right x axis y axis)
DVI-D-0 connected 1920x1080+1920+0 (normal left inverted right x axis y axis) 531mm x 298mm
 1920x1080 60.0*+
 1680x1050 60.0 
 1600x900 60.0 
 1280x1024 75.0 60.0 
 1280x800 59.8 
 1280x720 60.0 
 1024x768 75.0 60.0 
 800x600 75.0 60.3 
 640x480 75.0 59.9 
HDMI-0 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 477mm x 268mm
 1920x1080 60.0*+ 59.9 50.0 
 1680x1050 60.0 
 1600x900 60.0 
 1440x900 59.9 
 1280x1024 75.0 60.0 
 1280x800 59.8 
 1280x720 60.0 59.9 50.0 
 1152x864 75.0 
 1024x768 75.0 70.1 60.0 
 800x600 75.0 72.2 60.3 56.2 
 720x576 50.0 
 720x480 59.9 
 640x480 75.0 72.8 59.9</pre>

Once you know that you are golden and can make your script run.
Last two step where to run the script at start-up, and create and alias for the script since I have a complex setup with HDMI swithces etc which sometimes still
freaks out the monitors, so I run the script manually from terminal and all goes in the right place.

That’s it guys , happy Linux!

Jan 06

Having fun with cuda…. again

Having fun with cuda…. again















Hello there, today we are talking about cuda again, some time ago you saw me talking about pycuda in THIS post, here today I did something similar but with a more serious approach,

I decided to get better at c++,  parallel computation and Linux, this led me to start this learning project about image processing.

The first step was able of course to read an image and convert the data in something that can be easily processed both from cpu and gpu, also it gave me an excuse to learn how to read a binary file.

I decided to go with a BMP image format, because is one of the simplest there is around, with no compression and just simple header.

Now don’t get me wrong I am not trying to re-invent the wheel at all, I just made a simple bmp class to learn the process, the class itself is quite limited as well features wise, it just let me load and save the image or if needed generate an empty buffer then save it, as a reference I used THIS awesome article, be sure to have a look at it.

Once the BMP was out of the way I could finally start working on the image processing itself, first step is the most basic and simple black and withe filter, I first implemented the serial version then the TBB version and finally the cuda version, I am not sharing the code yet just because I don’t believe it is decent enough to be shown, but I will do that soon and you will see it popping up on timings


Let s start with the black and white, the first thing to notice is that we don’t have such big difference between cuda and TBB, such small computation was not so worth based on all the managing of the data that cuda needs ( allocating/copying memory etc ), we start get some more speed up when we start going up on heavier images. Another interesting thing was that if I was trying to run 8 thread with TBB (aka hyper-threading), I was actually getting lower performances that just running 4 threads, unluckily I don’t know much about hyperthreading to know why but I plan on looking a little into it.

Here is a carth of the running times:





Everything looks fairly standard, the increase in time is also quite linear, even though the gap between 4k and 5k is not that much, but we will see later that, that particular spot will hold some surprises for the blur aswelas well

Speaking of blur, we can see that since we have abit more computation in the blur we start getting some way better performances out of cuda, lets see the charts for the timings.





As you can see here we start to get some nice performance out of the GTX 780M, we get an ~76x speedup compared to the serial code and a ~21x speed up compared TBB on a 9k Image.

It is all good and nice, except the serial execution between 4k – 5k, you can see that the 5k is actually running faster then the 4k, don’t ask me because I have no clue about it, at the beginning I thought it

was an error, but I run the program other two times for both the 4k and 5k image the result were the same, the only guess might be some combination in memory that made the 4k particularly  slower for some reason.

Other then that I was quite pleased to see the GPU starting to crunch as it is supposed to, but I am not stopping here, the plan for the future are the following, first I want to implement a gaussian blur, with a radius parameter which will force me to implement a dynamic size stancil and implement a generic convolution kernel with that stancil, also I came across THIS page where they show the different effects of different stencils, like sharpening or edge detection etc. Once the generic kernel will be implemented it will be fairly easy to play with those.

In the road map there is also the implementation of some more complex filters, or better I hope to find some filters more computationally expensive but not necessarily   super complex math wise, I don’t want to dive too much yet into image processing , last step will be add a nice UI where the user will be able to load an image and display that using opengl and then stack on each other different filters and see the result (hopefully) realtime.

Once that is done I will be ready to jump back on my opengl sandbox and make it way more robust and start implementing some more cool stuff.

That is it guys , here below a small video demo of me running the program.

Parallel Image: simple image processing program from Marco Giordano on Vimeo.




Older posts «