mandelbrot.rs revision 572a5031a5d8602db0bec0b253428a034bd4dd59
1// Copyright (C) 2011 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "ip.rsh"
16
17uint32_t gMaxIteration = 500;
18uint32_t gDimX = 1024;
19uint32_t gDimY = 1024;
20
21float lowerBoundX = -2.f;
22float lowerBoundY = -2.f;
23float scaleFactor = 4.f;
24
25uchar4 __attribute__((kernel)) root(uint32_t x, uint32_t y) {
26  float2 p;
27  p.x = lowerBoundX + ((float)x / gDimX) * scaleFactor;
28  p.y = lowerBoundY + ((float)y / gDimY) * scaleFactor;
29
30  float2 t = 0;
31  float2 t2 = t * t;
32  int iter = 0;
33  while((t2.x + t2.y < 4.f) && (iter < gMaxIteration)) {
34    float xtemp = t2.x - t2.y + p.x;
35    t.y = 2 * t.x * t.y + p.y;
36    t.x = xtemp;
37    iter++;
38    t2 = t * t;
39  }
40
41  if(iter >= gMaxIteration) {
42    // write a non-transparent black pixel
43    return (uchar4){0, 0, 0, 0xff};
44  } else {
45    float mi3 = gMaxIteration / 3.f;
46    if (iter <= (gMaxIteration / 3))
47      return (uchar4){0xff * (iter / mi3), 0, 0, 0xff};
48    else if (iter <= (((gMaxIteration / 3) * 2)))
49      return (uchar4){0xff - (0xff * ((iter - mi3) / mi3)),
50                      (0xff * ((iter - mi3) / mi3)), 0, 0xff};
51    else
52      return (uchar4){0, 0xff - (0xff * ((iter - (mi3 * 2)) / mi3)),
53                      (0xff * ((iter - (mi3 * 2)) / mi3)), 0xff};
54  }
55}
56