Working on a parser the other day, I decided to use the new <regex> library in C++ 11.
It's awesome, once you get to know the regex sintax. To make development faster, I coded a couple functions. They are nothing fancy, but they speed up my workflow.

Well, after a lot of fighting, I managed to get the volumetric render working. I'm using a 128*128*128 3D texture, and then I raymarch it in a compute shader.
The results are quite good actually. I'm getting around 140 - 170 FPS with 40.000 particles (!).
There are things to fix though. Mainly, the voxel appears to be displaced from the real center, and the rendering against the scene ( can't do Z-Buffering just like that ) leaves a lot of room for improvement.
Next things to do, fix that, add proper lighting, and then I'll pass to one idea of mine, which involves hash-maps and voxel compression...

Some screens:




So, the other day I decided I wanted to try to add particles to my engine. I already have PhysX ( 3.3 beta ) working, so I grabbed the documentation and started from there. The documentation includes a guide that it's really complete, so it just took me a couple hours to get it working.
For rendering I'm using the most naive implementation possible : draw a cube for each particle, with one batch per particle. Really slow, but still, I manage to get 80 fps with a GTX 480 and a i3 @ 3.65 GHz in a simple scene (5000 triangles + 36*4000 of the particles)  with 4000 particles, simulated in the GPU, courtesy of nVIDIA.




Next stop, volumetric rendering using voxels.
Another of the great things added in C++11 was the random library. It provides a bunch of facilities to create random numbers, way better than the C rand().
As it happens a lot of time, its use is cumbersome, compared to the good old rand(). As I use a lot of random numbers in my code, I came up with a couple templates that make life easier, and which I'm sharing today:
As a side note, the min and max limit are inclusive. 

Random integer from 1 to 9:
randNatural<int>(1,9);

Random double from 0 to 1:
randReal<double>(0,1);

Here I present what I like to call a "Dynamic Struct".
The idea is to have a struct that you can define at run-time, and get the elements by a string.
What's the use, you might ask? Well, my reason to do this is to have a way to define a constant buffer in a text file, load and update it in the C++ side, and pass it to the GPU.
So, with that in mind, here's a partial implementation( partial in that it has just a couple types, but it's easy to extend ), and how to use it.

Code:


And to use it:


Or:


It should be noted that I'm using this in production code, with just a few changes, like clean-ups, proper ctors, etc.
Speed-wise, it shouldn't be that bad. I use a std::map instead of a std::hash_map, as constant buffer and structures in general tend to have few items, and a hash map suffers in that conditions. Also is O(1) in the creation of each element, so you can add all the elements TYPES you want( C++ 11 and lambda functions make it possible to throw the old switch() case away in a clearer and might even faster way. Functional programming to the rescue!)

PS: uint is just a typedef for unsigned int. Also XMFLOAT4X4 and XMFLOAT3 are from the DirectX Math Library ( DirectX 11).
Branches, while faster every day, can be slow in some systems, especially in GPUs. By slow I mean slower than some arithmetic operations. If the branch is operating with numeric values, and assuming that boolean operations are convertible to 0 and 1, then it can be written as:
            a * Cond + b * (1-Cond) .
With a,b numbers and Cond a 1 or 0 representing true or false

I implemented two C++ versions of it, one that takes a boolean, and other that takes a function(can be a lambda, praised be C++11!)

Also, in HLSL or similar it can be
Take into account that that implementation is intended to mimic the logic of the C ternary operator : (cond)? a : b
The speed gain is really system-dependent, I have seen gains from 2 % to over 60 %, so see for yourself!

Pages

Powered by Blogger.