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.

72 lines
2.1 KiB

  1. //
  2. // SCDepthToGrayscaleMetalRenderCommand.m
  3. // Snapchat
  4. //
  5. // Created by Brian Ng on 12/7/17.
  6. //
  7. //
  8. #import "SCDepthToGrayscaleMetalRenderCommand.h"
  9. #import "SCCameraTweaks.h"
  10. #import "SCMetalUtils.h"
  11. #import <SCFoundation/NSString+SCFormat.h>
  12. @import MetalPerformanceShaders;
  13. @implementation SCDepthToGrayscaleMetalRenderCommand
  14. typedef struct DepthToGrayscaleRenderData {
  15. float depthRange;
  16. float depthOffset;
  17. } DepthToGrayscaleRenderData;
  18. #pragma mark - SCMetalRenderCommand
  19. - (id<MTLComputeCommandEncoder>)encodeMetalCommand:(id<MTLCommandBuffer>)commandBuffer
  20. pipelineState:(id<MTLComputePipelineState>)pipelineState
  21. textureResource:(SCMetalTextureResource *)textureResource
  22. {
  23. #if !TARGET_IPHONE_SIMULATOR
  24. DepthToGrayscaleRenderData depthToGrayscaleRenderData = {
  25. .depthRange = textureResource.depthRange, .depthOffset = textureResource.depthOffset,
  26. };
  27. id<MTLBuffer> depthToGrayscaleDataBuffer =
  28. [textureResource.device newBufferWithLength:sizeof(DepthToGrayscaleRenderData)
  29. options:MTLResourceOptionCPUCacheModeDefault];
  30. memcpy(depthToGrayscaleDataBuffer.contents, &depthToGrayscaleRenderData, sizeof(DepthToGrayscaleRenderData));
  31. id<MTLComputeCommandEncoder> commandEncoder = [commandBuffer computeCommandEncoder];
  32. [commandEncoder setComputePipelineState:pipelineState];
  33. [commandEncoder setTexture:textureResource.sourceDepthTexture atIndex:0];
  34. [commandEncoder setTexture:textureResource.destinationYTexture atIndex:1];
  35. [commandEncoder setTexture:textureResource.destinationUVTexture atIndex:2];
  36. [commandEncoder setBuffer:depthToGrayscaleDataBuffer offset:0 atIndex:0];
  37. return commandEncoder;
  38. #else
  39. return nil;
  40. #endif
  41. }
  42. - (BOOL)requiresDepthData
  43. {
  44. return YES;
  45. }
  46. #pragma mark - SCMetalModuleFunctionProvider
  47. - (NSString *)functionName
  48. {
  49. return @"kernel_depth_to_grayscale";
  50. }
  51. - (NSString *)description
  52. {
  53. return [NSString
  54. sc_stringWithFormat:@"SCDepthToGrayscaleMetalRenderCommand (shader function = %@)", self.functionName];
  55. }
  56. @end