2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
2.1 KiB

  1. //
  2. // SCDepthBlurMetalModule.metal
  3. // Snapchat
  4. //
  5. // Created by Brian Ng on 10/31/17.
  6. //
  7. #include <metal_stdlib>
  8. using namespace metal;
  9. struct DepthBlurRenderData {
  10. float depthRange;
  11. float depthOffset;
  12. float depthBlurForegroundThreshold;
  13. float depthBlurBackgroundThreshold;
  14. };
  15. kernel void kernel_depth_blur(texture2d<float, access::read> sourceYTexture [[texture(0)]],
  16. texture2d<float, access::read> sourceUVTexture [[texture(1)]],
  17. texture2d<float, access::read> sourceDepthTexture[[texture(2)]],
  18. texture2d<float, access::read> sourceBlurredYTexture [[texture(3)]],
  19. texture2d<float, access::write> destinationYTexture [[texture(4)]],
  20. texture2d<float, access::write> destinationUVTexture [[texture(5)]],
  21. constant DepthBlurRenderData &renderData [[buffer(0)]],
  22. uint2 gid [[thread_position_in_grid]],
  23. uint2 size [[threads_per_grid]]) {
  24. float2 valueUV = sourceUVTexture.read(gid).rg;
  25. float depthValue = sourceDepthTexture.read(uint2(gid.x/4, gid.y/4)).r;
  26. float normalizedDepthValue = (depthValue - renderData.depthOffset) / renderData.depthRange;
  27. float valueYUnblurred = sourceYTexture.read(gid).r;
  28. float valueYBlurred = sourceBlurredYTexture.read(gid).r;
  29. float valueY = 0;
  30. if (normalizedDepthValue > renderData.depthBlurForegroundThreshold) {
  31. valueY = valueYUnblurred;
  32. } else if (normalizedDepthValue < renderData.depthBlurBackgroundThreshold) {
  33. valueY = valueYBlurred;
  34. } else {
  35. float blendRange = renderData.depthBlurForegroundThreshold - renderData.depthBlurBackgroundThreshold;
  36. float normalizedBlendDepthValue = (normalizedDepthValue - renderData.depthBlurBackgroundThreshold) / blendRange;
  37. valueY = valueYUnblurred * normalizedBlendDepthValue + valueYBlurred * (1 - normalizedBlendDepthValue);
  38. }
  39. destinationYTexture.write(valueY, gid);
  40. destinationUVTexture.write(float4(valueUV.r, valueUV.g, 0, 0), gid);
  41. }