vulkan-playground/image-decomposite.comp

119 lines
4.4 KiB
Text

#version 450
#extension GL_ARB_separate_shader_objects : enable
#define WORKGROUP_SIZE 32
layout (local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1 ) in;
layout (constant_id = 0) const float width = 1920;
layout (constant_id = 1) const float height = 1080;
const vec2 in_res = vec2(width,height);
#include "conversion.glsl"
layout (binding = 0, r16) writeonly uniform image2D resultImage;
layout (binding = 1, rg16) writeonly uniform image2D resultImage2;
#if CONV_MODE == CONV_332
layout (binding = 2, r8ui) writeonly uniform uimage2D reconstructionImage;
#else
layout (binding = 2, rgba8) writeonly uniform image2D reconstructionImage;
#endif
layout (binding = 3) uniform UBO
{
float frameNum;
};
vec4 samplef(float x0, float y0)
{
float x = float(x0) / width;
float y = float(y0) / height;
/*
What follows is code for rendering the mandelbrot set.
*/
vec2 uv = vec2(x,y);
float n = 0.0;
vec2 c = vec2(cos(frameNum / 500) - 0.6,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);
t = sqrt(t);
// return vec4(t,t,t,t);//
return vec4( d + e*cos( 6.28318*(f*t+g) ) ,1.0);
}
vec4 sample4f(float x0, float y0)
{
return (samplef(x0,y0) + samplef(x0+0.5,y0)+samplef(x0,y0+0.5)+samplef(x0+0.5,y0+0.5))/4;
}
const float cmul = 32.0;
const float off = 31.0/63.0;//7.0/15;//128.0/255;
const float off2 = 15.0/31.0;
float cmul2 = 64;
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 r1 = sample4f(gl_GlobalInvocationID.x * 4 + i*2, gl_GlobalInvocationID.y * 4 + j*2);
vec4 r2 = sample4f(gl_GlobalInvocationID.x * 4 + i*2 + 1, gl_GlobalInvocationID.y * 4 + j*2);
vec4 r3 = sample4f(gl_GlobalInvocationID.x * 4 + i*2, gl_GlobalInvocationID.y * 4 + j*2 + 1);
vec4 r4 = sample4f(gl_GlobalInvocationID.x * 4 + i*2 + 1, gl_GlobalInvocationID.y * 4 + j*2 + 1);
float cy1 = r1.r * 0.2126 + r1.g * 0.7152 + r1.b * 0.0722;
float cy2 = r2.r * 0.2126 + r2.g * 0.7152 + r2.b * 0.0722;
float cy3 = r3.r * 0.2126 + r3.g * 0.7152 + r3.b * 0.0722;
float cy4 = r4.r * 0.2126 + r4.g * 0.7152 + r4.b * 0.0722;
float ca = cy1 + cy2;
float cb = cy1 - cy2;
float cc = cy3 + cy4;
float cd = cy3 - cy4;
float ca1 = ca + cc;
float cb1 = ca - cc;
float cc1 = cb + cd;
float cd1 = cb - cd;
float val2 = float(gl_GlobalInvocationID.x * 4 + i*2)/1920*2.0-1.0;
//float val = abs(ainvconv(rn(aconv(val2))));
imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j), vec4(ca1/4,0,0,0));
// imageStore(reconstructionImage, ivec2(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j), vec4(aconv(val2),aconv(val2),aconv2(val2),1));//vec4(aconv(cb1),aconv(cc1),aconv(cd1),aconv(cd1)));//cd1/4 + 0.5));
imageStore(reconstructionImage, ivec2(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j), conv_store(vec3(cb1,cc1,cd1)));
//imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j), vec4(r1.r,0,0,0));
//imageStore(reconstructionImage, ivec2(gl_GlobalInvocationID.x * 2 + i, gl_GlobalInvocationID.y * 2 + j), vec4(r2.r,r3.r,r4.r,1));
//vec4(aconv(cb1/2),aconv(cc1/2),aconv(cd1/2),1));//cd1/4 + 0.5));
// if(i == 0 && j == 0)
// cl = r;
cl += r1 + r2 + r3 + r4;
}
cl = cl / 16;
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));
}