2 * Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
5 * with this source code for terms and conditions that govern your use of
6 * this software. Any use, reproduction, disclosure, or distribution of
7 * this software and related documentation outside the terms of the EULA
8 * is strictly prohibited.
14 ////////////////////////////////////////////////////////////////////////////////
16 ////////////////////////////////////////////////////////////////////////////////
25 const int ix = blockDim.x * blockIdx.x + threadIdx.x;
26 const int iy = blockDim.y * blockIdx.y + threadIdx.y;
27 //Add half of a texel to always address exact texel centers
28 const float x = (float)ix + 0.5f;
29 const float y = (float)iy + 0.5f;
31 if (ix < imageW && iy < imageH)
33 //Normalized counter for the weight threshold
35 //Total sum of pixel weights
38 float3 clr = {0, 0, 0};
39 //Center of the KNN window
40 float4 clr00 = tex2D(texImage, x, y);
42 //Cycle through KNN window, surrounding (x, y) texel
43 for (float i = -KNN_WINDOW_RADIUS; i <= KNN_WINDOW_RADIUS; i++)
44 for (float j = -KNN_WINDOW_RADIUS; j <= KNN_WINDOW_RADIUS; j++)
46 float4 clrIJ = tex2D(texImage, x + j, y + i);
47 float distanceIJ = vecLen(clr00, clrIJ);
49 //Derive final weight from color distance
50 float weightIJ = __expf(- (distanceIJ * Noise + (i * i + j * j) * INV_KNN_WINDOW_AREA));
52 //Accumulate (x + j, y + i) texel color with computed weight
53 clr.x += clrIJ.x * weightIJ;
54 clr.y += clrIJ.y * weightIJ;
55 clr.z += clrIJ.z * weightIJ;
57 //Sum of weights for color normalization to [0..1] range
58 sumWeights += weightIJ;
60 //Update weight counter, if KNN weight for current window texel
61 //exceeds the weight threshold
62 fCount += (weightIJ > KNN_WEIGHT_THRESHOLD) ? INV_KNN_WINDOW_AREA : 0;
65 //Normalize result color by sum of weights
66 sumWeights = 1.0f / sumWeights;
71 //Choose LERP quotent basing on how many texels
72 //within the KNN window exceeded the weight threshold
73 float lerpQ = (fCount > KNN_LERP_THRESHOLD) ? lerpC : 1.0f - lerpC;
75 //Write final result to global memory
76 clr.x = lerpf(clr.x, clr00.x, lerpQ);
77 clr.y = lerpf(clr.y, clr00.y, lerpQ);
78 clr.z = lerpf(clr.z, clr00.z, lerpQ);
79 dst[imageW * iy + ix] = make_color(clr.x, clr.y, clr.z, 0);
92 dim3 threads(BLOCKDIM_X, BLOCKDIM_Y);
93 dim3 grid(iDivUp(imageW, BLOCKDIM_X), iDivUp(imageH, BLOCKDIM_Y));
95 KNN<<<grid, threads>>>(d_dst, imageW, imageH, Noise, lerpC);
99 ////////////////////////////////////////////////////////////////////////////////
100 // Stripped KNN kernel, only highlighting areas with different LERP directions
101 ////////////////////////////////////////////////////////////////////////////////
102 __global__ void KNNdiag(
110 const int ix = blockDim.x * blockIdx.x + threadIdx.x;
111 const int iy = blockDim.y * blockIdx.y + threadIdx.y;
112 //Add half of a texel to always address exact texel centers
113 const float x = (float)ix + 0.5f;
114 const float y = (float)iy + 0.5f;
116 if (ix < imageW && iy < imageH)
118 //Normalized counter for the weight threshold
120 //Center of the KNN window
121 float4 clr00 = tex2D(texImage, x, y);
123 //Cycle through KNN window, surrounding (x, y) texel
124 for (float i = -KNN_WINDOW_RADIUS; i <= KNN_WINDOW_RADIUS; i++)
125 for (float j = -KNN_WINDOW_RADIUS; j <= KNN_WINDOW_RADIUS; j++)
127 float4 clrIJ = tex2D(texImage, x + j, y + i);
128 float distanceIJ = vecLen(clr00, clrIJ);
130 //Derive final weight from color and geometric distance
131 float weightIJ = __expf(- (distanceIJ * Noise + (i * i + j * j) * INV_KNN_WINDOW_AREA));
133 //Update weight counter, if KNN weight for current window texel
134 //exceeds the weight threshold
135 fCount += (weightIJ > KNN_WEIGHT_THRESHOLD) ? INV_KNN_WINDOW_AREA : 0.0f;
138 //Choose LERP quotent basing on how many texels
139 //within the KNN window exceeded the weight threshold
140 float lerpQ = (fCount > KNN_LERP_THRESHOLD) ? 1.0f : 0;
142 //Write final result to global memory
143 dst[imageW * iy + ix] = make_color(lerpQ, 0, (1.0f - lerpQ), 0);
156 dim3 threads(BLOCKDIM_X, BLOCKDIM_Y);
157 dim3 grid(iDivUp(imageW, BLOCKDIM_X), iDivUp(imageH, BLOCKDIM_Y));
159 KNNdiag<<<grid, threads>>>(d_dst, imageW, imageH, Noise, lerpC);