Monthly Archives: April 2013

Don’t trust fseek(), it lies!

When I press the Build button I Always hope to see a compilation without errors. Obviously this almost never happens, but yesterday it did! Too good to be true: when I ran the application OpenCL failed to compile the rendering kernel and I got errors at lines beyond the end of the file. Since I was sure that the error was not in my code I started blaming everyone: Intel for a buggy compiler, NVIDIA for his lack of support and Khronos for some other invented reason. Of course it wasn’t their fault, I just should have been a bit more careful when reading the .cl files. This was my code:

// Read the source code file
char* buffer;
FILE* file = fopen(fileName, "r");

fseek(file , 0 , SEEK_END);
long size = ftell(file);
buffer = (char*)malloc(size);
rewind(file);
fread(buffer, 1, size, file);

// Create OpenCL program
char* sources[] = { buffer };
size_t lengths[] = { size };
cl_program program = clCreateProgramWithSource(
     context, 1, (const char**)sources, lengths, &error);

It turned out that the problem was in the function fseek(). The C standard states:

A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.

This means that the code above doesn’t really get the real size of the file. In my case the value of size was sometimes bigger than the real file size, causing dirty memory to be included in the source code. To fix this problem I took advantage of a powerful library that I was already using for other tasks: boost.

boost::filesystem::ifstream fIn;
fIn.open(fileName, std::ios::in);

std::stringstream ss;
ss << fIn.rdbuf();
char* buffer = ss.str().c_str();

This solved the problem. Now the kernel compiles smoothly. I had an access violation exception in nvcompiler.dll when running the kernel on x64 but I fixed it recompiling everything for x86 and then for x64 again. Don’t ask me why does it work! I have no idea and the NVIDIA OpenCL compiler is well-known for his bugs. Anyway I was finally able to run a basic raytracing pipeline with OpenCL!

Advertisements

SYN: A way to explore the fractal universe

My first reaction when I see a beautiful image is to ask myself if and how can I do something similar. Fractals were no exception for me: I tried some existing softwares like Mandelbulb 3D and Ultrafractal, but none of them really satisfied me. I felt somehow limited, and as a programmer I prefer to build myself the tools I need. Fractals are a huge world and, to explore it deeply, a flexible software is needed. Speaking with the people of fractalforums.com I discovered that many artists are also waiting for more powerful tools, so, in collaboration with a friend who will write the GUI, I decided to start my own project: SYN.

SYN is an image synthesizer: a software that allows definition and real-time exploration of procedural shapes. It will also include a raytracer for the final rendering of images and animations. To ensure a high level of customization the rendering pipeline will be programmable at different stages:

  • Sampling stage: defines how rays are shot from a pixel. This allows effects like depth-of-field and motion blur.
  • Shape definition stage: the engine accepts scripts that defines either a distance estimator or a brute force intersection function.
  • Shading stage: defines the color of the light on an intersection.
  • Post processing stage: once the raytracing process finishes it’s possible to apply custom effects on the final image.

For those people who don’t like writing code, SYN will allow scripts to be loaded in a GUI where parameters can be modified with visual controls. In the image below you can see an early storyboarding draft showing how the user interface is going to look like.

SYN GUI draft

Since raytracing requires heavy calculations, SYN will use all available resources through OpenCL. The rendering process will be distributed over both CPU and GPU for real-time previews. For the final renderings the engine will be able to distribute rendering tasks over multiple computers.

In this post I tried to give a general idea about what is SYN going to be. We will spend the next weeks programming, I hope to be able to publish a demo soon. In the meantime feel free to give suggestions and to express your opinion about this project.

Mattia.

First words

I have always been impressed by people spending hours writing tutorials, articles, making videos, releasing free softwares just for nothing. I mean: why do they do that? It’s a job for which you don’t get paid! As soon as I got the idea of starting my own blog I understood that I was completely wrong: it’s not a job, it’s a passion! Once you find something you are really interested in, then helping, writing and sharing knowledges looks very natural and rewarding.

The name of the blog (Synthverse) is the combination of two words: universe and synthesization. The idea I want to represent with these words is that with the help of a computer it is possible to create (synthesize) infinite virtual worlds (a universe). Images of these worlds can show an incredible variety of details.

I will start this blog talking mostly about SYN: a 3D fractal rendering software I am developing with a friend. I will present it in a later post. Then I will also cover topics like fractals and procedural generation as well as rendering techniques.

In the first post I will present SYN.

Until next time,
Mattia.