Draft decomposite/reconstruction shaders

This commit is contained in:
mittorn 2024-10-30 05:04:45 +03:00
parent b4eca34dfc
commit f4b6e2b08d
2 changed files with 133 additions and 0 deletions

93
image-decomposite.comp Normal file
View file

@ -0,0 +1,93 @@
#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);
layout (binding = 0, r8) uniform image2D resultImage;
layout (binding = 1, rg8) uniform image2D resultImage2;
layout (binding = 2, rgba8) uniform image2D reconstructionImage;
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(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 r1 = samplef(gl_GlobalInvocationID.x * 4 + i, gl_GlobalInvocationID.y * 4 + j);
vec4 r2 = samplef(gl_GlobalInvocationID.x * 4 + i + 1, gl_GlobalInvocationID.y * 4 + j);
vec4 r3 = samplef(gl_GlobalInvocationID.x * 4 + i, gl_GlobalInvocationID.y * 4 + j + 1);
vec4 r4 = samplef(gl_GlobalInvocationID.x * 4 + i + 1, gl_GlobalInvocationID.y * 4 + j + 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;
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(cb1/ 4 + 0.5,cc1/4 + 0.5,cd1/4 + 0.5,1));
// 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));
}

40
reconstruction.comp Normal file
View file

@ -0,0 +1,40 @@
#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 (binding = 0) uniform sampler2D ycbcrSampler;
// todo: should it be sampler?
layout (binding = 1, rgba8) uniform image2D inputImage;
layout (binding = 2, rgba8) uniform image2D resultImage;
layout (constant_id = 0) const float width = 1920;
layout (constant_id = 1) const float height = 1080;
const vec2 in_res = vec2(width,height);
vec4 convertYuv(float y, float u, float v)
{
return vec4(y + 1.403*v, y - 0.344*u - 0.714*v, y + 1.770*u,1);
}
void main()
{
vec2 uv = vec2(gl_GlobalInvocationID.xy)/in_res;
vec3 yuv = texture(ycbcrSampler,uv).xyz;
vec3 rec = vec3(imageLoad(inputImage, ivec2(gl_GlobalInvocationID.xy)).xyz);
float ca1 = yuv.y;
float cb1 = rec.x - 0.5;
float cc1 = rec.y - 0.5;
float cd1 = rec.z - 0.5;
float ca = ca1 + cb1;
float cb = cc1 + cd1;
float cc = ca1 - cb1;
float cd = cc1 - cd1;
float y1 = ca + cb;
float y2 = ca - cb;
float y3 = cc + cd;
float y4 = cc - cd;
imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2, gl_GlobalInvocationID.y * 2), convertYuv(y1, yuv.z, yuv.x));
imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2 + 1, gl_GlobalInvocationID.y * 2), convertYuv(y2, yuv.z, yuv.x));
imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2, gl_GlobalInvocationID.y * 2 + 1), convertYuv(y3, yuv.z, yuv.x));
imageStore(resultImage, ivec2(gl_GlobalInvocationID.x * 2 + 1 , gl_GlobalInvocationID.y * 2 + 1), convertYuv(y4, yuv.z, yuv.x));
}