-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCppAMPFractalRenderer.h
165 lines (140 loc) · 4.78 KB
/
CppAMPFractalRenderer.h
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
#ifndef CPPAMP_FRACTAL_RENDERER_H
#define CPPAMP_FRACTAL_RENDERER_H
#include <vector>
#include <amp.h>
#include <amp_math.h>
#include "FractalRenderer.h"
class CppAMPFractalRenderer : public IFractalRenderer
{
public:
CppAMPFractalRenderer(int width, int height)
{
}
virtual void ResetBuffer(int width, int height)
{
}
virtual void RenderMandelbrot(
int *buffer, int width, int height, int maxIteration,
TFloat _minX, TFloat _maxX, TFloat _minY, TFloat _maxY) override
{
using namespace concurrency;
using namespace concurrency::fast_math;
float minX = (float)_minX, maxX = (float)_maxX, minY = (float)_minY, maxY = (float)_maxY;
array_view<int, 2> &view = GetViewFromCache(buffer, width, height);
parallel_for_each(view.extent, [=](index<2> idx) restrict(amp)
{
int y = idx[0];
int x = idx[1];
float fx = minX + (maxX - minX) * x / width;
float fy = minY + (maxY - minY) * y / height;
float iteration = 0;
float zx = 0, zy = 0;
for (; iteration < maxIteration && zx * zx + zy * zy < 4; ++iteration)
{
float newZx = zx * zx - zy * zy + fx;
zy = 2 * zx * zy + fy;
zx = newZx;
}
if (iteration < maxIteration)
{
float logZn = log(zx * zx + zy * zy) / 2;
float nu = log(logZn / log(2.0f)) / log(2.0f);
iteration = iteration + 1 - nu;
}
float value = iteration / maxIteration;
view[idx] = HSV2RGB_amp(value, 1 - value * value, sqrt(value));
});
view.synchronize();
}
virtual void RenderJuliaSet(
int *buffer, int width, int height, int maxIteration, TFloat _cx, TFloat _cy,
TFloat _minX, TFloat _maxX, TFloat _minY, TFloat _maxY) override
{
using namespace concurrency;
using namespace concurrency::fast_math;
float minX = (float)_minX, maxX = (float)_maxX, minY = (float)_minY, maxY = (float)_maxY;
float cx = (float)_cx, cy = (float)_cy;
array_view<int, 2> &view = GetViewFromCache(buffer, width, height);
parallel_for_each(view.extent, [=](index<2> idx) restrict(amp)
{
int y = idx[0];
int x = idx[1];
float fx = minX + (maxX - minX) * x / width;
float fy = minY + (maxY - minY) * y / height;
float iteration = 0;
float zx = fx, zy = fy;
for (; iteration < maxIteration && zx * zx + zy * zy < 4; ++iteration)
{
float newZx = zx * zx - zy * zy + cx;
zy = 2 * zx * zy + cy;
zx = newZx;
}
if (iteration < maxIteration)
{
float logZn = log(zx * zx + zy * zy) / 2;
float nu = log(logZn / log(2)) / log(2);
iteration = iteration + 1 - nu;
}
float value = iteration / maxIteration;
view[idx] = HSV2RGB_amp(value, 1 - value * value, sqrt(value));
});
view.synchronize();
}
private:
static int HSV2RGB_amp(float H, float S, float V) restrict(amp)
{
using namespace concurrency::fast_math;
float C = V * S;
float H1 = H * 6;
float X = C * (1 - fabs(fmod(H1, 2) - 1));
float R1, G1, B1;
switch ((int)H1)
{
case 0:
R1 = C; G1 = X; B1 = 0;
break;
case 1:
R1 = X; G1 = C; B1 = 0;
break;
case 2:
R1 = 0; G1 = C; B1 = X;
break;
case 3:
R1 = 0; G1 = X; B1 = C;
break;
case 4:
R1 = X; G1 = 0; B1 = C;
break;
case 5:
R1 = C; G1 = 0; B1 = X;
break;
default:
R1 = 0; G1 = 0; B1 = 0;
break;
}
float m = V - C;
int r = (int)((R1 + m) * 255);
int g = (int)((G1 + m) * 255);
int b = (int)((B1 + m) * 255);
return (0 << 24) | (r << 16) | (g << 8) | (b << 0);
}
private:
concurrency::array_view<int, 2>& GetViewFromCache(int *buffer, int width, int height)
{
int idx = -1;
for (int i = 0; i < (int)mViewCache.size(); ++i)
{
if (mViewCache[i].first == buffer) idx = i;
}
if (idx == -1)
{
if (mViewCache.size() == 4) mViewCache.pop_back();
mViewCache.insert(mViewCache.begin(), make_pair(buffer, concurrency::array_view<int, 2>(height, width, buffer)));
idx = 0;
}
return mViewCache[idx].second;
}
private:
std::vector<pair<int*, concurrency::array_view<int, 2>>> mViewCache;
};
#endif