 		{
 			for (int u = -radius; u <= radius; u++)
 			{
-				auto sampleaov = sampleAov(aovs, ix + u, iy + v, width, height);
-				
-				auto moment = sampleaov->moments;
-				moment /= moment.w;
-
-				auto sampleNml = sampleaov->normal;
-
-				float sampleDepth = sampleaov->depth;
-				int sampleMeshId = sampleaov->meshid;
-
-				float n = 1 - dot(sampleNml, centerNormal);
-				float Wn = exp(-0.5f * n * n / (sigmaN * sigmaN));
-
-				float d = 1 - min(centerDepth, sampleDepth) / max(centerDepth, sampleDepth);
-				float Wd = exp(-0.5f * d * d / (sigmaD * sigmaD));
-
-				float Ws = exp(-0.5f * (u * u + v * v) / (sigmaS * sigmaS));
-
-				float Wm = centerMeshId == sampleMeshId ? 1.0f : 0.0f;
-
-				float W = Ws * Wn * Wd * Wm;
-				sum += moment * W;
-				weight += W;
+				if (IS_IN_BOUND(ix + u, 0, width)
+					&& IS_IN_BOUND(iy + v, 0, height))
+				{
+					auto sampleaov = sampleAov(aovs, ix + u, iy + v, width, height);
+
+					auto moment = sampleaov->moments;
+					moment /= moment.w;
+
+					auto sampleNml = sampleaov->normal;
+
+					float sampleDepth = sampleaov->depth;
+					int sampleMeshId = sampleaov->meshid;
+
+#if 0
+					float n = 1 - dot(sampleNml, centerNormal);
+					float Wn = exp(-0.5f * n * n / (sigmaN * sigmaN));
+
+					float d = 1 - min(centerDepth, sampleDepth) / max(centerDepth, sampleDepth);
+					float Wd = exp(-0.5f * d * d / (sigmaD * sigmaD));
+
+					float Ws = exp(-0.5f * (u * u + v * v) / (sigmaS * sigmaS));
+#else
+					float Wn = 1.0f;
+					{
+						float normalCloseness = dot(sampleNml, centerNormal);
+						normalCloseness = normalCloseness * normalCloseness;
+						normalCloseness = normalCloseness * normalCloseness;
+						float normalError = (1.0f - normalCloseness);
+						Wn = max((1.0f - normalError), 0.0f);
+					}
+
+					float Wd = max(0.0f, 1.0f - abs(centerDepth - sampleDepth));
+
+					float Ws = 1.0f;
+					{
+						auto sampleViewPos = computeViewSpace(ix + u, iy + v, sampleDepth, width, height, &mtxC2V);
+
+						// Change in position in camera space.
+						auto dq = centerViewPos - sampleViewPos;
+
+						// How far away is this point from the original sample in camera space? (Max value is unbounded).
+						auto dist2 = dot(dq, dq);
+
+						// How far off the expected plane (on the perpendicular) is this point?  Max value is unbounded.
+						float err = max(abs(dot(dq, sampleNml)), abs(dot(dq, centerNormal)));
+
+						Ws = (dist2 < 0.001f)
+							? 1.0
+							: pow(max(0.0, 1.0 - 2.0 * err / sqrt(dist2)), 2.0);
+					}
+#endif
+
+					float Wm = centerMeshId == sampleMeshId ? 1.0f : 0.0f;
+
+					float W = Ws * Wn * Wd * Wm;
+					sum += moment * W;
+					weight += W;
+				}
 			}
 		}
 

		//varianceEstimation << <1, 1 >> > (
			outputSurf,
			curaov.ptr(),
			width, height);

		checkCudaKernel(varianceEstimation);

		static const int radius = 3;
		static const float sigmaN = 0.005f;
		static const float sigmaD = 0.005f;
		static const float sigmaS = 0.965f;

		auto centerNormal = aovs[idx].normal;

