-
-
Save bluebear94/25d7e2b787d9e880b0d2c2f3ef7b5c4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <complex> | |
#include <CL/sycl.hpp> | |
#include <png++/png.hpp> | |
namespace sycl = cl::sycl; | |
class GenerateFractal; | |
constexpr size_t width = 1280, height = 960; | |
constexpr size_t max_iterations = 16; | |
// Change these to get a different window | |
constexpr float min_x = -2, max_x = 1, | |
min_y = -9.0 / 8, max_y = 9.0 / 8; | |
constexpr const uint8_t mandelbrot_colors[][3] = { | |
{0x0A, 0x2F, 0x51}, | |
{0x0C, 0x3D, 0x5B}, | |
{0x0E, 0x4D, 0x64}, | |
{0x10, 0x5E, 0x6E}, | |
{0x13, 0x71, 0x77}, | |
{0x15, 0x80, 0x7B}, | |
{0x18, 0x89, 0x77}, | |
{0x1A, 0x91, 0x72}, | |
{0x1D, 0x9A, 0x6C}, | |
{0x32, 0xA6, 0x6B}, | |
{0x48, 0xB1, 0x6D}, | |
{0x5E, 0xBC, 0x72}, | |
{0x74, 0xC6, 0x7A}, | |
{0x8F, 0xD0, 0x8A}, | |
{0xAD, 0xDA, 0xA1}, | |
{0xC7, 0xE4, 0xB8}, | |
{0xDE, 0xED, 0xCF}, | |
}; | |
size_t escape_time(std::complex<float> c) { | |
std::complex<float> z = 0; | |
for (size_t i = 0; i < max_iterations; ++i) { | |
z = z * z + c; | |
if (std::abs(z) > 2) return i; | |
} | |
return max_iterations; | |
} | |
float mix(float x, float y, float a) { | |
return x + a * (y - x); // Good enough for our purposes | |
} | |
std::complex<float> get_coordinates(size_t pixel_x, size_t pixel_y) { | |
return std::complex{ | |
mix(min_x, max_x, ((float) pixel_x) / width), | |
mix(max_y, min_y, ((float) pixel_y) / height)}; | |
} | |
int main() { | |
sycl::queue q; | |
sycl::buffer<uint8_t, 2> image{{height, width}}; | |
q.submit([&](sycl::handler& cgh) { | |
auto I = image.get_access<sycl::access::mode::write>(cgh); | |
cgh.parallel_for<GenerateFractal>(sycl::range<2>{height, width}, [=](sycl::id<2> index) { | |
I[index] = (uint8_t) escape_time(get_coordinates(index[1], index[0])); | |
}); | |
}); | |
auto I = image.get_access<sycl::access::mode::read>(); | |
png::image<png::rgb_pixel> png_image(width, height); | |
for (png::uint_32 y = 0; y < height; ++y) { | |
for (png::uint_32 x = 0; x < height; ++x) { | |
uint8_t idx = I[y][x]; | |
const uint8_t* col = mandelbrot_colors[idx]; | |
png_image[y][x] = png::rgb_pixel(col[0], col[1], col[2]); | |
} | |
} | |
png_image.write("mandelbrot.png"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment