Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <stdio.h>
- #include "olcPixelGameEngine.h"
- __device__ void complex_square (float* r, float* i)
- {
- float ri = (*r) * (*i);
- *r = (*r) * (*r) - (*i) * (*i);
- *i = 2.0f * ri;
- }
- __global__ void kernel (int* iterations, int width, int height, float ro, float io)
- {
- int y = threadIdx.x;
- float ci = -1.0f + 2.0f * (float)(y) / height;
- for (int x = 0; x < width; ++x)
- {
- float cr = -1.0f + 2.0f * (float)(x) / width;
- float zr = cr;
- float zi = ci;
- int iteration = 0;
- for (iteration; iteration < 100 && zr * zr + zi * zi < 16.0f; ++iteration)
- {
- complex_square (&zr, &zi);
- zr += ro;
- zi += io;
- }
- iterations[height * x + y] = iteration;
- }
- }
- class Display : public olc::PixelGameEngine
- {
- public:
- bool OnUserCreate () override
- {
- cudaMallocManaged (&dh_iterations, sizeof (int) * ScreenWidth () * ScreenHeight ());
- return true;
- }
- bool OnUserUpdate (float elapsed) override
- {
- kernel <<<1, ScreenHeight () >>> (dh_iterations, ScreenWidth (), ScreenHeight (), sinf(time),cosf(time * 0.25f));
- cudaDeviceSynchronize ();
- for (int x = 0; x < ScreenWidth (); ++x)
- {
- for (int y = 0; y < ScreenHeight (); ++y)
- {
- int it = dh_iterations[ScreenHeight () * x + y];
- if (it == 100)
- {
- Draw (x, y, olc::BLACK);
- }
- else
- Draw (x, y, olc::Pixel (it, it * 2, it * 8));
- }
- }
- time += elapsed;
- return true;
- }
- bool OnUserDestroy () override
- {
- cudaFree (dh_iterations);
- return true;
- }
- Display ()
- {
- sAppName = "Fractal Renderer";
- }
- private:
- int* dh_iterations;
- float time = 0.0f;
- };
- int main ()
- {
- Display d;
- d.Construct (800, 600, 1, 1);
- d.Start ();
- }
Add Comment
Please, Sign In to add comment