-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cl
More file actions
36 lines (34 loc) · 1.79 KB
/
kernel.cl
File metadata and controls
36 lines (34 loc) · 1.79 KB
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
float getBlurStrength(float depth, float focus, float bounds, float depthLimit)
{
return fmax(0.0f,(fabs(depth-focus)-bounds)/depthLimit);
}
__kernel void kernelMain(__read_only image2d_t inputImage, __write_only image2d_t outputImage, __read_only image2d_t inputDepth, float depthLimit, int strength, float focus, float bounds)
{
const sampler_t imageSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
const sampler_t depthSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
int2 coords = (int2)(get_global_id(0), get_global_id(1));
float4 depth = read_imagef(inputDepth, depthSampler, coords);
float blurStrength = getBlurStrength(depth[0], focus, bounds, depthLimit);
uint kernelSize = round((strength*2)*blurStrength);
if (kernelSize%2 == 0)
kernelSize++;
const int kernelHalf = kernelSize/2;
float weightSum = 1.0f;
float4 originalPixel = convert_float4(read_imageui(inputImage, imageSampler, coords));
float4 pixelSum = originalPixel;
for (int x = -kernelHalf; x <= kernelHalf; x++)
for (int y = -kernelHalf; y <= kernelHalf; y++)
{
int2 newCoords = coords + (int2)(x, y);
float4 sampleDepth = read_imagef(inputDepth, depthSampler, newCoords);
float sampleStrength = getBlurStrength(sampleDepth[0], focus, bounds, depthLimit);
float kernelWeight = distance(convert_float2(coords), convert_float2(newCoords));
float weight = kernelWeight;
if (sampleStrength < 0.1f)
weight *= sampleStrength;
weightSum += weight;
pixelSum += convert_float4(read_imageui(inputImage, imageSampler, newCoords))*weight;
}
uint4 pixel = convert_uint4(round(pixelSum/weightSum));
write_imageui(outputImage, coords, pixel);
}