vulkan-playground/image.comp
2024-10-12 02:44:21 +03:00

69 lines
2.2 KiB
Text

#version 450
#extension GL_ARB_separate_shader_objects : enable
#define WIDTH 1920
#define HEIGHT 1080
#define WORKGROUP_SIZE 32
layout (local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1 ) in;
layout (binding = 0, r8) uniform image2D resultImage;
layout (binding = 1, rg8) uniform image2D resultImage2;
layout (binding = 2) uniform UBO
{
float frameNum;
};
vec4 samplef(float x0, float y0)
{
float x = float(x0) / float(WIDTH);
float y = float(y0) / float(HEIGHT);
/*
What follows is code for rendering the mandelbrot set.
*/
vec2 uv = vec2(x,y);
float n = 0.0;
vec2 c = vec2(frameNum / 1000,0.1);//vec2(-.445, 0.0) + (uv - 0.5)*(2.0+ 1.7*0.2 ),
vec2 z = vec2(-.445, 0.0) + (uv - 0.5)*(2.0+ 1.7*0.2 );//,//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);
return vec4( d + e*cos( 6.28318*(f*t+g) ) ,1.0);
}
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;
vec4 cl = vec4(0,0,0,0);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
{
vec4 r = samplef(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j);
float cy = r.r * 0.2126 + r.g * 0.7152 + r.b * 0.0722;
imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j), vec4(cy,0,0,0));
// if(i == 0 && j == 0)
// cl = r;
cl += r;
}
cl = cl / 4;
vec2 cuv = vec2(0.5 + cl.r * -0.1146 + cl.g * -0.3854 + cl.b * 0.5, 0.5 + cl.r * 0.5 + cl.g * -0.4542 + cl.b * -0.0458);
// store the rendered mandelbrot set into a storage buffer:
imageStore(resultImage2, ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y), vec4(cuv,0,0));
}