GrMtlPipelineState.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrMtlPipelineState_DEFINED
  8. #define GrMtlPipelineState_DEFINED
  9. #include "include/private/GrTypesPriv.h"
  10. #include "src/gpu/GrStencilSettings.h"
  11. #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
  12. #include "src/gpu/mtl/GrMtlBuffer.h"
  13. #include "src/gpu/mtl/GrMtlPipelineStateDataManager.h"
  14. #import <Metal/Metal.h>
  15. class GrMtlGpu;
  16. class GrMtlPipelineStateDataManager;
  17. class GrMtlSampler;
  18. class GrMtlTexture;
  19. class GrPipeline;
  20. /**
  21. * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed
  22. * by Ganesh
  23. */
  24. class GrMtlPipelineState {
  25. public:
  26. using UniformInfoArray = GrMtlPipelineStateDataManager::UniformInfoArray;
  27. using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
  28. GrMtlPipelineState(
  29. GrMtlGpu* gpu,
  30. id<MTLRenderPipelineState> pipelineState,
  31. MTLPixelFormat pixelFormat,
  32. const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
  33. const UniformInfoArray& uniforms,
  34. uint32_t geometryUniformBufferSize,
  35. uint32_t fragmentUniformBufferSize,
  36. uint32_t numSamplers,
  37. std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
  38. std::unique_ptr<GrGLSLXferProcessor> xferPRocessor,
  39. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
  40. int fFragmentProcessorCnt);
  41. id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; }
  42. void setData(const GrRenderTarget*, GrSurfaceOrigin,
  43. const GrPrimitiveProcessor& primPRoc, const GrPipeline& pipeline,
  44. const GrTextureProxy* const primProcTextures[]);
  45. void setDrawState(id<MTLRenderCommandEncoder>, const GrSwizzle& outputSwizzle,
  46. const GrXferProcessor&);
  47. static void SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder,
  48. const GrRenderTarget* renderTarget,
  49. GrSurfaceOrigin rtOrigin,
  50. SkIRect scissorRect);
  51. bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const;
  52. private:
  53. /**
  54. * We use the RT's size and origin to adjust from Skia device space to Metal normalized device
  55. * space and to make device space positions have the correct origin for processors that require
  56. * them.
  57. */
  58. struct RenderTargetState {
  59. SkISize fRenderTargetSize;
  60. GrSurfaceOrigin fRenderTargetOrigin;
  61. RenderTargetState() { this->invalidate(); }
  62. void invalidate() {
  63. fRenderTargetSize.fWidth = -1;
  64. fRenderTargetSize.fHeight = -1;
  65. fRenderTargetOrigin = (GrSurfaceOrigin)-1;
  66. }
  67. /**
  68. * Gets a float4 that adjusts the position from Skia device coords to Metals normalized
  69. * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec,
  70. * v, is applied as such:
  71. * pos.x = dot(v.xy, pos.xz)
  72. * pos.y = dot(v.zw, pos.yz)
  73. */
  74. void getRTAdjustmentVec(float* destVec) {
  75. destVec[0] = 2.f / fRenderTargetSize.fWidth;
  76. destVec[1] = -1.f;
  77. if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
  78. destVec[2] = -2.f / fRenderTargetSize.fHeight;
  79. destVec[3] = 1.f;
  80. } else {
  81. destVec[2] = 2.f / fRenderTargetSize.fHeight;
  82. destVec[3] = -1.f;
  83. }
  84. }
  85. };
  86. void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
  87. void bind(id<MTLRenderCommandEncoder>);
  88. void setBlendConstants(id<MTLRenderCommandEncoder>, const GrSwizzle&, const GrXferProcessor&);
  89. void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder);
  90. struct SamplerBindings {
  91. GrMtlSampler* fSampler;
  92. id<MTLTexture> fTexture;
  93. SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrMtlGpu*);
  94. };
  95. GrMtlGpu* fGpu;
  96. id<MTLRenderPipelineState> fPipelineState;
  97. MTLPixelFormat fPixelFormat;
  98. RenderTargetState fRenderTargetState;
  99. GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
  100. GrStencilSettings fStencil;
  101. int fNumSamplers;
  102. SkTArray<SamplerBindings> fSamplerBindings;
  103. std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
  104. std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
  105. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
  106. int fFragmentProcessorCnt;
  107. GrMtlPipelineStateDataManager fDataManager;
  108. };
  109. #endif