aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jogamp/opencl/demos/julia3d/rendering_kernel.cl
blob: 5082c51227f0464c45ced90469230a1cf961362a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
Copyright (c) 2009 David Bucciarelli (davibu@interfree.it)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#define GPU_KERNEL


#define BOUNDING_RADIUS_2 4.f
#define ESCAPE_THRESHOLD 1e1f
#define DELTA 1e-4f

typedef struct {
    float x, y, z; // position, also color (r,g,b)
} Vec;

typedef struct {
    Vec orig, target;
    Vec dir, x, y;
} Camera;

typedef struct {
    unsigned int width, height;
    int superSamplingSize;
    int actvateFastRendering;
    int enableShadow;

    unsigned int maxIterations;
    float epsilon;
    float mu[4];
    float light[3];
    Camera camera;
} RenderingConfig;


float4 QuatMult(const float4 q1, const float4 q2) {
    float4 r;

    // a1a2 - b1b2 - c1c2 - d1d2
    r.x = q1.x * q2.x - q1.y * q2.y - q1.z * q2.z - q1.w * q2.w;
    // a1b2 + b1a2 + c1d2 - d1c2
    r.y = q1.x * q2.y + q1.y * q2.x + q1.z * q2.w - q1.w * q2.z;
    // a1c2 - b1d2 + c1a2 + d1b2
    r.z = q1.x * q2.z - q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
    // a1d2 + b1c2 - c1b2 + d1a2
    r.w = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;

    return r;
}

float4 QuatSqr(const float4 q) {
    float4 r;

    r.x = q.x * q.x - q.y * q.y - q.z * q.z - q.w * q.w;
    r.y = 2.f * q.x * q.y;
    r.z = 2.f * q.x * q.z;
    r.w = 2.f * q.x * q.w;

    return r;
}

void IterateIntersect(float4 *q, float4 *qp, const float4 c, const uint maxIterations) {
    float4 q0 = *q;
    float4 qp0 = *qp;

    for (uint i = 0; i < maxIterations; ++i) {
        qp0 = 2.f * QuatMult(q0, qp0);
        q0 = QuatSqr(q0) + c;

        if (dot(q0, q0) > ESCAPE_THRESHOLD)
            break;
    }

    *q = q0;
    *qp = qp0;
}

float IntersectJulia(const float4 eyeRayOrig, const float4 eyeRayDir,
        const float4 c, const uint maxIterations, const float epsilon,
        float4 *hitPoint, uint *steps) {
    float dist;
    float4 r0 = eyeRayOrig;

    uint s = 0;
    do {
        float4 z = r0;
        float4 zp = (float4) (1.f, 0.f, 0.f, 0.f);

        IterateIntersect(&z, &zp, c, maxIterations);

        const float normZP = length(zp);

        // We are inside
        if (normZP == 0.f)
            break;

        const float normZ = length(z);
        dist = 0.5f * normZ * log(normZ) / normZP;

        r0 += eyeRayDir * dist;
        s++;
    } while ((dist > epsilon) && (dot(r0, r0) < BOUNDING_RADIUS_2));

    *hitPoint = r0;
    *steps = s;
    return dist;
}

#define WORLD_RADIUS 1000.f
#define WORLD_CENTER ((float4)(0.f, -WORLD_RADIUS - 2.f, 0.f, 0.f))

float IntersectFloorSphere(const float4 eyeRayOrig, const float4 eyeRayDir) {
    const float4 op = WORLD_CENTER - eyeRayOrig;
    const float b = dot(op, eyeRayDir);
    float det = b * b - dot(op, op) + WORLD_RADIUS * WORLD_RADIUS;

    if (det < 0.f)
        return -1.f;
    else
        det = sqrt(det);

    float t = b - det;
    if (t > 0.f)
        return t;
    else {
        // We are inside, avoid the hit
        return -1.f;
    }
}

float IntersectBoundingSphere(const float4 eyeRayOrig, const float4 eyeRayDir) {
    const float4 op = -eyeRayOrig;
    const float b = dot(op, eyeRayDir);
    float det = b * b - dot(op, op) + BOUNDING_RADIUS_2;

    if (det < 0.f)
        return -1.f;
    else
        det = sqrt(det);

    float t = b - det;
    if (t > 0.f)
        return t;
    else {
        t = b + det;

        if (t > 0.f) {
            // We are inside, start from the ray origin
            return 0.0f;
        } else
            return -1.f;
    }
}

float4 NormEstimate(const float4 p, const float4 c,
        const float delta, const uint maxIterations) {
    float4 N;
    float4 qP = p;
    float gradX, gradY, gradZ;

    float4 gx1 = qP - (float4) (DELTA, 0.f, 0.f, 0.f);
    float4 gx2 = qP + (float4) (DELTA, 0.f, 0.f, 0.f);
    float4 gy1 = qP - (float4) (0.f, DELTA, 0.f, 0.f);
    float4 gy2 = qP + (float4) (0.f, DELTA, 0.f, 0.f);
    float4 gz1 = qP - (float4) (0.f, 0.f, DELTA, 0.f);
    float4 gz2 = qP + (float4) (0.f, 0.f, DELTA, 0.f);

    for (uint i = 0; i < maxIterations; ++i) {
        gx1 = QuatSqr(gx1) + c;
        gx2 = QuatSqr(gx2) + c;
        gy1 = QuatSqr(gy1) + c;
        gy2 = QuatSqr(gy2) + c;
        gz1 = QuatSqr(gz1) + c;
        gz2 = QuatSqr(gz2) + c;
    }

    gradX = length(gx2) - length(gx1);
    gradY = length(gy2) - length(gy1);
    gradZ = length(gz2) - length(gz1);

    N = normalize((float4) (gradX, gradY, gradZ, 0.f));

    return N;
}

float4 Phong(const float4 light, const float4 eye, const float4 pt, const float4 N, const float4 diffuse) {

    const float4 ambient = (float4) (0.05f, 0.05f, 0.05f, 0.f);
    float4 L = normalize(light - pt);
    float NdotL = dot(N, L);
    if (NdotL < 0.f)
        return diffuse * ambient;

    const float specularExponent = 30.f;
    const float specularity = 0.65f;

    float4 E = normalize(eye - pt);
    float4 H = (L + E) * (float) 0.5f;

    return diffuse * NdotL +
           specularity * pow(dot(N, H), specularExponent) +
           diffuse * ambient;
}

kernel void JuliaGPU(   global float *pixels,
                        const global RenderingConfig *config,
                        int enableAccumulation,
                        float sampleX,
                        float sampleY ) {

    const int gid = get_global_id(0);
    unsigned width = config->width;
    unsigned height = config->height;

    const unsigned int x = gid % width;
    const int y = gid / width;

    // Check if we have to do something
    if (y >= height)
        return;

    const float epsilon = config->actvateFastRendering ? (config->epsilon * (1.f / 0.75f)) : config->epsilon;
    const uint maxIterations = max(1u, config->actvateFastRendering ? (config->maxIterations - 1) : config->maxIterations);

    const float4 mu = (float4)(config->mu[0], config->mu[1], config->mu[2], config->mu[3]);
    const float4 light = (float4) (config->light[0], config->light[1], config->light[2], 0.f);
    const global Camera *camera = &config->camera;

    //--------------------------------------------------------------------------
    // Calculate eye ray
    //--------------------------------------------------------------------------

    const float invWidth = 1.f / width;
    const float invHeight = 1.f / height;
    const float kcx = (x + sampleX) * invWidth - .5f;
    const float4 kcx4 = (float4) kcx;
    const float kcy = (y + sampleY) * invHeight - .5f;
    const float4 kcy4 = (float4) kcy;

    const float4 cameraX = (float4) (camera->x.x, camera->x.y, camera->x.z, 0.f);
    const float4 cameraY = (float4) (camera->y.x, camera->y.y, camera->y.z, 0.f);
    const float4 cameraDir = (float4) (camera->dir.x, camera->dir.y, camera->dir.z, 0.f);
    const float4 cameraOrig = (float4) (camera->orig.x, camera->orig.y, camera->orig.z, 0.f);

    const float4 eyeRayDir = normalize(cameraX * kcx4 + cameraY * kcy4 + cameraDir);
    const float4 eyeRayOrig = eyeRayDir * (float4) 0.1f + cameraOrig;

    //--------------------------------------------------------------------------
    // Check if we hit the bounding sphere
    //--------------------------------------------------------------------------

    float distSet = IntersectBoundingSphere(eyeRayOrig, eyeRayDir);
    float4 hitPoint;
    if (distSet >= 0.f) {
        //--------------------------------------------------------------------------
        // Find the intersection with the set
        //--------------------------------------------------------------------------

        uint steps;
        float4 rayOrig = eyeRayOrig + eyeRayDir * (float4) distSet;
        distSet = IntersectJulia(rayOrig, eyeRayDir, mu, maxIterations,
                epsilon, &hitPoint, &steps);
        if (distSet > epsilon)
            distSet = -1.f;
    }

    //--------------------------------------------------------------------------
    // Check if we hit the floor
    //--------------------------------------------------------------------------

    float distFloor = IntersectFloorSphere(eyeRayOrig, eyeRayDir);

    //--------------------------------------------------------------------------
    // Select the hit point
    //--------------------------------------------------------------------------

    int doShade = 0;
    int useAO = 1;
    float4 diffuse, n, color;
    if ((distSet < 0.f) && (distFloor < 0.f)) {
        // Sky hit
        color = (float4) (0.f, 0.1f, 0.3f, 0.f);
    } else if ((distSet >= 0.f) && ((distFloor < 0.f) || (distSet <= distFloor))) {
        // Set hit
        diffuse = (float4) (1.f, 0.35f, 0.15f, 0.f);
        n = NormEstimate(hitPoint, mu, distSet, maxIterations);
        doShade = 1;
    } else if ((distFloor >= 0.f) && ((distSet < 0.f) || (distFloor <= distSet))) {
        // Floor hit
        hitPoint = eyeRayOrig + eyeRayDir * (float4) distFloor;
        n = hitPoint - WORLD_CENTER;
        n = normalize(n);
        // The most important feature in a ray tracer: a checker texture !
        const int ix = (hitPoint.x > 0.f) ? hitPoint.x : (1.f - hitPoint.x);
        const int iz = (hitPoint.z > 0.f) ? hitPoint.z : (1.f - hitPoint.z);
        if ((ix + iz) % 2)
            diffuse = (float4) (0.75f, 0.75f, 0.75f, 0.f);
        else
            diffuse = (float4) (0.75f, 0.f, 0.f, 0.f);
        doShade = 1;
        useAO = 0;
    }

    //--------------------------------------------------------------------------
    // Select the shadow pass
    //--------------------------------------------------------------------------

    if (doShade) {
        float shadowFactor = 1.f;
        if (config->enableShadow) {
            float4 L = normalize(light - hitPoint);
            float4 rO = hitPoint + n * 1e-2f;
            float4 shadowHitPoint;

            // Check bounding sphere
            float shadowDistSet = IntersectBoundingSphere(rO, L);
            if (shadowDistSet >= 0.f) {
                uint steps;

                rO = rO + L * (float4) shadowDistSet;
                shadowDistSet = IntersectJulia(rO, L, mu, maxIterations, epsilon,
                        &shadowHitPoint, &steps);
                if (shadowDistSet < epsilon) {
                    if (useAO) {
                        // Use steps count to simulate ambient occlusion
                        shadowFactor = 0.6f - min(steps / 255.f, 0.5f);
                    } else
                        shadowFactor = 0.6f;
                }
            } else
                shadowDistSet = -1.f;
        }

        //--------------------------------------------------------------------------
        // Direct lighting of hit point
        //--------------------------------------------------------------------------

        color = Phong(light, eyeRayOrig, hitPoint, n, diffuse) * shadowFactor;
    }

    //--------------------------------------------------------------------------
    // Write pixel
    //--------------------------------------------------------------------------

    int offset = 3 * (x + y * width);
    color = clamp(color, (float4) (0.f, 0.f, 0.f, 0.f), (float4) (1.f, 1.f, 1.f, 0.f));
    if (enableAccumulation) {
        pixels[offset++] += color.s0;
        pixels[offset++] += color.s1;
        pixels[offset  ] += color.s2;
    } else {
        pixels[offset++] = color.s0;
        pixels[offset++] = color.s1;
        pixels[offset  ] = color.s2;
    }
}

kernel void multiply(global float *array, const int numElements, const float s) {
    const int gid = get_global_id(0);
    if (gid >= numElements)  {
        return;
    }
    array[gid] *= s;
}