OpenCL struct bug?

Whenever I pass my Ray struct as a pointer to be set by a function, I get a very long seemly random number half the time and the correct answer the other half.


typedef struct
{
    float4 dir;
    float4 pos;
}Ray;

float4 rayGetPoint(const Ray* ray,float t)
{
    return ray->pos+ray->dir*t;
}

kernel void radiance(global float* image)
{
    const float PI = 3.14159265359f;
    Ray r = {{0.f,0.f,1.f,0.f},{0.f,0.f,-10.f,0.f}};
    //float4 scale = {1.f,1.f,1.f,1.f};
    //float4 rot = quatAxisAngle(PI/2.f,(float4)(1.f,0.f,0.f,0.f));
    //float4 translate = {0.f,0.f,0.f,0.f};
    
    float4 color = rayGetPoint(&r,5.f);
    int i;
    for(i=0;i<4;)
    {
        image[i++] = color.x;
        image[i++] = color.y;
        image[i++] = color.z;
        image[i++] = color.w;
    }
}

At first I was getting something like -2843472074729924.0000 0.0000 0.0000 0.0000. Then, the program started outputting 0.000 0.000 -5.000 0.0000 without any changes to the code. Does anyone have an explanation for this?

First of all, you don’t need to pass Ray r by pointer. Non-kernel functions are inlined into kernel and so rayGetPoint will be.

Is it some simplified kernel? Why there is no get_global_id(…) function?


int i;
  for(i=0;i<4;)
    {
        image[i++] = color.x;
        image[i++] = color.y;
        image[i++] = color.z;
        image[i++] = color.w;
    }

I don’t get this. You make a loop i < 4, there is no “i++” in loop, then increase i four times, so it equals 4 and loop ends with one iteration. Loop is unnecessary. Also you can make global float4* image instead of global float* image and just write [i]image = color.