Moving from C++ to C -> extremely slow

I've been working on a path tracer project for the few days, and since I ultimately want it to run on the GPU using OpenCL, I wrote most of it in C. Earlier today I decided to move it entirely to C, just to be sure that the code I write will require minimum effort to port to OpenCL, and in doing so, the only part of the path tracer code I even touched was the random generator, going from this:

1
2
3
4
5
6
7
typedef struct _mt_state {
    static unsigned long mt[N]; /* the array for the state vector  */
    static int mti;
} mt_state;

unsigned long _mt_state::mt[N];
int _mt_state::mti;


to this

1
2
3
4
typedef struct _mt_state {
    unsigned long mt[N]; /* the array for the state vector  */
    int mti;
} mt_state;


After that I compiled with a C compiler without problems, but when I run my path tracer now, it is radically slower than it was with the C++ version. I know this isn't much info to go on, but does anyone know why this would happen? Again, the path tracer code has remained untouched except for the above adjustments.


Fafner
Last edited on
Why do you need to go to C at all? Why do you think C++ cannot interact with the GPU or OpenCL?

Your slowdown is probably because you changed static (global) variables into non-static ones and now have duplicates of the same data everywhere.
I wanted to do it so that when I move my rendering code over to an OpenCL kernel its all C. OpenCL still doesn't have native support for C++. I thought about the change of the static variables, but I only have one struct, passing pointers to it to other functions. Its not being passed by value anywhere, so there shouldn't be any copies of it, right?

EDIT: You were right, I had overlooked a couple of crucial functions to which the struct was being passed by value! It's all working like it did before now, cheers :)
Last edited on
Topic archived. No new replies allowed.