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.

90 lines
3.1 KiB

  1. //
  2. // SCDepthBlurMetalRenderCommand.m
  3. // Snapchat
  4. //
  5. // Created by Brian Ng on 11/8/17.
  6. //
  7. //
  8. #import "SCDepthBlurMetalRenderCommand.h"
  9. #import "SCCameraTweaks.h"
  10. #import "SCMetalUtils.h"
  11. #import <SCFoundation/NSString+SCFormat.h>
  12. @import MetalPerformanceShaders;
  13. @implementation SCDepthBlurMetalRenderCommand
  14. typedef struct DepthBlurRenderData {
  15. float depthRange;
  16. float depthOffset;
  17. float depthBlurForegroundThreshold;
  18. float depthBlurBackgroundThreshold;
  19. } DepthBlurRenderData;
  20. #pragma mark - SCMetalRenderCommand
  21. - (id<MTLComputeCommandEncoder>)encodeMetalCommand:(id<MTLCommandBuffer>)commandBuffer
  22. pipelineState:(id<MTLComputePipelineState>)pipelineState
  23. textureResource:(SCMetalTextureResource *)textureResource
  24. {
  25. #if !TARGET_IPHONE_SIMULATOR
  26. CGFloat depthBlurForegroundThreshold = textureResource.depthBlurForegroundThreshold;
  27. CGFloat depthBlurBackgroundThreshold =
  28. textureResource.depthBlurForegroundThreshold > SCCameraTweaksDepthBlurBackgroundThreshold()
  29. ? SCCameraTweaksDepthBlurBackgroundThreshold()
  30. : 0;
  31. DepthBlurRenderData depthBlurRenderData = {
  32. .depthRange = textureResource.depthRange,
  33. .depthOffset = textureResource.depthOffset,
  34. .depthBlurBackgroundThreshold = depthBlurBackgroundThreshold,
  35. .depthBlurForegroundThreshold = depthBlurForegroundThreshold,
  36. };
  37. id<MTLBuffer> depthBlurRenderDataBuffer =
  38. [textureResource.device newBufferWithLength:sizeof(DepthBlurRenderData)
  39. options:MTLResourceOptionCPUCacheModeDefault];
  40. memcpy(depthBlurRenderDataBuffer.contents, &depthBlurRenderData, sizeof(DepthBlurRenderData));
  41. MPSImageGaussianBlur *kernel =
  42. [[MPSImageGaussianBlur alloc] initWithDevice:textureResource.device sigma:SCCameraTweaksBlurSigma()];
  43. [kernel encodeToCommandBuffer:commandBuffer
  44. sourceTexture:textureResource.sourceYTexture
  45. destinationTexture:textureResource.sourceBlurredYTexture];
  46. id<MTLComputeCommandEncoder> commandEncoder = [commandBuffer computeCommandEncoder];
  47. [commandEncoder setComputePipelineState:pipelineState];
  48. [commandEncoder setTexture:textureResource.sourceYTexture atIndex:0];
  49. [commandEncoder setTexture:textureResource.sourceUVTexture atIndex:1];
  50. [commandEncoder setTexture:textureResource.sourceDepthTexture atIndex:2];
  51. [commandEncoder setTexture:textureResource.sourceBlurredYTexture atIndex:3];
  52. [commandEncoder setTexture:textureResource.destinationYTexture atIndex:4];
  53. [commandEncoder setTexture:textureResource.destinationUVTexture atIndex:5];
  54. [commandEncoder setBuffer:depthBlurRenderDataBuffer offset:0 atIndex:0];
  55. return commandEncoder;
  56. #else
  57. return nil;
  58. #endif
  59. }
  60. - (BOOL)requiresDepthData
  61. {
  62. return YES;
  63. }
  64. #pragma mark - SCMetalModuleFunctionProvider
  65. - (NSString *)functionName
  66. {
  67. return @"kernel_depth_blur";
  68. }
  69. - (NSString *)description
  70. {
  71. return [NSString sc_stringWithFormat:@"SCDepthBlurMetalRenderCommand (shader function = %@)", self.functionName];
  72. }
  73. @end