GrMtlPipelineState.mm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "src/gpu/mtl/GrMtlPipelineState.h"
  8. #include "include/gpu/GrContext.h"
  9. #include "include/gpu/GrRenderTarget.h"
  10. #include "src/gpu/GrContextPriv.h"
  11. #include "src/gpu/GrPipeline.h"
  12. #include "src/gpu/GrRenderTargetPriv.h"
  13. #include "src/gpu/GrTexturePriv.h"
  14. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  15. #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
  16. #include "src/gpu/glsl/GrGLSLXferProcessor.h"
  17. #include "src/gpu/mtl/GrMtlBuffer.h"
  18. #include "src/gpu/mtl/GrMtlGpu.h"
  19. #include "src/gpu/mtl/GrMtlTexture.h"
  20. #if !__has_feature(objc_arc)
  21. #error This file must be compiled with Arc. Use -fobjc-arc flag
  22. #endif
  23. GrMtlPipelineState::SamplerBindings::SamplerBindings(const GrSamplerState& state,
  24. GrTexture* texture,
  25. GrMtlGpu* gpu)
  26. : fTexture(static_cast<GrMtlTexture*>(texture)->mtlTexture()) {
  27. uint32_t maxMipMapLevel = texture->texturePriv().maxMipMapLevel();
  28. fSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(state, maxMipMapLevel);
  29. }
  30. GrMtlPipelineState::GrMtlPipelineState(
  31. GrMtlGpu* gpu,
  32. id<MTLRenderPipelineState> pipelineState,
  33. MTLPixelFormat pixelFormat,
  34. const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
  35. const UniformInfoArray& uniforms,
  36. uint32_t geometryUniformBufferSize,
  37. uint32_t fragmentUniformBufferSize,
  38. uint32_t numSamplers,
  39. std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
  40. std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
  41. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
  42. int fragmentProcessorCnt)
  43. : fGpu(gpu)
  44. , fPipelineState(pipelineState)
  45. , fPixelFormat(pixelFormat)
  46. , fBuiltinUniformHandles(builtinUniformHandles)
  47. , fNumSamplers(numSamplers)
  48. , fGeometryProcessor(std::move(geometryProcessor))
  49. , fXferProcessor(std::move(xferProcessor))
  50. , fFragmentProcessors(std::move(fragmentProcessors))
  51. , fFragmentProcessorCnt(fragmentProcessorCnt)
  52. , fDataManager(uniforms, geometryUniformBufferSize, fragmentUniformBufferSize) {
  53. (void) fPixelFormat; // Suppress unused-var warning.
  54. }
  55. void GrMtlPipelineState::setData(const GrRenderTarget* renderTarget,
  56. GrSurfaceOrigin origin,
  57. const GrPrimitiveProcessor& primProc,
  58. const GrPipeline& pipeline,
  59. const GrTextureProxy* const primProcTextures[]) {
  60. SkASSERT(primProcTextures || !primProc.numTextureSamplers());
  61. this->setRenderTargetState(renderTarget, origin);
  62. fGeometryProcessor->setData(fDataManager, primProc,
  63. GrFragmentProcessor::CoordTransformIter(pipeline));
  64. fSamplerBindings.reset();
  65. for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
  66. const auto& sampler = primProc.textureSampler(i);
  67. auto texture = static_cast<GrMtlTexture*>(primProcTextures[i]->peekTexture());
  68. fSamplerBindings.emplace_back(sampler.samplerState(), texture, fGpu);
  69. }
  70. GrFragmentProcessor::Iter iter(pipeline);
  71. GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
  72. const GrFragmentProcessor* fp = iter.next();
  73. GrGLSLFragmentProcessor* glslFP = glslIter.next();
  74. while (fp && glslFP) {
  75. glslFP->setData(fDataManager, *fp);
  76. for (int i = 0; i < fp->numTextureSamplers(); ++i) {
  77. const auto& sampler = fp->textureSampler(i);
  78. fSamplerBindings.emplace_back(sampler.samplerState(), sampler.peekTexture(), fGpu);
  79. }
  80. fp = iter.next();
  81. glslFP = glslIter.next();
  82. }
  83. SkASSERT(!fp && !glslFP);
  84. {
  85. SkIPoint offset;
  86. GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
  87. fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
  88. }
  89. if (GrTextureProxy* dstTextureProxy = pipeline.dstTextureProxy()) {
  90. fSamplerBindings.emplace_back(GrSamplerState::ClampNearest(),
  91. dstTextureProxy->peekTexture(),
  92. fGpu);
  93. }
  94. SkASSERT(fNumSamplers == fSamplerBindings.count());
  95. fDataManager.resetDirtyBits();
  96. if (pipeline.isStencilEnabled()) {
  97. SkASSERT(renderTarget->renderTargetPriv().getStencilAttachment());
  98. fStencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
  99. renderTarget->renderTargetPriv().numStencilBits());
  100. }
  101. }
  102. void GrMtlPipelineState::setDrawState(id<MTLRenderCommandEncoder> renderCmdEncoder,
  103. const GrSwizzle& outputSwizzle,
  104. const GrXferProcessor& xferProcessor) {
  105. [renderCmdEncoder pushDebugGroup:@"setDrawState"];
  106. this->bind(renderCmdEncoder);
  107. this->setBlendConstants(renderCmdEncoder, outputSwizzle, xferProcessor);
  108. this->setDepthStencilState(renderCmdEncoder);
  109. [renderCmdEncoder popDebugGroup];
  110. }
  111. void GrMtlPipelineState::bind(id<MTLRenderCommandEncoder> renderCmdEncoder) {
  112. fDataManager.uploadAndBindUniformBuffers(fGpu, renderCmdEncoder);
  113. SkASSERT(fNumSamplers == fSamplerBindings.count());
  114. for (int index = 0; index < fNumSamplers; ++index) {
  115. [renderCmdEncoder setFragmentTexture: fSamplerBindings[index].fTexture
  116. atIndex: index];
  117. [renderCmdEncoder setFragmentSamplerState: fSamplerBindings[index].fSampler->mtlSampler()
  118. atIndex: index];
  119. }
  120. }
  121. void GrMtlPipelineState::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
  122. // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
  123. if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
  124. fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
  125. fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
  126. }
  127. // set RT adjustment
  128. SkISize size;
  129. size.set(rt->width(), rt->height());
  130. SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
  131. if (fRenderTargetState.fRenderTargetOrigin != origin ||
  132. fRenderTargetState.fRenderTargetSize != size) {
  133. fRenderTargetState.fRenderTargetSize = size;
  134. fRenderTargetState.fRenderTargetOrigin = origin;
  135. float rtAdjustmentVec[4];
  136. fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
  137. fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
  138. }
  139. }
  140. static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
  141. switch (coeff) {
  142. case kConstC_GrBlendCoeff:
  143. case kIConstC_GrBlendCoeff:
  144. case kConstA_GrBlendCoeff:
  145. case kIConstA_GrBlendCoeff:
  146. return true;
  147. default:
  148. return false;
  149. }
  150. }
  151. void GrMtlPipelineState::setBlendConstants(id<MTLRenderCommandEncoder> renderCmdEncoder,
  152. const GrSwizzle& swizzle,
  153. const GrXferProcessor& xferProcessor) {
  154. if (!renderCmdEncoder) {
  155. return;
  156. }
  157. const GrXferProcessor::BlendInfo& blendInfo = xferProcessor.getBlendInfo();
  158. GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
  159. GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
  160. if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
  161. // Swizzle the blend to match what the shader will output.
  162. SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
  163. [renderCmdEncoder setBlendColorRed: blendConst.fR
  164. green: blendConst.fG
  165. blue: blendConst.fB
  166. alpha: blendConst.fA];
  167. }
  168. }
  169. void GrMtlPipelineState::setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder) {
  170. const GrSurfaceOrigin& origin = fRenderTargetState.fRenderTargetOrigin;
  171. GrMtlDepthStencil* state =
  172. fGpu->resourceProvider().findOrCreateCompatibleDepthStencilState(fStencil, origin);
  173. if (!fStencil.isDisabled()) {
  174. if (fStencil.isTwoSided()) {
  175. [renderCmdEncoder setStencilFrontReferenceValue:fStencil.front(origin).fRef
  176. backReferenceValue:fStencil.back(origin).fRef];
  177. }
  178. else {
  179. [renderCmdEncoder setStencilReferenceValue:fStencil.frontAndBack().fRef];
  180. }
  181. }
  182. [renderCmdEncoder setDepthStencilState:state->mtlDepthStencil()];
  183. }
  184. void GrMtlPipelineState::SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder,
  185. const GrRenderTarget* renderTarget,
  186. GrSurfaceOrigin rtOrigin,
  187. SkIRect scissorRect) {
  188. if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
  189. scissorRect.setEmpty();
  190. }
  191. MTLScissorRect scissor;
  192. scissor.x = scissorRect.fLeft;
  193. scissor.width = scissorRect.width();
  194. if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
  195. scissor.y = scissorRect.fTop;
  196. } else {
  197. SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
  198. scissor.y = renderTarget->height() - scissorRect.fBottom;
  199. }
  200. scissor.height = scissorRect.height();
  201. SkASSERT(scissor.x >= 0);
  202. SkASSERT(scissor.y >= 0);
  203. [renderCmdEncoder setScissorRect: scissor];
  204. }
  205. bool GrMtlPipelineState::doesntSampleAttachment(
  206. const MTLRenderPassAttachmentDescriptor* attachment) const {
  207. for (int i = 0; i < fSamplerBindings.count(); ++i) {
  208. if (attachment.texture == fSamplerBindings[i].fTexture) {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }