-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshader.comp
56 lines (47 loc) · 1.49 KB
/
shader.comp
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
#version 450
#extension GL_ARB_separate_shader_objects : enable
#define WIDTH 3200
#define HEIGHT 2400
#define WORKGROUP_SIZE 32
layout (local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1 ) in;
struct Pixel{
vec4 value;
};
layout(std140, binding = 0) buffer buf
{
Pixel imageData[];
};
void main() {
/*
In order to fit the work into workgroups, some unnecessary threads are launched.
We terminate those threads here.
*/
if(gl_GlobalInvocationID.x >= WIDTH || gl_GlobalInvocationID.y >= HEIGHT)
return;
float x = float(gl_GlobalInvocationID.x) / float(WIDTH);
float y = float(gl_GlobalInvocationID.y) / float(HEIGHT);
/*
What follows is code for rendering the mandelbrot set.
*/
vec2 uv = vec2(x,y);
float n = 0.0;
vec2 c = vec2(-.445, 0.0) + (uv - 0.5)*(2.0+ 1.7*0.2 ),
z = vec2(0.0);
const int M =128;
for (int i = 0; i<M; i++)
{
z = vec2(z.x*z.x - z.y*z.y, 2.*z.x*z.y) + c;
if (dot(z, z) > 2) break;
n++;
}
// we use a simple cosine palette to determine color:
// http://iquilezles.org/www/articles/palettes/palettes.htm
float t = float(n) / float(M);
vec3 d = vec3(0.3, 0.3 ,0.5);
vec3 e = vec3(-0.2, -0.3 ,-0.5);
vec3 f = vec3(2.1, 2.0, 3.0);
vec3 g = vec3(0.0, 0.1, 0.0);
vec4 color = vec4( d + e*cos( 6.28318*(f*t+g) ) ,1.0);
// store the rendered mandelbrot set into a storage buffer:
imageData[WIDTH * gl_GlobalInvocationID.y + gl_GlobalInvocationID.x].value = color;
}