I just wrote a small time-measurement-class in C++11 to be able to measure the time spended in a loop. It is just a simple struct which will measure the time via RAII ( see Resource_acquisition_is_initialization ) . The time will be measured by using the std::clock() ( see std::clock-doc ) class. So this solution will work on all platforms which are supported by a c++11-compliant compiler like g++, clang or the Visual-Studio.

In Asset-Importer-Lib we got a lot of feature-requests to provide point-clouds somehow. Doing this will be a big step for our library because the intention when designing Assimp was to give the user an optimized scene with meshes ready for rendering. And we need triangles to get a valid scene definition. And all free vertices, which are not referenced by a triangle / face will be removed during the post-processing. So a point-cloud, free vertices which are not referenced by any triangle, will be not possible with our initial design.

My first step ( and the easiest way to go I guest ) is to be able to export point-cloud data as an ASCII-STL-file. So you can find this feature on our current master branch on github. to export this you have to follow these steps:

Define a scene containing some vertices without any face declarations.

Create your exporter class, use the new introduced boolean property property AI_CONFIG_EXPORT_POINT_CLOUDS and set it to true

When I started to work on my OSRE-Engine I was just curious how to answer the following question: how can I work with a separate render-thread without getting mad all day long. And what the fuck is a thread. So I decided to implement my own multi-threaded renderer some years ago. Special when you want to learn something like this you need some time to think about it. Unfortunately I was getting a father during this period of time ( 6 years ago ) as well. So it took quit a while to get a good understanding about it.

After 6 years I have the feeling that maybe someone else is interesting in my design as well so I decided to write a blogpost about it. Hopefully you can enjoy it:

Before C++11 threading was not a default feature of C/C++. So if you wanted to use it you needed to implement it for the runtime of your Operation-System. I started using the Win32-API, because my plan was to run a separate render-thread for my own RenderDevice based on DirectX9. You can find some kind of documentation about threading here .

When you wants to start aapplication it first starts its own process. A process is some kind of os-specific container for your application. When you are writing a simple Hello-World-app you compiler will generate an executable which contains an entry point for a new process ( in C/C++this entry-point will be called main() ). A process owns its own adress-space, simplified it encapsulates the context ( handles / memory / resources ) of your application. No other application is allowed to access any memory adress of your process. And: in a process you can start different threads. When starting a process there will be one thread running you Hello-World. A thread is a OS-specific way to run code. A process can start several threads. SO what is the reason to run more than one thread. There are a lot of reasons to run threads:

You have to run a time-consuming computation and you do not want to block you main thread because he is controlling your UI ( and you do not want to have a blocked UI )

You want to run several computations at the same time on different cores

You are running a server and you don’t want to get one client blocked by other clients, so each client handling is running in its own thread

In my ase my plan was to run one main thread, which will run the main engine code. And I wanted to spawn one render thread, which will encapsulating the rendering completely. THe draw calls will not be blocked by any other computation. I started a simple Win32-Thread and implemented a renderer based on D3D9 for it. This is simple: just take an example Triangle render-example and start it in a separate thread:

Looks simple, you have to use the Win32-API to spawn the function RenderFunc in a separate thread and your triangle will be rendered. Unfortunately we need some kind of way to communicate with this renderer during runtime:

I want to update the uniform buffers, because my triangle needs to move

I want to change the scene

You need a functionality to communicate with this thread: a concurrent-queue. When the thread will be spawned it looks for any stuff which was enqueued and handles it:

The main- and the render-thread are able to access the concurrent queue, If the main thread wants to add any updates he can enqueue the update data for the renderer. In the render-thread the thread will look before rendering the next frame if there are any updates for the scene he shall render. If this is the case this data will be dequeued from the concurrent-queue and the updateScene-call will be performed. Afterwards the rendering of the next frame will be performed.

This concepts works fine for long-runnning tasks like rendering a frame 60 times a second. But the design works only for a renderer at the moment. If you want to be able to run arbitrary stuff in a thread you need a way to install your own handlers. So I introduced an abstract interface called AbstractEventHandler. The data will be encapsulated in an event ( a small class / struct which contains the payload for the next handling ):

So a developer can install his own handler-code by deriving his own EventHandler-classed from AbstractEventHandler, the data to deal will be descriped by the event and the corresponding data is stored in EventData. If you want to stop the thread execution you can use the Condition, which is a way to use a flag in a threadsafe way.

The next step was to build an interface for working with threads on different API’s. I called it AbstractThread. It encapsulates the implementation details for running a thread:

The interface offers a way to define a priority for the thread execution

You can assign a name to your thread if your operation system is offering this feature ( when using Win32-API there is a way to assign a name to a dedicated thread which will be shows in your debugger )

There is a state-machine which shows the internal state of the thread. This will help a user to monitor the state itself for debugging during runtime.

To define a way how to execute a thread with its assigned EventHandler-Instance there is a class called SystemTask:

You can start a system-task by assigning it its thread instance and its event-handler. Of course you can stop it as well. And you can send events to your thread. The events will be enqueued in the thread-specific concurrent-queue.

So I resolved my targets:

I was able to run a deticated thread for dealing with my render device by implementing a render-specific eventhandler class

I can communicate to the thread by using a concurrent queue

I can start / stop the thread execution

I can use this concept to define other tasks by implementing different event-handlers

One nice side-effect: By defining only the event-based protocoll how to render a scene you can decouple the way how to implement your renderer. It is encapsulated by the render-thread-specific event-handler. The user will only see the events how to work with it.

Think about the following situation:

There is an interface / class interface / function in your application used by others which conains non-standard enums / types / whatever coming from an external API like OpenGL, DirectX, some kind of commercial API. The underlying code strongly uses this API. So using parts of this API was straight forward. Instead of having a strong encapsulation your API was spreaded all oer your codebase. Unfortunately the Product-Manager generates this requests: we need to exchange the underlying API because of whatever … Easy, just change this one API below your code and … wait a minute. There are all these symbols coming from the old API. WTF!

What is this all about?

At the moment I have to deal with legacy code during my day-job. And I got the idea that, maybe, some other people are interested how I solved all the different issues as well. So I decided to start a serie of blogposts about the different kind of challenges I faced and how I dealt with them.

Oh my fucking god, what a mess …

Maybe you know the following situation as well: your boss told you to take care of this suspicious project no one in the company wants to touch. When people hear about your new task they look like you told them that you gonne die a painful dead at the weekend. So WFT!
You try to find the repo and there is no. There is only a folder containing some binaries and a zip-file containing the source. Or there is a repo with only one version checked in some weeks ago. Or there are different named folders with version names in it containing the source. WFT, again!
You will try to open the project and you don’t have a clue how to do this. No obvious entry points how to start. WTF, WTF, WTF!
You want to build the project and you cannot find any makefiles at all. The you find the makefile but not documentation how to run the build. Or you are able to runf the build but the 3-party-libs are not there.
After a hard week you are going into your weekend. You think about quitting the job and try to get a shepherd in Australia. But stop! Next week we will start to get out of this mess.

Getting back control: try to build it

The first thing what I try to understand is to get an overview about the project. Often I saw often used peaces of code next to dead ends, commented code or just experiments, which are not in use any more. The makefile / build description shows you which parts are currently in use for the production code. In the companies I worked people are using one kind of build system in a bundle of projects. If there are still colleages left which dealted with this project you can ask them which how this worked. For instance if a GNU-make was in use you can grep ( see How to use grep on linux or How to use findstr on Windows ) to look for Makefile. Visual Studio project files are using the sln-extension. Grep for it and try to run the build.
Copy the build output and look for errors. Each build error provides information about the struture of your project:

Dependencies are missing:

The project is using the following dependencies

Try to find it and intregate is as well

The project is looking for different includes ( in c++ ), modules ( python ) or packackes ( Java / C# ) and the build cannot find them:

The project is using an environment variable to locate different packages

Identify the packages and install them

Modify your environment until it works

If this creates issues: try to run your build in verbose mode, this helps a lot to analyze what is going wrong

The source does not build, syntax errors all over the place:

Which version of the language was in use? Any extensions? For instance if you are using Cuda-Code in c++ you need to install a special compiler for that.

Try different compilers, some code which build fine with VS-2010 does not build when using g++ / clangs

The problem:

Think of the following situation: you want to import a model using Asset-Importer-Lib and store some values like the version of the current asset or the author / company. Or when you want to manage the models using modules for a much more efficient way for your SCM you need to store grouping information. How can you do that using the Asset-Importer-Lib?

The solution: The Metadata API:

Asset-Importer-Lib provides a meta-data API to offer a solution for these kind of use-cases. It is straight-forward to use:

One of my favorite tools to debug MFC-Applications was the Win32-call:

::OutputDebugString( "Test\n" );

You can use it to trace information withing your application during runtime without a log-file. You only have to run the tool DbgView to monitor these log-entries ( if you want to try it out you can download DbgView here. )

Because I am currently working with C# in my job I wanted to use this tool as well for WVF-applications. And of course there is a corresponding API-method in the .NET-framework called:

It is easy to use. Just insert your Trace-log-entries in your code. When you want to take a look into your application you can start DbgView to see, what is ongoing. Just make sure, that you have defined

I am currently working on C# wit generics. The base concept seems to be the same as in C++ ( which I really like honestly spoken ).
And I tried to use specialization. There was a class which needs to deal with special data types. And for some special cases you need a type-specific semantic, because bool cannot be added for example. So I tried to do something like this: