Skip to content

Commit afd6af0

Browse files
author
Andre Cunha
committed
Initial commit
0 parents commit afd6af0

File tree

12 files changed

+11175
-0
lines changed

12 files changed

+11175
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required (VERSION 3.8)
2+
project (mandelbrot)
3+
4+
find_package(Vulkan)
5+
6+
set (CMAKE_CXX_STANDARD 14)
7+
8+
include_directories(${Vulkan_INCLUDE_DIR})
9+
10+
add_executable(mandelbrot src/mandelbrot.cc src/lodepng.cpp src/vulkan_ext.c)
11+
12+
target_link_libraries(mandelbrot ${Vulkan_LIBRARY})

LICENSE.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Andre Cunha
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
23+
24+
License for [Erkaman/vulkan_minimal_compute](https://github.com/Erkaman/vulkan_minimal_compute/)
25+
================================================================================================
26+
27+
The MIT License (MIT)
28+
29+
Copyright (c) 2017 Eric Arnebäck
30+
31+
Permission is hereby granted, free of charge, to any person obtaining a copy
32+
of this software and associated documentation files (the "Software"), to deal
33+
in the Software without restriction, including without limitation the rights
34+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
35+
copies of the Software, and to permit persons to whom the Software is
36+
furnished to do so, subject to the following conditions:
37+
38+
The above copyright notice and this permission notice shall be included in
39+
all copies or substantial portions of the Software.
40+
41+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
44+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
46+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
47+
THE SOFTWARE.
48+
49+
50+
License for [lvandeve/lodepng](https://github.com/lvandeve/lodepng)
51+
===================================================================
52+
53+
LodePNG version 20180611
54+
55+
Copyright (c) 2005-2018 Lode Vandevenne
56+
57+
This software is provided 'as-is', without any express or implied
58+
warranty. In no event will the authors be held liable for any damages
59+
arising from the use of this software.
60+
61+
Permission is granted to anyone to use this software for any purpose,
62+
including commercial applications, and to alter it and redistribute it
63+
freely, subject to the following restrictions:
64+
65+
1. The origin of this software must not be misrepresented; you must not
66+
claim that you wrote the original software. If you use this software
67+
in a product, an acknowledgment in the product documentation would be
68+
appreciated but is not required.
69+
70+
2. Altered source versions must be plainly marked as such, and must not be
71+
misrepresented as being the original software.
72+
73+
3. This notice may not be removed or altered from any source
74+
distribution.
75+
76+
77+
License for the [Vulkan Extension Loader](https://github.com/KhronosGroup/Vulkan-Docs/blob/1.0/src/ext_loader/)
78+
===============================================================================================================
79+
80+
Copyright (c) 2015-2018 The Khronos Group Inc.
81+
82+
Licensed under the Apache License, Version 2.0 (the "License");
83+
you may not use this file except in compliance with the License.
84+
You may obtain a copy of the License at
85+
86+
http://www.apache.org/licenses/LICENSE-2.0
87+
88+
Unless required by applicable law or agreed to in writing, software
89+
distributed under the License is distributed on an "AS IS" BASIS,
90+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
91+
See the License for the specific language governing permissions and
92+
limitations under the License.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Mandelbrot Fractal in C++ using Vulkan Compute
2+
3+
This is a demo application that uses the Vulkan C++ API to draw a Mandelbrot fractal.
4+
5+
This application is an adaptation of [Erkaman/vulkan_minimal_compute](https://github.com/Erkaman/vulkan_minimal_compute/)
6+
to use the [Vulkan-Hpp](https://github.com/KhronosGroup/Vulkan-Hpp) C++ library from Khronos.
7+
8+
![](image.png)
9+
10+
# Dependencies
11+
12+
You need a C++14 compiler.
13+
14+
All the library dependencies are included.
15+
16+
* [lodepng](https://github.com/lvandeve/lodepng)
17+
* [Vulkan Extension Loader](https://github.com/KhronosGroup/Vulkan-Docs/blob/1.0/src/ext_loader/)
18+
19+
# Building
20+
21+
```shell
22+
mkdir build
23+
cd build
24+
cmake ..
25+
make
26+
```
27+
28+
# Execution
29+
30+
From the `mandelbrot_vulkan_cpp` directory, run:
31+
32+
```shell
33+
build/mandelbrot
34+
```
35+
36+
The application launches a compute shader that renders the Mandelbrot set into a storage buffer on the GPU.
37+
The storage buffer is then read and saved as `mandelbrot.png`.

image.png

384 KB
Loading

shaders/comp.spv

3.59 KB
Binary file not shown.

shaders/shader.comp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#version 450
2+
#extension GL_ARB_separate_shader_objects : enable
3+
4+
#define WIDTH 3200
5+
#define HEIGHT 2400
6+
#define WORKGROUP_SIZE 32
7+
layout (local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1 ) in;
8+
9+
struct Pixel{
10+
vec4 value;
11+
};
12+
13+
layout(std140, binding = 0) buffer buf
14+
{
15+
Pixel imageData[];
16+
};
17+
18+
void main() {
19+
20+
/*
21+
In order to fit the work into workgroups, some unnecessary threads are launched.
22+
We terminate those threads here.
23+
*/
24+
if(gl_GlobalInvocationID.x >= WIDTH || gl_GlobalInvocationID.y >= HEIGHT)
25+
return;
26+
27+
float x = float(gl_GlobalInvocationID.x) / float(WIDTH);
28+
float y = float(gl_GlobalInvocationID.y) / float(HEIGHT);
29+
30+
/*
31+
What follows is code for rendering the mandelbrot set.
32+
*/
33+
vec2 uv = vec2(x,y);
34+
float n = 0.0;
35+
vec2 c = vec2(-.445, 0.0) + (uv - 0.5)*(2.0+ 1.7*0.2 ),
36+
z = vec2(0.0);
37+
const int M =128;
38+
for (int i = 0; i<M; i++)
39+
{
40+
z = vec2(z.x*z.x - z.y*z.y, 2.*z.x*z.y) + c;
41+
if (dot(z, z) > 2) break;
42+
n++;
43+
}
44+
45+
// we use a simple cosine palette to determine color:
46+
// http://iquilezles.org/www/articles/palettes/palettes.htm
47+
float t = float(n) / float(M);
48+
vec3 d = vec3(0.3, 0.3 ,0.5);
49+
vec3 e = vec3(-0.2, -0.3 ,-0.5);
50+
vec3 f = vec3(2.1, 2.0, 3.0);
51+
vec3 g = vec3(0.0, 0.1, 0.0);
52+
vec4 color = vec4( d + e*cos( 6.28318*(f*t+g) ) ,1.0);
53+
54+
// store the rendered mandelbrot set into a storage buffer:
55+
imageData[WIDTH * gl_GlobalInvocationID.y + gl_GlobalInvocationID.x].value = color;
56+
}

0 commit comments

Comments
 (0)