#version 330 core
#extension GL_ARB_compute_shader : require
#extension GL_ARB_shader_image_load_store : require

#define LOCAL_SIZE 8

uniform sampler3D texVolume; //input data, only intensity is set
uniform writeonly image3D imgTarget; //output data, alpha = filtered intensity

//maximum kernel size = 9x9x9
uniform float afWeights[729];
uniform ivec3 ivSize; // kernel size, should usually be the same in each direction
uniform ivec3 ivOffset;

float getVal(in ivec3 pos)
{
	return texelFetch(texVolume,pos,0).a;
}

//Gaussian smoothing of 3D volume data
layout(local_size_x = LOCAL_SIZE, local_size_y = LOCAL_SIZE, local_size_z = LOCAL_SIZE) in;
void main(void)
{
	//invocation id equals to 3d coords here
	ivec3 pos = ivec3(gl_GlobalInvocationID);

	float value = 0;
	
	//convolution for single voxel
	for (uint i = 0; i < ivSize.z; i++) {
		for (uint j = 0; j < ivSize.y; j++) {
			for (uint k = 0; k < ivSize.x; k++) {
				value += afWeights[i*ivSize.x*ivSize.y + j*ivSize.x + k] * getVal( pos + ivec3(i, j, k) + ivOffset );
			}
		}
	}

	vec4 finalVal = vec4(value,0,0,0);
	imageStore(imgTarget,pos,finalVal);
}

